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