OSDN Git Service

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