df.c 97.1 KB
Newer Older
Jeff Law committed
1
/* Dataflow support routines.
2
   Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
Jeff Law committed
3 4 5
   Contributed by Michael P. Hayes (m.hayes@elec.canterbury.ac.nz,
                                    mhayes@redhat.com)

6
This file is part of GCC.
Jeff Law committed
7

8 9 10 11
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.
Jeff Law committed
12

13 14 15 16
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.
Jeff Law committed
17 18

You should have received a copy of the GNU General Public License
19 20 21
along with GCC; see the file COPYING.  If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
Jeff Law committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88


OVERVIEW:

This file provides some dataflow routines for computing reaching defs,
upward exposed uses, live variables, def-use chains, and use-def
chains.  The global dataflow is performed using simple iterative
methods with a worklist and could be sped up by ordering the blocks
with a depth first search order.

A `struct ref' data structure (ref) is allocated for every register
reference (def or use) and this records the insn and bb the ref is
found within.  The refs are linked together in chains of uses and defs
for each insn and for each register.  Each ref also has a chain field
that links all the use refs for a def or all the def refs for a use.
This is used to create use-def or def-use chains.


USAGE:

Here's an example of using the dataflow routines.

      struct df *df;

      df = df_init ();

      df_analyse (df, 0, DF_ALL);

      df_dump (df, DF_ALL, stderr);

      df_finish (df);


df_init simply creates a poor man's object (df) that needs to be
passed to all the dataflow routines.  df_finish destroys this
object and frees up any allocated memory.

df_analyse performs the following:

1. Records defs and uses by scanning the insns in each basic block
   or by scanning the insns queued by df_insn_modify.
2. Links defs and uses into insn-def and insn-use chains.
3. Links defs and uses into reg-def and reg-use chains.
4. Assigns LUIDs to each insn (for modified blocks).
5. Calculates local reaching definitions.
6. Calculates global reaching definitions.
7. Creates use-def chains.
8. Calculates local reaching uses (upwards exposed uses).
9. Calculates global reaching uses.
10. Creates def-use chains.
11. Calculates local live registers.
12. Calculates global live registers.
13. Calculates register lifetimes and determines local registers.


PHILOSOPHY:

Note that the dataflow information is not updated for every newly
deleted or created insn.  If the dataflow information requires
updating then all the changed, new, or deleted insns needs to be
marked with df_insn_modify (or df_insns_modify) either directly or
indirectly (say through calling df_insn_delete).  df_insn_modify
marks all the modified insns to get processed the next time df_analyse
 is called.

Beware that tinkering with insns may invalidate the dataflow information.
The philosophy behind these routines is that once the dataflow
89
information has been gathered, the user should store what they require
Jeff Law committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
before they tinker with any insn.  Once a reg is replaced, for example,
then the reg-def/reg-use chains will point to the wrong place.  Once a
whole lot of changes have been made, df_analyse can be called again
to update the dataflow information.  Currently, this is not very smart
with regard to propagating changes to the dataflow so it should not
be called very often.


DATA STRUCTURES:

The basic object is a REF (reference) and this may either be a DEF
(definition) or a USE of a register.

These are linked into a variety of lists; namely reg-def, reg-use,
  insn-def, insn-use, def-use, and use-def lists.  For example,
the reg-def lists contain all the refs that define a given register
while the insn-use lists contain all the refs used by an insn.

Note that the reg-def and reg-use chains are generally short (except for the
hard registers) and thus it is much faster to search these chains
110
rather than searching the def or use bitmaps.
Jeff Law committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

If the insns are in SSA form then the reg-def and use-def lists
should only contain the single defining ref.

TODO:

1) Incremental dataflow analysis.

Note that if a loop invariant insn is hoisted (or sunk), we do not
need to change the def-use or use-def chains.  All we have to do is to
change the bb field for all the associated defs and uses and to
renumber the LUIDs for the original and new basic blocks of the insn.

When shadowing loop mems we create new uses and defs for new pseudos
so we do not affect the existing dataflow information.

My current strategy is to queue up all modified, created, or deleted
insns so when df_analyse is called we can easily determine all the new
or deleted refs.  Currently the global dataflow information is
recomputed from scratch but this could be propagated more efficiently.

2) Improved global data flow computation using depth first search.

3) Reduced memory requirements.

We could operate a pool of ref structures.  When a ref is deleted it
gets returned to the pool (say by linking on to a chain of free refs).
This will require a pair of bitmaps for defs and uses so that we can
tell which ones have been changed.  Alternatively, we could
periodically squeeze the def and use tables and associated bitmaps and
renumber the def and use ids.

143
4) Ordering of reg-def and reg-use lists.
Jeff Law committed
144 145 146

Should the first entry in the def list be the first def (within a BB)?
Similarly, should the first entry in the use list be the last use
147
(within a BB)?
Jeff Law committed
148 149 150 151 152 153 154 155 156 157

5) Working with a sub-CFG.

Often the whole CFG does not need to be analysed, for example,
when optimising a loop, only certain registers are of interest.
Perhaps there should be a bitmap argument to df_analyse to specify
 which registers should be analysed?   */

#include "config.h"
#include "system.h"
158 159 160 161 162 163 164
#include "rtl.h"
#include "tm_p.h"
#include "insn-config.h"
#include "recog.h"
#include "function.h"
#include "regs.h"
#include "obstack.h"
Jeff Law committed
165 166
#include "hard-reg-set.h"
#include "basic-block.h"
167
#include "sbitmap.h"
Jeff Law committed
168 169
#include "bitmap.h"
#include "df.h"
170
#include "fibheap.h"
Jeff Law committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184

#define FOR_EACH_BB_IN_BITMAP(BITMAP, MIN, BB, CODE)		\
do {								\
  unsigned int node_;						\
  EXECUTE_IF_SET_IN_BITMAP (BITMAP, MIN, node_, 		\
    {(BB) = BASIC_BLOCK (node_); CODE;});} while (0)

static struct obstack df_ref_obstack;
static struct df *ddf;

static void df_reg_table_realloc PARAMS((struct df *, int));
#if 0
static void df_def_table_realloc PARAMS((struct df *, int));
#endif
185
static void df_insn_table_realloc PARAMS((struct df *, unsigned int));
Jeff Law committed
186 187 188 189 190 191 192 193
static void df_bitmaps_alloc PARAMS((struct df *, int));
static void df_bitmaps_free PARAMS((struct df *, int));
static void df_free PARAMS((struct df *));
static void df_alloc PARAMS((struct df *, int));

static rtx df_reg_clobber_gen PARAMS((unsigned int));
static rtx df_reg_use_gen PARAMS((unsigned int));

194
static inline struct df_link *df_link_create PARAMS((struct ref *,
Jeff Law committed
195 196 197 198 199 200
						     struct df_link *));
static struct df_link *df_ref_unlink PARAMS((struct df_link **, struct ref *));
static void df_def_unlink PARAMS((struct df *, struct ref *));
static void df_use_unlink PARAMS((struct df *, struct ref *));
static void df_insn_refs_unlink PARAMS ((struct df *, basic_block, rtx));
#if 0
201
static void df_bb_refs_unlink PARAMS ((struct df *, basic_block));
Jeff Law committed
202 203 204
static void df_refs_unlink PARAMS ((struct df *, bitmap));
#endif

205
static struct ref *df_ref_create PARAMS((struct df *,
206
					 rtx, rtx *, rtx,
207
					 enum df_ref_type, enum df_ref_flags));
208
static void df_ref_record_1 PARAMS((struct df *, rtx, rtx *,
209
				    rtx, enum df_ref_type,
210
				    enum df_ref_flags));
211
static void df_ref_record PARAMS((struct df *, rtx, rtx *,
212
				  rtx, enum df_ref_type,
213
				  enum df_ref_flags));
Jeff Law committed
214 215 216
static void df_def_record_1 PARAMS((struct df *, rtx, basic_block, rtx));
static void df_defs_record PARAMS((struct df *, rtx, basic_block, rtx));
static void df_uses_record PARAMS((struct df *, rtx *,
217 218
				   enum df_ref_type, basic_block, rtx,
				   enum df_ref_flags));
Jeff Law committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
static void df_insn_refs_record PARAMS((struct df *, basic_block, rtx));
static void df_bb_refs_record PARAMS((struct df *, basic_block));
static void df_refs_record PARAMS((struct df *, bitmap));

static void df_bb_reg_def_chain_create PARAMS((struct df *, basic_block));
static void df_reg_def_chain_create PARAMS((struct df *, bitmap));
static void df_bb_reg_use_chain_create PARAMS((struct df *, basic_block));
static void df_reg_use_chain_create PARAMS((struct df *, bitmap));
static void df_bb_du_chain_create PARAMS((struct df *, basic_block, bitmap));
static void df_du_chain_create PARAMS((struct df *, bitmap));
static void df_bb_ud_chain_create PARAMS((struct df *, basic_block));
static void df_ud_chain_create PARAMS((struct df *, bitmap));
static void df_bb_rd_local_compute PARAMS((struct df *, basic_block));
static void df_rd_local_compute PARAMS((struct df *, bitmap));
static void df_bb_ru_local_compute PARAMS((struct df *, basic_block));
static void df_ru_local_compute PARAMS((struct df *, bitmap));
static void df_bb_lr_local_compute PARAMS((struct df *, basic_block));
static void df_lr_local_compute PARAMS((struct df *, bitmap));
static void df_bb_reg_info_compute PARAMS((struct df *, basic_block, bitmap));
static void df_reg_info_compute PARAMS((struct df *, bitmap));

static int df_bb_luids_set PARAMS((struct df *df, basic_block));
static int df_luids_set PARAMS((struct df *df, bitmap));

static int df_modified_p PARAMS ((struct df *, bitmap));
static int df_refs_queue PARAMS ((struct df *));
static int df_refs_process PARAMS ((struct df *));
static int df_bb_refs_update PARAMS ((struct df *, basic_block));
static int df_refs_update PARAMS ((struct df *));
static void df_analyse_1 PARAMS((struct df *, bitmap, int, int));

static void df_insns_modify PARAMS((struct df *, basic_block,
				    rtx, rtx));
static int df_rtx_mem_replace PARAMS ((rtx *, void *));
static int df_rtx_reg_replace PARAMS ((rtx *, void *));
void df_refs_reg_replace PARAMS ((struct df *, bitmap,
					 struct df_link *, rtx, rtx));

static int df_def_dominates_all_uses_p PARAMS((struct df *, struct ref *def));
static int df_def_dominates_uses_p PARAMS((struct df *,
					   struct ref *def, bitmap));
static struct ref *df_bb_regno_last_use_find PARAMS((struct df *, basic_block,
						     unsigned int));
static struct ref *df_bb_regno_first_def_find PARAMS((struct df *, basic_block,
						      unsigned int));
static struct ref *df_bb_insn_regno_last_use_find PARAMS((struct df *,
							  basic_block,
							  rtx, unsigned int));
static struct ref *df_bb_insn_regno_first_def_find PARAMS((struct df *,
							   basic_block,
							   rtx, unsigned int));

static void df_chain_dump PARAMS((struct df_link *, FILE *file));
static void df_chain_dump_regno PARAMS((struct df_link *, FILE *file));
static void df_regno_debug PARAMS ((struct df *, unsigned int, FILE *));
static void df_ref_debug PARAMS ((struct df *, struct ref *, FILE *));
Kazu Hirata committed
275
static void df_rd_transfer_function PARAMS ((int, int *, bitmap, bitmap,
276
					     bitmap, bitmap, void *));
Kazu Hirata committed
277
static void df_ru_transfer_function PARAMS ((int, int *, bitmap, bitmap,
278
					     bitmap, bitmap, void *));
Kazu Hirata committed
279
static void df_lr_transfer_function PARAMS ((int, int *, bitmap, bitmap,
280
					     bitmap, bitmap, void *));
Kazu Hirata committed
281 282 283 284
static void hybrid_search_bitmap PARAMS ((basic_block, bitmap *, bitmap *,
					  bitmap *, bitmap *, enum df_flow_dir,
					  enum df_confluence_op,
					  transfer_function_bitmap,
285 286 287 288 289 290
					  sbitmap, sbitmap, void *));
static void hybrid_search_sbitmap PARAMS ((basic_block, sbitmap *, sbitmap *,
					   sbitmap *, sbitmap *, enum df_flow_dir,
					   enum df_confluence_op,
					   transfer_function_sbitmap,
					   sbitmap, sbitmap, void *));
291
static inline bool read_modify_subreg_p PARAMS ((rtx));
Jeff Law committed
292 293 294 295 296


/* Local memory allocation/deallocation routines.  */


297 298
/* Increase the insn info table to have space for at least SIZE + 1
   elements.  */
Jeff Law committed
299 300 301
static void
df_insn_table_realloc (df, size)
     struct df *df;
302
     unsigned int size;
Jeff Law committed
303
{
304 305 306
  size++;
  if (size <= df->insn_size)
    return;
Jeff Law committed
307

308 309 310
  /* Make the table a little larger than requested, so we don't need
     to enlarge it so often.  */
  size += df->insn_size / 4;
311

Jeff Law committed
312 313
  df->insns = (struct insn_info *)
    xrealloc (df->insns, size * sizeof (struct insn_info));
314 315

  memset (df->insns + df->insn_size, 0,
Jeff Law committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
	  (size - df->insn_size) * sizeof (struct insn_info));

  df->insn_size = size;

  if (! df->insns_modified)
    {
      df->insns_modified = BITMAP_XMALLOC ();
      bitmap_zero (df->insns_modified);
    }
}


/* Increase the reg info table by SIZE more elements.  */
static void
df_reg_table_realloc (df, size)
     struct df *df;
     int size;
{
  /* Make table 25 percent larger by default.  */
  if (! size)
    size = df->reg_size / 4;

  size += df->reg_size;
339 340
  if (size < max_reg_num ())
    size = max_reg_num ();
Jeff Law committed
341 342 343 344 345

  df->regs = (struct reg_info *)
    xrealloc (df->regs, size * sizeof (struct reg_info));

  /* Zero the new entries.  */
346
  memset (df->regs + df->reg_size, 0,
Jeff Law committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
	  (size - df->reg_size) * sizeof (struct reg_info));

  df->reg_size = size;
}


#if 0
/* Not currently used.  */
static void
df_def_table_realloc (df, size)
     struct df *df;
     int size;
{
  int i;
  struct ref *refs;

  /* Make table 25 percent larger by default.  */
  if (! size)
    size = df->def_size / 4;

  df->def_size += size;
368
  df->defs = xrealloc (df->defs,
Jeff Law committed
369 370 371 372 373 374
		       df->def_size * sizeof (*df->defs));

  /* Allocate a new block of memory and link into list of blocks
     that will need to be freed later.  */

  refs = xmalloc (size * sizeof (*refs));
375

Jeff Law committed
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
  /* Link all the new refs together, overloading the chain field.  */
  for (i = 0; i < size - 1; i++)
      refs[i].chain = (struct df_link *)(refs + i + 1);
  refs[size - 1].chain = 0;
}
#endif



/* Allocate bitmaps for each basic block.  */
static void
df_bitmaps_alloc (df, flags)
     struct df *df;
     int flags;
{
  int dflags = 0;
392
  basic_block bb;
Jeff Law committed
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

  /* Free the bitmaps if they need resizing.  */
  if ((flags & DF_LR) && df->n_regs < (unsigned int)max_reg_num ())
    dflags |= DF_LR | DF_RU;
  if ((flags & DF_RU) && df->n_uses < df->use_id)
    dflags |= DF_RU;
  if ((flags & DF_RD) && df->n_defs < df->def_id)
    dflags |= DF_RD;

  if (dflags)
    df_bitmaps_free (df, dflags);

  df->n_defs = df->def_id;
  df->n_uses = df->use_id;

408
  FOR_EACH_BB (bb)
Jeff Law committed
409 410
    {
      struct bb_info *bb_info = DF_BB_INFO (df, bb);
411

Jeff Law committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
      if (flags & DF_RD && ! bb_info->rd_in)
	{
	  /* Allocate bitmaps for reaching definitions.  */
	  bb_info->rd_kill = BITMAP_XMALLOC ();
	  bitmap_zero (bb_info->rd_kill);
	  bb_info->rd_gen = BITMAP_XMALLOC ();
	  bitmap_zero (bb_info->rd_gen);
	  bb_info->rd_in = BITMAP_XMALLOC ();
	  bb_info->rd_out = BITMAP_XMALLOC ();
	  bb_info->rd_valid = 0;
	}

      if (flags & DF_RU && ! bb_info->ru_in)
	{
	  /* Allocate bitmaps for upward exposed uses.  */
	  bb_info->ru_kill = BITMAP_XMALLOC ();
	  bitmap_zero (bb_info->ru_kill);
	  /* Note the lack of symmetry.  */
	  bb_info->ru_gen = BITMAP_XMALLOC ();
	  bitmap_zero (bb_info->ru_gen);
	  bb_info->ru_in = BITMAP_XMALLOC ();
	  bb_info->ru_out = BITMAP_XMALLOC ();
	  bb_info->ru_valid = 0;
	}

      if (flags & DF_LR && ! bb_info->lr_in)
	{
	  /* Allocate bitmaps for live variables.  */
	  bb_info->lr_def = BITMAP_XMALLOC ();
	  bitmap_zero (bb_info->lr_def);
	  bb_info->lr_use = BITMAP_XMALLOC ();
	  bitmap_zero (bb_info->lr_use);
	  bb_info->lr_in = BITMAP_XMALLOC ();
	  bb_info->lr_out = BITMAP_XMALLOC ();
	  bb_info->lr_valid = 0;
	}
    }
}


/* Free bitmaps for each basic block.  */
static void
df_bitmaps_free (df, flags)
     struct df *df ATTRIBUTE_UNUSED;
     int flags;
{
458
  basic_block bb;
Jeff Law committed
459

460
  FOR_EACH_BB (bb)
Jeff Law committed
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
    {
      struct bb_info *bb_info = DF_BB_INFO (df, bb);

      if (!bb_info)
	continue;

      if ((flags & DF_RD) && bb_info->rd_in)
	{
	  /* Free bitmaps for reaching definitions.  */
	  BITMAP_XFREE (bb_info->rd_kill);
	  bb_info->rd_kill = NULL;
	  BITMAP_XFREE (bb_info->rd_gen);
	  bb_info->rd_gen = NULL;
	  BITMAP_XFREE (bb_info->rd_in);
	  bb_info->rd_in = NULL;
	  BITMAP_XFREE (bb_info->rd_out);
	  bb_info->rd_out = NULL;
	}

      if ((flags & DF_RU) && bb_info->ru_in)
	{
	  /* Free bitmaps for upward exposed uses.  */
	  BITMAP_XFREE (bb_info->ru_kill);
	  bb_info->ru_kill = NULL;
	  BITMAP_XFREE (bb_info->ru_gen);
	  bb_info->ru_gen = NULL;
	  BITMAP_XFREE (bb_info->ru_in);
	  bb_info->ru_in = NULL;
	  BITMAP_XFREE (bb_info->ru_out);
	  bb_info->ru_out = NULL;
	}

      if ((flags & DF_LR) && bb_info->lr_in)
	{
	  /* Free bitmaps for live variables.  */
	  BITMAP_XFREE (bb_info->lr_def);
	  bb_info->lr_def = NULL;
	  BITMAP_XFREE (bb_info->lr_use);
	  bb_info->lr_use = NULL;
	  BITMAP_XFREE (bb_info->lr_in);
	  bb_info->lr_in = NULL;
	  BITMAP_XFREE (bb_info->lr_out);
	  bb_info->lr_out = NULL;
	}
    }
  df->flags &= ~(flags & (DF_RD | DF_RU | DF_LR));
}


/* Allocate and initialise dataflow memory.  */
static void
df_alloc (df, n_regs)
     struct df *df;
     int n_regs;
{
  int n_insns;
517
  basic_block bb;
Jeff Law committed
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537

  gcc_obstack_init (&df_ref_obstack);

  /* Perhaps we should use LUIDs to save memory for the insn_refs
     table.  This is only a small saving; a few pointers.  */
  n_insns = get_max_uid () + 1;

  df->def_id = 0;
  df->n_defs = 0;
  /* Approximate number of defs by number of insns.  */
  df->def_size = n_insns;
  df->defs = xmalloc (df->def_size * sizeof (*df->defs));

  df->use_id = 0;
  df->n_uses = 0;
  /* Approximate number of uses by twice number of insns.  */
  df->use_size = n_insns * 2;
  df->uses = xmalloc (df->use_size * sizeof (*df->uses));

  df->n_regs = n_regs;
538
  df->n_bbs = last_basic_block;
Jeff Law committed
539 540 541 542 543 544 545 546 547 548 549 550 551

  /* Allocate temporary working array used during local dataflow analysis.  */
  df->reg_def_last = xmalloc (df->n_regs * sizeof (struct ref *));

  df_insn_table_realloc (df, n_insns);

  df_reg_table_realloc (df, df->n_regs);

  df->bbs_modified = BITMAP_XMALLOC ();
  bitmap_zero (df->bbs_modified);

  df->flags = 0;

552
  df->bbs = xcalloc (last_basic_block, sizeof (struct bb_info));
Jeff Law committed
553 554

  df->all_blocks = BITMAP_XMALLOC ();
555 556
  FOR_EACH_BB (bb)
    bitmap_set_bit (df->all_blocks, bb->index);
Jeff Law committed
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
}


/* Free all the dataflow info.  */
static void
df_free (df)
     struct df *df;
{
  df_bitmaps_free (df, DF_ALL);

  if (df->bbs)
    free (df->bbs);
  df->bbs = 0;

  if (df->insns)
    free (df->insns);
  df->insns = 0;
  df->insn_size = 0;

  if (df->defs)
    free (df->defs);
  df->defs = 0;
  df->def_size = 0;
  df->def_id = 0;

  if (df->uses)
    free (df->uses);
  df->uses = 0;
  df->use_size = 0;
  df->use_id = 0;

  if (df->regs)
    free (df->regs);
  df->regs = 0;
  df->reg_size = 0;

  if (df->bbs_modified)
    BITMAP_XFREE (df->bbs_modified);
  df->bbs_modified = 0;

  if (df->insns_modified)
    BITMAP_XFREE (df->insns_modified);
  df->insns_modified = 0;

  BITMAP_XFREE (df->all_blocks);
  df->all_blocks = 0;

  obstack_free (&df_ref_obstack, NULL);
}

/* Local miscellaneous routines.  */

/* Return a USE for register REGNO.  */
static rtx df_reg_use_gen (regno)
     unsigned int regno;
{
  rtx reg;
  rtx use;

616
  reg = regno_reg_rtx[regno];
617

Jeff Law committed
618 619 620 621 622 623 624 625 626 627 628 629
  use = gen_rtx_USE (GET_MODE (reg), reg);
  return use;
}


/* Return a CLOBBER for register REGNO.  */
static rtx df_reg_clobber_gen (regno)
     unsigned int regno;
{
  rtx reg;
  rtx use;

630
  reg = regno_reg_rtx[regno];
Jeff Law committed
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645

  use = gen_rtx_CLOBBER (GET_MODE (reg), reg);
  return use;
}

/* Local chain manipulation routines.  */

/* Create a link in a def-use or use-def chain.  */
static inline struct df_link *
df_link_create (ref, next)
     struct ref *ref;
     struct df_link *next;
{
  struct df_link *link;

646
  link = (struct df_link *) obstack_alloc (&df_ref_obstack,
Jeff Law committed
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
					   sizeof (*link));
  link->next = next;
  link->ref = ref;
  return link;
}


/* Add REF to chain head pointed to by PHEAD.  */
static struct df_link *
df_ref_unlink (phead, ref)
     struct df_link **phead;
     struct ref *ref;
{
  struct df_link *link = *phead;

  if (link)
    {
      if (! link->next)
	{
	  /* Only a single ref.  It must be the one we want.
	     If not, the def-use and use-def chains are likely to
	     be inconsistent.  */
	  if (link->ref != ref)
	    abort ();
	  /* Now have an empty chain.  */
	  *phead = NULL;
	}
      else
	{
	  /* Multiple refs.  One of them must be us.  */
	  if (link->ref == ref)
	    *phead = link->next;
	  else
	    {
	      /* Follow chain.  */
	      for (; link->next; link = link->next)
		{
		  if (link->next->ref == ref)
		    {
		      /* Unlink from list.  */
		      link->next = link->next->next;
		      return link->next;
		    }
		}
	    }
	}
    }
  return link;
}


/* Unlink REF from all def-use/use-def chains, etc.  */
int
df_ref_remove (df, ref)
     struct df *df;
     struct ref *ref;
{
  if (DF_REF_REG_DEF_P (ref))
    {
      df_def_unlink (df, ref);
      df_ref_unlink (&df->insns[DF_REF_INSN_UID (ref)].defs, ref);
    }
  else
    {
      df_use_unlink (df, ref);
      df_ref_unlink (&df->insns[DF_REF_INSN_UID (ref)].uses, ref);
    }
  return 1;
}


/* Unlink DEF from use-def and reg-def chains.  */
719
static void
Jeff Law committed
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
df_def_unlink (df, def)
     struct df *df ATTRIBUTE_UNUSED;
     struct ref *def;
{
  struct df_link *du_link;
  unsigned int dregno = DF_REF_REGNO (def);

  /* Follow def-use chain to find all the uses of this def.  */
  for (du_link = DF_REF_CHAIN (def); du_link; du_link = du_link->next)
    {
      struct ref *use = du_link->ref;

      /* Unlink this def from the use-def chain.  */
      df_ref_unlink (&DF_REF_CHAIN (use), def);
    }
  DF_REF_CHAIN (def) = 0;

  /* Unlink def from reg-def chain.  */
  df_ref_unlink (&df->regs[dregno].defs, def);

  df->defs[DF_REF_ID (def)] = 0;
}


/* Unlink use from def-use and reg-use chains.  */
745
static void
Jeff Law committed
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
df_use_unlink (df, use)
     struct df *df ATTRIBUTE_UNUSED;
     struct ref *use;
{
  struct df_link *ud_link;
  unsigned int uregno = DF_REF_REGNO (use);

  /* Follow use-def chain to find all the defs of this use.  */
  for (ud_link = DF_REF_CHAIN (use); ud_link; ud_link = ud_link->next)
    {
      struct ref *def = ud_link->ref;

      /* Unlink this use from the def-use chain.  */
      df_ref_unlink (&DF_REF_CHAIN (def), use);
    }
  DF_REF_CHAIN (use) = 0;

  /* Unlink use from reg-use chain.  */
  df_ref_unlink (&df->regs[uregno].uses, use);

  df->uses[DF_REF_ID (use)] = 0;
}

/* Local routines for recording refs.  */


/* Create a new ref of type DF_REF_TYPE for register REG at address
   LOC within INSN of BB.  */
static struct ref *
775
df_ref_create (df, reg, loc, insn, ref_type, ref_flags)
776
     struct df *df;
Jeff Law committed
777 778 779 780
     rtx reg;
     rtx *loc;
     rtx insn;
     enum df_ref_type ref_type;
781
     enum df_ref_flags ref_flags;
Jeff Law committed
782 783 784
{
  struct ref *this_ref;
  unsigned int uid;
785 786

  this_ref = (struct ref *) obstack_alloc (&df_ref_obstack,
Jeff Law committed
787 788 789 790 791 792
					   sizeof (*this_ref));
  DF_REF_REG (this_ref) = reg;
  DF_REF_LOC (this_ref) = loc;
  DF_REF_INSN (this_ref) = insn;
  DF_REF_CHAIN (this_ref) = 0;
  DF_REF_TYPE (this_ref) = ref_type;
793
  DF_REF_FLAGS (this_ref) = ref_flags;
Jeff Law committed
794 795 796 797 798 799 800 801
  uid = INSN_UID (insn);

  if (ref_type == DF_REF_REG_DEF)
    {
      if (df->def_id >= df->def_size)
	{
	  /* Make table 25 percent larger.  */
	  df->def_size += (df->def_size / 4);
802
	  df->defs = xrealloc (df->defs,
Jeff Law committed
803 804 805 806 807 808 809 810 811 812 813
			       df->def_size * sizeof (*df->defs));
	}
      DF_REF_ID (this_ref) = df->def_id;
      df->defs[df->def_id++] = this_ref;
    }
  else
    {
      if (df->use_id >= df->use_size)
	{
	  /* Make table 25 percent larger.  */
	  df->use_size += (df->use_size / 4);
814
	  df->uses = xrealloc (df->uses,
Jeff Law committed
815 816 817 818 819 820 821 822 823 824 825 826
			       df->use_size * sizeof (*df->uses));
	}
      DF_REF_ID (this_ref) = df->use_id;
      df->uses[df->use_id++] = this_ref;
    }
  return this_ref;
}


/* Create a new reference of type DF_REF_TYPE for a single register REG,
   used inside the LOC rtx of INSN.  */
static void
827
df_ref_record_1 (df, reg, loc, insn, ref_type, ref_flags)
Jeff Law committed
828 829 830 831 832
     struct df *df;
     rtx reg;
     rtx *loc;
     rtx insn;
     enum df_ref_type ref_type;
833
     enum df_ref_flags ref_flags;
Jeff Law committed
834
{
835
  df_ref_create (df, reg, loc, insn, ref_type, ref_flags);
Jeff Law committed
836 837 838 839 840 841
}


/* Create new references of type DF_REF_TYPE for each part of register REG
   at address LOC within INSN of BB.  */
static void
842
df_ref_record (df, reg, loc, insn, ref_type, ref_flags)
Jeff Law committed
843 844 845 846 847
     struct df *df;
     rtx reg;
     rtx *loc;
     rtx insn;
     enum df_ref_type ref_type;
848
     enum df_ref_flags ref_flags;
Jeff Law committed
849 850 851 852 853 854 855 856 857 858 859 860 861
{
  unsigned int regno;

  if (GET_CODE (reg) != REG && GET_CODE (reg) != SUBREG)
    abort ();

  /* For the reg allocator we are interested in some SUBREG rtx's, but not
     all.  Notably only those representing a word extraction from a multi-word
     reg.  As written in the docu those should have the form
     (subreg:SI (reg:M A) N), with size(SImode) > size(Mmode).
     XXX Is that true?  We could also use the global word_mode variable.  */
  if (GET_CODE (reg) == SUBREG
      && (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (word_mode)
Kazu Hirata committed
862
	  || GET_MODE_SIZE (GET_MODE (reg))
Jeff Law committed
863 864 865 866 867 868 869 870 871 872 873
	       >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (reg)))))
    {
      loc = &SUBREG_REG (reg);
      reg = *loc;
    }

  regno = REGNO (GET_CODE (reg) == SUBREG ? SUBREG_REG (reg) : reg);
  if (regno < FIRST_PSEUDO_REGISTER)
    {
      int i;
      int endregno;
874

Jeff Law committed
875 876 877 878 879 880 881 882
      if (! (df->flags & DF_HARD_REGS))
	return;

      /* GET_MODE (reg) is correct here.  We don't want to go into a SUBREG
         for the mode, because we only want to add references to regs, which
	 are really referenced.  E.g. a (subreg:SI (reg:DI 0) 0) does _not_
	 reference the whole reg 0 in DI mode (which would also include
	 reg 1, at least, if 0 and 1 are SImode registers).  */
883 884 885 886 887
      endregno = HARD_REGNO_NREGS (regno, GET_MODE (reg));
      if (GET_CODE (reg) == SUBREG)
        regno += subreg_regno_offset (regno, GET_MODE (SUBREG_REG (reg)),
				      SUBREG_BYTE (reg), GET_MODE (reg));
      endregno += regno;
Jeff Law committed
888 889

      for (i = regno; i < endregno; i++)
890
	df_ref_record_1 (df, regno_reg_rtx[i],
891
			 loc, insn, ref_type, ref_flags);
Jeff Law committed
892 893 894
    }
  else
    {
895
      df_ref_record_1 (df, reg, loc, insn, ref_type, ref_flags);
Jeff Law committed
896 897 898
    }
}

899 900
/* Writes to paradoxical subregs, or subregs which are too narrow
   are read-modify-write.  */
901 902 903 904 905

static inline bool
read_modify_subreg_p (x)
     rtx x;
{
906
  unsigned int isize, osize;
907 908
  if (GET_CODE (x) != SUBREG)
    return false;
909 910 911 912 913
  isize = GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)));
  osize = GET_MODE_SIZE (GET_MODE (x));
  if (isize <= osize)
    return true;
  if (isize <= UNITS_PER_WORD)
914
    return false;
915
  if (osize >= UNITS_PER_WORD)
916 917 918 919
    return false;
  return true;
}

Jeff Law committed
920 921 922 923 924 925 926 927 928 929
/* Process all the registers defined in the rtx, X.  */
static void
df_def_record_1 (df, x, bb, insn)
     struct df *df;
     rtx x;
     basic_block bb;
     rtx insn;
{
  rtx *loc = &SET_DEST (x);
  rtx dst = *loc;
930
  enum df_ref_flags flags = 0;
Jeff Law committed
931 932 933 934 935 936 937 938 939 940 941 942

  /* Some targets place small structures in registers for
     return values of functions.  */
  if (GET_CODE (dst) == PARALLEL && GET_MODE (dst) == BLKmode)
    {
      int i;

      for (i = XVECLEN (dst, 0) - 1; i >= 0; i--)
	  df_def_record_1 (df, XVECEXP (dst, 0, i), bb, insn);
      return;
    }

943 944 945 946 947 948 949
#ifdef CLASS_CANNOT_CHANGE_MODE
  if (GET_CODE (dst) == SUBREG
      && CLASS_CANNOT_CHANGE_MODE_P (GET_MODE (dst),
				     GET_MODE (SUBREG_REG (dst))))
    flags |= DF_REF_MODE_CHANGE;
#endif

Jeff Law committed
950 951 952
  /* May be, we should flag the use of strict_low_part somehow.  Might be
     handy for the reg allocator.  */
  while (GET_CODE (dst) == STRICT_LOW_PART
Kazu Hirata committed
953
	 || GET_CODE (dst) == ZERO_EXTRACT
Jeff Law committed
954
	 || GET_CODE (dst) == SIGN_EXTRACT
955
	 || read_modify_subreg_p (dst))
Jeff Law committed
956
    {
957 958
      /* Strict low part always contains SUBREG, but we don't want to make
	 it appear outside, as whole register is always considered.  */
959 960 961 962 963
      if (GET_CODE (dst) == STRICT_LOW_PART)
	{
	  loc = &XEXP (dst, 0);
	  dst = *loc;
	}
964 965 966 967 968 969
#ifdef CLASS_CANNOT_CHANGE_MODE
      if (GET_CODE (dst) == SUBREG
	  && CLASS_CANNOT_CHANGE_MODE_P (GET_MODE (dst),
				         GET_MODE (SUBREG_REG (dst))))
        flags |= DF_REF_MODE_CHANGE;
#endif
Jeff Law committed
970 971
      loc = &XEXP (dst, 0);
      dst = *loc;
972
      flags |= DF_REF_READ_WRITE;
Jeff Law committed
973
    }
Kazu Hirata committed
974

975
    if (GET_CODE (dst) == REG
Kazu Hirata committed
976
	|| (GET_CODE (dst) == SUBREG && GET_CODE (SUBREG_REG (dst)) == REG))
977
      df_ref_record (df, dst, loc, insn, DF_REF_REG_DEF, flags);
Jeff Law committed
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
}


/* Process all the registers defined in the pattern rtx, X.  */
static void
df_defs_record (df, x, bb, insn)
     struct df *df;
     rtx x;
     basic_block bb;
     rtx insn;
{
  RTX_CODE code = GET_CODE (x);

  if (code == SET || code == CLOBBER)
    {
      /* Mark the single def within the pattern.  */
      df_def_record_1 (df, x, bb, insn);
    }
  else if (code == PARALLEL)
    {
      int i;

      /* Mark the multiple defs within the pattern.  */
      for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
	{
	  code = GET_CODE (XVECEXP (x, 0, i));
	  if (code == SET || code == CLOBBER)
	    df_def_record_1 (df, XVECEXP (x, 0, i), bb, insn);
	}
    }
}


/* Process all the registers used in the rtx at address LOC.  */
static void
1013
df_uses_record (df, loc, ref_type, bb, insn, flags)
Jeff Law committed
1014 1015 1016 1017 1018
     struct df *df;
     rtx *loc;
     enum df_ref_type ref_type;
     basic_block bb;
     rtx insn;
1019
     enum df_ref_flags flags;
Jeff Law committed
1020 1021 1022 1023 1024
{
  RTX_CODE code;
  rtx x;
 retry:
  x = *loc;
1025 1026
  if (!x)
    return;
Jeff Law committed
1027 1028 1029 1030 1031 1032 1033 1034
  code = GET_CODE (x);
  switch (code)
    {
    case LABEL_REF:
    case SYMBOL_REF:
    case CONST_INT:
    case CONST:
    case CONST_DOUBLE:
1035
    case CONST_VECTOR:
Jeff Law committed
1036 1037 1038 1039 1040 1041 1042 1043 1044
    case PC:
    case ADDR_VEC:
    case ADDR_DIFF_VEC:
      return;

    case CLOBBER:
      /* If we are clobbering a MEM, mark any registers inside the address
	 as being used.  */
      if (GET_CODE (XEXP (x, 0)) == MEM)
1045
	df_uses_record (df, &XEXP (XEXP (x, 0), 0),
1046
			DF_REF_REG_MEM_STORE, bb, insn, flags);
Jeff Law committed
1047 1048 1049 1050 1051

      /* If we're clobbering a REG then we have a def so ignore.  */
      return;

    case MEM:
1052
      df_uses_record (df, &XEXP (x, 0), DF_REF_REG_MEM_LOAD, bb, insn, flags);
Jeff Law committed
1053 1054 1055 1056 1057 1058 1059 1060 1061
      return;

    case SUBREG:
      /* While we're here, optimize this case.  */

      /* In case the SUBREG is not of a register, don't optimize.  */
      if (GET_CODE (SUBREG_REG (x)) != REG)
	{
	  loc = &SUBREG_REG (x);
1062
	  df_uses_record (df, loc, ref_type, bb, insn, flags);
Jeff Law committed
1063 1064
	  return;
	}
1065 1066 1067 1068 1069
#ifdef CLASS_CANNOT_CHANGE_MODE
      if (CLASS_CANNOT_CHANGE_MODE_P (GET_MODE (x),
				      GET_MODE (SUBREG_REG (x))))
        flags |= DF_REF_MODE_CHANGE;
#endif
1070

Jeff Law committed
1071 1072 1073 1074
      /* ... Fall through ...  */

    case REG:
      /* See a register (or subreg) other than being set.  */
1075
      df_ref_record (df, x, loc, insn, ref_type, flags);
Jeff Law committed
1076 1077 1078 1079 1080 1081
      return;

    case SET:
      {
	rtx dst = SET_DEST (x);

1082
	df_uses_record (df, &SET_SRC (x), DF_REF_REG_USE, bb, insn, 0);
1083

1084
	switch (GET_CODE (dst))
Jeff Law committed
1085
	  {
1086
	    enum df_ref_flags use_flags;
1087 1088 1089
	    case SUBREG:
	      if (read_modify_subreg_p (dst))
		{
1090 1091 1092 1093 1094 1095
		  use_flags = DF_REF_READ_WRITE;
#ifdef CLASS_CANNOT_CHANGE_MODE
		  if (CLASS_CANNOT_CHANGE_MODE_P (GET_MODE (dst),
						  GET_MODE (SUBREG_REG (dst))))
		    use_flags |= DF_REF_MODE_CHANGE;
#endif
1096
		  df_uses_record (df, &SUBREG_REG (dst), DF_REF_REG_USE, bb,
1097
				  insn, use_flags);
1098 1099
		  break;
		}
1100
	      /* ... FALLTHRU ...  */
1101 1102
	    case REG:
	    case PC:
1103
	    case PARALLEL:
1104 1105
	      break;
	    case MEM:
Kazu Hirata committed
1106
	      df_uses_record (df, &XEXP (dst, 0),
1107 1108 1109 1110 1111 1112 1113 1114
			      DF_REF_REG_MEM_STORE,
			      bb, insn, 0);
	      break;
	    case STRICT_LOW_PART:
	      /* A strict_low_part uses the whole reg not only the subreg.  */
	      dst = XEXP (dst, 0);
	      if (GET_CODE (dst) != SUBREG)
		abort ();
1115 1116 1117 1118 1119 1120
	      use_flags = DF_REF_READ_WRITE;
#ifdef CLASS_CANNOT_CHANGE_MODE
	      if (CLASS_CANNOT_CHANGE_MODE_P (GET_MODE (dst),
					      GET_MODE (SUBREG_REG (dst))))
		use_flags |= DF_REF_MODE_CHANGE;
#endif
1121
	      df_uses_record (df, &SUBREG_REG (dst), DF_REF_REG_USE, bb,
1122
			     insn, use_flags);
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
	      break;
	    case ZERO_EXTRACT:
	    case SIGN_EXTRACT:
	      df_uses_record (df, &XEXP (dst, 0), DF_REF_REG_USE, bb, insn,
			      DF_REF_READ_WRITE);
	      df_uses_record (df, &XEXP (dst, 1), DF_REF_REG_USE, bb, insn, 0);
	      df_uses_record (df, &XEXP (dst, 2), DF_REF_REG_USE, bb, insn, 0);
	      dst = XEXP (dst, 0);
	      break;
	    default:
	      abort ();
Jeff Law committed
1134
	  }
1135
	return;
Jeff Law committed
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
      }

    case RETURN:
      break;

    case ASM_OPERANDS:
    case UNSPEC_VOLATILE:
    case TRAP_IF:
    case ASM_INPUT:
      {
	/* Traditional and volatile asm instructions must be considered to use
	   and clobber all hard registers, all pseudo-registers and all of
	   memory.  So must TRAP_IF and UNSPEC_VOLATILE operations.

	   Consider for instance a volatile asm that changes the fpu rounding
	   mode.  An insn should not be moved across this even if it only uses
1152
	   pseudo-regs because it might give an incorrectly rounded result.
Jeff Law committed
1153 1154 1155 1156

	   For now, just mark any regs we can find in ASM_OPERANDS as
	   used.  */

Kazu Hirata committed
1157
	/* For all ASM_OPERANDS, we must traverse the vector of input operands.
Jeff Law committed
1158 1159 1160 1161 1162 1163 1164 1165
	   We can not just fall through here since then we would be confused
	   by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
	   traditional asms unlike their normal usage.  */
	if (code == ASM_OPERANDS)
	  {
	    int j;

	    for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
1166
	      df_uses_record (df, &ASM_OPERANDS_INPUT (x, j),
1167
			      DF_REF_REG_USE, bb, insn, 0);
1168
	    return;
Jeff Law committed
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
	  }
	break;
      }

    case PRE_DEC:
    case POST_DEC:
    case PRE_INC:
    case POST_INC:
    case PRE_MODIFY:
    case POST_MODIFY:
      /* Catch the def of the register being modified.  */
1180
      df_ref_record (df, XEXP (x, 0), &XEXP (x, 0), insn, DF_REF_REG_DEF, DF_REF_READ_WRITE);
Jeff Law committed
1181

1182
      /* ... Fall through to handle uses ...  */
Jeff Law committed
1183 1184 1185 1186 1187 1188 1189

    default:
      break;
    }

  /* Recursively scan the operands of this expression.  */
  {
1190
    const char *fmt = GET_RTX_FORMAT (code);
Jeff Law committed
1191
    int i;
1192

Jeff Law committed
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
    for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
      {
	if (fmt[i] == 'e')
	  {
	    /* Tail recursive case: save a function call level.  */
	    if (i == 0)
	      {
		loc = &XEXP (x, 0);
		goto retry;
	      }
1203
	    df_uses_record (df, &XEXP (x, i), ref_type, bb, insn, flags);
Jeff Law committed
1204 1205 1206 1207 1208 1209
	  }
	else if (fmt[i] == 'E')
	  {
	    int j;
	    for (j = 0; j < XVECLEN (x, i); j++)
	      df_uses_record (df, &XVECEXP (x, i, j), ref_type,
1210
			      bb, insn, flags);
Jeff Law committed
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
	  }
      }
  }
}


/* Record all the df within INSN of basic block BB.  */
static void
df_insn_refs_record (df, bb, insn)
     struct df *df;
     basic_block bb;
     rtx insn;
{
  int i;

  if (INSN_P (insn))
    {
1228 1229
      rtx note;

Jeff Law committed
1230 1231
      /* Record register defs */
      df_defs_record (df, PATTERN (insn), bb, insn);
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241

      if (df->flags & DF_EQUIV_NOTES)
	for (note = REG_NOTES (insn); note;
	     note = XEXP (note, 1))
	  {
	    switch (REG_NOTE_KIND (note))
	      {
		case REG_EQUIV:
		case REG_EQUAL:
		  df_uses_record (df, &XEXP (note, 0), DF_REF_REG_USE,
1242
				  bb, insn, 0);
1243 1244 1245 1246
		default:
		  break;
	      }
	  }
1247

Jeff Law committed
1248 1249 1250 1251
      if (GET_CODE (insn) == CALL_INSN)
	{
	  rtx note;
	  rtx x;
1252

Jeff Law committed
1253 1254 1255 1256 1257
	  /* Record the registers used to pass arguments.  */
	  for (note = CALL_INSN_FUNCTION_USAGE (insn); note;
	       note = XEXP (note, 1))
	    {
	      if (GET_CODE (XEXP (note, 0)) == USE)
1258
		df_uses_record (df, &XEXP (XEXP (note, 0), 0), DF_REF_REG_USE,
1259
				bb, insn, 0);
Jeff Law committed
1260 1261 1262 1263
	    }

	  /* The stack ptr is used (honorarily) by a CALL insn.  */
	  x = df_reg_use_gen (STACK_POINTER_REGNUM);
1264
	  df_uses_record (df, &XEXP (x, 0), DF_REF_REG_USE, bb, insn, 0);
1265

Jeff Law committed
1266 1267 1268 1269 1270 1271 1272 1273 1274
	  if (df->flags & DF_HARD_REGS)
	    {
	      /* Calls may also reference any of the global registers,
		 so they are recorded as used.  */
	      for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
		if (global_regs[i])
		  {
		    x = df_reg_use_gen (i);
		    df_uses_record (df, &SET_DEST (x),
Kazu Hirata committed
1275
				    DF_REF_REG_USE, bb, insn, 0);
Jeff Law committed
1276 1277 1278
		  }
	    }
	}
1279

Jeff Law committed
1280
      /* Record the register uses.  */
1281
      df_uses_record (df, &PATTERN (insn),
1282
		      DF_REF_REG_USE, bb, insn, 0);
1283

Jeff Law committed
1284 1285 1286 1287 1288 1289 1290

      if (GET_CODE (insn) == CALL_INSN)
	{
	  rtx note;

	  if (df->flags & DF_HARD_REGS)
	    {
1291
	      /* Kill all registers invalidated by a call.  */
Jeff Law committed
1292
	      for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1293
		if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
Jeff Law committed
1294 1295 1296 1297 1298
		  {
		    rtx reg_clob = df_reg_clobber_gen (i);
		    df_defs_record (df, reg_clob, bb, insn);
		  }
	    }
1299

Jeff Law committed
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
	  /* There may be extra registers to be clobbered.  */
	  for (note = CALL_INSN_FUNCTION_USAGE (insn);
	       note;
	       note = XEXP (note, 1))
	    if (GET_CODE (XEXP (note, 0)) == CLOBBER)
	      df_defs_record (df, XEXP (note, 0), bb, insn);
	}
    }
}


/* Record all the refs within the basic block BB.  */
static void
df_bb_refs_record (df, bb)
     struct df *df;
     basic_block bb;
{
  rtx insn;

  /* Scan the block an insn at a time from beginning to end.  */
  for (insn = bb->head; ; insn = NEXT_INSN (insn))
    {
      if (INSN_P (insn))
	{
	  /* Record defs within INSN.  */
	  df_insn_refs_record (df, bb, insn);
	}
      if (insn == bb->end)
	break;
    }
}


/* Record all the refs in the basic blocks specified by BLOCKS.  */
static void
df_refs_record (df, blocks)
     struct df *df;
     bitmap blocks;
{
  basic_block bb;

  FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
    {
      df_bb_refs_record (df, bb);
    });
}

/* Dataflow analysis routines.  */


/* Create reg-def chains for basic block BB.  These are a list of
   definitions for each register.  */
static void
df_bb_reg_def_chain_create (df, bb)
     struct df *df;
     basic_block bb;
{
  rtx insn;
1358

Jeff Law committed
1359 1360 1361
  /* Perhaps the defs should be sorted using a depth first search
     of the CFG (or possibly a breadth first search).  We currently
     scan the basic blocks in reverse order so that the first defs
1362
     appear at the start of the chain.  */
1363

Jeff Law committed
1364 1365 1366 1367 1368 1369 1370 1371
  for (insn = bb->end; insn && insn != PREV_INSN (bb->head);
       insn = PREV_INSN (insn))
    {
      struct df_link *link;
      unsigned int uid = INSN_UID (insn);

      if (! INSN_P (insn))
	continue;
1372

Jeff Law committed
1373 1374 1375 1376
      for (link = df->insns[uid].defs; link; link = link->next)
	{
	  struct ref *def = link->ref;
	  unsigned int dregno = DF_REF_REGNO (def);
1377 1378 1379 1380 1381
          /* Don't add ref's to the chain two times.  I.e. only add
             new refs.  XXX the same could be done by testing if the current
             insn is a modified (or a new) one.  This would be faster.  */
          if (DF_REF_ID (def) < df->def_id_save)
            continue;
1382

Jeff Law committed
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
	  df->regs[dregno].defs
	    = df_link_create (def, df->regs[dregno].defs);
	}
    }
}


/* Create reg-def chains for each basic block within BLOCKS.  These
   are a list of definitions for each register.  */
static void
df_reg_def_chain_create (df, blocks)
     struct df *df;
     bitmap blocks;
{
  basic_block bb;

  FOR_EACH_BB_IN_BITMAP/*_REV*/ (blocks, 0, bb,
    {
      df_bb_reg_def_chain_create (df, bb);
    });
}


/* Create reg-use chains for basic block BB.  These are a list of uses
   for each register.  */
static void
df_bb_reg_use_chain_create (df, bb)
     struct df *df;
     basic_block bb;
{
  rtx insn;
1414

Jeff Law committed
1415 1416
  /* Scan in forward order so that the last uses appear at the
	 start of the chain.  */
1417

Jeff Law committed
1418 1419 1420 1421 1422 1423 1424 1425
  for (insn = bb->head; insn && insn != NEXT_INSN (bb->end);
       insn = NEXT_INSN (insn))
    {
      struct df_link *link;
      unsigned int uid = INSN_UID (insn);

      if (! INSN_P (insn))
	continue;
1426

Jeff Law committed
1427 1428 1429 1430
      for (link = df->insns[uid].uses; link; link = link->next)
	{
	  struct ref *use = link->ref;
	  unsigned int uregno = DF_REF_REGNO (use);
1431 1432 1433 1434 1435
          /* Don't add ref's to the chain two times.  I.e. only add
             new refs.  XXX the same could be done by testing if the current
             insn is a modified (or a new) one.  This would be faster.  */
          if (DF_REF_ID (use) < df->use_id_save)
            continue;
1436

Jeff Law committed
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468
	  df->regs[uregno].uses
	    = df_link_create (use, df->regs[uregno].uses);
	}
    }
}


/* Create reg-use chains for each basic block within BLOCKS.  These
   are a list of uses for each register.  */
static void
df_reg_use_chain_create (df, blocks)
     struct df *df;
     bitmap blocks;
{
  basic_block bb;

  FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
    {
      df_bb_reg_use_chain_create (df, bb);
    });
}


/* Create def-use chains from reaching use bitmaps for basic block BB.  */
static void
df_bb_du_chain_create (df, bb, ru)
     struct df *df;
     basic_block bb;
     bitmap ru;
{
  struct bb_info *bb_info = DF_BB_INFO (df, bb);
  rtx insn;
1469

Jeff Law committed
1470
  bitmap_copy (ru, bb_info->ru_out);
1471

Jeff Law committed
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
  /* For each def in BB create a linked list (chain) of uses
     reached from the def.  */
  for (insn = bb->end; insn && insn != PREV_INSN (bb->head);
       insn = PREV_INSN (insn))
    {
      struct df_link *def_link;
      struct df_link *use_link;
      unsigned int uid = INSN_UID (insn);

      if (! INSN_P (insn))
	continue;
1483

Jeff Law committed
1484 1485 1486 1487 1488
      /* For each def in insn...  */
      for (def_link = df->insns[uid].defs; def_link; def_link = def_link->next)
	{
	  struct ref *def = def_link->ref;
	  unsigned int dregno = DF_REF_REGNO (def);
1489

Jeff Law committed
1490 1491 1492 1493 1494
	  DF_REF_CHAIN (def) = 0;

	  /* While the reg-use chains are not essential, it
	     is _much_ faster to search these short lists rather
	     than all the reaching uses, especially for large functions.  */
1495
	  for (use_link = df->regs[dregno].uses; use_link;
Jeff Law committed
1496 1497 1498
	       use_link = use_link->next)
	    {
	      struct ref *use = use_link->ref;
1499

Jeff Law committed
1500 1501
	      if (bitmap_bit_p (ru, DF_REF_ID (use)))
		{
1502
		  DF_REF_CHAIN (def)
Jeff Law committed
1503
		    = df_link_create (use, DF_REF_CHAIN (def));
1504

Jeff Law committed
1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
		  bitmap_clear_bit (ru, DF_REF_ID (use));
		}
	    }
	}

      /* For each use in insn...  */
      for (use_link = df->insns[uid].uses; use_link; use_link = use_link->next)
	{
	  struct ref *use = use_link->ref;
	  bitmap_set_bit (ru, DF_REF_ID (use));
	}
    }
}


/* Create def-use chains from reaching use bitmaps for basic blocks
   in BLOCKS.  */
static void
df_du_chain_create (df, blocks)
     struct df *df;
     bitmap blocks;
{
  bitmap ru;
  basic_block bb;

  ru = BITMAP_XMALLOC ();

  FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
    {
      df_bb_du_chain_create (df, bb, ru);
    });

  BITMAP_XFREE (ru);
}


/* Create use-def chains from reaching def bitmaps for basic block BB.  */
static void
df_bb_ud_chain_create (df, bb)
     struct df *df;
     basic_block bb;
{
  struct bb_info *bb_info = DF_BB_INFO (df, bb);
  struct ref **reg_def_last = df->reg_def_last;
  rtx insn;
1550

Jeff Law committed
1551
  memset (reg_def_last, 0, df->n_regs * sizeof (struct ref *));
1552

Jeff Law committed
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
  /* For each use in BB create a linked list (chain) of defs
     that reach the use.  */
  for (insn = bb->head; insn && insn != NEXT_INSN (bb->end);
       insn = NEXT_INSN (insn))
    {
      unsigned int uid = INSN_UID (insn);
      struct df_link *use_link;
      struct df_link *def_link;

      if (! INSN_P (insn))
	continue;

1565
      /* For each use in insn...  */
Jeff Law committed
1566 1567 1568 1569
      for (use_link = df->insns[uid].uses; use_link; use_link = use_link->next)
	{
	  struct ref *use = use_link->ref;
	  unsigned int regno = DF_REF_REGNO (use);
1570

Jeff Law committed
1571 1572 1573 1574 1575 1576 1577 1578 1579
	  DF_REF_CHAIN (use) = 0;

	  /* Has regno been defined in this BB yet?  If so, use
	     the last def as the single entry for the use-def
	     chain for this use.  Otherwise, we need to add all
	     the defs using this regno that reach the start of
	     this BB.  */
	  if (reg_def_last[regno])
	    {
1580
	      DF_REF_CHAIN (use)
Jeff Law committed
1581 1582 1583 1584 1585 1586 1587 1588
		= df_link_create (reg_def_last[regno], 0);
	    }
	  else
	    {
	      /* While the reg-def chains are not essential, it is
		 _much_ faster to search these short lists rather than
		 all the reaching defs, especially for large
		 functions.  */
1589
	      for (def_link = df->regs[regno].defs; def_link;
Jeff Law committed
1590 1591 1592
		   def_link = def_link->next)
		{
		  struct ref *def = def_link->ref;
1593

Jeff Law committed
1594 1595
		  if (bitmap_bit_p (bb_info->rd_in, DF_REF_ID (def)))
		    {
1596
		      DF_REF_CHAIN (use)
Jeff Law committed
1597 1598 1599 1600 1601
			= df_link_create (def, DF_REF_CHAIN (use));
		    }
		}
	    }
	}
1602

Jeff Law committed
1603 1604 1605 1606 1607 1608

      /* For each def in insn...record the last def of each reg.  */
      for (def_link = df->insns[uid].defs; def_link; def_link = def_link->next)
	{
	  struct ref *def = def_link->ref;
	  int dregno = DF_REF_REGNO (def);
1609

Jeff Law committed
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
	  reg_def_last[dregno] = def;
	}
    }
}


/* Create use-def chains from reaching def bitmaps for basic blocks
   within BLOCKS.  */
static void
df_ud_chain_create (df, blocks)
     struct df *df;
     bitmap blocks;
{
  basic_block bb;

  FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
    {
      df_bb_ud_chain_create (df, bb);
    });
}


1632

Jeff Law committed
1633
static void
1634 1635 1636 1637 1638
df_rd_transfer_function (bb, changed, in, out, gen, kill, data)
     int bb ATTRIBUTE_UNUSED;
     int *changed;
     bitmap in, out, gen, kill;
     void *data ATTRIBUTE_UNUSED;
Jeff Law committed
1639
{
1640
  *changed = bitmap_union_of_diff (out, gen, in, kill);
Jeff Law committed
1641 1642
}
static void
1643 1644 1645 1646 1647
df_ru_transfer_function (bb, changed, in, out, gen, kill, data)
     int bb ATTRIBUTE_UNUSED;
     int *changed;
     bitmap in, out, gen, kill;
     void *data ATTRIBUTE_UNUSED;
Jeff Law committed
1648
{
1649
  *changed = bitmap_union_of_diff (in, gen, out, kill);
Jeff Law committed
1650 1651 1652
}

static void
1653 1654 1655 1656 1657
df_lr_transfer_function (bb, changed, in, out, use, def, data)
     int bb ATTRIBUTE_UNUSED;
     int *changed;
     bitmap in, out, use, def;
     void *data ATTRIBUTE_UNUSED;
Jeff Law committed
1658
{
1659
  *changed = bitmap_union_of_diff (in, use, out, def);
Jeff Law committed
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
}


/* Compute local reaching def info for basic block BB.  */
static void
df_bb_rd_local_compute (df, bb)
     struct df *df;
     basic_block bb;
{
  struct bb_info *bb_info = DF_BB_INFO (df, bb);
  rtx insn;
1671

Jeff Law committed
1672 1673 1674 1675 1676 1677 1678 1679
  for (insn = bb->head; insn && insn != NEXT_INSN (bb->end);
       insn = NEXT_INSN (insn))
    {
      unsigned int uid = INSN_UID (insn);
      struct df_link *def_link;

      if (! INSN_P (insn))
	continue;
1680

Jeff Law committed
1681 1682 1683 1684 1685 1686
      for (def_link = df->insns[uid].defs; def_link; def_link = def_link->next)
	{
	  struct ref *def = def_link->ref;
	  unsigned int regno = DF_REF_REGNO (def);
	  struct df_link *def2_link;

1687
	  for (def2_link = df->regs[regno].defs; def2_link;
Jeff Law committed
1688 1689 1690 1691 1692 1693 1694 1695 1696
	       def2_link = def2_link->next)
	    {
	      struct ref *def2 = def2_link->ref;

	      /* Add all defs of this reg to the set of kills.  This
		 is greedy since many of these defs will not actually
		 be killed by this BB but it keeps things a lot
		 simpler.  */
	      bitmap_set_bit (bb_info->rd_kill, DF_REF_ID (def2));
1697

Jeff Law committed
1698 1699 1700 1701 1702 1703 1704
	      /* Zap from the set of gens for this BB.  */
	      bitmap_clear_bit (bb_info->rd_gen, DF_REF_ID (def2));
	    }

	  bitmap_set_bit (bb_info->rd_gen, DF_REF_ID (def));
	}
    }
Kazu Hirata committed
1705

Jeff Law committed
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734
  bb_info->rd_valid = 1;
}


/* Compute local reaching def info for each basic block within BLOCKS.  */
static void
df_rd_local_compute (df, blocks)
     struct df *df;
     bitmap blocks;
{
  basic_block bb;

  FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
  {
    df_bb_rd_local_compute (df, bb);
  });
}


/* Compute local reaching use (upward exposed use) info for basic
   block BB.  */
static void
df_bb_ru_local_compute (df, bb)
     struct df *df;
     basic_block bb;
{
  /* This is much more tricky than computing reaching defs.  With
     reaching defs, defs get killed by other defs.  With upwards
     exposed uses, these get killed by defs with the same regno.  */
Kazu Hirata committed
1735

Jeff Law committed
1736 1737 1738
  struct bb_info *bb_info = DF_BB_INFO (df, bb);
  rtx insn;

1739

Jeff Law committed
1740 1741 1742 1743 1744 1745 1746 1747 1748
  for (insn = bb->end; insn && insn != PREV_INSN (bb->head);
       insn = PREV_INSN (insn))
    {
      unsigned int uid = INSN_UID (insn);
      struct df_link *def_link;
      struct df_link *use_link;

      if (! INSN_P (insn))
	continue;
1749

Jeff Law committed
1750 1751 1752 1753 1754
      for (def_link = df->insns[uid].defs; def_link; def_link = def_link->next)
	{
	  struct ref *def = def_link->ref;
	  unsigned int dregno = DF_REF_REGNO (def);

1755
	  for (use_link = df->regs[dregno].uses; use_link;
Jeff Law committed
1756 1757 1758 1759 1760 1761 1762 1763 1764
	       use_link = use_link->next)
	    {
	      struct ref *use = use_link->ref;

	      /* Add all uses of this reg to the set of kills.  This
		 is greedy since many of these uses will not actually
		 be killed by this BB but it keeps things a lot
		 simpler.  */
	      bitmap_set_bit (bb_info->ru_kill, DF_REF_ID (use));
1765

Jeff Law committed
1766 1767 1768 1769
	      /* Zap from the set of gens for this BB.  */
	      bitmap_clear_bit (bb_info->ru_gen, DF_REF_ID (use));
	    }
	}
1770

Jeff Law committed
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
      for (use_link = df->insns[uid].uses; use_link; use_link = use_link->next)
	{
	  struct ref *use = use_link->ref;
	  /* Add use to set of gens in this BB.  */
	  bitmap_set_bit (bb_info->ru_gen, DF_REF_ID (use));
	}
    }
  bb_info->ru_valid = 1;
}


/* Compute local reaching use (upward exposed use) info for each basic
   block within BLOCKS.  */
static void
df_ru_local_compute (df, blocks)
     struct df *df;
     bitmap blocks;
{
  basic_block bb;

  FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
  {
    df_bb_ru_local_compute (df, bb);
  });
}


/* Compute local live variable info for basic block BB.  */
static void
df_bb_lr_local_compute (df, bb)
     struct df *df;
     basic_block bb;
{
  struct bb_info *bb_info = DF_BB_INFO (df, bb);
  rtx insn;
1806

Jeff Law committed
1807 1808 1809 1810 1811 1812 1813 1814
  for (insn = bb->end; insn && insn != PREV_INSN (bb->head);
       insn = PREV_INSN (insn))
    {
      unsigned int uid = INSN_UID (insn);
      struct df_link *link;

      if (! INSN_P (insn))
	continue;
1815

Jeff Law committed
1816 1817 1818 1819
      for (link = df->insns[uid].defs; link; link = link->next)
	{
	  struct ref *def = link->ref;
	  unsigned int dregno = DF_REF_REGNO (def);
1820

Jeff Law committed
1821 1822
	  /* Add def to set of defs in this BB.  */
	  bitmap_set_bit (bb_info->lr_def, dregno);
1823

Jeff Law committed
1824 1825
	  bitmap_clear_bit (bb_info->lr_use, dregno);
	}
1826

Jeff Law committed
1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858
      for (link = df->insns[uid].uses; link; link = link->next)
	{
	  struct ref *use = link->ref;
	  /* Add use to set of uses in this BB.  */
	  bitmap_set_bit (bb_info->lr_use, DF_REF_REGNO (use));
	}
    }
  bb_info->lr_valid = 1;
}


/* Compute local live variable info for each basic block within BLOCKS.  */
static void
df_lr_local_compute (df, blocks)
     struct df *df;
     bitmap blocks;
{
  basic_block bb;

  FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
  {
    df_bb_lr_local_compute (df, bb);
  });
}


/* Compute register info: lifetime, bb, and number of defs and uses
   for basic block BB.  */
static void
df_bb_reg_info_compute (df, bb, live)
     struct df *df;
     basic_block bb;
1859
     bitmap live;
Jeff Law committed
1860 1861 1862 1863
{
  struct reg_info *reg_info = df->regs;
  struct bb_info *bb_info = DF_BB_INFO (df, bb);
  rtx insn;
1864

Jeff Law committed
1865
  bitmap_copy (live, bb_info->lr_out);
1866

Jeff Law committed
1867 1868 1869 1870 1871 1872
  for (insn = bb->end; insn && insn != PREV_INSN (bb->head);
       insn = PREV_INSN (insn))
    {
      unsigned int uid = INSN_UID (insn);
      unsigned int regno;
      struct df_link *link;
1873

Jeff Law committed
1874 1875
      if (! INSN_P (insn))
	continue;
1876

Jeff Law committed
1877 1878 1879 1880
      for (link = df->insns[uid].defs; link; link = link->next)
	{
	  struct ref *def = link->ref;
	  unsigned int dregno = DF_REF_REGNO (def);
1881

Jeff Law committed
1882 1883 1884 1885
	  /* Kill this register.  */
	  bitmap_clear_bit (live, dregno);
	  reg_info[dregno].n_defs++;
	}
1886

Jeff Law committed
1887 1888 1889 1890
      for (link = df->insns[uid].uses; link; link = link->next)
	{
	  struct ref *use = link->ref;
	  unsigned int uregno = DF_REF_REGNO (use);
1891

Jeff Law committed
1892 1893 1894 1895
	  /* This register is now live.  */
	  bitmap_set_bit (live, uregno);
	  reg_info[uregno].n_uses++;
	}
1896

Jeff Law committed
1897 1898
      /* Increment lifetimes of all live registers.  */
      EXECUTE_IF_SET_IN_BITMAP (live, 0, regno,
1899
      {
Jeff Law committed
1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976
	reg_info[regno].lifetime++;
      });
    }
}


/* Compute register info: lifetime, bb, and number of defs and uses.  */
static void
df_reg_info_compute (df, blocks)
     struct df *df;
     bitmap blocks;
{
  basic_block bb;
  bitmap live;

  live = BITMAP_XMALLOC ();

  FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
  {
    df_bb_reg_info_compute (df, bb, live);
  });

  BITMAP_XFREE (live);
}


/* Assign LUIDs for BB.  */
static int
df_bb_luids_set (df, bb)
     struct df *df;
     basic_block bb;
{
  rtx insn;
  int luid = 0;

  /* The LUIDs are monotonically increasing for each basic block.  */

  for (insn = bb->head; ; insn = NEXT_INSN (insn))
    {
      if (INSN_P (insn))
	DF_INSN_LUID (df, insn) = luid++;
      DF_INSN_LUID (df, insn) = luid;

      if (insn == bb->end)
	break;
    }
  return luid;
}


/* Assign LUIDs for each basic block within BLOCKS.  */
static int
df_luids_set (df, blocks)
     struct df *df;
     bitmap blocks;
{
  basic_block bb;
  int total = 0;

  FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
    {
      total += df_bb_luids_set (df, bb);
    });
  return total;
}

/* Perform dataflow analysis using existing DF structure for blocks
   within BLOCKS.  If BLOCKS is zero, use all basic blocks in the CFG.  */
static void
df_analyse_1 (df, blocks, flags, update)
     struct df *df;
     bitmap blocks;
     int flags;
     int update;
{
  int aflags;
  int dflags;
1977
  int i;
1978 1979
  basic_block bb;

Jeff Law committed
1980
  dflags = 0;
1981
  aflags = flags;
Jeff Law committed
1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042
  if (flags & DF_UD_CHAIN)
    aflags |= DF_RD | DF_RD_CHAIN;

  if (flags & DF_DU_CHAIN)
    aflags |= DF_RU;

  if (flags & DF_RU)
    aflags |= DF_RU_CHAIN;

  if (flags & DF_REG_INFO)
    aflags |= DF_LR;

  if (! blocks)
      blocks = df->all_blocks;

  df->flags = flags;
  if (update)
    {
      df_refs_update (df);
      /* More fine grained incremental dataflow analysis would be
	 nice.  For now recompute the whole shebang for the
	 modified blocks.  */
#if 0
      df_refs_unlink (df, blocks);
#endif
      /* All the def-use, use-def chains can be potentially
	 modified by changes in one block.  The size of the
	 bitmaps can also change.  */
    }
  else
    {
      /* Scan the function for all register defs and uses.  */
      df_refs_queue (df);
      df_refs_record (df, blocks);

      /* Link all the new defs and uses to the insns.  */
      df_refs_process (df);
    }

  /* Allocate the bitmaps now the total number of defs and uses are
     known.  If the number of defs or uses have changed, then
     these bitmaps need to be reallocated.  */
  df_bitmaps_alloc (df, aflags);

  /* Set the LUIDs for each specified basic block.  */
  df_luids_set (df, blocks);

  /* Recreate reg-def and reg-use chains from scratch so that first
     def is at the head of the reg-def chain and the last use is at
     the head of the reg-use chain.  This is only important for
     regs local to a basic block as it speeds up searching.  */
  if (aflags & DF_RD_CHAIN)
    {
      df_reg_def_chain_create (df, blocks);
    }

  if (aflags & DF_RU_CHAIN)
    {
      df_reg_use_chain_create (df, blocks);
    }

2043 2044 2045
  df->dfs_order = xmalloc (sizeof(int) * n_basic_blocks);
  df->rc_order = xmalloc (sizeof(int) * n_basic_blocks);
  df->rts_order = xmalloc (sizeof(int) * n_basic_blocks);
2046 2047 2048
  df->inverse_dfs_map = xmalloc (sizeof(int) * last_basic_block);
  df->inverse_rc_map = xmalloc (sizeof(int) * last_basic_block);
  df->inverse_rts_map = xmalloc (sizeof(int) * last_basic_block);
2049

Jeff Law committed
2050
  flow_depth_first_order_compute (df->dfs_order, df->rc_order);
2051
  flow_reverse_top_sort_order_compute (df->rts_order);
2052
  for (i = 0; i < n_basic_blocks; i ++)
2053 2054 2055 2056 2057
   {
     df->inverse_dfs_map[df->dfs_order[i]] = i;
     df->inverse_rc_map[df->rc_order[i]] = i;
     df->inverse_rts_map[df->rts_order[i]] = i;
   }
Jeff Law committed
2058 2059 2060 2061
  if (aflags & DF_RD)
    {
      /* Compute the sets of gens and kills for the defs of each bb.  */
      df_rd_local_compute (df, df->flags & DF_RD ? blocks : df->all_blocks);
2062
      {
2063 2064 2065 2066
	bitmap *in = xmalloc (sizeof (bitmap) * last_basic_block);
	bitmap *out = xmalloc (sizeof (bitmap) * last_basic_block);
	bitmap *gen = xmalloc (sizeof (bitmap) * last_basic_block);
	bitmap *kill = xmalloc (sizeof (bitmap) * last_basic_block);
2067
	FOR_EACH_BB (bb)
2068
	  {
2069 2070 2071 2072
	    in[bb->index] = DF_BB_INFO (df, bb)->rd_in;
	    out[bb->index] = DF_BB_INFO (df, bb)->rd_out;
	    gen[bb->index] = DF_BB_INFO (df, bb)->rd_gen;
	    kill[bb->index] = DF_BB_INFO (df, bb)->rd_kill;
2073
	  }
Kazu Hirata committed
2074
	iterative_dataflow_bitmap (in, out, gen, kill, df->all_blocks,
2075 2076 2077 2078 2079 2080 2081
				   FORWARD, UNION, df_rd_transfer_function,
				   df->inverse_rc_map, NULL);
	free (in);
	free (out);
	free (gen);
	free (kill);
      }
Jeff Law committed
2082 2083 2084 2085 2086 2087 2088 2089 2090 2091
    }

  if (aflags & DF_UD_CHAIN)
    {
      /* Create use-def chains.  */
      df_ud_chain_create (df, df->all_blocks);

      if (! (flags & DF_RD))
	dflags |= DF_RD;
    }
2092

Jeff Law committed
2093 2094 2095 2096 2097
  if (aflags & DF_RU)
    {
      /* Compute the sets of gens and kills for the upwards exposed
	 uses in each bb.  */
      df_ru_local_compute (df, df->flags & DF_RU ? blocks : df->all_blocks);
2098
      {
2099 2100 2101 2102
	bitmap *in = xmalloc (sizeof (bitmap) * last_basic_block);
	bitmap *out = xmalloc (sizeof (bitmap) * last_basic_block);
	bitmap *gen = xmalloc (sizeof (bitmap) * last_basic_block);
	bitmap *kill = xmalloc (sizeof (bitmap) * last_basic_block);
2103
	FOR_EACH_BB (bb)
2104
	  {
2105 2106 2107 2108
	    in[bb->index] = DF_BB_INFO (df, bb)->ru_in;
	    out[bb->index] = DF_BB_INFO (df, bb)->ru_out;
	    gen[bb->index] = DF_BB_INFO (df, bb)->ru_gen;
	    kill[bb->index] = DF_BB_INFO (df, bb)->ru_kill;
2109
	  }
Kazu Hirata committed
2110
	iterative_dataflow_bitmap (in, out, gen, kill, df->all_blocks,
2111 2112 2113 2114 2115 2116 2117
				   BACKWARD, UNION, df_ru_transfer_function,
				   df->inverse_rts_map, NULL);
	free (in);
	free (out);
	free (gen);
	free (kill);
      }
Jeff Law committed
2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135
    }

  if (aflags & DF_DU_CHAIN)
    {
      /* Create def-use chains.  */
      df_du_chain_create (df, df->all_blocks);

      if (! (flags & DF_RU))
	dflags |= DF_RU;
    }

  /* Free up bitmaps that are no longer required.  */
  if (dflags)
     df_bitmaps_free (df, dflags);

  if (aflags & DF_LR)
    {
      /* Compute the sets of defs and uses of live variables.  */
Kazu Hirata committed
2136
      df_lr_local_compute (df, df->flags & DF_LR ? blocks : df->all_blocks);
2137
      {
2138 2139 2140 2141
	bitmap *in = xmalloc (sizeof (bitmap) * last_basic_block);
	bitmap *out = xmalloc (sizeof (bitmap) * last_basic_block);
	bitmap *use = xmalloc (sizeof (bitmap) * last_basic_block);
	bitmap *def = xmalloc (sizeof (bitmap) * last_basic_block);
2142
	FOR_EACH_BB (bb)
2143
	  {
2144 2145 2146 2147
	    in[bb->index] = DF_BB_INFO (df, bb)->lr_in;
	    out[bb->index] = DF_BB_INFO (df, bb)->lr_out;
	    use[bb->index] = DF_BB_INFO (df, bb)->lr_use;
	    def[bb->index] = DF_BB_INFO (df, bb)->lr_def;
2148
	  }
Kazu Hirata committed
2149
	iterative_dataflow_bitmap (in, out, use, def, df->all_blocks,
2150 2151 2152 2153 2154 2155 2156
				   BACKWARD, UNION, df_lr_transfer_function,
				   df->inverse_rts_map, NULL);
	free (in);
	free (out);
	free (use);
	free (def);
      }
Jeff Law committed
2157 2158 2159 2160 2161
    }

  if (aflags & DF_REG_INFO)
    {
      df_reg_info_compute (df, df->all_blocks);
2162
    }
Jeff Law committed
2163 2164
  free (df->dfs_order);
  free (df->rc_order);
2165
  free (df->rts_order);
2166 2167 2168
  free (df->inverse_rc_map);
  free (df->inverse_dfs_map);
  free (df->inverse_rts_map);
Jeff Law committed
2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181
}


/* Initialise dataflow analysis.  */
struct df *
df_init ()
{
  struct df *df;

  df = xcalloc (1, sizeof (struct df));

  /* Squirrel away a global for debugging.  */
  ddf = df;
2182

Jeff Law committed
2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232
  return df;
}


/* Start queuing refs.  */
static int
df_refs_queue (df)
     struct df *df;
{
  df->def_id_save = df->def_id;
  df->use_id_save = df->use_id;
  /* ???? Perhaps we should save current obstack state so that we can
     unwind it.  */
  return 0;
}


/* Process queued refs.  */
static int
df_refs_process (df)
     struct df *df;
{
  unsigned int i;

  /* Build new insn-def chains.  */
  for (i = df->def_id_save; i != df->def_id; i++)
    {
      struct ref *def = df->defs[i];
      unsigned int uid = DF_REF_INSN_UID (def);

      /* Add def to head of def list for INSN.  */
      df->insns[uid].defs
	= df_link_create (def, df->insns[uid].defs);
    }

  /* Build new insn-use chains.  */
  for (i = df->use_id_save; i != df->use_id; i++)
    {
      struct ref *use = df->uses[i];
      unsigned int uid = DF_REF_INSN_UID (use);

      /* Add use to head of use list for INSN.  */
      df->insns[uid].uses
	= df_link_create (use, df->insns[uid].uses);
    }
  return 0;
}


/* Update refs for basic block BB.  */
2233
static int
Jeff Law committed
2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255
df_bb_refs_update (df, bb)
     struct df *df;
     basic_block bb;
{
  rtx insn;
  int count = 0;

  /* While we have to scan the chain of insns for this BB, we don't
     need to allocate and queue a long chain of BB/INSN pairs.  Using
     a bitmap for insns_modified saves memory and avoids queuing
     duplicates.  */

  for (insn = bb->head; ; insn = NEXT_INSN (insn))
    {
      unsigned int uid;

      uid = INSN_UID (insn);

      if (bitmap_bit_p (df->insns_modified, uid))
	{
	  /* Delete any allocated refs of this insn.  MPH,  FIXME.  */
	  df_insn_refs_unlink (df, bb, insn);
2256

Jeff Law committed
2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299
	  /* Scan the insn for refs.  */
	  df_insn_refs_record (df, bb, insn);

	  count++;
	}
      if (insn == bb->end)
	break;
    }
  return count;
}


/* Process all the modified/deleted insns that were queued.  */
static int
df_refs_update (df)
     struct df *df;
{
  basic_block bb;
  int count = 0;

  if ((unsigned int)max_reg_num () >= df->reg_size)
    df_reg_table_realloc (df, 0);

  df_refs_queue (df);

  FOR_EACH_BB_IN_BITMAP (df->bbs_modified, 0, bb,
    {
      count += df_bb_refs_update (df, bb);
    });

  df_refs_process (df);
  return count;
}


/* Return non-zero if any of the requested blocks in the bitmap
   BLOCKS have been modified.  */
static int
df_modified_p (df, blocks)
     struct df *df;
     bitmap blocks;
{
  int update = 0;
2300 2301 2302 2303
  basic_block bb;

  if (!df->n_bbs)
    return 0;
Jeff Law committed
2304

2305 2306 2307
  FOR_EACH_BB (bb)
    if (bitmap_bit_p (df->bbs_modified, bb->index)
	&& (! blocks || (blocks == (bitmap) -1) || bitmap_bit_p (blocks, bb->index)))
Jeff Law committed
2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329
    {
      update = 1;
      break;
    }

  return update;
}


/* Analyse dataflow info for the basic blocks specified by the bitmap
   BLOCKS, or for the whole CFG if BLOCKS is zero, or just for the
   modified blocks if BLOCKS is -1.  */
int
df_analyse (df, blocks, flags)
     struct df *df;
     bitmap blocks;
     int flags;
{
  int update;

  /* We could deal with additional basic blocks being created by
     rescanning everything again.  */
2330
  if (df->n_bbs && df->n_bbs != (unsigned int) last_basic_block)
Jeff Law committed
2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357
    abort ();

  update = df_modified_p (df, blocks);
  if (update || (flags != df->flags))
    {
      if (! blocks)
	{
	  if (df->n_bbs)
	    {
	      /* Recompute everything from scratch.  */
	      df_free (df);
	    }
	  /* Allocate and initialise data structures.  */
	  df_alloc (df, max_reg_num ());
	  df_analyse_1 (df, 0, flags, 0);
	  update = 1;
	}
      else
	{
	  if (blocks == (bitmap) -1)
	    blocks = df->bbs_modified;

	  if (! df->n_bbs)
	    abort ();

	  df_analyse_1 (df, blocks, flags, 1);
	  bitmap_zero (df->bbs_modified);
2358
	  bitmap_zero (df->insns_modified);
Jeff Law committed
2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383
	}
    }
  return update;
}


/* Free all the dataflow info and the DF structure.  */
void
df_finish (df)
     struct df *df;
{
  df_free (df);
  free (df);
}


/* Unlink INSN from its reference information.  */
static void
df_insn_refs_unlink (df, bb, insn)
     struct df *df;
     basic_block bb ATTRIBUTE_UNUSED;
     rtx insn;
{
  struct df_link *link;
  unsigned int uid;
2384

Jeff Law committed
2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399
  uid = INSN_UID (insn);

  /* Unlink all refs defined by this insn.  */
  for (link = df->insns[uid].defs; link; link = link->next)
    df_def_unlink (df, link->ref);

  /* Unlink all refs used by this insn.  */
  for (link = df->insns[uid].uses; link; link = link->next)
    df_use_unlink (df, link->ref);

  df->insns[uid].defs = 0;
  df->insns[uid].uses = 0;
}


2400
#if 0
Jeff Law committed
2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440
/* Unlink all the insns within BB from their reference information.  */
static void
df_bb_refs_unlink (df, bb)
     struct df *df;
     basic_block bb;
{
  rtx insn;

  /* Scan the block an insn at a time from beginning to end.  */
  for (insn = bb->head; ; insn = NEXT_INSN (insn))
    {
      if (INSN_P (insn))
	{
	  /* Unlink refs for INSN.  */
	  df_insn_refs_unlink (df, bb, insn);
	}
      if (insn == bb->end)
	break;
    }
}


/* Unlink all the refs in the basic blocks specified by BLOCKS.
   Not currently used.  */
static void
df_refs_unlink (df, blocks)
     struct df *df;
     bitmap blocks;
{
  basic_block bb;

  if (blocks)
    {
      FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
      {
	df_bb_refs_unlink (df, bb);
      });
    }
  else
    {
2441
      FOR_EACH_BB (bb)
Jeff Law committed
2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464
	df_bb_refs_unlink (df, bb);
    }
}
#endif

/* Functions to modify insns.  */


/* Delete INSN and all its reference information.  */
rtx
df_insn_delete (df, bb, insn)
     struct df *df;
     basic_block bb ATTRIBUTE_UNUSED;
     rtx insn;
{
  /* If the insn is a jump, we should perhaps call delete_insn to
     handle the JUMP_LABEL?  */

  /* We should not be deleting the NOTE_INSN_BASIC_BLOCK or label.  */
  if (insn == bb->head)
    abort ();

  /* Delete the insn.  */
2465
  delete_insn (insn);
Jeff Law committed
2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485

  df_insn_modify (df, bb, insn);

  return NEXT_INSN (insn);
}


/* Mark that INSN within BB may have changed  (created/modified/deleted).
   This may be called multiple times for the same insn.  There is no
   harm calling this function if the insn wasn't changed; it will just
   slow down the rescanning of refs.  */
void
df_insn_modify (df, bb, insn)
     struct df *df;
     basic_block bb;
     rtx insn;
{
  unsigned int uid;

  uid = INSN_UID (insn);
2486
  if (uid >= df->insn_size)
2487
    df_insn_table_realloc (df, uid);
2488

2489
  bitmap_set_bit (df->bbs_modified, bb->index);
Jeff Law committed
2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565
  bitmap_set_bit (df->insns_modified, uid);

  /* For incremental updating on the fly, perhaps we could make a copy
     of all the refs of the original insn and turn them into
     anti-refs.  When df_refs_update finds these anti-refs, it annihilates
     the original refs.  If validate_change fails then these anti-refs
     will just get ignored.  */
}


typedef struct replace_args
{
  rtx match;
  rtx replacement;
  rtx insn;
  int modified;
} replace_args;


/* Replace mem pointed to by PX with its associated pseudo register.
   DATA is actually a pointer to a structure describing the
   instruction currently being scanned and the MEM we are currently
   replacing.  */
static int
df_rtx_mem_replace (px, data)
     rtx *px;
     void *data;
{
  replace_args *args = (replace_args *) data;
  rtx mem = *px;

  if (mem == NULL_RTX)
    return 0;

  switch (GET_CODE (mem))
    {
    case MEM:
      break;

    case CONST_DOUBLE:
      /* We're not interested in the MEM associated with a
	 CONST_DOUBLE, so there's no need to traverse into one.  */
      return -1;

    default:
      /* This is not a MEM.  */
      return 0;
    }

  if (!rtx_equal_p (args->match, mem))
    /* This is not the MEM we are currently replacing.  */
    return 0;

  /* Actually replace the MEM.  */
  validate_change (args->insn, px, args->replacement, 1);
  args->modified++;

  return 0;
}


int
df_insn_mem_replace (df, bb, insn, mem, reg)
     struct df *df;
     basic_block bb;
     rtx insn;
     rtx mem;
     rtx reg;
{
  replace_args args;

  args.insn = insn;
  args.match = mem;
  args.replacement = reg;
  args.modified = 0;

2566
  /* Search and replace all matching mems within insn.  */
Jeff Law committed
2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747
  for_each_rtx (&insn, df_rtx_mem_replace, &args);

  if (args.modified)
    df_insn_modify (df, bb, insn);

  /* ???? FIXME.  We may have a new def or one or more new uses of REG
     in INSN.  REG should be a new pseudo so it won't affect the
     dataflow information that we currently have.  We should add
     the new uses and defs to INSN and then recreate the chains
     when df_analyse is called.  */
  return args.modified;
}


/* Replace one register with another.  Called through for_each_rtx; PX
   points to the rtx being scanned.  DATA is actually a pointer to a
   structure of arguments.  */
static int
df_rtx_reg_replace (px, data)
     rtx *px;
     void *data;
{
  rtx x = *px;
  replace_args *args = (replace_args *) data;

  if (x == NULL_RTX)
    return 0;

  if (x == args->match)
    {
      validate_change (args->insn, px, args->replacement, 1);
      args->modified++;
    }

  return 0;
}


/* Replace the reg within every ref on CHAIN that is within the set
   BLOCKS of basic blocks with NEWREG.  Also update the regs within
   REG_NOTES.  */
void
df_refs_reg_replace (df, blocks, chain, oldreg, newreg)
     struct df *df;
     bitmap blocks;
     struct df_link *chain;
     rtx oldreg;
     rtx newreg;
{
  struct df_link *link;
  replace_args args;

  if (! blocks)
    blocks = df->all_blocks;

  args.match = oldreg;
  args.replacement = newreg;
  args.modified = 0;

  for (link = chain; link; link = link->next)
    {
      struct ref *ref = link->ref;
      rtx insn = DF_REF_INSN (ref);

      if (! INSN_P (insn))
	continue;

      if (bitmap_bit_p (blocks, DF_REF_BBNO (ref)))
	{
	  df_ref_reg_replace (df, ref, oldreg, newreg);

	  /* Replace occurrences of the reg within the REG_NOTES.  */
	  if ((! link->next || DF_REF_INSN (ref)
	      != DF_REF_INSN (link->next->ref))
	      && REG_NOTES (insn))
	    {
	      args.insn = insn;
	      for_each_rtx (&REG_NOTES (insn), df_rtx_reg_replace, &args);
	    }
	}
      else
	{
	  /* Temporary check to ensure that we have a grip on which
	     regs should be replaced.  */
	  abort ();
	}
    }
}


/* Replace all occurrences of register OLDREG with register NEWREG in
   blocks defined by bitmap BLOCKS.  This also replaces occurrences of
   OLDREG in the REG_NOTES but only for insns containing OLDREG.  This
   routine expects the reg-use and reg-def chains to be valid.  */
int
df_reg_replace (df, blocks, oldreg, newreg)
     struct df *df;
     bitmap blocks;
     rtx oldreg;
     rtx newreg;
{
  unsigned int oldregno = REGNO (oldreg);

  df_refs_reg_replace (df, blocks, df->regs[oldregno].defs, oldreg, newreg);
  df_refs_reg_replace (df, blocks, df->regs[oldregno].uses, oldreg, newreg);
  return 1;
}


/* Try replacing the reg within REF with NEWREG.  Do not modify
   def-use/use-def chains.  */
int
df_ref_reg_replace (df, ref, oldreg, newreg)
     struct df *df;
     struct ref *ref;
     rtx oldreg;
     rtx newreg;
{
  /* Check that insn was deleted by being converted into a NOTE.  If
   so ignore this insn.  */
  if (! INSN_P (DF_REF_INSN (ref)))
    return 0;

  if (oldreg && oldreg != DF_REF_REG (ref))
    abort ();

  if (! validate_change (DF_REF_INSN (ref), DF_REF_LOC (ref), newreg, 1))
    return 0;

  df_insn_modify (df, DF_REF_BB (ref), DF_REF_INSN (ref));
  return 1;
}


struct ref*
df_bb_def_use_swap (df, bb, def_insn, use_insn, regno)
     struct df * df;
     basic_block bb;
     rtx def_insn;
     rtx use_insn;
     unsigned int regno;
{
  struct ref *def;
  struct ref *use;
  int def_uid;
  int use_uid;
  struct df_link *link;

  def = df_bb_insn_regno_first_def_find (df, bb, def_insn, regno);
  if (! def)
    return 0;

  use = df_bb_insn_regno_last_use_find (df, bb, use_insn, regno);
  if (! use)
    return 0;

  /* The USE no longer exists.  */
  use_uid = INSN_UID (use_insn);
  df_use_unlink (df, use);
  df_ref_unlink (&df->insns[use_uid].uses, use);

  /* The DEF requires shifting so remove it from DEF_INSN
     and add it to USE_INSN by reusing LINK.  */
  def_uid = INSN_UID (def_insn);
  link = df_ref_unlink (&df->insns[def_uid].defs, def);
  link->ref = def;
  link->next = df->insns[use_uid].defs;
  df->insns[use_uid].defs = link;

#if 0
  link = df_ref_unlink (&df->regs[regno].defs, def);
  link->ref = def;
  link->next = df->regs[regno].defs;
  df->insns[regno].defs = link;
#endif

  DF_REF_INSN (def) = use_insn;
  return def;
}


2748
/* Record df between FIRST_INSN and LAST_INSN inclusive.  All new
Jeff Law committed
2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766
   insns must be processed by this routine.  */
static void
df_insns_modify (df, bb, first_insn, last_insn)
     struct df *df;
     basic_block bb;
     rtx first_insn;
     rtx last_insn;
{
  rtx insn;

  for (insn = first_insn; ; insn = NEXT_INSN (insn))
    {
      unsigned int uid;

      /* A non-const call should not have slipped through the net.  If
	 it does, we need to create a new basic block.  Ouch.  The
	 same applies for a label.  */
      if ((GET_CODE (insn) == CALL_INSN
2767
	   && ! CONST_OR_PURE_CALL_P (insn))
Jeff Law committed
2768 2769 2770 2771 2772 2773
	  || GET_CODE (insn) == CODE_LABEL)
	abort ();

      uid = INSN_UID (insn);

      if (uid >= df->insn_size)
2774
	df_insn_table_realloc (df, uid);
Jeff Law committed
2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800

      df_insn_modify (df, bb, insn);

      if (insn == last_insn)
	break;
    }
}


/* Emit PATTERN before INSN within BB.  */
rtx
df_pattern_emit_before (df, pattern, bb, insn)
     struct df *df ATTRIBUTE_UNUSED;
     rtx pattern;
     basic_block bb;
     rtx insn;
{
  rtx ret_insn;
  rtx prev_insn = PREV_INSN (insn);

  /* We should not be inserting before the start of the block.  */
  if (insn == bb->head)
    abort ();
  ret_insn = emit_insn_before (pattern, insn);
  if (ret_insn == insn)
    return ret_insn;
2801

Jeff Law committed
2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866
  df_insns_modify (df, bb, NEXT_INSN (prev_insn), ret_insn);
  return ret_insn;
}


/* Emit PATTERN after INSN within BB.  */
rtx
df_pattern_emit_after (df, pattern, bb, insn)
     struct df *df;
     rtx pattern;
     basic_block bb;
     rtx insn;
{
  rtx ret_insn;

  ret_insn = emit_insn_after (pattern, insn);
  if (ret_insn == insn)
    return ret_insn;

  df_insns_modify (df, bb, NEXT_INSN (insn), ret_insn);
  return ret_insn;
}


/* Emit jump PATTERN after INSN within BB.  */
rtx
df_jump_pattern_emit_after (df, pattern, bb, insn)
     struct df *df;
     rtx pattern;
     basic_block bb;
     rtx insn;
{
  rtx ret_insn;

  ret_insn = emit_jump_insn_after (pattern, insn);
  if (ret_insn == insn)
    return ret_insn;

  df_insns_modify (df, bb, NEXT_INSN (insn), ret_insn);
  return ret_insn;
}


/* Move INSN within BB before BEFORE_INSN within BEFORE_BB.

   This function should only be used to move loop invariant insns
   out of a loop where it has been proven that the def-use info
   will still be valid.  */
rtx
df_insn_move_before (df, bb, insn, before_bb, before_insn)
     struct df *df;
     basic_block bb;
     rtx insn;
     basic_block before_bb;
     rtx before_insn;
{
  struct df_link *link;
  unsigned int uid;

  if (! bb)
    return df_pattern_emit_before (df, insn, before_bb, before_insn);

  uid = INSN_UID (insn);

  /* Change bb for all df defined and used by this insn.  */
2867
  for (link = df->insns[uid].defs; link; link = link->next)
Jeff Law committed
2868
    DF_REF_BB (link->ref) = before_bb;
2869
  for (link = df->insns[uid].uses; link; link = link->next)
Jeff Law committed
2870 2871 2872 2873 2874 2875 2876 2877 2878
    DF_REF_BB (link->ref) = before_bb;

  /* The lifetimes of the registers used in this insn will be reduced
     while the lifetimes of the registers defined in this insn
     are likely to be increased.  */

  /* ???? Perhaps all the insns moved should be stored on a list
     which df_analyse removes when it recalculates data flow.  */

2879
  return emit_insn_before (insn, before_insn);
Jeff Law committed
2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896
}

/* Functions to query dataflow information.  */


int
df_insn_regno_def_p (df, bb, insn, regno)
     struct df *df;
     basic_block bb ATTRIBUTE_UNUSED;
     rtx insn;
     unsigned int regno;
{
  unsigned int uid;
  struct df_link *link;

  uid = INSN_UID (insn);

2897
  for (link = df->insns[uid].defs; link; link = link->next)
Jeff Law committed
2898 2899
    {
      struct ref *def = link->ref;
2900

Jeff Law committed
2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920
      if (DF_REF_REGNO (def) == regno)
	return 1;
    }

  return 0;
}


static int
df_def_dominates_all_uses_p (df, def)
     struct df *df ATTRIBUTE_UNUSED;
     struct ref *def;
{
  struct df_link *du_link;

  /* Follow def-use chain to find all the uses of this def.  */
  for (du_link = DF_REF_CHAIN (def); du_link; du_link = du_link->next)
    {
      struct ref *use = du_link->ref;
      struct df_link *ud_link;
2921

Jeff Law committed
2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941
      /* Follow use-def chain to check all the defs for this use.  */
      for (ud_link = DF_REF_CHAIN (use); ud_link; ud_link = ud_link->next)
	if (ud_link->ref != def)
	  return 0;
    }
  return 1;
}


int
df_insn_dominates_all_uses_p (df, bb, insn)
     struct df *df;
     basic_block bb ATTRIBUTE_UNUSED;
     rtx insn;
{
  unsigned int uid;
  struct df_link *link;

  uid = INSN_UID (insn);

2942
  for (link = df->insns[uid].defs; link; link = link->next)
Jeff Law committed
2943 2944
    {
      struct ref *def = link->ref;
2945

Jeff Law committed
2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998
      if (! df_def_dominates_all_uses_p (df, def))
	return 0;
    }

  return 1;
}


/* Return non-zero if all DF dominates all the uses within the bitmap
   BLOCKS.  */
static int
df_def_dominates_uses_p (df, def, blocks)
     struct df *df ATTRIBUTE_UNUSED;
     struct ref *def;
     bitmap blocks;
{
  struct df_link *du_link;

  /* Follow def-use chain to find all the uses of this def.  */
  for (du_link = DF_REF_CHAIN (def); du_link; du_link = du_link->next)
    {
      struct ref *use = du_link->ref;
      struct df_link *ud_link;

      /* Only worry about the uses within BLOCKS.  For example,
      consider a register defined within a loop that is live at the
      loop exits.  */
      if (bitmap_bit_p (blocks, DF_REF_BBNO (use)))
	{
	  /* Follow use-def chain to check all the defs for this use.  */
	  for (ud_link = DF_REF_CHAIN (use); ud_link; ud_link = ud_link->next)
	    if (ud_link->ref != def)
	      return 0;
	}
    }
  return 1;
}


/* Return non-zero if all the defs of INSN within BB dominates
   all the corresponding uses.  */
int
df_insn_dominates_uses_p (df, bb, insn, blocks)
     struct df *df;
     basic_block bb ATTRIBUTE_UNUSED;
     rtx insn;
     bitmap blocks;
{
  unsigned int uid;
  struct df_link *link;

  uid = INSN_UID (insn);

2999
  for (link = df->insns[uid].defs; link; link = link->next)
Jeff Law committed
3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064
    {
      struct ref *def = link->ref;

      /* Only consider the defs within BLOCKS.  */
      if (bitmap_bit_p (blocks, DF_REF_BBNO (def))
	  && ! df_def_dominates_uses_p (df, def, blocks))
	return 0;
    }
  return 1;
}


/* Return the basic block that REG referenced in or NULL if referenced
   in multiple basic blocks.  */
basic_block
df_regno_bb (df, regno)
     struct df *df;
     unsigned int regno;
{
  struct df_link *defs = df->regs[regno].defs;
  struct df_link *uses = df->regs[regno].uses;
  struct ref *def = defs ? defs->ref : 0;
  struct ref *use = uses ? uses->ref : 0;
  basic_block bb_def = def ? DF_REF_BB (def) : 0;
  basic_block bb_use = use ? DF_REF_BB (use) : 0;

  /* Compare blocks of first def and last use.  ???? FIXME.  What if
     the reg-def and reg-use lists are not correctly ordered.  */
  return bb_def == bb_use ? bb_def : 0;
}


/* Return non-zero if REG used in multiple basic blocks.  */
int
df_reg_global_p (df, reg)
     struct df *df;
     rtx reg;
{
  return df_regno_bb (df, REGNO (reg)) != 0;
}


/* Return total lifetime (in insns) of REG.  */
int
df_reg_lifetime (df, reg)
     struct df *df;
     rtx reg;
{
  return df->regs[REGNO (reg)].lifetime;
}


/* Return non-zero if REG live at start of BB.  */
int
df_bb_reg_live_start_p (df, bb, reg)
     struct df *df ATTRIBUTE_UNUSED;
     basic_block bb;
     rtx reg;
{
  struct bb_info *bb_info = DF_BB_INFO (df, bb);

#ifdef ENABLE_CHECKING
  if (! bb_info->lr_in)
    abort ();
#endif
3065

Jeff Law committed
3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077
  return bitmap_bit_p (bb_info->lr_in, REGNO (reg));
}


/* Return non-zero if REG live at end of BB.  */
int
df_bb_reg_live_end_p (df, bb, reg)
     struct df *df ATTRIBUTE_UNUSED;
     basic_block bb;
     rtx reg;
{
  struct bb_info *bb_info = DF_BB_INFO (df, bb);
3078

Jeff Law committed
3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103
#ifdef ENABLE_CHECKING
  if (! bb_info->lr_in)
    abort ();
#endif

  return bitmap_bit_p (bb_info->lr_out, REGNO (reg));
}


/* Return -1 if life of REG1 before life of REG2, 1 if life of REG1
   after life of REG2, or 0, if the lives overlap.  */
int
df_bb_regs_lives_compare (df, bb, reg1, reg2)
     struct df *df;
     basic_block bb;
     rtx reg1;
     rtx reg2;
{
  unsigned int regno1 = REGNO (reg1);
  unsigned int regno2 = REGNO (reg2);
  struct ref *def1;
  struct ref *use1;
  struct ref *def2;
  struct ref *use2;

3104

Jeff Law committed
3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140
  /* The regs must be local to BB.  */
  if (df_regno_bb (df, regno1) != bb
      || df_regno_bb (df, regno2) != bb)
    abort ();

  def2 = df_bb_regno_first_def_find (df, bb, regno2);
  use1 = df_bb_regno_last_use_find (df, bb, regno1);

  if (DF_INSN_LUID (df, DF_REF_INSN (def2))
      > DF_INSN_LUID (df, DF_REF_INSN (use1)))
    return -1;

  def1 = df_bb_regno_first_def_find (df, bb, regno1);
  use2 = df_bb_regno_last_use_find (df, bb, regno2);

  if (DF_INSN_LUID (df, DF_REF_INSN (def1))
      > DF_INSN_LUID (df, DF_REF_INSN (use2)))
    return 1;

  return 0;
}


/* Return last use of REGNO within BB.  */
static struct ref *
df_bb_regno_last_use_find (df, bb, regno)
     struct df * df;
     basic_block bb ATTRIBUTE_UNUSED;
     unsigned int regno;
{
  struct df_link *link;

  /* This assumes that the reg-use list is ordered such that for any
     BB, the last use is found first.  However, since the BBs are not
     ordered, the first use in the chain is not necessarily the last
     use in the function.  */
3141
  for (link = df->regs[regno].uses; link; link = link->next)
Jeff Law committed
3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164
    {
      struct ref *use = link->ref;

      if (DF_REF_BB (use) == bb)
	return use;
    }
  return 0;
}


/* Return first def of REGNO within BB.  */
static struct ref *
df_bb_regno_first_def_find (df, bb, regno)
     struct df * df;
     basic_block bb ATTRIBUTE_UNUSED;
     unsigned int regno;
{
  struct df_link *link;

  /* This assumes that the reg-def list is ordered such that for any
     BB, the first def is found first.  However, since the BBs are not
     ordered, the first def in the chain is not necessarily the first
     def in the function.  */
3165
  for (link = df->regs[regno].defs; link; link = link->next)
Jeff Law committed
3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188
    {
      struct ref *def = link->ref;

      if (DF_REF_BB (def) == bb)
	return def;
    }
  return 0;
}


/* Return first use of REGNO inside INSN within BB.  */
static struct ref *
df_bb_insn_regno_last_use_find (df, bb, insn, regno)
     struct df * df;
     basic_block bb ATTRIBUTE_UNUSED;
     rtx insn;
     unsigned int regno;
{
  unsigned int uid;
  struct df_link *link;

  uid = INSN_UID (insn);

3189
  for (link = df->insns[uid].uses; link; link = link->next)
Jeff Law committed
3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213
    {
      struct ref *use = link->ref;

      if (DF_REF_REGNO (use) == regno)
	return use;
    }

  return 0;
}


/* Return first def of REGNO inside INSN within BB.  */
static struct ref *
df_bb_insn_regno_first_def_find (df, bb, insn, regno)
     struct df * df;
     basic_block bb ATTRIBUTE_UNUSED;
     rtx insn;
     unsigned int regno;
{
  unsigned int uid;
  struct df_link *link;

  uid = INSN_UID (insn);

3214
  for (link = df->insns[uid].defs; link; link = link->next)
Jeff Law committed
3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257
    {
      struct ref *def = link->ref;

      if (DF_REF_REGNO (def) == regno)
	return def;
    }

  return 0;
}


/* Return insn using REG if the BB contains only a single
   use and def of REG.  */
rtx
df_bb_single_def_use_insn_find (df, bb, insn, reg)
     struct df * df;
     basic_block bb;
     rtx insn;
     rtx reg;
{
  struct ref *def;
  struct ref *use;
  struct df_link *du_link;

  def = df_bb_insn_regno_first_def_find (df, bb, insn, REGNO (reg));

  if (! def)
    abort ();

  du_link = DF_REF_CHAIN (def);

  if (! du_link)
    return NULL_RTX;

  use = du_link->ref;

  /* Check if def is dead.  */
  if (! use)
    return NULL_RTX;

  /* Check for multiple uses.  */
  if (du_link->next)
    return NULL_RTX;
3258

Jeff Law committed
3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289
  return DF_REF_INSN (use);
}

/* Functions for debugging/dumping dataflow information.  */


/* Dump a def-use or use-def chain for REF to FILE.  */
static void
df_chain_dump (link, file)
     struct df_link *link;
     FILE *file;
{
  fprintf (file, "{ ");
  for (; link; link = link->next)
    {
      fprintf (file, "%c%d ",
	       DF_REF_REG_DEF_P (link->ref) ? 'd' : 'u',
	       DF_REF_ID (link->ref));
    }
  fprintf (file, "}");
}

static void
df_chain_dump_regno (link, file)
     struct df_link *link;
     FILE *file;
{
  fprintf (file, "{ ");
  for (; link; link = link->next)
    {
      fprintf (file, "%c%d(%d) ",
Kazu Hirata committed
3290 3291 3292
	       DF_REF_REG_DEF_P (link->ref) ? 'd' : 'u',
	       DF_REF_ID (link->ref),
	       DF_REF_REGNO (link->ref));
Jeff Law committed
3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304
    }
  fprintf (file, "}");
}

/* Dump dataflow info.  */
void
df_dump (df, flags, file)
     struct df *df;
     int flags;
     FILE *file;
{
  unsigned int j;
3305
  basic_block bb;
Jeff Law committed
3306 3307 3308 3309 3310 3311 3312 3313 3314 3315

  if (! df || ! file)
    return;

  fprintf (file, "\nDataflow summary:\n");
  fprintf (file, "n_regs = %d, n_defs = %d, n_uses = %d, n_bbs = %d\n",
	   df->n_regs, df->n_defs, df->n_uses, df->n_bbs);

  if (flags & DF_RD)
    {
3316 3317
      basic_block bb;

Jeff Law committed
3318
      fprintf (file, "Reaching defs:\n");
3319
      FOR_EACH_BB (bb)
Jeff Law committed
3320
	{
3321 3322
	  struct bb_info *bb_info = DF_BB_INFO (df, bb);

Jeff Law committed
3323 3324 3325
	  if (! bb_info->rd_in)
	    continue;

3326
	  fprintf (file, "bb %d in  \t", bb->index);
Jeff Law committed
3327
	  dump_bitmap (file, bb_info->rd_in);
3328
	  fprintf (file, "bb %d gen \t", bb->index);
Jeff Law committed
3329
	  dump_bitmap (file, bb_info->rd_gen);
3330
	  fprintf (file, "bb %d kill\t", bb->index);
Jeff Law committed
3331
	  dump_bitmap (file, bb_info->rd_kill);
3332
	  fprintf (file, "bb %d out \t", bb->index);
Jeff Law committed
3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348
	  dump_bitmap (file, bb_info->rd_out);
	}
    }

  if (flags & DF_UD_CHAIN)
    {
      fprintf (file, "Use-def chains:\n");
      for (j = 0; j < df->n_defs; j++)
	{
	  if (df->defs[j])
	    {
	      fprintf (file, "d%d bb %d luid %d insn %d reg %d ",
		       j, DF_REF_BBNO (df->defs[j]),
		       DF_INSN_LUID (df, DF_REF_INSN (df->defs[j])),
		       DF_REF_INSN_UID (df->defs[j]),
		       DF_REF_REGNO (df->defs[j]));
3349 3350
	      if (df->defs[j]->flags & DF_REF_READ_WRITE)
		fprintf (file, "read/write ");
Jeff Law committed
3351 3352 3353 3354 3355 3356 3357 3358 3359
	      df_chain_dump (DF_REF_CHAIN (df->defs[j]), file);
	      fprintf (file, "\n");
	    }
	}
    }

  if (flags & DF_RU)
    {
      fprintf (file, "Reaching uses:\n");
3360
      FOR_EACH_BB (bb)
Jeff Law committed
3361
	{
3362 3363
	  struct bb_info *bb_info = DF_BB_INFO (df, bb);

Jeff Law committed
3364 3365 3366
	  if (! bb_info->ru_in)
	    continue;

3367
	  fprintf (file, "bb %d in  \t", bb->index);
Jeff Law committed
3368
	  dump_bitmap (file, bb_info->ru_in);
3369
	  fprintf (file, "bb %d gen \t", bb->index);
Jeff Law committed
3370
	  dump_bitmap (file, bb_info->ru_gen);
3371
	  fprintf (file, "bb %d kill\t", bb->index);
Jeff Law committed
3372
	  dump_bitmap (file, bb_info->ru_kill);
3373
	  fprintf (file, "bb %d out \t", bb->index);
Jeff Law committed
3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389
	  dump_bitmap (file, bb_info->ru_out);
	}
    }

  if (flags & DF_DU_CHAIN)
    {
      fprintf (file, "Def-use chains:\n");
      for (j = 0; j < df->n_uses; j++)
	{
	  if (df->uses[j])
	    {
	      fprintf (file, "u%d bb %d luid %d insn %d reg %d ",
		       j, DF_REF_BBNO (df->uses[j]),
		       DF_INSN_LUID (df, DF_REF_INSN (df->uses[j])),
		       DF_REF_INSN_UID (df->uses[j]),
		       DF_REF_REGNO (df->uses[j]));
3390 3391
	      if (df->uses[j]->flags & DF_REF_READ_WRITE)
		fprintf (file, "read/write ");
Jeff Law committed
3392 3393 3394 3395 3396 3397 3398 3399 3400
	      df_chain_dump (DF_REF_CHAIN (df->uses[j]), file);
	      fprintf (file, "\n");
	    }
	}
    }

  if (flags & DF_LR)
    {
      fprintf (file, "Live regs:\n");
3401
      FOR_EACH_BB (bb)
Jeff Law committed
3402
	{
3403 3404
	  struct bb_info *bb_info = DF_BB_INFO (df, bb);

Jeff Law committed
3405 3406 3407
	  if (! bb_info->lr_in)
	    continue;

3408
	  fprintf (file, "bb %d in  \t", bb->index);
Jeff Law committed
3409
	  dump_bitmap (file, bb_info->lr_in);
3410
	  fprintf (file, "bb %d use \t", bb->index);
Jeff Law committed
3411
	  dump_bitmap (file, bb_info->lr_use);
3412
	  fprintf (file, "bb %d def \t", bb->index);
Jeff Law committed
3413
	  dump_bitmap (file, bb_info->lr_def);
3414
	  fprintf (file, "bb %d out \t", bb->index);
Jeff Law committed
3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425
	  dump_bitmap (file, bb_info->lr_out);
	}
    }

  if (flags & (DF_REG_INFO | DF_RD_CHAIN | DF_RU_CHAIN))
    {
      struct reg_info *reg_info = df->regs;

      fprintf (file, "Register info:\n");
      for (j = 0; j < df->n_regs; j++)
	{
3426
	  if (((flags & DF_REG_INFO)
Jeff Law committed
3427 3428 3429 3430 3431 3432 3433 3434
	       && (reg_info[j].n_uses || reg_info[j].n_defs))
	      || ((flags & DF_RD_CHAIN) && reg_info[j].defs)
	      || ((flags & DF_RU_CHAIN) && reg_info[j].uses))
	  {
	    fprintf (file, "reg %d", j);
	    if ((flags & DF_RD_CHAIN) && (flags & DF_RU_CHAIN))
	      {
		basic_block bb = df_regno_bb (df, j);
3435

Jeff Law committed
3436
		if (bb)
3437
		  fprintf (file, " bb %d", bb->index);
Jeff Law committed
3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520
		else
		  fprintf (file, " bb ?");
	      }
	    if (flags & DF_REG_INFO)
	      {
		fprintf (file, " life %d", reg_info[j].lifetime);
	      }

	    if ((flags & DF_REG_INFO) || (flags & DF_RD_CHAIN))
	      {
		fprintf (file, " defs ");
		if (flags & DF_REG_INFO)
		  fprintf (file, "%d ", reg_info[j].n_defs);
		if (flags & DF_RD_CHAIN)
		  df_chain_dump (reg_info[j].defs, file);
	      }

	    if ((flags & DF_REG_INFO) || (flags & DF_RU_CHAIN))
	      {
		fprintf (file, " uses ");
		if (flags & DF_REG_INFO)
		  fprintf (file, "%d ", reg_info[j].n_uses);
		if (flags & DF_RU_CHAIN)
		  df_chain_dump (reg_info[j].uses, file);
	      }

	    fprintf (file, "\n");
	  }
	}
    }
  fprintf (file, "\n");
}


void
df_insn_debug (df, insn, file)
     struct df *df;
     rtx insn;
     FILE *file;
{
  unsigned int uid;
  int bbi;

  uid = INSN_UID (insn);
  if (uid >= df->insn_size)
    return;

  if (df->insns[uid].defs)
    bbi = DF_REF_BBNO (df->insns[uid].defs->ref);
  else  if (df->insns[uid].uses)
    bbi = DF_REF_BBNO (df->insns[uid].uses->ref);
  else
    bbi = -1;

  fprintf (file, "insn %d bb %d luid %d defs ",
	   uid, bbi, DF_INSN_LUID (df, insn));
  df_chain_dump (df->insns[uid].defs, file);
  fprintf (file, " uses ");
  df_chain_dump (df->insns[uid].uses, file);
  fprintf (file, "\n");
}

void
df_insn_debug_regno (df, insn, file)
     struct df *df;
     rtx insn;
     FILE *file;
{
  unsigned int uid;
  int bbi;

  uid = INSN_UID (insn);
  if (uid >= df->insn_size)
    return;

  if (df->insns[uid].defs)
    bbi = DF_REF_BBNO (df->insns[uid].defs->ref);
  else  if (df->insns[uid].uses)
    bbi = DF_REF_BBNO (df->insns[uid].uses->ref);
  else
    bbi = -1;

  fprintf (file, "insn %d bb %d luid %d defs ",
Kazu Hirata committed
3521
	   uid, bbi, DF_INSN_LUID (df, insn));
Jeff Law committed
3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548
  df_chain_dump_regno (df->insns[uid].defs, file);
  fprintf (file, " uses ");
  df_chain_dump_regno (df->insns[uid].uses, file);
  fprintf (file, "\n");
}

static void
df_regno_debug (df, regno, file)
     struct df *df;
     unsigned int regno;
     FILE *file;
{
  if (regno >= df->reg_size)
    return;

  fprintf (file, "reg %d life %d defs ",
	   regno, df->regs[regno].lifetime);
  df_chain_dump (df->regs[regno].defs, file);
  fprintf (file, " uses ");
  df_chain_dump (df->regs[regno].uses, file);
  fprintf (file, "\n");
}


static void
df_ref_debug (df, ref, file)
     struct df *df;
3549
     struct ref *ref;
Jeff Law committed
3550 3551 3552 3553 3554
     FILE *file;
{
  fprintf (file, "%c%d ",
	   DF_REF_REG_DEF_P (ref) ? 'd' : 'u',
	   DF_REF_ID (ref));
3555
  fprintf (file, "reg %d bb %d luid %d insn %d chain ",
Jeff Law committed
3556
	   DF_REF_REGNO (ref),
3557
	   DF_REF_BBNO (ref),
Jeff Law committed
3558 3559 3560 3561 3562 3563 3564
	   DF_INSN_LUID (df, DF_REF_INSN (ref)),
	   INSN_UID (DF_REF_INSN (ref)));
  df_chain_dump (DF_REF_CHAIN (ref), file);
  fprintf (file, "\n");
}


3565
void
Jeff Law committed
3566 3567 3568 3569 3570 3571 3572 3573
debug_df_insn (insn)
     rtx insn;
{
  df_insn_debug (ddf, insn, stderr);
  debug_rtx (insn);
}


3574
void
Jeff Law committed
3575 3576 3577 3578 3579 3580 3581
debug_df_reg (reg)
     rtx reg;
{
  df_regno_debug (ddf, REGNO (reg), stderr);
}


3582
void
Jeff Law committed
3583 3584 3585 3586 3587 3588 3589
debug_df_regno (regno)
     unsigned int regno;
{
  df_regno_debug (ddf, regno, stderr);
}


3590
void
Jeff Law committed
3591 3592 3593 3594 3595 3596 3597
debug_df_ref (ref)
     struct ref *ref;
{
  df_ref_debug (ddf, ref, stderr);
}


3598
void
Jeff Law committed
3599 3600 3601 3602 3603 3604 3605
debug_df_defno (defno)
     unsigned int defno;
{
  df_ref_debug (ddf, ddf->defs[defno], stderr);
}


3606
void
Jeff Law committed
3607 3608 3609 3610 3611 3612 3613
debug_df_useno (defno)
     unsigned int defno;
{
  df_ref_debug (ddf, ddf->uses[defno], stderr);
}


3614
void
Jeff Law committed
3615 3616 3617 3618 3619 3620
debug_df_chain (link)
     struct df_link *link;
{
  df_chain_dump (link, stderr);
  fputc ('\n', stderr);
}
3621

3622 3623
/* Hybrid search algorithm from "Implementation Techniques for
   Efficient Data-Flow Analysis of Large Programs".  */
Kazu Hirata committed
3624 3625 3626
static void
hybrid_search_bitmap (block, in, out, gen, kill, dir,
		      conf_op, transfun, visited, pending,
3627 3628 3629
		      data)
     basic_block block;
     bitmap *in, *out, *gen, *kill;
3630 3631
     enum df_flow_dir dir;
     enum df_confluence_op conf_op;
3632 3633 3634
     transfer_function_bitmap transfun;
     sbitmap visited;
     sbitmap pending;
3635 3636 3637
     void *data;
{
  int changed;
3638
  int i = block->index;
3639
  edge e;
3640 3641 3642
  basic_block bb= block;
  SET_BIT (visited, block->index);
  if (TEST_BIT (pending, block->index))
3643 3644 3645 3646
    {
      if (dir == FORWARD)
	{
	  /*  Calculate <conf_op> of predecessor_outs */
3647
	  bitmap_zero (in[i]);
3648 3649 3650 3651 3652 3653 3654
	  for (e = bb->pred; e != 0; e = e->pred_next)
	    {
	      if (e->src == ENTRY_BLOCK_PTR)
		continue;
	      switch (conf_op)
		{
		case UNION:
3655
		  bitmap_a_or_b (in[i], in[i], out[e->src->index]);
3656 3657
		  break;
		case INTERSECTION:
3658
		  bitmap_a_and_b (in[i], in[i], out[e->src->index]);
3659 3660 3661 3662
		  break;
		}
	    }
	}
Kazu Hirata committed
3663
      else
3664 3665
	{
	  /* Calculate <conf_op> of successor ins */
3666
	  bitmap_zero(out[i]);
3667 3668 3669 3670 3671
	  for (e = bb->succ; e != 0; e = e->succ_next)
	    {
	      if (e->dest == EXIT_BLOCK_PTR)
		continue;
	      switch (conf_op)
Kazu Hirata committed
3672
		{
3673
		case UNION:
3674
		  bitmap_a_or_b (out[i], out[i], in[e->dest->index]);
3675 3676
		  break;
		case INTERSECTION:
3677
		  bitmap_a_and_b (out[i], out[i], in[e->dest->index]);
3678 3679 3680
		  break;
		}
	    }
Kazu Hirata committed
3681
	}
3682 3683
      /* Common part */
      (*transfun)(i, &changed, in[i], out[i], gen[i], kill[i], data);
3684
      RESET_BIT (pending, i);
3685 3686 3687 3688 3689 3690
      if (changed)
	{
	  if (dir == FORWARD)
	    {
	      for (e = bb->succ; e != 0; e = e->succ_next)
		{
3691
		  if (e->dest == EXIT_BLOCK_PTR || e->dest->index == i)
3692
		    continue;
3693
		  SET_BIT (pending, e->dest->index);
3694 3695 3696 3697 3698 3699
		}
	    }
	  else
	    {
	      for (e = bb->pred; e != 0; e = e->pred_next)
		{
3700
		  if (e->src == ENTRY_BLOCK_PTR || e->dest->index == i)
3701
		    continue;
3702
		  SET_BIT (pending, e->src->index);
3703 3704 3705 3706
		}
	    }
	}
    }
3707 3708 3709 3710
  if (dir == FORWARD)
    {
      for (e = bb->succ; e != 0; e = e->succ_next)
	{
3711
	  if (e->dest == EXIT_BLOCK_PTR || e->dest->index == i)
3712
	    continue;
3713 3714 3715
	  if (!TEST_BIT (visited, e->dest->index))
	    hybrid_search_bitmap (e->dest, in, out, gen, kill, dir,
				  conf_op, transfun, visited, pending,
3716 3717 3718 3719 3720 3721 3722
				  data);
	}
    }
  else
    {
      for (e = bb->pred; e != 0; e = e->pred_next)
	{
3723
	  if (e->src == ENTRY_BLOCK_PTR || e->src->index == i)
3724
	    continue;
3725
	  if (!TEST_BIT (visited, e->src->index))
Kazu Hirata committed
3726 3727
	    hybrid_search_bitmap (e->src, in, out, gen, kill, dir,
				  conf_op, transfun, visited, pending,
3728 3729 3730
				  data);
	}
    }
3731
}
3732 3733 3734


/* Hybrid search for sbitmaps, rather than bitmaps.  */
Kazu Hirata committed
3735 3736
static void
hybrid_search_sbitmap (block, in, out, gen, kill, dir,
3737 3738 3739 3740
		       conf_op, transfun, visited, pending,
		       data)
     basic_block block;
     sbitmap *in, *out, *gen, *kill;
3741 3742
     enum df_flow_dir dir;
     enum df_confluence_op conf_op;
3743 3744 3745
     transfer_function_sbitmap transfun;
     sbitmap visited;
     sbitmap pending;
3746 3747 3748
     void *data;
{
  int changed;
3749
  int i = block->index;
3750
  edge e;
3751 3752 3753
  basic_block bb= block;
  SET_BIT (visited, block->index);
  if (TEST_BIT (pending, block->index))
3754
    {
3755 3756 3757
      if (dir == FORWARD)
	{
	  /*  Calculate <conf_op> of predecessor_outs */
3758
	  sbitmap_zero (in[i]);
3759 3760 3761 3762 3763 3764 3765
	  for (e = bb->pred; e != 0; e = e->pred_next)
	    {
	      if (e->src == ENTRY_BLOCK_PTR)
		continue;
	      switch (conf_op)
		{
		case UNION:
3766
		  sbitmap_a_or_b (in[i], in[i], out[e->src->index]);
3767 3768
		  break;
		case INTERSECTION:
3769
		  sbitmap_a_and_b (in[i], in[i], out[e->src->index]);
3770 3771 3772 3773
		  break;
		}
	    }
	}
Kazu Hirata committed
3774
      else
3775 3776
	{
	  /* Calculate <conf_op> of successor ins */
3777
	  sbitmap_zero(out[i]);
3778 3779 3780 3781 3782
	  for (e = bb->succ; e != 0; e = e->succ_next)
	    {
	      if (e->dest == EXIT_BLOCK_PTR)
		continue;
	      switch (conf_op)
Kazu Hirata committed
3783
		{
3784
		case UNION:
3785
		  sbitmap_a_or_b (out[i], out[i], in[e->dest->index]);
3786 3787
		  break;
		case INTERSECTION:
3788
		  sbitmap_a_and_b (out[i], out[i], in[e->dest->index]);
3789 3790 3791
		  break;
		}
	    }
Kazu Hirata committed
3792
	}
3793 3794
      /* Common part */
      (*transfun)(i, &changed, in[i], out[i], gen[i], kill[i], data);
3795
      RESET_BIT (pending, i);
3796 3797 3798 3799 3800 3801
      if (changed)
	{
	  if (dir == FORWARD)
	    {
	      for (e = bb->succ; e != 0; e = e->succ_next)
		{
3802
		  if (e->dest == EXIT_BLOCK_PTR || e->dest->index == i)
3803
		    continue;
3804
		  SET_BIT (pending, e->dest->index);
3805 3806 3807 3808 3809 3810
		}
	    }
	  else
	    {
	      for (e = bb->pred; e != 0; e = e->pred_next)
		{
3811
		  if (e->src == ENTRY_BLOCK_PTR || e->dest->index == i)
3812
		    continue;
3813
		  SET_BIT (pending, e->src->index);
3814 3815 3816 3817
		}
	    }
	}
    }
3818 3819 3820 3821
  if (dir == FORWARD)
    {
      for (e = bb->succ; e != 0; e = e->succ_next)
	{
3822
	  if (e->dest == EXIT_BLOCK_PTR || e->dest->index == i)
3823
	    continue;
3824
	  if (!TEST_BIT (visited, e->dest->index))
Kazu Hirata committed
3825 3826
	    hybrid_search_sbitmap (e->dest, in, out, gen, kill, dir,
				   conf_op, transfun, visited, pending,
3827 3828 3829 3830 3831 3832 3833
				   data);
	}
    }
  else
    {
      for (e = bb->pred; e != 0; e = e->pred_next)
	{
3834
	  if (e->src == ENTRY_BLOCK_PTR || e->src->index == i)
3835
	    continue;
3836
	  if (!TEST_BIT (visited, e->src->index))
Kazu Hirata committed
3837 3838
	    hybrid_search_sbitmap (e->src, in, out, gen, kill, dir,
				   conf_op, transfun, visited, pending,
3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855
				   data);
	}
    }
}




/* gen = GEN set.
   kill = KILL set.
   in, out = Filled in by function.
   blocks = Blocks to analyze.
   dir = Dataflow direction.
   conf_op = Confluence operation.
   transfun = Transfer function.
   order = Order to iterate in. (Should map block numbers -> order)
   data = Whatever you want.  It's passed to the transfer function.
Kazu Hirata committed
3856

3857 3858 3859 3860 3861 3862
   This function will perform iterative bitvector dataflow, producing
   the in and out sets.  Even if you only want to perform it for a
   small number of blocks, the vectors for in and out must be large
   enough for *all* blocks, because changing one block might affect
   others.  However, it'll only put what you say to analyze on the
   initial worklist.
Kazu Hirata committed
3863

3864 3865 3866 3867
   For forward problems, you probably want to pass in a mapping of
   block number to rc_order (like df->inverse_rc_map).
*/
void
Kazu Hirata committed
3868 3869
iterative_dataflow_sbitmap (in, out, gen, kill, blocks,
			    dir, conf_op, transfun, order, data)
3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881
     sbitmap *in, *out, *gen, *kill;
     bitmap blocks;
     enum df_flow_dir dir;
     enum df_confluence_op conf_op;
     transfer_function_sbitmap transfun;
     int *order;
     void *data;
{
  int i;
  fibheap_t worklist;
  basic_block bb;
  sbitmap visited, pending;
3882 3883
  pending = sbitmap_alloc (last_basic_block);
  visited = sbitmap_alloc (last_basic_block);
3884 3885 3886 3887 3888
  sbitmap_zero (pending);
  sbitmap_zero (visited);
  worklist = fibheap_new ();
  EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i,
  {
Kazu Hirata committed
3889
    fibheap_insert (worklist, order[i], (void *) (size_t) i);
3890 3891 3892 3893 3894 3895 3896 3897 3898 3899
    SET_BIT (pending, i);
    if (dir == FORWARD)
      sbitmap_copy (out[i], gen[i]);
    else
      sbitmap_copy (in[i], gen[i]);
  });
  while (sbitmap_first_set_bit (pending) != -1)
    {
      while (!fibheap_empty (worklist))
	{
3900
	  i = (size_t) fibheap_extract_min (worklist);
3901
	  bb = BASIC_BLOCK (i);
3902
	  if (!TEST_BIT (visited, bb->index))
Kazu Hirata committed
3903
	    hybrid_search_sbitmap (bb, in, out, gen, kill, dir,
3904
				   conf_op, transfun, visited, pending, data);
3905 3906 3907 3908 3909
	}
      if (sbitmap_first_set_bit (pending) != -1)
	{
	  EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i,
	  {
3910
	    fibheap_insert (worklist, order[i], (void *) (size_t) i);
3911 3912 3913 3914 3915 3916
	  });
	  sbitmap_zero (visited);
	}
      else
	{
	  break;
Kazu Hirata committed
3917
	}
3918 3919 3920
    }
  sbitmap_free (pending);
  sbitmap_free (visited);
3921
  fibheap_delete (worklist);
3922 3923 3924 3925 3926
}

/* Exactly the same as iterative_dataflow_sbitmap, except it works on
   bitmaps instead */
void
Kazu Hirata committed
3927 3928
iterative_dataflow_bitmap (in, out, gen, kill, blocks,
			   dir, conf_op, transfun, order, data)
3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940
     bitmap *in, *out, *gen, *kill;
     bitmap blocks;
     enum df_flow_dir dir;
     enum df_confluence_op conf_op;
     transfer_function_bitmap transfun;
     int *order;
     void *data;
{
  int i;
  fibheap_t worklist;
  basic_block bb;
  sbitmap visited, pending;
3941 3942
  pending = sbitmap_alloc (last_basic_block);
  visited = sbitmap_alloc (last_basic_block);
3943 3944 3945 3946 3947
  sbitmap_zero (pending);
  sbitmap_zero (visited);
  worklist = fibheap_new ();
  EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i,
  {
3948
    fibheap_insert (worklist, order[i], (void *) (size_t) i);
3949 3950 3951 3952 3953 3954 3955 3956 3957 3958
    SET_BIT (pending, i);
    if (dir == FORWARD)
      bitmap_copy (out[i], gen[i]);
    else
      bitmap_copy (in[i], gen[i]);
  });
  while (sbitmap_first_set_bit (pending) != -1)
    {
      while (!fibheap_empty (worklist))
	{
3959
	  i = (size_t) fibheap_extract_min (worklist);
3960
	  bb = BASIC_BLOCK (i);
3961
	  if (!TEST_BIT (visited, bb->index))
Kazu Hirata committed
3962
	    hybrid_search_bitmap (bb, in, out, gen, kill, dir,
3963
				  conf_op, transfun, visited, pending, data);
3964 3965 3966 3967 3968
	}
      if (sbitmap_first_set_bit (pending) != -1)
	{
	  EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i,
	  {
3969
	    fibheap_insert (worklist, order[i], (void *) (size_t) i);
3970 3971 3972 3973 3974 3975
	  });
	  sbitmap_zero (visited);
	}
      else
	{
	  break;
Kazu Hirata committed
3976
	}
3977 3978 3979
    }
  sbitmap_free (pending);
  sbitmap_free (visited);
Kazu Hirata committed
3980
  fibheap_delete (worklist);
3981
}