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