* gcc.target/powerpc/altivec-volatile.c: Adjust expected warning.
[official-gcc.git] / gcc / ira-costs.c
blobda26ad3d8c95314b32ea00d36534acc5bac81195
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 "toplev.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 /* Initialized once. It is a maximal possible size of the allocated
71 struct costs. */
72 static int max_struct_costs_size;
74 /* Allocated and initialized once, and used to initialize cost values
75 for each insn. */
76 static struct costs *init_cost;
78 /* Allocated once, and used for temporary purposes. */
79 static struct costs *temp_costs;
81 /* Allocated once, and used for the cost calculation. */
82 static struct costs *op_costs[MAX_RECOG_OPERANDS];
83 static struct costs *this_op_costs[MAX_RECOG_OPERANDS];
85 /* Costs of each class for each allocno or pseudo. */
86 static struct costs *costs;
88 /* Accumulated costs of each class for each allocno. */
89 static struct costs *total_allocno_costs;
91 /* Classes used for cost calculation. They may be different on
92 different iterations of the cost calculations or in different
93 optimization modes. */
94 static enum reg_class *cost_classes;
96 /* The size of the previous array. */
97 static int cost_classes_num;
99 /* Map: cost class -> order number (they start with 0) of the cost
100 class. The order number is negative for non-cost classes. */
101 static int cost_class_nums[N_REG_CLASSES];
103 /* It is the current size of struct costs. */
104 static int struct_costs_size;
106 /* Return pointer to structure containing costs of allocno or pseudo
107 with given NUM in array ARR. */
108 #define COSTS(arr, num) \
109 ((struct costs *) ((char *) (arr) + (num) * struct_costs_size))
111 /* Return index in COSTS when processing reg with REGNO. */
112 #define COST_INDEX(regno) (allocno_p \
113 ? ALLOCNO_NUM (ira_curr_regno_allocno_map[regno]) \
114 : (int) regno)
116 /* Record register class preferences of each allocno or pseudo. Null
117 value means no preferences. It happens on the 1st iteration of the
118 cost calculation. */
119 static enum reg_class *pref;
121 /* Allocated buffers for pref. */
122 static enum reg_class *pref_buffer;
124 /* Record cover register class of each allocno with the same regno. */
125 static enum reg_class *regno_cover_class;
127 /* Record cost gains for not allocating a register with an invariant
128 equivalence. */
129 static int *regno_equiv_gains;
131 /* Execution frequency of the current insn. */
132 static int frequency;
136 /* Compute the cost of loading X into (if TO_P is TRUE) or from (if
137 TO_P is FALSE) a register of class RCLASS in mode MODE. X must not
138 be a pseudo register. */
139 static int
140 copy_cost (rtx x, enum machine_mode mode, enum reg_class rclass, bool to_p,
141 secondary_reload_info *prev_sri)
143 secondary_reload_info sri;
144 enum reg_class secondary_class = NO_REGS;
146 /* If X is a SCRATCH, there is actually nothing to move since we are
147 assuming optimal allocation. */
148 if (GET_CODE (x) == SCRATCH)
149 return 0;
151 /* Get the class we will actually use for a reload. */
152 rclass = PREFERRED_RELOAD_CLASS (x, rclass);
154 /* If we need a secondary reload for an intermediate, the cost is
155 that to load the input into the intermediate register, then to
156 copy it. */
157 sri.prev_sri = prev_sri;
158 sri.extra_cost = 0;
159 secondary_class
160 = (enum reg_class) targetm.secondary_reload (to_p, x, rclass, mode, &sri);
162 if (secondary_class != NO_REGS)
164 if (!move_cost[mode])
165 init_move_cost (mode);
166 return (move_cost[mode][secondary_class][rclass] + sri.extra_cost
167 + copy_cost (x, mode, secondary_class, to_p, &sri));
170 /* For memory, use the memory move cost, for (hard) registers, use
171 the cost to move between the register classes, and use 2 for
172 everything else (constants). */
173 if (MEM_P (x) || rclass == NO_REGS)
174 return sri.extra_cost + ira_memory_move_cost[mode][rclass][to_p != 0];
175 else if (REG_P (x))
177 if (!move_cost[mode])
178 init_move_cost (mode);
179 return (sri.extra_cost + move_cost[mode][REGNO_REG_CLASS (REGNO (x))][rclass]);
181 else
182 /* If this is a constant, we may eventually want to call rtx_cost
183 here. */
184 return sri.extra_cost + COSTS_N_INSNS (1);
189 /* Record the cost of using memory or hard registers of various
190 classes for the operands in INSN.
192 N_ALTS is the number of alternatives.
193 N_OPS is the number of operands.
194 OPS is an array of the operands.
195 MODES are the modes of the operands, in case any are VOIDmode.
196 CONSTRAINTS are the constraints to use for the operands. This array
197 is modified by this procedure.
199 This procedure works alternative by alternative. For each
200 alternative we assume that we will be able to allocate all allocnos
201 to their ideal register class and calculate the cost of using that
202 alternative. Then we compute, for each operand that is a
203 pseudo-register, the cost of having the allocno allocated to each
204 register class and using it in that alternative. To this cost is
205 added the cost of the alternative.
207 The cost of each class for this insn is its lowest cost among all
208 the alternatives. */
209 static void
210 record_reg_classes (int n_alts, int n_ops, rtx *ops,
211 enum machine_mode *modes, const char **constraints,
212 rtx insn, struct costs **op_costs,
213 enum reg_class *pref)
215 int alt;
216 int i, j, k;
217 rtx set;
218 int insn_allows_mem[MAX_RECOG_OPERANDS];
220 for (i = 0; i < n_ops; i++)
221 insn_allows_mem[i] = 0;
223 /* Process each alternative, each time minimizing an operand's cost
224 with the cost for each operand in that alternative. */
225 for (alt = 0; alt < n_alts; alt++)
227 enum reg_class classes[MAX_RECOG_OPERANDS];
228 int allows_mem[MAX_RECOG_OPERANDS];
229 enum reg_class rclass;
230 int alt_fail = 0;
231 int alt_cost = 0, op_cost_add;
233 if (!recog_data.alternative_enabled_p[alt])
235 for (i = 0; i < recog_data.n_operands; i++)
236 constraints[i] = skip_alternative (constraints[i]);
238 continue;
241 for (i = 0; i < n_ops; i++)
243 unsigned char c;
244 const char *p = constraints[i];
245 rtx op = ops[i];
246 enum machine_mode mode = modes[i];
247 int allows_addr = 0;
248 int win = 0;
250 /* Initially show we know nothing about the register class. */
251 classes[i] = NO_REGS;
252 allows_mem[i] = 0;
254 /* If this operand has no constraints at all, we can
255 conclude nothing about it since anything is valid. */
256 if (*p == 0)
258 if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
259 memset (this_op_costs[i], 0, struct_costs_size);
260 continue;
263 /* If this alternative is only relevant when this operand
264 matches a previous operand, we do different things
265 depending on whether this operand is a allocno-reg or not.
266 We must process any modifiers for the operand before we
267 can make this test. */
268 while (*p == '%' || *p == '=' || *p == '+' || *p == '&')
269 p++;
271 if (p[0] >= '0' && p[0] <= '0' + i && (p[1] == ',' || p[1] == 0))
273 /* Copy class and whether memory is allowed from the
274 matching alternative. Then perform any needed cost
275 computations and/or adjustments. */
276 j = p[0] - '0';
277 classes[i] = classes[j];
278 allows_mem[i] = allows_mem[j];
279 if (allows_mem[i])
280 insn_allows_mem[i] = 1;
282 if (! REG_P (op) || REGNO (op) < FIRST_PSEUDO_REGISTER)
284 /* If this matches the other operand, we have no
285 added cost and we win. */
286 if (rtx_equal_p (ops[j], op))
287 win = 1;
288 /* If we can put the other operand into a register,
289 add to the cost of this alternative the cost to
290 copy this operand to the register used for the
291 other operand. */
292 else if (classes[j] != NO_REGS)
294 alt_cost += copy_cost (op, mode, classes[j], 1, NULL);
295 win = 1;
298 else if (! REG_P (ops[j])
299 || REGNO (ops[j]) < FIRST_PSEUDO_REGISTER)
301 /* This op is an allocno but the one it matches is
302 not. */
304 /* If we can't put the other operand into a
305 register, this alternative can't be used. */
307 if (classes[j] == NO_REGS)
308 alt_fail = 1;
309 /* Otherwise, add to the cost of this alternative
310 the cost to copy the other operand to the hard
311 register used for this operand. */
312 else
313 alt_cost += copy_cost (ops[j], mode, classes[j], 1, NULL);
315 else
317 /* The costs of this operand are not the same as the
318 other operand since move costs are not symmetric.
319 Moreover, if we cannot tie them, this alternative
320 needs to do a copy, which is one insn. */
321 struct costs *pp = this_op_costs[i];
323 for (k = 0; k < cost_classes_num; k++)
325 rclass = cost_classes[k];
326 pp->cost[k]
327 = (((recog_data.operand_type[i] != OP_OUT
328 ? ira_get_may_move_cost (mode, rclass,
329 classes[i], true) : 0)
330 + (recog_data.operand_type[i] != OP_IN
331 ? ira_get_may_move_cost (mode, classes[i],
332 rclass, false) : 0))
333 * frequency);
336 /* If the alternative actually allows memory, make
337 things a bit cheaper since we won't need an extra
338 insn to load it. */
339 pp->mem_cost
340 = ((recog_data.operand_type[i] != OP_IN
341 ? ira_memory_move_cost[mode][classes[i]][0] : 0)
342 + (recog_data.operand_type[i] != OP_OUT
343 ? ira_memory_move_cost[mode][classes[i]][1] : 0)
344 - allows_mem[i]) * frequency;
346 /* If we have assigned a class to this allocno in our
347 first pass, add a cost to this alternative
348 corresponding to what we would add if this allocno
349 were not in the appropriate class. We could use
350 cover class here but it is less accurate
351 approximation. */
352 if (pref)
354 enum reg_class pref_class = pref[COST_INDEX (REGNO (op))];
356 if (pref_class == NO_REGS)
357 alt_cost
358 += ((recog_data.operand_type[i] != OP_IN
359 ? ira_memory_move_cost[mode][classes[i]][0]
360 : 0)
361 + (recog_data.operand_type[i] != OP_OUT
362 ? ira_memory_move_cost[mode][classes[i]][1]
363 : 0));
364 else if (ira_reg_class_intersect
365 [pref_class][classes[i]] == NO_REGS)
366 alt_cost += ira_get_register_move_cost (mode,
367 pref_class,
368 classes[i]);
370 if (REGNO (ops[i]) != REGNO (ops[j])
371 && ! find_reg_note (insn, REG_DEAD, op))
372 alt_cost += 2;
374 /* This is in place of ordinary cost computation for
375 this operand, so skip to the end of the
376 alternative (should be just one character). */
377 while (*p && *p++ != ',')
380 constraints[i] = p;
381 continue;
385 /* Scan all the constraint letters. See if the operand
386 matches any of the constraints. Collect the valid
387 register classes and see if this operand accepts
388 memory. */
389 while ((c = *p))
391 switch (c)
393 case ',':
394 break;
395 case '*':
396 /* Ignore the next letter for this pass. */
397 c = *++p;
398 break;
400 case '?':
401 alt_cost += 2;
402 case '!': case '#': case '&':
403 case '0': case '1': case '2': case '3': case '4':
404 case '5': case '6': case '7': case '8': case '9':
405 break;
407 case 'p':
408 allows_addr = 1;
409 win = address_operand (op, GET_MODE (op));
410 /* We know this operand is an address, so we want it
411 to be allocated to a register that can be the
412 base of an address, i.e. BASE_REG_CLASS. */
413 classes[i]
414 = ira_reg_class_union[classes[i]]
415 [base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
416 break;
418 case 'm': case 'o': case 'V':
419 /* It doesn't seem worth distinguishing between
420 offsettable and non-offsettable addresses
421 here. */
422 insn_allows_mem[i] = allows_mem[i] = 1;
423 if (MEM_P (op))
424 win = 1;
425 break;
427 case '<':
428 if (MEM_P (op)
429 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
430 || GET_CODE (XEXP (op, 0)) == POST_DEC))
431 win = 1;
432 break;
434 case '>':
435 if (MEM_P (op)
436 && (GET_CODE (XEXP (op, 0)) == PRE_INC
437 || GET_CODE (XEXP (op, 0)) == POST_INC))
438 win = 1;
439 break;
441 case 'E':
442 case 'F':
443 if (GET_CODE (op) == CONST_DOUBLE
444 || (GET_CODE (op) == CONST_VECTOR
445 && (GET_MODE_CLASS (GET_MODE (op))
446 == MODE_VECTOR_FLOAT)))
447 win = 1;
448 break;
450 case 'G':
451 case 'H':
452 if (GET_CODE (op) == CONST_DOUBLE
453 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
454 win = 1;
455 break;
457 case 's':
458 if (CONST_INT_P (op)
459 || (GET_CODE (op) == CONST_DOUBLE
460 && GET_MODE (op) == VOIDmode))
461 break;
463 case 'i':
464 if (CONSTANT_P (op)
465 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op)))
466 win = 1;
467 break;
469 case 'n':
470 if (CONST_INT_P (op)
471 || (GET_CODE (op) == CONST_DOUBLE
472 && GET_MODE (op) == VOIDmode))
473 win = 1;
474 break;
476 case 'I':
477 case 'J':
478 case 'K':
479 case 'L':
480 case 'M':
481 case 'N':
482 case 'O':
483 case 'P':
484 if (CONST_INT_P (op)
485 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
486 win = 1;
487 break;
489 case 'X':
490 win = 1;
491 break;
493 case 'g':
494 if (MEM_P (op)
495 || (CONSTANT_P (op)
496 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))))
497 win = 1;
498 insn_allows_mem[i] = allows_mem[i] = 1;
499 case 'r':
500 classes[i] = ira_reg_class_union[classes[i]][GENERAL_REGS];
501 break;
503 default:
504 if (REG_CLASS_FROM_CONSTRAINT (c, p) != NO_REGS)
505 classes[i] = ira_reg_class_union[classes[i]]
506 [REG_CLASS_FROM_CONSTRAINT (c, p)];
507 #ifdef EXTRA_CONSTRAINT_STR
508 else if (EXTRA_CONSTRAINT_STR (op, c, p))
509 win = 1;
511 if (EXTRA_MEMORY_CONSTRAINT (c, p))
513 /* Every MEM can be reloaded to fit. */
514 insn_allows_mem[i] = allows_mem[i] = 1;
515 if (MEM_P (op))
516 win = 1;
518 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
520 /* Every address can be reloaded to fit. */
521 allows_addr = 1;
522 if (address_operand (op, GET_MODE (op)))
523 win = 1;
524 /* We know this operand is an address, so we
525 want it to be allocated to a hard register
526 that can be the base of an address,
527 i.e. BASE_REG_CLASS. */
528 classes[i]
529 = ira_reg_class_union[classes[i]]
530 [base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
532 #endif
533 break;
535 p += CONSTRAINT_LEN (c, p);
536 if (c == ',')
537 break;
540 constraints[i] = p;
542 /* How we account for this operand now depends on whether it
543 is a pseudo register or not. If it is, we first check if
544 any register classes are valid. If not, we ignore this
545 alternative, since we want to assume that all allocnos get
546 allocated for register preferencing. If some register
547 class is valid, compute the costs of moving the allocno
548 into that class. */
549 if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
551 if (classes[i] == NO_REGS)
553 /* We must always fail if the operand is a REG, but
554 we did not find a suitable class.
556 Otherwise we may perform an uninitialized read
557 from this_op_costs after the `continue' statement
558 below. */
559 alt_fail = 1;
561 else
563 struct costs *pp = this_op_costs[i];
565 for (k = 0; k < cost_classes_num; k++)
567 rclass = cost_classes[k];
568 pp->cost[k]
569 = (((recog_data.operand_type[i] != OP_OUT
570 ? ira_get_may_move_cost (mode, rclass,
571 classes[i], true) : 0)
572 + (recog_data.operand_type[i] != OP_IN
573 ? ira_get_may_move_cost (mode, classes[i],
574 rclass, false) : 0))
575 * frequency);
578 /* If the alternative actually allows memory, make
579 things a bit cheaper since we won't need an extra
580 insn to load it. */
581 pp->mem_cost
582 = ((recog_data.operand_type[i] != OP_IN
583 ? ira_memory_move_cost[mode][classes[i]][0] : 0)
584 + (recog_data.operand_type[i] != OP_OUT
585 ? ira_memory_move_cost[mode][classes[i]][1] : 0)
586 - allows_mem[i]) * frequency;
587 /* If we have assigned a class to this allocno in our
588 first pass, add a cost to this alternative
589 corresponding to what we would add if this allocno
590 were not in the appropriate class. We could use
591 cover class here but it is less accurate
592 approximation. */
593 if (pref)
595 enum reg_class pref_class = pref[COST_INDEX (REGNO (op))];
597 if (pref_class == NO_REGS)
598 alt_cost
599 += ((recog_data.operand_type[i] != OP_IN
600 ? ira_memory_move_cost[mode][classes[i]][0]
601 : 0)
602 + (recog_data.operand_type[i] != OP_OUT
603 ? ira_memory_move_cost[mode][classes[i]][1]
604 : 0));
605 else if (ira_reg_class_intersect[pref_class][classes[i]]
606 == NO_REGS)
607 alt_cost += ira_get_register_move_cost (mode,
608 pref_class,
609 classes[i]);
614 /* Otherwise, if this alternative wins, either because we
615 have already determined that or if we have a hard
616 register of the proper class, there is no cost for this
617 alternative. */
618 else if (win || (REG_P (op)
619 && reg_fits_class_p (op, classes[i],
620 0, GET_MODE (op))))
623 /* If registers are valid, the cost of this alternative
624 includes copying the object to and/or from a
625 register. */
626 else if (classes[i] != NO_REGS)
628 if (recog_data.operand_type[i] != OP_OUT)
629 alt_cost += copy_cost (op, mode, classes[i], 1, NULL);
631 if (recog_data.operand_type[i] != OP_IN)
632 alt_cost += copy_cost (op, mode, classes[i], 0, NULL);
634 /* The only other way this alternative can be used is if
635 this is a constant that could be placed into memory. */
636 else if (CONSTANT_P (op) && (allows_addr || allows_mem[i]))
637 alt_cost += ira_memory_move_cost[mode][classes[i]][1];
638 else
639 alt_fail = 1;
642 if (alt_fail)
643 continue;
645 op_cost_add = alt_cost * frequency;
646 /* Finally, update the costs with the information we've
647 calculated about this alternative. */
648 for (i = 0; i < n_ops; i++)
649 if (REG_P (ops[i]) && REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
651 struct costs *pp = op_costs[i], *qq = this_op_costs[i];
652 int scale = 1 + (recog_data.operand_type[i] == OP_INOUT);
654 pp->mem_cost = MIN (pp->mem_cost,
655 (qq->mem_cost + op_cost_add) * scale);
657 for (k = 0; k < cost_classes_num; k++)
658 pp->cost[k]
659 = MIN (pp->cost[k], (qq->cost[k] + op_cost_add) * scale);
663 if (allocno_p)
664 for (i = 0; i < n_ops; i++)
666 ira_allocno_t a;
667 rtx op = ops[i];
669 if (! REG_P (op) || REGNO (op) < FIRST_PSEUDO_REGISTER)
670 continue;
671 a = ira_curr_regno_allocno_map [REGNO (op)];
672 if (! ALLOCNO_BAD_SPILL_P (a) && insn_allows_mem[i] == 0)
673 ALLOCNO_BAD_SPILL_P (a) = true;
676 /* If this insn is a single set copying operand 1 to operand 0 and
677 one operand is an allocno with the other a hard reg or an allocno
678 that prefers a hard register that is in its own register class
679 then we may want to adjust the cost of that register class to -1.
681 Avoid the adjustment if the source does not die to avoid
682 stressing of register allocator by preferrencing two colliding
683 registers into single class.
685 Also avoid the adjustment if a copy between hard registers of the
686 class is expensive (ten times the cost of a default copy is
687 considered arbitrarily expensive). This avoids losing when the
688 preferred class is very expensive as the source of a copy
689 instruction. */
690 if ((set = single_set (insn)) != 0
691 && ops[0] == SET_DEST (set) && ops[1] == SET_SRC (set)
692 && REG_P (ops[0]) && REG_P (ops[1])
693 && find_regno_note (insn, REG_DEAD, REGNO (ops[1])))
694 for (i = 0; i <= 1; i++)
695 if (REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
697 unsigned int regno = REGNO (ops[!i]);
698 enum machine_mode mode = GET_MODE (ops[!i]);
699 enum reg_class rclass;
700 unsigned int nr;
702 if (regno < FIRST_PSEUDO_REGISTER)
703 for (k = 0; k < cost_classes_num; k++)
705 rclass = cost_classes[k];
706 if (TEST_HARD_REG_BIT (reg_class_contents[rclass], regno)
707 && (reg_class_size[rclass]
708 == (unsigned) CLASS_MAX_NREGS (rclass, mode)))
710 if (reg_class_size[rclass] == 1)
711 op_costs[i]->cost[k] = -frequency;
712 else
714 for (nr = 0;
715 nr < (unsigned) hard_regno_nregs[regno][mode];
716 nr++)
717 if (! TEST_HARD_REG_BIT (reg_class_contents[rclass],
718 regno + nr))
719 break;
721 if (nr == (unsigned) hard_regno_nregs[regno][mode])
722 op_costs[i]->cost[k] = -frequency;
731 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudo registers. */
732 static inline bool
733 ok_for_index_p_nonstrict (rtx reg)
735 unsigned regno = REGNO (reg);
737 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
740 /* A version of regno_ok_for_base_p for use here, when all
741 pseudo-registers should count as OK. Arguments as for
742 regno_ok_for_base_p. */
743 static inline bool
744 ok_for_base_p_nonstrict (rtx reg, enum machine_mode mode,
745 enum rtx_code outer_code, enum rtx_code index_code)
747 unsigned regno = REGNO (reg);
749 if (regno >= FIRST_PSEUDO_REGISTER)
750 return true;
751 return ok_for_base_p_1 (regno, mode, outer_code, index_code);
754 /* Record the pseudo registers we must reload into hard registers in a
755 subexpression of a memory address, X.
757 If CONTEXT is 0, we are looking at the base part of an address,
758 otherwise we are looking at the index part.
760 MODE is the mode of the memory reference; OUTER_CODE and INDEX_CODE
761 give the context that the rtx appears in. These three arguments
762 are passed down to base_reg_class.
764 SCALE is twice the amount to multiply the cost by (it is twice so
765 we can represent half-cost adjustments). */
766 static void
767 record_address_regs (enum machine_mode mode, rtx x, int context,
768 enum rtx_code outer_code, enum rtx_code index_code,
769 int scale)
771 enum rtx_code code = GET_CODE (x);
772 enum reg_class rclass;
774 if (context == 1)
775 rclass = INDEX_REG_CLASS;
776 else
777 rclass = base_reg_class (mode, outer_code, index_code);
779 switch (code)
781 case CONST_INT:
782 case CONST:
783 case CC0:
784 case PC:
785 case SYMBOL_REF:
786 case LABEL_REF:
787 return;
789 case PLUS:
790 /* When we have an address that is a sum, we must determine
791 whether registers are "base" or "index" regs. If there is a
792 sum of two registers, we must choose one to be the "base".
793 Luckily, we can use the REG_POINTER to make a good choice
794 most of the time. We only need to do this on machines that
795 can have two registers in an address and where the base and
796 index register classes are different.
798 ??? This code used to set REGNO_POINTER_FLAG in some cases,
799 but that seems bogus since it should only be set when we are
800 sure the register is being used as a pointer. */
802 rtx arg0 = XEXP (x, 0);
803 rtx arg1 = XEXP (x, 1);
804 enum rtx_code code0 = GET_CODE (arg0);
805 enum rtx_code code1 = GET_CODE (arg1);
807 /* Look inside subregs. */
808 if (code0 == SUBREG)
809 arg0 = SUBREG_REG (arg0), code0 = GET_CODE (arg0);
810 if (code1 == SUBREG)
811 arg1 = SUBREG_REG (arg1), code1 = GET_CODE (arg1);
813 /* If this machine only allows one register per address, it
814 must be in the first operand. */
815 if (MAX_REGS_PER_ADDRESS == 1)
816 record_address_regs (mode, arg0, 0, PLUS, code1, scale);
818 /* If index and base registers are the same on this machine,
819 just record registers in any non-constant operands. We
820 assume here, as well as in the tests below, that all
821 addresses are in canonical form. */
822 else if (INDEX_REG_CLASS == base_reg_class (VOIDmode, PLUS, SCRATCH))
824 record_address_regs (mode, arg0, context, PLUS, code1, scale);
825 if (! CONSTANT_P (arg1))
826 record_address_regs (mode, arg1, context, PLUS, code0, scale);
829 /* If the second operand is a constant integer, it doesn't
830 change what class the first operand must be. */
831 else if (code1 == CONST_INT || code1 == CONST_DOUBLE)
832 record_address_regs (mode, arg0, context, PLUS, code1, scale);
833 /* If the second operand is a symbolic constant, the first
834 operand must be an index register. */
835 else if (code1 == SYMBOL_REF || code1 == CONST || code1 == LABEL_REF)
836 record_address_regs (mode, arg0, 1, PLUS, code1, scale);
837 /* If both operands are registers but one is already a hard
838 register of index or reg-base class, give the other the
839 class that the hard register is not. */
840 else if (code0 == REG && code1 == REG
841 && REGNO (arg0) < FIRST_PSEUDO_REGISTER
842 && (ok_for_base_p_nonstrict (arg0, mode, PLUS, REG)
843 || ok_for_index_p_nonstrict (arg0)))
844 record_address_regs (mode, arg1,
845 ok_for_base_p_nonstrict (arg0, mode, PLUS, REG)
846 ? 1 : 0,
847 PLUS, REG, scale);
848 else if (code0 == REG && code1 == REG
849 && REGNO (arg1) < FIRST_PSEUDO_REGISTER
850 && (ok_for_base_p_nonstrict (arg1, mode, PLUS, REG)
851 || ok_for_index_p_nonstrict (arg1)))
852 record_address_regs (mode, arg0,
853 ok_for_base_p_nonstrict (arg1, mode, PLUS, REG)
854 ? 1 : 0,
855 PLUS, REG, scale);
856 /* If one operand is known to be a pointer, it must be the
857 base with the other operand the index. Likewise if the
858 other operand is a MULT. */
859 else if ((code0 == REG && REG_POINTER (arg0)) || code1 == MULT)
861 record_address_regs (mode, arg0, 0, PLUS, code1, scale);
862 record_address_regs (mode, arg1, 1, PLUS, code0, scale);
864 else if ((code1 == REG && REG_POINTER (arg1)) || code0 == MULT)
866 record_address_regs (mode, arg0, 1, PLUS, code1, scale);
867 record_address_regs (mode, arg1, 0, PLUS, code0, scale);
869 /* Otherwise, count equal chances that each might be a base or
870 index register. This case should be rare. */
871 else
873 record_address_regs (mode, arg0, 0, PLUS, code1, scale / 2);
874 record_address_regs (mode, arg0, 1, PLUS, code1, scale / 2);
875 record_address_regs (mode, arg1, 0, PLUS, code0, scale / 2);
876 record_address_regs (mode, arg1, 1, PLUS, code0, scale / 2);
879 break;
881 /* Double the importance of an allocno that is incremented or
882 decremented, since it would take two extra insns if it ends
883 up in the wrong place. */
884 case POST_MODIFY:
885 case PRE_MODIFY:
886 record_address_regs (mode, XEXP (x, 0), 0, code,
887 GET_CODE (XEXP (XEXP (x, 1), 1)), 2 * scale);
888 if (REG_P (XEXP (XEXP (x, 1), 1)))
889 record_address_regs (mode, XEXP (XEXP (x, 1), 1), 1, code, REG,
890 2 * scale);
891 break;
893 case POST_INC:
894 case PRE_INC:
895 case POST_DEC:
896 case PRE_DEC:
897 /* Double the importance of an allocno that is incremented or
898 decremented, since it would take two extra insns if it ends
899 up in the wrong place. If the operand is a pseudo-register,
900 show it is being used in an INC_DEC context. */
901 #ifdef FORBIDDEN_INC_DEC_CLASSES
902 if (REG_P (XEXP (x, 0))
903 && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER)
904 in_inc_dec[COST_INDEX (REGNO (XEXP (x, 0)))] = true;
905 #endif
906 record_address_regs (mode, XEXP (x, 0), 0, code, SCRATCH, 2 * scale);
907 break;
909 case REG:
911 struct costs *pp;
912 enum reg_class i;
913 int k;
915 if (REGNO (x) < FIRST_PSEUDO_REGISTER)
916 break;
918 if (allocno_p)
919 ALLOCNO_BAD_SPILL_P (ira_curr_regno_allocno_map[REGNO (x)]) = true;
920 pp = COSTS (costs, COST_INDEX (REGNO (x)));
921 pp->mem_cost += (ira_memory_move_cost[Pmode][rclass][1] * scale) / 2;
922 for (k = 0; k < cost_classes_num; k++)
924 i = cost_classes[k];
925 pp->cost[k]
926 += (ira_get_may_move_cost (Pmode, i, rclass, true) * scale) / 2;
929 break;
931 default:
933 const char *fmt = GET_RTX_FORMAT (code);
934 int i;
935 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
936 if (fmt[i] == 'e')
937 record_address_regs (mode, XEXP (x, i), context, code, SCRATCH,
938 scale);
945 /* Calculate the costs of insn operands. */
946 static void
947 record_operand_costs (rtx insn, struct costs **op_costs, enum reg_class *pref)
949 const char *constraints[MAX_RECOG_OPERANDS];
950 enum machine_mode modes[MAX_RECOG_OPERANDS];
951 int i;
953 for (i = 0; i < recog_data.n_operands; i++)
955 constraints[i] = recog_data.constraints[i];
956 modes[i] = recog_data.operand_mode[i];
959 /* If we get here, we are set up to record the costs of all the
960 operands for this insn. Start by initializing the costs. Then
961 handle any address registers. Finally record the desired classes
962 for any allocnos, doing it twice if some pair of operands are
963 commutative. */
964 for (i = 0; i < recog_data.n_operands; i++)
966 memcpy (op_costs[i], init_cost, struct_costs_size);
968 if (GET_CODE (recog_data.operand[i]) == SUBREG)
969 recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
971 if (MEM_P (recog_data.operand[i]))
972 record_address_regs (GET_MODE (recog_data.operand[i]),
973 XEXP (recog_data.operand[i], 0),
974 0, MEM, SCRATCH, frequency * 2);
975 else if (constraints[i][0] == 'p'
976 || EXTRA_ADDRESS_CONSTRAINT (constraints[i][0],
977 constraints[i]))
978 record_address_regs (VOIDmode, recog_data.operand[i], 0, ADDRESS,
979 SCRATCH, frequency * 2);
982 /* Check for commutative in a separate loop so everything will have
983 been initialized. We must do this even if one operand is a
984 constant--see addsi3 in m68k.md. */
985 for (i = 0; i < (int) recog_data.n_operands - 1; i++)
986 if (constraints[i][0] == '%')
988 const char *xconstraints[MAX_RECOG_OPERANDS];
989 int j;
991 /* Handle commutative operands by swapping the constraints.
992 We assume the modes are the same. */
993 for (j = 0; j < recog_data.n_operands; j++)
994 xconstraints[j] = constraints[j];
996 xconstraints[i] = constraints[i+1];
997 xconstraints[i+1] = constraints[i];
998 record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
999 recog_data.operand, modes,
1000 xconstraints, insn, op_costs, pref);
1002 record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
1003 recog_data.operand, modes,
1004 constraints, insn, op_costs, pref);
1009 /* Process one insn INSN. Scan it and record each time it would save
1010 code to put a certain allocnos in a certain class. Return the last
1011 insn processed, so that the scan can be continued from there. */
1012 static rtx
1013 scan_one_insn (rtx insn)
1015 enum rtx_code pat_code;
1016 rtx set, note;
1017 int i, k;
1019 if (!NONDEBUG_INSN_P (insn))
1020 return insn;
1022 pat_code = GET_CODE (PATTERN (insn));
1023 if (pat_code == USE || pat_code == CLOBBER || pat_code == ASM_INPUT
1024 || pat_code == ADDR_VEC || pat_code == ADDR_DIFF_VEC)
1025 return insn;
1027 set = single_set (insn);
1028 extract_insn (insn);
1030 /* If this insn loads a parameter from its stack slot, then it
1031 represents a savings, rather than a cost, if the parameter is
1032 stored in memory. Record this fact. */
1033 if (set != 0 && REG_P (SET_DEST (set)) && MEM_P (SET_SRC (set))
1034 && (note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != NULL_RTX
1035 && MEM_P (XEXP (note, 0)))
1037 enum reg_class cl = GENERAL_REGS;
1038 rtx reg = SET_DEST (set);
1039 int num = COST_INDEX (REGNO (reg));
1041 if (pref)
1042 cl = pref[num];
1043 COSTS (costs, num)->mem_cost
1044 -= ira_memory_move_cost[GET_MODE (reg)][cl][1] * frequency;
1045 record_address_regs (GET_MODE (SET_SRC (set)), XEXP (SET_SRC (set), 0),
1046 0, MEM, SCRATCH, frequency * 2);
1049 record_operand_costs (insn, op_costs, pref);
1051 /* Now add the cost for each operand to the total costs for its
1052 allocno. */
1053 for (i = 0; i < recog_data.n_operands; i++)
1054 if (REG_P (recog_data.operand[i])
1055 && REGNO (recog_data.operand[i]) >= FIRST_PSEUDO_REGISTER)
1057 int regno = REGNO (recog_data.operand[i]);
1058 struct costs *p = COSTS (costs, COST_INDEX (regno));
1059 struct costs *q = op_costs[i];
1061 p->mem_cost += q->mem_cost;
1062 for (k = 0; k < cost_classes_num; k++)
1063 p->cost[k] += q->cost[k];
1066 return insn;
1071 /* Print allocnos costs to file F. */
1072 static void
1073 print_allocno_costs (FILE *f)
1075 int k;
1076 ira_allocno_t a;
1077 ira_allocno_iterator ai;
1079 ira_assert (allocno_p);
1080 fprintf (f, "\n");
1081 FOR_EACH_ALLOCNO (a, ai)
1083 int i, rclass;
1084 basic_block bb;
1085 int regno = ALLOCNO_REGNO (a);
1087 i = ALLOCNO_NUM (a);
1088 fprintf (f, " a%d(r%d,", i, regno);
1089 if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
1090 fprintf (f, "b%d", bb->index);
1091 else
1092 fprintf (f, "l%d", ALLOCNO_LOOP_TREE_NODE (a)->loop->num);
1093 fprintf (f, ") costs:");
1094 for (k = 0; k < cost_classes_num; k++)
1096 rclass = cost_classes[k];
1097 if (contains_reg_of_mode[rclass][PSEUDO_REGNO_MODE (regno)]
1098 #ifdef FORBIDDEN_INC_DEC_CLASSES
1099 && (! in_inc_dec[i] || ! forbidden_inc_dec_class[rclass])
1100 #endif
1101 #ifdef CANNOT_CHANGE_MODE_CLASS
1102 && ! invalid_mode_change_p (regno, (enum reg_class) rclass,
1103 PSEUDO_REGNO_MODE (regno))
1104 #endif
1107 fprintf (f, " %s:%d", reg_class_names[rclass],
1108 COSTS (costs, i)->cost[k]);
1109 if (flag_ira_region == IRA_REGION_ALL
1110 || flag_ira_region == IRA_REGION_MIXED)
1111 fprintf (f, ",%d", COSTS (total_allocno_costs, i)->cost[k]);
1114 fprintf (f, " MEM:%i\n", COSTS (costs, i)->mem_cost);
1118 /* Print pseudo costs to file F. */
1119 static void
1120 print_pseudo_costs (FILE *f)
1122 int regno, k;
1123 int rclass;
1125 ira_assert (! allocno_p);
1126 fprintf (f, "\n");
1127 for (regno = max_reg_num () - 1; regno >= FIRST_PSEUDO_REGISTER; regno--)
1129 if (regno_reg_rtx[regno] == NULL_RTX)
1130 continue;
1131 fprintf (f, " r%d costs:", regno);
1132 for (k = 0; k < cost_classes_num; k++)
1134 rclass = cost_classes[k];
1135 if (contains_reg_of_mode[rclass][PSEUDO_REGNO_MODE (regno)]
1136 #ifdef FORBIDDEN_INC_DEC_CLASSES
1137 && (! in_inc_dec[regno] || ! forbidden_inc_dec_class[rclass])
1138 #endif
1139 #ifdef CANNOT_CHANGE_MODE_CLASS
1140 && ! invalid_mode_change_p (regno, (enum reg_class) rclass,
1141 PSEUDO_REGNO_MODE (regno))
1142 #endif
1144 fprintf (f, " %s:%d", reg_class_names[rclass],
1145 COSTS (costs, regno)->cost[k]);
1147 fprintf (f, " MEM:%i\n", COSTS (costs, regno)->mem_cost);
1151 /* Traverse the BB represented by LOOP_TREE_NODE to update the allocno
1152 costs. */
1153 static void
1154 process_bb_for_costs (basic_block bb)
1156 rtx insn;
1158 frequency = REG_FREQ_FROM_BB (bb);
1159 if (frequency == 0)
1160 frequency = 1;
1161 FOR_BB_INSNS (bb, insn)
1162 insn = scan_one_insn (insn);
1165 /* Traverse the BB represented by LOOP_TREE_NODE to update the allocno
1166 costs. */
1167 static void
1168 process_bb_node_for_costs (ira_loop_tree_node_t loop_tree_node)
1170 basic_block bb;
1172 bb = loop_tree_node->bb;
1173 if (bb != NULL)
1174 process_bb_for_costs (bb);
1177 /* Find costs of register classes and memory for allocnos or pseudos
1178 and their best costs. Set up preferred, alternative and cover
1179 classes for pseudos. */
1180 static void
1181 find_costs_and_classes (FILE *dump_file)
1183 int i, k, start;
1184 int pass;
1185 basic_block bb;
1187 init_recog ();
1188 #ifdef FORBIDDEN_INC_DEC_CLASSES
1189 in_inc_dec = ira_allocate (sizeof (bool) * cost_elements_num);
1190 #endif /* FORBIDDEN_INC_DEC_CLASSES */
1191 pref = NULL;
1192 start = 0;
1193 if (!resize_reg_info () && allocno_p && pseudo_classes_defined_p)
1195 ira_allocno_t a;
1196 ira_allocno_iterator ai;
1198 pref = pref_buffer;
1199 FOR_EACH_ALLOCNO (a, ai)
1200 pref[ALLOCNO_NUM (a)] = reg_preferred_class (ALLOCNO_REGNO (a));
1201 if (flag_expensive_optimizations)
1202 start = 1;
1204 if (allocno_p)
1205 /* Clear the flag for the next compiled function. */
1206 pseudo_classes_defined_p = false;
1207 /* Normally we scan the insns once and determine the best class to
1208 use for each allocno. However, if -fexpensive-optimizations are
1209 on, we do so twice, the second time using the tentative best
1210 classes to guide the selection. */
1211 for (pass = start; pass <= flag_expensive_optimizations; pass++)
1213 if ((!allocno_p || internal_flag_ira_verbose > 0) && dump_file)
1214 fprintf (dump_file,
1215 "\nPass %i for finding pseudo/allocno costs\n\n", pass);
1216 /* We could use only cover classes. Unfortunately it does not
1217 work well for some targets where some subclass of cover class
1218 is costly and wrong cover class is chosen. */
1219 for (i = 0; i < N_REG_CLASSES; i++)
1220 cost_class_nums[i] = -1;
1221 for (cost_classes_num = 0;
1222 cost_classes_num < ira_important_classes_num;
1223 cost_classes_num++)
1225 cost_classes[cost_classes_num]
1226 = ira_important_classes[cost_classes_num];
1227 cost_class_nums[cost_classes[cost_classes_num]]
1228 = cost_classes_num;
1230 struct_costs_size
1231 = sizeof (struct costs) + sizeof (int) * (cost_classes_num - 1);
1232 /* Zero out our accumulation of the cost of each class for each
1233 allocno. */
1234 memset (costs, 0, cost_elements_num * struct_costs_size);
1235 #ifdef FORBIDDEN_INC_DEC_CLASSES
1236 memset (in_inc_dec, 0, cost_elements_num * sizeof (bool));
1237 #endif
1239 if (allocno_p)
1241 /* Scan the instructions and record each time it would save code
1242 to put a certain allocno in a certain class. */
1243 ira_traverse_loop_tree (true, ira_loop_tree_root,
1244 process_bb_node_for_costs, NULL);
1246 memcpy (total_allocno_costs, costs,
1247 max_struct_costs_size * ira_allocnos_num);
1249 else
1251 basic_block bb;
1253 FOR_EACH_BB (bb)
1254 process_bb_for_costs (bb);
1257 if (pass == 0)
1258 pref = pref_buffer;
1260 /* Now for each allocno look at how desirable each class is and
1261 find which class is preferred. */
1262 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
1264 ira_allocno_t a, parent_a;
1265 int rclass, a_num, parent_a_num;
1266 ira_loop_tree_node_t parent;
1267 int best_cost, allocno_cost;
1268 enum reg_class best, alt_class;
1269 #ifdef FORBIDDEN_INC_DEC_CLASSES
1270 int inc_dec_p = false;
1271 #endif
1272 int equiv_savings = regno_equiv_gains[i];
1274 if (! allocno_p)
1276 if (regno_reg_rtx[i] == NULL_RTX)
1277 continue;
1278 #ifdef FORBIDDEN_INC_DEC_CLASSES
1279 inc_dec_p = in_inc_dec[i];
1280 #endif
1281 memcpy (temp_costs, COSTS (costs, i), struct_costs_size);
1283 else
1285 if (ira_regno_allocno_map[i] == NULL)
1286 continue;
1287 memset (temp_costs, 0, struct_costs_size);
1288 /* Find cost of all allocnos with the same regno. */
1289 for (a = ira_regno_allocno_map[i];
1290 a != NULL;
1291 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
1293 a_num = ALLOCNO_NUM (a);
1294 if ((flag_ira_region == IRA_REGION_ALL
1295 || flag_ira_region == IRA_REGION_MIXED)
1296 && (parent = ALLOCNO_LOOP_TREE_NODE (a)->parent) != NULL
1297 && (parent_a = parent->regno_allocno_map[i]) != NULL
1298 /* There are no caps yet. */
1299 && bitmap_bit_p (ALLOCNO_LOOP_TREE_NODE
1300 (a)->border_allocnos,
1301 ALLOCNO_NUM (a)))
1303 /* Propagate costs to upper levels in the region
1304 tree. */
1305 parent_a_num = ALLOCNO_NUM (parent_a);
1306 for (k = 0; k < cost_classes_num; k++)
1307 COSTS (total_allocno_costs, parent_a_num)->cost[k]
1308 += COSTS (total_allocno_costs, a_num)->cost[k];
1309 COSTS (total_allocno_costs, parent_a_num)->mem_cost
1310 += COSTS (total_allocno_costs, a_num)->mem_cost;
1312 for (k = 0; k < cost_classes_num; k++)
1313 temp_costs->cost[k] += COSTS (costs, a_num)->cost[k];
1314 temp_costs->mem_cost += COSTS (costs, a_num)->mem_cost;
1315 #ifdef FORBIDDEN_INC_DEC_CLASSES
1316 if (in_inc_dec[a_num])
1317 inc_dec_p = true;
1318 #endif
1321 if (equiv_savings < 0)
1322 temp_costs->mem_cost = -equiv_savings;
1323 else if (equiv_savings > 0)
1325 temp_costs->mem_cost = 0;
1326 for (k = 0; k < cost_classes_num; k++)
1327 temp_costs->cost[k] += equiv_savings;
1330 best_cost = (1 << (HOST_BITS_PER_INT - 2)) - 1;
1331 best = ALL_REGS;
1332 alt_class = NO_REGS;
1333 /* Find best common class for all allocnos with the same
1334 regno. */
1335 for (k = 0; k < cost_classes_num; k++)
1337 rclass = cost_classes[k];
1338 /* Ignore classes that are too small for this operand or
1339 invalid for an operand that was auto-incremented. */
1340 if (! contains_reg_of_mode[rclass][PSEUDO_REGNO_MODE (i)]
1341 #ifdef FORBIDDEN_INC_DEC_CLASSES
1342 || (inc_dec_p && forbidden_inc_dec_class[rclass])
1343 #endif
1344 #ifdef CANNOT_CHANGE_MODE_CLASS
1345 || invalid_mode_change_p (i, (enum reg_class) rclass,
1346 PSEUDO_REGNO_MODE (i))
1347 #endif
1349 continue;
1350 if (temp_costs->cost[k] < best_cost)
1352 best_cost = temp_costs->cost[k];
1353 best = (enum reg_class) rclass;
1355 else if (temp_costs->cost[k] == best_cost)
1356 best = ira_reg_class_union[best][rclass];
1357 if (pass == flag_expensive_optimizations
1358 && temp_costs->cost[k] < temp_costs->mem_cost
1359 && (reg_class_size[reg_class_subunion[alt_class][rclass]]
1360 > reg_class_size[alt_class]))
1361 alt_class = reg_class_subunion[alt_class][rclass];
1363 alt_class = ira_class_translate[alt_class];
1364 if (best_cost > temp_costs->mem_cost)
1365 regno_cover_class[i] = NO_REGS;
1366 else if (flag_ira_algorithm == IRA_ALGORITHM_PRIORITY)
1367 /* Make the common class the biggest class of best and
1368 alt_class. */
1369 regno_cover_class[i] = alt_class == NO_REGS ? best : alt_class;
1370 else
1371 /* Make the common class a cover class. Remember all
1372 allocnos with the same regno should have the same cover
1373 class. */
1374 regno_cover_class[i] = ira_class_translate[best];
1375 if (pass == flag_expensive_optimizations)
1377 if (best_cost > temp_costs->mem_cost)
1378 best = alt_class = NO_REGS;
1379 else if (best == alt_class)
1380 alt_class = NO_REGS;
1381 setup_reg_classes (i, best, alt_class, regno_cover_class[i]);
1382 if ((!allocno_p || internal_flag_ira_verbose > 2)
1383 && dump_file != NULL)
1384 fprintf (dump_file,
1385 " r%d: preferred %s, alternative %s, cover %s\n",
1386 i, reg_class_names[best], reg_class_names[alt_class],
1387 reg_class_names[regno_cover_class[i]]);
1389 if (! allocno_p)
1391 pref[i] = best_cost > temp_costs->mem_cost ? NO_REGS : best;
1392 continue;
1394 for (a = ira_regno_allocno_map[i];
1395 a != NULL;
1396 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
1398 a_num = ALLOCNO_NUM (a);
1399 if (regno_cover_class[i] == NO_REGS)
1400 best = NO_REGS;
1401 else
1403 /* Finding best class which is subset of the common
1404 class. */
1405 best_cost = (1 << (HOST_BITS_PER_INT - 2)) - 1;
1406 allocno_cost = best_cost;
1407 best = ALL_REGS;
1408 for (k = 0; k < cost_classes_num; k++)
1410 rclass = cost_classes[k];
1411 if (! ira_class_subset_p[rclass][regno_cover_class[i]])
1412 continue;
1413 /* Ignore classes that are too small for this
1414 operand or invalid for an operand that was
1415 auto-incremented. */
1416 if (! contains_reg_of_mode[rclass][PSEUDO_REGNO_MODE (i)]
1417 #ifdef FORBIDDEN_INC_DEC_CLASSES
1418 || (inc_dec_p && forbidden_inc_dec_class[rclass])
1419 #endif
1420 #ifdef CANNOT_CHANGE_MODE_CLASS
1421 || invalid_mode_change_p (i, (enum reg_class) rclass,
1422 PSEUDO_REGNO_MODE (i))
1423 #endif
1426 else if (COSTS (total_allocno_costs, a_num)->cost[k]
1427 < best_cost)
1429 best_cost
1430 = COSTS (total_allocno_costs, a_num)->cost[k];
1431 allocno_cost = COSTS (costs, a_num)->cost[k];
1432 best = (enum reg_class) rclass;
1434 else if (COSTS (total_allocno_costs, a_num)->cost[k]
1435 == best_cost)
1437 best = ira_reg_class_union[best][rclass];
1438 allocno_cost
1439 = MAX (allocno_cost, COSTS (costs, a_num)->cost[k]);
1442 ALLOCNO_COVER_CLASS_COST (a) = allocno_cost;
1444 ira_assert (flag_ira_algorithm == IRA_ALGORITHM_PRIORITY
1445 || ira_class_translate[best] == regno_cover_class[i]);
1446 if (internal_flag_ira_verbose > 2 && dump_file != NULL
1447 && (pass == 0 || pref[a_num] != best))
1449 fprintf (dump_file, " a%d (r%d,", a_num, i);
1450 if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
1451 fprintf (dump_file, "b%d", bb->index);
1452 else
1453 fprintf (dump_file, "l%d",
1454 ALLOCNO_LOOP_TREE_NODE (a)->loop->num);
1455 fprintf (dump_file, ") best %s, cover %s\n",
1456 reg_class_names[best],
1457 reg_class_names[regno_cover_class[i]]);
1459 pref[a_num] = best;
1463 if (internal_flag_ira_verbose > 4 && dump_file)
1465 if (allocno_p)
1466 print_allocno_costs (dump_file);
1467 else
1468 print_pseudo_costs (dump_file);
1469 fprintf (dump_file,"\n");
1472 #ifdef FORBIDDEN_INC_DEC_CLASSES
1473 ira_free (in_inc_dec);
1474 #endif
1479 /* Process moves involving hard regs to modify allocno hard register
1480 costs. We can do this only after determining allocno cover class.
1481 If a hard register forms a register class, than moves with the hard
1482 register are already taken into account in class costs for the
1483 allocno. */
1484 static void
1485 process_bb_node_for_hard_reg_moves (ira_loop_tree_node_t loop_tree_node)
1487 int i, freq, cost, src_regno, dst_regno, hard_regno;
1488 bool to_p;
1489 ira_allocno_t a;
1490 enum reg_class rclass, hard_reg_class;
1491 enum machine_mode mode;
1492 basic_block bb;
1493 rtx insn, set, src, dst;
1495 bb = loop_tree_node->bb;
1496 if (bb == NULL)
1497 return;
1498 freq = REG_FREQ_FROM_BB (bb);
1499 if (freq == 0)
1500 freq = 1;
1501 FOR_BB_INSNS (bb, insn)
1503 if (!NONDEBUG_INSN_P (insn))
1504 continue;
1505 set = single_set (insn);
1506 if (set == NULL_RTX)
1507 continue;
1508 dst = SET_DEST (set);
1509 src = SET_SRC (set);
1510 if (! REG_P (dst) || ! REG_P (src))
1511 continue;
1512 dst_regno = REGNO (dst);
1513 src_regno = REGNO (src);
1514 if (dst_regno >= FIRST_PSEUDO_REGISTER
1515 && src_regno < FIRST_PSEUDO_REGISTER)
1517 hard_regno = src_regno;
1518 to_p = true;
1519 a = ira_curr_regno_allocno_map[dst_regno];
1521 else if (src_regno >= FIRST_PSEUDO_REGISTER
1522 && dst_regno < FIRST_PSEUDO_REGISTER)
1524 hard_regno = dst_regno;
1525 to_p = false;
1526 a = ira_curr_regno_allocno_map[src_regno];
1528 else
1529 continue;
1530 rclass = ALLOCNO_COVER_CLASS (a);
1531 if (! TEST_HARD_REG_BIT (reg_class_contents[rclass], hard_regno))
1532 continue;
1533 i = ira_class_hard_reg_index[rclass][hard_regno];
1534 if (i < 0)
1535 continue;
1536 mode = ALLOCNO_MODE (a);
1537 hard_reg_class = REGNO_REG_CLASS (hard_regno);
1538 cost
1539 = (to_p ? ira_get_register_move_cost (mode, hard_reg_class, rclass)
1540 : ira_get_register_move_cost (mode, rclass, hard_reg_class)) * freq;
1541 ira_allocate_and_set_costs (&ALLOCNO_HARD_REG_COSTS (a), rclass,
1542 ALLOCNO_COVER_CLASS_COST (a));
1543 ira_allocate_and_set_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a),
1544 rclass, 0);
1545 ALLOCNO_HARD_REG_COSTS (a)[i] -= cost;
1546 ALLOCNO_CONFLICT_HARD_REG_COSTS (a)[i] -= cost;
1547 ALLOCNO_COVER_CLASS_COST (a) = MIN (ALLOCNO_COVER_CLASS_COST (a),
1548 ALLOCNO_HARD_REG_COSTS (a)[i]);
1552 /* After we find hard register and memory costs for allocnos, define
1553 its cover class and modify hard register cost because insns moving
1554 allocno to/from hard registers. */
1555 static void
1556 setup_allocno_cover_class_and_costs (void)
1558 int i, j, n, regno, num;
1559 int *reg_costs;
1560 enum reg_class cover_class, rclass;
1561 ira_allocno_t a;
1562 ira_allocno_iterator ai;
1564 ira_assert (allocno_p);
1565 FOR_EACH_ALLOCNO (a, ai)
1567 i = ALLOCNO_NUM (a);
1568 cover_class = regno_cover_class[ALLOCNO_REGNO (a)];
1569 ira_assert (pref[i] == NO_REGS || cover_class != NO_REGS);
1570 ALLOCNO_MEMORY_COST (a) = COSTS (costs, i)->mem_cost;
1571 ira_set_allocno_cover_class (a, cover_class);
1572 if (cover_class == NO_REGS)
1573 continue;
1574 ALLOCNO_AVAILABLE_REGS_NUM (a) = ira_available_class_regs[cover_class];
1575 if (optimize && ALLOCNO_COVER_CLASS (a) != pref[i])
1577 n = ira_class_hard_regs_num[cover_class];
1578 ALLOCNO_HARD_REG_COSTS (a)
1579 = reg_costs = ira_allocate_cost_vector (cover_class);
1580 for (j = n - 1; j >= 0; j--)
1582 regno = ira_class_hard_regs[cover_class][j];
1583 if (TEST_HARD_REG_BIT (reg_class_contents[pref[i]], regno))
1584 reg_costs[j] = ALLOCNO_COVER_CLASS_COST (a);
1585 else
1587 rclass = REGNO_REG_CLASS (regno);
1588 num = cost_class_nums[rclass];
1589 if (num < 0)
1591 /* The hard register class is not a cover class or a
1592 class not fully inside in a cover class -- use
1593 the allocno cover class. */
1594 ira_assert (ira_hard_regno_cover_class[regno]
1595 == cover_class);
1596 num = cost_class_nums[cover_class];
1598 reg_costs[j] = COSTS (costs, i)->cost[num];
1603 if (optimize)
1604 ira_traverse_loop_tree (true, ira_loop_tree_root,
1605 process_bb_node_for_hard_reg_moves, NULL);
1610 /* Function called once during compiler work. */
1611 void
1612 ira_init_costs_once (void)
1614 int i;
1616 init_cost = NULL;
1617 for (i = 0; i < MAX_RECOG_OPERANDS; i++)
1619 op_costs[i] = NULL;
1620 this_op_costs[i] = NULL;
1622 temp_costs = NULL;
1623 cost_classes = NULL;
1626 /* Free allocated temporary cost vectors. */
1627 static void
1628 free_ira_costs (void)
1630 int i;
1632 if (init_cost != NULL)
1633 free (init_cost);
1634 init_cost = NULL;
1635 for (i = 0; i < MAX_RECOG_OPERANDS; i++)
1637 if (op_costs[i] != NULL)
1638 free (op_costs[i]);
1639 if (this_op_costs[i] != NULL)
1640 free (this_op_costs[i]);
1641 op_costs[i] = this_op_costs[i] = NULL;
1643 if (temp_costs != NULL)
1644 free (temp_costs);
1645 temp_costs = NULL;
1646 if (cost_classes != NULL)
1647 free (cost_classes);
1648 cost_classes = NULL;
1651 /* This is called each time register related information is
1652 changed. */
1653 void
1654 ira_init_costs (void)
1656 int i;
1658 free_ira_costs ();
1659 max_struct_costs_size
1660 = sizeof (struct costs) + sizeof (int) * (ira_important_classes_num - 1);
1661 /* Don't use ira_allocate because vectors live through several IRA calls. */
1662 init_cost = (struct costs *) xmalloc (max_struct_costs_size);
1663 init_cost->mem_cost = 1000000;
1664 for (i = 0; i < ira_important_classes_num; i++)
1665 init_cost->cost[i] = 1000000;
1666 for (i = 0; i < MAX_RECOG_OPERANDS; i++)
1668 op_costs[i] = (struct costs *) xmalloc (max_struct_costs_size);
1669 this_op_costs[i] = (struct costs *) xmalloc (max_struct_costs_size);
1671 temp_costs = (struct costs *) xmalloc (max_struct_costs_size);
1672 cost_classes = (enum reg_class *) xmalloc (sizeof (enum reg_class)
1673 * ira_important_classes_num);
1676 /* Function called once at the end of compiler work. */
1677 void
1678 ira_finish_costs_once (void)
1680 free_ira_costs ();
1685 /* Common initialization function for ira_costs and
1686 ira_set_pseudo_classes. */
1687 static void
1688 init_costs (void)
1690 init_subregs_of_mode ();
1691 costs = (struct costs *) ira_allocate (max_struct_costs_size
1692 * cost_elements_num);
1693 pref_buffer
1694 = (enum reg_class *) ira_allocate (sizeof (enum reg_class)
1695 * cost_elements_num);
1696 regno_cover_class
1697 = (enum reg_class *) ira_allocate (sizeof (enum reg_class)
1698 * max_reg_num ());
1699 regno_equiv_gains = (int *) ira_allocate (sizeof (int) * max_reg_num ());
1700 memset (regno_equiv_gains, 0, sizeof (int) * max_reg_num ());
1703 /* Common finalization function for ira_costs and
1704 ira_set_pseudo_classes. */
1705 static void
1706 finish_costs (void)
1708 ira_free (regno_equiv_gains);
1709 ira_free (regno_cover_class);
1710 ira_free (pref_buffer);
1711 ira_free (costs);
1714 /* Entry function which defines cover class, memory and hard register
1715 costs for each allocno. */
1716 void
1717 ira_costs (void)
1719 allocno_p = true;
1720 cost_elements_num = ira_allocnos_num;
1721 init_costs ();
1722 total_allocno_costs = (struct costs *) ira_allocate (max_struct_costs_size
1723 * ira_allocnos_num);
1724 calculate_elim_costs_all_insns ();
1725 find_costs_and_classes (ira_dump_file);
1726 setup_allocno_cover_class_and_costs ();
1727 finish_costs ();
1728 ira_free (total_allocno_costs);
1731 /* Entry function which defines classes for pseudos. */
1732 void
1733 ira_set_pseudo_classes (FILE *dump_file)
1735 allocno_p = false;
1736 internal_flag_ira_verbose = flag_ira_verbose;
1737 cost_elements_num = max_reg_num ();
1738 init_costs ();
1739 find_costs_and_classes (dump_file);
1740 pseudo_classes_defined_p = true;
1741 finish_costs ();
1746 /* Change hard register costs for allocnos which lives through
1747 function calls. This is called only when we found all intersected
1748 calls during building allocno live ranges. */
1749 void
1750 ira_tune_allocno_costs_and_cover_classes (void)
1752 int j, n, regno;
1753 int cost, min_cost, *reg_costs;
1754 enum reg_class cover_class, rclass;
1755 enum machine_mode mode;
1756 ira_allocno_t a;
1757 ira_allocno_iterator ai;
1759 FOR_EACH_ALLOCNO (a, ai)
1761 cover_class = ALLOCNO_COVER_CLASS (a);
1762 if (cover_class == NO_REGS)
1763 continue;
1764 mode = ALLOCNO_MODE (a);
1765 n = ira_class_hard_regs_num[cover_class];
1766 min_cost = INT_MAX;
1767 if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
1769 ira_allocate_and_set_costs
1770 (&ALLOCNO_HARD_REG_COSTS (a), cover_class,
1771 ALLOCNO_COVER_CLASS_COST (a));
1772 reg_costs = ALLOCNO_HARD_REG_COSTS (a);
1773 for (j = n - 1; j >= 0; j--)
1775 regno = ira_class_hard_regs[cover_class][j];
1776 rclass = REGNO_REG_CLASS (regno);
1777 cost = 0;
1778 if (! ira_hard_reg_not_in_set_p (regno, mode, call_used_reg_set)
1779 || HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
1780 cost += (ALLOCNO_CALL_FREQ (a)
1781 * (ira_memory_move_cost[mode][rclass][0]
1782 + ira_memory_move_cost[mode][rclass][1]));
1783 #ifdef IRA_HARD_REGNO_ADD_COST_MULTIPLIER
1784 cost += ((ira_memory_move_cost[mode][rclass][0]
1785 + ira_memory_move_cost[mode][rclass][1])
1786 * ALLOCNO_FREQ (a)
1787 * IRA_HARD_REGNO_ADD_COST_MULTIPLIER (regno) / 2);
1788 #endif
1789 reg_costs[j] += cost;
1790 if (min_cost > reg_costs[j])
1791 min_cost = reg_costs[j];
1794 if (min_cost != INT_MAX)
1795 ALLOCNO_COVER_CLASS_COST (a) = min_cost;
1797 /* Some targets allow pseudos to be allocated to unaligned
1798 sequences of hard registers. However, selecting an unaligned
1799 sequence can unnecessarily restrict later allocations. So
1800 increase the cost of unaligned hard regs to encourage the use
1801 of aligned hard regs. */
1803 int nregs, index;
1805 if ((nregs = ira_reg_class_nregs[cover_class][ALLOCNO_MODE (a)]) > 1)
1807 ira_allocate_and_set_costs
1808 (&ALLOCNO_HARD_REG_COSTS (a), cover_class,
1809 ALLOCNO_COVER_CLASS_COST (a));
1810 reg_costs = ALLOCNO_HARD_REG_COSTS (a);
1811 for (j = n - 1; j >= 0; j--)
1813 if (j % nregs != 0)
1815 regno = ira_non_ordered_class_hard_regs[cover_class][j];
1816 index = ira_class_hard_reg_index[cover_class][regno];
1817 ira_assert (index != -1);
1818 reg_costs[index] += ALLOCNO_FREQ (a);
1826 /* Add COST to the estimated gain for eliminating REGNO with its
1827 equivalence. If COST is zero, record that no such elimination is
1828 possible. */
1830 void
1831 ira_adjust_equiv_reg_cost (unsigned regno, int cost)
1833 if (cost == 0)
1834 regno_equiv_gains[regno] = 0;
1835 else
1836 regno_equiv_gains[regno] += cost;