ira-conflicts.c 28.1 KB
Newer Older
Vladimir Makarov committed
1
/* IRA conflict builder.
2
   Copyright (C) 2006, 2007, 2008, 2009, 2010
Vladimir Makarov committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
   Free Software Foundation, Inc.
   Contributed by Vladimir Makarov <vmakarov@redhat.com>.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

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

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

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "regs.h"
#include "rtl.h"
#include "tm_p.h"
#include "target.h"
#include "flags.h"
#include "hard-reg-set.h"
#include "basic-block.h"
#include "insn-config.h"
#include "recog.h"
35
#include "diagnostic-core.h"
Vladimir Makarov committed
36 37 38 39
#include "params.h"
#include "df.h"
#include "sparseset.h"
#include "ira-int.h"
40
#include "addresses.h"
Vladimir Makarov committed
41 42 43 44 45 46 47 48 49

/* This file contains code responsible for allocno conflict creation,
   allocno copy creation and allocno info accumulation on upper level
   regions.  */

/* ira_allocnos_num array of arrays of bits, recording whether two
   allocno's conflict (can't go in the same hardware register).

   Some arrays will be used as conflict bit vector of the
50
   corresponding allocnos see function build_object_conflicts.  */
Vladimir Makarov committed
51 52
static IRA_INT_TYPE **conflicts;

53
/* Macro to test a conflict of C1 and C2 in `conflicts'.  */
54
#define OBJECTS_CONFLICT_P(C1, C2)					\
55 56 57 58 59
  (OBJECT_MIN (C1) <= OBJECT_CONFLICT_ID (C2)				\
   && OBJECT_CONFLICT_ID (C2) <= OBJECT_MAX (C1)			\
   && TEST_MINMAX_SET_BIT (conflicts[OBJECT_CONFLICT_ID (C1)],		\
			   OBJECT_CONFLICT_ID (C2),			\
			   OBJECT_MIN (C1), OBJECT_MAX (C1)))
Vladimir Makarov committed
60 61


62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
/* Record a conflict between objects OBJ1 and OBJ2.  If necessary,
   canonicalize the conflict by recording it for lower-order subobjects
   of the corresponding allocnos. */
static void
record_object_conflict (ira_object_t obj1, ira_object_t obj2)
{
  ira_allocno_t a1 = OBJECT_ALLOCNO (obj1);
  ira_allocno_t a2 = OBJECT_ALLOCNO (obj2);
  int w1 = OBJECT_SUBWORD (obj1);
  int w2 = OBJECT_SUBWORD (obj2);
  int id1, id2;

  /* Canonicalize the conflict.  If two identically-numbered words
     conflict, always record this as a conflict between words 0.  That
     is the only information we need, and it is easier to test for if
     it is collected in each allocno's lowest-order object.  */
  if (w1 == w2 && w1 > 0)
    {
      obj1 = ALLOCNO_OBJECT (a1, 0);
      obj2 = ALLOCNO_OBJECT (a2, 0);
    }
  id1 = OBJECT_CONFLICT_ID (obj1);
  id2 = OBJECT_CONFLICT_ID (obj2);

  SET_MINMAX_SET_BIT (conflicts[id1], id2, OBJECT_MIN (obj1),
		      OBJECT_MAX (obj1));
  SET_MINMAX_SET_BIT (conflicts[id2], id1, OBJECT_MIN (obj2),
		      OBJECT_MAX (obj2));
}

92 93 94 95
/* Build allocno conflict table by processing allocno live ranges.
   Return true if the table was built.  The table is not built if it
   is too big.  */
static bool
Vladimir Makarov committed
96 97
build_conflict_bit_table (void)
{
98
  int i;
Vladimir Makarov committed
99
  unsigned int j;
Vladimir Makarov committed
100
  enum reg_class aclass;
101
  int object_set_words, allocated_words_num, conflict_bit_vec_words_num;
102
  live_range_t r;
103
  ira_allocno_t allocno;
Vladimir Makarov committed
104
  ira_allocno_iterator ai;
105
  sparseset objects_live;
106 107
  ira_object_t obj;
  ira_allocno_object_iterator aoi;
Vladimir Makarov committed
108

109 110
  allocated_words_num = 0;
  FOR_EACH_ALLOCNO (allocno, ai)
111 112 113
    FOR_EACH_ALLOCNO_OBJECT (allocno, obj, aoi)
      {
	if (OBJECT_MAX (obj) < OBJECT_MIN (obj))
114
	  continue;
115 116 117 118
	conflict_bit_vec_words_num
	  = ((OBJECT_MAX (obj) - OBJECT_MIN (obj) + IRA_INT_BITS)
	     / IRA_INT_BITS);
	allocated_words_num += conflict_bit_vec_words_num;
119 120
	if ((unsigned HOST_WIDEST_INT) allocated_words_num * sizeof (IRA_INT_TYPE)
	    > (unsigned HOST_WIDEST_INT) IRA_MAX_CONFLICT_TABLE_SIZE * 1024 * 1024)
121 122 123 124 125 126 127 128 129
	  {
	    if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
	      fprintf
		(ira_dump_file,
		 "+++Conflict table will be too big(>%dMB) -- don't use it\n",
		 IRA_MAX_CONFLICT_TABLE_SIZE);
	    return false;
	  }
      }
130

Vladimir Makarov committed
131
  conflicts = (IRA_INT_TYPE **) ira_allocate (sizeof (IRA_INT_TYPE *)
132
					      * ira_objects_num);
Vladimir Makarov committed
133 134
  allocated_words_num = 0;
  FOR_EACH_ALLOCNO (allocno, ai)
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    FOR_EACH_ALLOCNO_OBJECT (allocno, obj, aoi)
      {
	int id = OBJECT_CONFLICT_ID (obj);
	if (OBJECT_MAX (obj) < OBJECT_MIN (obj))
	  {
	    conflicts[id] = NULL;
	    continue;
	  }
	conflict_bit_vec_words_num
	  = ((OBJECT_MAX (obj) - OBJECT_MIN (obj) + IRA_INT_BITS)
	     / IRA_INT_BITS);
	allocated_words_num += conflict_bit_vec_words_num;
	conflicts[id]
	  = (IRA_INT_TYPE *) ira_allocate (sizeof (IRA_INT_TYPE)
					   * conflict_bit_vec_words_num);
	memset (conflicts[id], 0,
		sizeof (IRA_INT_TYPE) * conflict_bit_vec_words_num);
      }
153 154

  object_set_words = (ira_objects_num + IRA_INT_BITS - 1) / IRA_INT_BITS;
Vladimir Makarov committed
155 156 157 158 159
  if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
    fprintf
      (ira_dump_file,
       "+++Allocating %ld bytes for conflict table (uncompressed size %ld)\n",
       (long) allocated_words_num * sizeof (IRA_INT_TYPE),
160 161 162
       (long) object_set_words * ira_objects_num * sizeof (IRA_INT_TYPE));

  objects_live = sparseset_alloc (ira_objects_num);
Vladimir Makarov committed
163 164 165 166
  for (i = 0; i < ira_max_point; i++)
    {
      for (r = ira_start_point_ranges[i]; r != NULL; r = r->start_next)
	{
167 168
	  ira_object_t obj = r->object;
	  ira_allocno_t allocno = OBJECT_ALLOCNO (obj);
169 170
	  int id = OBJECT_CONFLICT_ID (obj);

171 172
	  gcc_assert (id < ira_objects_num);

Vladimir Makarov committed
173
	  aclass = ALLOCNO_CLASS (allocno);
174 175
	  sparseset_set_bit (objects_live, id);
	  EXECUTE_IF_SET_IN_SPARSESET (objects_live, j)
Vladimir Makarov committed
176
	    {
177 178
	      ira_object_t live_obj = ira_object_id_map[j];
	      ira_allocno_t live_a = OBJECT_ALLOCNO (live_obj);
Vladimir Makarov committed
179
	      enum reg_class live_aclass = ALLOCNO_CLASS (live_a);
180

Vladimir Makarov committed
181
	      if (ira_reg_classes_intersect_p[aclass][live_aclass]
Vladimir Makarov committed
182
		  /* Don't set up conflict for the allocno with itself.  */
183
		  && live_a != allocno)
Vladimir Makarov committed
184
		{
185
		  record_object_conflict (obj, live_obj);
Vladimir Makarov committed
186 187 188
		}
	    }
	}
H.J. Lu committed
189

Vladimir Makarov committed
190
      for (r = ira_finish_point_ranges[i]; r != NULL; r = r->finish_next)
191
	sparseset_clear_bit (objects_live, OBJECT_CONFLICT_ID (r->object));
Vladimir Makarov committed
192
    }
193
  sparseset_free (objects_live);
194
  return true;
Vladimir Makarov committed
195 196
}

197 198 199 200
/* Return true iff allocnos A1 and A2 cannot be allocated to the same
   register due to conflicts.  */

static bool
201
allocnos_conflict_for_copy_p (ira_allocno_t a1, ira_allocno_t a2)
202
{
203 204 205 206 207
  /* Due to the fact that we canonicalize conflicts (see
     record_object_conflict), we only need to test for conflicts of
     the lowest order words.  */
  ira_object_t obj1 = ALLOCNO_OBJECT (a1, 0);
  ira_object_t obj2 = ALLOCNO_OBJECT (a2, 0);
Vladimir Makarov committed
208

209 210
  return OBJECTS_CONFLICT_P (obj1, obj2);
}
Vladimir Makarov committed
211 212 213 214 215

/* Return TRUE if the operand constraint STR is commutative.  */
static bool
commutative_constraint_p (const char *str)
{
216
  int curr_alt, c;
Vladimir Makarov committed
217 218
  bool ignore_p;

219
  for (ignore_p = false, curr_alt = 0;;)
Vladimir Makarov committed
220 221 222 223 224
    {
      c = *str;
      if (c == '\0')
	break;
      str += CONSTRAINT_LEN (c, str);
225
      if (c == '#' || !recog_data.alternative_enabled_p[curr_alt])
Vladimir Makarov committed
226 227
	ignore_p = true;
      else if (c == ',')
228 229 230 231
	{
	  curr_alt++;
	  ignore_p = false;
	}
Vladimir Makarov committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
      else if (! ignore_p)
	{
	  /* Usually `%' is the first constraint character but the
	     documentation does not require this.  */
	  if (c == '%')
	    return true;
	}
    }
  return false;
}

/* Return the number of the operand which should be the same in any
   case as operand with number OP_NUM (or negative value if there is
   no such operand).  If USE_COMMUT_OP_P is TRUE, the function makes
   temporarily commutative operand exchange before this.  The function
   takes only really possible alternatives into consideration.  */
static int
get_dup_num (int op_num, bool use_commut_op_p)
{
  int curr_alt, c, original, dup;
  bool ignore_p, commut_op_used_p;
  const char *str;
  rtx op;

  if (op_num < 0 || recog_data.n_alternatives == 0)
    return -1;
  op = recog_data.operand[op_num];
  commut_op_used_p = true;
  if (use_commut_op_p)
    {
      if (commutative_constraint_p (recog_data.constraints[op_num]))
	op_num++;
      else if (op_num > 0 && commutative_constraint_p (recog_data.constraints
						       [op_num - 1]))
	op_num--;
      else
	commut_op_used_p = false;
    }
  str = recog_data.constraints[op_num];
  for (ignore_p = false, original = -1, curr_alt = 0;;)
    {
      c = *str;
      if (c == '\0')
	break;
276
      if (c == '#' || !recog_data.alternative_enabled_p[curr_alt])
Vladimir Makarov committed
277 278 279 280 281 282 283 284 285 286 287
	ignore_p = true;
      else if (c == ',')
	{
	  curr_alt++;
	  ignore_p = false;
	}
      else if (! ignore_p)
	switch (c)
	  {
	  case 'X':
	    return -1;
H.J. Lu committed
288

Vladimir Makarov committed
289 290 291 292 293 294 295 296 297 298 299 300
	  case 'm':
	  case 'o':
	    /* Accept a register which might be placed in memory.  */
	    return -1;
	    break;

	  case 'V':
	  case '<':
	  case '>':
	    break;

	  case 'p':
301 302
	    if (address_operand (op, VOIDmode))
	      return -1;
Vladimir Makarov committed
303
	    break;
304

Vladimir Makarov committed
305 306
	  case 'g':
	    return -1;
H.J. Lu committed
307

Vladimir Makarov committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
	  case 'r':
	  case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
	  case 'h': case 'j': case 'k': case 'l':
	  case 'q': case 't': case 'u':
	  case 'v': case 'w': case 'x': case 'y': case 'z':
	  case 'A': case 'B': case 'C': case 'D':
	  case 'Q': case 'R': case 'S': case 'T': case 'U':
	  case 'W': case 'Y': case 'Z':
	    {
	      enum reg_class cl;

	      cl = (c == 'r'
		    ? GENERAL_REGS : REG_CLASS_FROM_CONSTRAINT (c, str));
	      if (cl != NO_REGS)
		return -1;
#ifdef EXTRA_CONSTRAINT_STR
	      else if (EXTRA_CONSTRAINT_STR (op, c, str))
		return -1;
#endif
	      break;
	    }
H.J. Lu committed
329

Vladimir Makarov committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
	  case '0': case '1': case '2': case '3': case '4':
	  case '5': case '6': case '7': case '8': case '9':
	    if (original != -1 && original != c)
	      return -1;
	    original = c;
	    break;
	  }
      str += CONSTRAINT_LEN (c, str);
    }
  if (original == -1)
    return -1;
  dup = original - '0';
  if (use_commut_op_p)
    {
      if (commutative_constraint_p (recog_data.constraints[dup]))
	dup++;
      else if (dup > 0
	       && commutative_constraint_p (recog_data.constraints[dup -1]))
	dup--;
      else if (! commut_op_used_p)
	return -1;
    }
  return dup;
}

355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
/* Check that X is REG or SUBREG of REG.  */
#define REG_SUBREG_P(x)							\
   (REG_P (x) || (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x))))

/* Return X if X is a REG, otherwise it should be SUBREG of REG and
   the function returns the reg in this case.  *OFFSET will be set to
   0 in the first case or the regno offset in the first case.  */
static rtx
go_through_subreg (rtx x, int *offset)
{
  rtx reg;

  *offset = 0;
  if (REG_P (x))
    return x;
  ira_assert (GET_CODE (x) == SUBREG);
  reg = SUBREG_REG (x);
  ira_assert (REG_P (reg));
  if (REGNO (reg) < FIRST_PSEUDO_REGISTER)
    *offset = subreg_regno_offset (REGNO (reg), GET_MODE (reg),
				   SUBREG_BYTE (x), GET_MODE (x));
  else
    *offset = (SUBREG_BYTE (x) / REGMODE_NATURAL_SIZE (GET_MODE (x)));
  return reg;
}

Vladimir Makarov committed
381 382 383 384 385 386 387 388 389
/* Process registers REG1 and REG2 in move INSN with execution
   frequency FREQ.  The function also processes the registers in a
   potential move insn (INSN == NULL in this case) with frequency
   FREQ.  The function can modify hard register costs of the
   corresponding allocnos or create a copy involving the corresponding
   allocnos.  The function does nothing if the both registers are hard
   registers.  When nothing is changed, the function returns
   FALSE.  */
static bool
390 391
process_regs_for_copy (rtx reg1, rtx reg2, bool constraint_p,
		       rtx insn, int freq)
Vladimir Makarov committed
392
{
393
  int allocno_preferenced_hard_regno, cost, index, offset1, offset2;
394
  bool only_regs_p;
Vladimir Makarov committed
395
  ira_allocno_t a;
396
  reg_class_t rclass, aclass;
Vladimir Makarov committed
397 398 399
  enum machine_mode mode;
  ira_copy_t cp;

400 401 402 403
  gcc_assert (REG_SUBREG_P (reg1) && REG_SUBREG_P (reg2));
  only_regs_p = REG_P (reg1) && REG_P (reg2);
  reg1 = go_through_subreg (reg1, &offset1);
  reg2 = go_through_subreg (reg2, &offset2);
404 405
  /* Set up hard regno preferenced by allocno.  If allocno gets the
     hard regno the copy (or potential move) insn will be removed.  */
Vladimir Makarov committed
406 407 408 409
  if (HARD_REGISTER_P (reg1))
    {
      if (HARD_REGISTER_P (reg2))
	return false;
410
      allocno_preferenced_hard_regno = REGNO (reg1) + offset1 - offset2;
Vladimir Makarov committed
411 412 413 414
      a = ira_curr_regno_allocno_map[REGNO (reg2)];
    }
  else if (HARD_REGISTER_P (reg2))
    {
415
      allocno_preferenced_hard_regno = REGNO (reg2) + offset2 - offset1;
Vladimir Makarov committed
416 417
      a = ira_curr_regno_allocno_map[REGNO (reg1)];
    }
418
  else
Vladimir Makarov committed
419
    {
420 421
      ira_allocno_t a1 = ira_curr_regno_allocno_map[REGNO (reg1)];
      ira_allocno_t a2 = ira_curr_regno_allocno_map[REGNO (reg2)];
422

423
      if (!allocnos_conflict_for_copy_p (a1, a2) && offset1 == offset2)
424 425 426 427 428 429 430 431
	{
	  cp = ira_add_allocno_copy (a1, a2, freq, constraint_p, insn,
				     ira_curr_loop_tree_node);
	  bitmap_set_bit (ira_curr_loop_tree_node->local_copies, cp->num);
	  return true;
	}
      else
	return false;
Vladimir Makarov committed
432
    }
433

Vladimir Makarov committed
434 435
  if (! IN_RANGE (allocno_preferenced_hard_regno,
		  0, FIRST_PSEUDO_REGISTER - 1))
436 437 438
    /* Can not be tied.  */
    return false;
  rclass = REGNO_REG_CLASS (allocno_preferenced_hard_regno);
Vladimir Makarov committed
439
  mode = ALLOCNO_MODE (a);
Vladimir Makarov committed
440
  aclass = ALLOCNO_CLASS (a);
441
  if (only_regs_p && insn != NULL_RTX
442
      && reg_class_size[rclass] <= ira_reg_class_max_nregs [rclass][mode])
Vladimir Makarov committed
443 444
    /* It is already taken into account in ira-costs.c.  */
    return false;
Vladimir Makarov committed
445
  index = ira_class_hard_reg_index[aclass][allocno_preferenced_hard_regno];
Vladimir Makarov committed
446
  if (index < 0)
Vladimir Makarov committed
447
    /* Can not be tied.  It is not in the allocno class.  */
Vladimir Makarov committed
448
    return false;
Vladimir Makarov committed
449
  ira_init_register_move_cost_if_necessary (mode);
Vladimir Makarov committed
450
  if (HARD_REGISTER_P (reg1))
Vladimir Makarov committed
451
    cost = ira_register_move_cost[mode][aclass][rclass] * freq;
Vladimir Makarov committed
452
  else
Vladimir Makarov committed
453
    cost = ira_register_move_cost[mode][rclass][aclass] * freq;
454
  do
455 456
    {
      ira_allocate_and_set_costs
Vladimir Makarov committed
457 458
	(&ALLOCNO_HARD_REG_COSTS (a), aclass,
	 ALLOCNO_CLASS_COST (a));
459
      ira_allocate_and_set_costs
Vladimir Makarov committed
460
	(&ALLOCNO_CONFLICT_HARD_REG_COSTS (a), aclass, 0);
461 462
      ALLOCNO_HARD_REG_COSTS (a)[index] -= cost;
      ALLOCNO_CONFLICT_HARD_REG_COSTS (a)[index] -= cost;
Vladimir Makarov committed
463 464
      if (ALLOCNO_HARD_REG_COSTS (a)[index] < ALLOCNO_CLASS_COST (a))
	ALLOCNO_CLASS_COST (a) = ALLOCNO_HARD_REG_COSTS (a)[index];
465
      a = ira_parent_or_cap_allocno (a);
466
    }
467
  while (a != NULL);
Vladimir Makarov committed
468 469 470
  return true;
}

471 472 473 474
/* Process all of the output registers of the current insn which are
   not bound (BOUND_P) and the input register REG (its operand number
   OP_NUM) which dies in the insn as if there were a move insn between
   them with frequency FREQ.  */
Vladimir Makarov committed
475
static void
476
process_reg_shuffles (rtx reg, int op_num, int freq, bool *bound_p)
Vladimir Makarov committed
477 478 479 480
{
  int i;
  rtx another_reg;

481
  gcc_assert (REG_SUBREG_P (reg));
Vladimir Makarov committed
482 483 484
  for (i = 0; i < recog_data.n_operands; i++)
    {
      another_reg = recog_data.operand[i];
H.J. Lu committed
485

486
      if (!REG_SUBREG_P (another_reg) || op_num == i
487 488
	  || recog_data.operand_type[i] != OP_OUT
	  || bound_p[i])
Vladimir Makarov committed
489
	continue;
H.J. Lu committed
490

491
      process_regs_for_copy (reg, another_reg, false, NULL_RTX, freq);
Vladimir Makarov committed
492 493 494 495 496 497 498 499 500
    }
}

/* Process INSN and create allocno copies if necessary.  For example,
   it might be because INSN is a pseudo-register move or INSN is two
   operand insn.  */
static void
add_insn_allocno_copies (rtx insn)
{
501
  rtx set, operand, dup;
Vladimir Makarov committed
502
  const char *str;
503 504
  bool commut_p, bound_p[MAX_RECOG_OPERANDS];
  int i, j, n, freq;
505

Vladimir Makarov committed
506 507 508 509
  freq = REG_FREQ_FROM_BB (BLOCK_FOR_INSN (insn));
  if (freq == 0)
    freq = 1;
  if ((set = single_set (insn)) != NULL_RTX
510
      && REG_SUBREG_P (SET_DEST (set)) && REG_SUBREG_P (SET_SRC (set))
Vladimir Makarov committed
511
      && ! side_effects_p (set)
512 513 514 515
      && find_reg_note (insn, REG_DEAD,
			REG_P (SET_SRC (set))
			? SET_SRC (set)
			: SUBREG_REG (SET_SRC (set))) != NULL_RTX)
Vladimir Makarov committed
516
    {
Vladimir Makarov committed
517 518
      process_regs_for_copy (SET_DEST (set), SET_SRC (set),
			     false, insn, freq);
519 520
      return;
    }
521 522 523
  /* Fast check of possibility of constraint or shuffle copies.  If
     there are no dead registers, there will be no such copies.  */
  if (! find_reg_note (insn, REG_DEAD, NULL_RTX))
524 525 526 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
    return;
  extract_insn (insn);
  for (i = 0; i < recog_data.n_operands; i++)
    bound_p[i] = false;
  for (i = 0; i < recog_data.n_operands; i++)
    {
      operand = recog_data.operand[i];
      if (! REG_SUBREG_P (operand))
	continue;
      str = recog_data.constraints[i];
      while (*str == ' ' || *str == '\t')
	str++;
      for (j = 0, commut_p = false; j < 2; j++, commut_p = true)
	if ((n = get_dup_num (i, commut_p)) >= 0)
	  {
	    bound_p[n] = true;
	    dup = recog_data.operand[n];
	    if (REG_SUBREG_P (dup)
		&& find_reg_note (insn, REG_DEAD,
				  REG_P (operand)
				  ? operand
				  : SUBREG_REG (operand)) != NULL_RTX)
	      process_regs_for_copy (operand, dup, true, NULL_RTX, freq);
	  }
    }
  for (i = 0; i < recog_data.n_operands; i++)
    {
      operand = recog_data.operand[i];
      if (REG_SUBREG_P (operand)
	  && find_reg_note (insn, REG_DEAD,
			    REG_P (operand)
			    ? operand : SUBREG_REG (operand)) != NULL_RTX)
	/* If an operand dies, prefer its hard register for the output
	   operands by decreasing the hard register cost or creating
	   the corresponding allocno copies.  The cost will not
	   correspond to a real move insn cost, so make the frequency
	   smaller.  */
	process_reg_shuffles (operand, i, freq < 8 ? 1 : freq / 8, bound_p);
Vladimir Makarov committed
562 563 564 565 566 567 568 569 570 571 572 573 574 575
    }
}

/* Add copies originated from BB given by LOOP_TREE_NODE.  */
static void
add_copies (ira_loop_tree_node_t loop_tree_node)
{
  basic_block bb;
  rtx insn;

  bb = loop_tree_node->bb;
  if (bb == NULL)
    return;
  FOR_BB_INSNS (bb, insn)
576
    if (NONDEBUG_INSN_P (insn))
Vladimir Makarov committed
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
      add_insn_allocno_copies (insn);
}

/* Propagate copies the corresponding allocnos on upper loop tree
   level.  */
static void
propagate_copies (void)
{
  ira_copy_t cp;
  ira_copy_iterator ci;
  ira_allocno_t a1, a2, parent_a1, parent_a2;

  FOR_EACH_COPY (cp, ci)
    {
      a1 = cp->first;
      a2 = cp->second;
      if (ALLOCNO_LOOP_TREE_NODE (a1) == ira_loop_tree_root)
	continue;
      ira_assert ((ALLOCNO_LOOP_TREE_NODE (a2) != ira_loop_tree_root));
596 597
      parent_a1 = ira_parent_or_cap_allocno (a1);
      parent_a2 = ira_parent_or_cap_allocno (a2);
Vladimir Makarov committed
598
      ira_assert (parent_a1 != NULL && parent_a2 != NULL);
599
      if (! allocnos_conflict_for_copy_p (parent_a1, parent_a2))
600 601
	ira_add_allocno_copy (parent_a1, parent_a2, cp->freq,
			      cp->constraint_p, cp->insn, cp->loop_tree_node);
Vladimir Makarov committed
602 603 604 605
    }
}

/* Array used to collect all conflict allocnos for given allocno.  */
606
static ira_object_t *collected_conflict_objects;
Vladimir Makarov committed
607 608

/* Build conflict vectors or bit conflict vectors (whatever is more
609
   profitable) for object OBJ from the conflict table.  */
Vladimir Makarov committed
610
static void
611
build_object_conflicts (ira_object_t obj)
Vladimir Makarov committed
612 613
{
  int i, px, parent_num;
614
  ira_allocno_t parent_a, another_parent_a;
615 616 617
  ira_object_t parent_obj;
  ira_allocno_t a = OBJECT_ALLOCNO (obj);
  IRA_INT_TYPE *object_conflicts;
618
  minmax_set_iterator asi;
619
  int parent_min, parent_max ATTRIBUTE_UNUSED;
Vladimir Makarov committed
620

621
  object_conflicts = conflicts[OBJECT_CONFLICT_ID (obj)];
Vladimir Makarov committed
622
  px = 0;
623
  FOR_EACH_BIT_IN_MINMAX_SET (object_conflicts,
624
			      OBJECT_MIN (obj), OBJECT_MAX (obj), i, asi)
Vladimir Makarov committed
625
    {
626 627
      ira_object_t another_obj = ira_object_id_map[i];
      ira_allocno_t another_a = OBJECT_ALLOCNO (obj);
Vladimir Makarov committed
628

629
      ira_assert (ira_reg_classes_intersect_p
Vladimir Makarov committed
630
		  [ALLOCNO_CLASS (a)][ALLOCNO_CLASS (another_a)]);
631
      collected_conflict_objects[px++] = another_obj;
Vladimir Makarov committed
632
    }
633
  if (ira_conflict_vector_profitable_p (obj, px))
Vladimir Makarov committed
634
    {
635
      ira_object_t *vec;
636 637 638
      ira_allocate_conflict_vec (obj, px);
      vec = OBJECT_CONFLICT_VEC (obj);
      memcpy (vec, collected_conflict_objects, sizeof (ira_object_t) * px);
Vladimir Makarov committed
639
      vec[px] = NULL;
640
      OBJECT_NUM_CONFLICTS (obj) = px;
Vladimir Makarov committed
641 642 643
    }
  else
    {
644
      int conflict_bit_vec_words_num;
Vladimir Makarov committed
645

646
      OBJECT_CONFLICT_ARRAY (obj) = object_conflicts;
647
      if (OBJECT_MAX (obj) < OBJECT_MIN (obj))
Vladimir Makarov committed
648 649 650
	conflict_bit_vec_words_num = 0;
      else
	conflict_bit_vec_words_num
651
	  = ((OBJECT_MAX (obj) - OBJECT_MIN (obj) + IRA_INT_BITS)
Vladimir Makarov committed
652
	     / IRA_INT_BITS);
653
      OBJECT_CONFLICT_ARRAY_SIZE (obj)
Vladimir Makarov committed
654 655
	= conflict_bit_vec_words_num * sizeof (IRA_INT_TYPE);
    }
656

657 658
  parent_a = ira_parent_or_cap_allocno (a);
  if (parent_a == NULL)
Vladimir Makarov committed
659
    return;
Vladimir Makarov committed
660
  ira_assert (ALLOCNO_CLASS (a) == ALLOCNO_CLASS (parent_a));
661 662
  ira_assert (ALLOCNO_NUM_OBJECTS (a) == ALLOCNO_NUM_OBJECTS (parent_a));
  parent_obj = ALLOCNO_OBJECT (parent_a, OBJECT_SUBWORD (obj));
663
  parent_num = OBJECT_CONFLICT_ID (parent_obj);
Vladimir Makarov committed
664 665
  parent_min = OBJECT_MIN (parent_obj);
  parent_max = OBJECT_MAX (parent_obj);
666
  FOR_EACH_BIT_IN_MINMAX_SET (object_conflicts,
667
			      OBJECT_MIN (obj), OBJECT_MAX (obj), i, asi)
Vladimir Makarov committed
668
    {
669 670
      ira_object_t another_obj = ira_object_id_map[i];
      ira_allocno_t another_a = OBJECT_ALLOCNO (another_obj);
671
      int another_word = OBJECT_SUBWORD (another_obj);
672

673
      ira_assert (ira_reg_classes_intersect_p
Vladimir Makarov committed
674
		  [ALLOCNO_CLASS (a)][ALLOCNO_CLASS (another_a)]);
675

676 677
      another_parent_a = ira_parent_or_cap_allocno (another_a);
      if (another_parent_a == NULL)
Vladimir Makarov committed
678 679
	continue;
      ira_assert (ALLOCNO_NUM (another_parent_a) >= 0);
Vladimir Makarov committed
680 681
      ira_assert (ALLOCNO_CLASS (another_a)
		  == ALLOCNO_CLASS (another_parent_a));
682 683
      ira_assert (ALLOCNO_NUM_OBJECTS (another_a)
		  == ALLOCNO_NUM_OBJECTS (another_parent_a));
684
      SET_MINMAX_SET_BIT (conflicts[parent_num],
685
			  OBJECT_CONFLICT_ID (ALLOCNO_OBJECT (another_parent_a,
Vladimir Makarov committed
686 687
							      another_word)),
			  parent_min, parent_max);
Vladimir Makarov committed
688 689 690 691 692 693 694 695 696 697 698
    }
}

/* Build conflict vectors or bit conflict vectors (whatever is more
   profitable) of all allocnos from the conflict table.  */
static void
build_conflicts (void)
{
  int i;
  ira_allocno_t a, cap;

699 700 701
  collected_conflict_objects
    = (ira_object_t *) ira_allocate (sizeof (ira_object_t)
					  * ira_objects_num);
Vladimir Makarov committed
702 703 704 705 706
  for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
    for (a = ira_regno_allocno_map[i];
	 a != NULL;
	 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
      {
707 708 709 710 711 712 713 714 715 716 717 718
	int j, nregs = ALLOCNO_NUM_OBJECTS (a);
	for (j = 0; j < nregs; j++)
	  {
	    ira_object_t obj = ALLOCNO_OBJECT (a, j);
	    build_object_conflicts (obj);
	    for (cap = ALLOCNO_CAP (a); cap != NULL; cap = ALLOCNO_CAP (cap))
	      {
		ira_object_t cap_obj = ALLOCNO_OBJECT (cap, j);
		gcc_assert (ALLOCNO_NUM_OBJECTS (cap) == ALLOCNO_NUM_OBJECTS (a));
		build_object_conflicts (cap_obj);
	      }
	  }
Vladimir Makarov committed
719
      }
720
  ira_free (collected_conflict_objects);
Vladimir Makarov committed
721 722 723 724 725 726 727 728 729 730
}



/* Print hard reg set SET with TITLE to FILE.  */
static void
print_hard_reg_set (FILE *file, const char *title, HARD_REG_SET set)
{
  int i, start;

731
  fputs (title, file);
Vladimir Makarov committed
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
  for (start = -1, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
    {
      if (TEST_HARD_REG_BIT (set, i))
	{
	  if (i == 0 || ! TEST_HARD_REG_BIT (set, i - 1))
	    start = i;
	}
      if (start >= 0
	  && (i == FIRST_PSEUDO_REGISTER - 1 || ! TEST_HARD_REG_BIT (set, i)))
	{
	  if (start == i - 1)
	    fprintf (file, " %d", start);
	  else if (start == i - 2)
	    fprintf (file, " %d %d", start, start + 1);
	  else
	    fprintf (file, " %d-%d", start, i - 1);
	  start = -1;
	}
    }
751
  putc ('\n', file);
Vladimir Makarov committed
752 753 754
}

static void
755
print_allocno_conflicts (FILE * file, bool reg_p, ira_allocno_t a)
Vladimir Makarov committed
756 757
{
  HARD_REG_SET conflicting_hard_regs;
758
  basic_block bb;
759
  int n, i;
Vladimir Makarov committed
760

761 762 763
  if (reg_p)
    fprintf (file, ";; r%d", ALLOCNO_REGNO (a));
  else
Vladimir Makarov committed
764
    {
765 766 767
      fprintf (file, ";; a%d(r%d,", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
      if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
        fprintf (file, "b%d", bb->index);
Vladimir Makarov committed
768
      else
769
        fprintf (file, "l%d", ALLOCNO_LOOP_TREE_NODE (a)->loop_num);
770 771
      putc (')', file);
    }
772

773
  fputs (" conflicts:", file);
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
  n = ALLOCNO_NUM_OBJECTS (a);
  for (i = 0; i < n; i++)
    {
      ira_object_t obj = ALLOCNO_OBJECT (a, i);
      ira_object_t conflict_obj;
      ira_object_conflict_iterator oci;

      if (OBJECT_CONFLICT_ARRAY (obj) == NULL)
	continue;
      if (n > 1)
	fprintf (file, "\n;;   subobject %d:", i);
      FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
	{
	  ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
	  if (reg_p)
	    fprintf (file, " r%d,", ALLOCNO_REGNO (conflict_a));
	  else
	    {
	      fprintf (file, " a%d(r%d", ALLOCNO_NUM (conflict_a),
		       ALLOCNO_REGNO (conflict_a));
	      if (ALLOCNO_NUM_OBJECTS (conflict_a) > 1)
		fprintf (file, ",w%d", OBJECT_SUBWORD (conflict_obj));
	      if ((bb = ALLOCNO_LOOP_TREE_NODE (conflict_a)->bb) != NULL)
		fprintf (file, ",b%d", bb->index);
	      else
		fprintf (file, ",l%d",
800
			 ALLOCNO_LOOP_TREE_NODE (conflict_a)->loop_num);
801 802 803 804 805 806
	      putc (')', file);
	    }
	}
      COPY_HARD_REG_SET (conflicting_hard_regs, OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
      AND_COMPL_HARD_REG_SET (conflicting_hard_regs, ira_no_alloc_regs);
      AND_HARD_REG_SET (conflicting_hard_regs,
Vladimir Makarov committed
807
			reg_class_contents[ALLOCNO_CLASS (a)]);
808 809 810 811 812 813
      print_hard_reg_set (file, "\n;;     total conflict hard regs:",
			  conflicting_hard_regs);

      COPY_HARD_REG_SET (conflicting_hard_regs, OBJECT_CONFLICT_HARD_REGS (obj));
      AND_COMPL_HARD_REG_SET (conflicting_hard_regs, ira_no_alloc_regs);
      AND_HARD_REG_SET (conflicting_hard_regs,
Vladimir Makarov committed
814
			reg_class_contents[ALLOCNO_CLASS (a)]);
815 816 817 818
      print_hard_reg_set (file, ";;     conflict hard regs:",
			  conflicting_hard_regs);
      putc ('\n', file);
    }
819

Vladimir Makarov committed
820 821 822
}

/* Print information about allocno or only regno (if REG_P) conflicts
823 824 825 826 827 828 829 830 831 832 833 834
   to FILE.  */
static void
print_conflicts (FILE *file, bool reg_p)
{
  ira_allocno_t a;
  ira_allocno_iterator ai;

  FOR_EACH_ALLOCNO (a, ai)
    print_allocno_conflicts (file, reg_p, a);
}

/* Print information about allocno or only regno (if REG_P) conflicts
Vladimir Makarov committed
835 836 837 838 839 840 841 842 843 844 845 846 847 848
   to stderr.  */
void
ira_debug_conflicts (bool reg_p)
{
  print_conflicts (stderr, reg_p);
}



/* Entry function which builds allocno conflicts and allocno copies
   and accumulate some allocno info on upper level regions.  */
void
ira_build_conflicts (void)
{
849
  enum reg_class base;
Vladimir Makarov committed
850 851
  ira_allocno_t a;
  ira_allocno_iterator ai;
852
  HARD_REG_SET temp_hard_reg_set;
Vladimir Makarov committed
853

854
  if (ira_conflicts_p)
Vladimir Makarov committed
855
    {
856 857
      ira_conflicts_p = build_conflict_bit_table ();
      if (ira_conflicts_p)
Vladimir Makarov committed
858
	{
859 860 861
	  ira_object_t obj;
	  ira_object_iterator oi;

862 863 864 865 866 867
	  build_conflicts ();
	  ira_traverse_loop_tree (true, ira_loop_tree_root, NULL, add_copies);
	  /* We need finished conflict table for the subsequent call.  */
	  if (flag_ira_region == IRA_REGION_ALL
	      || flag_ira_region == IRA_REGION_MIXED)
	    propagate_copies ();
868

869
	  /* Now we can free memory for the conflict table (see function
870
	     build_object_conflicts for details).  */
871
	  FOR_EACH_OBJECT (obj, oi)
872
	    {
873 874
	      if (OBJECT_CONFLICT_ARRAY (obj) != conflicts[OBJECT_CONFLICT_ID (obj)])
		ira_free (conflicts[OBJECT_CONFLICT_ID (obj)]);
875 876
	    }
	  ira_free (conflicts);
Vladimir Makarov committed
877 878
	}
    }
879 880
  base = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC, ADDRESS, SCRATCH);
  if (! targetm.class_likely_spilled_p (base))
881 882 883
    CLEAR_HARD_REG_SET (temp_hard_reg_set);
  else
    {
884
      COPY_HARD_REG_SET (temp_hard_reg_set, reg_class_contents[base]);
885 886 887
      AND_COMPL_HARD_REG_SET (temp_hard_reg_set, ira_no_alloc_regs);
      AND_HARD_REG_SET (temp_hard_reg_set, call_used_reg_set);
    }
Vladimir Makarov committed
888 889
  FOR_EACH_ALLOCNO (a, ai)
    {
890
      int i, n = ALLOCNO_NUM_OBJECTS (a);
Vladimir Makarov committed
891

892
      for (i = 0; i < n; i++)
Vladimir Makarov committed
893
	{
894
	  ira_object_t obj = ALLOCNO_OBJECT (a, i);
895
	  rtx allocno_reg = regno_reg_rtx [ALLOCNO_REGNO (a)];
896 897 898 899

	  if ((! flag_caller_saves && ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
	      /* For debugging purposes don't put user defined variables in
		 callee-clobbered registers.  */
900
	      || (optimize == 0 && REG_USERVAR_P (allocno_reg)))
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
	    {
	      IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
				call_used_reg_set);
	      IOR_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj),
				call_used_reg_set);
	    }
	  else if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
	    {
	      IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
				no_caller_save_reg_set);
	      IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
				temp_hard_reg_set);
	      IOR_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj),
				no_caller_save_reg_set);
	      IOR_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj),
				temp_hard_reg_set);
	    }
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934

	  if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
	    {
	      int regno;

	      /* Allocnos bigger than the saved part of call saved
		 regs must conflict with them.  */
	      for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
		if (!TEST_HARD_REG_BIT (call_used_reg_set, regno)
		    && HARD_REGNO_CALL_PART_CLOBBERED (regno,
						       obj->allocno->mode))
		  {
		    SET_HARD_REG_BIT (OBJECT_CONFLICT_HARD_REGS (obj), regno);
		    SET_HARD_REG_BIT (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
				      regno);
		  }
	    }
Vladimir Makarov committed
935 936
	}
    }
937 938
  if (optimize && ira_conflicts_p
      && internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
Vladimir Makarov committed
939 940
    print_conflicts (ira_dump_file, false);
}