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

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

9 10 11 12
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
13

14 15 16 17
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
18 19

You should have received a copy of the GNU General Public License
20
along with GCC; see the file COPYING.  If not, write to the Free
Kelley Cook committed
21 22
Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
Jeff Law committed
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


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 ();

49
      df_analyze (df, 0, DF_ALL);
Jeff Law committed
50 51 52 53 54 55 56 57

      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
58
object and frees up any allocated memory.   DF_ALL says to analyze
59
everything.
Jeff Law committed
60

61
df_analyze performs the following:
Jeff Law committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

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
86
marks all the modified insns to get processed the next time df_analyze
Jeff Law committed
87 88 89 90
 is called.

Beware that tinkering with insns may invalidate the dataflow information.
The philosophy behind these routines is that once the dataflow
91
information has been gathered, the user should store what they require
Jeff Law committed
92 93
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
94
whole lot of changes have been made, df_analyze can be called again
Jeff Law committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
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.

110 111
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
112
rather than searching the def or use bitmaps.
Jeff Law committed
113 114 115 116

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

117

Jeff Law committed
118 119 120 121 122 123 124 125 126 127 128 129 130
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
131
insns so when df_analyze is called we can easily determine all the new
Jeff Law committed
132 133 134
or deleted refs.  Currently the global dataflow information is
recomputed from scratch but this could be propagated more efficiently.

135
2) Reduced memory requirements.
Jeff Law committed
136 137 138 139 140 141 142 143

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.

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

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
148
(within a BB)?
Jeff Law committed
149

150
4) Working with a sub-CFG.
Jeff Law committed
151

152
Often the whole CFG does not need to be analyzed, for example,
153
when optimizing a loop, only certain registers are of interest.
154
Perhaps there should be a bitmap argument to df_analyze to specify
155
which registers should be analyzed?
156 157 158 159 160


NOTES:

Embedded addressing side-effects, such as POST_INC or PRE_INC, generate
161
both a use and a def.  These are both marked read/write to show that they
162
are dependent. For example, (set (reg 40) (mem (post_inc (reg 42))))
163 164
will generate a use of reg 42 followed by a def of reg 42 (both marked
read/write).  Similarly, (set (reg 40) (mem (pre_dec (reg 41))))
165 166 167 168
generates a use of reg 41 then a def of reg 41 (both marked read/write),
even though reg 41 is decremented before it is used for the memory
address in this second example.

169 170 171 172 173 174 175
A set to a REG inside a ZERO_EXTRACT, or a set to a non-paradoxical SUBREG
for which the number of word_mode units covered by the outer mode is
smaller than that covered by the inner mode, invokes a read-modify-write.
operation.  We generate both a use and a def and again mark them
read/write.
Paradoxical subreg writes don't leave a trace of the old content, so they
are write-only operations.  */
Jeff Law committed
176 177 178

#include "config.h"
#include "system.h"
179 180
#include "coretypes.h"
#include "tm.h"
181 182 183 184 185 186
#include "rtl.h"
#include "tm_p.h"
#include "insn-config.h"
#include "recog.h"
#include "function.h"
#include "regs.h"
187
#include "alloc-pool.h"
Jeff Law committed
188 189
#include "hard-reg-set.h"
#include "basic-block.h"
190
#include "sbitmap.h"
Jeff Law committed
191 192 193
#include "bitmap.h"
#include "df.h"

Kazu Hirata committed
194 195 196 197
#define FOR_EACH_BB_IN_BITMAP(BITMAP, MIN, BB, CODE)	\
  do							\
    {							\
      unsigned int node_;				\
198 199 200 201 202 203
      bitmap_iterator bi;				\
      EXECUTE_IF_SET_IN_BITMAP (BITMAP, MIN, node_, bi)	\
	{						\
	  (BB) = BASIC_BLOCK (node_);			\
	  CODE;						\
	}						\
Kazu Hirata committed
204 205
    }							\
  while (0)
Jeff Law committed
206

207 208
static alloc_pool df_ref_pool;
static alloc_pool df_link_pool;
Jeff Law committed
209 210
static struct df *ddf;

211 212
static void df_reg_table_realloc (struct df *, int);
static void df_insn_table_realloc (struct df *, unsigned int);
213 214
static void df_bb_table_realloc (struct df *, unsigned int);
static void df_bitmaps_alloc (struct df *, bitmap, int);
215 216 217 218 219 220 221 222 223 224 225
static void df_bitmaps_free (struct df *, int);
static void df_free (struct df *);
static void df_alloc (struct df *, int);

static rtx df_reg_use_gen (unsigned int);

static inline struct df_link *df_link_create (struct ref *, struct df_link *);
static struct df_link *df_ref_unlink (struct df_link **, struct ref *);
static void df_def_unlink (struct df *, struct ref *);
static void df_use_unlink (struct df *, struct ref *);
static void df_insn_refs_unlink (struct df *, basic_block, rtx);
Jeff Law committed
226
#if 0
227 228
static void df_bb_refs_unlink (struct df *, basic_block);
static void df_refs_unlink (struct df *, bitmap);
Jeff Law committed
229 230
#endif

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
static struct ref *df_ref_create (struct df *, rtx, rtx *, rtx,
				  enum df_ref_type, enum df_ref_flags);
static void df_ref_record_1 (struct df *, rtx, rtx *, rtx, enum df_ref_type,
			     enum df_ref_flags);
static void df_ref_record (struct df *, rtx, rtx *, rtx, enum df_ref_type,
			   enum df_ref_flags);
static void df_def_record_1 (struct df *, rtx, basic_block, rtx);
static void df_defs_record (struct df *, rtx, basic_block, rtx);
static void df_uses_record (struct df *, rtx *, enum df_ref_type,
			    basic_block, rtx, enum df_ref_flags);
static void df_insn_refs_record (struct df *, basic_block, rtx);
static void df_bb_refs_record (struct df *, basic_block);
static void df_refs_record (struct df *, bitmap);

static void df_bb_reg_def_chain_create (struct df *, basic_block);
246
static void df_reg_def_chain_create (struct df *, bitmap, bool);
247
static void df_bb_reg_use_chain_create (struct df *, basic_block);
248
static void df_reg_use_chain_create (struct df *, bitmap, bool);
249 250 251 252
static void df_bb_du_chain_create (struct df *, basic_block, bitmap);
static void df_du_chain_create (struct df *, bitmap);
static void df_bb_ud_chain_create (struct df *, basic_block);
static void df_ud_chain_create (struct df *, bitmap);
253
static void df_bb_rd_local_compute (struct df *, basic_block, bitmap);
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
static void df_rd_local_compute (struct df *, bitmap);
static void df_bb_ru_local_compute (struct df *, basic_block);
static void df_ru_local_compute (struct df *, bitmap);
static void df_bb_lr_local_compute (struct df *, basic_block);
static void df_lr_local_compute (struct df *, bitmap);
static void df_bb_reg_info_compute (struct df *, basic_block, bitmap);
static void df_reg_info_compute (struct df *, bitmap);

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

static int df_modified_p (struct df *, bitmap);
static int df_refs_queue (struct df *);
static int df_refs_process (struct df *);
static int df_bb_refs_update (struct df *, basic_block);
269
static int df_refs_update (struct df *, bitmap);
270
static void df_analyze_1 (struct df *, bitmap, int, int);
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287

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

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

static void df_chain_dump (struct df_link *, FILE *file);
static void df_chain_dump_regno (struct df_link *, FILE *file);
static void df_regno_debug (struct df *, unsigned int, FILE *);
static void df_ref_debug (struct df *, struct ref *, FILE *);
288 289 290 291 292 293 294 295
static void df_rd_transfer_function (int, int *, void *, void *, void *,
				     void *, void *);
static void df_ru_transfer_function (int, int *, void *, void *, void *,
				     void *, void *);
static void df_lr_transfer_function (int, int *, void *, void *, void *,
				     void *, void *);
static void hybrid_search (basic_block, struct dataflow *,
			   sbitmap, sbitmap, sbitmap);
Jeff Law committed
296 297 298 299 300


/* Local memory allocation/deallocation routines.  */


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

310
  /* Make the table a little larger than requested, so we do not need
311 312
     to enlarge it so often.  */
  size += df->insn_size / 4;
313

314
  df->insns = xrealloc (df->insns, size * sizeof (struct insn_info));
315 316

  memset (df->insns + df->insn_size, 0,
Jeff Law committed
317 318 319 320 321 322
	  (size - df->insn_size) * sizeof (struct insn_info));

  df->insn_size = size;

  if (! df->insns_modified)
    {
323
      df->insns_modified = BITMAP_ALLOC (NULL);
Jeff Law committed
324 325 326 327
      bitmap_zero (df->insns_modified);
    }
}

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
/* Increase the bb info table to have space for at least SIZE + 1
   elements.  */

static void
df_bb_table_realloc (struct df *df, unsigned int size)
{
  size++;
  if (size <= df->n_bbs)
    return;

  /* Make the table a little larger than requested, so we do not need
     to enlarge it so often.  */
  size += df->n_bbs / 4;

  df->bbs = xrealloc (df->bbs, size * sizeof (struct bb_info));

  memset (df->bbs + df->n_bbs, 0, (size - df->n_bbs) * sizeof (struct bb_info));

  df->n_bbs = size;
}
Jeff Law committed
348 349 350

/* Increase the reg info table by SIZE more elements.  */
static void
351
df_reg_table_realloc (struct df *df, int size)
Jeff Law committed
352 353 354 355 356 357
{
  /* Make table 25 percent larger by default.  */
  if (! size)
    size = df->reg_size / 4;

  size += df->reg_size;
358 359
  if (size < max_reg_num ())
    size = max_reg_num ();
Jeff Law committed
360

361
  df->regs = xrealloc (df->regs, size * sizeof (struct reg_info));
362 363
  df->reg_def_last = xrealloc (df->reg_def_last,
			       size * sizeof (struct ref *));
Jeff Law committed
364 365

  /* Zero the new entries.  */
366
  memset (df->regs + df->reg_size, 0,
Jeff Law committed
367 368 369 370 371 372 373
	  (size - df->reg_size) * sizeof (struct reg_info));

  df->reg_size = size;
}


/* Allocate bitmaps for each basic block.  */
374

Jeff Law committed
375
static void
376
df_bitmaps_alloc (struct df *df, bitmap blocks, int flags)
Jeff Law committed
377
{
378
  basic_block bb;
Jeff Law committed
379 380 381 382

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

383 384 385 386
  if (!blocks)
    blocks = df->all_blocks;

  FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
Jeff Law committed
387 388
    {
      struct bb_info *bb_info = DF_BB_INFO (df, bb);
389

390
      if (flags & DF_RD)
Jeff Law committed
391
	{
392 393 394
	  if (!bb_info->rd_in)
	    {
	      /* Allocate bitmaps for reaching definitions.  */
395 396 397 398
	      bb_info->rd_kill = BITMAP_ALLOC (NULL);
	      bb_info->rd_gen = BITMAP_ALLOC (NULL);
	      bb_info->rd_in = BITMAP_ALLOC (NULL);
	      bb_info->rd_out = BITMAP_ALLOC (NULL);
399 400 401 402 403 404 405 406
	    }
	  else
	    {
	      bitmap_clear (bb_info->rd_kill);
	      bitmap_clear (bb_info->rd_gen);
	      bitmap_clear (bb_info->rd_in);
	      bitmap_clear (bb_info->rd_out);
	    }
Jeff Law committed
407 408
	}

409
      if (flags & DF_RU)
Jeff Law committed
410
	{
411 412 413
	  if (!bb_info->ru_in)
	    {
	      /* Allocate bitmaps for upward exposed uses.  */
414 415 416 417
	      bb_info->ru_kill = BITMAP_ALLOC (NULL);
	      bb_info->ru_gen = BITMAP_ALLOC (NULL);
	      bb_info->ru_in = BITMAP_ALLOC (NULL);
	      bb_info->ru_out = BITMAP_ALLOC (NULL);
418 419 420 421 422 423 424 425
	    }
	  else
	    {
	      bitmap_clear (bb_info->ru_kill);
	      bitmap_clear (bb_info->ru_gen);
	      bitmap_clear (bb_info->ru_in);
	      bitmap_clear (bb_info->ru_out);
	    }
Jeff Law committed
426 427
	}

428
      if (flags & DF_LR)
Jeff Law committed
429
	{
430 431 432
	  if (!bb_info->lr_in)
	    {
	      /* Allocate bitmaps for live variables.  */
433 434 435 436
	      bb_info->lr_def = BITMAP_ALLOC (NULL);
	      bb_info->lr_use = BITMAP_ALLOC (NULL);
	      bb_info->lr_in = BITMAP_ALLOC (NULL);
	      bb_info->lr_out = BITMAP_ALLOC (NULL);
437 438 439 440 441 442 443 444
	    }
	  else
	    {
	      bitmap_clear (bb_info->lr_def);
	      bitmap_clear (bb_info->lr_use);
	      bitmap_clear (bb_info->lr_in);
	      bitmap_clear (bb_info->lr_out);
	    }
Jeff Law committed
445
	}
446
    });
Jeff Law committed
447 448 449 450 451
}


/* Free bitmaps for each basic block.  */
static void
452
df_bitmaps_free (struct df *df, int flags)
Jeff Law committed
453
{
454
  unsigned i;
Jeff Law committed
455

456
  for (i = 0; i < df->n_bbs; i++)
Jeff Law committed
457
    {
458
      struct bb_info *bb_info = &df->bbs[i];
Jeff Law committed
459 460 461 462

      if ((flags & DF_RD) && bb_info->rd_in)
	{
	  /* Free bitmaps for reaching definitions.  */
463
	  BITMAP_FREE (bb_info->rd_kill);
Jeff Law committed
464
	  bb_info->rd_kill = NULL;
465
	  BITMAP_FREE (bb_info->rd_gen);
Jeff Law committed
466
	  bb_info->rd_gen = NULL;
467
	  BITMAP_FREE (bb_info->rd_in);
Jeff Law committed
468
	  bb_info->rd_in = NULL;
469
	  BITMAP_FREE (bb_info->rd_out);
Jeff Law committed
470 471 472 473 474 475
	  bb_info->rd_out = NULL;
	}

      if ((flags & DF_RU) && bb_info->ru_in)
	{
	  /* Free bitmaps for upward exposed uses.  */
476
	  BITMAP_FREE (bb_info->ru_kill);
Jeff Law committed
477
	  bb_info->ru_kill = NULL;
478
	  BITMAP_FREE (bb_info->ru_gen);
Jeff Law committed
479
	  bb_info->ru_gen = NULL;
480
	  BITMAP_FREE (bb_info->ru_in);
Jeff Law committed
481
	  bb_info->ru_in = NULL;
482
	  BITMAP_FREE (bb_info->ru_out);
Jeff Law committed
483 484 485 486 487 488
	  bb_info->ru_out = NULL;
	}

      if ((flags & DF_LR) && bb_info->lr_in)
	{
	  /* Free bitmaps for live variables.  */
489
	  BITMAP_FREE (bb_info->lr_def);
Jeff Law committed
490
	  bb_info->lr_def = NULL;
491
	  BITMAP_FREE (bb_info->lr_use);
Jeff Law committed
492
	  bb_info->lr_use = NULL;
493
	  BITMAP_FREE (bb_info->lr_in);
Jeff Law committed
494
	  bb_info->lr_in = NULL;
495
	  BITMAP_FREE (bb_info->lr_out);
Jeff Law committed
496 497 498 499 500 501 502
	  bb_info->lr_out = NULL;
	}
    }
  df->flags &= ~(flags & (DF_RD | DF_RU | DF_LR));
}


503
/* Allocate and initialize dataflow memory.  */
Jeff Law committed
504
static void
505
df_alloc (struct df *df, int n_regs)
Jeff Law committed
506 507
{
  int n_insns;
508
  basic_block bb;
Jeff Law committed
509

510 511 512
  df_link_pool = create_alloc_pool ("df_link pool", sizeof (struct df_link),
				    100);
  df_ref_pool  = create_alloc_pool ("df_ref pool", sizeof (struct ref), 100);
Jeff Law committed
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530

  /* 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;
531
  df->n_bbs = last_basic_block;
Jeff Law committed
532 533 534 535 536 537

  /* Allocate temporary working array used during local dataflow analysis.  */
  df_insn_table_realloc (df, n_insns);

  df_reg_table_realloc (df, df->n_regs);

538
  df->bbs_modified = BITMAP_ALLOC (NULL);
Jeff Law committed
539 540 541 542
  bitmap_zero (df->bbs_modified);

  df->flags = 0;

543
  df->bbs = xcalloc (last_basic_block, sizeof (struct bb_info));
Jeff Law committed
544

545
  df->all_blocks = BITMAP_ALLOC (NULL);
546 547
  FOR_EACH_BB (bb)
    bitmap_set_bit (df->all_blocks, bb->index);
Jeff Law committed
548 549 550 551 552
}


/* Free all the dataflow info.  */
static void
553
df_free (struct df *df)
Jeff Law committed
554 555 556 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
{
  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;

583
  BITMAP_FREE (df->bbs_modified);
Jeff Law committed
584 585
  df->bbs_modified = 0;

586
  BITMAP_FREE (df->insns_modified);
Jeff Law committed
587 588
  df->insns_modified = 0;

589
  BITMAP_FREE (df->all_blocks);
Jeff Law committed
590 591
  df->all_blocks = 0;

592 593
  free_alloc_pool (df_ref_pool);
  free_alloc_pool (df_link_pool);
Jeff Law committed
594 595 596 597 598
}

/* Local miscellaneous routines.  */

/* Return a USE for register REGNO.  */
599
static rtx df_reg_use_gen (unsigned int regno)
Jeff Law committed
600 601 602 603
{
  rtx reg;
  rtx use;

604
  reg = regno_reg_rtx[regno];
605

Jeff Law committed
606 607 608 609 610 611 612 613
  use = gen_rtx_USE (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 *
614
df_link_create (struct ref *ref, struct df_link *next)
Jeff Law committed
615 616 617
{
  struct df_link *link;

618
  link = pool_alloc (df_link_pool);
Jeff Law committed
619 620 621 622 623
  link->next = next;
  link->ref = ref;
  return link;
}

624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
/* Releases members of the CHAIN.  */

static void
free_reg_ref_chain (struct df_link **chain)
{
  struct df_link *act, *next;

  for (act = *chain; act; act = next)
    {
      next = act->next;
      pool_free (df_link_pool, act);
    }

  *chain = NULL;
}
Jeff Law committed
639 640 641

/* Add REF to chain head pointed to by PHEAD.  */
static struct df_link *
642
df_ref_unlink (struct df_link **phead, struct ref *ref)
Jeff Law committed
643 644 645 646 647 648 649 650 651 652
{
  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.  */
653 654
	  gcc_assert (link->ref == ref);
	  
Jeff Law committed
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
	  /* 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
684
df_ref_remove (struct df *df, struct ref *ref)
Jeff Law committed
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
{
  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.  */
701
static void
702
df_def_unlink (struct df *df ATTRIBUTE_UNUSED, struct ref *def)
Jeff Law committed
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
{
  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.  */
725
static void
726
df_use_unlink (struct df *df ATTRIBUTE_UNUSED, struct ref *use)
Jeff Law committed
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
{
  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 *
753 754
df_ref_create (struct df *df, rtx reg, rtx *loc, rtx insn,
	       enum df_ref_type ref_type, enum df_ref_flags ref_flags)
Jeff Law committed
755 756
{
  struct ref *this_ref;
757

758
  this_ref = pool_alloc (df_ref_pool);
Jeff Law committed
759 760 761 762 763
  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;
764
  DF_REF_FLAGS (this_ref) = ref_flags;
765
  DF_REF_DATA (this_ref) = NULL;
Jeff Law committed
766 767 768 769 770 771 772

  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);
773
	  df->defs = xrealloc (df->defs,
Jeff Law committed
774 775 776 777 778 779 780 781 782 783 784
			       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);
785
	  df->uses = xrealloc (df->uses,
Jeff Law committed
786 787 788 789 790 791 792 793 794 795 796 797
			       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
798 799
df_ref_record_1 (struct df *df, rtx reg, rtx *loc, rtx insn,
		 enum df_ref_type ref_type, enum df_ref_flags ref_flags)
Jeff Law committed
800
{
801
  df_ref_create (df, reg, loc, insn, ref_type, ref_flags);
Jeff Law committed
802 803 804 805 806 807
}


/* Create new references of type DF_REF_TYPE for each part of register REG
   at address LOC within INSN of BB.  */
static void
808 809
df_ref_record (struct df *df, rtx reg, rtx *loc, rtx insn,
	       enum df_ref_type ref_type, enum df_ref_flags ref_flags)
Jeff Law committed
810 811 812
{
  unsigned int regno;

813
  gcc_assert (REG_P (reg) || GET_CODE (reg) == SUBREG);
Jeff Law committed
814 815 816 817 818 819

  /* 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.  */
820 821
  if ((df->flags & DF_SUBREGS) == 0
      && GET_CODE (reg) == SUBREG
Jeff Law committed
822
      && (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (word_mode)
Kazu Hirata committed
823
	  || GET_MODE_SIZE (GET_MODE (reg))
Jeff Law committed
824 825 826 827
	       >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (reg)))))
    {
      loc = &SUBREG_REG (reg);
      reg = *loc;
828
      ref_flags |= DF_REF_STRIPPED;
Jeff Law committed
829 830 831 832 833 834 835
    }

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

Jeff Law committed
837 838 839
      if (! (df->flags & DF_HARD_REGS))
	return;

840
      /* GET_MODE (reg) is correct here.  We do not want to go into a SUBREG
Jeff Law committed
841
         for the mode, because we only want to add references to regs, which
842
	 are really referenced.  E.g., a (subreg:SI (reg:DI 0) 0) does _not_
Jeff Law committed
843 844
	 reference the whole reg 0 in DI mode (which would also include
	 reg 1, at least, if 0 and 1 are SImode registers).  */
845
      endregno = hard_regno_nregs[regno][GET_MODE (reg)];
846 847 848 849
      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
850 851

      for (i = regno; i < endregno; i++)
852
	df_ref_record_1 (df, regno_reg_rtx[i],
853
			 loc, insn, ref_type, ref_flags);
Jeff Law committed
854 855 856
    }
  else
    {
857
      df_ref_record_1 (df, reg, loc, insn, ref_type, ref_flags);
Jeff Law committed
858 859 860
    }
}

861

862 863 864 865
/* A set to a non-paradoxical SUBREG for which the number of word_mode units
   covered by the outer mode is smaller than that covered by the inner mode,
   is a read-modify-write operation.
   This function returns true iff the SUBREG X is such a SUBREG.  */
866
bool
867
read_modify_subreg_p (rtx x)
868
{
869
  unsigned int isize, osize;
870 871
  if (GET_CODE (x) != SUBREG)
    return false;
872 873
  isize = GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)));
  osize = GET_MODE_SIZE (GET_MODE (x));
874
  return (isize > osize && isize > UNITS_PER_WORD);
875 876
}

877

Jeff Law committed
878 879
/* Process all the registers defined in the rtx, X.  */
static void
880
df_def_record_1 (struct df *df, rtx x, basic_block bb, rtx insn)
Jeff Law committed
881
{
882 883
  rtx *loc;
  rtx dst;
884
  enum df_ref_flags flags = 0;
Jeff Law committed
885

886
 /* We may recursively call ourselves on EXPR_LIST when dealing with PARALLEL
887
     construct.  */
888 889 890 891 892 893
  if (GET_CODE (x) == EXPR_LIST || GET_CODE (x) == CLOBBER)
    loc = &XEXP (x, 0);
  else
    loc = &SET_DEST (x);
  dst = *loc;

Jeff Law committed
894 895 896 897 898 899 900
  /* 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--)
901 902 903 904 905 906
	{
	  rtx temp = XVECEXP (dst, 0, i);
	  if (GET_CODE (temp) == EXPR_LIST || GET_CODE (temp) == CLOBBER
	      || GET_CODE (temp) == SET)
	    df_def_record_1 (df, temp, bb, insn);
	}
Jeff Law committed
907 908 909
      return;
    }

910 911
  /* Maybe, we should flag the use of STRICT_LOW_PART somehow.  It might
     be handy for the reg allocator.  */
Jeff Law committed
912
  while (GET_CODE (dst) == STRICT_LOW_PART
Kazu Hirata committed
913
	 || GET_CODE (dst) == ZERO_EXTRACT
914
	 || read_modify_subreg_p (dst))
Jeff Law committed
915
    {
916
      /* Strict low part always contains SUBREG, but we do not want to make
917
	 it appear outside, as whole register is always considered.  */
918 919 920 921 922
      if (GET_CODE (dst) == STRICT_LOW_PART)
	{
	  loc = &XEXP (dst, 0);
	  dst = *loc;
	}
Jeff Law committed
923 924
      loc = &XEXP (dst, 0);
      dst = *loc;
925
      flags |= DF_REF_READ_WRITE;
Jeff Law committed
926
    }
Kazu Hirata committed
927

928 929
  if (REG_P (dst)
      || (GET_CODE (dst) == SUBREG && REG_P (SUBREG_REG (dst))))
Kazu Hirata committed
930
    df_ref_record (df, dst, loc, insn, DF_REF_REG_DEF, flags);
Jeff Law committed
931 932 933 934 935
}


/* Process all the registers defined in the pattern rtx, X.  */
static void
936
df_defs_record (struct df *df, rtx x, basic_block bb, rtx insn)
Jeff Law committed
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
{
  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
962 963
df_uses_record (struct df *df, rtx *loc, enum df_ref_type ref_type,
		basic_block bb, rtx insn, enum df_ref_flags flags)
Jeff Law committed
964 965 966 967 968
{
  RTX_CODE code;
  rtx x;
 retry:
  x = *loc;
969 970
  if (!x)
    return;
Jeff Law committed
971 972 973 974 975 976 977 978
  code = GET_CODE (x);
  switch (code)
    {
    case LABEL_REF:
    case SYMBOL_REF:
    case CONST_INT:
    case CONST:
    case CONST_DOUBLE:
979
    case CONST_VECTOR:
Jeff Law committed
980
    case PC:
981
    case CC0:
Jeff Law committed
982 983 984 985 986 987 988
    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.  */
989
      if (MEM_P (XEXP (x, 0)))
990
	df_uses_record (df, &XEXP (XEXP (x, 0), 0),
991
			DF_REF_REG_MEM_STORE, bb, insn, flags);
Jeff Law committed
992 993 994 995 996

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

    case MEM:
997
      df_uses_record (df, &XEXP (x, 0), DF_REF_REG_MEM_LOAD, bb, insn, 0);
Jeff Law committed
998 999 1000 1001 1002
      return;

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

1003
      /* In case the SUBREG is not of a REG, do not optimize.  */
1004
      if (!REG_P (SUBREG_REG (x)))
Jeff Law committed
1005 1006
	{
	  loc = &SUBREG_REG (x);
1007
	  df_uses_record (df, loc, ref_type, bb, insn, flags);
Jeff Law committed
1008 1009 1010 1011 1012
	  return;
	}
      /* ... Fall through ...  */

    case REG:
1013
      df_ref_record (df, x, loc, insn, ref_type, flags);
Jeff Law committed
1014 1015 1016 1017 1018 1019
      return;

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

1020
	df_uses_record (df, &SET_SRC (x), DF_REF_REG_USE, bb, insn, 0);
1021

1022
	switch (GET_CODE (dst))
Jeff Law committed
1023
	  {
1024
	    case SUBREG:
1025
	      if (read_modify_subreg_p (dst))
1026 1027
		{
		  df_uses_record (df, &SUBREG_REG (dst), DF_REF_REG_USE, bb,
1028
				  insn, DF_REF_READ_WRITE);
1029 1030
		  break;
		}
1031
	      /* Fall through.  */
1032
	    case REG:
1033
	    case PARALLEL:
1034
	    case SCRATCH:
1035 1036 1037
	    case PC:
	    case CC0:
		break;
1038
	    case MEM:
Kazu Hirata committed
1039
	      df_uses_record (df, &XEXP (dst, 0),
1040 1041 1042 1043
			      DF_REF_REG_MEM_STORE,
			      bb, insn, 0);
	      break;
	    case STRICT_LOW_PART:
1044 1045
	      /* A strict_low_part uses the whole REG and not just the
		 SUBREG.  */
1046
	      dst = XEXP (dst, 0);
1047
	      gcc_assert (GET_CODE (dst) == SUBREG);
1048
	      df_uses_record (df, &SUBREG_REG (dst), DF_REF_REG_USE, bb,
1049
			     insn, DF_REF_READ_WRITE);
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
	      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:
1060
	      gcc_unreachable ();
Jeff Law committed
1061
	  }
1062
	return;
Jeff Law committed
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
      }

    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
1079
	   pseudo-regs because it might give an incorrectly rounded result.
Jeff Law committed
1080 1081 1082 1083

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

Kazu Hirata committed
1084
	/* For all ASM_OPERANDS, we must traverse the vector of input operands.
Jeff Law committed
1085 1086 1087 1088 1089 1090 1091 1092
	   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++)
1093
	      df_uses_record (df, &ASM_OPERANDS_INPUT (x, j),
1094
			      DF_REF_REG_USE, bb, insn, 0);
1095
	    return;
Jeff Law committed
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
	  }
	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.  */
1107
      df_ref_record (df, XEXP (x, 0), &XEXP (x, 0), insn, DF_REF_REG_DEF, DF_REF_READ_WRITE);
Jeff Law committed
1108

1109
      /* ... Fall through to handle uses ...  */
Jeff Law committed
1110 1111 1112 1113 1114 1115 1116

    default:
      break;
    }

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

Jeff Law committed
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
    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;
	      }
1130
	    df_uses_record (df, &XEXP (x, i), ref_type, bb, insn, flags);
Jeff Law committed
1131 1132 1133 1134 1135 1136
	  }
	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,
1137
			      bb, insn, flags);
Jeff Law committed
1138 1139 1140 1141 1142 1143 1144 1145
	  }
      }
  }
}


/* Record all the df within INSN of basic block BB.  */
static void
1146
df_insn_refs_record (struct df *df, basic_block bb, rtx insn)
Jeff Law committed
1147 1148 1149 1150 1151
{
  int i;

  if (INSN_P (insn))
    {
1152 1153
      rtx note;

1154
      /* Record register defs.  */
Jeff Law committed
1155
      df_defs_record (df, PATTERN (insn), bb, insn);
1156 1157 1158 1159 1160 1161 1162

      if (df->flags & DF_EQUIV_NOTES)
	for (note = REG_NOTES (insn); note;
	     note = XEXP (note, 1))
	  {
	    switch (REG_NOTE_KIND (note))
	      {
Kazu Hirata committed
1163 1164 1165 1166 1167 1168
	      case REG_EQUIV:
	      case REG_EQUAL:
		df_uses_record (df, &XEXP (note, 0), DF_REF_REG_USE,
				bb, insn, 0);
	      default:
		break;
1169 1170
	      }
	  }
1171

1172
      if (CALL_P (insn))
Jeff Law committed
1173 1174 1175
	{
	  rtx note;
	  rtx x;
1176

Jeff Law committed
1177 1178 1179 1180 1181
	  /* 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)
1182
		df_uses_record (df, &XEXP (XEXP (note, 0), 0), DF_REF_REG_USE,
1183
				bb, insn, 0);
Jeff Law committed
1184 1185 1186 1187
	    }

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

Jeff Law committed
1190 1191 1192 1193 1194 1195 1196 1197
	  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);
1198
		    df_uses_record (df, &XEXP (x, 0),
Kazu Hirata committed
1199
				    DF_REF_REG_USE, bb, insn, 0);
Jeff Law committed
1200 1201 1202
		  }
	    }
	}
1203

Jeff Law committed
1204
      /* Record the register uses.  */
1205
      df_uses_record (df, &PATTERN (insn),
1206
		      DF_REF_REG_USE, bb, insn, 0);
1207

1208
      if (CALL_P (insn))
Jeff Law committed
1209 1210 1211
	{
	  rtx note;

1212 1213 1214 1215 1216
	  /* We do not record hard registers clobbered by the call,
	     since there are awfully many of them and "defs" created
	     through them are not interesting (since no use can be legally
	     reached by them).  So we must just make sure we include them when
	     computing kill bitmaps.  */
1217

Jeff Law committed
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
	  /* 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
1231
df_bb_refs_record (struct df *df, basic_block bb)
Jeff Law committed
1232 1233 1234 1235
{
  rtx insn;

  /* Scan the block an insn at a time from beginning to end.  */
1236
  FOR_BB_INSNS (bb, insn)
Jeff Law committed
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
    {
      if (INSN_P (insn))
	{
	  /* Record defs within INSN.  */
	  df_insn_refs_record (df, bb, insn);
	}
    }
}


/* Record all the refs in the basic blocks specified by BLOCKS.  */
static void
1249
df_refs_record (struct df *df, bitmap blocks)
Jeff Law committed
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
{
  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.  */
1263

Jeff Law committed
1264
static void
1265
df_bb_reg_def_chain_create (struct df *df, basic_block bb)
Jeff Law committed
1266 1267
{
  rtx insn;
1268

Jeff Law committed
1269
  /* Perhaps the defs should be sorted using a depth first search
1270
     of the CFG (or possibly a breadth first search).  */
1271

1272
  FOR_BB_INSNS_REVERSE (bb, insn)
Jeff Law committed
1273 1274 1275 1276 1277 1278
    {
      struct df_link *link;
      unsigned int uid = INSN_UID (insn);

      if (! INSN_P (insn))
	continue;
1279

Jeff Law committed
1280 1281 1282 1283
      for (link = df->insns[uid].defs; link; link = link->next)
	{
	  struct ref *def = link->ref;
	  unsigned int dregno = DF_REF_REGNO (def);
1284 1285 1286 1287 1288

          /* Do not add ref's to the chain twice, 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.  */
1289 1290
          if (DF_REF_ID (def) < df->def_id_save)
            continue;
1291

1292
	  df->regs[dregno].defs = df_link_create (def, df->regs[dregno].defs);
Jeff Law committed
1293 1294 1295 1296 1297 1298
	}
    }
}


/* Create reg-def chains for each basic block within BLOCKS.  These
1299 1300 1301
   are a list of definitions for each register.  If REDO is true, add
   all defs, otherwise just add the new defs.  */

Jeff Law committed
1302
static void
1303
df_reg_def_chain_create (struct df *df, bitmap blocks, bool redo)
Jeff Law committed
1304 1305
{
  basic_block bb;
1306 1307 1308 1309
#ifdef ENABLE_CHECKING
  unsigned regno;
#endif
  unsigned old_def_id_save = df->def_id_save;
Jeff Law committed
1310

1311 1312 1313 1314
  if (redo)
    {
#ifdef ENABLE_CHECKING
      for (regno = 0; regno < df->n_regs; regno++)
1315
	gcc_assert (!df->regs[regno].defs);
1316 1317 1318 1319 1320 1321 1322
#endif

      /* Pretend that all defs are new.  */
      df->def_id_save = 0;
    }

  FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
Jeff Law committed
1323 1324 1325
    {
      df_bb_reg_def_chain_create (df, bb);
    });
1326 1327

  df->def_id_save = old_def_id_save;
Jeff Law committed
1328 1329
}

1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
/* Remove all reg-def chains stored in the dataflow object DF.  */

static void
df_reg_def_chain_clean (struct df *df)
{
  unsigned regno;

  for (regno = 0; regno < df->n_regs; regno++)
    free_reg_ref_chain (&df->regs[regno].defs);
}
Jeff Law committed
1340 1341 1342

/* Create reg-use chains for basic block BB.  These are a list of uses
   for each register.  */
1343

Jeff Law committed
1344
static void
1345
df_bb_reg_use_chain_create (struct df *df, basic_block bb)
Jeff Law committed
1346 1347
{
  rtx insn;
1348

1349 1350
  /* Scan in forward order so that the last uses appear at the start
     of the chain.  */
1351

1352
  FOR_BB_INSNS (bb, insn)
Jeff Law committed
1353 1354 1355 1356 1357 1358
    {
      struct df_link *link;
      unsigned int uid = INSN_UID (insn);

      if (! INSN_P (insn))
	continue;
1359

Jeff Law committed
1360 1361 1362 1363
      for (link = df->insns[uid].uses; link; link = link->next)
	{
	  struct ref *use = link->ref;
	  unsigned int uregno = DF_REF_REGNO (use);
1364 1365 1366 1367 1368

          /* Do not add ref's to the chain twice, 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.  */
1369 1370
          if (DF_REF_ID (use) < df->use_id_save)
            continue;
1371

Jeff Law committed
1372 1373 1374 1375 1376 1377 1378 1379
	  df->regs[uregno].uses
	    = df_link_create (use, df->regs[uregno].uses);
	}
    }
}


/* Create reg-use chains for each basic block within BLOCKS.  These
1380 1381 1382
   are a list of uses for each register.  If REDO is true, remove the
   old reg-use chains first, otherwise just add new uses to them.  */

Jeff Law committed
1383
static void
1384
df_reg_use_chain_create (struct df *df, bitmap blocks, bool redo)
Jeff Law committed
1385 1386
{
  basic_block bb;
1387 1388 1389 1390 1391 1392 1393 1394 1395
#ifdef ENABLE_CHECKING
  unsigned regno;
#endif
  unsigned old_use_id_save = df->use_id_save;

  if (redo)
    {
#ifdef ENABLE_CHECKING
      for (regno = 0; regno < df->n_regs; regno++)
1396
	gcc_assert (!df->regs[regno].uses);
1397 1398 1399 1400 1401
#endif

      /* Pretend that all uses are new.  */
      df->use_id_save = 0;
    }
Jeff Law committed
1402 1403 1404 1405 1406

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

  df->use_id_save = old_use_id_save;
Jeff Law committed
1409 1410
}

1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
/* Remove all reg-use chains stored in the dataflow object DF.  */

static void
df_reg_use_chain_clean (struct df *df)
{
  unsigned regno;

  for (regno = 0; regno < df->n_regs; regno++)
    free_reg_ref_chain (&df->regs[regno].uses);
}
Jeff Law committed
1421 1422 1423

/* Create def-use chains from reaching use bitmaps for basic block BB.  */
static void
1424
df_bb_du_chain_create (struct df *df, basic_block bb, bitmap ru)
Jeff Law committed
1425 1426 1427
{
  struct bb_info *bb_info = DF_BB_INFO (df, bb);
  rtx insn;
1428

Jeff Law committed
1429
  bitmap_copy (ru, bb_info->ru_out);
1430

Jeff Law committed
1431 1432
  /* For each def in BB create a linked list (chain) of uses
     reached from the def.  */
1433
  FOR_BB_INSNS_REVERSE (bb, insn)
Jeff Law committed
1434 1435 1436 1437 1438 1439 1440
    {
      struct df_link *def_link;
      struct df_link *use_link;
      unsigned int uid = INSN_UID (insn);

      if (! INSN_P (insn))
	continue;
1441

Jeff Law committed
1442 1443 1444 1445 1446
      /* 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);
1447

Jeff Law committed
1448 1449 1450 1451 1452
	  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.  */
1453
	  for (use_link = df->regs[dregno].uses; use_link;
Jeff Law committed
1454 1455 1456
	       use_link = use_link->next)
	    {
	      struct ref *use = use_link->ref;
1457

Jeff Law committed
1458 1459
	      if (bitmap_bit_p (ru, DF_REF_ID (use)))
		{
1460
		  DF_REF_CHAIN (def)
Jeff Law committed
1461
		    = df_link_create (use, DF_REF_CHAIN (def));
1462

Jeff Law committed
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480
		  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
1481
df_du_chain_create (struct df *df, bitmap blocks)
Jeff Law committed
1482 1483 1484 1485
{
  bitmap ru;
  basic_block bb;

1486
  ru = BITMAP_ALLOC (NULL);
Jeff Law committed
1487 1488 1489 1490 1491 1492

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

1493
  BITMAP_FREE (ru);
Jeff Law committed
1494 1495 1496 1497 1498
}


/* Create use-def chains from reaching def bitmaps for basic block BB.  */
static void
1499
df_bb_ud_chain_create (struct df *df, basic_block bb)
Jeff Law committed
1500 1501 1502 1503
{
  struct bb_info *bb_info = DF_BB_INFO (df, bb);
  struct ref **reg_def_last = df->reg_def_last;
  rtx insn;
1504

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

Jeff Law committed
1507 1508
  /* For each use in BB create a linked list (chain) of defs
     that reach the use.  */
1509
  FOR_BB_INSNS (bb, insn)
Jeff Law committed
1510 1511 1512 1513 1514 1515 1516 1517
    {
      unsigned int uid = INSN_UID (insn);
      struct df_link *use_link;
      struct df_link *def_link;

      if (! INSN_P (insn))
	continue;

1518
      /* For each use in insn...  */
Jeff Law committed
1519 1520 1521 1522
      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);
1523

Jeff Law committed
1524 1525 1526 1527 1528 1529 1530 1531 1532
	  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])
	    {
1533
	      DF_REF_CHAIN (use)
Jeff Law committed
1534 1535 1536 1537 1538 1539 1540 1541
		= 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.  */
1542
	      for (def_link = df->regs[regno].defs; def_link;
Jeff Law committed
1543 1544 1545
		   def_link = def_link->next)
		{
		  struct ref *def = def_link->ref;
1546

Jeff Law committed
1547 1548
		  if (bitmap_bit_p (bb_info->rd_in, DF_REF_ID (def)))
		    {
1549
		      DF_REF_CHAIN (use)
Jeff Law committed
1550 1551 1552 1553 1554
			= df_link_create (def, DF_REF_CHAIN (use));
		    }
		}
	    }
	}
1555

Jeff Law committed
1556

1557
      /* For each def in insn... record the last def of each reg.  */
Jeff Law committed
1558 1559 1560 1561
      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);
1562

Jeff Law committed
1563 1564 1565 1566 1567 1568 1569 1570 1571
	  reg_def_last[dregno] = def;
	}
    }
}


/* Create use-def chains from reaching def bitmaps for basic blocks
   within BLOCKS.  */
static void
1572
df_ud_chain_create (struct df *df, bitmap blocks)
Jeff Law committed
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
{
  basic_block bb;

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

1583

Jeff Law committed
1584
static void
1585 1586
df_rd_transfer_function (int bb ATTRIBUTE_UNUSED, int *changed, void *in,
			 void *out, void *gen, void *kill,
1587
			 void *data ATTRIBUTE_UNUSED)
Jeff Law committed
1588
{
1589
  *changed = bitmap_ior_and_compl (out, gen, in, kill);
Jeff Law committed
1590
}
1591 1592


Jeff Law committed
1593
static void
1594 1595
df_ru_transfer_function (int bb ATTRIBUTE_UNUSED, int *changed, void *in,
			 void *out, void *gen, void *kill,
1596
			 void *data ATTRIBUTE_UNUSED)
Jeff Law committed
1597
{
1598
  *changed = bitmap_ior_and_compl (in, gen, out, kill);
Jeff Law committed
1599 1600
}

1601

Jeff Law committed
1602
static void
1603 1604
df_lr_transfer_function (int bb ATTRIBUTE_UNUSED, int *changed, void *in,
			 void *out, void *use, void *def,
1605
			 void *data ATTRIBUTE_UNUSED)
Jeff Law committed
1606
{
1607
  *changed = bitmap_ior_and_compl (in, use, out, def);
Jeff Law committed
1608 1609 1610 1611 1612
}


/* Compute local reaching def info for basic block BB.  */
static void
1613
df_bb_rd_local_compute (struct df *df, basic_block bb, bitmap call_killed_defs)
Jeff Law committed
1614 1615 1616
{
  struct bb_info *bb_info = DF_BB_INFO (df, bb);
  rtx insn;
1617
  bitmap seen = BITMAP_ALLOC (NULL);
1618
  bool call_seen = false;
1619

1620
  FOR_BB_INSNS_REVERSE (bb, insn)
Jeff Law committed
1621 1622 1623 1624 1625 1626
    {
      unsigned int uid = INSN_UID (insn);
      struct df_link *def_link;

      if (! INSN_P (insn))
	continue;
1627

Jeff Law committed
1628 1629 1630 1631 1632 1633
      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;

1634 1635 1636 1637 1638 1639
	  if (bitmap_bit_p (seen, regno)
	      || (call_seen
		  && regno < FIRST_PSEUDO_REGISTER
		  && TEST_HARD_REG_BIT (regs_invalidated_by_call, regno)))
	    continue;

1640
	  for (def2_link = df->regs[regno].defs; def2_link;
Jeff Law committed
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652
	       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));
	    }

	  bitmap_set_bit (bb_info->rd_gen, DF_REF_ID (def));
1653 1654 1655
	  bitmap_set_bit (seen, regno);
	}

1656
      if (CALL_P (insn) && (df->flags & DF_HARD_REGS))
1657
	{
1658
	  bitmap_ior_into (bb_info->rd_kill, call_killed_defs);
1659
	  call_seen = 1;
Jeff Law committed
1660 1661
	}
    }
Kazu Hirata committed
1662

1663
  BITMAP_FREE (seen);
Jeff Law committed
1664 1665 1666 1667 1668
}


/* Compute local reaching def info for each basic block within BLOCKS.  */
static void
1669
df_rd_local_compute (struct df *df, bitmap blocks)
Jeff Law committed
1670 1671
{
  basic_block bb;
1672 1673 1674 1675 1676 1677
  bitmap killed_by_call = NULL;
  unsigned regno;
  struct df_link *def_link;

  if (df->flags & DF_HARD_REGS)
    {
1678
      killed_by_call = BITMAP_ALLOC (NULL);
1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689
      for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
	{
	  if (!TEST_HARD_REG_BIT (regs_invalidated_by_call, regno))
	    continue;
	  
	  for (def_link = df->regs[regno].defs;
	       def_link;
	       def_link = def_link->next)
	    bitmap_set_bit (killed_by_call, DF_REF_ID (def_link->ref));
	}
    }
Jeff Law committed
1690 1691 1692

  FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
  {
1693
    df_bb_rd_local_compute (df, bb, killed_by_call);
Jeff Law committed
1694
  });
1695 1696

  if (df->flags & DF_HARD_REGS)
1697
    BITMAP_FREE (killed_by_call);
Jeff Law committed
1698 1699 1700 1701 1702 1703
}


/* Compute local reaching use (upward exposed use) info for basic
   block BB.  */
static void
1704
df_bb_ru_local_compute (struct df *df, basic_block bb)
Jeff Law committed
1705 1706 1707 1708
{
  /* 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
1709

Jeff Law committed
1710 1711 1712
  struct bb_info *bb_info = DF_BB_INFO (df, bb);
  rtx insn;

1713

1714
  FOR_BB_INSNS_REVERSE (bb, insn)
Jeff Law committed
1715 1716 1717 1718 1719 1720 1721
    {
      unsigned int uid = INSN_UID (insn);
      struct df_link *def_link;
      struct df_link *use_link;

      if (! INSN_P (insn))
	continue;
1722

Jeff Law committed
1723 1724 1725 1726 1727
      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);

1728
	  for (use_link = df->regs[dregno].uses; use_link;
Jeff Law committed
1729 1730 1731 1732 1733 1734 1735 1736 1737
	       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));
1738

Jeff Law committed
1739 1740 1741 1742
	      /* Zap from the set of gens for this BB.  */
	      bitmap_clear_bit (bb_info->ru_gen, DF_REF_ID (use));
	    }
	}
1743

Jeff Law committed
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
      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));
	}
    }
}


/* Compute local reaching use (upward exposed use) info for each basic
   block within BLOCKS.  */
static void
1757
df_ru_local_compute (struct df *df, bitmap blocks)
Jeff Law committed
1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769
{
  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
1770
df_bb_lr_local_compute (struct df *df, basic_block bb)
Jeff Law committed
1771 1772 1773
{
  struct bb_info *bb_info = DF_BB_INFO (df, bb);
  rtx insn;
1774

1775
  FOR_BB_INSNS_REVERSE (bb, insn)
Jeff Law committed
1776 1777 1778 1779 1780 1781
    {
      unsigned int uid = INSN_UID (insn);
      struct df_link *link;

      if (! INSN_P (insn))
	continue;
1782

Jeff Law committed
1783 1784 1785 1786
      for (link = df->insns[uid].defs; link; link = link->next)
	{
	  struct ref *def = link->ref;
	  unsigned int dregno = DF_REF_REGNO (def);
1787

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

Jeff Law committed
1791 1792
	  bitmap_clear_bit (bb_info->lr_use, dregno);
	}
1793

Jeff Law committed
1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
      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));
	}
    }
}


/* Compute local live variable info for each basic block within BLOCKS.  */
static void
1806
df_lr_local_compute (struct df *df, bitmap blocks)
Jeff Law committed
1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819
{
  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
1820
df_bb_reg_info_compute (struct df *df, basic_block bb, bitmap live)
Jeff Law committed
1821 1822 1823 1824
{
  struct reg_info *reg_info = df->regs;
  struct bb_info *bb_info = DF_BB_INFO (df, bb);
  rtx insn;
1825

Jeff Law committed
1826
  bitmap_copy (live, bb_info->lr_out);
1827

1828
  FOR_BB_INSNS_REVERSE (bb, insn)
Jeff Law committed
1829 1830 1831 1832
    {
      unsigned int uid = INSN_UID (insn);
      unsigned int regno;
      struct df_link *link;
1833
      bitmap_iterator bi;
1834

Jeff Law committed
1835 1836
      if (! INSN_P (insn))
	continue;
1837

Jeff Law committed
1838 1839 1840 1841
      for (link = df->insns[uid].defs; link; link = link->next)
	{
	  struct ref *def = link->ref;
	  unsigned int dregno = DF_REF_REGNO (def);
1842

Jeff Law committed
1843 1844 1845 1846
	  /* Kill this register.  */
	  bitmap_clear_bit (live, dregno);
	  reg_info[dregno].n_defs++;
	}
1847

Jeff Law committed
1848 1849 1850 1851
      for (link = df->insns[uid].uses; link; link = link->next)
	{
	  struct ref *use = link->ref;
	  unsigned int uregno = DF_REF_REGNO (use);
1852

Jeff Law committed
1853 1854 1855 1856
	  /* This register is now live.  */
	  bitmap_set_bit (live, uregno);
	  reg_info[uregno].n_uses++;
	}
1857

Jeff Law committed
1858
      /* Increment lifetimes of all live registers.  */
1859 1860 1861 1862
      EXECUTE_IF_SET_IN_BITMAP (live, 0, regno, bi)
	{
	  reg_info[regno].lifetime++;
	}
Jeff Law committed
1863 1864 1865 1866 1867 1868
    }
}


/* Compute register info: lifetime, bb, and number of defs and uses.  */
static void
1869
df_reg_info_compute (struct df *df, bitmap blocks)
Jeff Law committed
1870 1871 1872 1873
{
  basic_block bb;
  bitmap live;

1874
  live = BITMAP_ALLOC (NULL);
Jeff Law committed
1875 1876 1877 1878 1879 1880

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

1881
  BITMAP_FREE (live);
Jeff Law committed
1882 1883 1884 1885 1886
}


/* Assign LUIDs for BB.  */
static int
1887
df_bb_luids_set (struct df *df, basic_block bb)
Jeff Law committed
1888 1889 1890 1891 1892 1893
{
  rtx insn;
  int luid = 0;

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

1894
  FOR_BB_INSNS (bb, insn)
Jeff Law committed
1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905
    {
      if (INSN_P (insn))
	DF_INSN_LUID (df, insn) = luid++;
      DF_INSN_LUID (df, insn) = luid;
    }
  return luid;
}


/* Assign LUIDs for each basic block within BLOCKS.  */
static int
1906
df_luids_set (struct df *df, bitmap blocks)
Jeff Law committed
1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917
{
  basic_block bb;
  int total = 0;

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

1918

Jeff Law committed
1919 1920 1921
/* Perform dataflow analysis using existing DF structure for blocks
   within BLOCKS.  If BLOCKS is zero, use all basic blocks in the CFG.  */
static void
1922
df_analyze_1 (struct df *df, bitmap blocks, int flags, int update)
Jeff Law committed
1923 1924 1925
{
  int aflags;
  int dflags;
1926
  basic_block bb;
1927
  struct dataflow dflow;
1928

Jeff Law committed
1929
  dflags = 0;
1930
  aflags = flags;
Jeff Law committed
1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
  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)
Kazu Hirata committed
1944
    blocks = df->all_blocks;
Jeff Law committed
1945 1946 1947 1948

  df->flags = flags;
  if (update)
    {
1949
      df_refs_update (df, NULL);
Jeff Law committed
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972
      /* 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.  */
1973
  df_bitmaps_alloc (df, NULL, aflags);
Jeff Law committed
1974 1975 1976 1977 1978 1979 1980 1981 1982 1983

  /* 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)
    {
1984
      df_reg_def_chain_create (df, blocks, false);
Jeff Law committed
1985 1986 1987 1988
    }

  if (aflags & DF_RU_CHAIN)
    {
1989
      df_reg_use_chain_create (df, blocks, false);
Jeff Law committed
1990 1991
    }

1992 1993 1994
  df->dfs_order = xmalloc (sizeof (int) * n_basic_blocks - NUM_FIXED_BLOCKS);
  df->rc_order = xmalloc (sizeof (int) * n_basic_blocks - NUM_FIXED_BLOCKS);
  df->rts_order = xmalloc (sizeof (int) * n_basic_blocks - NUM_FIXED_BLOCKS);
1995

1996 1997
  pre_and_rev_post_order_compute (df->dfs_order, df->rc_order, false);
  post_order_compute (df->rts_order, false);
Jeff Law committed
1998 1999 2000
  if (aflags & DF_RD)
    {
      /* Compute the sets of gens and kills for the defs of each bb.  */
2001 2002 2003 2004 2005
      dflow.in = xmalloc (sizeof (bitmap) * last_basic_block);
      dflow.out = xmalloc (sizeof (bitmap) * last_basic_block);
      dflow.gen = xmalloc (sizeof (bitmap) * last_basic_block);
      dflow.kill = xmalloc (sizeof (bitmap) * last_basic_block);

Jeff Law committed
2006
      df_rd_local_compute (df, df->flags & DF_RD ? blocks : df->all_blocks);
2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
      FOR_EACH_BB (bb)
	{
	  dflow.in[bb->index] = DF_BB_INFO (df, bb)->rd_in;
	  dflow.out[bb->index] = DF_BB_INFO (df, bb)->rd_out;
	  dflow.gen[bb->index] = DF_BB_INFO (df, bb)->rd_gen;
	  dflow.kill[bb->index] = DF_BB_INFO (df, bb)->rd_kill;
	}

      dflow.repr = SR_BITMAP;
      dflow.dir = DF_FORWARD;
      dflow.conf_op = DF_UNION;
      dflow.transfun = df_rd_transfer_function;
2019
      dflow.n_blocks = n_basic_blocks - NUM_FIXED_BLOCKS;
2020 2021 2022 2023 2024 2025 2026 2027
      dflow.order = df->rc_order;
      dflow.data = NULL;

      iterative_dataflow (&dflow);
      free (dflow.in);
      free (dflow.out);
      free (dflow.gen);
      free (dflow.kill);
Jeff Law committed
2028 2029 2030 2031 2032 2033 2034 2035 2036 2037
    }

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

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

Jeff Law committed
2039 2040 2041 2042
  if (aflags & DF_RU)
    {
      /* Compute the sets of gens and kills for the upwards exposed
	 uses in each bb.  */
2043 2044 2045 2046 2047
      dflow.in = xmalloc (sizeof (bitmap) * last_basic_block);
      dflow.out = xmalloc (sizeof (bitmap) * last_basic_block);
      dflow.gen = xmalloc (sizeof (bitmap) * last_basic_block);
      dflow.kill = xmalloc (sizeof (bitmap) * last_basic_block);

Jeff Law committed
2048
      df_ru_local_compute (df, df->flags & DF_RU ? blocks : df->all_blocks);
2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061

      FOR_EACH_BB (bb)
	{
	  dflow.in[bb->index] = DF_BB_INFO (df, bb)->ru_in;
	  dflow.out[bb->index] = DF_BB_INFO (df, bb)->ru_out;
	  dflow.gen[bb->index] = DF_BB_INFO (df, bb)->ru_gen;
	  dflow.kill[bb->index] = DF_BB_INFO (df, bb)->ru_kill;
	}

      dflow.repr = SR_BITMAP;
      dflow.dir = DF_BACKWARD;
      dflow.conf_op = DF_UNION;
      dflow.transfun = df_ru_transfer_function;
2062
      dflow.n_blocks = n_basic_blocks - NUM_FIXED_BLOCKS;
2063 2064 2065 2066 2067 2068 2069 2070
      dflow.order = df->rts_order;
      dflow.data = NULL;

      iterative_dataflow (&dflow);
      free (dflow.in);
      free (dflow.out);
      free (dflow.gen);
      free (dflow.kill);
Jeff Law committed
2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083
    }

  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)
Kazu Hirata committed
2084
    df_bitmaps_free (df, dflags);
Jeff Law committed
2085 2086 2087 2088

  if (aflags & DF_LR)
    {
      /* Compute the sets of defs and uses of live variables.  */
2089 2090 2091 2092 2093
      dflow.in = xmalloc (sizeof (bitmap) * last_basic_block);
      dflow.out = xmalloc (sizeof (bitmap) * last_basic_block);
      dflow.gen = xmalloc (sizeof (bitmap) * last_basic_block);
      dflow.kill = xmalloc (sizeof (bitmap) * last_basic_block);

Kazu Hirata committed
2094
      df_lr_local_compute (df, df->flags & DF_LR ? blocks : df->all_blocks);
2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107

      FOR_EACH_BB (bb)
	{
	  dflow.in[bb->index] = DF_BB_INFO (df, bb)->lr_in;
	  dflow.out[bb->index] = DF_BB_INFO (df, bb)->lr_out;
	  dflow.gen[bb->index] = DF_BB_INFO (df, bb)->lr_use;
	  dflow.kill[bb->index] = DF_BB_INFO (df, bb)->lr_def;
	}

      dflow.repr = SR_BITMAP;
      dflow.dir = DF_BACKWARD;
      dflow.conf_op = DF_UNION;
      dflow.transfun = df_lr_transfer_function;
2108
      dflow.n_blocks = n_basic_blocks - NUM_FIXED_BLOCKS;
2109 2110 2111 2112 2113 2114 2115 2116
      dflow.order = df->rts_order;
      dflow.data = NULL;

      iterative_dataflow (&dflow);
      free (dflow.in);
      free (dflow.out);
      free (dflow.gen);
      free (dflow.kill);
Jeff Law committed
2117 2118 2119 2120 2121
    }

  if (aflags & DF_REG_INFO)
    {
      df_reg_info_compute (df, df->all_blocks);
2122
    }
2123

Jeff Law committed
2124 2125
  free (df->dfs_order);
  free (df->rc_order);
2126
  free (df->rts_order);
Jeff Law committed
2127 2128 2129
}


2130
/* Initialize dataflow analysis.  */
Jeff Law committed
2131
struct df *
2132
df_init (void)
Jeff Law committed
2133 2134 2135 2136 2137 2138 2139
{
  struct df *df;

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

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

Jeff Law committed
2141 2142 2143 2144 2145 2146
  return df;
}


/* Start queuing refs.  */
static int
2147
df_refs_queue (struct df *df)
Jeff Law committed
2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158
{
  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
2159
df_refs_process (struct df *df)
Jeff Law committed
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188
{
  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.  */
2189
static int
2190
df_bb_refs_update (struct df *df, basic_block bb)
Jeff Law committed
2191 2192 2193 2194
{
  rtx insn;
  int count = 0;

2195
  /* While we have to scan the chain of insns for this BB, we do not
Jeff Law committed
2196 2197 2198 2199
     need to allocate and queue a long chain of BB/INSN pairs.  Using
     a bitmap for insns_modified saves memory and avoids queuing
     duplicates.  */

2200
  FOR_BB_INSNS (bb, insn)
Jeff Law committed
2201 2202 2203 2204 2205 2206 2207 2208 2209
    {
      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);
2210

Jeff Law committed
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222
	  /* Scan the insn for refs.  */
	  df_insn_refs_record (df, bb, insn);

	  count++;
	}
    }
  return count;
}


/* Process all the modified/deleted insns that were queued.  */
static int
2223
df_refs_update (struct df *df, bitmap blocks)
Jeff Law committed
2224 2225
{
  basic_block bb;
2226
  unsigned count = 0, bbno;
Jeff Law committed
2227

2228 2229
  df->n_regs = max_reg_num ();
  if (df->n_regs >= df->reg_size)
Jeff Law committed
2230 2231 2232 2233
    df_reg_table_realloc (df, 0);

  df_refs_queue (df);

2234
  if (!blocks)
Jeff Law committed
2235
    {
2236 2237 2238 2239 2240 2241 2242
      FOR_EACH_BB_IN_BITMAP (df->bbs_modified, 0, bb,
	{
	  count += df_bb_refs_update (df, bb);
	});
    }
  else
    {
2243 2244 2245
      bitmap_iterator bi;

      EXECUTE_IF_AND_IN_BITMAP (df->bbs_modified, blocks, 0, bbno, bi)
2246 2247
	{
	  count += df_bb_refs_update (df, BASIC_BLOCK (bbno));
2248
	}
2249
    }
Jeff Law committed
2250 2251 2252 2253 2254 2255

  df_refs_process (df);
  return count;
}


2256
/* Return nonzero if any of the requested blocks in the bitmap
Jeff Law committed
2257 2258
   BLOCKS have been modified.  */
static int
2259
df_modified_p (struct df *df, bitmap blocks)
Jeff Law committed
2260 2261
{
  int update = 0;
2262 2263 2264 2265
  basic_block bb;

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

2267 2268 2269
  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
2270 2271 2272 2273 2274 2275 2276 2277
    {
      update = 1;
      break;
    }

  return update;
}

2278
/* Analyze dataflow info for the basic blocks specified by the bitmap
Jeff Law committed
2279 2280
   BLOCKS, or for the whole CFG if BLOCKS is zero, or just for the
   modified blocks if BLOCKS is -1.  */
2281

Jeff Law committed
2282
int
2283
df_analyze (struct df *df, bitmap blocks, int flags)
Jeff Law committed
2284 2285 2286 2287 2288
{
  int update;

  /* We could deal with additional basic blocks being created by
     rescanning everything again.  */
2289
  gcc_assert (!df->n_bbs || df->n_bbs == (unsigned int) last_basic_block);
Jeff Law committed
2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300

  update = df_modified_p (df, blocks);
  if (update || (flags != df->flags))
    {
      if (! blocks)
	{
	  if (df->n_bbs)
	    {
	      /* Recompute everything from scratch.  */
	      df_free (df);
	    }
2301
	  /* Allocate and initialize data structures.  */
Jeff Law committed
2302
	  df_alloc (df, max_reg_num ());
2303
	  df_analyze_1 (df, 0, flags, 0);
Jeff Law committed
2304 2305 2306 2307 2308 2309 2310
	  update = 1;
	}
      else
	{
	  if (blocks == (bitmap) -1)
	    blocks = df->bbs_modified;

2311
	  gcc_assert (df->n_bbs);
Jeff Law committed
2312

2313
	  df_analyze_1 (df, blocks, flags, 1);
Jeff Law committed
2314
	  bitmap_zero (df->bbs_modified);
2315
	  bitmap_zero (df->insns_modified);
Jeff Law committed
2316 2317 2318 2319 2320
	}
    }
  return update;
}

2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336
/* Remove the entries not in BLOCKS from the LIST of length LEN, preserving
   the order of the remaining entries.  Returns the length of the resulting
   list.  */

static unsigned
prune_to_subcfg (int list[], unsigned len, bitmap blocks)
{
  unsigned act, last;

  for (act = 0, last = 0; act < len; act++)
    if (bitmap_bit_p (blocks, list[act]))
      list[last++] = list[act];

  return last;
}

2337
/* Alternative entry point to the analysis.  Analyze just the part of the cfg
2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 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 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419
   graph induced by BLOCKS.
   
   TODO I am not quite sure how to avoid code duplication with df_analyze_1
   here, and simultaneously not make even greater chaos in it.  We behave
   slightly differently in some details, especially in handling modified
   insns.  */

void
df_analyze_subcfg (struct df *df, bitmap blocks, int flags)
{
  rtx insn;
  basic_block bb;
  struct dataflow dflow;
  unsigned n_blocks;

  if (flags & DF_UD_CHAIN)
    flags |= DF_RD | DF_RD_CHAIN;
  if (flags & DF_DU_CHAIN)
    flags |= DF_RU;
  if (flags & DF_RU)
    flags |= DF_RU_CHAIN;
  if (flags & DF_REG_INFO)
    flags |= DF_LR;

  if (!df->n_bbs)
    {
      df_alloc (df, max_reg_num ());

      /* Mark all insns as modified.  */

      FOR_EACH_BB (bb)
	{
	  FOR_BB_INSNS (bb, insn)
	    {
	      df_insn_modify (df, bb, insn);
	    }
	}
    }
  
  df->flags = flags;

  df_reg_def_chain_clean (df);
  df_reg_use_chain_clean (df);

  df_refs_update (df, blocks);

  /* Clear the updated stuff from ``modified'' bitmaps.  */
  FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
    {
      if (bitmap_bit_p (df->bbs_modified, bb->index))
	{
	  FOR_BB_INSNS (bb, insn)
	    {
	      bitmap_clear_bit (df->insns_modified, INSN_UID (insn));
	    }

	  bitmap_clear_bit (df->bbs_modified, bb->index);
	}
    });

  /* 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, blocks, flags);

  /* 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 (flags & DF_RD_CHAIN)
    {
      df_reg_def_chain_create (df, blocks, true);
    }

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

2420 2421 2422
  df->dfs_order = xmalloc (sizeof (int) * n_basic_blocks - NUM_FIXED_BLOCKS);
  df->rc_order = xmalloc (sizeof (int) * n_basic_blocks - NUM_FIXED_BLOCKS);
  df->rts_order = xmalloc (sizeof (int) * n_basic_blocks - NUM_FIXED_BLOCKS);
2423

2424 2425
  pre_and_rev_post_order_compute (df->dfs_order, df->rc_order, false);
  post_order_compute (df->rts_order, false);
2426

2427 2428 2429
  n_blocks = prune_to_subcfg (df->dfs_order, n_basic_blocks - NUM_FIXED_BLOCKS, blocks);
  prune_to_subcfg (df->rc_order, n_basic_blocks - NUM_FIXED_BLOCKS, blocks);
  prune_to_subcfg (df->rts_order, n_basic_blocks - NUM_FIXED_BLOCKS, blocks);
2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 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

  dflow.in = xmalloc (sizeof (bitmap) * last_basic_block);
  dflow.out = xmalloc (sizeof (bitmap) * last_basic_block);
  dflow.gen = xmalloc (sizeof (bitmap) * last_basic_block);
  dflow.kill = xmalloc (sizeof (bitmap) * last_basic_block);

  if (flags & DF_RD)
    {
      /* Compute the sets of gens and kills for the defs of each bb.  */
      df_rd_local_compute (df, blocks);

      FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
	{
	  dflow.in[bb->index] = DF_BB_INFO (df, bb)->rd_in;
	  dflow.out[bb->index] = DF_BB_INFO (df, bb)->rd_out;
	  dflow.gen[bb->index] = DF_BB_INFO (df, bb)->rd_gen;
	  dflow.kill[bb->index] = DF_BB_INFO (df, bb)->rd_kill;
	});

      dflow.repr = SR_BITMAP;
      dflow.dir = DF_FORWARD;
      dflow.conf_op = DF_UNION;
      dflow.transfun = df_rd_transfer_function;
      dflow.n_blocks = n_blocks;
      dflow.order = df->rc_order;
      dflow.data = NULL;

      iterative_dataflow (&dflow);
    }

  if (flags & DF_UD_CHAIN)
    {
      /* Create use-def chains.  */
      df_ud_chain_create (df, blocks);
    }

  if (flags & DF_RU)
    {
      /* Compute the sets of gens and kills for the upwards exposed
	 uses in each bb.  */
      df_ru_local_compute (df, blocks);

      FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
	{
	  dflow.in[bb->index] = DF_BB_INFO (df, bb)->ru_in;
	  dflow.out[bb->index] = DF_BB_INFO (df, bb)->ru_out;
	  dflow.gen[bb->index] = DF_BB_INFO (df, bb)->ru_gen;
	  dflow.kill[bb->index] = DF_BB_INFO (df, bb)->ru_kill;
	});

      dflow.repr = SR_BITMAP;
      dflow.dir = DF_BACKWARD;
      dflow.conf_op = DF_UNION;
      dflow.transfun = df_ru_transfer_function;
      dflow.n_blocks = n_blocks;
      dflow.order = df->rts_order;
      dflow.data = NULL;

      iterative_dataflow (&dflow);
    }

  if (flags & DF_DU_CHAIN)
    {
      /* Create def-use chains.  */
      df_du_chain_create (df, blocks);
    }

  if (flags & DF_LR)
    {
      /* Compute the sets of defs and uses of live variables.  */
      df_lr_local_compute (df, blocks);

      FOR_EACH_BB (bb)
	{
	  dflow.in[bb->index] = DF_BB_INFO (df, bb)->lr_in;
	  dflow.out[bb->index] = DF_BB_INFO (df, bb)->lr_out;
	  dflow.gen[bb->index] = DF_BB_INFO (df, bb)->lr_use;
	  dflow.kill[bb->index] = DF_BB_INFO (df, bb)->lr_def;
	}

      dflow.repr = SR_BITMAP;
      dflow.dir = DF_BACKWARD;
      dflow.conf_op = DF_UNION;
      dflow.transfun = df_lr_transfer_function;
      dflow.n_blocks = n_blocks;
      dflow.order = df->rts_order;
      dflow.data = NULL;

      iterative_dataflow (&dflow);
    }

  if (flags & DF_REG_INFO)
    {
      df_reg_info_compute (df, blocks);
    }

  free (dflow.in);
  free (dflow.out);
  free (dflow.gen);
  free (dflow.kill);

  free (df->dfs_order);
  free (df->rc_order);
  free (df->rts_order);
}
Jeff Law committed
2535 2536 2537

/* Free all the dataflow info and the DF structure.  */
void
2538
df_finish (struct df *df)
Jeff Law committed
2539 2540 2541 2542 2543 2544 2545
{
  df_free (df);
  free (df);
}

/* Unlink INSN from its reference information.  */
static void
2546
df_insn_refs_unlink (struct df *df, basic_block bb ATTRIBUTE_UNUSED, rtx insn)
Jeff Law committed
2547 2548 2549
{
  struct df_link *link;
  unsigned int uid;
2550

Jeff Law committed
2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565
  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;
}


2566
#if 0
Jeff Law committed
2567 2568
/* Unlink all the insns within BB from their reference information.  */
static void
2569
df_bb_refs_unlink (struct df *df, basic_block bb)
Jeff Law committed
2570 2571 2572 2573
{
  rtx insn;

  /* Scan the block an insn at a time from beginning to end.  */
2574
  for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
Jeff Law committed
2575 2576 2577 2578 2579 2580
    {
      if (INSN_P (insn))
	{
	  /* Unlink refs for INSN.  */
	  df_insn_refs_unlink (df, bb, insn);
	}
2581
      if (insn == BB_END (bb))
Jeff Law committed
2582 2583 2584 2585 2586 2587 2588 2589
	break;
    }
}


/* Unlink all the refs in the basic blocks specified by BLOCKS.
   Not currently used.  */
static void
2590
df_refs_unlink (struct df *df, bitmap blocks)
Jeff Law committed
2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602
{
  basic_block bb;

  if (blocks)
    {
      FOR_EACH_BB_IN_BITMAP (blocks, 0, bb,
      {
	df_bb_refs_unlink (df, bb);
      });
    }
  else
    {
2603
      FOR_EACH_BB (bb)
Jeff Law committed
2604 2605 2606 2607 2608 2609 2610 2611 2612 2613
	df_bb_refs_unlink (df, bb);
    }
}
#endif

/* Functions to modify insns.  */


/* Delete INSN and all its reference information.  */
rtx
2614
df_insn_delete (struct df *df, basic_block bb ATTRIBUTE_UNUSED, rtx insn)
Jeff Law committed
2615 2616 2617 2618 2619
{
  /* 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.  */
2620
  gcc_assert (insn != BB_HEAD (bb));
Jeff Law committed
2621 2622

  /* Delete the insn.  */
2623
  delete_insn (insn);
Jeff Law committed
2624 2625 2626 2627 2628 2629

  df_insn_modify (df, bb, insn);

  return NEXT_INSN (insn);
}

2630 2631 2632 2633 2634 2635
/* Mark that basic block BB was modified.  */

static void
df_bb_modify (struct df *df, basic_block bb)
{
  if ((unsigned) bb->index >= df->n_bbs)
2636
    df_bb_table_realloc (df, bb->index);
2637 2638 2639

  bitmap_set_bit (df->bbs_modified, bb->index);
}
Jeff Law committed
2640 2641 2642 2643 2644 2645

/* 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
2646
df_insn_modify (struct df *df, basic_block bb, rtx insn)
Jeff Law committed
2647 2648 2649 2650
{
  unsigned int uid;

  uid = INSN_UID (insn);
2651
  if (uid >= df->insn_size)
2652
    df_insn_table_realloc (df, uid);
2653

2654
  df_bb_modify (df, bb);
Jeff Law committed
2655 2656 2657 2658 2659 2660 2661 2662 2663
  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.  */
}

2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677
/* Check if INSN was marked as changed.  Of course the correctness of
   the information depends on whether the instruction was really modified
   at the time df_insn_modify was called.  */
bool
df_insn_modified_p (struct df *df, rtx insn)
{
  unsigned int uid;

  uid = INSN_UID (insn);
  return (df->insns_modified
	  && uid < df->insn_size
          && bitmap_bit_p (df->insns_modified, uid));
}

2678 2679
typedef struct replace_args
{
Jeff Law committed
2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691
  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
2692
df_rtx_mem_replace (rtx *px, void *data)
Jeff Law committed
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
{
  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
2728
df_insn_mem_replace (struct df *df, basic_block bb, rtx insn, rtx mem, rtx reg)
Jeff Law committed
2729 2730 2731 2732 2733 2734 2735 2736
{
  replace_args args;

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

2737
  /* Search and replace all matching mems within insn.  */
Jeff Law committed
2738 2739 2740 2741 2742 2743 2744 2745 2746
  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
2747
     when df_analyze is called.  */
Jeff Law committed
2748 2749 2750 2751 2752 2753 2754 2755
  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
2756
df_rtx_reg_replace (rtx *px, void *data)
Jeff Law committed
2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777
{
  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
2778
df_refs_reg_replace (struct df *df, bitmap blocks, struct df_link *chain, rtx oldreg, rtx newreg)
Jeff Law committed
2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797
{
  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;

2798 2799 2800
      gcc_assert (bitmap_bit_p (blocks, DF_REF_BBNO (ref)));
      
      df_ref_reg_replace (df, ref, oldreg, newreg);
Jeff Law committed
2801

2802 2803 2804 2805
      /* 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))
Jeff Law committed
2806
	{
2807 2808
	  args.insn = insn;
	  for_each_rtx (&REG_NOTES (insn), df_rtx_reg_replace, &args);
Jeff Law committed
2809 2810 2811 2812 2813 2814 2815 2816 2817 2818
	}
    }
}


/* 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
2819
df_reg_replace (struct df *df, bitmap blocks, rtx oldreg, rtx newreg)
Jeff Law committed
2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831
{
  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
2832
df_ref_reg_replace (struct df *df, struct ref *ref, rtx oldreg, rtx newreg)
Jeff Law committed
2833 2834 2835 2836 2837 2838
{
  /* 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;

2839
  gcc_assert (!oldreg || oldreg == DF_REF_REG (ref));
Jeff Law committed
2840 2841 2842 2843 2844 2845 2846 2847 2848 2849

  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*
2850
df_bb_def_use_swap (struct df *df, basic_block bb, rtx def_insn, rtx use_insn, unsigned int regno)
Jeff Law committed
2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890
{
  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;
}


2891
/* Record df between FIRST_INSN and LAST_INSN inclusive.  All new
Jeff Law committed
2892 2893
   insns must be processed by this routine.  */
static void
2894
df_insns_modify (struct df *df, basic_block bb, rtx first_insn, rtx last_insn)
Jeff Law committed
2895 2896 2897 2898 2899 2900 2901 2902 2903 2904
{
  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.  */
2905 2906
      gcc_assert ((!CALL_P (insn) || CONST_OR_PURE_CALL_P (insn))
		  && !LABEL_P (insn));
Jeff Law committed
2907 2908 2909 2910

      uid = INSN_UID (insn);

      if (uid >= df->insn_size)
2911
	df_insn_table_realloc (df, uid);
Jeff Law committed
2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922

      df_insn_modify (df, bb, insn);

      if (insn == last_insn)
	break;
    }
}


/* Emit PATTERN before INSN within BB.  */
rtx
2923
df_pattern_emit_before (struct df *df, rtx pattern, basic_block bb, rtx insn)
Jeff Law committed
2924 2925 2926 2927 2928
{
  rtx ret_insn;
  rtx prev_insn = PREV_INSN (insn);

  /* We should not be inserting before the start of the block.  */
2929
  gcc_assert (insn != BB_HEAD (bb));
Jeff Law committed
2930 2931 2932
  ret_insn = emit_insn_before (pattern, insn);
  if (ret_insn == insn)
    return ret_insn;
2933

Jeff Law committed
2934 2935 2936 2937 2938 2939 2940
  df_insns_modify (df, bb, NEXT_INSN (prev_insn), ret_insn);
  return ret_insn;
}


/* Emit PATTERN after INSN within BB.  */
rtx
2941
df_pattern_emit_after (struct df *df, rtx pattern, basic_block bb, rtx insn)
Jeff Law committed
2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955
{
  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
2956
df_jump_pattern_emit_after (struct df *df, rtx pattern, basic_block bb, rtx insn)
Jeff Law committed
2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974
{
  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
2975
df_insn_move_before (struct df *df, basic_block bb, rtx insn, basic_block before_bb, rtx before_insn)
Jeff Law committed
2976 2977 2978 2979 2980 2981 2982 2983 2984 2985
{
  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.  */
2986
  for (link = df->insns[uid].defs; link; link = link->next)
Jeff Law committed
2987
    DF_REF_BB (link->ref) = before_bb;
2988
  for (link = df->insns[uid].uses; link; link = link->next)
Jeff Law committed
2989 2990 2991 2992 2993 2994 2995
    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
2996
     which df_analyze removes when it recalculates data flow.  */
Jeff Law committed
2997

2998
  return emit_insn_before (insn, before_insn);
Jeff Law committed
2999 3000 3001 3002 3003 3004
}

/* Functions to query dataflow information.  */


int
3005 3006
df_insn_regno_def_p (struct df *df, basic_block bb ATTRIBUTE_UNUSED,
		     rtx insn, unsigned int regno)
Jeff Law committed
3007 3008 3009 3010 3011 3012
{
  unsigned int uid;
  struct df_link *link;

  uid = INSN_UID (insn);

3013
  for (link = df->insns[uid].defs; link; link = link->next)
Jeff Law committed
3014 3015
    {
      struct ref *def = link->ref;
3016

Jeff Law committed
3017 3018 3019 3020 3021 3022 3023
      if (DF_REF_REGNO (def) == regno)
	return 1;
    }

  return 0;
}

Ayal Zaks committed
3024 3025 3026 3027 3028 3029 3030 3031
/* Finds the reference corresponding to the definition of REG in INSN.
   DF is the dataflow object.  */

struct ref *
df_find_def (struct df *df, rtx insn, rtx reg)
{
  struct df_link *defs;

3032 3033 3034 3035
  if (GET_CODE (reg) == SUBREG)
    reg = SUBREG_REG (reg);
  gcc_assert (REG_P (reg));

Ayal Zaks committed
3036
  for (defs = DF_INSN_DEFS (df, insn); defs; defs = defs->next)
3037
    if (rtx_equal_p (DF_REF_REAL_REG (defs->ref), reg))
Ayal Zaks committed
3038 3039 3040 3041 3042
      return defs->ref;

  return NULL;
}

3043 3044
/* Finds the reference corresponding to the use of REG in INSN.
   DF is the dataflow object.  */
Ayal Zaks committed
3045

3046 3047
struct ref *
df_find_use (struct df *df, rtx insn, rtx reg)
Ayal Zaks committed
3048 3049 3050
{
  struct df_link *uses;

3051 3052 3053 3054
  if (GET_CODE (reg) == SUBREG)
    reg = SUBREG_REG (reg);
  gcc_assert (REG_P (reg));

Ayal Zaks committed
3055
  for (uses = DF_INSN_USES (df, insn); uses; uses = uses->next)
3056 3057
    if (rtx_equal_p (DF_REF_REAL_REG (uses->ref), reg))
      return uses->ref;
Ayal Zaks committed
3058

3059 3060 3061 3062 3063 3064 3065 3066 3067
  return NULL;
}

/* Return 1 if REG is referenced in INSN, zero otherwise.  */ 

int
df_reg_used (struct df *df, rtx insn, rtx reg)
{
  return df_find_use (df, insn, reg) != NULL;
Ayal Zaks committed
3068
}
Jeff Law committed
3069 3070

static int
3071
df_def_dominates_all_uses_p (struct df *df ATTRIBUTE_UNUSED, struct ref *def)
Jeff Law committed
3072 3073 3074 3075 3076 3077 3078 3079
{
  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;
3080

Jeff Law committed
3081 3082 3083 3084 3085 3086 3087 3088 3089 3090
      /* 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
3091 3092
df_insn_dominates_all_uses_p (struct df *df, basic_block bb ATTRIBUTE_UNUSED,
			      rtx insn)
Jeff Law committed
3093 3094 3095 3096 3097 3098
{
  unsigned int uid;
  struct df_link *link;

  uid = INSN_UID (insn);

3099
  for (link = df->insns[uid].defs; link; link = link->next)
Jeff Law committed
3100 3101
    {
      struct ref *def = link->ref;
3102

Jeff Law committed
3103 3104 3105 3106 3107 3108 3109 3110
      if (! df_def_dominates_all_uses_p (df, def))
	return 0;
    }

  return 1;
}


3111
/* Return nonzero if all DF dominates all the uses within the bitmap
Jeff Law committed
3112 3113
   BLOCKS.  */
static int
3114 3115
df_def_dominates_uses_p (struct df *df ATTRIBUTE_UNUSED, struct ref *def,
			 bitmap blocks)
Jeff Law committed
3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139
{
  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;
}


3140
/* Return nonzero if all the defs of INSN within BB dominates
Jeff Law committed
3141 3142
   all the corresponding uses.  */
int
3143 3144
df_insn_dominates_uses_p (struct df *df, basic_block bb ATTRIBUTE_UNUSED,
			  rtx insn, bitmap blocks)
Jeff Law committed
3145 3146 3147 3148 3149 3150
{
  unsigned int uid;
  struct df_link *link;

  uid = INSN_UID (insn);

3151
  for (link = df->insns[uid].defs; link; link = link->next)
Jeff Law committed
3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166
    {
      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
3167
df_regno_bb (struct df *df, unsigned int regno)
Jeff Law committed
3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181
{
  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;
}


3182
/* Return nonzero if REG used in multiple basic blocks.  */
Jeff Law committed
3183
int
3184
df_reg_global_p (struct df *df, rtx reg)
Jeff Law committed
3185 3186 3187 3188 3189 3190 3191
{
  return df_regno_bb (df, REGNO (reg)) != 0;
}


/* Return total lifetime (in insns) of REG.  */
int
3192
df_reg_lifetime (struct df *df, rtx reg)
Jeff Law committed
3193 3194 3195 3196 3197
{
  return df->regs[REGNO (reg)].lifetime;
}


3198
/* Return nonzero if REG live at start of BB.  */
Jeff Law committed
3199
int
3200
df_bb_reg_live_start_p (struct df *df, basic_block bb, rtx reg)
Jeff Law committed
3201 3202 3203
{
  struct bb_info *bb_info = DF_BB_INFO (df, bb);

3204
  gcc_assert (bb_info->lr_in);
3205

Jeff Law committed
3206 3207 3208 3209
  return bitmap_bit_p (bb_info->lr_in, REGNO (reg));
}


3210
/* Return nonzero if REG live at end of BB.  */
Jeff Law committed
3211
int
3212
df_bb_reg_live_end_p (struct df *df, basic_block bb, rtx reg)
Jeff Law committed
3213 3214
{
  struct bb_info *bb_info = DF_BB_INFO (df, bb);
3215

3216
  gcc_assert (bb_info->lr_in);
Jeff Law committed
3217 3218 3219 3220 3221 3222 3223 3224

  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
3225
df_bb_regs_lives_compare (struct df *df, basic_block bb, rtx reg1, rtx reg2)
Jeff Law committed
3226 3227 3228 3229 3230 3231 3232 3233
{
  unsigned int regno1 = REGNO (reg1);
  unsigned int regno2 = REGNO (reg2);
  struct ref *def1;
  struct ref *use1;
  struct ref *def2;
  struct ref *use2;

3234

Jeff Law committed
3235
  /* The regs must be local to BB.  */
3236 3237
  gcc_assert (df_regno_bb (df, regno1) == bb
	      && df_regno_bb (df, regno2) == bb);
Jeff Law committed
3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256

  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;
}


3257 3258 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 3290 3291 3292 3293 3294 3295 3296 3297 3298
/* Return true if the definition DEF, which is in the same basic
   block as USE, is available at USE.  So DEF may as well be
   dead, in which case using it will extend its live range.  */
bool
df_local_def_available_p (struct df *df, struct ref *def, struct ref *use)
{
  struct df_link *link;
  int def_luid = DF_INSN_LUID (df, DF_REF_INSN (def));
  int in_bb = 0;
  unsigned int regno = REGNO (def->reg);
  basic_block bb;

  /* The regs must be local to BB.  */
  gcc_assert (DF_REF_BB (def) == DF_REF_BB (use));
  bb = DF_REF_BB (def);

  /* 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.  */
  for (link = df->regs[regno].defs; link; link = link->next)
    {
      struct ref *this_def = link->ref;
      if (DF_REF_BB (this_def) == bb)
	{
	  int this_luid = DF_INSN_LUID (df, DF_REF_INSN (this_def));
	  /* Do nothing with defs coming before DEF.  */
	  if (this_luid > def_luid)
	    return this_luid > DF_INSN_LUID (df, DF_REF_INSN (use));

	  in_bb = 1;
        }
      else if (in_bb)
	/* DEF was the last in its basic block.  */
        return 1;
    }

  /* DEF was the last in the function.  */
  return 1;
}


Jeff Law committed
3299
/* Return last use of REGNO within BB.  */
Ayal Zaks committed
3300
struct ref *
3301
df_bb_regno_last_use_find (struct df *df, basic_block bb, unsigned int regno)
Jeff Law committed
3302 3303 3304 3305 3306 3307 3308
{
  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.  */
3309
  for (link = df->regs[regno].uses; link; link = link->next)
Jeff Law committed
3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320
    {
      struct ref *use = link->ref;

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


/* Return first def of REGNO within BB.  */
Ayal Zaks committed
3321
struct ref *
3322
df_bb_regno_first_def_find (struct df *df, basic_block bb, unsigned int regno)
Jeff Law committed
3323 3324 3325 3326 3327 3328 3329
{
  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.  */
3330
  for (link = df->regs[regno].defs; link; link = link->next)
Jeff Law committed
3331 3332 3333 3334 3335 3336 3337 3338 3339
    {
      struct ref *def = link->ref;

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

Ayal Zaks committed
3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364
/* Return last def of REGNO within BB.  */
struct ref *
df_bb_regno_last_def_find (struct df *df, basic_block bb, unsigned int regno)
{
  struct df_link *link;
  struct ref *last_def = NULL;
  int in_bb = 0;

  /* 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.  */
  for (link = df->regs[regno].defs; link; link = link->next)
    {
      struct ref *def = link->ref;
      /* The first time in the desired block.  */ 
      if (DF_REF_BB (def) == bb)
	  in_bb = 1;
      /* The last def in the desired block.  */
      else if (in_bb)
        return last_def;
      last_def = def;
    }
  return last_def;
}
Jeff Law committed
3365

3366
/* Return last use of REGNO inside INSN within BB.  */
Jeff Law committed
3367
static struct ref *
3368 3369 3370
df_bb_insn_regno_last_use_find (struct df *df,
				basic_block bb ATTRIBUTE_UNUSED, rtx insn,
				unsigned int regno)
Jeff Law committed
3371 3372 3373 3374 3375 3376
{
  unsigned int uid;
  struct df_link *link;

  uid = INSN_UID (insn);

3377
  for (link = df->insns[uid].uses; link; link = link->next)
Jeff Law committed
3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390
    {
      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 *
3391 3392 3393
df_bb_insn_regno_first_def_find (struct df *df,
				 basic_block bb ATTRIBUTE_UNUSED, rtx insn,
				 unsigned int regno)
Jeff Law committed
3394 3395 3396 3397 3398 3399
{
  unsigned int uid;
  struct df_link *link;

  uid = INSN_UID (insn);

3400
  for (link = df->insns[uid].defs; link; link = link->next)
Jeff Law committed
3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414
    {
      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
3415
df_bb_single_def_use_insn_find (struct df *df, basic_block bb, rtx insn, rtx reg)
Jeff Law committed
3416 3417 3418 3419 3420 3421 3422
{
  struct ref *def;
  struct ref *use;
  struct df_link *du_link;

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

3423
  gcc_assert (def);
Jeff Law committed
3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438

  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;
3439

Jeff Law committed
3440 3441 3442 3443 3444 3445 3446 3447
  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
3448
df_chain_dump (struct df_link *link, FILE *file)
Jeff Law committed
3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459
{
  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, "}");
}

3460 3461

/* Dump a chain of refs with the associated regno.  */
Jeff Law committed
3462
static void
3463
df_chain_dump_regno (struct df_link *link, FILE *file)
Jeff Law committed
3464 3465 3466 3467 3468
{
  fprintf (file, "{ ");
  for (; link; link = link->next)
    {
      fprintf (file, "%c%d(%d) ",
Kazu Hirata committed
3469 3470 3471
	       DF_REF_REG_DEF_P (link->ref) ? 'd' : 'u',
	       DF_REF_ID (link->ref),
	       DF_REF_REGNO (link->ref));
Jeff Law committed
3472 3473 3474 3475
    }
  fprintf (file, "}");
}

3476

Jeff Law committed
3477 3478
/* Dump dataflow info.  */
void
3479
df_dump (struct df *df, int flags, FILE *file)
Jeff Law committed
3480 3481
{
  unsigned int j;
3482
  basic_block bb;
Jeff Law committed
3483 3484 3485 3486 3487 3488 3489 3490 3491 3492

  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)
    {
3493 3494
      basic_block bb;

Jeff Law committed
3495
      fprintf (file, "Reaching defs:\n");
3496
      FOR_EACH_BB (bb)
Jeff Law committed
3497
	{
3498 3499
	  struct bb_info *bb_info = DF_BB_INFO (df, bb);

Jeff Law committed
3500 3501 3502
	  if (! bb_info->rd_in)
	    continue;

3503
	  fprintf (file, "bb %d in  \t", bb->index);
Jeff Law committed
3504
	  dump_bitmap (file, bb_info->rd_in);
3505
	  fprintf (file, "bb %d gen \t", bb->index);
Jeff Law committed
3506
	  dump_bitmap (file, bb_info->rd_gen);
3507
	  fprintf (file, "bb %d kill\t", bb->index);
Jeff Law committed
3508
	  dump_bitmap (file, bb_info->rd_kill);
3509
	  fprintf (file, "bb %d out \t", bb->index);
Jeff Law committed
3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525
	  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]));
3526 3527
	      if (df->defs[j]->flags & DF_REF_READ_WRITE)
		fprintf (file, "read/write ");
Jeff Law committed
3528 3529 3530 3531 3532 3533 3534 3535 3536
	      df_chain_dump (DF_REF_CHAIN (df->defs[j]), file);
	      fprintf (file, "\n");
	    }
	}
    }

  if (flags & DF_RU)
    {
      fprintf (file, "Reaching uses:\n");
3537
      FOR_EACH_BB (bb)
Jeff Law committed
3538
	{
3539 3540
	  struct bb_info *bb_info = DF_BB_INFO (df, bb);

Jeff Law committed
3541 3542 3543
	  if (! bb_info->ru_in)
	    continue;

3544
	  fprintf (file, "bb %d in  \t", bb->index);
Jeff Law committed
3545
	  dump_bitmap (file, bb_info->ru_in);
3546
	  fprintf (file, "bb %d gen \t", bb->index);
Jeff Law committed
3547
	  dump_bitmap (file, bb_info->ru_gen);
3548
	  fprintf (file, "bb %d kill\t", bb->index);
Jeff Law committed
3549
	  dump_bitmap (file, bb_info->ru_kill);
3550
	  fprintf (file, "bb %d out \t", bb->index);
Jeff Law committed
3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566
	  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]));
3567 3568
	      if (df->uses[j]->flags & DF_REF_READ_WRITE)
		fprintf (file, "read/write ");
Jeff Law committed
3569 3570 3571 3572 3573 3574 3575 3576 3577
	      df_chain_dump (DF_REF_CHAIN (df->uses[j]), file);
	      fprintf (file, "\n");
	    }
	}
    }

  if (flags & DF_LR)
    {
      fprintf (file, "Live regs:\n");
3578
      FOR_EACH_BB (bb)
Jeff Law committed
3579
	{
3580 3581
	  struct bb_info *bb_info = DF_BB_INFO (df, bb);

Jeff Law committed
3582 3583 3584
	  if (! bb_info->lr_in)
	    continue;

3585
	  fprintf (file, "bb %d in  \t", bb->index);
Jeff Law committed
3586
	  dump_bitmap (file, bb_info->lr_in);
3587
	  fprintf (file, "bb %d use \t", bb->index);
Jeff Law committed
3588
	  dump_bitmap (file, bb_info->lr_use);
3589
	  fprintf (file, "bb %d def \t", bb->index);
Jeff Law committed
3590
	  dump_bitmap (file, bb_info->lr_def);
3591
	  fprintf (file, "bb %d out \t", bb->index);
Jeff Law committed
3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602
	  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++)
	{
3603
	  if (((flags & DF_REG_INFO)
Jeff Law committed
3604 3605 3606
	       && (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))
Kazu Hirata committed
3607 3608 3609 3610 3611
	    {
	      fprintf (file, "reg %d", j);
	      if ((flags & DF_RD_CHAIN) && (flags & DF_RU_CHAIN))
		{
		  basic_block bb = df_regno_bb (df, j);
3612

Kazu Hirata committed
3613 3614 3615 3616 3617 3618 3619 3620 3621
		  if (bb)
		    fprintf (file, " bb %d", bb->index);
		  else
		    fprintf (file, " bb ?");
		}
	      if (flags & DF_REG_INFO)
		{
		  fprintf (file, " life %d", reg_info[j].lifetime);
		}
Jeff Law committed
3622

Kazu Hirata committed
3623 3624 3625 3626 3627 3628 3629 3630
	      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);
		}
Jeff Law committed
3631

Kazu Hirata committed
3632 3633 3634 3635 3636 3637 3638 3639
	      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);
		}
Jeff Law committed
3640

Kazu Hirata committed
3641 3642
	      fprintf (file, "\n");
	    }
Jeff Law committed
3643 3644 3645 3646 3647 3648 3649
	}
    }
  fprintf (file, "\n");
}


void
3650
df_insn_debug (struct df *df, rtx insn, FILE *file)
Jeff Law committed
3651 3652 3653 3654 3655 3656 3657 3658 3659 3660
{
  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);
Kazu Hirata committed
3661
  else if (df->insns[uid].uses)
Jeff Law committed
3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673
    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");
}

3674

Jeff Law committed
3675
void
3676
df_insn_debug_regno (struct df *df, rtx insn, FILE *file)
Jeff Law committed
3677 3678 3679 3680 3681 3682 3683 3684 3685 3686
{
  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);
Kazu Hirata committed
3687
  else if (df->insns[uid].uses)
Jeff Law committed
3688 3689 3690 3691 3692
    bbi = DF_REF_BBNO (df->insns[uid].uses->ref);
  else
    bbi = -1;

  fprintf (file, "insn %d bb %d luid %d defs ",
Kazu Hirata committed
3693
	   uid, bbi, DF_INSN_LUID (df, insn));
Jeff Law committed
3694 3695 3696 3697 3698 3699
  df_chain_dump_regno (df->insns[uid].defs, file);
  fprintf (file, " uses ");
  df_chain_dump_regno (df->insns[uid].uses, file);
  fprintf (file, "\n");
}

3700

Jeff Law committed
3701
static void
3702
df_regno_debug (struct df *df, unsigned int regno, FILE *file)
Jeff Law committed
3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716
{
  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
3717
df_ref_debug (struct df *df, struct ref *ref, FILE *file)
Jeff Law committed
3718 3719 3720 3721
{
  fprintf (file, "%c%d ",
	   DF_REF_REG_DEF_P (ref) ? 'd' : 'u',
	   DF_REF_ID (ref));
3722
  fprintf (file, "reg %d bb %d luid %d insn %d chain ",
Jeff Law committed
3723
	   DF_REF_REGNO (ref),
3724
	   DF_REF_BBNO (ref),
Jeff Law committed
3725 3726 3727 3728 3729
	   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");
}
3730 3731

/* Functions for debugging from GDB.  */
Jeff Law committed
3732

3733
void
3734
debug_df_insn (rtx insn)
Jeff Law committed
3735 3736 3737 3738 3739 3740
{
  df_insn_debug (ddf, insn, stderr);
  debug_rtx (insn);
}


3741
void
3742
debug_df_reg (rtx reg)
Jeff Law committed
3743 3744 3745 3746 3747
{
  df_regno_debug (ddf, REGNO (reg), stderr);
}


3748
void
3749
debug_df_regno (unsigned int regno)
Jeff Law committed
3750 3751 3752 3753 3754
{
  df_regno_debug (ddf, regno, stderr);
}


3755
void
3756
debug_df_ref (struct ref *ref)
Jeff Law committed
3757 3758 3759 3760 3761
{
  df_ref_debug (ddf, ref, stderr);
}


3762
void
3763
debug_df_defno (unsigned int defno)
Jeff Law committed
3764 3765 3766 3767 3768
{
  df_ref_debug (ddf, ddf->defs[defno], stderr);
}


3769
void
3770
debug_df_useno (unsigned int defno)
Jeff Law committed
3771 3772 3773 3774 3775
{
  df_ref_debug (ddf, ddf->uses[defno], stderr);
}


3776
void
3777
debug_df_chain (struct df_link *link)
Jeff Law committed
3778 3779 3780 3781
{
  df_chain_dump (link, stderr);
  fputc ('\n', stderr);
}
3782

3783

3784 3785 3786
/* Perform the set operation OP1 OP OP2, using set representation REPR, and
   storing the result in OP1.  */

Kazu Hirata committed
3787
static void
3788 3789
dataflow_set_a_op_b (enum set_representation repr,
		     enum df_confluence_op op,
3790
		     void *op1, void *op2)
3791
{
3792
  switch (repr)
3793
    {
3794 3795
    case SR_SBITMAP:
      switch (op)
3796
	{
3797
	case DF_UNION:
3798
	  sbitmap_a_or_b (op1, op1, op2);
3799 3800 3801
	  break;

	case DF_INTERSECTION:
3802
	  sbitmap_a_and_b (op1, op1, op2);
3803 3804 3805
	  break;

    	default:
3806
	  gcc_unreachable ();
3807
	}
3808 3809 3810 3811
      break;

    case SR_BITMAP:
      switch (op)
3812
	{
3813
	case DF_UNION:
3814
	  bitmap_ior_into (op1, op2);
3815 3816 3817
	  break;

	case DF_INTERSECTION:
3818
	  bitmap_and_into (op1, op2);
3819 3820 3821
	  break;

    	default:
3822
	  gcc_unreachable ();
3823
	}
3824 3825 3826
      break;

    default:
3827
      gcc_unreachable ();
3828
    }
3829 3830 3831 3832 3833 3834
}

static void
dataflow_set_copy (enum set_representation repr, void *dest, void *src)
{
  switch (repr)
3835
    {
3836 3837 3838 3839 3840 3841 3842 3843 3844
    case SR_SBITMAP:
      sbitmap_copy (dest, src);
      break;

    case SR_BITMAP:
      bitmap_copy (dest, src);
      break;

    default:
3845
      gcc_unreachable ();
3846
    }
3847
}
3848

3849 3850
/* Hybrid search algorithm from "Implementation Techniques for
   Efficient Data-Flow Analysis of Large Programs".  */
3851

Kazu Hirata committed
3852
static void
3853 3854
hybrid_search (basic_block bb, struct dataflow *dataflow,
	       sbitmap visited, sbitmap pending, sbitmap considered)
3855 3856
{
  int changed;
3857
  int i = bb->index;
3858
  edge e;
3859
  edge_iterator ei;
3860

3861
  SET_BIT (visited, bb->index);
3862
  gcc_assert (TEST_BIT (pending, bb->index));
3863 3864
  RESET_BIT (pending, i);

3865 3866
#define HS(E_ANTI, E_ANTI_BB, E_ANTI_START_BB, IN_SET,			\
	   E, E_BB, E_START_BB, OUT_SET)				\
3867 3868 3869 3870
  do									\
    {									\
      /*  Calculate <conf_op> of predecessor_outs.  */			\
      bitmap_zero (IN_SET[i]);						\
3871
      FOR_EACH_EDGE (e, ei, bb->E_ANTI)					\
3872 3873 3874 3875 3876 3877 3878
	{								\
	  if (e->E_ANTI_BB == E_ANTI_START_BB)				\
	    continue;							\
	  if (!TEST_BIT (considered, e->E_ANTI_BB->index))		\
	    continue;							\
									\
	  dataflow_set_a_op_b (dataflow->repr, dataflow->conf_op,	\
3879
			       IN_SET[i],      			        \
3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890
			       OUT_SET[e->E_ANTI_BB->index]);		\
	}								\
									\
      (*dataflow->transfun)(i, &changed,				\
			    dataflow->in[i], dataflow->out[i],		\
			    dataflow->gen[i], dataflow->kill[i],	\
			    dataflow->data);				\
									\
      if (!changed)							\
	break;								\
									\
3891
      FOR_EACH_EDGE (e, ei, bb->E)						\
3892 3893 3894 3895 3896 3897 3898 3899 3900 3901
	{								\
	  if (e->E_BB == E_START_BB || e->E_BB->index == i)		\
	    continue;							\
									\
	  if (!TEST_BIT (considered, e->E_BB->index))			\
	    continue;							\
									\
	  SET_BIT (pending, e->E_BB->index);				\
      	}								\
									\
3902
      FOR_EACH_EDGE (e, ei, bb->E)						\
3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915
	{								\
	  if (e->E_BB == E_START_BB || e->E_BB->index == i)		\
	    continue;							\
									\
	  if (!TEST_BIT (considered, e->E_BB->index))			\
	    continue;							\
									\
	  if (!TEST_BIT (visited, e->E_BB->index))			\
	    hybrid_search (e->E_BB, dataflow, visited, pending, considered); \
	}								\
    } while (0)

  if (dataflow->dir == DF_FORWARD)
3916 3917
    HS (preds, src, ENTRY_BLOCK_PTR, dataflow->in,
	succs, dest, EXIT_BLOCK_PTR, dataflow->out);
3918
  else
3919 3920
    HS (succs, dest, EXIT_BLOCK_PTR, dataflow->out,
	preds, src, ENTRY_BLOCK_PTR, dataflow->in);
3921 3922
}

3923 3924 3925
/* This function will perform iterative bitvector dataflow described by
   DATAFLOW, producing the in and out sets.  Only the part of the cfg
   induced by blocks in DATAFLOW->order is taken into account.
Kazu Hirata committed
3926

3927
   For forward problems, you probably want to pass in rc_order.  */
3928

3929
void
3930
iterative_dataflow (struct dataflow *dataflow)
3931
{
3932 3933
  unsigned i, idx;
  sbitmap visited, pending, considered;
3934

3935 3936
  pending = sbitmap_alloc (last_basic_block);
  visited = sbitmap_alloc (last_basic_block);
3937
  considered = sbitmap_alloc (last_basic_block);
3938 3939
  sbitmap_zero (pending);
  sbitmap_zero (visited);
3940
  sbitmap_zero (considered);
3941

3942
  for (i = 0; i < dataflow->n_blocks; i++)
3943
    {
3944 3945 3946 3947 3948 3949
      idx = dataflow->order[i];
      SET_BIT (pending, idx);
      SET_BIT (considered, idx);
      if (dataflow->dir == DF_FORWARD)
	dataflow_set_copy (dataflow->repr,
			   dataflow->out[idx], dataflow->gen[idx]);
3950
      else
3951 3952 3953
	dataflow_set_copy (dataflow->repr,
			   dataflow->in[idx], dataflow->gen[idx]);
    };
3954

3955
  while (1)
3956
    {
3957
      for (i = 0; i < dataflow->n_blocks; i++)
3958
	{
3959
	  idx = dataflow->order[i];
3960

3961 3962 3963
	  if (TEST_BIT (pending, idx) && !TEST_BIT (visited, idx))
	    hybrid_search (BASIC_BLOCK (idx), dataflow,
			   visited, pending, considered);
Kazu Hirata committed
3964
	}
3965 3966 3967 3968 3969

      if (sbitmap_first_set_bit (pending) == -1)
	break;

      sbitmap_zero (visited);
3970
    }
3971

3972 3973
  sbitmap_free (pending);
  sbitmap_free (visited);
3974
  sbitmap_free (considered);
3975
}