2008-05-21 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ira-costs.c
blobbb9c57013a7543c368bea1e5df8d40efa66a1e39
1 /* IRA hard register and memory cost calculation for allocnos.
2 Copyright (C) 2006, 2007, 2008
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 "toplev.h"
37 #include "target.h"
38 #include "params.h"
39 #include "ira-int.h"
41 /* The file contains code is similar to one in regclass but the code
42 works on the allocno basis. */
44 #ifdef FORBIDDEN_INC_DEC_CLASSES
45 /* Indexed by n, is nonzero if allocno with number N is used in an
46 auto-inc or auto-dec context. */
47 static char *in_inc_dec;
48 #endif
50 /* The `costs' struct records the cost of using hard registers of each
51 class considered for the calculation and of using memory for each
52 allocno. */
53 struct costs
55 int mem_cost;
56 /* Costs for register classes start here. We process only some
57 register classes (cover classes on the 1st cost calculation
58 iteration and important classes on the 2nd iteration). */
59 int cost[1];
62 /* Initialized once. It is a maximal possible size of the allocated
63 struct costs. */
64 static int max_struct_costs_size;
66 /* Allocated and initialized once, and used to initialize cost values
67 for each insn. */
68 static struct costs *init_cost;
70 /* Allocated once, and used for temporary purposes. */
71 static struct costs *temp_costs;
73 /* Allocated once, and used for the cost calculation. */
74 static struct costs *op_costs[MAX_RECOG_OPERANDS];
75 static struct costs *this_op_costs[MAX_RECOG_OPERANDS];
77 /* Record the initial and accumulated cost of each class for each
78 allocno. */
79 static struct costs *total_costs;
81 /* Classes used for cost calculation. They may be different on
82 different iterations of the cost calculations or in different
83 optimization modes. */
84 static enum reg_class *cost_classes;
86 /* The size of the previous array. */
87 static int cost_classes_num;
89 /* Map: cost class -> order number (they start with 0) of the cost
90 class. */
91 static int cost_class_nums[N_REG_CLASSES];
93 /* It is the current size of struct costs. */
94 static int struct_costs_size;
96 /* Return pointer to structure containing costs of allocno with given
97 NUM in array ARR. */
98 #define COSTS_OF_ALLOCNO(arr, num) \
99 ((struct costs *) ((char *) (arr) + (num) * struct_costs_size))
101 /* Record register class preferences of each allocno. Null value
102 means no preferences. It happens on the 1st iteration of the cost
103 calculation. */
104 static enum reg_class *allocno_pref;
106 /* Allocated buffers for allocno_pref. */
107 static enum reg_class *allocno_pref_buffer;
109 /* Execution frequency of the current insn. */
110 static int frequency;
112 static int copy_cost (rtx, enum machine_mode, enum reg_class, int,
113 secondary_reload_info *);
114 static void record_reg_classes (int, int, rtx *, enum machine_mode *,
115 const char **, rtx, struct costs **,
116 enum reg_class *);
117 static inline int ok_for_index_p_nonstrict (rtx);
118 static inline int ok_for_base_p_nonstrict (rtx, enum machine_mode,
119 enum rtx_code, enum rtx_code);
120 static void record_address_regs (enum machine_mode, rtx x, int,
121 enum rtx_code, enum rtx_code, int scale);
122 static void record_operand_costs (rtx, struct costs **, enum reg_class *);
123 static rtx scan_one_insn (rtx);
124 static void print_costs (FILE *);
125 static void process_bb_node_for_costs (loop_tree_node_t);
126 static void find_allocno_class_costs (void);
127 static void process_bb_node_for_hard_reg_moves (loop_tree_node_t);
128 static void setup_allocno_cover_class_and_costs (void);
129 static void free_ira_costs (void);
133 /* Compute the cost of loading X into (if TO_P is nonzero) or from (if
134 TO_P is zero) a register of class CLASS in mode MODE. X must not
135 be a pseudo register. */
136 static int
137 copy_cost (rtx x, enum machine_mode mode, enum reg_class class, int to_p,
138 secondary_reload_info *prev_sri)
140 secondary_reload_info sri;
141 enum reg_class secondary_class = NO_REGS;
143 /* If X is a SCRATCH, there is actually nothing to move since we are
144 assuming optimal allocation. */
145 if (GET_CODE (x) == SCRATCH)
146 return 0;
148 /* Get the class we will actually use for a reload. */
149 class = PREFERRED_RELOAD_CLASS (x, class);
151 /* If we need a secondary reload for an intermediate, the cost is
152 that to load the input into the intermediate register, then to
153 copy it. */
154 sri.prev_sri = prev_sri;
155 sri.extra_cost = 0;
156 secondary_class = targetm.secondary_reload (to_p, x, class, mode, &sri);
158 if (register_move_cost[mode] == NULL)
159 init_register_move_cost (mode);
161 if (secondary_class != NO_REGS)
162 return (move_cost[mode][secondary_class][class] + sri.extra_cost
163 + copy_cost (x, mode, secondary_class, to_p, &sri));
165 /* For memory, use the memory move cost, for (hard) registers, use
166 the cost to move between the register classes, and use 2 for
167 everything else (constants). */
168 if (MEM_P (x) || class == NO_REGS)
169 return sri.extra_cost + memory_move_cost[mode][class][to_p != 0];
170 else if (REG_P (x))
171 return
172 (sri.extra_cost + move_cost[mode][REGNO_REG_CLASS (REGNO (x))][class]);
173 else
174 /* If this is a constant, we may eventually want to call rtx_cost
175 here. */
176 return sri.extra_cost + COSTS_N_INSNS (1);
181 /* Record the cost of using memory or hard registers of various
182 classes for the operands in INSN.
184 N_ALTS is the number of alternatives.
185 N_OPS is the number of operands.
186 OPS is an array of the operands.
187 MODES are the modes of the operands, in case any are VOIDmode.
188 CONSTRAINTS are the constraints to use for the operands. This array
189 is modified by this procedure.
191 This procedure works alternative by alternative. For each
192 alternative we assume that we will be able to allocate all allocnos
193 to their ideal register class and calculate the cost of using that
194 alternative. Then we compute for each operand that is a
195 pseudo-register, the cost of having the allocno allocated to each
196 register class and using it in that alternative. To this cost is
197 added the cost of the alternative.
199 The cost of each class for this insn is its lowest cost among all
200 the alternatives. */
201 static void
202 record_reg_classes (int n_alts, int n_ops, rtx *ops,
203 enum machine_mode *modes, const char **constraints,
204 rtx insn, struct costs **op_costs,
205 enum reg_class *allocno_pref)
207 int alt;
208 int i, j, k;
209 rtx set;
211 /* Process each alternative, each time minimizing an operand's cost
212 with the cost for each operand in that alternative. */
213 for (alt = 0; alt < n_alts; alt++)
215 enum reg_class classes[MAX_RECOG_OPERANDS];
216 int allows_mem[MAX_RECOG_OPERANDS];
217 int class;
218 int alt_fail = 0;
219 int alt_cost = 0, op_cost_add;
221 for (i = 0; i < n_ops; i++)
223 unsigned char c;
224 const char *p = constraints[i];
225 rtx op = ops[i];
226 enum machine_mode mode = modes[i];
227 int allows_addr = 0;
228 int win = 0;
230 /* Initially show we know nothing about the register class. */
231 classes[i] = NO_REGS;
232 allows_mem[i] = 0;
234 /* If this operand has no constraints at all, we can
235 conclude nothing about it since anything is valid. */
236 if (*p == 0)
238 if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
239 memset (this_op_costs[i], 0, struct_costs_size);
240 continue;
243 /* If this alternative is only relevant when this operand
244 matches a previous operand, we do different things
245 depending on whether this operand is a allocno-reg or not.
246 We must process any modifiers for the operand before we
247 can make this test. */
248 while (*p == '%' || *p == '=' || *p == '+' || *p == '&')
249 p++;
251 if (p[0] >= '0' && p[0] <= '0' + i && (p[1] == ',' || p[1] == 0))
253 /* Copy class and whether memory is allowed from the
254 matching alternative. Then perform any needed cost
255 computations and/or adjustments. */
256 j = p[0] - '0';
257 classes[i] = classes[j];
258 allows_mem[i] = allows_mem[j];
260 if (! REG_P (op) || REGNO (op) < FIRST_PSEUDO_REGISTER)
262 /* If this matches the other operand, we have no
263 added cost and we win. */
264 if (rtx_equal_p (ops[j], op))
265 win = 1;
266 /* If we can put the other operand into a register,
267 add to the cost of this alternative the cost to
268 copy this operand to the register used for the
269 other operand. */
270 else if (classes[j] != NO_REGS)
272 alt_cost += copy_cost (op, mode, classes[j], 1, NULL);
273 win = 1;
276 else if (! REG_P (ops[j])
277 || REGNO (ops[j]) < FIRST_PSEUDO_REGISTER)
279 /* This op is an allocno but the one it matches is
280 not. */
282 /* If we can't put the other operand into a
283 register, this alternative can't be used. */
285 if (classes[j] == NO_REGS)
286 alt_fail = 1;
287 /* Otherwise, add to the cost of this alternative
288 the cost to copy the other operand to the hard
289 register used for this operand. */
290 else
291 alt_cost += copy_cost (ops[j], mode, classes[j], 1, NULL);
293 else
295 /* The costs of this operand are not the same as the
296 other operand since move costs are not symmetric.
297 Moreover, if we cannot tie them, this alternative
298 needs to do a copy, which is one insn. */
299 struct costs *pp = this_op_costs[i];
301 if (register_move_cost[mode] == NULL)
302 init_register_move_cost (mode);
304 for (k = 0; k < cost_classes_num; k++)
306 class = cost_classes[k];
307 pp->cost[k]
308 = ((recog_data.operand_type[i] != OP_OUT
309 ? register_may_move_in_cost[mode][class]
310 [classes[i]] * frequency : 0)
311 + (recog_data.operand_type[i] != OP_IN
312 ? register_may_move_out_cost[mode][classes[i]]
313 [class] * frequency : 0));
316 /* If the alternative actually allows memory, make
317 things a bit cheaper since we won't need an extra
318 insn to load it. */
319 pp->mem_cost
320 = ((recog_data.operand_type[i] != OP_IN
321 ? memory_move_cost[mode][classes[i]][0] : 0)
322 + (recog_data.operand_type[i] != OP_OUT
323 ? memory_move_cost[mode][classes[i]][1] : 0)
324 - allows_mem[i]) * frequency;
325 /* If we have assigned a class to this allocno in our
326 first pass, add a cost to this alternative
327 corresponding to what we would add if this allocno
328 were not in the appropriate class. We could use
329 cover class here but it is less accurate
330 approximation. */
331 if (allocno_pref)
333 enum reg_class pref_class
334 = allocno_pref[ALLOCNO_NUM
335 (ira_curr_regno_allocno_map
336 [REGNO (op)])];
338 if (pref_class == NO_REGS)
339 alt_cost
340 += ((recog_data.operand_type[i] != OP_IN
341 ? memory_move_cost[mode][classes[i]][0] : 0)
342 + (recog_data.operand_type[i] != OP_OUT
343 ? memory_move_cost[mode][classes[i]][1] : 0));
344 else if (reg_class_intersect
345 [pref_class][classes[i]] == NO_REGS)
346 alt_cost
347 += register_move_cost[mode][pref_class][classes[i]];
349 if (REGNO (ops[i]) != REGNO (ops[j])
350 && ! find_reg_note (insn, REG_DEAD, op))
351 alt_cost += 2;
353 /* This is in place of ordinary cost computation for
354 this operand, so skip to the end of the
355 alternative (should be just one character). */
356 while (*p && *p++ != ',')
359 constraints[i] = p;
360 continue;
364 /* Scan all the constraint letters. See if the operand
365 matches any of the constraints. Collect the valid
366 register classes and see if this operand accepts
367 memory. */
368 while ((c = *p))
370 switch (c)
372 case ',':
373 break;
374 case '*':
375 /* Ignore the next letter for this pass. */
376 c = *++p;
377 break;
379 case '?':
380 alt_cost += 2;
381 case '!': case '#': case '&':
382 case '0': case '1': case '2': case '3': case '4':
383 case '5': case '6': case '7': case '8': case '9':
384 break;
386 case 'p':
387 allows_addr = 1;
388 win = address_operand (op, GET_MODE (op));
389 /* We know this operand is an address, so we want it
390 to be allocated to a register that can be the
391 base of an address, i.e. BASE_REG_CLASS. */
392 classes[i]
393 = reg_class_union[classes[i]]
394 [base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
395 break;
397 case 'm': case 'o': case 'V':
398 /* It doesn't seem worth distinguishing between
399 offsettable and non-offsettable addresses
400 here. */
401 allows_mem[i] = 1;
402 if (MEM_P (op))
403 win = 1;
404 break;
406 case '<':
407 if (MEM_P (op)
408 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
409 || GET_CODE (XEXP (op, 0)) == POST_DEC))
410 win = 1;
411 break;
413 case '>':
414 if (MEM_P (op)
415 && (GET_CODE (XEXP (op, 0)) == PRE_INC
416 || GET_CODE (XEXP (op, 0)) == POST_INC))
417 win = 1;
418 break;
420 case 'E':
421 case 'F':
422 if (GET_CODE (op) == CONST_DOUBLE
423 || (GET_CODE (op) == CONST_VECTOR
424 && (GET_MODE_CLASS (GET_MODE (op))
425 == MODE_VECTOR_FLOAT)))
426 win = 1;
427 break;
429 case 'G':
430 case 'H':
431 if (GET_CODE (op) == CONST_DOUBLE
432 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
433 win = 1;
434 break;
436 case 's':
437 if (GET_CODE (op) == CONST_INT
438 || (GET_CODE (op) == CONST_DOUBLE
439 && GET_MODE (op) == VOIDmode))
440 break;
442 case 'i':
443 if (CONSTANT_P (op)
444 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op)))
445 win = 1;
446 break;
448 case 'n':
449 if (GET_CODE (op) == CONST_INT
450 || (GET_CODE (op) == CONST_DOUBLE
451 && GET_MODE (op) == VOIDmode))
452 win = 1;
453 break;
455 case 'I':
456 case 'J':
457 case 'K':
458 case 'L':
459 case 'M':
460 case 'N':
461 case 'O':
462 case 'P':
463 if (GET_CODE (op) == CONST_INT
464 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
465 win = 1;
466 break;
468 case 'X':
469 win = 1;
470 break;
472 case 'g':
473 if (MEM_P (op)
474 || (CONSTANT_P (op)
475 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))))
476 win = 1;
477 allows_mem[i] = 1;
478 case 'r':
479 classes[i] = reg_class_union[classes[i]][GENERAL_REGS];
480 break;
482 default:
483 if (REG_CLASS_FROM_CONSTRAINT (c, p) != NO_REGS)
484 classes[i] = reg_class_union[classes[i]]
485 [REG_CLASS_FROM_CONSTRAINT (c, p)];
486 #ifdef EXTRA_CONSTRAINT_STR
487 else if (EXTRA_CONSTRAINT_STR (op, c, p))
488 win = 1;
490 if (EXTRA_MEMORY_CONSTRAINT (c, p))
492 /* Every MEM can be reloaded to fit. */
493 allows_mem[i] = 1;
494 if (MEM_P (op))
495 win = 1;
497 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
499 /* Every address can be reloaded to fit. */
500 allows_addr = 1;
501 if (address_operand (op, GET_MODE (op)))
502 win = 1;
503 /* We know this operand is an address, so we
504 want it to be allocated to a hard register
505 that can be the base of an address,
506 i.e. BASE_REG_CLASS. */
507 classes[i]
508 = reg_class_union[classes[i]]
509 [base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
511 #endif
512 break;
514 p += CONSTRAINT_LEN (c, p);
515 if (c == ',')
516 break;
519 constraints[i] = p;
521 /* How we account for this operand now depends on whether it
522 is a pseudo register or not. If it is, we first check if
523 any register classes are valid. If not, we ignore this
524 alternative, since we want to assume that all allocnos get
525 allocated for register preferencing. If some register
526 class is valid, compute the costs of moving the allocno
527 into that class. */
528 if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
530 if (classes[i] == NO_REGS)
532 /* We must always fail if the operand is a REG, but
533 we did not find a suitable class.
535 Otherwise we may perform an uninitialized read
536 from this_op_costs after the `continue' statement
537 below. */
538 alt_fail = 1;
540 else
542 struct costs *pp = this_op_costs[i];
544 if (register_move_cost[mode] == NULL)
545 init_register_move_cost (mode);
547 for (k = 0; k < cost_classes_num; k++)
549 class = cost_classes[k];
550 pp->cost[k]
551 = ((recog_data.operand_type[i] != OP_OUT
552 ? register_may_move_in_cost[mode][class]
553 [classes[i]] * frequency : 0)
554 + (recog_data.operand_type[i] != OP_IN
555 ? register_may_move_out_cost[mode][classes[i]]
556 [class] * frequency : 0));
559 /* If the alternative actually allows memory, make
560 things a bit cheaper since we won't need an extra
561 insn to load it. */
562 pp->mem_cost
563 = ((recog_data.operand_type[i] != OP_IN
564 ? memory_move_cost[mode][classes[i]][0] : 0)
565 + (recog_data.operand_type[i] != OP_OUT
566 ? memory_move_cost[mode][classes[i]][1] : 0)
567 - allows_mem[i]) * frequency;
568 /* If we have assigned a class to this allocno in our
569 first pass, add a cost to this alternative
570 corresponding to what we would add if this allocno
571 were not in the appropriate class. We could use
572 cover class here but it is less accurate
573 approximation. */
574 if (allocno_pref)
576 enum reg_class pref_class
577 = allocno_pref[ALLOCNO_NUM
578 (ira_curr_regno_allocno_map
579 [REGNO (op)])];
581 if (pref_class == NO_REGS)
582 alt_cost
583 += ((recog_data.operand_type[i] != OP_IN
584 ? memory_move_cost[mode][classes[i]][0] : 0)
585 + (recog_data.operand_type[i] != OP_OUT
586 ? memory_move_cost[mode][classes[i]][1] : 0));
587 else if (reg_class_intersect[pref_class][classes[i]]
588 == NO_REGS)
589 alt_cost
590 += register_move_cost[mode][pref_class][classes[i]];
595 /* Otherwise, if this alternative wins, either because we
596 have already determined that or if we have a hard
597 register of the proper class, there is no cost for this
598 alternative. */
599 else if (win || (REG_P (op)
600 && reg_fits_class_p (op, classes[i],
601 0, GET_MODE (op))))
604 /* If registers are valid, the cost of this alternative
605 includes copying the object to and/or from a
606 register. */
607 else if (classes[i] != NO_REGS)
609 if (recog_data.operand_type[i] != OP_OUT)
610 alt_cost += copy_cost (op, mode, classes[i], 1, NULL);
612 if (recog_data.operand_type[i] != OP_IN)
613 alt_cost += copy_cost (op, mode, classes[i], 0, NULL);
615 /* The only other way this alternative can be used is if
616 this is a constant that could be placed into memory. */
617 else if (CONSTANT_P (op) && (allows_addr || allows_mem[i]))
618 alt_cost += memory_move_cost[mode][classes[i]][1];
619 else
620 alt_fail = 1;
623 if (alt_fail)
624 continue;
626 op_cost_add = alt_cost * frequency;
627 /* Finally, update the costs with the information we've
628 calculated about this alternative. */
629 for (i = 0; i < n_ops; i++)
630 if (REG_P (ops[i]) && REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
632 struct costs *pp = op_costs[i], *qq = this_op_costs[i];
633 int scale = 1 + (recog_data.operand_type[i] == OP_INOUT);
635 pp->mem_cost = MIN (pp->mem_cost,
636 (qq->mem_cost + op_cost_add) * scale);
638 for (k = 0; k < cost_classes_num; k++)
639 pp->cost[k]
640 = MIN (pp->cost[k], (qq->cost[k] + op_cost_add) * scale);
644 /* If this insn is a single set copying operand 1 to operand 0 and
645 one operand is an allocno with the other a hard reg or an allocno
646 that prefers a hard register that is in its own register class
647 then we may want to adjust the cost of that register class to -1.
649 Avoid the adjustment if the source does not die to avoid
650 stressing of register allocator by preferrencing two colliding
651 registers into single class.
653 Also avoid the adjustment if a copy between hard registers of the
654 class is expensive (ten times the cost of a default copy is
655 considered arbitrarily expensive). This avoids losing when the
656 preferred class is very expensive as the source of a copy
657 instruction. */
658 if ((set = single_set (insn)) != 0
659 && ops[0] == SET_DEST (set) && ops[1] == SET_SRC (set)
660 && REG_P (ops[0]) && REG_P (ops[1])
661 && find_regno_note (insn, REG_DEAD, REGNO (ops[1])))
662 for (i = 0; i <= 1; i++)
663 if (REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
665 unsigned int regno = REGNO (ops[!i]);
666 enum machine_mode mode = GET_MODE (ops[!i]);
667 int class;
668 unsigned int nr;
670 if (regno < FIRST_PSEUDO_REGISTER)
671 for (k = 0; k < cost_classes_num; k++)
673 class = cost_classes[k];
674 if (TEST_HARD_REG_BIT (reg_class_contents[class], regno)
675 && (reg_class_size[class]
676 == (unsigned) CLASS_MAX_NREGS (class, mode)))
678 if (reg_class_size[class] == 1)
679 op_costs[i]->cost[k] = -frequency;
680 else
682 for (nr = 0;
683 nr < (unsigned) hard_regno_nregs[regno][mode];
684 nr++)
685 if (! TEST_HARD_REG_BIT (reg_class_contents[class],
686 regno + nr))
687 break;
689 if (nr == (unsigned) hard_regno_nregs[regno][mode])
690 op_costs[i]->cost[k] = -frequency;
699 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudo registers. */
700 static inline int
701 ok_for_index_p_nonstrict (rtx reg)
703 unsigned regno = REGNO (reg);
705 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
708 /* A version of regno_ok_for_base_p for use here, when all
709 pseudo-registers should count as OK. Arguments as for
710 regno_ok_for_base_p. */
711 static inline int
712 ok_for_base_p_nonstrict (rtx reg, enum machine_mode mode,
713 enum rtx_code outer_code, enum rtx_code index_code)
715 unsigned regno = REGNO (reg);
717 if (regno >= FIRST_PSEUDO_REGISTER)
718 return TRUE;
719 return ok_for_base_p_1 (regno, mode, outer_code, index_code);
722 /* Record the pseudo registers we must reload into hard registers in a
723 subexpression of a memory address, X.
725 If CONTEXT is 0, we are looking at the base part of an address,
726 otherwise we are looking at the index part.
728 MODE is the mode of the memory reference; OUTER_CODE and INDEX_CODE
729 give the context that the rtx appears in. These three arguments
730 are passed down to base_reg_class.
732 SCALE is twice the amount to multiply the cost by (it is twice so
733 we can represent half-cost adjustments). */
734 static void
735 record_address_regs (enum machine_mode mode, rtx x, int context,
736 enum rtx_code outer_code, enum rtx_code index_code,
737 int scale)
739 enum rtx_code code = GET_CODE (x);
740 enum reg_class class;
742 if (context == 1)
743 class = INDEX_REG_CLASS;
744 else
745 class = base_reg_class (mode, outer_code, index_code);
747 switch (code)
749 case CONST_INT:
750 case CONST:
751 case CC0:
752 case PC:
753 case SYMBOL_REF:
754 case LABEL_REF:
755 return;
757 case PLUS:
758 /* When we have an address that is a sum, we must determine
759 whether registers are "base" or "index" regs. If there is a
760 sum of two registers, we must choose one to be the "base".
761 Luckily, we can use the REG_POINTER to make a good choice
762 most of the time. We only need to do this on machines that
763 can have two registers in an address and where the base and
764 index register classes are different.
766 ??? This code used to set REGNO_POINTER_FLAG in some cases,
767 but that seems bogus since it should only be set when we are
768 sure the register is being used as a pointer. */
770 rtx arg0 = XEXP (x, 0);
771 rtx arg1 = XEXP (x, 1);
772 enum rtx_code code0 = GET_CODE (arg0);
773 enum rtx_code code1 = GET_CODE (arg1);
775 /* Look inside subregs. */
776 if (code0 == SUBREG)
777 arg0 = SUBREG_REG (arg0), code0 = GET_CODE (arg0);
778 if (code1 == SUBREG)
779 arg1 = SUBREG_REG (arg1), code1 = GET_CODE (arg1);
781 /* If this machine only allows one register per address, it
782 must be in the first operand. */
783 if (MAX_REGS_PER_ADDRESS == 1)
784 record_address_regs (mode, arg0, 0, PLUS, code1, scale);
786 /* If index and base registers are the same on this machine,
787 just record registers in any non-constant operands. We
788 assume here, as well as in the tests below, that all
789 addresses are in canonical form. */
790 else if (INDEX_REG_CLASS == base_reg_class (VOIDmode, PLUS, SCRATCH))
792 record_address_regs (mode, arg0, context, PLUS, code1, scale);
793 if (! CONSTANT_P (arg1))
794 record_address_regs (mode, arg1, context, PLUS, code0, scale);
797 /* If the second operand is a constant integer, it doesn't
798 change what class the first operand must be. */
799 else if (code1 == CONST_INT || code1 == CONST_DOUBLE)
800 record_address_regs (mode, arg0, context, PLUS, code1, scale);
801 /* If the second operand is a symbolic constant, the first
802 operand must be an index register. */
803 else if (code1 == SYMBOL_REF || code1 == CONST || code1 == LABEL_REF)
804 record_address_regs (mode, arg0, 1, PLUS, code1, scale);
805 /* If both operands are registers but one is already a hard
806 register of index or reg-base class, give the other the
807 class that the hard register is not. */
808 else if (code0 == REG && code1 == REG
809 && REGNO (arg0) < FIRST_PSEUDO_REGISTER
810 && (ok_for_base_p_nonstrict (arg0, mode, PLUS, REG)
811 || ok_for_index_p_nonstrict (arg0)))
812 record_address_regs (mode, arg1,
813 ok_for_base_p_nonstrict (arg0, mode, PLUS, REG)
814 ? 1 : 0,
815 PLUS, REG, scale);
816 else if (code0 == REG && code1 == REG
817 && REGNO (arg1) < FIRST_PSEUDO_REGISTER
818 && (ok_for_base_p_nonstrict (arg1, mode, PLUS, REG)
819 || ok_for_index_p_nonstrict (arg1)))
820 record_address_regs (mode, arg0,
821 ok_for_base_p_nonstrict (arg1, mode, PLUS, REG)
822 ? 1 : 0,
823 PLUS, REG, scale);
824 /* If one operand is known to be a pointer, it must be the
825 base with the other operand the index. Likewise if the
826 other operand is a MULT. */
827 else if ((code0 == REG && REG_POINTER (arg0)) || code1 == MULT)
829 record_address_regs (mode, arg0, 0, PLUS, code1, scale);
830 record_address_regs (mode, arg1, 1, PLUS, code0, scale);
832 else if ((code1 == REG && REG_POINTER (arg1)) || code0 == MULT)
834 record_address_regs (mode, arg0, 1, PLUS, code1, scale);
835 record_address_regs (mode, arg1, 0, PLUS, code0, scale);
837 /* Otherwise, count equal chances that each might be a base or
838 index register. This case should be rare. */
839 else
841 record_address_regs (mode, arg0, 0, PLUS, code1, scale / 2);
842 record_address_regs (mode, arg0, 1, PLUS, code1, scale / 2);
843 record_address_regs (mode, arg1, 0, PLUS, code0, scale / 2);
844 record_address_regs (mode, arg1, 1, PLUS, code0, scale / 2);
847 break;
849 /* Double the importance of an allocno that is incremented or
850 decremented, since it would take two extra insns if it ends
851 up in the wrong place. */
852 case POST_MODIFY:
853 case PRE_MODIFY:
854 record_address_regs (mode, XEXP (x, 0), 0, code,
855 GET_CODE (XEXP (XEXP (x, 1), 1)), 2 * scale);
856 if (REG_P (XEXP (XEXP (x, 1), 1)))
857 record_address_regs (mode, XEXP (XEXP (x, 1), 1), 1, code, REG,
858 2 * scale);
859 break;
861 case POST_INC:
862 case PRE_INC:
863 case POST_DEC:
864 case PRE_DEC:
865 /* Double the importance of an allocno that is incremented or
866 decremented, since it would take two extra insns if it ends
867 up in the wrong place. If the operand is a pseudo-register,
868 show it is being used in an INC_DEC context. */
869 #ifdef FORBIDDEN_INC_DEC_CLASSES
870 if (REG_P (XEXP (x, 0))
871 && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER)
872 in_inc_dec[ALLOCNO_NUM (ira_curr_regno_allocno_map
873 [REGNO (XEXP (x, 0))])] = 1;
874 #endif
875 record_address_regs (mode, XEXP (x, 0), 0, code, SCRATCH, 2 * scale);
876 break;
878 case REG:
880 struct costs *pp;
881 int i, k;
883 if (REGNO (x) < FIRST_PSEUDO_REGISTER)
884 break;
886 pp = COSTS_OF_ALLOCNO (total_costs,
887 ALLOCNO_NUM (ira_curr_regno_allocno_map
888 [REGNO (x)]));
889 pp->mem_cost += (memory_move_cost[Pmode][class][1] * scale) / 2;
890 if (register_move_cost[Pmode] == NULL)
891 init_register_move_cost (Pmode);
892 for (k = 0; k < cost_classes_num; k++)
894 i = cost_classes[k];
895 pp->cost[k]
896 += (register_may_move_in_cost[Pmode][i][class] * scale) / 2;
899 break;
901 default:
903 const char *fmt = GET_RTX_FORMAT (code);
904 int i;
905 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
906 if (fmt[i] == 'e')
907 record_address_regs (mode, XEXP (x, i), context, code, SCRATCH,
908 scale);
915 /* Calculate the costs of insn operands. */
916 static void
917 record_operand_costs (rtx insn, struct costs **op_costs,
918 enum reg_class *allocno_pref)
920 const char *constraints[MAX_RECOG_OPERANDS];
921 enum machine_mode modes[MAX_RECOG_OPERANDS];
922 int i;
924 for (i = 0; i < recog_data.n_operands; i++)
926 constraints[i] = recog_data.constraints[i];
927 modes[i] = recog_data.operand_mode[i];
930 /* If we get here, we are set up to record the costs of all the
931 operands for this insn. Start by initializing the costs. Then
932 handle any address registers. Finally record the desired classes
933 for any allocnos, doing it twice if some pair of operands are
934 commutative. */
935 for (i = 0; i < recog_data.n_operands; i++)
937 memcpy (op_costs[i], init_cost, struct_costs_size);
939 if (GET_CODE (recog_data.operand[i]) == SUBREG)
940 recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
942 if (MEM_P (recog_data.operand[i]))
943 record_address_regs (GET_MODE (recog_data.operand[i]),
944 XEXP (recog_data.operand[i], 0),
945 0, MEM, SCRATCH, frequency * 2);
946 else if (constraints[i][0] == 'p'
947 || EXTRA_ADDRESS_CONSTRAINT (constraints[i][0],
948 constraints[i]))
949 record_address_regs (VOIDmode, recog_data.operand[i], 0, ADDRESS,
950 SCRATCH, frequency * 2);
953 /* Check for commutative in a separate loop so everything will have
954 been initialized. We must do this even if one operand is a
955 constant--see addsi3 in m68k.md. */
956 for (i = 0; i < (int) recog_data.n_operands - 1; i++)
957 if (constraints[i][0] == '%')
959 const char *xconstraints[MAX_RECOG_OPERANDS];
960 int j;
962 /* Handle commutative operands by swapping the constraints.
963 We assume the modes are the same. */
964 for (j = 0; j < recog_data.n_operands; j++)
965 xconstraints[j] = constraints[j];
967 xconstraints[i] = constraints[i+1];
968 xconstraints[i+1] = constraints[i];
969 record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
970 recog_data.operand, modes,
971 xconstraints, insn, op_costs, allocno_pref);
973 record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
974 recog_data.operand, modes,
975 constraints, insn, op_costs, allocno_pref);
980 /* Process one insn INSN. Scan it and record each time it would save
981 code to put a certain allocnos in a certain class. Return the last
982 insn processed, so that the scan can be continued from there. */
983 static rtx
984 scan_one_insn (rtx insn)
986 enum rtx_code pat_code;
987 rtx set, note;
988 int i, k;
990 if (!INSN_P (insn))
991 return insn;
993 pat_code = GET_CODE (PATTERN (insn));
994 if (pat_code == USE || pat_code == CLOBBER || pat_code == ASM_INPUT
995 || pat_code == ADDR_VEC || pat_code == ADDR_DIFF_VEC)
996 return insn;
998 set = single_set (insn);
999 extract_insn (insn);
1001 /* If this insn loads a parameter from its stack slot, then it
1002 represents a savings, rather than a cost, if the parameter is
1003 stored in memory. Record this fact. */
1004 if (set != 0 && REG_P (SET_DEST (set)) && MEM_P (SET_SRC (set))
1005 && (note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != NULL_RTX
1006 && MEM_P (XEXP (note, 0)))
1008 COSTS_OF_ALLOCNO (total_costs,
1009 ALLOCNO_NUM (ira_curr_regno_allocno_map
1010 [REGNO (SET_DEST (set))]))->mem_cost
1011 -= (memory_move_cost[GET_MODE (SET_DEST (set))][GENERAL_REGS][1]
1012 * frequency);
1013 record_address_regs (GET_MODE (SET_SRC (set)), XEXP (SET_SRC (set), 0),
1014 0, MEM, SCRATCH, frequency * 2);
1017 record_operand_costs (insn, op_costs, allocno_pref);
1019 /* Now add the cost for each operand to the total costs for its
1020 allocno. */
1021 for (i = 0; i < recog_data.n_operands; i++)
1022 if (REG_P (recog_data.operand[i])
1023 && REGNO (recog_data.operand[i]) >= FIRST_PSEUDO_REGISTER)
1025 int regno = REGNO (recog_data.operand[i]);
1026 struct costs *p
1027 = COSTS_OF_ALLOCNO (total_costs,
1028 ALLOCNO_NUM (ira_curr_regno_allocno_map[regno]));
1029 struct costs *q = op_costs[i];
1031 p->mem_cost += q->mem_cost;
1032 for (k = 0; k < cost_classes_num; k++)
1033 p->cost[k] += q->cost[k];
1036 return insn;
1041 /* Print allocnos costs to file F. */
1042 static void
1043 print_costs (FILE *f)
1045 int k;
1046 allocno_t a;
1047 allocno_iterator ai;
1049 fprintf (f, "\n");
1050 FOR_EACH_ALLOCNO (a, ai)
1052 int i, class;
1053 basic_block bb;
1054 int regno = ALLOCNO_REGNO (a);
1056 i = ALLOCNO_NUM (a);
1057 fprintf (f, " a%d(r%d,", i, regno);
1058 if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
1059 fprintf (f, "b%d", bb->index);
1060 else
1061 fprintf (f, "l%d", ALLOCNO_LOOP_TREE_NODE (a)->loop->num);
1062 fprintf (f, ") costs:");
1063 for (k = 0; k < cost_classes_num; k++)
1065 class = cost_classes[k];
1066 if (contains_reg_of_mode[class][PSEUDO_REGNO_MODE (regno)]
1067 #ifdef FORBIDDEN_INC_DEC_CLASSES
1068 && (! in_inc_dec[i] || ! forbidden_inc_dec_class[class])
1069 #endif
1070 #ifdef CANNOT_CHANGE_MODE_CLASS
1071 && ! invalid_mode_change_p (regno, (enum reg_class) class,
1072 PSEUDO_REGNO_MODE (regno))
1073 #endif
1075 fprintf (f, " %s:%d", reg_class_names[class],
1076 COSTS_OF_ALLOCNO (total_costs, i)->cost[k]);
1078 fprintf (f, " MEM:%i\n", COSTS_OF_ALLOCNO (total_costs, i)->mem_cost);
1082 /* The function traverses BB represented by LOOP_TREE_NODE to update
1083 the allocno costs. */
1084 static void
1085 process_bb_node_for_costs (loop_tree_node_t loop_tree_node)
1087 basic_block bb;
1088 rtx insn;
1090 bb = loop_tree_node->bb;
1091 if (bb == NULL)
1092 return;
1093 frequency = REG_FREQ_FROM_BB (bb);
1094 if (frequency == 0)
1095 frequency = 1;
1096 FOR_BB_INSNS (bb, insn)
1097 insn = scan_one_insn (insn);
1100 /* Find costs of register classes and memory for allocnos and their
1101 best costs. */
1102 static void
1103 find_allocno_class_costs (void)
1105 int i, k;
1106 int pass;
1107 basic_block bb;
1109 init_recog ();
1110 #ifdef FORBIDDEN_INC_DEC_CLASSES
1111 in_inc_dec = ira_allocate (sizeof (char) * allocnos_num);
1112 #endif /* FORBIDDEN_INC_DEC_CLASSES */
1114 allocno_pref = NULL;
1115 /* Normally we scan the insns once and determine the best class to
1116 use for each allocno. However, if -fexpensive-optimizations are
1117 on, we do so twice, the second time using the tentative best
1118 classes to guide the selection. */
1119 for (pass = 0; pass <= flag_expensive_optimizations; pass++)
1121 if (internal_flag_ira_verbose > 0 && ira_dump_file)
1122 fprintf (ira_dump_file, "\nPass %i for finding allocno costs\n\n",
1123 pass);
1124 if (optimize)
1126 /* We could use only cover classes on the 1st iteration.
1127 Unfortunately it does not work well for some targets where
1128 some subclass of cover class is costly and wrong cover class
1129 is chosen on the first iteration and it can not be fixed on
1130 the 2nd iteration. */
1131 for (cost_classes_num = 0;
1132 cost_classes_num < important_classes_num;
1133 cost_classes_num++)
1135 cost_classes[cost_classes_num]
1136 = important_classes[cost_classes_num];
1137 cost_class_nums[cost_classes[cost_classes_num]]
1138 = cost_classes_num;
1141 else
1143 for (cost_classes_num = 0;
1144 cost_classes_num < reg_class_cover_size;
1145 cost_classes_num++)
1147 cost_classes[cost_classes_num]
1148 = reg_class_cover[cost_classes_num];
1149 cost_class_nums[cost_classes[cost_classes_num]]
1150 = cost_classes_num;
1153 struct_costs_size
1154 = sizeof (struct costs) + sizeof (int) * (cost_classes_num - 1);
1155 /* Zero out our accumulation of the cost of each class for each
1156 allocno. */
1157 memset (total_costs, 0, allocnos_num * struct_costs_size);
1158 #ifdef FORBIDDEN_INC_DEC_CLASSES
1159 memset (in_inc_dec, 0, allocnos_num * sizeof (char));
1160 #endif
1162 /* Scan the instructions and record each time it would save code
1163 to put a certain allocno in a certain class. */
1164 traverse_loop_tree (TRUE, ira_loop_tree_root,
1165 process_bb_node_for_costs, NULL);
1167 if (pass == 0)
1168 allocno_pref = allocno_pref_buffer;
1170 /* Now for each allocno look at how desirable each class is and
1171 find which class is preferred. */
1172 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
1174 allocno_t a, father_a;
1175 int class, a_num, father_a_num;
1176 loop_tree_node_t father;
1177 int best_cost;
1178 enum reg_class best, alt_class, common_class;
1179 #ifdef FORBIDDEN_INC_DEC_CLASSES
1180 int inc_dec_p = FALSE;
1181 #endif
1183 if (regno_allocno_map[i] == NULL)
1184 continue;
1185 memset (temp_costs, 0, struct_costs_size);
1186 /* Find cost of all allocnos with the same regno. */
1187 for (a = regno_allocno_map[i];
1188 a != NULL;
1189 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
1191 a_num = ALLOCNO_NUM (a);
1192 if ((flag_ira_algorithm == IRA_ALGORITHM_REGIONAL
1193 || flag_ira_algorithm == IRA_ALGORITHM_MIXED)
1194 && (father = ALLOCNO_LOOP_TREE_NODE (a)->father) != NULL
1195 && (father_a = father->regno_allocno_map[i]) != NULL)
1197 /* Propagate costs to upper levels in the region
1198 tree. */
1199 father_a_num = ALLOCNO_NUM (father_a);
1200 for (k = 0; k < cost_classes_num; k++)
1201 COSTS_OF_ALLOCNO (total_costs, father_a_num)->cost[k]
1202 += COSTS_OF_ALLOCNO (total_costs, a_num)->cost[k];
1203 COSTS_OF_ALLOCNO (total_costs, father_a_num)->mem_cost
1204 += COSTS_OF_ALLOCNO (total_costs, a_num)->mem_cost;
1206 for (k = 0; k < cost_classes_num; k++)
1207 temp_costs->cost[k]
1208 += COSTS_OF_ALLOCNO (total_costs, a_num)->cost[k];
1209 temp_costs->mem_cost
1210 += COSTS_OF_ALLOCNO (total_costs, a_num)->mem_cost;
1211 #ifdef FORBIDDEN_INC_DEC_CLASSES
1212 if (in_inc_dec[a_num])
1213 inc_dec_p = TRUE;
1214 #endif
1216 best_cost = (1 << (HOST_BITS_PER_INT - 2)) - 1;
1217 best = ALL_REGS;
1218 alt_class = NO_REGS;
1219 /* Find best common class for all allocnos with the same
1220 regno. */
1221 for (k = 0; k < cost_classes_num; k++)
1223 class = cost_classes[k];
1224 /* Ignore classes that are too small for this operand or
1225 invalid for an operand that was auto-incremented. */
1226 if (! contains_reg_of_mode[class][PSEUDO_REGNO_MODE (i)]
1227 #ifdef FORBIDDEN_INC_DEC_CLASSES
1228 || (inc_dec_p && forbidden_inc_dec_class[class])
1229 #endif
1230 #ifdef CANNOT_CHANGE_MODE_CLASS
1231 || invalid_mode_change_p (i, (enum reg_class) class,
1232 PSEUDO_REGNO_MODE (i))
1233 #endif
1235 continue;
1236 if (temp_costs->cost[k] < best_cost)
1238 best_cost = temp_costs->cost[k];
1239 best = (enum reg_class) class;
1241 else if (temp_costs->cost[k] == best_cost)
1242 best = reg_class_union[best][class];
1243 if (pass == flag_expensive_optimizations
1244 && temp_costs->cost[k] < temp_costs->mem_cost
1245 && (reg_class_size[reg_class_subunion[alt_class][class]]
1246 > reg_class_size[alt_class]))
1247 alt_class = reg_class_subunion[alt_class][class];
1249 if (pass == flag_expensive_optimizations)
1251 if (best_cost > temp_costs->mem_cost)
1252 best = alt_class = NO_REGS;
1253 else if (best == alt_class)
1254 alt_class = NO_REGS;
1255 setup_reg_classes (i, best, alt_class);
1256 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
1257 fprintf (ira_dump_file,
1258 " r%d: preferred %s, alternative %s\n",
1259 i, reg_class_names[best], reg_class_names[alt_class]);
1261 if (best_cost > temp_costs->mem_cost)
1262 common_class = NO_REGS;
1263 else
1264 /* Make the common class a cover class. Remember all
1265 allocnos with the same regno should have the same cover
1266 class. */
1267 common_class = class_translate[best];
1268 for (a = regno_allocno_map[i];
1269 a != NULL;
1270 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
1272 a_num = ALLOCNO_NUM (a);
1273 if (common_class == NO_REGS)
1274 best = NO_REGS;
1275 else
1277 /* Finding best class which is subset of the common
1278 class. */
1279 best_cost = (1 << (HOST_BITS_PER_INT - 2)) - 1;
1280 best = ALL_REGS;
1281 for (k = 0; k < cost_classes_num; k++)
1283 class = cost_classes[k];
1284 if (! class_subset_p[class][common_class])
1285 continue;
1286 /* Ignore classes that are too small for this
1287 operand or invalid for an operand that was
1288 auto-incremented. */
1289 if (! contains_reg_of_mode[class][PSEUDO_REGNO_MODE (i)]
1290 #ifdef FORBIDDEN_INC_DEC_CLASSES
1291 || (inc_dec_p && forbidden_inc_dec_class[class])
1292 #endif
1293 #ifdef CANNOT_CHANGE_MODE_CLASS
1294 || invalid_mode_change_p (i, (enum reg_class) class,
1295 PSEUDO_REGNO_MODE (i))
1296 #endif
1299 else if (COSTS_OF_ALLOCNO (total_costs, a_num)->cost[k]
1300 < best_cost)
1302 best_cost
1303 = COSTS_OF_ALLOCNO (total_costs, a_num)->cost[k];
1304 best = (enum reg_class) class;
1306 else if (COSTS_OF_ALLOCNO (total_costs, a_num)->cost[k]
1307 == best_cost)
1308 best = reg_class_union[best][class];
1311 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL
1312 && (pass == 0 || allocno_pref[a_num] != best))
1314 fprintf (ira_dump_file, " a%d (r%d,", a_num, i);
1315 if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
1316 fprintf (ira_dump_file, "b%d", bb->index);
1317 else
1318 fprintf (ira_dump_file, "l%d",
1319 ALLOCNO_LOOP_TREE_NODE (a)->loop->num);
1320 fprintf (ira_dump_file, ") best %s, cover %s\n",
1321 reg_class_names[best],
1322 reg_class_names[class_translate[best]]);
1324 allocno_pref[a_num] = best;
1328 if (internal_flag_ira_verbose > 4 && ira_dump_file)
1330 print_costs (ira_dump_file);
1331 fprintf (ira_dump_file,"\n");
1335 #ifdef FORBIDDEN_INC_DEC_CLASSES
1336 ira_free (in_inc_dec);
1337 #endif
1342 /* Process moves involving hard regs to modify allocno hard register
1343 costs. We can do this only after determining allocno cover class.
1344 If a hard register forms a register class, than moves with the hard
1345 register are already taken into account in class costs for the
1346 allocno. */
1347 static void
1348 process_bb_node_for_hard_reg_moves (loop_tree_node_t loop_tree_node)
1350 int i, freq, cost, src_regno, dst_regno, hard_regno, to_p;
1351 allocno_t a;
1352 enum reg_class class, cover_class, hard_reg_class;
1353 enum machine_mode mode;
1354 basic_block bb;
1355 rtx insn, set, src, dst;
1357 bb = loop_tree_node->bb;
1358 if (bb == NULL)
1359 return;
1360 freq = REG_FREQ_FROM_BB (bb);
1361 if (freq == 0)
1362 freq = 1;
1363 FOR_BB_INSNS (bb, insn)
1365 if (! INSN_P (insn))
1366 continue;
1367 set = single_set (insn);
1368 if (set == NULL_RTX)
1369 continue;
1370 dst = SET_DEST (set);
1371 src = SET_SRC (set);
1372 if (! REG_P (dst) || ! REG_P (src))
1373 continue;
1374 dst_regno = REGNO (dst);
1375 src_regno = REGNO (src);
1376 if (dst_regno >= FIRST_PSEUDO_REGISTER
1377 && src_regno < FIRST_PSEUDO_REGISTER)
1379 hard_regno = src_regno;
1380 to_p = TRUE;
1381 a = ira_curr_regno_allocno_map[dst_regno];
1383 else if (src_regno >= FIRST_PSEUDO_REGISTER
1384 && dst_regno < FIRST_PSEUDO_REGISTER)
1386 hard_regno = dst_regno;
1387 to_p = FALSE;
1388 a = ira_curr_regno_allocno_map[src_regno];
1390 else
1391 continue;
1392 class = ALLOCNO_COVER_CLASS (a);
1393 if (! TEST_HARD_REG_BIT (reg_class_contents[class], hard_regno))
1394 continue;
1395 i = class_hard_reg_index[class][hard_regno];
1396 if (i < 0)
1397 continue;
1398 mode = ALLOCNO_MODE (a);
1399 hard_reg_class = REGNO_REG_CLASS (hard_regno);
1400 cost = (to_p ? register_move_cost[mode][hard_reg_class][class]
1401 : register_move_cost[mode][class][hard_reg_class]) * freq;
1402 allocate_and_set_costs (&ALLOCNO_HARD_REG_COSTS (a), class,
1403 ALLOCNO_COVER_CLASS_COST (a));
1404 allocate_and_set_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a), class, 0);
1405 ALLOCNO_HARD_REG_COSTS (a)[i] -= cost;
1406 ALLOCNO_CONFLICT_HARD_REG_COSTS (a)[i] -= cost;
1407 ALLOCNO_COVER_CLASS_COST (a) = MIN (ALLOCNO_COVER_CLASS_COST (a),
1408 ALLOCNO_HARD_REG_COSTS (a)[i]);
1409 if (flag_ira_algorithm == IRA_ALGORITHM_REGIONAL
1410 || flag_ira_algorithm == IRA_ALGORITHM_MIXED)
1412 /* Propagate changes to the upper levels in the region
1413 tree. */
1414 loop_tree_node_t father;
1415 int regno = ALLOCNO_REGNO (a);
1417 for (;;)
1419 if ((father = ALLOCNO_LOOP_TREE_NODE (a)->father) == NULL)
1420 break;
1421 if ((a = father->regno_allocno_map[regno]) == NULL)
1422 break;
1423 cover_class = ALLOCNO_COVER_CLASS (a);
1424 allocate_and_set_costs
1425 (&ALLOCNO_HARD_REG_COSTS (a), cover_class,
1426 ALLOCNO_COVER_CLASS_COST (a));
1427 allocate_and_set_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a),
1428 cover_class, 0);
1429 ALLOCNO_HARD_REG_COSTS (a)[i] -= cost;
1430 ALLOCNO_CONFLICT_HARD_REG_COSTS (a)[i] -= cost;
1431 ALLOCNO_COVER_CLASS_COST (a)
1432 = MIN (ALLOCNO_COVER_CLASS_COST (a),
1433 ALLOCNO_HARD_REG_COSTS (a)[i]);
1439 /* After we find hard register and memory costs for allocnos, define
1440 its cover class and modify hard register cost because insns moving
1441 allocno to/from hard registers. */
1442 static void
1443 setup_allocno_cover_class_and_costs (void)
1445 int i, j, n, regno;
1446 int *reg_costs;
1447 enum reg_class cover_class, class;
1448 enum machine_mode mode;
1449 allocno_t a;
1450 allocno_iterator ai;
1452 FOR_EACH_ALLOCNO (a, ai)
1454 i = ALLOCNO_NUM (a);
1455 mode = ALLOCNO_MODE (a);
1456 cover_class = class_translate[allocno_pref[i]];
1457 ira_assert (allocno_pref[i] == NO_REGS || cover_class != NO_REGS);
1458 ALLOCNO_MEMORY_COST (a) = ALLOCNO_UPDATED_MEMORY_COST (a)
1459 = COSTS_OF_ALLOCNO (total_costs, i)->mem_cost;
1460 set_allocno_cover_class (a, cover_class);
1461 if (cover_class == NO_REGS)
1462 continue;
1463 ALLOCNO_AVAILABLE_REGS_NUM (a) = available_class_regs[cover_class];
1464 ALLOCNO_COVER_CLASS_COST (a)
1465 = (COSTS_OF_ALLOCNO (total_costs, i)
1466 ->cost[cost_class_nums[allocno_pref[i]]]);
1467 if (optimize && ALLOCNO_COVER_CLASS (a) != allocno_pref[i])
1469 n = class_hard_regs_num[cover_class];
1470 ALLOCNO_HARD_REG_COSTS (a)
1471 = reg_costs = allocate_cost_vector (cover_class);
1472 for (j = n - 1; j >= 0; j--)
1474 regno = class_hard_regs[cover_class][j];
1475 class = REGNO_REG_CLASS (regno);
1476 reg_costs[j] = (COSTS_OF_ALLOCNO (total_costs, i)
1477 ->cost[cost_class_nums[class]]);
1481 if (optimize)
1482 traverse_loop_tree (TRUE, ira_loop_tree_root,
1483 process_bb_node_for_hard_reg_moves, NULL);
1488 /* Function called once during compiler work. */
1489 void
1490 init_ira_costs_once (void)
1492 int i;
1494 init_cost = NULL;
1495 for (i = 0; i < MAX_RECOG_OPERANDS; i++)
1497 op_costs[i] = NULL;
1498 this_op_costs[i] = NULL;
1500 temp_costs = NULL;
1501 cost_classes = NULL;
1504 /* The function frees allocated temporary cost vectors. */
1505 static void
1506 free_ira_costs (void)
1508 int i;
1510 if (init_cost != NULL)
1511 free (init_cost);
1512 init_cost = NULL;
1513 for (i = 0; i < MAX_RECOG_OPERANDS; i++)
1515 if (op_costs[i] != NULL)
1516 free (op_costs[i]);
1517 if (this_op_costs[i] != NULL)
1518 free (this_op_costs[i]);
1519 op_costs[i] = this_op_costs[i] = NULL;
1521 if (temp_costs != NULL)
1522 free (temp_costs);
1523 temp_costs = NULL;
1524 if (cost_classes != NULL)
1525 free (cost_classes);
1526 cost_classes = NULL;
1529 /* The function called every time when register related information is
1530 changed. */
1531 void
1532 init_ira_costs (void)
1534 int i;
1536 free_ira_costs ();
1537 max_struct_costs_size
1538 = sizeof (struct costs) + sizeof (int) * (important_classes_num - 1);
1539 /* Don't use ira_allocate because vectors live through several IRA calls. */
1540 init_cost = xmalloc (max_struct_costs_size);
1541 init_cost->mem_cost = 1000000;
1542 for (i = 0; i < important_classes_num; i++)
1543 init_cost->cost[i] = 1000000;
1544 for (i = 0; i < MAX_RECOG_OPERANDS; i++)
1546 op_costs[i] = xmalloc (max_struct_costs_size);
1547 this_op_costs[i] = xmalloc (max_struct_costs_size);
1549 temp_costs = xmalloc (max_struct_costs_size);
1550 cost_classes = xmalloc (sizeof (enum reg_class) * important_classes_num);
1553 /* Function called once at the end of compiler work. */
1554 void
1555 finish_ira_costs_once (void)
1557 free_ira_costs ();
1562 /* Entry function which defines cover class, memory and hard register
1563 costs for each allocno. */
1564 void
1565 ira_costs (void)
1567 allocno_t a;
1568 allocno_iterator ai;
1570 total_costs = ira_allocate (max_struct_costs_size * allocnos_num);
1571 allocno_pref_buffer = ira_allocate (sizeof (enum reg_class) * allocnos_num);
1572 find_allocno_class_costs ();
1573 setup_allocno_cover_class_and_costs ();
1574 /* Because we could process operands only as subregs, check mode of
1575 the registers themselves too. */
1576 FOR_EACH_ALLOCNO (a, ai)
1577 if (register_move_cost[ALLOCNO_MODE (a)] == NULL
1578 && have_regs_of_mode[ALLOCNO_MODE (a)])
1579 init_register_move_cost (ALLOCNO_MODE (a));
1580 ira_free (allocno_pref_buffer);
1581 ira_free (total_costs);
1586 /* This function changes hard register costs for allocnos which lives
1587 through function calls. The function is called only when we found
1588 all intersected calls during building allocno live ranges. */
1589 void
1590 tune_allocno_costs_and_cover_classes (void)
1592 int j, k, n, regno, freq;
1593 int cost, min_cost, *reg_costs;
1594 enum reg_class cover_class, class;
1595 enum machine_mode mode;
1596 allocno_t a;
1597 rtx call, *allocno_calls;
1598 HARD_REG_SET clobbered_regs;
1599 allocno_iterator ai;
1601 FOR_EACH_ALLOCNO (a, ai)
1603 cover_class = ALLOCNO_COVER_CLASS (a);
1604 if (cover_class == NO_REGS)
1605 continue;
1606 mode = ALLOCNO_MODE (a);
1607 n = class_hard_regs_num[cover_class];
1608 min_cost = INT_MAX;
1609 if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
1611 allocate_and_set_costs
1612 (&ALLOCNO_HARD_REG_COSTS (a), cover_class,
1613 ALLOCNO_COVER_CLASS_COST (a));
1614 reg_costs = ALLOCNO_HARD_REG_COSTS (a);
1615 for (j = n - 1; j >= 0; j--)
1617 regno = class_hard_regs[cover_class][j];
1618 class = REGNO_REG_CLASS (regno);
1619 cost = 0;
1620 if (! flag_ira_ipra)
1622 /* ??? If only part is call clobbered. */
1623 if (! hard_reg_not_in_set_p (regno, mode, call_used_reg_set))
1625 cost += (ALLOCNO_CALL_FREQ (a)
1626 * (memory_move_cost[mode][class][0]
1627 + memory_move_cost[mode][class][1]));
1630 else
1632 allocno_calls
1633 = (VEC_address (rtx, regno_calls[ALLOCNO_REGNO (a)])
1634 + ALLOCNO_CALLS_CROSSED_START (a));
1635 ira_assert (allocno_calls != NULL);
1636 for (k = ALLOCNO_CALLS_CROSSED_NUM (a) - 1; k >= 0; k--)
1638 call = allocno_calls[k];
1639 freq = REG_FREQ_FROM_BB (BLOCK_FOR_INSN (call));
1640 if (freq == 0)
1641 freq = 1;
1642 get_call_invalidated_used_regs (call, &clobbered_regs,
1643 FALSE);
1644 /* ??? If only part is call clobbered. */
1645 if (! hard_reg_not_in_set_p (regno, mode,
1646 clobbered_regs))
1647 cost
1648 += freq * (memory_move_cost[mode][class][0]
1649 + memory_move_cost[mode][class][1]);
1652 #ifdef IRA_HARD_REGNO_ADD_COST_MULTIPLIER
1653 cost += ((memory_move_cost[mode][class][0]
1654 + memory_move_cost[mode][class][1]) * ALLOCNO_FREQ (a)
1655 * IRA_HARD_REGNO_ADD_COST_MULTIPLIER (regno) / 2);
1656 #endif
1657 reg_costs[j] += cost;
1658 if (min_cost > reg_costs[j])
1659 min_cost = reg_costs[j];
1662 if (min_cost != INT_MAX)
1663 ALLOCNO_COVER_CLASS_COST (a) = min_cost;