fwprop.c 34 KB
Newer Older
Paolo Bonzini committed
1
/* RTL-based forward propagation pass for GNU compiler.
2
   Copyright (C) 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
Paolo Bonzini committed
3 4 5 6 7 8
   Contributed by Paolo Bonzini and Steven Bosscher.

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
9
Software Foundation; either version 3, or (at your option) any later
Paolo Bonzini committed
10 11 12 13 14 15 16 17
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
18 19
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */
Paolo Bonzini committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "toplev.h"

#include "timevar.h"
#include "rtl.h"
#include "tm_p.h"
#include "emit-rtl.h"
#include "insn-config.h"
#include "recog.h"
#include "flags.h"
#include "obstack.h"
#include "basic-block.h"
#include "output.h"
#include "df.h"
#include "target.h"
#include "cfgloop.h"
#include "tree-pass.h"


/* This pass does simple forward propagation and simplification when an
   operand of an insn can only come from a single def.  This pass uses
   df.c, so it is global.  However, we only do limited analysis of
   available expressions.

   1) The pass tries to propagate the source of the def into the use,
   and checks if the result is independent of the substituted value.
   For example, the high word of a (zero_extend:DI (reg:SI M)) is always
   zero, independent of the source register.

   In particular, we propagate constants into the use site.  Sometimes
   RTL expansion did not put the constant in the same insn on purpose,
   to satisfy a predicate, and the result will fail to be recognized;
   but this happens rarely and in this case we can still create a
   REG_EQUAL note.  For multi-word operations, this

      (set (subreg:SI (reg:DI 120) 0) (const_int 0))
      (set (subreg:SI (reg:DI 120) 4) (const_int -1))
      (set (subreg:SI (reg:DI 122) 0)
         (ior:SI (subreg:SI (reg:DI 119) 0) (subreg:SI (reg:DI 120) 0)))
      (set (subreg:SI (reg:DI 122) 4)
         (ior:SI (subreg:SI (reg:DI 119) 4) (subreg:SI (reg:DI 120) 4)))

   can be simplified to the much simpler

      (set (subreg:SI (reg:DI 122) 0) (subreg:SI (reg:DI 119)))
      (set (subreg:SI (reg:DI 122) 4) (const_int -1))

   This particular propagation is also effective at putting together
   complex addressing modes.  We are more aggressive inside MEMs, in
   that all definitions are propagated if the use is in a MEM; if the
   result is a valid memory address we check address_cost to decide
   whether the substitution is worthwhile.

   2) The pass propagates register copies.  This is not as effective as
   the copy propagation done by CSE's canon_reg, which works by walking
   the instruction chain, it can help the other transformations.

   We should consider removing this optimization, and instead reorder the
   RTL passes, because GCSE does this transformation too.  With some luck,
   the CSE pass at the end of rest_of_handle_gcse could also go away.

   3) The pass looks for paradoxical subregs that are actually unnecessary.
   Things like this:

     (set (reg:QI 120) (subreg:QI (reg:SI 118) 0))
     (set (reg:QI 121) (subreg:QI (reg:SI 119) 0))
     (set (reg:SI 122) (plus:SI (subreg:SI (reg:QI 120) 0)
                                (subreg:SI (reg:QI 121) 0)))

   are very common on machines that can only do word-sized operations.
   For each use of a paradoxical subreg (subreg:WIDER (reg:NARROW N) 0),
   if it has a single def and it is (subreg:NARROW (reg:WIDE M) 0),
   we can replace the paradoxical subreg with simply (reg:WIDE M).  The
   above will simplify this to

     (set (reg:QI 120) (subreg:QI (reg:SI 118) 0))
     (set (reg:QI 121) (subreg:QI (reg:SI 119) 0))
     (set (reg:SI 122) (plus:SI (reg:SI 118) (reg:SI 119)))

   where the first two insns are now dead.  */


static int num_changes;


/* Do not try to replace constant addresses or addresses of local and
   argument slots.  These MEM expressions are made only once and inserted
   in many instructions, as well as being used to control symbol table
   output.  It is not safe to clobber them.

   There are some uncommon cases where the address is already in a register
   for some reason, but we cannot take advantage of that because we have
   no easy way to unshare the MEM.  In addition, looking up all stack
   addresses is costly.  */

static bool
can_simplify_addr (rtx addr)
{
  rtx reg;

  if (CONSTANT_ADDRESS_P (addr))
    return false;

  if (GET_CODE (addr) == PLUS)
    reg = XEXP (addr, 0);
  else
    reg = addr;

  return (!REG_P (reg)
	  || (REGNO (reg) != FRAME_POINTER_REGNUM
	      && REGNO (reg) != HARD_FRAME_POINTER_REGNUM
	      && REGNO (reg) != ARG_POINTER_REGNUM));
}

/* Returns a canonical version of X for the address, from the point of view,
   that all multiplications are represented as MULT instead of the multiply
   by a power of 2 being represented as ASHIFT.

   Every ASHIFT we find has been made by simplify_gen_binary and was not
   there before, so it is not shared.  So we can do this in place.  */

static void
canonicalize_address (rtx x)
{
  for (;;)
    switch (GET_CODE (x))
      {
      case ASHIFT:
        if (GET_CODE (XEXP (x, 1)) == CONST_INT
            && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (GET_MODE (x))
            && INTVAL (XEXP (x, 1)) >= 0)
	  {
	    HOST_WIDE_INT shift = INTVAL (XEXP (x, 1));
	    PUT_CODE (x, MULT);
	    XEXP (x, 1) = gen_int_mode ((HOST_WIDE_INT) 1 << shift,
					GET_MODE (x));
	  }

	x = XEXP (x, 0);
        break;

      case PLUS:
        if (GET_CODE (XEXP (x, 0)) == PLUS
	    || GET_CODE (XEXP (x, 0)) == ASHIFT
	    || GET_CODE (XEXP (x, 0)) == CONST)
	  canonicalize_address (XEXP (x, 0));

	x = XEXP (x, 1);
        break;

      case CONST:
	x = XEXP (x, 0);
        break;

      default:
        return;
      }
}

/* OLD is a memory address.  Return whether it is good to use NEW instead,
   for a memory access in the given MODE.  */

static bool
187 188
should_replace_address (rtx old_rtx, rtx new_rtx, enum machine_mode mode,
			bool speed)
Paolo Bonzini committed
189 190 191
{
  int gain;

192
  if (rtx_equal_p (old_rtx, new_rtx) || !memory_address_p (mode, new_rtx))
Paolo Bonzini committed
193 194 195
    return false;

  /* Copy propagation is always ok.  */
196
  if (REG_P (old_rtx) && REG_P (new_rtx))
Paolo Bonzini committed
197 198 199
    return true;

  /* Prefer the new address if it is less expensive.  */
200
  gain = address_cost (old_rtx, mode, speed) - address_cost (new_rtx, mode, speed);
Paolo Bonzini committed
201 202 203 204 205 206

  /* If the addresses have equivalent cost, prefer the new address
     if it has the highest `rtx_cost'.  That has the potential of
     eliminating the most insns without additional costs, and it
     is the same that cse.c used to do.  */
  if (gain == 0)
207
    gain = rtx_cost (new_rtx, SET, speed) - rtx_cost (old_rtx, SET, speed);
Paolo Bonzini committed
208 209 210 211

  return (gain > 0);
}

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234

/* Flags for the last parameter of propagate_rtx_1.  */

enum {
  /* If PR_CAN_APPEAR is true, propagate_rtx_1 always returns true;
     if it is false, propagate_rtx_1 returns false if, for at least
     one occurrence OLD, it failed to collapse the result to a constant.
     For example, (mult:M (reg:M A) (minus:M (reg:M B) (reg:M A))) may
     collapse to zero if replacing (reg:M B) with (reg:M A).

     PR_CAN_APPEAR is disregarded inside MEMs: in that case,
     propagate_rtx_1 just tries to make cheaper and valid memory
     addresses.  */
  PR_CAN_APPEAR = 1,

  /* If PR_HANDLE_MEM is not set, propagate_rtx_1 won't attempt any replacement
     outside memory addresses.  This is needed because propagate_rtx_1 does
     not do any analysis on memory; thus it is very conservative and in general
     it will fail if non-read-only MEMs are found in the source expression.

     PR_HANDLE_MEM is set when the source of the propagation was not
     another MEM.  Then, it is safe not to treat non-read-only MEMs as
     ``opaque'' objects.  */
235 236 237 238
  PR_HANDLE_MEM = 2,

  /* Set when costs should be optimized for speed.  */
  PR_OPTIMIZE_FOR_SPEED = 4
239 240 241
};


Paolo Bonzini committed
242 243 244 245 246 247 248 249 250
/* Replace all occurrences of OLD in *PX with NEW and try to simplify the
   resulting expression.  Replace *PX with a new RTL expression if an
   occurrence of OLD was found.

   This is only a wrapper around simplify-rtx.c: do not add any pattern
   matching code here.  (The sole exception is the handling of LO_SUM, but
   that is because there is no simplify_gen_* function for LO_SUM).  */

static bool
251
propagate_rtx_1 (rtx *px, rtx old_rtx, rtx new_rtx, int flags)
Paolo Bonzini committed
252 253 254 255 256
{
  rtx x = *px, tem = NULL_RTX, op0, op1, op2;
  enum rtx_code code = GET_CODE (x);
  enum machine_mode mode = GET_MODE (x);
  enum machine_mode op_mode;
257
  bool can_appear = (flags & PR_CAN_APPEAR) != 0;
Paolo Bonzini committed
258 259
  bool valid_ops = true;

260 261 262 263 264 265 266 267 268
  if (!(flags & PR_HANDLE_MEM) && MEM_P (x) && !MEM_READONLY_P (x))
    {
      /* If unsafe, change MEMs to CLOBBERs or SCRATCHes (to preserve whether
	 they have side effects or not).  */
      *px = (side_effects_p (x)
	     ? gen_rtx_CLOBBER (GET_MODE (x), const0_rtx)
	     : gen_rtx_SCRATCH (GET_MODE (x)));
      return false;
    }
Paolo Bonzini committed
269

270 271
  /* If X is OLD_RTX, return NEW_RTX.  But not if replacing only within an
     address, and we are *not* inside one.  */
272
  if (x == old_rtx)
Paolo Bonzini committed
273
    {
274
      *px = new_rtx;
Paolo Bonzini committed
275 276 277
      return can_appear;
    }

278
  /* If this is an expression, try recursive substitution.  */
Paolo Bonzini committed
279 280 281 282 283
  switch (GET_RTX_CLASS (code))
    {
    case RTX_UNARY:
      op0 = XEXP (x, 0);
      op_mode = GET_MODE (op0);
284
      valid_ops &= propagate_rtx_1 (&op0, old_rtx, new_rtx, flags);
Paolo Bonzini committed
285 286 287 288 289 290 291 292 293
      if (op0 == XEXP (x, 0))
	return true;
      tem = simplify_gen_unary (code, mode, op0, op_mode);
      break;

    case RTX_BIN_ARITH:
    case RTX_COMM_ARITH:
      op0 = XEXP (x, 0);
      op1 = XEXP (x, 1);
294 295
      valid_ops &= propagate_rtx_1 (&op0, old_rtx, new_rtx, flags);
      valid_ops &= propagate_rtx_1 (&op1, old_rtx, new_rtx, flags);
Paolo Bonzini committed
296 297 298 299 300 301 302 303 304 305
      if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
	return true;
      tem = simplify_gen_binary (code, mode, op0, op1);
      break;

    case RTX_COMPARE:
    case RTX_COMM_COMPARE:
      op0 = XEXP (x, 0);
      op1 = XEXP (x, 1);
      op_mode = GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
306 307
      valid_ops &= propagate_rtx_1 (&op0, old_rtx, new_rtx, flags);
      valid_ops &= propagate_rtx_1 (&op1, old_rtx, new_rtx, flags);
Paolo Bonzini committed
308 309 310 311 312 313 314 315 316 317 318
      if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
	return true;
      tem = simplify_gen_relational (code, mode, op_mode, op0, op1);
      break;

    case RTX_TERNARY:
    case RTX_BITFIELD_OPS:
      op0 = XEXP (x, 0);
      op1 = XEXP (x, 1);
      op2 = XEXP (x, 2);
      op_mode = GET_MODE (op0);
319 320 321
      valid_ops &= propagate_rtx_1 (&op0, old_rtx, new_rtx, flags);
      valid_ops &= propagate_rtx_1 (&op1, old_rtx, new_rtx, flags);
      valid_ops &= propagate_rtx_1 (&op2, old_rtx, new_rtx, flags);
Paolo Bonzini committed
322 323 324 325 326 327 328 329 330 331 332 333
      if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1) && op2 == XEXP (x, 2))
	return true;
      if (op_mode == VOIDmode)
	op_mode = GET_MODE (op0);
      tem = simplify_gen_ternary (code, mode, op_mode, op0, op1, op2);
      break;

    case RTX_EXTRA:
      /* The only case we try to handle is a SUBREG.  */
      if (code == SUBREG)
	{
          op0 = XEXP (x, 0);
334
	  valid_ops &= propagate_rtx_1 (&op0, old_rtx, new_rtx, flags);
Paolo Bonzini committed
335 336 337 338 339 340 341 342
          if (op0 == XEXP (x, 0))
	    return true;
	  tem = simplify_gen_subreg (mode, op0, GET_MODE (SUBREG_REG (x)),
				     SUBREG_BYTE (x));
	}
      break;

    case RTX_OBJ:
343
      if (code == MEM && x != new_rtx)
Paolo Bonzini committed
344 345 346 347 348 349 350 351 352
	{
	  rtx new_op0;
	  op0 = XEXP (x, 0);

	  /* There are some addresses that we cannot work on.  */
	  if (!can_simplify_addr (op0))
	    return true;

	  op0 = new_op0 = targetm.delegitimize_address (op0);
353
	  valid_ops &= propagate_rtx_1 (&new_op0, old_rtx, new_rtx,
354
					flags | PR_CAN_APPEAR);
Paolo Bonzini committed
355 356 357 358

	  /* Dismiss transformation that we do not want to carry on.  */
	  if (!valid_ops
	      || new_op0 == op0
359 360
	      || !(GET_MODE (new_op0) == GET_MODE (op0)
		   || GET_MODE (new_op0) == VOIDmode))
Paolo Bonzini committed
361 362 363 364 365
	    return true;

	  canonicalize_address (new_op0);

	  /* Copy propagations are always ok.  Otherwise check the costs.  */
366
	  if (!(REG_P (old_rtx) && REG_P (new_rtx))
367 368
	      && !should_replace_address (op0, new_op0, GET_MODE (x),
	      			 	  flags & PR_OPTIMIZE_FOR_SPEED))
Paolo Bonzini committed
369 370 371 372 373 374 375 376 377 378 379 380 381
	    return true;

	  tem = replace_equiv_address_nv (x, new_op0);
	}

      else if (code == LO_SUM)
	{
          op0 = XEXP (x, 0);
          op1 = XEXP (x, 1);

	  /* The only simplification we do attempts to remove references to op0
	     or make it constant -- in both cases, op0's invalidity will not
	     make the result invalid.  */
382 383
	  propagate_rtx_1 (&op0, old_rtx, new_rtx, flags | PR_CAN_APPEAR);
	  valid_ops &= propagate_rtx_1 (&op1, old_rtx, new_rtx, flags);
Paolo Bonzini committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
          if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
	    return true;

	  /* (lo_sum (high x) x) -> x  */
	  if (GET_CODE (op0) == HIGH && rtx_equal_p (XEXP (op0, 0), op1))
	    tem = op1;
	  else
	    tem = gen_rtx_LO_SUM (mode, op0, op1);

	  /* OP1 is likely not a legitimate address, otherwise there would have
	     been no LO_SUM.  We want it to disappear if it is invalid, return
	     false in that case.  */
	  return memory_address_p (mode, tem);
	}

      else if (code == REG)
	{
401
	  if (rtx_equal_p (x, old_rtx))
Paolo Bonzini committed
402
	    {
403
              *px = new_rtx;
Paolo Bonzini committed
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
              return can_appear;
	    }
	}
      break;

    default:
      break;
    }

  /* No change, no trouble.  */
  if (tem == NULL_RTX)
    return true;

  *px = tem;

  /* The replacement we made so far is valid, if all of the recursive
     replacements were valid, or we could simplify everything to
     a constant.  */
  return valid_ops || can_appear || CONSTANT_P (tem);
}

425 426 427 428 429 430 431 432 433 434 435 436

/* for_each_rtx traversal function that returns 1 if BODY points to
   a non-constant mem.  */

static int
varying_mem_p (rtx *body, void *data ATTRIBUTE_UNUSED)
{
  rtx x = *body;
  return MEM_P (x) && !MEM_READONLY_P (x);
}


Paolo Bonzini committed
437
/* Replace all occurrences of OLD in X with NEW and try to simplify the
438
   resulting expression (in mode MODE).  Return a new expression if it is
Paolo Bonzini committed
439 440 441 442 443 444 445
   a constant, otherwise X.

   Simplifications where occurrences of NEW collapse to a constant are always
   accepted.  All simplifications are accepted if NEW is a pseudo too.
   Otherwise, we accept simplifications that have a lower or equal cost.  */

static rtx
446 447
propagate_rtx (rtx x, enum machine_mode mode, rtx old_rtx, rtx new_rtx,
	       bool speed)
Paolo Bonzini committed
448 449 450
{
  rtx tem;
  bool collapsed;
451
  int flags;
Paolo Bonzini committed
452

453
  if (REG_P (new_rtx) && REGNO (new_rtx) < FIRST_PSEUDO_REGISTER)
Paolo Bonzini committed
454 455
    return NULL_RTX;

456
  flags = 0;
457
  if (REG_P (new_rtx) || CONSTANT_P (new_rtx))
458
    flags |= PR_CAN_APPEAR;
459
  if (!for_each_rtx (&new_rtx, varying_mem_p, NULL))
460
    flags |= PR_HANDLE_MEM;
Paolo Bonzini committed
461

462 463 464
  if (speed)
    flags |= PR_OPTIMIZE_FOR_SPEED;

Paolo Bonzini committed
465
  tem = x;
466
  collapsed = propagate_rtx_1 (&tem, old_rtx, copy_rtx (new_rtx), flags);
Paolo Bonzini committed
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
  if (tem == x || !collapsed)
    return NULL_RTX;

  /* gen_lowpart_common will not be able to process VOIDmode entities other
     than CONST_INTs.  */
  if (GET_MODE (tem) == VOIDmode && GET_CODE (tem) != CONST_INT)
    return NULL_RTX;

  if (GET_MODE (tem) == VOIDmode)
    tem = rtl_hooks.gen_lowpart_no_emit (mode, tem);
  else
    gcc_assert (GET_MODE (tem) == mode);

  return tem;
}




/* Return true if the register from reference REF is killed
   between FROM to (but not including) TO.  */

489
static bool
490
local_ref_killed_between_p (df_ref ref, rtx from, rtx to)
Paolo Bonzini committed
491 492 493 494 495
{
  rtx insn;

  for (insn = from; insn != to; insn = NEXT_INSN (insn))
    {
496
      df_ref *def_rec;
Paolo Bonzini committed
497 498 499
      if (!INSN_P (insn))
	continue;

500
      for (def_rec = DF_INSN_DEFS (insn); *def_rec; def_rec++)
Paolo Bonzini committed
501
	{
502
	  df_ref def = *def_rec;
Paolo Bonzini committed
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
	  if (DF_REF_REGNO (ref) == DF_REF_REGNO (def))
	    return true;
	}
    }
  return false;
}


/* Check if the given DEF is available in INSN.  This would require full
   computation of available expressions; we check only restricted conditions:
   - if DEF is the sole definition of its register, go ahead;
   - in the same basic block, we check for no definitions killing the
     definition of DEF_INSN;
   - if USE's basic block has DEF's basic block as the sole predecessor,
     we check if the definition is killed after DEF_INSN or before
     TARGET_INSN insn, in their respective basic blocks.  */
static bool
520
use_killed_between (df_ref use, rtx def_insn, rtx target_insn)
Paolo Bonzini committed
521
{
522 523
  basic_block def_bb = BLOCK_FOR_INSN (def_insn);
  basic_block target_bb = BLOCK_FOR_INSN (target_insn);
Paolo Bonzini committed
524
  int regno;
525
  df_ref def;
Paolo Bonzini committed
526

527 528 529 530 531 532 533
  /* In some obscure situations we can have a def reaching a use
     that is _before_ the def.  In other words the def does not
     dominate the use even though the use and def are in the same
     basic block.  This can happen when a register may be used
     uninitialized in a loop.  In such cases, we must assume that
     DEF is not available.  */
  if (def_bb == target_bb
534
      ? DF_INSN_LUID (def_insn) >= DF_INSN_LUID (target_insn)
535 536 537
      : !dominated_by_p (CDI_DOMINATORS, target_bb, def_bb))
    return true;

Paolo Bonzini committed
538
  /* Check if the reg in USE has only one definition.  We already
539 540 541 542
     know that this definition reaches use, or we wouldn't be here.
     However, this is invalid for hard registers because if they are
     live at the beginning of the function it does not mean that we
     have an uninitialized access.  */
Paolo Bonzini committed
543
  regno = DF_REF_REGNO (use);
544
  def = DF_REG_DEF_CHAIN (regno);
545
  if (def
546
      && DF_REF_NEXT_REG (def) == NULL
547
      && regno >= FIRST_PSEUDO_REGISTER)
Paolo Bonzini committed
548 549
    return false;

550
  /* Check locally if we are in the same basic block.  */
Paolo Bonzini committed
551
  if (def_bb == target_bb)
552
    return local_ref_killed_between_p (use, def_insn, target_insn);
Paolo Bonzini committed
553 554 555 556 557

  /* Finally, if DEF_BB is the sole predecessor of TARGET_BB.  */
  if (single_pred_p (target_bb)
      && single_pred (target_bb) == def_bb)
    {
558
      df_ref x;
Paolo Bonzini committed
559 560 561

      /* See if USE is killed between DEF_INSN and the last insn in the
	 basic block containing DEF_INSN.  */
562
      x = df_bb_regno_last_def_find (def_bb, regno);
563
      if (x && DF_INSN_LUID (DF_REF_INSN (x)) >= DF_INSN_LUID (def_insn))
Paolo Bonzini committed
564 565 566 567
	return true;

      /* See if USE is killed between TARGET_INSN and the first insn in the
	 basic block containing TARGET_INSN.  */
568
      x = df_bb_regno_first_def_find (target_bb, regno);
569
      if (x && DF_INSN_LUID (DF_REF_INSN (x)) < DF_INSN_LUID (target_insn))
Paolo Bonzini committed
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
	return true;

      return false;
    }

  /* Otherwise assume the worst case.  */
  return true;
}


/* Check if all uses in DEF_INSN can be used in TARGET_INSN.  This
   would require full computation of available expressions;
   we check only restricted conditions, see use_killed_between.  */
static bool
all_uses_available_at (rtx def_insn, rtx target_insn)
{
586
  df_ref *use_rec;
587
  struct df_insn_info *insn_info = DF_INSN_INFO_GET (def_insn);
Paolo Bonzini committed
588 589 590 591 592 593 594 595 596 597 598 599 600
  rtx def_set = single_set (def_insn);

  gcc_assert (def_set);

  /* If target_insn comes right after def_insn, which is very common
     for addresses, we can use a quicker test.  */
  if (NEXT_INSN (def_insn) == target_insn
      && REG_P (SET_DEST (def_set)))
    {
      rtx def_reg = SET_DEST (def_set);

      /* If the insn uses the reg that it defines, the substitution is
         invalid.  */
601
      for (use_rec = DF_INSN_INFO_USES (insn_info); *use_rec; use_rec++)
602
	{
603
	  df_ref use = *use_rec;
604 605 606
	  if (rtx_equal_p (DF_REF_REG (use), def_reg))
	    return false;
	}
607
      for (use_rec = DF_INSN_INFO_EQ_USES (insn_info); *use_rec; use_rec++)
608
	{
609 610
	  df_ref use = *use_rec;
	  if (rtx_equal_p (DF_REF_REG (use), def_reg))
611 612
	    return false;
	}
Paolo Bonzini committed
613 614 615 616 617
    }
  else
    {
      /* Look at all the uses of DEF_INSN, and see if they are not
	 killed between DEF_INSN and TARGET_INSN.  */
618
      for (use_rec = DF_INSN_INFO_USES (insn_info); *use_rec; use_rec++)
619
	{
620
	  df_ref use = *use_rec;
621 622 623
	  if (use_killed_between (use, def_insn, target_insn))
	    return false;
	}
624
      for (use_rec = DF_INSN_INFO_EQ_USES (insn_info); *use_rec; use_rec++)
625
	{
626
	  df_ref use = *use_rec;
627 628 629
	  if (use_killed_between (use, def_insn, target_insn))
	    return false;
	}
Paolo Bonzini committed
630 631
    }

632
  return true;
Paolo Bonzini committed
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
}


struct find_occurrence_data
{
  rtx find;
  rtx *retval;
};

/* Callback for for_each_rtx, used in find_occurrence.
   See if PX is the rtx we have to find.  Return 1 to stop for_each_rtx
   if successful, or 0 to continue traversing otherwise.  */

static int
find_occurrence_callback (rtx *px, void *data)
{
  struct find_occurrence_data *fod = (struct find_occurrence_data *) data;
  rtx x = *px;
  rtx find = fod->find;

  if (x == find)
    {
      fod->retval = px;
      return 1;
    }

  return 0;
}

/* Return a pointer to one of the occurrences of register FIND in *PX.  */

static rtx *
find_occurrence (rtx *px, rtx find)
{
  struct find_occurrence_data data;

  gcc_assert (REG_P (find)
	      || (GET_CODE (find) == SUBREG
		  && REG_P (SUBREG_REG (find))));

  data.find = find;
  data.retval = NULL;
  for_each_rtx (px, find_occurrence_callback, &data);
  return data.retval;
}


/* Inside INSN, the expression rooted at *LOC has been changed, moving some
681
   uses from USE_VEC.  Find those that are present, and create new items
Paolo Bonzini committed
682 683 684
   in the data flow object of the pass.  Mark any new uses as having the
   given TYPE.  */
static void
685
update_df (rtx insn, rtx *loc, df_ref *use_rec, enum df_ref_type type,
Paolo Bonzini committed
686 687
	   int new_flags)
{
688
  bool changed = false;
Paolo Bonzini committed
689 690

  /* Add a use for the registers that were propagated.  */
691
  while (*use_rec)
Paolo Bonzini committed
692
    {
693 694
      df_ref use = *use_rec;
      df_ref orig_use = use, new_use;
695 696
      int width = -1;
      int offset = -1;
697
      enum machine_mode mode = 0;
Paolo Bonzini committed
698
      rtx *new_loc = find_occurrence (loc, DF_REF_REG (orig_use));
699
      use_rec++;
Paolo Bonzini committed
700 701 702 703

      if (!new_loc)
	continue;

704 705
      if (DF_REF_FLAGS_IS_SET (orig_use, DF_REF_SIGN_EXTRACT | DF_REF_ZERO_EXTRACT))
	{
706 707 708
	  width = DF_REF_EXTRACT_WIDTH (orig_use);
	  offset = DF_REF_EXTRACT_OFFSET (orig_use);
	  mode = DF_REF_EXTRACT_MODE (orig_use);
709 710
	}

Paolo Bonzini committed
711 712
      /* Add a new insn use.  Use the original type, because it says if the
         use was within a MEM.  */
713
      new_use = df_ref_create (DF_REF_REG (orig_use), new_loc,
Paolo Bonzini committed
714
			       insn, BLOCK_FOR_INSN (insn),
715 716
			       type, DF_REF_FLAGS (orig_use) | new_flags, 
			       width, offset, mode);
Paolo Bonzini committed
717 718

      /* Set up the use-def chain.  */
719 720
      df_chain_copy (new_use, DF_REF_CHAIN (orig_use));
      changed = true;
Paolo Bonzini committed
721
    }
722 723
  if (changed)
    df_insn_rescan (insn);
Paolo Bonzini committed
724 725 726 727 728 729 730 731 732 733
}


/* Try substituting NEW into LOC, which originated from forward propagation
   of USE's value from DEF_INSN.  SET_REG_EQUAL says whether we are
   substituting the whole SET_SRC, so we can set a REG_EQUAL note if the
   new insn is not recognized.  Return whether the substitution was
   performed.  */

static bool
734
try_fwprop_subst (df_ref use, rtx *loc, rtx new_rtx, rtx def_insn, bool set_reg_equal)
Paolo Bonzini committed
735 736 737 738
{
  rtx insn = DF_REF_INSN (use);
  enum df_ref_type type = DF_REF_TYPE (use);
  int flags = DF_REF_FLAGS (use);
739
  rtx set = single_set (insn);
740 741
  bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
  int old_cost = rtx_cost (SET_SRC (set), SET, speed);
742
  bool ok;
Paolo Bonzini committed
743 744 745 746 747 748

  if (dump_file)
    {
      fprintf (dump_file, "\nIn insn %d, replacing\n ", INSN_UID (insn));
      print_inline_rtx (dump_file, *loc, 2);
      fprintf (dump_file, "\n with ");
749
      print_inline_rtx (dump_file, new_rtx, 2);
Paolo Bonzini committed
750 751 752
      fprintf (dump_file, "\n");
    }

753
  validate_unshare_change (insn, loc, new_rtx, true);
754 755 756 757 758 759 760 761
  if (!verify_changes (0))
    {
      if (dump_file)
	fprintf (dump_file, "Changes to insn %d not recognized\n",
		 INSN_UID (insn));
      ok = false;
    }

762
  else if (DF_REF_TYPE (use) == DF_REF_REG_USE
763
	   && rtx_cost (SET_SRC (set), SET, speed) > old_cost)
764 765 766 767 768 769 770 771
    {
      if (dump_file)
	fprintf (dump_file, "Changes to insn %d not profitable\n",
		 INSN_UID (insn));
      ok = false;
    }

  else
Paolo Bonzini committed
772 773 774
    {
      if (dump_file)
	fprintf (dump_file, "Changed insn %d\n", INSN_UID (insn));
775 776 777 778 779 780 781
      ok = true;
    }

  if (ok)
    {
      confirm_change_group ();
      num_changes++;
Paolo Bonzini committed
782

783
      df_ref_remove (use);
784
      if (!CONSTANT_P (new_rtx))
785
	{
786 787 788
	  struct df_insn_info *insn_info = DF_INSN_INFO_GET (def_insn);
	  update_df (insn, loc, DF_INSN_INFO_USES (insn_info), type, flags);
	  update_df (insn, loc, DF_INSN_INFO_EQ_USES (insn_info), type, flags);
789
	}
Paolo Bonzini committed
790 791 792
    }
  else
    {
793
      cancel_changes (0);
Paolo Bonzini committed
794

795
      /* Can also record a simplified value in a REG_EQUAL note,
Steven Bosscher committed
796 797
	 making a new one if one does not already exist.  */
      if (set_reg_equal)
Paolo Bonzini committed
798 799 800 801
	{
	  if (dump_file)
	    fprintf (dump_file, " Setting REG_EQUAL note\n");

802
	  set_unique_reg_note (insn, REG_EQUAL, copy_rtx (new_rtx));
Paolo Bonzini committed
803

804 805
	  /* ??? Is this still necessary if we add the note through
	     set_unique_reg_note?  */
806
          if (!CONSTANT_P (new_rtx))
807
	    {
808 809
	      struct df_insn_info *insn_info = DF_INSN_INFO_GET (def_insn);
	      update_df (insn, loc, DF_INSN_INFO_USES (insn_info),
810
			 type, DF_REF_IN_NOTE);
811
	      update_df (insn, loc, DF_INSN_INFO_EQ_USES (insn_info),
812 813
			 type, DF_REF_IN_NOTE);
	    }
Paolo Bonzini committed
814 815
	}
    }
816 817

  return ok;
Paolo Bonzini committed
818 819 820 821 822 823
}


/* If USE is a paradoxical subreg, see if it can be replaced by a pseudo.  */

static bool
824
forward_propagate_subreg (df_ref use, rtx def_insn, rtx def_set)
Paolo Bonzini committed
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
{
  rtx use_reg = DF_REF_REG (use);
  rtx use_insn, src;

  /* Only consider paradoxical subregs... */
  enum machine_mode use_mode = GET_MODE (use_reg);
  if (GET_CODE (use_reg) != SUBREG
      || !REG_P (SET_DEST (def_set))
      || GET_MODE_SIZE (use_mode)
	 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (use_reg))))
    return false;

  /* If this is a paradoxical SUBREG, we have no idea what value the
     extra bits would have.  However, if the operand is equivalent to
     a SUBREG whose operand is the same as our mode, and all the modes
     are within a word, we can just use the inner operand because
     these SUBREGs just say how to treat the register.  */
  use_insn = DF_REF_INSN (use);
  src = SET_SRC (def_set);
  if (GET_CODE (src) == SUBREG
      && REG_P (SUBREG_REG (src))
      && GET_MODE (SUBREG_REG (src)) == use_mode
      && subreg_lowpart_p (src)
      && all_uses_available_at (def_insn, use_insn))
    return try_fwprop_subst (use, DF_REF_LOC (use), SUBREG_REG (src),
			     def_insn, false);
  else
    return false;
}

/* Try to replace USE with SRC (defined in DEF_INSN) and simplify the
   result.  */

static bool
859
forward_propagate_and_simplify (df_ref use, rtx def_insn, rtx def_set)
Paolo Bonzini committed
860 861 862
{
  rtx use_insn = DF_REF_INSN (use);
  rtx use_set = single_set (use_insn);
863
  rtx src, reg, new_rtx, *loc;
Paolo Bonzini committed
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
  bool set_reg_equal;
  enum machine_mode mode;

  if (!use_set)
    return false;

  /* Do not propagate into PC, CC0, etc.  */
  if (GET_MODE (SET_DEST (use_set)) == VOIDmode)
    return false;

  /* If def and use are subreg, check if they match.  */
  reg = DF_REF_REG (use);
  if (GET_CODE (reg) == SUBREG
      && GET_CODE (SET_DEST (def_set)) == SUBREG
      && (SUBREG_BYTE (SET_DEST (def_set)) != SUBREG_BYTE (reg)
	  || GET_MODE (SET_DEST (def_set)) != GET_MODE (reg)))
    return false;

  /* Check if the def had a subreg, but the use has the whole reg.  */
  if (REG_P (reg) && GET_CODE (SET_DEST (def_set)) == SUBREG)
    return false;

  /* Check if the use has a subreg, but the def had the whole reg.  Unlike the
     previous case, the optimization is possible and often useful indeed.  */
  if (GET_CODE (reg) == SUBREG && REG_P (SET_DEST (def_set)))
    reg = SUBREG_REG (reg);

  /* Check if the substitution is valid (last, because it's the most
     expensive check!).  */
  src = SET_SRC (def_set);
  if (!CONSTANT_P (src) && !all_uses_available_at (def_insn, use_insn))
    return false;

  /* Check if the def is loading something from the constant pool; in this
     case we would undo optimization such as compress_float_constant.
     Still, we can set a REG_EQUAL note.  */
  if (MEM_P (src) && MEM_READONLY_P (src))
    {
      rtx x = avoid_constant_pool_reference (src);
      if (x != src)
	{
          rtx note = find_reg_note (use_insn, REG_EQUAL, NULL_RTX);
906 907 908 909
	  rtx old_rtx = note ? XEXP (note, 0) : SET_SRC (use_set);
	  rtx new_rtx = simplify_replace_rtx (old_rtx, src, x);
	  if (old_rtx != new_rtx)
            set_unique_reg_note (use_insn, REG_EQUAL, copy_rtx (new_rtx));
Paolo Bonzini committed
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
	}
      return false;
    }

  /* Else try simplifying.  */

  if (DF_REF_TYPE (use) == DF_REF_REG_MEM_STORE)
    {
      loc = &SET_DEST (use_set);
      set_reg_equal = false;
    }
  else
    {
      rtx note = find_reg_note (use_insn, REG_EQUAL, NULL_RTX);
      if (DF_REF_FLAGS (use) & DF_REF_IN_NOTE)
	loc = &XEXP (note, 0);
      else
	loc = &SET_SRC (use_set);
928

Paolo Bonzini committed
929 930 931 932 933 934 935 936 937 938 939 940
      /* Do not replace an existing REG_EQUAL note if the insn is not
	 recognized.  Either we're already replacing in the note, or
	 we'll separately try plugging the definition in the note and
	 simplifying.  */
      set_reg_equal = (note == NULL_RTX);
    }

  if (GET_MODE (*loc) == VOIDmode)
    mode = GET_MODE (SET_DEST (use_set));
  else
    mode = GET_MODE (*loc);

941 942
  new_rtx = propagate_rtx (*loc, mode, reg, src,
  			   optimize_bb_for_speed_p (BLOCK_FOR_INSN (use_insn)));
943

944
  if (!new_rtx)
Paolo Bonzini committed
945 946
    return false;

947
  return try_fwprop_subst (use, loc, new_rtx, def_insn, set_reg_equal);
Paolo Bonzini committed
948 949 950 951 952 953 954
}


/* Given a use USE of an insn, if it has a single reaching
   definition, try to forward propagate it into that insn.  */

static void
955
forward_propagate_into (df_ref use)
Paolo Bonzini committed
956 957
{
  struct df_link *defs;
958
  df_ref def;
Paolo Bonzini committed
959
  rtx def_insn, def_set, use_insn;
960
  rtx parent;
Paolo Bonzini committed
961 962 963

  if (DF_REF_FLAGS (use) & DF_REF_READ_WRITE)
    return;
964
  if (DF_REF_IS_ARTIFICIAL (use))
965
    return;
Paolo Bonzini committed
966 967 968 969 970 971 972 973 974

  /* Only consider uses that have a single definition.  */
  defs = DF_REF_CHAIN (use);
  if (!defs || defs->next)
    return;

  def = defs->ref;
  if (DF_REF_FLAGS (def) & DF_REF_READ_WRITE)
    return;
975
  if (DF_REF_IS_ARTIFICIAL (def))
976
    return;
Paolo Bonzini committed
977

978 979
  /* Do not propagate loop invariant definitions inside the loop.  */
  if (DF_REF_BB (def)->loop_father != DF_REF_BB (use)->loop_father)
Paolo Bonzini committed
980 981 982 983 984 985 986 987 988
    return;

  /* Check if the use is still present in the insn!  */
  use_insn = DF_REF_INSN (use);
  if (DF_REF_FLAGS (use) & DF_REF_IN_NOTE)
    parent = find_reg_note (use_insn, REG_EQUAL, NULL_RTX);
  else
    parent = PATTERN (use_insn);

989
  if (!reg_mentioned_p (DF_REF_REG (use), parent))
Paolo Bonzini committed
990 991 992
    return;

  def_insn = DF_REF_INSN (def);
993 994
  if (multiple_sets (def_insn))
    return;
Paolo Bonzini committed
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
  def_set = single_set (def_insn);
  if (!def_set)
    return;

  /* Only try one kind of propagation.  If two are possible, we'll
     do it on the following iterations.  */
  if (!forward_propagate_and_simplify (use, def_insn, def_set))
    forward_propagate_subreg (use, def_insn, def_set);
}


static void
fwprop_init (void)
{
  num_changes = 0;
1010
  calculate_dominance_info (CDI_DOMINATORS);
Paolo Bonzini committed
1011 1012 1013 1014 1015

  /* We do not always want to propagate into loops, so we have to find
     loops and be careful about them.  But we have to call flow_loops_find
     before df_analyze, because flow_loops_find may introduce new jump
     insns (sadly) if we are not working in cfglayout mode.  */
1016
  loop_optimizer_init (0);
Paolo Bonzini committed
1017 1018 1019

  /* Now set up the dataflow problem (we only want use-def chains) and
     put the dataflow solver to work.  */
1020 1021 1022 1023 1024
  df_set_flags (DF_EQ_NOTES);
  df_chain_add_problem (DF_UD_CHAIN);
  df_analyze ();
  df_maybe_reorganize_use_refs (DF_REF_ORDER_BY_INSN_WITH_NOTES);
  df_set_flags (DF_DEFER_INSN_RESCAN);
Paolo Bonzini committed
1025 1026 1027 1028 1029
}

static void
fwprop_done (void)
{
1030
  loop_optimizer_finalize ();
1031

1032
  free_dominance_info (CDI_DOMINATORS);
Paolo Bonzini committed
1033 1034 1035 1036 1037 1038 1039
  cleanup_cfg (0);
  delete_trivially_dead_insns (get_insns (), max_reg_num ());

  if (dump_file)
    fprintf (dump_file,
	     "\nNumber of successful forward propagations: %d\n\n",
	     num_changes);
1040
  df_remove_problem (df_chain);
Paolo Bonzini committed
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
}



/* Main entry point.  */

static bool
gate_fwprop (void)
{
  return optimize > 0 && flag_forward_propagate;
}

static unsigned int
fwprop (void)
{
  unsigned i;

  fwprop_init ();

  /* Go through all the uses.  update_df will create new ones at the
     end, and we'll go through them as well.

     Do not forward propagate addresses into loops until after unrolling.
     CSE did so because it was able to fix its own mess, but we are not.  */

1066
  for (i = 0; i < DF_USES_TABLE_SIZE (); i++)
Paolo Bonzini committed
1067
    {
1068
      df_ref use = DF_USES_GET (i);
Paolo Bonzini committed
1069
      if (use)
1070
	if (DF_REF_TYPE (use) == DF_REF_REG_USE
1071 1072 1073
	    || DF_REF_BB (use)->loop_father == NULL
	    /* The outer most loop is not really a loop.  */
	    || loop_outer (DF_REF_BB (use)->loop_father) == NULL)
Paolo Bonzini committed
1074 1075 1076 1077 1078 1079 1080
	  forward_propagate_into (use);
    }

  fwprop_done ();
  return 0;
}

1081
struct rtl_opt_pass pass_rtl_fwprop =
Paolo Bonzini committed
1082
{
1083 1084
 {
  RTL_PASS,
Paolo Bonzini committed
1085
  "fwprop1",                            /* name */
1086 1087
  gate_fwprop,				/* gate */
  fwprop,				/* execute */
Paolo Bonzini committed
1088 1089 1090 1091 1092 1093 1094 1095
  NULL,                                 /* sub */
  NULL,                                 /* next */
  0,                                    /* static_pass_number */
  TV_FWPROP,                            /* tv_id */
  0,                                    /* properties_required */
  0,                                    /* properties_provided */
  0,                                    /* properties_destroyed */
  0,                                    /* todo_flags_start */
1096
  TODO_df_finish | TODO_verify_rtl_sharing |
1097 1098
  TODO_dump_func                        /* todo_flags_finish */
 }
Paolo Bonzini committed
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
};

static unsigned int
fwprop_addr (void)
{
  unsigned i;
  fwprop_init ();

  /* Go through all the uses.  update_df will create new ones at the
     end, and we'll go through them as well.  */
1109 1110 1111
  df_set_flags (DF_DEFER_INSN_RESCAN);

  for (i = 0; i < DF_USES_TABLE_SIZE (); i++)
Paolo Bonzini committed
1112
    {
1113
      df_ref use = DF_USES_GET (i);
Paolo Bonzini committed
1114 1115
      if (use)
	if (DF_REF_TYPE (use) != DF_REF_REG_USE
1116 1117 1118
	    && DF_REF_BB (use)->loop_father != NULL
	    /* The outer most loop is not really a loop.  */
	    && loop_outer (DF_REF_BB (use)->loop_father) != NULL)
Paolo Bonzini committed
1119 1120 1121 1122 1123 1124 1125 1126
	  forward_propagate_into (use);
    }

  fwprop_done ();

  return 0;
}

1127
struct rtl_opt_pass pass_rtl_fwprop_addr =
Paolo Bonzini committed
1128
{
1129 1130
 {
  RTL_PASS,
Paolo Bonzini committed
1131
  "fwprop2",                            /* name */
1132 1133
  gate_fwprop,				/* gate */
  fwprop_addr,				/* execute */
Paolo Bonzini committed
1134 1135 1136 1137 1138 1139 1140 1141
  NULL,                                 /* sub */
  NULL,                                 /* next */
  0,                                    /* static_pass_number */
  TV_FWPROP,                            /* tv_id */
  0,                                    /* properties_required */
  0,                                    /* properties_provided */
  0,                                    /* properties_destroyed */
  0,                                    /* todo_flags_start */
1142
  TODO_df_finish | TODO_verify_rtl_sharing |
1143 1144
  TODO_dump_func                        /* todo_flags_finish */
 }
Paolo Bonzini committed
1145
};