]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/df-scan.c
re PR c/65345 (ICE with _Generic selection on _Atomic int)
[thirdparty/gcc.git] / gcc / df-scan.c
CommitLineData
4d779342 1/* Scanning of rtl for dataflow analysis.
5624e564 2 Copyright (C) 1999-2015 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
AM
27#include "backend.h"
28#include "tree.h"
4d779342 29#include "rtl.h"
c7131fb2 30#include "df.h"
4d779342
DB
31#include "tm_p.h"
32#include "insn-config.h"
33#include "recog.h"
40e23961 34#include "alias.h"
4d779342 35#include "regs.h"
4d779342
DB
36#include "alloc-pool.h"
37#include "flags.h"
7ee2468b 38#include "dumpfile.h"
912f2dac 39#include "target.h"
5936d944 40#include "emit-rtl.h" /* FIXME: Can go away once crtl is moved to rtl.h. */
4d779342 41
c2569604
ILT
42
43typedef struct df_mw_hardreg *df_mw_hardreg_ptr;
44
c2569604 45
4d779342
DB
46/* The set of hard registers in eliminables[i].from. */
47
48static HARD_REG_SET elim_reg_set;
49
4d779342
DB
50/* Initialize ur_in and ur_out as if all hard registers were partially
51 available. */
52
6fb5fa3c
DB
53struct df_collection_rec
54{
00f96dc9
TS
55 auto_vec<df_ref, 128> def_vec;
56 auto_vec<df_ref, 32> use_vec;
57 auto_vec<df_ref, 32> eq_use_vec;
58 auto_vec<df_mw_hardreg_ptr, 32> mw_vec;
6fb5fa3c
DB
59};
60
57512f53 61static void df_ref_record (enum df_ref_class, struct df_collection_rec *,
b8698a0f 62 rtx, rtx *,
50e94c7e 63 basic_block, struct df_insn_info *,
502ef838 64 enum df_ref_type, int ref_flags);
c5ebdc25 65static void df_def_record_1 (struct df_collection_rec *, rtx *,
50e94c7e 66 basic_block, struct df_insn_info *,
bbbbb16a 67 int ref_flags);
50e94c7e
SB
68static void df_defs_record (struct df_collection_rec *, rtx,
69 basic_block, struct df_insn_info *,
bbbbb16a 70 int ref_flags);
502ef838 71static void df_uses_record (struct df_collection_rec *,
6fb5fa3c 72 rtx *, enum df_ref_type,
50e94c7e 73 basic_block, struct df_insn_info *,
502ef838 74 int ref_flags);
4d779342 75
dc007c1f 76static void df_install_ref_incremental (df_ref);
b8698a0f
L
77static void df_insn_refs_collect (struct df_collection_rec*,
78 basic_block, struct df_insn_info *);
6fb5fa3c
DB
79static void df_canonize_collection_rec (struct df_collection_rec *);
80
81static void df_get_regular_block_artificial_uses (bitmap);
82static void df_get_eh_block_artificial_uses (bitmap);
83
84static void df_record_entry_block_defs (bitmap);
85static void df_record_exit_block_uses (bitmap);
86static void df_get_exit_block_use_set (bitmap);
87static void df_get_entry_block_def_set (bitmap);
4d779342 88static void df_grow_ref_info (struct df_ref_info *, unsigned int);
b512946c
RS
89static void df_ref_chain_delete_du_chain (df_ref);
90static void df_ref_chain_delete (df_ref);
4d779342 91
b8698a0f 92static void df_refs_add_to_chains (struct df_collection_rec *,
b2908ba6 93 basic_block, rtx_insn *, unsigned int);
6fb5fa3c 94
b2908ba6
DM
95static bool df_insn_refs_verify (struct df_collection_rec *, basic_block,
96 rtx_insn *, bool);
6fb5fa3c
DB
97static void df_entry_block_defs_collect (struct df_collection_rec *, bitmap);
98static void df_exit_block_uses_collect (struct df_collection_rec *, bitmap);
b8698a0f 99static void df_install_ref (df_ref, struct df_reg_info *,
6fb5fa3c
DB
100 struct df_ref_info *, bool);
101
b512946c
RS
102static int df_ref_compare (df_ref, df_ref);
103static int df_ref_ptr_compare (const void *, const void *);
104static int df_mw_compare (const df_mw_hardreg *, const df_mw_hardreg *);
105static int df_mw_ptr_compare (const void *, const void *);
6fb5fa3c 106
80eb8028
SB
107static void df_insn_info_delete (unsigned int);
108
6fb5fa3c
DB
109/* Indexed by hardware reg number, is true if that register is ever
110 used in the current function.
111
112 In df-scan.c, this is set up to record the hard regs used
113 explicitly. Reload adds in the hard regs used for holding pseudo
114 regs. Final uses it to generate the code in the function prologue
115 and epilogue to save and restore registers as needed. */
116
117static bool regs_ever_live[FIRST_PSEUDO_REGISTER];
ff4c81cc
TS
118
119/* Flags used to tell df_refs_add_to_chains() which vectors it should copy. */
120static const unsigned int copy_defs = 0x1;
121static const unsigned int copy_uses = 0x2;
122static const unsigned int copy_eq_uses = 0x4;
123static const unsigned int copy_mw = 0x8;
124static const unsigned int copy_all = copy_defs | copy_uses | copy_eq_uses
125| copy_mw;
4d779342
DB
126\f
127/*----------------------------------------------------------------------------
128 SCANNING DATAFLOW PROBLEM
129
130 There are several ways in which scanning looks just like the other
131 dataflow problems. It shares the all the mechanisms for local info
132 as well as basic block info. Where it differs is when and how often
133 it gets run. It also has no need for the iterative solver.
134----------------------------------------------------------------------------*/
135
136/* Problem data for the scanning dataflow function. */
137struct df_scan_problem_data
138{
fb0b2914
ML
139 object_allocator<df_base_ref> *ref_base_pool;
140 object_allocator<df_artificial_ref> *ref_artificial_pool;
141 object_allocator<df_regular_ref> *ref_regular_pool;
142 object_allocator<df_insn_info> *insn_pool;
143 object_allocator<df_reg_info> *reg_pool;
144 object_allocator<df_mw_hardreg> *mw_reg_pool;
e956943e 145
6fb5fa3c
DB
146 bitmap_obstack reg_bitmaps;
147 bitmap_obstack insn_bitmaps;
4d779342
DB
148};
149
150typedef struct df_scan_bb_info *df_scan_bb_info_t;
151
370f38e8
KZ
152
153/* Internal function to shut down the scanning problem. */
b8698a0f 154static void
6fb5fa3c 155df_scan_free_internal (void)
4d779342 156{
23249ac4 157 struct df_scan_problem_data *problem_data
6fb5fa3c 158 = (struct df_scan_problem_data *) df_scan->problem_data;
4d779342 159
4d779342 160 free (df->def_info.refs);
6fb5fa3c
DB
161 free (df->def_info.begin);
162 free (df->def_info.count);
4d779342
DB
163 memset (&df->def_info, 0, (sizeof (struct df_ref_info)));
164
4d779342 165 free (df->use_info.refs);
6fb5fa3c
DB
166 free (df->use_info.begin);
167 free (df->use_info.count);
4d779342
DB
168 memset (&df->use_info, 0, (sizeof (struct df_ref_info)));
169
6fb5fa3c
DB
170 free (df->def_regs);
171 df->def_regs = NULL;
172 free (df->use_regs);
173 df->use_regs = NULL;
174 free (df->eq_use_regs);
175 df->eq_use_regs = NULL;
176 df->regs_size = 0;
c3284718 177 DF_REG_SIZE (df) = 0;
6fb5fa3c 178
4d779342
DB
179 free (df->insns);
180 df->insns = NULL;
6fb5fa3c 181 DF_INSN_SIZE () = 0;
4d779342 182
6fb5fa3c
DB
183 free (df_scan->block_info);
184 df_scan->block_info = NULL;
185 df_scan->block_info_size = 0;
4d779342 186
a7e3698d
JH
187 bitmap_clear (&df->hardware_regs_used);
188 bitmap_clear (&df->regular_block_artificial_uses);
189 bitmap_clear (&df->eh_block_artificial_uses);
912f2dac 190 BITMAP_FREE (df->entry_block_defs);
4d779342 191 BITMAP_FREE (df->exit_block_uses);
a7e3698d
JH
192 bitmap_clear (&df->insns_to_delete);
193 bitmap_clear (&df->insns_to_rescan);
194 bitmap_clear (&df->insns_to_notes_rescan);
4d779342 195
e956943e
ML
196 delete problem_data->ref_base_pool;
197 delete problem_data->ref_artificial_pool;
198 delete problem_data->ref_regular_pool;
199 delete problem_data->insn_pool;
200 delete problem_data->reg_pool;
201 delete problem_data->mw_reg_pool;
6fb5fa3c
DB
202 bitmap_obstack_release (&problem_data->reg_bitmaps);
203 bitmap_obstack_release (&problem_data->insn_bitmaps);
204 free (df_scan->problem_data);
4d779342
DB
205}
206
207
4d779342
DB
208/* Free basic block info. */
209
210static void
6fb5fa3c 211df_scan_free_bb_info (basic_block bb, void *vbb_info)
4d779342
DB
212{
213 struct df_scan_bb_info *bb_info = (struct df_scan_bb_info *) vbb_info;
6fb5fa3c 214 unsigned int bb_index = bb->index;
dd3eed93 215 rtx_insn *insn;
e285df08 216
b512946c
RS
217 FOR_BB_INSNS (bb, insn)
218 if (INSN_P (insn))
219 df_insn_info_delete (INSN_UID (insn));
220
221 if (bb_index < df_scan->block_info_size)
222 bb_info = df_scan_get_bb_info (bb_index);
223
224 /* Get rid of any artificial uses or defs. */
225 df_ref_chain_delete_du_chain (bb_info->artificial_defs);
226 df_ref_chain_delete_du_chain (bb_info->artificial_uses);
227 df_ref_chain_delete (bb_info->artificial_defs);
228 df_ref_chain_delete (bb_info->artificial_uses);
229 bb_info->artificial_defs = NULL;
230 bb_info->artificial_uses = NULL;
4d779342
DB
231}
232
233
234/* Allocate the problem data for the scanning problem. This should be
235 called when the problem is created or when the entire function is to
236 be rescanned. */
b8698a0f 237void
6fb5fa3c 238df_scan_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
4d779342 239{
4d779342
DB
240 struct df_scan_problem_data *problem_data;
241 unsigned int insn_num = get_max_uid () + 1;
6fb5fa3c 242 basic_block bb;
4d779342
DB
243
244 /* Given the number of pools, this is really faster than tearing
245 everything apart. */
6fb5fa3c
DB
246 if (df_scan->problem_data)
247 df_scan_free_internal ();
4d779342 248
5ed6ace5 249 problem_data = XNEW (struct df_scan_problem_data);
6fb5fa3c
DB
250 df_scan->problem_data = problem_data;
251 df_scan->computed = true;
4d779342 252
fb0b2914 253 problem_data->ref_base_pool = new object_allocator<df_base_ref>
fcb87c50 254 ("df_scan ref base");
fb0b2914 255 problem_data->ref_artificial_pool = new object_allocator<df_artificial_ref>
fcb87c50 256 ("df_scan ref artificial");
fb0b2914 257 problem_data->ref_regular_pool = new object_allocator<df_regular_ref>
fcb87c50 258 ("df_scan ref regular");
fb0b2914 259 problem_data->insn_pool = new object_allocator<df_insn_info>
fcb87c50 260 ("df_scan insn");
fb0b2914 261 problem_data->reg_pool = new object_allocator<df_reg_info>
fcb87c50 262 ("df_scan reg");
fb0b2914 263 problem_data->mw_reg_pool = new object_allocator<df_mw_hardreg>
fcb87c50 264 ("df_scan mw_reg");
4d779342 265
6fb5fa3c
DB
266 bitmap_obstack_initialize (&problem_data->reg_bitmaps);
267 bitmap_obstack_initialize (&problem_data->insn_bitmaps);
4d779342 268
b8698a0f 269 insn_num += insn_num / 4;
6fb5fa3c 270 df_grow_reg_info ();
4d779342 271
6fb5fa3c
DB
272 df_grow_insn_info ();
273 df_grow_bb_info (df_scan);
4d779342 274
04a90bec 275 FOR_ALL_BB_FN (bb, cfun)
4d779342 276 {
6fb5fa3c
DB
277 unsigned int bb_index = bb->index;
278 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (bb_index);
4d779342
DB
279 bb_info->artificial_defs = NULL;
280 bb_info->artificial_uses = NULL;
281 }
282
a7e3698d
JH
283 bitmap_initialize (&df->hardware_regs_used, &problem_data->reg_bitmaps);
284 bitmap_initialize (&df->regular_block_artificial_uses, &problem_data->reg_bitmaps);
285 bitmap_initialize (&df->eh_block_artificial_uses, &problem_data->reg_bitmaps);
6fb5fa3c
DB
286 df->entry_block_defs = BITMAP_ALLOC (&problem_data->reg_bitmaps);
287 df->exit_block_uses = BITMAP_ALLOC (&problem_data->reg_bitmaps);
a7e3698d
JH
288 bitmap_initialize (&df->insns_to_delete, &problem_data->insn_bitmaps);
289 bitmap_initialize (&df->insns_to_rescan, &problem_data->insn_bitmaps);
290 bitmap_initialize (&df->insns_to_notes_rescan, &problem_data->insn_bitmaps);
89a95777 291 df_scan->optional_p = false;
4d779342
DB
292}
293
294
295/* Free all of the data associated with the scan problem. */
296
b8698a0f 297static void
6fb5fa3c 298df_scan_free (void)
4d779342 299{
6fb5fa3c
DB
300 if (df_scan->problem_data)
301 df_scan_free_internal ();
3b8266e2 302
4d779342 303 if (df->blocks_to_analyze)
6fb5fa3c
DB
304 {
305 BITMAP_FREE (df->blocks_to_analyze);
306 df->blocks_to_analyze = NULL;
307 }
4d779342 308
6fb5fa3c 309 free (df_scan);
4d779342
DB
310}
311
6fb5fa3c 312/* Dump the preamble for DF_SCAN dump. */
b8698a0f 313static void
6fb5fa3c 314df_scan_start_dump (FILE *file ATTRIBUTE_UNUSED)
4d779342 315{
4d779342 316 int i;
57512f53
KZ
317 int dcount = 0;
318 int ucount = 0;
319 int ecount = 0;
320 int icount = 0;
321 int ccount = 0;
322 basic_block bb;
dd3eed93 323 rtx_insn *insn;
4d779342 324
6fb5fa3c 325 fprintf (file, ";; invalidated by call \t");
f2ecb626 326 df_print_regset (file, regs_invalidated_by_call_regset);
6fb5fa3c 327 fprintf (file, ";; hardware regs used \t");
a7e3698d 328 df_print_regset (file, &df->hardware_regs_used);
6fb5fa3c 329 fprintf (file, ";; regular block artificial uses \t");
a7e3698d 330 df_print_regset (file, &df->regular_block_artificial_uses);
6fb5fa3c 331 fprintf (file, ";; eh block artificial uses \t");
a7e3698d 332 df_print_regset (file, &df->eh_block_artificial_uses);
6fb5fa3c
DB
333 fprintf (file, ";; entry block defs \t");
334 df_print_regset (file, df->entry_block_defs);
335 fprintf (file, ";; exit block uses \t");
336 df_print_regset (file, df->exit_block_uses);
337 fprintf (file, ";; regs ever live \t");
4d779342 338 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
6fb5fa3c 339 if (df_regs_ever_live_p (i))
b4147b63 340 fprintf (file, " %d [%s]", i, reg_names[i]);
57512f53 341 fprintf (file, "\n;; ref usage \t");
b8698a0f 342
57512f53
KZ
343 for (i = 0; i < (int)df->regs_inited; i++)
344 if (DF_REG_DEF_COUNT (i) || DF_REG_USE_COUNT (i) || DF_REG_EQ_USE_COUNT (i))
345 {
346 const char * sep = "";
347
348 fprintf (file, "r%d={", i);
349 if (DF_REG_DEF_COUNT (i))
350 {
351 fprintf (file, "%dd", DF_REG_DEF_COUNT (i));
352 sep = ",";
353 dcount += DF_REG_DEF_COUNT (i);
354 }
355 if (DF_REG_USE_COUNT (i))
356 {
357 fprintf (file, "%s%du", sep, DF_REG_USE_COUNT (i));
358 sep = ",";
359 ucount += DF_REG_USE_COUNT (i);
360 }
361 if (DF_REG_EQ_USE_COUNT (i))
362 {
885c9b5d 363 fprintf (file, "%s%de", sep, DF_REG_EQ_USE_COUNT (i));
57512f53
KZ
364 ecount += DF_REG_EQ_USE_COUNT (i);
365 }
366 fprintf (file, "} ");
367 }
6fb5fa3c 368
11cd3bed 369 FOR_EACH_BB_FN (bb, cfun)
57512f53
KZ
370 FOR_BB_INSNS (bb, insn)
371 if (INSN_P (insn))
372 {
373 if (CALL_P (insn))
374 ccount++;
375 else
376 icount++;
377 }
378
885c9b5d
EB
379 fprintf (file, "\n;; total ref usage %d{%dd,%du,%de}"
380 " in %d{%d regular + %d call} insns.\n",
381 dcount + ucount + ecount, dcount, ucount, ecount,
382 icount + ccount, icount, ccount);
4d779342
DB
383}
384
6fb5fa3c 385/* Dump the bb_info for a given basic block. */
b8698a0f 386static void
6fb5fa3c
DB
387df_scan_start_block (basic_block bb, FILE *file)
388{
389 struct df_scan_bb_info *bb_info
390 = df_scan_get_bb_info (bb->index);
391
392 if (bb_info)
393 {
394 fprintf (file, ";; bb %d artificial_defs: ", bb->index);
395 df_refs_chain_dump (bb_info->artificial_defs, true, file);
396 fprintf (file, "\n;; bb %d artificial_uses: ", bb->index);
397 df_refs_chain_dump (bb_info->artificial_uses, true, file);
398 fprintf (file, "\n");
399 }
400#if 0
401 {
dd3eed93 402 rtx_insn *insn;
6fb5fa3c
DB
403 FOR_BB_INSNS (bb, insn)
404 if (INSN_P (insn))
405 df_insn_debug (insn, false, file);
406 }
407#endif
408}
409
4d779342
DB
410static struct df_problem problem_SCAN =
411{
412 DF_SCAN, /* Problem id. */
413 DF_NONE, /* Direction. */
414 df_scan_alloc, /* Allocate the problem specific data. */
30cb87a0 415 NULL, /* Reset global information. */
4d779342
DB
416 df_scan_free_bb_info, /* Free basic block info. */
417 NULL, /* Local compute function. */
418 NULL, /* Init the solution specific data. */
419 NULL, /* Iterative solver. */
b8698a0f
L
420 NULL, /* Confluence operator 0. */
421 NULL, /* Confluence operator n. */
4d779342
DB
422 NULL, /* Transfer function. */
423 NULL, /* Finalize function. */
424 df_scan_free, /* Free all of the problem information. */
6fb5fa3c
DB
425 NULL, /* Remove this problem from the stack of dataflow problems. */
426 df_scan_start_dump, /* Debugging. */
427 df_scan_start_block, /* Debugging start block. */
428 NULL, /* Debugging end block. */
7b19209f
SB
429 NULL, /* Debugging start insn. */
430 NULL, /* Debugging end insn. */
6fb5fa3c 431 NULL, /* Incremental solution verify start. */
6ed3da00 432 NULL, /* Incremental solution verify end. */
23249ac4 433 NULL, /* Dependent problem. */
e285df08 434 sizeof (struct df_scan_bb_info),/* Size of entry of block_info array. */
89a95777
KZ
435 TV_DF_SCAN, /* Timing variable. */
436 false /* Reset blocks on dropping out of blocks_to_analyze. */
4d779342
DB
437};
438
439
440/* Create a new DATAFLOW instance and add it to an existing instance
441 of DF. The returned structure is what is used to get at the
442 solution. */
443
6fb5fa3c
DB
444void
445df_scan_add_problem (void)
4d779342 446{
6fb5fa3c 447 df_add_problem (&problem_SCAN);
4d779342
DB
448}
449
6fb5fa3c 450\f
4d779342
DB
451/*----------------------------------------------------------------------------
452 Storage Allocation Utilities
453----------------------------------------------------------------------------*/
454
455
456/* First, grow the reg_info information. If the current size is less than
fa10beec 457 the number of pseudos, grow to 25% more than the number of
b8698a0f 458 pseudos.
4d779342
DB
459
460 Second, assure that all of the slots up to max_reg_num have been
461 filled with reg_info structures. */
462
b8698a0f 463void
6fb5fa3c 464df_grow_reg_info (void)
4d779342
DB
465{
466 unsigned int max_reg = max_reg_num ();
467 unsigned int new_size = max_reg;
23249ac4 468 struct df_scan_problem_data *problem_data
6fb5fa3c 469 = (struct df_scan_problem_data *) df_scan->problem_data;
4d779342
DB
470 unsigned int i;
471
6fb5fa3c 472 if (df->regs_size < new_size)
4d779342
DB
473 {
474 new_size += new_size / 4;
f883e0a7
KG
475 df->def_regs = XRESIZEVEC (struct df_reg_info *, df->def_regs, new_size);
476 df->use_regs = XRESIZEVEC (struct df_reg_info *, df->use_regs, new_size);
477 df->eq_use_regs = XRESIZEVEC (struct df_reg_info *, df->eq_use_regs,
478 new_size);
479 df->def_info.begin = XRESIZEVEC (unsigned, df->def_info.begin, new_size);
480 df->def_info.count = XRESIZEVEC (unsigned, df->def_info.count, new_size);
481 df->use_info.begin = XRESIZEVEC (unsigned, df->use_info.begin, new_size);
482 df->use_info.count = XRESIZEVEC (unsigned, df->use_info.count, new_size);
6fb5fa3c 483 df->regs_size = new_size;
4d779342
DB
484 }
485
6fb5fa3c 486 for (i = df->regs_inited; i < max_reg; i++)
4d779342 487 {
6fb5fa3c
DB
488 struct df_reg_info *reg_info;
489
e956943e
ML
490 // TODO
491 reg_info = problem_data->reg_pool->allocate ();
6fb5fa3c
DB
492 memset (reg_info, 0, sizeof (struct df_reg_info));
493 df->def_regs[i] = reg_info;
e956943e 494 reg_info = problem_data->reg_pool->allocate ();
6fb5fa3c
DB
495 memset (reg_info, 0, sizeof (struct df_reg_info));
496 df->use_regs[i] = reg_info;
e956943e 497 reg_info = problem_data->reg_pool->allocate ();
4d779342 498 memset (reg_info, 0, sizeof (struct df_reg_info));
6fb5fa3c
DB
499 df->eq_use_regs[i] = reg_info;
500 df->def_info.begin[i] = 0;
501 df->def_info.count[i] = 0;
502 df->use_info.begin[i] = 0;
503 df->use_info.count[i] = 0;
4d779342 504 }
b8698a0f 505
6fb5fa3c 506 df->regs_inited = max_reg;
4d779342
DB
507}
508
509
510/* Grow the ref information. */
511
b8698a0f 512static void
4d779342
DB
513df_grow_ref_info (struct df_ref_info *ref_info, unsigned int new_size)
514{
515 if (ref_info->refs_size < new_size)
516 {
57512f53 517 ref_info->refs = XRESIZEVEC (df_ref, ref_info->refs, new_size);
4d779342 518 memset (ref_info->refs + ref_info->refs_size, 0,
57512f53 519 (new_size - ref_info->refs_size) *sizeof (df_ref));
4d779342
DB
520 ref_info->refs_size = new_size;
521 }
522}
523
524
6fb5fa3c
DB
525/* Check and grow the ref information if necessary. This routine
526 guarantees total_size + BITMAP_ADDEND amount of entries in refs
527 array. It updates ref_info->refs_size only and does not change
528 ref_info->total_size. */
529
530static void
b8698a0f 531df_check_and_grow_ref_info (struct df_ref_info *ref_info,
6fb5fa3c
DB
532 unsigned bitmap_addend)
533{
534 if (ref_info->refs_size < ref_info->total_size + bitmap_addend)
535 {
536 int new_size = ref_info->total_size + bitmap_addend;
537 new_size += ref_info->total_size / 4;
538 df_grow_ref_info (ref_info, new_size);
539 }
540}
541
542
4d779342
DB
543/* Grow the ref information. If the current size is less than the
544 number of instructions, grow to 25% more than the number of
545 instructions. */
546
b8698a0f 547void
6fb5fa3c 548df_grow_insn_info (void)
4d779342
DB
549{
550 unsigned int new_size = get_max_uid () + 1;
6fb5fa3c 551 if (DF_INSN_SIZE () < new_size)
4d779342
DB
552 {
553 new_size += new_size / 4;
f883e0a7 554 df->insns = XRESIZEVEC (struct df_insn_info *, df->insns, new_size);
4d779342 555 memset (df->insns + df->insns_size, 0,
6fb5fa3c
DB
556 (new_size - DF_INSN_SIZE ()) *sizeof (struct df_insn_info *));
557 DF_INSN_SIZE () = new_size;
4d779342
DB
558 }
559}
560
561
562
563\f
564/*----------------------------------------------------------------------------
565 PUBLIC INTERFACES FOR SMALL GRAIN CHANGES TO SCANNING.
566----------------------------------------------------------------------------*/
567
6fb5fa3c
DB
568/* Rescan all of the block_to_analyze or all of the blocks in the
569 function if df_set_blocks if blocks_to_analyze is NULL; */
4d779342
DB
570
571void
6fb5fa3c 572df_scan_blocks (void)
4d779342 573{
4d779342
DB
574 basic_block bb;
575
6fb5fa3c
DB
576 df->def_info.ref_order = DF_REF_ORDER_NO_TABLE;
577 df->use_info.ref_order = DF_REF_ORDER_NO_TABLE;
23249ac4 578
a7e3698d
JH
579 df_get_regular_block_artificial_uses (&df->regular_block_artificial_uses);
580 df_get_eh_block_artificial_uses (&df->eh_block_artificial_uses);
4d779342 581
a7e3698d
JH
582 bitmap_ior_into (&df->eh_block_artificial_uses,
583 &df->regular_block_artificial_uses);
30cb87a0 584
6fb5fa3c
DB
585 /* ENTRY and EXIT blocks have special defs/uses. */
586 df_get_entry_block_def_set (df->entry_block_defs);
587 df_record_entry_block_defs (df->entry_block_defs);
588 df_get_exit_block_use_set (df->exit_block_uses);
589 df_record_exit_block_uses (df->exit_block_uses);
06e28de2
DM
590 df_set_bb_dirty (BASIC_BLOCK_FOR_FN (cfun, ENTRY_BLOCK));
591 df_set_bb_dirty (BASIC_BLOCK_FOR_FN (cfun, EXIT_BLOCK));
4d779342 592
6fb5fa3c 593 /* Regular blocks */
11cd3bed 594 FOR_EACH_BB_FN (bb, cfun)
4d779342 595 {
6fb5fa3c
DB
596 unsigned int bb_index = bb->index;
597 df_bb_refs_record (bb_index, true);
4d779342 598 }
4d779342
DB
599}
600
dc007c1f
PB
601/* Create new refs under address LOC within INSN. This function is
602 only used externally. REF_FLAGS must be either 0 or DF_REF_IN_NOTE,
603 depending on whether LOC is inside PATTERN (INSN) or a note. */
604
605void
b2908ba6 606df_uses_create (rtx *loc, rtx_insn *insn, int ref_flags)
dc007c1f
PB
607{
608 gcc_assert (!(ref_flags & ~DF_REF_IN_NOTE));
609 df_uses_record (NULL, loc, DF_REF_REG_USE,
610 BLOCK_FOR_INSN (insn),
611 DF_INSN_INFO_GET (insn),
612 ref_flags);
613}
23249ac4 614
dc007c1f
PB
615static void
616df_install_ref_incremental (df_ref ref)
617{
618 struct df_reg_info **reg_info;
619 struct df_ref_info *ref_info;
b512946c 620 df_ref *ref_ptr;
dc007c1f
PB
621 bool add_to_table;
622
dd3eed93 623 rtx_insn *insn = DF_REF_INSN (ref);
dc007c1f 624 basic_block bb = BLOCK_FOR_INSN (insn);
4d779342 625
57512f53 626 if (DF_REF_REG_DEF_P (ref))
6fb5fa3c
DB
627 {
628 reg_info = df->def_regs;
629 ref_info = &df->def_info;
b512946c 630 ref_ptr = &DF_INSN_DEFS (insn);
6fb5fa3c
DB
631 add_to_table = ref_info->ref_order != DF_REF_ORDER_NO_TABLE;
632 }
633 else if (DF_REF_FLAGS (ref) & DF_REF_IN_NOTE)
634 {
635 reg_info = df->eq_use_regs;
636 ref_info = &df->use_info;
b512946c 637 ref_ptr = &DF_INSN_EQ_USES (insn);
6fb5fa3c
DB
638 switch (ref_info->ref_order)
639 {
640 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
641 case DF_REF_ORDER_BY_REG_WITH_NOTES:
642 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
643 add_to_table = true;
644 break;
645 default:
646 add_to_table = false;
647 break;
648 }
649 }
650 else
651 {
652 reg_info = df->use_regs;
653 ref_info = &df->use_info;
b512946c 654 ref_ptr = &DF_INSN_USES (insn);
6fb5fa3c
DB
655 add_to_table = ref_info->ref_order != DF_REF_ORDER_NO_TABLE;
656 }
4d779342 657
6fb5fa3c
DB
658 /* Do not add if ref is not in the right blocks. */
659 if (add_to_table && df->analyze_subset)
660 add_to_table = bitmap_bit_p (df->blocks_to_analyze, bb->index);
4d779342 661
6fb5fa3c 662 df_install_ref (ref, reg_info[DF_REF_REGNO (ref)], ref_info, add_to_table);
b8698a0f 663
6fb5fa3c
DB
664 if (add_to_table)
665 switch (ref_info->ref_order)
666 {
667 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
668 case DF_REF_ORDER_BY_REG_WITH_NOTES:
669 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
670 ref_info->ref_order = DF_REF_ORDER_UNORDERED_WITH_NOTES;
671 break;
672 default:
673 ref_info->ref_order = DF_REF_ORDER_UNORDERED;
674 break;
675 }
4d779342 676
b512946c
RS
677 while (*ref_ptr && df_ref_compare (*ref_ptr, ref) < 0)
678 ref_ptr = &DF_REF_NEXT_LOC (*ref_ptr);
4d779342 679
b512946c
RS
680 DF_REF_NEXT_LOC (ref) = *ref_ptr;
681 *ref_ptr = ref;
4d779342 682
6fb5fa3c
DB
683#if 0
684 if (dump_file)
685 {
686 fprintf (dump_file, "adding ref ");
687 df_ref_debug (ref, dump_file);
4d779342 688 }
6fb5fa3c
DB
689#endif
690 /* By adding the ref directly, df_insn_rescan my not find any
691 differences even though the block will have changed. So we need
b8698a0f 692 to mark the block dirty ourselves. */
d785e46f
AO
693 if (!DEBUG_INSN_P (DF_REF_INSN (ref)))
694 df_set_bb_dirty (bb);
4d779342
DB
695}
696
697
6fb5fa3c
DB
698\f
699/*----------------------------------------------------------------------------
700 UTILITIES TO CREATE AND DESTROY REFS AND CHAINS.
701----------------------------------------------------------------------------*/
702
ca9052ce 703static void
57512f53 704df_free_ref (df_ref ref)
ca9052ce
KZ
705{
706 struct df_scan_problem_data *problem_data
707 = (struct df_scan_problem_data *) df_scan->problem_data;
708
57512f53
KZ
709 switch (DF_REF_CLASS (ref))
710 {
711 case DF_REF_BASE:
e956943e 712 problem_data->ref_base_pool->remove ((df_base_ref *) (ref));
57512f53
KZ
713 break;
714
715 case DF_REF_ARTIFICIAL:
e956943e
ML
716 problem_data->ref_artificial_pool->remove
717 ((df_artificial_ref *) (ref));
57512f53
KZ
718 break;
719
720 case DF_REF_REGULAR:
e956943e
ML
721 problem_data->ref_regular_pool->remove
722 ((df_regular_ref *) (ref));
57512f53 723 break;
57512f53 724 }
ca9052ce
KZ
725}
726
4d779342 727
6fb5fa3c
DB
728/* Unlink and delete REF at the reg_use, reg_eq_use or reg_def chain.
729 Also delete the def-use or use-def chain if it exists. */
730
731static void
b8698a0f 732df_reg_chain_unlink (df_ref ref)
4d779342 733{
b8698a0f 734 df_ref next = DF_REF_NEXT_REG (ref);
57512f53 735 df_ref prev = DF_REF_PREV_REG (ref);
6fb5fa3c 736 int id = DF_REF_ID (ref);
4d779342 737 struct df_reg_info *reg_info;
57512f53 738 df_ref *refs = NULL;
4d779342 739
57512f53 740 if (DF_REF_REG_DEF_P (ref))
4d779342 741 {
57512f53
KZ
742 int regno = DF_REF_REGNO (ref);
743 reg_info = DF_REG_DEF_GET (regno);
6fb5fa3c 744 refs = df->def_info.refs;
4d779342 745 }
b8698a0f 746 else
4d779342 747 {
6fb5fa3c
DB
748 if (DF_REF_FLAGS (ref) & DF_REF_IN_NOTE)
749 {
750 reg_info = DF_REG_EQ_USE_GET (DF_REF_REGNO (ref));
751 switch (df->use_info.ref_order)
752 {
753 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
754 case DF_REF_ORDER_BY_REG_WITH_NOTES:
755 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
756 refs = df->use_info.refs;
757 break;
758 default:
759 break;
760 }
761 }
762 else
763 {
764 reg_info = DF_REG_USE_GET (DF_REF_REGNO (ref));
765 refs = df->use_info.refs;
766 }
767 }
768
769 if (refs)
770 {
771 if (df->analyze_subset)
772 {
57512f53 773 if (bitmap_bit_p (df->blocks_to_analyze, DF_REF_BBNO (ref)))
6fb5fa3c
DB
774 refs[id] = NULL;
775 }
776 else
777 refs[id] = NULL;
4d779342 778 }
b8698a0f 779
6fb5fa3c
DB
780 /* Delete any def-use or use-def chains that start here. It is
781 possible that there is trash in this field. This happens for
782 insns that have been deleted when rescanning has been deferred
783 and the chain problem has also been deleted. The chain tear down
784 code skips deleted insns. */
785 if (df_chain && DF_REF_CHAIN (ref))
786 df_chain_unlink (ref);
b8698a0f 787
4d779342 788 reg_info->n_refs--;
6fb5fa3c
DB
789 if (DF_REF_FLAGS_IS_SET (ref, DF_HARD_REG_LIVE))
790 {
791 gcc_assert (DF_REF_REGNO (ref) < FIRST_PSEUDO_REGISTER);
792 df->hard_regs_live_count[DF_REF_REGNO (ref)]--;
793 }
4d779342
DB
794
795 /* Unlink from the reg chain. If there is no prev, this is the
796 first of the list. If not, just join the next and prev. */
797 if (prev)
6fb5fa3c 798 DF_REF_NEXT_REG (prev) = next;
4d779342
DB
799 else
800 {
6fb5fa3c 801 gcc_assert (reg_info->reg_chain == ref);
4d779342 802 reg_info->reg_chain = next;
4d779342 803 }
6fb5fa3c
DB
804 if (next)
805 DF_REF_PREV_REG (next) = prev;
4d779342 806
ca9052ce 807 df_free_ref (ref);
6fb5fa3c
DB
808}
809
762613be
RS
810/* Initialize INSN_INFO to describe INSN. */
811
812static void
813df_insn_info_init_fields (df_insn_info *insn_info, rtx_insn *insn)
814{
815 memset (insn_info, 0, sizeof (struct df_insn_info));
816 insn_info->insn = insn;
817}
6fb5fa3c 818
6fb5fa3c
DB
819/* Create the insn record for INSN. If there was one there, zero it
820 out. */
4d779342 821
6fb5fa3c 822struct df_insn_info *
b2908ba6 823df_insn_create_insn_record (rtx_insn *insn)
4d779342 824{
23249ac4 825 struct df_scan_problem_data *problem_data
6fb5fa3c
DB
826 = (struct df_scan_problem_data *) df_scan->problem_data;
827 struct df_insn_info *insn_rec;
4d779342 828
6fb5fa3c 829 df_grow_insn_info ();
50e94c7e 830 insn_rec = DF_INSN_INFO_GET (insn);
4d779342
DB
831 if (!insn_rec)
832 {
e956943e 833 insn_rec = problem_data->insn_pool->allocate ();
50e94c7e 834 DF_INSN_INFO_SET (insn, insn_rec);
4d779342 835 }
762613be 836 df_insn_info_init_fields (insn_rec, insn);
4d779342
DB
837 return insn_rec;
838}
839
3b8266e2 840
6fb5fa3c 841/* Delete all du chain (DF_REF_CHAIN()) of all refs in the ref chain. */
4d779342 842
6fb5fa3c 843static void
b512946c 844df_ref_chain_delete_du_chain (df_ref ref)
4d779342 845{
b512946c
RS
846 for (; ref; ref = DF_REF_NEXT_LOC (ref))
847 /* CHAIN is allocated by DF_CHAIN. So make sure to
848 pass df_scan instance for the problem. */
849 if (DF_REF_CHAIN (ref))
850 df_chain_unlink (ref);
6fb5fa3c 851}
23249ac4 852
4d779342 853
6fb5fa3c
DB
854/* Delete all refs in the ref chain. */
855
856static void
b512946c 857df_ref_chain_delete (df_ref ref)
6fb5fa3c 858{
b512946c
RS
859 df_ref next;
860 for (; ref; ref = next)
6fb5fa3c 861 {
b512946c
RS
862 next = DF_REF_NEXT_LOC (ref);
863 df_reg_chain_unlink (ref);
6fb5fa3c 864 }
6fb5fa3c
DB
865}
866
867
868/* Delete the hardreg chain. */
869
870static void
b512946c 871df_mw_hardreg_chain_delete (struct df_mw_hardreg *hardregs)
6fb5fa3c 872{
b512946c
RS
873 struct df_scan_problem_data *problem_data
874 = (struct df_scan_problem_data *) df_scan->problem_data;
875 df_mw_hardreg *next;
6fb5fa3c 876
b512946c 877 for (; hardregs; hardregs = next)
6fb5fa3c 878 {
b512946c 879 next = DF_MWS_NEXT (hardregs);
e956943e 880 problem_data->mw_reg_pool->remove (hardregs);
6fb5fa3c
DB
881 }
882}
883
762613be
RS
884/* Remove the contents of INSN_INFO (but don't free INSN_INFO itself). */
885
886static void
887df_insn_info_free_fields (df_insn_info *insn_info)
888{
889 /* In general, notes do not have the insn_info fields
890 initialized. However, combine deletes insns by changing them
891 to notes. How clever. So we cannot just check if it is a
892 valid insn before short circuiting this code, we need to see
893 if we actually initialized it. */
894 df_mw_hardreg_chain_delete (insn_info->mw_hardregs);
895
896 if (df_chain)
897 {
898 df_ref_chain_delete_du_chain (insn_info->defs);
899 df_ref_chain_delete_du_chain (insn_info->uses);
900 df_ref_chain_delete_du_chain (insn_info->eq_uses);
901 }
902
903 df_ref_chain_delete (insn_info->defs);
904 df_ref_chain_delete (insn_info->uses);
905 df_ref_chain_delete (insn_info->eq_uses);
906}
6fb5fa3c 907
80eb8028
SB
908/* Delete all of the refs information from the insn with UID.
909 Internal helper for df_insn_delete, df_insn_rescan, and other
910 df-scan routines that don't have to work in deferred mode
911 and do not have to mark basic blocks for re-processing. */
6fb5fa3c 912
80eb8028
SB
913static void
914df_insn_info_delete (unsigned int uid)
6fb5fa3c 915{
80eb8028 916 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
6fb5fa3c 917
a7e3698d
JH
918 bitmap_clear_bit (&df->insns_to_delete, uid);
919 bitmap_clear_bit (&df->insns_to_rescan, uid);
920 bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
6fb5fa3c
DB
921 if (insn_info)
922 {
b8698a0f 923 struct df_scan_problem_data *problem_data
6fb5fa3c
DB
924 = (struct df_scan_problem_data *) df_scan->problem_data;
925
762613be 926 df_insn_info_free_fields (insn_info);
e956943e 927 problem_data->insn_pool->remove (insn_info);
6fb5fa3c 928 DF_INSN_UID_SET (uid, NULL);
4d779342
DB
929 }
930}
931
80eb8028
SB
932/* Delete all of the refs information from INSN, either right now
933 or marked for later in deferred mode. */
934
935void
b2908ba6 936df_insn_delete (rtx_insn *insn)
80eb8028
SB
937{
938 unsigned int uid;
939 basic_block bb;
940
941 gcc_checking_assert (INSN_P (insn));
942
943 if (!df)
944 return;
945
946 uid = INSN_UID (insn);
947 bb = BLOCK_FOR_INSN (insn);
948
949 /* ??? bb can be NULL after pass_free_cfg. At that point, DF should
950 not exist anymore (as mentioned in df-core.c: "The only requirement
951 [for DF] is that there be a correct control flow graph." Clearly
952 that isn't the case after pass_free_cfg. But DF is freed much later
953 because some back-ends want to use DF info even though the CFG is
954 already gone. It's not clear to me whether that is safe, actually.
955 In any case, we expect BB to be non-NULL at least up to register
956 allocation, so disallow a non-NULL BB up to there. Not perfect
957 but better than nothing... */
80eb8028
SB
958 gcc_checking_assert (bb != NULL || reload_completed);
959
960 df_grow_bb_info (df_scan);
961 df_grow_reg_info ();
962
963 /* The block must be marked as dirty now, rather than later as in
964 df_insn_rescan and df_notes_rescan because it may not be there at
965 rescanning time and the mark would blow up.
966 DEBUG_INSNs do not make a block's data flow solution dirty (at
967 worst the LUIDs are no longer contiguous). */
968 if (bb != NULL && NONDEBUG_INSN_P (insn))
969 df_set_bb_dirty (bb);
970
971 /* The client has deferred rescanning. */
972 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
973 {
974 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
975 if (insn_info)
976 {
977 bitmap_clear_bit (&df->insns_to_rescan, uid);
978 bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
979 bitmap_set_bit (&df->insns_to_delete, uid);
980 }
981 if (dump_file)
982 fprintf (dump_file, "deferring deletion of insn with uid = %d.\n", uid);
983 return;
984 }
985
986 if (dump_file)
987 fprintf (dump_file, "deleting insn with uid = %d.\n", uid);
988
989 df_insn_info_delete (uid);
990}
991
4d779342 992
6fb5fa3c 993/* Free all of the refs and the mw_hardregs in COLLECTION_REC. */
3b8266e2 994
6fb5fa3c
DB
995static void
996df_free_collection_rec (struct df_collection_rec *collection_rec)
3b8266e2 997{
c2569604 998 unsigned int ix;
b8698a0f 999 struct df_scan_problem_data *problem_data
6fb5fa3c 1000 = (struct df_scan_problem_data *) df_scan->problem_data;
c2569604
ILT
1001 df_ref ref;
1002 struct df_mw_hardreg *mw;
1003
9771b263 1004 FOR_EACH_VEC_ELT (collection_rec->def_vec, ix, ref)
c2569604 1005 df_free_ref (ref);
9771b263 1006 FOR_EACH_VEC_ELT (collection_rec->use_vec, ix, ref)
c2569604 1007 df_free_ref (ref);
9771b263 1008 FOR_EACH_VEC_ELT (collection_rec->eq_use_vec, ix, ref)
c2569604 1009 df_free_ref (ref);
9771b263 1010 FOR_EACH_VEC_ELT (collection_rec->mw_vec, ix, mw)
e956943e 1011 problem_data->mw_reg_pool->remove (mw);
c2569604 1012
9771b263
DN
1013 collection_rec->def_vec.release ();
1014 collection_rec->use_vec.release ();
1015 collection_rec->eq_use_vec.release ();
1016 collection_rec->mw_vec.release ();
6fb5fa3c 1017}
3b8266e2 1018
6fb5fa3c
DB
1019/* Rescan INSN. Return TRUE if the rescanning produced any changes. */
1020
b8698a0f 1021bool
b2908ba6 1022df_insn_rescan (rtx_insn *insn)
6fb5fa3c
DB
1023{
1024 unsigned int uid = INSN_UID (insn);
1025 struct df_insn_info *insn_info = NULL;
1026 basic_block bb = BLOCK_FOR_INSN (insn);
1027 struct df_collection_rec collection_rec;
6fb5fa3c
DB
1028
1029 if ((!df) || (!INSN_P (insn)))
1030 return false;
1031
1032 if (!bb)
3b8266e2 1033 {
6fb5fa3c
DB
1034 if (dump_file)
1035 fprintf (dump_file, "no bb for insn with uid = %d.\n", uid);
1036 return false;
1037 }
1038
1039 /* The client has disabled rescanning and plans to do it itself. */
1040 if (df->changeable_flags & DF_NO_INSN_RESCAN)
1041 return false;
1042
1043 df_grow_bb_info (df_scan);
1044 df_grow_reg_info ();
1045
1046 insn_info = DF_INSN_UID_SAFE_GET (uid);
1047
0a41f3b2 1048 /* The client has deferred rescanning. */
6fb5fa3c
DB
1049 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1050 {
1051 if (!insn_info)
1052 {
1053 insn_info = df_insn_create_insn_record (insn);
b512946c
RS
1054 insn_info->defs = 0;
1055 insn_info->uses = 0;
1056 insn_info->eq_uses = 0;
1057 insn_info->mw_hardregs = 0;
6fb5fa3c
DB
1058 }
1059 if (dump_file)
b718216c 1060 fprintf (dump_file, "deferring rescan insn with uid = %d.\n", uid);
b8698a0f 1061
a7e3698d
JH
1062 bitmap_clear_bit (&df->insns_to_delete, uid);
1063 bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
1064 bitmap_set_bit (&df->insns_to_rescan, INSN_UID (insn));
6fb5fa3c
DB
1065 return false;
1066 }
1067
a7e3698d
JH
1068 bitmap_clear_bit (&df->insns_to_delete, uid);
1069 bitmap_clear_bit (&df->insns_to_rescan, uid);
1070 bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
6fb5fa3c
DB
1071 if (insn_info)
1072 {
4a81774c 1073 int luid;
6fb5fa3c
DB
1074 bool the_same = df_insn_refs_verify (&collection_rec, bb, insn, false);
1075 /* If there's no change, return false. */
1076 if (the_same)
3b8266e2 1077 {
6fb5fa3c
DB
1078 df_free_collection_rec (&collection_rec);
1079 if (dump_file)
1080 fprintf (dump_file, "verify found no changes in insn with uid = %d.\n", uid);
1081 return false;
3b8266e2 1082 }
6fb5fa3c
DB
1083 if (dump_file)
1084 fprintf (dump_file, "rescanning insn with uid = %d.\n", uid);
1085
4a81774c
SB
1086 /* There's change - we need to delete the existing info.
1087 Since the insn isn't moved, we can salvage its LUID. */
1088 luid = DF_INSN_LUID (insn);
762613be
RS
1089 df_insn_info_free_fields (insn_info);
1090 df_insn_info_init_fields (insn_info, insn);
4a81774c 1091 DF_INSN_LUID (insn) = luid;
6fb5fa3c
DB
1092 }
1093 else
1094 {
50e94c7e
SB
1095 struct df_insn_info *insn_info = df_insn_create_insn_record (insn);
1096 df_insn_refs_collect (&collection_rec, bb, insn_info);
6fb5fa3c
DB
1097 if (dump_file)
1098 fprintf (dump_file, "scanning new insn with uid = %d.\n", uid);
1099 }
1100
ff4c81cc 1101 df_refs_add_to_chains (&collection_rec, bb, insn, copy_all);
f9aeaebf 1102 if (!DEBUG_INSN_P (insn))
c23cd1d6 1103 df_set_bb_dirty (bb);
c2569604 1104
6fb5fa3c
DB
1105 return true;
1106}
1107
b5b8b0ac
AO
1108/* Same as df_insn_rescan, but don't mark the basic block as
1109 dirty. */
1110
1111bool
b2908ba6 1112df_insn_rescan_debug_internal (rtx_insn *insn)
b5b8b0ac
AO
1113{
1114 unsigned int uid = INSN_UID (insn);
1115 struct df_insn_info *insn_info;
1116
a099f7d4
RG
1117 gcc_assert (DEBUG_INSN_P (insn)
1118 && VAR_LOC_UNKNOWN_P (INSN_VAR_LOCATION_LOC (insn)));
b5b8b0ac
AO
1119
1120 if (!df)
1121 return false;
1122
1123 insn_info = DF_INSN_UID_SAFE_GET (INSN_UID (insn));
1124 if (!insn_info)
1125 return false;
1126
1127 if (dump_file)
1128 fprintf (dump_file, "deleting debug_insn with uid = %d.\n", uid);
1129
a7e3698d
JH
1130 bitmap_clear_bit (&df->insns_to_delete, uid);
1131 bitmap_clear_bit (&df->insns_to_rescan, uid);
1132 bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
b5b8b0ac 1133
b512946c
RS
1134 if (insn_info->defs == 0
1135 && insn_info->uses == 0
1136 && insn_info->eq_uses == 0
1137 && insn_info->mw_hardregs == 0)
b5b8b0ac
AO
1138 return false;
1139
1140 df_mw_hardreg_chain_delete (insn_info->mw_hardregs);
1141
1142 if (df_chain)
1143 {
1144 df_ref_chain_delete_du_chain (insn_info->defs);
1145 df_ref_chain_delete_du_chain (insn_info->uses);
1146 df_ref_chain_delete_du_chain (insn_info->eq_uses);
1147 }
1148
1149 df_ref_chain_delete (insn_info->defs);
1150 df_ref_chain_delete (insn_info->uses);
1151 df_ref_chain_delete (insn_info->eq_uses);
1152
b512946c
RS
1153 insn_info->defs = 0;
1154 insn_info->uses = 0;
1155 insn_info->eq_uses = 0;
1156 insn_info->mw_hardregs = 0;
b5b8b0ac
AO
1157
1158 return true;
1159}
1160
6fb5fa3c
DB
1161
1162/* Rescan all of the insns in the function. Note that the artificial
80eb8028 1163 uses and defs are not touched. This function will destroy def-use
6fb5fa3c
DB
1164 or use-def chains. */
1165
1166void
1167df_insn_rescan_all (void)
1168{
1169 bool no_insn_rescan = false;
1170 bool defer_insn_rescan = false;
1171 basic_block bb;
1172 bitmap_iterator bi;
1173 unsigned int uid;
a7e3698d
JH
1174 bitmap_head tmp;
1175
1176 bitmap_initialize (&tmp, &df_bitmap_obstack);
b8698a0f 1177
6fb5fa3c
DB
1178 if (df->changeable_flags & DF_NO_INSN_RESCAN)
1179 {
1180 df_clear_flags (DF_NO_INSN_RESCAN);
1181 no_insn_rescan = true;
3b8266e2 1182 }
b8698a0f 1183
6fb5fa3c 1184 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
3b8266e2 1185 {
6fb5fa3c
DB
1186 df_clear_flags (DF_DEFER_INSN_RESCAN);
1187 defer_insn_rescan = true;
1188 }
1189
a7e3698d
JH
1190 bitmap_copy (&tmp, &df->insns_to_delete);
1191 EXECUTE_IF_SET_IN_BITMAP (&tmp, 0, uid, bi)
6fb5fa3c
DB
1192 {
1193 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1194 if (insn_info)
80eb8028 1195 df_insn_info_delete (uid);
3b8266e2 1196 }
6fb5fa3c 1197
a7e3698d
JH
1198 bitmap_clear (&tmp);
1199 bitmap_clear (&df->insns_to_delete);
1200 bitmap_clear (&df->insns_to_rescan);
1201 bitmap_clear (&df->insns_to_notes_rescan);
6fb5fa3c 1202
11cd3bed 1203 FOR_EACH_BB_FN (bb, cfun)
6fb5fa3c 1204 {
dd3eed93 1205 rtx_insn *insn;
6fb5fa3c
DB
1206 FOR_BB_INSNS (bb, insn)
1207 {
1208 df_insn_rescan (insn);
1209 }
1210 }
1211
1212 if (no_insn_rescan)
1213 df_set_flags (DF_NO_INSN_RESCAN);
1214 if (defer_insn_rescan)
1215 df_set_flags (DF_DEFER_INSN_RESCAN);
3b8266e2
KZ
1216}
1217
1218
0a41f3b2 1219/* Process all of the deferred rescans or deletions. */
4d779342 1220
6fb5fa3c
DB
1221void
1222df_process_deferred_rescans (void)
4d779342 1223{
6fb5fa3c
DB
1224 bool no_insn_rescan = false;
1225 bool defer_insn_rescan = false;
4d779342 1226 bitmap_iterator bi;
6fb5fa3c 1227 unsigned int uid;
a7e3698d
JH
1228 bitmap_head tmp;
1229
1230 bitmap_initialize (&tmp, &df_bitmap_obstack);
b8698a0f 1231
6fb5fa3c
DB
1232 if (df->changeable_flags & DF_NO_INSN_RESCAN)
1233 {
1234 df_clear_flags (DF_NO_INSN_RESCAN);
1235 no_insn_rescan = true;
1236 }
b8698a0f 1237
6fb5fa3c
DB
1238 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1239 {
1240 df_clear_flags (DF_DEFER_INSN_RESCAN);
1241 defer_insn_rescan = true;
1242 }
1243
1244 if (dump_file)
0a41f3b2 1245 fprintf (dump_file, "starting the processing of deferred insns\n");
6fb5fa3c 1246
a7e3698d
JH
1247 bitmap_copy (&tmp, &df->insns_to_delete);
1248 EXECUTE_IF_SET_IN_BITMAP (&tmp, 0, uid, bi)
6fb5fa3c
DB
1249 {
1250 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1251 if (insn_info)
80eb8028 1252 df_insn_info_delete (uid);
6fb5fa3c
DB
1253 }
1254
a7e3698d
JH
1255 bitmap_copy (&tmp, &df->insns_to_rescan);
1256 EXECUTE_IF_SET_IN_BITMAP (&tmp, 0, uid, bi)
6fb5fa3c
DB
1257 {
1258 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1259 if (insn_info)
1260 df_insn_rescan (insn_info->insn);
1261 }
1262
a7e3698d
JH
1263 bitmap_copy (&tmp, &df->insns_to_notes_rescan);
1264 EXECUTE_IF_SET_IN_BITMAP (&tmp, 0, uid, bi)
6fb5fa3c
DB
1265 {
1266 struct df_insn_info *insn_info = DF_INSN_UID_SAFE_GET (uid);
1267 if (insn_info)
1268 df_notes_rescan (insn_info->insn);
1269 }
1270
1271 if (dump_file)
0a41f3b2 1272 fprintf (dump_file, "ending the processing of deferred insns\n");
6fb5fa3c 1273
a7e3698d
JH
1274 bitmap_clear (&tmp);
1275 bitmap_clear (&df->insns_to_delete);
1276 bitmap_clear (&df->insns_to_rescan);
1277 bitmap_clear (&df->insns_to_notes_rescan);
6fb5fa3c
DB
1278
1279 if (no_insn_rescan)
1280 df_set_flags (DF_NO_INSN_RESCAN);
1281 if (defer_insn_rescan)
1282 df_set_flags (DF_DEFER_INSN_RESCAN);
1283
1284 /* If someone changed regs_ever_live during this pass, fix up the
1285 entry and exit blocks. */
1286 if (df->redo_entry_and_exit)
1287 {
1288 df_update_entry_exit_and_calls ();
1289 df->redo_entry_and_exit = false;
1290 }
1291}
1292
4d779342 1293
6fb5fa3c
DB
1294/* Count the number of refs. Include the defs if INCLUDE_DEFS. Include
1295 the uses if INCLUDE_USES. Include the eq_uses if
1296 INCLUDE_EQ_USES. */
1297
1298static unsigned int
b8698a0f 1299df_count_refs (bool include_defs, bool include_uses,
6fb5fa3c
DB
1300 bool include_eq_uses)
1301{
1302 unsigned int regno;
1303 int size = 0;
1304 unsigned int m = df->regs_inited;
b8698a0f 1305
6fb5fa3c 1306 for (regno = 0; regno < m; regno++)
4d779342 1307 {
6fb5fa3c
DB
1308 if (include_defs)
1309 size += DF_REG_DEF_COUNT (regno);
1310 if (include_uses)
1311 size += DF_REG_USE_COUNT (regno);
1312 if (include_eq_uses)
1313 size += DF_REG_EQ_USE_COUNT (regno);
4d779342 1314 }
6fb5fa3c 1315 return size;
4d779342
DB
1316}
1317
1318
1319/* Take build ref table for either the uses or defs from the reg-use
6fb5fa3c
DB
1320 or reg-def chains. This version processes the refs in reg order
1321 which is likely to be best if processing the whole function. */
4d779342 1322
b8698a0f 1323static void
6fb5fa3c 1324df_reorganize_refs_by_reg_by_reg (struct df_ref_info *ref_info,
b8698a0f
L
1325 bool include_defs,
1326 bool include_uses,
6fb5fa3c 1327 bool include_eq_uses)
4d779342 1328{
6fb5fa3c 1329 unsigned int m = df->regs_inited;
4d779342
DB
1330 unsigned int regno;
1331 unsigned int offset = 0;
6fb5fa3c 1332 unsigned int start;
4d779342 1333
6fb5fa3c
DB
1334 if (df->changeable_flags & DF_NO_HARD_REGS)
1335 {
1336 start = FIRST_PSEUDO_REGISTER;
1337 memset (ref_info->begin, 0, sizeof (int) * FIRST_PSEUDO_REGISTER);
1338 memset (ref_info->count, 0, sizeof (int) * FIRST_PSEUDO_REGISTER);
4d779342 1339 }
6fb5fa3c
DB
1340 else
1341 start = 0;
4d779342 1342
b8698a0f 1343 ref_info->total_size
6fb5fa3c
DB
1344 = df_count_refs (include_defs, include_uses, include_eq_uses);
1345
1346 df_check_and_grow_ref_info (ref_info, 1);
1347
1348 for (regno = start; regno < m; regno++)
4d779342 1349 {
4d779342 1350 int count = 0;
6fb5fa3c
DB
1351 ref_info->begin[regno] = offset;
1352 if (include_defs)
1353 {
57512f53 1354 df_ref ref = DF_REG_DEF_CHAIN (regno);
b8698a0f 1355 while (ref)
6fb5fa3c
DB
1356 {
1357 ref_info->refs[offset] = ref;
1358 DF_REF_ID (ref) = offset++;
1359 count++;
1360 ref = DF_REF_NEXT_REG (ref);
7a40b8b1 1361 gcc_checking_assert (offset < ref_info->refs_size);
6fb5fa3c
DB
1362 }
1363 }
1364 if (include_uses)
4d779342 1365 {
57512f53 1366 df_ref ref = DF_REG_USE_CHAIN (regno);
b8698a0f 1367 while (ref)
4d779342
DB
1368 {
1369 ref_info->refs[offset] = ref;
1370 DF_REF_ID (ref) = offset++;
6fb5fa3c 1371 count++;
4d779342 1372 ref = DF_REF_NEXT_REG (ref);
7a40b8b1 1373 gcc_checking_assert (offset < ref_info->refs_size);
6fb5fa3c
DB
1374 }
1375 }
1376 if (include_eq_uses)
1377 {
57512f53 1378 df_ref ref = DF_REG_EQ_USE_CHAIN (regno);
b8698a0f 1379 while (ref)
6fb5fa3c
DB
1380 {
1381 ref_info->refs[offset] = ref;
1382 DF_REF_ID (ref) = offset++;
4d779342 1383 count++;
6fb5fa3c 1384 ref = DF_REF_NEXT_REG (ref);
7a40b8b1 1385 gcc_checking_assert (offset < ref_info->refs_size);
4d779342 1386 }
4d779342 1387 }
6fb5fa3c 1388 ref_info->count[regno] = count;
4d779342 1389 }
b8698a0f 1390
4d779342
DB
1391 /* The bitmap size is not decremented when refs are deleted. So
1392 reset it now that we have squished out all of the empty
1393 slots. */
6fb5fa3c 1394 ref_info->table_size = offset;
4d779342
DB
1395}
1396
4d779342 1397
6fb5fa3c
DB
1398/* Take build ref table for either the uses or defs from the reg-use
1399 or reg-def chains. This version processes the refs in insn order
1400 which is likely to be best if processing some segment of the
1401 function. */
4d779342 1402
b8698a0f 1403static void
6fb5fa3c 1404df_reorganize_refs_by_reg_by_insn (struct df_ref_info *ref_info,
b8698a0f
L
1405 bool include_defs,
1406 bool include_uses,
6fb5fa3c 1407 bool include_eq_uses)
4d779342 1408{
6fb5fa3c
DB
1409 bitmap_iterator bi;
1410 unsigned int bb_index;
1411 unsigned int m = df->regs_inited;
1412 unsigned int offset = 0;
1413 unsigned int r;
b8698a0f 1414 unsigned int start
6fb5fa3c 1415 = (df->changeable_flags & DF_NO_HARD_REGS) ? FIRST_PSEUDO_REGISTER : 0;
4d779342 1416
6fb5fa3c
DB
1417 memset (ref_info->begin, 0, sizeof (int) * df->regs_inited);
1418 memset (ref_info->count, 0, sizeof (int) * df->regs_inited);
1419
1420 ref_info->total_size = df_count_refs (include_defs, include_uses, include_eq_uses);
1421 df_check_and_grow_ref_info (ref_info, 1);
4d779342 1422
6fb5fa3c 1423 EXECUTE_IF_SET_IN_BITMAP (df->blocks_to_analyze, 0, bb_index, bi)
4d779342 1424 {
06e28de2 1425 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
dd3eed93 1426 rtx_insn *insn;
bfac633a 1427 df_ref def, use;
6fb5fa3c
DB
1428
1429 if (include_defs)
292321a5 1430 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
23249ac4 1431 {
292321a5 1432 unsigned int regno = DF_REF_REGNO (def);
6fb5fa3c 1433 ref_info->count[regno]++;
23249ac4 1434 }
6fb5fa3c 1435 if (include_uses)
292321a5 1436 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
23249ac4 1437 {
292321a5 1438 unsigned int regno = DF_REF_REGNO (use);
6fb5fa3c 1439 ref_info->count[regno]++;
23249ac4 1440 }
4d779342 1441
6fb5fa3c
DB
1442 FOR_BB_INSNS (bb, insn)
1443 {
1444 if (INSN_P (insn))
1445 {
bfac633a 1446 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
b8698a0f 1447
6fb5fa3c 1448 if (include_defs)
bfac633a 1449 FOR_EACH_INSN_INFO_DEF (def, insn_info)
6fb5fa3c 1450 {
bfac633a 1451 unsigned int regno = DF_REF_REGNO (def);
6fb5fa3c
DB
1452 ref_info->count[regno]++;
1453 }
1454 if (include_uses)
bfac633a 1455 FOR_EACH_INSN_INFO_USE (use, insn_info)
6fb5fa3c 1456 {
bfac633a 1457 unsigned int regno = DF_REF_REGNO (use);
6fb5fa3c
DB
1458 ref_info->count[regno]++;
1459 }
1460 if (include_eq_uses)
bfac633a 1461 FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
6fb5fa3c 1462 {
bfac633a 1463 unsigned int regno = DF_REF_REGNO (use);
6fb5fa3c
DB
1464 ref_info->count[regno]++;
1465 }
1466 }
1467 }
1468 }
1469
1470 for (r = start; r < m; r++)
1471 {
1472 ref_info->begin[r] = offset;
1473 offset += ref_info->count[r];
1474 ref_info->count[r] = 0;
1475 }
b8698a0f 1476
6fb5fa3c
DB
1477 EXECUTE_IF_SET_IN_BITMAP (df->blocks_to_analyze, 0, bb_index, bi)
1478 {
06e28de2 1479 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
dd3eed93 1480 rtx_insn *insn;
bfac633a 1481 df_ref def, use;
6fb5fa3c
DB
1482
1483 if (include_defs)
292321a5 1484 FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
23249ac4 1485 {
292321a5 1486 unsigned int regno = DF_REF_REGNO (def);
6fb5fa3c 1487 if (regno >= start)
23249ac4 1488 {
6fb5fa3c
DB
1489 unsigned int id
1490 = ref_info->begin[regno] + ref_info->count[regno]++;
292321a5
RS
1491 DF_REF_ID (def) = id;
1492 ref_info->refs[id] = def;
23249ac4 1493 }
23249ac4 1494 }
6fb5fa3c 1495 if (include_uses)
292321a5 1496 FOR_EACH_ARTIFICIAL_USE (use, bb_index)
23249ac4 1497 {
292321a5 1498 unsigned int regno = DF_REF_REGNO (def);
6fb5fa3c
DB
1499 if (regno >= start)
1500 {
1501 unsigned int id
1502 = ref_info->begin[regno] + ref_info->count[regno]++;
292321a5
RS
1503 DF_REF_ID (use) = id;
1504 ref_info->refs[id] = use;
6fb5fa3c 1505 }
23249ac4 1506 }
6fb5fa3c
DB
1507
1508 FOR_BB_INSNS (bb, insn)
1509 {
1510 if (INSN_P (insn))
1511 {
bfac633a 1512 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
b8698a0f 1513
6fb5fa3c 1514 if (include_defs)
bfac633a 1515 FOR_EACH_INSN_INFO_DEF (def, insn_info)
6fb5fa3c 1516 {
bfac633a 1517 unsigned int regno = DF_REF_REGNO (def);
6fb5fa3c
DB
1518 if (regno >= start)
1519 {
1520 unsigned int id
1521 = ref_info->begin[regno] + ref_info->count[regno]++;
bfac633a
RS
1522 DF_REF_ID (def) = id;
1523 ref_info->refs[id] = def;
6fb5fa3c
DB
1524 }
1525 }
1526 if (include_uses)
bfac633a 1527 FOR_EACH_INSN_INFO_USE (use, insn_info)
6fb5fa3c 1528 {
bfac633a 1529 unsigned int regno = DF_REF_REGNO (use);
6fb5fa3c
DB
1530 if (regno >= start)
1531 {
1532 unsigned int id
1533 = ref_info->begin[regno] + ref_info->count[regno]++;
bfac633a
RS
1534 DF_REF_ID (use) = id;
1535 ref_info->refs[id] = use;
6fb5fa3c
DB
1536 }
1537 }
1538 if (include_eq_uses)
bfac633a 1539 FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
6fb5fa3c 1540 {
bfac633a 1541 unsigned int regno = DF_REF_REGNO (use);
6fb5fa3c
DB
1542 if (regno >= start)
1543 {
1544 unsigned int id
1545 = ref_info->begin[regno] + ref_info->count[regno]++;
bfac633a
RS
1546 DF_REF_ID (use) = id;
1547 ref_info->refs[id] = use;
6fb5fa3c
DB
1548 }
1549 }
1550 }
1551 }
1552 }
1553
1554 /* The bitmap size is not decremented when refs are deleted. So
1555 reset it now that we have squished out all of the empty
1556 slots. */
1557
1558 ref_info->table_size = offset;
1559}
1560
1561/* Take build ref table for either the uses or defs from the reg-use
1562 or reg-def chains. */
1563
b8698a0f 1564static void
6fb5fa3c 1565df_reorganize_refs_by_reg (struct df_ref_info *ref_info,
b8698a0f
L
1566 bool include_defs,
1567 bool include_uses,
6fb5fa3c
DB
1568 bool include_eq_uses)
1569{
1570 if (df->analyze_subset)
b8698a0f 1571 df_reorganize_refs_by_reg_by_insn (ref_info, include_defs,
6fb5fa3c
DB
1572 include_uses, include_eq_uses);
1573 else
b8698a0f 1574 df_reorganize_refs_by_reg_by_reg (ref_info, include_defs,
6fb5fa3c
DB
1575 include_uses, include_eq_uses);
1576}
1577
1578
1579/* Add the refs in REF_VEC to the table in REF_INFO starting at OFFSET. */
b8698a0f
L
1580static unsigned int
1581df_add_refs_to_table (unsigned int offset,
1582 struct df_ref_info *ref_info,
b512946c 1583 df_ref ref)
6fb5fa3c 1584{
b512946c
RS
1585 for (; ref; ref = DF_REF_NEXT_LOC (ref))
1586 if (!(df->changeable_flags & DF_NO_HARD_REGS)
1587 || (DF_REF_REGNO (ref) >= FIRST_PSEUDO_REGISTER))
1588 {
1589 ref_info->refs[offset] = ref;
1590 DF_REF_ID (ref) = offset++;
1591 }
6fb5fa3c
DB
1592 return offset;
1593}
1594
1595
1596/* Count the number of refs in all of the insns of BB. Include the
1597 defs if INCLUDE_DEFS. Include the uses if INCLUDE_USES. Include the
1598 eq_uses if INCLUDE_EQ_USES. */
1599
1600static unsigned int
b8698a0f 1601df_reorganize_refs_by_insn_bb (basic_block bb, unsigned int offset,
6fb5fa3c 1602 struct df_ref_info *ref_info,
b8698a0f 1603 bool include_defs, bool include_uses,
6fb5fa3c
DB
1604 bool include_eq_uses)
1605{
dd3eed93 1606 rtx_insn *insn;
6fb5fa3c
DB
1607
1608 if (include_defs)
b8698a0f 1609 offset = df_add_refs_to_table (offset, ref_info,
6fb5fa3c
DB
1610 df_get_artificial_defs (bb->index));
1611 if (include_uses)
b8698a0f 1612 offset = df_add_refs_to_table (offset, ref_info,
6fb5fa3c
DB
1613 df_get_artificial_uses (bb->index));
1614
1615 FOR_BB_INSNS (bb, insn)
1616 if (INSN_P (insn))
1617 {
1618 unsigned int uid = INSN_UID (insn);
1619 if (include_defs)
b8698a0f 1620 offset = df_add_refs_to_table (offset, ref_info,
6fb5fa3c
DB
1621 DF_INSN_UID_DEFS (uid));
1622 if (include_uses)
b8698a0f 1623 offset = df_add_refs_to_table (offset, ref_info,
6fb5fa3c
DB
1624 DF_INSN_UID_USES (uid));
1625 if (include_eq_uses)
b8698a0f 1626 offset = df_add_refs_to_table (offset, ref_info,
6fb5fa3c 1627 DF_INSN_UID_EQ_USES (uid));
23249ac4 1628 }
6fb5fa3c
DB
1629 return offset;
1630}
1631
1632
0d52bcc1 1633/* Organize the refs by insn into the table in REF_INFO. If
6fb5fa3c
DB
1634 blocks_to_analyze is defined, use that set, otherwise the entire
1635 program. Include the defs if INCLUDE_DEFS. Include the uses if
1636 INCLUDE_USES. Include the eq_uses if INCLUDE_EQ_USES. */
1637
1638static void
1639df_reorganize_refs_by_insn (struct df_ref_info *ref_info,
b8698a0f 1640 bool include_defs, bool include_uses,
6fb5fa3c
DB
1641 bool include_eq_uses)
1642{
1643 basic_block bb;
1644 unsigned int offset = 0;
1645
1646 ref_info->total_size = df_count_refs (include_defs, include_uses, include_eq_uses);
1647 df_check_and_grow_ref_info (ref_info, 1);
1648 if (df->blocks_to_analyze)
1649 {
1650 bitmap_iterator bi;
1651 unsigned int index;
1652
1653 EXECUTE_IF_SET_IN_BITMAP (df->blocks_to_analyze, 0, index, bi)
1654 {
06e28de2
DM
1655 offset = df_reorganize_refs_by_insn_bb (BASIC_BLOCK_FOR_FN (cfun,
1656 index),
1657 offset, ref_info,
b8698a0f 1658 include_defs, include_uses,
6fb5fa3c
DB
1659 include_eq_uses);
1660 }
1661
1662 ref_info->table_size = offset;
1663 }
1664 else
1665 {
04a90bec 1666 FOR_ALL_BB_FN (bb, cfun)
b8698a0f
L
1667 offset = df_reorganize_refs_by_insn_bb (bb, offset, ref_info,
1668 include_defs, include_uses,
6fb5fa3c
DB
1669 include_eq_uses);
1670 ref_info->table_size = offset;
1671 }
1672}
1673
1674
1675/* If the use refs in DF are not organized, reorganize them. */
1676
b8698a0f 1677void
6fb5fa3c
DB
1678df_maybe_reorganize_use_refs (enum df_ref_order order)
1679{
1680 if (order == df->use_info.ref_order)
1681 return;
1682
1683 switch (order)
1684 {
1685 case DF_REF_ORDER_BY_REG:
1686 df_reorganize_refs_by_reg (&df->use_info, false, true, false);
1687 break;
1688
1689 case DF_REF_ORDER_BY_REG_WITH_NOTES:
1690 df_reorganize_refs_by_reg (&df->use_info, false, true, true);
1691 break;
1692
1693 case DF_REF_ORDER_BY_INSN:
1694 df_reorganize_refs_by_insn (&df->use_info, false, true, false);
1695 break;
1696
1697 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
1698 df_reorganize_refs_by_insn (&df->use_info, false, true, true);
1699 break;
1700
1701 case DF_REF_ORDER_NO_TABLE:
1702 free (df->use_info.refs);
1703 df->use_info.refs = NULL;
1704 df->use_info.refs_size = 0;
1705 break;
1706
1707 case DF_REF_ORDER_UNORDERED:
1708 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
1709 gcc_unreachable ();
1710 break;
1711 }
b8698a0f 1712
6fb5fa3c
DB
1713 df->use_info.ref_order = order;
1714}
1715
1716
1717/* If the def refs in DF are not organized, reorganize them. */
1718
b8698a0f 1719void
6fb5fa3c
DB
1720df_maybe_reorganize_def_refs (enum df_ref_order order)
1721{
1722 if (order == df->def_info.ref_order)
1723 return;
1724
1725 switch (order)
1726 {
1727 case DF_REF_ORDER_BY_REG:
1728 df_reorganize_refs_by_reg (&df->def_info, true, false, false);
1729 break;
1730
1731 case DF_REF_ORDER_BY_INSN:
1732 df_reorganize_refs_by_insn (&df->def_info, true, false, false);
1733 break;
1734
1735 case DF_REF_ORDER_NO_TABLE:
1736 free (df->def_info.refs);
1737 df->def_info.refs = NULL;
1738 df->def_info.refs_size = 0;
1739 break;
1740
1741 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
1742 case DF_REF_ORDER_BY_REG_WITH_NOTES:
1743 case DF_REF_ORDER_UNORDERED:
1744 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
1745 gcc_unreachable ();
1746 break;
1747 }
b8698a0f 1748
6fb5fa3c
DB
1749 df->def_info.ref_order = order;
1750}
1751
1752
6fb5fa3c 1753/* Change all of the basic block references in INSN to use the insn's
b8698a0f
L
1754 current basic block. This function is called from routines that move
1755 instructions from one block to another. */
6fb5fa3c
DB
1756
1757void
b2908ba6 1758df_insn_change_bb (rtx_insn *insn, basic_block new_bb)
6fb5fa3c 1759{
63642d5a 1760 basic_block old_bb = BLOCK_FOR_INSN (insn);
6fb5fa3c
DB
1761 struct df_insn_info *insn_info;
1762 unsigned int uid = INSN_UID (insn);
1763
63642d5a
AO
1764 if (old_bb == new_bb)
1765 return;
1766
1767 set_block_for_insn (insn, new_bb);
1768
6fb5fa3c
DB
1769 if (!df)
1770 return;
1771
1772 if (dump_file)
1773 fprintf (dump_file, "changing bb of uid %d\n", uid);
1774
1775 insn_info = DF_INSN_UID_SAFE_GET (uid);
1776 if (insn_info == NULL)
1777 {
1778 if (dump_file)
1779 fprintf (dump_file, " unscanned insn\n");
1780 df_insn_rescan (insn);
1781 return;
1782 }
1783
1784 if (!INSN_P (insn))
1785 return;
1786
6fb5fa3c
DB
1787 df_set_bb_dirty (new_bb);
1788 if (old_bb)
1789 {
1790 if (dump_file)
b8698a0f 1791 fprintf (dump_file, " from %d to %d\n",
6fb5fa3c
DB
1792 old_bb->index, new_bb->index);
1793 df_set_bb_dirty (old_bb);
1794 }
1795 else
1796 if (dump_file)
1797 fprintf (dump_file, " to %d\n", new_bb->index);
1798}
1799
1800
1801/* Helper function for df_ref_change_reg_with_loc. */
1802
1803static void
b8698a0f 1804df_ref_change_reg_with_loc_1 (struct df_reg_info *old_df,
57512f53 1805 struct df_reg_info *new_df,
e1a2b021 1806 unsigned int new_regno, rtx loc)
6fb5fa3c 1807{
57512f53 1808 df_ref the_ref = old_df->reg_chain;
6fb5fa3c
DB
1809
1810 while (the_ref)
1811 {
57512f53 1812 if ((!DF_REF_IS_ARTIFICIAL (the_ref))
502ef838 1813 && DF_REF_LOC (the_ref)
57512f53 1814 && (*DF_REF_LOC (the_ref) == loc))
6fb5fa3c 1815 {
57512f53
KZ
1816 df_ref next_ref = DF_REF_NEXT_REG (the_ref);
1817 df_ref prev_ref = DF_REF_PREV_REG (the_ref);
b512946c 1818 df_ref *ref_ptr;
57512f53 1819 struct df_insn_info *insn_info = DF_REF_INSN_INFO (the_ref);
6fb5fa3c
DB
1820
1821 DF_REF_REGNO (the_ref) = new_regno;
1822 DF_REF_REG (the_ref) = regno_reg_rtx[new_regno];
1823
1824 /* Pull the_ref out of the old regno chain. */
1825 if (prev_ref)
57512f53 1826 DF_REF_NEXT_REG (prev_ref) = next_ref;
6fb5fa3c 1827 else
60564289 1828 old_df->reg_chain = next_ref;
6fb5fa3c 1829 if (next_ref)
57512f53 1830 DF_REF_PREV_REG (next_ref) = prev_ref;
60564289 1831 old_df->n_refs--;
6fb5fa3c
DB
1832
1833 /* Put the ref into the new regno chain. */
57512f53
KZ
1834 DF_REF_PREV_REG (the_ref) = NULL;
1835 DF_REF_NEXT_REG (the_ref) = new_df->reg_chain;
60564289 1836 if (new_df->reg_chain)
57512f53 1837 DF_REF_PREV_REG (new_df->reg_chain) = the_ref;
60564289
KG
1838 new_df->reg_chain = the_ref;
1839 new_df->n_refs++;
5288f999
KZ
1840 if (DF_REF_BB (the_ref))
1841 df_set_bb_dirty (DF_REF_BB (the_ref));
6fb5fa3c 1842
57512f53
KZ
1843 /* Need to sort the record again that the ref was in because
1844 the regno is a sorting key. First, find the right
1845 record. */
b512946c
RS
1846 if (DF_REF_REG_DEF_P (the_ref))
1847 ref_ptr = &insn_info->defs;
1848 else if (DF_REF_FLAGS (the_ref) & DF_REF_IN_NOTE)
1849 ref_ptr = &insn_info->eq_uses;
6fb5fa3c 1850 else
b512946c 1851 ref_ptr = &insn_info->uses;
57512f53 1852 if (dump_file)
b8698a0f
L
1853 fprintf (dump_file, "changing reg in insn %d\n",
1854 DF_REF_INSN_UID (the_ref));
1855
b512946c
RS
1856 /* Stop if we find the current reference or where the reference
1857 needs to be. */
1858 while (*ref_ptr != the_ref && df_ref_compare (*ref_ptr, the_ref) < 0)
1859 ref_ptr = &DF_REF_NEXT_LOC (*ref_ptr);
1860 if (*ref_ptr != the_ref)
6fb5fa3c 1861 {
b512946c
RS
1862 /* The reference needs to be promoted up the list. */
1863 df_ref next = DF_REF_NEXT_LOC (the_ref);
1864 DF_REF_NEXT_LOC (the_ref) = *ref_ptr;
1865 *ref_ptr = the_ref;
1866 do
1867 ref_ptr = &DF_REF_NEXT_LOC (*ref_ptr);
1868 while (*ref_ptr != the_ref);
1869 *ref_ptr = next;
1870 }
1871 else if (DF_REF_NEXT_LOC (the_ref)
1872 && df_ref_compare (the_ref, DF_REF_NEXT_LOC (the_ref)) > 0)
1873 {
1874 /* The reference needs to be demoted down the list. */
1875 *ref_ptr = DF_REF_NEXT_LOC (the_ref);
1876 do
1877 ref_ptr = &DF_REF_NEXT_LOC (*ref_ptr);
1878 while (*ref_ptr && df_ref_compare (the_ref, *ref_ptr) > 0);
1879 DF_REF_NEXT_LOC (the_ref) = *ref_ptr;
1880 *ref_ptr = the_ref;
6fb5fa3c 1881 }
6fb5fa3c
DB
1882
1883 the_ref = next_ref;
1884 }
1885 else
57512f53 1886 the_ref = DF_REF_NEXT_REG (the_ref);
6fb5fa3c
DB
1887 }
1888}
1889
1890
e1a2b021
RS
1891/* Change the regno of register LOC to NEW_REGNO and update the df
1892 information accordingly. Refs that do not match LOC are not changed
1893 which means that artificial refs are not changed since they have no loc.
1894 This call is to support the SET_REGNO macro. */
6fb5fa3c
DB
1895
1896void
e1a2b021 1897df_ref_change_reg_with_loc (rtx loc, unsigned int new_regno)
6fb5fa3c 1898{
e1a2b021
RS
1899 unsigned int old_regno = REGNO (loc);
1900 if (old_regno == new_regno)
6fb5fa3c
DB
1901 return;
1902
e1a2b021
RS
1903 if (df)
1904 {
1905 df_grow_reg_info ();
1906
1907 df_ref_change_reg_with_loc_1 (DF_REG_DEF_GET (old_regno),
1908 DF_REG_DEF_GET (new_regno),
1909 new_regno, loc);
1910 df_ref_change_reg_with_loc_1 (DF_REG_USE_GET (old_regno),
1911 DF_REG_USE_GET (new_regno),
1912 new_regno, loc);
1913 df_ref_change_reg_with_loc_1 (DF_REG_EQ_USE_GET (old_regno),
1914 DF_REG_EQ_USE_GET (new_regno),
1915 new_regno, loc);
1916 }
9188b286 1917 set_mode_and_regno (loc, GET_MODE (loc), new_regno);
6fb5fa3c
DB
1918}
1919
1920
1921/* Delete the mw_hardregs that point into the eq_notes. */
1922
b512946c 1923static void
6fb5fa3c
DB
1924df_mw_hardreg_chain_delete_eq_uses (struct df_insn_info *insn_info)
1925{
b512946c 1926 struct df_mw_hardreg **mw_ptr = &insn_info->mw_hardregs;
b8698a0f 1927 struct df_scan_problem_data *problem_data
6fb5fa3c
DB
1928 = (struct df_scan_problem_data *) df_scan->problem_data;
1929
b512946c 1930 while (*mw_ptr)
6fb5fa3c 1931 {
b512946c
RS
1932 df_mw_hardreg *mw = *mw_ptr;
1933 if (mw->flags & DF_REF_IN_NOTE)
6fb5fa3c 1934 {
b512946c 1935 *mw_ptr = DF_MWS_NEXT (mw);
e956943e 1936 problem_data->mw_reg_pool->remove (mw);
6fb5fa3c
DB
1937 }
1938 else
b512946c 1939 mw_ptr = &DF_MWS_NEXT (mw);
6fb5fa3c 1940 }
6fb5fa3c
DB
1941}
1942
1943
1944/* Rescan only the REG_EQUIV/REG_EQUAL notes part of INSN. */
1945
1946void
b2908ba6 1947df_notes_rescan (rtx_insn *insn)
6fb5fa3c
DB
1948{
1949 struct df_insn_info *insn_info;
1950 unsigned int uid = INSN_UID (insn);
1951
1952 if (!df)
1953 return;
1954
1955 /* The client has disabled rescanning and plans to do it itself. */
1956 if (df->changeable_flags & DF_NO_INSN_RESCAN)
1957 return;
1958
c9b69ba2
RS
1959 /* Do nothing if the insn hasn't been emitted yet. */
1960 if (!BLOCK_FOR_INSN (insn))
1961 return;
1962
6fb5fa3c
DB
1963 df_grow_bb_info (df_scan);
1964 df_grow_reg_info ();
1965
c3284718 1966 insn_info = DF_INSN_UID_SAFE_GET (INSN_UID (insn));
6fb5fa3c 1967
0a41f3b2 1968 /* The client has deferred rescanning. */
6fb5fa3c
DB
1969 if (df->changeable_flags & DF_DEFER_INSN_RESCAN)
1970 {
1971 if (!insn_info)
1972 {
1973 insn_info = df_insn_create_insn_record (insn);
b512946c
RS
1974 insn_info->defs = 0;
1975 insn_info->uses = 0;
1976 insn_info->eq_uses = 0;
1977 insn_info->mw_hardregs = 0;
6fb5fa3c 1978 }
b8698a0f 1979
a7e3698d 1980 bitmap_clear_bit (&df->insns_to_delete, uid);
6fb5fa3c
DB
1981 /* If the insn is set to be rescanned, it does not need to also
1982 be notes rescanned. */
a7e3698d
JH
1983 if (!bitmap_bit_p (&df->insns_to_rescan, uid))
1984 bitmap_set_bit (&df->insns_to_notes_rescan, INSN_UID (insn));
6fb5fa3c
DB
1985 return;
1986 }
1987
a7e3698d
JH
1988 bitmap_clear_bit (&df->insns_to_delete, uid);
1989 bitmap_clear_bit (&df->insns_to_notes_rescan, uid);
6fb5fa3c
DB
1990
1991 if (insn_info)
1992 {
1993 basic_block bb = BLOCK_FOR_INSN (insn);
1994 rtx note;
1995 struct df_collection_rec collection_rec;
b512946c 1996 unsigned int i;
6fb5fa3c 1997
b512946c 1998 df_mw_hardreg_chain_delete_eq_uses (insn_info);
6fb5fa3c
DB
1999 df_ref_chain_delete (insn_info->eq_uses);
2000 insn_info->eq_uses = NULL;
2001
2002 /* Process REG_EQUIV/REG_EQUAL notes */
2003 for (note = REG_NOTES (insn); note;
2004 note = XEXP (note, 1))
2005 {
2006 switch (REG_NOTE_KIND (note))
2007 {
2008 case REG_EQUIV:
2009 case REG_EQUAL:
502ef838 2010 df_uses_record (&collection_rec,
6fb5fa3c 2011 &XEXP (note, 0), DF_REF_REG_USE,
502ef838 2012 bb, insn_info, DF_REF_IN_NOTE);
6fb5fa3c
DB
2013 default:
2014 break;
2015 }
2016 }
2017
2018 /* Find some place to put any new mw_hardregs. */
2019 df_canonize_collection_rec (&collection_rec);
b512946c
RS
2020 struct df_mw_hardreg **mw_ptr = &insn_info->mw_hardregs, *mw;
2021 FOR_EACH_VEC_ELT (collection_rec.mw_vec, i, mw)
6fb5fa3c 2022 {
b512946c
RS
2023 while (*mw_ptr && df_mw_compare (*mw_ptr, mw) < 0)
2024 mw_ptr = &DF_MWS_NEXT (*mw_ptr);
2025 DF_MWS_NEXT (mw) = *mw_ptr;
2026 *mw_ptr = mw;
2027 mw_ptr = &DF_MWS_NEXT (mw);
6fb5fa3c 2028 }
ff4c81cc 2029 df_refs_add_to_chains (&collection_rec, bb, insn, copy_eq_uses);
6fb5fa3c
DB
2030 }
2031 else
2032 df_insn_rescan (insn);
2033
2034}
2035
2036\f
2037/*----------------------------------------------------------------------------
2038 Hard core instruction scanning code. No external interfaces here,
2039 just a lot of routines that look inside insns.
2040----------------------------------------------------------------------------*/
2041
2042
b8698a0f 2043/* Return true if the contents of two df_ref's are identical.
6fb5fa3c
DB
2044 It ignores DF_REF_MARKER. */
2045
2046static bool
57512f53 2047df_ref_equal_p (df_ref ref1, df_ref ref2)
6fb5fa3c
DB
2048{
2049 if (!ref2)
2050 return false;
b8698a0f 2051
57512f53
KZ
2052 if (ref1 == ref2)
2053 return true;
2054
2055 if (DF_REF_CLASS (ref1) != DF_REF_CLASS (ref2)
2056 || DF_REF_REGNO (ref1) != DF_REF_REGNO (ref2)
2057 || DF_REF_REG (ref1) != DF_REF_REG (ref2)
2058 || DF_REF_TYPE (ref1) != DF_REF_TYPE (ref2)
b8698a0f 2059 || ((DF_REF_FLAGS (ref1) & ~(DF_REF_REG_MARKER + DF_REF_MW_HARDREG))
57512f53
KZ
2060 != (DF_REF_FLAGS (ref2) & ~(DF_REF_REG_MARKER + DF_REF_MW_HARDREG)))
2061 || DF_REF_BB (ref1) != DF_REF_BB (ref2)
2062 || DF_REF_INSN_INFO (ref1) != DF_REF_INSN_INFO (ref2))
2063 return false;
b8698a0f 2064
57512f53
KZ
2065 switch (DF_REF_CLASS (ref1))
2066 {
2067 case DF_REF_ARTIFICIAL:
2068 case DF_REF_BASE:
2069 return true;
ca9052ce 2070
57512f53
KZ
2071 case DF_REF_REGULAR:
2072 return DF_REF_LOC (ref1) == DF_REF_LOC (ref2);
ca9052ce 2073
57512f53
KZ
2074 default:
2075 gcc_unreachable ();
2076 }
2077 return false;
6fb5fa3c
DB
2078}
2079
2080
2081/* Compare REF1 and REF2 for sorting. This is only called from places
2082 where all of the refs are of the same type, in the same insn, and
2083 have the same bb. So these fields are not checked. */
2084
2085static int
b512946c 2086df_ref_compare (df_ref ref1, df_ref ref2)
6fb5fa3c 2087{
57512f53
KZ
2088 if (DF_REF_CLASS (ref1) != DF_REF_CLASS (ref2))
2089 return (int)DF_REF_CLASS (ref1) - (int)DF_REF_CLASS (ref2);
2090
6fb5fa3c
DB
2091 if (DF_REF_REGNO (ref1) != DF_REF_REGNO (ref2))
2092 return (int)DF_REF_REGNO (ref1) - (int)DF_REF_REGNO (ref2);
b8698a0f 2093
6fb5fa3c
DB
2094 if (DF_REF_TYPE (ref1) != DF_REF_TYPE (ref2))
2095 return (int)DF_REF_TYPE (ref1) - (int)DF_REF_TYPE (ref2);
2096
57512f53
KZ
2097 if (DF_REF_REG (ref1) != DF_REF_REG (ref2))
2098 return (int)DF_REF_ORDER (ref1) - (int)DF_REF_ORDER (ref2);
2099
2100 /* Cannot look at the LOC field on artificial refs. */
2101 if (DF_REF_CLASS (ref1) != DF_REF_ARTIFICIAL
2102 && DF_REF_LOC (ref1) != DF_REF_LOC (ref2))
6fb5fa3c
DB
2103 return (int)DF_REF_ORDER (ref1) - (int)DF_REF_ORDER (ref2);
2104
2105 if (DF_REF_FLAGS (ref1) != DF_REF_FLAGS (ref2))
2106 {
2107 /* If two refs are identical except that one of them has is from
2108 a mw and one is not, we need to have the one with the mw
2109 first. */
2110 if (DF_REF_FLAGS_IS_SET (ref1, DF_REF_MW_HARDREG) ==
2111 DF_REF_FLAGS_IS_SET (ref2, DF_REF_MW_HARDREG))
2112 return DF_REF_FLAGS (ref1) - DF_REF_FLAGS (ref2);
2113 else if (DF_REF_FLAGS_IS_SET (ref1, DF_REF_MW_HARDREG))
2114 return -1;
2115 else
2116 return 1;
2117 }
ca9052ce 2118
5797be12 2119 return (int)DF_REF_ORDER (ref1) - (int)DF_REF_ORDER (ref2);
6fb5fa3c
DB
2120}
2121
b512946c
RS
2122/* Like df_ref_compare, but compare two df_ref* pointers R1 and R2. */
2123
2124static int
2125df_ref_ptr_compare (const void *r1, const void *r2)
2126{
2127 return df_ref_compare (*(const df_ref *) r1, *(const df_ref *) r2);
2128}
2129
6fb5fa3c
DB
2130/* Sort and compress a set of refs. */
2131
c2569604 2132static void
ff4c81cc 2133df_sort_and_compress_refs (vec<df_ref, va_heap> *ref_vec)
6fb5fa3c 2134{
c2569604 2135 unsigned int count;
6fb5fa3c
DB
2136 unsigned int i;
2137 unsigned int dist = 0;
2138
9771b263 2139 count = ref_vec->length ();
c2569604 2140
6fb5fa3c
DB
2141 /* If there are 1 or 0 elements, there is nothing to do. */
2142 if (count < 2)
c2569604 2143 return;
6fb5fa3c
DB
2144 else if (count == 2)
2145 {
9771b263
DN
2146 df_ref r0 = (*ref_vec)[0];
2147 df_ref r1 = (*ref_vec)[1];
b512946c 2148 if (df_ref_compare (r0, r1) > 0)
6b4db501 2149 std::swap ((*ref_vec)[0], (*ref_vec)[1]);
6fb5fa3c
DB
2150 }
2151 else
2152 {
2153 for (i = 0; i < count - 1; i++)
c2569604 2154 {
9771b263
DN
2155 df_ref r0 = (*ref_vec)[i];
2156 df_ref r1 = (*ref_vec)[i + 1];
b512946c 2157 if (df_ref_compare (r0, r1) >= 0)
c2569604
ILT
2158 break;
2159 }
6fb5fa3c
DB
2160 /* If the array is already strictly ordered,
2161 which is the most common case for large COUNT case
2162 (which happens for CALL INSNs),
2163 no need to sort and filter out duplicate.
b8698a0f 2164 Simply return the count.
6fb5fa3c
DB
2165 Make sure DF_GET_ADD_REFS adds refs in the increasing order
2166 of DF_REF_COMPARE. */
2167 if (i == count - 1)
c2569604 2168 return;
b512946c 2169 ref_vec->qsort (df_ref_ptr_compare);
6fb5fa3c
DB
2170 }
2171
2172 for (i=0; i<count-dist; i++)
2173 {
2174 /* Find the next ref that is not equal to the current ref. */
c2569604 2175 while (i + dist + 1 < count
9771b263
DN
2176 && df_ref_equal_p ((*ref_vec)[i],
2177 (*ref_vec)[i + dist + 1]))
6fb5fa3c 2178 {
9771b263 2179 df_free_ref ((*ref_vec)[i + dist + 1]);
6fb5fa3c
DB
2180 dist++;
2181 }
2182 /* Copy it down to the next position. */
c2569604 2183 if (dist && i + dist + 1 < count)
9771b263 2184 (*ref_vec)[i + 1] = (*ref_vec)[i + dist + 1];
6fb5fa3c
DB
2185 }
2186
2187 count -= dist;
9771b263 2188 ref_vec->truncate (count);
6fb5fa3c
DB
2189}
2190
2191
b8698a0f 2192/* Return true if the contents of two df_ref's are identical.
6fb5fa3c
DB
2193 It ignores DF_REF_MARKER. */
2194
2195static bool
2196df_mw_equal_p (struct df_mw_hardreg *mw1, struct df_mw_hardreg *mw2)
2197{
2198 if (!mw2)
2199 return false;
2200 return (mw1 == mw2) ||
2201 (mw1->mw_reg == mw2->mw_reg
2202 && mw1->type == mw2->type
2203 && mw1->flags == mw2->flags
2204 && mw1->start_regno == mw2->start_regno
2205 && mw1->end_regno == mw2->end_regno);
2206}
2207
2208
2209/* Compare MW1 and MW2 for sorting. */
2210
2211static int
b512946c 2212df_mw_compare (const df_mw_hardreg *mw1, const df_mw_hardreg *mw2)
6fb5fa3c 2213{
6fb5fa3c
DB
2214 if (mw1->type != mw2->type)
2215 return mw1->type - mw2->type;
2216
2217 if (mw1->flags != mw2->flags)
2218 return mw1->flags - mw2->flags;
2219
2220 if (mw1->start_regno != mw2->start_regno)
2221 return mw1->start_regno - mw2->start_regno;
2222
2223 if (mw1->end_regno != mw2->end_regno)
2224 return mw1->end_regno - mw2->end_regno;
2225
2226 if (mw1->mw_reg != mw2->mw_reg)
2227 return mw1->mw_order - mw2->mw_order;
2228
2229 return 0;
2230}
2231
b512946c
RS
2232/* Like df_mw_compare, but compare two df_mw_hardreg** pointers R1 and R2. */
2233
2234static int
2235df_mw_ptr_compare (const void *m1, const void *m2)
2236{
2237 return df_mw_compare (*(const df_mw_hardreg *const *) m1,
2238 *(const df_mw_hardreg *const *) m2);
2239}
6fb5fa3c
DB
2240
2241/* Sort and compress a set of refs. */
2242
c2569604 2243static void
ff4c81cc 2244df_sort_and_compress_mws (vec<df_mw_hardreg_ptr, va_heap> *mw_vec)
6fb5fa3c 2245{
c2569604 2246 unsigned int count;
b8698a0f 2247 struct df_scan_problem_data *problem_data
6fb5fa3c
DB
2248 = (struct df_scan_problem_data *) df_scan->problem_data;
2249 unsigned int i;
2250 unsigned int dist = 0;
6fb5fa3c 2251
9771b263 2252 count = mw_vec->length ();
6fb5fa3c 2253 if (count < 2)
c2569604 2254 return;
6fb5fa3c
DB
2255 else if (count == 2)
2256 {
9771b263
DN
2257 struct df_mw_hardreg *m0 = (*mw_vec)[0];
2258 struct df_mw_hardreg *m1 = (*mw_vec)[1];
b512946c 2259 if (df_mw_compare (m0, m1) > 0)
6fb5fa3c 2260 {
9771b263
DN
2261 struct df_mw_hardreg *tmp = (*mw_vec)[0];
2262 (*mw_vec)[0] = (*mw_vec)[1];
2263 (*mw_vec)[1] = tmp;
6fb5fa3c
DB
2264 }
2265 }
2266 else
b512946c 2267 mw_vec->qsort (df_mw_ptr_compare);
6fb5fa3c
DB
2268
2269 for (i=0; i<count-dist; i++)
2270 {
2271 /* Find the next ref that is not equal to the current ref. */
c2569604 2272 while (i + dist + 1 < count
9771b263 2273 && df_mw_equal_p ((*mw_vec)[i], (*mw_vec)[i + dist + 1]))
6fb5fa3c 2274 {
e956943e 2275 problem_data->mw_reg_pool->remove ((*mw_vec)[i + dist + 1]);
6fb5fa3c
DB
2276 dist++;
2277 }
2278 /* Copy it down to the next position. */
c2569604 2279 if (dist && i + dist + 1 < count)
9771b263 2280 (*mw_vec)[i + 1] = (*mw_vec)[i + dist + 1];
6fb5fa3c
DB
2281 }
2282
2283 count -= dist;
9771b263 2284 mw_vec->truncate (count);
6fb5fa3c
DB
2285}
2286
2287
2288/* Sort and remove duplicates from the COLLECTION_REC. */
2289
2290static void
2291df_canonize_collection_rec (struct df_collection_rec *collection_rec)
2292{
c2569604
ILT
2293 df_sort_and_compress_refs (&collection_rec->def_vec);
2294 df_sort_and_compress_refs (&collection_rec->use_vec);
2295 df_sort_and_compress_refs (&collection_rec->eq_use_vec);
2296 df_sort_and_compress_mws (&collection_rec->mw_vec);
6fb5fa3c
DB
2297}
2298
2299
2300/* Add the new df_ref to appropriate reg_info/ref_info chains. */
2301
2302static void
b8698a0f
L
2303df_install_ref (df_ref this_ref,
2304 struct df_reg_info *reg_info,
6fb5fa3c
DB
2305 struct df_ref_info *ref_info,
2306 bool add_to_table)
2307{
2308 unsigned int regno = DF_REF_REGNO (this_ref);
2309 /* Add the ref to the reg_{def,use,eq_use} chain. */
57512f53 2310 df_ref head = reg_info->reg_chain;
6fb5fa3c
DB
2311
2312 reg_info->reg_chain = this_ref;
2313 reg_info->n_refs++;
2314
2315 if (DF_REF_FLAGS_IS_SET (this_ref, DF_HARD_REG_LIVE))
2316 {
2317 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
2318 df->hard_regs_live_count[regno]++;
2319 }
2320
7a40b8b1
JH
2321 gcc_checking_assert (DF_REF_NEXT_REG (this_ref) == NULL
2322 && DF_REF_PREV_REG (this_ref) == NULL);
6fb5fa3c
DB
2323
2324 DF_REF_NEXT_REG (this_ref) = head;
2325
2326 /* We cannot actually link to the head of the chain. */
2327 DF_REF_PREV_REG (this_ref) = NULL;
2328
2329 if (head)
2330 DF_REF_PREV_REG (head) = this_ref;
b8698a0f 2331
6fb5fa3c
DB
2332 if (add_to_table)
2333 {
2334 gcc_assert (ref_info->ref_order != DF_REF_ORDER_NO_TABLE);
2335 df_check_and_grow_ref_info (ref_info, 1);
2336 DF_REF_ID (this_ref) = ref_info->table_size;
2337 /* Add the ref to the big array of defs. */
2338 ref_info->refs[ref_info->table_size] = this_ref;
2339 ref_info->table_size++;
b8698a0f 2340 }
6fb5fa3c
DB
2341 else
2342 DF_REF_ID (this_ref) = -1;
b8698a0f 2343
6fb5fa3c
DB
2344 ref_info->total_size++;
2345}
2346
2347
2348/* This function takes one of the groups of refs (defs, uses or
2349 eq_uses) and installs the entire group into the insn. It also adds
2350 each of these refs into the appropriate chains. */
2351
b512946c 2352static df_ref
6fb5fa3c 2353df_install_refs (basic_block bb,
ff4c81cc 2354 const vec<df_ref, va_heap> *old_vec,
b8698a0f 2355 struct df_reg_info **reg_info,
6fb5fa3c
DB
2356 struct df_ref_info *ref_info,
2357 bool is_notes)
2358{
ff4c81cc 2359 unsigned int count = old_vec->length ();
6fb5fa3c
DB
2360 if (count)
2361 {
6fb5fa3c 2362 bool add_to_table;
c2569604
ILT
2363 df_ref this_ref;
2364 unsigned int ix;
6fb5fa3c
DB
2365
2366 switch (ref_info->ref_order)
2367 {
2368 case DF_REF_ORDER_UNORDERED_WITH_NOTES:
2369 case DF_REF_ORDER_BY_REG_WITH_NOTES:
2370 case DF_REF_ORDER_BY_INSN_WITH_NOTES:
2371 ref_info->ref_order = DF_REF_ORDER_UNORDERED_WITH_NOTES;
2372 add_to_table = true;
2373 break;
2374 case DF_REF_ORDER_UNORDERED:
2375 case DF_REF_ORDER_BY_REG:
2376 case DF_REF_ORDER_BY_INSN:
2377 ref_info->ref_order = DF_REF_ORDER_UNORDERED;
2378 add_to_table = !is_notes;
2379 break;
2380 default:
2381 add_to_table = false;
2382 break;
2383 }
2384
2385 /* Do not add if ref is not in the right blocks. */
2386 if (add_to_table && df->analyze_subset)
2387 add_to_table = bitmap_bit_p (df->blocks_to_analyze, bb->index);
2388
ff4c81cc 2389 FOR_EACH_VEC_ELT (*old_vec, ix, this_ref)
6fb5fa3c 2390 {
b512946c
RS
2391 DF_REF_NEXT_LOC (this_ref) = (ix + 1 < old_vec->length ()
2392 ? (*old_vec)[ix + 1]
2393 : NULL);
b8698a0f 2394 df_install_ref (this_ref, reg_info[DF_REF_REGNO (this_ref)],
6fb5fa3c
DB
2395 ref_info, add_to_table);
2396 }
b512946c 2397 return (*old_vec)[0];
6fb5fa3c
DB
2398 }
2399 else
b512946c 2400 return 0;
6fb5fa3c
DB
2401}
2402
2403
2404/* This function takes the mws installs the entire group into the
2405 insn. */
2406
b512946c 2407static struct df_mw_hardreg *
ff4c81cc 2408df_install_mws (const vec<df_mw_hardreg_ptr, va_heap> *old_vec)
6fb5fa3c 2409{
ff4c81cc 2410 unsigned int count = old_vec->length ();
6fb5fa3c
DB
2411 if (count)
2412 {
b512946c
RS
2413 for (unsigned int i = 0; i < count - 1; i++)
2414 DF_MWS_NEXT ((*old_vec)[i]) = (*old_vec)[i + 1];
2415 DF_MWS_NEXT ((*old_vec)[count - 1]) = 0;
2416 return (*old_vec)[0];
6fb5fa3c
DB
2417 }
2418 else
b512946c 2419 return 0;
6fb5fa3c
DB
2420}
2421
2422
2423/* Add a chain of df_refs to appropriate ref chain/reg_info/ref_info
2424 chains and update other necessary information. */
2425
2426static void
b8698a0f 2427df_refs_add_to_chains (struct df_collection_rec *collection_rec,
b2908ba6 2428 basic_block bb, rtx_insn *insn, unsigned int flags)
6fb5fa3c
DB
2429{
2430 if (insn)
2431 {
50e94c7e 2432 struct df_insn_info *insn_rec = DF_INSN_INFO_GET (insn);
6fb5fa3c
DB
2433 /* If there is a vector in the collection rec, add it to the
2434 insn. A null rec is a signal that the caller will handle the
2435 chain specially. */
ff4c81cc 2436 if (flags & copy_defs)
6fb5fa3c 2437 {
b512946c 2438 gcc_checking_assert (!insn_rec->defs);
b8698a0f 2439 insn_rec->defs
ff4c81cc 2440 = df_install_refs (bb, &collection_rec->def_vec,
6fb5fa3c
DB
2441 df->def_regs,
2442 &df->def_info, false);
2443 }
ff4c81cc 2444 if (flags & copy_uses)
6fb5fa3c 2445 {
b512946c 2446 gcc_checking_assert (!insn_rec->uses);
b8698a0f 2447 insn_rec->uses
ff4c81cc 2448 = df_install_refs (bb, &collection_rec->use_vec,
6fb5fa3c
DB
2449 df->use_regs,
2450 &df->use_info, false);
2451 }
ff4c81cc 2452 if (flags & copy_eq_uses)
6fb5fa3c 2453 {
b512946c 2454 gcc_checking_assert (!insn_rec->eq_uses);
b8698a0f 2455 insn_rec->eq_uses
ff4c81cc 2456 = df_install_refs (bb, &collection_rec->eq_use_vec,
6fb5fa3c
DB
2457 df->eq_use_regs,
2458 &df->use_info, true);
2459 }
ff4c81cc 2460 if (flags & copy_mw)
6fb5fa3c 2461 {
b512946c 2462 gcc_checking_assert (!insn_rec->mw_hardregs);
b8698a0f 2463 insn_rec->mw_hardregs
ff4c81cc 2464 = df_install_mws (&collection_rec->mw_vec);
6fb5fa3c
DB
2465 }
2466 }
2467 else
2468 {
2469 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (bb->index);
2470
b512946c 2471 gcc_checking_assert (!bb_info->artificial_defs);
b8698a0f 2472 bb_info->artificial_defs
ff4c81cc 2473 = df_install_refs (bb, &collection_rec->def_vec,
6fb5fa3c
DB
2474 df->def_regs,
2475 &df->def_info, false);
b512946c 2476 gcc_checking_assert (!bb_info->artificial_uses);
b8698a0f 2477 bb_info->artificial_uses
ff4c81cc 2478 = df_install_refs (bb, &collection_rec->use_vec,
6fb5fa3c
DB
2479 df->use_regs,
2480 &df->use_info, false);
2481 }
2482}
4d779342 2483
4d779342 2484
502ef838 2485/* Allocate a ref and initialize its fields. */
6fb5fa3c 2486
b8698a0f
L
2487static df_ref
2488df_ref_create_structure (enum df_ref_class cl,
57512f53 2489 struct df_collection_rec *collection_rec,
b8698a0f 2490 rtx reg, rtx *loc,
50e94c7e 2491 basic_block bb, struct df_insn_info *info,
b8698a0f 2492 enum df_ref_type ref_type,
502ef838 2493 int ref_flags)
6fb5fa3c 2494{
57512f53 2495 df_ref this_ref = NULL;
6fb5fa3c
DB
2496 int regno = REGNO (GET_CODE (reg) == SUBREG ? SUBREG_REG (reg) : reg);
2497 struct df_scan_problem_data *problem_data
2498 = (struct df_scan_problem_data *) df_scan->problem_data;
2499
57512f53 2500 switch (cl)
ca9052ce 2501 {
57512f53 2502 case DF_REF_BASE:
e956943e 2503 this_ref = (df_ref) (problem_data->ref_base_pool->allocate ());
7a40b8b1 2504 gcc_checking_assert (loc == NULL);
57512f53
KZ
2505 break;
2506
2507 case DF_REF_ARTIFICIAL:
e956943e 2508 this_ref = (df_ref) (problem_data->ref_artificial_pool->allocate ());
57512f53 2509 this_ref->artificial_ref.bb = bb;
7a40b8b1 2510 gcc_checking_assert (loc == NULL);
57512f53
KZ
2511 break;
2512
2513 case DF_REF_REGULAR:
e956943e 2514 this_ref = (df_ref) (problem_data->ref_regular_pool->allocate ());
57512f53 2515 this_ref->regular_ref.loc = loc;
7a40b8b1 2516 gcc_checking_assert (loc);
57512f53 2517 break;
ca9052ce 2518 }
57512f53
KZ
2519
2520 DF_REF_CLASS (this_ref) = cl;
6fb5fa3c
DB
2521 DF_REF_ID (this_ref) = -1;
2522 DF_REF_REG (this_ref) = reg;
2523 DF_REF_REGNO (this_ref) = regno;
57512f53 2524 DF_REF_TYPE (this_ref) = ref_type;
50e94c7e 2525 DF_REF_INSN_INFO (this_ref) = info;
6fb5fa3c 2526 DF_REF_CHAIN (this_ref) = NULL;
6fb5fa3c 2527 DF_REF_FLAGS (this_ref) = ref_flags;
6fb5fa3c
DB
2528 DF_REF_NEXT_REG (this_ref) = NULL;
2529 DF_REF_PREV_REG (this_ref) = NULL;
2530 DF_REF_ORDER (this_ref) = df->ref_order++;
2531
2532 /* We need to clear this bit because fwprop, and in the future
2533 possibly other optimizations sometimes create new refs using ond
2534 refs as the model. */
2535 DF_REF_FLAGS_CLEAR (this_ref, DF_HARD_REG_LIVE);
2536
2537 /* See if this ref needs to have DF_HARD_REG_LIVE bit set. */
bf743fc4
JJ
2538 if (regno < FIRST_PSEUDO_REGISTER
2539 && !DF_REF_IS_ARTIFICIAL (this_ref)
2540 && !DEBUG_INSN_P (DF_REF_INSN (this_ref)))
6fb5fa3c 2541 {
57512f53 2542 if (DF_REF_REG_DEF_P (this_ref))
6fb5fa3c
DB
2543 {
2544 if (!DF_REF_FLAGS_IS_SET (this_ref, DF_REF_MAY_CLOBBER))
2545 DF_REF_FLAGS_SET (this_ref, DF_HARD_REG_LIVE);
2546 }
2547 else if (!(TEST_HARD_REG_BIT (elim_reg_set, regno)
2548 && (regno == FRAME_POINTER_REGNUM
2549 || regno == ARG_POINTER_REGNUM)))
2550 DF_REF_FLAGS_SET (this_ref, DF_HARD_REG_LIVE);
2551 }
2552
2553 if (collection_rec)
2554 {
57512f53 2555 if (DF_REF_REG_DEF_P (this_ref))
9771b263 2556 collection_rec->def_vec.safe_push (this_ref);
6fb5fa3c 2557 else if (DF_REF_FLAGS (this_ref) & DF_REF_IN_NOTE)
9771b263 2558 collection_rec->eq_use_vec.safe_push (this_ref);
6fb5fa3c 2559 else
9771b263 2560 collection_rec->use_vec.safe_push (this_ref);
4d779342 2561 }
dc007c1f
PB
2562 else
2563 df_install_ref_incremental (this_ref);
6fb5fa3c 2564
4d779342
DB
2565 return this_ref;
2566}
2567
2568
2569/* Create new references of type DF_REF_TYPE for each part of register REG
502ef838 2570 at address LOC within INSN of BB. */
ca9052ce 2571
4d779342
DB
2572
2573static void
b8698a0f 2574df_ref_record (enum df_ref_class cl,
57512f53 2575 struct df_collection_rec *collection_rec,
b8698a0f 2576 rtx reg, rtx *loc,
50e94c7e 2577 basic_block bb, struct df_insn_info *insn_info,
b8698a0f 2578 enum df_ref_type ref_type,
502ef838 2579 int ref_flags)
4d779342 2580{
23249ac4 2581 unsigned int regno;
4d779342 2582
7a40b8b1 2583 gcc_checking_assert (REG_P (reg) || GET_CODE (reg) == SUBREG);
4d779342 2584
4d779342
DB
2585 regno = REGNO (GET_CODE (reg) == SUBREG ? SUBREG_REG (reg) : reg);
2586 if (regno < FIRST_PSEUDO_REGISTER)
2587 {
23249ac4
DB
2588 struct df_mw_hardreg *hardreg = NULL;
2589 struct df_scan_problem_data *problem_data
6fb5fa3c
DB
2590 = (struct df_scan_problem_data *) df_scan->problem_data;
2591 unsigned int i;
2592 unsigned int endregno;
57512f53 2593 df_ref ref;
4d779342 2594
4d779342 2595 if (GET_CODE (reg) == SUBREG)
f1f4e530
JM
2596 {
2597 regno += subreg_regno_offset (regno, GET_MODE (SUBREG_REG (reg)),
2598 SUBREG_BYTE (reg), GET_MODE (reg));
09e18274 2599 endregno = regno + subreg_nregs (reg);
f1f4e530
JM
2600 }
2601 else
72d19505 2602 endregno = END_REGNO (reg);
4d779342 2603
6fb5fa3c
DB
2604 /* If this is a multiword hardreg, we create some extra
2605 datastructures that will enable us to easily build REG_DEAD
2606 and REG_UNUSED notes. */
dc007c1f
PB
2607 if (collection_rec
2608 && (endregno != regno + 1) && insn_info)
23249ac4 2609 {
b8698a0f 2610 /* Sets to a subreg of a multiword register are partial.
23249ac4 2611 Sets to a non-subreg of a multiword register are not. */
6f5c1520 2612 if (GET_CODE (reg) == SUBREG)
23249ac4
DB
2613 ref_flags |= DF_REF_PARTIAL;
2614 ref_flags |= DF_REF_MW_HARDREG;
6fb5fa3c 2615
e956943e 2616 hardreg = problem_data->mw_reg_pool->allocate ();
23249ac4
DB
2617 hardreg->type = ref_type;
2618 hardreg->flags = ref_flags;
2619 hardreg->mw_reg = reg;
6fb5fa3c
DB
2620 hardreg->start_regno = regno;
2621 hardreg->end_regno = endregno - 1;
2622 hardreg->mw_order = df->ref_order++;
9771b263 2623 collection_rec->mw_vec.safe_push (hardreg);
23249ac4
DB
2624 }
2625
4d779342
DB
2626 for (i = regno; i < endregno; i++)
2627 {
b8698a0f 2628 ref = df_ref_create_structure (cl, collection_rec, regno_reg_rtx[i], loc,
502ef838 2629 bb, insn_info, ref_type, ref_flags);
23249ac4 2630
6fb5fa3c 2631 gcc_assert (ORIGINAL_REGNO (DF_REF_REG (ref)) == i);
4d779342
DB
2632 }
2633 }
2634 else
2635 {
b8698a0f 2636 df_ref_create_structure (cl, collection_rec, reg, loc, bb, insn_info,
502ef838 2637 ref_type, ref_flags);
4d779342
DB
2638 }
2639}
2640
2641
2642/* A set to a non-paradoxical SUBREG for which the number of word_mode units
2643 covered by the outer mode is smaller than that covered by the inner mode,
2644 is a read-modify-write operation.
2645 This function returns true iff the SUBREG X is such a SUBREG. */
2646
2647bool
2648df_read_modify_subreg_p (rtx x)
2649{
2650 unsigned int isize, osize;
2651 if (GET_CODE (x) != SUBREG)
2652 return false;
2653 isize = GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)));
2654 osize = GET_MODE_SIZE (GET_MODE (x));
7bb3ae2f
RZ
2655 return isize > osize
2656 && isize > REGMODE_NATURAL_SIZE (GET_MODE (SUBREG_REG (x)));
4d779342
DB
2657}
2658
2659
c5ebdc25
DA
2660/* Process all the registers defined in the rtx pointed by LOC.
2661 Autoincrement/decrement definitions will be picked up by df_uses_record.
2662 Any change here has to be matched in df_find_hard_reg_defs_1. */
4d779342
DB
2663
2664static void
6fb5fa3c 2665df_def_record_1 (struct df_collection_rec *collection_rec,
c5ebdc25 2666 rtx *loc, basic_block bb, struct df_insn_info *insn_info,
bbbbb16a 2667 int flags)
4d779342 2668{
c5ebdc25 2669 rtx dst = *loc;
4d779342 2670
b5642e20
DB
2671 /* It is legal to have a set destination be a parallel. */
2672 if (GET_CODE (dst) == PARALLEL)
4d779342
DB
2673 {
2674 int i;
4d779342
DB
2675 for (i = XVECLEN (dst, 0) - 1; i >= 0; i--)
2676 {
2677 rtx temp = XVECEXP (dst, 0, i);
c5ebdc25
DA
2678 gcc_assert (GET_CODE (temp) == EXPR_LIST);
2679 df_def_record_1 (collection_rec, &XEXP (temp, 0),
2680 bb, insn_info, flags);
4d779342
DB
2681 }
2682 return;
2683 }
2684
ca9052ce 2685 if (GET_CODE (dst) == STRICT_LOW_PART)
4d779342 2686 {
ca9052ce
KZ
2687 flags |= DF_REF_READ_WRITE | DF_REF_PARTIAL | DF_REF_STRICT_LOW_PART;
2688
2689 loc = &XEXP (dst, 0);
2690 dst = *loc;
2691 }
2692
2693 if (GET_CODE (dst) == ZERO_EXTRACT)
2694 {
2695 flags |= DF_REF_READ_WRITE | DF_REF_PARTIAL | DF_REF_ZERO_EXTRACT;
b8698a0f 2696
4d779342
DB
2697 loc = &XEXP (dst, 0);
2698 dst = *loc;
4d779342
DB
2699 }
2700
ba49cb7b
KZ
2701 /* At this point if we do not have a reg or a subreg, just return. */
2702 if (REG_P (dst))
2703 {
502ef838
PB
2704 df_ref_record (DF_REF_REGULAR, collection_rec,
2705 dst, loc, bb, insn_info, DF_REF_REG_DEF, flags);
ba49cb7b
KZ
2706
2707 /* We want to keep sp alive everywhere - by making all
2708 writes to sp also use of sp. */
2709 if (REGNO (dst) == STACK_POINTER_REGNUM)
57512f53 2710 df_ref_record (DF_REF_BASE, collection_rec,
502ef838 2711 dst, NULL, bb, insn_info, DF_REF_REG_USE, flags);
ba49cb7b
KZ
2712 }
2713 else if (GET_CODE (dst) == SUBREG && REG_P (SUBREG_REG (dst)))
2714 {
2715 if (df_read_modify_subreg_p (dst))
2716 flags |= DF_REF_READ_WRITE | DF_REF_PARTIAL;
7bb3ae2f 2717
ba49cb7b 2718 flags |= DF_REF_SUBREG;
47220f03 2719
502ef838
PB
2720 df_ref_record (DF_REF_REGULAR, collection_rec,
2721 dst, loc, bb, insn_info, DF_REF_REG_DEF, flags);
ba49cb7b 2722 }
4d779342
DB
2723}
2724
2725
c5ebdc25
DA
2726/* Process all the registers defined in the pattern rtx, X. Any change
2727 here has to be matched in df_find_hard_reg_defs. */
4d779342
DB
2728
2729static void
b8698a0f 2730df_defs_record (struct df_collection_rec *collection_rec,
50e94c7e 2731 rtx x, basic_block bb, struct df_insn_info *insn_info,
bbbbb16a 2732 int flags)
4d779342
DB
2733{
2734 RTX_CODE code = GET_CODE (x);
c5ebdc25 2735 int i;
4d779342 2736
c5ebdc25 2737 switch (code)
4d779342 2738 {
c5ebdc25
DA
2739 case SET:
2740 df_def_record_1 (collection_rec, &SET_DEST (x), bb, insn_info, flags);
2741 break;
2742
2743 case CLOBBER:
2744 flags |= DF_REF_MUST_CLOBBER;
2745 df_def_record_1 (collection_rec, &XEXP (x, 0), bb, insn_info, flags);
2746 break;
2747
2748 case COND_EXEC:
b8698a0f 2749 df_defs_record (collection_rec, COND_EXEC_CODE (x),
50e94c7e 2750 bb, insn_info, DF_REF_CONDITIONAL);
c5ebdc25
DA
2751 break;
2752
2753 case PARALLEL:
2754 for (i = 0; i < XVECLEN (x, 0); i++)
2755 df_defs_record (collection_rec, XVECEXP (x, 0, i),
2756 bb, insn_info, flags);
2757 break;
2758 default:
2759 /* No DEFs to record in other cases */
2760 break;
4d779342 2761 }
c5ebdc25
DA
2762}
2763
2764/* Set bits in *DEFS for hard registers found in the rtx DST, which is the
2765 destination of a set or clobber. This has to match the logic in
2766 df_defs_record_1. */
2767
2768static void
2769df_find_hard_reg_defs_1 (rtx dst, HARD_REG_SET *defs)
2770{
2771 /* It is legal to have a set destination be a parallel. */
2772 if (GET_CODE (dst) == PARALLEL)
4d779342
DB
2773 {
2774 int i;
c5ebdc25
DA
2775 for (i = XVECLEN (dst, 0) - 1; i >= 0; i--)
2776 {
2777 rtx temp = XVECEXP (dst, 0, i);
2778 gcc_assert (GET_CODE (temp) == EXPR_LIST);
2779 df_find_hard_reg_defs_1 (XEXP (temp, 0), defs);
2780 }
2781 return;
2782 }
2783
2784 if (GET_CODE (dst) == STRICT_LOW_PART)
2785 dst = XEXP (dst, 0);
2786
2787 if (GET_CODE (dst) == ZERO_EXTRACT)
2788 dst = XEXP (dst, 0);
2789
2790 /* At this point if we do not have a reg or a subreg, just return. */
2791 if (REG_P (dst) && HARD_REGISTER_P (dst))
2792 SET_HARD_REG_BIT (*defs, REGNO (dst));
2793 else if (GET_CODE (dst) == SUBREG
2794 && REG_P (SUBREG_REG (dst)) && HARD_REGISTER_P (dst))
2795 SET_HARD_REG_BIT (*defs, REGNO (SUBREG_REG (dst)));
2796}
2797
2798/* Set bits in *DEFS for hard registers defined in the pattern X. This
2799 has to match the logic in df_defs_record. */
4d779342 2800
c5ebdc25
DA
2801static void
2802df_find_hard_reg_defs (rtx x, HARD_REG_SET *defs)
2803{
2804 RTX_CODE code = GET_CODE (x);
2805 int i;
2806
2807 switch (code)
2808 {
2809 case SET:
2810 df_find_hard_reg_defs_1 (SET_DEST (x), defs);
2811 break;
2812
2813 case CLOBBER:
2814 df_find_hard_reg_defs_1 (XEXP (x, 0), defs);
2815 break;
2816
2817 case COND_EXEC:
2818 df_find_hard_reg_defs (COND_EXEC_CODE (x), defs);
2819 break;
2820
2821 case PARALLEL:
2822 for (i = 0; i < XVECLEN (x, 0); i++)
2823 df_find_hard_reg_defs (XVECEXP (x, 0, i), defs);
2824 break;
2825 default:
2826 /* No DEFs to record in other cases */
2827 break;
4d779342
DB
2828 }
2829}
2830
2831
502ef838 2832/* Process all the registers used in the rtx at address LOC. */
4d779342
DB
2833
2834static void
502ef838 2835df_uses_record (struct df_collection_rec *collection_rec,
6fb5fa3c 2836 rtx *loc, enum df_ref_type ref_type,
50e94c7e 2837 basic_block bb, struct df_insn_info *insn_info,
502ef838 2838 int flags)
4d779342
DB
2839{
2840 RTX_CODE code;
2841 rtx x;
6fb5fa3c 2842
4d779342
DB
2843 retry:
2844 x = *loc;
2845 if (!x)
2846 return;
2847 code = GET_CODE (x);
2848 switch (code)
2849 {
2850 case LABEL_REF:
2851 case SYMBOL_REF:
4d779342 2852 case CONST:
d8116890 2853 CASE_CONST_ANY:
4d779342
DB
2854 case PC:
2855 case CC0:
2856 case ADDR_VEC:
2857 case ADDR_DIFF_VEC:
2858 return;
2859
2860 case CLOBBER:
2861 /* If we are clobbering a MEM, mark any registers inside the address
2862 as being used. */
2863 if (MEM_P (XEXP (x, 0)))
502ef838 2864 df_uses_record (collection_rec,
6fb5fa3c 2865 &XEXP (XEXP (x, 0), 0),
50e94c7e
SB
2866 DF_REF_REG_MEM_STORE,
2867 bb, insn_info,
502ef838 2868 flags);
4d779342
DB
2869
2870 /* If we're clobbering a REG then we have a def so ignore. */
2871 return;
2872
2873 case MEM:
502ef838 2874 df_uses_record (collection_rec,
b8698a0f 2875 &XEXP (x, 0), DF_REF_REG_MEM_LOAD,
502ef838 2876 bb, insn_info, flags & DF_REF_IN_NOTE);
4d779342
DB
2877 return;
2878
2879 case SUBREG:
2880 /* While we're here, optimize this case. */
23249ac4 2881 flags |= DF_REF_PARTIAL;
4d779342
DB
2882 /* In case the SUBREG is not of a REG, do not optimize. */
2883 if (!REG_P (SUBREG_REG (x)))
2884 {
2885 loc = &SUBREG_REG (x);
502ef838 2886 df_uses_record (collection_rec, loc, ref_type, bb, insn_info, flags);
4d779342
DB
2887 return;
2888 }
2889 /* ... Fall through ... */
2890
2891 case REG:
502ef838 2892 df_ref_record (DF_REF_REGULAR, collection_rec,
50e94c7e 2893 x, loc, bb, insn_info,
502ef838 2894 ref_type, flags);
4d779342
DB
2895 return;
2896
ca9052ce
KZ
2897 case SIGN_EXTRACT:
2898 case ZERO_EXTRACT:
2899 {
502ef838
PB
2900 df_uses_record (collection_rec,
2901 &XEXP (x, 1), ref_type, bb, insn_info, flags);
2902 df_uses_record (collection_rec,
2903 &XEXP (x, 2), ref_type, bb, insn_info, flags);
2904
2905 /* If the parameters to the zero or sign extract are
2906 constants, strip them off and recurse, otherwise there is
2907 no information that we can gain from this operation. */
2908 if (code == ZERO_EXTRACT)
2909 flags |= DF_REF_ZERO_EXTRACT;
2910 else
2911 flags |= DF_REF_SIGN_EXTRACT;
2912
2913 df_uses_record (collection_rec,
2914 &XEXP (x, 0), ref_type, bb, insn_info, flags);
2915 return;
ca9052ce
KZ
2916 }
2917 break;
2918
4d779342
DB
2919 case SET:
2920 {
2921 rtx dst = SET_DEST (x);
2922 gcc_assert (!(flags & DF_REF_IN_NOTE));
502ef838
PB
2923 df_uses_record (collection_rec,
2924 &SET_SRC (x), DF_REF_REG_USE, bb, insn_info, flags);
4d779342
DB
2925
2926 switch (GET_CODE (dst))
2927 {
2928 case SUBREG:
2929 if (df_read_modify_subreg_p (dst))
2930 {
502ef838 2931 df_uses_record (collection_rec, &SUBREG_REG (dst),
b8698a0f 2932 DF_REF_REG_USE, bb, insn_info,
502ef838 2933 flags | DF_REF_READ_WRITE | DF_REF_SUBREG);
4d779342
DB
2934 break;
2935 }
2936 /* Fall through. */
2937 case REG:
2938 case PARALLEL:
2939 case SCRATCH:
2940 case PC:
2941 case CC0:
2942 break;
2943 case MEM:
502ef838
PB
2944 df_uses_record (collection_rec, &XEXP (dst, 0),
2945 DF_REF_REG_MEM_STORE, bb, insn_info, flags);
4d779342
DB
2946 break;
2947 case STRICT_LOW_PART:
2948 {
2949 rtx *temp = &XEXP (dst, 0);
2950 /* A strict_low_part uses the whole REG and not just the
2951 SUBREG. */
2952 dst = XEXP (dst, 0);
502ef838 2953 df_uses_record (collection_rec,
b8698a0f 2954 (GET_CODE (dst) == SUBREG) ? &SUBREG_REG (dst) : temp,
50e94c7e 2955 DF_REF_REG_USE, bb, insn_info,
502ef838 2956 DF_REF_READ_WRITE | DF_REF_STRICT_LOW_PART);
4d779342
DB
2957 }
2958 break;
2959 case ZERO_EXTRACT:
ca9052ce 2960 {
502ef838
PB
2961 df_uses_record (collection_rec, &XEXP (dst, 1),
2962 DF_REF_REG_USE, bb, insn_info, flags);
2963 df_uses_record (collection_rec, &XEXP (dst, 2),
2964 DF_REF_REG_USE, bb, insn_info, flags);
2965 if (GET_CODE (XEXP (dst,0)) == MEM)
2966 df_uses_record (collection_rec, &XEXP (dst, 0),
2967 DF_REF_REG_USE, bb, insn_info,
2968 flags);
2969 else
2970 df_uses_record (collection_rec, &XEXP (dst, 0),
2971 DF_REF_REG_USE, bb, insn_info,
2972 DF_REF_READ_WRITE | DF_REF_ZERO_EXTRACT);
ca9052ce 2973 }
4d779342 2974 break;
ca9052ce 2975
4d779342
DB
2976 default:
2977 gcc_unreachable ();
2978 }
2979 return;
2980 }
2981
2982 case RETURN:
26898771 2983 case SIMPLE_RETURN:
4d779342
DB
2984 break;
2985
2986 case ASM_OPERANDS:
2987 case UNSPEC_VOLATILE:
2988 case TRAP_IF:
2989 case ASM_INPUT:
2990 {
2991 /* Traditional and volatile asm instructions must be
2992 considered to use and clobber all hard registers, all
2993 pseudo-registers and all of memory. So must TRAP_IF and
2994 UNSPEC_VOLATILE operations.
2995
2996 Consider for instance a volatile asm that changes the fpu
2997 rounding mode. An insn should not be moved across this
2998 even if it only uses pseudo-regs because it might give an
2999 incorrectly rounded result.
3000
3001 However, flow.c's liveness computation did *not* do this,
3002 giving the reasoning as " ?!? Unfortunately, marking all
3003 hard registers as live causes massive problems for the
3004 register allocator and marking all pseudos as live creates
3005 mountains of uninitialized variable warnings."
3006
3007 In order to maintain the status quo with regard to liveness
3008 and uses, we do what flow.c did and just mark any regs we
6fb5fa3c
DB
3009 can find in ASM_OPERANDS as used. In global asm insns are
3010 scanned and regs_asm_clobbered is filled out.
4d779342
DB
3011
3012 For all ASM_OPERANDS, we must traverse the vector of input
3013 operands. We can not just fall through here since then we
3014 would be confused by the ASM_INPUT rtx inside ASM_OPERANDS,
3015 which do not indicate traditional asms unlike their normal
3016 usage. */
3017 if (code == ASM_OPERANDS)
3018 {
3019 int j;
3020
3021 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
502ef838
PB
3022 df_uses_record (collection_rec, &ASM_OPERANDS_INPUT (x, j),
3023 DF_REF_REG_USE, bb, insn_info, flags);
4d779342
DB
3024 return;
3025 }
3026 break;
3027 }
3028
b5b8b0ac 3029 case VAR_LOCATION:
502ef838 3030 df_uses_record (collection_rec,
b5b8b0ac 3031 &PAT_VAR_LOCATION_LOC (x),
502ef838 3032 DF_REF_REG_USE, bb, insn_info, flags);
b5b8b0ac
AO
3033 return;
3034
4d779342
DB
3035 case PRE_DEC:
3036 case POST_DEC:
3037 case PRE_INC:
3038 case POST_INC:
3039 case PRE_MODIFY:
3040 case POST_MODIFY:
b5b8b0ac 3041 gcc_assert (!DEBUG_INSN_P (insn_info->insn));
4d779342 3042 /* Catch the def of the register being modified. */
502ef838 3043 df_ref_record (DF_REF_REGULAR, collection_rec, XEXP (x, 0), &XEXP (x, 0),
b8698a0f 3044 bb, insn_info,
203927fc 3045 DF_REF_REG_DEF,
502ef838 3046 flags | DF_REF_READ_WRITE | DF_REF_PRE_POST_MODIFY);
4d779342
DB
3047
3048 /* ... Fall through to handle uses ... */
3049
3050 default:
3051 break;
3052 }
3053
3054 /* Recursively scan the operands of this expression. */
3055 {
3056 const char *fmt = GET_RTX_FORMAT (code);
3057 int i;
3058
3059 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3060 {
3061 if (fmt[i] == 'e')
3062 {
3063 /* Tail recursive case: save a function call level. */
3064 if (i == 0)
3065 {
3066 loc = &XEXP (x, 0);
3067 goto retry;
3068 }
502ef838
PB
3069 df_uses_record (collection_rec, &XEXP (x, i), ref_type,
3070 bb, insn_info, flags);
4d779342
DB
3071 }
3072 else if (fmt[i] == 'E')
3073 {
3074 int j;
3075 for (j = 0; j < XVECLEN (x, i); j++)
502ef838 3076 df_uses_record (collection_rec,
b8698a0f 3077 &XVECEXP (x, i, j), ref_type,
502ef838 3078 bb, insn_info, flags);
4d779342
DB
3079 }
3080 }
3081 }
4d779342 3082
6fb5fa3c 3083 return;
4d779342
DB
3084}
3085
3086
6fb5fa3c 3087/* For all DF_REF_CONDITIONAL defs, add a corresponding uses. */
4d779342 3088
6fb5fa3c
DB
3089static void
3090df_get_conditional_uses (struct df_collection_rec *collection_rec)
4d779342 3091{
c2569604
ILT
3092 unsigned int ix;
3093 df_ref ref;
3094
9771b263 3095 FOR_EACH_VEC_ELT (collection_rec->def_vec, ix, ref)
6fb5fa3c 3096 {
6fb5fa3c
DB
3097 if (DF_REF_FLAGS_IS_SET (ref, DF_REF_CONDITIONAL))
3098 {
57512f53 3099 df_ref use;
ca9052ce 3100
57512f53 3101 use = df_ref_create_structure (DF_REF_CLASS (ref), collection_rec, DF_REF_REG (ref),
ca9052ce 3102 DF_REF_LOC (ref), DF_REF_BB (ref),
50e94c7e 3103 DF_REF_INSN_INFO (ref), DF_REF_REG_USE,
502ef838 3104 DF_REF_FLAGS (ref) & ~DF_REF_CONDITIONAL);
6fb5fa3c
DB
3105 DF_REF_REGNO (use) = DF_REF_REGNO (ref);
3106 }
3107 }
4d779342
DB
3108}
3109
3110
c5ebdc25 3111/* Get call's extra defs and uses (track caller-saved registers). */
4d779342
DB
3112
3113static void
c5ebdc25 3114df_get_call_refs (struct df_collection_rec *collection_rec,
b8698a0f 3115 basic_block bb,
50e94c7e 3116 struct df_insn_info *insn_info,
bbbbb16a 3117 int flags)
4d779342 3118{
6fb5fa3c 3119 rtx note;
6fb5fa3c
DB
3120 bool is_sibling_call;
3121 unsigned int i;
c5ebdc25 3122 HARD_REG_SET defs_generated;
c2ba7e7a 3123 HARD_REG_SET fn_reg_set_usage;
a7e3698d 3124
c5ebdc25
DA
3125 CLEAR_HARD_REG_SET (defs_generated);
3126 df_find_hard_reg_defs (PATTERN (insn_info->insn), &defs_generated);
3127 is_sibling_call = SIBLING_CALL_P (insn_info->insn);
c2ba7e7a
RO
3128 get_call_reg_set_usage (insn_info->insn, &fn_reg_set_usage,
3129 regs_invalidated_by_call);
4d779342 3130
c5ebdc25
DA
3131 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3132 {
3133 if (i == STACK_POINTER_REGNUM)
3134 /* The stack ptr is used (honorarily) by a CALL insn. */
3135 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[i],
3136 NULL, bb, insn_info, DF_REF_REG_USE,
3137 DF_REF_CALL_STACK_USAGE | flags);
3138 else if (global_regs[i])
3139 {
3140 /* Calls to const functions cannot access any global registers and
3141 calls to pure functions cannot set them. All other calls may
3142 reference any of the global registers, so they are recorded as
3143 used. */
3144 if (!RTL_CONST_CALL_P (insn_info->insn))
3145 {
3146 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[i],
3147 NULL, bb, insn_info, DF_REF_REG_USE, flags);
3148 if (!RTL_PURE_CALL_P (insn_info->insn))
3149 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[i],
3150 NULL, bb, insn_info, DF_REF_REG_DEF, flags);
3151 }
3152 }
c2ba7e7a 3153 else if (TEST_HARD_REG_BIT (fn_reg_set_usage, i)
c5ebdc25
DA
3154 /* no clobbers for regs that are the result of the call */
3155 && !TEST_HARD_REG_BIT (defs_generated, i)
3156 && (!is_sibling_call
3157 || !bitmap_bit_p (df->exit_block_uses, i)
c9bd6bcd 3158 || refers_to_regno_p (i, crtl->return_rtx)))
c5ebdc25
DA
3159 df_ref_record (DF_REF_BASE, collection_rec, regno_reg_rtx[i],
3160 NULL, bb, insn_info, DF_REF_REG_DEF,
3161 DF_REF_MAY_CLOBBER | flags);
3162 }
4d779342 3163
6fb5fa3c
DB
3164 /* Record the registers used to pass arguments, and explicitly
3165 noted as clobbered. */
50e94c7e 3166 for (note = CALL_INSN_FUNCTION_USAGE (insn_info->insn); note;
6fb5fa3c
DB
3167 note = XEXP (note, 1))
3168 {
3169 if (GET_CODE (XEXP (note, 0)) == USE)
502ef838
PB
3170 df_uses_record (collection_rec, &XEXP (XEXP (note, 0), 0),
3171 DF_REF_REG_USE, bb, insn_info, flags);
6fb5fa3c
DB
3172 else if (GET_CODE (XEXP (note, 0)) == CLOBBER)
3173 {
7e657ec2
EB
3174 if (REG_P (XEXP (XEXP (note, 0), 0)))
3175 {
3176 unsigned int regno = REGNO (XEXP (XEXP (note, 0), 0));
c5ebdc25 3177 if (!TEST_HARD_REG_BIT (defs_generated, regno))
7e657ec2 3178 df_defs_record (collection_rec, XEXP (note, 0), bb,
50e94c7e 3179 insn_info, flags);
7e657ec2
EB
3180 }
3181 else
502ef838
PB
3182 df_uses_record (collection_rec, &XEXP (note, 0),
3183 DF_REF_REG_USE, bb, insn_info, flags);
6fb5fa3c
DB
3184 }
3185 }
4d779342 3186
6fb5fa3c
DB
3187 return;
3188}
4d779342 3189
6fb5fa3c
DB
3190/* Collect all refs in the INSN. This function is free of any
3191 side-effect - it will create and return a lists of df_ref's in the
3192 COLLECTION_REC without putting those refs into existing ref chains
3193 and reg chains. */
4d779342 3194
6fb5fa3c 3195static void
c5ebdc25 3196df_insn_refs_collect (struct df_collection_rec *collection_rec,
b8698a0f 3197 basic_block bb, struct df_insn_info *insn_info)
6fb5fa3c
DB
3198{
3199 rtx note;
50e94c7e 3200 bool is_cond_exec = (GET_CODE (PATTERN (insn_info->insn)) == COND_EXEC);
4d779342 3201
6fb5fa3c 3202 /* Clear out the collection record. */
9771b263
DN
3203 collection_rec->def_vec.truncate (0);
3204 collection_rec->use_vec.truncate (0);
3205 collection_rec->eq_use_vec.truncate (0);
3206 collection_rec->mw_vec.truncate (0);
4d779342 3207
50e94c7e
SB
3208 /* Process REG_EQUIV/REG_EQUAL notes. */
3209 for (note = REG_NOTES (insn_info->insn); note;
6fb5fa3c
DB
3210 note = XEXP (note, 1))
3211 {
3212 switch (REG_NOTE_KIND (note))
3213 {
3214 case REG_EQUIV:
3215 case REG_EQUAL:
502ef838 3216 df_uses_record (collection_rec,
6fb5fa3c 3217 &XEXP (note, 0), DF_REF_REG_USE,
502ef838 3218 bb, insn_info, DF_REF_IN_NOTE);
6fb5fa3c
DB
3219 break;
3220 case REG_NON_LOCAL_GOTO:
3221 /* The frame ptr is used by a non-local goto. */
57512f53 3222 df_ref_record (DF_REF_BASE, collection_rec,
6fb5fa3c 3223 regno_reg_rtx[FRAME_POINTER_REGNUM],
50e94c7e 3224 NULL, bb, insn_info,
502ef838 3225 DF_REF_REG_USE, 0);
c3e08036
TS
3226 if (!HARD_FRAME_POINTER_IS_FRAME_POINTER)
3227 df_ref_record (DF_REF_BASE, collection_rec,
3228 regno_reg_rtx[HARD_FRAME_POINTER_REGNUM],
3229 NULL, bb, insn_info,
3230 DF_REF_REG_USE, 0);
6fb5fa3c
DB
3231 break;
3232 default:
3233 break;
3234 }
4d779342 3235 }
6fb5fa3c 3236
c5ebdc25
DA
3237 /* For CALL_INSNs, first record DF_REF_BASE register defs, as well as
3238 uses from CALL_INSN_FUNCTION_USAGE. */
50e94c7e 3239 if (CALL_P (insn_info->insn))
b8698a0f 3240 df_get_call_refs (collection_rec, bb, insn_info,
6fb5fa3c
DB
3241 (is_cond_exec) ? DF_REF_CONDITIONAL : 0);
3242
c5ebdc25
DA
3243 /* Record other defs. These should be mostly for DF_REF_REGULAR, so
3244 that a qsort on the defs is unnecessary in most cases. */
3245 df_defs_record (collection_rec,
3246 PATTERN (insn_info->insn), bb, insn_info, 0);
3247
6fb5fa3c 3248 /* Record the register uses. */
502ef838
PB
3249 df_uses_record (collection_rec,
3250 &PATTERN (insn_info->insn), DF_REF_REG_USE, bb, insn_info, 0);
6fb5fa3c
DB
3251
3252 /* DF_REF_CONDITIONAL needs corresponding USES. */
3253 if (is_cond_exec)
3254 df_get_conditional_uses (collection_rec);
3255
3256 df_canonize_collection_rec (collection_rec);
4d779342
DB
3257}
3258
6fb5fa3c
DB
3259/* Recompute the luids for the insns in BB. */
3260
3261void
3262df_recompute_luids (basic_block bb)
4d779342 3263{
dd3eed93 3264 rtx_insn *insn;
4d779342 3265 int luid = 0;
23249ac4 3266
6fb5fa3c 3267 df_grow_insn_info ();
4d779342
DB
3268
3269 /* Scan the block an insn at a time from beginning to end. */
3270 FOR_BB_INSNS (bb, insn)
3271 {
50e94c7e 3272 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
6fb5fa3c
DB
3273 /* Inserting labels does not always trigger the incremental
3274 rescanning. */
3275 if (!insn_info)
4d779342 3276 {
6fb5fa3c 3277 gcc_assert (!INSN_P (insn));
50e94c7e 3278 insn_info = df_insn_create_insn_record (insn);
4d779342 3279 }
6fb5fa3c 3280
50e94c7e 3281 DF_INSN_INFO_LUID (insn_info) = luid;
6fb5fa3c
DB
3282 if (INSN_P (insn))
3283 luid++;
3284 }
3285}
3286
3287
6fb5fa3c
DB
3288/* Collect all artificial refs at the block level for BB and add them
3289 to COLLECTION_REC. */
3290
3291static void
3292df_bb_refs_collect (struct df_collection_rec *collection_rec, basic_block bb)
3293{
9771b263
DN
3294 collection_rec->def_vec.truncate (0);
3295 collection_rec->use_vec.truncate (0);
3296 collection_rec->eq_use_vec.truncate (0);
3297 collection_rec->mw_vec.truncate (0);
6fb5fa3c
DB
3298
3299 if (bb->index == ENTRY_BLOCK)
3300 {
3301 df_entry_block_defs_collect (collection_rec, df->entry_block_defs);
3302 return;
3303 }
3304 else if (bb->index == EXIT_BLOCK)
3305 {
3306 df_exit_block_uses_collect (collection_rec, df->exit_block_uses);
3307 return;
4d779342
DB
3308 }
3309
ba49cb7b 3310 if (bb_has_eh_pred (bb))
4d779342
DB
3311 {
3312 unsigned int i;
3313 /* Mark the registers that will contain data for the handler. */
912f2dac
DB
3314 for (i = 0; ; ++i)
3315 {
3316 unsigned regno = EH_RETURN_DATA_REGNO (i);
3317 if (regno == INVALID_REGNUM)
3318 break;
57512f53 3319 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[regno], NULL,
502ef838 3320 bb, NULL, DF_REF_REG_DEF, DF_REF_AT_TOP);
912f2dac 3321 }
4d779342 3322 }
4d779342 3323
6fb5fa3c
DB
3324 /* Add the hard_frame_pointer if this block is the target of a
3325 non-local goto. */
3326 if (bb->flags & BB_NON_LOCAL_GOTO_TARGET)
57512f53 3327 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, hard_frame_pointer_rtx, NULL,
502ef838 3328 bb, NULL, DF_REF_REG_DEF, DF_REF_AT_TOP);
b8698a0f 3329
6fb5fa3c
DB
3330 /* Add the artificial uses. */
3331 if (bb->index >= NUM_FIXED_BLOCKS)
23249ac4
DB
3332 {
3333 bitmap_iterator bi;
3334 unsigned int regno;
b8698a0f 3335 bitmap au = bb_has_eh_pred (bb)
a7e3698d
JH
3336 ? &df->eh_block_artificial_uses
3337 : &df->regular_block_artificial_uses;
23249ac4 3338
6fb5fa3c 3339 EXECUTE_IF_SET_IN_BITMAP (au, 0, regno, bi)
23249ac4 3340 {
57512f53 3341 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[regno], NULL,
502ef838 3342 bb, NULL, DF_REF_REG_USE, 0);
23249ac4 3343 }
4d779342 3344 }
6fb5fa3c
DB
3345
3346 df_canonize_collection_rec (collection_rec);
4d779342
DB
3347}
3348
268886f3 3349
6fb5fa3c
DB
3350/* Record all the refs within the basic block BB_INDEX and scan the instructions if SCAN_INSNS. */
3351
3352void
3353df_bb_refs_record (int bb_index, bool scan_insns)
268886f3 3354{
06e28de2 3355 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
dd3eed93 3356 rtx_insn *insn;
6fb5fa3c 3357 int luid = 0;
268886f3 3358
6fb5fa3c 3359 if (!df)
268886f3
ZD
3360 return;
3361
ff4c81cc 3362 df_collection_rec collection_rec;
e285df08 3363 df_grow_bb_info (df_scan);
6fb5fa3c
DB
3364 if (scan_insns)
3365 /* Scan the block an insn at a time from beginning to end. */
3366 FOR_BB_INSNS (bb, insn)
3367 {
50e94c7e 3368 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
6fb5fa3c
DB
3369 gcc_assert (!insn_info);
3370
50e94c7e 3371 insn_info = df_insn_create_insn_record (insn);
6fb5fa3c
DB
3372 if (INSN_P (insn))
3373 {
3374 /* Record refs within INSN. */
50e94c7e
SB
3375 DF_INSN_INFO_LUID (insn_info) = luid++;
3376 df_insn_refs_collect (&collection_rec, bb, DF_INSN_INFO_GET (insn));
ff4c81cc 3377 df_refs_add_to_chains (&collection_rec, bb, insn, copy_all);
6fb5fa3c 3378 }
50e94c7e 3379 DF_INSN_INFO_LUID (insn_info) = luid;
6fb5fa3c
DB
3380 }
3381
3382 /* Other block level artificial refs */
3383 df_bb_refs_collect (&collection_rec, bb);
ff4c81cc 3384 df_refs_add_to_chains (&collection_rec, bb, NULL, copy_all);
c2569604 3385
6fb5fa3c 3386 /* Now that the block has been processed, set the block as dirty so
05c219bb 3387 LR and LIVE will get it processed. */
6fb5fa3c 3388 df_set_bb_dirty (bb);
268886f3 3389}
4d779342 3390
6fb5fa3c
DB
3391
3392/* Get the artificial use set for a regular (i.e. non-exit/non-entry)
3393 block. */
4d779342
DB
3394
3395static void
6fb5fa3c 3396df_get_regular_block_artificial_uses (bitmap regular_block_artificial_uses)
4d779342 3397{
60d52d0d
JJ
3398#ifdef EH_USES
3399 unsigned int i;
3400#endif
3401
6fb5fa3c 3402 bitmap_clear (regular_block_artificial_uses);
4d779342 3403
6fb5fa3c 3404 if (reload_completed)
4d779342 3405 {
6fb5fa3c
DB
3406 if (frame_pointer_needed)
3407 bitmap_set_bit (regular_block_artificial_uses, HARD_FRAME_POINTER_REGNUM);
3408 }
3409 else
3410 /* Before reload, there are a few registers that must be forced
3411 live everywhere -- which might not already be the case for
3412 blocks within infinite loops. */
3413 {
2098e438
JL
3414 unsigned int picreg = PIC_OFFSET_TABLE_REGNUM;
3415
6fb5fa3c
DB
3416 /* Any reference to any pseudo before reload is a potential
3417 reference of the frame pointer. */
3418 bitmap_set_bit (regular_block_artificial_uses, FRAME_POINTER_REGNUM);
b8698a0f 3419
c3e08036
TS
3420 if (!HARD_FRAME_POINTER_IS_FRAME_POINTER)
3421 bitmap_set_bit (regular_block_artificial_uses,
3422 HARD_FRAME_POINTER_REGNUM);
6fb5fa3c 3423
6fb5fa3c
DB
3424 /* Pseudos with argument area equivalences may require
3425 reloading via the argument pointer. */
3f393fc6
TS
3426 if (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3427 && fixed_regs[ARG_POINTER_REGNUM])
6fb5fa3c 3428 bitmap_set_bit (regular_block_artificial_uses, ARG_POINTER_REGNUM);
b8698a0f 3429
6fb5fa3c
DB
3430 /* Any constant, or pseudo with constant equivalences, may
3431 require reloading from memory using the pic register. */
2098e438
JL
3432 if (picreg != INVALID_REGNUM
3433 && fixed_regs[picreg])
3434 bitmap_set_bit (regular_block_artificial_uses, picreg);
4d779342 3435 }
6fb5fa3c
DB
3436 /* The all-important stack pointer must always be live. */
3437 bitmap_set_bit (regular_block_artificial_uses, STACK_POINTER_REGNUM);
60d52d0d
JJ
3438
3439#ifdef EH_USES
3440 /* EH_USES registers are used:
3441 1) at all insns that might throw (calls or with -fnon-call-exceptions
3442 trapping insns)
3443 2) in all EH edges
3444 3) to support backtraces and/or debugging, anywhere between their
3445 initialization and where they the saved registers are restored
3446 from them, including the cases where we don't reach the epilogue
3447 (noreturn call or infinite loop). */
3448 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3449 if (EH_USES (i))
3450 bitmap_set_bit (regular_block_artificial_uses, i);
3451#endif
6fb5fa3c
DB
3452}
3453
4d779342 3454
6fb5fa3c 3455/* Get the artificial use set for an eh block. */
912f2dac 3456
6fb5fa3c
DB
3457static void
3458df_get_eh_block_artificial_uses (bitmap eh_block_artificial_uses)
3459{
3460 bitmap_clear (eh_block_artificial_uses);
268886f3 3461
073a8998 3462 /* The following code (down through the arg_pointer setting APPEARS
6fb5fa3c
DB
3463 to be necessary because there is nothing that actually
3464 describes what the exception handling code may actually need
3465 to keep alive. */
3466 if (reload_completed)
3467 {
3468 if (frame_pointer_needed)
3469 {
3470 bitmap_set_bit (eh_block_artificial_uses, FRAME_POINTER_REGNUM);
c3e08036
TS
3471 if (!HARD_FRAME_POINTER_IS_FRAME_POINTER)
3472 bitmap_set_bit (eh_block_artificial_uses,
3473 HARD_FRAME_POINTER_REGNUM);
6fb5fa3c 3474 }
3f393fc6
TS
3475 if (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3476 && fixed_regs[ARG_POINTER_REGNUM])
6fb5fa3c 3477 bitmap_set_bit (eh_block_artificial_uses, ARG_POINTER_REGNUM);
6fb5fa3c 3478 }
4d779342
DB
3479}
3480
3481
6fb5fa3c 3482\f
4d779342
DB
3483/*----------------------------------------------------------------------------
3484 Specialized hard register scanning functions.
3485----------------------------------------------------------------------------*/
3486
6fb5fa3c 3487
4d779342
DB
3488/* Mark a register in SET. Hard registers in large modes get all
3489 of their component registers set as well. */
3490
3491static void
3492df_mark_reg (rtx reg, void *vset)
3493{
07a737f3 3494 bitmap_set_range ((bitmap) vset, REGNO (reg), REG_NREGS (reg));
4d779342
DB
3495}
3496
912f2dac 3497
6fb5fa3c 3498/* Set the bit for regs that are considered being defined at the entry. */
912f2dac
DB
3499
3500static void
6fb5fa3c 3501df_get_entry_block_def_set (bitmap entry_block_defs)
912f2dac 3502{
912f2dac 3503 rtx r;
6fb5fa3c 3504 int i;
912f2dac 3505
6fb5fa3c 3506 bitmap_clear (entry_block_defs);
912f2dac
DB
3507
3508 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
211d71a7
SB
3509 {
3510 if (global_regs[i])
3511 bitmap_set_bit (entry_block_defs, i);
3512 if (FUNCTION_ARG_REGNO_P (i))
3513 bitmap_set_bit (entry_block_defs, INCOMING_REGNO (i));
3514 }
b8698a0f 3515
1d489435
RS
3516 /* The always important stack pointer. */
3517 bitmap_set_bit (entry_block_defs, STACK_POINTER_REGNUM);
3518
912f2dac
DB
3519 /* Once the prologue has been generated, all of these registers
3520 should just show up in the first regular block. */
e86a9946 3521 if (targetm.have_prologue () && epilogue_completed)
912f2dac
DB
3522 {
3523 /* Defs for the callee saved registers are inserted so that the
3524 pushes have some defining location. */
3525 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
6fb5fa3c
DB
3526 if ((call_used_regs[i] == 0) && (df_regs_ever_live_p (i)))
3527 bitmap_set_bit (entry_block_defs, i);
912f2dac 3528 }
912f2dac 3529
bff98546
JJ
3530 r = targetm.calls.struct_value_rtx (current_function_decl, true);
3531 if (r && REG_P (r))
3532 bitmap_set_bit (entry_block_defs, REGNO (r));
3533
531ca746
RH
3534 /* If the function has an incoming STATIC_CHAIN, it has to show up
3535 in the entry def set. */
3536 r = targetm.calls.static_chain (current_function_decl, true);
3537 if (r && REG_P (r))
3538 bitmap_set_bit (entry_block_defs, REGNO (r));
3539
23249ac4 3540 if ((!reload_completed) || frame_pointer_needed)
912f2dac
DB
3541 {
3542 /* Any reference to any pseudo before reload is a potential
3543 reference of the frame pointer. */
6fb5fa3c 3544 bitmap_set_bit (entry_block_defs, FRAME_POINTER_REGNUM);
c3e08036 3545
23249ac4 3546 /* If they are different, also mark the hard frame pointer as live. */
c3e08036
TS
3547 if (!HARD_FRAME_POINTER_IS_FRAME_POINTER
3548 && !LOCAL_REGNO (HARD_FRAME_POINTER_REGNUM))
6fb5fa3c 3549 bitmap_set_bit (entry_block_defs, HARD_FRAME_POINTER_REGNUM);
23249ac4 3550 }
912f2dac 3551
23249ac4
DB
3552 /* These registers are live everywhere. */
3553 if (!reload_completed)
3554 {
912f2dac
DB
3555 /* Pseudos with argument area equivalences may require
3556 reloading via the argument pointer. */
3f393fc6
TS
3557 if (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3558 && fixed_regs[ARG_POINTER_REGNUM])
6fb5fa3c 3559 bitmap_set_bit (entry_block_defs, ARG_POINTER_REGNUM);
b8698a0f 3560
912f2dac
DB
3561 /* Any constant, or pseudo with constant equivalences, may
3562 require reloading from memory using the pic register. */
faa7b0de 3563 unsigned int picreg = PIC_OFFSET_TABLE_REGNUM;
2098e438
JL
3564 if (picreg != INVALID_REGNUM
3565 && fixed_regs[picreg])
3566 bitmap_set_bit (entry_block_defs, picreg);
6fb5fa3c
DB
3567 }
3568
3569#ifdef INCOMING_RETURN_ADDR_RTX
3570 if (REG_P (INCOMING_RETURN_ADDR_RTX))
3571 bitmap_set_bit (entry_block_defs, REGNO (INCOMING_RETURN_ADDR_RTX));
3572#endif
b8698a0f 3573
38f8b050 3574 targetm.extra_live_on_entry (entry_block_defs);
6fb5fa3c
DB
3575}
3576
3577
3578/* Return the (conservative) set of hard registers that are defined on
b8698a0f
L
3579 entry to the function.
3580 It uses df->entry_block_defs to determine which register
6fb5fa3c
DB
3581 reference to include. */
3582
3583static void
b8698a0f 3584df_entry_block_defs_collect (struct df_collection_rec *collection_rec,
6fb5fa3c
DB
3585 bitmap entry_block_defs)
3586{
b8698a0f 3587 unsigned int i;
6fb5fa3c
DB
3588 bitmap_iterator bi;
3589
3590 EXECUTE_IF_SET_IN_BITMAP (entry_block_defs, 0, i, bi)
3591 {
b8698a0f 3592 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[i], NULL,
fefa31b5 3593 ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, DF_REF_REG_DEF, 0);
6fb5fa3c
DB
3594 }
3595
3596 df_canonize_collection_rec (collection_rec);
3597}
3598
3599
3600/* Record the (conservative) set of hard registers that are defined on
3601 entry to the function. */
3602
3603static void
3604df_record_entry_block_defs (bitmap entry_block_defs)
3605{
3606 struct df_collection_rec collection_rec;
6fb5fa3c
DB
3607 df_entry_block_defs_collect (&collection_rec, entry_block_defs);
3608
3609 /* Process bb_refs chain */
06e28de2
DM
3610 df_refs_add_to_chains (&collection_rec,
3611 BASIC_BLOCK_FOR_FN (cfun, ENTRY_BLOCK),
3612 NULL,
ff4c81cc 3613 copy_defs);
6fb5fa3c
DB
3614}
3615
3616
15dc95cb 3617/* Update the defs in the entry block. */
6fb5fa3c
DB
3618
3619void
3620df_update_entry_block_defs (void)
3621{
a7e3698d 3622 bitmap_head refs;
6fb5fa3c 3623 bool changed = false;
912f2dac 3624
a7e3698d
JH
3625 bitmap_initialize (&refs, &df_bitmap_obstack);
3626 df_get_entry_block_def_set (&refs);
6fb5fa3c
DB
3627 if (df->entry_block_defs)
3628 {
a7e3698d 3629 if (!bitmap_equal_p (df->entry_block_defs, &refs))
6fb5fa3c
DB
3630 {
3631 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (ENTRY_BLOCK);
3632 df_ref_chain_delete_du_chain (bb_info->artificial_defs);
3633 df_ref_chain_delete (bb_info->artificial_defs);
3634 bb_info->artificial_defs = NULL;
3635 changed = true;
3636 }
3637 }
3638 else
3639 {
3640 struct df_scan_problem_data *problem_data
3641 = (struct df_scan_problem_data *) df_scan->problem_data;
a7e3698d 3642 gcc_unreachable ();
6fb5fa3c
DB
3643 df->entry_block_defs = BITMAP_ALLOC (&problem_data->reg_bitmaps);
3644 changed = true;
3645 }
912f2dac 3646
6fb5fa3c 3647 if (changed)
912f2dac 3648 {
a7e3698d
JH
3649 df_record_entry_block_defs (&refs);
3650 bitmap_copy (df->entry_block_defs, &refs);
06e28de2 3651 df_set_bb_dirty (BASIC_BLOCK_FOR_FN (cfun, ENTRY_BLOCK));
912f2dac 3652 }
a7e3698d 3653 bitmap_clear (&refs);
912f2dac
DB
3654}
3655
3656
6fb5fa3c 3657/* Set the bit for regs that are considered being used at the exit. */
4d779342
DB
3658
3659static void
6fb5fa3c 3660df_get_exit_block_use_set (bitmap exit_block_uses)
4d779342 3661{
b8698a0f 3662 unsigned int i;
2098e438 3663 unsigned int picreg = PIC_OFFSET_TABLE_REGNUM;
4d779342 3664
6fb5fa3c 3665 bitmap_clear (exit_block_uses);
b718216c
SP
3666
3667 /* Stack pointer is always live at the exit. */
3668 bitmap_set_bit (exit_block_uses, STACK_POINTER_REGNUM);
b8698a0f 3669
4d779342
DB
3670 /* Mark the frame pointer if needed at the end of the function.
3671 If we end up eliminating it, it will be removed from the live
3672 list of each basic block by reload. */
b8698a0f 3673
23249ac4 3674 if ((!reload_completed) || frame_pointer_needed)
4d779342 3675 {
6fb5fa3c 3676 bitmap_set_bit (exit_block_uses, FRAME_POINTER_REGNUM);
c3e08036 3677
4d779342 3678 /* If they are different, also mark the hard frame pointer as live. */
c3e08036
TS
3679 if (!HARD_FRAME_POINTER_IS_FRAME_POINTER
3680 && !LOCAL_REGNO (HARD_FRAME_POINTER_REGNUM))
6fb5fa3c 3681 bitmap_set_bit (exit_block_uses, HARD_FRAME_POINTER_REGNUM);
4d779342
DB
3682 }
3683
4d779342
DB
3684 /* Many architectures have a GP register even without flag_pic.
3685 Assume the pic register is not in use, or will be handled by
3686 other means, if it is not fixed. */
f8fe0a4a 3687 if (!PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
2098e438
JL
3688 && picreg != INVALID_REGNUM
3689 && fixed_regs[picreg])
3690 bitmap_set_bit (exit_block_uses, picreg);
b8698a0f 3691
4d779342
DB
3692 /* Mark all global registers, and all registers used by the
3693 epilogue as being live at the end of the function since they
3694 may be referenced by our caller. */
3695 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3696 if (global_regs[i] || EPILOGUE_USES (i))
6fb5fa3c 3697 bitmap_set_bit (exit_block_uses, i);
b8698a0f 3698
e86a9946 3699 if (targetm.have_epilogue () && epilogue_completed)
4d779342
DB
3700 {
3701 /* Mark all call-saved registers that we actually used. */
3702 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
6fb5fa3c 3703 if (df_regs_ever_live_p (i) && !LOCAL_REGNO (i)
23249ac4 3704 && !TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
6fb5fa3c 3705 bitmap_set_bit (exit_block_uses, i);
4d779342 3706 }
b8698a0f 3707
4d779342 3708 /* Mark the registers that will contain data for the handler. */
e3b5732b 3709 if (reload_completed && crtl->calls_eh_return)
4d779342
DB
3710 for (i = 0; ; ++i)
3711 {
3712 unsigned regno = EH_RETURN_DATA_REGNO (i);
3713 if (regno == INVALID_REGNUM)
3714 break;
6fb5fa3c 3715 bitmap_set_bit (exit_block_uses, regno);
4d779342 3716 }
4d779342
DB
3717
3718#ifdef EH_RETURN_STACKADJ_RTX
e86a9946 3719 if ((!targetm.have_epilogue () || ! epilogue_completed)
e3b5732b 3720 && crtl->calls_eh_return)
4d779342
DB
3721 {
3722 rtx tmp = EH_RETURN_STACKADJ_RTX;
3723 if (tmp && REG_P (tmp))
6fb5fa3c 3724 df_mark_reg (tmp, exit_block_uses);
4d779342
DB
3725 }
3726#endif
3727
3728#ifdef EH_RETURN_HANDLER_RTX
e86a9946 3729 if ((!targetm.have_epilogue () || ! epilogue_completed)
e3b5732b 3730 && crtl->calls_eh_return)
4d779342
DB
3731 {
3732 rtx tmp = EH_RETURN_HANDLER_RTX;
3733 if (tmp && REG_P (tmp))
6fb5fa3c 3734 df_mark_reg (tmp, exit_block_uses);
4d779342 3735 }
b8698a0f 3736#endif
6fb5fa3c 3737
4d779342 3738 /* Mark function return value. */
6fb5fa3c
DB
3739 diddle_return_value (df_mark_reg, (void*) exit_block_uses);
3740}
3741
3742
b8698a0f 3743/* Return the refs of hard registers that are used in the exit block.
6fb5fa3c
DB
3744 It uses df->exit_block_uses to determine register to include. */
3745
3746static void
3747df_exit_block_uses_collect (struct df_collection_rec *collection_rec, bitmap exit_block_uses)
3748{
b8698a0f 3749 unsigned int i;
6fb5fa3c
DB
3750 bitmap_iterator bi;
3751
3752 EXECUTE_IF_SET_IN_BITMAP (exit_block_uses, 0, i, bi)
57512f53 3753 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[i], NULL,
fefa31b5 3754 EXIT_BLOCK_PTR_FOR_FN (cfun), NULL, DF_REF_REG_USE, 0);
4d779342 3755
6fb5fa3c
DB
3756 /* It is deliberate that this is not put in the exit block uses but
3757 I do not know why. */
3f393fc6
TS
3758 if (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3759 && reload_completed
6fb5fa3c 3760 && !bitmap_bit_p (exit_block_uses, ARG_POINTER_REGNUM)
fefa31b5 3761 && bb_has_eh_pred (EXIT_BLOCK_PTR_FOR_FN (cfun))
6fb5fa3c 3762 && fixed_regs[ARG_POINTER_REGNUM])
57512f53 3763 df_ref_record (DF_REF_ARTIFICIAL, collection_rec, regno_reg_rtx[ARG_POINTER_REGNUM], NULL,
fefa31b5 3764 EXIT_BLOCK_PTR_FOR_FN (cfun), NULL, DF_REF_REG_USE, 0);
6fb5fa3c
DB
3765
3766 df_canonize_collection_rec (collection_rec);
3767}
3768
3769
b8698a0f 3770/* Record the set of hard registers that are used in the exit block.
6fb5fa3c
DB
3771 It uses df->exit_block_uses to determine which bit to include. */
3772
3773static void
3774df_record_exit_block_uses (bitmap exit_block_uses)
3775{
3776 struct df_collection_rec collection_rec;
6fb5fa3c
DB
3777 df_exit_block_uses_collect (&collection_rec, exit_block_uses);
3778
3779 /* Process bb_refs chain */
06e28de2
DM
3780 df_refs_add_to_chains (&collection_rec,
3781 BASIC_BLOCK_FOR_FN (cfun, EXIT_BLOCK),
3782 NULL,
ff4c81cc 3783 copy_uses);
6fb5fa3c
DB
3784}
3785
3786
3787/* Update the uses in the exit block. */
3788
3789void
3790df_update_exit_block_uses (void)
3791{
a7e3698d 3792 bitmap_head refs;
6fb5fa3c
DB
3793 bool changed = false;
3794
a7e3698d
JH
3795 bitmap_initialize (&refs, &df_bitmap_obstack);
3796 df_get_exit_block_use_set (&refs);
6fb5fa3c
DB
3797 if (df->exit_block_uses)
3798 {
a7e3698d 3799 if (!bitmap_equal_p (df->exit_block_uses, &refs))
6fb5fa3c
DB
3800 {
3801 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (EXIT_BLOCK);
3802 df_ref_chain_delete_du_chain (bb_info->artificial_uses);
3803 df_ref_chain_delete (bb_info->artificial_uses);
3804 bb_info->artificial_uses = NULL;
3805 changed = true;
3806 }
3807 }
3808 else
3809 {
3810 struct df_scan_problem_data *problem_data
3811 = (struct df_scan_problem_data *) df_scan->problem_data;
a7e3698d 3812 gcc_unreachable ();
6fb5fa3c
DB
3813 df->exit_block_uses = BITMAP_ALLOC (&problem_data->reg_bitmaps);
3814 changed = true;
3815 }
3816
3817 if (changed)
3818 {
a7e3698d
JH
3819 df_record_exit_block_uses (&refs);
3820 bitmap_copy (df->exit_block_uses,& refs);
06e28de2 3821 df_set_bb_dirty (BASIC_BLOCK_FOR_FN (cfun, EXIT_BLOCK));
6fb5fa3c 3822 }
a7e3698d 3823 bitmap_clear (&refs);
4d779342
DB
3824}
3825
3826static bool initialized = false;
3827
6fb5fa3c 3828
4d779342
DB
3829/* Initialize some platform specific structures. */
3830
b8698a0f 3831void
4d779342
DB
3832df_hard_reg_init (void)
3833{
fcfd0ebb 3834#ifdef ELIMINABLE_REGS
7bda4a1d 3835 int i;
4d779342
DB
3836 static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
3837#endif
4d779342
DB
3838 if (initialized)
3839 return;
3840
4d779342
DB
3841 /* Record which registers will be eliminated. We use this in
3842 mark_used_regs. */
3843 CLEAR_HARD_REG_SET (elim_reg_set);
b8698a0f 3844
4d779342
DB
3845#ifdef ELIMINABLE_REGS
3846 for (i = 0; i < (int) ARRAY_SIZE (eliminables); i++)
3847 SET_HARD_REG_BIT (elim_reg_set, eliminables[i].from);
3848#else
3849 SET_HARD_REG_BIT (elim_reg_set, FRAME_POINTER_REGNUM);
3850#endif
b8698a0f 3851
4d779342
DB
3852 initialized = true;
3853}
6fb5fa3c
DB
3854
3855
3856/* Recompute the parts of scanning that are based on regs_ever_live
b8698a0f 3857 because something changed in that array. */
6fb5fa3c 3858
b8698a0f 3859void
6fb5fa3c
DB
3860df_update_entry_exit_and_calls (void)
3861{
3862 basic_block bb;
3863
3864 df_update_entry_block_defs ();
3865 df_update_exit_block_uses ();
3866
3867 /* The call insns need to be rescanned because there may be changes
3868 in the set of registers clobbered across the call. */
11cd3bed 3869 FOR_EACH_BB_FN (bb, cfun)
6fb5fa3c 3870 {
dd3eed93 3871 rtx_insn *insn;
6fb5fa3c
DB
3872 FOR_BB_INSNS (bb, insn)
3873 {
3874 if (INSN_P (insn) && CALL_P (insn))
3875 df_insn_rescan (insn);
3876 }
3877 }
3878}
3879
3880
3881/* Return true if hard REG is actually used in the some instruction.
3882 There are a fair number of conditions that affect the setting of
3883 this array. See the comment in df.h for df->hard_regs_live_count
3884 for the conditions that this array is set. */
3885
b8698a0f 3886bool
6fb5fa3c
DB
3887df_hard_reg_used_p (unsigned int reg)
3888{
6fb5fa3c
DB
3889 return df->hard_regs_live_count[reg] != 0;
3890}
3891
3892
3893/* A count of the number of times REG is actually used in the some
3894 instruction. There are a fair number of conditions that affect the
3895 setting of this array. See the comment in df.h for
3896 df->hard_regs_live_count for the conditions that this array is
3897 set. */
3898
3899
3900unsigned int
3901df_hard_reg_used_count (unsigned int reg)
3902{
6fb5fa3c
DB
3903 return df->hard_regs_live_count[reg];
3904}
3905
3906
3907/* Get the value of regs_ever_live[REGNO]. */
3908
b8698a0f 3909bool
6fb5fa3c
DB
3910df_regs_ever_live_p (unsigned int regno)
3911{
3912 return regs_ever_live[regno];
3913}
3914
3915
3916/* Set regs_ever_live[REGNO] to VALUE. If this cause regs_ever_live
3917 to change, schedule that change for the next update. */
3918
b8698a0f 3919void
6fb5fa3c
DB
3920df_set_regs_ever_live (unsigned int regno, bool value)
3921{
3922 if (regs_ever_live[regno] == value)
3923 return;
3924
3925 regs_ever_live[regno] = value;
3926 if (df)
3927 df->redo_entry_and_exit = true;
3928}
3929
3930
3931/* Compute "regs_ever_live" information from the underlying df
3932 information. Set the vector to all false if RESET. */
3933
3934void
3935df_compute_regs_ever_live (bool reset)
3936{
3937 unsigned int i;
3938 bool changed = df->redo_entry_and_exit;
b8698a0f 3939
6fb5fa3c
DB
3940 if (reset)
3941 memset (regs_ever_live, 0, sizeof (regs_ever_live));
3942
3943 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3944 if ((!regs_ever_live[i]) && df_hard_reg_used_p (i))
3945 {
3946 regs_ever_live[i] = true;
3947 changed = true;
3948 }
3949 if (changed)
3950 df_update_entry_exit_and_calls ();
3951 df->redo_entry_and_exit = false;
3952}
3953
3954\f
3955/*----------------------------------------------------------------------------
3956 Dataflow ref information verification functions.
3957
3958 df_reg_chain_mark (refs, regno, is_def, is_eq_use)
3959 df_reg_chain_verify_unmarked (refs)
9771b263 3960 df_refs_verify (vec<stack, va_df_ref>, ref*, bool)
6fb5fa3c
DB
3961 df_mws_verify (mw*, mw*, bool)
3962 df_insn_refs_verify (collection_rec, bb, insn, bool)
3963 df_bb_refs_verify (bb, refs, bool)
3964 df_bb_verify (bb)
3965 df_exit_block_bitmap_verify (bool)
3966 df_entry_block_bitmap_verify (bool)
3967 df_scan_verify ()
3968----------------------------------------------------------------------------*/
3969
3970
3971/* Mark all refs in the reg chain. Verify that all of the registers
b8698a0f 3972are in the correct chain. */
6fb5fa3c
DB
3973
3974static unsigned int
b8698a0f 3975df_reg_chain_mark (df_ref refs, unsigned int regno,
6fb5fa3c
DB
3976 bool is_def, bool is_eq_use)
3977{
3978 unsigned int count = 0;
57512f53 3979 df_ref ref;
6fb5fa3c
DB
3980 for (ref = refs; ref; ref = DF_REF_NEXT_REG (ref))
3981 {
3982 gcc_assert (!DF_REF_IS_REG_MARKED (ref));
3983
3984 /* If there are no def-use or use-def chains, make sure that all
3985 of the chains are clear. */
3986 if (!df_chain)
3987 gcc_assert (!DF_REF_CHAIN (ref));
3988
3989 /* Check to make sure the ref is in the correct chain. */
3990 gcc_assert (DF_REF_REGNO (ref) == regno);
3991 if (is_def)
57512f53 3992 gcc_assert (DF_REF_REG_DEF_P (ref));
6fb5fa3c 3993 else
57512f53 3994 gcc_assert (!DF_REF_REG_DEF_P (ref));
6fb5fa3c
DB
3995
3996 if (is_eq_use)
3997 gcc_assert ((DF_REF_FLAGS (ref) & DF_REF_IN_NOTE));
3998 else
3999 gcc_assert ((DF_REF_FLAGS (ref) & DF_REF_IN_NOTE) == 0);
4000
57512f53
KZ
4001 if (DF_REF_NEXT_REG (ref))
4002 gcc_assert (DF_REF_PREV_REG (DF_REF_NEXT_REG (ref)) == ref);
6fb5fa3c
DB
4003 count++;
4004 DF_REF_REG_MARK (ref);
4005 }
4006 return count;
4007}
4008
4009
b8698a0f 4010/* Verify that all of the registers in the chain are unmarked. */
6fb5fa3c
DB
4011
4012static void
57512f53 4013df_reg_chain_verify_unmarked (df_ref refs)
6fb5fa3c 4014{
57512f53 4015 df_ref ref;
6fb5fa3c
DB
4016 for (ref = refs; ref; ref = DF_REF_NEXT_REG (ref))
4017 gcc_assert (!DF_REF_IS_REG_MARKED (ref));
4018}
4019
4020
4021/* Verify that NEW_REC and OLD_REC have exactly the same members. */
4022
4023static bool
b512946c 4024df_refs_verify (const vec<df_ref, va_heap> *new_rec, df_ref old_rec,
6fb5fa3c
DB
4025 bool abort_if_fail)
4026{
c2569604
ILT
4027 unsigned int ix;
4028 df_ref new_ref;
4029
ff4c81cc 4030 FOR_EACH_VEC_ELT (*new_rec, ix, new_ref)
6fb5fa3c 4031 {
b512946c 4032 if (old_rec == NULL || !df_ref_equal_p (new_ref, old_rec))
6fb5fa3c
DB
4033 {
4034 if (abort_if_fail)
4035 gcc_assert (0);
4036 else
4037 return false;
4038 }
4039
4040 /* Abort if fail is called from the function level verifier. If
4041 that is the context, mark this reg as being seem. */
4042 if (abort_if_fail)
4043 {
b512946c
RS
4044 gcc_assert (DF_REF_IS_REG_MARKED (old_rec));
4045 DF_REF_REG_UNMARK (old_rec);
6fb5fa3c
DB
4046 }
4047
b512946c 4048 old_rec = DF_REF_NEXT_LOC (old_rec);
6fb5fa3c
DB
4049 }
4050
4051 if (abort_if_fail)
b512946c 4052 gcc_assert (old_rec == NULL);
6fb5fa3c 4053 else
b512946c 4054 return old_rec == NULL;
6fb5fa3c
DB
4055 return false;
4056}
4057
4058
4059/* Verify that NEW_REC and OLD_REC have exactly the same members. */
4060
4061static bool
ff4c81cc 4062df_mws_verify (const vec<df_mw_hardreg_ptr, va_heap> *new_rec,
b512946c 4063 struct df_mw_hardreg *old_rec,
6fb5fa3c
DB
4064 bool abort_if_fail)
4065{
c2569604
ILT
4066 unsigned int ix;
4067 struct df_mw_hardreg *new_reg;
4068
ff4c81cc 4069 FOR_EACH_VEC_ELT (*new_rec, ix, new_reg)
6fb5fa3c 4070 {
b512946c 4071 if (old_rec == NULL || !df_mw_equal_p (new_reg, old_rec))
6fb5fa3c
DB
4072 {
4073 if (abort_if_fail)
4074 gcc_assert (0);
4075 else
4076 return false;
4077 }
b512946c 4078 old_rec = DF_MWS_NEXT (old_rec);
6fb5fa3c
DB
4079 }
4080
4081 if (abort_if_fail)
b512946c 4082 gcc_assert (old_rec == NULL);
6fb5fa3c 4083 else
b512946c 4084 return old_rec == NULL;
6fb5fa3c
DB
4085 return false;
4086}
4087
4088
4089/* Return true if the existing insn refs information is complete and
4090 correct. Otherwise (i.e. if there's any missing or extra refs),
b8698a0f 4091 return the correct df_ref chain in REFS_RETURN.
6fb5fa3c
DB
4092
4093 If ABORT_IF_FAIL, leave the refs that are verified (already in the
4094 ref chain) as DF_REF_MARKED(). If it's false, then it's a per-insn
4095 verification mode instead of the whole function, so unmark
4096 everything.
4097
4098 If ABORT_IF_FAIL is set, this function never returns false. */
4099
4100static bool
4101df_insn_refs_verify (struct df_collection_rec *collection_rec,
b8698a0f 4102 basic_block bb,
b2908ba6 4103 rtx_insn *insn,
6fb5fa3c
DB
4104 bool abort_if_fail)
4105{
4106 bool ret1, ret2, ret3, ret4;
4107 unsigned int uid = INSN_UID (insn);
50e94c7e 4108 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
6fb5fa3c 4109
50e94c7e 4110 df_insn_refs_collect (collection_rec, bb, insn_info);
6fb5fa3c 4111
6fb5fa3c
DB
4112 /* Unfortunately we cannot opt out early if one of these is not
4113 right because the marks will not get cleared. */
ff4c81cc 4114 ret1 = df_refs_verify (&collection_rec->def_vec, DF_INSN_UID_DEFS (uid),
6fb5fa3c 4115 abort_if_fail);
ff4c81cc 4116 ret2 = df_refs_verify (&collection_rec->use_vec, DF_INSN_UID_USES (uid),
6fb5fa3c 4117 abort_if_fail);
ff4c81cc 4118 ret3 = df_refs_verify (&collection_rec->eq_use_vec, DF_INSN_UID_EQ_USES (uid),
6fb5fa3c 4119 abort_if_fail);
ff4c81cc 4120 ret4 = df_mws_verify (&collection_rec->mw_vec, DF_INSN_UID_MWS (uid),
6fb5fa3c
DB
4121 abort_if_fail);
4122 return (ret1 && ret2 && ret3 && ret4);
4123}
4124
4125
4126/* Return true if all refs in the basic block are correct and complete.
4127 Due to df_ref_chain_verify, it will cause all refs
4128 that are verified to have DF_REF_MARK bit set. */
4129
4130static bool
4131df_bb_verify (basic_block bb)
4132{
dd3eed93 4133 rtx_insn *insn;
6fb5fa3c
DB
4134 struct df_scan_bb_info *bb_info = df_scan_get_bb_info (bb->index);
4135 struct df_collection_rec collection_rec;
b8698a0f 4136
6fb5fa3c
DB
4137 gcc_assert (bb_info);
4138
57512f53 4139 /* Scan the block, one insn at a time, from beginning to end. */
6fb5fa3c
DB
4140 FOR_BB_INSNS_REVERSE (bb, insn)
4141 {
4142 if (!INSN_P (insn))
4143 continue;
4144 df_insn_refs_verify (&collection_rec, bb, insn, true);
1aee8991 4145 df_free_collection_rec (&collection_rec);
6fb5fa3c
DB
4146 }
4147
4148 /* Do the artificial defs and uses. */
4149 df_bb_refs_collect (&collection_rec, bb);
ff4c81cc
TS
4150 df_refs_verify (&collection_rec.def_vec, df_get_artificial_defs (bb->index), true);
4151 df_refs_verify (&collection_rec.use_vec, df_get_artificial_uses (bb->index), true);
6fb5fa3c 4152 df_free_collection_rec (&collection_rec);
b8698a0f 4153
6fb5fa3c
DB
4154 return true;
4155}
4156
4157
b8698a0f 4158/* Returns true if the entry block has correct and complete df_ref set.
6fb5fa3c
DB
4159 If not it either aborts if ABORT_IF_FAIL is true or returns false. */
4160
4161static bool
4162df_entry_block_bitmap_verify (bool abort_if_fail)
4163{
a7e3698d 4164 bitmap_head entry_block_defs;
6fb5fa3c
DB
4165 bool is_eq;
4166
a7e3698d
JH
4167 bitmap_initialize (&entry_block_defs, &df_bitmap_obstack);
4168 df_get_entry_block_def_set (&entry_block_defs);
6fb5fa3c 4169
a7e3698d 4170 is_eq = bitmap_equal_p (&entry_block_defs, df->entry_block_defs);
6fb5fa3c
DB
4171
4172 if (!is_eq && abort_if_fail)
4173 {
6fb5fa3c 4174 fprintf (stderr, "entry_block_defs = ");
a7e3698d 4175 df_print_regset (stderr, &entry_block_defs);
6fb5fa3c
DB
4176 fprintf (stderr, "df->entry_block_defs = ");
4177 df_print_regset (stderr, df->entry_block_defs);
4178 gcc_assert (0);
4179 }
4180
a7e3698d 4181 bitmap_clear (&entry_block_defs);
6fb5fa3c
DB
4182
4183 return is_eq;
4184}
4185
4186
b8698a0f 4187/* Returns true if the exit block has correct and complete df_ref set.
6fb5fa3c
DB
4188 If not it either aborts if ABORT_IF_FAIL is true or returns false. */
4189
4190static bool
4191df_exit_block_bitmap_verify (bool abort_if_fail)
4192{
a7e3698d 4193 bitmap_head exit_block_uses;
6fb5fa3c
DB
4194 bool is_eq;
4195
a7e3698d
JH
4196 bitmap_initialize (&exit_block_uses, &df_bitmap_obstack);
4197 df_get_exit_block_use_set (&exit_block_uses);
6fb5fa3c 4198
a7e3698d 4199 is_eq = bitmap_equal_p (&exit_block_uses, df->exit_block_uses);
6fb5fa3c
DB
4200
4201 if (!is_eq && abort_if_fail)
4202 {
6fb5fa3c 4203 fprintf (stderr, "exit_block_uses = ");
a7e3698d 4204 df_print_regset (stderr, &exit_block_uses);
6fb5fa3c
DB
4205 fprintf (stderr, "df->exit_block_uses = ");
4206 df_print_regset (stderr, df->exit_block_uses);
4207 gcc_assert (0);
4208 }
4209
a7e3698d 4210 bitmap_clear (&exit_block_uses);
6fb5fa3c
DB
4211
4212 return is_eq;
4213}
4214
4215
cc806ac1
RS
4216/* Return true if df_ref information for all insns in all blocks are
4217 correct and complete. */
6fb5fa3c
DB
4218
4219void
4220df_scan_verify (void)
4221{
4222 unsigned int i;
4223 basic_block bb;
a7e3698d
JH
4224 bitmap_head regular_block_artificial_uses;
4225 bitmap_head eh_block_artificial_uses;
6fb5fa3c
DB
4226
4227 if (!df)
4228 return;
4229
6fb5fa3c
DB
4230 /* Verification is a 4 step process. */
4231
073a8998 4232 /* (1) All of the refs are marked by going through the reg chains. */
6fb5fa3c
DB
4233 for (i = 0; i < DF_REG_SIZE (df); i++)
4234 {
b8698a0f 4235 gcc_assert (df_reg_chain_mark (DF_REG_DEF_CHAIN (i), i, true, false)
c3284718 4236 == DF_REG_DEF_COUNT (i));
b8698a0f 4237 gcc_assert (df_reg_chain_mark (DF_REG_USE_CHAIN (i), i, false, false)
c3284718 4238 == DF_REG_USE_COUNT (i));
b8698a0f 4239 gcc_assert (df_reg_chain_mark (DF_REG_EQ_USE_CHAIN (i), i, false, true)
c3284718 4240 == DF_REG_EQ_USE_COUNT (i));
6fb5fa3c
DB
4241 }
4242
4243 /* (2) There are various bitmaps whose value may change over the
4244 course of the compilation. This step recomputes them to make
4245 sure that they have not slipped out of date. */
a7e3698d
JH
4246 bitmap_initialize (&regular_block_artificial_uses, &df_bitmap_obstack);
4247 bitmap_initialize (&eh_block_artificial_uses, &df_bitmap_obstack);
6fb5fa3c 4248
a7e3698d
JH
4249 df_get_regular_block_artificial_uses (&regular_block_artificial_uses);
4250 df_get_eh_block_artificial_uses (&eh_block_artificial_uses);
6fb5fa3c 4251
a7e3698d
JH
4252 bitmap_ior_into (&eh_block_artificial_uses,
4253 &regular_block_artificial_uses);
6fb5fa3c
DB
4254
4255 /* Check artificial_uses bitmaps didn't change. */
a7e3698d
JH
4256 gcc_assert (bitmap_equal_p (&regular_block_artificial_uses,
4257 &df->regular_block_artificial_uses));
4258 gcc_assert (bitmap_equal_p (&eh_block_artificial_uses,
4259 &df->eh_block_artificial_uses));
6fb5fa3c 4260
a7e3698d
JH
4261 bitmap_clear (&regular_block_artificial_uses);
4262 bitmap_clear (&eh_block_artificial_uses);
6fb5fa3c
DB
4263
4264 /* Verify entry block and exit block. These only verify the bitmaps,
4265 the refs are verified in df_bb_verify. */
4266 df_entry_block_bitmap_verify (true);
4267 df_exit_block_bitmap_verify (true);
b8698a0f 4268
6fb5fa3c
DB
4269 /* (3) All of the insns in all of the blocks are traversed and the
4270 marks are cleared both in the artificial refs attached to the
4271 blocks and the real refs inside the insns. It is a failure to
4272 clear a mark that has not been set as this means that the ref in
4273 the block or insn was not in the reg chain. */
4274
04a90bec 4275 FOR_ALL_BB_FN (bb, cfun)
6fb5fa3c
DB
4276 df_bb_verify (bb);
4277
4278 /* (4) See if all reg chains are traversed a second time. This time
4279 a check is made that the marks are clear. A set mark would be a
4280 from a reg that is not in any insn or basic block. */
4281
4282 for (i = 0; i < DF_REG_SIZE (df); i++)
4283 {
4284 df_reg_chain_verify_unmarked (DF_REG_DEF_CHAIN (i));
4285 df_reg_chain_verify_unmarked (DF_REG_USE_CHAIN (i));
4286 df_reg_chain_verify_unmarked (DF_REG_EQ_USE_CHAIN (i));
4287 }
4288}