OSDN Git Service

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