Multiple exit loop handling in ivopts. Regression tested on x86-64/linux
[official-gcc.git] / gcc / ira-costs.c
blob24e8393dd0bd08dbe863aedbbc9dacb576011152
1 /* IRA hard register and memory cost calculation for allocnos or pseudos.
2 Copyright (C) 2006, 2007, 2008, 2009
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 "toplev.h"
39 #include "target.h"
40 #include "params.h"
41 #include "ira-int.h"
43 /* The flags is set up every time when we calculate pseudo register
44 classes through function ira_set_pseudo_classes. */
45 static bool pseudo_classes_defined_p = false;
47 /* TRUE if we work with allocnos. Otherwise we work with pseudos. */
48 static bool allocno_p;
50 /* Number of elements in arrays `in_inc_dec' and `costs'. */
51 static int cost_elements_num;
53 #ifdef FORBIDDEN_INC_DEC_CLASSES
54 /* Indexed by n, is TRUE if allocno or pseudo with number N is used in
55 an auto-inc or auto-dec context. */
56 static bool *in_inc_dec;
57 #endif
59 /* The `costs' struct records the cost of using hard registers of each
60 class considered for the calculation and of using memory for each
61 allocno or pseudo. */
62 struct costs
64 int mem_cost;
65 /* Costs for register classes start here. We process only some
66 register classes (cover classes on the 1st cost calculation
67 iteration and important classes on the 2nd iteration). */
68 int cost[1];
71 #define max_struct_costs_size \
72 (this_target_ira_int->x_max_struct_costs_size)
73 #define init_cost \
74 (this_target_ira_int->x_init_cost)
75 #define temp_costs \
76 (this_target_ira_int->x_temp_costs)
77 #define op_costs \
78 (this_target_ira_int->x_op_costs)
79 #define this_op_costs \
80 (this_target_ira_int->x_this_op_costs)
81 #define cost_classes \
82 (this_target_ira_int->x_cost_classes)
84 /* Costs of each class for each allocno or pseudo. */
85 static struct costs *costs;
87 /* Accumulated costs of each class for each allocno. */
88 static struct costs *total_allocno_costs;
90 /* The size of the previous array. */
91 static int cost_classes_num;
93 /* Map: cost class -> order number (they start with 0) of the cost
94 class. The order number is negative for non-cost classes. */
95 static int cost_class_nums[N_REG_CLASSES];
97 /* It is the current size of struct costs. */
98 static int struct_costs_size;
100 /* Return pointer to structure containing costs of allocno or pseudo
101 with given NUM in array ARR. */
102 #define COSTS(arr, num) \
103 ((struct costs *) ((char *) (arr) + (num) * struct_costs_size))
105 /* Return index in COSTS when processing reg with REGNO. */
106 #define COST_INDEX(regno) (allocno_p \
107 ? ALLOCNO_NUM (ira_curr_regno_allocno_map[regno]) \
108 : (int) regno)
110 /* Record register class preferences of each allocno or pseudo. Null
111 value means no preferences. It happens on the 1st iteration of the
112 cost calculation. */
113 static enum reg_class *pref;
115 /* Allocated buffers for pref. */
116 static enum reg_class *pref_buffer;
118 /* Record cover register class of each allocno with the same regno. */
119 static enum reg_class *regno_cover_class;
121 /* Record cost gains for not allocating a register with an invariant
122 equivalence. */
123 static int *regno_equiv_gains;
125 /* Execution frequency of the current insn. */
126 static int frequency;
130 /* Compute the cost of loading X into (if TO_P is TRUE) or from (if
131 TO_P is FALSE) a register of class RCLASS in mode MODE. X must not
132 be a pseudo register. */
133 static int
134 copy_cost (rtx x, enum machine_mode mode, enum reg_class rclass, bool to_p,
135 secondary_reload_info *prev_sri)
137 secondary_reload_info sri;
138 enum reg_class secondary_class = NO_REGS;
140 /* If X is a SCRATCH, there is actually nothing to move since we are
141 assuming optimal allocation. */
142 if (GET_CODE (x) == SCRATCH)
143 return 0;
145 /* Get the class we will actually use for a reload. */
146 rclass = PREFERRED_RELOAD_CLASS (x, rclass);
148 /* If we need a secondary reload for an intermediate, the cost is
149 that to load the input into the intermediate register, then to
150 copy it. */
151 sri.prev_sri = prev_sri;
152 sri.extra_cost = 0;
153 secondary_class
154 = (enum reg_class) targetm.secondary_reload (to_p, x, rclass, mode, &sri);
156 if (secondary_class != NO_REGS)
158 if (!move_cost[mode])
159 init_move_cost (mode);
160 return (move_cost[mode][secondary_class][rclass] + sri.extra_cost
161 + copy_cost (x, mode, secondary_class, to_p, &sri));
164 /* For memory, use the memory move cost, for (hard) registers, use
165 the cost to move between the register classes, and use 2 for
166 everything else (constants). */
167 if (MEM_P (x) || rclass == NO_REGS)
168 return sri.extra_cost + ira_memory_move_cost[mode][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 + move_cost[mode][REGNO_REG_CLASS (REGNO (x))][rclass]);
175 else
176 /* If this is a constant, we may eventually want to call rtx_cost
177 here. */
178 return sri.extra_cost + COSTS_N_INSNS (1);
183 /* Record the cost of using memory or hard registers of various
184 classes for the operands in INSN.
186 N_ALTS is the number of alternatives.
187 N_OPS is the number of operands.
188 OPS is an array of the operands.
189 MODES are the modes of the operands, in case any are VOIDmode.
190 CONSTRAINTS are the constraints to use for the operands. This array
191 is modified by this procedure.
193 This procedure works alternative by alternative. For each
194 alternative we assume that we will be able to allocate all allocnos
195 to their ideal register class and calculate the cost of using that
196 alternative. Then we compute, for each operand that is a
197 pseudo-register, the cost of having the allocno allocated to each
198 register class and using it in that alternative. To this cost is
199 added the cost of the alternative.
201 The cost of each class for this insn is its lowest cost among all
202 the alternatives. */
203 static void
204 record_reg_classes (int n_alts, int n_ops, rtx *ops,
205 enum machine_mode *modes, const char **constraints,
206 rtx insn, enum reg_class *pref)
208 int alt;
209 int i, j, k;
210 rtx set;
211 int insn_allows_mem[MAX_RECOG_OPERANDS];
213 for (i = 0; i < n_ops; i++)
214 insn_allows_mem[i] = 0;
216 /* Process each alternative, each time minimizing an operand's cost
217 with the cost for each operand in that alternative. */
218 for (alt = 0; alt < n_alts; alt++)
220 enum reg_class classes[MAX_RECOG_OPERANDS];
221 int allows_mem[MAX_RECOG_OPERANDS];
222 enum reg_class rclass;
223 int alt_fail = 0;
224 int alt_cost = 0, op_cost_add;
226 if (!recog_data.alternative_enabled_p[alt])
228 for (i = 0; i < recog_data.n_operands; i++)
229 constraints[i] = skip_alternative (constraints[i]);
231 continue;
234 for (i = 0; i < n_ops; i++)
236 unsigned char c;
237 const char *p = constraints[i];
238 rtx op = ops[i];
239 enum machine_mode mode = modes[i];
240 int allows_addr = 0;
241 int win = 0;
243 /* Initially show we know nothing about the register class. */
244 classes[i] = NO_REGS;
245 allows_mem[i] = 0;
247 /* If this operand has no constraints at all, we can
248 conclude nothing about it since anything is valid. */
249 if (*p == 0)
251 if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
252 memset (this_op_costs[i], 0, struct_costs_size);
253 continue;
256 /* If this alternative is only relevant when this operand
257 matches a previous operand, we do different things
258 depending on whether this operand is a allocno-reg or not.
259 We must process any modifiers for the operand before we
260 can make this test. */
261 while (*p == '%' || *p == '=' || *p == '+' || *p == '&')
262 p++;
264 if (p[0] >= '0' && p[0] <= '0' + i && (p[1] == ',' || p[1] == 0))
266 /* Copy class and whether memory is allowed from the
267 matching alternative. Then perform any needed cost
268 computations and/or adjustments. */
269 j = p[0] - '0';
270 classes[i] = classes[j];
271 allows_mem[i] = allows_mem[j];
272 if (allows_mem[i])
273 insn_allows_mem[i] = 1;
275 if (! REG_P (op) || REGNO (op) < FIRST_PSEUDO_REGISTER)
277 /* If this matches the other operand, we have no
278 added cost and we win. */
279 if (rtx_equal_p (ops[j], op))
280 win = 1;
281 /* If we can put the other operand into a register,
282 add to the cost of this alternative the cost to
283 copy this operand to the register used for the
284 other operand. */
285 else if (classes[j] != NO_REGS)
287 alt_cost += copy_cost (op, mode, classes[j], 1, NULL);
288 win = 1;
291 else if (! REG_P (ops[j])
292 || REGNO (ops[j]) < FIRST_PSEUDO_REGISTER)
294 /* This op is an allocno but the one it matches is
295 not. */
297 /* If we can't put the other operand into a
298 register, this alternative can't be used. */
300 if (classes[j] == NO_REGS)
301 alt_fail = 1;
302 /* Otherwise, add to the cost of this alternative
303 the cost to copy the other operand to the hard
304 register used for this operand. */
305 else
306 alt_cost += copy_cost (ops[j], mode, classes[j], 1, NULL);
308 else
310 /* The costs of this operand are not the same as the
311 other operand since move costs are not symmetric.
312 Moreover, if we cannot tie them, this alternative
313 needs to do a copy, which is one insn. */
314 struct costs *pp = this_op_costs[i];
316 for (k = 0; k < cost_classes_num; k++)
318 rclass = cost_classes[k];
319 pp->cost[k]
320 = (((recog_data.operand_type[i] != OP_OUT
321 ? ira_get_may_move_cost (mode, rclass,
322 classes[i], true) : 0)
323 + (recog_data.operand_type[i] != OP_IN
324 ? ira_get_may_move_cost (mode, classes[i],
325 rclass, false) : 0))
326 * frequency);
329 /* If the alternative actually allows memory, make
330 things a bit cheaper since we won't need an extra
331 insn to load it. */
332 pp->mem_cost
333 = ((recog_data.operand_type[i] != OP_IN
334 ? ira_memory_move_cost[mode][classes[i]][0] : 0)
335 + (recog_data.operand_type[i] != OP_OUT
336 ? ira_memory_move_cost[mode][classes[i]][1] : 0)
337 - allows_mem[i]) * frequency;
339 /* If we have assigned a class to this allocno in our
340 first pass, add a cost to this alternative
341 corresponding to what we would add if this allocno
342 were not in the appropriate class. We could use
343 cover class here but it is less accurate
344 approximation. */
345 if (pref)
347 enum reg_class pref_class = pref[COST_INDEX (REGNO (op))];
349 if (pref_class == NO_REGS)
350 alt_cost
351 += ((recog_data.operand_type[i] != OP_IN
352 ? ira_memory_move_cost[mode][classes[i]][0]
353 : 0)
354 + (recog_data.operand_type[i] != OP_OUT
355 ? ira_memory_move_cost[mode][classes[i]][1]
356 : 0));
357 else if (ira_reg_class_intersect
358 [pref_class][classes[i]] == NO_REGS)
359 alt_cost += ira_get_register_move_cost (mode,
360 pref_class,
361 classes[i]);
363 if (REGNO (ops[i]) != REGNO (ops[j])
364 && ! find_reg_note (insn, REG_DEAD, op))
365 alt_cost += 2;
367 /* This is in place of ordinary cost computation for
368 this operand, so skip to the end of the
369 alternative (should be just one character). */
370 while (*p && *p++ != ',')
373 constraints[i] = p;
374 continue;
378 /* Scan all the constraint letters. See if the operand
379 matches any of the constraints. Collect the valid
380 register classes and see if this operand accepts
381 memory. */
382 while ((c = *p))
384 switch (c)
386 case ',':
387 break;
388 case '*':
389 /* Ignore the next letter for this pass. */
390 c = *++p;
391 break;
393 case '?':
394 alt_cost += 2;
395 case '!': case '#': case '&':
396 case '0': case '1': case '2': case '3': case '4':
397 case '5': case '6': case '7': case '8': case '9':
398 break;
400 case 'p':
401 allows_addr = 1;
402 win = address_operand (op, GET_MODE (op));
403 /* We know this operand is an address, so we want it
404 to be allocated to a register that can be the
405 base of an address, i.e. BASE_REG_CLASS. */
406 classes[i]
407 = ira_reg_class_union[classes[i]]
408 [base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
409 break;
411 case 'm': case 'o': case 'V':
412 /* It doesn't seem worth distinguishing between
413 offsettable and non-offsettable addresses
414 here. */
415 insn_allows_mem[i] = allows_mem[i] = 1;
416 if (MEM_P (op))
417 win = 1;
418 break;
420 case '<':
421 if (MEM_P (op)
422 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
423 || GET_CODE (XEXP (op, 0)) == POST_DEC))
424 win = 1;
425 break;
427 case '>':
428 if (MEM_P (op)
429 && (GET_CODE (XEXP (op, 0)) == PRE_INC
430 || GET_CODE (XEXP (op, 0)) == POST_INC))
431 win = 1;
432 break;
434 case 'E':
435 case 'F':
436 if (GET_CODE (op) == CONST_DOUBLE
437 || (GET_CODE (op) == CONST_VECTOR
438 && (GET_MODE_CLASS (GET_MODE (op))
439 == MODE_VECTOR_FLOAT)))
440 win = 1;
441 break;
443 case 'G':
444 case 'H':
445 if (GET_CODE (op) == CONST_DOUBLE
446 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
447 win = 1;
448 break;
450 case 's':
451 if (CONST_INT_P (op)
452 || (GET_CODE (op) == CONST_DOUBLE
453 && GET_MODE (op) == VOIDmode))
454 break;
456 case 'i':
457 if (CONSTANT_P (op)
458 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op)))
459 win = 1;
460 break;
462 case 'n':
463 if (CONST_INT_P (op)
464 || (GET_CODE (op) == CONST_DOUBLE
465 && GET_MODE (op) == VOIDmode))
466 win = 1;
467 break;
469 case 'I':
470 case 'J':
471 case 'K':
472 case 'L':
473 case 'M':
474 case 'N':
475 case 'O':
476 case 'P':
477 if (CONST_INT_P (op)
478 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
479 win = 1;
480 break;
482 case 'X':
483 win = 1;
484 break;
486 case 'g':
487 if (MEM_P (op)
488 || (CONSTANT_P (op)
489 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))))
490 win = 1;
491 insn_allows_mem[i] = allows_mem[i] = 1;
492 case 'r':
493 classes[i] = ira_reg_class_union[classes[i]][GENERAL_REGS];
494 break;
496 default:
497 if (REG_CLASS_FROM_CONSTRAINT (c, p) != NO_REGS)
498 classes[i] = ira_reg_class_union[classes[i]]
499 [REG_CLASS_FROM_CONSTRAINT (c, p)];
500 #ifdef EXTRA_CONSTRAINT_STR
501 else if (EXTRA_CONSTRAINT_STR (op, c, p))
502 win = 1;
504 if (EXTRA_MEMORY_CONSTRAINT (c, p))
506 /* Every MEM can be reloaded to fit. */
507 insn_allows_mem[i] = allows_mem[i] = 1;
508 if (MEM_P (op))
509 win = 1;
511 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
513 /* Every address can be reloaded to fit. */
514 allows_addr = 1;
515 if (address_operand (op, GET_MODE (op)))
516 win = 1;
517 /* We know this operand is an address, so we
518 want it to be allocated to a hard register
519 that can be the base of an address,
520 i.e. BASE_REG_CLASS. */
521 classes[i]
522 = ira_reg_class_union[classes[i]]
523 [base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
525 #endif
526 break;
528 p += CONSTRAINT_LEN (c, p);
529 if (c == ',')
530 break;
533 constraints[i] = p;
535 /* How we account for this operand now depends on whether it
536 is a pseudo register or not. If it is, we first check if
537 any register classes are valid. If not, we ignore this
538 alternative, since we want to assume that all allocnos get
539 allocated for register preferencing. If some register
540 class is valid, compute the costs of moving the allocno
541 into that class. */
542 if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
544 if (classes[i] == NO_REGS)
546 /* We must always fail if the operand is a REG, but
547 we did not find a suitable class.
549 Otherwise we may perform an uninitialized read
550 from this_op_costs after the `continue' statement
551 below. */
552 alt_fail = 1;
554 else
556 struct costs *pp = this_op_costs[i];
558 for (k = 0; k < cost_classes_num; k++)
560 rclass = cost_classes[k];
561 pp->cost[k]
562 = (((recog_data.operand_type[i] != OP_OUT
563 ? ira_get_may_move_cost (mode, rclass,
564 classes[i], true) : 0)
565 + (recog_data.operand_type[i] != OP_IN
566 ? ira_get_may_move_cost (mode, classes[i],
567 rclass, false) : 0))
568 * frequency);
571 /* If the alternative actually allows memory, make
572 things a bit cheaper since we won't need an extra
573 insn to load it. */
574 pp->mem_cost
575 = ((recog_data.operand_type[i] != OP_IN
576 ? ira_memory_move_cost[mode][classes[i]][0] : 0)
577 + (recog_data.operand_type[i] != OP_OUT
578 ? ira_memory_move_cost[mode][classes[i]][1] : 0)
579 - allows_mem[i]) * frequency;
580 /* If we have assigned a class to this allocno in our
581 first pass, add a cost to this alternative
582 corresponding to what we would add if this allocno
583 were not in the appropriate class. We could use
584 cover class here but it is less accurate
585 approximation. */
586 if (pref)
588 enum reg_class pref_class = pref[COST_INDEX (REGNO (op))];
590 if (pref_class == NO_REGS)
591 alt_cost
592 += ((recog_data.operand_type[i] != OP_IN
593 ? ira_memory_move_cost[mode][classes[i]][0]
594 : 0)
595 + (recog_data.operand_type[i] != OP_OUT
596 ? ira_memory_move_cost[mode][classes[i]][1]
597 : 0));
598 else if (ira_reg_class_intersect[pref_class][classes[i]]
599 == NO_REGS)
600 alt_cost += ira_get_register_move_cost (mode,
601 pref_class,
602 classes[i]);
607 /* Otherwise, if this alternative wins, either because we
608 have already determined that or if we have a hard
609 register of the proper class, there is no cost for this
610 alternative. */
611 else if (win || (REG_P (op)
612 && reg_fits_class_p (op, classes[i],
613 0, GET_MODE (op))))
616 /* If registers are valid, the cost of this alternative
617 includes copying the object to and/or from a
618 register. */
619 else if (classes[i] != NO_REGS)
621 if (recog_data.operand_type[i] != OP_OUT)
622 alt_cost += copy_cost (op, mode, classes[i], 1, NULL);
624 if (recog_data.operand_type[i] != OP_IN)
625 alt_cost += copy_cost (op, mode, classes[i], 0, NULL);
627 /* The only other way this alternative can be used is if
628 this is a constant that could be placed into memory. */
629 else if (CONSTANT_P (op) && (allows_addr || allows_mem[i]))
630 alt_cost += ira_memory_move_cost[mode][classes[i]][1];
631 else
632 alt_fail = 1;
635 if (alt_fail)
636 continue;
638 op_cost_add = alt_cost * frequency;
639 /* Finally, update the costs with the information we've
640 calculated about this alternative. */
641 for (i = 0; i < n_ops; i++)
642 if (REG_P (ops[i]) && REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
644 struct costs *pp = op_costs[i], *qq = this_op_costs[i];
645 int scale = 1 + (recog_data.operand_type[i] == OP_INOUT);
647 pp->mem_cost = MIN (pp->mem_cost,
648 (qq->mem_cost + op_cost_add) * scale);
650 for (k = 0; k < cost_classes_num; k++)
651 pp->cost[k]
652 = MIN (pp->cost[k], (qq->cost[k] + op_cost_add) * scale);
656 if (allocno_p)
657 for (i = 0; i < n_ops; i++)
659 ira_allocno_t a;
660 rtx op = ops[i];
662 if (! REG_P (op) || REGNO (op) < FIRST_PSEUDO_REGISTER)
663 continue;
664 a = ira_curr_regno_allocno_map [REGNO (op)];
665 if (! ALLOCNO_BAD_SPILL_P (a) && insn_allows_mem[i] == 0)
666 ALLOCNO_BAD_SPILL_P (a) = true;
669 /* If this insn is a single set copying operand 1 to operand 0 and
670 one operand is an allocno with the other a hard reg or an allocno
671 that prefers a hard register that is in its own register class
672 then we may want to adjust the cost of that register class to -1.
674 Avoid the adjustment if the source does not die to avoid
675 stressing of register allocator by preferrencing two colliding
676 registers into single class.
678 Also avoid the adjustment if a copy between hard registers of the
679 class is expensive (ten times the cost of a default copy is
680 considered arbitrarily expensive). This avoids losing when the
681 preferred class is very expensive as the source of a copy
682 instruction. */
683 if ((set = single_set (insn)) != 0
684 && ops[0] == SET_DEST (set) && ops[1] == SET_SRC (set)
685 && REG_P (ops[0]) && REG_P (ops[1])
686 && find_regno_note (insn, REG_DEAD, REGNO (ops[1])))
687 for (i = 0; i <= 1; i++)
688 if (REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
690 unsigned int regno = REGNO (ops[!i]);
691 enum machine_mode mode = GET_MODE (ops[!i]);
692 enum reg_class rclass;
693 unsigned int nr;
695 if (regno < FIRST_PSEUDO_REGISTER)
696 for (k = 0; k < cost_classes_num; k++)
698 rclass = cost_classes[k];
699 if (TEST_HARD_REG_BIT (reg_class_contents[rclass], regno)
700 && (reg_class_size[rclass]
701 == (unsigned) CLASS_MAX_NREGS (rclass, mode)))
703 if (reg_class_size[rclass] == 1)
704 op_costs[i]->cost[k] = -frequency;
705 else
707 for (nr = 0;
708 nr < (unsigned) hard_regno_nregs[regno][mode];
709 nr++)
710 if (! TEST_HARD_REG_BIT (reg_class_contents[rclass],
711 regno + nr))
712 break;
714 if (nr == (unsigned) hard_regno_nregs[regno][mode])
715 op_costs[i]->cost[k] = -frequency;
724 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudo registers. */
725 static inline bool
726 ok_for_index_p_nonstrict (rtx reg)
728 unsigned regno = REGNO (reg);
730 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
733 /* A version of regno_ok_for_base_p for use here, when all
734 pseudo-registers should count as OK. Arguments as for
735 regno_ok_for_base_p. */
736 static inline bool
737 ok_for_base_p_nonstrict (rtx reg, enum machine_mode mode,
738 enum rtx_code outer_code, enum rtx_code index_code)
740 unsigned regno = REGNO (reg);
742 if (regno >= FIRST_PSEUDO_REGISTER)
743 return true;
744 return ok_for_base_p_1 (regno, mode, outer_code, index_code);
747 /* Record the pseudo registers we must reload into hard registers in a
748 subexpression of a memory address, X.
750 If CONTEXT is 0, we are looking at the base part of an address,
751 otherwise we are looking at the index part.
753 MODE is the mode of the memory reference; OUTER_CODE and INDEX_CODE
754 give the context that the rtx appears in. These three arguments
755 are passed down to base_reg_class.
757 SCALE is twice the amount to multiply the cost by (it is twice so
758 we can represent half-cost adjustments). */
759 static void
760 record_address_regs (enum machine_mode mode, rtx x, int context,
761 enum rtx_code outer_code, enum rtx_code index_code,
762 int scale)
764 enum rtx_code code = GET_CODE (x);
765 enum reg_class rclass;
767 if (context == 1)
768 rclass = INDEX_REG_CLASS;
769 else
770 rclass = base_reg_class (mode, outer_code, index_code);
772 switch (code)
774 case CONST_INT:
775 case CONST:
776 case CC0:
777 case PC:
778 case SYMBOL_REF:
779 case LABEL_REF:
780 return;
782 case PLUS:
783 /* When we have an address that is a sum, we must determine
784 whether registers are "base" or "index" regs. If there is a
785 sum of two registers, we must choose one to be the "base".
786 Luckily, we can use the REG_POINTER to make a good choice
787 most of the time. We only need to do this on machines that
788 can have two registers in an address and where the base and
789 index register classes are different.
791 ??? This code used to set REGNO_POINTER_FLAG in some cases,
792 but that seems bogus since it should only be set when we are
793 sure the register is being used as a pointer. */
795 rtx arg0 = XEXP (x, 0);
796 rtx arg1 = XEXP (x, 1);
797 enum rtx_code code0 = GET_CODE (arg0);
798 enum rtx_code code1 = GET_CODE (arg1);
800 /* Look inside subregs. */
801 if (code0 == SUBREG)
802 arg0 = SUBREG_REG (arg0), code0 = GET_CODE (arg0);
803 if (code1 == SUBREG)
804 arg1 = SUBREG_REG (arg1), code1 = GET_CODE (arg1);
806 /* If this machine only allows one register per address, it
807 must be in the first operand. */
808 if (MAX_REGS_PER_ADDRESS == 1)
809 record_address_regs (mode, arg0, 0, PLUS, code1, scale);
811 /* If index and base registers are the same on this machine,
812 just record registers in any non-constant operands. We
813 assume here, as well as in the tests below, that all
814 addresses are in canonical form. */
815 else if (INDEX_REG_CLASS == base_reg_class (VOIDmode, PLUS, SCRATCH))
817 record_address_regs (mode, arg0, context, PLUS, code1, scale);
818 if (! CONSTANT_P (arg1))
819 record_address_regs (mode, arg1, context, PLUS, code0, scale);
822 /* If the second operand is a constant integer, it doesn't
823 change what class the first operand must be. */
824 else if (code1 == CONST_INT || code1 == CONST_DOUBLE)
825 record_address_regs (mode, arg0, context, PLUS, code1, scale);
826 /* If the second operand is a symbolic constant, the first
827 operand must be an index register. */
828 else if (code1 == SYMBOL_REF || code1 == CONST || code1 == LABEL_REF)
829 record_address_regs (mode, arg0, 1, PLUS, code1, scale);
830 /* If both operands are registers but one is already a hard
831 register of index or reg-base class, give the other the
832 class that the hard register is not. */
833 else if (code0 == REG && code1 == REG
834 && REGNO (arg0) < FIRST_PSEUDO_REGISTER
835 && (ok_for_base_p_nonstrict (arg0, mode, PLUS, REG)
836 || ok_for_index_p_nonstrict (arg0)))
837 record_address_regs (mode, arg1,
838 ok_for_base_p_nonstrict (arg0, mode, PLUS, REG)
839 ? 1 : 0,
840 PLUS, REG, scale);
841 else if (code0 == REG && code1 == REG
842 && REGNO (arg1) < FIRST_PSEUDO_REGISTER
843 && (ok_for_base_p_nonstrict (arg1, mode, PLUS, REG)
844 || ok_for_index_p_nonstrict (arg1)))
845 record_address_regs (mode, arg0,
846 ok_for_base_p_nonstrict (arg1, mode, PLUS, REG)
847 ? 1 : 0,
848 PLUS, REG, scale);
849 /* If one operand is known to be a pointer, it must be the
850 base with the other operand the index. Likewise if the
851 other operand is a MULT. */
852 else if ((code0 == REG && REG_POINTER (arg0)) || code1 == MULT)
854 record_address_regs (mode, arg0, 0, PLUS, code1, scale);
855 record_address_regs (mode, arg1, 1, PLUS, code0, scale);
857 else if ((code1 == REG && REG_POINTER (arg1)) || code0 == MULT)
859 record_address_regs (mode, arg0, 1, PLUS, code1, scale);
860 record_address_regs (mode, arg1, 0, PLUS, code0, scale);
862 /* Otherwise, count equal chances that each might be a base or
863 index register. This case should be rare. */
864 else
866 record_address_regs (mode, arg0, 0, PLUS, code1, scale / 2);
867 record_address_regs (mode, arg0, 1, PLUS, code1, scale / 2);
868 record_address_regs (mode, arg1, 0, PLUS, code0, scale / 2);
869 record_address_regs (mode, arg1, 1, PLUS, code0, scale / 2);
872 break;
874 /* Double the importance of an allocno that is incremented or
875 decremented, since it would take two extra insns if it ends
876 up in the wrong place. */
877 case POST_MODIFY:
878 case PRE_MODIFY:
879 record_address_regs (mode, XEXP (x, 0), 0, code,
880 GET_CODE (XEXP (XEXP (x, 1), 1)), 2 * scale);
881 if (REG_P (XEXP (XEXP (x, 1), 1)))
882 record_address_regs (mode, XEXP (XEXP (x, 1), 1), 1, code, REG,
883 2 * scale);
884 break;
886 case POST_INC:
887 case PRE_INC:
888 case POST_DEC:
889 case PRE_DEC:
890 /* Double the importance of an allocno that is incremented or
891 decremented, since it would take two extra insns if it ends
892 up in the wrong place. If the operand is a pseudo-register,
893 show it is being used in an INC_DEC context. */
894 #ifdef FORBIDDEN_INC_DEC_CLASSES
895 if (REG_P (XEXP (x, 0))
896 && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER)
897 in_inc_dec[COST_INDEX (REGNO (XEXP (x, 0)))] = true;
898 #endif
899 record_address_regs (mode, XEXP (x, 0), 0, code, SCRATCH, 2 * scale);
900 break;
902 case REG:
904 struct costs *pp;
905 enum reg_class i;
906 int k;
908 if (REGNO (x) < FIRST_PSEUDO_REGISTER)
909 break;
911 if (allocno_p)
912 ALLOCNO_BAD_SPILL_P (ira_curr_regno_allocno_map[REGNO (x)]) = true;
913 pp = COSTS (costs, COST_INDEX (REGNO (x)));
914 pp->mem_cost += (ira_memory_move_cost[Pmode][rclass][1] * scale) / 2;
915 for (k = 0; k < cost_classes_num; k++)
917 i = cost_classes[k];
918 pp->cost[k]
919 += (ira_get_may_move_cost (Pmode, i, rclass, true) * scale) / 2;
922 break;
924 default:
926 const char *fmt = GET_RTX_FORMAT (code);
927 int i;
928 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
929 if (fmt[i] == 'e')
930 record_address_regs (mode, XEXP (x, i), context, code, SCRATCH,
931 scale);
938 /* Calculate the costs of insn operands. */
939 static void
940 record_operand_costs (rtx insn, enum reg_class *pref)
942 const char *constraints[MAX_RECOG_OPERANDS];
943 enum machine_mode modes[MAX_RECOG_OPERANDS];
944 int i;
946 for (i = 0; i < recog_data.n_operands; i++)
948 constraints[i] = recog_data.constraints[i];
949 modes[i] = recog_data.operand_mode[i];
952 /* If we get here, we are set up to record the costs of all the
953 operands for this insn. Start by initializing the costs. Then
954 handle any address registers. Finally record the desired classes
955 for any allocnos, doing it twice if some pair of operands are
956 commutative. */
957 for (i = 0; i < recog_data.n_operands; i++)
959 memcpy (op_costs[i], init_cost, struct_costs_size);
961 if (GET_CODE (recog_data.operand[i]) == SUBREG)
962 recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
964 if (MEM_P (recog_data.operand[i]))
965 record_address_regs (GET_MODE (recog_data.operand[i]),
966 XEXP (recog_data.operand[i], 0),
967 0, MEM, SCRATCH, frequency * 2);
968 else if (constraints[i][0] == 'p'
969 || EXTRA_ADDRESS_CONSTRAINT (constraints[i][0],
970 constraints[i]))
971 record_address_regs (VOIDmode, recog_data.operand[i], 0, ADDRESS,
972 SCRATCH, frequency * 2);
975 /* Check for commutative in a separate loop so everything will have
976 been initialized. We must do this even if one operand is a
977 constant--see addsi3 in m68k.md. */
978 for (i = 0; i < (int) recog_data.n_operands - 1; i++)
979 if (constraints[i][0] == '%')
981 const char *xconstraints[MAX_RECOG_OPERANDS];
982 int j;
984 /* Handle commutative operands by swapping the constraints.
985 We assume the modes are the same. */
986 for (j = 0; j < recog_data.n_operands; j++)
987 xconstraints[j] = constraints[j];
989 xconstraints[i] = constraints[i+1];
990 xconstraints[i+1] = constraints[i];
991 record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
992 recog_data.operand, modes,
993 xconstraints, insn, pref);
995 record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
996 recog_data.operand, modes,
997 constraints, insn, pref);
1002 /* Process one insn INSN. Scan it and record each time it would save
1003 code to put a certain allocnos in a certain class. Return the last
1004 insn processed, so that the scan can be continued from there. */
1005 static rtx
1006 scan_one_insn (rtx insn)
1008 enum rtx_code pat_code;
1009 rtx set, note;
1010 int i, k;
1012 if (!NONDEBUG_INSN_P (insn))
1013 return insn;
1015 pat_code = GET_CODE (PATTERN (insn));
1016 if (pat_code == USE || pat_code == CLOBBER || pat_code == ASM_INPUT
1017 || pat_code == ADDR_VEC || pat_code == ADDR_DIFF_VEC)
1018 return insn;
1020 set = single_set (insn);
1021 extract_insn (insn);
1023 /* If this insn loads a parameter from its stack slot, then it
1024 represents a savings, rather than a cost, if the parameter is
1025 stored in memory. Record this fact. */
1026 if (set != 0 && REG_P (SET_DEST (set)) && MEM_P (SET_SRC (set))
1027 && (note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != NULL_RTX
1028 && MEM_P (XEXP (note, 0)))
1030 enum reg_class cl = GENERAL_REGS;
1031 rtx reg = SET_DEST (set);
1032 int num = COST_INDEX (REGNO (reg));
1034 if (pref)
1035 cl = pref[num];
1036 COSTS (costs, num)->mem_cost
1037 -= ira_memory_move_cost[GET_MODE (reg)][cl][1] * frequency;
1038 record_address_regs (GET_MODE (SET_SRC (set)), XEXP (SET_SRC (set), 0),
1039 0, MEM, SCRATCH, frequency * 2);
1042 record_operand_costs (insn, pref);
1044 /* Now add the cost for each operand to the total costs for its
1045 allocno. */
1046 for (i = 0; i < recog_data.n_operands; i++)
1047 if (REG_P (recog_data.operand[i])
1048 && REGNO (recog_data.operand[i]) >= FIRST_PSEUDO_REGISTER)
1050 int regno = REGNO (recog_data.operand[i]);
1051 struct costs *p = COSTS (costs, COST_INDEX (regno));
1052 struct costs *q = op_costs[i];
1054 p->mem_cost += q->mem_cost;
1055 for (k = 0; k < cost_classes_num; k++)
1056 p->cost[k] += q->cost[k];
1059 return insn;
1064 /* Print allocnos costs to file F. */
1065 static void
1066 print_allocno_costs (FILE *f)
1068 int k;
1069 ira_allocno_t a;
1070 ira_allocno_iterator ai;
1072 ira_assert (allocno_p);
1073 fprintf (f, "\n");
1074 FOR_EACH_ALLOCNO (a, ai)
1076 int i, rclass;
1077 basic_block bb;
1078 int regno = ALLOCNO_REGNO (a);
1080 i = ALLOCNO_NUM (a);
1081 fprintf (f, " a%d(r%d,", i, regno);
1082 if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
1083 fprintf (f, "b%d", bb->index);
1084 else
1085 fprintf (f, "l%d", ALLOCNO_LOOP_TREE_NODE (a)->loop->num);
1086 fprintf (f, ") costs:");
1087 for (k = 0; k < cost_classes_num; k++)
1089 rclass = cost_classes[k];
1090 if (contains_reg_of_mode[rclass][PSEUDO_REGNO_MODE (regno)]
1091 #ifdef FORBIDDEN_INC_DEC_CLASSES
1092 && (! in_inc_dec[i] || ! forbidden_inc_dec_class[rclass])
1093 #endif
1094 #ifdef CANNOT_CHANGE_MODE_CLASS
1095 && ! invalid_mode_change_p (regno, (enum reg_class) rclass,
1096 PSEUDO_REGNO_MODE (regno))
1097 #endif
1100 fprintf (f, " %s:%d", reg_class_names[rclass],
1101 COSTS (costs, i)->cost[k]);
1102 if (flag_ira_region == IRA_REGION_ALL
1103 || flag_ira_region == IRA_REGION_MIXED)
1104 fprintf (f, ",%d", COSTS (total_allocno_costs, i)->cost[k]);
1107 fprintf (f, " MEM:%i\n", COSTS (costs, i)->mem_cost);
1111 /* Print pseudo costs to file F. */
1112 static void
1113 print_pseudo_costs (FILE *f)
1115 int regno, k;
1116 int rclass;
1118 ira_assert (! allocno_p);
1119 fprintf (f, "\n");
1120 for (regno = max_reg_num () - 1; regno >= FIRST_PSEUDO_REGISTER; regno--)
1122 if (regno_reg_rtx[regno] == NULL_RTX)
1123 continue;
1124 fprintf (f, " r%d costs:", regno);
1125 for (k = 0; k < cost_classes_num; k++)
1127 rclass = cost_classes[k];
1128 if (contains_reg_of_mode[rclass][PSEUDO_REGNO_MODE (regno)]
1129 #ifdef FORBIDDEN_INC_DEC_CLASSES
1130 && (! in_inc_dec[regno] || ! forbidden_inc_dec_class[rclass])
1131 #endif
1132 #ifdef CANNOT_CHANGE_MODE_CLASS
1133 && ! invalid_mode_change_p (regno, (enum reg_class) rclass,
1134 PSEUDO_REGNO_MODE (regno))
1135 #endif
1137 fprintf (f, " %s:%d", reg_class_names[rclass],
1138 COSTS (costs, regno)->cost[k]);
1140 fprintf (f, " MEM:%i\n", COSTS (costs, regno)->mem_cost);
1144 /* Traverse the BB represented by LOOP_TREE_NODE to update the allocno
1145 costs. */
1146 static void
1147 process_bb_for_costs (basic_block bb)
1149 rtx insn;
1151 frequency = REG_FREQ_FROM_BB (bb);
1152 if (frequency == 0)
1153 frequency = 1;
1154 FOR_BB_INSNS (bb, insn)
1155 insn = scan_one_insn (insn);
1158 /* Traverse the BB represented by LOOP_TREE_NODE to update the allocno
1159 costs. */
1160 static void
1161 process_bb_node_for_costs (ira_loop_tree_node_t loop_tree_node)
1163 basic_block bb;
1165 bb = loop_tree_node->bb;
1166 if (bb != NULL)
1167 process_bb_for_costs (bb);
1170 /* Find costs of register classes and memory for allocnos or pseudos
1171 and their best costs. Set up preferred, alternative and cover
1172 classes for pseudos. */
1173 static void
1174 find_costs_and_classes (FILE *dump_file)
1176 int i, k, start;
1177 int pass;
1178 basic_block bb;
1180 init_recog ();
1181 #ifdef FORBIDDEN_INC_DEC_CLASSES
1182 in_inc_dec = ira_allocate (sizeof (bool) * cost_elements_num);
1183 #endif /* FORBIDDEN_INC_DEC_CLASSES */
1184 pref = NULL;
1185 start = 0;
1186 if (!resize_reg_info () && allocno_p && pseudo_classes_defined_p)
1188 ira_allocno_t a;
1189 ira_allocno_iterator ai;
1191 pref = pref_buffer;
1192 FOR_EACH_ALLOCNO (a, ai)
1193 pref[ALLOCNO_NUM (a)] = reg_preferred_class (ALLOCNO_REGNO (a));
1194 if (flag_expensive_optimizations)
1195 start = 1;
1197 if (allocno_p)
1198 /* Clear the flag for the next compiled function. */
1199 pseudo_classes_defined_p = false;
1200 /* Normally we scan the insns once and determine the best class to
1201 use for each allocno. However, if -fexpensive-optimizations are
1202 on, we do so twice, the second time using the tentative best
1203 classes to guide the selection. */
1204 for (pass = start; pass <= flag_expensive_optimizations; pass++)
1206 if ((!allocno_p || internal_flag_ira_verbose > 0) && dump_file)
1207 fprintf (dump_file,
1208 "\nPass %i for finding pseudo/allocno costs\n\n", pass);
1209 /* We could use only cover classes. Unfortunately it does not
1210 work well for some targets where some subclass of cover class
1211 is costly and wrong cover class is chosen. */
1212 for (i = 0; i < N_REG_CLASSES; i++)
1213 cost_class_nums[i] = -1;
1214 for (cost_classes_num = 0;
1215 cost_classes_num < ira_important_classes_num;
1216 cost_classes_num++)
1218 cost_classes[cost_classes_num]
1219 = ira_important_classes[cost_classes_num];
1220 cost_class_nums[cost_classes[cost_classes_num]]
1221 = cost_classes_num;
1223 struct_costs_size
1224 = sizeof (struct costs) + sizeof (int) * (cost_classes_num - 1);
1225 /* Zero out our accumulation of the cost of each class for each
1226 allocno. */
1227 memset (costs, 0, cost_elements_num * struct_costs_size);
1228 #ifdef FORBIDDEN_INC_DEC_CLASSES
1229 memset (in_inc_dec, 0, cost_elements_num * sizeof (bool));
1230 #endif
1232 if (allocno_p)
1234 /* Scan the instructions and record each time it would save code
1235 to put a certain allocno in a certain class. */
1236 ira_traverse_loop_tree (true, ira_loop_tree_root,
1237 process_bb_node_for_costs, NULL);
1239 memcpy (total_allocno_costs, costs,
1240 max_struct_costs_size * ira_allocnos_num);
1242 else
1244 basic_block bb;
1246 FOR_EACH_BB (bb)
1247 process_bb_for_costs (bb);
1250 if (pass == 0)
1251 pref = pref_buffer;
1253 /* Now for each allocno look at how desirable each class is and
1254 find which class is preferred. */
1255 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
1257 ira_allocno_t a, parent_a;
1258 int rclass, a_num, parent_a_num;
1259 ira_loop_tree_node_t parent;
1260 int best_cost, allocno_cost;
1261 enum reg_class best, alt_class;
1262 #ifdef FORBIDDEN_INC_DEC_CLASSES
1263 int inc_dec_p = false;
1264 #endif
1265 int equiv_savings = regno_equiv_gains[i];
1267 if (! allocno_p)
1269 if (regno_reg_rtx[i] == NULL_RTX)
1270 continue;
1271 #ifdef FORBIDDEN_INC_DEC_CLASSES
1272 inc_dec_p = in_inc_dec[i];
1273 #endif
1274 memcpy (temp_costs, COSTS (costs, i), struct_costs_size);
1276 else
1278 if (ira_regno_allocno_map[i] == NULL)
1279 continue;
1280 memset (temp_costs, 0, struct_costs_size);
1281 /* Find cost of all allocnos with the same regno. */
1282 for (a = ira_regno_allocno_map[i];
1283 a != NULL;
1284 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
1286 a_num = ALLOCNO_NUM (a);
1287 if ((flag_ira_region == IRA_REGION_ALL
1288 || flag_ira_region == IRA_REGION_MIXED)
1289 && (parent = ALLOCNO_LOOP_TREE_NODE (a)->parent) != NULL
1290 && (parent_a = parent->regno_allocno_map[i]) != NULL
1291 /* There are no caps yet. */
1292 && bitmap_bit_p (ALLOCNO_LOOP_TREE_NODE
1293 (a)->border_allocnos,
1294 ALLOCNO_NUM (a)))
1296 /* Propagate costs to upper levels in the region
1297 tree. */
1298 parent_a_num = ALLOCNO_NUM (parent_a);
1299 for (k = 0; k < cost_classes_num; k++)
1300 COSTS (total_allocno_costs, parent_a_num)->cost[k]
1301 += COSTS (total_allocno_costs, a_num)->cost[k];
1302 COSTS (total_allocno_costs, parent_a_num)->mem_cost
1303 += COSTS (total_allocno_costs, a_num)->mem_cost;
1305 for (k = 0; k < cost_classes_num; k++)
1306 temp_costs->cost[k] += COSTS (costs, a_num)->cost[k];
1307 temp_costs->mem_cost += COSTS (costs, a_num)->mem_cost;
1308 #ifdef FORBIDDEN_INC_DEC_CLASSES
1309 if (in_inc_dec[a_num])
1310 inc_dec_p = true;
1311 #endif
1314 if (equiv_savings < 0)
1315 temp_costs->mem_cost = -equiv_savings;
1316 else if (equiv_savings > 0)
1318 temp_costs->mem_cost = 0;
1319 for (k = 0; k < cost_classes_num; k++)
1320 temp_costs->cost[k] += equiv_savings;
1323 best_cost = (1 << (HOST_BITS_PER_INT - 2)) - 1;
1324 best = ALL_REGS;
1325 alt_class = NO_REGS;
1326 /* Find best common class for all allocnos with the same
1327 regno. */
1328 for (k = 0; k < cost_classes_num; k++)
1330 rclass = cost_classes[k];
1331 /* Ignore classes that are too small for this operand or
1332 invalid for an operand that was auto-incremented. */
1333 if (! contains_reg_of_mode[rclass][PSEUDO_REGNO_MODE (i)]
1334 #ifdef FORBIDDEN_INC_DEC_CLASSES
1335 || (inc_dec_p && forbidden_inc_dec_class[rclass])
1336 #endif
1337 #ifdef CANNOT_CHANGE_MODE_CLASS
1338 || invalid_mode_change_p (i, (enum reg_class) rclass,
1339 PSEUDO_REGNO_MODE (i))
1340 #endif
1342 continue;
1343 if (temp_costs->cost[k] < best_cost)
1345 best_cost = temp_costs->cost[k];
1346 best = (enum reg_class) rclass;
1348 else if (temp_costs->cost[k] == best_cost)
1349 best = ira_reg_class_union[best][rclass];
1350 if (pass == flag_expensive_optimizations
1351 && temp_costs->cost[k] < temp_costs->mem_cost
1352 && (reg_class_size[reg_class_subunion[alt_class][rclass]]
1353 > reg_class_size[alt_class]))
1354 alt_class = reg_class_subunion[alt_class][rclass];
1356 alt_class = ira_class_translate[alt_class];
1357 if (best_cost > temp_costs->mem_cost)
1358 regno_cover_class[i] = NO_REGS;
1359 else if (flag_ira_algorithm == IRA_ALGORITHM_PRIORITY)
1360 /* Make the common class the biggest class of best and
1361 alt_class. */
1362 regno_cover_class[i] = alt_class == NO_REGS ? best : alt_class;
1363 else
1364 /* Make the common class a cover class. Remember all
1365 allocnos with the same regno should have the same cover
1366 class. */
1367 regno_cover_class[i] = ira_class_translate[best];
1368 if (pass == flag_expensive_optimizations)
1370 if (best_cost > temp_costs->mem_cost)
1371 best = alt_class = NO_REGS;
1372 else if (best == alt_class)
1373 alt_class = NO_REGS;
1374 setup_reg_classes (i, best, alt_class, regno_cover_class[i]);
1375 if ((!allocno_p || internal_flag_ira_verbose > 2)
1376 && dump_file != NULL)
1377 fprintf (dump_file,
1378 " r%d: preferred %s, alternative %s, cover %s\n",
1379 i, reg_class_names[best], reg_class_names[alt_class],
1380 reg_class_names[regno_cover_class[i]]);
1382 if (! allocno_p)
1384 pref[i] = best_cost > temp_costs->mem_cost ? NO_REGS : best;
1385 continue;
1387 for (a = ira_regno_allocno_map[i];
1388 a != NULL;
1389 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
1391 a_num = ALLOCNO_NUM (a);
1392 if (regno_cover_class[i] == NO_REGS)
1393 best = NO_REGS;
1394 else
1396 /* Finding best class which is subset of the common
1397 class. */
1398 best_cost = (1 << (HOST_BITS_PER_INT - 2)) - 1;
1399 allocno_cost = best_cost;
1400 best = ALL_REGS;
1401 for (k = 0; k < cost_classes_num; k++)
1403 rclass = cost_classes[k];
1404 if (! ira_class_subset_p[rclass][regno_cover_class[i]])
1405 continue;
1406 /* Ignore classes that are too small for this
1407 operand or invalid for an operand that was
1408 auto-incremented. */
1409 if (! contains_reg_of_mode[rclass][PSEUDO_REGNO_MODE (i)]
1410 #ifdef FORBIDDEN_INC_DEC_CLASSES
1411 || (inc_dec_p && forbidden_inc_dec_class[rclass])
1412 #endif
1413 #ifdef CANNOT_CHANGE_MODE_CLASS
1414 || invalid_mode_change_p (i, (enum reg_class) rclass,
1415 PSEUDO_REGNO_MODE (i))
1416 #endif
1419 else if (COSTS (total_allocno_costs, a_num)->cost[k]
1420 < best_cost)
1422 best_cost
1423 = COSTS (total_allocno_costs, a_num)->cost[k];
1424 allocno_cost = COSTS (costs, a_num)->cost[k];
1425 best = (enum reg_class) rclass;
1427 else if (COSTS (total_allocno_costs, a_num)->cost[k]
1428 == best_cost)
1430 best = ira_reg_class_union[best][rclass];
1431 allocno_cost
1432 = MAX (allocno_cost, COSTS (costs, a_num)->cost[k]);
1435 ALLOCNO_COVER_CLASS_COST (a) = allocno_cost;
1437 ira_assert (flag_ira_algorithm == IRA_ALGORITHM_PRIORITY
1438 || ira_class_translate[best] == regno_cover_class[i]);
1439 if (internal_flag_ira_verbose > 2 && dump_file != NULL
1440 && (pass == 0 || pref[a_num] != best))
1442 fprintf (dump_file, " a%d (r%d,", a_num, i);
1443 if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
1444 fprintf (dump_file, "b%d", bb->index);
1445 else
1446 fprintf (dump_file, "l%d",
1447 ALLOCNO_LOOP_TREE_NODE (a)->loop->num);
1448 fprintf (dump_file, ") best %s, cover %s\n",
1449 reg_class_names[best],
1450 reg_class_names[regno_cover_class[i]]);
1452 pref[a_num] = best;
1456 if (internal_flag_ira_verbose > 4 && dump_file)
1458 if (allocno_p)
1459 print_allocno_costs (dump_file);
1460 else
1461 print_pseudo_costs (dump_file);
1462 fprintf (dump_file,"\n");
1465 #ifdef FORBIDDEN_INC_DEC_CLASSES
1466 ira_free (in_inc_dec);
1467 #endif
1472 /* Process moves involving hard regs to modify allocno hard register
1473 costs. We can do this only after determining allocno cover class.
1474 If a hard register forms a register class, than moves with the hard
1475 register are already taken into account in class costs for the
1476 allocno. */
1477 static void
1478 process_bb_node_for_hard_reg_moves (ira_loop_tree_node_t loop_tree_node)
1480 int i, freq, cost, src_regno, dst_regno, hard_regno;
1481 bool to_p;
1482 ira_allocno_t a;
1483 enum reg_class rclass, hard_reg_class;
1484 enum machine_mode mode;
1485 basic_block bb;
1486 rtx insn, set, src, dst;
1488 bb = loop_tree_node->bb;
1489 if (bb == NULL)
1490 return;
1491 freq = REG_FREQ_FROM_BB (bb);
1492 if (freq == 0)
1493 freq = 1;
1494 FOR_BB_INSNS (bb, insn)
1496 if (!NONDEBUG_INSN_P (insn))
1497 continue;
1498 set = single_set (insn);
1499 if (set == NULL_RTX)
1500 continue;
1501 dst = SET_DEST (set);
1502 src = SET_SRC (set);
1503 if (! REG_P (dst) || ! REG_P (src))
1504 continue;
1505 dst_regno = REGNO (dst);
1506 src_regno = REGNO (src);
1507 if (dst_regno >= FIRST_PSEUDO_REGISTER
1508 && src_regno < FIRST_PSEUDO_REGISTER)
1510 hard_regno = src_regno;
1511 to_p = true;
1512 a = ira_curr_regno_allocno_map[dst_regno];
1514 else if (src_regno >= FIRST_PSEUDO_REGISTER
1515 && dst_regno < FIRST_PSEUDO_REGISTER)
1517 hard_regno = dst_regno;
1518 to_p = false;
1519 a = ira_curr_regno_allocno_map[src_regno];
1521 else
1522 continue;
1523 rclass = ALLOCNO_COVER_CLASS (a);
1524 if (! TEST_HARD_REG_BIT (reg_class_contents[rclass], hard_regno))
1525 continue;
1526 i = ira_class_hard_reg_index[rclass][hard_regno];
1527 if (i < 0)
1528 continue;
1529 mode = ALLOCNO_MODE (a);
1530 hard_reg_class = REGNO_REG_CLASS (hard_regno);
1531 cost
1532 = (to_p ? ira_get_register_move_cost (mode, hard_reg_class, rclass)
1533 : ira_get_register_move_cost (mode, rclass, hard_reg_class)) * freq;
1534 ira_allocate_and_set_costs (&ALLOCNO_HARD_REG_COSTS (a), rclass,
1535 ALLOCNO_COVER_CLASS_COST (a));
1536 ira_allocate_and_set_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a),
1537 rclass, 0);
1538 ALLOCNO_HARD_REG_COSTS (a)[i] -= cost;
1539 ALLOCNO_CONFLICT_HARD_REG_COSTS (a)[i] -= cost;
1540 ALLOCNO_COVER_CLASS_COST (a) = MIN (ALLOCNO_COVER_CLASS_COST (a),
1541 ALLOCNO_HARD_REG_COSTS (a)[i]);
1545 /* After we find hard register and memory costs for allocnos, define
1546 its cover class and modify hard register cost because insns moving
1547 allocno to/from hard registers. */
1548 static void
1549 setup_allocno_cover_class_and_costs (void)
1551 int i, j, n, regno, num;
1552 int *reg_costs;
1553 enum reg_class cover_class, rclass;
1554 ira_allocno_t a;
1555 ira_allocno_iterator ai;
1557 ira_assert (allocno_p);
1558 FOR_EACH_ALLOCNO (a, ai)
1560 i = ALLOCNO_NUM (a);
1561 cover_class = regno_cover_class[ALLOCNO_REGNO (a)];
1562 ira_assert (pref[i] == NO_REGS || cover_class != NO_REGS);
1563 ALLOCNO_MEMORY_COST (a) = COSTS (costs, i)->mem_cost;
1564 ira_set_allocno_cover_class (a, cover_class);
1565 if (cover_class == NO_REGS)
1566 continue;
1567 ALLOCNO_AVAILABLE_REGS_NUM (a) = ira_available_class_regs[cover_class];
1568 if (optimize && ALLOCNO_COVER_CLASS (a) != pref[i])
1570 n = ira_class_hard_regs_num[cover_class];
1571 ALLOCNO_HARD_REG_COSTS (a)
1572 = reg_costs = ira_allocate_cost_vector (cover_class);
1573 for (j = n - 1; j >= 0; j--)
1575 regno = ira_class_hard_regs[cover_class][j];
1576 if (TEST_HARD_REG_BIT (reg_class_contents[pref[i]], regno))
1577 reg_costs[j] = ALLOCNO_COVER_CLASS_COST (a);
1578 else
1580 rclass = REGNO_REG_CLASS (regno);
1581 num = cost_class_nums[rclass];
1582 if (num < 0)
1584 /* The hard register class is not a cover class or a
1585 class not fully inside in a cover class -- use
1586 the allocno cover class. */
1587 ira_assert (ira_hard_regno_cover_class[regno]
1588 == cover_class);
1589 num = cost_class_nums[cover_class];
1591 reg_costs[j] = COSTS (costs, i)->cost[num];
1596 if (optimize)
1597 ira_traverse_loop_tree (true, ira_loop_tree_root,
1598 process_bb_node_for_hard_reg_moves, NULL);
1603 /* Function called once during compiler work. */
1604 void
1605 ira_init_costs_once (void)
1607 int i;
1609 init_cost = NULL;
1610 for (i = 0; i < MAX_RECOG_OPERANDS; i++)
1612 op_costs[i] = NULL;
1613 this_op_costs[i] = NULL;
1615 temp_costs = NULL;
1616 cost_classes = NULL;
1619 /* Free allocated temporary cost vectors. */
1620 static void
1621 free_ira_costs (void)
1623 int i;
1625 if (init_cost != NULL)
1626 free (init_cost);
1627 init_cost = NULL;
1628 for (i = 0; i < MAX_RECOG_OPERANDS; i++)
1630 if (op_costs[i] != NULL)
1631 free (op_costs[i]);
1632 if (this_op_costs[i] != NULL)
1633 free (this_op_costs[i]);
1634 op_costs[i] = this_op_costs[i] = NULL;
1636 if (temp_costs != NULL)
1637 free (temp_costs);
1638 temp_costs = NULL;
1639 if (cost_classes != NULL)
1640 free (cost_classes);
1641 cost_classes = NULL;
1644 /* This is called each time register related information is
1645 changed. */
1646 void
1647 ira_init_costs (void)
1649 int i;
1651 free_ira_costs ();
1652 max_struct_costs_size
1653 = sizeof (struct costs) + sizeof (int) * (ira_important_classes_num - 1);
1654 /* Don't use ira_allocate because vectors live through several IRA calls. */
1655 init_cost = (struct costs *) xmalloc (max_struct_costs_size);
1656 init_cost->mem_cost = 1000000;
1657 for (i = 0; i < ira_important_classes_num; i++)
1658 init_cost->cost[i] = 1000000;
1659 for (i = 0; i < MAX_RECOG_OPERANDS; i++)
1661 op_costs[i] = (struct costs *) xmalloc (max_struct_costs_size);
1662 this_op_costs[i] = (struct costs *) xmalloc (max_struct_costs_size);
1664 temp_costs = (struct costs *) xmalloc (max_struct_costs_size);
1665 cost_classes = (enum reg_class *) xmalloc (sizeof (enum reg_class)
1666 * ira_important_classes_num);
1669 /* Function called once at the end of compiler work. */
1670 void
1671 ira_finish_costs_once (void)
1673 free_ira_costs ();
1678 /* Common initialization function for ira_costs and
1679 ira_set_pseudo_classes. */
1680 static void
1681 init_costs (void)
1683 init_subregs_of_mode ();
1684 costs = (struct costs *) ira_allocate (max_struct_costs_size
1685 * cost_elements_num);
1686 pref_buffer
1687 = (enum reg_class *) ira_allocate (sizeof (enum reg_class)
1688 * cost_elements_num);
1689 regno_cover_class
1690 = (enum reg_class *) ira_allocate (sizeof (enum reg_class)
1691 * max_reg_num ());
1692 regno_equiv_gains = (int *) ira_allocate (sizeof (int) * max_reg_num ());
1693 memset (regno_equiv_gains, 0, sizeof (int) * max_reg_num ());
1696 /* Common finalization function for ira_costs and
1697 ira_set_pseudo_classes. */
1698 static void
1699 finish_costs (void)
1701 ira_free (regno_equiv_gains);
1702 ira_free (regno_cover_class);
1703 ira_free (pref_buffer);
1704 ira_free (costs);
1707 /* Entry function which defines cover class, memory and hard register
1708 costs for each allocno. */
1709 void
1710 ira_costs (void)
1712 allocno_p = true;
1713 cost_elements_num = ira_allocnos_num;
1714 init_costs ();
1715 total_allocno_costs = (struct costs *) ira_allocate (max_struct_costs_size
1716 * ira_allocnos_num);
1717 calculate_elim_costs_all_insns ();
1718 find_costs_and_classes (ira_dump_file);
1719 setup_allocno_cover_class_and_costs ();
1720 finish_costs ();
1721 ira_free (total_allocno_costs);
1724 /* Entry function which defines classes for pseudos. */
1725 void
1726 ira_set_pseudo_classes (FILE *dump_file)
1728 allocno_p = false;
1729 internal_flag_ira_verbose = flag_ira_verbose;
1730 cost_elements_num = max_reg_num ();
1731 init_costs ();
1732 find_costs_and_classes (dump_file);
1733 pseudo_classes_defined_p = true;
1734 finish_costs ();
1739 /* Change hard register costs for allocnos which lives through
1740 function calls. This is called only when we found all intersected
1741 calls during building allocno live ranges. */
1742 void
1743 ira_tune_allocno_costs_and_cover_classes (void)
1745 int j, n, regno;
1746 int cost, min_cost, *reg_costs;
1747 enum reg_class cover_class, rclass;
1748 enum machine_mode mode;
1749 ira_allocno_t a;
1750 ira_allocno_iterator ai;
1752 FOR_EACH_ALLOCNO (a, ai)
1754 cover_class = ALLOCNO_COVER_CLASS (a);
1755 if (cover_class == NO_REGS)
1756 continue;
1757 mode = ALLOCNO_MODE (a);
1758 n = ira_class_hard_regs_num[cover_class];
1759 min_cost = INT_MAX;
1760 if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
1762 ira_allocate_and_set_costs
1763 (&ALLOCNO_HARD_REG_COSTS (a), cover_class,
1764 ALLOCNO_COVER_CLASS_COST (a));
1765 reg_costs = ALLOCNO_HARD_REG_COSTS (a);
1766 for (j = n - 1; j >= 0; j--)
1768 regno = ira_class_hard_regs[cover_class][j];
1769 rclass = REGNO_REG_CLASS (regno);
1770 cost = 0;
1771 if (! ira_hard_reg_not_in_set_p (regno, mode, call_used_reg_set)
1772 || HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
1773 cost += (ALLOCNO_CALL_FREQ (a)
1774 * (ira_memory_move_cost[mode][rclass][0]
1775 + ira_memory_move_cost[mode][rclass][1]));
1776 #ifdef IRA_HARD_REGNO_ADD_COST_MULTIPLIER
1777 cost += ((ira_memory_move_cost[mode][rclass][0]
1778 + ira_memory_move_cost[mode][rclass][1])
1779 * ALLOCNO_FREQ (a)
1780 * IRA_HARD_REGNO_ADD_COST_MULTIPLIER (regno) / 2);
1781 #endif
1782 reg_costs[j] += cost;
1783 if (min_cost > reg_costs[j])
1784 min_cost = reg_costs[j];
1787 if (min_cost != INT_MAX)
1788 ALLOCNO_COVER_CLASS_COST (a) = min_cost;
1790 /* Some targets allow pseudos to be allocated to unaligned
1791 sequences of hard registers. However, selecting an unaligned
1792 sequence can unnecessarily restrict later allocations. So
1793 increase the cost of unaligned hard regs to encourage the use
1794 of aligned hard regs. */
1796 int nregs, index;
1798 if ((nregs = ira_reg_class_nregs[cover_class][ALLOCNO_MODE (a)]) > 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 if (j % nregs != 0)
1808 regno = ira_non_ordered_class_hard_regs[cover_class][j];
1809 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;