ira-costs.c (ira_tune_allocno_costs_and_cover_classes): Fix typo.
[gcc.git] / gcc / ira-costs.c
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>.
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "hard-reg-set.h"
27 #include "rtl.h"
28 #include "expr.h"
29 #include "tm_p.h"
30 #include "flags.h"
31 #include "basic-block.h"
32 #include "regs.h"
33 #include "addresses.h"
34 #include "insn-config.h"
35 #include "recog.h"
36 #include "toplev.h"
37 #include "target.h"
38 #include "params.h"
39 #include "ira-int.h"
40
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;
44
45 /* TRUE if we work with allocnos. Otherwise we work with pseudos. */
46 static bool allocno_p;
47
48 /* Number of elements in arrays `in_inc_dec' and `costs'. */
49 static int cost_elements_num;
50
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;
55 #endif
56
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
59 allocno or pseudo. */
60 struct costs
61 {
62 int mem_cost;
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). */
66 int cost[1];
67 };
68
69 /* Initialized once. It is a maximal possible size of the allocated
70 struct costs. */
71 static int max_struct_costs_size;
72
73 /* Allocated and initialized once, and used to initialize cost values
74 for each insn. */
75 static struct costs *init_cost;
76
77 /* Allocated once, and used for temporary purposes. */
78 static struct costs *temp_costs;
79
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];
83
84 /* Costs of each class for each allocno or pseudo. */
85 static struct costs *costs;
86
87 /* Accumulated costs of each class for each allocno. */
88 static struct costs *total_allocno_costs;
89
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;
94
95 /* The size of the previous array. */
96 static int cost_classes_num;
97
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];
101
102 /* It is the current size of struct costs. */
103 static int struct_costs_size;
104
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))
109
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]) \
113 : (int) regno)
114
115 /* Record register class preferences of each allocno or pseudo. Null
116 value means no preferences. It happens on the 1st iteration of the
117 cost calculation. */
118 static enum reg_class *pref;
119
120 /* Allocated buffers for pref. */
121 static enum reg_class *pref_buffer;
122
123 /* Record cover register class of each allocno with the same regno. */
124 static enum reg_class *regno_cover_class;
125
126 /* Execution frequency of the current insn. */
127 static int frequency;
128
129 \f
130
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. */
134 static int
135 copy_cost (rtx x, enum machine_mode mode, enum reg_class rclass, bool to_p,
136 secondary_reload_info *prev_sri)
137 {
138 secondary_reload_info sri;
139 enum reg_class secondary_class = NO_REGS;
140
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)
144 return 0;
145
146 /* Get the class we will actually use for a reload. */
147 rclass = PREFERRED_RELOAD_CLASS (x, rclass);
148
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
151 copy it. */
152 sri.prev_sri = prev_sri;
153 sri.extra_cost = 0;
154 secondary_class = targetm.secondary_reload (to_p, x, rclass, mode, &sri);
155
156 if (secondary_class != NO_REGS)
157 {
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));
162 }
163
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];
169 else if (REG_P (x))
170 {
171 if (!move_cost[mode])
172 init_move_cost (mode);
173 return (sri.extra_cost + move_cost[mode][REGNO_REG_CLASS (REGNO (x))][rclass]);
174 }
175 else
176 /* If this is a constant, we may eventually want to call rtx_cost
177 here. */
178 return sri.extra_cost + COSTS_N_INSNS (1);
179 }
180
181 \f
182
183 /* Record the cost of using memory or hard registers of various
184 classes for the operands in INSN.
185
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.
192
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.
200
201 The cost of each class for this insn is its lowest cost among all
202 the alternatives. */
203 static void
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)
208 {
209 int alt;
210 int i, j, k;
211 rtx set;
212 int insn_allows_mem[MAX_RECOG_OPERANDS];
213
214 for (i = 0; i < n_ops; i++)
215 insn_allows_mem[i] = 0;
216
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++)
220 {
221 enum reg_class classes[MAX_RECOG_OPERANDS];
222 int allows_mem[MAX_RECOG_OPERANDS];
223 enum reg_class rclass;
224 int alt_fail = 0;
225 int alt_cost = 0, op_cost_add;
226
227 if (!recog_data.alternative_enabled_p[alt])
228 {
229 for (i = 0; i < recog_data.n_operands; i++)
230 constraints[i] = skip_alternative (constraints[i]);
231
232 continue;
233 }
234
235 for (i = 0; i < n_ops; i++)
236 {
237 unsigned char c;
238 const char *p = constraints[i];
239 rtx op = ops[i];
240 enum machine_mode mode = modes[i];
241 int allows_addr = 0;
242 int win = 0;
243
244 /* Initially show we know nothing about the register class. */
245 classes[i] = NO_REGS;
246 allows_mem[i] = 0;
247
248 /* If this operand has no constraints at all, we can
249 conclude nothing about it since anything is valid. */
250 if (*p == 0)
251 {
252 if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
253 memset (this_op_costs[i], 0, struct_costs_size);
254 continue;
255 }
256
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 == '&')
263 p++;
264
265 if (p[0] >= '0' && p[0] <= '0' + i && (p[1] == ',' || p[1] == 0))
266 {
267 /* Copy class and whether memory is allowed from the
268 matching alternative. Then perform any needed cost
269 computations and/or adjustments. */
270 j = p[0] - '0';
271 classes[i] = classes[j];
272 allows_mem[i] = allows_mem[j];
273 if (allows_mem[i])
274 insn_allows_mem[i] = 1;
275
276 if (! REG_P (op) || REGNO (op) < FIRST_PSEUDO_REGISTER)
277 {
278 /* If this matches the other operand, we have no
279 added cost and we win. */
280 if (rtx_equal_p (ops[j], op))
281 win = 1;
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
285 other operand. */
286 else if (classes[j] != NO_REGS)
287 {
288 alt_cost += copy_cost (op, mode, classes[j], 1, NULL);
289 win = 1;
290 }
291 }
292 else if (! REG_P (ops[j])
293 || REGNO (ops[j]) < FIRST_PSEUDO_REGISTER)
294 {
295 /* This op is an allocno but the one it matches is
296 not. */
297
298 /* If we can't put the other operand into a
299 register, this alternative can't be used. */
300
301 if (classes[j] == NO_REGS)
302 alt_fail = 1;
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. */
306 else
307 alt_cost += copy_cost (ops[j], mode, classes[j], 1, NULL);
308 }
309 else
310 {
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];
316
317 for (k = 0; k < cost_classes_num; k++)
318 {
319 rclass = cost_classes[k];
320 pp->cost[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],
326 rclass, false) : 0))
327 * frequency);
328 }
329
330 /* If the alternative actually allows memory, make
331 things a bit cheaper since we won't need an extra
332 insn to load it. */
333 pp->mem_cost
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;
339
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
345 approximation. */
346 if (pref)
347 {
348 enum reg_class pref_class = pref[COST_INDEX (REGNO (op))];
349
350 if (pref_class == NO_REGS)
351 alt_cost
352 += ((recog_data.operand_type[i] != OP_IN
353 ? ira_memory_move_cost[mode][classes[i]][0]
354 : 0)
355 + (recog_data.operand_type[i] != OP_OUT
356 ? ira_memory_move_cost[mode][classes[i]][1]
357 : 0));
358 else if (ira_reg_class_intersect
359 [pref_class][classes[i]] == NO_REGS)
360 alt_cost += ira_get_register_move_cost (mode,
361 pref_class,
362 classes[i]);
363 }
364 if (REGNO (ops[i]) != REGNO (ops[j])
365 && ! find_reg_note (insn, REG_DEAD, op))
366 alt_cost += 2;
367
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++ != ',')
372 ;
373
374 constraints[i] = p;
375 continue;
376 }
377 }
378
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
382 memory. */
383 while ((c = *p))
384 {
385 switch (c)
386 {
387 case ',':
388 break;
389 case '*':
390 /* Ignore the next letter for this pass. */
391 c = *++p;
392 break;
393
394 case '?':
395 alt_cost += 2;
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':
399 break;
400
401 case 'p':
402 allows_addr = 1;
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. */
407 classes[i]
408 = ira_reg_class_union[classes[i]]
409 [base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
410 break;
411
412 case 'm': case 'o': case 'V':
413 /* It doesn't seem worth distinguishing between
414 offsettable and non-offsettable addresses
415 here. */
416 insn_allows_mem[i] = allows_mem[i] = 1;
417 if (MEM_P (op))
418 win = 1;
419 break;
420
421 case '<':
422 if (MEM_P (op)
423 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
424 || GET_CODE (XEXP (op, 0)) == POST_DEC))
425 win = 1;
426 break;
427
428 case '>':
429 if (MEM_P (op)
430 && (GET_CODE (XEXP (op, 0)) == PRE_INC
431 || GET_CODE (XEXP (op, 0)) == POST_INC))
432 win = 1;
433 break;
434
435 case 'E':
436 case 'F':
437 if (GET_CODE (op) == CONST_DOUBLE
438 || (GET_CODE (op) == CONST_VECTOR
439 && (GET_MODE_CLASS (GET_MODE (op))
440 == MODE_VECTOR_FLOAT)))
441 win = 1;
442 break;
443
444 case 'G':
445 case 'H':
446 if (GET_CODE (op) == CONST_DOUBLE
447 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
448 win = 1;
449 break;
450
451 case 's':
452 if (CONST_INT_P (op)
453 || (GET_CODE (op) == CONST_DOUBLE
454 && GET_MODE (op) == VOIDmode))
455 break;
456
457 case 'i':
458 if (CONSTANT_P (op)
459 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op)))
460 win = 1;
461 break;
462
463 case 'n':
464 if (CONST_INT_P (op)
465 || (GET_CODE (op) == CONST_DOUBLE
466 && GET_MODE (op) == VOIDmode))
467 win = 1;
468 break;
469
470 case 'I':
471 case 'J':
472 case 'K':
473 case 'L':
474 case 'M':
475 case 'N':
476 case 'O':
477 case 'P':
478 if (CONST_INT_P (op)
479 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
480 win = 1;
481 break;
482
483 case 'X':
484 win = 1;
485 break;
486
487 case 'g':
488 if (MEM_P (op)
489 || (CONSTANT_P (op)
490 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))))
491 win = 1;
492 insn_allows_mem[i] = allows_mem[i] = 1;
493 case 'r':
494 classes[i] = ira_reg_class_union[classes[i]][GENERAL_REGS];
495 break;
496
497 default:
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))
503 win = 1;
504
505 if (EXTRA_MEMORY_CONSTRAINT (c, p))
506 {
507 /* Every MEM can be reloaded to fit. */
508 insn_allows_mem[i] = allows_mem[i] = 1;
509 if (MEM_P (op))
510 win = 1;
511 }
512 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
513 {
514 /* Every address can be reloaded to fit. */
515 allows_addr = 1;
516 if (address_operand (op, GET_MODE (op)))
517 win = 1;
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. */
522 classes[i]
523 = ira_reg_class_union[classes[i]]
524 [base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
525 }
526 #endif
527 break;
528 }
529 p += CONSTRAINT_LEN (c, p);
530 if (c == ',')
531 break;
532 }
533
534 constraints[i] = p;
535
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
542 into that class. */
543 if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
544 {
545 if (classes[i] == NO_REGS)
546 {
547 /* We must always fail if the operand is a REG, but
548 we did not find a suitable class.
549
550 Otherwise we may perform an uninitialized read
551 from this_op_costs after the `continue' statement
552 below. */
553 alt_fail = 1;
554 }
555 else
556 {
557 struct costs *pp = this_op_costs[i];
558
559 for (k = 0; k < cost_classes_num; k++)
560 {
561 rclass = cost_classes[k];
562 pp->cost[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],
568 rclass, false) : 0))
569 * frequency);
570 }
571
572 /* If the alternative actually allows memory, make
573 things a bit cheaper since we won't need an extra
574 insn to load it. */
575 pp->mem_cost
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
586 approximation. */
587 if (pref)
588 {
589 enum reg_class pref_class = pref[COST_INDEX (REGNO (op))];
590
591 if (pref_class == NO_REGS)
592 alt_cost
593 += ((recog_data.operand_type[i] != OP_IN
594 ? ira_memory_move_cost[mode][classes[i]][0]
595 : 0)
596 + (recog_data.operand_type[i] != OP_OUT
597 ? ira_memory_move_cost[mode][classes[i]][1]
598 : 0));
599 else if (ira_reg_class_intersect[pref_class][classes[i]]
600 == NO_REGS)
601 alt_cost += ira_get_register_move_cost (mode,
602 pref_class,
603 classes[i]);
604 }
605 }
606 }
607
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
611 alternative. */
612 else if (win || (REG_P (op)
613 && reg_fits_class_p (op, classes[i],
614 0, GET_MODE (op))))
615 ;
616
617 /* If registers are valid, the cost of this alternative
618 includes copying the object to and/or from a
619 register. */
620 else if (classes[i] != NO_REGS)
621 {
622 if (recog_data.operand_type[i] != OP_OUT)
623 alt_cost += copy_cost (op, mode, classes[i], 1, NULL);
624
625 if (recog_data.operand_type[i] != OP_IN)
626 alt_cost += copy_cost (op, mode, classes[i], 0, NULL);
627 }
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];
632 else
633 alt_fail = 1;
634 }
635
636 if (alt_fail)
637 continue;
638
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)
644 {
645 struct costs *pp = op_costs[i], *qq = this_op_costs[i];
646 int scale = 1 + (recog_data.operand_type[i] == OP_INOUT);
647
648 pp->mem_cost = MIN (pp->mem_cost,
649 (qq->mem_cost + op_cost_add) * scale);
650
651 for (k = 0; k < cost_classes_num; k++)
652 pp->cost[k]
653 = MIN (pp->cost[k], (qq->cost[k] + op_cost_add) * scale);
654 }
655 }
656
657 if (allocno_p)
658 for (i = 0; i < n_ops; i++)
659 {
660 ira_allocno_t a;
661 rtx op = ops[i];
662
663 if (! REG_P (op) || REGNO (op) < FIRST_PSEUDO_REGISTER)
664 continue;
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;
668 }
669
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.
674
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.
678
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
683 instruction. */
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)
690 {
691 unsigned int regno = REGNO (ops[!i]);
692 enum machine_mode mode = GET_MODE (ops[!i]);
693 enum reg_class rclass;
694 unsigned int nr;
695
696 if (regno < FIRST_PSEUDO_REGISTER)
697 for (k = 0; k < cost_classes_num; k++)
698 {
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)))
703 {
704 if (reg_class_size[rclass] == 1)
705 op_costs[i]->cost[k] = -frequency;
706 else
707 {
708 for (nr = 0;
709 nr < (unsigned) hard_regno_nregs[regno][mode];
710 nr++)
711 if (! TEST_HARD_REG_BIT (reg_class_contents[rclass],
712 regno + nr))
713 break;
714
715 if (nr == (unsigned) hard_regno_nregs[regno][mode])
716 op_costs[i]->cost[k] = -frequency;
717 }
718 }
719 }
720 }
721 }
722
723 \f
724
725 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudo registers. */
726 static inline bool
727 ok_for_index_p_nonstrict (rtx reg)
728 {
729 unsigned regno = REGNO (reg);
730
731 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
732 }
733
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. */
737 static inline bool
738 ok_for_base_p_nonstrict (rtx reg, enum machine_mode mode,
739 enum rtx_code outer_code, enum rtx_code index_code)
740 {
741 unsigned regno = REGNO (reg);
742
743 if (regno >= FIRST_PSEUDO_REGISTER)
744 return true;
745 return ok_for_base_p_1 (regno, mode, outer_code, index_code);
746 }
747
748 /* Record the pseudo registers we must reload into hard registers in a
749 subexpression of a memory address, X.
750
751 If CONTEXT is 0, we are looking at the base part of an address,
752 otherwise we are looking at the index part.
753
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.
757
758 SCALE is twice the amount to multiply the cost by (it is twice so
759 we can represent half-cost adjustments). */
760 static void
761 record_address_regs (enum machine_mode mode, rtx x, int context,
762 enum rtx_code outer_code, enum rtx_code index_code,
763 int scale)
764 {
765 enum rtx_code code = GET_CODE (x);
766 enum reg_class rclass;
767
768 if (context == 1)
769 rclass = INDEX_REG_CLASS;
770 else
771 rclass = base_reg_class (mode, outer_code, index_code);
772
773 switch (code)
774 {
775 case CONST_INT:
776 case CONST:
777 case CC0:
778 case PC:
779 case SYMBOL_REF:
780 case LABEL_REF:
781 return;
782
783 case PLUS:
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.
791
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. */
795 {
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);
800
801 /* Look inside subregs. */
802 if (code0 == SUBREG)
803 arg0 = SUBREG_REG (arg0), code0 = GET_CODE (arg0);
804 if (code1 == SUBREG)
805 arg1 = SUBREG_REG (arg1), code1 = GET_CODE (arg1);
806
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);
811
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))
817 {
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);
821 }
822
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)
840 ? 1 : 0,
841 PLUS, REG, scale);
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)
848 ? 1 : 0,
849 PLUS, REG, scale);
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)
854 {
855 record_address_regs (mode, arg0, 0, PLUS, code1, scale);
856 record_address_regs (mode, arg1, 1, PLUS, code0, scale);
857 }
858 else if ((code1 == REG && REG_POINTER (arg1)) || code0 == MULT)
859 {
860 record_address_regs (mode, arg0, 1, PLUS, code1, scale);
861 record_address_regs (mode, arg1, 0, PLUS, code0, scale);
862 }
863 /* Otherwise, count equal chances that each might be a base or
864 index register. This case should be rare. */
865 else
866 {
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);
871 }
872 }
873 break;
874
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. */
878 case POST_MODIFY:
879 case PRE_MODIFY:
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,
884 2 * scale);
885 break;
886
887 case POST_INC:
888 case PRE_INC:
889 case POST_DEC:
890 case PRE_DEC:
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;
899 #endif
900 record_address_regs (mode, XEXP (x, 0), 0, code, SCRATCH, 2 * scale);
901 break;
902
903 case REG:
904 {
905 struct costs *pp;
906 enum reg_class i;
907 int k;
908
909 if (REGNO (x) < FIRST_PSEUDO_REGISTER)
910 break;
911
912 if (allocno_p)
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++)
917 {
918 i = cost_classes[k];
919 pp->cost[k]
920 += (ira_get_may_move_cost (Pmode, i, rclass, true) * scale) / 2;
921 }
922 }
923 break;
924
925 default:
926 {
927 const char *fmt = GET_RTX_FORMAT (code);
928 int i;
929 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
930 if (fmt[i] == 'e')
931 record_address_regs (mode, XEXP (x, i), context, code, SCRATCH,
932 scale);
933 }
934 }
935 }
936
937 \f
938
939 /* Calculate the costs of insn operands. */
940 static void
941 record_operand_costs (rtx insn, struct costs **op_costs, enum reg_class *pref)
942 {
943 const char *constraints[MAX_RECOG_OPERANDS];
944 enum machine_mode modes[MAX_RECOG_OPERANDS];
945 int i;
946
947 for (i = 0; i < recog_data.n_operands; i++)
948 {
949 constraints[i] = recog_data.constraints[i];
950 modes[i] = recog_data.operand_mode[i];
951 }
952
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
957 commutative. */
958 for (i = 0; i < recog_data.n_operands; i++)
959 {
960 memcpy (op_costs[i], init_cost, struct_costs_size);
961
962 if (GET_CODE (recog_data.operand[i]) == SUBREG)
963 recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
964
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],
971 constraints[i]))
972 record_address_regs (VOIDmode, recog_data.operand[i], 0, ADDRESS,
973 SCRATCH, frequency * 2);
974 }
975
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] == '%')
981 {
982 const char *xconstraints[MAX_RECOG_OPERANDS];
983 int j;
984
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];
989
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);
995 }
996 record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
997 recog_data.operand, modes,
998 constraints, insn, op_costs, pref);
999 }
1000
1001 \f
1002
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. */
1006 static rtx
1007 scan_one_insn (rtx insn)
1008 {
1009 enum rtx_code pat_code;
1010 rtx set, note;
1011 int i, k;
1012
1013 if (!NONDEBUG_INSN_P (insn))
1014 return insn;
1015
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)
1019 return insn;
1020
1021 set = single_set (insn);
1022 extract_insn (insn);
1023
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)))
1030 {
1031 enum reg_class cl = GENERAL_REGS;
1032 rtx reg = SET_DEST (set);
1033 int num = COST_INDEX (REGNO (reg));
1034
1035 if (pref)
1036 cl = pref[num];
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);
1041 }
1042
1043 record_operand_costs (insn, op_costs, pref);
1044
1045 /* Now add the cost for each operand to the total costs for its
1046 allocno. */
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)
1050 {
1051 int regno = REGNO (recog_data.operand[i]);
1052 struct costs *p = COSTS (costs, COST_INDEX (regno));
1053 struct costs *q = op_costs[i];
1054
1055 p->mem_cost += q->mem_cost;
1056 for (k = 0; k < cost_classes_num; k++)
1057 p->cost[k] += q->cost[k];
1058 }
1059
1060 return insn;
1061 }
1062
1063 \f
1064
1065 /* Print allocnos costs to file F. */
1066 static void
1067 print_allocno_costs (FILE *f)
1068 {
1069 int k;
1070 ira_allocno_t a;
1071 ira_allocno_iterator ai;
1072
1073 ira_assert (allocno_p);
1074 fprintf (f, "\n");
1075 FOR_EACH_ALLOCNO (a, ai)
1076 {
1077 int i, rclass;
1078 basic_block bb;
1079 int regno = ALLOCNO_REGNO (a);
1080
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);
1085 else
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++)
1089 {
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])
1094 #endif
1095 #ifdef CANNOT_CHANGE_MODE_CLASS
1096 && ! invalid_mode_change_p (regno, (enum reg_class) rclass,
1097 PSEUDO_REGNO_MODE (regno))
1098 #endif
1099 )
1100 {
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]);
1106 }
1107 }
1108 fprintf (f, " MEM:%i\n", COSTS (costs, i)->mem_cost);
1109 }
1110 }
1111
1112 /* Print pseudo costs to file F. */
1113 static void
1114 print_pseudo_costs (FILE *f)
1115 {
1116 int regno, k;
1117 int rclass;
1118
1119 ira_assert (! allocno_p);
1120 fprintf (f, "\n");
1121 for (regno = max_reg_num () - 1; regno >= FIRST_PSEUDO_REGISTER; regno--)
1122 {
1123 if (regno_reg_rtx[regno] == NULL_RTX)
1124 continue;
1125 fprintf (f, " r%d costs:", regno);
1126 for (k = 0; k < cost_classes_num; k++)
1127 {
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])
1132 #endif
1133 #ifdef CANNOT_CHANGE_MODE_CLASS
1134 && ! invalid_mode_change_p (regno, (enum reg_class) rclass,
1135 PSEUDO_REGNO_MODE (regno))
1136 #endif
1137 )
1138 fprintf (f, " %s:%d", reg_class_names[rclass],
1139 COSTS (costs, regno)->cost[k]);
1140 }
1141 fprintf (f, " MEM:%i\n", COSTS (costs, regno)->mem_cost);
1142 }
1143 }
1144
1145 /* Traverse the BB represented by LOOP_TREE_NODE to update the allocno
1146 costs. */
1147 static void
1148 process_bb_for_costs (basic_block bb)
1149 {
1150 rtx insn;
1151
1152 frequency = REG_FREQ_FROM_BB (bb);
1153 if (frequency == 0)
1154 frequency = 1;
1155 FOR_BB_INSNS (bb, insn)
1156 insn = scan_one_insn (insn);
1157 }
1158
1159 /* Traverse the BB represented by LOOP_TREE_NODE to update the allocno
1160 costs. */
1161 static void
1162 process_bb_node_for_costs (ira_loop_tree_node_t loop_tree_node)
1163 {
1164 basic_block bb;
1165
1166 bb = loop_tree_node->bb;
1167 if (bb != NULL)
1168 process_bb_for_costs (bb);
1169 }
1170
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. */
1174 static void
1175 find_costs_and_classes (FILE *dump_file)
1176 {
1177 int i, k, start;
1178 int pass;
1179 basic_block bb;
1180
1181 init_recog ();
1182 #ifdef FORBIDDEN_INC_DEC_CLASSES
1183 in_inc_dec = ira_allocate (sizeof (bool) * cost_elements_num);
1184 #endif /* FORBIDDEN_INC_DEC_CLASSES */
1185 pref = NULL;
1186 start = 0;
1187 if (!resize_reg_info () && allocno_p && pseudo_classes_defined_p)
1188 {
1189 ira_allocno_t a;
1190 ira_allocno_iterator ai;
1191
1192 pref = pref_buffer;
1193 FOR_EACH_ALLOCNO (a, ai)
1194 pref[ALLOCNO_NUM (a)] = reg_preferred_class (ALLOCNO_REGNO (a));
1195 if (flag_expensive_optimizations)
1196 start = 1;
1197 }
1198 if (allocno_p)
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++)
1206 {
1207 if ((!allocno_p || internal_flag_ira_verbose > 0) && dump_file)
1208 fprintf (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;
1217 cost_classes_num++)
1218 {
1219 cost_classes[cost_classes_num]
1220 = ira_important_classes[cost_classes_num];
1221 cost_class_nums[cost_classes[cost_classes_num]]
1222 = cost_classes_num;
1223 }
1224 struct_costs_size
1225 = sizeof (struct costs) + sizeof (int) * (cost_classes_num - 1);
1226 /* Zero out our accumulation of the cost of each class for each
1227 allocno. */
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));
1231 #endif
1232
1233 if (allocno_p)
1234 {
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);
1239
1240 memcpy (total_allocno_costs, costs,
1241 max_struct_costs_size * ira_allocnos_num);
1242 }
1243 else
1244 {
1245 basic_block bb;
1246
1247 FOR_EACH_BB (bb)
1248 process_bb_for_costs (bb);
1249 }
1250
1251 if (pass == 0)
1252 pref = pref_buffer;
1253
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--)
1257 {
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;
1265 #endif
1266
1267 if (! allocno_p)
1268 {
1269 if (regno_reg_rtx[i] == NULL_RTX)
1270 continue;
1271 #ifdef FORBIDDEN_INC_DEC_CLASSES
1272 inc_dec_p = in_inc_dec[i];
1273 #endif
1274 memcpy (temp_costs, COSTS (costs, i), struct_costs_size);
1275 }
1276 else
1277 {
1278 if (ira_regno_allocno_map[i] == NULL)
1279 continue;
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];
1283 a != NULL;
1284 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
1285 {
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,
1294 ALLOCNO_NUM (a)))
1295 {
1296 /* Propagate costs to upper levels in the region
1297 tree. */
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;
1304 }
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])
1310 inc_dec_p = true;
1311 #endif
1312 }
1313 }
1314 best_cost = (1 << (HOST_BITS_PER_INT - 2)) - 1;
1315 best = ALL_REGS;
1316 alt_class = NO_REGS;
1317 /* Find best common class for all allocnos with the same
1318 regno. */
1319 for (k = 0; k < cost_classes_num; k++)
1320 {
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])
1327 #endif
1328 #ifdef CANNOT_CHANGE_MODE_CLASS
1329 || invalid_mode_change_p (i, (enum reg_class) rclass,
1330 PSEUDO_REGNO_MODE (i))
1331 #endif
1332 )
1333 continue;
1334 if (temp_costs->cost[k] < best_cost)
1335 {
1336 best_cost = temp_costs->cost[k];
1337 best = (enum reg_class) rclass;
1338 }
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];
1346 }
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
1352 alt_class. */
1353 regno_cover_class[i] = alt_class == NO_REGS ? best : alt_class;
1354 else
1355 /* Make the common class a cover class. Remember all
1356 allocnos with the same regno should have the same cover
1357 class. */
1358 regno_cover_class[i] = ira_class_translate[best];
1359 if (pass == flag_expensive_optimizations)
1360 {
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)
1368 fprintf (dump_file,
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]]);
1372 }
1373 if (! allocno_p)
1374 {
1375 pref[i] = best_cost > temp_costs->mem_cost ? NO_REGS : best;
1376 continue;
1377 }
1378 for (a = ira_regno_allocno_map[i];
1379 a != NULL;
1380 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
1381 {
1382 a_num = ALLOCNO_NUM (a);
1383 if (regno_cover_class[i] == NO_REGS)
1384 best = NO_REGS;
1385 else
1386 {
1387 /* Finding best class which is subset of the common
1388 class. */
1389 best_cost = (1 << (HOST_BITS_PER_INT - 2)) - 1;
1390 allocno_cost = best_cost;
1391 best = ALL_REGS;
1392 for (k = 0; k < cost_classes_num; k++)
1393 {
1394 rclass = cost_classes[k];
1395 if (! ira_class_subset_p[rclass][regno_cover_class[i]])
1396 continue;
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])
1403 #endif
1404 #ifdef CANNOT_CHANGE_MODE_CLASS
1405 || invalid_mode_change_p (i, (enum reg_class) rclass,
1406 PSEUDO_REGNO_MODE (i))
1407 #endif
1408 )
1409 ;
1410 else if (COSTS (total_allocno_costs, a_num)->cost[k]
1411 < best_cost)
1412 {
1413 best_cost
1414 = COSTS (total_allocno_costs, a_num)->cost[k];
1415 allocno_cost = COSTS (costs, a_num)->cost[k];
1416 best = (enum reg_class) rclass;
1417 }
1418 else if (COSTS (total_allocno_costs, a_num)->cost[k]
1419 == best_cost)
1420 {
1421 best = ira_reg_class_union[best][rclass];
1422 allocno_cost
1423 = MAX (allocno_cost, COSTS (costs, a_num)->cost[k]);
1424 }
1425 }
1426 ALLOCNO_COVER_CLASS_COST (a) = allocno_cost;
1427 }
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))
1432 {
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);
1436 else
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]]);
1442 }
1443 pref[a_num] = best;
1444 }
1445 }
1446
1447 if (internal_flag_ira_verbose > 4 && dump_file)
1448 {
1449 if (allocno_p)
1450 print_allocno_costs (dump_file);
1451 else
1452 print_pseudo_costs (dump_file);
1453 fprintf (dump_file,"\n");
1454 }
1455 }
1456 #ifdef FORBIDDEN_INC_DEC_CLASSES
1457 ira_free (in_inc_dec);
1458 #endif
1459 }
1460
1461 \f
1462
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
1467 allocno. */
1468 static void
1469 process_bb_node_for_hard_reg_moves (ira_loop_tree_node_t loop_tree_node)
1470 {
1471 int i, freq, cost, src_regno, dst_regno, hard_regno;
1472 bool to_p;
1473 ira_allocno_t a;
1474 enum reg_class rclass, hard_reg_class;
1475 enum machine_mode mode;
1476 basic_block bb;
1477 rtx insn, set, src, dst;
1478
1479 bb = loop_tree_node->bb;
1480 if (bb == NULL)
1481 return;
1482 freq = REG_FREQ_FROM_BB (bb);
1483 if (freq == 0)
1484 freq = 1;
1485 FOR_BB_INSNS (bb, insn)
1486 {
1487 if (!NONDEBUG_INSN_P (insn))
1488 continue;
1489 set = single_set (insn);
1490 if (set == NULL_RTX)
1491 continue;
1492 dst = SET_DEST (set);
1493 src = SET_SRC (set);
1494 if (! REG_P (dst) || ! REG_P (src))
1495 continue;
1496 dst_regno = REGNO (dst);
1497 src_regno = REGNO (src);
1498 if (dst_regno >= FIRST_PSEUDO_REGISTER
1499 && src_regno < FIRST_PSEUDO_REGISTER)
1500 {
1501 hard_regno = src_regno;
1502 to_p = true;
1503 a = ira_curr_regno_allocno_map[dst_regno];
1504 }
1505 else if (src_regno >= FIRST_PSEUDO_REGISTER
1506 && dst_regno < FIRST_PSEUDO_REGISTER)
1507 {
1508 hard_regno = dst_regno;
1509 to_p = false;
1510 a = ira_curr_regno_allocno_map[src_regno];
1511 }
1512 else
1513 continue;
1514 rclass = ALLOCNO_COVER_CLASS (a);
1515 if (! TEST_HARD_REG_BIT (reg_class_contents[rclass], hard_regno))
1516 continue;
1517 i = ira_class_hard_reg_index[rclass][hard_regno];
1518 if (i < 0)
1519 continue;
1520 mode = ALLOCNO_MODE (a);
1521 hard_reg_class = REGNO_REG_CLASS (hard_regno);
1522 cost
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),
1528 rclass, 0);
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]);
1533 }
1534 }
1535
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. */
1539 static void
1540 setup_allocno_cover_class_and_costs (void)
1541 {
1542 int i, j, n, regno, num;
1543 int *reg_costs;
1544 enum reg_class cover_class, rclass;
1545 ira_allocno_t a;
1546 ira_allocno_iterator ai;
1547
1548 ira_assert (allocno_p);
1549 FOR_EACH_ALLOCNO (a, ai)
1550 {
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)
1557 continue;
1558 ALLOCNO_AVAILABLE_REGS_NUM (a) = ira_available_class_regs[cover_class];
1559 if (optimize && ALLOCNO_COVER_CLASS (a) != pref[i])
1560 {
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--)
1565 {
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);
1569 else
1570 {
1571 rclass = REGNO_REG_CLASS (regno);
1572 num = cost_class_nums[rclass];
1573 if (num < 0)
1574 {
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]
1579 == cover_class);
1580 num = cost_class_nums[cover_class];
1581 }
1582 reg_costs[j] = COSTS (costs, i)->cost[num];
1583 }
1584 }
1585 }
1586 }
1587 if (optimize)
1588 ira_traverse_loop_tree (true, ira_loop_tree_root,
1589 process_bb_node_for_hard_reg_moves, NULL);
1590 }
1591
1592 \f
1593
1594 /* Function called once during compiler work. */
1595 void
1596 ira_init_costs_once (void)
1597 {
1598 int i;
1599
1600 init_cost = NULL;
1601 for (i = 0; i < MAX_RECOG_OPERANDS; i++)
1602 {
1603 op_costs[i] = NULL;
1604 this_op_costs[i] = NULL;
1605 }
1606 temp_costs = NULL;
1607 cost_classes = NULL;
1608 }
1609
1610 /* Free allocated temporary cost vectors. */
1611 static void
1612 free_ira_costs (void)
1613 {
1614 int i;
1615
1616 if (init_cost != NULL)
1617 free (init_cost);
1618 init_cost = NULL;
1619 for (i = 0; i < MAX_RECOG_OPERANDS; i++)
1620 {
1621 if (op_costs[i] != NULL)
1622 free (op_costs[i]);
1623 if (this_op_costs[i] != NULL)
1624 free (this_op_costs[i]);
1625 op_costs[i] = this_op_costs[i] = NULL;
1626 }
1627 if (temp_costs != NULL)
1628 free (temp_costs);
1629 temp_costs = NULL;
1630 if (cost_classes != NULL)
1631 free (cost_classes);
1632 cost_classes = NULL;
1633 }
1634
1635 /* This is called each time register related information is
1636 changed. */
1637 void
1638 ira_init_costs (void)
1639 {
1640 int i;
1641
1642 free_ira_costs ();
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++)
1651 {
1652 op_costs[i] = (struct costs *) xmalloc (max_struct_costs_size);
1653 this_op_costs[i] = (struct costs *) xmalloc (max_struct_costs_size);
1654 }
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);
1658 }
1659
1660 /* Function called once at the end of compiler work. */
1661 void
1662 ira_finish_costs_once (void)
1663 {
1664 free_ira_costs ();
1665 }
1666
1667 \f
1668
1669 /* Common initialization function for ira_costs and
1670 ira_set_pseudo_classes. */
1671 static void
1672 init_costs (void)
1673 {
1674 init_subregs_of_mode ();
1675 costs = (struct costs *) ira_allocate (max_struct_costs_size
1676 * cost_elements_num);
1677 pref_buffer
1678 = (enum reg_class *) ira_allocate (sizeof (enum reg_class)
1679 * cost_elements_num);
1680 regno_cover_class
1681 = (enum reg_class *) ira_allocate (sizeof (enum reg_class)
1682 * max_reg_num ());
1683 }
1684
1685 /* Common finalization function for ira_costs and
1686 ira_set_pseudo_classes. */
1687 static void
1688 finish_costs (void)
1689 {
1690 ira_free (regno_cover_class);
1691 ira_free (pref_buffer);
1692 ira_free (costs);
1693 }
1694
1695 /* Entry function which defines cover class, memory and hard register
1696 costs for each allocno. */
1697 void
1698 ira_costs (void)
1699 {
1700 allocno_p = true;
1701 cost_elements_num = ira_allocnos_num;
1702 init_costs ();
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 ();
1707 finish_costs ();
1708 ira_free (total_allocno_costs);
1709 }
1710
1711 /* Entry function which defines classes for pseudos. */
1712 void
1713 ira_set_pseudo_classes (FILE *dump_file)
1714 {
1715 allocno_p = false;
1716 internal_flag_ira_verbose = flag_ira_verbose;
1717 cost_elements_num = max_reg_num ();
1718 init_costs ();
1719 find_costs_and_classes (dump_file);
1720 pseudo_classes_defined_p = true;
1721 finish_costs ();
1722 }
1723
1724 \f
1725
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. */
1729 void
1730 ira_tune_allocno_costs_and_cover_classes (void)
1731 {
1732 int j, n, regno;
1733 int cost, min_cost, *reg_costs;
1734 enum reg_class cover_class, rclass;
1735 enum machine_mode mode;
1736 ira_allocno_t a;
1737 ira_allocno_iterator ai;
1738
1739 FOR_EACH_ALLOCNO (a, ai)
1740 {
1741 cover_class = ALLOCNO_COVER_CLASS (a);
1742 if (cover_class == NO_REGS)
1743 continue;
1744 mode = ALLOCNO_MODE (a);
1745 n = ira_class_hard_regs_num[cover_class];
1746 min_cost = INT_MAX;
1747 if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
1748 {
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--)
1754 {
1755 regno = ira_class_hard_regs[cover_class][j];
1756 rclass = REGNO_REG_CLASS (regno);
1757 cost = 0;
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])
1766 * ALLOCNO_FREQ (a)
1767 * IRA_HARD_REGNO_ADD_COST_MULTIPLIER (regno) / 2);
1768 #endif
1769 reg_costs[j] += cost;
1770 if (min_cost > reg_costs[j])
1771 min_cost = reg_costs[j];
1772 }
1773 }
1774 if (min_cost != INT_MAX)
1775 ALLOCNO_COVER_CLASS_COST (a) = min_cost;
1776
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. */
1782 {
1783 int nregs, index;
1784
1785 if ((nregs = ira_reg_class_nregs[cover_class][ALLOCNO_MODE (a)]) > 1)
1786 {
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--)
1792 {
1793 if (j % nregs != 0)
1794 {
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);
1799 }
1800 }
1801 }
1802 }
1803 }
1804 }