1 /* IRA hard register and memory cost calculation for allocnos or pseudos.
2 Copyright (C) 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
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
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/>. */
24 #include "coretypes.h"
26 #include "hard-reg-set.h"
31 #include "basic-block.h"
33 #include "addresses.h"
34 #include "insn-config.h"
41 /* The flags is set up every time when we calculate pseudo register
42 classes through function ira_set_pseudo_classes. */
43 static bool pseudo_classes_defined_p
= false;
45 /* TRUE if we work with allocnos. Otherwise we work with pseudos. */
46 static bool allocno_p
;
48 /* Number of elements in arrays `in_inc_dec' and `costs'. */
49 static int cost_elements_num
;
51 #ifdef FORBIDDEN_INC_DEC_CLASSES
52 /* Indexed by n, is TRUE if allocno or pseudo with number N is used in
53 an auto-inc or auto-dec context. */
54 static bool *in_inc_dec
;
57 /* The `costs' struct records the cost of using hard registers of each
58 class considered for the calculation and of using memory for each
63 /* Costs for register classes start here. We process only some
64 register classes (cover classes on the 1st cost calculation
65 iteration and important classes on the 2nd iteration). */
69 /* Initialized once. It is a maximal possible size of the allocated
71 static int max_struct_costs_size
;
73 /* Allocated and initialized once, and used to initialize cost values
75 static struct costs
*init_cost
;
77 /* Allocated once, and used for temporary purposes. */
78 static struct costs
*temp_costs
;
80 /* Allocated once, and used for the cost calculation. */
81 static struct costs
*op_costs
[MAX_RECOG_OPERANDS
];
82 static struct costs
*this_op_costs
[MAX_RECOG_OPERANDS
];
84 /* Costs of each class for each allocno or pseudo. */
85 static struct costs
*costs
;
87 /* Accumulated costs of each class for each allocno. */
88 static struct costs
*total_allocno_costs
;
90 /* Classes used for cost calculation. They may be different on
91 different iterations of the cost calculations or in different
92 optimization modes. */
93 static enum reg_class
*cost_classes
;
95 /* The size of the previous array. */
96 static int cost_classes_num
;
98 /* Map: cost class -> order number (they start with 0) of the cost
99 class. The order number is negative for non-cost classes. */
100 static int cost_class_nums
[N_REG_CLASSES
];
102 /* It is the current size of struct costs. */
103 static int struct_costs_size
;
105 /* Return pointer to structure containing costs of allocno or pseudo
106 with given NUM in array ARR. */
107 #define COSTS(arr, num) \
108 ((struct costs *) ((char *) (arr) + (num) * struct_costs_size))
110 /* Return index in COSTS when processing reg with REGNO. */
111 #define COST_INDEX(regno) (allocno_p \
112 ? ALLOCNO_NUM (ira_curr_regno_allocno_map[regno]) \
115 /* Record register class preferences of each allocno or pseudo. Null
116 value means no preferences. It happens on the 1st iteration of the
118 static enum reg_class
*pref
;
120 /* Allocated buffers for pref. */
121 static enum reg_class
*pref_buffer
;
123 /* Record cover register class of each allocno with the same regno. */
124 static enum reg_class
*regno_cover_class
;
126 /* Execution frequency of the current insn. */
127 static int frequency
;
131 /* Compute the cost of loading X into (if TO_P is TRUE) or from (if
132 TO_P is FALSE) a register of class RCLASS in mode MODE. X must not
133 be a pseudo register. */
135 copy_cost (rtx x
, enum machine_mode mode
, enum reg_class rclass
, bool to_p
,
136 secondary_reload_info
*prev_sri
)
138 secondary_reload_info sri
;
139 enum reg_class secondary_class
= NO_REGS
;
141 /* If X is a SCRATCH, there is actually nothing to move since we are
142 assuming optimal allocation. */
143 if (GET_CODE (x
) == SCRATCH
)
146 /* Get the class we will actually use for a reload. */
147 rclass
= PREFERRED_RELOAD_CLASS (x
, rclass
);
149 /* If we need a secondary reload for an intermediate, the cost is
150 that to load the input into the intermediate register, then to
152 sri
.prev_sri
= prev_sri
;
154 secondary_class
= targetm
.secondary_reload (to_p
, x
, rclass
, mode
, &sri
);
156 if (secondary_class
!= NO_REGS
)
158 if (!move_cost
[mode
])
159 init_move_cost (mode
);
160 return (move_cost
[mode
][secondary_class
][rclass
] + sri
.extra_cost
161 + copy_cost (x
, mode
, secondary_class
, to_p
, &sri
));
164 /* For memory, use the memory move cost, for (hard) registers, use
165 the cost to move between the register classes, and use 2 for
166 everything else (constants). */
167 if (MEM_P (x
) || rclass
== NO_REGS
)
168 return sri
.extra_cost
+ ira_memory_move_cost
[mode
][rclass
][to_p
!= 0];
171 if (!move_cost
[mode
])
172 init_move_cost (mode
);
173 return (sri
.extra_cost
+ move_cost
[mode
][REGNO_REG_CLASS (REGNO (x
))][rclass
]);
176 /* If this is a constant, we may eventually want to call rtx_cost
178 return sri
.extra_cost
+ COSTS_N_INSNS (1);
183 /* Record the cost of using memory or hard registers of various
184 classes for the operands in INSN.
186 N_ALTS is the number of alternatives.
187 N_OPS is the number of operands.
188 OPS is an array of the operands.
189 MODES are the modes of the operands, in case any are VOIDmode.
190 CONSTRAINTS are the constraints to use for the operands. This array
191 is modified by this procedure.
193 This procedure works alternative by alternative. For each
194 alternative we assume that we will be able to allocate all allocnos
195 to their ideal register class and calculate the cost of using that
196 alternative. Then we compute, for each operand that is a
197 pseudo-register, the cost of having the allocno allocated to each
198 register class and using it in that alternative. To this cost is
199 added the cost of the alternative.
201 The cost of each class for this insn is its lowest cost among all
204 record_reg_classes (int n_alts
, int n_ops
, rtx
*ops
,
205 enum machine_mode
*modes
, const char **constraints
,
206 rtx insn
, struct costs
**op_costs
,
207 enum reg_class
*pref
)
212 int insn_allows_mem
[MAX_RECOG_OPERANDS
];
214 for (i
= 0; i
< n_ops
; i
++)
215 insn_allows_mem
[i
] = 0;
217 /* Process each alternative, each time minimizing an operand's cost
218 with the cost for each operand in that alternative. */
219 for (alt
= 0; alt
< n_alts
; alt
++)
221 enum reg_class classes
[MAX_RECOG_OPERANDS
];
222 int allows_mem
[MAX_RECOG_OPERANDS
];
223 enum reg_class rclass
;
225 int alt_cost
= 0, op_cost_add
;
227 if (!recog_data
.alternative_enabled_p
[alt
])
229 for (i
= 0; i
< recog_data
.n_operands
; i
++)
230 constraints
[i
] = skip_alternative (constraints
[i
]);
235 for (i
= 0; i
< n_ops
; i
++)
238 const char *p
= constraints
[i
];
240 enum machine_mode mode
= modes
[i
];
244 /* Initially show we know nothing about the register class. */
245 classes
[i
] = NO_REGS
;
248 /* If this operand has no constraints at all, we can
249 conclude nothing about it since anything is valid. */
252 if (REG_P (op
) && REGNO (op
) >= FIRST_PSEUDO_REGISTER
)
253 memset (this_op_costs
[i
], 0, struct_costs_size
);
257 /* If this alternative is only relevant when this operand
258 matches a previous operand, we do different things
259 depending on whether this operand is a allocno-reg or not.
260 We must process any modifiers for the operand before we
261 can make this test. */
262 while (*p
== '%' || *p
== '=' || *p
== '+' || *p
== '&')
265 if (p
[0] >= '0' && p
[0] <= '0' + i
&& (p
[1] == ',' || p
[1] == 0))
267 /* Copy class and whether memory is allowed from the
268 matching alternative. Then perform any needed cost
269 computations and/or adjustments. */
271 classes
[i
] = classes
[j
];
272 allows_mem
[i
] = allows_mem
[j
];
274 insn_allows_mem
[i
] = 1;
276 if (! REG_P (op
) || REGNO (op
) < FIRST_PSEUDO_REGISTER
)
278 /* If this matches the other operand, we have no
279 added cost and we win. */
280 if (rtx_equal_p (ops
[j
], op
))
282 /* If we can put the other operand into a register,
283 add to the cost of this alternative the cost to
284 copy this operand to the register used for the
286 else if (classes
[j
] != NO_REGS
)
288 alt_cost
+= copy_cost (op
, mode
, classes
[j
], 1, NULL
);
292 else if (! REG_P (ops
[j
])
293 || REGNO (ops
[j
]) < FIRST_PSEUDO_REGISTER
)
295 /* This op is an allocno but the one it matches is
298 /* If we can't put the other operand into a
299 register, this alternative can't be used. */
301 if (classes
[j
] == NO_REGS
)
303 /* Otherwise, add to the cost of this alternative
304 the cost to copy the other operand to the hard
305 register used for this operand. */
307 alt_cost
+= copy_cost (ops
[j
], mode
, classes
[j
], 1, NULL
);
311 /* The costs of this operand are not the same as the
312 other operand since move costs are not symmetric.
313 Moreover, if we cannot tie them, this alternative
314 needs to do a copy, which is one insn. */
315 struct costs
*pp
= this_op_costs
[i
];
317 for (k
= 0; k
< cost_classes_num
; k
++)
319 rclass
= cost_classes
[k
];
321 = (((recog_data
.operand_type
[i
] != OP_OUT
322 ? ira_get_may_move_cost (mode
, rclass
,
323 classes
[i
], true) : 0)
324 + (recog_data
.operand_type
[i
] != OP_IN
325 ? ira_get_may_move_cost (mode
, classes
[i
],
330 /* If the alternative actually allows memory, make
331 things a bit cheaper since we won't need an extra
334 = ((recog_data
.operand_type
[i
] != OP_IN
335 ? ira_memory_move_cost
[mode
][classes
[i
]][0] : 0)
336 + (recog_data
.operand_type
[i
] != OP_OUT
337 ? ira_memory_move_cost
[mode
][classes
[i
]][1] : 0)
338 - allows_mem
[i
]) * frequency
;
340 /* If we have assigned a class to this allocno in our
341 first pass, add a cost to this alternative
342 corresponding to what we would add if this allocno
343 were not in the appropriate class. We could use
344 cover class here but it is less accurate
348 enum reg_class pref_class
= pref
[COST_INDEX (REGNO (op
))];
350 if (pref_class
== NO_REGS
)
352 += ((recog_data
.operand_type
[i
] != OP_IN
353 ? ira_memory_move_cost
[mode
][classes
[i
]][0]
355 + (recog_data
.operand_type
[i
] != OP_OUT
356 ? ira_memory_move_cost
[mode
][classes
[i
]][1]
358 else if (ira_reg_class_intersect
359 [pref_class
][classes
[i
]] == NO_REGS
)
360 alt_cost
+= ira_get_register_move_cost (mode
,
364 if (REGNO (ops
[i
]) != REGNO (ops
[j
])
365 && ! find_reg_note (insn
, REG_DEAD
, op
))
368 /* This is in place of ordinary cost computation for
369 this operand, so skip to the end of the
370 alternative (should be just one character). */
371 while (*p
&& *p
++ != ',')
379 /* Scan all the constraint letters. See if the operand
380 matches any of the constraints. Collect the valid
381 register classes and see if this operand accepts
390 /* Ignore the next letter for this pass. */
396 case '!': case '#': case '&':
397 case '0': case '1': case '2': case '3': case '4':
398 case '5': case '6': case '7': case '8': case '9':
403 win
= address_operand (op
, GET_MODE (op
));
404 /* We know this operand is an address, so we want it
405 to be allocated to a register that can be the
406 base of an address, i.e. BASE_REG_CLASS. */
408 = ira_reg_class_union
[classes
[i
]]
409 [base_reg_class (VOIDmode
, ADDRESS
, SCRATCH
)];
412 case 'm': case 'o': case 'V':
413 /* It doesn't seem worth distinguishing between
414 offsettable and non-offsettable addresses
416 insn_allows_mem
[i
] = allows_mem
[i
] = 1;
423 && (GET_CODE (XEXP (op
, 0)) == PRE_DEC
424 || GET_CODE (XEXP (op
, 0)) == POST_DEC
))
430 && (GET_CODE (XEXP (op
, 0)) == PRE_INC
431 || GET_CODE (XEXP (op
, 0)) == POST_INC
))
437 if (GET_CODE (op
) == CONST_DOUBLE
438 || (GET_CODE (op
) == CONST_VECTOR
439 && (GET_MODE_CLASS (GET_MODE (op
))
440 == MODE_VECTOR_FLOAT
)))
446 if (GET_CODE (op
) == CONST_DOUBLE
447 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op
, c
, p
))
453 || (GET_CODE (op
) == CONST_DOUBLE
454 && GET_MODE (op
) == VOIDmode
))
459 && (! flag_pic
|| LEGITIMATE_PIC_OPERAND_P (op
)))
465 || (GET_CODE (op
) == CONST_DOUBLE
466 && GET_MODE (op
) == VOIDmode
))
479 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op
), c
, p
))
490 && (! flag_pic
|| LEGITIMATE_PIC_OPERAND_P (op
))))
492 insn_allows_mem
[i
] = allows_mem
[i
] = 1;
494 classes
[i
] = ira_reg_class_union
[classes
[i
]][GENERAL_REGS
];
498 if (REG_CLASS_FROM_CONSTRAINT (c
, p
) != NO_REGS
)
499 classes
[i
] = ira_reg_class_union
[classes
[i
]]
500 [REG_CLASS_FROM_CONSTRAINT (c
, p
)];
501 #ifdef EXTRA_CONSTRAINT_STR
502 else if (EXTRA_CONSTRAINT_STR (op
, c
, p
))
505 if (EXTRA_MEMORY_CONSTRAINT (c
, p
))
507 /* Every MEM can be reloaded to fit. */
508 insn_allows_mem
[i
] = allows_mem
[i
] = 1;
512 if (EXTRA_ADDRESS_CONSTRAINT (c
, p
))
514 /* Every address can be reloaded to fit. */
516 if (address_operand (op
, GET_MODE (op
)))
518 /* We know this operand is an address, so we
519 want it to be allocated to a hard register
520 that can be the base of an address,
521 i.e. BASE_REG_CLASS. */
523 = ira_reg_class_union
[classes
[i
]]
524 [base_reg_class (VOIDmode
, ADDRESS
, SCRATCH
)];
529 p
+= CONSTRAINT_LEN (c
, p
);
536 /* How we account for this operand now depends on whether it
537 is a pseudo register or not. If it is, we first check if
538 any register classes are valid. If not, we ignore this
539 alternative, since we want to assume that all allocnos get
540 allocated for register preferencing. If some register
541 class is valid, compute the costs of moving the allocno
543 if (REG_P (op
) && REGNO (op
) >= FIRST_PSEUDO_REGISTER
)
545 if (classes
[i
] == NO_REGS
)
547 /* We must always fail if the operand is a REG, but
548 we did not find a suitable class.
550 Otherwise we may perform an uninitialized read
551 from this_op_costs after the `continue' statement
557 struct costs
*pp
= this_op_costs
[i
];
559 for (k
= 0; k
< cost_classes_num
; k
++)
561 rclass
= cost_classes
[k
];
563 = (((recog_data
.operand_type
[i
] != OP_OUT
564 ? ira_get_may_move_cost (mode
, rclass
,
565 classes
[i
], true) : 0)
566 + (recog_data
.operand_type
[i
] != OP_IN
567 ? ira_get_may_move_cost (mode
, classes
[i
],
572 /* If the alternative actually allows memory, make
573 things a bit cheaper since we won't need an extra
576 = ((recog_data
.operand_type
[i
] != OP_IN
577 ? ira_memory_move_cost
[mode
][classes
[i
]][0] : 0)
578 + (recog_data
.operand_type
[i
] != OP_OUT
579 ? ira_memory_move_cost
[mode
][classes
[i
]][1] : 0)
580 - allows_mem
[i
]) * frequency
;
581 /* If we have assigned a class to this allocno in our
582 first pass, add a cost to this alternative
583 corresponding to what we would add if this allocno
584 were not in the appropriate class. We could use
585 cover class here but it is less accurate
589 enum reg_class pref_class
= pref
[COST_INDEX (REGNO (op
))];
591 if (pref_class
== NO_REGS
)
593 += ((recog_data
.operand_type
[i
] != OP_IN
594 ? ira_memory_move_cost
[mode
][classes
[i
]][0]
596 + (recog_data
.operand_type
[i
] != OP_OUT
597 ? ira_memory_move_cost
[mode
][classes
[i
]][1]
599 else if (ira_reg_class_intersect
[pref_class
][classes
[i
]]
601 alt_cost
+= ira_get_register_move_cost (mode
,
608 /* Otherwise, if this alternative wins, either because we
609 have already determined that or if we have a hard
610 register of the proper class, there is no cost for this
612 else if (win
|| (REG_P (op
)
613 && reg_fits_class_p (op
, classes
[i
],
617 /* If registers are valid, the cost of this alternative
618 includes copying the object to and/or from a
620 else if (classes
[i
] != NO_REGS
)
622 if (recog_data
.operand_type
[i
] != OP_OUT
)
623 alt_cost
+= copy_cost (op
, mode
, classes
[i
], 1, NULL
);
625 if (recog_data
.operand_type
[i
] != OP_IN
)
626 alt_cost
+= copy_cost (op
, mode
, classes
[i
], 0, NULL
);
628 /* The only other way this alternative can be used is if
629 this is a constant that could be placed into memory. */
630 else if (CONSTANT_P (op
) && (allows_addr
|| allows_mem
[i
]))
631 alt_cost
+= ira_memory_move_cost
[mode
][classes
[i
]][1];
639 op_cost_add
= alt_cost
* frequency
;
640 /* Finally, update the costs with the information we've
641 calculated about this alternative. */
642 for (i
= 0; i
< n_ops
; i
++)
643 if (REG_P (ops
[i
]) && REGNO (ops
[i
]) >= FIRST_PSEUDO_REGISTER
)
645 struct costs
*pp
= op_costs
[i
], *qq
= this_op_costs
[i
];
646 int scale
= 1 + (recog_data
.operand_type
[i
] == OP_INOUT
);
648 pp
->mem_cost
= MIN (pp
->mem_cost
,
649 (qq
->mem_cost
+ op_cost_add
) * scale
);
651 for (k
= 0; k
< cost_classes_num
; k
++)
653 = MIN (pp
->cost
[k
], (qq
->cost
[k
] + op_cost_add
) * scale
);
658 for (i
= 0; i
< n_ops
; i
++)
663 if (! REG_P (op
) || REGNO (op
) < FIRST_PSEUDO_REGISTER
)
665 a
= ira_curr_regno_allocno_map
[REGNO (op
)];
666 if (! ALLOCNO_BAD_SPILL_P (a
) && insn_allows_mem
[i
] == 0)
667 ALLOCNO_BAD_SPILL_P (a
) = true;
670 /* If this insn is a single set copying operand 1 to operand 0 and
671 one operand is an allocno with the other a hard reg or an allocno
672 that prefers a hard register that is in its own register class
673 then we may want to adjust the cost of that register class to -1.
675 Avoid the adjustment if the source does not die to avoid
676 stressing of register allocator by preferrencing two colliding
677 registers into single class.
679 Also avoid the adjustment if a copy between hard registers of the
680 class is expensive (ten times the cost of a default copy is
681 considered arbitrarily expensive). This avoids losing when the
682 preferred class is very expensive as the source of a copy
684 if ((set
= single_set (insn
)) != 0
685 && ops
[0] == SET_DEST (set
) && ops
[1] == SET_SRC (set
)
686 && REG_P (ops
[0]) && REG_P (ops
[1])
687 && find_regno_note (insn
, REG_DEAD
, REGNO (ops
[1])))
688 for (i
= 0; i
<= 1; i
++)
689 if (REGNO (ops
[i
]) >= FIRST_PSEUDO_REGISTER
)
691 unsigned int regno
= REGNO (ops
[!i
]);
692 enum machine_mode mode
= GET_MODE (ops
[!i
]);
693 enum reg_class rclass
;
696 if (regno
< FIRST_PSEUDO_REGISTER
)
697 for (k
= 0; k
< cost_classes_num
; k
++)
699 rclass
= cost_classes
[k
];
700 if (TEST_HARD_REG_BIT (reg_class_contents
[rclass
], regno
)
701 && (reg_class_size
[rclass
]
702 == (unsigned) CLASS_MAX_NREGS (rclass
, mode
)))
704 if (reg_class_size
[rclass
] == 1)
705 op_costs
[i
]->cost
[k
] = -frequency
;
709 nr
< (unsigned) hard_regno_nregs
[regno
][mode
];
711 if (! TEST_HARD_REG_BIT (reg_class_contents
[rclass
],
715 if (nr
== (unsigned) hard_regno_nregs
[regno
][mode
])
716 op_costs
[i
]->cost
[k
] = -frequency
;
725 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudo registers. */
727 ok_for_index_p_nonstrict (rtx reg
)
729 unsigned regno
= REGNO (reg
);
731 return regno
>= FIRST_PSEUDO_REGISTER
|| REGNO_OK_FOR_INDEX_P (regno
);
734 /* A version of regno_ok_for_base_p for use here, when all
735 pseudo-registers should count as OK. Arguments as for
736 regno_ok_for_base_p. */
738 ok_for_base_p_nonstrict (rtx reg
, enum machine_mode mode
,
739 enum rtx_code outer_code
, enum rtx_code index_code
)
741 unsigned regno
= REGNO (reg
);
743 if (regno
>= FIRST_PSEUDO_REGISTER
)
745 return ok_for_base_p_1 (regno
, mode
, outer_code
, index_code
);
748 /* Record the pseudo registers we must reload into hard registers in a
749 subexpression of a memory address, X.
751 If CONTEXT is 0, we are looking at the base part of an address,
752 otherwise we are looking at the index part.
754 MODE is the mode of the memory reference; OUTER_CODE and INDEX_CODE
755 give the context that the rtx appears in. These three arguments
756 are passed down to base_reg_class.
758 SCALE is twice the amount to multiply the cost by (it is twice so
759 we can represent half-cost adjustments). */
761 record_address_regs (enum machine_mode mode
, rtx x
, int context
,
762 enum rtx_code outer_code
, enum rtx_code index_code
,
765 enum rtx_code code
= GET_CODE (x
);
766 enum reg_class rclass
;
769 rclass
= INDEX_REG_CLASS
;
771 rclass
= base_reg_class (mode
, outer_code
, index_code
);
784 /* When we have an address that is a sum, we must determine
785 whether registers are "base" or "index" regs. If there is a
786 sum of two registers, we must choose one to be the "base".
787 Luckily, we can use the REG_POINTER to make a good choice
788 most of the time. We only need to do this on machines that
789 can have two registers in an address and where the base and
790 index register classes are different.
792 ??? This code used to set REGNO_POINTER_FLAG in some cases,
793 but that seems bogus since it should only be set when we are
794 sure the register is being used as a pointer. */
796 rtx arg0
= XEXP (x
, 0);
797 rtx arg1
= XEXP (x
, 1);
798 enum rtx_code code0
= GET_CODE (arg0
);
799 enum rtx_code code1
= GET_CODE (arg1
);
801 /* Look inside subregs. */
803 arg0
= SUBREG_REG (arg0
), code0
= GET_CODE (arg0
);
805 arg1
= SUBREG_REG (arg1
), code1
= GET_CODE (arg1
);
807 /* If this machine only allows one register per address, it
808 must be in the first operand. */
809 if (MAX_REGS_PER_ADDRESS
== 1)
810 record_address_regs (mode
, arg0
, 0, PLUS
, code1
, scale
);
812 /* If index and base registers are the same on this machine,
813 just record registers in any non-constant operands. We
814 assume here, as well as in the tests below, that all
815 addresses are in canonical form. */
816 else if (INDEX_REG_CLASS
== base_reg_class (VOIDmode
, PLUS
, SCRATCH
))
818 record_address_regs (mode
, arg0
, context
, PLUS
, code1
, scale
);
819 if (! CONSTANT_P (arg1
))
820 record_address_regs (mode
, arg1
, context
, PLUS
, code0
, scale
);
823 /* If the second operand is a constant integer, it doesn't
824 change what class the first operand must be. */
825 else if (code1
== CONST_INT
|| code1
== CONST_DOUBLE
)
826 record_address_regs (mode
, arg0
, context
, PLUS
, code1
, scale
);
827 /* If the second operand is a symbolic constant, the first
828 operand must be an index register. */
829 else if (code1
== SYMBOL_REF
|| code1
== CONST
|| code1
== LABEL_REF
)
830 record_address_regs (mode
, arg0
, 1, PLUS
, code1
, scale
);
831 /* If both operands are registers but one is already a hard
832 register of index or reg-base class, give the other the
833 class that the hard register is not. */
834 else if (code0
== REG
&& code1
== REG
835 && REGNO (arg0
) < FIRST_PSEUDO_REGISTER
836 && (ok_for_base_p_nonstrict (arg0
, mode
, PLUS
, REG
)
837 || ok_for_index_p_nonstrict (arg0
)))
838 record_address_regs (mode
, arg1
,
839 ok_for_base_p_nonstrict (arg0
, mode
, PLUS
, REG
)
842 else if (code0
== REG
&& code1
== REG
843 && REGNO (arg1
) < FIRST_PSEUDO_REGISTER
844 && (ok_for_base_p_nonstrict (arg1
, mode
, PLUS
, REG
)
845 || ok_for_index_p_nonstrict (arg1
)))
846 record_address_regs (mode
, arg0
,
847 ok_for_base_p_nonstrict (arg1
, mode
, PLUS
, REG
)
850 /* If one operand is known to be a pointer, it must be the
851 base with the other operand the index. Likewise if the
852 other operand is a MULT. */
853 else if ((code0
== REG
&& REG_POINTER (arg0
)) || code1
== MULT
)
855 record_address_regs (mode
, arg0
, 0, PLUS
, code1
, scale
);
856 record_address_regs (mode
, arg1
, 1, PLUS
, code0
, scale
);
858 else if ((code1
== REG
&& REG_POINTER (arg1
)) || code0
== MULT
)
860 record_address_regs (mode
, arg0
, 1, PLUS
, code1
, scale
);
861 record_address_regs (mode
, arg1
, 0, PLUS
, code0
, scale
);
863 /* Otherwise, count equal chances that each might be a base or
864 index register. This case should be rare. */
867 record_address_regs (mode
, arg0
, 0, PLUS
, code1
, scale
/ 2);
868 record_address_regs (mode
, arg0
, 1, PLUS
, code1
, scale
/ 2);
869 record_address_regs (mode
, arg1
, 0, PLUS
, code0
, scale
/ 2);
870 record_address_regs (mode
, arg1
, 1, PLUS
, code0
, scale
/ 2);
875 /* Double the importance of an allocno that is incremented or
876 decremented, since it would take two extra insns if it ends
877 up in the wrong place. */
880 record_address_regs (mode
, XEXP (x
, 0), 0, code
,
881 GET_CODE (XEXP (XEXP (x
, 1), 1)), 2 * scale
);
882 if (REG_P (XEXP (XEXP (x
, 1), 1)))
883 record_address_regs (mode
, XEXP (XEXP (x
, 1), 1), 1, code
, REG
,
891 /* Double the importance of an allocno that is incremented or
892 decremented, since it would take two extra insns if it ends
893 up in the wrong place. If the operand is a pseudo-register,
894 show it is being used in an INC_DEC context. */
895 #ifdef FORBIDDEN_INC_DEC_CLASSES
896 if (REG_P (XEXP (x
, 0))
897 && REGNO (XEXP (x
, 0)) >= FIRST_PSEUDO_REGISTER
)
898 in_inc_dec
[COST_INDEX (REGNO (XEXP (x
, 0)))] = true;
900 record_address_regs (mode
, XEXP (x
, 0), 0, code
, SCRATCH
, 2 * scale
);
909 if (REGNO (x
) < FIRST_PSEUDO_REGISTER
)
913 ALLOCNO_BAD_SPILL_P (ira_curr_regno_allocno_map
[REGNO (x
)]) = true;
914 pp
= COSTS (costs
, COST_INDEX (REGNO (x
)));
915 pp
->mem_cost
+= (ira_memory_move_cost
[Pmode
][rclass
][1] * scale
) / 2;
916 for (k
= 0; k
< cost_classes_num
; k
++)
920 += (ira_get_may_move_cost (Pmode
, i
, rclass
, true) * scale
) / 2;
927 const char *fmt
= GET_RTX_FORMAT (code
);
929 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
931 record_address_regs (mode
, XEXP (x
, i
), context
, code
, SCRATCH
,
939 /* Calculate the costs of insn operands. */
941 record_operand_costs (rtx insn
, struct costs
**op_costs
, enum reg_class
*pref
)
943 const char *constraints
[MAX_RECOG_OPERANDS
];
944 enum machine_mode modes
[MAX_RECOG_OPERANDS
];
947 for (i
= 0; i
< recog_data
.n_operands
; i
++)
949 constraints
[i
] = recog_data
.constraints
[i
];
950 modes
[i
] = recog_data
.operand_mode
[i
];
953 /* If we get here, we are set up to record the costs of all the
954 operands for this insn. Start by initializing the costs. Then
955 handle any address registers. Finally record the desired classes
956 for any allocnos, doing it twice if some pair of operands are
958 for (i
= 0; i
< recog_data
.n_operands
; i
++)
960 memcpy (op_costs
[i
], init_cost
, struct_costs_size
);
962 if (GET_CODE (recog_data
.operand
[i
]) == SUBREG
)
963 recog_data
.operand
[i
] = SUBREG_REG (recog_data
.operand
[i
]);
965 if (MEM_P (recog_data
.operand
[i
]))
966 record_address_regs (GET_MODE (recog_data
.operand
[i
]),
967 XEXP (recog_data
.operand
[i
], 0),
968 0, MEM
, SCRATCH
, frequency
* 2);
969 else if (constraints
[i
][0] == 'p'
970 || EXTRA_ADDRESS_CONSTRAINT (constraints
[i
][0],
972 record_address_regs (VOIDmode
, recog_data
.operand
[i
], 0, ADDRESS
,
973 SCRATCH
, frequency
* 2);
976 /* Check for commutative in a separate loop so everything will have
977 been initialized. We must do this even if one operand is a
978 constant--see addsi3 in m68k.md. */
979 for (i
= 0; i
< (int) recog_data
.n_operands
- 1; i
++)
980 if (constraints
[i
][0] == '%')
982 const char *xconstraints
[MAX_RECOG_OPERANDS
];
985 /* Handle commutative operands by swapping the constraints.
986 We assume the modes are the same. */
987 for (j
= 0; j
< recog_data
.n_operands
; j
++)
988 xconstraints
[j
] = constraints
[j
];
990 xconstraints
[i
] = constraints
[i
+1];
991 xconstraints
[i
+1] = constraints
[i
];
992 record_reg_classes (recog_data
.n_alternatives
, recog_data
.n_operands
,
993 recog_data
.operand
, modes
,
994 xconstraints
, insn
, op_costs
, pref
);
996 record_reg_classes (recog_data
.n_alternatives
, recog_data
.n_operands
,
997 recog_data
.operand
, modes
,
998 constraints
, insn
, op_costs
, pref
);
1003 /* Process one insn INSN. Scan it and record each time it would save
1004 code to put a certain allocnos in a certain class. Return the last
1005 insn processed, so that the scan can be continued from there. */
1007 scan_one_insn (rtx insn
)
1009 enum rtx_code pat_code
;
1013 if (!NONDEBUG_INSN_P (insn
))
1016 pat_code
= GET_CODE (PATTERN (insn
));
1017 if (pat_code
== USE
|| pat_code
== CLOBBER
|| pat_code
== ASM_INPUT
1018 || pat_code
== ADDR_VEC
|| pat_code
== ADDR_DIFF_VEC
)
1021 set
= single_set (insn
);
1022 extract_insn (insn
);
1024 /* If this insn loads a parameter from its stack slot, then it
1025 represents a savings, rather than a cost, if the parameter is
1026 stored in memory. Record this fact. */
1027 if (set
!= 0 && REG_P (SET_DEST (set
)) && MEM_P (SET_SRC (set
))
1028 && (note
= find_reg_note (insn
, REG_EQUIV
, NULL_RTX
)) != NULL_RTX
1029 && MEM_P (XEXP (note
, 0)))
1031 enum reg_class cl
= GENERAL_REGS
;
1032 rtx reg
= SET_DEST (set
);
1033 int num
= COST_INDEX (REGNO (reg
));
1037 COSTS (costs
, num
)->mem_cost
1038 -= ira_memory_move_cost
[GET_MODE (reg
)][cl
][1] * frequency
;
1039 record_address_regs (GET_MODE (SET_SRC (set
)), XEXP (SET_SRC (set
), 0),
1040 0, MEM
, SCRATCH
, frequency
* 2);
1043 record_operand_costs (insn
, op_costs
, pref
);
1045 /* Now add the cost for each operand to the total costs for its
1047 for (i
= 0; i
< recog_data
.n_operands
; i
++)
1048 if (REG_P (recog_data
.operand
[i
])
1049 && REGNO (recog_data
.operand
[i
]) >= FIRST_PSEUDO_REGISTER
)
1051 int regno
= REGNO (recog_data
.operand
[i
]);
1052 struct costs
*p
= COSTS (costs
, COST_INDEX (regno
));
1053 struct costs
*q
= op_costs
[i
];
1055 p
->mem_cost
+= q
->mem_cost
;
1056 for (k
= 0; k
< cost_classes_num
; k
++)
1057 p
->cost
[k
] += q
->cost
[k
];
1065 /* Print allocnos costs to file F. */
1067 print_allocno_costs (FILE *f
)
1071 ira_allocno_iterator ai
;
1073 ira_assert (allocno_p
);
1075 FOR_EACH_ALLOCNO (a
, ai
)
1079 int regno
= ALLOCNO_REGNO (a
);
1081 i
= ALLOCNO_NUM (a
);
1082 fprintf (f
, " a%d(r%d,", i
, regno
);
1083 if ((bb
= ALLOCNO_LOOP_TREE_NODE (a
)->bb
) != NULL
)
1084 fprintf (f
, "b%d", bb
->index
);
1086 fprintf (f
, "l%d", ALLOCNO_LOOP_TREE_NODE (a
)->loop
->num
);
1087 fprintf (f
, ") costs:");
1088 for (k
= 0; k
< cost_classes_num
; k
++)
1090 rclass
= cost_classes
[k
];
1091 if (contains_reg_of_mode
[rclass
][PSEUDO_REGNO_MODE (regno
)]
1092 #ifdef FORBIDDEN_INC_DEC_CLASSES
1093 && (! in_inc_dec
[i
] || ! forbidden_inc_dec_class
[rclass
])
1095 #ifdef CANNOT_CHANGE_MODE_CLASS
1096 && ! invalid_mode_change_p (regno
, (enum reg_class
) rclass
,
1097 PSEUDO_REGNO_MODE (regno
))
1101 fprintf (f
, " %s:%d", reg_class_names
[rclass
],
1102 COSTS (costs
, i
)->cost
[k
]);
1103 if (flag_ira_region
== IRA_REGION_ALL
1104 || flag_ira_region
== IRA_REGION_MIXED
)
1105 fprintf (f
, ",%d", COSTS (total_allocno_costs
, i
)->cost
[k
]);
1108 fprintf (f
, " MEM:%i\n", COSTS (costs
, i
)->mem_cost
);
1112 /* Print pseudo costs to file F. */
1114 print_pseudo_costs (FILE *f
)
1119 ira_assert (! allocno_p
);
1121 for (regno
= max_reg_num () - 1; regno
>= FIRST_PSEUDO_REGISTER
; regno
--)
1123 if (regno_reg_rtx
[regno
] == NULL_RTX
)
1125 fprintf (f
, " r%d costs:", regno
);
1126 for (k
= 0; k
< cost_classes_num
; k
++)
1128 rclass
= cost_classes
[k
];
1129 if (contains_reg_of_mode
[rclass
][PSEUDO_REGNO_MODE (regno
)]
1130 #ifdef FORBIDDEN_INC_DEC_CLASSES
1131 && (! in_inc_dec
[regno
] || ! forbidden_inc_dec_class
[rclass
])
1133 #ifdef CANNOT_CHANGE_MODE_CLASS
1134 && ! invalid_mode_change_p (regno
, (enum reg_class
) rclass
,
1135 PSEUDO_REGNO_MODE (regno
))
1138 fprintf (f
, " %s:%d", reg_class_names
[rclass
],
1139 COSTS (costs
, regno
)->cost
[k
]);
1141 fprintf (f
, " MEM:%i\n", COSTS (costs
, regno
)->mem_cost
);
1145 /* Traverse the BB represented by LOOP_TREE_NODE to update the allocno
1148 process_bb_for_costs (basic_block bb
)
1152 frequency
= REG_FREQ_FROM_BB (bb
);
1155 FOR_BB_INSNS (bb
, insn
)
1156 insn
= scan_one_insn (insn
);
1159 /* Traverse the BB represented by LOOP_TREE_NODE to update the allocno
1162 process_bb_node_for_costs (ira_loop_tree_node_t loop_tree_node
)
1166 bb
= loop_tree_node
->bb
;
1168 process_bb_for_costs (bb
);
1171 /* Find costs of register classes and memory for allocnos or pseudos
1172 and their best costs. Set up preferred, alternative and cover
1173 classes for pseudos. */
1175 find_costs_and_classes (FILE *dump_file
)
1182 #ifdef FORBIDDEN_INC_DEC_CLASSES
1183 in_inc_dec
= ira_allocate (sizeof (bool) * cost_elements_num
);
1184 #endif /* FORBIDDEN_INC_DEC_CLASSES */
1187 if (!resize_reg_info () && allocno_p
&& pseudo_classes_defined_p
)
1190 ira_allocno_iterator ai
;
1193 FOR_EACH_ALLOCNO (a
, ai
)
1194 pref
[ALLOCNO_NUM (a
)] = reg_preferred_class (ALLOCNO_REGNO (a
));
1195 if (flag_expensive_optimizations
)
1199 /* Clear the flag for the next compiled function. */
1200 pseudo_classes_defined_p
= false;
1201 /* Normally we scan the insns once and determine the best class to
1202 use for each allocno. However, if -fexpensive-optimizations are
1203 on, we do so twice, the second time using the tentative best
1204 classes to guide the selection. */
1205 for (pass
= start
; pass
<= flag_expensive_optimizations
; pass
++)
1207 if ((!allocno_p
|| internal_flag_ira_verbose
> 0) && dump_file
)
1209 "\nPass %i for finding pseudo/allocno costs\n\n", pass
);
1210 /* We could use only cover classes. Unfortunately it does not
1211 work well for some targets where some subclass of cover class
1212 is costly and wrong cover class is chosen. */
1213 for (i
= 0; i
< N_REG_CLASSES
; i
++)
1214 cost_class_nums
[i
] = -1;
1215 for (cost_classes_num
= 0;
1216 cost_classes_num
< ira_important_classes_num
;
1219 cost_classes
[cost_classes_num
]
1220 = ira_important_classes
[cost_classes_num
];
1221 cost_class_nums
[cost_classes
[cost_classes_num
]]
1225 = sizeof (struct costs
) + sizeof (int) * (cost_classes_num
- 1);
1226 /* Zero out our accumulation of the cost of each class for each
1228 memset (costs
, 0, cost_elements_num
* struct_costs_size
);
1229 #ifdef FORBIDDEN_INC_DEC_CLASSES
1230 memset (in_inc_dec
, 0, cost_elements_num
* sizeof (bool));
1235 /* Scan the instructions and record each time it would save code
1236 to put a certain allocno in a certain class. */
1237 ira_traverse_loop_tree (true, ira_loop_tree_root
,
1238 process_bb_node_for_costs
, NULL
);
1240 memcpy (total_allocno_costs
, costs
,
1241 max_struct_costs_size
* ira_allocnos_num
);
1248 process_bb_for_costs (bb
);
1254 /* Now for each allocno look at how desirable each class is and
1255 find which class is preferred. */
1256 for (i
= max_reg_num () - 1; i
>= FIRST_PSEUDO_REGISTER
; i
--)
1258 ira_allocno_t a
, parent_a
;
1259 int rclass
, a_num
, parent_a_num
;
1260 ira_loop_tree_node_t parent
;
1261 int best_cost
, allocno_cost
;
1262 enum reg_class best
, alt_class
;
1263 #ifdef FORBIDDEN_INC_DEC_CLASSES
1264 int inc_dec_p
= false;
1269 if (regno_reg_rtx
[i
] == NULL_RTX
)
1271 #ifdef FORBIDDEN_INC_DEC_CLASSES
1272 inc_dec_p
= in_inc_dec
[i
];
1274 memcpy (temp_costs
, COSTS (costs
, i
), struct_costs_size
);
1278 if (ira_regno_allocno_map
[i
] == NULL
)
1280 memset (temp_costs
, 0, struct_costs_size
);
1281 /* Find cost of all allocnos with the same regno. */
1282 for (a
= ira_regno_allocno_map
[i
];
1284 a
= ALLOCNO_NEXT_REGNO_ALLOCNO (a
))
1286 a_num
= ALLOCNO_NUM (a
);
1287 if ((flag_ira_region
== IRA_REGION_ALL
1288 || flag_ira_region
== IRA_REGION_MIXED
)
1289 && (parent
= ALLOCNO_LOOP_TREE_NODE (a
)->parent
) != NULL
1290 && (parent_a
= parent
->regno_allocno_map
[i
]) != NULL
1291 /* There are no caps yet. */
1292 && bitmap_bit_p (ALLOCNO_LOOP_TREE_NODE
1293 (a
)->border_allocnos
,
1296 /* Propagate costs to upper levels in the region
1298 parent_a_num
= ALLOCNO_NUM (parent_a
);
1299 for (k
= 0; k
< cost_classes_num
; k
++)
1300 COSTS (total_allocno_costs
, parent_a_num
)->cost
[k
]
1301 += COSTS (total_allocno_costs
, a_num
)->cost
[k
];
1302 COSTS (total_allocno_costs
, parent_a_num
)->mem_cost
1303 += COSTS (total_allocno_costs
, a_num
)->mem_cost
;
1305 for (k
= 0; k
< cost_classes_num
; k
++)
1306 temp_costs
->cost
[k
] += COSTS (costs
, a_num
)->cost
[k
];
1307 temp_costs
->mem_cost
+= COSTS (costs
, a_num
)->mem_cost
;
1308 #ifdef FORBIDDEN_INC_DEC_CLASSES
1309 if (in_inc_dec
[a_num
])
1314 best_cost
= (1 << (HOST_BITS_PER_INT
- 2)) - 1;
1316 alt_class
= NO_REGS
;
1317 /* Find best common class for all allocnos with the same
1319 for (k
= 0; k
< cost_classes_num
; k
++)
1321 rclass
= cost_classes
[k
];
1322 /* Ignore classes that are too small for this operand or
1323 invalid for an operand that was auto-incremented. */
1324 if (! contains_reg_of_mode
[rclass
][PSEUDO_REGNO_MODE (i
)]
1325 #ifdef FORBIDDEN_INC_DEC_CLASSES
1326 || (inc_dec_p
&& forbidden_inc_dec_class
[rclass
])
1328 #ifdef CANNOT_CHANGE_MODE_CLASS
1329 || invalid_mode_change_p (i
, (enum reg_class
) rclass
,
1330 PSEUDO_REGNO_MODE (i
))
1334 if (temp_costs
->cost
[k
] < best_cost
)
1336 best_cost
= temp_costs
->cost
[k
];
1337 best
= (enum reg_class
) rclass
;
1339 else if (temp_costs
->cost
[k
] == best_cost
)
1340 best
= ira_reg_class_union
[best
][rclass
];
1341 if (pass
== flag_expensive_optimizations
1342 && temp_costs
->cost
[k
] < temp_costs
->mem_cost
1343 && (reg_class_size
[reg_class_subunion
[alt_class
][rclass
]]
1344 > reg_class_size
[alt_class
]))
1345 alt_class
= reg_class_subunion
[alt_class
][rclass
];
1347 alt_class
= ira_class_translate
[alt_class
];
1348 if (best_cost
> temp_costs
->mem_cost
)
1349 regno_cover_class
[i
] = NO_REGS
;
1350 else if (flag_ira_algorithm
== IRA_ALGORITHM_PRIORITY
)
1351 /* Make the common class the biggest class of best and
1353 regno_cover_class
[i
] = alt_class
== NO_REGS
? best
: alt_class
;
1355 /* Make the common class a cover class. Remember all
1356 allocnos with the same regno should have the same cover
1358 regno_cover_class
[i
] = ira_class_translate
[best
];
1359 if (pass
== flag_expensive_optimizations
)
1361 if (best_cost
> temp_costs
->mem_cost
)
1362 best
= alt_class
= NO_REGS
;
1363 else if (best
== alt_class
)
1364 alt_class
= NO_REGS
;
1365 setup_reg_classes (i
, best
, alt_class
, regno_cover_class
[i
]);
1366 if ((!allocno_p
|| internal_flag_ira_verbose
> 2)
1367 && dump_file
!= NULL
)
1369 " r%d: preferred %s, alternative %s, cover %s\n",
1370 i
, reg_class_names
[best
], reg_class_names
[alt_class
],
1371 reg_class_names
[regno_cover_class
[i
]]);
1375 pref
[i
] = best_cost
> temp_costs
->mem_cost
? NO_REGS
: best
;
1378 for (a
= ira_regno_allocno_map
[i
];
1380 a
= ALLOCNO_NEXT_REGNO_ALLOCNO (a
))
1382 a_num
= ALLOCNO_NUM (a
);
1383 if (regno_cover_class
[i
] == NO_REGS
)
1387 /* Finding best class which is subset of the common
1389 best_cost
= (1 << (HOST_BITS_PER_INT
- 2)) - 1;
1390 allocno_cost
= best_cost
;
1392 for (k
= 0; k
< cost_classes_num
; k
++)
1394 rclass
= cost_classes
[k
];
1395 if (! ira_class_subset_p
[rclass
][regno_cover_class
[i
]])
1397 /* Ignore classes that are too small for this
1398 operand or invalid for an operand that was
1399 auto-incremented. */
1400 if (! contains_reg_of_mode
[rclass
][PSEUDO_REGNO_MODE (i
)]
1401 #ifdef FORBIDDEN_INC_DEC_CLASSES
1402 || (inc_dec_p
&& forbidden_inc_dec_class
[rclass
])
1404 #ifdef CANNOT_CHANGE_MODE_CLASS
1405 || invalid_mode_change_p (i
, (enum reg_class
) rclass
,
1406 PSEUDO_REGNO_MODE (i
))
1410 else if (COSTS (total_allocno_costs
, a_num
)->cost
[k
]
1414 = COSTS (total_allocno_costs
, a_num
)->cost
[k
];
1415 allocno_cost
= COSTS (costs
, a_num
)->cost
[k
];
1416 best
= (enum reg_class
) rclass
;
1418 else if (COSTS (total_allocno_costs
, a_num
)->cost
[k
]
1421 best
= ira_reg_class_union
[best
][rclass
];
1423 = MAX (allocno_cost
, COSTS (costs
, a_num
)->cost
[k
]);
1426 ALLOCNO_COVER_CLASS_COST (a
) = allocno_cost
;
1428 ira_assert (flag_ira_algorithm
== IRA_ALGORITHM_PRIORITY
1429 || ira_class_translate
[best
] == regno_cover_class
[i
]);
1430 if (internal_flag_ira_verbose
> 2 && dump_file
!= NULL
1431 && (pass
== 0 || pref
[a_num
] != best
))
1433 fprintf (dump_file
, " a%d (r%d,", a_num
, i
);
1434 if ((bb
= ALLOCNO_LOOP_TREE_NODE (a
)->bb
) != NULL
)
1435 fprintf (dump_file
, "b%d", bb
->index
);
1437 fprintf (dump_file
, "l%d",
1438 ALLOCNO_LOOP_TREE_NODE (a
)->loop
->num
);
1439 fprintf (dump_file
, ") best %s, cover %s\n",
1440 reg_class_names
[best
],
1441 reg_class_names
[regno_cover_class
[i
]]);
1447 if (internal_flag_ira_verbose
> 4 && dump_file
)
1450 print_allocno_costs (dump_file
);
1452 print_pseudo_costs (dump_file
);
1453 fprintf (dump_file
,"\n");
1456 #ifdef FORBIDDEN_INC_DEC_CLASSES
1457 ira_free (in_inc_dec
);
1463 /* Process moves involving hard regs to modify allocno hard register
1464 costs. We can do this only after determining allocno cover class.
1465 If a hard register forms a register class, than moves with the hard
1466 register are already taken into account in class costs for the
1469 process_bb_node_for_hard_reg_moves (ira_loop_tree_node_t loop_tree_node
)
1471 int i
, freq
, cost
, src_regno
, dst_regno
, hard_regno
;
1474 enum reg_class rclass
, hard_reg_class
;
1475 enum machine_mode mode
;
1477 rtx insn
, set
, src
, dst
;
1479 bb
= loop_tree_node
->bb
;
1482 freq
= REG_FREQ_FROM_BB (bb
);
1485 FOR_BB_INSNS (bb
, insn
)
1487 if (!NONDEBUG_INSN_P (insn
))
1489 set
= single_set (insn
);
1490 if (set
== NULL_RTX
)
1492 dst
= SET_DEST (set
);
1493 src
= SET_SRC (set
);
1494 if (! REG_P (dst
) || ! REG_P (src
))
1496 dst_regno
= REGNO (dst
);
1497 src_regno
= REGNO (src
);
1498 if (dst_regno
>= FIRST_PSEUDO_REGISTER
1499 && src_regno
< FIRST_PSEUDO_REGISTER
)
1501 hard_regno
= src_regno
;
1503 a
= ira_curr_regno_allocno_map
[dst_regno
];
1505 else if (src_regno
>= FIRST_PSEUDO_REGISTER
1506 && dst_regno
< FIRST_PSEUDO_REGISTER
)
1508 hard_regno
= dst_regno
;
1510 a
= ira_curr_regno_allocno_map
[src_regno
];
1514 rclass
= ALLOCNO_COVER_CLASS (a
);
1515 if (! TEST_HARD_REG_BIT (reg_class_contents
[rclass
], hard_regno
))
1517 i
= ira_class_hard_reg_index
[rclass
][hard_regno
];
1520 mode
= ALLOCNO_MODE (a
);
1521 hard_reg_class
= REGNO_REG_CLASS (hard_regno
);
1523 = (to_p
? ira_get_register_move_cost (mode
, hard_reg_class
, rclass
)
1524 : ira_get_register_move_cost (mode
, rclass
, hard_reg_class
)) * freq
;
1525 ira_allocate_and_set_costs (&ALLOCNO_HARD_REG_COSTS (a
), rclass
,
1526 ALLOCNO_COVER_CLASS_COST (a
));
1527 ira_allocate_and_set_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a
),
1529 ALLOCNO_HARD_REG_COSTS (a
)[i
] -= cost
;
1530 ALLOCNO_CONFLICT_HARD_REG_COSTS (a
)[i
] -= cost
;
1531 ALLOCNO_COVER_CLASS_COST (a
) = MIN (ALLOCNO_COVER_CLASS_COST (a
),
1532 ALLOCNO_HARD_REG_COSTS (a
)[i
]);
1536 /* After we find hard register and memory costs for allocnos, define
1537 its cover class and modify hard register cost because insns moving
1538 allocno to/from hard registers. */
1540 setup_allocno_cover_class_and_costs (void)
1542 int i
, j
, n
, regno
, num
;
1544 enum reg_class cover_class
, rclass
;
1546 ira_allocno_iterator ai
;
1548 ira_assert (allocno_p
);
1549 FOR_EACH_ALLOCNO (a
, ai
)
1551 i
= ALLOCNO_NUM (a
);
1552 cover_class
= regno_cover_class
[ALLOCNO_REGNO (a
)];
1553 ira_assert (pref
[i
] == NO_REGS
|| cover_class
!= NO_REGS
);
1554 ALLOCNO_MEMORY_COST (a
) = COSTS (costs
, i
)->mem_cost
;
1555 ira_set_allocno_cover_class (a
, cover_class
);
1556 if (cover_class
== NO_REGS
)
1558 ALLOCNO_AVAILABLE_REGS_NUM (a
) = ira_available_class_regs
[cover_class
];
1559 if (optimize
&& ALLOCNO_COVER_CLASS (a
) != pref
[i
])
1561 n
= ira_class_hard_regs_num
[cover_class
];
1562 ALLOCNO_HARD_REG_COSTS (a
)
1563 = reg_costs
= ira_allocate_cost_vector (cover_class
);
1564 for (j
= n
- 1; j
>= 0; j
--)
1566 regno
= ira_class_hard_regs
[cover_class
][j
];
1567 if (TEST_HARD_REG_BIT (reg_class_contents
[pref
[i
]], regno
))
1568 reg_costs
[j
] = ALLOCNO_COVER_CLASS_COST (a
);
1571 rclass
= REGNO_REG_CLASS (regno
);
1572 num
= cost_class_nums
[rclass
];
1575 /* The hard register class is not a cover class or a
1576 class not fully inside in a cover class -- use
1577 the allocno cover class. */
1578 ira_assert (ira_hard_regno_cover_class
[regno
]
1580 num
= cost_class_nums
[cover_class
];
1582 reg_costs
[j
] = COSTS (costs
, i
)->cost
[num
];
1588 ira_traverse_loop_tree (true, ira_loop_tree_root
,
1589 process_bb_node_for_hard_reg_moves
, NULL
);
1594 /* Function called once during compiler work. */
1596 ira_init_costs_once (void)
1601 for (i
= 0; i
< MAX_RECOG_OPERANDS
; i
++)
1604 this_op_costs
[i
] = NULL
;
1607 cost_classes
= NULL
;
1610 /* Free allocated temporary cost vectors. */
1612 free_ira_costs (void)
1616 if (init_cost
!= NULL
)
1619 for (i
= 0; i
< MAX_RECOG_OPERANDS
; i
++)
1621 if (op_costs
[i
] != NULL
)
1623 if (this_op_costs
[i
] != NULL
)
1624 free (this_op_costs
[i
]);
1625 op_costs
[i
] = this_op_costs
[i
] = NULL
;
1627 if (temp_costs
!= NULL
)
1630 if (cost_classes
!= NULL
)
1631 free (cost_classes
);
1632 cost_classes
= NULL
;
1635 /* This is called each time register related information is
1638 ira_init_costs (void)
1643 max_struct_costs_size
1644 = sizeof (struct costs
) + sizeof (int) * (ira_important_classes_num
- 1);
1645 /* Don't use ira_allocate because vectors live through several IRA calls. */
1646 init_cost
= (struct costs
*) xmalloc (max_struct_costs_size
);
1647 init_cost
->mem_cost
= 1000000;
1648 for (i
= 0; i
< ira_important_classes_num
; i
++)
1649 init_cost
->cost
[i
] = 1000000;
1650 for (i
= 0; i
< MAX_RECOG_OPERANDS
; i
++)
1652 op_costs
[i
] = (struct costs
*) xmalloc (max_struct_costs_size
);
1653 this_op_costs
[i
] = (struct costs
*) xmalloc (max_struct_costs_size
);
1655 temp_costs
= (struct costs
*) xmalloc (max_struct_costs_size
);
1656 cost_classes
= (enum reg_class
*) xmalloc (sizeof (enum reg_class
)
1657 * ira_important_classes_num
);
1660 /* Function called once at the end of compiler work. */
1662 ira_finish_costs_once (void)
1669 /* Common initialization function for ira_costs and
1670 ira_set_pseudo_classes. */
1674 init_subregs_of_mode ();
1675 costs
= (struct costs
*) ira_allocate (max_struct_costs_size
1676 * cost_elements_num
);
1678 = (enum reg_class
*) ira_allocate (sizeof (enum reg_class
)
1679 * cost_elements_num
);
1681 = (enum reg_class
*) ira_allocate (sizeof (enum reg_class
)
1685 /* Common finalization function for ira_costs and
1686 ira_set_pseudo_classes. */
1690 ira_free (regno_cover_class
);
1691 ira_free (pref_buffer
);
1695 /* Entry function which defines cover class, memory and hard register
1696 costs for each allocno. */
1701 cost_elements_num
= ira_allocnos_num
;
1703 total_allocno_costs
= (struct costs
*) ira_allocate (max_struct_costs_size
1704 * ira_allocnos_num
);
1705 find_costs_and_classes (ira_dump_file
);
1706 setup_allocno_cover_class_and_costs ();
1708 ira_free (total_allocno_costs
);
1711 /* Entry function which defines classes for pseudos. */
1713 ira_set_pseudo_classes (FILE *dump_file
)
1716 internal_flag_ira_verbose
= flag_ira_verbose
;
1717 cost_elements_num
= max_reg_num ();
1719 find_costs_and_classes (dump_file
);
1720 pseudo_classes_defined_p
= true;
1726 /* Change hard register costs for allocnos which lives through
1727 function calls. This is called only when we found all intersected
1728 calls during building allocno live ranges. */
1730 ira_tune_allocno_costs_and_cover_classes (void)
1733 int cost
, min_cost
, *reg_costs
;
1734 enum reg_class cover_class
, rclass
;
1735 enum machine_mode mode
;
1737 ira_allocno_iterator ai
;
1739 FOR_EACH_ALLOCNO (a
, ai
)
1741 cover_class
= ALLOCNO_COVER_CLASS (a
);
1742 if (cover_class
== NO_REGS
)
1744 mode
= ALLOCNO_MODE (a
);
1745 n
= ira_class_hard_regs_num
[cover_class
];
1747 if (ALLOCNO_CALLS_CROSSED_NUM (a
) != 0)
1749 ira_allocate_and_set_costs
1750 (&ALLOCNO_HARD_REG_COSTS (a
), cover_class
,
1751 ALLOCNO_COVER_CLASS_COST (a
));
1752 reg_costs
= ALLOCNO_HARD_REG_COSTS (a
);
1753 for (j
= n
- 1; j
>= 0; j
--)
1755 regno
= ira_class_hard_regs
[cover_class
][j
];
1756 rclass
= REGNO_REG_CLASS (regno
);
1758 if (! ira_hard_reg_not_in_set_p (regno
, mode
, call_used_reg_set
)
1759 || HARD_REGNO_CALL_PART_CLOBBERED (regno
, mode
))
1760 cost
+= (ALLOCNO_CALL_FREQ (a
)
1761 * (ira_memory_move_cost
[mode
][rclass
][0]
1762 + ira_memory_move_cost
[mode
][rclass
][1]));
1763 #ifdef IRA_HARD_REGNO_ADD_COST_MULTIPLIER
1764 cost
+= ((ira_memory_move_cost
[mode
][rclass
][0]
1765 + ira_memory_move_cost
[mode
][rclass
][1])
1767 * IRA_HARD_REGNO_ADD_COST_MULTIPLIER (regno
) / 2);
1769 reg_costs
[j
] += cost
;
1770 if (min_cost
> reg_costs
[j
])
1771 min_cost
= reg_costs
[j
];
1774 if (min_cost
!= INT_MAX
)
1775 ALLOCNO_COVER_CLASS_COST (a
) = min_cost
;
1777 /* Some targets allow pseudos to be allocated to unaligned
1778 sequences of hard registers. However, selecting an unaligned
1779 sequence can unnecessarily restrict later allocations. So
1780 increase the cost of unaligned hard regs to encourage the use
1781 of aligned hard regs. */
1785 if ((nregs
= ira_reg_class_nregs
[cover_class
][ALLOCNO_MODE (a
)]) > 1)
1787 ira_allocate_and_set_costs
1788 (&ALLOCNO_HARD_REG_COSTS (a
), cover_class
,
1789 ALLOCNO_COVER_CLASS_COST (a
));
1790 reg_costs
= ALLOCNO_HARD_REG_COSTS (a
);
1791 for (j
= n
- 1; j
>= 0; j
--)
1795 regno
= ira_non_ordered_class_hard_regs
[cover_class
][j
];
1796 index
= ira_class_hard_reg_index
[cover_class
][regno
];
1797 ira_assert (index
!= -1);
1798 reg_costs
[index
] += ALLOCNO_FREQ (a
);