OSDN Git Service

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