OSDN Git Service

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