OSDN Git Service

* regclass.c (regclass): Properly compute loop_cost. Adjust
[pf3gnuchains/gcc-fork.git] / gcc / regclass.c
1 /* Compute register class preferences for pseudo-registers.
2    Copyright (C) 1987, 88, 91-98, 1999, 2000 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21
22 /* This file contains two passes of the compiler: reg_scan and reg_class.
23    It also defines some tables of information about the hardware registers
24    and a function init_reg_sets to initialize the tables.  */
25
26 #include "config.h"
27 #include "system.h"
28 #include "rtl.h"
29 #include "tm_p.h"
30 #include "hard-reg-set.h"
31 #include "flags.h"
32 #include "basic-block.h"
33 #include "regs.h"
34 #include "function.h"
35 #include "insn-config.h"
36 #include "recog.h"
37 #include "reload.h"
38 #include "real.h"
39 #include "toplev.h"
40 #include "output.h"
41 #include "ggc.h"
42
43 #ifndef REGISTER_MOVE_COST
44 #define REGISTER_MOVE_COST(x, y) 2
45 #endif
46
47 static void init_reg_sets_1     PROTO((void));
48 static void init_reg_modes      PROTO((void));
49
50 /* If we have auto-increment or auto-decrement and we can have secondary
51    reloads, we are not allowed to use classes requiring secondary
52    reloads for pseudos auto-incremented since reload can't handle it.  */
53
54 #ifdef AUTO_INC_DEC
55 #if defined(SECONDARY_INPUT_RELOAD_CLASS) || defined(SECONDARY_OUTPUT_RELOAD_CLASS)
56 #define FORBIDDEN_INC_DEC_CLASSES
57 #endif
58 #endif
59 \f
60 /* Register tables used by many passes.  */
61
62 /* Indexed by hard register number, contains 1 for registers
63    that are fixed use (stack pointer, pc, frame pointer, etc.).
64    These are the registers that cannot be used to allocate
65    a pseudo reg for general use.  */
66
67 char fixed_regs[FIRST_PSEUDO_REGISTER];
68
69 /* Same info as a HARD_REG_SET.  */
70
71 HARD_REG_SET fixed_reg_set;
72
73 /* Data for initializing the above.  */
74
75 static char initial_fixed_regs[] = FIXED_REGISTERS;
76
77 /* Indexed by hard register number, contains 1 for registers
78    that are fixed use or are clobbered by function calls.
79    These are the registers that cannot be used to allocate
80    a pseudo reg whose life crosses calls unless we are able
81    to save/restore them across the calls.  */
82
83 char call_used_regs[FIRST_PSEUDO_REGISTER];
84
85 /* Same info as a HARD_REG_SET.  */
86
87 HARD_REG_SET call_used_reg_set;
88
89 /* HARD_REG_SET of registers we want to avoid caller saving.  */
90 HARD_REG_SET losing_caller_save_reg_set;
91
92 /* Data for initializing the above.  */
93
94 static char initial_call_used_regs[] = CALL_USED_REGISTERS;
95   
96 /* Indexed by hard register number, contains 1 for registers that are
97    fixed use or call used registers that cannot hold quantities across
98    calls even if we are willing to save and restore them.  call fixed
99    registers are a subset of call used registers.  */
100
101 char call_fixed_regs[FIRST_PSEUDO_REGISTER];
102
103 /* The same info as a HARD_REG_SET.  */
104
105 HARD_REG_SET call_fixed_reg_set;
106
107 /* Number of non-fixed registers.  */
108
109 int n_non_fixed_regs;
110
111 /* Indexed by hard register number, contains 1 for registers
112    that are being used for global register decls.
113    These must be exempt from ordinary flow analysis
114    and are also considered fixed.  */
115
116 char global_regs[FIRST_PSEUDO_REGISTER];
117   
118 /* Table of register numbers in the order in which to try to use them.  */
119 #ifdef REG_ALLOC_ORDER
120 int reg_alloc_order[FIRST_PSEUDO_REGISTER] = REG_ALLOC_ORDER;
121
122 /* The inverse of reg_alloc_order.  */
123 int inv_reg_alloc_order[FIRST_PSEUDO_REGISTER];
124 #endif
125
126 /* For each reg class, a HARD_REG_SET saying which registers are in it.  */
127
128 HARD_REG_SET reg_class_contents[N_REG_CLASSES];
129
130 /* The same information, but as an array of unsigned ints.  We copy from
131    these unsigned ints to the table above.  We do this so the tm.h files
132    do not have to be aware of the wordsize for machines with <= 64 regs.  */
133
134 #define N_REG_INTS  \
135   ((FIRST_PSEUDO_REGISTER + (HOST_BITS_PER_INT - 1)) / HOST_BITS_PER_INT)
136
137 static unsigned int_reg_class_contents[N_REG_CLASSES][N_REG_INTS] 
138   = REG_CLASS_CONTENTS;
139
140 /* For each reg class, number of regs it contains.  */
141
142 int reg_class_size[N_REG_CLASSES];
143
144 /* For each reg class, table listing all the containing classes.  */
145
146 enum reg_class reg_class_superclasses[N_REG_CLASSES][N_REG_CLASSES];
147
148 /* For each reg class, table listing all the classes contained in it.  */
149
150 enum reg_class reg_class_subclasses[N_REG_CLASSES][N_REG_CLASSES];
151
152 /* For each pair of reg classes,
153    a largest reg class contained in their union.  */
154
155 enum reg_class reg_class_subunion[N_REG_CLASSES][N_REG_CLASSES];
156
157 /* For each pair of reg classes,
158    the smallest reg class containing their union.  */
159
160 enum reg_class reg_class_superunion[N_REG_CLASSES][N_REG_CLASSES];
161
162 /* Array containing all of the register names */
163
164 const char *reg_names[] = REGISTER_NAMES;
165
166 /* For each hard register, the widest mode object that it can contain.
167    This will be a MODE_INT mode if the register can hold integers.  Otherwise
168    it will be a MODE_FLOAT or a MODE_CC mode, whichever is valid for the
169    register.  */
170
171 enum machine_mode reg_raw_mode[FIRST_PSEUDO_REGISTER];
172
173 /* Maximum cost of moving from a register in one class to a register in
174    another class.  Based on REGISTER_MOVE_COST.  */
175
176 static int move_cost[N_REG_CLASSES][N_REG_CLASSES];
177
178 /* Similar, but here we don't have to move if the first index is a subset
179    of the second so in that case the cost is zero.  */
180
181 static int may_move_in_cost[N_REG_CLASSES][N_REG_CLASSES];
182
183 /* Similar, but here we don't have to move if the first index is a superset
184    of the second so in that case the cost is zero.  */
185
186 static int may_move_out_cost[N_REG_CLASSES][N_REG_CLASSES];
187
188 #ifdef FORBIDDEN_INC_DEC_CLASSES
189
190 /* These are the classes that regs which are auto-incremented or decremented
191    cannot be put in.  */
192
193 static int forbidden_inc_dec_class[N_REG_CLASSES];
194
195 /* Indexed by n, is non-zero if (REG n) is used in an auto-inc or auto-dec
196    context.  */
197
198 static char *in_inc_dec;
199
200 #endif /* FORBIDDEN_INC_DEC_CLASSES */
201
202 #ifdef HAVE_SECONDARY_RELOADS
203
204 /* Sample MEM values for use by memory_move_secondary_cost.  */
205
206 static rtx top_of_stack[MAX_MACHINE_MODE];
207
208 #endif /* HAVE_SECONDARY_RELOADS */
209
210 /* Linked list of reg_info structures allocated for reg_n_info array.
211    Grouping all of the allocated structures together in one lump
212    means only one call to bzero to clear them, rather than n smaller
213    calls.  */
214 struct reg_info_data {
215   struct reg_info_data *next;   /* next set of reg_info structures */
216   size_t min_index;             /* minimum index # */
217   size_t max_index;             /* maximum index # */
218   char used_p;                  /* non-zero if this has been used previously */
219   reg_info data[1];             /* beginning of the reg_info data */
220 };
221
222 static struct reg_info_data *reg_info_head;
223
224 /* No more global register variables may be declared; true once
225    regclass has been initialized. */
226
227 static int no_global_reg_vars = 0;
228
229
230 /* Function called only once to initialize the above data on reg usage.
231    Once this is done, various switches may override.  */
232
233 void
234 init_reg_sets ()
235 {
236   register int i, j;
237
238   /* First copy the register information from the initial int form into
239      the regsets.  */
240
241   for (i = 0; i < N_REG_CLASSES; i++)
242     {
243       CLEAR_HARD_REG_SET (reg_class_contents[i]);
244
245       for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
246         if (int_reg_class_contents[i][j / HOST_BITS_PER_INT]
247             & ((unsigned) 1 << (j % HOST_BITS_PER_INT)))
248           SET_HARD_REG_BIT (reg_class_contents[i], j);
249     }
250
251   bcopy (initial_fixed_regs, fixed_regs, sizeof fixed_regs);
252   bcopy (initial_call_used_regs, call_used_regs, sizeof call_used_regs);
253   bzero (global_regs, sizeof global_regs);
254
255   /* Do any additional initialization regsets may need */
256   INIT_ONCE_REG_SET ();
257
258 #ifdef REG_ALLOC_ORDER
259   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
260     inv_reg_alloc_order[reg_alloc_order[i]] = i;
261 #endif
262 }
263
264 /* After switches have been processed, which perhaps alter
265    `fixed_regs' and `call_used_regs', convert them to HARD_REG_SETs.  */
266
267 static void
268 init_reg_sets_1 ()
269 {
270   register unsigned int i, j;
271
272   /* This macro allows the fixed or call-used registers
273      and the register classes to depend on target flags.  */
274
275 #ifdef CONDITIONAL_REGISTER_USAGE
276   CONDITIONAL_REGISTER_USAGE;
277 #endif
278
279   /* Compute number of hard regs in each class.  */
280
281   bzero ((char *) reg_class_size, sizeof reg_class_size);
282   for (i = 0; i < N_REG_CLASSES; i++)
283     for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
284       if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
285         reg_class_size[i]++;
286
287   /* Initialize the table of subunions.
288      reg_class_subunion[I][J] gets the largest-numbered reg-class
289      that is contained in the union of classes I and J.  */
290
291   for (i = 0; i < N_REG_CLASSES; i++)
292     {
293       for (j = 0; j < N_REG_CLASSES; j++)
294         {
295 #ifdef HARD_REG_SET
296           register              /* Declare it register if it's a scalar.  */
297 #endif
298             HARD_REG_SET c;
299           register int k;
300
301           COPY_HARD_REG_SET (c, reg_class_contents[i]);
302           IOR_HARD_REG_SET (c, reg_class_contents[j]);
303           for (k = 0; k < N_REG_CLASSES; k++)
304             {
305               GO_IF_HARD_REG_SUBSET (reg_class_contents[k], c,
306                                      subclass1);
307               continue;
308
309             subclass1:
310               /* keep the largest subclass */           /* SPEE 900308 */
311               GO_IF_HARD_REG_SUBSET (reg_class_contents[k],
312                                      reg_class_contents[(int) reg_class_subunion[i][j]],
313                                      subclass2);
314               reg_class_subunion[i][j] = (enum reg_class) k;
315             subclass2:
316               ;
317             }
318         }
319     }
320
321   /* Initialize the table of superunions.
322      reg_class_superunion[I][J] gets the smallest-numbered reg-class
323      containing the union of classes I and J.  */
324
325   for (i = 0; i < N_REG_CLASSES; i++)
326     {
327       for (j = 0; j < N_REG_CLASSES; j++)
328         {
329 #ifdef HARD_REG_SET
330           register              /* Declare it register if it's a scalar.  */
331 #endif
332             HARD_REG_SET c;
333           register int k;
334
335           COPY_HARD_REG_SET (c, reg_class_contents[i]);
336           IOR_HARD_REG_SET (c, reg_class_contents[j]);
337           for (k = 0; k < N_REG_CLASSES; k++)
338             GO_IF_HARD_REG_SUBSET (c, reg_class_contents[k], superclass);
339
340         superclass:
341           reg_class_superunion[i][j] = (enum reg_class) k;
342         }
343     }
344
345   /* Initialize the tables of subclasses and superclasses of each reg class.
346      First clear the whole table, then add the elements as they are found.  */
347
348   for (i = 0; i < N_REG_CLASSES; i++)
349     {
350       for (j = 0; j < N_REG_CLASSES; j++)
351         {
352           reg_class_superclasses[i][j] = LIM_REG_CLASSES;
353           reg_class_subclasses[i][j] = LIM_REG_CLASSES;
354         }
355     }
356
357   for (i = 0; i < N_REG_CLASSES; i++)
358     {
359       if (i == (int) NO_REGS)
360         continue;
361
362       for (j = i + 1; j < N_REG_CLASSES; j++)
363         {
364           enum reg_class *p;
365
366           GO_IF_HARD_REG_SUBSET (reg_class_contents[i], reg_class_contents[j],
367                                  subclass);
368           continue;
369         subclass:
370           /* Reg class I is a subclass of J.
371              Add J to the table of superclasses of I.  */
372           p = &reg_class_superclasses[i][0];
373           while (*p != LIM_REG_CLASSES) p++;
374           *p = (enum reg_class) j;
375           /* Add I to the table of superclasses of J.  */
376           p = &reg_class_subclasses[j][0];
377           while (*p != LIM_REG_CLASSES) p++;
378           *p = (enum reg_class) i;
379         }
380     }
381
382   /* Initialize "constant" tables.  */
383
384   CLEAR_HARD_REG_SET (fixed_reg_set);
385   CLEAR_HARD_REG_SET (call_used_reg_set);
386   CLEAR_HARD_REG_SET (call_fixed_reg_set);
387
388   bcopy (fixed_regs, call_fixed_regs, sizeof call_fixed_regs);
389
390   n_non_fixed_regs = 0;
391
392   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
393     {
394       if (fixed_regs[i])
395         SET_HARD_REG_BIT (fixed_reg_set, i);
396       else
397         n_non_fixed_regs++;
398
399       if (call_used_regs[i])
400         SET_HARD_REG_BIT (call_used_reg_set, i);
401       if (call_fixed_regs[i])
402         SET_HARD_REG_BIT (call_fixed_reg_set, i);
403       if (CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (i)))
404         SET_HARD_REG_BIT (losing_caller_save_reg_set, i);
405     }
406
407   /* Initialize the move cost table.  Find every subset of each class
408      and take the maximum cost of moving any subset to any other.  */
409
410   for (i = 0; i < N_REG_CLASSES; i++)
411     for (j = 0; j < N_REG_CLASSES; j++)
412       {
413         int cost = i == j ? 2 : REGISTER_MOVE_COST (i, j);
414         enum reg_class *p1, *p2;
415
416         for (p2 = &reg_class_subclasses[j][0]; *p2 != LIM_REG_CLASSES; p2++)
417           if (*p2 != i)
418             cost = MAX (cost, REGISTER_MOVE_COST (i, *p2));
419
420         for (p1 = &reg_class_subclasses[i][0]; *p1 != LIM_REG_CLASSES; p1++)
421           {
422             if (*p1 != j)
423               cost = MAX (cost, REGISTER_MOVE_COST (*p1, j));
424
425             for (p2 = &reg_class_subclasses[j][0];
426                  *p2 != LIM_REG_CLASSES; p2++)
427               if (*p1 != *p2)
428                 cost = MAX (cost, REGISTER_MOVE_COST (*p1, *p2));
429           }
430
431         move_cost[i][j] = cost;
432
433         if (reg_class_subset_p (i, j))
434           may_move_in_cost[i][j] = 0;
435         else
436           may_move_in_cost[i][j] = cost;
437
438         if (reg_class_subset_p (j, i))
439           may_move_out_cost[i][j] = 0;
440         else
441           may_move_out_cost[i][j] = cost;
442       }
443 }
444
445 /* Compute the table of register modes.
446    These values are used to record death information for individual registers
447    (as opposed to a multi-register mode).  */
448
449 static void
450 init_reg_modes ()
451 {
452   register int i;
453
454   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
455     {
456       reg_raw_mode[i] = choose_hard_reg_mode (i, 1);
457
458       /* If we couldn't find a valid mode, just use the previous mode.
459          ??? One situation in which we need to do this is on the mips where
460          HARD_REGNO_NREGS (fpreg, [SD]Fmode) returns 2.  Ideally we'd like
461          to use DF mode for the even registers and VOIDmode for the odd
462          (for the cpu models where the odd ones are inaccessible).  */
463       if (reg_raw_mode[i] == VOIDmode)
464         reg_raw_mode[i] = i == 0 ? word_mode : reg_raw_mode[i-1];
465     }
466 }
467
468 /* Finish initializing the register sets and
469    initialize the register modes.  */
470
471 void
472 init_regs ()
473 {
474   /* This finishes what was started by init_reg_sets, but couldn't be done
475      until after register usage was specified.  */
476   init_reg_sets_1 ();
477
478   init_reg_modes ();
479
480 #ifdef HAVE_SECONDARY_RELOADS
481   {
482     /* Make some fake stack-frame MEM references for use in
483        memory_move_secondary_cost.  */
484     int i;
485     for (i = 0; i < MAX_MACHINE_MODE; i++)
486       top_of_stack[i] = gen_rtx_MEM (i, stack_pointer_rtx);
487     ggc_add_rtx_root (top_of_stack, MAX_MACHINE_MODE);
488   }
489 #endif
490 }
491
492 #ifdef HAVE_SECONDARY_RELOADS
493
494 /* Compute extra cost of moving registers to/from memory due to reloads.
495    Only needed if secondary reloads are required for memory moves.  */
496
497 int
498 memory_move_secondary_cost (mode, class, in)
499      enum machine_mode mode;
500      enum reg_class class;
501      int in;
502 {
503   enum reg_class altclass;
504   int partial_cost = 0;
505   /* We need a memory reference to feed to SECONDARY... macros.  */
506   /* mem may be unused even if the SECONDARY_ macros are defined. */
507   rtx mem ATTRIBUTE_UNUSED = top_of_stack[(int) mode];
508
509
510   if (in)
511     {
512 #ifdef SECONDARY_INPUT_RELOAD_CLASS
513       altclass = SECONDARY_INPUT_RELOAD_CLASS (class, mode, mem);
514 #else
515       altclass = NO_REGS;
516 #endif
517     }
518   else
519     {
520 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
521       altclass = SECONDARY_OUTPUT_RELOAD_CLASS (class, mode, mem);
522 #else
523       altclass = NO_REGS;
524 #endif
525     }
526
527   if (altclass == NO_REGS)
528     return 0;
529
530   if (in)
531     partial_cost = REGISTER_MOVE_COST (altclass, class);
532   else
533     partial_cost = REGISTER_MOVE_COST (class, altclass);
534
535   if (class == altclass)
536     /* This isn't simply a copy-to-temporary situation.  Can't guess
537        what it is, so MEMORY_MOVE_COST really ought not to be calling
538        here in that case.
539
540        I'm tempted to put in an abort here, but returning this will
541        probably only give poor estimates, which is what we would've
542        had before this code anyways.  */
543     return partial_cost;
544
545   /* Check if the secondary reload register will also need a
546      secondary reload.  */
547   return memory_move_secondary_cost (mode, altclass, in) + partial_cost;
548 }
549 #endif
550
551 /* Return a machine mode that is legitimate for hard reg REGNO and large
552    enough to save nregs.  If we can't find one, return VOIDmode.  */
553
554 enum machine_mode
555 choose_hard_reg_mode (regno, nregs)
556      int regno;
557      int nregs;
558 {
559   enum machine_mode found_mode = VOIDmode, mode;
560
561   /* We first look for the largest integer mode that can be validly
562      held in REGNO.  If none, we look for the largest floating-point mode.
563      If we still didn't find a valid mode, try CCmode.  */
564
565   for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
566        mode != VOIDmode;
567        mode = GET_MODE_WIDER_MODE (mode))
568     if (HARD_REGNO_NREGS (regno, mode) == nregs
569         && HARD_REGNO_MODE_OK (regno, mode))
570       found_mode = mode;
571
572   if (found_mode != VOIDmode)
573     return found_mode;
574
575   for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
576        mode != VOIDmode;
577        mode = GET_MODE_WIDER_MODE (mode))
578     if (HARD_REGNO_NREGS (regno, mode) == nregs
579         && HARD_REGNO_MODE_OK (regno, mode))
580       found_mode = mode;
581
582   if (found_mode != VOIDmode)
583     return found_mode;
584
585   if (HARD_REGNO_NREGS (regno, CCmode) == nregs
586       && HARD_REGNO_MODE_OK (regno, CCmode))
587     return CCmode;
588
589   /* We can't find a mode valid for this register.  */
590   return VOIDmode;
591 }
592
593 /* Specify the usage characteristics of the register named NAME.
594    It should be a fixed register if FIXED and a
595    call-used register if CALL_USED.  */
596
597 void
598 fix_register (name, fixed, call_used)
599      const char *name;
600      int fixed, call_used;
601 {
602   int i;
603
604   /* Decode the name and update the primary form of
605      the register info.  */
606
607   if ((i = decode_reg_name (name)) >= 0)
608     {
609       if ((i == STACK_POINTER_REGNUM
610 #ifdef HARD_FRAME_POINTER_REGNUM
611            || i == HARD_FRAME_POINTER_REGNUM
612 #else
613            || i == FRAME_POINTER_REGNUM
614 #endif
615            )
616           && (fixed == 0 || call_used == 0))
617         {
618           static const char * const what_option[2][2] = {
619             { "call-saved", "call-used" },
620             { "no-such-option", "fixed" }};
621           
622           error ("can't use '%s' as a %s register", name, 
623                  what_option[fixed][call_used]);
624         }
625       else
626         {
627           fixed_regs[i] = fixed;
628           call_used_regs[i] = call_used;
629         }
630     }
631   else
632     {
633       warning ("unknown register name: %s", name);
634     }
635 }
636
637 /* Mark register number I as global.  */
638
639 void
640 globalize_reg (i)
641      int i;
642 {
643   if (fixed_regs[i] == 0 && no_global_reg_vars)
644     error ("global register variable follows a function definition");
645
646   if (global_regs[i])
647     {
648       warning ("register used for two global register variables");
649       return;
650     }
651
652   if (call_used_regs[i] && ! fixed_regs[i])
653     warning ("call-clobbered register used for global register variable");
654
655   global_regs[i] = 1;
656
657   /* If already fixed, nothing else to do.  */
658   if (fixed_regs[i])
659     return;
660
661   fixed_regs[i] = call_used_regs[i] = call_fixed_regs[i] = 1;
662   n_non_fixed_regs--;
663
664   SET_HARD_REG_BIT (fixed_reg_set, i);
665   SET_HARD_REG_BIT (call_used_reg_set, i);
666   SET_HARD_REG_BIT (call_fixed_reg_set, i);
667 }
668 \f
669 /* Now the data and code for the `regclass' pass, which happens
670    just before local-alloc.  */
671
672 /* The `costs' struct records the cost of using a hard register of each class
673    and of using memory for each pseudo.  We use this data to set up
674    register class preferences.  */
675
676 struct costs
677 {
678   int cost[N_REG_CLASSES];
679   int mem_cost;
680 };
681
682 /* Structure used to record preferrences of given pseudo.  */
683 struct reg_pref
684 {
685   /* (enum reg_class) prefclass is the preferred class.  */
686   char prefclass;
687
688   /* altclass is a register class that we should use for allocating
689      pseudo if no register in the preferred class is available.
690      If no register in this class is available, memory is preferred.
691
692      It might appear to be more general to have a bitmask of classes here,
693      but since it is recommended that there be a class corresponding to the
694      union of most major pair of classes, that generality is not required.  */
695   char altclass;
696 };
697
698 /* Record the cost of each class for each pseudo.  */
699
700 static struct costs *costs;
701
702 /* Initialized once, and used to initialize cost values for each insn.  */
703
704 static struct costs init_cost;
705
706 /* Record preferrences of each pseudo.
707    This is available after `regclass' is run.  */
708
709 static struct reg_pref *reg_pref;
710
711 /* Allocated buffers for reg_pref. */
712
713 static struct reg_pref *reg_pref_buffer;
714
715 /* Account for the fact that insns within a loop are executed very commonly,
716    but don't keep doing this as loops go too deep.  */
717
718 static int loop_cost;
719
720 static rtx scan_one_insn        PROTO((rtx, int));
721 static void record_operand_costs PROTO((rtx, struct costs *, struct reg_pref *));
722 static void dump_regclass       PROTO((FILE *));
723 static void record_reg_classes  PROTO((int, int, rtx *, enum machine_mode *,
724                                        char *, const char **, rtx,
725                                        struct costs *, struct reg_pref *));
726 static int copy_cost            PROTO((rtx, enum machine_mode, 
727                                        enum reg_class, int));
728 static void record_address_regs PROTO((rtx, enum reg_class, int));
729 #ifdef FORBIDDEN_INC_DEC_CLASSES
730 static int auto_inc_dec_reg_p   PROTO((rtx, enum machine_mode));
731 #endif
732 static void reg_scan_mark_refs  PROTO((rtx, rtx, int, int));
733
734 /* Return the reg_class in which pseudo reg number REGNO is best allocated.
735    This function is sometimes called before the info has been computed.
736    When that happens, just return GENERAL_REGS, which is innocuous.  */
737
738 enum reg_class
739 reg_preferred_class (regno)
740      int regno;
741 {
742   if (reg_pref == 0)
743     return GENERAL_REGS;
744   return (enum reg_class) reg_pref[regno].prefclass;
745 }
746
747 enum reg_class
748 reg_alternate_class (regno)
749      int regno;
750 {
751   if (reg_pref == 0)
752     return ALL_REGS;
753
754   return (enum reg_class) reg_pref[regno].altclass;
755 }
756
757 /* Initialize some global data for this pass.  */
758
759 void
760 regclass_init ()
761 {
762   int i;
763
764   init_cost.mem_cost = 10000;
765   for (i = 0; i < N_REG_CLASSES; i++)
766     init_cost.cost[i] = 10000;
767
768   /* This prevents dump_flow_info from losing if called
769      before regclass is run.  */
770   reg_pref = NULL;
771
772   /* No more global register variables may be declared. */
773   no_global_reg_vars = 1;
774 }
775 \f
776 /* Dump register costs.  */
777 static void
778 dump_regclass (dump)
779      FILE *dump;
780 {
781   static const char *const reg_class_names[] = REG_CLASS_NAMES;
782   int i;
783   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
784     {
785       enum reg_class class;
786       if (REG_N_REFS (i))
787         {
788           fprintf (dump, "  Register %i costs:", i);
789           for (class = 0; class < N_REG_CLASSES; class++)
790             fprintf (dump, " %s:%i", reg_class_names[(int) class],
791                      costs[i].cost[class]);
792           fprintf (dump, " MEM:%i\n", costs[i].mem_cost);
793         }
794     }
795 }
796 \f
797
798 /* Calculate the costs of insn operands.  */
799
800 static void
801 record_operand_costs (insn, op_costs, reg_pref)
802      rtx insn;
803      struct costs *op_costs;
804      struct reg_pref *reg_pref;
805 {
806   const char *constraints[MAX_RECOG_OPERANDS];
807   enum machine_mode modes[MAX_RECOG_OPERANDS];
808   char subreg_changes_size[MAX_RECOG_OPERANDS];
809   int i;
810
811   for (i = 0; i < recog_data.n_operands; i++)
812     {
813       constraints[i] = recog_data.constraints[i];
814       modes[i] = recog_data.operand_mode[i];
815     }
816   memset (subreg_changes_size, 0, sizeof (subreg_changes_size));
817
818   /* If we get here, we are set up to record the costs of all the
819      operands for this insn.  Start by initializing the costs.
820      Then handle any address registers.  Finally record the desired
821      classes for any pseudos, doing it twice if some pair of
822      operands are commutative.  */
823              
824   for (i = 0; i < recog_data.n_operands; i++)
825     {
826       op_costs[i] = init_cost;
827
828       if (GET_CODE (recog_data.operand[i]) == SUBREG)
829         {
830           rtx inner = SUBREG_REG (recog_data.operand[i]);
831           if (GET_MODE_SIZE (modes[i]) != GET_MODE_SIZE (GET_MODE (inner)))
832             subreg_changes_size[i] = 1;
833           recog_data.operand[i] = inner;
834         }
835
836       if (GET_CODE (recog_data.operand[i]) == MEM)
837         record_address_regs (XEXP (recog_data.operand[i], 0),
838                              BASE_REG_CLASS, loop_cost * 2);
839       else if (constraints[i][0] == 'p')
840         record_address_regs (recog_data.operand[i],
841                              BASE_REG_CLASS, loop_cost * 2);
842     }
843
844   /* Check for commutative in a separate loop so everything will
845      have been initialized.  We must do this even if one operand
846      is a constant--see addsi3 in m68k.md.  */
847
848   for (i = 0; i < (int) recog_data.n_operands - 1; i++)
849     if (constraints[i][0] == '%')
850       {
851         const char *xconstraints[MAX_RECOG_OPERANDS];
852         int j;
853
854         /* Handle commutative operands by swapping the constraints.
855            We assume the modes are the same.  */
856
857         for (j = 0; j < recog_data.n_operands; j++)
858           xconstraints[j] = constraints[j];
859
860         xconstraints[i] = constraints[i+1];
861         xconstraints[i+1] = constraints[i];
862         record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
863                             recog_data.operand, modes, subreg_changes_size,
864                             xconstraints, insn, op_costs, reg_pref);
865       }
866
867   record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
868                       recog_data.operand, modes, subreg_changes_size,
869                       constraints, insn, op_costs, reg_pref);
870 }
871 \f
872 /* Subroutine of regclass, processes one insn INSN.  Scan it and record each
873    time it would save code to put a certain register in a certain class.
874    PASS, when nonzero, inhibits some optimizations which need only be done
875    once.
876    Return the last insn processed, so that the scan can be continued from
877    there.  */
878
879 static rtx
880 scan_one_insn (insn, pass)
881      rtx insn;
882      int pass;
883 {
884   enum rtx_code code = GET_CODE (insn);
885   enum rtx_code pat_code;
886   rtx set, note;
887   int i, j;
888   struct costs op_costs[MAX_RECOG_OPERANDS];
889
890   if (GET_RTX_CLASS (code) != 'i')
891     return insn;
892
893   pat_code = GET_CODE (PATTERN (insn));
894   if (pat_code == USE
895       || pat_code == CLOBBER
896       || pat_code == ASM_INPUT
897       || pat_code == ADDR_VEC
898       || pat_code == ADDR_DIFF_VEC)
899     return insn;
900
901   set = single_set (insn);
902   extract_insn (insn);
903
904   /* If this insn loads a parameter from its stack slot, then
905      it represents a savings, rather than a cost, if the
906      parameter is stored in memory.  Record this fact.  */
907
908   if (set != 0 && GET_CODE (SET_DEST (set)) == REG
909       && GET_CODE (SET_SRC (set)) == MEM
910       && (note = find_reg_note (insn, REG_EQUIV,
911                                 NULL_RTX)) != 0
912       && GET_CODE (XEXP (note, 0)) == MEM)
913     {
914       costs[REGNO (SET_DEST (set))].mem_cost
915         -= (MEMORY_MOVE_COST (GET_MODE (SET_DEST (set)),
916                               GENERAL_REGS, 1)
917             * loop_cost);
918       record_address_regs (XEXP (SET_SRC (set), 0),
919                            BASE_REG_CLASS, loop_cost * 2);
920       return insn;
921     }
922
923   /* Improve handling of two-address insns such as
924      (set X (ashift CONST Y)) where CONST must be made to
925      match X. Change it into two insns: (set X CONST)
926      (set X (ashift X Y)).  If we left this for reloading, it
927      would probably get three insns because X and Y might go
928      in the same place. This prevents X and Y from receiving
929      the same hard reg.
930
931      We can only do this if the modes of operands 0 and 1
932      (which might not be the same) are tieable and we only need
933      do this during our first pass.  */
934
935   if (pass == 0 && optimize
936       && recog_data.n_operands >= 3
937       && recog_data.constraints[1][0] == '0'
938       && recog_data.constraints[1][1] == 0
939       && CONSTANT_P (recog_data.operand[1])
940       && ! rtx_equal_p (recog_data.operand[0], recog_data.operand[1])
941       && ! rtx_equal_p (recog_data.operand[0], recog_data.operand[2])
942       && GET_CODE (recog_data.operand[0]) == REG
943       && MODES_TIEABLE_P (GET_MODE (recog_data.operand[0]),
944                           recog_data.operand_mode[1]))
945     {
946       rtx previnsn = prev_real_insn (insn);
947       rtx dest
948         = gen_lowpart (recog_data.operand_mode[1],
949                        recog_data.operand[0]);
950       rtx newinsn
951         = emit_insn_before (gen_move_insn (dest, recog_data.operand[1]), insn);
952
953       /* If this insn was the start of a basic block,
954          include the new insn in that block.
955          We need not check for code_label here;
956          while a basic block can start with a code_label,
957          INSN could not be at the beginning of that block.  */
958       if (previnsn == 0 || GET_CODE (previnsn) == JUMP_INSN)
959         {
960           int b;
961           for (b = 0; b < n_basic_blocks; b++)
962             if (insn == BLOCK_HEAD (b))
963               BLOCK_HEAD (b) = newinsn;
964         }
965
966       /* This makes one more setting of new insns's dest.  */
967       REG_N_SETS (REGNO (recog_data.operand[0]))++;
968
969       *recog_data.operand_loc[1] = recog_data.operand[0];
970       for (i = recog_data.n_dups - 1; i >= 0; i--)
971         if (recog_data.dup_num[i] == 1)
972           *recog_data.dup_loc[i] = recog_data.operand[0];
973
974       return PREV_INSN (newinsn);
975     }
976
977   record_operand_costs (insn, op_costs, reg_pref);
978
979   /* Now add the cost for each operand to the total costs for
980      its register.  */
981
982   for (i = 0; i < recog_data.n_operands; i++)
983     if (GET_CODE (recog_data.operand[i]) == REG
984         && REGNO (recog_data.operand[i]) >= FIRST_PSEUDO_REGISTER)
985       {
986         int regno = REGNO (recog_data.operand[i]);
987         struct costs *p = &costs[regno], *q = &op_costs[i];
988
989         p->mem_cost += q->mem_cost * loop_cost;
990         for (j = 0; j < N_REG_CLASSES; j++)
991           p->cost[j] += q->cost[j] * loop_cost;
992       }
993
994   return insn;
995 }
996
997 /* This is a pass of the compiler that scans all instructions
998    and calculates the preferred class for each pseudo-register.
999    This information can be accessed later by calling `reg_preferred_class'.
1000    This pass comes just before local register allocation.  */
1001
1002 void
1003 regclass (f, nregs, dump)
1004      rtx f;
1005      int nregs;
1006      FILE *dump;
1007 {
1008   register rtx insn;
1009   register int i;
1010   int pass;
1011
1012   init_recog ();
1013
1014   costs = (struct costs *) xmalloc (nregs * sizeof (struct costs));
1015
1016 #ifdef FORBIDDEN_INC_DEC_CLASSES
1017
1018   in_inc_dec = (char *) xmalloc (nregs);
1019
1020   /* Initialize information about which register classes can be used for
1021      pseudos that are auto-incremented or auto-decremented.  It would
1022      seem better to put this in init_reg_sets, but we need to be able
1023      to allocate rtx, which we can't do that early.  */
1024
1025   for (i = 0; i < N_REG_CLASSES; i++)
1026     {
1027       rtx r = gen_rtx_REG (VOIDmode, 0);
1028       enum machine_mode m;
1029       register int j;
1030
1031       for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1032         if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
1033           {
1034             REGNO (r) = j;
1035
1036             for (m = VOIDmode; (int) m < (int) MAX_MACHINE_MODE;
1037                  m = (enum machine_mode) ((int) m + 1))
1038               if (HARD_REGNO_MODE_OK (j, m))
1039                 {
1040                   PUT_MODE (r, m);
1041
1042                   /* If a register is not directly suitable for an
1043                      auto-increment or decrement addressing mode and
1044                      requires secondary reloads, disallow its class from
1045                      being used in such addresses.  */
1046
1047                   if ((0
1048 #ifdef SECONDARY_RELOAD_CLASS
1049                        || (SECONDARY_RELOAD_CLASS (BASE_REG_CLASS, m, r)
1050                            != NO_REGS)
1051 #else
1052 #ifdef SECONDARY_INPUT_RELOAD_CLASS
1053                        || (SECONDARY_INPUT_RELOAD_CLASS (BASE_REG_CLASS, m, r)
1054                            != NO_REGS)
1055 #endif
1056 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
1057                        || (SECONDARY_OUTPUT_RELOAD_CLASS (BASE_REG_CLASS, m, r)
1058                            != NO_REGS)
1059 #endif
1060 #endif
1061                        )
1062                       && ! auto_inc_dec_reg_p (r, m))
1063                     forbidden_inc_dec_class[i] = 1;
1064                 }
1065           }
1066     }
1067 #endif /* FORBIDDEN_INC_DEC_CLASSES */
1068
1069   /* Normally we scan the insns once and determine the best class to use for
1070      each register.  However, if -fexpensive_optimizations are on, we do so
1071      twice, the second time using the tentative best classes to guide the
1072      selection.  */
1073
1074   for (pass = 0; pass <= flag_expensive_optimizations; pass++)
1075     {
1076       int index;
1077
1078       if (dump)
1079         fprintf (dump, "\n\nPass %i\n\n",pass);
1080       /* Zero out our accumulation of the cost of each class for each reg.  */
1081
1082       bzero ((char *) costs, nregs * sizeof (struct costs));
1083
1084 #ifdef FORBIDDEN_INC_DEC_CLASSES
1085       bzero (in_inc_dec, nregs);
1086 #endif
1087
1088       /* Scan the instructions and record each time it would
1089          save code to put a certain register in a certain class.  */
1090
1091       if (!optimize)
1092         {
1093           loop_cost = 1;
1094           for (insn = f; insn; insn = NEXT_INSN (insn))
1095             insn = scan_one_insn (insn, pass);
1096         }
1097       else
1098         for (index = 0; index < n_basic_blocks; index++)        
1099           {
1100             basic_block bb = BASIC_BLOCK (index);
1101
1102             /* Show that an insn inside a loop is likely to be executed three
1103                times more than insns outside a loop.  This is much more
1104                aggressive than the assumptions made elsewhere and is being
1105                tried as an experiment. 
1106
1107                Note that a block's loop depth starts at zero, not one!  We
1108                must not subract one from the loop depth as that could give
1109                a negative shift count below.  */
1110             if (optimize_size)
1111               loop_cost = 1;
1112             else
1113               loop_cost = 1 << (2 * MIN (bb->loop_depth, 5));
1114             for (insn = bb->head; ; insn = NEXT_INSN (insn))
1115               {
1116                 insn = scan_one_insn (insn, pass);
1117                 if (insn == bb->end)
1118                   break;
1119               }
1120           }
1121       
1122       /* Now for each register look at how desirable each class is
1123          and find which class is preferred.  Store that in
1124          `prefclass'.  Record in `altclass' the largest register
1125          class any of whose registers is better than memory.  */
1126     
1127       if (pass == 0)
1128         reg_pref = reg_pref_buffer;
1129
1130       if (dump)
1131         {
1132           dump_regclass (dump);
1133           fprintf (dump,"\n");
1134         }
1135       for (i = FIRST_PSEUDO_REGISTER; i < nregs; i++)
1136         {
1137           register int best_cost = (1 << (HOST_BITS_PER_INT - 2)) - 1;
1138           enum reg_class best = ALL_REGS, alt = NO_REGS;
1139           /* This is an enum reg_class, but we call it an int
1140              to save lots of casts.  */
1141           register int class;
1142           register struct costs *p = &costs[i];
1143
1144           if (!REG_N_REFS (i))
1145             continue;
1146
1147           for (class = (int) ALL_REGS - 1; class > 0; class--)
1148             {
1149               /* Ignore classes that are too small for this operand or
1150                  invalid for a operand that was auto-incremented.  */
1151               if (CLASS_MAX_NREGS (class, PSEUDO_REGNO_MODE (i))
1152                   > reg_class_size[class]
1153 #ifdef FORBIDDEN_INC_DEC_CLASSES
1154                   || (in_inc_dec[i] && forbidden_inc_dec_class[class])
1155 #endif
1156                   )
1157                 ;
1158               else if (p->cost[class] < best_cost)
1159                 {
1160                   best_cost = p->cost[class];
1161                   best = (enum reg_class) class;
1162                 }
1163               else if (p->cost[class] == best_cost)
1164                 best = reg_class_subunion[(int)best][class];
1165             }
1166
1167           /* Record the alternate register class; i.e., a class for which
1168              every register in it is better than using memory.  If adding a
1169              class would make a smaller class (i.e., no union of just those
1170              classes exists), skip that class.  The major unions of classes
1171              should be provided as a register class.  Don't do this if we
1172              will be doing it again later.  */
1173
1174           if ((pass == 1  || dump) || ! flag_expensive_optimizations)
1175             for (class = 0; class < N_REG_CLASSES; class++)
1176               if (p->cost[class] < p->mem_cost
1177                   && (reg_class_size[(int) reg_class_subunion[(int) alt][class]]
1178                       > reg_class_size[(int) alt])
1179 #ifdef FORBIDDEN_INC_DEC_CLASSES
1180                   && ! (in_inc_dec[i] && forbidden_inc_dec_class[class])
1181 #endif
1182                   )
1183                 alt = reg_class_subunion[(int) alt][class];
1184           
1185           /* If we don't add any classes, nothing to try.  */
1186           if (alt == best)
1187             alt = NO_REGS;
1188
1189           if (dump 
1190               && (reg_pref[i].prefclass != (int) best
1191                   || reg_pref[i].altclass != (int) alt))
1192             {
1193               static const char *const reg_class_names[] = REG_CLASS_NAMES;
1194               fprintf (dump, "  Register %i", i);
1195               if (alt == ALL_REGS || best == ALL_REGS)
1196                 fprintf (dump, " pref %s\n", reg_class_names[(int) best]);
1197               else if (alt == NO_REGS)
1198                 fprintf (dump, " pref %s or none\n", reg_class_names[(int) best]);
1199               else
1200                 fprintf (dump, " pref %s, else %s\n",
1201                          reg_class_names[(int) best],
1202                          reg_class_names[(int) alt]);
1203             }
1204
1205           /* We cast to (int) because (char) hits bugs in some compilers.  */
1206           reg_pref[i].prefclass = (int) best;
1207           reg_pref[i].altclass = (int) alt;
1208         }
1209     }
1210
1211 #ifdef FORBIDDEN_INC_DEC_CLASSES
1212   free (in_inc_dec);
1213 #endif
1214   free (costs);
1215 }
1216 \f
1217 /* Record the cost of using memory or registers of various classes for
1218    the operands in INSN.
1219
1220    N_ALTS is the number of alternatives.
1221
1222    N_OPS is the number of operands.
1223
1224    OPS is an array of the operands.
1225
1226    MODES are the modes of the operands, in case any are VOIDmode.
1227
1228    CONSTRAINTS are the constraints to use for the operands.  This array
1229    is modified by this procedure.
1230
1231    This procedure works alternative by alternative.  For each alternative
1232    we assume that we will be able to allocate all pseudos to their ideal
1233    register class and calculate the cost of using that alternative.  Then
1234    we compute for each operand that is a pseudo-register, the cost of 
1235    having the pseudo allocated to each register class and using it in that
1236    alternative.  To this cost is added the cost of the alternative.
1237
1238    The cost of each class for this insn is its lowest cost among all the
1239    alternatives.  */
1240
1241 static void
1242 record_reg_classes (n_alts, n_ops, ops, modes, subreg_changes_size,
1243                     constraints, insn, op_costs, reg_pref)
1244      int n_alts;
1245      int n_ops;
1246      rtx *ops;
1247      enum machine_mode *modes;
1248      char *subreg_changes_size ATTRIBUTE_UNUSED;
1249      const char **constraints;
1250      rtx insn;
1251      struct costs *op_costs;
1252      struct reg_pref *reg_pref;
1253 {
1254   int alt;
1255   int i, j;
1256   rtx set;
1257
1258   /* Process each alternative, each time minimizing an operand's cost with
1259      the cost for each operand in that alternative.  */
1260
1261   for (alt = 0; alt < n_alts; alt++)
1262     {
1263       struct costs this_op_costs[MAX_RECOG_OPERANDS];
1264       int alt_fail = 0;
1265       int alt_cost = 0;
1266       enum reg_class classes[MAX_RECOG_OPERANDS];
1267       int allows_mem[MAX_RECOG_OPERANDS];
1268       int class;
1269
1270       for (i = 0; i < n_ops; i++)
1271         {
1272           const char *p = constraints[i];
1273           rtx op = ops[i];
1274           enum machine_mode mode = modes[i];
1275           int allows_addr = 0;
1276           int win = 0;
1277           unsigned char c;
1278
1279           /* Initially show we know nothing about the register class.  */
1280           classes[i] = NO_REGS;
1281           allows_mem[i] = 0;
1282
1283           /* If this operand has no constraints at all, we can conclude 
1284              nothing about it since anything is valid.  */
1285
1286           if (*p == 0)
1287             {
1288               if (GET_CODE (op) == REG && REGNO (op) >= FIRST_PSEUDO_REGISTER)
1289                 bzero ((char *) &this_op_costs[i], sizeof this_op_costs[i]);
1290
1291               continue;
1292             }
1293
1294           /* If this alternative is only relevant when this operand
1295              matches a previous operand, we do different things depending
1296              on whether this operand is a pseudo-reg or not.  We must process
1297              any modifiers for the operand before we can make this test.  */
1298
1299           while (*p == '%' || *p == '=' || *p == '+' || *p == '&')
1300             p++;
1301
1302           if (p[0] >= '0' && p[0] <= '0' + i && (p[1] == ',' || p[1] == 0))
1303             {
1304               /* Copy class and whether memory is allowed from the matching
1305                  alternative.  Then perform any needed cost computations
1306                  and/or adjustments.  */
1307               j = p[0] - '0';
1308               classes[i] = classes[j];
1309               allows_mem[i] = allows_mem[j];
1310
1311               if (GET_CODE (op) != REG || REGNO (op) < FIRST_PSEUDO_REGISTER)
1312                 {
1313                   /* If this matches the other operand, we have no added
1314                      cost and we win.  */
1315                   if (rtx_equal_p (ops[j], op))
1316                     win = 1;
1317
1318                   /* If we can put the other operand into a register, add to
1319                      the cost of this alternative the cost to copy this
1320                      operand to the register used for the other operand.  */
1321
1322                   else if (classes[j] != NO_REGS)
1323                     alt_cost += copy_cost (op, mode, classes[j], 1), win = 1;
1324                 }
1325               else if (GET_CODE (ops[j]) != REG
1326                        || REGNO (ops[j]) < FIRST_PSEUDO_REGISTER)
1327                 {
1328                   /* This op is a pseudo but the one it matches is not.  */
1329                   
1330                   /* If we can't put the other operand into a register, this
1331                      alternative can't be used.  */
1332
1333                   if (classes[j] == NO_REGS)
1334                     alt_fail = 1;
1335
1336                   /* Otherwise, add to the cost of this alternative the cost
1337                      to copy the other operand to the register used for this
1338                      operand.  */
1339
1340                   else
1341                     alt_cost += copy_cost (ops[j], mode, classes[j], 1);
1342                 }
1343               else
1344                 {
1345                   /* The costs of this operand are not the same as the other
1346                      operand since move costs are not symmetric.  Moreover,
1347                      if we cannot tie them, this alternative needs to do a
1348                      copy, which is one instruction.  */
1349
1350                   struct costs *pp = &this_op_costs[i];
1351
1352                   for (class = 0; class < N_REG_CLASSES; class++)
1353                     pp->cost[class]
1354                       = ((recog_data.operand_type[i] != OP_OUT
1355                           ? may_move_in_cost[class][(int) classes[i]]
1356                           : 0)
1357                          + (recog_data.operand_type[i] != OP_IN
1358                             ? may_move_out_cost[(int) classes[i]][class]
1359                             : 0));
1360                   
1361                   /* If the alternative actually allows memory, make things
1362                      a bit cheaper since we won't need an extra insn to
1363                      load it.  */
1364
1365                   pp->mem_cost
1366                     = ((recog_data.operand_type[i] != OP_IN
1367                         ? MEMORY_MOVE_COST (mode, classes[i], 0)
1368                         : 0)
1369                        + (recog_data.operand_type[i] != OP_OUT
1370                           ? MEMORY_MOVE_COST (mode, classes[i], 1)
1371                           : 0) - allows_mem[i]);
1372
1373                   /* If we have assigned a class to this register in our
1374                      first pass, add a cost to this alternative corresponding
1375                      to what we would add if this register were not in the
1376                      appropriate class.  */
1377
1378                   if (reg_pref)
1379                     alt_cost
1380                       += (may_move_in_cost[(unsigned char) reg_pref[REGNO (op)].prefclass]
1381                           [(int) classes[i]]);
1382
1383                   if (REGNO (ops[i]) != REGNO (ops[j])
1384                       && ! find_reg_note (insn, REG_DEAD, op))
1385                     alt_cost += 2;
1386
1387                   /* This is in place of ordinary cost computation
1388                      for this operand, so skip to the end of the
1389                      alternative (should be just one character).  */
1390                   while (*p && *p++ != ',')
1391                     ;
1392
1393                   constraints[i] = p;
1394                   continue;
1395                 }
1396             }
1397
1398           /* Scan all the constraint letters.  See if the operand matches
1399              any of the constraints.  Collect the valid register classes
1400              and see if this operand accepts memory.  */
1401
1402           while (*p && (c = *p++) != ',')
1403             switch (c)
1404               {
1405               case '*':
1406                 /* Ignore the next letter for this pass.  */
1407                 p++;
1408                 break;
1409
1410               case '?':
1411                 alt_cost += 2;
1412               case '!':  case '#':  case '&':
1413               case '0':  case '1':  case '2':  case '3':  case '4':
1414               case '5':  case '6':  case '7':  case '8':  case '9':
1415                 break;
1416
1417               case 'p':
1418                 allows_addr = 1;
1419                 win = address_operand (op, GET_MODE (op));
1420                 /* We know this operand is an address, so we want it to be
1421                    allocated to a register that can be the base of an
1422                    address, ie BASE_REG_CLASS.  */
1423                 classes[i]
1424                   = reg_class_subunion[(int) classes[i]]
1425                     [(int) BASE_REG_CLASS];
1426                 break;
1427
1428               case 'm':  case 'o':  case 'V':
1429                 /* It doesn't seem worth distinguishing between offsettable
1430                    and non-offsettable addresses here.  */
1431                 allows_mem[i] = 1;
1432                 if (GET_CODE (op) == MEM)
1433                   win = 1;
1434                 break;
1435
1436               case '<':
1437                 if (GET_CODE (op) == MEM
1438                     && (GET_CODE (XEXP (op, 0)) == PRE_DEC
1439                         || GET_CODE (XEXP (op, 0)) == POST_DEC))
1440                   win = 1;
1441                 break;
1442
1443               case '>':
1444                 if (GET_CODE (op) == MEM
1445                     && (GET_CODE (XEXP (op, 0)) == PRE_INC
1446                         || GET_CODE (XEXP (op, 0)) == POST_INC))
1447                   win = 1;
1448                 break;
1449
1450               case 'E':
1451 #ifndef REAL_ARITHMETIC
1452                 /* Match any floating double constant, but only if
1453                    we can examine the bits of it reliably.  */
1454                 if ((HOST_FLOAT_FORMAT != TARGET_FLOAT_FORMAT
1455                      || HOST_BITS_PER_WIDE_INT != BITS_PER_WORD)
1456                     && GET_MODE (op) != VOIDmode && ! flag_pretend_float)
1457                   break;
1458 #endif
1459                 if (GET_CODE (op) == CONST_DOUBLE)
1460                   win = 1;
1461                 break;
1462
1463               case 'F':
1464                 if (GET_CODE (op) == CONST_DOUBLE)
1465                   win = 1;
1466                 break;
1467
1468               case 'G':
1469               case 'H':
1470                 if (GET_CODE (op) == CONST_DOUBLE
1471                     && CONST_DOUBLE_OK_FOR_LETTER_P (op, c))
1472                   win = 1;
1473                 break;
1474
1475               case 's':
1476                 if (GET_CODE (op) == CONST_INT
1477                     || (GET_CODE (op) == CONST_DOUBLE
1478                         && GET_MODE (op) == VOIDmode))
1479                   break;
1480               case 'i':
1481                 if (CONSTANT_P (op)
1482 #ifdef LEGITIMATE_PIC_OPERAND_P
1483                     && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))
1484 #endif
1485                     )
1486                   win = 1;
1487                 break;
1488
1489               case 'n':
1490                 if (GET_CODE (op) == CONST_INT
1491                     || (GET_CODE (op) == CONST_DOUBLE
1492                         && GET_MODE (op) == VOIDmode))
1493                   win = 1;
1494                 break;
1495
1496               case 'I':
1497               case 'J':
1498               case 'K':
1499               case 'L':
1500               case 'M':
1501               case 'N':
1502               case 'O':
1503               case 'P':
1504                 if (GET_CODE (op) == CONST_INT
1505                     && CONST_OK_FOR_LETTER_P (INTVAL (op), c))
1506                   win = 1;
1507                 break;
1508
1509               case 'X':
1510                 win = 1;
1511                 break;
1512
1513 #ifdef EXTRA_CONSTRAINT
1514               case 'Q':
1515               case 'R':
1516               case 'S':
1517               case 'T':
1518               case 'U':
1519                 if (EXTRA_CONSTRAINT (op, c))
1520                   win = 1;
1521                 break;
1522 #endif
1523
1524               case 'g':
1525                 if (GET_CODE (op) == MEM
1526                     || (CONSTANT_P (op)
1527 #ifdef LEGITIMATE_PIC_OPERAND_P
1528                         && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))
1529 #endif
1530                         ))
1531                   win = 1;
1532                 allows_mem[i] = 1;
1533               case 'r':
1534                 classes[i]
1535                   = reg_class_subunion[(int) classes[i]][(int) GENERAL_REGS];
1536                 break;
1537
1538               default:
1539                 classes[i]
1540                   = reg_class_subunion[(int) classes[i]]
1541                     [(int) REG_CLASS_FROM_LETTER (c)];
1542               }
1543
1544           constraints[i] = p;
1545
1546 #ifdef CLASS_CANNOT_CHANGE_SIZE
1547           /* If we noted a subreg earlier, and the selected class is a 
1548              subclass of CLASS_CANNOT_CHANGE_SIZE, zap it.  */
1549           if (subreg_changes_size[i]
1550               && (reg_class_subunion[(int) CLASS_CANNOT_CHANGE_SIZE]
1551                                     [(int) classes[i]]
1552                   == CLASS_CANNOT_CHANGE_SIZE))
1553             classes[i] = NO_REGS;
1554 #endif
1555
1556           /* How we account for this operand now depends on whether it is  a
1557              pseudo register or not.  If it is, we first check if any
1558              register classes are valid.  If not, we ignore this alternative,
1559              since we want to assume that all pseudos get allocated for
1560              register preferencing.  If some register class is valid, compute
1561              the costs of moving the pseudo into that class.  */
1562
1563           if (GET_CODE (op) == REG && REGNO (op) >= FIRST_PSEUDO_REGISTER)
1564             {
1565               if (classes[i] == NO_REGS)
1566                 {
1567                     /* We must always fail if the operand is a REG, but
1568                        we did not find a suitable class.
1569
1570                        Otherwise we may perform an uninitialized read
1571                        from this_op_costs after the `continue' statement
1572                        below.  */
1573                     alt_fail = 1;
1574                 }
1575               else
1576                 {
1577                   struct costs *pp = &this_op_costs[i];
1578
1579                   for (class = 0; class < N_REG_CLASSES; class++)
1580                     pp->cost[class]
1581                       = ((recog_data.operand_type[i] != OP_OUT
1582                           ? may_move_in_cost[class][(int) classes[i]]
1583                           : 0)
1584                          + (recog_data.operand_type[i] != OP_IN
1585                             ? may_move_out_cost[(int) classes[i]][class]
1586                             : 0));
1587
1588                   /* If the alternative actually allows memory, make things
1589                      a bit cheaper since we won't need an extra insn to
1590                      load it.  */
1591
1592                   pp->mem_cost
1593                     = ((recog_data.operand_type[i] != OP_IN
1594                         ? MEMORY_MOVE_COST (mode, classes[i], 0)
1595                         : 0)
1596                        + (recog_data.operand_type[i] != OP_OUT
1597                           ? MEMORY_MOVE_COST (mode, classes[i], 1)
1598                           : 0) - allows_mem[i]);
1599
1600                   /* If we have assigned a class to this register in our
1601                      first pass, add a cost to this alternative corresponding
1602                      to what we would add if this register were not in the
1603                      appropriate class.  */
1604
1605                   if (reg_pref)
1606                     alt_cost
1607                       += (may_move_in_cost[(unsigned char) reg_pref[REGNO (op)].prefclass]
1608                           [(int) classes[i]]);
1609                 }
1610             }
1611
1612           /* Otherwise, if this alternative wins, either because we
1613              have already determined that or if we have a hard register of
1614              the proper class, there is no cost for this alternative.  */
1615
1616           else if (win
1617                    || (GET_CODE (op) == REG
1618                        && reg_fits_class_p (op, classes[i], 0, GET_MODE (op))))
1619             ;
1620
1621           /* If registers are valid, the cost of this alternative includes
1622              copying the object to and/or from a register.  */
1623
1624           else if (classes[i] != NO_REGS)
1625             {
1626               if (recog_data.operand_type[i] != OP_OUT)
1627                 alt_cost += copy_cost (op, mode, classes[i], 1);
1628
1629               if (recog_data.operand_type[i] != OP_IN)
1630                 alt_cost += copy_cost (op, mode, classes[i], 0);
1631             }
1632
1633           /* The only other way this alternative can be used is if this is a
1634              constant that could be placed into memory.  */
1635
1636           else if (CONSTANT_P (op) && (allows_addr || allows_mem[i]))
1637             alt_cost += MEMORY_MOVE_COST (mode, classes[i], 1);
1638           else
1639             alt_fail = 1;
1640         }
1641
1642       if (alt_fail)
1643         continue;
1644
1645       /* Finally, update the costs with the information we've calculated
1646          about this alternative.  */
1647
1648       for (i = 0; i < n_ops; i++)
1649         if (GET_CODE (ops[i]) == REG
1650             && REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
1651           {
1652             struct costs *pp = &op_costs[i], *qq = &this_op_costs[i];
1653             int scale = 1 + (recog_data.operand_type[i] == OP_INOUT);
1654
1655             pp->mem_cost = MIN (pp->mem_cost,
1656                                 (qq->mem_cost + alt_cost) * scale);
1657
1658             for (class = 0; class < N_REG_CLASSES; class++)
1659               pp->cost[class] = MIN (pp->cost[class],
1660                                      (qq->cost[class] + alt_cost) * scale);
1661           }
1662     }
1663
1664   /* If this insn is a single set copying operand 1 to operand 0
1665      and one operand is a pseudo with the other a hard reg or a pseudo
1666      that prefers a register that is in its own register class then
1667      we may want to adjust the cost of that register class to -1.
1668  
1669      Avoid the adjustment if the source does not die to avoid stressing of
1670      register allocator by preferrencing two coliding registers into single
1671      class.
1672
1673      Also avoid the adjustment if a copy between registers of the class
1674      is expensive (ten times the cost of a default copy is considered
1675      arbitrarily expensive).  This avoids losing when the preferred class
1676      is very expensive as the source of a copy instruction.  */
1677
1678   if ((set = single_set (insn)) != 0
1679       && ops[0] == SET_DEST (set) && ops[1] == SET_SRC (set)
1680       && GET_CODE (ops[0]) == REG && GET_CODE (ops[1]) == REG
1681       && find_regno_note (insn, REG_DEAD, REGNO (ops[1])))
1682     for (i = 0; i <= 1; i++)
1683       if (REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
1684         {
1685           int regno = REGNO (ops[!i]);
1686           enum machine_mode mode = GET_MODE (ops[!i]);
1687           int class;
1688           int nr;
1689
1690           if (regno >= FIRST_PSEUDO_REGISTER && reg_pref != 0)
1691             {
1692               enum reg_class pref = reg_pref[regno].prefclass;
1693
1694               if ((reg_class_size[(unsigned char) pref]
1695                    == CLASS_MAX_NREGS (pref, mode))
1696                   && REGISTER_MOVE_COST (pref, pref) < 10 * 2)
1697                 op_costs[i].cost[(unsigned char) pref] = -1;
1698             }
1699           else if (regno < FIRST_PSEUDO_REGISTER)
1700             for (class = 0; class < N_REG_CLASSES; class++)
1701               if (TEST_HARD_REG_BIT (reg_class_contents[class], regno)
1702                   && reg_class_size[class] == CLASS_MAX_NREGS (class, mode))
1703                 {
1704                   if (reg_class_size[class] == 1)
1705                     op_costs[i].cost[class] = -1;
1706                   else
1707                     {
1708                       for (nr = 0; nr < HARD_REGNO_NREGS(regno, mode); nr++)
1709                         {
1710                           if (!TEST_HARD_REG_BIT (reg_class_contents[class], regno + nr))
1711                             break;
1712                         }
1713
1714                       if (nr == HARD_REGNO_NREGS(regno,mode))
1715                         op_costs[i].cost[class] = -1;
1716                     }
1717                 }
1718         }
1719 }
1720 \f
1721 /* Compute the cost of loading X into (if TO_P is non-zero) or from (if
1722    TO_P is zero) a register of class CLASS in mode MODE.
1723
1724    X must not be a pseudo.  */
1725
1726 static int
1727 copy_cost (x, mode, class, to_p)
1728      rtx x;
1729      enum machine_mode mode;
1730      enum reg_class class;
1731      int to_p;
1732 {
1733 #ifdef HAVE_SECONDARY_RELOADS
1734   enum reg_class secondary_class = NO_REGS;
1735 #endif
1736
1737   /* If X is a SCRATCH, there is actually nothing to move since we are
1738      assuming optimal allocation.  */
1739
1740   if (GET_CODE (x) == SCRATCH)
1741     return 0;
1742
1743   /* Get the class we will actually use for a reload.  */
1744   class = PREFERRED_RELOAD_CLASS (x, class);
1745
1746 #ifdef HAVE_SECONDARY_RELOADS
1747   /* If we need a secondary reload (we assume here that we are using 
1748      the secondary reload as an intermediate, not a scratch register), the
1749      cost is that to load the input into the intermediate register, then
1750      to copy them.  We use a special value of TO_P to avoid recursion.  */
1751
1752 #ifdef SECONDARY_INPUT_RELOAD_CLASS
1753   if (to_p == 1)
1754     secondary_class = SECONDARY_INPUT_RELOAD_CLASS (class, mode, x);
1755 #endif
1756
1757 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
1758   if (! to_p)
1759     secondary_class = SECONDARY_OUTPUT_RELOAD_CLASS (class, mode, x);
1760 #endif
1761
1762   if (secondary_class != NO_REGS)
1763     return (move_cost[(int) secondary_class][(int) class]
1764             + copy_cost (x, mode, secondary_class, 2));
1765 #endif  /* HAVE_SECONDARY_RELOADS */
1766
1767   /* For memory, use the memory move cost, for (hard) registers, use the
1768      cost to move between the register classes, and use 2 for everything
1769      else (constants).  */
1770
1771   if (GET_CODE (x) == MEM || class == NO_REGS)
1772     return MEMORY_MOVE_COST (mode, class, to_p);
1773
1774   else if (GET_CODE (x) == REG)
1775     return move_cost[(int) REGNO_REG_CLASS (REGNO (x))][(int) class];
1776
1777   else
1778     /* If this is a constant, we may eventually want to call rtx_cost here.  */
1779     return 2;
1780 }
1781 \f
1782 /* Record the pseudo registers we must reload into hard registers
1783    in a subexpression of a memory address, X.
1784
1785    CLASS is the class that the register needs to be in and is either
1786    BASE_REG_CLASS or INDEX_REG_CLASS.
1787
1788    SCALE is twice the amount to multiply the cost by (it is twice so we
1789    can represent half-cost adjustments).  */
1790
1791 static void
1792 record_address_regs (x, class, scale)
1793      rtx x;
1794      enum reg_class class;
1795      int scale;
1796 {
1797   register enum rtx_code code = GET_CODE (x);
1798
1799   switch (code)
1800     {
1801     case CONST_INT:
1802     case CONST:
1803     case CC0:
1804     case PC:
1805     case SYMBOL_REF:
1806     case LABEL_REF:
1807       return;
1808
1809     case PLUS:
1810       /* When we have an address that is a sum,
1811          we must determine whether registers are "base" or "index" regs.
1812          If there is a sum of two registers, we must choose one to be
1813          the "base".  Luckily, we can use the REGNO_POINTER_FLAG
1814          to make a good choice most of the time.  We only need to do this
1815          on machines that can have two registers in an address and where
1816          the base and index register classes are different.
1817
1818          ??? This code used to set REGNO_POINTER_FLAG in some cases, but
1819          that seems bogus since it should only be set when we are sure
1820          the register is being used as a pointer.  */
1821
1822       {
1823         rtx arg0 = XEXP (x, 0);
1824         rtx arg1 = XEXP (x, 1);
1825         register enum rtx_code code0 = GET_CODE (arg0);
1826         register enum rtx_code code1 = GET_CODE (arg1);
1827
1828         /* Look inside subregs.  */
1829         if (code0 == SUBREG)
1830           arg0 = SUBREG_REG (arg0), code0 = GET_CODE (arg0);
1831         if (code1 == SUBREG)
1832           arg1 = SUBREG_REG (arg1), code1 = GET_CODE (arg1);
1833
1834         /* If this machine only allows one register per address, it must
1835            be in the first operand.  */
1836
1837         if (MAX_REGS_PER_ADDRESS == 1)
1838           record_address_regs (arg0, class, scale);
1839
1840         /* If index and base registers are the same on this machine, just
1841            record registers in any non-constant operands.  We assume here,
1842            as well as in the tests below, that all addresses are in 
1843            canonical form.  */
1844
1845         else if (INDEX_REG_CLASS == BASE_REG_CLASS)
1846           {
1847             record_address_regs (arg0, class, scale);
1848             if (! CONSTANT_P (arg1))
1849               record_address_regs (arg1, class, scale);
1850           }
1851
1852         /* If the second operand is a constant integer, it doesn't change
1853            what class the first operand must be.  */
1854
1855         else if (code1 == CONST_INT || code1 == CONST_DOUBLE)
1856           record_address_regs (arg0, class, scale);
1857
1858         /* If the second operand is a symbolic constant, the first operand
1859            must be an index register.  */
1860
1861         else if (code1 == SYMBOL_REF || code1 == CONST || code1 == LABEL_REF)
1862           record_address_regs (arg0, INDEX_REG_CLASS, scale);
1863
1864         /* If both operands are registers but one is already a hard register
1865            of index or base class, give the other the class that the hard
1866            register is not.  */
1867
1868 #ifdef REG_OK_FOR_BASE_P
1869         else if (code0 == REG && code1 == REG
1870                  && REGNO (arg0) < FIRST_PSEUDO_REGISTER
1871                  && (REG_OK_FOR_BASE_P (arg0) || REG_OK_FOR_INDEX_P (arg0)))
1872           record_address_regs (arg1,
1873                                REG_OK_FOR_BASE_P (arg0)
1874                                ? INDEX_REG_CLASS : BASE_REG_CLASS,
1875                                scale);
1876         else if (code0 == REG && code1 == REG
1877                  && REGNO (arg1) < FIRST_PSEUDO_REGISTER
1878                  && (REG_OK_FOR_BASE_P (arg1) || REG_OK_FOR_INDEX_P (arg1)))
1879           record_address_regs (arg0,
1880                                REG_OK_FOR_BASE_P (arg1)
1881                                ? INDEX_REG_CLASS : BASE_REG_CLASS,
1882                                scale);
1883 #endif
1884
1885         /* If one operand is known to be a pointer, it must be the base
1886            with the other operand the index.  Likewise if the other operand
1887            is a MULT.  */
1888
1889         else if ((code0 == REG && REGNO_POINTER_FLAG (REGNO (arg0)))
1890                  || code1 == MULT)
1891           {
1892             record_address_regs (arg0, BASE_REG_CLASS, scale);
1893             record_address_regs (arg1, INDEX_REG_CLASS, scale);
1894           }
1895         else if ((code1 == REG && REGNO_POINTER_FLAG (REGNO (arg1)))
1896                  || code0 == MULT)
1897           {
1898             record_address_regs (arg0, INDEX_REG_CLASS, scale);
1899             record_address_regs (arg1, BASE_REG_CLASS, scale);
1900           }
1901
1902         /* Otherwise, count equal chances that each might be a base
1903            or index register.  This case should be rare.  */
1904
1905         else
1906           {
1907             record_address_regs (arg0, BASE_REG_CLASS, scale / 2);
1908             record_address_regs (arg0, INDEX_REG_CLASS, scale / 2);
1909             record_address_regs (arg1, BASE_REG_CLASS, scale / 2);
1910             record_address_regs (arg1, INDEX_REG_CLASS, scale / 2);
1911           }
1912       }
1913       break;
1914
1915     case POST_INC:
1916     case PRE_INC:
1917     case POST_DEC:
1918     case PRE_DEC:
1919       /* Double the importance of a pseudo register that is incremented
1920          or decremented, since it would take two extra insns
1921          if it ends up in the wrong place.  If the operand is a pseudo,
1922          show it is being used in an INC_DEC context.  */
1923
1924 #ifdef FORBIDDEN_INC_DEC_CLASSES
1925       if (GET_CODE (XEXP (x, 0)) == REG
1926           && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER)
1927         in_inc_dec[REGNO (XEXP (x, 0))] = 1;
1928 #endif
1929
1930       record_address_regs (XEXP (x, 0), class, 2 * scale);
1931       break;
1932
1933     case REG:
1934       {
1935         register struct costs *pp = &costs[REGNO (x)];
1936         register int i;
1937
1938         pp->mem_cost += (MEMORY_MOVE_COST (Pmode, class, 1) * scale) / 2;
1939
1940         for (i = 0; i < N_REG_CLASSES; i++)
1941           pp->cost[i] += (may_move_in_cost[i][(int) class] * scale) / 2;
1942       }
1943       break;
1944
1945     default:
1946       {
1947         register const char *fmt = GET_RTX_FORMAT (code);
1948         register int i;
1949         for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1950           if (fmt[i] == 'e')
1951             record_address_regs (XEXP (x, i), class, scale);
1952       }
1953     }
1954 }
1955 \f
1956 #ifdef FORBIDDEN_INC_DEC_CLASSES
1957
1958 /* Return 1 if REG is valid as an auto-increment memory reference
1959    to an object of MODE.  */
1960
1961 static int
1962 auto_inc_dec_reg_p (reg, mode)
1963      rtx reg;
1964      enum machine_mode mode;
1965 {
1966   if (HAVE_POST_INCREMENT
1967       && memory_address_p (mode, gen_rtx_POST_INC (Pmode, reg)))
1968     return 1;
1969
1970   if (HAVE_POST_DECREMENT
1971       && memory_address_p (mode, gen_rtx_POST_DEC (Pmode, reg)))
1972     return 1;
1973
1974   if (HAVE_PRE_INCREMENT
1975       && memory_address_p (mode, gen_rtx_PRE_INC (Pmode, reg)))
1976     return 1;
1977
1978   if (HAVE_PRE_DECREMENT
1979       && memory_address_p (mode, gen_rtx_PRE_DEC (Pmode, reg)))
1980     return 1;
1981
1982   return 0;
1983 }
1984 #endif
1985 \f
1986 static short *renumber = (short *)0;
1987 static size_t regno_allocated = 0;
1988
1989 /* Allocate enough space to hold NUM_REGS registers for the tables used for
1990    reg_scan and flow_analysis that are indexed by the register number.  If
1991    NEW_P is non zero, initialize all of the registers, otherwise only
1992    initialize the new registers allocated.  The same table is kept from
1993    function to function, only reallocating it when we need more room.  If
1994    RENUMBER_P is non zero, allocate the reg_renumber array also.  */
1995
1996 void
1997 allocate_reg_info (num_regs, new_p, renumber_p)
1998      size_t num_regs;
1999      int new_p;
2000      int renumber_p;
2001 {
2002   size_t size_info;
2003   size_t size_renumber;
2004   size_t min = (new_p) ? 0 : reg_n_max;
2005   struct reg_info_data *reg_data;
2006   struct reg_info_data *reg_next;
2007
2008   if (num_regs > regno_allocated)
2009     {
2010       size_t old_allocated = regno_allocated;
2011
2012       regno_allocated = num_regs + (num_regs / 20);     /* add some slop space */
2013       size_renumber = regno_allocated * sizeof (short);
2014
2015       if (!reg_n_info)
2016         {
2017           VARRAY_REG_INIT (reg_n_info, regno_allocated, "reg_n_info");
2018           renumber = (short *) xmalloc (size_renumber);
2019           reg_pref_buffer = (struct reg_pref *) xmalloc (regno_allocated 
2020                                               * sizeof (struct reg_pref));
2021         }
2022
2023       else
2024         {
2025           VARRAY_GROW (reg_n_info, regno_allocated);
2026
2027           if (new_p)            /* if we're zapping everything, no need to realloc */
2028             {
2029               free ((char *)renumber);
2030               free ((char *)reg_pref);
2031               renumber = (short *) xmalloc (size_renumber);
2032               reg_pref_buffer = (struct reg_pref *) xmalloc (regno_allocated 
2033                                                   * sizeof (struct reg_pref));
2034             }
2035
2036           else
2037             {
2038               renumber = (short *) xrealloc ((char *)renumber, size_renumber);
2039               reg_pref_buffer = (struct reg_pref *) xrealloc ((char *)reg_pref_buffer,
2040                                                    regno_allocated 
2041                                                    * sizeof (struct reg_pref));
2042             }
2043         }
2044
2045       size_info = (regno_allocated - old_allocated) * sizeof (reg_info)
2046         + sizeof (struct reg_info_data) - sizeof (reg_info);
2047       reg_data = (struct reg_info_data *) xcalloc (size_info, 1);
2048       reg_data->min_index = old_allocated;
2049       reg_data->max_index = regno_allocated - 1;
2050       reg_data->next = reg_info_head;
2051       reg_info_head = reg_data;
2052     }
2053
2054   reg_n_max = num_regs;
2055   if (min < num_regs)
2056     {
2057       /* Loop through each of the segments allocated for the actual
2058          reg_info pages, and set up the pointers, zero the pages, etc.  */
2059       for (reg_data = reg_info_head; reg_data; reg_data = reg_next)
2060         {
2061           size_t min_index = reg_data->min_index;
2062           size_t max_index = reg_data->max_index;
2063
2064           reg_next = reg_data->next;
2065           if (min <= max_index)
2066             {
2067               size_t max = max_index;
2068               size_t local_min = min - min_index;
2069               size_t i;
2070
2071               if (min < min_index)
2072                 local_min = 0;
2073               if (!reg_data->used_p)    /* page just allocated with calloc */
2074                 reg_data->used_p = 1;   /* no need to zero */
2075               else
2076                 bzero ((char *) &reg_data->data[local_min],
2077                        sizeof (reg_info) * (max - min_index - local_min + 1));
2078
2079               for (i = min_index+local_min; i <= max; i++)
2080                 {
2081                   VARRAY_REG (reg_n_info, i) = &reg_data->data[i-min_index];
2082                   REG_BASIC_BLOCK (i) = REG_BLOCK_UNKNOWN;
2083                   renumber[i] = -1;
2084                   reg_pref_buffer[i].prefclass = (char) NO_REGS;
2085                   reg_pref_buffer[i].altclass = (char) NO_REGS;
2086                 }
2087             }
2088         }
2089     }
2090
2091   /* If {pref,alt}class have already been allocated, update the pointers to
2092      the newly realloced ones.  */
2093   if (reg_pref)
2094     reg_pref = reg_pref_buffer;
2095
2096   if (renumber_p)
2097     reg_renumber = renumber;
2098
2099   /* Tell the regset code about the new number of registers */
2100   MAX_REGNO_REG_SET (num_regs, new_p, renumber_p);
2101 }
2102
2103 /* Free up the space allocated by allocate_reg_info.  */
2104 void
2105 free_reg_info ()
2106 {
2107   if (reg_n_info)
2108     {
2109       struct reg_info_data *reg_data;
2110       struct reg_info_data *reg_next;
2111
2112       VARRAY_FREE (reg_n_info);
2113       for (reg_data = reg_info_head; reg_data; reg_data = reg_next)
2114         {
2115           reg_next = reg_data->next;
2116           free ((char *)reg_data);
2117         }
2118
2119       free (reg_pref_buffer);
2120       reg_pref_buffer = (struct reg_pref *)0;
2121       reg_info_head = (struct reg_info_data *)0;
2122       renumber = (short *)0;
2123     }
2124   regno_allocated = 0;
2125   reg_n_max = 0;
2126 }
2127 \f
2128 /* This is the `regscan' pass of the compiler, run just before cse
2129    and again just before loop.
2130
2131    It finds the first and last use of each pseudo-register
2132    and records them in the vectors regno_first_uid, regno_last_uid
2133    and counts the number of sets in the vector reg_n_sets.
2134
2135    REPEAT is nonzero the second time this is called.  */
2136
2137 /* Maximum number of parallel sets and clobbers in any insn in this fn.
2138    Always at least 3, since the combiner could put that many together
2139    and we want this to remain correct for all the remaining passes.  */
2140
2141 int max_parallel;
2142
2143 void
2144 reg_scan (f, nregs, repeat)
2145      rtx f;
2146      int nregs;
2147      int repeat ATTRIBUTE_UNUSED;
2148 {
2149   register rtx insn;
2150
2151   allocate_reg_info (nregs, TRUE, FALSE);
2152   max_parallel = 3;
2153
2154   for (insn = f; insn; insn = NEXT_INSN (insn))
2155     if (GET_CODE (insn) == INSN
2156         || GET_CODE (insn) == CALL_INSN
2157         || GET_CODE (insn) == JUMP_INSN)
2158       {
2159         if (GET_CODE (PATTERN (insn)) == PARALLEL
2160             && XVECLEN (PATTERN (insn), 0) > max_parallel)
2161           max_parallel = XVECLEN (PATTERN (insn), 0);
2162         reg_scan_mark_refs (PATTERN (insn), insn, 0, 0);
2163
2164         if (REG_NOTES (insn))
2165           reg_scan_mark_refs (REG_NOTES (insn), insn, 1, 0);
2166       }
2167 }
2168
2169 /* Update 'regscan' information by looking at the insns
2170    from FIRST to LAST.  Some new REGs have been created,
2171    and any REG with number greater than OLD_MAX_REGNO is
2172    such a REG.  We only update information for those.  */
2173
2174 void
2175 reg_scan_update(first, last, old_max_regno)
2176      rtx first;
2177      rtx last;
2178      int old_max_regno;
2179 {
2180   register rtx insn;
2181
2182   allocate_reg_info (max_reg_num (), FALSE, FALSE);
2183
2184   for (insn = first; insn != last; insn = NEXT_INSN (insn))
2185     if (GET_CODE (insn) == INSN
2186         || GET_CODE (insn) == CALL_INSN
2187         || GET_CODE (insn) == JUMP_INSN)
2188       {
2189         if (GET_CODE (PATTERN (insn)) == PARALLEL
2190             && XVECLEN (PATTERN (insn), 0) > max_parallel)
2191           max_parallel = XVECLEN (PATTERN (insn), 0);
2192         reg_scan_mark_refs (PATTERN (insn), insn, 0, old_max_regno);
2193
2194         if (REG_NOTES (insn))
2195           reg_scan_mark_refs (REG_NOTES (insn), insn, 1, old_max_regno);
2196       }
2197 }
2198
2199 /* X is the expression to scan.  INSN is the insn it appears in.
2200    NOTE_FLAG is nonzero if X is from INSN's notes rather than its body.
2201    We should only record information for REGs with numbers
2202    greater than or equal to MIN_REGNO.  */
2203
2204 static void
2205 reg_scan_mark_refs (x, insn, note_flag, min_regno)
2206      rtx x;
2207      rtx insn;
2208      int note_flag;
2209      int min_regno;
2210 {
2211   register enum rtx_code code;
2212   register rtx dest;
2213   register rtx note;
2214
2215   code = GET_CODE (x);
2216   switch (code)
2217     {
2218     case CONST:
2219     case CONST_INT:
2220     case CONST_DOUBLE:
2221     case CC0:
2222     case PC:
2223     case SYMBOL_REF:
2224     case LABEL_REF:
2225     case ADDR_VEC:
2226     case ADDR_DIFF_VEC:
2227       return;
2228
2229     case REG:
2230       {
2231         register int regno = REGNO (x);
2232
2233         if (regno >= min_regno)
2234           {
2235             REGNO_LAST_NOTE_UID (regno) = INSN_UID (insn);
2236             if (!note_flag)
2237               REGNO_LAST_UID (regno) = INSN_UID (insn);
2238             if (REGNO_FIRST_UID (regno) == 0)
2239               REGNO_FIRST_UID (regno) = INSN_UID (insn);
2240           }
2241       }
2242       break;
2243
2244     case EXPR_LIST:
2245       if (XEXP (x, 0))
2246         reg_scan_mark_refs (XEXP (x, 0), insn, note_flag, min_regno);
2247       if (XEXP (x, 1))
2248         reg_scan_mark_refs (XEXP (x, 1), insn, note_flag, min_regno);
2249       break;
2250
2251     case INSN_LIST:
2252       if (XEXP (x, 1))
2253         reg_scan_mark_refs (XEXP (x, 1), insn, note_flag, min_regno);
2254       break;
2255
2256     case SET:
2257       /* Count a set of the destination if it is a register.  */
2258       for (dest = SET_DEST (x);
2259            GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
2260            || GET_CODE (dest) == ZERO_EXTEND;
2261            dest = XEXP (dest, 0))
2262         ;
2263
2264       if (GET_CODE (dest) == REG
2265           && REGNO (dest) >= min_regno)
2266         REG_N_SETS (REGNO (dest))++;
2267
2268       /* If this is setting a pseudo from another pseudo or the sum of a
2269          pseudo and a constant integer and the other pseudo is known to be
2270          a pointer, set the destination to be a pointer as well.
2271
2272          Likewise if it is setting the destination from an address or from a
2273          value equivalent to an address or to the sum of an address and
2274          something else.
2275                      
2276          But don't do any of this if the pseudo corresponds to a user
2277          variable since it should have already been set as a pointer based
2278          on the type.  */
2279
2280       if (GET_CODE (SET_DEST (x)) == REG
2281           && REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER
2282           && REGNO (SET_DEST (x)) >= min_regno
2283           /* If the destination pseudo is set more than once, then other
2284              sets might not be to a pointer value (consider access to a
2285              union in two threads of control in the presense of global
2286              optimizations).  So only set REGNO_POINTER_FLAG on the destination
2287              pseudo if this is the only set of that pseudo.  */
2288           && REG_N_SETS (REGNO (SET_DEST (x))) == 1
2289           && ! REG_USERVAR_P (SET_DEST (x))
2290           && ! REGNO_POINTER_FLAG (REGNO (SET_DEST (x)))
2291           && ((GET_CODE (SET_SRC (x)) == REG
2292                && REGNO_POINTER_FLAG (REGNO (SET_SRC (x))))
2293               || ((GET_CODE (SET_SRC (x)) == PLUS
2294                    || GET_CODE (SET_SRC (x)) == LO_SUM)
2295                   && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2296                   && GET_CODE (XEXP (SET_SRC (x), 0)) == REG
2297                   && REGNO_POINTER_FLAG (REGNO (XEXP (SET_SRC (x), 0))))
2298               || GET_CODE (SET_SRC (x)) == CONST
2299               || GET_CODE (SET_SRC (x)) == SYMBOL_REF
2300               || GET_CODE (SET_SRC (x)) == LABEL_REF
2301               || (GET_CODE (SET_SRC (x)) == HIGH
2302                   && (GET_CODE (XEXP (SET_SRC (x), 0)) == CONST
2303                       || GET_CODE (XEXP (SET_SRC (x), 0)) == SYMBOL_REF
2304                       || GET_CODE (XEXP (SET_SRC (x), 0)) == LABEL_REF))
2305               || ((GET_CODE (SET_SRC (x)) == PLUS
2306                    || GET_CODE (SET_SRC (x)) == LO_SUM)
2307                   && (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST
2308                       || GET_CODE (XEXP (SET_SRC (x), 1)) == SYMBOL_REF
2309                       || GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF))
2310               || ((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
2311                   && (GET_CODE (XEXP (note, 0)) == CONST
2312                       || GET_CODE (XEXP (note, 0)) == SYMBOL_REF
2313                       || GET_CODE (XEXP (note, 0)) == LABEL_REF))))
2314         REGNO_POINTER_FLAG (REGNO (SET_DEST (x))) = 1;
2315
2316       /* ... fall through ...  */
2317
2318     default:
2319       {
2320         register const char *fmt = GET_RTX_FORMAT (code);
2321         register int i;
2322         for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2323           {
2324             if (fmt[i] == 'e')
2325               reg_scan_mark_refs (XEXP (x, i), insn, note_flag, min_regno);
2326             else if (fmt[i] == 'E' && XVEC (x, i) != 0)
2327               {
2328                 register int j;
2329                 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2330                   reg_scan_mark_refs (XVECEXP (x, i, j), insn, note_flag, min_regno);
2331               }
2332           }
2333       }
2334     }
2335 }
2336 \f
2337 /* Return nonzero if C1 is a subset of C2, i.e., if every register in C1
2338    is also in C2.  */
2339
2340 int
2341 reg_class_subset_p (c1, c2)
2342      register enum reg_class c1;
2343      register enum reg_class c2;
2344 {
2345   if (c1 == c2) return 1;
2346
2347   if (c2 == ALL_REGS)
2348   win:
2349     return 1;
2350   GO_IF_HARD_REG_SUBSET (reg_class_contents[(int)c1],
2351                          reg_class_contents[(int)c2],
2352                          win);
2353   return 0;
2354 }
2355
2356 /* Return nonzero if there is a register that is in both C1 and C2.  */
2357
2358 int
2359 reg_classes_intersect_p (c1, c2)
2360      register enum reg_class c1;
2361      register enum reg_class c2;
2362 {
2363 #ifdef HARD_REG_SET
2364   register
2365 #endif
2366     HARD_REG_SET c;
2367
2368   if (c1 == c2) return 1;
2369
2370   if (c1 == ALL_REGS || c2 == ALL_REGS)
2371     return 1;
2372
2373   COPY_HARD_REG_SET (c, reg_class_contents[(int) c1]);
2374   AND_HARD_REG_SET (c, reg_class_contents[(int) c2]);
2375
2376   GO_IF_HARD_REG_SUBSET (c, reg_class_contents[(int) NO_REGS], lose);
2377   return 1;
2378
2379  lose:
2380   return 0;
2381 }
2382
2383 /* Release any memory allocated by register sets.  */
2384
2385 void
2386 regset_release_memory ()
2387 {
2388   bitmap_release_memory ();
2389 }