new tests
[official-gcc.git] / gcc / ira-costs.c
blobf517386ceef0c2525cecb0cbaa22869ddd1ac725
1 /* IRA hard register and memory cost calculation for allocnos or pseudos.
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "hard-reg-set.h"
27 #include "rtl.h"
28 #include "expr.h"
29 #include "tm_p.h"
30 #include "flags.h"
31 #include "basic-block.h"
32 #include "regs.h"
33 #include "addresses.h"
34 #include "insn-config.h"
35 #include "recog.h"
36 #include "reload.h"
37 #include "diagnostic-core.h"
38 #include "target.h"
39 #include "params.h"
40 #include "ira-int.h"
42 /* The flags is set up every time when we calculate pseudo register
43 classes through function ira_set_pseudo_classes. */
44 static bool pseudo_classes_defined_p = false;
46 /* TRUE if we work with allocnos. Otherwise we work with pseudos. */
47 static bool allocno_p;
49 /* Number of elements in arrays `in_inc_dec' and `costs'. */
50 static int cost_elements_num;
52 #ifdef FORBIDDEN_INC_DEC_CLASSES
53 /* Indexed by n, is TRUE if allocno or pseudo with number N is used in
54 an auto-inc or auto-dec context. */
55 static bool *in_inc_dec;
56 #endif
58 /* The `costs' struct records the cost of using hard registers of each
59 class considered for the calculation and of using memory for each
60 allocno or pseudo. */
61 struct costs
63 int mem_cost;
64 /* Costs for register classes start here. We process only some
65 allocno classes. */
66 int cost[1];
69 #define max_struct_costs_size \
70 (this_target_ira_int->x_max_struct_costs_size)
71 #define init_cost \
72 (this_target_ira_int->x_init_cost)
73 #define temp_costs \
74 (this_target_ira_int->x_temp_costs)
75 #define op_costs \
76 (this_target_ira_int->x_op_costs)
77 #define this_op_costs \
78 (this_target_ira_int->x_this_op_costs)
80 /* Costs of each class for each allocno or pseudo. */
81 static struct costs *costs;
83 /* Accumulated costs of each class for each allocno. */
84 static struct costs *total_allocno_costs;
86 /* It is the current size of struct costs. */
87 static int struct_costs_size;
89 /* Return pointer to structure containing costs of allocno or pseudo
90 with given NUM in array ARR. */
91 #define COSTS(arr, num) \
92 ((struct costs *) ((char *) (arr) + (num) * struct_costs_size))
94 /* Return index in COSTS when processing reg with REGNO. */
95 #define COST_INDEX(regno) (allocno_p \
96 ? ALLOCNO_NUM (ira_curr_regno_allocno_map[regno]) \
97 : (int) regno)
99 /* Record register class preferences of each allocno or pseudo. Null
100 value means no preferences. It happens on the 1st iteration of the
101 cost calculation. */
102 static enum reg_class *pref;
104 /* Allocated buffers for pref. */
105 static enum reg_class *pref_buffer;
107 /* Record allocno class of each allocno with the same regno. */
108 static enum reg_class *regno_aclass;
110 /* Record cost gains for not allocating a register with an invariant
111 equivalence. */
112 static int *regno_equiv_gains;
114 /* Execution frequency of the current insn. */
115 static int frequency;
119 /* Info about reg classes whose costs are calculated for a pseudo. */
120 struct cost_classes
122 /* Number of the cost classes in the subsequent array. */
123 int num;
124 /* Container of the cost classes. */
125 enum reg_class classes[N_REG_CLASSES];
126 /* Map reg class -> index of the reg class in the previous array.
127 -1 if it is not a cost classe. */
128 int index[N_REG_CLASSES];
129 /* Map hard regno index of first class in array CLASSES containing
130 the hard regno, -1 otherwise. */
131 int hard_regno_index[FIRST_PSEUDO_REGISTER];
134 /* Types of pointers to the structure above. */
135 typedef struct cost_classes *cost_classes_t;
136 typedef const struct cost_classes *const_cost_classes_t;
138 /* Info about cost classes for each pseudo. */
139 static cost_classes_t *regno_cost_classes;
141 /* Returns hash value for cost classes info V. */
142 static hashval_t
143 cost_classes_hash (const void *v)
145 const_cost_classes_t hv = (const_cost_classes_t) v;
147 return iterative_hash (&hv->classes, sizeof (enum reg_class) * hv->num, 0);
150 /* Compares cost classes info V1 and V2. */
151 static int
152 cost_classes_eq (const void *v1, const void *v2)
154 const_cost_classes_t hv1 = (const_cost_classes_t) v1;
155 const_cost_classes_t hv2 = (const_cost_classes_t) v2;
157 return hv1->num == hv2->num && memcmp (hv1->classes, hv2->classes,
158 sizeof (enum reg_class) * hv1->num);
161 /* Delete cost classes info V from the hash table. */
162 static void
163 cost_classes_del (void *v)
165 ira_free (v);
168 /* Hash table of unique cost classes. */
169 static htab_t cost_classes_htab;
171 /* Map allocno class -> cost classes for pseudo of given allocno
172 class. */
173 static cost_classes_t cost_classes_aclass_cache[N_REG_CLASSES];
175 /* Map mode -> cost classes for pseudo of give mode. */
176 static cost_classes_t cost_classes_mode_cache[MAX_MACHINE_MODE];
178 /* Initialize info about the cost classes for each pseudo. */
179 static void
180 initiate_regno_cost_classes (void)
182 int size = sizeof (cost_classes_t) * max_reg_num ();
184 regno_cost_classes = (cost_classes_t *) ira_allocate (size);
185 memset (regno_cost_classes, 0, size);
186 memset (cost_classes_aclass_cache, 0,
187 sizeof (cost_classes_t) * N_REG_CLASSES);
188 memset (cost_classes_mode_cache, 0,
189 sizeof (cost_classes_t) * MAX_MACHINE_MODE);
190 cost_classes_htab
191 = htab_create (200, cost_classes_hash, cost_classes_eq, cost_classes_del);
194 /* Create new cost classes from cost classes FROM and set up members
195 index and hard_regno_index. Return the new classes. The function
196 implements some common code of two functions
197 setup_regno_cost_classes_by_aclass and
198 setup_regno_cost_classes_by_mode. */
199 static cost_classes_t
200 setup_cost_classes (cost_classes_t from)
202 cost_classes_t classes_ptr;
203 enum reg_class cl;
204 int i, j, hard_regno;
206 classes_ptr = (cost_classes_t) ira_allocate (sizeof (struct cost_classes));
207 classes_ptr->num = from->num;
208 for (i = 0; i < N_REG_CLASSES; i++)
209 classes_ptr->index[i] = -1;
210 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
211 classes_ptr->hard_regno_index[i] = -1;
212 for (i = 0; i < from->num; i++)
214 cl = classes_ptr->classes[i] = from->classes[i];
215 classes_ptr->index[cl] = i;
216 for (j = ira_class_hard_regs_num[cl] - 1; j >= 0; j--)
218 hard_regno = ira_class_hard_regs[cl][j];
219 if (classes_ptr->hard_regno_index[hard_regno] < 0)
220 classes_ptr->hard_regno_index[hard_regno] = i;
223 return classes_ptr;
226 /* Setup cost classes for pseudo REGNO whose allocno class is ACLASS.
227 This function is used when we know an initial approximation of
228 allocno class of the pseudo already, e.g. on the second iteration
229 of class cost calculation or after class cost calculation in
230 register-pressure sensitive insn scheduling or register-pressure
231 sensitive loop-invariant motion. */
232 static void
233 setup_regno_cost_classes_by_aclass (int regno, enum reg_class aclass)
235 static struct cost_classes classes;
236 cost_classes_t classes_ptr;
237 enum reg_class cl;
238 int i;
239 PTR *slot;
240 HARD_REG_SET temp, temp2;
242 if ((classes_ptr = cost_classes_aclass_cache[aclass]) == NULL)
244 COPY_HARD_REG_SET (temp, reg_class_contents[aclass]);
245 AND_COMPL_HARD_REG_SET (temp, ira_no_alloc_regs);
246 classes.num = 0;
247 for (i = 0; i < ira_important_classes_num; i++)
249 cl = ira_important_classes[i];
250 COPY_HARD_REG_SET (temp2, reg_class_contents[cl]);
251 AND_COMPL_HARD_REG_SET (temp2, ira_no_alloc_regs);
252 if (! ira_reg_pressure_class_p[cl]
253 && hard_reg_set_subset_p (temp2, temp) && cl != aclass)
254 continue;
255 classes.classes[classes.num++] = cl;
257 slot = htab_find_slot (cost_classes_htab, &classes, INSERT);
258 if (*slot == NULL)
260 classes_ptr = setup_cost_classes (&classes);
261 *slot = classes_ptr;
263 classes_ptr = cost_classes_aclass_cache[aclass] = (cost_classes_t) *slot;
265 regno_cost_classes[regno] = classes_ptr;
268 /* Setup cost classes for pseudo REGNO with MODE. Usage of MODE can
269 decrease number of cost classes for the pseudo, if hard registers
270 of some important classes can not hold a value of MODE. So the
271 pseudo can not get hard register of some important classes and cost
272 calculation for such important classes is only waisting CPU
273 time. */
274 static void
275 setup_regno_cost_classes_by_mode (int regno, enum machine_mode mode)
277 static struct cost_classes classes;
278 cost_classes_t classes_ptr;
279 enum reg_class cl;
280 int i;
281 PTR *slot;
282 HARD_REG_SET temp;
284 if ((classes_ptr = cost_classes_mode_cache[mode]) == NULL)
286 classes.num = 0;
287 for (i = 0; i < ira_important_classes_num; i++)
289 cl = ira_important_classes[i];
290 COPY_HARD_REG_SET (temp, ira_prohibited_class_mode_regs[cl][mode]);
291 IOR_HARD_REG_SET (temp, ira_no_alloc_regs);
292 if (hard_reg_set_subset_p (reg_class_contents[cl], temp))
293 continue;
294 classes.classes[classes.num++] = cl;
296 slot = htab_find_slot (cost_classes_htab, &classes, INSERT);
297 if (*slot == NULL)
299 classes_ptr = setup_cost_classes (&classes);
300 *slot = classes_ptr;
302 cost_classes_mode_cache[mode] = (cost_classes_t) *slot;
304 regno_cost_classes[regno] = classes_ptr;
307 /* Finilize info about the cost classes for each pseudo. */
308 static void
309 finish_regno_cost_classes (void)
311 ira_free (regno_cost_classes);
312 htab_delete (cost_classes_htab);
317 /* Compute the cost of loading X into (if TO_P is TRUE) or from (if
318 TO_P is FALSE) a register of class RCLASS in mode MODE. X must not
319 be a pseudo register. */
320 static int
321 copy_cost (rtx x, enum machine_mode mode, reg_class_t rclass, bool to_p,
322 secondary_reload_info *prev_sri)
324 secondary_reload_info sri;
325 reg_class_t secondary_class = NO_REGS;
327 /* If X is a SCRATCH, there is actually nothing to move since we are
328 assuming optimal allocation. */
329 if (GET_CODE (x) == SCRATCH)
330 return 0;
332 /* Get the class we will actually use for a reload. */
333 rclass = targetm.preferred_reload_class (x, rclass);
335 /* If we need a secondary reload for an intermediate, the cost is
336 that to load the input into the intermediate register, then to
337 copy it. */
338 sri.prev_sri = prev_sri;
339 sri.extra_cost = 0;
340 secondary_class = targetm.secondary_reload (to_p, x, rclass, mode, &sri);
342 if (secondary_class != NO_REGS)
344 if (!move_cost[mode])
345 init_move_cost (mode);
346 return (move_cost[mode][(int) secondary_class][(int) rclass]
347 + sri.extra_cost
348 + copy_cost (x, mode, secondary_class, to_p, &sri));
351 /* For memory, use the memory move cost, for (hard) registers, use
352 the cost to move between the register classes, and use 2 for
353 everything else (constants). */
354 if (MEM_P (x) || rclass == NO_REGS)
355 return sri.extra_cost
356 + ira_memory_move_cost[mode][(int) rclass][to_p != 0];
357 else if (REG_P (x))
359 if (!move_cost[mode])
360 init_move_cost (mode);
361 return (sri.extra_cost
362 + move_cost[mode][REGNO_REG_CLASS (REGNO (x))][(int) rclass]);
364 else
365 /* If this is a constant, we may eventually want to call rtx_cost
366 here. */
367 return sri.extra_cost + COSTS_N_INSNS (1);
372 /* Record the cost of using memory or hard registers of various
373 classes for the operands in INSN.
375 N_ALTS is the number of alternatives.
376 N_OPS is the number of operands.
377 OPS is an array of the operands.
378 MODES are the modes of the operands, in case any are VOIDmode.
379 CONSTRAINTS are the constraints to use for the operands. This array
380 is modified by this procedure.
382 This procedure works alternative by alternative. For each
383 alternative we assume that we will be able to allocate all allocnos
384 to their ideal register class and calculate the cost of using that
385 alternative. Then we compute, for each operand that is a
386 pseudo-register, the cost of having the allocno allocated to each
387 register class and using it in that alternative. To this cost is
388 added the cost of the alternative.
390 The cost of each class for this insn is its lowest cost among all
391 the alternatives. */
392 static void
393 record_reg_classes (int n_alts, int n_ops, rtx *ops,
394 enum machine_mode *modes, const char **constraints,
395 rtx insn, enum reg_class *pref)
397 int alt;
398 int i, j, k;
399 rtx set;
400 int insn_allows_mem[MAX_RECOG_OPERANDS];
402 for (i = 0; i < n_ops; i++)
403 insn_allows_mem[i] = 0;
405 /* Process each alternative, each time minimizing an operand's cost
406 with the cost for each operand in that alternative. */
407 for (alt = 0; alt < n_alts; alt++)
409 enum reg_class classes[MAX_RECOG_OPERANDS];
410 int allows_mem[MAX_RECOG_OPERANDS];
411 enum reg_class rclass;
412 int alt_fail = 0;
413 int alt_cost = 0, op_cost_add;
415 if (!recog_data.alternative_enabled_p[alt])
417 for (i = 0; i < recog_data.n_operands; i++)
418 constraints[i] = skip_alternative (constraints[i]);
420 continue;
423 for (i = 0; i < n_ops; i++)
425 unsigned char c;
426 const char *p = constraints[i];
427 rtx op = ops[i];
428 enum machine_mode mode = modes[i];
429 int allows_addr = 0;
430 int win = 0;
432 /* Initially show we know nothing about the register class. */
433 classes[i] = NO_REGS;
434 allows_mem[i] = 0;
436 /* If this operand has no constraints at all, we can
437 conclude nothing about it since anything is valid. */
438 if (*p == 0)
440 if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
441 memset (this_op_costs[i], 0, struct_costs_size);
442 continue;
445 /* If this alternative is only relevant when this operand
446 matches a previous operand, we do different things
447 depending on whether this operand is a allocno-reg or not.
448 We must process any modifiers for the operand before we
449 can make this test. */
450 while (*p == '%' || *p == '=' || *p == '+' || *p == '&')
451 p++;
453 if (p[0] >= '0' && p[0] <= '0' + i && (p[1] == ',' || p[1] == 0))
455 /* Copy class and whether memory is allowed from the
456 matching alternative. Then perform any needed cost
457 computations and/or adjustments. */
458 j = p[0] - '0';
459 classes[i] = classes[j];
460 allows_mem[i] = allows_mem[j];
461 if (allows_mem[i])
462 insn_allows_mem[i] = 1;
464 if (! REG_P (op) || REGNO (op) < FIRST_PSEUDO_REGISTER)
466 /* If this matches the other operand, we have no
467 added cost and we win. */
468 if (rtx_equal_p (ops[j], op))
469 win = 1;
470 /* If we can put the other operand into a register,
471 add to the cost of this alternative the cost to
472 copy this operand to the register used for the
473 other operand. */
474 else if (classes[j] != NO_REGS)
476 alt_cost += copy_cost (op, mode, classes[j], 1, NULL);
477 win = 1;
480 else if (! REG_P (ops[j])
481 || REGNO (ops[j]) < FIRST_PSEUDO_REGISTER)
483 /* This op is an allocno but the one it matches is
484 not. */
486 /* If we can't put the other operand into a
487 register, this alternative can't be used. */
489 if (classes[j] == NO_REGS)
490 alt_fail = 1;
491 /* Otherwise, add to the cost of this alternative
492 the cost to copy the other operand to the hard
493 register used for this operand. */
494 else
495 alt_cost += copy_cost (ops[j], mode, classes[j], 1, NULL);
497 else
499 /* The costs of this operand are not the same as the
500 other operand since move costs are not symmetric.
501 Moreover, if we cannot tie them, this alternative
502 needs to do a copy, which is one insn. */
503 struct costs *pp = this_op_costs[i];
504 int *pp_costs = pp->cost;
505 cost_classes_t cost_classes_ptr
506 = regno_cost_classes[REGNO (op)];
507 enum reg_class *cost_classes = cost_classes_ptr->classes;
508 bool in_p = recog_data.operand_type[i] != OP_OUT;
509 bool out_p = recog_data.operand_type[i] != OP_IN;
510 enum reg_class op_class = classes[i];
511 move_table *move_in_cost, *move_out_cost;
513 ira_init_register_move_cost_if_necessary (mode);
514 if (! in_p)
516 ira_assert (out_p);
517 move_out_cost = ira_may_move_out_cost[mode];
518 for (k = cost_classes_ptr->num - 1; k >= 0; k--)
520 rclass = cost_classes[k];
521 pp_costs[k]
522 = move_out_cost[op_class][rclass] * frequency;
525 else if (! out_p)
527 ira_assert (in_p);
528 move_in_cost = ira_may_move_in_cost[mode];
529 for (k = cost_classes_ptr->num - 1; k >= 0; k--)
531 rclass = cost_classes[k];
532 pp_costs[k]
533 = move_in_cost[rclass][op_class] * frequency;
536 else
538 move_in_cost = ira_may_move_in_cost[mode];
539 move_out_cost = ira_may_move_out_cost[mode];
540 for (k = cost_classes_ptr->num - 1; k >= 0; k--)
542 rclass = cost_classes[k];
543 pp_costs[k] = ((move_in_cost[rclass][op_class]
544 + move_out_cost[op_class][rclass])
545 * frequency);
549 /* If the alternative actually allows memory, make
550 things a bit cheaper since we won't need an extra
551 insn to load it. */
552 pp->mem_cost
553 = ((out_p ? ira_memory_move_cost[mode][op_class][0] : 0)
554 + (in_p ? ira_memory_move_cost[mode][op_class][1] : 0)
555 - allows_mem[i]) * frequency;
557 /* If we have assigned a class to this allocno in
558 our first pass, add a cost to this alternative
559 corresponding to what we would add if this
560 allocno were not in the appropriate class. */
561 if (pref)
563 enum reg_class pref_class = pref[COST_INDEX (REGNO (op))];
565 if (pref_class == NO_REGS)
566 alt_cost
567 += ((out_p
568 ? ira_memory_move_cost[mode][op_class][0] : 0)
569 + (in_p
570 ? ira_memory_move_cost[mode][op_class][1]
571 : 0));
572 else if (ira_reg_class_intersect
573 [pref_class][op_class] == NO_REGS)
574 alt_cost
575 += ira_register_move_cost[mode][pref_class][op_class];
577 if (REGNO (ops[i]) != REGNO (ops[j])
578 && ! find_reg_note (insn, REG_DEAD, op))
579 alt_cost += 2;
581 /* This is in place of ordinary cost computation for
582 this operand, so skip to the end of the
583 alternative (should be just one character). */
584 while (*p && *p++ != ',')
587 constraints[i] = p;
588 continue;
592 /* Scan all the constraint letters. See if the operand
593 matches any of the constraints. Collect the valid
594 register classes and see if this operand accepts
595 memory. */
596 while ((c = *p))
598 switch (c)
600 case ',':
601 break;
602 case '*':
603 /* Ignore the next letter for this pass. */
604 c = *++p;
605 break;
607 case '?':
608 alt_cost += 2;
609 case '!': case '#': case '&':
610 case '0': case '1': case '2': case '3': case '4':
611 case '5': case '6': case '7': case '8': case '9':
612 break;
614 case 'p':
615 allows_addr = 1;
616 win = address_operand (op, GET_MODE (op));
617 /* We know this operand is an address, so we want it
618 to be allocated to a register that can be the
619 base of an address, i.e. BASE_REG_CLASS. */
620 classes[i]
621 = ira_reg_class_subunion[classes[i]]
622 [base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
623 break;
625 case 'm': case 'o': case 'V':
626 /* It doesn't seem worth distinguishing between
627 offsettable and non-offsettable addresses
628 here. */
629 insn_allows_mem[i] = allows_mem[i] = 1;
630 if (MEM_P (op))
631 win = 1;
632 break;
634 case '<':
635 if (MEM_P (op)
636 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
637 || GET_CODE (XEXP (op, 0)) == POST_DEC))
638 win = 1;
639 break;
641 case '>':
642 if (MEM_P (op)
643 && (GET_CODE (XEXP (op, 0)) == PRE_INC
644 || GET_CODE (XEXP (op, 0)) == POST_INC))
645 win = 1;
646 break;
648 case 'E':
649 case 'F':
650 if (GET_CODE (op) == CONST_DOUBLE
651 || (GET_CODE (op) == CONST_VECTOR
652 && (GET_MODE_CLASS (GET_MODE (op))
653 == MODE_VECTOR_FLOAT)))
654 win = 1;
655 break;
657 case 'G':
658 case 'H':
659 if (GET_CODE (op) == CONST_DOUBLE
660 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
661 win = 1;
662 break;
664 case 's':
665 if (CONST_INT_P (op)
666 || (GET_CODE (op) == CONST_DOUBLE
667 && GET_MODE (op) == VOIDmode))
668 break;
670 case 'i':
671 if (CONSTANT_P (op)
672 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op)))
673 win = 1;
674 break;
676 case 'n':
677 if (CONST_INT_P (op)
678 || (GET_CODE (op) == CONST_DOUBLE
679 && GET_MODE (op) == VOIDmode))
680 win = 1;
681 break;
683 case 'I':
684 case 'J':
685 case 'K':
686 case 'L':
687 case 'M':
688 case 'N':
689 case 'O':
690 case 'P':
691 if (CONST_INT_P (op)
692 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
693 win = 1;
694 break;
696 case 'X':
697 win = 1;
698 break;
700 case 'g':
701 if (MEM_P (op)
702 || (CONSTANT_P (op)
703 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))))
704 win = 1;
705 insn_allows_mem[i] = allows_mem[i] = 1;
706 case 'r':
707 classes[i] = ira_reg_class_subunion[classes[i]][GENERAL_REGS];
708 break;
710 default:
711 if (REG_CLASS_FROM_CONSTRAINT (c, p) != NO_REGS)
712 classes[i] = ira_reg_class_subunion[classes[i]]
713 [REG_CLASS_FROM_CONSTRAINT (c, p)];
714 #ifdef EXTRA_CONSTRAINT_STR
715 else if (EXTRA_CONSTRAINT_STR (op, c, p))
716 win = 1;
718 if (EXTRA_MEMORY_CONSTRAINT (c, p))
720 /* Every MEM can be reloaded to fit. */
721 insn_allows_mem[i] = allows_mem[i] = 1;
722 if (MEM_P (op))
723 win = 1;
725 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
727 /* Every address can be reloaded to fit. */
728 allows_addr = 1;
729 if (address_operand (op, GET_MODE (op)))
730 win = 1;
731 /* We know this operand is an address, so we
732 want it to be allocated to a hard register
733 that can be the base of an address,
734 i.e. BASE_REG_CLASS. */
735 classes[i]
736 = ira_reg_class_subunion[classes[i]]
737 [base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
739 #endif
740 break;
742 p += CONSTRAINT_LEN (c, p);
743 if (c == ',')
744 break;
747 constraints[i] = p;
749 /* How we account for this operand now depends on whether it
750 is a pseudo register or not. If it is, we first check if
751 any register classes are valid. If not, we ignore this
752 alternative, since we want to assume that all allocnos get
753 allocated for register preferencing. If some register
754 class is valid, compute the costs of moving the allocno
755 into that class. */
756 if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
758 if (classes[i] == NO_REGS)
760 /* We must always fail if the operand is a REG, but
761 we did not find a suitable class.
763 Otherwise we may perform an uninitialized read
764 from this_op_costs after the `continue' statement
765 below. */
766 alt_fail = 1;
768 else
770 unsigned int regno = REGNO (op);
771 struct costs *pp = this_op_costs[i];
772 int *pp_costs = pp->cost;
773 cost_classes_t cost_classes_ptr = regno_cost_classes[regno];
774 enum reg_class *cost_classes = cost_classes_ptr->classes;
775 bool in_p = recog_data.operand_type[i] != OP_OUT;
776 bool out_p = recog_data.operand_type[i] != OP_IN;
777 enum reg_class op_class = classes[i];
778 move_table *move_in_cost, *move_out_cost;
780 ira_init_register_move_cost_if_necessary (mode);
781 if (! in_p)
783 ira_assert (out_p);
784 move_out_cost = ira_may_move_out_cost[mode];
785 for (k = cost_classes_ptr->num - 1; k >= 0; k--)
787 rclass = cost_classes[k];
788 pp_costs[k]
789 = move_out_cost[op_class][rclass] * frequency;
792 else if (! out_p)
794 ira_assert (in_p);
795 move_in_cost = ira_may_move_in_cost[mode];
796 for (k = cost_classes_ptr->num - 1; k >= 0; k--)
798 rclass = cost_classes[k];
799 pp_costs[k]
800 = move_in_cost[rclass][op_class] * frequency;
803 else
805 move_in_cost = ira_may_move_in_cost[mode];
806 move_out_cost = ira_may_move_out_cost[mode];
807 for (k = cost_classes_ptr->num - 1; k >= 0; k--)
809 rclass = cost_classes[k];
810 pp_costs[k] = ((move_in_cost[rclass][op_class]
811 + move_out_cost[op_class][rclass])
812 * frequency);
816 /* If the alternative actually allows memory, make
817 things a bit cheaper since we won't need an extra
818 insn to load it. */
819 pp->mem_cost
820 = ((out_p ? ira_memory_move_cost[mode][op_class][0] : 0)
821 + (in_p ? ira_memory_move_cost[mode][op_class][1] : 0)
822 - allows_mem[i]) * frequency;
823 /* If we have assigned a class to this allocno in
824 our first pass, add a cost to this alternative
825 corresponding to what we would add if this
826 allocno were not in the appropriate class. */
827 if (pref)
829 enum reg_class pref_class = pref[COST_INDEX (REGNO (op))];
831 if (pref_class == NO_REGS)
832 alt_cost
833 += ((out_p
834 ? ira_memory_move_cost[mode][op_class][0] : 0)
835 + (in_p
836 ? ira_memory_move_cost[mode][op_class][1]
837 : 0));
838 else if (ira_reg_class_intersect[pref_class][op_class]
839 == NO_REGS)
840 alt_cost += ira_register_move_cost[mode][pref_class][op_class];
845 /* Otherwise, if this alternative wins, either because we
846 have already determined that or if we have a hard
847 register of the proper class, there is no cost for this
848 alternative. */
849 else if (win || (REG_P (op)
850 && reg_fits_class_p (op, classes[i],
851 0, GET_MODE (op))))
854 /* If registers are valid, the cost of this alternative
855 includes copying the object to and/or from a
856 register. */
857 else if (classes[i] != NO_REGS)
859 if (recog_data.operand_type[i] != OP_OUT)
860 alt_cost += copy_cost (op, mode, classes[i], 1, NULL);
862 if (recog_data.operand_type[i] != OP_IN)
863 alt_cost += copy_cost (op, mode, classes[i], 0, NULL);
865 /* The only other way this alternative can be used is if
866 this is a constant that could be placed into memory. */
867 else if (CONSTANT_P (op) && (allows_addr || allows_mem[i]))
868 alt_cost += ira_memory_move_cost[mode][classes[i]][1];
869 else
870 alt_fail = 1;
873 if (alt_fail)
874 continue;
876 op_cost_add = alt_cost * frequency;
877 /* Finally, update the costs with the information we've
878 calculated about this alternative. */
879 for (i = 0; i < n_ops; i++)
880 if (REG_P (ops[i]) && REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
882 struct costs *pp = op_costs[i], *qq = this_op_costs[i];
883 int *pp_costs = pp->cost, *qq_costs = qq->cost;
884 int scale = 1 + (recog_data.operand_type[i] == OP_INOUT);
885 cost_classes_t cost_classes_ptr
886 = regno_cost_classes[REGNO (ops[i])];
888 pp->mem_cost = MIN (pp->mem_cost,
889 (qq->mem_cost + op_cost_add) * scale);
891 for (k = cost_classes_ptr->num - 1; k >= 0; k--)
892 pp_costs[k]
893 = MIN (pp_costs[k], (qq_costs[k] + op_cost_add) * scale);
897 if (allocno_p)
898 for (i = 0; i < n_ops; i++)
900 ira_allocno_t a;
901 rtx op = ops[i];
903 if (! REG_P (op) || REGNO (op) < FIRST_PSEUDO_REGISTER)
904 continue;
905 a = ira_curr_regno_allocno_map [REGNO (op)];
906 if (! ALLOCNO_BAD_SPILL_P (a) && insn_allows_mem[i] == 0)
907 ALLOCNO_BAD_SPILL_P (a) = true;
910 /* If this insn is a single set copying operand 1 to operand 0 and
911 one operand is an allocno with the other a hard reg or an allocno
912 that prefers a hard register that is in its own register class
913 then we may want to adjust the cost of that register class to -1.
915 Avoid the adjustment if the source does not die to avoid
916 stressing of register allocator by preferrencing two colliding
917 registers into single class.
919 Also avoid the adjustment if a copy between hard registers of the
920 class is expensive (ten times the cost of a default copy is
921 considered arbitrarily expensive). This avoids losing when the
922 preferred class is very expensive as the source of a copy
923 instruction. */
924 if ((set = single_set (insn)) != 0
925 && ops[0] == SET_DEST (set) && ops[1] == SET_SRC (set)
926 && REG_P (ops[0]) && REG_P (ops[1])
927 && find_regno_note (insn, REG_DEAD, REGNO (ops[1])))
928 for (i = 0; i <= 1; i++)
929 if (REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER
930 && REGNO (ops[!i]) < FIRST_PSEUDO_REGISTER)
932 unsigned int regno = REGNO (ops[i]);
933 unsigned int other_regno = REGNO (ops[!i]);
934 enum machine_mode mode = GET_MODE (ops[!i]);
935 cost_classes_t cost_classes_ptr = regno_cost_classes[regno];
936 enum reg_class *cost_classes = cost_classes_ptr->classes;
937 enum reg_class rclass;
938 int nr;
940 for (k = cost_classes_ptr->num - 1; k >= 0; k--)
942 rclass = cost_classes[k];
943 if (TEST_HARD_REG_BIT (reg_class_contents[rclass], other_regno)
944 && (reg_class_size[rclass]
945 == (unsigned) CLASS_MAX_NREGS (rclass, mode)))
947 if (reg_class_size[rclass] == 1)
948 op_costs[i]->cost[k] = -frequency;
949 else
951 for (nr = 0;
952 nr < hard_regno_nregs[other_regno][mode];
953 nr++)
954 if (! TEST_HARD_REG_BIT (reg_class_contents[rclass],
955 other_regno + nr))
956 break;
958 if (nr == hard_regno_nregs[other_regno][mode])
959 op_costs[i]->cost[k] = -frequency;
968 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudo registers. */
969 static inline bool
970 ok_for_index_p_nonstrict (rtx reg)
972 unsigned regno = REGNO (reg);
974 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
977 /* A version of regno_ok_for_base_p for use here, when all
978 pseudo-registers should count as OK. Arguments as for
979 regno_ok_for_base_p. */
980 static inline bool
981 ok_for_base_p_nonstrict (rtx reg, enum machine_mode mode,
982 enum rtx_code outer_code, enum rtx_code index_code)
984 unsigned regno = REGNO (reg);
986 if (regno >= FIRST_PSEUDO_REGISTER)
987 return true;
988 return ok_for_base_p_1 (regno, mode, outer_code, index_code);
991 /* Record the pseudo registers we must reload into hard registers in a
992 subexpression of a memory address, X.
994 If CONTEXT is 0, we are looking at the base part of an address,
995 otherwise we are looking at the index part.
997 MODE is the mode of the memory reference; OUTER_CODE and INDEX_CODE
998 give the context that the rtx appears in. These three arguments
999 are passed down to base_reg_class.
1001 SCALE is twice the amount to multiply the cost by (it is twice so
1002 we can represent half-cost adjustments). */
1003 static void
1004 record_address_regs (enum machine_mode mode, rtx x, int context,
1005 enum rtx_code outer_code, enum rtx_code index_code,
1006 int scale)
1008 enum rtx_code code = GET_CODE (x);
1009 enum reg_class rclass;
1011 if (context == 1)
1012 rclass = INDEX_REG_CLASS;
1013 else
1014 rclass = base_reg_class (mode, outer_code, index_code);
1016 switch (code)
1018 case CONST_INT:
1019 case CONST:
1020 case CC0:
1021 case PC:
1022 case SYMBOL_REF:
1023 case LABEL_REF:
1024 return;
1026 case PLUS:
1027 /* When we have an address that is a sum, we must determine
1028 whether registers are "base" or "index" regs. If there is a
1029 sum of two registers, we must choose one to be the "base".
1030 Luckily, we can use the REG_POINTER to make a good choice
1031 most of the time. We only need to do this on machines that
1032 can have two registers in an address and where the base and
1033 index register classes are different.
1035 ??? This code used to set REGNO_POINTER_FLAG in some cases,
1036 but that seems bogus since it should only be set when we are
1037 sure the register is being used as a pointer. */
1039 rtx arg0 = XEXP (x, 0);
1040 rtx arg1 = XEXP (x, 1);
1041 enum rtx_code code0 = GET_CODE (arg0);
1042 enum rtx_code code1 = GET_CODE (arg1);
1044 /* Look inside subregs. */
1045 if (code0 == SUBREG)
1046 arg0 = SUBREG_REG (arg0), code0 = GET_CODE (arg0);
1047 if (code1 == SUBREG)
1048 arg1 = SUBREG_REG (arg1), code1 = GET_CODE (arg1);
1050 /* If this machine only allows one register per address, it
1051 must be in the first operand. */
1052 if (MAX_REGS_PER_ADDRESS == 1)
1053 record_address_regs (mode, arg0, 0, PLUS, code1, scale);
1055 /* If index and base registers are the same on this machine,
1056 just record registers in any non-constant operands. We
1057 assume here, as well as in the tests below, that all
1058 addresses are in canonical form. */
1059 else if (INDEX_REG_CLASS == base_reg_class (VOIDmode, PLUS, SCRATCH))
1061 record_address_regs (mode, arg0, context, PLUS, code1, scale);
1062 if (! CONSTANT_P (arg1))
1063 record_address_regs (mode, arg1, context, PLUS, code0, scale);
1066 /* If the second operand is a constant integer, it doesn't
1067 change what class the first operand must be. */
1068 else if (code1 == CONST_INT || code1 == CONST_DOUBLE)
1069 record_address_regs (mode, arg0, context, PLUS, code1, scale);
1070 /* If the second operand is a symbolic constant, the first
1071 operand must be an index register. */
1072 else if (code1 == SYMBOL_REF || code1 == CONST || code1 == LABEL_REF)
1073 record_address_regs (mode, arg0, 1, PLUS, code1, scale);
1074 /* If both operands are registers but one is already a hard
1075 register of index or reg-base class, give the other the
1076 class that the hard register is not. */
1077 else if (code0 == REG && code1 == REG
1078 && REGNO (arg0) < FIRST_PSEUDO_REGISTER
1079 && (ok_for_base_p_nonstrict (arg0, mode, PLUS, REG)
1080 || ok_for_index_p_nonstrict (arg0)))
1081 record_address_regs (mode, arg1,
1082 ok_for_base_p_nonstrict (arg0, mode, PLUS, REG)
1083 ? 1 : 0,
1084 PLUS, REG, scale);
1085 else if (code0 == REG && code1 == REG
1086 && REGNO (arg1) < FIRST_PSEUDO_REGISTER
1087 && (ok_for_base_p_nonstrict (arg1, mode, PLUS, REG)
1088 || ok_for_index_p_nonstrict (arg1)))
1089 record_address_regs (mode, arg0,
1090 ok_for_base_p_nonstrict (arg1, mode, PLUS, REG)
1091 ? 1 : 0,
1092 PLUS, REG, scale);
1093 /* If one operand is known to be a pointer, it must be the
1094 base with the other operand the index. Likewise if the
1095 other operand is a MULT. */
1096 else if ((code0 == REG && REG_POINTER (arg0)) || code1 == MULT)
1098 record_address_regs (mode, arg0, 0, PLUS, code1, scale);
1099 record_address_regs (mode, arg1, 1, PLUS, code0, scale);
1101 else if ((code1 == REG && REG_POINTER (arg1)) || code0 == MULT)
1103 record_address_regs (mode, arg0, 1, PLUS, code1, scale);
1104 record_address_regs (mode, arg1, 0, PLUS, code0, scale);
1106 /* Otherwise, count equal chances that each might be a base or
1107 index register. This case should be rare. */
1108 else
1110 record_address_regs (mode, arg0, 0, PLUS, code1, scale / 2);
1111 record_address_regs (mode, arg0, 1, PLUS, code1, scale / 2);
1112 record_address_regs (mode, arg1, 0, PLUS, code0, scale / 2);
1113 record_address_regs (mode, arg1, 1, PLUS, code0, scale / 2);
1116 break;
1118 /* Double the importance of an allocno that is incremented or
1119 decremented, since it would take two extra insns if it ends
1120 up in the wrong place. */
1121 case POST_MODIFY:
1122 case PRE_MODIFY:
1123 record_address_regs (mode, XEXP (x, 0), 0, code,
1124 GET_CODE (XEXP (XEXP (x, 1), 1)), 2 * scale);
1125 if (REG_P (XEXP (XEXP (x, 1), 1)))
1126 record_address_regs (mode, XEXP (XEXP (x, 1), 1), 1, code, REG,
1127 2 * scale);
1128 break;
1130 case POST_INC:
1131 case PRE_INC:
1132 case POST_DEC:
1133 case PRE_DEC:
1134 /* Double the importance of an allocno that is incremented or
1135 decremented, since it would take two extra insns if it ends
1136 up in the wrong place. If the operand is a pseudo-register,
1137 show it is being used in an INC_DEC context. */
1138 #ifdef FORBIDDEN_INC_DEC_CLASSES
1139 if (REG_P (XEXP (x, 0))
1140 && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER)
1141 in_inc_dec[COST_INDEX (REGNO (XEXP (x, 0)))] = true;
1142 #endif
1143 record_address_regs (mode, XEXP (x, 0), 0, code, SCRATCH, 2 * scale);
1144 break;
1146 case REG:
1148 struct costs *pp;
1149 int *pp_costs;
1150 enum reg_class i;
1151 int k, regno, add_cost;
1152 cost_classes_t cost_classes_ptr;
1153 enum reg_class *cost_classes;
1154 move_table *move_in_cost;
1156 if (REGNO (x) < FIRST_PSEUDO_REGISTER)
1157 break;
1159 regno = REGNO (x);
1160 if (allocno_p)
1161 ALLOCNO_BAD_SPILL_P (ira_curr_regno_allocno_map[regno]) = true;
1162 pp = COSTS (costs, COST_INDEX (regno));
1163 add_cost = (ira_memory_move_cost[Pmode][rclass][1] * scale) / 2;
1164 if (INT_MAX - add_cost < pp->mem_cost)
1165 pp->mem_cost = INT_MAX;
1166 else
1167 pp->mem_cost += add_cost;
1168 cost_classes_ptr = regno_cost_classes[regno];
1169 cost_classes = cost_classes_ptr->classes;
1170 pp_costs = pp->cost;
1171 ira_init_register_move_cost_if_necessary (Pmode);
1172 move_in_cost = ira_may_move_in_cost[Pmode];
1173 for (k = cost_classes_ptr->num - 1; k >= 0; k--)
1175 i = cost_classes[k];
1176 add_cost = (move_in_cost[i][rclass] * scale) / 2;
1177 if (INT_MAX - add_cost < pp_costs[k])
1178 pp_costs[k] = INT_MAX;
1179 else
1180 pp_costs[k] += add_cost;
1183 break;
1185 default:
1187 const char *fmt = GET_RTX_FORMAT (code);
1188 int i;
1189 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1190 if (fmt[i] == 'e')
1191 record_address_regs (mode, XEXP (x, i), context, code, SCRATCH,
1192 scale);
1199 /* Calculate the costs of insn operands. */
1200 static void
1201 record_operand_costs (rtx insn, enum reg_class *pref)
1203 const char *constraints[MAX_RECOG_OPERANDS];
1204 enum machine_mode modes[MAX_RECOG_OPERANDS];
1205 int i;
1207 for (i = 0; i < recog_data.n_operands; i++)
1209 constraints[i] = recog_data.constraints[i];
1210 modes[i] = recog_data.operand_mode[i];
1213 /* If we get here, we are set up to record the costs of all the
1214 operands for this insn. Start by initializing the costs. Then
1215 handle any address registers. Finally record the desired classes
1216 for any allocnos, doing it twice if some pair of operands are
1217 commutative. */
1218 for (i = 0; i < recog_data.n_operands; i++)
1220 memcpy (op_costs[i], init_cost, struct_costs_size);
1222 if (GET_CODE (recog_data.operand[i]) == SUBREG)
1223 recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
1225 if (MEM_P (recog_data.operand[i]))
1226 record_address_regs (GET_MODE (recog_data.operand[i]),
1227 XEXP (recog_data.operand[i], 0),
1228 0, MEM, SCRATCH, frequency * 2);
1229 else if (constraints[i][0] == 'p'
1230 || EXTRA_ADDRESS_CONSTRAINT (constraints[i][0],
1231 constraints[i]))
1232 record_address_regs (VOIDmode, recog_data.operand[i], 0, ADDRESS,
1233 SCRATCH, frequency * 2);
1236 /* Check for commutative in a separate loop so everything will have
1237 been initialized. We must do this even if one operand is a
1238 constant--see addsi3 in m68k.md. */
1239 for (i = 0; i < (int) recog_data.n_operands - 1; i++)
1240 if (constraints[i][0] == '%')
1242 const char *xconstraints[MAX_RECOG_OPERANDS];
1243 int j;
1245 /* Handle commutative operands by swapping the constraints.
1246 We assume the modes are the same. */
1247 for (j = 0; j < recog_data.n_operands; j++)
1248 xconstraints[j] = constraints[j];
1250 xconstraints[i] = constraints[i+1];
1251 xconstraints[i+1] = constraints[i];
1252 record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
1253 recog_data.operand, modes,
1254 xconstraints, insn, pref);
1256 record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
1257 recog_data.operand, modes,
1258 constraints, insn, pref);
1263 /* Process one insn INSN. Scan it and record each time it would save
1264 code to put a certain allocnos in a certain class. Return the last
1265 insn processed, so that the scan can be continued from there. */
1266 static rtx
1267 scan_one_insn (rtx insn)
1269 enum rtx_code pat_code;
1270 rtx set, note;
1271 int i, k;
1272 bool counted_mem;
1274 if (!NONDEBUG_INSN_P (insn))
1275 return insn;
1277 pat_code = GET_CODE (PATTERN (insn));
1278 if (pat_code == USE || pat_code == CLOBBER || pat_code == ASM_INPUT
1279 || pat_code == ADDR_VEC || pat_code == ADDR_DIFF_VEC)
1280 return insn;
1282 counted_mem = false;
1283 set = single_set (insn);
1284 extract_insn (insn);
1286 /* If this insn loads a parameter from its stack slot, then it
1287 represents a savings, rather than a cost, if the parameter is
1288 stored in memory. Record this fact.
1290 Similarly if we're loading other constants from memory (constant
1291 pool, TOC references, small data areas, etc) and this is the only
1292 assignment to the destination pseudo. */
1293 if (set != 0 && REG_P (SET_DEST (set)) && MEM_P (SET_SRC (set))
1294 && (note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != NULL_RTX
1295 && ((MEM_P (XEXP (note, 0)))
1296 || (CONSTANT_P (XEXP (note, 0))
1297 && targetm.legitimate_constant_p (GET_MODE (SET_DEST (set)),
1298 XEXP (note, 0))
1299 && REG_N_SETS (REGNO (SET_DEST (set))) == 1)))
1301 enum reg_class cl = GENERAL_REGS;
1302 rtx reg = SET_DEST (set);
1303 int num = COST_INDEX (REGNO (reg));
1305 COSTS (costs, num)->mem_cost
1306 -= ira_memory_move_cost[GET_MODE (reg)][cl][1] * frequency;
1307 record_address_regs (GET_MODE (SET_SRC (set)), XEXP (SET_SRC (set), 0),
1308 0, MEM, SCRATCH, frequency * 2);
1309 counted_mem = true;
1312 record_operand_costs (insn, pref);
1314 /* Now add the cost for each operand to the total costs for its
1315 allocno. */
1316 for (i = 0; i < recog_data.n_operands; i++)
1317 if (REG_P (recog_data.operand[i])
1318 && REGNO (recog_data.operand[i]) >= FIRST_PSEUDO_REGISTER)
1320 int regno = REGNO (recog_data.operand[i]);
1321 struct costs *p = COSTS (costs, COST_INDEX (regno));
1322 struct costs *q = op_costs[i];
1323 int *p_costs = p->cost, *q_costs = q->cost;
1324 cost_classes_t cost_classes_ptr = regno_cost_classes[regno];
1325 int add_cost;
1327 /* If the already accounted for the memory "cost" above, don't
1328 do so again. */
1329 if (!counted_mem)
1331 add_cost = q->mem_cost;
1332 if (add_cost > 0 && INT_MAX - add_cost < p->mem_cost)
1333 p->mem_cost = INT_MAX;
1334 else
1335 p->mem_cost += add_cost;
1337 for (k = cost_classes_ptr->num - 1; k >= 0; k--)
1339 add_cost = q_costs[k];
1340 if (add_cost > 0 && INT_MAX - add_cost < p_costs[k])
1341 p_costs[k] = INT_MAX;
1342 else
1343 p_costs[k] += add_cost;
1347 return insn;
1352 /* Print allocnos costs to file F. */
1353 static void
1354 print_allocno_costs (FILE *f)
1356 int k;
1357 ira_allocno_t a;
1358 ira_allocno_iterator ai;
1360 ira_assert (allocno_p);
1361 fprintf (f, "\n");
1362 FOR_EACH_ALLOCNO (a, ai)
1364 int i, rclass;
1365 basic_block bb;
1366 int regno = ALLOCNO_REGNO (a);
1367 cost_classes_t cost_classes_ptr = regno_cost_classes[regno];
1368 enum reg_class *cost_classes = cost_classes_ptr->classes;
1370 i = ALLOCNO_NUM (a);
1371 fprintf (f, " a%d(r%d,", i, regno);
1372 if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
1373 fprintf (f, "b%d", bb->index);
1374 else
1375 fprintf (f, "l%d", ALLOCNO_LOOP_TREE_NODE (a)->loop->num);
1376 fprintf (f, ") costs:");
1377 for (k = 0; k < cost_classes_ptr->num; k++)
1379 rclass = cost_classes[k];
1380 if (contains_reg_of_mode[rclass][PSEUDO_REGNO_MODE (regno)]
1381 #ifdef FORBIDDEN_INC_DEC_CLASSES
1382 && (! in_inc_dec[i] || ! forbidden_inc_dec_class[rclass])
1383 #endif
1384 #ifdef CANNOT_CHANGE_MODE_CLASS
1385 && ! invalid_mode_change_p (regno, (enum reg_class) rclass)
1386 #endif
1389 fprintf (f, " %s:%d", reg_class_names[rclass],
1390 COSTS (costs, i)->cost[k]);
1391 if (flag_ira_region == IRA_REGION_ALL
1392 || flag_ira_region == IRA_REGION_MIXED)
1393 fprintf (f, ",%d", COSTS (total_allocno_costs, i)->cost[k]);
1396 fprintf (f, " MEM:%i", COSTS (costs, i)->mem_cost);
1397 if (flag_ira_region == IRA_REGION_ALL
1398 || flag_ira_region == IRA_REGION_MIXED)
1399 fprintf (f, ",%d", COSTS (total_allocno_costs, i)->mem_cost);
1400 fprintf (f, "\n");
1404 /* Print pseudo costs to file F. */
1405 static void
1406 print_pseudo_costs (FILE *f)
1408 int regno, k;
1409 int rclass;
1410 cost_classes_t cost_classes_ptr;
1411 enum reg_class *cost_classes;
1413 ira_assert (! allocno_p);
1414 fprintf (f, "\n");
1415 for (regno = max_reg_num () - 1; regno >= FIRST_PSEUDO_REGISTER; regno--)
1417 if (REG_N_REFS (regno) <= 0)
1418 continue;
1419 cost_classes_ptr = regno_cost_classes[regno];
1420 cost_classes = cost_classes_ptr->classes;
1421 fprintf (f, " r%d costs:", regno);
1422 for (k = 0; k < cost_classes_ptr->num; k++)
1424 rclass = cost_classes[k];
1425 if (contains_reg_of_mode[rclass][PSEUDO_REGNO_MODE (regno)]
1426 #ifdef FORBIDDEN_INC_DEC_CLASSES
1427 && (! in_inc_dec[regno] || ! forbidden_inc_dec_class[rclass])
1428 #endif
1429 #ifdef CANNOT_CHANGE_MODE_CLASS
1430 && ! invalid_mode_change_p (regno, (enum reg_class) rclass)
1431 #endif
1433 fprintf (f, " %s:%d", reg_class_names[rclass],
1434 COSTS (costs, regno)->cost[k]);
1436 fprintf (f, " MEM:%i\n", COSTS (costs, regno)->mem_cost);
1440 /* Traverse the BB represented by LOOP_TREE_NODE to update the allocno
1441 costs. */
1442 static void
1443 process_bb_for_costs (basic_block bb)
1445 rtx insn;
1447 frequency = REG_FREQ_FROM_BB (bb);
1448 if (frequency == 0)
1449 frequency = 1;
1450 FOR_BB_INSNS (bb, insn)
1451 insn = scan_one_insn (insn);
1454 /* Traverse the BB represented by LOOP_TREE_NODE to update the allocno
1455 costs. */
1456 static void
1457 process_bb_node_for_costs (ira_loop_tree_node_t loop_tree_node)
1459 basic_block bb;
1461 bb = loop_tree_node->bb;
1462 if (bb != NULL)
1463 process_bb_for_costs (bb);
1466 /* Find costs of register classes and memory for allocnos or pseudos
1467 and their best costs. Set up preferred, alternative and allocno
1468 classes for pseudos. */
1469 static void
1470 find_costs_and_classes (FILE *dump_file)
1472 int i, k, start, max_cost_classes_num;
1473 int pass;
1474 basic_block bb;
1475 enum reg_class *regno_best_class;
1477 init_recog ();
1478 #ifdef FORBIDDEN_INC_DEC_CLASSES
1479 in_inc_dec = ira_allocate (sizeof (bool) * cost_elements_num);
1480 #endif /* FORBIDDEN_INC_DEC_CLASSES */
1481 regno_best_class
1482 = (enum reg_class *) ira_allocate (max_reg_num ()
1483 * sizeof (enum reg_class));
1484 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
1485 regno_best_class[i] = NO_REGS;
1486 if (!resize_reg_info () && allocno_p
1487 && pseudo_classes_defined_p && flag_expensive_optimizations)
1489 ira_allocno_t a;
1490 ira_allocno_iterator ai;
1492 pref = pref_buffer;
1493 max_cost_classes_num = 1;
1494 FOR_EACH_ALLOCNO (a, ai)
1496 pref[ALLOCNO_NUM (a)] = reg_preferred_class (ALLOCNO_REGNO (a));
1497 setup_regno_cost_classes_by_aclass
1498 (ALLOCNO_REGNO (a), pref[ALLOCNO_NUM (a)]);
1499 max_cost_classes_num
1500 = MAX (max_cost_classes_num,
1501 regno_cost_classes[ALLOCNO_REGNO (a)]->num);
1503 start = 1;
1505 else
1507 pref = NULL;
1508 max_cost_classes_num = ira_important_classes_num;
1509 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
1510 if (regno_reg_rtx[i] != NULL_RTX)
1511 setup_regno_cost_classes_by_mode (i, PSEUDO_REGNO_MODE (i));
1512 else
1513 setup_regno_cost_classes_by_aclass (i, ALL_REGS);
1514 start = 0;
1516 if (allocno_p)
1517 /* Clear the flag for the next compiled function. */
1518 pseudo_classes_defined_p = false;
1519 /* Normally we scan the insns once and determine the best class to
1520 use for each allocno. However, if -fexpensive-optimizations are
1521 on, we do so twice, the second time using the tentative best
1522 classes to guide the selection. */
1523 for (pass = start; pass <= flag_expensive_optimizations; pass++)
1525 if ((!allocno_p || internal_flag_ira_verbose > 0) && dump_file)
1526 fprintf (dump_file,
1527 "\nPass %i for finding pseudo/allocno costs\n\n", pass);
1529 if (pass != start)
1531 max_cost_classes_num = 1;
1532 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
1534 setup_regno_cost_classes_by_aclass (i, regno_best_class[i]);
1535 max_cost_classes_num
1536 = MAX (max_cost_classes_num, regno_cost_classes[i]->num);
1540 struct_costs_size
1541 = sizeof (struct costs) + sizeof (int) * (max_cost_classes_num - 1);
1542 /* Zero out our accumulation of the cost of each class for each
1543 allocno. */
1544 memset (costs, 0, cost_elements_num * struct_costs_size);
1545 #ifdef FORBIDDEN_INC_DEC_CLASSES
1546 memset (in_inc_dec, 0, cost_elements_num * sizeof (bool));
1547 #endif
1549 if (allocno_p)
1551 /* Scan the instructions and record each time it would save code
1552 to put a certain allocno in a certain class. */
1553 ira_traverse_loop_tree (true, ira_loop_tree_root,
1554 process_bb_node_for_costs, NULL);
1556 memcpy (total_allocno_costs, costs,
1557 max_struct_costs_size * ira_allocnos_num);
1559 else
1561 basic_block bb;
1563 FOR_EACH_BB (bb)
1564 process_bb_for_costs (bb);
1567 if (pass == 0)
1568 pref = pref_buffer;
1570 /* Now for each allocno look at how desirable each class is and
1571 find which class is preferred. */
1572 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
1574 ira_allocno_t a, parent_a;
1575 int rclass, a_num, parent_a_num, add_cost;
1576 ira_loop_tree_node_t parent;
1577 int best_cost, allocno_cost;
1578 enum reg_class best, alt_class;
1579 #ifdef FORBIDDEN_INC_DEC_CLASSES
1580 int inc_dec_p = false;
1581 #endif
1582 cost_classes_t cost_classes_ptr = regno_cost_classes[i];
1583 enum reg_class *cost_classes = cost_classes_ptr->classes;
1584 int *i_costs = temp_costs->cost;
1585 int i_mem_cost;
1586 int equiv_savings = regno_equiv_gains[i];
1588 if (! allocno_p)
1590 if (regno_reg_rtx[i] == NULL_RTX)
1591 continue;
1592 #ifdef FORBIDDEN_INC_DEC_CLASSES
1593 inc_dec_p = in_inc_dec[i];
1594 #endif
1595 memcpy (temp_costs, COSTS (costs, i), struct_costs_size);
1596 i_mem_cost = temp_costs->mem_cost;
1598 else
1600 if (ira_regno_allocno_map[i] == NULL)
1601 continue;
1602 memset (temp_costs, 0, struct_costs_size);
1603 i_mem_cost = 0;
1604 /* Find cost of all allocnos with the same regno. */
1605 for (a = ira_regno_allocno_map[i];
1606 a != NULL;
1607 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
1609 int *a_costs, *p_costs;
1611 a_num = ALLOCNO_NUM (a);
1612 if ((flag_ira_region == IRA_REGION_ALL
1613 || flag_ira_region == IRA_REGION_MIXED)
1614 && (parent = ALLOCNO_LOOP_TREE_NODE (a)->parent) != NULL
1615 && (parent_a = parent->regno_allocno_map[i]) != NULL
1616 /* There are no caps yet. */
1617 && bitmap_bit_p (ALLOCNO_LOOP_TREE_NODE
1618 (a)->border_allocnos,
1619 ALLOCNO_NUM (a)))
1621 /* Propagate costs to upper levels in the region
1622 tree. */
1623 parent_a_num = ALLOCNO_NUM (parent_a);
1624 a_costs = COSTS (total_allocno_costs, a_num)->cost;
1625 p_costs = COSTS (total_allocno_costs, parent_a_num)->cost;
1626 for (k = cost_classes_ptr->num - 1; k >= 0; k--)
1628 add_cost = a_costs[k];
1629 if (add_cost > 0 && INT_MAX - add_cost < p_costs[k])
1630 p_costs[k] = INT_MAX;
1631 else
1632 p_costs[k] += add_cost;
1634 add_cost = COSTS (total_allocno_costs, a_num)->mem_cost;
1635 if (add_cost > 0
1636 && (INT_MAX - add_cost
1637 < COSTS (total_allocno_costs,
1638 parent_a_num)->mem_cost))
1639 COSTS (total_allocno_costs, parent_a_num)->mem_cost
1640 = INT_MAX;
1641 else
1642 COSTS (total_allocno_costs, parent_a_num)->mem_cost
1643 += add_cost;
1646 a_costs = COSTS (costs, a_num)->cost;
1647 for (k = cost_classes_ptr->num - 1; k >= 0; k--)
1649 add_cost = a_costs[k];
1650 if (add_cost > 0 && INT_MAX - add_cost < i_costs[k])
1651 i_costs[k] = INT_MAX;
1652 else
1653 i_costs[k] += add_cost;
1655 add_cost = COSTS (costs, a_num)->mem_cost;
1656 if (add_cost > 0 && INT_MAX - add_cost < i_mem_cost)
1657 i_mem_cost = INT_MAX;
1658 else
1659 i_mem_cost += add_cost;
1660 #ifdef FORBIDDEN_INC_DEC_CLASSES
1661 if (in_inc_dec[a_num])
1662 inc_dec_p = true;
1663 #endif
1666 if (equiv_savings < 0)
1667 i_mem_cost = -equiv_savings;
1668 else if (equiv_savings > 0)
1670 i_mem_cost = 0;
1671 for (k = cost_classes_ptr->num - 1; k >= 0; k--)
1672 i_costs[k] += equiv_savings;
1675 best_cost = (1 << (HOST_BITS_PER_INT - 2)) - 1;
1676 best = ALL_REGS;
1677 alt_class = NO_REGS;
1678 /* Find best common class for all allocnos with the same
1679 regno. */
1680 for (k = 0; k < cost_classes_ptr->num; k++)
1682 rclass = cost_classes[k];
1683 /* Ignore classes that are too small for this operand or
1684 invalid for an operand that was auto-incremented. */
1685 if (! contains_reg_of_mode[rclass][PSEUDO_REGNO_MODE (i)]
1686 #ifdef FORBIDDEN_INC_DEC_CLASSES
1687 || (inc_dec_p && forbidden_inc_dec_class[rclass])
1688 #endif
1689 #ifdef CANNOT_CHANGE_MODE_CLASS
1690 || invalid_mode_change_p (i, (enum reg_class) rclass)
1691 #endif
1693 continue;
1694 if (i_costs[k] < best_cost)
1696 best_cost = i_costs[k];
1697 best = (enum reg_class) rclass;
1699 else if (i_costs[k] == best_cost)
1700 best = ira_reg_class_subunion[best][rclass];
1701 if (pass == flag_expensive_optimizations
1702 && i_costs[k] < i_mem_cost
1703 && (reg_class_size[reg_class_subunion[alt_class][rclass]]
1704 > reg_class_size[alt_class]))
1705 alt_class = reg_class_subunion[alt_class][rclass];
1707 alt_class = ira_allocno_class_translate[alt_class];
1708 if (best_cost > i_mem_cost)
1709 regno_aclass[i] = NO_REGS;
1710 else
1712 /* Make the common class the biggest class of best and
1713 alt_class. */
1714 regno_aclass[i]
1715 = ira_reg_class_superunion[best][alt_class];
1716 ira_assert (regno_aclass[i] != NO_REGS
1717 && ira_reg_allocno_class_p[regno_aclass[i]]);
1719 if (pass == flag_expensive_optimizations)
1721 if (best_cost > i_mem_cost)
1722 best = alt_class = NO_REGS;
1723 else if (best == alt_class)
1724 alt_class = NO_REGS;
1725 setup_reg_classes (i, best, alt_class, regno_aclass[i]);
1726 if ((!allocno_p || internal_flag_ira_verbose > 2)
1727 && dump_file != NULL)
1728 fprintf (dump_file,
1729 " r%d: preferred %s, alternative %s, allocno %s\n",
1730 i, reg_class_names[best], reg_class_names[alt_class],
1731 reg_class_names[regno_aclass[i]]);
1733 regno_best_class[i] = best;
1734 if (! allocno_p)
1736 pref[i] = best_cost > i_mem_cost ? NO_REGS : best;
1737 continue;
1739 for (a = ira_regno_allocno_map[i];
1740 a != NULL;
1741 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
1743 a_num = ALLOCNO_NUM (a);
1744 if (regno_aclass[i] == NO_REGS)
1745 best = NO_REGS;
1746 else
1748 int *total_a_costs = COSTS (total_allocno_costs, a_num)->cost;
1749 int *a_costs = COSTS (costs, a_num)->cost;
1751 /* Finding best class which is subset of the common
1752 class. */
1753 best_cost = (1 << (HOST_BITS_PER_INT - 2)) - 1;
1754 allocno_cost = best_cost;
1755 best = ALL_REGS;
1756 for (k = 0; k < cost_classes_ptr->num; k++)
1758 rclass = cost_classes[k];
1759 if (! ira_class_subset_p[rclass][regno_aclass[i]])
1760 continue;
1761 /* Ignore classes that are too small for this
1762 operand or invalid for an operand that was
1763 auto-incremented. */
1764 if (! contains_reg_of_mode[rclass][PSEUDO_REGNO_MODE (i)]
1765 #ifdef FORBIDDEN_INC_DEC_CLASSES
1766 || (inc_dec_p && forbidden_inc_dec_class[rclass])
1767 #endif
1768 #ifdef CANNOT_CHANGE_MODE_CLASS
1769 || invalid_mode_change_p (i, (enum reg_class) rclass)
1770 #endif
1773 else if (total_a_costs[k] < best_cost)
1775 best_cost = total_a_costs[k];
1776 allocno_cost = a_costs[k];
1777 best = (enum reg_class) rclass;
1779 else if (total_a_costs[k] == best_cost)
1781 best = ira_reg_class_subunion[best][rclass];
1782 allocno_cost = MAX (allocno_cost, a_costs[k]);
1785 ALLOCNO_CLASS_COST (a) = allocno_cost;
1787 if (internal_flag_ira_verbose > 2 && dump_file != NULL
1788 && (pass == 0 || pref[a_num] != best))
1790 fprintf (dump_file, " a%d (r%d,", a_num, i);
1791 if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
1792 fprintf (dump_file, "b%d", bb->index);
1793 else
1794 fprintf (dump_file, "l%d",
1795 ALLOCNO_LOOP_TREE_NODE (a)->loop->num);
1796 fprintf (dump_file, ") best %s, allocno %s\n",
1797 reg_class_names[best],
1798 reg_class_names[regno_aclass[i]]);
1800 pref[a_num] = best;
1804 if (internal_flag_ira_verbose > 4 && dump_file)
1806 if (allocno_p)
1807 print_allocno_costs (dump_file);
1808 else
1809 print_pseudo_costs (dump_file);
1810 fprintf (dump_file,"\n");
1813 ira_free (regno_best_class);
1814 #ifdef FORBIDDEN_INC_DEC_CLASSES
1815 ira_free (in_inc_dec);
1816 #endif
1821 /* Process moves involving hard regs to modify allocno hard register
1822 costs. We can do this only after determining allocno class. If a
1823 hard register forms a register class, than moves with the hard
1824 register are already taken into account in class costs for the
1825 allocno. */
1826 static void
1827 process_bb_node_for_hard_reg_moves (ira_loop_tree_node_t loop_tree_node)
1829 int i, freq, cost, src_regno, dst_regno, hard_regno;
1830 bool to_p;
1831 ira_allocno_t a;
1832 enum reg_class rclass, hard_reg_class;
1833 enum machine_mode mode;
1834 basic_block bb;
1835 rtx insn, set, src, dst;
1837 bb = loop_tree_node->bb;
1838 if (bb == NULL)
1839 return;
1840 freq = REG_FREQ_FROM_BB (bb);
1841 if (freq == 0)
1842 freq = 1;
1843 FOR_BB_INSNS (bb, insn)
1845 if (!NONDEBUG_INSN_P (insn))
1846 continue;
1847 set = single_set (insn);
1848 if (set == NULL_RTX)
1849 continue;
1850 dst = SET_DEST (set);
1851 src = SET_SRC (set);
1852 if (! REG_P (dst) || ! REG_P (src))
1853 continue;
1854 dst_regno = REGNO (dst);
1855 src_regno = REGNO (src);
1856 if (dst_regno >= FIRST_PSEUDO_REGISTER
1857 && src_regno < FIRST_PSEUDO_REGISTER)
1859 hard_regno = src_regno;
1860 to_p = true;
1861 a = ira_curr_regno_allocno_map[dst_regno];
1863 else if (src_regno >= FIRST_PSEUDO_REGISTER
1864 && dst_regno < FIRST_PSEUDO_REGISTER)
1866 hard_regno = dst_regno;
1867 to_p = false;
1868 a = ira_curr_regno_allocno_map[src_regno];
1870 else
1871 continue;
1872 rclass = ALLOCNO_CLASS (a);
1873 if (! TEST_HARD_REG_BIT (reg_class_contents[rclass], hard_regno))
1874 continue;
1875 i = ira_class_hard_reg_index[rclass][hard_regno];
1876 if (i < 0)
1877 continue;
1878 mode = ALLOCNO_MODE (a);
1879 hard_reg_class = REGNO_REG_CLASS (hard_regno);
1880 ira_init_register_move_cost_if_necessary (mode);
1881 cost
1882 = (to_p ? ira_register_move_cost[mode][hard_reg_class][rclass]
1883 : ira_register_move_cost[mode][rclass][hard_reg_class]) * freq;
1884 ira_allocate_and_set_costs (&ALLOCNO_HARD_REG_COSTS (a), rclass,
1885 ALLOCNO_CLASS_COST (a));
1886 ira_allocate_and_set_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a),
1887 rclass, 0);
1888 ALLOCNO_HARD_REG_COSTS (a)[i] -= cost;
1889 ALLOCNO_CONFLICT_HARD_REG_COSTS (a)[i] -= cost;
1890 ALLOCNO_CLASS_COST (a) = MIN (ALLOCNO_CLASS_COST (a),
1891 ALLOCNO_HARD_REG_COSTS (a)[i]);
1895 /* After we find hard register and memory costs for allocnos, define
1896 its class and modify hard register cost because insns moving
1897 allocno to/from hard registers. */
1898 static void
1899 setup_allocno_class_and_costs (void)
1901 int i, j, n, regno, hard_regno, num;
1902 int *reg_costs;
1903 enum reg_class aclass, rclass;
1904 ira_allocno_t a;
1905 ira_allocno_iterator ai;
1906 cost_classes_t cost_classes_ptr;
1908 ira_assert (allocno_p);
1909 FOR_EACH_ALLOCNO (a, ai)
1911 i = ALLOCNO_NUM (a);
1912 regno = ALLOCNO_REGNO (a);
1913 aclass = regno_aclass[regno];
1914 cost_classes_ptr = regno_cost_classes[regno];
1915 ira_assert (pref[i] == NO_REGS || aclass != NO_REGS);
1916 ALLOCNO_MEMORY_COST (a) = COSTS (costs, i)->mem_cost;
1917 ira_set_allocno_class (a, aclass);
1918 if (aclass == NO_REGS)
1919 continue;
1920 if (optimize && ALLOCNO_CLASS (a) != pref[i])
1922 n = ira_class_hard_regs_num[aclass];
1923 ALLOCNO_HARD_REG_COSTS (a)
1924 = reg_costs = ira_allocate_cost_vector (aclass);
1925 for (j = n - 1; j >= 0; j--)
1927 hard_regno = ira_class_hard_regs[aclass][j];
1928 if (TEST_HARD_REG_BIT (reg_class_contents[pref[i]], hard_regno))
1929 reg_costs[j] = ALLOCNO_CLASS_COST (a);
1930 else
1932 rclass = REGNO_REG_CLASS (hard_regno);
1933 num = cost_classes_ptr->index[rclass];
1934 if (num < 0)
1936 num = cost_classes_ptr->hard_regno_index[hard_regno];
1937 ira_assert (num >= 0);
1939 reg_costs[j] = COSTS (costs, i)->cost[num];
1944 if (optimize)
1945 ira_traverse_loop_tree (true, ira_loop_tree_root,
1946 process_bb_node_for_hard_reg_moves, NULL);
1951 /* Function called once during compiler work. */
1952 void
1953 ira_init_costs_once (void)
1955 int i;
1957 init_cost = NULL;
1958 for (i = 0; i < MAX_RECOG_OPERANDS; i++)
1960 op_costs[i] = NULL;
1961 this_op_costs[i] = NULL;
1963 temp_costs = NULL;
1966 /* Free allocated temporary cost vectors. */
1967 static void
1968 free_ira_costs (void)
1970 int i;
1972 free (init_cost);
1973 init_cost = NULL;
1974 for (i = 0; i < MAX_RECOG_OPERANDS; i++)
1976 free (op_costs[i]);
1977 free (this_op_costs[i]);
1978 op_costs[i] = this_op_costs[i] = NULL;
1980 free (temp_costs);
1981 temp_costs = NULL;
1984 /* This is called each time register related information is
1985 changed. */
1986 void
1987 ira_init_costs (void)
1989 int i;
1991 free_ira_costs ();
1992 max_struct_costs_size
1993 = sizeof (struct costs) + sizeof (int) * (ira_important_classes_num - 1);
1994 /* Don't use ira_allocate because vectors live through several IRA
1995 calls. */
1996 init_cost = (struct costs *) xmalloc (max_struct_costs_size);
1997 init_cost->mem_cost = 1000000;
1998 for (i = 0; i < ira_important_classes_num; i++)
1999 init_cost->cost[i] = 1000000;
2000 for (i = 0; i < MAX_RECOG_OPERANDS; i++)
2002 op_costs[i] = (struct costs *) xmalloc (max_struct_costs_size);
2003 this_op_costs[i] = (struct costs *) xmalloc (max_struct_costs_size);
2005 temp_costs = (struct costs *) xmalloc (max_struct_costs_size);
2008 /* Function called once at the end of compiler work. */
2009 void
2010 ira_finish_costs_once (void)
2012 free_ira_costs ();
2017 /* Common initialization function for ira_costs and
2018 ira_set_pseudo_classes. */
2019 static void
2020 init_costs (void)
2022 init_subregs_of_mode ();
2023 costs = (struct costs *) ira_allocate (max_struct_costs_size
2024 * cost_elements_num);
2025 pref_buffer = (enum reg_class *) ira_allocate (sizeof (enum reg_class)
2026 * cost_elements_num);
2027 regno_aclass = (enum reg_class *) ira_allocate (sizeof (enum reg_class)
2028 * max_reg_num ());
2029 regno_equiv_gains = (int *) ira_allocate (sizeof (int) * max_reg_num ());
2030 memset (regno_equiv_gains, 0, sizeof (int) * max_reg_num ());
2033 /* Common finalization function for ira_costs and
2034 ira_set_pseudo_classes. */
2035 static void
2036 finish_costs (void)
2038 finish_subregs_of_mode ();
2039 ira_free (regno_equiv_gains);
2040 ira_free (regno_aclass);
2041 ira_free (pref_buffer);
2042 ira_free (costs);
2045 /* Entry function which defines register class, memory and hard
2046 register costs for each allocno. */
2047 void
2048 ira_costs (void)
2050 allocno_p = true;
2051 cost_elements_num = ira_allocnos_num;
2052 init_costs ();
2053 total_allocno_costs = (struct costs *) ira_allocate (max_struct_costs_size
2054 * ira_allocnos_num);
2055 initiate_regno_cost_classes ();
2056 calculate_elim_costs_all_insns ();
2057 find_costs_and_classes (ira_dump_file);
2058 setup_allocno_class_and_costs ();
2059 finish_regno_cost_classes ();
2060 finish_costs ();
2061 ira_free (total_allocno_costs);
2064 /* Entry function which defines classes for pseudos. */
2065 void
2066 ira_set_pseudo_classes (FILE *dump_file)
2068 allocno_p = false;
2069 internal_flag_ira_verbose = flag_ira_verbose;
2070 cost_elements_num = max_reg_num ();
2071 init_costs ();
2072 initiate_regno_cost_classes ();
2073 find_costs_and_classes (dump_file);
2074 finish_regno_cost_classes ();
2075 pseudo_classes_defined_p = true;
2076 finish_costs ();
2081 /* Change hard register costs for allocnos which lives through
2082 function calls. This is called only when we found all intersected
2083 calls during building allocno live ranges. */
2084 void
2085 ira_tune_allocno_costs (void)
2087 int j, n, regno;
2088 int cost, min_cost, *reg_costs;
2089 enum reg_class aclass, rclass;
2090 enum machine_mode mode;
2091 ira_allocno_t a;
2092 ira_allocno_iterator ai;
2093 ira_allocno_object_iterator oi;
2094 ira_object_t obj;
2095 bool skip_p;
2097 FOR_EACH_ALLOCNO (a, ai)
2099 aclass = ALLOCNO_CLASS (a);
2100 if (aclass == NO_REGS)
2101 continue;
2102 mode = ALLOCNO_MODE (a);
2103 n = ira_class_hard_regs_num[aclass];
2104 min_cost = INT_MAX;
2105 if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
2107 ira_allocate_and_set_costs
2108 (&ALLOCNO_HARD_REG_COSTS (a), aclass,
2109 ALLOCNO_CLASS_COST (a));
2110 reg_costs = ALLOCNO_HARD_REG_COSTS (a);
2111 for (j = n - 1; j >= 0; j--)
2113 regno = ira_class_hard_regs[aclass][j];
2114 skip_p = false;
2115 FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
2117 if (! ira_hard_reg_not_in_set_p (regno, mode,
2118 OBJECT_CONFLICT_HARD_REGS
2119 (obj)))
2121 skip_p = true;
2122 break;
2125 if (skip_p)
2126 continue;
2127 rclass = REGNO_REG_CLASS (regno);
2128 cost = 0;
2129 if (! ira_hard_reg_not_in_set_p (regno, mode, call_used_reg_set)
2130 || HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
2131 cost += (ALLOCNO_CALL_FREQ (a)
2132 * (ira_memory_move_cost[mode][rclass][0]
2133 + ira_memory_move_cost[mode][rclass][1]));
2134 #ifdef IRA_HARD_REGNO_ADD_COST_MULTIPLIER
2135 cost += ((ira_memory_move_cost[mode][rclass][0]
2136 + ira_memory_move_cost[mode][rclass][1])
2137 * ALLOCNO_FREQ (a)
2138 * IRA_HARD_REGNO_ADD_COST_MULTIPLIER (regno) / 2);
2139 #endif
2140 if (INT_MAX - cost < reg_costs[j])
2141 reg_costs[j] = INT_MAX;
2142 else
2143 reg_costs[j] += cost;
2144 if (min_cost > reg_costs[j])
2145 min_cost = reg_costs[j];
2148 if (min_cost != INT_MAX)
2149 ALLOCNO_CLASS_COST (a) = min_cost;
2151 /* Some targets allow pseudos to be allocated to unaligned sequences
2152 of hard registers. However, selecting an unaligned sequence can
2153 unnecessarily restrict later allocations. So increase the cost of
2154 unaligned hard regs to encourage the use of aligned hard regs. */
2156 const int nregs = ira_reg_class_max_nregs[aclass][ALLOCNO_MODE (a)];
2158 if (nregs > 1)
2160 ira_allocate_and_set_costs
2161 (&ALLOCNO_HARD_REG_COSTS (a), aclass, ALLOCNO_CLASS_COST (a));
2162 reg_costs = ALLOCNO_HARD_REG_COSTS (a);
2163 for (j = n - 1; j >= 0; j--)
2165 regno = ira_non_ordered_class_hard_regs[aclass][j];
2166 if ((regno % nregs) != 0)
2168 int index = ira_class_hard_reg_index[aclass][regno];
2169 ira_assert (index != -1);
2170 reg_costs[index] += ALLOCNO_FREQ (a);
2178 /* Add COST to the estimated gain for eliminating REGNO with its
2179 equivalence. If COST is zero, record that no such elimination is
2180 possible. */
2182 void
2183 ira_adjust_equiv_reg_cost (unsigned regno, int cost)
2185 if (cost == 0)
2186 regno_equiv_gains[regno] = 0;
2187 else
2188 regno_equiv_gains[regno] += cost;