ifcvt.c 75 KB
Newer Older
Richard Henderson committed
1
/* If-conversion support.
2
   Copyright (C) 2000, 2001 Free Software Foundation, Inc.
Richard Henderson committed
3

4
   This file is part of GCC.
Richard Henderson committed
5

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

11 12 13 14
   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.
Richard Henderson committed
15 16

   You should have received a copy of the GNU General Public License
17 18 19
   along with GCC; see the file COPYING.  If not, write to the Free
   Software Foundation, 59 Temple Place - Suite 330, Boston, MA
   02111-1307, USA.  */
Richard Henderson committed
20 21 22 23 24 25 26 27 28 29

#include "config.h"
#include "system.h"

#include "rtl.h"
#include "regs.h"
#include "function.h"
#include "flags.h"
#include "insn-config.h"
#include "recog.h"
30
#include "hard-reg-set.h"
Richard Henderson committed
31 32
#include "basic-block.h"
#include "expr.h"
33
#include "real.h"
Richard Henderson committed
34
#include "output.h"
35
#include "toplev.h"
Richard Henderson committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include "tm_p.h"


#ifndef HAVE_conditional_execution
#define HAVE_conditional_execution 0
#endif
#ifndef HAVE_conditional_move
#define HAVE_conditional_move 0
#endif
#ifndef HAVE_incscc
#define HAVE_incscc 0
#endif
#ifndef HAVE_decscc
#define HAVE_decscc 0
#endif
51 52 53 54 55 56
#ifndef HAVE_trap
#define HAVE_trap 0
#endif
#ifndef HAVE_conditional_trap
#define HAVE_conditional_trap 0
#endif
Richard Henderson committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

#ifndef MAX_CONDITIONAL_EXECUTE
#define MAX_CONDITIONAL_EXECUTE   (BRANCH_COST + 1)
#endif

#define NULL_EDGE	((struct edge_def *)NULL)
#define NULL_BLOCK	((struct basic_block_def *)NULL)

/* # of IF-THEN or IF-THEN-ELSE blocks we looked at  */
static int num_possible_if_blocks;

/* # of IF-THEN or IF-THEN-ELSE blocks were converted to conditional
   execution.  */
static int num_updated_if_blocks;

/* # of basic blocks that were removed.  */
static int num_removed_blocks;

75 76 77
/* True if life data ok at present.  */
static bool life_data_ok;

Richard Henderson committed
78 79 80 81 82 83 84
/* The post-dominator relation on the original block numbers.  */
static sbitmap *post_dominators;

/* Forward references.  */
static int count_bb_insns		PARAMS ((basic_block));
static rtx first_active_insn		PARAMS ((basic_block));
static int last_active_insn_p		PARAMS ((basic_block, rtx));
85
static int seq_contains_jump		PARAMS ((rtx));
Richard Henderson committed
86

87
static int cond_exec_process_insns	PARAMS ((rtx, rtx, rtx, rtx, int));
Richard Henderson committed
88 89 90 91 92
static rtx cond_exec_get_condition	PARAMS ((rtx));
static int cond_exec_process_if_block	PARAMS ((basic_block, basic_block,
						 basic_block, basic_block));

static rtx noce_get_condition		PARAMS ((rtx, rtx *));
93
static int noce_operand_ok		PARAMS ((rtx));
Richard Henderson committed
94 95 96 97 98 99 100 101 102 103 104 105
static int noce_process_if_block	PARAMS ((basic_block, basic_block,
						 basic_block, basic_block));

static int process_if_block		PARAMS ((basic_block, basic_block,
						 basic_block, basic_block));
static void merge_if_block		PARAMS ((basic_block, basic_block,
						 basic_block, basic_block));

static int find_if_header		PARAMS ((basic_block));
static int find_if_block		PARAMS ((basic_block, edge, edge));
static int find_if_case_1		PARAMS ((basic_block, edge, edge));
static int find_if_case_2		PARAMS ((basic_block, edge, edge));
106
static int find_cond_trap		PARAMS ((basic_block, edge, edge));
Richard Henderson committed
107 108
static int find_memory			PARAMS ((rtx *, void *));
static int dead_or_predicable		PARAMS ((basic_block, basic_block,
109
						 basic_block, basic_block, int));
110
static void noce_emit_move_insn		PARAMS ((rtx, rtx));
Richard Henderson committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 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 187 188

/* Abuse the basic_block AUX field to store the original block index,
   as well as a flag indicating that the block should be rescaned for
   life analysis.  */

#define SET_ORIG_INDEX(BB,I)	((BB)->aux = (void *)((size_t)(I) << 1))
#define ORIG_INDEX(BB)		((size_t)(BB)->aux >> 1)
#define SET_UPDATE_LIFE(BB)	((BB)->aux = (void *)((size_t)(BB)->aux | 1))
#define UPDATE_LIFE(BB)		((size_t)(BB)->aux & 1)


/* Count the number of non-jump active insns in BB.  */

static int
count_bb_insns (bb)
     basic_block bb;
{
  int count = 0;
  rtx insn = bb->head;

  while (1)
    {
      if (GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == INSN)
	count++;

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

  return count;
}

/* Return the first non-jump active insn in the basic block.  */

static rtx
first_active_insn (bb)
     basic_block bb;
{
  rtx insn = bb->head;

  if (GET_CODE (insn) == CODE_LABEL)
    {
      if (insn == bb->end)
	return NULL_RTX;
      insn = NEXT_INSN (insn);
    }

  while (GET_CODE (insn) == NOTE)
    {
      if (insn == bb->end)
	return NULL_RTX;
      insn = NEXT_INSN (insn);
    }

  if (GET_CODE (insn) == JUMP_INSN)
    return NULL_RTX;

  return insn;
}

/* Return true if INSN is the last active non-jump insn in BB.  */

static int
last_active_insn_p (bb, insn)
     basic_block bb;
     rtx insn;
{
  do
    {
      if (insn == bb->end)
	return TRUE;
      insn = NEXT_INSN (insn);
    }
  while (GET_CODE (insn) == NOTE);

  return GET_CODE (insn) == JUMP_INSN;
}
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

/* It is possible, especially when having dealt with multi-word 
   arithmetic, for the expanders to have emitted jumps.  Search
   through the sequence and return TRUE if a jump exists so that
   we can abort the conversion.  */

static int
seq_contains_jump (insn)
     rtx insn;
{
  while (insn)
    {
      if (GET_CODE (insn) == JUMP_INSN)
	return 1;
      insn = NEXT_INSN (insn);
    }
  return 0;
}
Richard Henderson committed
207 208 209 210 211 212

/* Go through a bunch of insns, converting them to conditional
   execution format if possible.  Return TRUE if all of the non-note
   insns were processed.  */

static int
213
cond_exec_process_insns (start, end, test, prob_val, mod_ok)
Richard Henderson committed
214 215 216
     rtx start;			/* first insn to look at */
     rtx end;			/* last insn to look at */
     rtx test;			/* conditional execution test */
217
     rtx prob_val;		/* probability of branch taken.  */
Richard Henderson committed
218 219 220 221
     int mod_ok;		/* true if modifications ok last insn.  */
{
  int must_be_last = FALSE;
  rtx insn;
222
  rtx pattern;
Richard Henderson committed
223 224 225 226 227 228 229 230 231

  for (insn = start; ; insn = NEXT_INSN (insn))
    {
      if (GET_CODE (insn) == NOTE)
	goto insn_done;

      if (GET_CODE (insn) != INSN && GET_CODE (insn) != CALL_INSN)
	abort ();

232 233
      /* Remove USE insns that get in the way.  */
      if (reload_completed && GET_CODE (PATTERN (insn)) == USE)
234 235 236 237 238 239 240 241 242
	{
	  /* ??? Ug.  Actually unlinking the thing is problematic, 
	     given what we'd have to coordinate with our callers.  */
	  PUT_CODE (insn, NOTE);
	  NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
	  NOTE_SOURCE_FILE (insn) = 0;
	  goto insn_done;
	}

Richard Henderson committed
243 244 245 246 247 248 249 250 251 252 253 254
      /* Last insn wasn't last?  */
      if (must_be_last)
	return FALSE;

      if (modified_in_p (test, insn))
	{
	  if (!mod_ok)
	    return FALSE;
	  must_be_last = TRUE;
	}

      /* Now build the conditional form of the instruction.  */
255 256 257 258 259 260 261 262 263 264 265
      pattern = PATTERN (insn);

      /* If the machine needs to modify the insn being conditionally executed,
         say for example to force a constant integer operand into a temp
         register, do so here.  */
#ifdef IFCVT_MODIFY_INSN
      IFCVT_MODIFY_INSN (pattern, insn);
      if (! pattern)
	return FALSE;
#endif

Richard Henderson committed
266 267
      validate_change (insn, &PATTERN (insn),
		       gen_rtx_COND_EXEC (VOIDmode, copy_rtx (test),
268
					  pattern), 1);
Richard Henderson committed
269

270 271 272 273 274
      if (GET_CODE (insn) == CALL_INSN && prob_val)
	validate_change (insn, &REG_NOTES (insn),
			 alloc_EXPR_LIST (REG_BR_PROB, prob_val,
					  REG_NOTES (insn)), 1);

Richard Henderson committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
    insn_done:
      if (insn == end)
	break;
    }

  return TRUE;
}

/* Return the condition for a jump.  Do not do any special processing.  */

static rtx
cond_exec_get_condition (jump)
     rtx jump;
{
  rtx test_if, cond;

291
  if (any_condjump_p (jump))
292
    test_if = SET_SRC (pc_set (jump));
Richard Henderson committed
293 294 295 296 297 298 299 300
  else
    return NULL_RTX;
  cond = XEXP (test_if, 0);

  /* If this branches to JUMP_LABEL when the condition is false,
     reverse the condition.  */
  if (GET_CODE (XEXP (test_if, 2)) == LABEL_REF
      && XEXP (XEXP (test_if, 2), 0) == JUMP_LABEL (jump))
301 302 303 304 305 306 307 308
    {
      enum rtx_code rev = reversed_comparison_code (cond, jump);
      if (rev == UNKNOWN)
	return NULL_RTX;

      cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
			     XEXP (cond, 1));
    }
Richard Henderson committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326

  return cond;
}

/* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
   to conditional execution.  Return TRUE if we were successful at
   converting the the block.  */

static int
cond_exec_process_if_block (test_bb, then_bb, else_bb, join_bb)
     basic_block test_bb;	/* Basic block test is in */
     basic_block then_bb;	/* Basic block for THEN block */
     basic_block else_bb;	/* Basic block for ELSE block */
     basic_block join_bb;	/* Basic block the join label is in */
{
  rtx test_expr;		/* expression in IF_THEN_ELSE that is tested */
  rtx then_start;		/* first insn in THEN block */
  rtx then_end;			/* last insn + 1 in THEN block */
Kaveh R. Ghazi committed
327 328
  rtx else_start = NULL_RTX;	/* first insn in ELSE block or NULL */
  rtx else_end = NULL_RTX;	/* last insn + 1 in ELSE block */
329
  int max;			/* max # of insns to convert.  */
Richard Henderson committed
330 331 332
  int then_mod_ok;		/* whether conditional mods are ok in THEN */
  rtx true_expr;		/* test for else block insns */
  rtx false_expr;		/* test for then block insns */
333 334
  rtx true_prob_val;		/* probability of else block */
  rtx false_prob_val;		/* probability of then block */
Richard Henderson committed
335
  int n_insns;
336
  enum rtx_code false_code;
Richard Henderson committed
337 338 339 340 341 342 343

  /* Find the conditional jump to the ELSE or JOIN part, and isolate
     the test.  */
  test_expr = cond_exec_get_condition (test_bb->end);
  if (! test_expr)
    return FALSE;

344 345 346 347 348
  /* If the conditional jump is more than just a conditional jump,
     then we can not do conditional execution conversion on this block.  */
  if (!onlyjump_p (test_bb->end))
    return FALSE;

Richard Henderson committed
349 350 351 352 353
  /* Collect the bounds of where we're to search.  */

  then_start = then_bb->head;
  then_end = then_bb->end;

354 355 356 357
  /* Skip a label heading THEN block.  */
  if (GET_CODE (then_start) == CODE_LABEL)
    then_start = NEXT_INSN (then_start);

Richard Henderson committed
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
  /* Skip a (use (const_int 0)) or branch as the final insn.  */
  if (GET_CODE (then_end) == INSN
      && GET_CODE (PATTERN (then_end)) == USE
      && GET_CODE (XEXP (PATTERN (then_end), 0)) == CONST_INT)
    then_end = PREV_INSN (then_end);
  else if (GET_CODE (then_end) == JUMP_INSN)
    then_end = PREV_INSN (then_end);

  if (else_bb)
    {
      /* Skip the ELSE block's label.  */
      else_start = NEXT_INSN (else_bb->head);
      else_end = else_bb->end;

      /* Skip a (use (const_int 0)) or branch as the final insn.  */
      if (GET_CODE (else_end) == INSN
	  && GET_CODE (PATTERN (else_end)) == USE
	  && GET_CODE (XEXP (PATTERN (else_end), 0)) == CONST_INT)
	else_end = PREV_INSN (else_end);
      else if (GET_CODE (else_end) == JUMP_INSN)
	else_end = PREV_INSN (else_end);
    }

  /* How many instructions should we convert in total?  */
  n_insns = 0;
  if (else_bb)
    {
      max = 2 * MAX_CONDITIONAL_EXECUTE;
      n_insns = count_bb_insns (else_bb);
    }
  else
    max = MAX_CONDITIONAL_EXECUTE;
  n_insns += count_bb_insns (then_bb);
  if (n_insns > max)
    return FALSE;

  /* Map test_expr/test_jump into the appropriate MD tests to use on
     the conditionally executed code.  */
  
  true_expr = test_expr;
398 399 400 401 402 403 404

  false_code = reversed_comparison_code (true_expr, test_bb->end);
  if (false_code != UNKNOWN)
    false_expr = gen_rtx_fmt_ee (false_code, GET_MODE (true_expr),
				 XEXP (true_expr, 0), XEXP (true_expr, 1));
  else
    false_expr = NULL_RTX;
Richard Henderson committed
405

406 407 408 409 410 411 412 413 414 415 416
#ifdef IFCVT_MODIFY_TESTS
  /* If the machine description needs to modify the tests, such as setting a
     conditional execution register from a comparison, it can do so here.  */
  IFCVT_MODIFY_TESTS (true_expr, false_expr, test_bb, then_bb, else_bb,
		      join_bb);

  /* See if the conversion failed */
  if (!true_expr || !false_expr)
    goto fail;
#endif

417 418 419 420 421 422 423 424 425
  true_prob_val = find_reg_note (test_bb->end, REG_BR_PROB, NULL_RTX);
  if (true_prob_val)
    {
      true_prob_val = XEXP (true_prob_val, 0);
      false_prob_val = GEN_INT (REG_BR_PROB_BASE - INTVAL (true_prob_val));
    }
  else
    false_prob_val = NULL_RTX;

Richard Henderson committed
426 427 428 429 430 431 432 433
  /* For IF-THEN-ELSE blocks, we don't allow modifications of the test
     on then THEN block.  */
  then_mod_ok = (else_bb == NULL_BLOCK);

  /* Go through the THEN and ELSE blocks converting the insns if possible
     to conditional execution.  */

  if (then_end
434 435 436
      && (! false_expr
	  || ! cond_exec_process_insns (then_start, then_end, false_expr,
					false_prob_val, then_mod_ok)))
Richard Henderson committed
437 438 439 440
    goto fail;

  if (else_bb
      && ! cond_exec_process_insns (else_start, else_end,
441
				    true_expr, true_prob_val, TRUE))
Richard Henderson committed
442 443 444 445 446
    goto fail;

  if (! apply_change_group ())
    return FALSE;

447 448 449 450 451
#ifdef IFCVT_MODIFY_FINAL
  /* Do any machine dependent final modifications */
  IFCVT_MODIFY_FINAL (test_bb, then_bb, else_bb, join_bb);
#endif

Richard Henderson committed
452 453 454 455 456 457 458 459 460 461
  /* Conversion succeeded.  */
  if (rtl_dump_file)
    fprintf (rtl_dump_file, "%d insn%s converted to conditional execution.\n",
	     n_insns, (n_insns == 1) ? " was" : "s were");

  /* Merge the blocks!  */
  merge_if_block (test_bb, then_bb, else_bb, join_bb);
  return TRUE;

 fail:
462 463 464 465 466
#ifdef IFCVT_MODIFY_CANCEL
  /* Cancel any machine dependent changes.  */
  IFCVT_MODIFY_CANCEL (test_bb, then_bb, else_bb, join_bb);
#endif

Richard Henderson committed
467 468 469 470 471 472 473 474 475 476 477 478
  cancel_changes (0);
  return FALSE;
}

/* Used by noce_process_if_block to communicate with its subroutines. 

   The subroutines know that A and B may be evaluated freely.  They
   know that X is a register.  They should insert new instructions 
   before cond_earliest.  */

struct noce_if_info
{
479
  basic_block test_bb;
Richard Henderson committed
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
  rtx insn_a, insn_b;
  rtx x, a, b;
  rtx jump, cond, cond_earliest;
};

static rtx noce_emit_store_flag		PARAMS ((struct noce_if_info *,
						 rtx, int, int));
static int noce_try_store_flag		PARAMS ((struct noce_if_info *));
static int noce_try_store_flag_inc	PARAMS ((struct noce_if_info *));
static int noce_try_store_flag_constants PARAMS ((struct noce_if_info *));
static int noce_try_store_flag_mask	PARAMS ((struct noce_if_info *));
static rtx noce_emit_cmove		PARAMS ((struct noce_if_info *,
						 rtx, enum rtx_code, rtx,
						 rtx, rtx, rtx));
static int noce_try_cmove		PARAMS ((struct noce_if_info *));
static int noce_try_cmove_arith		PARAMS ((struct noce_if_info *));
496 497 498 499
static rtx noce_get_alt_condition	PARAMS ((struct noce_if_info *,
						 rtx, rtx *));
static int noce_try_minmax		PARAMS ((struct noce_if_info *));
static int noce_try_abs			PARAMS ((struct noce_if_info *));
Richard Henderson committed
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519

/* Helper function for noce_try_store_flag*.  */

static rtx
noce_emit_store_flag (if_info, x, reversep, normalize)
     struct noce_if_info *if_info;
     rtx x;
     int reversep, normalize;
{
  rtx cond = if_info->cond;
  int cond_complex;
  enum rtx_code code;

  cond_complex = (! general_operand (XEXP (cond, 0), VOIDmode)
		  || ! general_operand (XEXP (cond, 1), VOIDmode));

  /* If earliest == jump, or when the condition is complex, try to
     build the store_flag insn directly.  */

  if (cond_complex)
520
    cond = XEXP (SET_SRC (pc_set (if_info->jump)), 0);
Richard Henderson committed
521

522 523 524 525 526
  if (reversep)
    code = reversed_comparison_code (cond, if_info->jump);
  else
    code = GET_CODE (cond);

Richard Henderson committed
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
  if ((if_info->cond_earliest == if_info->jump || cond_complex)
      && (normalize == 0 || STORE_FLAG_VALUE == normalize))
    {
      rtx tmp;

      tmp = gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (cond, 0),
			    XEXP (cond, 1));
      tmp = gen_rtx_SET (VOIDmode, x, tmp);

      start_sequence ();
      tmp = emit_insn (tmp);

      if (recog_memoized (tmp) >= 0)
	{
	  tmp = get_insns ();
	  end_sequence ();
	  emit_insns (tmp);

	  if_info->cond_earliest = if_info->jump;

	  return x;
	}

      end_sequence ();
    }

  /* Don't even try if the comparison operands are weird.  */
  if (cond_complex)
    return NULL_RTX;

  return emit_store_flag (x, code, XEXP (cond, 0),
			  XEXP (cond, 1), VOIDmode,
			  (code == LTU || code == LEU
			   || code == GEU || code == GTU), normalize);
}

563
/* Emit instruction to move an rtx into STRICT_LOW_PART.  */
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
static void
noce_emit_move_insn (x, y)
     rtx x, y;
{
  enum machine_mode outmode, inmode;
  rtx outer, inner;
  int bitpos;

  if (GET_CODE (x) != STRICT_LOW_PART)
    {
      emit_move_insn (x, y);
      return;
    }

  outer = XEXP (x, 0);
  inner = XEXP (outer, 0);
  outmode = GET_MODE (outer);
  inmode = GET_MODE (inner);
582
  bitpos = SUBREG_BYTE (outer) * BITS_PER_UNIT;
583
  store_bit_field (inner, GET_MODE_BITSIZE (outmode), bitpos, outmode, y,
584 585 586
		   GET_MODE_BITSIZE (inmode));
}

Richard Henderson committed
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
/* Convert "if (test) x = 1; else x = 0".

   Only try 0 and STORE_FLAG_VALUE here.  Other combinations will be
   tried in noce_try_store_flag_constants after noce_try_cmove has had
   a go at the conversion.  */

static int
noce_try_store_flag (if_info)
     struct noce_if_info *if_info;
{
  int reversep;
  rtx target, seq;

  if (GET_CODE (if_info->b) == CONST_INT
      && INTVAL (if_info->b) == STORE_FLAG_VALUE
      && if_info->a == const0_rtx)
    reversep = 0;
  else if (if_info->b == const0_rtx
	   && GET_CODE (if_info->a) == CONST_INT
	   && INTVAL (if_info->a) == STORE_FLAG_VALUE
607 608
	   && (reversed_comparison_code (if_info->cond, if_info->jump)
	       != UNKNOWN))
Richard Henderson committed
609 610 611 612 613 614 615 616 617 618
    reversep = 1;
  else
    return FALSE;

  start_sequence ();

  target = noce_emit_store_flag (if_info, if_info->x, reversep, 0);
  if (target)
    {
      if (target != if_info->x)
619
	noce_emit_move_insn (if_info->x, target);
Richard Henderson committed
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643

      seq = get_insns ();
      end_sequence ();
      emit_insns_before (seq, if_info->cond_earliest);

      return TRUE;
    }
  else
    {
      end_sequence ();
      return FALSE;
    }
}

/* Convert "if (test) x = a; else x = b", for A and B constant.  */

static int
noce_try_store_flag_constants (if_info)
     struct noce_if_info *if_info;
{
  rtx target, seq;
  int reversep;
  HOST_WIDE_INT itrue, ifalse, diff, tmp;
  int normalize, can_reverse;
644
  enum machine_mode mode;
Richard Henderson committed
645 646 647 648 649

  if (! no_new_pseudos
      && GET_CODE (if_info->a) == CONST_INT
      && GET_CODE (if_info->b) == CONST_INT)
    {
650
      mode = GET_MODE (if_info->x);
Richard Henderson committed
651 652
      ifalse = INTVAL (if_info->a);
      itrue = INTVAL (if_info->b);
653 654 655 656 657 658

      /* Make sure we can represent the difference between the two values.  */
      if ((itrue - ifalse > 0)
	  != ((ifalse < 0) != (itrue < 0) ? ifalse < 0 : ifalse < itrue))
	return FALSE;

659
      diff = trunc_int_for_mode (itrue - ifalse, mode);
Richard Henderson committed
660

661 662
      can_reverse = (reversed_comparison_code (if_info->cond, if_info->jump)
		     != UNKNOWN);
Richard Henderson committed
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689

      reversep = 0;
      if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
	normalize = 0;
      else if (ifalse == 0 && exact_log2 (itrue) >= 0
	       && (STORE_FLAG_VALUE == 1
		   || BRANCH_COST >= 2))
	normalize = 1;
      else if (itrue == 0 && exact_log2 (ifalse) >= 0 && can_reverse
	       && (STORE_FLAG_VALUE == 1 || BRANCH_COST >= 2))
	normalize = 1, reversep = 1;
      else if (itrue == -1
	       && (STORE_FLAG_VALUE == -1
		   || BRANCH_COST >= 2))
	normalize = -1;
      else if (ifalse == -1 && can_reverse
	       && (STORE_FLAG_VALUE == -1 || BRANCH_COST >= 2))
	normalize = -1, reversep = 1;
      else if ((BRANCH_COST >= 2 && STORE_FLAG_VALUE == -1)
	       || BRANCH_COST >= 3)
	normalize = -1;
      else
	return FALSE;

      if (reversep)
      	{
	  tmp = itrue; itrue = ifalse; ifalse = tmp;
690
	  diff = trunc_int_for_mode (-diff, mode);
Richard Henderson committed
691 692 693 694 695 696 697 698 699 700 701 702 703 704
	}

      start_sequence ();
      target = noce_emit_store_flag (if_info, if_info->x, reversep, normalize);
      if (! target)
	{
	  end_sequence ();
	  return FALSE;
	}

      /* if (test) x = 3; else x = 4;
	 =>   x = 3 + (test == 0);  */
      if (diff == STORE_FLAG_VALUE || diff == -STORE_FLAG_VALUE)
	{
705 706 707 708 709
	  target = expand_simple_binop (mode,
					(diff == STORE_FLAG_VALUE
					 ? PLUS : MINUS),
					GEN_INT (ifalse), target, if_info->x, 0,
					OPTAB_WIDEN);
Richard Henderson committed
710 711 712 713 714 715
	}

      /* if (test) x = 8; else x = 0;
	 =>   x = (test != 0) << 3;  */
      else if (ifalse == 0 && (tmp = exact_log2 (itrue)) >= 0)
	{
716 717 718
	  target = expand_simple_binop (mode, ASHIFT,
					target, GEN_INT (tmp), if_info->x, 0,
					OPTAB_WIDEN);
Richard Henderson committed
719 720 721 722 723 724
	}

      /* if (test) x = -1; else x = b;
	 =>   x = -(test != 0) | b;  */
      else if (itrue == -1)
	{
725 726 727
	  target = expand_simple_binop (mode, IOR,
					target, GEN_INT (ifalse), if_info->x, 0,
					OPTAB_WIDEN);
Richard Henderson committed
728 729 730 731 732 733
	}

      /* if (test) x = a; else x = b;
	 =>   x = (-(test != 0) & (b - a)) + a;  */
      else
	{
734 735 736
	  target = expand_simple_binop (mode, AND,
					target, GEN_INT (diff), if_info->x, 0,
					OPTAB_WIDEN);
Richard Henderson committed
737
	  if (target)
738 739 740
	    target = expand_simple_binop (mode, PLUS,
					  target, GEN_INT (ifalse),
					  if_info->x, 0, OPTAB_WIDEN);
Richard Henderson committed
741 742 743 744 745 746 747 748 749
	}

      if (! target)
	{
	  end_sequence ();
	  return FALSE;
	}

      if (target != if_info->x)
750
	noce_emit_move_insn (if_info->x, target);
Richard Henderson committed
751 752 753

      seq = get_insns ();
      end_sequence ();
754 755 756 757

      if (seq_contains_jump (seq))
	return FALSE;

Richard Henderson committed
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
      emit_insns_before (seq, if_info->cond_earliest);

      return TRUE;
    }

  return FALSE;
}

/* Convert "if (test) foo++" into "foo += (test != 0)", and 
   similarly for "foo--".  */

static int
noce_try_store_flag_inc (if_info)
     struct noce_if_info *if_info;
{
  rtx target, seq;
  int subtract, normalize;

  if (! no_new_pseudos
      && (BRANCH_COST >= 2
	  || HAVE_incscc
	  || HAVE_decscc)
      /* Should be no `else' case to worry about.  */
      && if_info->b == if_info->x
      && GET_CODE (if_info->a) == PLUS
      && (XEXP (if_info->a, 1) == const1_rtx
	  || XEXP (if_info->a, 1) == constm1_rtx)
      && rtx_equal_p (XEXP (if_info->a, 0), if_info->x)
786 787
      && (reversed_comparison_code (if_info->cond, if_info->jump)
	  != UNKNOWN))
Richard Henderson committed
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
    {
      if (STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
	subtract = 0, normalize = 0;
      else if (-STORE_FLAG_VALUE == INTVAL (XEXP (if_info->a, 1)))
	subtract = 1, normalize = 0;
      else
	subtract = 0, normalize = INTVAL (XEXP (if_info->a, 1));
      
      start_sequence ();

      target = noce_emit_store_flag (if_info,
				     gen_reg_rtx (GET_MODE (if_info->x)),
				     1, normalize);

      if (target)
803 804 805 806
	target = expand_simple_binop (GET_MODE (if_info->x),
				      subtract ? MINUS : PLUS,
				      if_info->x, target, if_info->x,
				      0, OPTAB_WIDEN);
Richard Henderson committed
807 808 809
      if (target)
	{
	  if (target != if_info->x)
810
	    noce_emit_move_insn (if_info->x, target);
Richard Henderson committed
811 812 813

	  seq = get_insns ();
	  end_sequence ();
814 815 816 817

	  if (seq_contains_jump (seq))
	    return FALSE;

Richard Henderson committed
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
	  emit_insns_before (seq, if_info->cond_earliest);

	  return TRUE;
	}

      end_sequence ();
    }

  return FALSE;
}

/* Convert "if (test) x = 0;" to "x &= -(test == 0);"  */

static int
noce_try_store_flag_mask (if_info)
     struct noce_if_info *if_info;
{
  rtx target, seq;
  int reversep;

  reversep = 0;
  if (! no_new_pseudos
      && (BRANCH_COST >= 2
	  || STORE_FLAG_VALUE == -1)
      && ((if_info->a == const0_rtx
	   && rtx_equal_p (if_info->b, if_info->x))
844 845 846
	  || ((reversep = (reversed_comparison_code (if_info->cond,
						     if_info->jump)
			   != UNKNOWN))
Richard Henderson committed
847 848 849 850 851 852 853 854
	      && if_info->b == const0_rtx
	      && rtx_equal_p (if_info->a, if_info->x))))
    {
      start_sequence ();
      target = noce_emit_store_flag (if_info,
				     gen_reg_rtx (GET_MODE (if_info->x)),
				     reversep, -1);
      if (target)
855 856 857
        target = expand_simple_binop (GET_MODE (if_info->x), AND,
				      if_info->x, target, if_info->x, 0,
				      OPTAB_WIDEN);
Richard Henderson committed
858 859 860 861

      if (target)
	{
	  if (target != if_info->x)
862
	    noce_emit_move_insn (if_info->x, target);
Richard Henderson committed
863 864 865

	  seq = get_insns ();
	  end_sequence ();
866 867 868 869

	  if (seq_contains_jump (seq))
	    return FALSE;

Richard Henderson committed
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 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
	  emit_insns_before (seq, if_info->cond_earliest);

	  return TRUE;
	}

      end_sequence ();
    }

  return FALSE;
}

/* Helper function for noce_try_cmove and noce_try_cmove_arith.  */

static rtx
noce_emit_cmove (if_info, x, code, cmp_a, cmp_b, vfalse, vtrue)
     struct noce_if_info *if_info;
     rtx x, cmp_a, cmp_b, vfalse, vtrue;
     enum rtx_code code;
{
  /* If earliest == jump, try to build the cmove insn directly.
     This is helpful when combine has created some complex condition
     (like for alpha's cmovlbs) that we can't hope to regenerate
     through the normal interface.  */

  if (if_info->cond_earliest == if_info->jump)
    {
      rtx tmp;

      tmp = gen_rtx_fmt_ee (code, GET_MODE (if_info->cond), cmp_a, cmp_b);
      tmp = gen_rtx_IF_THEN_ELSE (GET_MODE (x), tmp, vtrue, vfalse);
      tmp = gen_rtx_SET (VOIDmode, x, tmp);

      start_sequence ();
      tmp = emit_insn (tmp);

      if (recog_memoized (tmp) >= 0)
	{
	  tmp = get_insns ();
	  end_sequence ();
	  emit_insns (tmp);

	  return x;
	}

      end_sequence ();
    }

  /* Don't even try if the comparison operands are weird.  */
  if (! general_operand (cmp_a, GET_MODE (cmp_a))
      || ! general_operand (cmp_b, GET_MODE (cmp_b)))
    return NULL_RTX;

922
#if HAVE_conditional_move
Richard Henderson committed
923 924 925 926
  return emit_conditional_move (x, code, cmp_a, cmp_b, VOIDmode,
				vtrue, vfalse, GET_MODE (x),
			        (code == LTU || code == GEU
				 || code == LEU || code == GTU));
927 928 929 930 931 932 933
#else
  /* We'll never get here, as noce_process_if_block doesn't call the
     functions involved.  Ifdef code, however, should be discouraged
     because it leads to typos in the code not selected.  However, 
     emit_conditional_move won't exist either.  */
  return NULL_RTX;
#endif
Richard Henderson committed
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
}

/* Try only simple constants and registers here.  More complex cases
   are handled in noce_try_cmove_arith after noce_try_store_flag_arith
   has had a go at it.  */

static int
noce_try_cmove (if_info)
     struct noce_if_info *if_info;
{
  enum rtx_code code;
  rtx target, seq;

  if ((CONSTANT_P (if_info->a) || register_operand (if_info->a, VOIDmode))
      && (CONSTANT_P (if_info->b) || register_operand (if_info->b, VOIDmode)))
    {
      start_sequence ();

      code = GET_CODE (if_info->cond);
      target = noce_emit_cmove (if_info, if_info->x, code,
				XEXP (if_info->cond, 0),
				XEXP (if_info->cond, 1),
				if_info->a, if_info->b);

      if (target)
	{
	  if (target != if_info->x)
961
	    noce_emit_move_insn (if_info->x, target);
Richard Henderson committed
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006

	  seq = get_insns ();
	  end_sequence ();
	  emit_insns_before (seq, if_info->cond_earliest);
	  return TRUE;
	}
      else
	{
	  end_sequence ();
	  return FALSE;
	}
    }

  return FALSE;
}

/* Try more complex cases involving conditional_move.  */

static int
noce_try_cmove_arith (if_info)
     struct noce_if_info *if_info;
{
  rtx a = if_info->a;
  rtx b = if_info->b;
  rtx x = if_info->x;
  rtx insn_a, insn_b;
  rtx tmp, target;
  int is_mem = 0;
  enum rtx_code code;

  /* A conditional move from two memory sources is equivalent to a
     conditional on their addresses followed by a load.  Don't do this
     early because it'll screw alias analysis.  Note that we've
     already checked for no side effects.  */
  if (! no_new_pseudos && cse_not_expected
      && GET_CODE (a) == MEM && GET_CODE (b) == MEM
      && BRANCH_COST >= 5)
    {
      a = XEXP (a, 0);
      b = XEXP (b, 0);
      x = gen_reg_rtx (Pmode);
      is_mem = 1;
    }

  /* ??? We could handle this if we knew that a load from A or B could
1007
     not fault.  This is also true if we've already loaded
Richard Henderson committed
1008
     from the address along the path from ENTRY.  */
1009
  else if (may_trap_p (a) || may_trap_p (b))
Richard Henderson committed
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
    return FALSE;

  /* if (test) x = a + b; else x = c - d;
     => y = a + b;
        x = c - d;
	if (test)
	  x = y;
  */
  
  code = GET_CODE (if_info->cond);
  insn_a = if_info->insn_a;
  insn_b = if_info->insn_b;

  /* Possibly rearrange operands to make things come out more natural.  */
1024
  if (reversed_comparison_code (if_info->cond, if_info->jump) != UNKNOWN)
Richard Henderson committed
1025 1026 1027 1028 1029 1030 1031 1032 1033
    {
      int reversep = 0;
      if (rtx_equal_p (b, x))
	reversep = 1;
      else if (general_operand (b, GET_MODE (b)))
	reversep = 1;

      if (reversep)
	{
1034
	  code = reversed_comparison_code (if_info->cond, if_info->jump);
Richard Henderson committed
1035 1036 1037 1038 1039 1040 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 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
	  tmp = a, a = b, b = tmp;
	  tmp = insn_a, insn_a = insn_b, insn_b = tmp;
	}
    }

  start_sequence ();

  /* If either operand is complex, load it into a register first.
     The best way to do this is to copy the original insn.  In this
     way we preserve any clobbers etc that the insn may have had.  
     This is of course not possible in the IS_MEM case.  */
  if (! general_operand (a, GET_MODE (a)))
    {
      rtx set;

      if (no_new_pseudos)
	goto end_seq_and_fail;

      if (is_mem)
	{
	  tmp = gen_reg_rtx (GET_MODE (a));
	  tmp = emit_insn (gen_rtx_SET (VOIDmode, tmp, a));
	}
      else if (! insn_a)
	goto end_seq_and_fail;
      else
	{
	  a = gen_reg_rtx (GET_MODE (a));
	  tmp = copy_rtx (insn_a);
	  set = single_set (tmp);
	  SET_DEST (set) = a;
	  tmp = emit_insn (PATTERN (tmp));
	}
      if (recog_memoized (tmp) < 0)
	goto end_seq_and_fail;
    }
  if (! general_operand (b, GET_MODE (b)))
    {
      rtx set;

      if (no_new_pseudos)
	goto end_seq_and_fail;

      if (is_mem)
	{
          tmp = gen_reg_rtx (GET_MODE (b));
	  tmp = emit_insn (gen_rtx_SET (VOIDmode, tmp, b));
	}
      else if (! insn_b)
	goto end_seq_and_fail;
      else
	{
          b = gen_reg_rtx (GET_MODE (b));
	  tmp = copy_rtx (insn_b);
	  set = single_set (tmp);
	  SET_DEST (set) = b;
	  tmp = emit_insn (PATTERN (tmp));
	}
      if (recog_memoized (tmp) < 0)
	goto end_seq_and_fail;
    }

  target = noce_emit_cmove (if_info, x, code, XEXP (if_info->cond, 0),
			    XEXP (if_info->cond, 1), a, b);

  if (! target)
    goto end_seq_and_fail;

  /* If we're handling a memory for above, emit the load now.  */
  if (is_mem)
    {
      tmp = gen_rtx_MEM (GET_MODE (if_info->x), target);

      /* Copy over flags as appropriate.  */
      if (MEM_VOLATILE_P (if_info->a) || MEM_VOLATILE_P (if_info->b))
	MEM_VOLATILE_P (tmp) = 1;
      if (MEM_IN_STRUCT_P (if_info->a) && MEM_IN_STRUCT_P (if_info->b))
	MEM_IN_STRUCT_P (tmp) = 1;
      if (MEM_SCALAR_P (if_info->a) && MEM_SCALAR_P (if_info->b))
	MEM_SCALAR_P (tmp) = 1;
      if (MEM_ALIAS_SET (if_info->a) == MEM_ALIAS_SET (if_info->b))
1116
	set_mem_alias_set (tmp, MEM_ALIAS_SET (if_info->a));
1117 1118
      set_mem_align (tmp,
		     MIN (MEM_ALIGN (if_info->a), MEM_ALIGN (if_info->b)));
Richard Henderson committed
1119

1120
      noce_emit_move_insn (if_info->x, tmp);
Richard Henderson committed
1121 1122
    }
  else if (target != x)
1123
    noce_emit_move_insn (x, target);
Richard Henderson committed
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134

  tmp = get_insns ();
  end_sequence ();
  emit_insns_before (tmp, if_info->cond_earliest);
  return TRUE;

 end_seq_and_fail:
  end_sequence ();
  return FALSE;
}

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
/* For most cases, the simplified condition we found is the best
   choice, but this is not the case for the min/max/abs transforms.
   For these we wish to know that it is A or B in the condition.  */

static rtx
noce_get_alt_condition (if_info, target, earliest)
     struct noce_if_info *if_info;
     rtx target;
     rtx *earliest;
{
  rtx cond, set, insn;
  int reverse;

  /* If target is already mentioned in the known condition, return it.  */
  if (reg_mentioned_p (target, if_info->cond))
    {
      *earliest = if_info->cond_earliest;
      return if_info->cond;
    }

  set = pc_set (if_info->jump);
  cond = XEXP (SET_SRC (set), 0);
  reverse
    = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
      && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (if_info->jump);

1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
  /* If we're looking for a constant, try to make the conditional
     have that constant in it.  There are two reasons why it may
     not have the constant we want:

     1. GCC may have needed to put the constant in a register, because
        the target can't compare directly against that constant.  For
        this case, we look for a SET immediately before the comparison
        that puts a constant in that register.

     2. GCC may have canonicalized the conditional, for example
	replacing "if x < 4" with "if x <= 3".  We can undo that (or
	make equivalent types of changes) to get the constants we need
	if they're off by one in the right direction.  */

  if (GET_CODE (target) == CONST_INT)
    {
      enum rtx_code code = GET_CODE (if_info->cond);
      rtx op_a = XEXP (if_info->cond, 0);
      rtx op_b = XEXP (if_info->cond, 1);
      rtx prev_insn;

      /* First, look to see if we put a constant in a register.  */
      prev_insn = PREV_INSN (if_info->cond_earliest);
      if (prev_insn
	  && INSN_P (prev_insn)
	  && GET_CODE (PATTERN (prev_insn)) == SET)
	{
	  rtx src = find_reg_equal_equiv_note (prev_insn);
	  if (!src)
	    src = SET_SRC (PATTERN (prev_insn));
	  if (GET_CODE (src) == CONST_INT)
	    {
	      if (rtx_equal_p (op_a, SET_DEST (PATTERN (prev_insn))))
1194
		op_a = src;
1195
	      else if (rtx_equal_p (op_b, SET_DEST (PATTERN (prev_insn))))
1196
		op_b = src;
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 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 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262

	      if (GET_CODE (op_a) == CONST_INT)
		{
		  rtx tmp = op_a;
		  op_a = op_b;
		  op_b = tmp;
		  code = swap_condition (code);
		}
	    }
	}

      /* Now, look to see if we can get the right constant by
	 adjusting the conditional.  */
      if (GET_CODE (op_b) == CONST_INT)
	{
	  HOST_WIDE_INT desired_val = INTVAL (target);
	  HOST_WIDE_INT actual_val = INTVAL (op_b);

	  switch (code)
	    {
	    case LT:
	      if (actual_val == desired_val + 1)
		{
		  code = LE;
		  op_b = GEN_INT (desired_val);
		}
	      break;
	    case LE:
	      if (actual_val == desired_val - 1)
		{
		  code = LT;
		  op_b = GEN_INT (desired_val);
		}
	      break;
	    case GT:
	      if (actual_val == desired_val - 1)
		{
		  code = GE;
		  op_b = GEN_INT (desired_val);
		}
	      break;
	    case GE:
	      if (actual_val == desired_val + 1)
		{
		  code = GT;
		  op_b = GEN_INT (desired_val);
		}
	      break;
	    default:
	      break;
	    }
	}

      /* If we made any changes, generate a new conditional that is
	 equivalent to what we started with, but has the right
	 constants in it.  */
      if (code != GET_CODE (if_info->cond)
	  || op_a != XEXP (if_info->cond, 0)
	  || op_b != XEXP (if_info->cond, 1))
	{
	  cond = gen_rtx_fmt_ee (code, GET_MODE (cond), op_a, op_b);
	  *earliest = if_info->cond_earliest;
	  return cond;
	}
    }

1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
  cond = canonicalize_condition (if_info->jump, cond, reverse,
				 earliest, target);
  if (! cond || ! reg_mentioned_p (target, cond))
    return NULL;

  /* We almost certainly searched back to a different place.
     Need to re-verify correct lifetimes.  */

  /* X may not be mentioned in the range (cond_earliest, jump].  */
  for (insn = if_info->jump; insn != *earliest; insn = PREV_INSN (insn))
    if (INSN_P (insn) && reg_mentioned_p (if_info->x, insn))
      return NULL;

  /* A and B may not be modified in the range [cond_earliest, jump).  */
  for (insn = *earliest; insn != if_info->jump; insn = NEXT_INSN (insn))
    if (INSN_P (insn)
	&& (modified_in_p (if_info->a, insn)
	    || modified_in_p (if_info->b, insn)))
      return NULL;

  return cond;
}

/* Convert "if (a < b) x = a; else x = b;" to "x = min(a, b);", etc.  */

static int
noce_try_minmax (if_info)
     struct noce_if_info *if_info;
{ 
  rtx cond, earliest, target, seq;
1293
  enum rtx_code code, op;
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
  int unsignedp;

  /* ??? Can't guarantee that expand_binop won't create pseudos.  */
  if (no_new_pseudos)
    return FALSE;

  /* ??? Reject FP modes since we don't know how 0 vs -0 or NaNs
     will be resolved with an SMIN/SMAX.  It wouldn't be too hard
     to get the target to tell us...  */
  if (FLOAT_MODE_P (GET_MODE (if_info->x))
      && TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1305
      && ! flag_unsafe_math_optimizations)
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
    return FALSE;

  cond = noce_get_alt_condition (if_info, if_info->a, &earliest);
  if (!cond)
    return FALSE;

  /* Verify the condition is of the form we expect, and canonicalize
     the comparison code.  */
  code = GET_CODE (cond);
  if (rtx_equal_p (XEXP (cond, 0), if_info->a))
    {
      if (! rtx_equal_p (XEXP (cond, 1), if_info->b))
	return FALSE;
    }
  else if (rtx_equal_p (XEXP (cond, 1), if_info->a))
    {
      if (! rtx_equal_p (XEXP (cond, 0), if_info->b))
	return FALSE;
      code = swap_condition (code);
    }
  else
    return FALSE;

  /* Determine what sort of operation this is.  Note that the code is for
     a taken branch, so the code->operation mapping appears backwards.  */
  switch (code)
    {
    case LT:
    case LE:
    case UNLT:
    case UNLE:
1337
      op = SMAX;
1338 1339 1340 1341 1342 1343
      unsignedp = 0;
      break;
    case GT:
    case GE:
    case UNGT:
    case UNGE:
1344
      op = SMIN;
1345 1346 1347 1348
      unsignedp = 0;
      break;
    case LTU:
    case LEU:
1349
      op = UMAX;
1350 1351 1352 1353
      unsignedp = 1;
      break;
    case GTU:
    case GEU:
1354
      op = UMIN;
1355 1356 1357 1358 1359 1360 1361 1362
      unsignedp = 1;
      break;
    default:
      return FALSE;
    }

  start_sequence ();

1363 1364 1365
  target = expand_simple_binop (GET_MODE (if_info->x), op,
				if_info->a, if_info->b,
				if_info->x, unsignedp, OPTAB_WIDEN);
1366 1367 1368 1369 1370 1371
  if (! target)
    {
      end_sequence ();
      return FALSE;
    }
  if (target != if_info->x)
1372
    noce_emit_move_insn (if_info->x, target);
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475

  seq = get_insns ();
  end_sequence ();  

  if (seq_contains_jump (seq))
    return FALSE;

  emit_insns_before (seq, earliest);
  if_info->cond = cond;
  if_info->cond_earliest = earliest;

  return TRUE;
}

/* Convert "if (a < 0) x = -a; else x = a;" to "x = abs(a);", etc.  */

static int
noce_try_abs (if_info)
     struct noce_if_info *if_info;
{ 
  rtx cond, earliest, target, seq, a, b, c;
  int negate;

  /* ??? Can't guarantee that expand_binop won't create pseudos.  */
  if (no_new_pseudos)
    return FALSE;

  /* Recognize A and B as constituting an ABS or NABS.  */
  a = if_info->a;
  b = if_info->b;
  if (GET_CODE (a) == NEG && rtx_equal_p (XEXP (a, 0), b))
    negate = 0;
  else if (GET_CODE (b) == NEG && rtx_equal_p (XEXP (b, 0), a))
    {
      c = a; a = b; b = c;
      negate = 1;
    }
  else
    return FALSE;
   
  cond = noce_get_alt_condition (if_info, b, &earliest);
  if (!cond)
    return FALSE;

  /* Verify the condition is of the form we expect.  */
  if (rtx_equal_p (XEXP (cond, 0), b))
    c = XEXP (cond, 1);
  else if (rtx_equal_p (XEXP (cond, 1), b))
    c = XEXP (cond, 0);
  else
    return FALSE;

  /* Verify that C is zero.  Search backward through the block for
     a REG_EQUAL note if necessary.  */
  if (REG_P (c))
    {
      rtx insn, note = NULL;
      for (insn = earliest;
	   insn != if_info->test_bb->head;
	   insn = PREV_INSN (insn))
	if (INSN_P (insn) 
	    && ((note = find_reg_note (insn, REG_EQUAL, c))
		|| (note = find_reg_note (insn, REG_EQUIV, c))))
	  break;
      if (! note)
	return FALSE;
      c = XEXP (note, 0);
    }
  if (GET_CODE (c) == MEM
      && GET_CODE (XEXP (c, 0)) == SYMBOL_REF
      && CONSTANT_POOL_ADDRESS_P (XEXP (c, 0)))
    c = get_pool_constant (XEXP (c, 0));

  /* Work around funny ideas get_condition has wrt canonicalization.
     Note that these rtx constants are known to be CONST_INT, and 
     therefore imply integer comparisons.  */
  if (c == constm1_rtx && GET_CODE (cond) == GT)
    ;
  else if (c == const1_rtx && GET_CODE (cond) == LT)
    ;
  else if (c != CONST0_RTX (GET_MODE (b)))
    return FALSE;

  /* Determine what sort of operation this is.  */
  switch (GET_CODE (cond))
    {
    case LT:
    case LE:
    case UNLT:
    case UNLE:
      negate = !negate;
      break;
    case GT:
    case GE:
    case UNGT:
    case UNGE:
      break;
    default:
      return FALSE;
    }

  start_sequence ();

1476
  target = expand_simple_unop (GET_MODE (if_info->x), ABS, b, if_info->x, 0);
1477 1478 1479 1480

  /* ??? It's a quandry whether cmove would be better here, especially
     for integers.  Perhaps combine will clean things up.  */
  if (target && negate)
1481
    target = expand_simple_unop (GET_MODE (target), NEG, target, if_info->x, 0);
1482 1483 1484 1485 1486 1487 1488 1489

  if (! target)
    {
      end_sequence ();
      return FALSE;
    }

  if (target != if_info->x)
1490
    noce_emit_move_insn (if_info->x, target);
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504

  seq = get_insns ();
  end_sequence ();  

  if (seq_contains_jump (seq))
    return FALSE;

  emit_insns_before (seq, earliest);
  if_info->cond = cond;
  if_info->cond_earliest = earliest;

  return TRUE;
}

Richard Henderson committed
1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
/* Look for the condition for the jump first.  We'd prefer to avoid
   get_condition if we can -- it tries to look back for the contents
   of an original compare.  On targets that use normal integers for
   comparisons, e.g. alpha, this is wasteful.  */

static rtx
noce_get_condition (jump, earliest)
     rtx jump;
     rtx *earliest;
{
  rtx cond;
1516
  rtx set;
Richard Henderson committed
1517 1518 1519 1520

  /* If the condition variable is a register and is MODE_INT, accept it.
     Otherwise, fall back on get_condition.  */

1521
  if (! any_condjump_p (jump))
Richard Henderson committed
1522 1523
    return NULL_RTX;

1524 1525 1526
  set = pc_set (jump);

  cond = XEXP (SET_SRC (set), 0);
Richard Henderson committed
1527 1528 1529 1530 1531 1532 1533
  if (GET_CODE (XEXP (cond, 0)) == REG
      && GET_MODE_CLASS (GET_MODE (XEXP (cond, 0))) == MODE_INT)
    {
      *earliest = jump;

      /* If this branches to JUMP_LABEL when the condition is false,
	 reverse the condition.  */
1534 1535
      if (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
	  && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (jump))
Richard Henderson committed
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
	cond = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond)),
			       GET_MODE (cond), XEXP (cond, 0),
			       XEXP (cond, 1));
    }
  else
    cond = get_condition (jump, earliest);

  return cond;
}

1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
/* Return true if OP is ok for if-then-else processing.  */

static int
noce_operand_ok (op)
     rtx op;
{
  /* We special-case memories, so handle any of them with
     no address side effects.  */
  if (GET_CODE (op) == MEM)
    return ! side_effects_p (XEXP (op, 0));

  if (side_effects_p (op))
    return FALSE;

1560
  /* ??? Unfortuantely may_trap_p can't look at flag_trapping_math, due to
1561 1562
     being linked into the genfoo programs.  This is probably a mistake.
     With finite operands, most fp operations don't trap.  */
1563
  if (!flag_trapping_math && FLOAT_MODE_P (GET_MODE (op)))
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
    switch (GET_CODE (op))
      {
      case DIV:
      case MOD:
      case UDIV:
      case UMOD:
	/* ??? This is kinda lame -- almost every target will have forced
	   the constant into a register first.  But given the expense of
	   division, this is probably for the best.  */
	return (CONSTANT_P (XEXP (op, 1))
		&& XEXP (op, 1) != CONST0_RTX (GET_MODE (op))
		&& ! may_trap_p (XEXP (op, 0)));

      default:
	switch (GET_RTX_CLASS (GET_CODE (op)))
	  {
	  case '1':
1581 1582
	    return ! may_trap_p (XEXP (op, 0));
	  case 'c':
1583 1584 1585 1586 1587 1588 1589 1590 1591
	  case '2':
	    return ! may_trap_p (XEXP (op, 0)) && ! may_trap_p (XEXP (op, 1));
	  }
	break;
      }

  return ! may_trap_p (op);
}

Richard Henderson committed
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616
/* Given a simple IF-THEN or IF-THEN-ELSE block, attempt to convert it
   without using conditional execution.  Return TRUE if we were
   successful at converting the the block.  */

static int
noce_process_if_block (test_bb, then_bb, else_bb, join_bb)
     basic_block test_bb;	/* Basic block test is in */
     basic_block then_bb;	/* Basic block for THEN block */
     basic_block else_bb;	/* Basic block for ELSE block */
     basic_block join_bb;	/* Basic block the join label is in */
{
  /* We're looking for patterns of the form

     (1) if (...) x = a; else x = b;
     (2) x = b; if (...) x = a;
     (3) if (...) x = a;   // as if with an initial x = x.

     The later patterns require jumps to be more expensive.

     ??? For future expansion, look for multiple X in such patterns.  */

  struct noce_if_info if_info;
  rtx insn_a, insn_b;
  rtx set_a, set_b;
  rtx orig_x, x, a, b;
1617
  rtx jump, cond, insn;
Richard Henderson committed
1618 1619 1620 1621 1622 1623 1624

  /* If this is not a standard conditional jump, we can't parse it.  */
  jump = test_bb->end;
  cond = noce_get_condition (jump, &if_info.cond_earliest);
  if (! cond)
    return FALSE;

1625 1626 1627 1628 1629
  /* If the conditional jump is more than just a conditional jump,
     then we can not do if-conversion on this block.  */
  if (! onlyjump_p (jump))
    return FALSE;

Richard Henderson committed
1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
  /* We must be comparing objects whose modes imply the size.  */
  if (GET_MODE (XEXP (cond, 0)) == BLKmode)
    return FALSE;

  /* Look for one of the potential sets.  */
  insn_a = first_active_insn (then_bb);
  if (! insn_a
      || ! last_active_insn_p (then_bb, insn_a)
      || (set_a = single_set (insn_a)) == NULL_RTX)
    return FALSE;

  x = SET_DEST (set_a);
  a = SET_SRC (set_a);

  /* Look for the other potential set.  Make sure we've got equivalent
     destinations.  */
  /* ??? This is overconservative.  Storing to two different mems is
     as easy as conditionally computing the address.  Storing to a
     single mem merely requires a scratch memory to use as one of the
     destination addresses; often the memory immediately below the
     stack pointer is available for this.  */
  set_b = NULL_RTX;
  if (else_bb)
    {
      insn_b = first_active_insn (else_bb);
      if (! insn_b
	  || ! last_active_insn_p (else_bb, insn_b)
	  || (set_b = single_set (insn_b)) == NULL_RTX
	  || ! rtx_equal_p (x, SET_DEST (set_b)))
	return FALSE;
    }
  else
    {
      insn_b = prev_nonnote_insn (if_info.cond_earliest);
      if (! insn_b
	  || GET_CODE (insn_b) != INSN
	  || (set_b = single_set (insn_b)) == NULL_RTX
	  || ! rtx_equal_p (x, SET_DEST (set_b))
1668
	  || reg_mentioned_p (x, cond)
1669 1670
	  || reg_mentioned_p (x, a)
	  || reg_mentioned_p (x, SET_SRC (set_b)))
Richard Henderson committed
1671 1672 1673 1674
	insn_b = set_b = NULL_RTX;
    }
  b = (set_b ? SET_SRC (set_b) : x);

1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685
  /* X may not be mentioned in the range (cond_earliest, jump].  */
  for (insn = jump; insn != if_info.cond_earliest; insn = PREV_INSN (insn))
    if (INSN_P (insn) && reg_mentioned_p (x, insn))
      return FALSE;

  /* A and B may not be modified in the range [cond_earliest, jump).  */
  for (insn = if_info.cond_earliest; insn != jump; insn = NEXT_INSN (insn))
    if (INSN_P (insn)
	&& (modified_in_p (a, insn) || modified_in_p (b, insn)))
      return FALSE;

Richard Henderson committed
1686 1687 1688 1689 1690 1691 1692 1693 1694
  /* Only operate on register destinations, and even then avoid extending
     the lifetime of hard registers on small register class machines.  */
  orig_x = x;
  if (GET_CODE (x) != REG
      || (SMALL_REGISTER_CLASSES
	  && REGNO (x) < FIRST_PSEUDO_REGISTER))
    {
      if (no_new_pseudos)
	return FALSE;
1695 1696
      x = gen_reg_rtx (GET_MODE (GET_CODE (x) == STRICT_LOW_PART
				 ? XEXP (x, 0) : x));
Richard Henderson committed
1697 1698 1699
    }

  /* Don't operate on sources that may trap or are volatile.  */
1700
  if (! noce_operand_ok (a) || ! noce_operand_ok (b))
Richard Henderson committed
1701 1702 1703
    return FALSE;

  /* Set up the info block for our subroutines.  */
1704
  if_info.test_bb = test_bb;
Richard Henderson committed
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726
  if_info.cond = cond;
  if_info.jump = jump;
  if_info.insn_a = insn_a;
  if_info.insn_b = insn_b;
  if_info.x = x;
  if_info.a = a;
  if_info.b = b;

  /* Try optimizations in some approximation of a useful order.  */
  /* ??? Should first look to see if X is live incoming at all.  If it
     isn't, we don't need anything but an unconditional set.  */

  /* Look and see if A and B are really the same.  Avoid creating silly
     cmove constructs that no one will fix up later.  */
  if (rtx_equal_p (a, b))
    {
      /* If we have an INSN_B, we don't have to create any new rtl.  Just
	 move the instruction that we already have.  If we don't have an
	 INSN_B, that means that A == X, and we've got a noop move.  In
	 that case don't do anything and let the code below delete INSN_A.  */
      if (insn_b && else_bb)
	{
1727 1728
	  rtx note;

Richard Henderson committed
1729 1730 1731
	  if (else_bb && insn_b == else_bb->end)
	    else_bb->end = PREV_INSN (insn_b);
	  reorder_insns (insn_b, insn_b, PREV_INSN (if_info.cond_earliest));
1732 1733 1734 1735 1736 1737

	  /* If there was a REG_EQUAL note, delete it since it may have been
	     true due to this insn being after a jump.  */
	  if ((note = find_reg_note (insn_b, REG_EQUAL, NULL_RTX)) != 0)
	    remove_note (insn_b, note);

Richard Henderson committed
1738 1739
	  insn_b = NULL_RTX;
	}
1740 1741 1742 1743 1744
      /* If we have "x = b; if (...) x = a;", and x has side-effects, then
	 x must be executed twice.  */
      else if (insn_b && side_effects_p (orig_x))
	return FALSE;
	
1745
      x = orig_x;
Richard Henderson committed
1746 1747 1748 1749 1750
      goto success;
    }

  if (noce_try_store_flag (&if_info))
    goto success;
1751 1752 1753 1754
  if (noce_try_minmax (&if_info))
    goto success;
  if (noce_try_abs (&if_info))
    goto success;
Richard Henderson committed
1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774
  if (HAVE_conditional_move
      && noce_try_cmove (&if_info))
    goto success;
  if (! HAVE_conditional_execution)
    {
      if (noce_try_store_flag_constants (&if_info))
	goto success;
      if (noce_try_store_flag_inc (&if_info))
	goto success;
      if (noce_try_store_flag_mask (&if_info))
	goto success;
      if (HAVE_conditional_move
	  && noce_try_cmove_arith (&if_info))
	goto success;
    }

  return FALSE;

 success:
  /* The original sets may now be killed.  */
1775
  delete_insn (insn_a);
Richard Henderson committed
1776 1777 1778 1779 1780 1781 1782 1783

  /* Several special cases here: First, we may have reused insn_b above,
     in which case insn_b is now NULL.  Second, we want to delete insn_b
     if it came from the ELSE block, because follows the now correct
     write that appears in the TEST block.  However, if we got insn_b from
     the TEST block, it may in fact be loading data needed for the comparison.
     We'll let life_analysis remove the insn if it's really dead.  */
  if (insn_b && else_bb)
1784
    delete_insn (insn_b);
Richard Henderson committed
1785 1786

  /* The new insns will have been inserted before cond_earliest.  We should
1787 1788
     be able to remove the jump with impunity, but the condition itself may
     have been modified by gcse to be shared across basic blocks.  */
1789
  delete_insn (jump);
Richard Henderson committed
1790 1791 1792 1793 1794

  /* If we used a temporary, fix it up now.  */
  if (orig_x != x)
    {
      start_sequence ();
1795
      noce_emit_move_insn (orig_x, x);
Richard Henderson committed
1796 1797 1798
      insn_b = gen_sequence ();
      end_sequence ();

1799
      emit_insn_after (insn_b, test_bb->end);
Richard Henderson committed
1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847
    }

  /* Merge the blocks!  */
  merge_if_block (test_bb, then_bb, else_bb, join_bb);

  return TRUE;
}

/* Attempt to convert an IF-THEN or IF-THEN-ELSE block into
   straight line code.  Return true if successful.  */

static int
process_if_block (test_bb, then_bb, else_bb, join_bb)
     basic_block test_bb;	/* Basic block test is in */
     basic_block then_bb;	/* Basic block for THEN block */
     basic_block else_bb;	/* Basic block for ELSE block */
     basic_block join_bb;	/* Basic block the join label is in */
{
  if (! reload_completed
      && noce_process_if_block (test_bb, then_bb, else_bb, join_bb))
    return TRUE;

  if (HAVE_conditional_execution
      && reload_completed
      && cond_exec_process_if_block (test_bb, then_bb, else_bb, join_bb))
    return TRUE;

  return FALSE;
}

/* Merge the blocks and mark for local life update.  */

static void
merge_if_block (test_bb, then_bb, else_bb, join_bb)
     basic_block test_bb;	/* Basic block test is in */
     basic_block then_bb;	/* Basic block for THEN block */
     basic_block else_bb;	/* Basic block for ELSE block */
     basic_block join_bb;	/* Basic block the join label is in */
{
  basic_block combo_bb;

  /* All block merging is done into the lower block numbers.  */

  combo_bb = test_bb;

  /* First merge TEST block into THEN block.  This is a no-brainer since
     the THEN block did not have a code label to begin with.  */

1848
  if (life_data_ok)
Richard Henderson committed
1849 1850 1851 1852 1853 1854 1855 1856 1857
    COPY_REG_SET (combo_bb->global_live_at_end, then_bb->global_live_at_end);
  merge_blocks_nomove (combo_bb, then_bb);
  num_removed_blocks++;

  /* The ELSE block, if it existed, had a label.  That label count
     will almost always be zero, but odd things can happen when labels
     get their addresses taken.  */
  if (else_bb)
    {
1858 1859
      merge_blocks_nomove (combo_bb, else_bb);
      num_removed_blocks++;
Richard Henderson committed
1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871
    }

  /* If there was no join block reported, that means it was not adjacent
     to the others, and so we cannot merge them.  */

  if (! join_bb)
    {
      /* The outgoing edge for the current COMBO block should already
	 be correct.  Verify this.  */
      if (combo_bb->succ == NULL_EDGE)
	abort ();

1872
      /* There should still be a branch at the end of the THEN or ELSE
Richard Henderson committed
1873
         blocks taking us to our final destination.  */
1874
      if (GET_CODE (combo_bb->end) != JUMP_INSN)
Richard Henderson committed
1875 1876 1877
	abort ();
    }

1878 1879 1880
  /* The JOIN block may have had quite a number of other predecessors too.
     Since we've already merged the TEST, THEN and ELSE blocks, we should
     have only one remaining edge from our if-then-else diamond.  If there
1881 1882 1883
     is more than one remaining edge, it must come from elsewhere.  There
     may be zero incoming edges if the THEN block didn't actually join 
     back up (as with a call to abort).  */
1884 1885 1886
  else if ((join_bb->pred == NULL
	    || join_bb->pred->pred_next == NULL)
	   && join_bb != EXIT_BLOCK_PTR)
Richard Henderson committed
1887 1888
    {
      /* We can merge the JOIN.  */
1889
      if (life_data_ok)
Richard Henderson committed
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905
	COPY_REG_SET (combo_bb->global_live_at_end,
		      join_bb->global_live_at_end);
      merge_blocks_nomove (combo_bb, join_bb);
      num_removed_blocks++;
    }
  else
    {
      /* We cannot merge the JOIN.  */

      /* The outgoing edge for the current COMBO block should already
	 be correct.  Verify this.  */
      if (combo_bb->succ->succ_next != NULL_EDGE
	  || combo_bb->succ->dest != join_bb)
	abort ();

      /* Remove the jump and cruft from the end of the COMBO block.  */
1906 1907
      if (join_bb != EXIT_BLOCK_PTR)
        tidy_fallthru_edge (combo_bb->succ, combo_bb, join_bb);
Richard Henderson committed
1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951
    }

  /* Make sure we update life info properly.  */
  SET_UPDATE_LIFE (combo_bb);

  num_updated_if_blocks++;
}

/* Find a block ending in a simple IF condition.  Return TRUE if
   we were able to transform it in some way.  */

static int
find_if_header (test_bb)
     basic_block test_bb;
{
  edge then_edge;
  edge else_edge;

  /* The kind of block we're looking for has exactly two successors.  */
  if ((then_edge = test_bb->succ) == NULL_EDGE
      || (else_edge = then_edge->succ_next) == NULL_EDGE
      || else_edge->succ_next != NULL_EDGE)
    return FALSE;

  /* Neither edge should be abnormal.  */
  if ((then_edge->flags & EDGE_COMPLEX)
      || (else_edge->flags & EDGE_COMPLEX))
    return FALSE;

  /* The THEN edge is canonically the one that falls through.  */
  if (then_edge->flags & EDGE_FALLTHRU)
    ;
  else if (else_edge->flags & EDGE_FALLTHRU)
    {
      edge e = else_edge;
      else_edge = then_edge;
      then_edge = e;
    }
  else
    /* Otherwise this must be a multiway branch of some sort.  */
    return FALSE;

  if (find_if_block (test_bb, then_edge, else_edge))
    goto success;
1952 1953 1954
  if (HAVE_trap && HAVE_conditional_trap
      && find_cond_trap (test_bb, then_edge, else_edge))
    goto success;
Richard Henderson committed
1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991
  if (post_dominators
      && (! HAVE_conditional_execution || reload_completed))
    {
      if (find_if_case_1 (test_bb, then_edge, else_edge))
	goto success;
      if (find_if_case_2 (test_bb, then_edge, else_edge))
	goto success;
    }

  return FALSE;

 success:
  if (rtl_dump_file)
    fprintf (rtl_dump_file, "Conversion succeeded.\n");
  return TRUE;
}

/* Determine if a given basic block heads a simple IF-THEN or IF-THEN-ELSE
   block.  If so, we'll try to convert the insns to not require the branch.
   Return TRUE if we were successful at converting the the block.  */

static int
find_if_block (test_bb, then_edge, else_edge)
      basic_block test_bb;
      edge then_edge, else_edge;
{
  basic_block then_bb = then_edge->dest;
  basic_block else_bb = else_edge->dest;
  basic_block join_bb = NULL_BLOCK;
  edge then_succ = then_bb->succ;
  edge else_succ = else_bb->succ;
  int next_index;

  /* The THEN block of an IF-THEN combo must have exactly one predecessor.  */
  if (then_bb->pred->pred_next != NULL_EDGE)
    return FALSE;

1992 1993 1994 1995
  /* The THEN block of an IF-THEN combo must have zero or one successors.  */
  if (then_succ != NULL_EDGE
      && (then_succ->succ_next != NULL_EDGE
          || (then_succ->flags & EDGE_COMPLEX)))
Richard Henderson committed
1996 1997
    return FALSE;

1998 1999
  /* If the THEN block has no successors, conditional execution can still
     make a conditional call.  Don't do this unless the ELSE block has
2000 2001 2002 2003
     only one incoming edge -- the CFG manipulation is too ugly otherwise.
     Check for the last insn of the THEN block being an indirect jump, which
     is listed as not having any successors, but confuses the rest of the CE
     code processing.  XXX we should fix this in the future.  */
2004 2005 2006 2007
  if (then_succ == NULL)
    {
      if (else_bb->pred->pred_next == NULL_EDGE)
	{
2008 2009
	  rtx last_insn = then_bb->end;

2010 2011 2012 2013
	  while (last_insn
		 && GET_CODE (last_insn) == NOTE
		 && last_insn != then_bb->head)
	    last_insn = PREV_INSN (last_insn);
2014

2015 2016
	  if (last_insn
	      && GET_CODE (last_insn) == JUMP_INSN
2017 2018 2019
	      && ! simplejump_p (last_insn))
	    return FALSE;

2020 2021 2022 2023 2024 2025 2026
	  join_bb = else_bb;
	  else_bb = NULL_BLOCK;
	}
      else
	return FALSE;
    }

Richard Henderson committed
2027 2028
  /* If the THEN block's successor is the other edge out of the TEST block,
     then we have an IF-THEN combo without an ELSE.  */
2029
  else if (then_succ->dest == else_bb)
Richard Henderson committed
2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066
    {
      join_bb = else_bb;
      else_bb = NULL_BLOCK;
    }

  /* If the THEN and ELSE block meet in a subsequent block, and the ELSE
     has exactly one predecessor and one successor, and the outgoing edge
     is not complex, then we have an IF-THEN-ELSE combo.  */
  else if (else_succ != NULL_EDGE
	   && then_succ->dest == else_succ->dest
	   && else_bb->pred->pred_next == NULL_EDGE
	   && else_succ->succ_next == NULL_EDGE
	   && ! (else_succ->flags & EDGE_COMPLEX))
    join_bb = else_succ->dest;

  /* Otherwise it is not an IF-THEN or IF-THEN-ELSE combination.  */
  else
    return FALSE;	   

  num_possible_if_blocks++;

  if (rtl_dump_file)
    {
      if (else_bb)
	fprintf (rtl_dump_file,
		 "\nIF-THEN-ELSE block found, start %d, then %d, else %d, join %d\n",
		 test_bb->index, then_bb->index, else_bb->index,
		 join_bb->index);
      else
	fprintf (rtl_dump_file,
		 "\nIF-THEN block found, start %d, then %d, join %d\n",
		 test_bb->index, then_bb->index, join_bb->index);
    }

  /* Make sure IF, THEN, and ELSE, blocks are adjacent.  Actually, we
     get the first condition for free, since we've already asserted that
     there's a fallthru edge from IF to THEN.  */
2067
  /* ??? As an enhancement, move the ELSE block.  Have to deal with
Richard Henderson committed
2068 2069 2070 2071 2072
     BLOCK notes, if by no other means than aborting the merge if they
     exist.  Sticky enough I don't want to think about it now.  */
  next_index = then_bb->index;
  if (else_bb && ++next_index != else_bb->index)
    return FALSE;
2073
  if (++next_index != join_bb->index && join_bb->index != EXIT_BLOCK)
Richard Henderson committed
2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084
    {
      if (else_bb)
	join_bb = NULL;
      else
	return FALSE;
    }

  /* Do the real work.  */
  return process_if_block (test_bb, then_bb, else_bb, join_bb);
}

2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
/* Convert a branch over a trap, or a branch to a trap,
   into a conditional trap.  */

static int
find_cond_trap (test_bb, then_edge, else_edge)
     basic_block test_bb;
     edge then_edge, else_edge;
{
  basic_block then_bb, else_bb, join_bb, trap_bb;
  rtx trap, jump, cond, cond_earliest, seq;
  enum rtx_code code;

  then_bb = then_edge->dest;
  else_bb = else_edge->dest;
  join_bb = NULL;

  /* Locate the block with the trap instruction.  */
  /* ??? While we look for no successors, we really ought to allow
     EH successors.  Need to fix merge_if_block for that to work.  */
  /* ??? We can't currently handle merging the blocks if they are not
     already adjacent.  Prevent losage in merge_if_block by detecting
     this now.  */
  if (then_bb->succ == NULL)
    {
      trap_bb = then_bb;
      if (else_bb->index != then_bb->index + 1)
	return FALSE;
      join_bb = else_bb;
      else_bb = NULL;
    }
  else if (else_bb->succ == NULL)
    {
      trap_bb = else_bb;
      if (else_bb->index != then_bb->index + 1)
	else_bb = NULL;
      else if (then_bb->succ
	  && ! then_bb->succ->succ_next
	  && ! (then_bb->succ->flags & EDGE_COMPLEX)
	  && then_bb->succ->dest->index == else_bb->index + 1)
	join_bb = then_bb->succ->dest;
    }
  else
    return FALSE;

2129 2130 2131 2132 2133
  /* Don't confuse a conditional return with something we want to
     optimize here.  */
  if (trap_bb == EXIT_BLOCK_PTR)
    return FALSE;

2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191
  /* The only instruction in the THEN block must be the trap.  */
  trap = first_active_insn (trap_bb);
  if (! (trap == trap_bb->end
	 && GET_CODE (PATTERN (trap)) == TRAP_IF
         && TRAP_CONDITION (PATTERN (trap)) == const_true_rtx))
    return FALSE;

  if (rtl_dump_file)
    {
      if (trap_bb == then_bb)
	fprintf (rtl_dump_file,
		 "\nTRAP-IF block found, start %d, trap %d",
		 test_bb->index, then_bb->index);
      else
	fprintf (rtl_dump_file,
		 "\nTRAP-IF block found, start %d, then %d, trap %d",
		 test_bb->index, then_bb->index, trap_bb->index);
      if (join_bb)
	fprintf (rtl_dump_file, ", join %d\n", join_bb->index);
      else
	fputc ('\n', rtl_dump_file);
    }

  /* If this is not a standard conditional jump, we can't parse it.  */
  jump = test_bb->end;
  cond = noce_get_condition (jump, &cond_earliest);
  if (! cond)
    return FALSE;

  /* If the conditional jump is more than just a conditional jump,
     then we can not do if-conversion on this block.  */
  if (! onlyjump_p (jump))
    return FALSE;

  /* We must be comparing objects whose modes imply the size.  */
  if (GET_MODE (XEXP (cond, 0)) == BLKmode)
    return FALSE;

  /* Reverse the comparison code, if necessary.  */
  code = GET_CODE (cond);
  if (then_bb == trap_bb)
    {
      code = reversed_comparison_code (cond, jump);
      if (code == UNKNOWN)
	return FALSE;
    }

  /* Attempt to generate the conditional trap.  */
  seq = gen_cond_trap (code, XEXP (cond, 0), XEXP (cond, 1),
		       TRAP_CODE (PATTERN (trap)));
  if (seq == NULL)
    return FALSE;

  /* Emit the new insns before cond_earliest; delete the old jump
     and trap insns.  */

  emit_insn_before (seq, cond_earliest);

2192
  delete_insn (jump);
2193

2194
  delete_insn (trap);
2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206

  /* Merge the blocks!  */
  if (trap_bb != then_bb && ! else_bb)
    {
      flow_delete_block (trap_bb);
      num_removed_blocks++;
    }
  merge_if_block (test_bb, then_bb, else_bb, join_bb);

  return TRUE;
}

Richard Henderson committed
2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289
/* Look for IF-THEN-ELSE cases in which one of THEN or ELSE is
   transformable, but not necessarily the other.  There need be no
   JOIN block.

   Return TRUE if we were successful at converting the the block.

   Cases we'd like to look at:

   (1)
	if (test) goto over; // x not live
	x = a;
	goto label;
	over:

   becomes

	x = a;
	if (! test) goto label;

   (2)
	if (test) goto E; // x not live
	x = big();
	goto L;
	E:
	x = b;
	goto M;

   becomes

	x = b;
	if (test) goto M;
	x = big();
	goto L;

   (3) // This one's really only interesting for targets that can do
       // multiway branching, e.g. IA-64 BBB bundles.  For other targets
       // it results in multiple branches on a cache line, which often
       // does not sit well with predictors.

	if (test1) goto E; // predicted not taken
	x = a;
	if (test2) goto F;
	...
	E:
	x = b;
	J:

   becomes

	x = a;
	if (test1) goto E;
	if (test2) goto F;

   Notes:

   (A) Don't do (2) if the branch is predicted against the block we're
   eliminating.  Do it anyway if we can eliminate a branch; this requires
   that the sole successor of the eliminated block postdominate the other
   side of the if.

   (B) With CE, on (3) we can steal from both sides of the if, creating

	if (test1) x = a;
	if (!test1) x = b;
	if (test1) goto J;
	if (test2) goto F;
	...
	J:

   Again, this is most useful if J postdominates.

   (C) CE substitutes for helpful life information.

   (D) These heuristics need a lot of work.  */

/* Tests for case 1 above.  */

static int
find_if_case_1 (test_bb, then_edge, else_edge)
      basic_block test_bb;
      edge then_edge, else_edge;
{
  basic_block then_bb = then_edge->dest;
2290
  basic_block else_bb = else_edge->dest, new_bb;
Richard Henderson committed
2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304
  edge then_succ = then_bb->succ;

  /* THEN has one successor.  */
  if (!then_succ || then_succ->succ_next != NULL)
    return FALSE;

  /* THEN does not fall through, but is not strange either.  */
  if (then_succ->flags & (EDGE_COMPLEX | EDGE_FALLTHRU))
    return FALSE;

  /* THEN has one predecessor.  */
  if (then_bb->pred->pred_next != NULL)
    return FALSE;

2305 2306
  /* THEN must do something.  */
  if (forwarder_block_p (then_bb))
Richard Henderson committed
2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319
    return FALSE;

  num_possible_if_blocks++;
  if (rtl_dump_file)
    fprintf (rtl_dump_file,
	     "\nIF-CASE-1 found, start %d, then %d\n",
	     test_bb->index, then_bb->index);

  /* THEN is small.  */
  if (count_bb_insns (then_bb) > BRANCH_COST)
    return FALSE;

  /* Registers set are dead, or are predicable.  */
2320 2321
  if (! dead_or_predicable (test_bb, then_bb, else_bb, 
			    then_bb->succ->dest, 1))
Richard Henderson committed
2322 2323 2324 2325 2326 2327 2328 2329 2330 2331
    return FALSE;

  /* Conversion went ok, including moving the insns and fixing up the
     jump.  Adjust the CFG to match.  */

  SET_UPDATE_LIFE (test_bb);
  bitmap_operation (test_bb->global_live_at_end,
		    else_bb->global_live_at_start,
		    then_bb->global_live_at_end, BITMAP_IOR);
  
2332 2333 2334 2335 2336 2337 2338 2339
  new_bb = redirect_edge_and_branch_force (FALLTHRU_EDGE (test_bb), else_bb);
  /* Make rest of code believe that the newly created block is the THEN_BB
     block we are going to remove.  */
  if (new_bb)
    {
      new_bb->aux = then_bb->aux;
      SET_UPDATE_LIFE (then_bb);
    }
Richard Henderson committed
2340
  flow_delete_block (then_bb);
2341 2342
  /* We've possibly created jump to next insn, cleanup_cfg will solve that
     later.  */
Richard Henderson committed
2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359

  num_removed_blocks++;
  num_updated_if_blocks++;

  return TRUE;
}

/* Test for case 2 above.  */

static int
find_if_case_2 (test_bb, then_edge, else_edge)
      basic_block test_bb;
      edge then_edge, else_edge;
{
  basic_block then_bb = then_edge->dest;
  basic_block else_bb = else_edge->dest;
  edge else_succ = else_bb->succ;
2360
  rtx note;
Richard Henderson committed
2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373

  /* ELSE has one successor.  */
  if (!else_succ || else_succ->succ_next != NULL)
    return FALSE;

  /* ELSE outgoing edge is not complex.  */
  if (else_succ->flags & EDGE_COMPLEX)
    return FALSE;

  /* ELSE has one predecessor.  */
  if (else_bb->pred->pred_next != NULL)
    return FALSE;

2374 2375 2376 2377
  /* THEN is not EXIT.  */
  if (then_bb->index < 0)
    return FALSE;

Richard Henderson committed
2378 2379 2380 2381 2382
  /* ELSE is predicted or SUCC(ELSE) postdominates THEN.  */
  note = find_reg_note (test_bb->end, REG_BR_PROB, NULL_RTX);
  if (note && INTVAL (XEXP (note, 0)) >= REG_BR_PROB_BASE / 2)
    ;
  else if (else_succ->dest->index < 0
2383 2384
	   || TEST_BIT (post_dominators[ORIG_INDEX (then_bb)], 
			ORIG_INDEX (else_succ->dest)))
Richard Henderson committed
2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399
    ;
  else
    return FALSE;

  num_possible_if_blocks++;
  if (rtl_dump_file)
    fprintf (rtl_dump_file,
	     "\nIF-CASE-2 found, start %d, else %d\n",
	     test_bb->index, else_bb->index);

  /* ELSE is small.  */
  if (count_bb_insns (then_bb) > BRANCH_COST)
    return FALSE;

  /* Registers set are dead, or are predicable.  */
2400
  if (! dead_or_predicable (test_bb, else_bb, then_bb, else_succ->dest, 0))
Richard Henderson committed
2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443
    return FALSE;

  /* Conversion went ok, including moving the insns and fixing up the
     jump.  Adjust the CFG to match.  */

  SET_UPDATE_LIFE (test_bb);
  bitmap_operation (test_bb->global_live_at_end,
		    then_bb->global_live_at_start,
		    else_bb->global_live_at_end, BITMAP_IOR);
  
  flow_delete_block (else_bb);

  num_removed_blocks++;
  num_updated_if_blocks++;

  /* ??? We may now fallthru from one of THEN's successors into a join
     block.  Rerun cleanup_cfg?  Examine things manually?  Wait?  */

  return TRUE;
}

/* A subroutine of dead_or_predicable called through for_each_rtx.
   Return 1 if a memory is found.  */

static int
find_memory (px, data)
     rtx *px;
     void *data ATTRIBUTE_UNUSED;
{
  return GET_CODE (*px) == MEM;
}

/* Used by the code above to perform the actual rtl transformations.
   Return TRUE if successful.

   TEST_BB is the block containing the conditional branch.  MERGE_BB
   is the block containing the code to manipulate.  NEW_DEST is the
   label TEST_BB should be branching to after the conversion.
   REVERSEP is true if the sense of the branch should be reversed.  */

static int
dead_or_predicable (test_bb, merge_bb, other_bb, new_dest, reversep)
     basic_block test_bb, merge_bb, other_bb;
2444
     basic_block new_dest;
Richard Henderson committed
2445 2446
     int reversep;
{
2447
  rtx head, end, jump, earliest, old_dest, new_label;
Richard Henderson committed
2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476

  jump = test_bb->end;

  /* Find the extent of the real code in the merge block.  */
  head = merge_bb->head;
  end = merge_bb->end;

  if (GET_CODE (head) == CODE_LABEL)
    head = NEXT_INSN (head);
  if (GET_CODE (head) == NOTE)
    {
      if (head == end)
	{
	  head = end = NULL_RTX;
	  goto no_body;
	}
      head = NEXT_INSN (head);
    }

  if (GET_CODE (end) == JUMP_INSN)
    {
      if (head == end)
	{
	  head = end = NULL_RTX;
	  goto no_body;
	}
      end = PREV_INSN (end);
    }

2477 2478 2479
  /* Disable handling dead code by conditional execution if the machine needs
     to do anything funny with the tests, etc.  */
#ifndef IFCVT_MODIFY_TESTS
Richard Henderson committed
2480 2481 2482 2483 2484 2485 2486 2487
  if (HAVE_conditional_execution)
    {
      /* In the conditional execution case, we have things easy.  We know
	 the condition is reversable.  We don't have to check life info,
	 becase we're going to conditionally execute the code anyway.
	 All that's left is making sure the insns involved can actually
	 be predicated.  */

2488
      rtx cond, prob_val;
Richard Henderson committed
2489 2490

      cond = cond_exec_get_condition (jump);
2491 2492
      if (! cond)
	return FALSE;
2493 2494 2495 2496 2497

      prob_val = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
      if (prob_val)
	prob_val = XEXP (prob_val, 0);

Richard Henderson committed
2498
      if (reversep)
2499
	{
2500 2501
	  enum rtx_code rev = reversed_comparison_code (cond, jump);
	  if (rev == UNKNOWN)
2502
	    return FALSE;
2503
	  cond = gen_rtx_fmt_ee (rev, GET_MODE (cond), XEXP (cond, 0),
2504 2505 2506 2507
			         XEXP (cond, 1));
	  if (prob_val)
	    prob_val = GEN_INT (REG_BR_PROB_BASE - INTVAL (prob_val));
	}
Richard Henderson committed
2508

2509
      if (! cond_exec_process_insns (head, end, cond, prob_val, 0))
Richard Henderson committed
2510 2511 2512 2513 2514
	goto cancel;

      earliest = jump;
    }
  else
2515
#endif
Richard Henderson committed
2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539
    {
      /* In the non-conditional execution case, we have to verify that there
	 are no trapping operations, no calls, no references to memory, and
	 that any registers modified are dead at the branch site.  */

      rtx insn, cond, prev;
      regset_head merge_set_head, tmp_head, test_live_head, test_set_head;
      regset merge_set, tmp, test_live, test_set;
      struct propagate_block_info *pbi;
      int i, fail = 0;

      /* Check for no calls or trapping operations.  */
      for (insn = head; ; insn = NEXT_INSN (insn))
	{
	  if (GET_CODE (insn) == CALL_INSN)
	    return FALSE;
	  if (INSN_P (insn))
	    {
	      if (may_trap_p (PATTERN (insn)))
		return FALSE;

	      /* ??? Even non-trapping memories such as stack frame
		 references must be avoided.  For stores, we collect
		 no lifetime info; for reads, we'd have to assert
2540
		 true_dependence false against every store in the
Richard Henderson committed
2541 2542 2543 2544 2545 2546 2547 2548
		 TEST range.  */
	      if (for_each_rtx (&PATTERN (insn), find_memory, NULL))
		return FALSE;
	    }
	  if (insn == end)
	    break;
	}

2549
      if (! any_condjump_p (jump))
Richard Henderson committed
2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570
	return FALSE;

      /* Find the extent of the conditional.  */
      cond = noce_get_condition (jump, &earliest);
      if (! cond)
	return FALSE;

      /* Collect:
	   MERGE_SET = set of registers set in MERGE_BB
	   TEST_LIVE = set of registers live at EARLIEST
	   TEST_SET  = set of registers set between EARLIEST and the
		       end of the block.  */

      tmp = INITIALIZE_REG_SET (tmp_head);
      merge_set = INITIALIZE_REG_SET (merge_set_head);
      test_live = INITIALIZE_REG_SET (test_live_head);
      test_set = INITIALIZE_REG_SET (test_set_head);

      /* ??? bb->local_set is only valid during calculate_global_regs_live,
	 so we must recompute usage for MERGE_BB.  Not so bad, I suppose, 
         since we've already asserted that MERGE_BB is small.  */
2571
      propagate_block (merge_bb, tmp, merge_set, merge_set, 0);
Richard Henderson committed
2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590

      /* For small register class machines, don't lengthen lifetimes of
	 hard registers before reload.  */
      if (SMALL_REGISTER_CLASSES && ! reload_completed)
	{
          EXECUTE_IF_SET_IN_BITMAP
	    (merge_set, 0, i,
	     {
	       if (i < FIRST_PSEUDO_REGISTER
		   && ! fixed_regs[i]
		   && ! global_regs[i])
		fail = 1;
	     });
	}

      /* For TEST, we're interested in a range of insns, not a whole block.
	 Moreover, we're interested in the insns live from OTHER_BB.  */

      COPY_REG_SET (test_live, other_bb->global_live_at_start);
2591 2592
      pbi = init_propagate_block_info (test_bb, test_live, test_set, test_set,
				       0);
Richard Henderson committed
2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631

      for (insn = jump; ; insn = prev)
	{
	  prev = propagate_one_insn (pbi, insn);
	  if (insn == earliest)
	    break;
	}

      free_propagate_block_info (pbi);

      /* We can perform the transformation if
	   MERGE_SET & (TEST_SET | TEST_LIVE)
	 and
	   TEST_SET & merge_bb->global_live_at_start
	 are empty.  */

      bitmap_operation (tmp, test_set, test_live, BITMAP_IOR);
      bitmap_operation (tmp, tmp, merge_set, BITMAP_AND);
      EXECUTE_IF_SET_IN_BITMAP(tmp, 0, i, fail = 1);

      bitmap_operation (tmp, test_set, merge_bb->global_live_at_start,
			BITMAP_AND);
      EXECUTE_IF_SET_IN_BITMAP(tmp, 0, i, fail = 1);

      FREE_REG_SET (tmp);
      FREE_REG_SET (merge_set);
      FREE_REG_SET (test_live);
      FREE_REG_SET (test_set);

      if (fail)
	return FALSE;
    }

 no_body:
  /* We don't want to use normal invert_jump or redirect_jump because
     we don't want to delete_insn called.  Also, we want to do our own
     change group management.  */

  old_dest = JUMP_LABEL (jump);
2632
  new_label = block_label (new_dest);
Richard Henderson committed
2633
  if (reversep
2634 2635
      ? ! invert_jump_1 (jump, new_label)
      : ! redirect_jump_1 (jump, new_label))
Richard Henderson committed
2636 2637 2638 2639 2640 2641 2642
    goto cancel;

  if (! apply_change_group ())
    return FALSE;

  if (old_dest)
    LABEL_NUSES (old_dest) -= 1;
2643 2644 2645
  if (new_label)
    LABEL_NUSES (new_label) += 1;
  JUMP_LABEL (jump) = new_label;
Richard Henderson committed
2646 2647

  if (reversep)
Jan Hubicka committed
2648
    invert_br_probabilities (jump);
Richard Henderson committed
2649

2650 2651 2652 2653 2654 2655 2656 2657 2658 2659
  redirect_edge_succ (BRANCH_EDGE (test_bb), new_dest);
  if (reversep)
    {
      gcov_type count, probability;
      count = BRANCH_EDGE (test_bb)->count;
      BRANCH_EDGE (test_bb)->count = FALLTHRU_EDGE (test_bb)->count;
      FALLTHRU_EDGE (test_bb)->count = count;
      probability = BRANCH_EDGE (test_bb)->probability;
      BRANCH_EDGE (test_bb)->probability = FALLTHRU_EDGE (test_bb)->probability;
      FALLTHRU_EDGE (test_bb)->probability = probability;
2660
      update_br_prob_note (test_bb);
2661 2662
    }

Richard Henderson committed
2663 2664 2665
  /* Move the insns out of MERGE_BB to before the branch.  */
  if (head != NULL)
    {
2666 2667 2668
      if (end == merge_bb->end)
	merge_bb->end = PREV_INSN (head);

2669 2670
      if (squeeze_notes (&head, &end))
	return TRUE;
2671

Richard Henderson committed
2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683
      reorder_insns (head, end, PREV_INSN (earliest));
    }
  return TRUE;

 cancel:
  cancel_changes (0);
  return FALSE;
}

/* Main entry point for all if-conversion.  */

void
2684 2685
if_convert (x_life_data_ok)
     int x_life_data_ok;
Richard Henderson committed
2686 2687 2688 2689 2690 2691
{
  int block_num;

  num_possible_if_blocks = 0;
  num_updated_if_blocks = 0;
  num_removed_blocks = 0;
2692
  life_data_ok = (x_life_data_ok != 0);
Richard Henderson committed
2693 2694 2695 2696 2697 2698 2699 2700 2701 2702

  /* Free up basic_block_for_insn so that we don't have to keep it 
     up to date, either here or in merge_blocks_nomove.  */
  free_basic_block_vars (1);

  /* Compute postdominators if we think we'll use them.  */
  post_dominators = NULL;
  if (HAVE_conditional_execution || life_data_ok)
    {
      post_dominators = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
2703
      calculate_dominance_info (NULL, post_dominators, CDI_POST_DOMINATORS);
Richard Henderson committed
2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719
    }

  /* Record initial block numbers.  */
  for (block_num = 0; block_num < n_basic_blocks; block_num++)
    SET_ORIG_INDEX (BASIC_BLOCK (block_num), block_num);

  /* Go through each of the basic blocks looking for things to convert.  */
  for (block_num = 0; block_num < n_basic_blocks; )
    {
      basic_block bb = BASIC_BLOCK (block_num);
      if (find_if_header (bb))
	block_num = bb->index;
      else 
	block_num++;
    }

2720 2721
  if (post_dominators)
    sbitmap_vector_free (post_dominators);
Richard Henderson committed
2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743

  if (rtl_dump_file)
    fflush (rtl_dump_file);

  /* Rebuild life info for basic blocks that require it.  */
  if (num_removed_blocks && life_data_ok)
    {
      sbitmap update_life_blocks = sbitmap_alloc (n_basic_blocks);
      sbitmap_zero (update_life_blocks);

      /* If we allocated new pseudos, we must resize the array for sched1.  */
      if (max_regno < max_reg_num ())
	{
	  max_regno = max_reg_num ();
	  allocate_reg_info (max_regno, FALSE, FALSE);
	}

      for (block_num = 0; block_num < n_basic_blocks; block_num++)
	if (UPDATE_LIFE (BASIC_BLOCK (block_num)))
	  SET_BIT (update_life_blocks, block_num);

      count_or_remove_death_notes (update_life_blocks, 1);
2744 2745 2746
      /* ??? See about adding a mode that verifies that the initial
	set of blocks don't let registers come live.  */
      update_life_info (update_life_blocks, UPDATE_LIFE_GLOBAL,
2747 2748
			PROP_DEATH_NOTES | PROP_SCAN_DEAD_CODE
			| PROP_KILL_DEAD_CODE);
Richard Henderson committed
2749 2750 2751

      sbitmap_free (update_life_blocks);
    }
2752
  clear_aux_for_blocks ();
Richard Henderson committed
2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767

  /* Write the final stats.  */
  if (rtl_dump_file && num_possible_if_blocks > 0)
    {
      fprintf (rtl_dump_file,
	       "\n%d possible IF blocks searched.\n",
	       num_possible_if_blocks);
      fprintf (rtl_dump_file,
	       "%d IF blocks converted.\n",
	       num_updated_if_blocks);
      fprintf (rtl_dump_file,
	       "%d basic blocks deleted.\n\n\n",
	       num_removed_blocks);
    }

2768
#ifdef ENABLE_CHECKING
2769
  verify_flow_info ();
2770
#endif
Richard Henderson committed
2771
}