* config/darwin.c (darwin_assemble_visibility): Treat
[official-gcc.git] / gcc / ira-costs.c
blob0c59b03ded225cc878ffa79ce615684303e88408
1 /* IRA hard register and memory cost calculation for allocnos or pseudos.
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012
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 array `costs'. */
50 static int cost_elements_num;
52 /* The `costs' struct records the cost of using hard registers of each
53 class considered for the calculation and of using memory for each
54 allocno or pseudo. */
55 struct costs
57 int mem_cost;
58 /* Costs for register classes start here. We process only some
59 allocno classes. */
60 int cost[1];
63 #define max_struct_costs_size \
64 (this_target_ira_int->x_max_struct_costs_size)
65 #define init_cost \
66 (this_target_ira_int->x_init_cost)
67 #define temp_costs \
68 (this_target_ira_int->x_temp_costs)
69 #define op_costs \
70 (this_target_ira_int->x_op_costs)
71 #define this_op_costs \
72 (this_target_ira_int->x_this_op_costs)
74 /* Costs of each class for each allocno or pseudo. */
75 static struct costs *costs;
77 /* Accumulated costs of each class for each allocno. */
78 static struct costs *total_allocno_costs;
80 /* It is the current size of struct costs. */
81 static int struct_costs_size;
83 /* Return pointer to structure containing costs of allocno or pseudo
84 with given NUM in array ARR. */
85 #define COSTS(arr, num) \
86 ((struct costs *) ((char *) (arr) + (num) * struct_costs_size))
88 /* Return index in COSTS when processing reg with REGNO. */
89 #define COST_INDEX(regno) (allocno_p \
90 ? ALLOCNO_NUM (ira_curr_regno_allocno_map[regno]) \
91 : (int) regno)
93 /* Record register class preferences of each allocno or pseudo. Null
94 value means no preferences. It happens on the 1st iteration of the
95 cost calculation. */
96 static enum reg_class *pref;
98 /* Allocated buffers for pref. */
99 static enum reg_class *pref_buffer;
101 /* Record allocno class of each allocno with the same regno. */
102 static enum reg_class *regno_aclass;
104 /* Record cost gains for not allocating a register with an invariant
105 equivalence. */
106 static int *regno_equiv_gains;
108 /* Execution frequency of the current insn. */
109 static int frequency;
113 /* Info about reg classes whose costs are calculated for a pseudo. */
114 struct cost_classes
116 /* Number of the cost classes in the subsequent array. */
117 int num;
118 /* Container of the cost classes. */
119 enum reg_class classes[N_REG_CLASSES];
120 /* Map reg class -> index of the reg class in the previous array.
121 -1 if it is not a cost classe. */
122 int index[N_REG_CLASSES];
123 /* Map hard regno index of first class in array CLASSES containing
124 the hard regno, -1 otherwise. */
125 int hard_regno_index[FIRST_PSEUDO_REGISTER];
128 /* Types of pointers to the structure above. */
129 typedef struct cost_classes *cost_classes_t;
130 typedef const struct cost_classes *const_cost_classes_t;
132 /* Info about cost classes for each pseudo. */
133 static cost_classes_t *regno_cost_classes;
135 /* Returns hash value for cost classes info V. */
136 static hashval_t
137 cost_classes_hash (const void *v)
139 const_cost_classes_t hv = (const_cost_classes_t) v;
141 return iterative_hash (&hv->classes, sizeof (enum reg_class) * hv->num, 0);
144 /* Compares cost classes info V1 and V2. */
145 static int
146 cost_classes_eq (const void *v1, const void *v2)
148 const_cost_classes_t hv1 = (const_cost_classes_t) v1;
149 const_cost_classes_t hv2 = (const_cost_classes_t) v2;
151 return hv1->num == hv2->num && memcmp (hv1->classes, hv2->classes,
152 sizeof (enum reg_class) * hv1->num);
155 /* Delete cost classes info V from the hash table. */
156 static void
157 cost_classes_del (void *v)
159 ira_free (v);
162 /* Hash table of unique cost classes. */
163 static htab_t cost_classes_htab;
165 /* Map allocno class -> cost classes for pseudo of given allocno
166 class. */
167 static cost_classes_t cost_classes_aclass_cache[N_REG_CLASSES];
169 /* Map mode -> cost classes for pseudo of give mode. */
170 static cost_classes_t cost_classes_mode_cache[MAX_MACHINE_MODE];
172 /* Initialize info about the cost classes for each pseudo. */
173 static void
174 initiate_regno_cost_classes (void)
176 int size = sizeof (cost_classes_t) * max_reg_num ();
178 regno_cost_classes = (cost_classes_t *) ira_allocate (size);
179 memset (regno_cost_classes, 0, size);
180 memset (cost_classes_aclass_cache, 0,
181 sizeof (cost_classes_t) * N_REG_CLASSES);
182 memset (cost_classes_mode_cache, 0,
183 sizeof (cost_classes_t) * MAX_MACHINE_MODE);
184 cost_classes_htab
185 = htab_create (200, cost_classes_hash, cost_classes_eq, cost_classes_del);
188 /* Create new cost classes from cost classes FROM and set up members
189 index and hard_regno_index. Return the new classes. The function
190 implements some common code of two functions
191 setup_regno_cost_classes_by_aclass and
192 setup_regno_cost_classes_by_mode. */
193 static cost_classes_t
194 setup_cost_classes (cost_classes_t from)
196 cost_classes_t classes_ptr;
197 enum reg_class cl;
198 int i, j, hard_regno;
200 classes_ptr = (cost_classes_t) ira_allocate (sizeof (struct cost_classes));
201 classes_ptr->num = from->num;
202 for (i = 0; i < N_REG_CLASSES; i++)
203 classes_ptr->index[i] = -1;
204 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
205 classes_ptr->hard_regno_index[i] = -1;
206 for (i = 0; i < from->num; i++)
208 cl = classes_ptr->classes[i] = from->classes[i];
209 classes_ptr->index[cl] = i;
210 for (j = ira_class_hard_regs_num[cl] - 1; j >= 0; j--)
212 hard_regno = ira_class_hard_regs[cl][j];
213 if (classes_ptr->hard_regno_index[hard_regno] < 0)
214 classes_ptr->hard_regno_index[hard_regno] = i;
217 return classes_ptr;
220 /* Setup cost classes for pseudo REGNO whose allocno class is ACLASS.
221 This function is used when we know an initial approximation of
222 allocno class of the pseudo already, e.g. on the second iteration
223 of class cost calculation or after class cost calculation in
224 register-pressure sensitive insn scheduling or register-pressure
225 sensitive loop-invariant motion. */
226 static void
227 setup_regno_cost_classes_by_aclass (int regno, enum reg_class aclass)
229 static struct cost_classes classes;
230 cost_classes_t classes_ptr;
231 enum reg_class cl;
232 int i;
233 PTR *slot;
234 HARD_REG_SET temp, temp2;
235 bool exclude_p;
237 if ((classes_ptr = cost_classes_aclass_cache[aclass]) == NULL)
239 COPY_HARD_REG_SET (temp, reg_class_contents[aclass]);
240 AND_COMPL_HARD_REG_SET (temp, ira_no_alloc_regs);
241 /* We exclude classes from consideration which are subsets of
242 ACLASS only if ACLASS is an uniform class. */
243 exclude_p = ira_uniform_class_p[aclass];
244 classes.num = 0;
245 for (i = 0; i < ira_important_classes_num; i++)
247 cl = ira_important_classes[i];
248 if (exclude_p)
250 /* Exclude non-uniform classes which are subsets of
251 ACLASS. */
252 COPY_HARD_REG_SET (temp2, reg_class_contents[cl]);
253 AND_COMPL_HARD_REG_SET (temp2, ira_no_alloc_regs);
254 if (hard_reg_set_subset_p (temp2, temp) && cl != aclass)
255 continue;
257 classes.classes[classes.num++] = cl;
259 slot = htab_find_slot (cost_classes_htab, &classes, INSERT);
260 if (*slot == NULL)
262 classes_ptr = setup_cost_classes (&classes);
263 *slot = classes_ptr;
265 classes_ptr = cost_classes_aclass_cache[aclass] = (cost_classes_t) *slot;
267 regno_cost_classes[regno] = classes_ptr;
270 /* Setup cost classes for pseudo REGNO with MODE. Usage of MODE can
271 decrease number of cost classes for the pseudo, if hard registers
272 of some important classes can not hold a value of MODE. So the
273 pseudo can not get hard register of some important classes and cost
274 calculation for such important classes is only waisting CPU
275 time. */
276 static void
277 setup_regno_cost_classes_by_mode (int regno, enum machine_mode mode)
279 static struct cost_classes classes;
280 cost_classes_t classes_ptr;
281 enum reg_class cl;
282 int i;
283 PTR *slot;
284 HARD_REG_SET temp;
286 if ((classes_ptr = cost_classes_mode_cache[mode]) == NULL)
288 classes.num = 0;
289 for (i = 0; i < ira_important_classes_num; i++)
291 cl = ira_important_classes[i];
292 COPY_HARD_REG_SET (temp, ira_prohibited_class_mode_regs[cl][mode]);
293 IOR_HARD_REG_SET (temp, ira_no_alloc_regs);
294 if (hard_reg_set_subset_p (reg_class_contents[cl], temp))
295 continue;
296 classes.classes[classes.num++] = cl;
298 slot = htab_find_slot (cost_classes_htab, &classes, INSERT);
299 if (*slot == NULL)
301 classes_ptr = setup_cost_classes (&classes);
302 *slot = classes_ptr;
304 else
305 classes_ptr = (cost_classes_t) *slot;
306 cost_classes_mode_cache[mode] = (cost_classes_t) *slot;
308 regno_cost_classes[regno] = classes_ptr;
311 /* Finilize info about the cost classes for each pseudo. */
312 static void
313 finish_regno_cost_classes (void)
315 ira_free (regno_cost_classes);
316 htab_delete (cost_classes_htab);
321 /* Compute the cost of loading X into (if TO_P is TRUE) or from (if
322 TO_P is FALSE) a register of class RCLASS in mode MODE. X must not
323 be a pseudo register. */
324 static int
325 copy_cost (rtx x, enum machine_mode mode, reg_class_t rclass, bool to_p,
326 secondary_reload_info *prev_sri)
328 secondary_reload_info sri;
329 reg_class_t secondary_class = NO_REGS;
331 /* If X is a SCRATCH, there is actually nothing to move since we are
332 assuming optimal allocation. */
333 if (GET_CODE (x) == SCRATCH)
334 return 0;
336 /* Get the class we will actually use for a reload. */
337 rclass = targetm.preferred_reload_class (x, rclass);
339 /* If we need a secondary reload for an intermediate, the cost is
340 that to load the input into the intermediate register, then to
341 copy it. */
342 sri.prev_sri = prev_sri;
343 sri.extra_cost = 0;
344 secondary_class = targetm.secondary_reload (to_p, x, rclass, mode, &sri);
346 if (secondary_class != NO_REGS)
348 ira_init_register_move_cost_if_necessary (mode);
349 return (ira_register_move_cost[mode][(int) secondary_class][(int) rclass]
350 + sri.extra_cost
351 + copy_cost (x, mode, secondary_class, to_p, &sri));
354 /* For memory, use the memory move cost, for (hard) registers, use
355 the cost to move between the register classes, and use 2 for
356 everything else (constants). */
357 if (MEM_P (x) || rclass == NO_REGS)
358 return sri.extra_cost
359 + ira_memory_move_cost[mode][(int) rclass][to_p != 0];
360 else if (REG_P (x))
362 reg_class_t x_class = REGNO_REG_CLASS (REGNO (x));
364 ira_init_register_move_cost_if_necessary (mode);
365 return (sri.extra_cost
366 + ira_register_move_cost[mode][(int) x_class][(int) rclass]);
368 else
369 /* If this is a constant, we may eventually want to call rtx_cost
370 here. */
371 return sri.extra_cost + COSTS_N_INSNS (1);
376 /* Record the cost of using memory or hard registers of various
377 classes for the operands in INSN.
379 N_ALTS is the number of alternatives.
380 N_OPS is the number of operands.
381 OPS is an array of the operands.
382 MODES are the modes of the operands, in case any are VOIDmode.
383 CONSTRAINTS are the constraints to use for the operands. This array
384 is modified by this procedure.
386 This procedure works alternative by alternative. For each
387 alternative we assume that we will be able to allocate all allocnos
388 to their ideal register class and calculate the cost of using that
389 alternative. Then we compute, for each operand that is a
390 pseudo-register, the cost of having the allocno allocated to each
391 register class and using it in that alternative. To this cost is
392 added the cost of the alternative.
394 The cost of each class for this insn is its lowest cost among all
395 the alternatives. */
396 static void
397 record_reg_classes (int n_alts, int n_ops, rtx *ops,
398 enum machine_mode *modes, const char **constraints,
399 rtx insn, enum reg_class *pref)
401 int alt;
402 int i, j, k;
403 rtx set;
404 int insn_allows_mem[MAX_RECOG_OPERANDS];
406 for (i = 0; i < n_ops; i++)
407 insn_allows_mem[i] = 0;
409 /* Process each alternative, each time minimizing an operand's cost
410 with the cost for each operand in that alternative. */
411 for (alt = 0; alt < n_alts; alt++)
413 enum reg_class classes[MAX_RECOG_OPERANDS];
414 int allows_mem[MAX_RECOG_OPERANDS];
415 enum reg_class rclass;
416 int alt_fail = 0;
417 int alt_cost = 0, op_cost_add;
419 if (!recog_data.alternative_enabled_p[alt])
421 for (i = 0; i < recog_data.n_operands; i++)
422 constraints[i] = skip_alternative (constraints[i]);
424 continue;
427 for (i = 0; i < n_ops; i++)
429 unsigned char c;
430 const char *p = constraints[i];
431 rtx op = ops[i];
432 enum machine_mode mode = modes[i];
433 int allows_addr = 0;
434 int win = 0;
436 /* Initially show we know nothing about the register class. */
437 classes[i] = NO_REGS;
438 allows_mem[i] = 0;
440 /* If this operand has no constraints at all, we can
441 conclude nothing about it since anything is valid. */
442 if (*p == 0)
444 if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
445 memset (this_op_costs[i], 0, struct_costs_size);
446 continue;
449 /* If this alternative is only relevant when this operand
450 matches a previous operand, we do different things
451 depending on whether this operand is a allocno-reg or not.
452 We must process any modifiers for the operand before we
453 can make this test. */
454 while (*p == '%' || *p == '=' || *p == '+' || *p == '&')
455 p++;
457 if (p[0] >= '0' && p[0] <= '0' + i && (p[1] == ',' || p[1] == 0))
459 /* Copy class and whether memory is allowed from the
460 matching alternative. Then perform any needed cost
461 computations and/or adjustments. */
462 j = p[0] - '0';
463 classes[i] = classes[j];
464 allows_mem[i] = allows_mem[j];
465 if (allows_mem[i])
466 insn_allows_mem[i] = 1;
468 if (! REG_P (op) || REGNO (op) < FIRST_PSEUDO_REGISTER)
470 /* If this matches the other operand, we have no
471 added cost and we win. */
472 if (rtx_equal_p (ops[j], op))
473 win = 1;
474 /* If we can put the other operand into a register,
475 add to the cost of this alternative the cost to
476 copy this operand to the register used for the
477 other operand. */
478 else if (classes[j] != NO_REGS)
480 alt_cost += copy_cost (op, mode, classes[j], 1, NULL);
481 win = 1;
484 else if (! REG_P (ops[j])
485 || REGNO (ops[j]) < FIRST_PSEUDO_REGISTER)
487 /* This op is an allocno but the one it matches is
488 not. */
490 /* If we can't put the other operand into a
491 register, this alternative can't be used. */
493 if (classes[j] == NO_REGS)
494 alt_fail = 1;
495 /* Otherwise, add to the cost of this alternative
496 the cost to copy the other operand to the hard
497 register used for this operand. */
498 else
499 alt_cost += copy_cost (ops[j], mode, classes[j], 1, NULL);
501 else
503 /* The costs of this operand are not the same as the
504 other operand since move costs are not symmetric.
505 Moreover, if we cannot tie them, this alternative
506 needs to do a copy, which is one insn. */
507 struct costs *pp = this_op_costs[i];
508 int *pp_costs = pp->cost;
509 cost_classes_t cost_classes_ptr
510 = regno_cost_classes[REGNO (op)];
511 enum reg_class *cost_classes = cost_classes_ptr->classes;
512 bool in_p = recog_data.operand_type[i] != OP_OUT;
513 bool out_p = recog_data.operand_type[i] != OP_IN;
514 enum reg_class op_class = classes[i];
515 move_table *move_in_cost, *move_out_cost;
517 ira_init_register_move_cost_if_necessary (mode);
518 if (! in_p)
520 ira_assert (out_p);
521 move_out_cost = ira_may_move_out_cost[mode];
522 for (k = cost_classes_ptr->num - 1; k >= 0; k--)
524 rclass = cost_classes[k];
525 pp_costs[k]
526 = move_out_cost[op_class][rclass] * frequency;
529 else if (! out_p)
531 ira_assert (in_p);
532 move_in_cost = ira_may_move_in_cost[mode];
533 for (k = cost_classes_ptr->num - 1; k >= 0; k--)
535 rclass = cost_classes[k];
536 pp_costs[k]
537 = move_in_cost[rclass][op_class] * frequency;
540 else
542 move_in_cost = ira_may_move_in_cost[mode];
543 move_out_cost = ira_may_move_out_cost[mode];
544 for (k = cost_classes_ptr->num - 1; k >= 0; k--)
546 rclass = cost_classes[k];
547 pp_costs[k] = ((move_in_cost[rclass][op_class]
548 + move_out_cost[op_class][rclass])
549 * frequency);
553 /* If the alternative actually allows memory, make
554 things a bit cheaper since we won't need an extra
555 insn to load it. */
556 pp->mem_cost
557 = ((out_p ? ira_memory_move_cost[mode][op_class][0] : 0)
558 + (in_p ? ira_memory_move_cost[mode][op_class][1] : 0)
559 - allows_mem[i]) * frequency;
561 /* If we have assigned a class to this allocno in
562 our first pass, add a cost to this alternative
563 corresponding to what we would add if this
564 allocno were not in the appropriate class. */
565 if (pref)
567 enum reg_class pref_class = pref[COST_INDEX (REGNO (op))];
569 if (pref_class == NO_REGS)
570 alt_cost
571 += ((out_p
572 ? ira_memory_move_cost[mode][op_class][0] : 0)
573 + (in_p
574 ? ira_memory_move_cost[mode][op_class][1]
575 : 0));
576 else if (ira_reg_class_intersect
577 [pref_class][op_class] == NO_REGS)
578 alt_cost
579 += ira_register_move_cost[mode][pref_class][op_class];
581 if (REGNO (ops[i]) != REGNO (ops[j])
582 && ! find_reg_note (insn, REG_DEAD, op))
583 alt_cost += 2;
585 /* This is in place of ordinary cost computation for
586 this operand, so skip to the end of the
587 alternative (should be just one character). */
588 while (*p && *p++ != ',')
591 constraints[i] = p;
592 continue;
596 /* Scan all the constraint letters. See if the operand
597 matches any of the constraints. Collect the valid
598 register classes and see if this operand accepts
599 memory. */
600 while ((c = *p))
602 switch (c)
604 case ',':
605 break;
606 case '*':
607 /* Ignore the next letter for this pass. */
608 c = *++p;
609 break;
611 case '?':
612 alt_cost += 2;
613 case '!': case '#': case '&':
614 case '0': case '1': case '2': case '3': case '4':
615 case '5': case '6': case '7': case '8': case '9':
616 break;
618 case 'p':
619 allows_addr = 1;
620 win = address_operand (op, GET_MODE (op));
621 /* We know this operand is an address, so we want it
622 to be allocated to a register that can be the
623 base of an address, i.e. BASE_REG_CLASS. */
624 classes[i]
625 = ira_reg_class_subunion[classes[i]]
626 [base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
627 ADDRESS, SCRATCH)];
628 break;
630 case 'm': case 'o': case 'V':
631 /* It doesn't seem worth distinguishing between
632 offsettable and non-offsettable addresses
633 here. */
634 insn_allows_mem[i] = allows_mem[i] = 1;
635 if (MEM_P (op))
636 win = 1;
637 break;
639 case '<':
640 if (MEM_P (op)
641 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
642 || GET_CODE (XEXP (op, 0)) == POST_DEC))
643 win = 1;
644 break;
646 case '>':
647 if (MEM_P (op)
648 && (GET_CODE (XEXP (op, 0)) == PRE_INC
649 || GET_CODE (XEXP (op, 0)) == POST_INC))
650 win = 1;
651 break;
653 case 'E':
654 case 'F':
655 if (CONST_DOUBLE_AS_FLOAT_P (op)
656 || (GET_CODE (op) == CONST_VECTOR
657 && (GET_MODE_CLASS (GET_MODE (op))
658 == MODE_VECTOR_FLOAT)))
659 win = 1;
660 break;
662 case 'G':
663 case 'H':
664 if (CONST_DOUBLE_AS_FLOAT_P (op)
665 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
666 win = 1;
667 break;
669 case 's':
670 if (CONST_INT_P (op) || CONST_DOUBLE_AS_INT_P (op))
671 break;
673 case 'i':
674 if (CONSTANT_P (op)
675 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op)))
676 win = 1;
677 break;
679 case 'n':
680 if (CONST_INT_P (op) || CONST_DOUBLE_AS_INT_P (op))
681 win = 1;
682 break;
684 case 'I':
685 case 'J':
686 case 'K':
687 case 'L':
688 case 'M':
689 case 'N':
690 case 'O':
691 case 'P':
692 if (CONST_INT_P (op)
693 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
694 win = 1;
695 break;
697 case 'X':
698 win = 1;
699 break;
701 case 'g':
702 if (MEM_P (op)
703 || (CONSTANT_P (op)
704 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))))
705 win = 1;
706 insn_allows_mem[i] = allows_mem[i] = 1;
707 case 'r':
708 classes[i] = ira_reg_class_subunion[classes[i]][GENERAL_REGS];
709 break;
711 default:
712 if (REG_CLASS_FROM_CONSTRAINT (c, p) != NO_REGS)
713 classes[i] = ira_reg_class_subunion[classes[i]]
714 [REG_CLASS_FROM_CONSTRAINT (c, p)];
715 #ifdef EXTRA_CONSTRAINT_STR
716 else if (EXTRA_CONSTRAINT_STR (op, c, p))
717 win = 1;
719 if (EXTRA_MEMORY_CONSTRAINT (c, p))
721 /* Every MEM can be reloaded to fit. */
722 insn_allows_mem[i] = allows_mem[i] = 1;
723 if (MEM_P (op))
724 win = 1;
726 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
728 /* Every address can be reloaded to fit. */
729 allows_addr = 1;
730 if (address_operand (op, GET_MODE (op)))
731 win = 1;
732 /* We know this operand is an address, so we
733 want it to be allocated to a hard register
734 that can be the base of an address,
735 i.e. BASE_REG_CLASS. */
736 classes[i]
737 = ira_reg_class_subunion[classes[i]]
738 [base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
739 ADDRESS, SCRATCH)];
741 #endif
742 break;
744 p += CONSTRAINT_LEN (c, p);
745 if (c == ',')
746 break;
749 constraints[i] = p;
751 /* How we account for this operand now depends on whether it
752 is a pseudo register or not. If it is, we first check if
753 any register classes are valid. If not, we ignore this
754 alternative, since we want to assume that all allocnos get
755 allocated for register preferencing. If some register
756 class is valid, compute the costs of moving the allocno
757 into that class. */
758 if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
760 if (classes[i] == NO_REGS)
762 /* We must always fail if the operand is a REG, but
763 we did not find a suitable class.
765 Otherwise we may perform an uninitialized read
766 from this_op_costs after the `continue' statement
767 below. */
768 alt_fail = 1;
770 else
772 unsigned int regno = REGNO (op);
773 struct costs *pp = this_op_costs[i];
774 int *pp_costs = pp->cost;
775 cost_classes_t cost_classes_ptr = regno_cost_classes[regno];
776 enum reg_class *cost_classes = cost_classes_ptr->classes;
777 bool in_p = recog_data.operand_type[i] != OP_OUT;
778 bool out_p = recog_data.operand_type[i] != OP_IN;
779 enum reg_class op_class = classes[i];
780 move_table *move_in_cost, *move_out_cost;
782 ira_init_register_move_cost_if_necessary (mode);
783 if (! in_p)
785 ira_assert (out_p);
786 move_out_cost = ira_may_move_out_cost[mode];
787 for (k = cost_classes_ptr->num - 1; k >= 0; k--)
789 rclass = cost_classes[k];
790 pp_costs[k]
791 = move_out_cost[op_class][rclass] * frequency;
794 else if (! out_p)
796 ira_assert (in_p);
797 move_in_cost = ira_may_move_in_cost[mode];
798 for (k = cost_classes_ptr->num - 1; k >= 0; k--)
800 rclass = cost_classes[k];
801 pp_costs[k]
802 = move_in_cost[rclass][op_class] * frequency;
805 else
807 move_in_cost = ira_may_move_in_cost[mode];
808 move_out_cost = ira_may_move_out_cost[mode];
809 for (k = cost_classes_ptr->num - 1; k >= 0; k--)
811 rclass = cost_classes[k];
812 pp_costs[k] = ((move_in_cost[rclass][op_class]
813 + move_out_cost[op_class][rclass])
814 * frequency);
818 /* If the alternative actually allows memory, make
819 things a bit cheaper since we won't need an extra
820 insn to load it. */
821 pp->mem_cost
822 = ((out_p ? ira_memory_move_cost[mode][op_class][0] : 0)
823 + (in_p ? ira_memory_move_cost[mode][op_class][1] : 0)
824 - allows_mem[i]) * frequency;
825 /* If we have assigned a class to this allocno in
826 our first pass, add a cost to this alternative
827 corresponding to what we would add if this
828 allocno were not in the appropriate class. */
829 if (pref)
831 enum reg_class pref_class = pref[COST_INDEX (REGNO (op))];
833 if (pref_class == NO_REGS)
834 alt_cost
835 += ((out_p
836 ? ira_memory_move_cost[mode][op_class][0] : 0)
837 + (in_p
838 ? ira_memory_move_cost[mode][op_class][1]
839 : 0));
840 else if (ira_reg_class_intersect[pref_class][op_class]
841 == NO_REGS)
842 alt_cost += ira_register_move_cost[mode][pref_class][op_class];
847 /* Otherwise, if this alternative wins, either because we
848 have already determined that or if we have a hard
849 register of the proper class, there is no cost for this
850 alternative. */
851 else if (win || (REG_P (op)
852 && reg_fits_class_p (op, classes[i],
853 0, GET_MODE (op))))
856 /* If registers are valid, the cost of this alternative
857 includes copying the object to and/or from a
858 register. */
859 else if (classes[i] != NO_REGS)
861 if (recog_data.operand_type[i] != OP_OUT)
862 alt_cost += copy_cost (op, mode, classes[i], 1, NULL);
864 if (recog_data.operand_type[i] != OP_IN)
865 alt_cost += copy_cost (op, mode, classes[i], 0, NULL);
867 /* The only other way this alternative can be used is if
868 this is a constant that could be placed into memory. */
869 else if (CONSTANT_P (op) && (allows_addr || allows_mem[i]))
870 alt_cost += ira_memory_move_cost[mode][classes[i]][1];
871 else
872 alt_fail = 1;
875 if (alt_fail)
876 continue;
878 op_cost_add = alt_cost * frequency;
879 /* Finally, update the costs with the information we've
880 calculated about this alternative. */
881 for (i = 0; i < n_ops; i++)
882 if (REG_P (ops[i]) && REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
884 struct costs *pp = op_costs[i], *qq = this_op_costs[i];
885 int *pp_costs = pp->cost, *qq_costs = qq->cost;
886 int scale = 1 + (recog_data.operand_type[i] == OP_INOUT);
887 cost_classes_t cost_classes_ptr
888 = regno_cost_classes[REGNO (ops[i])];
890 pp->mem_cost = MIN (pp->mem_cost,
891 (qq->mem_cost + op_cost_add) * scale);
893 for (k = cost_classes_ptr->num - 1; k >= 0; k--)
894 pp_costs[k]
895 = MIN (pp_costs[k], (qq_costs[k] + op_cost_add) * scale);
899 if (allocno_p)
900 for (i = 0; i < n_ops; i++)
902 ira_allocno_t a;
903 rtx op = ops[i];
905 if (! REG_P (op) || REGNO (op) < FIRST_PSEUDO_REGISTER)
906 continue;
907 a = ira_curr_regno_allocno_map [REGNO (op)];
908 if (! ALLOCNO_BAD_SPILL_P (a) && insn_allows_mem[i] == 0)
909 ALLOCNO_BAD_SPILL_P (a) = true;
912 /* If this insn is a single set copying operand 1 to operand 0 and
913 one operand is an allocno with the other a hard reg or an allocno
914 that prefers a hard register that is in its own register class
915 then we may want to adjust the cost of that register class to -1.
917 Avoid the adjustment if the source does not die to avoid
918 stressing of register allocator by preferrencing two colliding
919 registers into single class.
921 Also avoid the adjustment if a copy between hard registers of the
922 class is expensive (ten times the cost of a default copy is
923 considered arbitrarily expensive). This avoids losing when the
924 preferred class is very expensive as the source of a copy
925 instruction. */
926 if ((set = single_set (insn)) != 0
927 && ops[0] == SET_DEST (set) && ops[1] == SET_SRC (set)
928 && REG_P (ops[0]) && REG_P (ops[1])
929 && find_regno_note (insn, REG_DEAD, REGNO (ops[1])))
930 for (i = 0; i <= 1; i++)
931 if (REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER
932 && REGNO (ops[!i]) < FIRST_PSEUDO_REGISTER)
934 unsigned int regno = REGNO (ops[i]);
935 unsigned int other_regno = REGNO (ops[!i]);
936 enum machine_mode mode = GET_MODE (ops[!i]);
937 cost_classes_t cost_classes_ptr = regno_cost_classes[regno];
938 enum reg_class *cost_classes = cost_classes_ptr->classes;
939 reg_class_t rclass;
940 int nr;
942 for (k = cost_classes_ptr->num - 1; k >= 0; k--)
944 rclass = cost_classes[k];
945 if (TEST_HARD_REG_BIT (reg_class_contents[rclass], other_regno)
946 && (reg_class_size[(int) rclass]
947 == ira_reg_class_max_nregs [(int) rclass][(int) mode]))
949 if (reg_class_size[rclass] == 1)
950 op_costs[i]->cost[k] = -frequency;
951 else
953 for (nr = 0;
954 nr < hard_regno_nregs[other_regno][mode];
955 nr++)
956 if (! TEST_HARD_REG_BIT (reg_class_contents[rclass],
957 other_regno + nr))
958 break;
960 if (nr == hard_regno_nregs[other_regno][mode])
961 op_costs[i]->cost[k] = -frequency;
970 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudo registers. */
971 static inline bool
972 ok_for_index_p_nonstrict (rtx reg)
974 unsigned regno = REGNO (reg);
976 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
979 /* A version of regno_ok_for_base_p for use here, when all
980 pseudo-registers should count as OK. Arguments as for
981 regno_ok_for_base_p. */
982 static inline bool
983 ok_for_base_p_nonstrict (rtx reg, enum machine_mode mode, addr_space_t as,
984 enum rtx_code outer_code, enum rtx_code index_code)
986 unsigned regno = REGNO (reg);
988 if (regno >= FIRST_PSEUDO_REGISTER)
989 return true;
990 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
993 /* Record the pseudo registers we must reload into hard registers in a
994 subexpression of a memory address, X.
996 If CONTEXT is 0, we are looking at the base part of an address,
997 otherwise we are looking at the index part.
999 MODE and AS are the mode and address space of the memory reference;
1000 OUTER_CODE and INDEX_CODE give the context that the rtx appears in.
1001 These four arguments are passed down to base_reg_class.
1003 SCALE is twice the amount to multiply the cost by (it is twice so
1004 we can represent half-cost adjustments). */
1005 static void
1006 record_address_regs (enum machine_mode mode, addr_space_t as, rtx x,
1007 int context, enum rtx_code outer_code,
1008 enum rtx_code index_code, int scale)
1010 enum rtx_code code = GET_CODE (x);
1011 enum reg_class rclass;
1013 if (context == 1)
1014 rclass = INDEX_REG_CLASS;
1015 else
1016 rclass = base_reg_class (mode, as, outer_code, index_code);
1018 switch (code)
1020 case CONST_INT:
1021 case CONST:
1022 case CC0:
1023 case PC:
1024 case SYMBOL_REF:
1025 case LABEL_REF:
1026 return;
1028 case PLUS:
1029 /* When we have an address that is a sum, we must determine
1030 whether registers are "base" or "index" regs. If there is a
1031 sum of two registers, we must choose one to be the "base".
1032 Luckily, we can use the REG_POINTER to make a good choice
1033 most of the time. We only need to do this on machines that
1034 can have two registers in an address and where the base and
1035 index register classes are different.
1037 ??? This code used to set REGNO_POINTER_FLAG in some cases,
1038 but that seems bogus since it should only be set when we are
1039 sure the register is being used as a pointer. */
1041 rtx arg0 = XEXP (x, 0);
1042 rtx arg1 = XEXP (x, 1);
1043 enum rtx_code code0 = GET_CODE (arg0);
1044 enum rtx_code code1 = GET_CODE (arg1);
1046 /* Look inside subregs. */
1047 if (code0 == SUBREG)
1048 arg0 = SUBREG_REG (arg0), code0 = GET_CODE (arg0);
1049 if (code1 == SUBREG)
1050 arg1 = SUBREG_REG (arg1), code1 = GET_CODE (arg1);
1052 /* If this machine only allows one register per address, it
1053 must be in the first operand. */
1054 if (MAX_REGS_PER_ADDRESS == 1)
1055 record_address_regs (mode, as, arg0, 0, PLUS, code1, scale);
1057 /* If index and base registers are the same on this machine,
1058 just record registers in any non-constant operands. We
1059 assume here, as well as in the tests below, that all
1060 addresses are in canonical form. */
1061 else if (INDEX_REG_CLASS
1062 == base_reg_class (VOIDmode, as, PLUS, SCRATCH))
1064 record_address_regs (mode, as, arg0, context, PLUS, code1, scale);
1065 if (! CONSTANT_P (arg1))
1066 record_address_regs (mode, as, arg1, context, PLUS, code0, scale);
1069 /* If the second operand is a constant integer, it doesn't
1070 change what class the first operand must be. */
1071 else if (code1 == CONST_INT || code1 == CONST_DOUBLE)
1072 record_address_regs (mode, as, arg0, context, PLUS, code1, scale);
1073 /* If the second operand is a symbolic constant, the first
1074 operand must be an index register. */
1075 else if (code1 == SYMBOL_REF || code1 == CONST || code1 == LABEL_REF)
1076 record_address_regs (mode, as, arg0, 1, PLUS, code1, scale);
1077 /* If both operands are registers but one is already a hard
1078 register of index or reg-base class, give the other the
1079 class that the hard register is not. */
1080 else if (code0 == REG && code1 == REG
1081 && REGNO (arg0) < FIRST_PSEUDO_REGISTER
1082 && (ok_for_base_p_nonstrict (arg0, mode, as, PLUS, REG)
1083 || ok_for_index_p_nonstrict (arg0)))
1084 record_address_regs (mode, as, arg1,
1085 ok_for_base_p_nonstrict (arg0, mode, as,
1086 PLUS, REG) ? 1 : 0,
1087 PLUS, REG, scale);
1088 else if (code0 == REG && code1 == REG
1089 && REGNO (arg1) < FIRST_PSEUDO_REGISTER
1090 && (ok_for_base_p_nonstrict (arg1, mode, as, PLUS, REG)
1091 || ok_for_index_p_nonstrict (arg1)))
1092 record_address_regs (mode, as, arg0,
1093 ok_for_base_p_nonstrict (arg1, mode, as,
1094 PLUS, REG) ? 1 : 0,
1095 PLUS, REG, scale);
1096 /* If one operand is known to be a pointer, it must be the
1097 base with the other operand the index. Likewise if the
1098 other operand is a MULT. */
1099 else if ((code0 == REG && REG_POINTER (arg0)) || code1 == MULT)
1101 record_address_regs (mode, as, arg0, 0, PLUS, code1, scale);
1102 record_address_regs (mode, as, arg1, 1, PLUS, code0, scale);
1104 else if ((code1 == REG && REG_POINTER (arg1)) || code0 == MULT)
1106 record_address_regs (mode, as, arg0, 1, PLUS, code1, scale);
1107 record_address_regs (mode, as, arg1, 0, PLUS, code0, scale);
1109 /* Otherwise, count equal chances that each might be a base or
1110 index register. This case should be rare. */
1111 else
1113 record_address_regs (mode, as, arg0, 0, PLUS, code1, scale / 2);
1114 record_address_regs (mode, as, arg0, 1, PLUS, code1, scale / 2);
1115 record_address_regs (mode, as, arg1, 0, PLUS, code0, scale / 2);
1116 record_address_regs (mode, as, arg1, 1, PLUS, code0, scale / 2);
1119 break;
1121 /* Double the importance of an allocno that is incremented or
1122 decremented, since it would take two extra insns if it ends
1123 up in the wrong place. */
1124 case POST_MODIFY:
1125 case PRE_MODIFY:
1126 record_address_regs (mode, as, XEXP (x, 0), 0, code,
1127 GET_CODE (XEXP (XEXP (x, 1), 1)), 2 * scale);
1128 if (REG_P (XEXP (XEXP (x, 1), 1)))
1129 record_address_regs (mode, as, XEXP (XEXP (x, 1), 1), 1, code, REG,
1130 2 * scale);
1131 break;
1133 case POST_INC:
1134 case PRE_INC:
1135 case POST_DEC:
1136 case PRE_DEC:
1137 /* Double the importance of an allocno that is incremented or
1138 decremented, since it would take two extra insns if it ends
1139 up in the wrong place. */
1140 record_address_regs (mode, as, XEXP (x, 0), 0, code, SCRATCH, 2 * scale);
1141 break;
1143 case REG:
1145 struct costs *pp;
1146 int *pp_costs;
1147 enum reg_class i;
1148 int k, regno, add_cost;
1149 cost_classes_t cost_classes_ptr;
1150 enum reg_class *cost_classes;
1151 move_table *move_in_cost;
1153 if (REGNO (x) < FIRST_PSEUDO_REGISTER)
1154 break;
1156 regno = REGNO (x);
1157 if (allocno_p)
1158 ALLOCNO_BAD_SPILL_P (ira_curr_regno_allocno_map[regno]) = true;
1159 pp = COSTS (costs, COST_INDEX (regno));
1160 add_cost = (ira_memory_move_cost[Pmode][rclass][1] * scale) / 2;
1161 if (INT_MAX - add_cost < pp->mem_cost)
1162 pp->mem_cost = INT_MAX;
1163 else
1164 pp->mem_cost += add_cost;
1165 cost_classes_ptr = regno_cost_classes[regno];
1166 cost_classes = cost_classes_ptr->classes;
1167 pp_costs = pp->cost;
1168 ira_init_register_move_cost_if_necessary (Pmode);
1169 move_in_cost = ira_may_move_in_cost[Pmode];
1170 for (k = cost_classes_ptr->num - 1; k >= 0; k--)
1172 i = cost_classes[k];
1173 add_cost = (move_in_cost[i][rclass] * scale) / 2;
1174 if (INT_MAX - add_cost < pp_costs[k])
1175 pp_costs[k] = INT_MAX;
1176 else
1177 pp_costs[k] += add_cost;
1180 break;
1182 default:
1184 const char *fmt = GET_RTX_FORMAT (code);
1185 int i;
1186 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1187 if (fmt[i] == 'e')
1188 record_address_regs (mode, as, XEXP (x, i), context, code, SCRATCH,
1189 scale);
1196 /* Calculate the costs of insn operands. */
1197 static void
1198 record_operand_costs (rtx insn, enum reg_class *pref)
1200 const char *constraints[MAX_RECOG_OPERANDS];
1201 enum machine_mode modes[MAX_RECOG_OPERANDS];
1202 int i;
1204 for (i = 0; i < recog_data.n_operands; i++)
1206 constraints[i] = recog_data.constraints[i];
1207 modes[i] = recog_data.operand_mode[i];
1210 /* If we get here, we are set up to record the costs of all the
1211 operands for this insn. Start by initializing the costs. Then
1212 handle any address registers. Finally record the desired classes
1213 for any allocnos, doing it twice if some pair of operands are
1214 commutative. */
1215 for (i = 0; i < recog_data.n_operands; i++)
1217 memcpy (op_costs[i], init_cost, struct_costs_size);
1219 if (GET_CODE (recog_data.operand[i]) == SUBREG)
1220 recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
1222 if (MEM_P (recog_data.operand[i]))
1223 record_address_regs (GET_MODE (recog_data.operand[i]),
1224 MEM_ADDR_SPACE (recog_data.operand[i]),
1225 XEXP (recog_data.operand[i], 0),
1226 0, MEM, SCRATCH, frequency * 2);
1227 else if (constraints[i][0] == 'p'
1228 || EXTRA_ADDRESS_CONSTRAINT (constraints[i][0],
1229 constraints[i]))
1230 record_address_regs (VOIDmode, ADDR_SPACE_GENERIC,
1231 recog_data.operand[i], 0, ADDRESS, SCRATCH,
1232 frequency * 2);
1235 /* Check for commutative in a separate loop so everything will have
1236 been initialized. We must do this even if one operand is a
1237 constant--see addsi3 in m68k.md. */
1238 for (i = 0; i < (int) recog_data.n_operands - 1; i++)
1239 if (constraints[i][0] == '%')
1241 const char *xconstraints[MAX_RECOG_OPERANDS];
1242 int j;
1244 /* Handle commutative operands by swapping the constraints.
1245 We assume the modes are the same. */
1246 for (j = 0; j < recog_data.n_operands; j++)
1247 xconstraints[j] = constraints[j];
1249 xconstraints[i] = constraints[i+1];
1250 xconstraints[i+1] = constraints[i];
1251 record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
1252 recog_data.operand, modes,
1253 xconstraints, insn, pref);
1255 record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
1256 recog_data.operand, modes,
1257 constraints, insn, pref);
1262 /* Process one insn INSN. Scan it and record each time it would save
1263 code to put a certain allocnos in a certain class. Return the last
1264 insn processed, so that the scan can be continued from there. */
1265 static rtx
1266 scan_one_insn (rtx insn)
1268 enum rtx_code pat_code;
1269 rtx set, note;
1270 int i, k;
1271 bool counted_mem;
1273 if (!NONDEBUG_INSN_P (insn))
1274 return insn;
1276 pat_code = GET_CODE (PATTERN (insn));
1277 if (pat_code == USE || pat_code == CLOBBER || pat_code == ASM_INPUT
1278 || pat_code == ADDR_VEC || pat_code == ADDR_DIFF_VEC)
1279 return insn;
1281 counted_mem = false;
1282 set = single_set (insn);
1283 extract_insn (insn);
1285 /* If this insn loads a parameter from its stack slot, then it
1286 represents a savings, rather than a cost, if the parameter is
1287 stored in memory. Record this fact.
1289 Similarly if we're loading other constants from memory (constant
1290 pool, TOC references, small data areas, etc) and this is the only
1291 assignment to the destination pseudo.
1293 Don't do this if SET_SRC (set) isn't a general operand, if it is
1294 a memory requiring special instructions to load it, decreasing
1295 mem_cost might result in it being loaded using the specialized
1296 instruction into a register, then stored into stack and loaded
1297 again from the stack. See PR52208. */
1298 if (set != 0 && REG_P (SET_DEST (set)) && MEM_P (SET_SRC (set))
1299 && (note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != NULL_RTX
1300 && ((MEM_P (XEXP (note, 0)))
1301 || (CONSTANT_P (XEXP (note, 0))
1302 && targetm.legitimate_constant_p (GET_MODE (SET_DEST (set)),
1303 XEXP (note, 0))
1304 && REG_N_SETS (REGNO (SET_DEST (set))) == 1))
1305 && general_operand (SET_SRC (set), GET_MODE (SET_SRC (set))))
1307 enum reg_class cl = GENERAL_REGS;
1308 rtx reg = SET_DEST (set);
1309 int num = COST_INDEX (REGNO (reg));
1311 COSTS (costs, num)->mem_cost
1312 -= ira_memory_move_cost[GET_MODE (reg)][cl][1] * frequency;
1313 record_address_regs (GET_MODE (SET_SRC (set)),
1314 MEM_ADDR_SPACE (SET_SRC (set)),
1315 XEXP (SET_SRC (set), 0), 0, MEM, SCRATCH,
1316 frequency * 2);
1317 counted_mem = true;
1320 record_operand_costs (insn, pref);
1322 /* Now add the cost for each operand to the total costs for its
1323 allocno. */
1324 for (i = 0; i < recog_data.n_operands; i++)
1325 if (REG_P (recog_data.operand[i])
1326 && REGNO (recog_data.operand[i]) >= FIRST_PSEUDO_REGISTER)
1328 int regno = REGNO (recog_data.operand[i]);
1329 struct costs *p = COSTS (costs, COST_INDEX (regno));
1330 struct costs *q = op_costs[i];
1331 int *p_costs = p->cost, *q_costs = q->cost;
1332 cost_classes_t cost_classes_ptr = regno_cost_classes[regno];
1333 int add_cost;
1335 /* If the already accounted for the memory "cost" above, don't
1336 do so again. */
1337 if (!counted_mem)
1339 add_cost = q->mem_cost;
1340 if (add_cost > 0 && INT_MAX - add_cost < p->mem_cost)
1341 p->mem_cost = INT_MAX;
1342 else
1343 p->mem_cost += add_cost;
1345 for (k = cost_classes_ptr->num - 1; k >= 0; k--)
1347 add_cost = q_costs[k];
1348 if (add_cost > 0 && INT_MAX - add_cost < p_costs[k])
1349 p_costs[k] = INT_MAX;
1350 else
1351 p_costs[k] += add_cost;
1355 return insn;
1360 /* Print allocnos costs to file F. */
1361 static void
1362 print_allocno_costs (FILE *f)
1364 int k;
1365 ira_allocno_t a;
1366 ira_allocno_iterator ai;
1368 ira_assert (allocno_p);
1369 fprintf (f, "\n");
1370 FOR_EACH_ALLOCNO (a, ai)
1372 int i, rclass;
1373 basic_block bb;
1374 int regno = ALLOCNO_REGNO (a);
1375 cost_classes_t cost_classes_ptr = regno_cost_classes[regno];
1376 enum reg_class *cost_classes = cost_classes_ptr->classes;
1378 i = ALLOCNO_NUM (a);
1379 fprintf (f, " a%d(r%d,", i, regno);
1380 if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
1381 fprintf (f, "b%d", bb->index);
1382 else
1383 fprintf (f, "l%d", ALLOCNO_LOOP_TREE_NODE (a)->loop_num);
1384 fprintf (f, ") costs:");
1385 for (k = 0; k < cost_classes_ptr->num; k++)
1387 rclass = cost_classes[k];
1388 if (contains_reg_of_mode[rclass][PSEUDO_REGNO_MODE (regno)]
1389 #ifdef CANNOT_CHANGE_MODE_CLASS
1390 && ! invalid_mode_change_p (regno, (enum reg_class) rclass)
1391 #endif
1394 fprintf (f, " %s:%d", reg_class_names[rclass],
1395 COSTS (costs, i)->cost[k]);
1396 if (flag_ira_region == IRA_REGION_ALL
1397 || flag_ira_region == IRA_REGION_MIXED)
1398 fprintf (f, ",%d", COSTS (total_allocno_costs, i)->cost[k]);
1401 fprintf (f, " MEM:%i", COSTS (costs, i)->mem_cost);
1402 if (flag_ira_region == IRA_REGION_ALL
1403 || flag_ira_region == IRA_REGION_MIXED)
1404 fprintf (f, ",%d", COSTS (total_allocno_costs, i)->mem_cost);
1405 fprintf (f, "\n");
1409 /* Print pseudo costs to file F. */
1410 static void
1411 print_pseudo_costs (FILE *f)
1413 int regno, k;
1414 int rclass;
1415 cost_classes_t cost_classes_ptr;
1416 enum reg_class *cost_classes;
1418 ira_assert (! allocno_p);
1419 fprintf (f, "\n");
1420 for (regno = max_reg_num () - 1; regno >= FIRST_PSEUDO_REGISTER; regno--)
1422 if (REG_N_REFS (regno) <= 0)
1423 continue;
1424 cost_classes_ptr = regno_cost_classes[regno];
1425 cost_classes = cost_classes_ptr->classes;
1426 fprintf (f, " r%d costs:", regno);
1427 for (k = 0; k < cost_classes_ptr->num; k++)
1429 rclass = cost_classes[k];
1430 if (contains_reg_of_mode[rclass][PSEUDO_REGNO_MODE (regno)]
1431 #ifdef CANNOT_CHANGE_MODE_CLASS
1432 && ! invalid_mode_change_p (regno, (enum reg_class) rclass)
1433 #endif
1435 fprintf (f, " %s:%d", reg_class_names[rclass],
1436 COSTS (costs, regno)->cost[k]);
1438 fprintf (f, " MEM:%i\n", COSTS (costs, regno)->mem_cost);
1442 /* Traverse the BB represented by LOOP_TREE_NODE to update the allocno
1443 costs. */
1444 static void
1445 process_bb_for_costs (basic_block bb)
1447 rtx insn;
1449 frequency = REG_FREQ_FROM_BB (bb);
1450 if (frequency == 0)
1451 frequency = 1;
1452 FOR_BB_INSNS (bb, insn)
1453 insn = scan_one_insn (insn);
1456 /* Traverse the BB represented by LOOP_TREE_NODE to update the allocno
1457 costs. */
1458 static void
1459 process_bb_node_for_costs (ira_loop_tree_node_t loop_tree_node)
1461 basic_block bb;
1463 bb = loop_tree_node->bb;
1464 if (bb != NULL)
1465 process_bb_for_costs (bb);
1468 /* Find costs of register classes and memory for allocnos or pseudos
1469 and their best costs. Set up preferred, alternative and allocno
1470 classes for pseudos. */
1471 static void
1472 find_costs_and_classes (FILE *dump_file)
1474 int i, k, start, max_cost_classes_num;
1475 int pass;
1476 basic_block bb;
1477 enum reg_class *regno_best_class;
1479 init_recog ();
1480 regno_best_class
1481 = (enum reg_class *) ira_allocate (max_reg_num ()
1482 * sizeof (enum reg_class));
1483 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
1484 regno_best_class[i] = NO_REGS;
1485 if (!resize_reg_info () && allocno_p
1486 && pseudo_classes_defined_p && flag_expensive_optimizations)
1488 ira_allocno_t a;
1489 ira_allocno_iterator ai;
1491 pref = pref_buffer;
1492 max_cost_classes_num = 1;
1493 FOR_EACH_ALLOCNO (a, ai)
1495 pref[ALLOCNO_NUM (a)] = reg_preferred_class (ALLOCNO_REGNO (a));
1496 setup_regno_cost_classes_by_aclass
1497 (ALLOCNO_REGNO (a), pref[ALLOCNO_NUM (a)]);
1498 max_cost_classes_num
1499 = MAX (max_cost_classes_num,
1500 regno_cost_classes[ALLOCNO_REGNO (a)]->num);
1502 start = 1;
1504 else
1506 pref = NULL;
1507 max_cost_classes_num = ira_important_classes_num;
1508 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
1509 if (regno_reg_rtx[i] != NULL_RTX)
1510 setup_regno_cost_classes_by_mode (i, PSEUDO_REGNO_MODE (i));
1511 else
1512 setup_regno_cost_classes_by_aclass (i, ALL_REGS);
1513 start = 0;
1515 if (allocno_p)
1516 /* Clear the flag for the next compiled function. */
1517 pseudo_classes_defined_p = false;
1518 /* Normally we scan the insns once and determine the best class to
1519 use for each allocno. However, if -fexpensive-optimizations are
1520 on, we do so twice, the second time using the tentative best
1521 classes to guide the selection. */
1522 for (pass = start; pass <= flag_expensive_optimizations; pass++)
1524 if ((!allocno_p || internal_flag_ira_verbose > 0) && dump_file)
1525 fprintf (dump_file,
1526 "\nPass %i for finding pseudo/allocno costs\n\n", pass);
1528 if (pass != start)
1530 max_cost_classes_num = 1;
1531 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
1533 setup_regno_cost_classes_by_aclass (i, regno_best_class[i]);
1534 max_cost_classes_num
1535 = MAX (max_cost_classes_num, regno_cost_classes[i]->num);
1539 struct_costs_size
1540 = sizeof (struct costs) + sizeof (int) * (max_cost_classes_num - 1);
1541 /* Zero out our accumulation of the cost of each class for each
1542 allocno. */
1543 memset (costs, 0, cost_elements_num * struct_costs_size);
1545 if (allocno_p)
1547 /* Scan the instructions and record each time it would save code
1548 to put a certain allocno in a certain class. */
1549 ira_traverse_loop_tree (true, ira_loop_tree_root,
1550 process_bb_node_for_costs, NULL);
1552 memcpy (total_allocno_costs, costs,
1553 max_struct_costs_size * ira_allocnos_num);
1555 else
1557 basic_block bb;
1559 FOR_EACH_BB (bb)
1560 process_bb_for_costs (bb);
1563 if (pass == 0)
1564 pref = pref_buffer;
1566 /* Now for each allocno look at how desirable each class is and
1567 find which class is preferred. */
1568 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
1570 ira_allocno_t a, parent_a;
1571 int rclass, a_num, parent_a_num, add_cost;
1572 ira_loop_tree_node_t parent;
1573 int best_cost, allocno_cost;
1574 enum reg_class best, alt_class;
1575 cost_classes_t cost_classes_ptr = regno_cost_classes[i];
1576 enum reg_class *cost_classes = cost_classes_ptr->classes;
1577 int *i_costs = temp_costs->cost;
1578 int i_mem_cost;
1579 int equiv_savings = regno_equiv_gains[i];
1581 if (! allocno_p)
1583 if (regno_reg_rtx[i] == NULL_RTX)
1584 continue;
1585 memcpy (temp_costs, COSTS (costs, i), struct_costs_size);
1586 i_mem_cost = temp_costs->mem_cost;
1588 else
1590 if (ira_regno_allocno_map[i] == NULL)
1591 continue;
1592 memset (temp_costs, 0, struct_costs_size);
1593 i_mem_cost = 0;
1594 /* Find cost of all allocnos with the same regno. */
1595 for (a = ira_regno_allocno_map[i];
1596 a != NULL;
1597 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
1599 int *a_costs, *p_costs;
1601 a_num = ALLOCNO_NUM (a);
1602 if ((flag_ira_region == IRA_REGION_ALL
1603 || flag_ira_region == IRA_REGION_MIXED)
1604 && (parent = ALLOCNO_LOOP_TREE_NODE (a)->parent) != NULL
1605 && (parent_a = parent->regno_allocno_map[i]) != NULL
1606 /* There are no caps yet. */
1607 && bitmap_bit_p (ALLOCNO_LOOP_TREE_NODE
1608 (a)->border_allocnos,
1609 ALLOCNO_NUM (a)))
1611 /* Propagate costs to upper levels in the region
1612 tree. */
1613 parent_a_num = ALLOCNO_NUM (parent_a);
1614 a_costs = COSTS (total_allocno_costs, a_num)->cost;
1615 p_costs = COSTS (total_allocno_costs, parent_a_num)->cost;
1616 for (k = cost_classes_ptr->num - 1; k >= 0; k--)
1618 add_cost = a_costs[k];
1619 if (add_cost > 0 && INT_MAX - add_cost < p_costs[k])
1620 p_costs[k] = INT_MAX;
1621 else
1622 p_costs[k] += add_cost;
1624 add_cost = COSTS (total_allocno_costs, a_num)->mem_cost;
1625 if (add_cost > 0
1626 && (INT_MAX - add_cost
1627 < COSTS (total_allocno_costs,
1628 parent_a_num)->mem_cost))
1629 COSTS (total_allocno_costs, parent_a_num)->mem_cost
1630 = INT_MAX;
1631 else
1632 COSTS (total_allocno_costs, parent_a_num)->mem_cost
1633 += add_cost;
1635 if (i >= first_moveable_pseudo && i < last_moveable_pseudo)
1636 COSTS (total_allocno_costs, parent_a_num)->mem_cost = 0;
1638 a_costs = COSTS (costs, a_num)->cost;
1639 for (k = cost_classes_ptr->num - 1; k >= 0; k--)
1641 add_cost = a_costs[k];
1642 if (add_cost > 0 && INT_MAX - add_cost < i_costs[k])
1643 i_costs[k] = INT_MAX;
1644 else
1645 i_costs[k] += add_cost;
1647 add_cost = COSTS (costs, a_num)->mem_cost;
1648 if (add_cost > 0 && INT_MAX - add_cost < i_mem_cost)
1649 i_mem_cost = INT_MAX;
1650 else
1651 i_mem_cost += add_cost;
1654 if (i >= first_moveable_pseudo && i < last_moveable_pseudo)
1655 i_mem_cost = 0;
1656 else if (equiv_savings < 0)
1657 i_mem_cost = -equiv_savings;
1658 else if (equiv_savings > 0)
1660 i_mem_cost = 0;
1661 for (k = cost_classes_ptr->num - 1; k >= 0; k--)
1662 i_costs[k] += equiv_savings;
1665 best_cost = (1 << (HOST_BITS_PER_INT - 2)) - 1;
1666 best = ALL_REGS;
1667 alt_class = NO_REGS;
1668 /* Find best common class for all allocnos with the same
1669 regno. */
1670 for (k = 0; k < cost_classes_ptr->num; k++)
1672 rclass = cost_classes[k];
1673 /* Ignore classes that are too small or invalid for this
1674 operand. */
1675 if (! contains_reg_of_mode[rclass][PSEUDO_REGNO_MODE (i)]
1676 #ifdef CANNOT_CHANGE_MODE_CLASS
1677 || invalid_mode_change_p (i, (enum reg_class) rclass)
1678 #endif
1680 continue;
1681 if (i_costs[k] < best_cost)
1683 best_cost = i_costs[k];
1684 best = (enum reg_class) rclass;
1686 else if (i_costs[k] == best_cost)
1687 best = ira_reg_class_subunion[best][rclass];
1688 if (pass == flag_expensive_optimizations
1689 /* We still prefer registers to memory even at this
1690 stage if their costs are the same. We will make
1691 a final decision during assigning hard registers
1692 when we have all info including more accurate
1693 costs which might be affected by assigning hard
1694 registers to other pseudos because the pseudos
1695 involved in moves can be coalesced. */
1696 && i_costs[k] <= i_mem_cost
1697 && (reg_class_size[reg_class_subunion[alt_class][rclass]]
1698 > reg_class_size[alt_class]))
1699 alt_class = reg_class_subunion[alt_class][rclass];
1701 alt_class = ira_allocno_class_translate[alt_class];
1702 if (best_cost > i_mem_cost)
1703 regno_aclass[i] = NO_REGS;
1704 else
1706 /* Make the common class the biggest class of best and
1707 alt_class. */
1708 regno_aclass[i]
1709 = ira_reg_class_superunion[best][alt_class];
1710 ira_assert (regno_aclass[i] != NO_REGS
1711 && ira_reg_allocno_class_p[regno_aclass[i]]);
1713 if (pass == flag_expensive_optimizations)
1715 if (best_cost > i_mem_cost)
1716 best = alt_class = NO_REGS;
1717 else if (best == alt_class)
1718 alt_class = NO_REGS;
1719 setup_reg_classes (i, best, alt_class, regno_aclass[i]);
1720 if ((!allocno_p || internal_flag_ira_verbose > 2)
1721 && dump_file != NULL)
1722 fprintf (dump_file,
1723 " r%d: preferred %s, alternative %s, allocno %s\n",
1724 i, reg_class_names[best], reg_class_names[alt_class],
1725 reg_class_names[regno_aclass[i]]);
1727 regno_best_class[i] = best;
1728 if (! allocno_p)
1730 pref[i] = best_cost > i_mem_cost ? NO_REGS : best;
1731 continue;
1733 for (a = ira_regno_allocno_map[i];
1734 a != NULL;
1735 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
1737 a_num = ALLOCNO_NUM (a);
1738 if (regno_aclass[i] == NO_REGS)
1739 best = NO_REGS;
1740 else
1742 int *total_a_costs = COSTS (total_allocno_costs, a_num)->cost;
1743 int *a_costs = COSTS (costs, a_num)->cost;
1745 /* Finding best class which is subset of the common
1746 class. */
1747 best_cost = (1 << (HOST_BITS_PER_INT - 2)) - 1;
1748 allocno_cost = best_cost;
1749 best = ALL_REGS;
1750 for (k = 0; k < cost_classes_ptr->num; k++)
1752 rclass = cost_classes[k];
1753 if (! ira_class_subset_p[rclass][regno_aclass[i]])
1754 continue;
1755 /* Ignore classes that are too small or invalid
1756 for this operand. */
1757 if (! contains_reg_of_mode[rclass][PSEUDO_REGNO_MODE (i)]
1758 #ifdef CANNOT_CHANGE_MODE_CLASS
1759 || invalid_mode_change_p (i, (enum reg_class) rclass)
1760 #endif
1763 else if (total_a_costs[k] < best_cost)
1765 best_cost = total_a_costs[k];
1766 allocno_cost = a_costs[k];
1767 best = (enum reg_class) rclass;
1769 else if (total_a_costs[k] == best_cost)
1771 best = ira_reg_class_subunion[best][rclass];
1772 allocno_cost = MAX (allocno_cost, a_costs[k]);
1775 ALLOCNO_CLASS_COST (a) = allocno_cost;
1777 if (internal_flag_ira_verbose > 2 && dump_file != NULL
1778 && (pass == 0 || pref[a_num] != best))
1780 fprintf (dump_file, " a%d (r%d,", a_num, i);
1781 if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
1782 fprintf (dump_file, "b%d", bb->index);
1783 else
1784 fprintf (dump_file, "l%d",
1785 ALLOCNO_LOOP_TREE_NODE (a)->loop_num);
1786 fprintf (dump_file, ") best %s, allocno %s\n",
1787 reg_class_names[best],
1788 reg_class_names[regno_aclass[i]]);
1790 pref[a_num] = best;
1794 if (internal_flag_ira_verbose > 4 && dump_file)
1796 if (allocno_p)
1797 print_allocno_costs (dump_file);
1798 else
1799 print_pseudo_costs (dump_file);
1800 fprintf (dump_file,"\n");
1803 ira_free (regno_best_class);
1808 /* Process moves involving hard regs to modify allocno hard register
1809 costs. We can do this only after determining allocno class. If a
1810 hard register forms a register class, than moves with the hard
1811 register are already taken into account in class costs for the
1812 allocno. */
1813 static void
1814 process_bb_node_for_hard_reg_moves (ira_loop_tree_node_t loop_tree_node)
1816 int i, freq, cost, src_regno, dst_regno, hard_regno;
1817 bool to_p;
1818 ira_allocno_t a;
1819 enum reg_class rclass, hard_reg_class;
1820 enum machine_mode mode;
1821 basic_block bb;
1822 rtx insn, set, src, dst;
1824 bb = loop_tree_node->bb;
1825 if (bb == NULL)
1826 return;
1827 freq = REG_FREQ_FROM_BB (bb);
1828 if (freq == 0)
1829 freq = 1;
1830 FOR_BB_INSNS (bb, insn)
1832 if (!NONDEBUG_INSN_P (insn))
1833 continue;
1834 set = single_set (insn);
1835 if (set == NULL_RTX)
1836 continue;
1837 dst = SET_DEST (set);
1838 src = SET_SRC (set);
1839 if (! REG_P (dst) || ! REG_P (src))
1840 continue;
1841 dst_regno = REGNO (dst);
1842 src_regno = REGNO (src);
1843 if (dst_regno >= FIRST_PSEUDO_REGISTER
1844 && src_regno < FIRST_PSEUDO_REGISTER)
1846 hard_regno = src_regno;
1847 to_p = true;
1848 a = ira_curr_regno_allocno_map[dst_regno];
1850 else if (src_regno >= FIRST_PSEUDO_REGISTER
1851 && dst_regno < FIRST_PSEUDO_REGISTER)
1853 hard_regno = dst_regno;
1854 to_p = false;
1855 a = ira_curr_regno_allocno_map[src_regno];
1857 else
1858 continue;
1859 rclass = ALLOCNO_CLASS (a);
1860 if (! TEST_HARD_REG_BIT (reg_class_contents[rclass], hard_regno))
1861 continue;
1862 i = ira_class_hard_reg_index[rclass][hard_regno];
1863 if (i < 0)
1864 continue;
1865 mode = ALLOCNO_MODE (a);
1866 hard_reg_class = REGNO_REG_CLASS (hard_regno);
1867 ira_init_register_move_cost_if_necessary (mode);
1868 cost
1869 = (to_p ? ira_register_move_cost[mode][hard_reg_class][rclass]
1870 : ira_register_move_cost[mode][rclass][hard_reg_class]) * freq;
1871 ira_allocate_and_set_costs (&ALLOCNO_HARD_REG_COSTS (a), rclass,
1872 ALLOCNO_CLASS_COST (a));
1873 ira_allocate_and_set_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a),
1874 rclass, 0);
1875 ALLOCNO_HARD_REG_COSTS (a)[i] -= cost;
1876 ALLOCNO_CONFLICT_HARD_REG_COSTS (a)[i] -= cost;
1877 ALLOCNO_CLASS_COST (a) = MIN (ALLOCNO_CLASS_COST (a),
1878 ALLOCNO_HARD_REG_COSTS (a)[i]);
1882 /* After we find hard register and memory costs for allocnos, define
1883 its class and modify hard register cost because insns moving
1884 allocno to/from hard registers. */
1885 static void
1886 setup_allocno_class_and_costs (void)
1888 int i, j, n, regno, hard_regno, num;
1889 int *reg_costs;
1890 enum reg_class aclass, rclass;
1891 ira_allocno_t a;
1892 ira_allocno_iterator ai;
1893 cost_classes_t cost_classes_ptr;
1895 ira_assert (allocno_p);
1896 FOR_EACH_ALLOCNO (a, ai)
1898 i = ALLOCNO_NUM (a);
1899 regno = ALLOCNO_REGNO (a);
1900 aclass = regno_aclass[regno];
1901 cost_classes_ptr = regno_cost_classes[regno];
1902 ira_assert (pref[i] == NO_REGS || aclass != NO_REGS);
1903 ALLOCNO_MEMORY_COST (a) = COSTS (costs, i)->mem_cost;
1904 ira_set_allocno_class (a, aclass);
1905 if (aclass == NO_REGS)
1906 continue;
1907 if (optimize && ALLOCNO_CLASS (a) != pref[i])
1909 n = ira_class_hard_regs_num[aclass];
1910 ALLOCNO_HARD_REG_COSTS (a)
1911 = reg_costs = ira_allocate_cost_vector (aclass);
1912 for (j = n - 1; j >= 0; j--)
1914 hard_regno = ira_class_hard_regs[aclass][j];
1915 if (TEST_HARD_REG_BIT (reg_class_contents[pref[i]], hard_regno))
1916 reg_costs[j] = ALLOCNO_CLASS_COST (a);
1917 else
1919 rclass = REGNO_REG_CLASS (hard_regno);
1920 num = cost_classes_ptr->index[rclass];
1921 if (num < 0)
1923 num = cost_classes_ptr->hard_regno_index[hard_regno];
1924 ira_assert (num >= 0);
1926 reg_costs[j] = COSTS (costs, i)->cost[num];
1931 if (optimize)
1932 ira_traverse_loop_tree (true, ira_loop_tree_root,
1933 process_bb_node_for_hard_reg_moves, NULL);
1938 /* Function called once during compiler work. */
1939 void
1940 ira_init_costs_once (void)
1942 int i;
1944 init_cost = NULL;
1945 for (i = 0; i < MAX_RECOG_OPERANDS; i++)
1947 op_costs[i] = NULL;
1948 this_op_costs[i] = NULL;
1950 temp_costs = NULL;
1953 /* Free allocated temporary cost vectors. */
1954 static void
1955 free_ira_costs (void)
1957 int i;
1959 free (init_cost);
1960 init_cost = NULL;
1961 for (i = 0; i < MAX_RECOG_OPERANDS; i++)
1963 free (op_costs[i]);
1964 free (this_op_costs[i]);
1965 op_costs[i] = this_op_costs[i] = NULL;
1967 free (temp_costs);
1968 temp_costs = NULL;
1971 /* This is called each time register related information is
1972 changed. */
1973 void
1974 ira_init_costs (void)
1976 int i;
1978 free_ira_costs ();
1979 max_struct_costs_size
1980 = sizeof (struct costs) + sizeof (int) * (ira_important_classes_num - 1);
1981 /* Don't use ira_allocate because vectors live through several IRA
1982 calls. */
1983 init_cost = (struct costs *) xmalloc (max_struct_costs_size);
1984 init_cost->mem_cost = 1000000;
1985 for (i = 0; i < ira_important_classes_num; i++)
1986 init_cost->cost[i] = 1000000;
1987 for (i = 0; i < MAX_RECOG_OPERANDS; i++)
1989 op_costs[i] = (struct costs *) xmalloc (max_struct_costs_size);
1990 this_op_costs[i] = (struct costs *) xmalloc (max_struct_costs_size);
1992 temp_costs = (struct costs *) xmalloc (max_struct_costs_size);
1995 /* Function called once at the end of compiler work. */
1996 void
1997 ira_finish_costs_once (void)
1999 free_ira_costs ();
2004 /* Common initialization function for ira_costs and
2005 ira_set_pseudo_classes. */
2006 static void
2007 init_costs (void)
2009 init_subregs_of_mode ();
2010 costs = (struct costs *) ira_allocate (max_struct_costs_size
2011 * cost_elements_num);
2012 pref_buffer = (enum reg_class *) ira_allocate (sizeof (enum reg_class)
2013 * cost_elements_num);
2014 regno_aclass = (enum reg_class *) ira_allocate (sizeof (enum reg_class)
2015 * max_reg_num ());
2016 regno_equiv_gains = (int *) ira_allocate (sizeof (int) * max_reg_num ());
2017 memset (regno_equiv_gains, 0, sizeof (int) * max_reg_num ());
2020 /* Common finalization function for ira_costs and
2021 ira_set_pseudo_classes. */
2022 static void
2023 finish_costs (void)
2025 finish_subregs_of_mode ();
2026 ira_free (regno_equiv_gains);
2027 ira_free (regno_aclass);
2028 ira_free (pref_buffer);
2029 ira_free (costs);
2032 /* Entry function which defines register class, memory and hard
2033 register costs for each allocno. */
2034 void
2035 ira_costs (void)
2037 allocno_p = true;
2038 cost_elements_num = ira_allocnos_num;
2039 init_costs ();
2040 total_allocno_costs = (struct costs *) ira_allocate (max_struct_costs_size
2041 * ira_allocnos_num);
2042 initiate_regno_cost_classes ();
2043 calculate_elim_costs_all_insns ();
2044 find_costs_and_classes (ira_dump_file);
2045 setup_allocno_class_and_costs ();
2046 finish_regno_cost_classes ();
2047 finish_costs ();
2048 ira_free (total_allocno_costs);
2051 /* Entry function which defines classes for pseudos. */
2052 void
2053 ira_set_pseudo_classes (FILE *dump_file)
2055 allocno_p = false;
2056 internal_flag_ira_verbose = flag_ira_verbose;
2057 cost_elements_num = max_reg_num ();
2058 init_costs ();
2059 initiate_regno_cost_classes ();
2060 find_costs_and_classes (dump_file);
2061 finish_regno_cost_classes ();
2062 pseudo_classes_defined_p = true;
2063 finish_costs ();
2068 /* Change hard register costs for allocnos which lives through
2069 function calls. This is called only when we found all intersected
2070 calls during building allocno live ranges. */
2071 void
2072 ira_tune_allocno_costs (void)
2074 int j, n, regno;
2075 int cost, min_cost, *reg_costs;
2076 enum reg_class aclass, rclass;
2077 enum machine_mode mode;
2078 ira_allocno_t a;
2079 ira_allocno_iterator ai;
2080 ira_allocno_object_iterator oi;
2081 ira_object_t obj;
2082 bool skip_p;
2084 FOR_EACH_ALLOCNO (a, ai)
2086 aclass = ALLOCNO_CLASS (a);
2087 if (aclass == NO_REGS)
2088 continue;
2089 mode = ALLOCNO_MODE (a);
2090 n = ira_class_hard_regs_num[aclass];
2091 min_cost = INT_MAX;
2092 if (ALLOCNO_CALLS_CROSSED_NUM (a)
2093 != ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a))
2095 ira_allocate_and_set_costs
2096 (&ALLOCNO_HARD_REG_COSTS (a), aclass,
2097 ALLOCNO_CLASS_COST (a));
2098 reg_costs = ALLOCNO_HARD_REG_COSTS (a);
2099 for (j = n - 1; j >= 0; j--)
2101 regno = ira_class_hard_regs[aclass][j];
2102 skip_p = false;
2103 FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
2105 if (ira_hard_reg_set_intersection_p (regno, mode,
2106 OBJECT_CONFLICT_HARD_REGS
2107 (obj)))
2109 skip_p = true;
2110 break;
2113 if (skip_p)
2114 continue;
2115 rclass = REGNO_REG_CLASS (regno);
2116 cost = 0;
2117 if (ira_hard_reg_set_intersection_p (regno, mode, call_used_reg_set)
2118 || HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
2119 cost += (ALLOCNO_CALL_FREQ (a)
2120 * (ira_memory_move_cost[mode][rclass][0]
2121 + ira_memory_move_cost[mode][rclass][1]));
2122 #ifdef IRA_HARD_REGNO_ADD_COST_MULTIPLIER
2123 cost += ((ira_memory_move_cost[mode][rclass][0]
2124 + ira_memory_move_cost[mode][rclass][1])
2125 * ALLOCNO_FREQ (a)
2126 * IRA_HARD_REGNO_ADD_COST_MULTIPLIER (regno) / 2);
2127 #endif
2128 if (INT_MAX - cost < reg_costs[j])
2129 reg_costs[j] = INT_MAX;
2130 else
2131 reg_costs[j] += cost;
2132 if (min_cost > reg_costs[j])
2133 min_cost = reg_costs[j];
2136 if (min_cost != INT_MAX)
2137 ALLOCNO_CLASS_COST (a) = min_cost;
2139 /* Some targets allow pseudos to be allocated to unaligned sequences
2140 of hard registers. However, selecting an unaligned sequence can
2141 unnecessarily restrict later allocations. So increase the cost of
2142 unaligned hard regs to encourage the use of aligned hard regs. */
2144 const int nregs = ira_reg_class_max_nregs[aclass][ALLOCNO_MODE (a)];
2146 if (nregs > 1)
2148 ira_allocate_and_set_costs
2149 (&ALLOCNO_HARD_REG_COSTS (a), aclass, ALLOCNO_CLASS_COST (a));
2150 reg_costs = ALLOCNO_HARD_REG_COSTS (a);
2151 for (j = n - 1; j >= 0; j--)
2153 regno = ira_non_ordered_class_hard_regs[aclass][j];
2154 if ((regno % nregs) != 0)
2156 int index = ira_class_hard_reg_index[aclass][regno];
2157 ira_assert (index != -1);
2158 reg_costs[index] += ALLOCNO_FREQ (a);
2166 /* Add COST to the estimated gain for eliminating REGNO with its
2167 equivalence. If COST is zero, record that no such elimination is
2168 possible. */
2170 void
2171 ira_adjust_equiv_reg_cost (unsigned regno, int cost)
2173 if (cost == 0)
2174 regno_equiv_gains[regno] = 0;
2175 else
2176 regno_equiv_gains[regno] += cost;