2011-11-16 Matthew Gretton-Dann <matthew.gretton-dann@arm.com>
[official-gcc.git] / gcc / reginfo.c
blobe9bf65f300342cb16c4db2b20dc1324c7ef3bd79
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.
6 This file is part of GCC.
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.
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.
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/>. */
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. */
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 "timevar.h"
49 #include "hashtab.h"
50 #include "target.h"
51 #include "tree-pass.h"
52 #include "df.h"
53 #include "ira.h"
55 /* Maximum register number used in this function, plus one. */
57 int max_regno;
60 struct target_hard_regs default_target_hard_regs;
61 struct target_regs default_target_regs;
62 #if SWITCHABLE_TARGET
63 struct target_hard_regs *this_target_hard_regs = &default_target_hard_regs;
64 struct target_regs *this_target_regs = &default_target_regs;
65 #endif
67 /* Data for initializing fixed_regs. */
68 static const char initial_fixed_regs[] = FIXED_REGISTERS;
70 /* Data for initializing call_used_regs. */
71 static const char initial_call_used_regs[] = CALL_USED_REGISTERS;
73 #ifdef CALL_REALLY_USED_REGISTERS
74 /* Data for initializing call_really_used_regs. */
75 static const char initial_call_really_used_regs[] = CALL_REALLY_USED_REGISTERS;
76 #endif
78 #ifdef CALL_REALLY_USED_REGISTERS
79 #define CALL_REALLY_USED_REGNO_P(X) call_really_used_regs[X]
80 #else
81 #define CALL_REALLY_USED_REGNO_P(X) call_used_regs[X]
82 #endif
84 /* Indexed by hard register number, contains 1 for registers
85 that are being used for global register decls.
86 These must be exempt from ordinary flow analysis
87 and are also considered fixed. */
88 char global_regs[FIRST_PSEUDO_REGISTER];
90 /* Declaration for the global register. */
91 static tree GTY(()) global_regs_decl[FIRST_PSEUDO_REGISTER];
93 /* Same information as REGS_INVALIDATED_BY_CALL but in regset form to be used
94 in dataflow more conveniently. */
95 regset regs_invalidated_by_call_regset;
97 /* Same information as FIXED_REG_SET but in regset form. */
98 regset fixed_reg_set_regset;
100 /* The bitmap_obstack is used to hold some static variables that
101 should not be reset after each function is compiled. */
102 static bitmap_obstack persistent_obstack;
104 /* Used to initialize reg_alloc_order. */
105 #ifdef REG_ALLOC_ORDER
106 static int initial_reg_alloc_order[FIRST_PSEUDO_REGISTER] = REG_ALLOC_ORDER;
107 #endif
109 /* The same information, but as an array of unsigned ints. We copy from
110 these unsigned ints to the table above. We do this so the tm.h files
111 do not have to be aware of the wordsize for machines with <= 64 regs.
112 Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
113 #define N_REG_INTS \
114 ((FIRST_PSEUDO_REGISTER + (32 - 1)) / 32)
116 static const unsigned int_reg_class_contents[N_REG_CLASSES][N_REG_INTS]
117 = REG_CLASS_CONTENTS;
119 /* Array containing all of the register names. */
120 static const char *const initial_reg_names[] = REGISTER_NAMES;
122 /* Array containing all of the register class names. */
123 const char * reg_class_names[] = REG_CLASS_NAMES;
125 #define last_mode_for_init_move_cost \
126 (this_target_regs->x_last_mode_for_init_move_cost)
128 /* No more global register variables may be declared; true once
129 reginfo has been initialized. */
130 static int no_global_reg_vars = 0;
132 /* Given a register bitmap, turn on the bits in a HARD_REG_SET that
133 correspond to the hard registers, if any, set in that map. This
134 could be done far more efficiently by having all sorts of special-cases
135 with moving single words, but probably isn't worth the trouble. */
136 void
137 reg_set_to_hard_reg_set (HARD_REG_SET *to, const_bitmap from)
139 unsigned i;
140 bitmap_iterator bi;
142 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
144 if (i >= FIRST_PSEUDO_REGISTER)
145 return;
146 SET_HARD_REG_BIT (*to, i);
150 /* Function called only once per target_globals to initialize the
151 target_hard_regs structure. Once this is done, various switches
152 may override. */
153 void
154 init_reg_sets (void)
156 int i, j;
158 /* First copy the register information from the initial int form into
159 the regsets. */
161 for (i = 0; i < N_REG_CLASSES; i++)
163 CLEAR_HARD_REG_SET (reg_class_contents[i]);
165 /* Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
166 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
167 if (int_reg_class_contents[i][j / 32]
168 & ((unsigned) 1 << (j % 32)))
169 SET_HARD_REG_BIT (reg_class_contents[i], j);
172 /* Sanity check: make sure the target macros FIXED_REGISTERS and
173 CALL_USED_REGISTERS had the right number of initializers. */
174 gcc_assert (sizeof fixed_regs == sizeof initial_fixed_regs);
175 gcc_assert (sizeof call_used_regs == sizeof initial_call_used_regs);
176 #ifdef CALL_REALLY_USED_REGISTERS
177 gcc_assert (sizeof call_really_used_regs
178 == sizeof initial_call_really_used_regs);
179 #endif
180 #ifdef REG_ALLOC_ORDER
181 gcc_assert (sizeof reg_alloc_order == sizeof initial_reg_alloc_order);
182 #endif
183 gcc_assert (sizeof reg_names == sizeof initial_reg_names);
185 memcpy (fixed_regs, initial_fixed_regs, sizeof fixed_regs);
186 memcpy (call_used_regs, initial_call_used_regs, sizeof call_used_regs);
187 #ifdef CALL_REALLY_USED_REGISTERS
188 memcpy (call_really_used_regs, initial_call_really_used_regs,
189 sizeof call_really_used_regs);
190 #endif
191 #ifdef REG_ALLOC_ORDER
192 memcpy (reg_alloc_order, initial_reg_alloc_order, sizeof reg_alloc_order);
193 #endif
194 memcpy (reg_names, initial_reg_names, sizeof reg_names);
197 /* Initialize may_move_cost and friends for mode M. */
198 void
199 init_move_cost (enum machine_mode m)
201 static unsigned short last_move_cost[N_REG_CLASSES][N_REG_CLASSES];
202 bool all_match = true;
203 unsigned int i, j;
205 gcc_assert (have_regs_of_mode[m]);
206 for (i = 0; i < N_REG_CLASSES; i++)
207 if (contains_reg_of_mode[i][m])
208 for (j = 0; j < N_REG_CLASSES; j++)
210 int cost;
211 if (!contains_reg_of_mode[j][m])
212 cost = 65535;
213 else
215 cost = register_move_cost (m, (enum reg_class) i,
216 (enum reg_class) j);
217 gcc_assert (cost < 65535);
219 all_match &= (last_move_cost[i][j] == cost);
220 last_move_cost[i][j] = cost;
222 if (all_match && last_mode_for_init_move_cost != -1)
224 move_cost[m] = move_cost[last_mode_for_init_move_cost];
225 may_move_in_cost[m] = may_move_in_cost[last_mode_for_init_move_cost];
226 may_move_out_cost[m] = may_move_out_cost[last_mode_for_init_move_cost];
227 return;
229 last_mode_for_init_move_cost = m;
230 move_cost[m] = (move_table *)xmalloc (sizeof (move_table)
231 * N_REG_CLASSES);
232 may_move_in_cost[m] = (move_table *)xmalloc (sizeof (move_table)
233 * N_REG_CLASSES);
234 may_move_out_cost[m] = (move_table *)xmalloc (sizeof (move_table)
235 * N_REG_CLASSES);
236 for (i = 0; i < N_REG_CLASSES; i++)
237 if (contains_reg_of_mode[i][m])
238 for (j = 0; j < N_REG_CLASSES; j++)
240 int cost;
241 enum reg_class *p1, *p2;
243 if (last_move_cost[i][j] == 65535)
245 move_cost[m][i][j] = 65535;
246 may_move_in_cost[m][i][j] = 65535;
247 may_move_out_cost[m][i][j] = 65535;
249 else
251 cost = last_move_cost[i][j];
253 for (p2 = &reg_class_subclasses[j][0];
254 *p2 != LIM_REG_CLASSES; p2++)
255 if (*p2 != i && contains_reg_of_mode[*p2][m])
256 cost = MAX (cost, move_cost[m][i][*p2]);
258 for (p1 = &reg_class_subclasses[i][0];
259 *p1 != LIM_REG_CLASSES; p1++)
260 if (*p1 != j && contains_reg_of_mode[*p1][m])
261 cost = MAX (cost, move_cost[m][*p1][j]);
263 gcc_assert (cost <= 65535);
264 move_cost[m][i][j] = cost;
266 if (reg_class_subset_p ((enum reg_class) i, (enum reg_class) j))
267 may_move_in_cost[m][i][j] = 0;
268 else
269 may_move_in_cost[m][i][j] = cost;
271 if (reg_class_subset_p ((enum reg_class) j, (enum reg_class) i))
272 may_move_out_cost[m][i][j] = 0;
273 else
274 may_move_out_cost[m][i][j] = cost;
277 else
278 for (j = 0; j < N_REG_CLASSES; j++)
280 move_cost[m][i][j] = 65535;
281 may_move_in_cost[m][i][j] = 65535;
282 may_move_out_cost[m][i][j] = 65535;
286 /* We need to save copies of some of the register information which
287 can be munged by command-line switches so we can restore it during
288 subsequent back-end reinitialization. */
289 static char saved_fixed_regs[FIRST_PSEUDO_REGISTER];
290 static char saved_call_used_regs[FIRST_PSEUDO_REGISTER];
291 #ifdef CALL_REALLY_USED_REGISTERS
292 static char saved_call_really_used_regs[FIRST_PSEUDO_REGISTER];
293 #endif
294 static const char *saved_reg_names[FIRST_PSEUDO_REGISTER];
296 /* Save the register information. */
297 void
298 save_register_info (void)
300 /* Sanity check: make sure the target macros FIXED_REGISTERS and
301 CALL_USED_REGISTERS had the right number of initializers. */
302 gcc_assert (sizeof fixed_regs == sizeof saved_fixed_regs);
303 gcc_assert (sizeof call_used_regs == sizeof saved_call_used_regs);
304 memcpy (saved_fixed_regs, fixed_regs, sizeof fixed_regs);
305 memcpy (saved_call_used_regs, call_used_regs, sizeof call_used_regs);
307 /* Likewise for call_really_used_regs. */
308 #ifdef CALL_REALLY_USED_REGISTERS
309 gcc_assert (sizeof call_really_used_regs
310 == sizeof saved_call_really_used_regs);
311 memcpy (saved_call_really_used_regs, call_really_used_regs,
312 sizeof call_really_used_regs);
313 #endif
315 /* And similarly for reg_names. */
316 gcc_assert (sizeof reg_names == sizeof saved_reg_names);
317 memcpy (saved_reg_names, reg_names, sizeof reg_names);
320 /* Restore the register information. */
321 static void
322 restore_register_info (void)
324 memcpy (fixed_regs, saved_fixed_regs, sizeof fixed_regs);
325 memcpy (call_used_regs, saved_call_used_regs, sizeof call_used_regs);
327 #ifdef CALL_REALLY_USED_REGISTERS
328 memcpy (call_really_used_regs, saved_call_really_used_regs,
329 sizeof call_really_used_regs);
330 #endif
332 memcpy (reg_names, saved_reg_names, sizeof reg_names);
335 /* After switches have been processed, which perhaps alter
336 `fixed_regs' and `call_used_regs', convert them to HARD_REG_SETs. */
337 static void
338 init_reg_sets_1 (void)
340 unsigned int i, j;
341 unsigned int /* enum machine_mode */ m;
343 restore_register_info ();
345 #ifdef REG_ALLOC_ORDER
346 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
347 inv_reg_alloc_order[reg_alloc_order[i]] = i;
348 #endif
350 /* Let the target tweak things if necessary. */
352 targetm.conditional_register_usage ();
354 /* Compute number of hard regs in each class. */
356 memset (reg_class_size, 0, sizeof reg_class_size);
357 for (i = 0; i < N_REG_CLASSES; i++)
359 bool any_nonfixed = false;
360 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
361 if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
363 reg_class_size[i]++;
364 if (!fixed_regs[j])
365 any_nonfixed = true;
367 class_only_fixed_regs[i] = !any_nonfixed;
370 /* Initialize the table of subunions.
371 reg_class_subunion[I][J] gets the largest-numbered reg-class
372 that is contained in the union of classes I and J. */
374 memset (reg_class_subunion, 0, sizeof reg_class_subunion);
375 for (i = 0; i < N_REG_CLASSES; i++)
377 for (j = 0; j < N_REG_CLASSES; j++)
379 HARD_REG_SET c;
380 int k;
382 COPY_HARD_REG_SET (c, reg_class_contents[i]);
383 IOR_HARD_REG_SET (c, reg_class_contents[j]);
384 for (k = 0; k < N_REG_CLASSES; k++)
385 if (hard_reg_set_subset_p (reg_class_contents[k], c)
386 && !hard_reg_set_subset_p (reg_class_contents[k],
387 reg_class_contents
388 [(int) reg_class_subunion[i][j]]))
389 reg_class_subunion[i][j] = (enum reg_class) k;
393 /* Initialize the table of superunions.
394 reg_class_superunion[I][J] gets the smallest-numbered reg-class
395 containing the union of classes I and J. */
397 memset (reg_class_superunion, 0, sizeof reg_class_superunion);
398 for (i = 0; i < N_REG_CLASSES; i++)
400 for (j = 0; j < N_REG_CLASSES; j++)
402 HARD_REG_SET c;
403 int k;
405 COPY_HARD_REG_SET (c, reg_class_contents[i]);
406 IOR_HARD_REG_SET (c, reg_class_contents[j]);
407 for (k = 0; k < N_REG_CLASSES; k++)
408 if (hard_reg_set_subset_p (c, reg_class_contents[k]))
409 break;
411 reg_class_superunion[i][j] = (enum reg_class) k;
415 /* Initialize the tables of subclasses and superclasses of each reg class.
416 First clear the whole table, then add the elements as they are found. */
418 for (i = 0; i < N_REG_CLASSES; i++)
420 for (j = 0; j < N_REG_CLASSES; j++)
421 reg_class_subclasses[i][j] = LIM_REG_CLASSES;
424 for (i = 0; i < N_REG_CLASSES; i++)
426 if (i == (int) NO_REGS)
427 continue;
429 for (j = i + 1; j < N_REG_CLASSES; j++)
430 if (hard_reg_set_subset_p (reg_class_contents[i],
431 reg_class_contents[j]))
433 /* Reg class I is a subclass of J.
434 Add J to the table of superclasses of I. */
435 enum reg_class *p;
437 /* Add I to the table of superclasses of J. */
438 p = &reg_class_subclasses[j][0];
439 while (*p != LIM_REG_CLASSES) p++;
440 *p = (enum reg_class) i;
444 /* Initialize "constant" tables. */
446 CLEAR_HARD_REG_SET (fixed_reg_set);
447 CLEAR_HARD_REG_SET (call_used_reg_set);
448 CLEAR_HARD_REG_SET (call_fixed_reg_set);
449 CLEAR_HARD_REG_SET (regs_invalidated_by_call);
450 if (!regs_invalidated_by_call_regset)
452 bitmap_obstack_initialize (&persistent_obstack);
453 regs_invalidated_by_call_regset = ALLOC_REG_SET (&persistent_obstack);
455 else
456 CLEAR_REG_SET (regs_invalidated_by_call_regset);
457 if (!fixed_reg_set_regset)
458 fixed_reg_set_regset = ALLOC_REG_SET (&persistent_obstack);
459 else
460 CLEAR_REG_SET (fixed_reg_set_regset);
462 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
464 /* call_used_regs must include fixed_regs. */
465 gcc_assert (!fixed_regs[i] || call_used_regs[i]);
466 #ifdef CALL_REALLY_USED_REGISTERS
467 /* call_used_regs must include call_really_used_regs. */
468 gcc_assert (!call_really_used_regs[i] || call_used_regs[i]);
469 #endif
471 if (fixed_regs[i])
473 SET_HARD_REG_BIT (fixed_reg_set, i);
474 SET_REGNO_REG_SET (fixed_reg_set_regset, i);
477 if (call_used_regs[i])
478 SET_HARD_REG_BIT (call_used_reg_set, i);
480 /* There are a couple of fixed registers that we know are safe to
481 exclude from being clobbered by calls:
483 The frame pointer is always preserved across calls. The arg
484 pointer is if it is fixed. The stack pointer usually is,
485 unless TARGET_RETURN_POPS_ARGS, in which case an explicit
486 CLOBBER will be present. If we are generating PIC code, the
487 PIC offset table register is preserved across calls, though the
488 target can override that. */
490 if (i == STACK_POINTER_REGNUM)
492 else if (global_regs[i])
494 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
495 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
497 else if (i == FRAME_POINTER_REGNUM)
499 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
500 else if (i == HARD_FRAME_POINTER_REGNUM)
502 #endif
503 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
504 else if (i == ARG_POINTER_REGNUM && fixed_regs[i])
506 #endif
507 else if (!PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
508 && i == (unsigned) PIC_OFFSET_TABLE_REGNUM && fixed_regs[i])
510 else if (CALL_REALLY_USED_REGNO_P (i))
512 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
513 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
517 COPY_HARD_REG_SET(call_fixed_reg_set, fixed_reg_set);
519 /* Preserve global registers if called more than once. */
520 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
522 if (global_regs[i])
524 fixed_regs[i] = call_used_regs[i] = 1;
525 SET_HARD_REG_BIT (fixed_reg_set, i);
526 SET_HARD_REG_BIT (call_used_reg_set, i);
527 SET_HARD_REG_BIT (call_fixed_reg_set, i);
531 memset (have_regs_of_mode, 0, sizeof (have_regs_of_mode));
532 memset (contains_reg_of_mode, 0, sizeof (contains_reg_of_mode));
533 for (m = 0; m < (unsigned int) MAX_MACHINE_MODE; m++)
535 HARD_REG_SET ok_regs;
536 CLEAR_HARD_REG_SET (ok_regs);
537 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
538 if (!fixed_regs [j] && HARD_REGNO_MODE_OK (j, (enum machine_mode) m))
539 SET_HARD_REG_BIT (ok_regs, j);
541 for (i = 0; i < N_REG_CLASSES; i++)
542 if ((targetm.class_max_nregs ((reg_class_t) i, (enum machine_mode) m)
543 <= reg_class_size[i])
544 && hard_reg_set_intersect_p (ok_regs, reg_class_contents[i]))
546 contains_reg_of_mode [i][m] = 1;
547 have_regs_of_mode [m] = 1;
551 /* Reset move_cost and friends, making sure we only free shared
552 table entries once. */
553 for (i = 0; i < MAX_MACHINE_MODE; i++)
554 if (move_cost[i])
556 for (j = 0; j < i && move_cost[i] != move_cost[j]; j++)
558 if (i == j)
560 free (move_cost[i]);
561 free (may_move_in_cost[i]);
562 free (may_move_out_cost[i]);
565 memset (move_cost, 0, sizeof move_cost);
566 memset (may_move_in_cost, 0, sizeof may_move_in_cost);
567 memset (may_move_out_cost, 0, sizeof may_move_out_cost);
568 last_mode_for_init_move_cost = -1;
571 /* Compute the table of register modes.
572 These values are used to record death information for individual registers
573 (as opposed to a multi-register mode).
574 This function might be invoked more than once, if the target has support
575 for changing register usage conventions on a per-function basis.
577 void
578 init_reg_modes_target (void)
580 int i, j;
582 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
583 for (j = 0; j < MAX_MACHINE_MODE; j++)
584 hard_regno_nregs[i][j] = HARD_REGNO_NREGS(i, (enum machine_mode)j);
586 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
588 reg_raw_mode[i] = choose_hard_reg_mode (i, 1, false);
590 /* If we couldn't find a valid mode, just use the previous mode.
591 ??? One situation in which we need to do this is on the mips where
592 HARD_REGNO_NREGS (fpreg, [SD]Fmode) returns 2. Ideally we'd like
593 to use DF mode for the even registers and VOIDmode for the odd
594 (for the cpu models where the odd ones are inaccessible). */
595 if (reg_raw_mode[i] == VOIDmode)
596 reg_raw_mode[i] = i == 0 ? word_mode : reg_raw_mode[i-1];
600 /* Finish initializing the register sets and initialize the register modes.
601 This function might be invoked more than once, if the target has support
602 for changing register usage conventions on a per-function basis.
604 void
605 init_regs (void)
607 /* This finishes what was started by init_reg_sets, but couldn't be done
608 until after register usage was specified. */
609 init_reg_sets_1 ();
612 /* The same as previous function plus initializing IRA. */
613 void
614 reinit_regs (void)
616 init_regs ();
617 /* caller_save needs to be re-initialized. */
618 caller_save_initialized_p = false;
619 ira_init ();
622 /* Initialize some fake stack-frame MEM references for use in
623 memory_move_secondary_cost. */
624 void
625 init_fake_stack_mems (void)
627 int i;
629 for (i = 0; i < MAX_MACHINE_MODE; i++)
630 top_of_stack[i] = gen_rtx_MEM ((enum machine_mode) i, stack_pointer_rtx);
634 /* Compute cost of moving data from a register of class FROM to one of
635 TO, using MODE. */
638 register_move_cost (enum machine_mode mode, reg_class_t from, reg_class_t to)
640 return targetm.register_move_cost (mode, from, to);
643 /* Compute cost of moving registers to/from memory. */
646 memory_move_cost (enum machine_mode mode, reg_class_t rclass, bool in)
648 return targetm.memory_move_cost (mode, rclass, in);
651 /* Compute extra cost of moving registers to/from memory due to reloads.
652 Only needed if secondary reloads are required for memory moves. */
654 memory_move_secondary_cost (enum machine_mode mode, reg_class_t rclass,
655 bool in)
657 reg_class_t altclass;
658 int partial_cost = 0;
659 /* We need a memory reference to feed to SECONDARY... macros. */
660 /* mem may be unused even if the SECONDARY_ macros are defined. */
661 rtx mem ATTRIBUTE_UNUSED = top_of_stack[(int) mode];
663 altclass = secondary_reload_class (in ? 1 : 0, rclass, mode, mem);
665 if (altclass == NO_REGS)
666 return 0;
668 if (in)
669 partial_cost = register_move_cost (mode, altclass, rclass);
670 else
671 partial_cost = register_move_cost (mode, rclass, altclass);
673 if (rclass == altclass)
674 /* This isn't simply a copy-to-temporary situation. Can't guess
675 what it is, so TARGET_MEMORY_MOVE_COST really ought not to be
676 calling here in that case.
678 I'm tempted to put in an assert here, but returning this will
679 probably only give poor estimates, which is what we would've
680 had before this code anyways. */
681 return partial_cost;
683 /* Check if the secondary reload register will also need a
684 secondary reload. */
685 return memory_move_secondary_cost (mode, altclass, in) + partial_cost;
688 /* Return a machine mode that is legitimate for hard reg REGNO and large
689 enough to save nregs. If we can't find one, return VOIDmode.
690 If CALL_SAVED is true, only consider modes that are call saved. */
691 enum machine_mode
692 choose_hard_reg_mode (unsigned int regno ATTRIBUTE_UNUSED,
693 unsigned int nregs, bool call_saved)
695 unsigned int /* enum machine_mode */ m;
696 enum machine_mode found_mode = VOIDmode, mode;
698 /* We first look for the largest integer mode that can be validly
699 held in REGNO. If none, we look for the largest floating-point mode.
700 If we still didn't find a valid mode, try CCmode. */
702 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
703 mode != VOIDmode;
704 mode = GET_MODE_WIDER_MODE (mode))
705 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
706 && HARD_REGNO_MODE_OK (regno, mode)
707 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
708 found_mode = mode;
710 if (found_mode != VOIDmode)
711 return found_mode;
713 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
714 mode != VOIDmode;
715 mode = GET_MODE_WIDER_MODE (mode))
716 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
717 && HARD_REGNO_MODE_OK (regno, mode)
718 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
719 found_mode = mode;
721 if (found_mode != VOIDmode)
722 return found_mode;
724 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
725 mode != VOIDmode;
726 mode = GET_MODE_WIDER_MODE (mode))
727 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
728 && HARD_REGNO_MODE_OK (regno, mode)
729 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
730 found_mode = mode;
732 if (found_mode != VOIDmode)
733 return found_mode;
735 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
736 mode != VOIDmode;
737 mode = GET_MODE_WIDER_MODE (mode))
738 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
739 && HARD_REGNO_MODE_OK (regno, mode)
740 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
741 found_mode = mode;
743 if (found_mode != VOIDmode)
744 return found_mode;
746 /* Iterate over all of the CCmodes. */
747 for (m = (unsigned int) CCmode; m < (unsigned int) NUM_MACHINE_MODES; ++m)
749 mode = (enum machine_mode) m;
750 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
751 && HARD_REGNO_MODE_OK (regno, mode)
752 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
753 return mode;
756 /* We can't find a mode valid for this register. */
757 return VOIDmode;
760 /* Specify the usage characteristics of the register named NAME.
761 It should be a fixed register if FIXED and a
762 call-used register if CALL_USED. */
763 void
764 fix_register (const char *name, int fixed, int call_used)
766 int i;
767 int reg, nregs;
769 /* Decode the name and update the primary form of
770 the register info. */
772 if ((reg = decode_reg_name_and_count (name, &nregs)) >= 0)
774 gcc_assert (nregs >= 1);
775 for (i = reg; i < reg + nregs; i++)
777 if ((i == STACK_POINTER_REGNUM
778 #ifdef HARD_FRAME_POINTER_REGNUM
779 || i == HARD_FRAME_POINTER_REGNUM
780 #else
781 || i == FRAME_POINTER_REGNUM
782 #endif
784 && (fixed == 0 || call_used == 0))
786 switch (fixed)
788 case 0:
789 switch (call_used)
791 case 0:
792 error ("can%'t use %qs as a call-saved register", name);
793 break;
795 case 1:
796 error ("can%'t use %qs as a call-used register", name);
797 break;
799 default:
800 gcc_unreachable ();
802 break;
804 case 1:
805 switch (call_used)
807 case 1:
808 error ("can%'t use %qs as a fixed register", name);
809 break;
811 case 0:
812 default:
813 gcc_unreachable ();
815 break;
817 default:
818 gcc_unreachable ();
821 else
823 fixed_regs[i] = fixed;
824 call_used_regs[i] = call_used;
825 #ifdef CALL_REALLY_USED_REGISTERS
826 if (fixed == 0)
827 call_really_used_regs[i] = call_used;
828 #endif
832 else
834 warning (0, "unknown register name: %s", name);
838 /* Mark register number I as global. */
839 void
840 globalize_reg (tree decl, int i)
842 location_t loc = DECL_SOURCE_LOCATION (decl);
844 #ifdef STACK_REGS
845 if (IN_RANGE (i, FIRST_STACK_REG, LAST_STACK_REG))
847 error ("stack register used for global register variable");
848 return;
850 #endif
852 if (fixed_regs[i] == 0 && no_global_reg_vars)
853 error_at (loc, "global register variable follows a function definition");
855 if (global_regs[i])
857 warning_at (loc, 0,
858 "register of %qD used for multiple global register variables",
859 decl);
860 inform (DECL_SOURCE_LOCATION (global_regs_decl[i]),
861 "conflicts with %qD", global_regs_decl[i]);
862 return;
865 if (call_used_regs[i] && ! fixed_regs[i])
866 warning_at (loc, 0, "call-clobbered register used for global register variable");
868 global_regs[i] = 1;
869 global_regs_decl[i] = decl;
871 /* If we're globalizing the frame pointer, we need to set the
872 appropriate regs_invalidated_by_call bit, even if it's already
873 set in fixed_regs. */
874 if (i != STACK_POINTER_REGNUM)
876 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
877 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
880 /* If already fixed, nothing else to do. */
881 if (fixed_regs[i])
882 return;
884 fixed_regs[i] = call_used_regs[i] = 1;
885 #ifdef CALL_REALLY_USED_REGISTERS
886 call_really_used_regs[i] = 1;
887 #endif
889 SET_HARD_REG_BIT (fixed_reg_set, i);
890 SET_HARD_REG_BIT (call_used_reg_set, i);
891 SET_HARD_REG_BIT (call_fixed_reg_set, i);
893 reinit_regs ();
897 /* Structure used to record preferences of given pseudo. */
898 struct reg_pref
900 /* (enum reg_class) prefclass is the preferred class. May be
901 NO_REGS if no class is better than memory. */
902 char prefclass;
904 /* altclass is a register class that we should use for allocating
905 pseudo if no register in the preferred class is available.
906 If no register in this class is available, memory is preferred.
908 It might appear to be more general to have a bitmask of classes here,
909 but since it is recommended that there be a class corresponding to the
910 union of most major pair of classes, that generality is not required. */
911 char altclass;
913 /* allocnoclass is a register class that IRA uses for allocating
914 the pseudo. */
915 char allocnoclass;
918 /* Record preferences of each pseudo. This is available after RA is
919 run. */
920 static struct reg_pref *reg_pref;
922 /* Current size of reg_info. */
923 static int reg_info_size;
925 /* Return the reg_class in which pseudo reg number REGNO is best allocated.
926 This function is sometimes called before the info has been computed.
927 When that happens, just return GENERAL_REGS, which is innocuous. */
928 enum reg_class
929 reg_preferred_class (int regno)
931 if (reg_pref == 0)
932 return GENERAL_REGS;
934 return (enum reg_class) reg_pref[regno].prefclass;
937 enum reg_class
938 reg_alternate_class (int regno)
940 if (reg_pref == 0)
941 return ALL_REGS;
943 return (enum reg_class) reg_pref[regno].altclass;
946 /* Return the reg_class which is used by IRA for its allocation. */
947 enum reg_class
948 reg_allocno_class (int regno)
950 if (reg_pref == 0)
951 return NO_REGS;
953 return (enum reg_class) reg_pref[regno].allocnoclass;
958 /* Allocate space for reg info. */
959 static void
960 allocate_reg_info (void)
962 reg_info_size = max_reg_num ();
963 gcc_assert (! reg_pref && ! reg_renumber);
964 reg_renumber = XNEWVEC (short, reg_info_size);
965 reg_pref = XCNEWVEC (struct reg_pref, reg_info_size);
966 memset (reg_renumber, -1, reg_info_size * sizeof (short));
970 /* Resize reg info. The new elements will be uninitialized. Return
971 TRUE if new elements (for new pseudos) were added. */
972 bool
973 resize_reg_info (void)
975 int old;
977 if (reg_pref == NULL)
979 allocate_reg_info ();
980 return true;
982 if (reg_info_size == max_reg_num ())
983 return false;
984 old = reg_info_size;
985 reg_info_size = max_reg_num ();
986 gcc_assert (reg_pref && reg_renumber);
987 reg_renumber = XRESIZEVEC (short, reg_renumber, reg_info_size);
988 reg_pref = XRESIZEVEC (struct reg_pref, reg_pref, reg_info_size);
989 memset (reg_pref + old, -1,
990 (reg_info_size - old) * sizeof (struct reg_pref));
991 memset (reg_renumber + old, -1, (reg_info_size - old) * sizeof (short));
992 return true;
996 /* Free up the space allocated by allocate_reg_info. */
997 void
998 free_reg_info (void)
1000 if (reg_pref)
1002 free (reg_pref);
1003 reg_pref = NULL;
1006 if (reg_renumber)
1008 free (reg_renumber);
1009 reg_renumber = NULL;
1013 /* Initialize some global data for this pass. */
1014 static unsigned int
1015 reginfo_init (void)
1017 if (df)
1018 df_compute_regs_ever_live (true);
1020 /* This prevents dump_flow_info from losing if called
1021 before reginfo is run. */
1022 reg_pref = NULL;
1023 /* No more global register variables may be declared. */
1024 no_global_reg_vars = 1;
1025 return 1;
1028 struct rtl_opt_pass pass_reginfo_init =
1031 RTL_PASS,
1032 "reginfo", /* name */
1033 NULL, /* gate */
1034 reginfo_init, /* execute */
1035 NULL, /* sub */
1036 NULL, /* next */
1037 0, /* static_pass_number */
1038 TV_NONE, /* tv_id */
1039 0, /* properties_required */
1040 0, /* properties_provided */
1041 0, /* properties_destroyed */
1042 0, /* todo_flags_start */
1043 0 /* todo_flags_finish */
1049 /* Set up preferred, alternate, and cover classes for REGNO as
1050 PREFCLASS, ALTCLASS, and ALLOCNOCLASS. */
1051 void
1052 setup_reg_classes (int regno,
1053 enum reg_class prefclass, enum reg_class altclass,
1054 enum reg_class allocnoclass)
1056 if (reg_pref == NULL)
1057 return;
1058 gcc_assert (reg_info_size == max_reg_num ());
1059 reg_pref[regno].prefclass = prefclass;
1060 reg_pref[regno].altclass = altclass;
1061 reg_pref[regno].allocnoclass = allocnoclass;
1065 /* This is the `regscan' pass of the compiler, run just before cse and
1066 again just before loop. It finds the first and last use of each
1067 pseudo-register. */
1069 static void reg_scan_mark_refs (rtx, rtx);
1071 void
1072 reg_scan (rtx f, unsigned int nregs ATTRIBUTE_UNUSED)
1074 rtx insn;
1076 timevar_push (TV_REG_SCAN);
1078 for (insn = f; insn; insn = NEXT_INSN (insn))
1079 if (INSN_P (insn))
1081 reg_scan_mark_refs (PATTERN (insn), insn);
1082 if (REG_NOTES (insn))
1083 reg_scan_mark_refs (REG_NOTES (insn), insn);
1086 timevar_pop (TV_REG_SCAN);
1090 /* X is the expression to scan. INSN is the insn it appears in.
1091 NOTE_FLAG is nonzero if X is from INSN's notes rather than its body.
1092 We should only record information for REGs with numbers
1093 greater than or equal to MIN_REGNO. */
1094 static void
1095 reg_scan_mark_refs (rtx x, rtx insn)
1097 enum rtx_code code;
1098 rtx dest;
1099 rtx note;
1101 if (!x)
1102 return;
1103 code = GET_CODE (x);
1104 switch (code)
1106 case CONST:
1107 case CONST_INT:
1108 case CONST_DOUBLE:
1109 case CONST_FIXED:
1110 case CONST_VECTOR:
1111 case CC0:
1112 case PC:
1113 case SYMBOL_REF:
1114 case LABEL_REF:
1115 case ADDR_VEC:
1116 case ADDR_DIFF_VEC:
1117 case REG:
1118 return;
1120 case EXPR_LIST:
1121 if (XEXP (x, 0))
1122 reg_scan_mark_refs (XEXP (x, 0), insn);
1123 if (XEXP (x, 1))
1124 reg_scan_mark_refs (XEXP (x, 1), insn);
1125 break;
1127 case INSN_LIST:
1128 if (XEXP (x, 1))
1129 reg_scan_mark_refs (XEXP (x, 1), insn);
1130 break;
1132 case CLOBBER:
1133 if (MEM_P (XEXP (x, 0)))
1134 reg_scan_mark_refs (XEXP (XEXP (x, 0), 0), insn);
1135 break;
1137 case SET:
1138 /* Count a set of the destination if it is a register. */
1139 for (dest = SET_DEST (x);
1140 GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
1141 || GET_CODE (dest) == ZERO_EXTEND;
1142 dest = XEXP (dest, 0))
1145 /* If this is setting a pseudo from another pseudo or the sum of a
1146 pseudo and a constant integer and the other pseudo is known to be
1147 a pointer, set the destination to be a pointer as well.
1149 Likewise if it is setting the destination from an address or from a
1150 value equivalent to an address or to the sum of an address and
1151 something else.
1153 But don't do any of this if the pseudo corresponds to a user
1154 variable since it should have already been set as a pointer based
1155 on the type. */
1157 if (REG_P (SET_DEST (x))
1158 && REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER
1159 /* If the destination pseudo is set more than once, then other
1160 sets might not be to a pointer value (consider access to a
1161 union in two threads of control in the presence of global
1162 optimizations). So only set REG_POINTER on the destination
1163 pseudo if this is the only set of that pseudo. */
1164 && DF_REG_DEF_COUNT (REGNO (SET_DEST (x))) == 1
1165 && ! REG_USERVAR_P (SET_DEST (x))
1166 && ! REG_POINTER (SET_DEST (x))
1167 && ((REG_P (SET_SRC (x))
1168 && REG_POINTER (SET_SRC (x)))
1169 || ((GET_CODE (SET_SRC (x)) == PLUS
1170 || GET_CODE (SET_SRC (x)) == LO_SUM)
1171 && CONST_INT_P (XEXP (SET_SRC (x), 1))
1172 && REG_P (XEXP (SET_SRC (x), 0))
1173 && REG_POINTER (XEXP (SET_SRC (x), 0)))
1174 || GET_CODE (SET_SRC (x)) == CONST
1175 || GET_CODE (SET_SRC (x)) == SYMBOL_REF
1176 || GET_CODE (SET_SRC (x)) == LABEL_REF
1177 || (GET_CODE (SET_SRC (x)) == HIGH
1178 && (GET_CODE (XEXP (SET_SRC (x), 0)) == CONST
1179 || GET_CODE (XEXP (SET_SRC (x), 0)) == SYMBOL_REF
1180 || GET_CODE (XEXP (SET_SRC (x), 0)) == LABEL_REF))
1181 || ((GET_CODE (SET_SRC (x)) == PLUS
1182 || GET_CODE (SET_SRC (x)) == LO_SUM)
1183 && (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST
1184 || GET_CODE (XEXP (SET_SRC (x), 1)) == SYMBOL_REF
1185 || GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF))
1186 || ((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
1187 && (GET_CODE (XEXP (note, 0)) == CONST
1188 || GET_CODE (XEXP (note, 0)) == SYMBOL_REF
1189 || GET_CODE (XEXP (note, 0)) == LABEL_REF))))
1190 REG_POINTER (SET_DEST (x)) = 1;
1192 /* If this is setting a register from a register or from a simple
1193 conversion of a register, propagate REG_EXPR. */
1194 if (REG_P (dest) && !REG_ATTRS (dest))
1196 rtx src = SET_SRC (x);
1198 while (GET_CODE (src) == SIGN_EXTEND
1199 || GET_CODE (src) == ZERO_EXTEND
1200 || GET_CODE (src) == TRUNCATE
1201 || (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)))
1202 src = XEXP (src, 0);
1204 set_reg_attrs_from_value (dest, src);
1207 /* ... fall through ... */
1209 default:
1211 const char *fmt = GET_RTX_FORMAT (code);
1212 int i;
1213 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1215 if (fmt[i] == 'e')
1216 reg_scan_mark_refs (XEXP (x, i), insn);
1217 else if (fmt[i] == 'E' && XVEC (x, i) != 0)
1219 int j;
1220 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1221 reg_scan_mark_refs (XVECEXP (x, i, j), insn);
1229 /* Return nonzero if C1 is a subset of C2, i.e., if every register in C1
1230 is also in C2. */
1232 reg_class_subset_p (reg_class_t c1, reg_class_t c2)
1234 return (c1 == c2
1235 || c2 == ALL_REGS
1236 || hard_reg_set_subset_p (reg_class_contents[(int) c1],
1237 reg_class_contents[(int) c2]));
1240 /* Return nonzero if there is a register that is in both C1 and C2. */
1242 reg_classes_intersect_p (reg_class_t c1, reg_class_t c2)
1244 return (c1 == c2
1245 || c1 == ALL_REGS
1246 || c2 == ALL_REGS
1247 || hard_reg_set_intersect_p (reg_class_contents[(int) c1],
1248 reg_class_contents[(int) c2]));
1253 /* Passes for keeping and updating info about modes of registers
1254 inside subregisters. */
1256 #ifdef CANNOT_CHANGE_MODE_CLASS
1258 static bitmap invalid_mode_changes;
1260 static void
1261 record_subregs_of_mode (rtx subreg, bitmap subregs_of_mode)
1263 enum machine_mode mode;
1264 unsigned int regno;
1266 if (!REG_P (SUBREG_REG (subreg)))
1267 return;
1269 regno = REGNO (SUBREG_REG (subreg));
1270 mode = GET_MODE (subreg);
1272 if (regno < FIRST_PSEUDO_REGISTER)
1273 return;
1275 if (bitmap_set_bit (subregs_of_mode,
1276 regno * NUM_MACHINE_MODES + (unsigned int) mode))
1278 unsigned int rclass;
1279 for (rclass = 0; rclass < N_REG_CLASSES; rclass++)
1280 if (!bitmap_bit_p (invalid_mode_changes,
1281 regno * N_REG_CLASSES + rclass)
1282 && CANNOT_CHANGE_MODE_CLASS (PSEUDO_REGNO_MODE (regno),
1283 mode, (enum reg_class) rclass))
1284 bitmap_set_bit (invalid_mode_changes,
1285 regno * N_REG_CLASSES + rclass);
1289 /* Call record_subregs_of_mode for all the subregs in X. */
1290 static void
1291 find_subregs_of_mode (rtx x, bitmap subregs_of_mode)
1293 enum rtx_code code = GET_CODE (x);
1294 const char * const fmt = GET_RTX_FORMAT (code);
1295 int i;
1297 if (code == SUBREG)
1298 record_subregs_of_mode (x, subregs_of_mode);
1300 /* Time for some deep diving. */
1301 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1303 if (fmt[i] == 'e')
1304 find_subregs_of_mode (XEXP (x, i), subregs_of_mode);
1305 else if (fmt[i] == 'E')
1307 int j;
1308 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1309 find_subregs_of_mode (XVECEXP (x, i, j), subregs_of_mode);
1314 void
1315 init_subregs_of_mode (void)
1317 basic_block bb;
1318 rtx insn;
1319 bitmap_obstack srom_obstack;
1320 bitmap subregs_of_mode;
1322 gcc_assert (invalid_mode_changes == NULL);
1323 invalid_mode_changes = BITMAP_ALLOC (NULL);
1324 bitmap_obstack_initialize (&srom_obstack);
1325 subregs_of_mode = BITMAP_ALLOC (&srom_obstack);
1327 FOR_EACH_BB (bb)
1328 FOR_BB_INSNS (bb, insn)
1329 if (NONDEBUG_INSN_P (insn))
1330 find_subregs_of_mode (PATTERN (insn), subregs_of_mode);
1332 BITMAP_FREE (subregs_of_mode);
1333 bitmap_obstack_release (&srom_obstack);
1336 /* Return 1 if REGNO has had an invalid mode change in CLASS from FROM
1337 mode. */
1338 bool
1339 invalid_mode_change_p (unsigned int regno,
1340 enum reg_class rclass)
1342 return bitmap_bit_p (invalid_mode_changes,
1343 regno * N_REG_CLASSES + (unsigned) rclass);
1346 void
1347 finish_subregs_of_mode (void)
1349 BITMAP_FREE (invalid_mode_changes);
1351 #else
1352 void
1353 init_subregs_of_mode (void)
1356 void
1357 finish_subregs_of_mode (void)
1361 #endif /* CANNOT_CHANGE_MODE_CLASS */