cprop.c 56.2 KB
Newer Older
1
/* Global constant/copy propagation for RTL.
2
   Copyright (C) 1997-2017 Free Software Foundation, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

This file is part of GCC.

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 3, or (at your option) any later
version.

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.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#include "config.h"
#include "system.h"
#include "coretypes.h"
23 24
#include "backend.h"
#include "rtl.h"
25
#include "cfghooks.h"
26
#include "df.h"
27
#include "insn-config.h"
28
#include "memmodel.h"
29 30
#include "emit-rtl.h"
#include "recog.h"
31 32
#include "diagnostic-core.h"
#include "toplev.h"
33 34 35 36
#include "cfgrtl.h"
#include "cfganal.h"
#include "lcm.h"
#include "cfgcleanup.h"
37 38 39 40 41
#include "params.h"
#include "cselib.h"
#include "intl.h"
#include "tree-pass.h"
#include "dbgcnt.h"
42
#include "cfgloop.h"
43
#include "gcse.h"
44 45 46


/* An obstack for our working variables.  */
47
static struct obstack cprop_obstack;
48 49 50 51 52

/* Occurrence of an expression.
   There is one per basic block.  If a pattern appears more than once the
   last appearance is used.  */

53
struct cprop_occr
54 55
{
  /* Next occurrence of this expression.  */
56
  struct cprop_occr *next;
57
  /* The insn that computes the expression.  */
David Malcolm committed
58
  rtx_insn *insn;
59 60
};

61
/* Hash table entry for assignment expressions.  */
62

63
struct cprop_expr
64 65 66 67 68 69 70 71
{
  /* The expression (DEST := SRC).  */
  rtx dest;
  rtx src;

  /* Index in the available expression bitmaps.  */
  int bitmap_index;
  /* Next entry with the same hash.  */
72
  struct cprop_expr *next_same_hash;
73 74
  /* List of available occurrence in basic blocks in the function.
     An "available occurrence" is one that is the last occurrence in the
75 76
     basic block and whose operands are not modified by following statements
     in the basic block [including this insn].  */
77
  struct cprop_occr *avail_occr;
78 79 80
};

/* Hash table for copy propagation expressions.
81 82 83 84 85 86 87 88 89 90 91 92
   Each hash table is an array of buckets.
   ??? It is known that if it were an array of entries, structure elements
   `next_same_hash' and `bitmap_index' wouldn't be necessary.  However, it is
   not clear whether in the final analysis a sufficient amount of memory would
   be saved as the size of the available expression bitmaps would be larger
   [one could build a mapping table without holes afterwards though].
   Someday I'll perform the computation and figure it out.  */

struct hash_table_d
{
  /* The table itself.
     This is an array of `set_hash_table_size' elements.  */
93
  struct cprop_expr **table;
94 95 96 97 98 99 100 101 102 103 104 105 106 107

  /* Size of the hash table, in elements.  */
  unsigned int size;

  /* Number of hash table elements.  */
  unsigned int n_elems;
};

/* Copy propagation hash table.  */
static struct hash_table_d set_hash_table;

/* Array of implicit set patterns indexed by basic block index.  */
static rtx *implicit_sets;

108 109 110 111 112
/* Array of indexes of expressions for implicit set patterns indexed by basic
   block index.  In other words, implicit_set_indexes[i] is the bitmap_index
   of the expression whose RTX is implicit_sets[i].  */
static int *implicit_set_indexes;

113 114
/* Bitmap containing one bit for each register in the program.
   Used when performing GCSE to track which registers have been set since
115
   the start or end of the basic block while traversing that block.  */
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
static regset reg_set_bitmap;

/* Various variables for statistics gathering.  */

/* Memory used in a pass.
   This isn't intended to be absolutely precise.  Its intent is only
   to keep an eye on memory usage.  */
static int bytes_used;

/* Number of local constants propagated.  */
static int local_const_prop_count;
/* Number of local copies propagated.  */
static int local_copy_prop_count;
/* Number of global constants propagated.  */
static int global_const_prop_count;
/* Number of global copies propagated.  */
static int global_copy_prop_count;

134 135
#define GOBNEW(T)		((T *) cprop_alloc (sizeof (T)))
#define GOBNEWVAR(T, S)		((T *) cprop_alloc ((S)))
136 137 138 139

/* Cover function to obstack_alloc.  */

static void *
140
cprop_alloc (unsigned long size)
141 142
{
  bytes_used += size;
143
  return obstack_alloc (&cprop_obstack, size);
144 145
}

146 147
/* Return nonzero if register X is unchanged from INSN to the end
   of INSN's basic block.  */
148 149

static int
David Malcolm committed
150
reg_available_p (const_rtx x, const rtx_insn *insn ATTRIBUTE_UNUSED)
151
{
152
  return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
153 154 155 156 157 158 159 160 161 162
}

/* Hash a set of register REGNO.

   Sets are hashed on the register that is set.  This simplifies the PRE copy
   propagation code.

   ??? May need to make things more elaborate.  Later, as necessary.  */

static unsigned int
163
hash_mod (int regno, int hash_table_size)
164
{
165
  return (unsigned) regno % hash_table_size;
166 167
}

168 169 170
/* Insert assignment DEST:=SET from INSN in the hash table.
   DEST is a register and SET is a register or a suitable constant.
   If the assignment is already present in the table, record it as
171 172
   the last occurrence in INSN's basic block.
   IMPLICIT is true if it's an implicit set, false otherwise.  */
173 174

static void
David Malcolm committed
175 176
insert_set_in_table (rtx dest, rtx src, rtx_insn *insn,
		     struct hash_table_d *table, bool implicit)
177
{
178
  bool found = false;
179
  unsigned int hash;
180 181
  struct cprop_expr *cur_expr, *last_expr = NULL;
  struct cprop_occr *cur_occr;
182

183
  hash = hash_mod (REGNO (dest), table->size);
184

185 186
  for (cur_expr = table->table[hash]; cur_expr;
       cur_expr = cur_expr->next_same_hash)
187
    {
188 189 190 191 192 193
      if (dest == cur_expr->dest
	  && src == cur_expr->src)
	{
	  found = true;
	  break;
	}
194 195 196 197 198
      last_expr = cur_expr;
    }

  if (! found)
    {
199 200
      cur_expr = GOBNEW (struct cprop_expr);
      bytes_used += sizeof (struct cprop_expr);
201 202 203 204 205 206 207 208 209 210
      if (table->table[hash] == NULL)
	/* This is the first pattern that hashed to this index.  */
	table->table[hash] = cur_expr;
      else
	/* Add EXPR to end of this hash chain.  */
	last_expr->next_same_hash = cur_expr;

      /* Set the fields of the expr element.
	 We must copy X because it can be modified when copy propagation is
	 performed on its operands.  */
211 212
      cur_expr->dest = copy_rtx (dest);
      cur_expr->src = copy_rtx (src);
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
      cur_expr->bitmap_index = table->n_elems++;
      cur_expr->next_same_hash = NULL;
      cur_expr->avail_occr = NULL;
    }

  /* Now record the occurrence.  */
  cur_occr = cur_expr->avail_occr;

  if (cur_occr
      && BLOCK_FOR_INSN (cur_occr->insn) == BLOCK_FOR_INSN (insn))
    {
      /* Found another instance of the expression in the same basic block.
	 Prefer this occurrence to the currently recorded one.  We want
	 the last one in the block and the block is scanned from start
	 to end.  */
      cur_occr->insn = insn;
    }
  else
    {
      /* First occurrence of this expression in this basic block.  */
233 234
      cur_occr = GOBNEW (struct cprop_occr);
      bytes_used += sizeof (struct cprop_occr);
235 236 237 238
      cur_occr->insn = insn;
      cur_occr->next = cur_expr->avail_occr;
      cur_expr->avail_occr = cur_occr;
    }
239 240 241

  /* Record bitmap_index of the implicit set in implicit_set_indexes.  */
  if (implicit)
242 243
    implicit_set_indexes[BLOCK_FOR_INSN (insn)->index]
      = cur_expr->bitmap_index;
244 245
}

246 247 248
/* Determine whether the rtx X should be treated as a constant for CPROP.
   Since X might be inserted more than once we have to take care that it
   is sharable.  */
249 250

static bool
251
cprop_constant_p (const_rtx x)
252 253 254 255
{
  return CONSTANT_P (x) && (GET_CODE (x) != CONST || shared_const_p (x));
}

256 257 258 259 260 261 262 263 264
/* Determine whether the rtx X should be treated as a register that can
   be propagated.  Any pseudo-register is fine.  */

static bool
cprop_reg_p (const_rtx x)
{
  return REG_P (x) && !HARD_REGISTER_P (x);
}

265 266
/* Scan SET present in INSN and add an entry to the hash TABLE.
   IMPLICIT is true if it's an implicit set, false otherwise.  */
267 268

static void
David Malcolm committed
269 270
hash_scan_set (rtx set, rtx_insn *insn, struct hash_table_d *table,
	       bool implicit)
271
{
272 273
  rtx src = SET_SRC (set);
  rtx dest = SET_DEST (set);
274

275
  if (cprop_reg_p (dest)
276 277
      && reg_available_p (dest, insn)
      && can_copy_p (GET_MODE (dest)))
278 279 280
    {
      /* See if a REG_EQUAL note shows this equivalent to a simpler expression.

281
	 This allows us to do a single CPROP pass and still eliminate
282 283 284 285 286 287
	 redundant constants, addresses or other expressions that are
	 constructed with multiple instructions.

	 However, keep the original SRC if INSN is a simple reg-reg move.  In
	 In this case, there will almost always be a REG_EQUAL note on the
	 insn that sets SRC.  By recording the REG_EQUAL value here as SRC
288
	 for INSN, we miss copy propagation opportunities.
289 290

	 Note that this does not impede profitable constant propagations.  We
291
	 "look through" reg-reg sets in lookup_set.  */
292
      rtx note = find_reg_equal_equiv_note (insn);
293 294 295
      if (note != 0
	  && REG_NOTE_KIND (note) == REG_EQUAL
	  && !REG_P (src)
296
	  && cprop_constant_p (XEXP (note, 0)))
297
	src = XEXP (note, 0), set = gen_rtx_SET (dest, src);
298 299

      /* Record sets for constant/copy propagation.  */
300
      if ((cprop_reg_p (src)
301 302
	   && src != dest
	   && reg_available_p (src, insn))
303
	  || cprop_constant_p (src))
304
	insert_set_in_table (dest, src, insn, table, implicit);
305 306 307
    }
}

308
/* Process INSN and add hash table entries as appropriate.  */
309 310

static void
David Malcolm committed
311
hash_scan_insn (rtx_insn *insn, struct hash_table_d *table)
312 313 314 315 316 317 318 319
{
  rtx pat = PATTERN (insn);
  int i;

  /* Pick out the sets of INSN and for other forms of instructions record
     what's been modified.  */

  if (GET_CODE (pat) == SET)
320
    hash_scan_set (pat, insn, table, false);
321 322 323 324 325 326
  else if (GET_CODE (pat) == PARALLEL)
    for (i = 0; i < XVECLEN (pat, 0); i++)
      {
	rtx x = XVECEXP (pat, 0, i);

	if (GET_CODE (x) == SET)
327
	  hash_scan_set (x, insn, table, false);
328 329 330
      }
}

331 332
/* Dump the hash table TABLE to file FILE under the name NAME.  */

333 334 335 336 337
static void
dump_hash_table (FILE *file, const char *name, struct hash_table_d *table)
{
  int i;
  /* Flattened out table, so it's printed in proper order.  */
338
  struct cprop_expr **flat_table;
339
  unsigned int *hash_val;
340
  struct cprop_expr *expr;
341

342
  flat_table = XCNEWVEC (struct cprop_expr *, table->n_elems);
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
  hash_val = XNEWVEC (unsigned int, table->n_elems);

  for (i = 0; i < (int) table->size; i++)
    for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
      {
	flat_table[expr->bitmap_index] = expr;
	hash_val[expr->bitmap_index] = i;
      }

  fprintf (file, "%s hash table (%d buckets, %d entries)\n",
	   name, table->size, table->n_elems);

  for (i = 0; i < (int) table->n_elems; i++)
    if (flat_table[i] != 0)
      {
	expr = flat_table[i];
	fprintf (file, "Index %d (hash value %d)\n  ",
		 expr->bitmap_index, hash_val[i]);
361 362 363
	print_rtl (file, expr->dest);
	fprintf (file, " := ");
	print_rtl (file, expr->src);
364 365 366 367 368 369 370 371 372
	fprintf (file, "\n");
      }

  fprintf (file, "\n");

  free (flat_table);
  free (hash_val);
}

373
/* Record as unavailable all registers that are DEF operands of INSN.  */
374

375
static void
David Malcolm committed
376
make_set_regs_unavailable (rtx_insn *insn)
377
{
378
  df_ref def;
379

380 381
  FOR_EACH_INSN_DEF (def, insn)
    SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (def));
382 383
}

384
/* Top level function to create an assignment hash table.
385 386 387 388 389 390 391 392 393 394 395 396 397

   Assignment entries are placed in the hash table if
   - they are of the form (set (pseudo-reg) src),
   - src is something we want to perform const/copy propagation on,
   - none of the operands or target are subsequently modified in the block

   Currently src must be a pseudo-reg or a const_int.

   TABLE is the table computed.  */

static void
compute_hash_table_work (struct hash_table_d *table)
{
398
  basic_block bb;
399

400 401 402
  /* Allocate vars to track sets of regs.  */
  reg_set_bitmap = ALLOC_REG_SET (NULL);

403
  FOR_EACH_BB_FN (bb, cfun)
404
    {
David Malcolm committed
405
      rtx_insn *insn;
406

407 408 409 410 411 412 413 414 415 416
      /* Reset tables used to keep track of what's not yet invalid [since
	 the end of the block].  */
      CLEAR_REG_SET (reg_set_bitmap);

      /* Go over all insns from the last to the first.  This is convenient
	 for tracking available registers, i.e. not set between INSN and
	 the end of the basic block BB.  */
      FOR_BB_INSNS_REVERSE (bb, insn)
        {
	  /* Only real insns are interesting.  */
417 418 419
	  if (!NONDEBUG_INSN_P (insn))
	    continue;

420 421
	  /* Record interesting sets from INSN in the hash table.  */
	  hash_scan_insn (insn, table);
422

423 424
	  /* Any registers set in INSN will make SETs above it not AVAIL.  */
	  make_set_regs_unavailable (insn);
425 426
	}

427 428 429
      /* Insert implicit sets in the hash table, pretending they appear as
	 insns at the head of the basic block.  */
      if (implicit_sets[bb->index] != NULL_RTX)
430
	hash_scan_set (implicit_sets[bb->index], BB_HEAD (bb), table, true);
431
    }
432 433

  FREE_REG_SET (reg_set_bitmap);
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
}

/* Allocate space for the set/expr hash TABLE.
   It is used to determine the number of buckets to use.  */

static void
alloc_hash_table (struct hash_table_d *table)
{
  int n;

  n = get_max_insn_count ();

  table->size = n / 4;
  if (table->size < 11)
    table->size = 11;

  /* Attempt to maintain efficient use of hash table.
     Making it an odd number is simplest for now.
     ??? Later take some measurements.  */
  table->size |= 1;
454 455
  n = table->size * sizeof (struct cprop_expr *);
  table->table = XNEWVAR (struct cprop_expr *, n);
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
}

/* Free things allocated by alloc_hash_table.  */

static void
free_hash_table (struct hash_table_d *table)
{
  free (table->table);
}

/* Compute the hash TABLE for doing copy/const propagation or
   expression hash table.  */

static void
compute_hash_table (struct hash_table_d *table)
{
  /* Initialize count of number of entries in hash table.  */
  table->n_elems = 0;
474
  memset (table->table, 0, table->size * sizeof (struct cprop_expr *));
475 476 477 478 479 480 481 482 483

  compute_hash_table_work (table);
}

/* Expression tracking support.  */

/* Lookup REGNO in the set TABLE.  The result is a pointer to the
   table entry, or NULL if not found.  */

484
static struct cprop_expr *
485 486
lookup_set (unsigned int regno, struct hash_table_d *table)
{
487
  unsigned int hash = hash_mod (regno, table->size);
488
  struct cprop_expr *expr;
489 490 491

  expr = table->table[hash];

492
  while (expr && REGNO (expr->dest) != regno)
493 494 495 496 497 498 499
    expr = expr->next_same_hash;

  return expr;
}

/* Return the next entry for REGNO in list EXPR.  */

500 501
static struct cprop_expr *
next_set (unsigned int regno, struct cprop_expr *expr)
502 503 504
{
  do
    expr = expr->next_same_hash;
505
  while (expr && REGNO (expr->dest) != regno);
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520

  return expr;
}

/* Reset tables used to keep track of what's still available [since the
   start of the block].  */

static void
reset_opr_set_tables (void)
{
  /* Maintain a bitmap of which regs have been set since beginning of
     the block.  */
  CLEAR_REG_SET (reg_set_bitmap);
}

521 522
/* Return nonzero if the register X has not been set yet [since the
   start of the basic block containing INSN].  */
523 524

static int
David Malcolm committed
525
reg_not_set_p (const_rtx x, const rtx_insn *insn ATTRIBUTE_UNUSED)
526
{
527
  return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
528 529 530
}

/* Record things set by INSN.
531
   This data is used by reg_not_set_p.  */
532 533

static void
David Malcolm committed
534
mark_oprs_set (rtx_insn *insn)
535
{
536
  df_ref def;
537

538 539
  FOR_EACH_INSN_DEF (def, insn)
    SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (def));
540 541 542 543 544
}

/* Compute copy/constant propagation working variables.  */

/* Local properties of assignments.  */
545 546
static sbitmap *cprop_avloc;
static sbitmap *cprop_kill;
547 548 549 550 551 552 553 554 555 556 557

/* Global properties of assignments (computed from the local properties).  */
static sbitmap *cprop_avin;
static sbitmap *cprop_avout;

/* Allocate vars used for copy/const propagation.  N_BLOCKS is the number of
   basic blocks.  N_SETS is the number of sets.  */

static void
alloc_cprop_mem (int n_blocks, int n_sets)
{
558 559
  cprop_avloc = sbitmap_vector_alloc (n_blocks, n_sets);
  cprop_kill = sbitmap_vector_alloc (n_blocks, n_sets);
560 561 562 563 564 565 566 567 568 569

  cprop_avin = sbitmap_vector_alloc (n_blocks, n_sets);
  cprop_avout = sbitmap_vector_alloc (n_blocks, n_sets);
}

/* Free vars used by copy/const propagation.  */

static void
free_cprop_mem (void)
{
570 571
  sbitmap_vector_free (cprop_avloc);
  sbitmap_vector_free (cprop_kill);
572 573 574 575 576 577 578 579 580
  sbitmap_vector_free (cprop_avin);
  sbitmap_vector_free (cprop_avout);
}

/* Compute the local properties of each recorded expression.

   Local properties are those that are defined by the block, irrespective of
   other blocks.

581 582
   An expression is killed in a block if its operands, either DEST or SRC, are
   modified in the block.
583 584 585 586 587

   An expression is computed (locally available) in a block if it is computed
   at least once and expression would contain the same value if the
   computation was moved to the end of the block.

588
   KILL and COMP are destination sbitmaps for recording local properties.  */
589 590

static void
591
compute_local_properties (sbitmap *kill, sbitmap *comp,
592 593 594 595
			  struct hash_table_d *table)
{
  unsigned int i;

596
  /* Initialize the bitmaps that were passed in.  */
597 598
  bitmap_vector_clear (kill, last_basic_block_for_fn (cfun));
  bitmap_vector_clear (comp, last_basic_block_for_fn (cfun));
599 600 601

  for (i = 0; i < table->size; i++)
    {
602
      struct cprop_expr *expr;
603 604 605 606

      for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
	{
	  int indx = expr->bitmap_index;
607
	  df_ref def;
608
	  struct cprop_occr *occr;
609

610 611
	  /* For each definition of the destination pseudo-reg, the expression
	     is killed in the block where the definition is.  */
612 613
	  for (def = DF_REG_DEF_CHAIN (REGNO (expr->dest));
	       def; def = DF_REF_NEXT_REG (def))
614
	    bitmap_set_bit (kill[DF_REF_BB (def)->index], indx);
615 616 617

	  /* If the source is a pseudo-reg, for each definition of the source,
	     the expression is killed in the block where the definition is.  */
618 619 620
	  if (REG_P (expr->src))
	    for (def = DF_REG_DEF_CHAIN (REGNO (expr->src));
		 def; def = DF_REF_NEXT_REG (def))
621
	      bitmap_set_bit (kill[DF_REF_BB (def)->index], indx);
622 623

	  /* The occurrences recorded in avail_occr are exactly those that
624
	     are locally available in the block where they are.  */
625 626
	  for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
	    {
627
	      bitmap_set_bit (comp[BLOCK_FOR_INSN (occr->insn)->index], indx);
628
	    }
629 630 631 632 633 634 635 636 637 638 639 640
	}
    }
}

/* Hash table support.  */

/* Top level routine to do the dataflow analysis needed by copy/const
   propagation.  */

static void
compute_cprop_data (void)
{
641 642
  basic_block bb;

643 644
  compute_local_properties (cprop_kill, cprop_avloc, &set_hash_table);
  compute_available (cprop_avloc, cprop_kill, cprop_avout, cprop_avin);
645 646 647 648 649 650

  /* Merge implicit sets into CPROP_AVIN.  They are always available at the
     entry of their basic block.  We need to do this because 1) implicit sets
     aren't recorded for the local pass so they cannot be propagated within
     their basic block by this pass and 2) the global pass would otherwise
     propagate them only in the successors of their basic block.  */
651
  FOR_EACH_BB_FN (bb, cfun)
652 653 654
    {
      int index = implicit_set_indexes[bb->index];
      if (index != -1)
655
	bitmap_set_bit (cprop_avin[bb->index], index);
656
    }
657 658 659 660 661 662 663
}

/* Copy/constant propagation.  */

/* Maximum number of register uses in an insn that we handle.  */
#define MAX_USES 8

664
/* Table of uses (registers, both hard and pseudo) found in an insn.
665
   Allocated statically to avoid alloc/free complexity and overhead.  */
666
static rtx reg_use_table[MAX_USES];
667 668

/* Index into `reg_use_table' while building it.  */
669
static unsigned reg_use_count;
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697

/* Set up a list of register numbers used in INSN.  The found uses are stored
   in `reg_use_table'.  `reg_use_count' is initialized to zero before entry,
   and contains the number of uses in the table upon exit.

   ??? If a register appears multiple times we will record it multiple times.
   This doesn't hurt anything but it will slow things down.  */

static void
find_used_regs (rtx *xptr, void *data ATTRIBUTE_UNUSED)
{
  int i, j;
  enum rtx_code code;
  const char *fmt;
  rtx x = *xptr;

  /* repeat is used to turn tail-recursion into iteration since GCC
     can't do it when there's no return value.  */
 repeat:
  if (x == 0)
    return;

  code = GET_CODE (x);
  if (REG_P (x))
    {
      if (reg_use_count == MAX_USES)
	return;

698
      reg_use_table[reg_use_count] = x;
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
      reg_use_count++;
    }

  /* Recursively scan the operands of this expression.  */

  for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
    {
      if (fmt[i] == 'e')
	{
	  /* If we are about to do the last recursive call
	     needed at this level, change it into iteration.
	     This function is called enough to be worth it.  */
	  if (i == 0)
	    {
	      x = XEXP (x, 0);
	      goto repeat;
	    }

	  find_used_regs (&XEXP (x, i), data);
	}
      else if (fmt[i] == 'E')
	for (j = 0; j < XVECLEN (x, i); j++)
	  find_used_regs (&XVECEXP (x, i, j), data);
    }
}

725 726
/* Try to replace all uses of FROM in INSN with TO.
   Return nonzero if successful.  */
727 728

static int
David Malcolm committed
729
try_replace_reg (rtx from, rtx to, rtx_insn *insn)
730 731 732 733 734 735
{
  rtx note = find_reg_equal_equiv_note (insn);
  rtx src = 0;
  int success = 0;
  rtx set = single_set (insn);

736 737 738 739
  bool check_rtx_costs = true;
  bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
  int old_cost = set ? set_rtx_cost (set, speed) : 0;

740 741 742 743 744 745
  if (!set
      || CONSTANT_P (SET_SRC (set))
      || (note != 0
	  && REG_NOTE_KIND (note) == REG_EQUAL
	  && (GET_CODE (XEXP (note, 0)) == CONST
	      || CONSTANT_P (XEXP (note, 0)))))
746 747
    check_rtx_costs = false;

748 749 750 751 752 753
  /* Usually we substitute easy stuff, so we won't copy everything.
     We however need to take care to not duplicate non-trivial CONST
     expressions.  */
  to = copy_rtx (to);

  validate_replace_src_group (from, to, insn);
754 755 756 757 758 759 760

  /* If TO is a constant, check the cost of the set after propagation
     to the cost of the set before the propagation.  If the cost is
     higher, then do not replace FROM with TO.  */

  if (check_rtx_costs
      && CONSTANT_P (to)
761
      && set_rtx_cost (set, speed) > old_cost)
762 763 764 765 766 767
    {
      cancel_changes (0);
      return false;
    }


768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
  if (num_changes_pending () && apply_change_group ())
    success = 1;

  /* Try to simplify SET_SRC if we have substituted a constant.  */
  if (success && set && CONSTANT_P (to))
    {
      src = simplify_rtx (SET_SRC (set));

      if (src)
	validate_change (insn, &SET_SRC (set), src, 0);
    }

  /* If there is already a REG_EQUAL note, update the expression in it
     with our replacement.  */
  if (note != 0 && REG_NOTE_KIND (note) == REG_EQUAL)
    set_unique_reg_note (insn, REG_EQUAL,
			 simplify_replace_rtx (XEXP (note, 0), from, to));
  if (!success && set && reg_mentioned_p (from, SET_SRC (set)))
    {
787 788 789
      /* If above failed and this is a single set, try to simplify the source
	 of the set given our substitution.  We could perhaps try this for
	 multiple SETs, but it probably won't buy us anything.  */
790 791 792 793 794 795 796 797 798 799 800 801 802
      src = simplify_replace_rtx (SET_SRC (set), from, to);

      if (!rtx_equal_p (src, SET_SRC (set))
	  && validate_change (insn, &SET_SRC (set), src, 0))
	success = 1;

      /* If we've failed perform the replacement, have a single SET to
	 a REG destination and don't yet have a note, add a REG_EQUAL note
	 to not lose information.  */
      if (!success && note == 0 && set != 0 && REG_P (SET_DEST (set)))
	note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
    }

803 804 805 806 807 808
  if (set && MEM_P (SET_DEST (set)) && reg_mentioned_p (from, SET_DEST (set)))
    {
      /* Registers can also appear as uses in SET_DEST if it is a MEM.
         We could perhaps try this for multiple SETs, but it probably
         won't buy us anything.  */
      rtx dest = simplify_replace_rtx (SET_DEST (set), from, to);
809

810 811 812 813 814
      if (!rtx_equal_p (dest, SET_DEST (set))
          && validate_change (insn, &SET_DEST (set), dest, 0))
        success = 1;
    }

815 816 817 818 819 820 821 822 823 824
  /* REG_EQUAL may get simplified into register.
     We don't allow that. Remove that note. This code ought
     not to happen, because previous code ought to synthesize
     reg-reg move, but be on the safe side.  */
  if (note && REG_NOTE_KIND (note) == REG_EQUAL && REG_P (XEXP (note, 0)))
    remove_note (insn, note);

  return success;
}

825 826 827 828
/* Find a set of REGNOs that are available on entry to INSN's block.  If found,
   SET_RET[0] will be assigned a set with a register source and SET_RET[1] a
   set with a constant source.  If not found the corresponding entry is set to
   NULL.  */
829

830 831
static void
find_avail_set (int regno, rtx_insn *insn, struct cprop_expr *set_ret[2])
832
{
833
  set_ret[0] = set_ret[1] = NULL;
834 835 836 837 838 839 840 841 842 843 844 845 846

  /* Loops are not possible here.  To get a loop we would need two sets
     available at the start of the block containing INSN.  i.e. we would
     need two sets like this available at the start of the block:

       (set (reg X) (reg Y))
       (set (reg Y) (reg X))

     This can not happen since the set of (reg Y) would have killed the
     set of (reg X) making it unavailable at the start of this block.  */
  while (1)
    {
      rtx src;
847
      struct cprop_expr *set = lookup_set (regno, &set_hash_table);
848 849 850 851 852

      /* Find a set that is available at the start of the block
	 which contains INSN.  */
      while (set)
	{
853
	  if (bitmap_bit_p (cprop_avin[BLOCK_FOR_INSN (insn)->index],
854 855 856 857 858 859 860 861 862 863
			set->bitmap_index))
	    break;
	  set = next_set (regno, set);
	}

      /* If no available set was found we've reached the end of the
	 (possibly empty) copy chain.  */
      if (set == 0)
	break;

864
      src = set->src;
865 866 867 868 869 870 871 872

      /* We know the set is available.
	 Now check that SRC is locally anticipatable (i.e. none of the
	 source operands have changed since the start of the block).

         If the source operand changed, we may still use it for the next
         iteration of this loop, but we may not use it for substitutions.  */

873 874 875 876
      if (cprop_constant_p (src))
	set_ret[1] = set;
      else if (reg_not_set_p (src, insn))
	set_ret[0] = set;
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892

      /* If the source of the set is anything except a register, then
	 we have reached the end of the copy chain.  */
      if (! REG_P (src))
	break;

      /* Follow the copy chain, i.e. start another iteration of the loop
	 and see if we have an available copy into SRC.  */
      regno = REGNO (src);
    }
}

/* Subroutine of cprop_insn that tries to propagate constants into
   JUMP_INSNS.  JUMP must be a conditional jump.  If SETCC is non-NULL
   it is the instruction that immediately precedes JUMP, and must be a
   single SET of a register.  FROM is what we will try to replace,
893
   SRC is the constant we will try to substitute for it.  Return nonzero
894 895 896
   if a change was made.  */

static int
David Malcolm committed
897
cprop_jump (basic_block bb, rtx_insn *setcc, rtx_insn *jump, rtx from, rtx src)
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
{
  rtx new_rtx, set_src, note_src;
  rtx set = pc_set (jump);
  rtx note = find_reg_equal_equiv_note (jump);

  if (note)
    {
      note_src = XEXP (note, 0);
      if (GET_CODE (note_src) == EXPR_LIST)
	note_src = NULL_RTX;
    }
  else note_src = NULL_RTX;

  /* Prefer REG_EQUAL notes except those containing EXPR_LISTs.  */
  set_src = note_src ? note_src : SET_SRC (set);

  /* First substitute the SETCC condition into the JUMP instruction,
     then substitute that given values into this expanded JUMP.  */
  if (setcc != NULL_RTX
      && !modified_between_p (from, setcc, jump)
      && !modified_between_p (src, setcc, jump))
    {
      rtx setcc_src;
      rtx setcc_set = single_set (setcc);
      rtx setcc_note = find_reg_equal_equiv_note (setcc);
      setcc_src = (setcc_note && GET_CODE (XEXP (setcc_note, 0)) != EXPR_LIST)
		? XEXP (setcc_note, 0) : SET_SRC (setcc_set);
      set_src = simplify_replace_rtx (set_src, SET_DEST (setcc_set),
				      setcc_src);
    }
  else
David Malcolm committed
929
    setcc = NULL;
930 931 932 933 934 935 936 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 962 963 964 965 966 967

  new_rtx = simplify_replace_rtx (set_src, from, src);

  /* If no simplification can be made, then try the next register.  */
  if (rtx_equal_p (new_rtx, SET_SRC (set)))
    return 0;

  /* If this is now a no-op delete it, otherwise this must be a valid insn.  */
  if (new_rtx == pc_rtx)
    delete_insn (jump);
  else
    {
      /* Ensure the value computed inside the jump insn to be equivalent
         to one computed by setcc.  */
      if (setcc && modified_in_p (new_rtx, setcc))
	return 0;
      if (! validate_unshare_change (jump, &SET_SRC (set), new_rtx, 0))
	{
	  /* When (some) constants are not valid in a comparison, and there
	     are two registers to be replaced by constants before the entire
	     comparison can be folded into a constant, we need to keep
	     intermediate information in REG_EQUAL notes.  For targets with
	     separate compare insns, such notes are added by try_replace_reg.
	     When we have a combined compare-and-branch instruction, however,
	     we need to attach a note to the branch itself to make this
	     optimization work.  */

	  if (!rtx_equal_p (new_rtx, note_src))
	    set_unique_reg_note (jump, REG_EQUAL, copy_rtx (new_rtx));
	  return 0;
	}

      /* Remove REG_EQUAL note after simplification.  */
      if (note_src)
	remove_note (jump, note);
     }

  /* Delete the cc0 setter.  */
968
  if (HAVE_cc0 && setcc != NULL && CC0_P (SET_DEST (single_set (setcc))))
969 970 971 972 973 974
    delete_insn (setcc);

  global_const_prop_count++;
  if (dump_file != NULL)
    {
      fprintf (dump_file,
975
	       "GLOBAL CONST-PROP: Replacing reg %d in jump_insn %d with "
976
	       "constant ", REGNO (from), INSN_UID (jump));
977 978 979 980 981 982 983 984 985 986 987 988 989
      print_rtl (dump_file, src);
      fprintf (dump_file, "\n");
    }
  purge_dead_edges (bb);

  /* If a conditional jump has been changed into unconditional jump, remove
     the jump and make the edge fallthru - this is always called in
     cfglayout mode.  */
  if (new_rtx != pc_rtx && simplejump_p (jump))
    {
      edge e;
      edge_iterator ei;

990
      FOR_EACH_EDGE (e, ei, bb->succs)
991
	if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
992 993 994 995 996 997 998 999 1000 1001 1002
	    && BB_HEAD (e->dest) == JUMP_LABEL (jump))
	  {
	    e->flags |= EDGE_FALLTHRU;
	    break;
	  }
      delete_insn (jump);
    }

  return 1;
}

1003 1004 1005 1006 1007
/* Subroutine of cprop_insn that tries to propagate constants.  FROM is what
   we will try to replace, SRC is the constant we will try to substitute for
   it and INSN is the instruction where this will be happening.  */

static int
David Malcolm committed
1008
constprop_register (rtx from, rtx src, rtx_insn *insn)
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
{
  rtx sset;

  /* Check for reg or cc0 setting instructions followed by
     conditional branch instructions first.  */
  if ((sset = single_set (insn)) != NULL
      && NEXT_INSN (insn)
      && any_condjump_p (NEXT_INSN (insn)) && onlyjump_p (NEXT_INSN (insn)))
    {
      rtx dest = SET_DEST (sset);
      if ((REG_P (dest) || CC0_P (dest))
1020 1021
	  && cprop_jump (BLOCK_FOR_INSN (insn), insn, NEXT_INSN (insn),
			 from, src))
1022 1023 1024 1025
	return 1;
    }

  /* Handle normal insns next.  */
1026
  if (NONJUMP_INSN_P (insn) && try_replace_reg (from, src, insn))
1027 1028 1029 1030 1031 1032 1033 1034 1035
    return 1;

  /* Try to propagate a CONST_INT into a conditional jump.
     We're pretty specific about what we will handle in this
     code, we can extend this as necessary over time.

     Right now the insn in question must look like
     (set (pc) (if_then_else ...))  */
  else if (any_condjump_p (insn) && onlyjump_p (insn))
1036
    return cprop_jump (BLOCK_FOR_INSN (insn), NULL, insn, from, src);
1037 1038 1039 1040
  return 0;
}

/* Perform constant and copy propagation on INSN.
1041
   Return nonzero if a change was made.  */
1042 1043

static int
David Malcolm committed
1044
cprop_insn (rtx_insn *insn)
1045
{
1046 1047
  unsigned i;
  int changed = 0, changed_this_round;
1048 1049
  rtx note;

1050
  do
1051
    {
1052 1053 1054
      changed_this_round = 0;
      reg_use_count = 0;
      note_uses (&PATTERN (insn), find_used_regs, NULL);
1055

1056 1057 1058 1059
      /* We may win even when propagating constants into notes.  */
      note = find_reg_equal_equiv_note (insn);
      if (note)
	find_used_regs (&XEXP (note, 0), NULL);
1060

1061
      for (i = 0; i < reg_use_count; i++)
1062
	{
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
	  rtx reg_used = reg_use_table[i];
	  unsigned int regno = REGNO (reg_used);
	  rtx src_cst = NULL, src_reg = NULL;
	  struct cprop_expr *set[2];

	  /* If the register has already been set in this block, there's
	     nothing we can do.  */
	  if (! reg_not_set_p (reg_used, insn))
	    continue;

	  /* Find an assignment that sets reg_used and is available
	     at the start of the block.  */
	  find_avail_set (regno, insn, set);
	  if (set[0])
	    src_reg = set[0]->src;
	  if (set[1])
	    src_cst = set[1]->src;

	  /* Constant propagation.  */
	  if (src_cst && cprop_constant_p (src_cst)
	      && constprop_register (reg_used, src_cst, insn))
1084
	    {
1085
	      changed_this_round = changed = 1;
1086 1087 1088
	      global_const_prop_count++;
	      if (dump_file != NULL)
		{
1089 1090 1091 1092
		  fprintf (dump_file,
			   "GLOBAL CONST-PROP: Replacing reg %d in ", regno);
		  fprintf (dump_file, "insn %d with constant ",
			   INSN_UID (insn));
1093
		  print_rtl (dump_file, src_cst);
1094 1095
		  fprintf (dump_file, "\n");
		}
1096
	      if (insn->deleted ())
1097 1098
		return 1;
	    }
1099 1100 1101 1102
	  /* Copy propagation.  */
	  else if (src_reg && cprop_reg_p (src_reg)
		   && REGNO (src_reg) != regno
		   && try_replace_reg (reg_used, src_reg, insn))
1103
	    {
1104
	      changed_this_round = changed = 1;
1105 1106 1107
	      global_copy_prop_count++;
	      if (dump_file != NULL)
		{
1108 1109
		  fprintf (dump_file,
			   "GLOBAL COPY-PROP: Replacing reg %d in insn %d",
1110
			   regno, INSN_UID (insn));
1111
		  fprintf (dump_file, " with reg %d\n", REGNO (src_reg));
1112 1113 1114
		}

	      /* The original insn setting reg_used may or may not now be
1115
		 deletable.  We leave the deletion to DCE.  */
1116 1117 1118 1119 1120 1121
	      /* FIXME: If it turns out that the insn isn't deletable,
		 then we may have unnecessarily extended register lifetimes
		 and made things worse.  */
	    }
	}
    }
1122 1123 1124
  /* If try_replace_reg simplified the insn, the regs found by find_used_regs
     may not be valid anymore.  Start over.  */
  while (changed_this_round);
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163

  if (changed && DEBUG_INSN_P (insn))
    return 0;

  return changed;
}

/* Like find_used_regs, but avoid recording uses that appear in
   input-output contexts such as zero_extract or pre_dec.  This
   restricts the cases we consider to those for which local cprop
   can legitimately make replacements.  */

static void
local_cprop_find_used_regs (rtx *xptr, void *data)
{
  rtx x = *xptr;

  if (x == 0)
    return;

  switch (GET_CODE (x))
    {
    case ZERO_EXTRACT:
    case SIGN_EXTRACT:
    case STRICT_LOW_PART:
      return;

    case PRE_DEC:
    case PRE_INC:
    case POST_DEC:
    case POST_INC:
    case PRE_MODIFY:
    case POST_MODIFY:
      /* Can only legitimately appear this early in the context of
	 stack pushes for function arguments, but handle all of the
	 codes nonetheless.  */
      return;

    case SUBREG:
1164
      if (df_read_modify_subreg_p (x))
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
	return;
      break;

    default:
      break;
    }

  find_used_regs (xptr, data);
}

/* Try to perform local const/copy propagation on X in INSN.  */

static bool
David Malcolm committed
1178
do_local_cprop (rtx x, rtx_insn *insn)
1179 1180 1181 1182
{
  rtx newreg = NULL, newcnst = NULL;

  /* Rule out USE instructions and ASM statements as we don't want to
Alan Modra committed
1183
     change the hard registers mentioned.  */
1184
  if (REG_P (x)
1185
      && (cprop_reg_p (x)
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
          || (GET_CODE (PATTERN (insn)) != USE
	      && asm_noperands (PATTERN (insn)) < 0)))
    {
      cselib_val *val = cselib_lookup (x, GET_MODE (x), 0, VOIDmode);
      struct elt_loc_list *l;

      if (!val)
	return false;
      for (l = val->locs; l; l = l->next)
	{
	  rtx this_rtx = l->loc;
	  rtx note;

1199
	  if (cprop_constant_p (this_rtx))
1200
	    newcnst = this_rtx;
1201
	  if (cprop_reg_p (this_rtx)
1202 1203 1204 1205 1206 1207 1208 1209 1210
	      /* Don't copy propagate if it has attached REG_EQUIV note.
		 At this point this only function parameters should have
		 REG_EQUIV notes and if the argument slot is used somewhere
		 explicitly, it means address of parameter has been taken,
		 so we should not extend the lifetime of the pseudo.  */
	      && (!(note = find_reg_note (l->setting_insn, REG_EQUIV, NULL_RTX))
		  || ! MEM_P (XEXP (note, 0))))
	    newreg = this_rtx;
	}
1211
      if (newcnst && constprop_register (x, newcnst, insn))
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
	{
	  if (dump_file != NULL)
	    {
	      fprintf (dump_file, "LOCAL CONST-PROP: Replacing reg %d in ",
		       REGNO (x));
	      fprintf (dump_file, "insn %d with constant ",
		       INSN_UID (insn));
	      print_rtl (dump_file, newcnst);
	      fprintf (dump_file, "\n");
	    }
	  local_const_prop_count++;
	  return true;
	}
      else if (newreg && newreg != x && try_replace_reg (x, newreg, insn))
	{
	  if (dump_file != NULL)
	    {
	      fprintf (dump_file,
		       "LOCAL COPY-PROP: Replacing reg %d in insn %d",
		       REGNO (x), INSN_UID (insn));
	      fprintf (dump_file, " with reg %d\n", REGNO (newreg));
	    }
	  local_copy_prop_count++;
	  return true;
	}
    }
  return false;
}

/* Do local const/copy propagation (i.e. within each basic block).  */

static int
local_cprop_pass (void)
{
  basic_block bb;
David Malcolm committed
1247
  rtx_insn *insn;
1248
  bool changed = false;
1249
  unsigned i;
1250

1251 1252
  auto_vec<rtx_insn *> uncond_traps;

1253
  cselib_init (0);
1254
  FOR_EACH_BB_FN (bb, cfun)
1255 1256 1257 1258 1259
    {
      FOR_BB_INSNS (bb, insn)
	{
	  if (INSN_P (insn))
	    {
1260 1261 1262
	      bool was_uncond_trap
		= (GET_CODE (PATTERN (insn)) == TRAP_IF
		   && XEXP (PATTERN (insn), 0) == const1_rtx);
1263 1264 1265 1266 1267 1268 1269 1270 1271
	      rtx note = find_reg_equal_equiv_note (insn);
	      do
		{
		  reg_use_count = 0;
		  note_uses (&PATTERN (insn), local_cprop_find_used_regs,
			     NULL);
		  if (note)
		    local_cprop_find_used_regs (&XEXP (note, 0), NULL);

1272
		  for (i = 0; i < reg_use_count; i++)
1273
		    {
1274
		      if (do_local_cprop (reg_use_table[i], insn))
1275
			{
1276 1277
			  if (!DEBUG_INSN_P (insn))
			    changed = true;
1278 1279 1280
			  break;
			}
		    }
1281 1282 1283 1284 1285 1286 1287
		  if (!was_uncond_trap
		      && GET_CODE (PATTERN (insn)) == TRAP_IF
		      && XEXP (PATTERN (insn), 0) == const1_rtx)
		    {
		      uncond_traps.safe_push (insn);
		      break;
		    }
1288
		  if (insn->deleted ())
1289 1290
		    break;
		}
1291
	      while (i < reg_use_count);
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
	    }
	  cselib_process_insn (insn);
	}

      /* Forget everything at the end of a basic block.  */
      cselib_clear_table ();
    }

  cselib_finish ();

1302 1303 1304 1305 1306 1307 1308 1309
  while (!uncond_traps.is_empty ())
    {
      rtx_insn *insn = uncond_traps.pop ();
      basic_block to_split = BLOCK_FOR_INSN (insn);
      remove_edge (split_block (to_split, insn));
      emit_barrier_after_bb (to_split);
    }

1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
  return changed;
}

/* Similar to get_condition, only the resulting condition must be
   valid at JUMP, instead of at EARLIEST.

   This differs from noce_get_condition in ifcvt.c in that we prefer not to
   settle for the condition variable in the jump instruction being integral.
   We prefer to be able to record the value of a user variable, rather than
   the value of a temporary used in a condition.  This could be solved by
   recording the value of *every* register scanned by canonicalize_condition,
   but this would require some code reorganization.  */

rtx
1324
fis_get_condition (rtx_insn *jump)
1325 1326 1327 1328
{
  return get_condition (jump, NULL, false, true);
}

1329 1330
/* Check the comparison COND to see if we can safely form an implicit
   set from it.  */
1331 1332 1333 1334

static bool
implicit_set_cond_p (const_rtx cond)
{
1335
  machine_mode mode;
1336 1337 1338 1339 1340 1341
  rtx cst;

  /* COND must be either an EQ or NE comparison.  */
  if (GET_CODE (cond) != EQ && GET_CODE (cond) != NE)
    return false;

1342 1343
  /* The first operand of COND must be a register we can propagate.  */
  if (!cprop_reg_p (XEXP (cond, 0)))
1344 1345 1346 1347 1348
    return false;

  /* The second operand of COND must be a suitable constant.  */
  mode = GET_MODE (XEXP (cond, 0));
  cst = XEXP (cond, 1);
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358

  /* We can't perform this optimization if either operand might be or might
     contain a signed zero.  */
  if (HONOR_SIGNED_ZEROS (mode))
    {
      /* It is sufficient to check if CST is or contains a zero.  We must
	 handle float, complex, and vector.  If any subpart is a zero, then
	 the optimization can't be performed.  */
      /* ??? The complex and vector checks are not implemented yet.  We just
	 always return zero for them.  */
1359 1360 1361
      if (CONST_DOUBLE_AS_FLOAT_P (cst)
	  && real_equal (CONST_DOUBLE_REAL_VALUE (cst), &dconst0))
	return 0;
1362 1363 1364 1365
      else
	return 0;
    }

1366
  return cprop_constant_p (cst);
1367 1368 1369 1370 1371 1372 1373 1374 1375
}

/* Find the implicit sets of a function.  An "implicit set" is a constraint
   on the value of a variable, implied by a conditional jump.  For example,
   following "if (x == 2)", the then branch may be optimized as though the
   conditional performed an "explicit set", in this example, "x = 2".  This
   function records the set patterns that are implicit at the start of each
   basic block.

1376 1377
   If an implicit set is found but the set is implicit on a critical edge,
   this critical edge is split.
1378

1379 1380 1381
   Return true if the CFG was modified, false otherwise.  */

static bool
1382 1383 1384 1385
find_implicit_sets (void)
{
  basic_block bb, dest;
  rtx cond, new_rtx;
1386 1387
  unsigned int count = 0;
  bool edges_split = false;
1388
  size_t implicit_sets_size = last_basic_block_for_fn (cfun) + 10;
1389 1390

  implicit_sets = XCNEWVEC (rtx, implicit_sets_size);
1391

1392
  FOR_EACH_BB_FN (bb, cfun)
1393 1394
    {
      /* Check for more than one successor.  */
1395
      if (EDGE_COUNT (bb->succs) <= 1)
1396
	continue;
1397

1398
      cond = fis_get_condition (BB_END (bb));
1399

1400 1401 1402 1403 1404 1405 1406 1407 1408
      /* If no condition is found or if it isn't of a suitable form,
	 ignore it.  */
      if (! cond || ! implicit_set_cond_p (cond))
	continue;

      dest = GET_CODE (cond) == EQ
	? BRANCH_EDGE (bb)->dest : FALLTHRU_EDGE (bb)->dest;

      /* If DEST doesn't go anywhere, ignore it.  */
1409
      if (! dest || dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
	continue;

      /* We have found a suitable implicit set.  Try to record it now as
	 a SET in DEST.  If DEST has more than one predecessor, the edge
	 between BB and DEST is a critical edge and we must split it,
	 because we can only record one implicit set per DEST basic block.  */
      if (! single_pred_p (dest))
        {
	  dest = split_edge (find_edge (bb, dest));
	  edges_split = true;
	}

      if (implicit_sets_size <= (size_t) dest->index)
      {
        size_t old_implicit_sets_size = implicit_sets_size;
	implicit_sets_size *= 2;
	implicit_sets = XRESIZEVEC (rtx, implicit_sets, implicit_sets_size);
	memset (implicit_sets + old_implicit_sets_size, 0,
		(implicit_sets_size - old_implicit_sets_size) * sizeof (rtx));
1429 1430
      }

1431
      new_rtx = gen_rtx_SET (XEXP (cond, 0), XEXP (cond, 1));
1432 1433 1434
      implicit_sets[dest->index] = new_rtx;
      if (dump_file)
	{
1435 1436 1437
	  fprintf (dump_file, "Implicit set of reg %d in ",
		   REGNO (XEXP (cond, 0)));
	  fprintf (dump_file, "basic block %d\n", dest->index);
1438 1439 1440 1441
	}
      count++;
    }

1442 1443
  if (dump_file)
    fprintf (dump_file, "Found %d implicit sets\n", count);
1444 1445 1446

  /* Confess our sins.  */
  return edges_split;
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
}

/* Bypass conditional jumps.  */

/* The value of last_basic_block at the beginning of the jump_bypass
   pass.  The use of redirect_edge_and_branch_force may introduce new
   basic blocks, but the data flow analysis is only valid for basic
   block indices less than bypass_last_basic_block.  */

static int bypass_last_basic_block;

/* Find a set of REGNO to a constant that is available at the end of basic
1459
   block BB.  Return NULL if no such set is found.  Based heavily upon
1460 1461
   find_avail_set.  */

1462
static struct cprop_expr *
1463 1464
find_bypass_set (int regno, int bb)
{
1465
  struct cprop_expr *result = 0;
1466 1467 1468 1469

  for (;;)
    {
      rtx src;
1470
      struct cprop_expr *set = lookup_set (regno, &set_hash_table);
1471 1472 1473

      while (set)
	{
1474
	  if (bitmap_bit_p (cprop_avout[bb], set->bitmap_index))
1475 1476 1477 1478 1479 1480 1481
	    break;
	  set = next_set (regno, set);
	}

      if (set == 0)
	break;

1482
      src = set->src;
1483
      if (cprop_constant_p (src))
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
	result = set;

      if (! REG_P (src))
	break;

      regno = REGNO (src);
    }
  return result;
}

/* Subroutine of bypass_block that checks whether a pseudo is killed by
   any of the instructions inserted on an edge.  Jump bypassing places
   condition code setters on CFG edges using insert_insn_on_edge.  This
   function is required to check that our data flow analysis is still
   valid prior to commit_edge_insertions.  */

static bool
reg_killed_on_edge (const_rtx reg, const_edge e)
{
1503
  rtx_insn *insn;
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522

  for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
    if (INSN_P (insn) && reg_set_p (reg, insn))
      return true;

  return false;
}

/* Subroutine of bypass_conditional_jumps that attempts to bypass the given
   basic block BB which has more than one predecessor.  If not NULL, SETCC
   is the first instruction of BB, which is immediately followed by JUMP_INSN
   JUMP.  Otherwise, SETCC is NULL, and JUMP is the first insn of BB.
   Returns nonzero if a change was made.

   During the jump bypassing pass, we may place copies of SETCC instructions
   on CFG edges.  The following routine must be careful to pay attention to
   these inserted insns when performing its transformations.  */

static int
David Malcolm committed
1523
bypass_block (basic_block bb, rtx_insn *setcc, rtx_insn *jump)
1524
{
David Malcolm committed
1525 1526
  rtx_insn *insn;
  rtx note;
1527
  edge e, edest;
1528
  int change;
1529
  int may_be_loop_header = false;
1530
  unsigned removed_p;
1531
  unsigned i;
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
  edge_iterator ei;

  insn = (setcc != NULL) ? setcc : jump;

  /* Determine set of register uses in INSN.  */
  reg_use_count = 0;
  note_uses (&PATTERN (insn), find_used_regs, NULL);
  note = find_reg_equal_equiv_note (insn);
  if (note)
    find_used_regs (&XEXP (note, 0), NULL);

1543 1544
  if (current_loops)
    {
1545 1546 1547 1548
      /* If we are to preserve loop structure then do not bypass
         a loop header.  This will either rotate the loop, create
	 multiple entry loops or even irreducible regions.  */
      if (bb == bb->loop_father->header)
1549 1550 1551 1552 1553 1554
	return 0;
    }
  else
    {
      FOR_EACH_EDGE (e, ei, bb->preds)
	if (e->flags & EDGE_DFS_BACK)
1555 1556 1557 1558
	  {
	    may_be_loop_header = true;
	    break;
	  }
1559
    }
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579

  change = 0;
  for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
    {
      removed_p = 0;

      if (e->flags & EDGE_COMPLEX)
	{
	  ei_next (&ei);
	  continue;
	}

      /* We can't redirect edges from new basic blocks.  */
      if (e->src->index >= bypass_last_basic_block)
	{
	  ei_next (&ei);
	  continue;
	}

      /* The irreducible loops created by redirecting of edges entering the
1580 1581
	 loop from outside would decrease effectiveness of some of the
	 following optimizations, so prevent this.  */
1582 1583 1584 1585 1586 1587 1588 1589 1590
      if (may_be_loop_header
	  && !(e->flags & EDGE_DFS_BACK))
	{
	  ei_next (&ei);
	  continue;
	}

      for (i = 0; i < reg_use_count; i++)
	{
1591 1592
	  rtx reg_used = reg_use_table[i];
	  unsigned int regno = REGNO (reg_used);
1593
	  basic_block dest, old_dest;
1594
	  struct cprop_expr *set;
1595 1596 1597 1598 1599 1600 1601 1602
	  rtx src, new_rtx;

	  set = find_bypass_set (regno, e->src->index);

	  if (! set)
	    continue;

	  /* Check the data flow is valid after edge insertions.  */
1603
	  if (e->insns.r && reg_killed_on_edge (reg_used, e))
1604 1605 1606 1607 1608 1609 1610 1611 1612
	    continue;

	  src = SET_SRC (pc_set (jump));

	  if (setcc != NULL)
	    src = simplify_replace_rtx (src,
					SET_DEST (PATTERN (setcc)),
					SET_SRC (PATTERN (setcc)));

1613
	  new_rtx = simplify_replace_rtx (src, reg_used, set->src);
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644

	  /* Jump bypassing may have already placed instructions on
	     edges of the CFG.  We can't bypass an outgoing edge that
	     has instructions associated with it, as these insns won't
	     get executed if the incoming edge is redirected.  */
	  if (new_rtx == pc_rtx)
	    {
	      edest = FALLTHRU_EDGE (bb);
	      dest = edest->insns.r ? NULL : edest->dest;
	    }
	  else if (GET_CODE (new_rtx) == LABEL_REF)
	    {
	      dest = BLOCK_FOR_INSN (XEXP (new_rtx, 0));
	      /* Don't bypass edges containing instructions.  */
	      edest = find_edge (bb, dest);
	      if (edest && edest->insns.r)
		dest = NULL;
	    }
	  else
	    dest = NULL;

	  /* Avoid unification of the edge with other edges from original
	     branch.  We would end up emitting the instruction on "both"
	     edges.  */
	  if (dest && setcc && !CC0_P (SET_DEST (PATTERN (setcc)))
	      && find_edge (e->src, dest))
	    dest = NULL;

	  old_dest = e->dest;
	  if (dest != NULL
	      && dest != old_dest
1645
	      && dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662
            {
	      redirect_edge_and_branch_force (e, dest);

	      /* Copy the register setter to the redirected edge.
		 Don't copy CC0 setters, as CC0 is dead after jump.  */
	      if (setcc)
		{
		  rtx pat = PATTERN (setcc);
		  if (!CC0_P (SET_DEST (pat)))
		    insert_insn_on_edge (copy_insn (pat), e);
		}

	      if (dump_file != NULL)
		{
		  fprintf (dump_file, "JUMP-BYPASS: Proved reg %d "
				      "in jump_insn %d equals constant ",
			   regno, INSN_UID (jump));
1663
		  print_rtl (dump_file, set->src);
Marek Polacek committed
1664 1665 1666 1667
		  fprintf (dump_file, "\n\t     when BB %d is entered from "
				      "BB %d.  Redirect edge %d->%d to %d.\n",
			   old_dest->index, e->src->index, e->src->index,
			   old_dest->index, dest->index);
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
		}
	      change = 1;
	      removed_p = 1;
	      break;
	    }
	}
      if (!removed_p)
	ei_next (&ei);
    }
  return change;
}

/* Find basic blocks with more than one predecessor that only contain a
   single conditional jump.  If the result of the comparison is known at
   compile-time from any incoming edge, redirect that edge to the
1683
   appropriate target.  Return nonzero if a change was made.
1684 1685 1686 1687 1688 1689 1690 1691

   This function is now mis-named, because we also handle indirect jumps.  */

static int
bypass_conditional_jumps (void)
{
  basic_block bb;
  int changed;
David Malcolm committed
1692 1693
  rtx_insn *setcc;
  rtx_insn *insn;
1694 1695 1696
  rtx dest;

  /* Note we start at block 1.  */
1697
  if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1698 1699 1700 1701 1702
    return 0;

  mark_dfs_back_edges ();

  changed = 0;
1703 1704
  FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
		  EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
1705 1706 1707 1708
    {
      /* Check for more than one predecessor.  */
      if (!single_pred_p (bb))
	{
David Malcolm committed
1709
	  setcc = NULL;
1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
	  FOR_BB_INSNS (bb, insn)
	    if (DEBUG_INSN_P (insn))
	      continue;
	    else if (NONJUMP_INSN_P (insn))
	      {
		if (setcc)
		  break;
		if (GET_CODE (PATTERN (insn)) != SET)
		  break;

		dest = SET_DEST (PATTERN (insn));
		if (REG_P (dest) || CC0_P (dest))
		  setcc = insn;
		else
		  break;
	      }
	    else if (JUMP_P (insn))
	      {
		if ((any_condjump_p (insn) || computed_jump_p (insn))
		    && onlyjump_p (insn))
		  changed |= bypass_block (bb, setcc, insn);
		break;
	      }
	    else if (INSN_P (insn))
	      break;
	}
    }

  /* If we bypassed any register setting insns, we inserted a
     copy on the redirected edge.  These need to be committed.  */
  if (changed)
    commit_edge_insertions ();

  return changed;
}

/* Main function for the CPROP pass.  */

static int
one_cprop_pass (void)
{
1751
  int i;
1752 1753 1754
  int changed = 0;

  /* Return if there's nothing to do, or it is too expensive.  */
1755
  if (n_basic_blocks_for_fn (cfun) <= NUM_FIXED_BLOCKS + 1
1756
      || gcse_or_cprop_is_too_expensive (_ ("const/copy propagation disabled")))
1757 1758 1759 1760 1761 1762
    return 0;

  global_const_prop_count = local_const_prop_count = 0;
  global_copy_prop_count = local_copy_prop_count = 0;

  bytes_used = 0;
1763
  gcc_obstack_init (&cprop_obstack);
1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777

  /* Do a local const/copy propagation pass first.  The global pass
     only handles global opportunities.
     If the local pass changes something, remove any unreachable blocks
     because the CPROP global dataflow analysis may get into infinite
     loops for CFGs with unreachable blocks.

     FIXME: This local pass should not be necessary after CSE (but for
	    some reason it still is).  It is also (proven) not necessary
	    to run the local pass right after FWPWOP.

     FIXME: The global analysis would not get into infinite loops if it
	    would use the DF solver (via df_simple_dataflow) instead of
	    the solver implemented in this file.  */
1778 1779
  changed |= local_cprop_pass ();
  if (changed)
1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797
    delete_unreachable_blocks ();

  /* Determine implicit sets.  This may change the CFG (split critical
     edges if that exposes an implicit set).
     Note that find_implicit_sets() does not rely on up-to-date DF caches
     so that we do not have to re-run df_analyze() even if local CPROP
     changed something.
     ??? This could run earlier so that any uncovered implicit sets
	 sets could be exploited in local_cprop_pass() also.  Later.  */
  changed |= find_implicit_sets ();

  /* If local_cprop_pass() or find_implicit_sets() changed something,
     run df_analyze() to bring all insn caches up-to-date, and to take
     new basic blocks from edge splitting on the DF radar.
     NB: This also runs the fast DCE pass, because execute_rtl_cprop
     sets DF_LR_RUN_DCE.  */
  if (changed)
    df_analyze ();
1798

1799
  /* Initialize implicit_set_indexes array.  */
1800 1801
  implicit_set_indexes = XNEWVEC (int, last_basic_block_for_fn (cfun));
  for (i = 0; i < last_basic_block_for_fn (cfun); i++)
1802 1803
    implicit_set_indexes[i] = -1;

1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
  alloc_hash_table (&set_hash_table);
  compute_hash_table (&set_hash_table);

  /* Free implicit_sets before peak usage.  */
  free (implicit_sets);
  implicit_sets = NULL;

  if (dump_file)
    dump_hash_table (dump_file, "SET", &set_hash_table);
  if (set_hash_table.n_elems > 0)
    {
      basic_block bb;
1816
      auto_vec<rtx_insn *> uncond_traps;
1817

1818 1819
      alloc_cprop_mem (last_basic_block_for_fn (cfun),
		       set_hash_table.n_elems);
1820 1821
      compute_cprop_data ();

1822 1823 1824
      free (implicit_set_indexes);
      implicit_set_indexes = NULL;

1825 1826 1827
      /* Allocate vars to track sets of regs.  */
      reg_set_bitmap = ALLOC_REG_SET (NULL);

1828 1829
      FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
		      EXIT_BLOCK_PTR_FOR_FN (cfun),
1830
		      next_bb)
1831
	{
1832 1833 1834
	  bool seen_uncond_trap = false;
	  rtx_insn *insn;

1835 1836 1837 1838 1839 1840 1841
	  /* Reset tables used to keep track of what's still valid [since
	     the start of the block].  */
	  reset_opr_set_tables ();

	  FOR_BB_INSNS (bb, insn)
	    if (INSN_P (insn))
	      {
1842 1843 1844 1845
		bool was_uncond_trap
		  = (GET_CODE (PATTERN (insn)) == TRAP_IF
		     && XEXP (PATTERN (insn), 0) == const1_rtx);

1846 1847 1848 1849 1850
		changed |= cprop_insn (insn);

		/* Keep track of everything modified by this insn.  */
		/* ??? Need to be careful w.r.t. mods done to INSN.
		       Don't call mark_oprs_set if we turned the
1851
		       insn into a NOTE, or deleted the insn.  */
1852
		if (! NOTE_P (insn) && ! insn->deleted ())
1853
		  mark_oprs_set (insn);
1854

1855
		if (!was_uncond_trap
1856 1857 1858
		    && GET_CODE (PATTERN (insn)) == TRAP_IF
		    && XEXP (PATTERN (insn), 0) == const1_rtx)
		  {
1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870
		    /* If we have already seen an unconditional trap
		       earlier, the rest of the bb is going to be removed
		       as unreachable.  Just turn it into a note, so that
		       RTL verification doesn't complain about it before
		       it is finally removed.  */
		    if (seen_uncond_trap)
		      set_insn_deleted (insn);
		    else
		      {
			seen_uncond_trap = true;
			uncond_traps.safe_push (insn);
		      }
1871
		  }
1872 1873 1874
	      }
	}

1875 1876 1877 1878 1879
      /* Make sure bypass_conditional_jumps will ignore not just its new
	 basic blocks, but also the ones after unconditional traps (those are
	 unreachable and will be eventually removed as such).  */
      bypass_last_basic_block = last_basic_block_for_fn (cfun);

1880 1881 1882 1883 1884 1885 1886 1887
      while (!uncond_traps.is_empty ())
	{
	  rtx_insn *insn = uncond_traps.pop ();
	  basic_block to_split = BLOCK_FOR_INSN (insn);
	  remove_edge (split_block (to_split, insn));
	  emit_barrier_after_bb (to_split);
	}

1888 1889
      changed |= bypass_conditional_jumps ();

1890
      FREE_REG_SET (reg_set_bitmap);
1891 1892
      free_cprop_mem ();
    }
1893 1894 1895 1896 1897
  else
    {
      free (implicit_set_indexes);
      implicit_set_indexes = NULL;
    }
1898 1899

  free_hash_table (&set_hash_table);
1900
  obstack_free (&cprop_obstack, NULL);
1901 1902 1903 1904

  if (dump_file)
    {
      fprintf (dump_file, "CPROP of %s, %d basic blocks, %d bytes needed, ",
1905 1906
	       current_function_name (), n_basic_blocks_for_fn (cfun),
	       bytes_used);
1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934
      fprintf (dump_file, "%d local const props, %d local copy props, ",
	       local_const_prop_count, local_copy_prop_count);
      fprintf (dump_file, "%d global const props, %d global copy props\n\n",
	       global_const_prop_count, global_copy_prop_count);
    }

  return changed;
}

/* All the passes implemented in this file.  Each pass has its
   own gate and execute function, and at the end of the file a
   pass definition for passes.c.

   We do not construct an accurate cfg in functions which call
   setjmp, so none of these passes runs if the function calls
   setjmp.
   FIXME: Should just handle setjmp via REG_SETJMP notes.  */

static unsigned int
execute_rtl_cprop (void)
{
  int changed;
  delete_unreachable_blocks ();
  df_set_flags (DF_LR_RUN_DCE);
  df_analyze ();
  changed = one_cprop_pass ();
  flag_rerun_cse_after_global_opts |= changed;
  if (changed)
1935
    cleanup_cfg (CLEANUP_CFG_CHANGED);
1936 1937 1938
  return 0;
}

1939 1940 1941
namespace {

const pass_data pass_data_rtl_cprop =
1942
{
1943 1944 1945 1946 1947 1948 1949 1950
  RTL_PASS, /* type */
  "cprop", /* name */
  OPTGROUP_NONE, /* optinfo_flags */
  TV_CPROP, /* tv_id */
  PROP_cfglayout, /* properties_required */
  0, /* properties_provided */
  0, /* properties_destroyed */
  0, /* todo_flags_start */
1951
  TODO_df_finish, /* todo_flags_finish */
1952
};
1953 1954 1955 1956

class pass_rtl_cprop : public rtl_opt_pass
{
public:
1957 1958
  pass_rtl_cprop (gcc::context *ctxt)
    : rtl_opt_pass (pass_data_rtl_cprop, ctxt)
1959 1960 1961
  {}

  /* opt_pass methods: */
1962
  opt_pass * clone () { return new pass_rtl_cprop (m_ctxt); }
1963 1964 1965 1966 1967 1968 1969
  virtual bool gate (function *fun)
    {
      return optimize > 0 && flag_gcse
	&& !fun->calls_setjmp
	&& dbg_cnt (cprop);
    }

1970
  virtual unsigned int execute (function *) { return execute_rtl_cprop (); }
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980

}; // class pass_rtl_cprop

} // anon namespace

rtl_opt_pass *
make_pass_rtl_cprop (gcc::context *ctxt)
{
  return new pass_rtl_cprop (ctxt);
}