OSDN Git Service

2010-06-25 Jerry DeLisle <jvdelisle@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / reginfo.c
1 /* Compute different info about registers.
2    Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996
3    1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4    2009  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 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22
23 /* This file contains regscan pass of the compiler and passes for
24    dealing with info about modes of pseudo-registers inside
25    subregisters.  It also defines some tables of information about the
26    hardware registers, function init_reg_sets to initialize the
27    tables, and other auxiliary functions to deal with info about
28    registers and their classes.  */
29
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include "hard-reg-set.h"
35 #include "rtl.h"
36 #include "expr.h"
37 #include "tm_p.h"
38 #include "flags.h"
39 #include "basic-block.h"
40 #include "regs.h"
41 #include "addresses.h"
42 #include "function.h"
43 #include "insn-config.h"
44 #include "recog.h"
45 #include "reload.h"
46 #include "toplev.h"
47 #include "output.h"
48 #include "ggc.h"
49 #include "timevar.h"
50 #include "hashtab.h"
51 #include "target.h"
52 #include "tree-pass.h"
53 #include "df.h"
54 #include "ira.h"
55
56 /* Maximum register number used in this function, plus one.  */
57
58 int max_regno;
59
60 \f
61 /* Register tables used by many passes.  */
62
63 /* Indexed by hard register number, contains 1 for registers
64    that are fixed use (stack pointer, pc, frame pointer, etc.).
65    These are the registers that cannot be used to allocate
66    a pseudo reg for general use.  */
67 char fixed_regs[FIRST_PSEUDO_REGISTER];
68
69 /* Same info as a HARD_REG_SET.  */
70 HARD_REG_SET fixed_reg_set;
71
72 /* Data for initializing the above.  */
73 static const char initial_fixed_regs[] = FIXED_REGISTERS;
74
75 /* Indexed by hard register number, contains 1 for registers
76    that are fixed use or are clobbered by function calls.
77    These are the registers that cannot be used to allocate
78    a pseudo reg whose life crosses calls unless we are able
79    to save/restore them across the calls.  */
80 char call_used_regs[FIRST_PSEUDO_REGISTER];
81
82 /* Same info as a HARD_REG_SET.  */
83 HARD_REG_SET call_used_reg_set;
84
85 /* Data for initializing the above.  */
86 static const char initial_call_used_regs[] = CALL_USED_REGISTERS;
87
88 /* This is much like call_used_regs, except it doesn't have to
89    be a superset of FIXED_REGISTERS. This vector indicates
90    what is really call clobbered, and is used when defining
91    regs_invalidated_by_call.  */
92 #ifdef CALL_REALLY_USED_REGISTERS
93 char call_really_used_regs[] = CALL_REALLY_USED_REGISTERS;
94 #endif
95
96 #ifdef CALL_REALLY_USED_REGISTERS
97 #define CALL_REALLY_USED_REGNO_P(X)  call_really_used_regs[X]
98 #else
99 #define CALL_REALLY_USED_REGNO_P(X)  call_used_regs[X]
100 #endif
101
102
103 /* Contains registers that are fixed use -- i.e. in fixed_reg_set -- or
104    a function value return register or TARGET_STRUCT_VALUE_RTX or
105    STATIC_CHAIN_REGNUM.  These are the registers that cannot hold quantities
106    across calls even if we are willing to save and restore them.  */
107
108 HARD_REG_SET call_fixed_reg_set;
109
110 /* Indexed by hard register number, contains 1 for registers
111    that are being used for global register decls.
112    These must be exempt from ordinary flow analysis
113    and are also considered fixed.  */
114 char global_regs[FIRST_PSEUDO_REGISTER];
115
116 /* Contains 1 for registers that are set or clobbered by calls.  */
117 /* ??? Ideally, this would be just call_used_regs plus global_regs, but
118    for someone's bright idea to have call_used_regs strictly include
119    fixed_regs.  Which leaves us guessing as to the set of fixed_regs
120    that are actually preserved.  We know for sure that those associated
121    with the local stack frame are safe, but scant others.  */
122 HARD_REG_SET regs_invalidated_by_call;
123
124 /* Same information as REGS_INVALIDATED_BY_CALL but in regset form to be used
125    in dataflow more conveniently.  */
126 regset regs_invalidated_by_call_regset;
127
128 /* The bitmap_obstack is used to hold some static variables that
129    should not be reset after each function is compiled.  */
130 static bitmap_obstack persistent_obstack;
131
132 /* Table of register numbers in the order in which to try to use them.  */
133 #ifdef REG_ALLOC_ORDER
134 int reg_alloc_order[FIRST_PSEUDO_REGISTER] = REG_ALLOC_ORDER;
135
136 /* The inverse of reg_alloc_order.  */
137 int inv_reg_alloc_order[FIRST_PSEUDO_REGISTER];
138 #endif
139
140 /* For each reg class, a HARD_REG_SET saying which registers are in it.  */
141 HARD_REG_SET reg_class_contents[N_REG_CLASSES];
142
143 /* The same information, but as an array of unsigned ints.  We copy from
144    these unsigned ints to the table above.  We do this so the tm.h files
145    do not have to be aware of the wordsize for machines with <= 64 regs.
146    Note that we hard-code 32 here, not HOST_BITS_PER_INT.  */
147 #define N_REG_INTS  \
148   ((FIRST_PSEUDO_REGISTER + (32 - 1)) / 32)
149
150 static const unsigned int_reg_class_contents[N_REG_CLASSES][N_REG_INTS]
151   = REG_CLASS_CONTENTS;
152
153 /* For each reg class, number of regs it contains.  */
154 unsigned int reg_class_size[N_REG_CLASSES];
155
156 /* For each reg class, table listing all the classes contained in it.  */
157 enum reg_class reg_class_subclasses[N_REG_CLASSES][N_REG_CLASSES];
158
159 /* For each pair of reg classes,
160    a largest reg class contained in their union.  */
161 enum reg_class reg_class_subunion[N_REG_CLASSES][N_REG_CLASSES];
162
163 /* For each pair of reg classes,
164    the smallest reg class containing their union.  */
165 enum reg_class reg_class_superunion[N_REG_CLASSES][N_REG_CLASSES];
166
167 /* Array containing all of the register names.  */
168 const char * reg_names[] = REGISTER_NAMES;
169
170 /* Array containing all of the register class names.  */
171 const char * reg_class_names[] = REG_CLASS_NAMES;
172
173 /* For each hard register, the widest mode object that it can contain.
174    This will be a MODE_INT mode if the register can hold integers.  Otherwise
175    it will be a MODE_FLOAT or a MODE_CC mode, whichever is valid for the
176    register.  */
177 enum machine_mode reg_raw_mode[FIRST_PSEUDO_REGISTER];
178
179 /* 1 if there is a register of given mode.  */
180 bool have_regs_of_mode [MAX_MACHINE_MODE];
181
182 /* 1 if class does contain register of given mode.  */
183 char contains_reg_of_mode [N_REG_CLASSES] [MAX_MACHINE_MODE];
184
185 /* Maximum cost of moving from a register in one class to a register in
186    another class.  Based on REGISTER_MOVE_COST.  */
187 move_table *move_cost[MAX_MACHINE_MODE];
188
189 /* Similar, but here we don't have to move if the first index is a subset
190    of the second so in that case the cost is zero.  */
191 move_table *may_move_in_cost[MAX_MACHINE_MODE];
192
193 /* Similar, but here we don't have to move if the first index is a superset
194    of the second so in that case the cost is zero.  */
195 move_table *may_move_out_cost[MAX_MACHINE_MODE];
196
197 /* Keep track of the last mode we initialized move costs for.  */
198 static int last_mode_for_init_move_cost;
199
200 /* Sample MEM values for use by memory_move_secondary_cost.  */
201 static GTY(()) rtx top_of_stack[MAX_MACHINE_MODE];
202
203 /* No more global register variables may be declared; true once
204    reginfo has been initialized.  */
205 static int no_global_reg_vars = 0;
206
207 /* Specify number of hard registers given machine mode occupy.  */
208 unsigned char hard_regno_nregs[FIRST_PSEUDO_REGISTER][MAX_MACHINE_MODE];
209
210 /* Given a register bitmap, turn on the bits in a HARD_REG_SET that
211    correspond to the hard registers, if any, set in that map.  This
212    could be done far more efficiently by having all sorts of special-cases
213    with moving single words, but probably isn't worth the trouble.  */
214 void
215 reg_set_to_hard_reg_set (HARD_REG_SET *to, const_bitmap from)
216 {
217   unsigned i;
218   bitmap_iterator bi;
219
220   EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
221     {
222       if (i >= FIRST_PSEUDO_REGISTER)
223         return;
224       SET_HARD_REG_BIT (*to, i);
225     }
226 }
227
228 /* Function called only once to initialize the above data on reg usage.
229    Once this is done, various switches may override.  */
230 void
231 init_reg_sets (void)
232 {
233   int i, j;
234
235   /* First copy the register information from the initial int form into
236      the regsets.  */
237
238   for (i = 0; i < N_REG_CLASSES; i++)
239     {
240       CLEAR_HARD_REG_SET (reg_class_contents[i]);
241
242       /* Note that we hard-code 32 here, not HOST_BITS_PER_INT.  */
243       for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
244         if (int_reg_class_contents[i][j / 32]
245             & ((unsigned) 1 << (j % 32)))
246           SET_HARD_REG_BIT (reg_class_contents[i], j);
247     }
248
249   /* Sanity check: make sure the target macros FIXED_REGISTERS and
250      CALL_USED_REGISTERS had the right number of initializers.  */
251   gcc_assert (sizeof fixed_regs == sizeof initial_fixed_regs);
252   gcc_assert (sizeof call_used_regs == sizeof initial_call_used_regs);
253
254   memcpy (fixed_regs, initial_fixed_regs, sizeof fixed_regs);
255   memcpy (call_used_regs, initial_call_used_regs, sizeof call_used_regs);
256   memset (global_regs, 0, sizeof global_regs);
257 }
258
259 /* Initialize may_move_cost and friends for mode M.  */
260 void
261 init_move_cost (enum machine_mode m)
262 {
263   static unsigned short last_move_cost[N_REG_CLASSES][N_REG_CLASSES];
264   bool all_match = true;
265   unsigned int i, j;
266
267   gcc_assert (have_regs_of_mode[m]);
268   for (i = 0; i < N_REG_CLASSES; i++)
269     if (contains_reg_of_mode[i][m])
270       for (j = 0; j < N_REG_CLASSES; j++)
271         {
272           int cost;
273           if (!contains_reg_of_mode[j][m])
274             cost = 65535;
275           else
276             {
277               cost = REGISTER_MOVE_COST (m, (enum reg_class) i,
278                                          (enum reg_class) j);
279               gcc_assert (cost < 65535);
280             }
281           all_match &= (last_move_cost[i][j] == cost);
282           last_move_cost[i][j] = cost;
283         }
284   if (all_match && last_mode_for_init_move_cost != -1)
285     {
286       move_cost[m] = move_cost[last_mode_for_init_move_cost];
287       may_move_in_cost[m] = may_move_in_cost[last_mode_for_init_move_cost];
288       may_move_out_cost[m] = may_move_out_cost[last_mode_for_init_move_cost];
289       return;
290     }
291   last_mode_for_init_move_cost = m;
292   move_cost[m] = (move_table *)xmalloc (sizeof (move_table)
293                                         * N_REG_CLASSES);
294   may_move_in_cost[m] = (move_table *)xmalloc (sizeof (move_table)
295                                                * N_REG_CLASSES);
296   may_move_out_cost[m] = (move_table *)xmalloc (sizeof (move_table)
297                                                 * N_REG_CLASSES);
298   for (i = 0; i < N_REG_CLASSES; i++)
299     if (contains_reg_of_mode[i][m])
300       for (j = 0; j < N_REG_CLASSES; j++)
301         {
302           int cost;
303           enum reg_class *p1, *p2;
304
305           if (last_move_cost[i][j] == 65535)
306             {
307               move_cost[m][i][j] = 65535;
308               may_move_in_cost[m][i][j] = 65535;
309               may_move_out_cost[m][i][j] = 65535;
310             }
311           else
312             {
313               cost = last_move_cost[i][j];
314
315               for (p2 = &reg_class_subclasses[j][0];
316                    *p2 != LIM_REG_CLASSES; p2++)
317                 if (*p2 != i && contains_reg_of_mode[*p2][m])
318                   cost = MAX (cost, move_cost[m][i][*p2]);
319
320               for (p1 = &reg_class_subclasses[i][0];
321                    *p1 != LIM_REG_CLASSES; p1++)
322                 if (*p1 != j && contains_reg_of_mode[*p1][m])
323                   cost = MAX (cost, move_cost[m][*p1][j]);
324
325               gcc_assert (cost <= 65535);
326               move_cost[m][i][j] = cost;
327
328               if (reg_class_subset_p ((enum reg_class) i, (enum reg_class) j))
329                 may_move_in_cost[m][i][j] = 0;
330               else
331                 may_move_in_cost[m][i][j] = cost;
332
333               if (reg_class_subset_p ((enum reg_class) j, (enum reg_class) i))
334                 may_move_out_cost[m][i][j] = 0;
335               else
336                 may_move_out_cost[m][i][j] = cost;
337             }
338         }
339     else
340       for (j = 0; j < N_REG_CLASSES; j++)
341         {
342           move_cost[m][i][j] = 65535;
343           may_move_in_cost[m][i][j] = 65535;
344           may_move_out_cost[m][i][j] = 65535;
345         }
346 }
347
348 /* We need to save copies of some of the register information which
349    can be munged by command-line switches so we can restore it during
350    subsequent back-end reinitialization.  */
351 static char saved_fixed_regs[FIRST_PSEUDO_REGISTER];
352 static char saved_call_used_regs[FIRST_PSEUDO_REGISTER];
353 #ifdef CALL_REALLY_USED_REGISTERS
354 static char saved_call_really_used_regs[FIRST_PSEUDO_REGISTER];
355 #endif
356 static const char *saved_reg_names[FIRST_PSEUDO_REGISTER];
357
358 /* Save the register information.  */
359 void
360 save_register_info (void)
361 {
362   /* Sanity check:  make sure the target macros FIXED_REGISTERS and
363      CALL_USED_REGISTERS had the right number of initializers.  */
364   gcc_assert (sizeof fixed_regs == sizeof saved_fixed_regs);
365   gcc_assert (sizeof call_used_regs == sizeof saved_call_used_regs);
366   memcpy (saved_fixed_regs, fixed_regs, sizeof fixed_regs);
367   memcpy (saved_call_used_regs, call_used_regs, sizeof call_used_regs);
368
369   /* Likewise for call_really_used_regs.  */
370 #ifdef CALL_REALLY_USED_REGISTERS
371   gcc_assert (sizeof call_really_used_regs
372               == sizeof saved_call_really_used_regs);
373   memcpy (saved_call_really_used_regs, call_really_used_regs,
374           sizeof call_really_used_regs);
375 #endif
376
377   /* And similarly for reg_names.  */
378   gcc_assert (sizeof reg_names == sizeof saved_reg_names);
379   memcpy (saved_reg_names, reg_names, sizeof reg_names);
380 }
381
382 /* Restore the register information.  */
383 static void
384 restore_register_info (void)
385 {
386   memcpy (fixed_regs, saved_fixed_regs, sizeof fixed_regs);
387   memcpy (call_used_regs, saved_call_used_regs, sizeof call_used_regs);
388
389 #ifdef CALL_REALLY_USED_REGISTERS
390   memcpy (call_really_used_regs, saved_call_really_used_regs,
391           sizeof call_really_used_regs);
392 #endif
393
394   memcpy (reg_names, saved_reg_names, sizeof reg_names);
395 }
396
397 /* After switches have been processed, which perhaps alter
398    `fixed_regs' and `call_used_regs', convert them to HARD_REG_SETs.  */
399 static void
400 init_reg_sets_1 (void)
401 {
402   unsigned int i, j;
403   unsigned int /* enum machine_mode */ m;
404
405   restore_register_info ();
406
407 #ifdef REG_ALLOC_ORDER
408   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
409     inv_reg_alloc_order[reg_alloc_order[i]] = i;
410 #endif
411
412   /* This macro allows the fixed or call-used registers
413      and the register classes to depend on target flags.  */
414
415 #ifdef CONDITIONAL_REGISTER_USAGE
416   CONDITIONAL_REGISTER_USAGE;
417 #endif
418
419   /* Compute number of hard regs in each class.  */
420
421   memset (reg_class_size, 0, sizeof reg_class_size);
422   for (i = 0; i < N_REG_CLASSES; i++)
423     for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
424       if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
425         reg_class_size[i]++;
426
427   /* Initialize the table of subunions.
428      reg_class_subunion[I][J] gets the largest-numbered reg-class
429      that is contained in the union of classes I and J.  */
430
431   memset (reg_class_subunion, 0, sizeof reg_class_subunion);
432   for (i = 0; i < N_REG_CLASSES; i++)
433     {
434       for (j = 0; j < N_REG_CLASSES; j++)
435         {
436           HARD_REG_SET c;
437           int k;
438
439           COPY_HARD_REG_SET (c, reg_class_contents[i]);
440           IOR_HARD_REG_SET (c, reg_class_contents[j]);
441           for (k = 0; k < N_REG_CLASSES; k++)
442             if (hard_reg_set_subset_p (reg_class_contents[k], c)
443                 && !hard_reg_set_subset_p (reg_class_contents[k],
444                                           reg_class_contents
445                                           [(int) reg_class_subunion[i][j]]))
446               reg_class_subunion[i][j] = (enum reg_class) k;
447         }
448     }
449
450   /* Initialize the table of superunions.
451      reg_class_superunion[I][J] gets the smallest-numbered reg-class
452      containing the union of classes I and J.  */
453
454   memset (reg_class_superunion, 0, sizeof reg_class_superunion);
455   for (i = 0; i < N_REG_CLASSES; i++)
456     {
457       for (j = 0; j < N_REG_CLASSES; j++)
458         {
459           HARD_REG_SET c;
460           int k;
461
462           COPY_HARD_REG_SET (c, reg_class_contents[i]);
463           IOR_HARD_REG_SET (c, reg_class_contents[j]);
464           for (k = 0; k < N_REG_CLASSES; k++)
465             if (hard_reg_set_subset_p (c, reg_class_contents[k]))
466               break;
467
468           reg_class_superunion[i][j] = (enum reg_class) k;
469         }
470     }
471
472   /* Initialize the tables of subclasses and superclasses of each reg class.
473      First clear the whole table, then add the elements as they are found.  */
474
475   for (i = 0; i < N_REG_CLASSES; i++)
476     {
477       for (j = 0; j < N_REG_CLASSES; j++)
478         reg_class_subclasses[i][j] = LIM_REG_CLASSES;
479     }
480
481   for (i = 0; i < N_REG_CLASSES; i++)
482     {
483       if (i == (int) NO_REGS)
484         continue;
485
486       for (j = i + 1; j < N_REG_CLASSES; j++)
487         if (hard_reg_set_subset_p (reg_class_contents[i],
488                                   reg_class_contents[j]))
489           {
490             /* Reg class I is a subclass of J.
491                Add J to the table of superclasses of I.  */
492             enum reg_class *p;
493
494             /* Add I to the table of superclasses of J.  */
495             p = &reg_class_subclasses[j][0];
496             while (*p != LIM_REG_CLASSES) p++;
497             *p = (enum reg_class) i;
498           }
499     }
500
501   /* Initialize "constant" tables.  */
502
503   CLEAR_HARD_REG_SET (fixed_reg_set);
504   CLEAR_HARD_REG_SET (call_used_reg_set);
505   CLEAR_HARD_REG_SET (call_fixed_reg_set);
506   CLEAR_HARD_REG_SET (regs_invalidated_by_call);
507   if (!regs_invalidated_by_call_regset)
508     {
509       bitmap_obstack_initialize (&persistent_obstack);
510       regs_invalidated_by_call_regset = ALLOC_REG_SET (&persistent_obstack);
511     }
512   else
513     CLEAR_REG_SET (regs_invalidated_by_call_regset);
514
515   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
516     {
517       /* call_used_regs must include fixed_regs.  */
518       gcc_assert (!fixed_regs[i] || call_used_regs[i]);
519 #ifdef CALL_REALLY_USED_REGISTERS
520       /* call_used_regs must include call_really_used_regs.  */
521       gcc_assert (!call_really_used_regs[i] || call_used_regs[i]);
522 #endif
523
524       if (fixed_regs[i])
525         SET_HARD_REG_BIT (fixed_reg_set, i);
526
527       if (call_used_regs[i])
528         SET_HARD_REG_BIT (call_used_reg_set, i);
529
530       /* There are a couple of fixed registers that we know are safe to
531          exclude from being clobbered by calls:
532
533          The frame pointer is always preserved across calls.  The arg pointer
534          is if it is fixed.  The stack pointer usually is, unless
535          RETURN_POPS_ARGS, in which case an explicit CLOBBER will be present.
536          If we are generating PIC code, the PIC offset table register is
537          preserved across calls, though the target can override that.  */
538
539       if (i == STACK_POINTER_REGNUM)
540         ;
541       else if (global_regs[i])
542         {
543           SET_HARD_REG_BIT (regs_invalidated_by_call, i);
544           SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
545         }
546       else if (i == FRAME_POINTER_REGNUM)
547         ;
548 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
549       else if (i == HARD_FRAME_POINTER_REGNUM)
550         ;
551 #endif
552 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
553       else if (i == ARG_POINTER_REGNUM && fixed_regs[i])
554         ;
555 #endif
556 #ifndef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
557       else if (i == (unsigned) PIC_OFFSET_TABLE_REGNUM && fixed_regs[i])
558         ;
559 #endif
560       else if (CALL_REALLY_USED_REGNO_P (i))
561         {
562           SET_HARD_REG_BIT (regs_invalidated_by_call, i);
563           SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
564         }
565     }
566
567   COPY_HARD_REG_SET(call_fixed_reg_set, fixed_reg_set);
568
569   /* Preserve global registers if called more than once.  */
570   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
571     {
572       if (global_regs[i])
573         {
574           fixed_regs[i] = call_used_regs[i] = 1;
575           SET_HARD_REG_BIT (fixed_reg_set, i);
576           SET_HARD_REG_BIT (call_used_reg_set, i);
577           SET_HARD_REG_BIT (call_fixed_reg_set, i);
578         }
579     }
580
581   memset (have_regs_of_mode, 0, sizeof (have_regs_of_mode));
582   memset (contains_reg_of_mode, 0, sizeof (contains_reg_of_mode));
583   for (m = 0; m < (unsigned int) MAX_MACHINE_MODE; m++)
584     {
585       HARD_REG_SET ok_regs;
586       CLEAR_HARD_REG_SET (ok_regs);
587       for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
588         if (!fixed_regs [j] && HARD_REGNO_MODE_OK (j, (enum machine_mode) m))
589           SET_HARD_REG_BIT (ok_regs, j);
590
591       for (i = 0; i < N_REG_CLASSES; i++)
592         if (((unsigned) CLASS_MAX_NREGS ((enum reg_class) i,
593                                          (enum machine_mode) m)
594              <= reg_class_size[i])
595             && hard_reg_set_intersect_p (ok_regs, reg_class_contents[i]))
596           {
597              contains_reg_of_mode [i][m] = 1;
598              have_regs_of_mode [m] = 1;
599           }
600      }
601
602   /* Reset move_cost and friends, making sure we only free shared
603      table entries once.  */
604   for (i = 0; i < MAX_MACHINE_MODE; i++)
605     if (move_cost[i])
606       {
607         for (j = 0; j < i && move_cost[i] != move_cost[j]; j++)
608           ;
609         if (i == j)
610           {
611             free (move_cost[i]);
612             free (may_move_in_cost[i]);
613             free (may_move_out_cost[i]);
614           }
615       }
616   memset (move_cost, 0, sizeof move_cost);
617   memset (may_move_in_cost, 0, sizeof may_move_in_cost);
618   memset (may_move_out_cost, 0, sizeof may_move_out_cost);
619   last_mode_for_init_move_cost = -1;
620 }
621
622 /* Compute the table of register modes.
623    These values are used to record death information for individual registers
624    (as opposed to a multi-register mode).
625    This function might be invoked more than once, if the target has support
626    for changing register usage conventions on a per-function basis.
627 */
628 void
629 init_reg_modes_target (void)
630 {
631   int i, j;
632
633   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
634     for (j = 0; j < MAX_MACHINE_MODE; j++)
635       hard_regno_nregs[i][j] = HARD_REGNO_NREGS(i, (enum machine_mode)j);
636
637   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
638     {
639       reg_raw_mode[i] = choose_hard_reg_mode (i, 1, false);
640
641       /* If we couldn't find a valid mode, just use the previous mode.
642          ??? One situation in which we need to do this is on the mips where
643          HARD_REGNO_NREGS (fpreg, [SD]Fmode) returns 2.  Ideally we'd like
644          to use DF mode for the even registers and VOIDmode for the odd
645          (for the cpu models where the odd ones are inaccessible).  */
646       if (reg_raw_mode[i] == VOIDmode)
647         reg_raw_mode[i] = i == 0 ? word_mode : reg_raw_mode[i-1];
648     }
649 }
650
651 /* Finish initializing the register sets and initialize the register modes.
652    This function might be invoked more than once, if the target has support
653    for changing register usage conventions on a per-function basis.
654 */
655 void
656 init_regs (void)
657 {
658   /* This finishes what was started by init_reg_sets, but couldn't be done
659      until after register usage was specified.  */
660   init_reg_sets_1 ();
661 }
662
663 /* The same as previous function plus initializing IRA.  */
664 void
665 reinit_regs (void)
666 {
667   init_regs ();
668   /* caller_save needs to be re-initialized.  */
669   caller_save_initialized_p = false;
670   ira_init ();
671 }
672
673 /* Initialize some fake stack-frame MEM references for use in
674    memory_move_secondary_cost.  */
675 void
676 init_fake_stack_mems (void)
677 {
678   int i;
679
680   for (i = 0; i < MAX_MACHINE_MODE; i++)
681     top_of_stack[i] = gen_rtx_MEM ((enum machine_mode) i, stack_pointer_rtx);
682 }
683
684 /* Compute cost of moving registers to/from memory.  */
685 int
686 memory_move_cost (enum machine_mode mode, enum reg_class rclass, bool in)
687 {
688   return targetm.memory_move_cost (mode, rclass, in);
689 }
690
691 /* Compute extra cost of moving registers to/from memory due to reloads.
692    Only needed if secondary reloads are required for memory moves.  */
693 int
694 memory_move_secondary_cost (enum machine_mode mode, enum reg_class rclass,
695                             bool in)
696 {
697   enum reg_class altclass;
698   int partial_cost = 0;
699   /* We need a memory reference to feed to SECONDARY... macros.  */
700   /* mem may be unused even if the SECONDARY_ macros are defined.  */
701   rtx mem ATTRIBUTE_UNUSED = top_of_stack[(int) mode];
702
703   altclass = secondary_reload_class (in ? 1 : 0, rclass, mode, mem);
704
705   if (altclass == NO_REGS)
706     return 0;
707
708   if (in)
709     partial_cost = REGISTER_MOVE_COST (mode, altclass, rclass);
710   else
711     partial_cost = REGISTER_MOVE_COST (mode, rclass, altclass);
712
713   if (rclass == altclass)
714     /* This isn't simply a copy-to-temporary situation.  Can't guess
715        what it is, so TARGET_MEMORY_MOVE_COST really ought not to be
716        calling here in that case.
717
718        I'm tempted to put in an assert here, but returning this will
719        probably only give poor estimates, which is what we would've
720        had before this code anyways.  */
721     return partial_cost;
722
723   /* Check if the secondary reload register will also need a
724      secondary reload.  */
725   return memory_move_secondary_cost (mode, altclass, in) + partial_cost;
726 }
727
728 /* Return a machine mode that is legitimate for hard reg REGNO and large
729    enough to save nregs.  If we can't find one, return VOIDmode.
730    If CALL_SAVED is true, only consider modes that are call saved.  */
731 enum machine_mode
732 choose_hard_reg_mode (unsigned int regno ATTRIBUTE_UNUSED,
733                       unsigned int nregs, bool call_saved)
734 {
735   unsigned int /* enum machine_mode */ m;
736   enum machine_mode found_mode = VOIDmode, mode;
737
738   /* We first look for the largest integer mode that can be validly
739      held in REGNO.  If none, we look for the largest floating-point mode.
740      If we still didn't find a valid mode, try CCmode.  */
741
742   for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
743        mode != VOIDmode;
744        mode = GET_MODE_WIDER_MODE (mode))
745     if ((unsigned) hard_regno_nregs[regno][mode] == nregs
746         && HARD_REGNO_MODE_OK (regno, mode)
747         && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
748       found_mode = mode;
749
750   if (found_mode != VOIDmode)
751     return found_mode;
752
753   for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
754        mode != VOIDmode;
755        mode = GET_MODE_WIDER_MODE (mode))
756     if ((unsigned) hard_regno_nregs[regno][mode] == nregs
757         && HARD_REGNO_MODE_OK (regno, mode)
758         && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
759       found_mode = mode;
760
761   if (found_mode != VOIDmode)
762     return found_mode;
763
764   for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
765        mode != VOIDmode;
766        mode = GET_MODE_WIDER_MODE (mode))
767     if ((unsigned) hard_regno_nregs[regno][mode] == nregs
768         && HARD_REGNO_MODE_OK (regno, mode)
769         && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
770       found_mode = mode;
771
772   if (found_mode != VOIDmode)
773     return found_mode;
774
775   for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
776        mode != VOIDmode;
777        mode = GET_MODE_WIDER_MODE (mode))
778     if ((unsigned) hard_regno_nregs[regno][mode] == nregs
779         && HARD_REGNO_MODE_OK (regno, mode)
780         && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
781       found_mode = mode;
782
783   if (found_mode != VOIDmode)
784     return found_mode;
785
786   /* Iterate over all of the CCmodes.  */
787   for (m = (unsigned int) CCmode; m < (unsigned int) NUM_MACHINE_MODES; ++m)
788     {
789       mode = (enum machine_mode) m;
790       if ((unsigned) hard_regno_nregs[regno][mode] == nregs
791           && HARD_REGNO_MODE_OK (regno, mode)
792           && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
793         return mode;
794     }
795
796   /* We can't find a mode valid for this register.  */
797   return VOIDmode;
798 }
799
800 /* Specify the usage characteristics of the register named NAME.
801    It should be a fixed register if FIXED and a
802    call-used register if CALL_USED.  */
803 void
804 fix_register (const char *name, int fixed, int call_used)
805 {
806   int i;
807
808   /* Decode the name and update the primary form of
809      the register info.  */
810
811   if ((i = decode_reg_name (name)) >= 0)
812     {
813       if ((i == STACK_POINTER_REGNUM
814 #ifdef HARD_FRAME_POINTER_REGNUM
815            || i == HARD_FRAME_POINTER_REGNUM
816 #else
817            || i == FRAME_POINTER_REGNUM
818 #endif
819            )
820           && (fixed == 0 || call_used == 0))
821         {
822           static const char * const what_option[2][2] = {
823             { "call-saved", "call-used" },
824             { "no-such-option", "fixed" }};
825
826           error ("can't use '%s' as a %s register", name,
827                  what_option[fixed][call_used]);
828         }
829       else
830         {
831           fixed_regs[i] = fixed;
832           call_used_regs[i] = call_used;
833 #ifdef CALL_REALLY_USED_REGISTERS
834           if (fixed == 0)
835             call_really_used_regs[i] = call_used;
836 #endif
837         }
838     }
839   else
840     {
841       warning (0, "unknown register name: %s", name);
842     }
843 }
844
845 /* Mark register number I as global.  */
846 void
847 globalize_reg (int i)
848 {
849   if (fixed_regs[i] == 0 && no_global_reg_vars)
850     error ("global register variable follows a function definition");
851
852   if (global_regs[i])
853     {
854       warning (0, "register used for two global register variables");
855       return;
856     }
857
858   if (call_used_regs[i] && ! fixed_regs[i])
859     warning (0, "call-clobbered register used for global register variable");
860
861   global_regs[i] = 1;
862
863   /* If we're globalizing the frame pointer, we need to set the
864      appropriate regs_invalidated_by_call bit, even if it's already
865      set in fixed_regs.  */
866   if (i != STACK_POINTER_REGNUM)
867     {
868       SET_HARD_REG_BIT (regs_invalidated_by_call, i);
869       SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
870     }
871
872   /* If already fixed, nothing else to do.  */
873   if (fixed_regs[i])
874     return;
875
876   fixed_regs[i] = call_used_regs[i] = 1;
877 #ifdef CALL_REALLY_USED_REGISTERS
878   call_really_used_regs[i] = 1;
879 #endif
880
881   SET_HARD_REG_BIT (fixed_reg_set, i);
882   SET_HARD_REG_BIT (call_used_reg_set, i);
883   SET_HARD_REG_BIT (call_fixed_reg_set, i);
884
885   reinit_regs ();
886 }
887 \f
888
889 /* Structure used to record preferences of given pseudo.  */
890 struct reg_pref
891 {
892   /* (enum reg_class) prefclass is the preferred class.  May be
893      NO_REGS if no class is better than memory.  */
894   char prefclass;
895
896   /* altclass is a register class that we should use for allocating
897      pseudo if no register in the preferred class is available.
898      If no register in this class is available, memory is preferred.
899
900      It might appear to be more general to have a bitmask of classes here,
901      but since it is recommended that there be a class corresponding to the
902      union of most major pair of classes, that generality is not required.  */
903   char altclass;
904
905   /* coverclass is a register class that IRA uses for allocating
906      the pseudo.  */
907   char coverclass;
908 };
909
910 /* Record preferences of each pseudo.  This is available after RA is
911    run.  */
912 static struct reg_pref *reg_pref;
913
914 /* Current size of reg_info.  */
915 static int reg_info_size;
916
917 /* Return the reg_class in which pseudo reg number REGNO is best allocated.
918    This function is sometimes called before the info has been computed.
919    When that happens, just return GENERAL_REGS, which is innocuous.  */
920 enum reg_class
921 reg_preferred_class (int regno)
922 {
923   if (reg_pref == 0)
924     return GENERAL_REGS;
925
926   return (enum reg_class) reg_pref[regno].prefclass;
927 }
928
929 enum reg_class
930 reg_alternate_class (int regno)
931 {
932   if (reg_pref == 0)
933     return ALL_REGS;
934
935   return (enum reg_class) reg_pref[regno].altclass;
936 }
937
938 /* Return the reg_class which is used by IRA for its allocation.  */
939 enum reg_class
940 reg_cover_class (int regno)
941 {
942   if (reg_pref == 0)
943     return NO_REGS;
944
945   return (enum reg_class) reg_pref[regno].coverclass;
946 }
947
948 \f
949
950 /* Allocate space for reg info.  */
951 static void
952 allocate_reg_info (void)
953 {
954   reg_info_size = max_reg_num ();
955   gcc_assert (! reg_pref && ! reg_renumber);
956   reg_renumber = XNEWVEC (short, reg_info_size);
957   reg_pref = XCNEWVEC (struct reg_pref, reg_info_size);
958   memset (reg_renumber, -1, reg_info_size * sizeof (short));
959 }
960
961
962 /* Resize reg info. The new elements will be uninitialized.  Return
963    TRUE if new elements (for new pseudos) were added.  */
964 bool
965 resize_reg_info (void)
966 {
967   int old;
968
969   if (reg_pref == NULL)
970     {
971       allocate_reg_info ();
972       return true;
973     }
974   if (reg_info_size == max_reg_num ())
975     return false;
976   old = reg_info_size;
977   reg_info_size = max_reg_num ();
978   gcc_assert (reg_pref && reg_renumber);
979   reg_renumber = XRESIZEVEC (short, reg_renumber, reg_info_size);
980   reg_pref = XRESIZEVEC (struct reg_pref, reg_pref, reg_info_size);
981   memset (reg_pref + old, -1,
982           (reg_info_size - old) * sizeof (struct reg_pref));
983   memset (reg_renumber + old, -1, (reg_info_size - old) * sizeof (short));
984   return true;
985 }
986
987
988 /* Free up the space allocated by allocate_reg_info.  */
989 void
990 free_reg_info (void)
991 {
992   if (reg_pref)
993     {
994       free (reg_pref);
995       reg_pref = NULL;
996     }
997
998   if (reg_renumber)
999     {
1000       free (reg_renumber);
1001       reg_renumber = NULL;
1002     }
1003 }
1004
1005 /* Initialize some global data for this pass.  */
1006 static unsigned int
1007 reginfo_init (void)
1008 {
1009   if (df)
1010     df_compute_regs_ever_live (true);
1011
1012   /* This prevents dump_flow_info from losing if called
1013      before reginfo is run.  */
1014   reg_pref = NULL;
1015   /* No more global register variables may be declared.  */
1016   no_global_reg_vars = 1;
1017   return 1;
1018 }
1019
1020 struct rtl_opt_pass pass_reginfo_init =
1021 {
1022  {
1023   RTL_PASS,
1024   "reginfo",                            /* name */
1025   NULL,                                 /* gate */
1026   reginfo_init,                         /* execute */
1027   NULL,                                 /* sub */
1028   NULL,                                 /* next */
1029   0,                                    /* static_pass_number */
1030   TV_NONE,                                    /* tv_id */
1031   0,                                    /* properties_required */
1032   0,                                    /* properties_provided */
1033   0,                                    /* properties_destroyed */
1034   0,                                    /* todo_flags_start */
1035   0                                     /* todo_flags_finish */
1036  }
1037 };
1038
1039 \f
1040
1041 /* Set up preferred, alternate, and cover classes for REGNO as
1042    PREFCLASS, ALTCLASS, and COVERCLASS.  */
1043 void
1044 setup_reg_classes (int regno,
1045                    enum reg_class prefclass, enum reg_class altclass,
1046                    enum reg_class coverclass)
1047 {
1048   if (reg_pref == NULL)
1049     return;
1050   gcc_assert (reg_info_size == max_reg_num ());
1051   reg_pref[regno].prefclass = prefclass;
1052   reg_pref[regno].altclass = altclass;
1053   reg_pref[regno].coverclass = coverclass;
1054 }
1055
1056 \f
1057 /* This is the `regscan' pass of the compiler, run just before cse and
1058    again just before loop.  It finds the first and last use of each
1059    pseudo-register.  */
1060
1061 static void reg_scan_mark_refs (rtx, rtx);
1062
1063 void
1064 reg_scan (rtx f, unsigned int nregs ATTRIBUTE_UNUSED)
1065 {
1066   rtx insn;
1067
1068   timevar_push (TV_REG_SCAN);
1069
1070   for (insn = f; insn; insn = NEXT_INSN (insn))
1071     if (INSN_P (insn))
1072       {
1073         reg_scan_mark_refs (PATTERN (insn), insn);
1074         if (REG_NOTES (insn))
1075           reg_scan_mark_refs (REG_NOTES (insn), insn);
1076       }
1077
1078   timevar_pop (TV_REG_SCAN);
1079 }
1080
1081
1082 /* X is the expression to scan.  INSN is the insn it appears in.
1083    NOTE_FLAG is nonzero if X is from INSN's notes rather than its body.
1084    We should only record information for REGs with numbers
1085    greater than or equal to MIN_REGNO.  */
1086 static void
1087 reg_scan_mark_refs (rtx x, rtx insn)
1088 {
1089   enum rtx_code code;
1090   rtx dest;
1091   rtx note;
1092
1093   if (!x)
1094     return;
1095   code = GET_CODE (x);
1096   switch (code)
1097     {
1098     case CONST:
1099     case CONST_INT:
1100     case CONST_DOUBLE:
1101     case CONST_FIXED:
1102     case CONST_VECTOR:
1103     case CC0:
1104     case PC:
1105     case SYMBOL_REF:
1106     case LABEL_REF:
1107     case ADDR_VEC:
1108     case ADDR_DIFF_VEC:
1109     case REG:
1110       return;
1111
1112     case EXPR_LIST:
1113       if (XEXP (x, 0))
1114         reg_scan_mark_refs (XEXP (x, 0), insn);
1115       if (XEXP (x, 1))
1116         reg_scan_mark_refs (XEXP (x, 1), insn);
1117       break;
1118
1119     case INSN_LIST:
1120       if (XEXP (x, 1))
1121         reg_scan_mark_refs (XEXP (x, 1), insn);
1122       break;
1123
1124     case CLOBBER:
1125       if (MEM_P (XEXP (x, 0)))
1126         reg_scan_mark_refs (XEXP (XEXP (x, 0), 0), insn);
1127       break;
1128
1129     case SET:
1130       /* Count a set of the destination if it is a register.  */
1131       for (dest = SET_DEST (x);
1132            GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
1133            || GET_CODE (dest) == ZERO_EXTEND;
1134            dest = XEXP (dest, 0))
1135         ;
1136
1137       /* If this is setting a pseudo from another pseudo or the sum of a
1138          pseudo and a constant integer and the other pseudo is known to be
1139          a pointer, set the destination to be a pointer as well.
1140
1141          Likewise if it is setting the destination from an address or from a
1142          value equivalent to an address or to the sum of an address and
1143          something else.
1144
1145          But don't do any of this if the pseudo corresponds to a user
1146          variable since it should have already been set as a pointer based
1147          on the type.  */
1148
1149       if (REG_P (SET_DEST (x))
1150           && REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER
1151           /* If the destination pseudo is set more than once, then other
1152              sets might not be to a pointer value (consider access to a
1153              union in two threads of control in the presence of global
1154              optimizations).  So only set REG_POINTER on the destination
1155              pseudo if this is the only set of that pseudo.  */
1156           && DF_REG_DEF_COUNT (REGNO (SET_DEST (x))) == 1
1157           && ! REG_USERVAR_P (SET_DEST (x))
1158           && ! REG_POINTER (SET_DEST (x))
1159           && ((REG_P (SET_SRC (x))
1160                && REG_POINTER (SET_SRC (x)))
1161               || ((GET_CODE (SET_SRC (x)) == PLUS
1162                    || GET_CODE (SET_SRC (x)) == LO_SUM)
1163                   && CONST_INT_P (XEXP (SET_SRC (x), 1))
1164                   && REG_P (XEXP (SET_SRC (x), 0))
1165                   && REG_POINTER (XEXP (SET_SRC (x), 0)))
1166               || GET_CODE (SET_SRC (x)) == CONST
1167               || GET_CODE (SET_SRC (x)) == SYMBOL_REF
1168               || GET_CODE (SET_SRC (x)) == LABEL_REF
1169               || (GET_CODE (SET_SRC (x)) == HIGH
1170                   && (GET_CODE (XEXP (SET_SRC (x), 0)) == CONST
1171                       || GET_CODE (XEXP (SET_SRC (x), 0)) == SYMBOL_REF
1172                       || GET_CODE (XEXP (SET_SRC (x), 0)) == LABEL_REF))
1173               || ((GET_CODE (SET_SRC (x)) == PLUS
1174                    || GET_CODE (SET_SRC (x)) == LO_SUM)
1175                   && (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST
1176                       || GET_CODE (XEXP (SET_SRC (x), 1)) == SYMBOL_REF
1177                       || GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF))
1178               || ((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
1179                   && (GET_CODE (XEXP (note, 0)) == CONST
1180                       || GET_CODE (XEXP (note, 0)) == SYMBOL_REF
1181                       || GET_CODE (XEXP (note, 0)) == LABEL_REF))))
1182         REG_POINTER (SET_DEST (x)) = 1;
1183
1184       /* If this is setting a register from a register or from a simple
1185          conversion of a register, propagate REG_EXPR.  */
1186       if (REG_P (dest) && !REG_ATTRS (dest))
1187         {
1188           rtx src = SET_SRC (x);
1189
1190           while (GET_CODE (src) == SIGN_EXTEND
1191                  || GET_CODE (src) == ZERO_EXTEND
1192                  || GET_CODE (src) == TRUNCATE
1193                  || (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)))
1194             src = XEXP (src, 0);
1195
1196           set_reg_attrs_from_value (dest, src);
1197         }
1198
1199       /* ... fall through ...  */
1200
1201     default:
1202       {
1203         const char *fmt = GET_RTX_FORMAT (code);
1204         int i;
1205         for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1206           {
1207             if (fmt[i] == 'e')
1208               reg_scan_mark_refs (XEXP (x, i), insn);
1209             else if (fmt[i] == 'E' && XVEC (x, i) != 0)
1210               {
1211                 int j;
1212                 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1213                   reg_scan_mark_refs (XVECEXP (x, i, j), insn);
1214               }
1215           }
1216       }
1217     }
1218 }
1219 \f
1220
1221 /* Return nonzero if C1 is a subset of C2, i.e., if every register in C1
1222    is also in C2.  */
1223 int
1224 reg_class_subset_p (enum reg_class c1, enum reg_class c2)
1225 {
1226   return (c1 == c2
1227           || c2 == ALL_REGS
1228           || hard_reg_set_subset_p (reg_class_contents[(int) c1],
1229                                    reg_class_contents[(int) c2]));
1230 }
1231
1232 /* Return nonzero if there is a register that is in both C1 and C2.  */
1233 int
1234 reg_classes_intersect_p (enum reg_class c1, enum reg_class c2)
1235 {
1236   return (c1 == c2
1237           || c1 == ALL_REGS
1238           || c2 == ALL_REGS
1239           || hard_reg_set_intersect_p (reg_class_contents[(int) c1],
1240                                       reg_class_contents[(int) c2]));
1241 }
1242
1243 \f
1244
1245 /* Passes for keeping and updating info about modes of registers
1246    inside subregisters.  */
1247
1248 #ifdef CANNOT_CHANGE_MODE_CLASS
1249
1250 struct subregs_of_mode_node
1251 {
1252   unsigned int block;
1253   unsigned char modes[MAX_MACHINE_MODE];
1254 };
1255
1256 static htab_t subregs_of_mode;
1257
1258 static hashval_t
1259 som_hash (const void *x)
1260 {
1261   const struct subregs_of_mode_node *const a =
1262     (const struct subregs_of_mode_node *) x;
1263   return a->block;
1264 }
1265
1266 static int
1267 som_eq (const void *x, const void *y)
1268 {
1269   const struct subregs_of_mode_node *const a =
1270     (const struct subregs_of_mode_node *) x;
1271   const struct subregs_of_mode_node *const b =
1272     (const struct subregs_of_mode_node *) y;
1273   return a->block == b->block;
1274 }
1275
1276 static void
1277 record_subregs_of_mode (rtx subreg)
1278 {
1279   struct subregs_of_mode_node dummy, *node;
1280   enum machine_mode mode;
1281   unsigned int regno;
1282   void **slot;
1283
1284   if (!REG_P (SUBREG_REG (subreg)))
1285     return;
1286
1287   regno = REGNO (SUBREG_REG (subreg));
1288   mode = GET_MODE (subreg);
1289
1290   if (regno < FIRST_PSEUDO_REGISTER)
1291     return;
1292
1293   dummy.block = regno & -8;
1294   slot = htab_find_slot_with_hash (subregs_of_mode, &dummy,
1295                                    dummy.block, INSERT);
1296   node = (struct subregs_of_mode_node *) *slot;
1297   if (node == NULL)
1298     {
1299       node = XCNEW (struct subregs_of_mode_node);
1300       node->block = regno & -8;
1301       *slot = node;
1302     }
1303
1304   node->modes[mode] |= 1 << (regno & 7);
1305 }
1306
1307 /* Call record_subregs_of_mode for all the subregs in X.  */
1308 static void
1309 find_subregs_of_mode (rtx x)
1310 {
1311   enum rtx_code code = GET_CODE (x);
1312   const char * const fmt = GET_RTX_FORMAT (code);
1313   int i;
1314
1315   if (code == SUBREG)
1316     record_subregs_of_mode (x);
1317
1318   /* Time for some deep diving.  */
1319   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1320     {
1321       if (fmt[i] == 'e')
1322         find_subregs_of_mode (XEXP (x, i));
1323       else if (fmt[i] == 'E')
1324         {
1325           int j;
1326           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1327             find_subregs_of_mode (XVECEXP (x, i, j));
1328         }
1329     }
1330 }
1331
1332 void
1333 init_subregs_of_mode (void)
1334 {
1335   basic_block bb;
1336   rtx insn;
1337
1338   if (subregs_of_mode)
1339     htab_empty (subregs_of_mode);
1340   else
1341     subregs_of_mode = htab_create (100, som_hash, som_eq, free);
1342
1343   FOR_EACH_BB (bb)
1344     FOR_BB_INSNS (bb, insn)
1345     if (INSN_P (insn))
1346       find_subregs_of_mode (PATTERN (insn));
1347 }
1348
1349 /* Return 1 if REGNO has had an invalid mode change in CLASS from FROM
1350    mode.  */
1351 bool
1352 invalid_mode_change_p (unsigned int regno,
1353                        enum reg_class rclass ATTRIBUTE_UNUSED,
1354                        enum machine_mode from)
1355 {
1356   struct subregs_of_mode_node dummy, *node;
1357   unsigned int to;
1358   unsigned char mask;
1359
1360   gcc_assert (subregs_of_mode);
1361   dummy.block = regno & -8;
1362   node = (struct subregs_of_mode_node *)
1363     htab_find_with_hash (subregs_of_mode, &dummy, dummy.block);
1364   if (node == NULL)
1365     return false;
1366
1367   mask = 1 << (regno & 7);
1368   for (to = VOIDmode; to < NUM_MACHINE_MODES; to++)
1369     if (node->modes[to] & mask)
1370       if (CANNOT_CHANGE_MODE_CLASS (from, (enum machine_mode) to, rclass))
1371         return true;
1372
1373   return false;
1374 }
1375
1376 void
1377 finish_subregs_of_mode (void)
1378 {
1379   htab_delete (subregs_of_mode);
1380   subregs_of_mode = 0;
1381 }
1382 #else
1383 void
1384 init_subregs_of_mode (void)
1385 {
1386 }
1387 void
1388 finish_subregs_of_mode (void)
1389 {
1390 }
1391
1392 #endif /* CANNOT_CHANGE_MODE_CLASS */
1393
1394 #include "gt-reginfo.h"