re PR bootstrap/46810 (sparc64-linux bootstrap fails with "C++ preprocessor "/lib...
[official-gcc.git] / gcc / ira-costs.c
blob2a7a9a8c41880364257bfc7f4bfa1ccd310f19e6
1 /* IRA hard register and memory cost calculation for allocnos or pseudos.
2 Copyright (C) 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "hard-reg-set.h"
27 #include "rtl.h"
28 #include "expr.h"
29 #include "tm_p.h"
30 #include "flags.h"
31 #include "basic-block.h"
32 #include "regs.h"
33 #include "addresses.h"
34 #include "insn-config.h"
35 #include "recog.h"
36 #include "reload.h"
37 #include "diagnostic-core.h"
38 #include "target.h"
39 #include "params.h"
40 #include "ira-int.h"
42 /* The flags is set up every time when we calculate pseudo register
43 classes through function ira_set_pseudo_classes. */
44 static bool pseudo_classes_defined_p = false;
46 /* TRUE if we work with allocnos. Otherwise we work with pseudos. */
47 static bool allocno_p;
49 /* Number of elements in arrays `in_inc_dec' and `costs'. */
50 static int cost_elements_num;
52 #ifdef FORBIDDEN_INC_DEC_CLASSES
53 /* Indexed by n, is TRUE if allocno or pseudo with number N is used in
54 an auto-inc or auto-dec context. */
55 static bool *in_inc_dec;
56 #endif
58 /* The `costs' struct records the cost of using hard registers of each
59 class considered for the calculation and of using memory for each
60 allocno or pseudo. */
61 struct costs
63 int mem_cost;
64 /* Costs for register classes start here. We process only some
65 register classes (cover classes on the 1st cost calculation
66 iteration and important classes on the 2nd iteration). */
67 int cost[1];
70 #define max_struct_costs_size \
71 (this_target_ira_int->x_max_struct_costs_size)
72 #define init_cost \
73 (this_target_ira_int->x_init_cost)
74 #define temp_costs \
75 (this_target_ira_int->x_temp_costs)
76 #define op_costs \
77 (this_target_ira_int->x_op_costs)
78 #define this_op_costs \
79 (this_target_ira_int->x_this_op_costs)
80 #define cost_classes \
81 (this_target_ira_int->x_cost_classes)
83 /* Costs of each class for each allocno or pseudo. */
84 static struct costs *costs;
86 /* Accumulated costs of each class for each allocno. */
87 static struct costs *total_allocno_costs;
89 /* The size of the previous array. */
90 static int cost_classes_num;
92 /* Map: cost class -> order number (they start with 0) of the cost
93 class. The order number is negative for non-cost classes. */
94 static int cost_class_nums[N_REG_CLASSES];
96 /* It is the current size of struct costs. */
97 static int struct_costs_size;
99 /* Return pointer to structure containing costs of allocno or pseudo
100 with given NUM in array ARR. */
101 #define COSTS(arr, num) \
102 ((struct costs *) ((char *) (arr) + (num) * struct_costs_size))
104 /* Return index in COSTS when processing reg with REGNO. */
105 #define COST_INDEX(regno) (allocno_p \
106 ? ALLOCNO_NUM (ira_curr_regno_allocno_map[regno]) \
107 : (int) regno)
109 /* Record register class preferences of each allocno or pseudo. Null
110 value means no preferences. It happens on the 1st iteration of the
111 cost calculation. */
112 static enum reg_class *pref;
114 /* Allocated buffers for pref. */
115 static enum reg_class *pref_buffer;
117 /* Record cover register class of each allocno with the same regno. */
118 static enum reg_class *regno_cover_class;
120 /* Record cost gains for not allocating a register with an invariant
121 equivalence. */
122 static int *regno_equiv_gains;
124 /* Execution frequency of the current insn. */
125 static int frequency;
129 /* Compute the cost of loading X into (if TO_P is TRUE) or from (if
130 TO_P is FALSE) a register of class RCLASS in mode MODE. X must not
131 be a pseudo register. */
132 static int
133 copy_cost (rtx x, enum machine_mode mode, reg_class_t rclass, bool to_p,
134 secondary_reload_info *prev_sri)
136 secondary_reload_info sri;
137 reg_class_t secondary_class = NO_REGS;
139 /* If X is a SCRATCH, there is actually nothing to move since we are
140 assuming optimal allocation. */
141 if (GET_CODE (x) == SCRATCH)
142 return 0;
144 /* Get the class we will actually use for a reload. */
145 rclass = targetm.preferred_reload_class (x, rclass);
147 /* If we need a secondary reload for an intermediate, the cost is
148 that to load the input into the intermediate register, then to
149 copy it. */
150 sri.prev_sri = prev_sri;
151 sri.extra_cost = 0;
152 secondary_class = targetm.secondary_reload (to_p, x, rclass, mode, &sri);
154 if (secondary_class != NO_REGS)
156 if (!move_cost[mode])
157 init_move_cost (mode);
158 return (move_cost[mode][(int) secondary_class][(int) rclass]
159 + sri.extra_cost
160 + copy_cost (x, mode, secondary_class, to_p, &sri));
163 /* For memory, use the memory move cost, for (hard) registers, use
164 the cost to move between the register classes, and use 2 for
165 everything else (constants). */
166 if (MEM_P (x) || rclass == NO_REGS)
167 return sri.extra_cost
168 + ira_memory_move_cost[mode][(int) rclass][to_p != 0];
169 else if (REG_P (x))
171 if (!move_cost[mode])
172 init_move_cost (mode);
173 return (sri.extra_cost
174 + move_cost[mode][REGNO_REG_CLASS (REGNO (x))][(int) rclass]);
176 else
177 /* If this is a constant, we may eventually want to call rtx_cost
178 here. */
179 return sri.extra_cost + COSTS_N_INSNS (1);
184 /* Record the cost of using memory or hard registers of various
185 classes for the operands in INSN.
187 N_ALTS is the number of alternatives.
188 N_OPS is the number of operands.
189 OPS is an array of the operands.
190 MODES are the modes of the operands, in case any are VOIDmode.
191 CONSTRAINTS are the constraints to use for the operands. This array
192 is modified by this procedure.
194 This procedure works alternative by alternative. For each
195 alternative we assume that we will be able to allocate all allocnos
196 to their ideal register class and calculate the cost of using that
197 alternative. Then we compute, for each operand that is a
198 pseudo-register, the cost of having the allocno allocated to each
199 register class and using it in that alternative. To this cost is
200 added the cost of the alternative.
202 The cost of each class for this insn is its lowest cost among all
203 the alternatives. */
204 static void
205 record_reg_classes (int n_alts, int n_ops, rtx *ops,
206 enum machine_mode *modes, const char **constraints,
207 rtx insn, enum reg_class *pref)
209 int alt;
210 int i, j, k;
211 rtx set;
212 int insn_allows_mem[MAX_RECOG_OPERANDS];
214 for (i = 0; i < n_ops; i++)
215 insn_allows_mem[i] = 0;
217 /* Process each alternative, each time minimizing an operand's cost
218 with the cost for each operand in that alternative. */
219 for (alt = 0; alt < n_alts; alt++)
221 enum reg_class classes[MAX_RECOG_OPERANDS];
222 int allows_mem[MAX_RECOG_OPERANDS];
223 enum reg_class rclass;
224 int alt_fail = 0;
225 int alt_cost = 0, op_cost_add;
227 if (!recog_data.alternative_enabled_p[alt])
229 for (i = 0; i < recog_data.n_operands; i++)
230 constraints[i] = skip_alternative (constraints[i]);
232 continue;
235 for (i = 0; i < n_ops; i++)
237 unsigned char c;
238 const char *p = constraints[i];
239 rtx op = ops[i];
240 enum machine_mode mode = modes[i];
241 int allows_addr = 0;
242 int win = 0;
244 /* Initially show we know nothing about the register class. */
245 classes[i] = NO_REGS;
246 allows_mem[i] = 0;
248 /* If this operand has no constraints at all, we can
249 conclude nothing about it since anything is valid. */
250 if (*p == 0)
252 if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
253 memset (this_op_costs[i], 0, struct_costs_size);
254 continue;
257 /* If this alternative is only relevant when this operand
258 matches a previous operand, we do different things
259 depending on whether this operand is a allocno-reg or not.
260 We must process any modifiers for the operand before we
261 can make this test. */
262 while (*p == '%' || *p == '=' || *p == '+' || *p == '&')
263 p++;
265 if (p[0] >= '0' && p[0] <= '0' + i && (p[1] == ',' || p[1] == 0))
267 /* Copy class and whether memory is allowed from the
268 matching alternative. Then perform any needed cost
269 computations and/or adjustments. */
270 j = p[0] - '0';
271 classes[i] = classes[j];
272 allows_mem[i] = allows_mem[j];
273 if (allows_mem[i])
274 insn_allows_mem[i] = 1;
276 if (! REG_P (op) || REGNO (op) < FIRST_PSEUDO_REGISTER)
278 /* If this matches the other operand, we have no
279 added cost and we win. */
280 if (rtx_equal_p (ops[j], op))
281 win = 1;
282 /* If we can put the other operand into a register,
283 add to the cost of this alternative the cost to
284 copy this operand to the register used for the
285 other operand. */
286 else if (classes[j] != NO_REGS)
288 alt_cost += copy_cost (op, mode, classes[j], 1, NULL);
289 win = 1;
292 else if (! REG_P (ops[j])
293 || REGNO (ops[j]) < FIRST_PSEUDO_REGISTER)
295 /* This op is an allocno but the one it matches is
296 not. */
298 /* If we can't put the other operand into a
299 register, this alternative can't be used. */
301 if (classes[j] == NO_REGS)
302 alt_fail = 1;
303 /* Otherwise, add to the cost of this alternative
304 the cost to copy the other operand to the hard
305 register used for this operand. */
306 else
307 alt_cost += copy_cost (ops[j], mode, classes[j], 1, NULL);
309 else
311 /* The costs of this operand are not the same as the
312 other operand since move costs are not symmetric.
313 Moreover, if we cannot tie them, this alternative
314 needs to do a copy, which is one insn. */
315 struct costs *pp = this_op_costs[i];
317 for (k = 0; k < cost_classes_num; k++)
319 rclass = cost_classes[k];
320 pp->cost[k]
321 = (((recog_data.operand_type[i] != OP_OUT
322 ? ira_get_may_move_cost (mode, rclass,
323 classes[i], true) : 0)
324 + (recog_data.operand_type[i] != OP_IN
325 ? ira_get_may_move_cost (mode, classes[i],
326 rclass, false) : 0))
327 * frequency);
330 /* If the alternative actually allows memory, make
331 things a bit cheaper since we won't need an extra
332 insn to load it. */
333 pp->mem_cost
334 = ((recog_data.operand_type[i] != OP_IN
335 ? ira_memory_move_cost[mode][classes[i]][0] : 0)
336 + (recog_data.operand_type[i] != OP_OUT
337 ? ira_memory_move_cost[mode][classes[i]][1] : 0)
338 - allows_mem[i]) * frequency;
340 /* If we have assigned a class to this allocno in our
341 first pass, add a cost to this alternative
342 corresponding to what we would add if this allocno
343 were not in the appropriate class. We could use
344 cover class here but it is less accurate
345 approximation. */
346 if (pref)
348 enum reg_class pref_class = pref[COST_INDEX (REGNO (op))];
350 if (pref_class == NO_REGS)
351 alt_cost
352 += ((recog_data.operand_type[i] != OP_IN
353 ? ira_memory_move_cost[mode][classes[i]][0]
354 : 0)
355 + (recog_data.operand_type[i] != OP_OUT
356 ? ira_memory_move_cost[mode][classes[i]][1]
357 : 0));
358 else if (ira_reg_class_intersect
359 [pref_class][classes[i]] == NO_REGS)
360 alt_cost += ira_get_register_move_cost (mode,
361 pref_class,
362 classes[i]);
364 if (REGNO (ops[i]) != REGNO (ops[j])
365 && ! find_reg_note (insn, REG_DEAD, op))
366 alt_cost += 2;
368 /* This is in place of ordinary cost computation for
369 this operand, so skip to the end of the
370 alternative (should be just one character). */
371 while (*p && *p++ != ',')
374 constraints[i] = p;
375 continue;
379 /* Scan all the constraint letters. See if the operand
380 matches any of the constraints. Collect the valid
381 register classes and see if this operand accepts
382 memory. */
383 while ((c = *p))
385 switch (c)
387 case ',':
388 break;
389 case '*':
390 /* Ignore the next letter for this pass. */
391 c = *++p;
392 break;
394 case '?':
395 alt_cost += 2;
396 case '!': case '#': case '&':
397 case '0': case '1': case '2': case '3': case '4':
398 case '5': case '6': case '7': case '8': case '9':
399 break;
401 case 'p':
402 allows_addr = 1;
403 win = address_operand (op, GET_MODE (op));
404 /* We know this operand is an address, so we want it
405 to be allocated to a register that can be the
406 base of an address, i.e. BASE_REG_CLASS. */
407 classes[i]
408 = ira_reg_class_union[classes[i]]
409 [base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
410 break;
412 case 'm': case 'o': case 'V':
413 /* It doesn't seem worth distinguishing between
414 offsettable and non-offsettable addresses
415 here. */
416 insn_allows_mem[i] = allows_mem[i] = 1;
417 if (MEM_P (op))
418 win = 1;
419 break;
421 case '<':
422 if (MEM_P (op)
423 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
424 || GET_CODE (XEXP (op, 0)) == POST_DEC))
425 win = 1;
426 break;
428 case '>':
429 if (MEM_P (op)
430 && (GET_CODE (XEXP (op, 0)) == PRE_INC
431 || GET_CODE (XEXP (op, 0)) == POST_INC))
432 win = 1;
433 break;
435 case 'E':
436 case 'F':
437 if (GET_CODE (op) == CONST_DOUBLE
438 || (GET_CODE (op) == CONST_VECTOR
439 && (GET_MODE_CLASS (GET_MODE (op))
440 == MODE_VECTOR_FLOAT)))
441 win = 1;
442 break;
444 case 'G':
445 case 'H':
446 if (GET_CODE (op) == CONST_DOUBLE
447 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
448 win = 1;
449 break;
451 case 's':
452 if (CONST_INT_P (op)
453 || (GET_CODE (op) == CONST_DOUBLE
454 && GET_MODE (op) == VOIDmode))
455 break;
457 case 'i':
458 if (CONSTANT_P (op)
459 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op)))
460 win = 1;
461 break;
463 case 'n':
464 if (CONST_INT_P (op)
465 || (GET_CODE (op) == CONST_DOUBLE
466 && GET_MODE (op) == VOIDmode))
467 win = 1;
468 break;
470 case 'I':
471 case 'J':
472 case 'K':
473 case 'L':
474 case 'M':
475 case 'N':
476 case 'O':
477 case 'P':
478 if (CONST_INT_P (op)
479 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
480 win = 1;
481 break;
483 case 'X':
484 win = 1;
485 break;
487 case 'g':
488 if (MEM_P (op)
489 || (CONSTANT_P (op)
490 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))))
491 win = 1;
492 insn_allows_mem[i] = allows_mem[i] = 1;
493 case 'r':
494 classes[i] = ira_reg_class_union[classes[i]][GENERAL_REGS];
495 break;
497 default:
498 if (REG_CLASS_FROM_CONSTRAINT (c, p) != NO_REGS)
499 classes[i] = ira_reg_class_union[classes[i]]
500 [REG_CLASS_FROM_CONSTRAINT (c, p)];
501 #ifdef EXTRA_CONSTRAINT_STR
502 else if (EXTRA_CONSTRAINT_STR (op, c, p))
503 win = 1;
505 if (EXTRA_MEMORY_CONSTRAINT (c, p))
507 /* Every MEM can be reloaded to fit. */
508 insn_allows_mem[i] = allows_mem[i] = 1;
509 if (MEM_P (op))
510 win = 1;
512 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
514 /* Every address can be reloaded to fit. */
515 allows_addr = 1;
516 if (address_operand (op, GET_MODE (op)))
517 win = 1;
518 /* We know this operand is an address, so we
519 want it to be allocated to a hard register
520 that can be the base of an address,
521 i.e. BASE_REG_CLASS. */
522 classes[i]
523 = ira_reg_class_union[classes[i]]
524 [base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
526 #endif
527 break;
529 p += CONSTRAINT_LEN (c, p);
530 if (c == ',')
531 break;
534 constraints[i] = p;
536 /* How we account for this operand now depends on whether it
537 is a pseudo register or not. If it is, we first check if
538 any register classes are valid. If not, we ignore this
539 alternative, since we want to assume that all allocnos get
540 allocated for register preferencing. If some register
541 class is valid, compute the costs of moving the allocno
542 into that class. */
543 if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
545 if (classes[i] == NO_REGS)
547 /* We must always fail if the operand is a REG, but
548 we did not find a suitable class.
550 Otherwise we may perform an uninitialized read
551 from this_op_costs after the `continue' statement
552 below. */
553 alt_fail = 1;
555 else
557 struct costs *pp = this_op_costs[i];
559 for (k = 0; k < cost_classes_num; k++)
561 rclass = cost_classes[k];
562 pp->cost[k]
563 = (((recog_data.operand_type[i] != OP_OUT
564 ? ira_get_may_move_cost (mode, rclass,
565 classes[i], true) : 0)
566 + (recog_data.operand_type[i] != OP_IN
567 ? ira_get_may_move_cost (mode, classes[i],
568 rclass, false) : 0))
569 * frequency);
572 /* If the alternative actually allows memory, make
573 things a bit cheaper since we won't need an extra
574 insn to load it. */
575 pp->mem_cost
576 = ((recog_data.operand_type[i] != OP_IN
577 ? ira_memory_move_cost[mode][classes[i]][0] : 0)
578 + (recog_data.operand_type[i] != OP_OUT
579 ? ira_memory_move_cost[mode][classes[i]][1] : 0)
580 - allows_mem[i]) * frequency;
581 /* If we have assigned a class to this allocno in our
582 first pass, add a cost to this alternative
583 corresponding to what we would add if this allocno
584 were not in the appropriate class. We could use
585 cover class here but it is less accurate
586 approximation. */
587 if (pref)
589 enum reg_class pref_class = pref[COST_INDEX (REGNO (op))];
591 if (pref_class == NO_REGS)
592 alt_cost
593 += ((recog_data.operand_type[i] != OP_IN
594 ? ira_memory_move_cost[mode][classes[i]][0]
595 : 0)
596 + (recog_data.operand_type[i] != OP_OUT
597 ? ira_memory_move_cost[mode][classes[i]][1]
598 : 0));
599 else if (ira_reg_class_intersect[pref_class][classes[i]]
600 == NO_REGS)
601 alt_cost += ira_get_register_move_cost (mode,
602 pref_class,
603 classes[i]);
608 /* Otherwise, if this alternative wins, either because we
609 have already determined that or if we have a hard
610 register of the proper class, there is no cost for this
611 alternative. */
612 else if (win || (REG_P (op)
613 && reg_fits_class_p (op, classes[i],
614 0, GET_MODE (op))))
617 /* If registers are valid, the cost of this alternative
618 includes copying the object to and/or from a
619 register. */
620 else if (classes[i] != NO_REGS)
622 if (recog_data.operand_type[i] != OP_OUT)
623 alt_cost += copy_cost (op, mode, classes[i], 1, NULL);
625 if (recog_data.operand_type[i] != OP_IN)
626 alt_cost += copy_cost (op, mode, classes[i], 0, NULL);
628 /* The only other way this alternative can be used is if
629 this is a constant that could be placed into memory. */
630 else if (CONSTANT_P (op) && (allows_addr || allows_mem[i]))
631 alt_cost += ira_memory_move_cost[mode][classes[i]][1];
632 else
633 alt_fail = 1;
636 if (alt_fail)
637 continue;
639 op_cost_add = alt_cost * frequency;
640 /* Finally, update the costs with the information we've
641 calculated about this alternative. */
642 for (i = 0; i < n_ops; i++)
643 if (REG_P (ops[i]) && REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
645 struct costs *pp = op_costs[i], *qq = this_op_costs[i];
646 int scale = 1 + (recog_data.operand_type[i] == OP_INOUT);
648 pp->mem_cost = MIN (pp->mem_cost,
649 (qq->mem_cost + op_cost_add) * scale);
651 for (k = 0; k < cost_classes_num; k++)
652 pp->cost[k]
653 = MIN (pp->cost[k], (qq->cost[k] + op_cost_add) * scale);
657 if (allocno_p)
658 for (i = 0; i < n_ops; i++)
660 ira_allocno_t a;
661 rtx op = ops[i];
663 if (! REG_P (op) || REGNO (op) < FIRST_PSEUDO_REGISTER)
664 continue;
665 a = ira_curr_regno_allocno_map [REGNO (op)];
666 if (! ALLOCNO_BAD_SPILL_P (a) && insn_allows_mem[i] == 0)
667 ALLOCNO_BAD_SPILL_P (a) = true;
670 /* If this insn is a single set copying operand 1 to operand 0 and
671 one operand is an allocno with the other a hard reg or an allocno
672 that prefers a hard register that is in its own register class
673 then we may want to adjust the cost of that register class to -1.
675 Avoid the adjustment if the source does not die to avoid
676 stressing of register allocator by preferrencing two colliding
677 registers into single class.
679 Also avoid the adjustment if a copy between hard registers of the
680 class is expensive (ten times the cost of a default copy is
681 considered arbitrarily expensive). This avoids losing when the
682 preferred class is very expensive as the source of a copy
683 instruction. */
684 if ((set = single_set (insn)) != 0
685 && ops[0] == SET_DEST (set) && ops[1] == SET_SRC (set)
686 && REG_P (ops[0]) && REG_P (ops[1])
687 && find_regno_note (insn, REG_DEAD, REGNO (ops[1])))
688 for (i = 0; i <= 1; i++)
689 if (REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
691 unsigned int regno = REGNO (ops[!i]);
692 enum machine_mode mode = GET_MODE (ops[!i]);
693 enum reg_class rclass;
694 unsigned int nr;
696 if (regno < FIRST_PSEUDO_REGISTER)
697 for (k = 0; k < cost_classes_num; k++)
699 rclass = cost_classes[k];
700 if (TEST_HARD_REG_BIT (reg_class_contents[rclass], regno)
701 && (reg_class_size[rclass]
702 == (unsigned) CLASS_MAX_NREGS (rclass, mode)))
704 if (reg_class_size[rclass] == 1)
705 op_costs[i]->cost[k] = -frequency;
706 else
708 for (nr = 0;
709 nr < (unsigned) hard_regno_nregs[regno][mode];
710 nr++)
711 if (! TEST_HARD_REG_BIT (reg_class_contents[rclass],
712 regno + nr))
713 break;
715 if (nr == (unsigned) hard_regno_nregs[regno][mode])
716 op_costs[i]->cost[k] = -frequency;
725 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudo registers. */
726 static inline bool
727 ok_for_index_p_nonstrict (rtx reg)
729 unsigned regno = REGNO (reg);
731 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
734 /* A version of regno_ok_for_base_p for use here, when all
735 pseudo-registers should count as OK. Arguments as for
736 regno_ok_for_base_p. */
737 static inline bool
738 ok_for_base_p_nonstrict (rtx reg, enum machine_mode mode,
739 enum rtx_code outer_code, enum rtx_code index_code)
741 unsigned regno = REGNO (reg);
743 if (regno >= FIRST_PSEUDO_REGISTER)
744 return true;
745 return ok_for_base_p_1 (regno, mode, outer_code, index_code);
748 /* Record the pseudo registers we must reload into hard registers in a
749 subexpression of a memory address, X.
751 If CONTEXT is 0, we are looking at the base part of an address,
752 otherwise we are looking at the index part.
754 MODE is the mode of the memory reference; OUTER_CODE and INDEX_CODE
755 give the context that the rtx appears in. These three arguments
756 are passed down to base_reg_class.
758 SCALE is twice the amount to multiply the cost by (it is twice so
759 we can represent half-cost adjustments). */
760 static void
761 record_address_regs (enum machine_mode mode, rtx x, int context,
762 enum rtx_code outer_code, enum rtx_code index_code,
763 int scale)
765 enum rtx_code code = GET_CODE (x);
766 enum reg_class rclass;
768 if (context == 1)
769 rclass = INDEX_REG_CLASS;
770 else
771 rclass = base_reg_class (mode, outer_code, index_code);
773 switch (code)
775 case CONST_INT:
776 case CONST:
777 case CC0:
778 case PC:
779 case SYMBOL_REF:
780 case LABEL_REF:
781 return;
783 case PLUS:
784 /* When we have an address that is a sum, we must determine
785 whether registers are "base" or "index" regs. If there is a
786 sum of two registers, we must choose one to be the "base".
787 Luckily, we can use the REG_POINTER to make a good choice
788 most of the time. We only need to do this on machines that
789 can have two registers in an address and where the base and
790 index register classes are different.
792 ??? This code used to set REGNO_POINTER_FLAG in some cases,
793 but that seems bogus since it should only be set when we are
794 sure the register is being used as a pointer. */
796 rtx arg0 = XEXP (x, 0);
797 rtx arg1 = XEXP (x, 1);
798 enum rtx_code code0 = GET_CODE (arg0);
799 enum rtx_code code1 = GET_CODE (arg1);
801 /* Look inside subregs. */
802 if (code0 == SUBREG)
803 arg0 = SUBREG_REG (arg0), code0 = GET_CODE (arg0);
804 if (code1 == SUBREG)
805 arg1 = SUBREG_REG (arg1), code1 = GET_CODE (arg1);
807 /* If this machine only allows one register per address, it
808 must be in the first operand. */
809 if (MAX_REGS_PER_ADDRESS == 1)
810 record_address_regs (mode, arg0, 0, PLUS, code1, scale);
812 /* If index and base registers are the same on this machine,
813 just record registers in any non-constant operands. We
814 assume here, as well as in the tests below, that all
815 addresses are in canonical form. */
816 else if (INDEX_REG_CLASS == base_reg_class (VOIDmode, PLUS, SCRATCH))
818 record_address_regs (mode, arg0, context, PLUS, code1, scale);
819 if (! CONSTANT_P (arg1))
820 record_address_regs (mode, arg1, context, PLUS, code0, scale);
823 /* If the second operand is a constant integer, it doesn't
824 change what class the first operand must be. */
825 else if (code1 == CONST_INT || code1 == CONST_DOUBLE)
826 record_address_regs (mode, arg0, context, PLUS, code1, scale);
827 /* If the second operand is a symbolic constant, the first
828 operand must be an index register. */
829 else if (code1 == SYMBOL_REF || code1 == CONST || code1 == LABEL_REF)
830 record_address_regs (mode, arg0, 1, PLUS, code1, scale);
831 /* If both operands are registers but one is already a hard
832 register of index or reg-base class, give the other the
833 class that the hard register is not. */
834 else if (code0 == REG && code1 == REG
835 && REGNO (arg0) < FIRST_PSEUDO_REGISTER
836 && (ok_for_base_p_nonstrict (arg0, mode, PLUS, REG)
837 || ok_for_index_p_nonstrict (arg0)))
838 record_address_regs (mode, arg1,
839 ok_for_base_p_nonstrict (arg0, mode, PLUS, REG)
840 ? 1 : 0,
841 PLUS, REG, scale);
842 else if (code0 == REG && code1 == REG
843 && REGNO (arg1) < FIRST_PSEUDO_REGISTER
844 && (ok_for_base_p_nonstrict (arg1, mode, PLUS, REG)
845 || ok_for_index_p_nonstrict (arg1)))
846 record_address_regs (mode, arg0,
847 ok_for_base_p_nonstrict (arg1, mode, PLUS, REG)
848 ? 1 : 0,
849 PLUS, REG, scale);
850 /* If one operand is known to be a pointer, it must be the
851 base with the other operand the index. Likewise if the
852 other operand is a MULT. */
853 else if ((code0 == REG && REG_POINTER (arg0)) || code1 == MULT)
855 record_address_regs (mode, arg0, 0, PLUS, code1, scale);
856 record_address_regs (mode, arg1, 1, PLUS, code0, scale);
858 else if ((code1 == REG && REG_POINTER (arg1)) || code0 == MULT)
860 record_address_regs (mode, arg0, 1, PLUS, code1, scale);
861 record_address_regs (mode, arg1, 0, PLUS, code0, scale);
863 /* Otherwise, count equal chances that each might be a base or
864 index register. This case should be rare. */
865 else
867 record_address_regs (mode, arg0, 0, PLUS, code1, scale / 2);
868 record_address_regs (mode, arg0, 1, PLUS, code1, scale / 2);
869 record_address_regs (mode, arg1, 0, PLUS, code0, scale / 2);
870 record_address_regs (mode, arg1, 1, PLUS, code0, scale / 2);
873 break;
875 /* Double the importance of an allocno that is incremented or
876 decremented, since it would take two extra insns if it ends
877 up in the wrong place. */
878 case POST_MODIFY:
879 case PRE_MODIFY:
880 record_address_regs (mode, XEXP (x, 0), 0, code,
881 GET_CODE (XEXP (XEXP (x, 1), 1)), 2 * scale);
882 if (REG_P (XEXP (XEXP (x, 1), 1)))
883 record_address_regs (mode, XEXP (XEXP (x, 1), 1), 1, code, REG,
884 2 * scale);
885 break;
887 case POST_INC:
888 case PRE_INC:
889 case POST_DEC:
890 case PRE_DEC:
891 /* Double the importance of an allocno that is incremented or
892 decremented, since it would take two extra insns if it ends
893 up in the wrong place. If the operand is a pseudo-register,
894 show it is being used in an INC_DEC context. */
895 #ifdef FORBIDDEN_INC_DEC_CLASSES
896 if (REG_P (XEXP (x, 0))
897 && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER)
898 in_inc_dec[COST_INDEX (REGNO (XEXP (x, 0)))] = true;
899 #endif
900 record_address_regs (mode, XEXP (x, 0), 0, code, SCRATCH, 2 * scale);
901 break;
903 case REG:
905 struct costs *pp;
906 enum reg_class i;
907 int k;
909 if (REGNO (x) < FIRST_PSEUDO_REGISTER)
910 break;
912 if (allocno_p)
913 ALLOCNO_BAD_SPILL_P (ira_curr_regno_allocno_map[REGNO (x)]) = true;
914 pp = COSTS (costs, COST_INDEX (REGNO (x)));
915 pp->mem_cost += (ira_memory_move_cost[Pmode][rclass][1] * scale) / 2;
916 for (k = 0; k < cost_classes_num; k++)
918 i = cost_classes[k];
919 pp->cost[k]
920 += (ira_get_may_move_cost (Pmode, i, rclass, true) * scale) / 2;
923 break;
925 default:
927 const char *fmt = GET_RTX_FORMAT (code);
928 int i;
929 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
930 if (fmt[i] == 'e')
931 record_address_regs (mode, XEXP (x, i), context, code, SCRATCH,
932 scale);
939 /* Calculate the costs of insn operands. */
940 static void
941 record_operand_costs (rtx insn, enum reg_class *pref)
943 const char *constraints[MAX_RECOG_OPERANDS];
944 enum machine_mode modes[MAX_RECOG_OPERANDS];
945 int i;
947 for (i = 0; i < recog_data.n_operands; i++)
949 constraints[i] = recog_data.constraints[i];
950 modes[i] = recog_data.operand_mode[i];
953 /* If we get here, we are set up to record the costs of all the
954 operands for this insn. Start by initializing the costs. Then
955 handle any address registers. Finally record the desired classes
956 for any allocnos, doing it twice if some pair of operands are
957 commutative. */
958 for (i = 0; i < recog_data.n_operands; i++)
960 memcpy (op_costs[i], init_cost, struct_costs_size);
962 if (GET_CODE (recog_data.operand[i]) == SUBREG)
963 recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
965 if (MEM_P (recog_data.operand[i]))
966 record_address_regs (GET_MODE (recog_data.operand[i]),
967 XEXP (recog_data.operand[i], 0),
968 0, MEM, SCRATCH, frequency * 2);
969 else if (constraints[i][0] == 'p'
970 || EXTRA_ADDRESS_CONSTRAINT (constraints[i][0],
971 constraints[i]))
972 record_address_regs (VOIDmode, recog_data.operand[i], 0, ADDRESS,
973 SCRATCH, frequency * 2);
976 /* Check for commutative in a separate loop so everything will have
977 been initialized. We must do this even if one operand is a
978 constant--see addsi3 in m68k.md. */
979 for (i = 0; i < (int) recog_data.n_operands - 1; i++)
980 if (constraints[i][0] == '%')
982 const char *xconstraints[MAX_RECOG_OPERANDS];
983 int j;
985 /* Handle commutative operands by swapping the constraints.
986 We assume the modes are the same. */
987 for (j = 0; j < recog_data.n_operands; j++)
988 xconstraints[j] = constraints[j];
990 xconstraints[i] = constraints[i+1];
991 xconstraints[i+1] = constraints[i];
992 record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
993 recog_data.operand, modes,
994 xconstraints, insn, pref);
996 record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
997 recog_data.operand, modes,
998 constraints, insn, pref);
1003 /* Process one insn INSN. Scan it and record each time it would save
1004 code to put a certain allocnos in a certain class. Return the last
1005 insn processed, so that the scan can be continued from there. */
1006 static rtx
1007 scan_one_insn (rtx insn)
1009 enum rtx_code pat_code;
1010 rtx set, note;
1011 int i, k;
1013 if (!NONDEBUG_INSN_P (insn))
1014 return insn;
1016 pat_code = GET_CODE (PATTERN (insn));
1017 if (pat_code == USE || pat_code == CLOBBER || pat_code == ASM_INPUT
1018 || pat_code == ADDR_VEC || pat_code == ADDR_DIFF_VEC)
1019 return insn;
1021 set = single_set (insn);
1022 extract_insn (insn);
1024 /* If this insn loads a parameter from its stack slot, then it
1025 represents a savings, rather than a cost, if the parameter is
1026 stored in memory. Record this fact. */
1027 if (set != 0 && REG_P (SET_DEST (set)) && MEM_P (SET_SRC (set))
1028 && (note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != NULL_RTX
1029 && MEM_P (XEXP (note, 0)))
1031 enum reg_class cl = GENERAL_REGS;
1032 rtx reg = SET_DEST (set);
1033 int num = COST_INDEX (REGNO (reg));
1035 if (pref)
1036 cl = pref[num];
1037 COSTS (costs, num)->mem_cost
1038 -= ira_memory_move_cost[GET_MODE (reg)][cl][1] * frequency;
1039 record_address_regs (GET_MODE (SET_SRC (set)), XEXP (SET_SRC (set), 0),
1040 0, MEM, SCRATCH, frequency * 2);
1043 record_operand_costs (insn, pref);
1045 /* Now add the cost for each operand to the total costs for its
1046 allocno. */
1047 for (i = 0; i < recog_data.n_operands; i++)
1048 if (REG_P (recog_data.operand[i])
1049 && REGNO (recog_data.operand[i]) >= FIRST_PSEUDO_REGISTER)
1051 int regno = REGNO (recog_data.operand[i]);
1052 struct costs *p = COSTS (costs, COST_INDEX (regno));
1053 struct costs *q = op_costs[i];
1055 p->mem_cost += q->mem_cost;
1056 for (k = 0; k < cost_classes_num; k++)
1057 p->cost[k] += q->cost[k];
1060 return insn;
1065 /* Print allocnos costs to file F. */
1066 static void
1067 print_allocno_costs (FILE *f)
1069 int k;
1070 ira_allocno_t a;
1071 ira_allocno_iterator ai;
1073 ira_assert (allocno_p);
1074 fprintf (f, "\n");
1075 FOR_EACH_ALLOCNO (a, ai)
1077 int i, rclass;
1078 basic_block bb;
1079 int regno = ALLOCNO_REGNO (a);
1081 i = ALLOCNO_NUM (a);
1082 fprintf (f, " a%d(r%d,", i, regno);
1083 if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
1084 fprintf (f, "b%d", bb->index);
1085 else
1086 fprintf (f, "l%d", ALLOCNO_LOOP_TREE_NODE (a)->loop->num);
1087 fprintf (f, ") costs:");
1088 for (k = 0; k < cost_classes_num; k++)
1090 rclass = cost_classes[k];
1091 if (contains_reg_of_mode[rclass][PSEUDO_REGNO_MODE (regno)]
1092 #ifdef FORBIDDEN_INC_DEC_CLASSES
1093 && (! in_inc_dec[i] || ! forbidden_inc_dec_class[rclass])
1094 #endif
1095 #ifdef CANNOT_CHANGE_MODE_CLASS
1096 && ! invalid_mode_change_p (regno, (enum reg_class) rclass,
1097 PSEUDO_REGNO_MODE (regno))
1098 #endif
1101 fprintf (f, " %s:%d", reg_class_names[rclass],
1102 COSTS (costs, i)->cost[k]);
1103 if (flag_ira_region == IRA_REGION_ALL
1104 || flag_ira_region == IRA_REGION_MIXED)
1105 fprintf (f, ",%d", COSTS (total_allocno_costs, i)->cost[k]);
1108 fprintf (f, " MEM:%i\n", COSTS (costs, i)->mem_cost);
1112 /* Print pseudo costs to file F. */
1113 static void
1114 print_pseudo_costs (FILE *f)
1116 int regno, k;
1117 int rclass;
1119 ira_assert (! allocno_p);
1120 fprintf (f, "\n");
1121 for (regno = max_reg_num () - 1; regno >= FIRST_PSEUDO_REGISTER; regno--)
1123 if (regno_reg_rtx[regno] == NULL_RTX)
1124 continue;
1125 fprintf (f, " r%d costs:", regno);
1126 for (k = 0; k < cost_classes_num; k++)
1128 rclass = cost_classes[k];
1129 if (contains_reg_of_mode[rclass][PSEUDO_REGNO_MODE (regno)]
1130 #ifdef FORBIDDEN_INC_DEC_CLASSES
1131 && (! in_inc_dec[regno] || ! forbidden_inc_dec_class[rclass])
1132 #endif
1133 #ifdef CANNOT_CHANGE_MODE_CLASS
1134 && ! invalid_mode_change_p (regno, (enum reg_class) rclass,
1135 PSEUDO_REGNO_MODE (regno))
1136 #endif
1138 fprintf (f, " %s:%d", reg_class_names[rclass],
1139 COSTS (costs, regno)->cost[k]);
1141 fprintf (f, " MEM:%i\n", COSTS (costs, regno)->mem_cost);
1145 /* Traverse the BB represented by LOOP_TREE_NODE to update the allocno
1146 costs. */
1147 static void
1148 process_bb_for_costs (basic_block bb)
1150 rtx insn;
1152 frequency = REG_FREQ_FROM_BB (bb);
1153 if (frequency == 0)
1154 frequency = 1;
1155 FOR_BB_INSNS (bb, insn)
1156 insn = scan_one_insn (insn);
1159 /* Traverse the BB represented by LOOP_TREE_NODE to update the allocno
1160 costs. */
1161 static void
1162 process_bb_node_for_costs (ira_loop_tree_node_t loop_tree_node)
1164 basic_block bb;
1166 bb = loop_tree_node->bb;
1167 if (bb != NULL)
1168 process_bb_for_costs (bb);
1171 /* Find costs of register classes and memory for allocnos or pseudos
1172 and their best costs. Set up preferred, alternative and cover
1173 classes for pseudos. */
1174 static void
1175 find_costs_and_classes (FILE *dump_file)
1177 int i, k, start;
1178 int pass;
1179 basic_block bb;
1181 init_recog ();
1182 #ifdef FORBIDDEN_INC_DEC_CLASSES
1183 in_inc_dec = ira_allocate (sizeof (bool) * cost_elements_num);
1184 #endif /* FORBIDDEN_INC_DEC_CLASSES */
1185 pref = NULL;
1186 start = 0;
1187 if (!resize_reg_info () && allocno_p && pseudo_classes_defined_p)
1189 ira_allocno_t a;
1190 ira_allocno_iterator ai;
1192 pref = pref_buffer;
1193 FOR_EACH_ALLOCNO (a, ai)
1194 pref[ALLOCNO_NUM (a)] = reg_preferred_class (ALLOCNO_REGNO (a));
1195 if (flag_expensive_optimizations)
1196 start = 1;
1198 if (allocno_p)
1199 /* Clear the flag for the next compiled function. */
1200 pseudo_classes_defined_p = false;
1201 /* Normally we scan the insns once and determine the best class to
1202 use for each allocno. However, if -fexpensive-optimizations are
1203 on, we do so twice, the second time using the tentative best
1204 classes to guide the selection. */
1205 for (pass = start; pass <= flag_expensive_optimizations; pass++)
1207 if ((!allocno_p || internal_flag_ira_verbose > 0) && dump_file)
1208 fprintf (dump_file,
1209 "\nPass %i for finding pseudo/allocno costs\n\n", pass);
1210 /* We could use only cover classes. Unfortunately it does not
1211 work well for some targets where some subclass of cover class
1212 is costly and wrong cover class is chosen. */
1213 for (i = 0; i < N_REG_CLASSES; i++)
1214 cost_class_nums[i] = -1;
1215 for (cost_classes_num = 0;
1216 cost_classes_num < ira_important_classes_num;
1217 cost_classes_num++)
1219 cost_classes[cost_classes_num]
1220 = ira_important_classes[cost_classes_num];
1221 cost_class_nums[cost_classes[cost_classes_num]]
1222 = cost_classes_num;
1224 struct_costs_size
1225 = sizeof (struct costs) + sizeof (int) * (cost_classes_num - 1);
1226 /* Zero out our accumulation of the cost of each class for each
1227 allocno. */
1228 memset (costs, 0, cost_elements_num * struct_costs_size);
1229 #ifdef FORBIDDEN_INC_DEC_CLASSES
1230 memset (in_inc_dec, 0, cost_elements_num * sizeof (bool));
1231 #endif
1233 if (allocno_p)
1235 /* Scan the instructions and record each time it would save code
1236 to put a certain allocno in a certain class. */
1237 ira_traverse_loop_tree (true, ira_loop_tree_root,
1238 process_bb_node_for_costs, NULL);
1240 memcpy (total_allocno_costs, costs,
1241 max_struct_costs_size * ira_allocnos_num);
1243 else
1245 basic_block bb;
1247 FOR_EACH_BB (bb)
1248 process_bb_for_costs (bb);
1251 if (pass == 0)
1252 pref = pref_buffer;
1254 /* Now for each allocno look at how desirable each class is and
1255 find which class is preferred. */
1256 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
1258 ira_allocno_t a, parent_a;
1259 int rclass, a_num, parent_a_num;
1260 ira_loop_tree_node_t parent;
1261 int best_cost, allocno_cost;
1262 enum reg_class best, alt_class;
1263 #ifdef FORBIDDEN_INC_DEC_CLASSES
1264 int inc_dec_p = false;
1265 #endif
1266 int equiv_savings = regno_equiv_gains[i];
1268 if (! allocno_p)
1270 if (regno_reg_rtx[i] == NULL_RTX)
1271 continue;
1272 #ifdef FORBIDDEN_INC_DEC_CLASSES
1273 inc_dec_p = in_inc_dec[i];
1274 #endif
1275 memcpy (temp_costs, COSTS (costs, i), struct_costs_size);
1277 else
1279 if (ira_regno_allocno_map[i] == NULL)
1280 continue;
1281 memset (temp_costs, 0, struct_costs_size);
1282 /* Find cost of all allocnos with the same regno. */
1283 for (a = ira_regno_allocno_map[i];
1284 a != NULL;
1285 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
1287 a_num = ALLOCNO_NUM (a);
1288 if ((flag_ira_region == IRA_REGION_ALL
1289 || flag_ira_region == IRA_REGION_MIXED)
1290 && (parent = ALLOCNO_LOOP_TREE_NODE (a)->parent) != NULL
1291 && (parent_a = parent->regno_allocno_map[i]) != NULL
1292 /* There are no caps yet. */
1293 && bitmap_bit_p (ALLOCNO_LOOP_TREE_NODE
1294 (a)->border_allocnos,
1295 ALLOCNO_NUM (a)))
1297 /* Propagate costs to upper levels in the region
1298 tree. */
1299 parent_a_num = ALLOCNO_NUM (parent_a);
1300 for (k = 0; k < cost_classes_num; k++)
1301 COSTS (total_allocno_costs, parent_a_num)->cost[k]
1302 += COSTS (total_allocno_costs, a_num)->cost[k];
1303 COSTS (total_allocno_costs, parent_a_num)->mem_cost
1304 += COSTS (total_allocno_costs, a_num)->mem_cost;
1306 for (k = 0; k < cost_classes_num; k++)
1307 temp_costs->cost[k] += COSTS (costs, a_num)->cost[k];
1308 temp_costs->mem_cost += COSTS (costs, a_num)->mem_cost;
1309 #ifdef FORBIDDEN_INC_DEC_CLASSES
1310 if (in_inc_dec[a_num])
1311 inc_dec_p = true;
1312 #endif
1315 if (equiv_savings < 0)
1316 temp_costs->mem_cost = -equiv_savings;
1317 else if (equiv_savings > 0)
1319 temp_costs->mem_cost = 0;
1320 for (k = 0; k < cost_classes_num; k++)
1321 temp_costs->cost[k] += equiv_savings;
1324 best_cost = (1 << (HOST_BITS_PER_INT - 2)) - 1;
1325 best = ALL_REGS;
1326 alt_class = NO_REGS;
1327 /* Find best common class for all allocnos with the same
1328 regno. */
1329 for (k = 0; k < cost_classes_num; k++)
1331 rclass = cost_classes[k];
1332 /* Ignore classes that are too small for this operand or
1333 invalid for an operand that was auto-incremented. */
1334 if (! contains_reg_of_mode[rclass][PSEUDO_REGNO_MODE (i)]
1335 #ifdef FORBIDDEN_INC_DEC_CLASSES
1336 || (inc_dec_p && forbidden_inc_dec_class[rclass])
1337 #endif
1338 #ifdef CANNOT_CHANGE_MODE_CLASS
1339 || invalid_mode_change_p (i, (enum reg_class) rclass,
1340 PSEUDO_REGNO_MODE (i))
1341 #endif
1343 continue;
1344 if (temp_costs->cost[k] < best_cost)
1346 best_cost = temp_costs->cost[k];
1347 best = (enum reg_class) rclass;
1349 else if (temp_costs->cost[k] == best_cost)
1350 best = ira_reg_class_union[best][rclass];
1351 if (pass == flag_expensive_optimizations
1352 && temp_costs->cost[k] < temp_costs->mem_cost
1353 && (reg_class_size[reg_class_subunion[alt_class][rclass]]
1354 > reg_class_size[alt_class]))
1355 alt_class = reg_class_subunion[alt_class][rclass];
1357 alt_class = ira_class_translate[alt_class];
1358 if (best_cost > temp_costs->mem_cost)
1359 regno_cover_class[i] = NO_REGS;
1360 else if (flag_ira_algorithm == IRA_ALGORITHM_PRIORITY)
1361 /* Make the common class the biggest class of best and
1362 alt_class. */
1363 regno_cover_class[i] = alt_class == NO_REGS ? best : alt_class;
1364 else
1365 /* Make the common class a cover class. Remember all
1366 allocnos with the same regno should have the same cover
1367 class. */
1368 regno_cover_class[i] = ira_class_translate[best];
1369 if (pass == flag_expensive_optimizations)
1371 if (best_cost > temp_costs->mem_cost)
1372 best = alt_class = NO_REGS;
1373 else if (best == alt_class)
1374 alt_class = NO_REGS;
1375 setup_reg_classes (i, best, alt_class, regno_cover_class[i]);
1376 if ((!allocno_p || internal_flag_ira_verbose > 2)
1377 && dump_file != NULL)
1378 fprintf (dump_file,
1379 " r%d: preferred %s, alternative %s, cover %s\n",
1380 i, reg_class_names[best], reg_class_names[alt_class],
1381 reg_class_names[regno_cover_class[i]]);
1383 if (! allocno_p)
1385 pref[i] = best_cost > temp_costs->mem_cost ? NO_REGS : best;
1386 continue;
1388 for (a = ira_regno_allocno_map[i];
1389 a != NULL;
1390 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
1392 a_num = ALLOCNO_NUM (a);
1393 if (regno_cover_class[i] == NO_REGS)
1394 best = NO_REGS;
1395 else
1397 /* Finding best class which is subset of the common
1398 class. */
1399 best_cost = (1 << (HOST_BITS_PER_INT - 2)) - 1;
1400 allocno_cost = best_cost;
1401 best = ALL_REGS;
1402 for (k = 0; k < cost_classes_num; k++)
1404 rclass = cost_classes[k];
1405 if (! ira_class_subset_p[rclass][regno_cover_class[i]])
1406 continue;
1407 /* Ignore classes that are too small for this
1408 operand or invalid for an operand that was
1409 auto-incremented. */
1410 if (! contains_reg_of_mode[rclass][PSEUDO_REGNO_MODE (i)]
1411 #ifdef FORBIDDEN_INC_DEC_CLASSES
1412 || (inc_dec_p && forbidden_inc_dec_class[rclass])
1413 #endif
1414 #ifdef CANNOT_CHANGE_MODE_CLASS
1415 || invalid_mode_change_p (i, (enum reg_class) rclass,
1416 PSEUDO_REGNO_MODE (i))
1417 #endif
1420 else if (COSTS (total_allocno_costs, a_num)->cost[k]
1421 < best_cost)
1423 best_cost
1424 = COSTS (total_allocno_costs, a_num)->cost[k];
1425 allocno_cost = COSTS (costs, a_num)->cost[k];
1426 best = (enum reg_class) rclass;
1428 else if (COSTS (total_allocno_costs, a_num)->cost[k]
1429 == best_cost)
1431 best = ira_reg_class_union[best][rclass];
1432 allocno_cost
1433 = MAX (allocno_cost, COSTS (costs, a_num)->cost[k]);
1436 ALLOCNO_COVER_CLASS_COST (a) = allocno_cost;
1438 ira_assert (flag_ira_algorithm == IRA_ALGORITHM_PRIORITY
1439 || ira_class_translate[best] == regno_cover_class[i]);
1440 if (internal_flag_ira_verbose > 2 && dump_file != NULL
1441 && (pass == 0 || pref[a_num] != best))
1443 fprintf (dump_file, " a%d (r%d,", a_num, i);
1444 if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
1445 fprintf (dump_file, "b%d", bb->index);
1446 else
1447 fprintf (dump_file, "l%d",
1448 ALLOCNO_LOOP_TREE_NODE (a)->loop->num);
1449 fprintf (dump_file, ") best %s, cover %s\n",
1450 reg_class_names[best],
1451 reg_class_names[regno_cover_class[i]]);
1453 pref[a_num] = best;
1457 if (internal_flag_ira_verbose > 4 && dump_file)
1459 if (allocno_p)
1460 print_allocno_costs (dump_file);
1461 else
1462 print_pseudo_costs (dump_file);
1463 fprintf (dump_file,"\n");
1466 #ifdef FORBIDDEN_INC_DEC_CLASSES
1467 ira_free (in_inc_dec);
1468 #endif
1473 /* Process moves involving hard regs to modify allocno hard register
1474 costs. We can do this only after determining allocno cover class.
1475 If a hard register forms a register class, than moves with the hard
1476 register are already taken into account in class costs for the
1477 allocno. */
1478 static void
1479 process_bb_node_for_hard_reg_moves (ira_loop_tree_node_t loop_tree_node)
1481 int i, freq, cost, src_regno, dst_regno, hard_regno;
1482 bool to_p;
1483 ira_allocno_t a;
1484 enum reg_class rclass, hard_reg_class;
1485 enum machine_mode mode;
1486 basic_block bb;
1487 rtx insn, set, src, dst;
1489 bb = loop_tree_node->bb;
1490 if (bb == NULL)
1491 return;
1492 freq = REG_FREQ_FROM_BB (bb);
1493 if (freq == 0)
1494 freq = 1;
1495 FOR_BB_INSNS (bb, insn)
1497 if (!NONDEBUG_INSN_P (insn))
1498 continue;
1499 set = single_set (insn);
1500 if (set == NULL_RTX)
1501 continue;
1502 dst = SET_DEST (set);
1503 src = SET_SRC (set);
1504 if (! REG_P (dst) || ! REG_P (src))
1505 continue;
1506 dst_regno = REGNO (dst);
1507 src_regno = REGNO (src);
1508 if (dst_regno >= FIRST_PSEUDO_REGISTER
1509 && src_regno < FIRST_PSEUDO_REGISTER)
1511 hard_regno = src_regno;
1512 to_p = true;
1513 a = ira_curr_regno_allocno_map[dst_regno];
1515 else if (src_regno >= FIRST_PSEUDO_REGISTER
1516 && dst_regno < FIRST_PSEUDO_REGISTER)
1518 hard_regno = dst_regno;
1519 to_p = false;
1520 a = ira_curr_regno_allocno_map[src_regno];
1522 else
1523 continue;
1524 rclass = ALLOCNO_COVER_CLASS (a);
1525 if (! TEST_HARD_REG_BIT (reg_class_contents[rclass], hard_regno))
1526 continue;
1527 i = ira_class_hard_reg_index[rclass][hard_regno];
1528 if (i < 0)
1529 continue;
1530 mode = ALLOCNO_MODE (a);
1531 hard_reg_class = REGNO_REG_CLASS (hard_regno);
1532 cost
1533 = (to_p ? ira_get_register_move_cost (mode, hard_reg_class, rclass)
1534 : ira_get_register_move_cost (mode, rclass, hard_reg_class)) * freq;
1535 ira_allocate_and_set_costs (&ALLOCNO_HARD_REG_COSTS (a), rclass,
1536 ALLOCNO_COVER_CLASS_COST (a));
1537 ira_allocate_and_set_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a),
1538 rclass, 0);
1539 ALLOCNO_HARD_REG_COSTS (a)[i] -= cost;
1540 ALLOCNO_CONFLICT_HARD_REG_COSTS (a)[i] -= cost;
1541 ALLOCNO_COVER_CLASS_COST (a) = MIN (ALLOCNO_COVER_CLASS_COST (a),
1542 ALLOCNO_HARD_REG_COSTS (a)[i]);
1546 /* After we find hard register and memory costs for allocnos, define
1547 its cover class and modify hard register cost because insns moving
1548 allocno to/from hard registers. */
1549 static void
1550 setup_allocno_cover_class_and_costs (void)
1552 int i, j, n, regno, num;
1553 int *reg_costs;
1554 enum reg_class cover_class, rclass;
1555 ira_allocno_t a;
1556 ira_allocno_iterator ai;
1558 ira_assert (allocno_p);
1559 FOR_EACH_ALLOCNO (a, ai)
1561 i = ALLOCNO_NUM (a);
1562 cover_class = regno_cover_class[ALLOCNO_REGNO (a)];
1563 ira_assert (pref[i] == NO_REGS || cover_class != NO_REGS);
1564 ALLOCNO_MEMORY_COST (a) = COSTS (costs, i)->mem_cost;
1565 ira_set_allocno_cover_class (a, cover_class);
1566 if (cover_class == NO_REGS)
1567 continue;
1568 ALLOCNO_AVAILABLE_REGS_NUM (a) = ira_available_class_regs[cover_class];
1569 if (optimize && ALLOCNO_COVER_CLASS (a) != pref[i])
1571 n = ira_class_hard_regs_num[cover_class];
1572 ALLOCNO_HARD_REG_COSTS (a)
1573 = reg_costs = ira_allocate_cost_vector (cover_class);
1574 for (j = n - 1; j >= 0; j--)
1576 regno = ira_class_hard_regs[cover_class][j];
1577 if (TEST_HARD_REG_BIT (reg_class_contents[pref[i]], regno))
1578 reg_costs[j] = ALLOCNO_COVER_CLASS_COST (a);
1579 else
1581 rclass = REGNO_REG_CLASS (regno);
1582 num = cost_class_nums[rclass];
1583 if (num < 0)
1585 /* The hard register class is not a cover class or a
1586 class not fully inside in a cover class -- use
1587 the allocno cover class. */
1588 ira_assert (ira_hard_regno_cover_class[regno]
1589 == cover_class);
1590 num = cost_class_nums[cover_class];
1592 reg_costs[j] = COSTS (costs, i)->cost[num];
1597 if (optimize)
1598 ira_traverse_loop_tree (true, ira_loop_tree_root,
1599 process_bb_node_for_hard_reg_moves, NULL);
1604 /* Function called once during compiler work. */
1605 void
1606 ira_init_costs_once (void)
1608 int i;
1610 init_cost = NULL;
1611 for (i = 0; i < MAX_RECOG_OPERANDS; i++)
1613 op_costs[i] = NULL;
1614 this_op_costs[i] = NULL;
1616 temp_costs = NULL;
1617 cost_classes = NULL;
1620 /* Free allocated temporary cost vectors. */
1621 static void
1622 free_ira_costs (void)
1624 int i;
1626 if (init_cost != NULL)
1627 free (init_cost);
1628 init_cost = NULL;
1629 for (i = 0; i < MAX_RECOG_OPERANDS; i++)
1631 if (op_costs[i] != NULL)
1632 free (op_costs[i]);
1633 if (this_op_costs[i] != NULL)
1634 free (this_op_costs[i]);
1635 op_costs[i] = this_op_costs[i] = NULL;
1637 if (temp_costs != NULL)
1638 free (temp_costs);
1639 temp_costs = NULL;
1640 if (cost_classes != NULL)
1641 free (cost_classes);
1642 cost_classes = NULL;
1645 /* This is called each time register related information is
1646 changed. */
1647 void
1648 ira_init_costs (void)
1650 int i;
1652 free_ira_costs ();
1653 max_struct_costs_size
1654 = sizeof (struct costs) + sizeof (int) * (ira_important_classes_num - 1);
1655 /* Don't use ira_allocate because vectors live through several IRA calls. */
1656 init_cost = (struct costs *) xmalloc (max_struct_costs_size);
1657 init_cost->mem_cost = 1000000;
1658 for (i = 0; i < ira_important_classes_num; i++)
1659 init_cost->cost[i] = 1000000;
1660 for (i = 0; i < MAX_RECOG_OPERANDS; i++)
1662 op_costs[i] = (struct costs *) xmalloc (max_struct_costs_size);
1663 this_op_costs[i] = (struct costs *) xmalloc (max_struct_costs_size);
1665 temp_costs = (struct costs *) xmalloc (max_struct_costs_size);
1666 cost_classes = (enum reg_class *) xmalloc (sizeof (enum reg_class)
1667 * ira_important_classes_num);
1670 /* Function called once at the end of compiler work. */
1671 void
1672 ira_finish_costs_once (void)
1674 free_ira_costs ();
1679 /* Common initialization function for ira_costs and
1680 ira_set_pseudo_classes. */
1681 static void
1682 init_costs (void)
1684 init_subregs_of_mode ();
1685 costs = (struct costs *) ira_allocate (max_struct_costs_size
1686 * cost_elements_num);
1687 pref_buffer
1688 = (enum reg_class *) ira_allocate (sizeof (enum reg_class)
1689 * cost_elements_num);
1690 regno_cover_class
1691 = (enum reg_class *) ira_allocate (sizeof (enum reg_class)
1692 * max_reg_num ());
1693 regno_equiv_gains = (int *) ira_allocate (sizeof (int) * max_reg_num ());
1694 memset (regno_equiv_gains, 0, sizeof (int) * max_reg_num ());
1697 /* Common finalization function for ira_costs and
1698 ira_set_pseudo_classes. */
1699 static void
1700 finish_costs (void)
1702 ira_free (regno_equiv_gains);
1703 ira_free (regno_cover_class);
1704 ira_free (pref_buffer);
1705 ira_free (costs);
1708 /* Entry function which defines cover class, memory and hard register
1709 costs for each allocno. */
1710 void
1711 ira_costs (void)
1713 allocno_p = true;
1714 cost_elements_num = ira_allocnos_num;
1715 init_costs ();
1716 total_allocno_costs = (struct costs *) ira_allocate (max_struct_costs_size
1717 * ira_allocnos_num);
1718 calculate_elim_costs_all_insns ();
1719 find_costs_and_classes (ira_dump_file);
1720 setup_allocno_cover_class_and_costs ();
1721 finish_costs ();
1722 ira_free (total_allocno_costs);
1725 /* Entry function which defines classes for pseudos. */
1726 void
1727 ira_set_pseudo_classes (FILE *dump_file)
1729 allocno_p = false;
1730 internal_flag_ira_verbose = flag_ira_verbose;
1731 cost_elements_num = max_reg_num ();
1732 init_costs ();
1733 find_costs_and_classes (dump_file);
1734 pseudo_classes_defined_p = true;
1735 finish_costs ();
1740 /* Change hard register costs for allocnos which lives through
1741 function calls. This is called only when we found all intersected
1742 calls during building allocno live ranges. */
1743 void
1744 ira_tune_allocno_costs_and_cover_classes (void)
1746 int j, n, regno;
1747 int cost, min_cost, *reg_costs;
1748 enum reg_class cover_class, rclass;
1749 enum machine_mode mode;
1750 ira_allocno_t a;
1751 ira_allocno_iterator ai;
1753 FOR_EACH_ALLOCNO (a, ai)
1755 cover_class = ALLOCNO_COVER_CLASS (a);
1756 if (cover_class == NO_REGS)
1757 continue;
1758 mode = ALLOCNO_MODE (a);
1759 n = ira_class_hard_regs_num[cover_class];
1760 min_cost = INT_MAX;
1761 if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
1763 ira_allocate_and_set_costs
1764 (&ALLOCNO_HARD_REG_COSTS (a), cover_class,
1765 ALLOCNO_COVER_CLASS_COST (a));
1766 reg_costs = ALLOCNO_HARD_REG_COSTS (a);
1767 for (j = n - 1; j >= 0; j--)
1769 regno = ira_class_hard_regs[cover_class][j];
1770 rclass = REGNO_REG_CLASS (regno);
1771 cost = 0;
1772 if (! ira_hard_reg_not_in_set_p (regno, mode, call_used_reg_set)
1773 || HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
1774 cost += (ALLOCNO_CALL_FREQ (a)
1775 * (ira_memory_move_cost[mode][rclass][0]
1776 + ira_memory_move_cost[mode][rclass][1]));
1777 #ifdef IRA_HARD_REGNO_ADD_COST_MULTIPLIER
1778 cost += ((ira_memory_move_cost[mode][rclass][0]
1779 + ira_memory_move_cost[mode][rclass][1])
1780 * ALLOCNO_FREQ (a)
1781 * IRA_HARD_REGNO_ADD_COST_MULTIPLIER (regno) / 2);
1782 #endif
1783 reg_costs[j] += cost;
1784 if (min_cost > reg_costs[j])
1785 min_cost = reg_costs[j];
1788 if (min_cost != INT_MAX)
1789 ALLOCNO_COVER_CLASS_COST (a) = min_cost;
1791 /* Some targets allow pseudos to be allocated to unaligned sequences
1792 of hard registers. However, selecting an unaligned sequence can
1793 unnecessarily restrict later allocations. So increase the cost of
1794 unaligned hard regs to encourage the use of aligned hard regs. */
1796 const int nregs = ira_reg_class_nregs[cover_class][ALLOCNO_MODE (a)];
1798 if (nregs > 1)
1800 ira_allocate_and_set_costs
1801 (&ALLOCNO_HARD_REG_COSTS (a), cover_class,
1802 ALLOCNO_COVER_CLASS_COST (a));
1803 reg_costs = ALLOCNO_HARD_REG_COSTS (a);
1804 for (j = n - 1; j >= 0; j--)
1806 regno = ira_non_ordered_class_hard_regs[cover_class][j];
1807 if ((regno % nregs) != 0)
1809 int index = ira_class_hard_reg_index[cover_class][regno];
1810 ira_assert (index != -1);
1811 reg_costs[index] += ALLOCNO_FREQ (a);
1819 /* Add COST to the estimated gain for eliminating REGNO with its
1820 equivalence. If COST is zero, record that no such elimination is
1821 possible. */
1823 void
1824 ira_adjust_equiv_reg_cost (unsigned regno, int cost)
1826 if (cost == 0)
1827 regno_equiv_gains[regno] = 0;
1828 else
1829 regno_equiv_gains[regno] += cost;