2011-10-08 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / reginfo.c
blob537364192da2d687b317e3a12134039d920e838a
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 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 /* The bitmap_obstack is used to hold some static variables that
98 should not be reset after each function is compiled. */
99 static bitmap_obstack persistent_obstack;
101 /* Used to initialize reg_alloc_order. */
102 #ifdef REG_ALLOC_ORDER
103 static int initial_reg_alloc_order[FIRST_PSEUDO_REGISTER] = REG_ALLOC_ORDER;
104 #endif
106 /* The same information, but as an array of unsigned ints. We copy from
107 these unsigned ints to the table above. We do this so the tm.h files
108 do not have to be aware of the wordsize for machines with <= 64 regs.
109 Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
110 #define N_REG_INTS \
111 ((FIRST_PSEUDO_REGISTER + (32 - 1)) / 32)
113 static const unsigned int_reg_class_contents[N_REG_CLASSES][N_REG_INTS]
114 = REG_CLASS_CONTENTS;
116 /* Array containing all of the register names. */
117 static const char *const initial_reg_names[] = REGISTER_NAMES;
119 /* Array containing all of the register class names. */
120 const char * reg_class_names[] = REG_CLASS_NAMES;
122 #define last_mode_for_init_move_cost \
123 (this_target_regs->x_last_mode_for_init_move_cost)
125 /* No more global register variables may be declared; true once
126 reginfo has been initialized. */
127 static int no_global_reg_vars = 0;
129 /* Given a register bitmap, turn on the bits in a HARD_REG_SET that
130 correspond to the hard registers, if any, set in that map. This
131 could be done far more efficiently by having all sorts of special-cases
132 with moving single words, but probably isn't worth the trouble. */
133 void
134 reg_set_to_hard_reg_set (HARD_REG_SET *to, const_bitmap from)
136 unsigned i;
137 bitmap_iterator bi;
139 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
141 if (i >= FIRST_PSEUDO_REGISTER)
142 return;
143 SET_HARD_REG_BIT (*to, i);
147 /* Function called only once per target_globals to initialize the
148 target_hard_regs structure. Once this is done, various switches
149 may override. */
150 void
151 init_reg_sets (void)
153 int i, j;
155 /* First copy the register information from the initial int form into
156 the regsets. */
158 for (i = 0; i < N_REG_CLASSES; i++)
160 CLEAR_HARD_REG_SET (reg_class_contents[i]);
162 /* Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
163 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
164 if (int_reg_class_contents[i][j / 32]
165 & ((unsigned) 1 << (j % 32)))
166 SET_HARD_REG_BIT (reg_class_contents[i], j);
169 /* Sanity check: make sure the target macros FIXED_REGISTERS and
170 CALL_USED_REGISTERS had the right number of initializers. */
171 gcc_assert (sizeof fixed_regs == sizeof initial_fixed_regs);
172 gcc_assert (sizeof call_used_regs == sizeof initial_call_used_regs);
173 #ifdef CALL_REALLY_USED_REGISTERS
174 gcc_assert (sizeof call_really_used_regs
175 == sizeof initial_call_really_used_regs);
176 #endif
177 #ifdef REG_ALLOC_ORDER
178 gcc_assert (sizeof reg_alloc_order == sizeof initial_reg_alloc_order);
179 #endif
180 gcc_assert (sizeof reg_names == sizeof initial_reg_names);
182 memcpy (fixed_regs, initial_fixed_regs, sizeof fixed_regs);
183 memcpy (call_used_regs, initial_call_used_regs, sizeof call_used_regs);
184 #ifdef CALL_REALLY_USED_REGISTERS
185 memcpy (call_really_used_regs, initial_call_really_used_regs,
186 sizeof call_really_used_regs);
187 #endif
188 #ifdef REG_ALLOC_ORDER
189 memcpy (reg_alloc_order, initial_reg_alloc_order, sizeof reg_alloc_order);
190 #endif
191 memcpy (reg_names, initial_reg_names, sizeof reg_names);
194 /* Initialize may_move_cost and friends for mode M. */
195 void
196 init_move_cost (enum machine_mode m)
198 static unsigned short last_move_cost[N_REG_CLASSES][N_REG_CLASSES];
199 bool all_match = true;
200 unsigned int i, j;
202 gcc_assert (have_regs_of_mode[m]);
203 for (i = 0; i < N_REG_CLASSES; i++)
204 if (contains_reg_of_mode[i][m])
205 for (j = 0; j < N_REG_CLASSES; j++)
207 int cost;
208 if (!contains_reg_of_mode[j][m])
209 cost = 65535;
210 else
212 cost = register_move_cost (m, (enum reg_class) i,
213 (enum reg_class) j);
214 gcc_assert (cost < 65535);
216 all_match &= (last_move_cost[i][j] == cost);
217 last_move_cost[i][j] = cost;
219 if (all_match && last_mode_for_init_move_cost != -1)
221 move_cost[m] = move_cost[last_mode_for_init_move_cost];
222 may_move_in_cost[m] = may_move_in_cost[last_mode_for_init_move_cost];
223 may_move_out_cost[m] = may_move_out_cost[last_mode_for_init_move_cost];
224 return;
226 last_mode_for_init_move_cost = m;
227 move_cost[m] = (move_table *)xmalloc (sizeof (move_table)
228 * N_REG_CLASSES);
229 may_move_in_cost[m] = (move_table *)xmalloc (sizeof (move_table)
230 * N_REG_CLASSES);
231 may_move_out_cost[m] = (move_table *)xmalloc (sizeof (move_table)
232 * N_REG_CLASSES);
233 for (i = 0; i < N_REG_CLASSES; i++)
234 if (contains_reg_of_mode[i][m])
235 for (j = 0; j < N_REG_CLASSES; j++)
237 int cost;
238 enum reg_class *p1, *p2;
240 if (last_move_cost[i][j] == 65535)
242 move_cost[m][i][j] = 65535;
243 may_move_in_cost[m][i][j] = 65535;
244 may_move_out_cost[m][i][j] = 65535;
246 else
248 cost = last_move_cost[i][j];
250 for (p2 = &reg_class_subclasses[j][0];
251 *p2 != LIM_REG_CLASSES; p2++)
252 if (*p2 != i && contains_reg_of_mode[*p2][m])
253 cost = MAX (cost, move_cost[m][i][*p2]);
255 for (p1 = &reg_class_subclasses[i][0];
256 *p1 != LIM_REG_CLASSES; p1++)
257 if (*p1 != j && contains_reg_of_mode[*p1][m])
258 cost = MAX (cost, move_cost[m][*p1][j]);
260 gcc_assert (cost <= 65535);
261 move_cost[m][i][j] = cost;
263 if (reg_class_subset_p ((enum reg_class) i, (enum reg_class) j))
264 may_move_in_cost[m][i][j] = 0;
265 else
266 may_move_in_cost[m][i][j] = cost;
268 if (reg_class_subset_p ((enum reg_class) j, (enum reg_class) i))
269 may_move_out_cost[m][i][j] = 0;
270 else
271 may_move_out_cost[m][i][j] = cost;
274 else
275 for (j = 0; j < N_REG_CLASSES; j++)
277 move_cost[m][i][j] = 65535;
278 may_move_in_cost[m][i][j] = 65535;
279 may_move_out_cost[m][i][j] = 65535;
283 /* We need to save copies of some of the register information which
284 can be munged by command-line switches so we can restore it during
285 subsequent back-end reinitialization. */
286 static char saved_fixed_regs[FIRST_PSEUDO_REGISTER];
287 static char saved_call_used_regs[FIRST_PSEUDO_REGISTER];
288 #ifdef CALL_REALLY_USED_REGISTERS
289 static char saved_call_really_used_regs[FIRST_PSEUDO_REGISTER];
290 #endif
291 static const char *saved_reg_names[FIRST_PSEUDO_REGISTER];
293 /* Save the register information. */
294 void
295 save_register_info (void)
297 /* Sanity check: make sure the target macros FIXED_REGISTERS and
298 CALL_USED_REGISTERS had the right number of initializers. */
299 gcc_assert (sizeof fixed_regs == sizeof saved_fixed_regs);
300 gcc_assert (sizeof call_used_regs == sizeof saved_call_used_regs);
301 memcpy (saved_fixed_regs, fixed_regs, sizeof fixed_regs);
302 memcpy (saved_call_used_regs, call_used_regs, sizeof call_used_regs);
304 /* Likewise for call_really_used_regs. */
305 #ifdef CALL_REALLY_USED_REGISTERS
306 gcc_assert (sizeof call_really_used_regs
307 == sizeof saved_call_really_used_regs);
308 memcpy (saved_call_really_used_regs, call_really_used_regs,
309 sizeof call_really_used_regs);
310 #endif
312 /* And similarly for reg_names. */
313 gcc_assert (sizeof reg_names == sizeof saved_reg_names);
314 memcpy (saved_reg_names, reg_names, sizeof reg_names);
317 /* Restore the register information. */
318 static void
319 restore_register_info (void)
321 memcpy (fixed_regs, saved_fixed_regs, sizeof fixed_regs);
322 memcpy (call_used_regs, saved_call_used_regs, sizeof call_used_regs);
324 #ifdef CALL_REALLY_USED_REGISTERS
325 memcpy (call_really_used_regs, saved_call_really_used_regs,
326 sizeof call_really_used_regs);
327 #endif
329 memcpy (reg_names, saved_reg_names, sizeof reg_names);
332 /* After switches have been processed, which perhaps alter
333 `fixed_regs' and `call_used_regs', convert them to HARD_REG_SETs. */
334 static void
335 init_reg_sets_1 (void)
337 unsigned int i, j;
338 unsigned int /* enum machine_mode */ m;
340 restore_register_info ();
342 #ifdef REG_ALLOC_ORDER
343 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
344 inv_reg_alloc_order[reg_alloc_order[i]] = i;
345 #endif
347 /* Let the target tweak things if necessary. */
349 targetm.conditional_register_usage ();
351 /* Compute number of hard regs in each class. */
353 memset (reg_class_size, 0, sizeof reg_class_size);
354 for (i = 0; i < N_REG_CLASSES; i++)
356 bool any_nonfixed = false;
357 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
358 if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
360 reg_class_size[i]++;
361 if (!fixed_regs[j])
362 any_nonfixed = true;
364 class_only_fixed_regs[i] = !any_nonfixed;
367 /* Initialize the table of subunions.
368 reg_class_subunion[I][J] gets the largest-numbered reg-class
369 that is contained in the union of classes I and J. */
371 memset (reg_class_subunion, 0, sizeof reg_class_subunion);
372 for (i = 0; i < N_REG_CLASSES; i++)
374 for (j = 0; j < N_REG_CLASSES; j++)
376 HARD_REG_SET c;
377 int k;
379 COPY_HARD_REG_SET (c, reg_class_contents[i]);
380 IOR_HARD_REG_SET (c, reg_class_contents[j]);
381 for (k = 0; k < N_REG_CLASSES; k++)
382 if (hard_reg_set_subset_p (reg_class_contents[k], c)
383 && !hard_reg_set_subset_p (reg_class_contents[k],
384 reg_class_contents
385 [(int) reg_class_subunion[i][j]]))
386 reg_class_subunion[i][j] = (enum reg_class) k;
390 /* Initialize the table of superunions.
391 reg_class_superunion[I][J] gets the smallest-numbered reg-class
392 containing the union of classes I and J. */
394 memset (reg_class_superunion, 0, sizeof reg_class_superunion);
395 for (i = 0; i < N_REG_CLASSES; i++)
397 for (j = 0; j < N_REG_CLASSES; j++)
399 HARD_REG_SET c;
400 int k;
402 COPY_HARD_REG_SET (c, reg_class_contents[i]);
403 IOR_HARD_REG_SET (c, reg_class_contents[j]);
404 for (k = 0; k < N_REG_CLASSES; k++)
405 if (hard_reg_set_subset_p (c, reg_class_contents[k]))
406 break;
408 reg_class_superunion[i][j] = (enum reg_class) k;
412 /* Initialize the tables of subclasses and superclasses of each reg class.
413 First clear the whole table, then add the elements as they are found. */
415 for (i = 0; i < N_REG_CLASSES; i++)
417 for (j = 0; j < N_REG_CLASSES; j++)
418 reg_class_subclasses[i][j] = LIM_REG_CLASSES;
421 for (i = 0; i < N_REG_CLASSES; i++)
423 if (i == (int) NO_REGS)
424 continue;
426 for (j = i + 1; j < N_REG_CLASSES; j++)
427 if (hard_reg_set_subset_p (reg_class_contents[i],
428 reg_class_contents[j]))
430 /* Reg class I is a subclass of J.
431 Add J to the table of superclasses of I. */
432 enum reg_class *p;
434 /* Add I to the table of superclasses of J. */
435 p = &reg_class_subclasses[j][0];
436 while (*p != LIM_REG_CLASSES) p++;
437 *p = (enum reg_class) i;
441 /* Initialize "constant" tables. */
443 CLEAR_HARD_REG_SET (fixed_reg_set);
444 CLEAR_HARD_REG_SET (call_used_reg_set);
445 CLEAR_HARD_REG_SET (call_fixed_reg_set);
446 CLEAR_HARD_REG_SET (regs_invalidated_by_call);
447 if (!regs_invalidated_by_call_regset)
449 bitmap_obstack_initialize (&persistent_obstack);
450 regs_invalidated_by_call_regset = ALLOC_REG_SET (&persistent_obstack);
452 else
453 CLEAR_REG_SET (regs_invalidated_by_call_regset);
455 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
457 /* call_used_regs must include fixed_regs. */
458 gcc_assert (!fixed_regs[i] || call_used_regs[i]);
459 #ifdef CALL_REALLY_USED_REGISTERS
460 /* call_used_regs must include call_really_used_regs. */
461 gcc_assert (!call_really_used_regs[i] || call_used_regs[i]);
462 #endif
464 if (fixed_regs[i])
465 SET_HARD_REG_BIT (fixed_reg_set, i);
467 if (call_used_regs[i])
468 SET_HARD_REG_BIT (call_used_reg_set, i);
470 /* There are a couple of fixed registers that we know are safe to
471 exclude from being clobbered by calls:
473 The frame pointer is always preserved across calls. The arg
474 pointer is if it is fixed. The stack pointer usually is,
475 unless TARGET_RETURN_POPS_ARGS, in which case an explicit
476 CLOBBER will be present. If we are generating PIC code, the
477 PIC offset table register is preserved across calls, though the
478 target can override that. */
480 if (i == STACK_POINTER_REGNUM)
482 else if (global_regs[i])
484 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
485 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
487 else if (i == FRAME_POINTER_REGNUM)
489 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
490 else if (i == HARD_FRAME_POINTER_REGNUM)
492 #endif
493 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
494 else if (i == ARG_POINTER_REGNUM && fixed_regs[i])
496 #endif
497 else if (!PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
498 && i == (unsigned) PIC_OFFSET_TABLE_REGNUM && fixed_regs[i])
500 else if (CALL_REALLY_USED_REGNO_P (i))
502 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
503 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
507 COPY_HARD_REG_SET(call_fixed_reg_set, fixed_reg_set);
509 /* Preserve global registers if called more than once. */
510 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
512 if (global_regs[i])
514 fixed_regs[i] = call_used_regs[i] = 1;
515 SET_HARD_REG_BIT (fixed_reg_set, i);
516 SET_HARD_REG_BIT (call_used_reg_set, i);
517 SET_HARD_REG_BIT (call_fixed_reg_set, i);
521 memset (have_regs_of_mode, 0, sizeof (have_regs_of_mode));
522 memset (contains_reg_of_mode, 0, sizeof (contains_reg_of_mode));
523 for (m = 0; m < (unsigned int) MAX_MACHINE_MODE; m++)
525 HARD_REG_SET ok_regs;
526 CLEAR_HARD_REG_SET (ok_regs);
527 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
528 if (!fixed_regs [j] && HARD_REGNO_MODE_OK (j, (enum machine_mode) m))
529 SET_HARD_REG_BIT (ok_regs, j);
531 for (i = 0; i < N_REG_CLASSES; i++)
532 if ((targetm.class_max_nregs ((reg_class_t) i, (enum machine_mode) m)
533 <= reg_class_size[i])
534 && hard_reg_set_intersect_p (ok_regs, reg_class_contents[i]))
536 contains_reg_of_mode [i][m] = 1;
537 have_regs_of_mode [m] = 1;
541 /* Reset move_cost and friends, making sure we only free shared
542 table entries once. */
543 for (i = 0; i < MAX_MACHINE_MODE; i++)
544 if (move_cost[i])
546 for (j = 0; j < i && move_cost[i] != move_cost[j]; j++)
548 if (i == j)
550 free (move_cost[i]);
551 free (may_move_in_cost[i]);
552 free (may_move_out_cost[i]);
555 memset (move_cost, 0, sizeof move_cost);
556 memset (may_move_in_cost, 0, sizeof may_move_in_cost);
557 memset (may_move_out_cost, 0, sizeof may_move_out_cost);
558 last_mode_for_init_move_cost = -1;
561 /* Compute the table of register modes.
562 These values are used to record death information for individual registers
563 (as opposed to a multi-register mode).
564 This function might be invoked more than once, if the target has support
565 for changing register usage conventions on a per-function basis.
567 void
568 init_reg_modes_target (void)
570 int i, j;
572 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
573 for (j = 0; j < MAX_MACHINE_MODE; j++)
574 hard_regno_nregs[i][j] = HARD_REGNO_NREGS(i, (enum machine_mode)j);
576 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
578 reg_raw_mode[i] = choose_hard_reg_mode (i, 1, false);
580 /* If we couldn't find a valid mode, just use the previous mode.
581 ??? One situation in which we need to do this is on the mips where
582 HARD_REGNO_NREGS (fpreg, [SD]Fmode) returns 2. Ideally we'd like
583 to use DF mode for the even registers and VOIDmode for the odd
584 (for the cpu models where the odd ones are inaccessible). */
585 if (reg_raw_mode[i] == VOIDmode)
586 reg_raw_mode[i] = i == 0 ? word_mode : reg_raw_mode[i-1];
590 /* Finish initializing the register sets and initialize the register modes.
591 This function might be invoked more than once, if the target has support
592 for changing register usage conventions on a per-function basis.
594 void
595 init_regs (void)
597 /* This finishes what was started by init_reg_sets, but couldn't be done
598 until after register usage was specified. */
599 init_reg_sets_1 ();
602 /* The same as previous function plus initializing IRA. */
603 void
604 reinit_regs (void)
606 init_regs ();
607 /* caller_save needs to be re-initialized. */
608 caller_save_initialized_p = false;
609 ira_init ();
612 /* Initialize some fake stack-frame MEM references for use in
613 memory_move_secondary_cost. */
614 void
615 init_fake_stack_mems (void)
617 int i;
619 for (i = 0; i < MAX_MACHINE_MODE; i++)
620 top_of_stack[i] = gen_rtx_MEM ((enum machine_mode) i, stack_pointer_rtx);
624 /* Compute cost of moving data from a register of class FROM to one of
625 TO, using MODE. */
628 register_move_cost (enum machine_mode mode, reg_class_t from, reg_class_t to)
630 return targetm.register_move_cost (mode, from, to);
633 /* Compute cost of moving registers to/from memory. */
636 memory_move_cost (enum machine_mode mode, reg_class_t rclass, bool in)
638 return targetm.memory_move_cost (mode, rclass, in);
641 /* Compute extra cost of moving registers to/from memory due to reloads.
642 Only needed if secondary reloads are required for memory moves. */
644 memory_move_secondary_cost (enum machine_mode mode, reg_class_t rclass,
645 bool in)
647 reg_class_t altclass;
648 int partial_cost = 0;
649 /* We need a memory reference to feed to SECONDARY... macros. */
650 /* mem may be unused even if the SECONDARY_ macros are defined. */
651 rtx mem ATTRIBUTE_UNUSED = top_of_stack[(int) mode];
653 altclass = secondary_reload_class (in ? 1 : 0, rclass, mode, mem);
655 if (altclass == NO_REGS)
656 return 0;
658 if (in)
659 partial_cost = register_move_cost (mode, altclass, rclass);
660 else
661 partial_cost = register_move_cost (mode, rclass, altclass);
663 if (rclass == altclass)
664 /* This isn't simply a copy-to-temporary situation. Can't guess
665 what it is, so TARGET_MEMORY_MOVE_COST really ought not to be
666 calling here in that case.
668 I'm tempted to put in an assert here, but returning this will
669 probably only give poor estimates, which is what we would've
670 had before this code anyways. */
671 return partial_cost;
673 /* Check if the secondary reload register will also need a
674 secondary reload. */
675 return memory_move_secondary_cost (mode, altclass, in) + partial_cost;
678 /* Return a machine mode that is legitimate for hard reg REGNO and large
679 enough to save nregs. If we can't find one, return VOIDmode.
680 If CALL_SAVED is true, only consider modes that are call saved. */
681 enum machine_mode
682 choose_hard_reg_mode (unsigned int regno ATTRIBUTE_UNUSED,
683 unsigned int nregs, bool call_saved)
685 unsigned int /* enum machine_mode */ m;
686 enum machine_mode found_mode = VOIDmode, mode;
688 /* We first look for the largest integer mode that can be validly
689 held in REGNO. If none, we look for the largest floating-point mode.
690 If we still didn't find a valid mode, try CCmode. */
692 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
693 mode != VOIDmode;
694 mode = GET_MODE_WIDER_MODE (mode))
695 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
696 && HARD_REGNO_MODE_OK (regno, mode)
697 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
698 found_mode = mode;
700 if (found_mode != VOIDmode)
701 return found_mode;
703 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
704 mode != VOIDmode;
705 mode = GET_MODE_WIDER_MODE (mode))
706 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
707 && HARD_REGNO_MODE_OK (regno, mode)
708 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
709 found_mode = mode;
711 if (found_mode != VOIDmode)
712 return found_mode;
714 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
715 mode != VOIDmode;
716 mode = GET_MODE_WIDER_MODE (mode))
717 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
718 && HARD_REGNO_MODE_OK (regno, mode)
719 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
720 found_mode = mode;
722 if (found_mode != VOIDmode)
723 return found_mode;
725 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
726 mode != VOIDmode;
727 mode = GET_MODE_WIDER_MODE (mode))
728 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
729 && HARD_REGNO_MODE_OK (regno, mode)
730 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
731 found_mode = mode;
733 if (found_mode != VOIDmode)
734 return found_mode;
736 /* Iterate over all of the CCmodes. */
737 for (m = (unsigned int) CCmode; m < (unsigned int) NUM_MACHINE_MODES; ++m)
739 mode = (enum machine_mode) m;
740 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
741 && HARD_REGNO_MODE_OK (regno, mode)
742 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
743 return mode;
746 /* We can't find a mode valid for this register. */
747 return VOIDmode;
750 /* Specify the usage characteristics of the register named NAME.
751 It should be a fixed register if FIXED and a
752 call-used register if CALL_USED. */
753 void
754 fix_register (const char *name, int fixed, int call_used)
756 int i;
757 int reg, nregs;
759 /* Decode the name and update the primary form of
760 the register info. */
762 if ((reg = decode_reg_name_and_count (name, &nregs)) >= 0)
764 gcc_assert (nregs >= 1);
765 for (i = reg; i < reg + nregs; i++)
767 if ((i == STACK_POINTER_REGNUM
768 #ifdef HARD_FRAME_POINTER_REGNUM
769 || i == HARD_FRAME_POINTER_REGNUM
770 #else
771 || i == FRAME_POINTER_REGNUM
772 #endif
774 && (fixed == 0 || call_used == 0))
776 switch (fixed)
778 case 0:
779 switch (call_used)
781 case 0:
782 error ("can%'t use %qs as a call-saved register", name);
783 break;
785 case 1:
786 error ("can%'t use %qs as a call-used register", name);
787 break;
789 default:
790 gcc_unreachable ();
792 break;
794 case 1:
795 switch (call_used)
797 case 1:
798 error ("can%'t use %qs as a fixed register", name);
799 break;
801 case 0:
802 default:
803 gcc_unreachable ();
805 break;
807 default:
808 gcc_unreachable ();
811 else
813 fixed_regs[i] = fixed;
814 call_used_regs[i] = call_used;
815 #ifdef CALL_REALLY_USED_REGISTERS
816 if (fixed == 0)
817 call_really_used_regs[i] = call_used;
818 #endif
822 else
824 warning (0, "unknown register name: %s", name);
828 /* Mark register number I as global. */
829 void
830 globalize_reg (tree decl, int i)
832 location_t loc = DECL_SOURCE_LOCATION (decl);
834 #ifdef STACK_REGS
835 if (IN_RANGE (i, FIRST_STACK_REG, LAST_STACK_REG))
837 error ("stack register used for global register variable");
838 return;
840 #endif
842 if (fixed_regs[i] == 0 && no_global_reg_vars)
843 error_at (loc, "global register variable follows a function definition");
845 if (global_regs[i])
847 warning_at (loc, 0,
848 "register of %qD used for multiple global register variables",
849 decl);
850 inform (DECL_SOURCE_LOCATION (global_regs_decl[i]),
851 "conflicts with %qD", global_regs_decl[i]);
852 return;
855 if (call_used_regs[i] && ! fixed_regs[i])
856 warning_at (loc, 0, "call-clobbered register used for global register variable");
858 global_regs[i] = 1;
859 global_regs_decl[i] = decl;
861 /* If we're globalizing the frame pointer, we need to set the
862 appropriate regs_invalidated_by_call bit, even if it's already
863 set in fixed_regs. */
864 if (i != STACK_POINTER_REGNUM)
866 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
867 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
870 /* If already fixed, nothing else to do. */
871 if (fixed_regs[i])
872 return;
874 fixed_regs[i] = call_used_regs[i] = 1;
875 #ifdef CALL_REALLY_USED_REGISTERS
876 call_really_used_regs[i] = 1;
877 #endif
879 SET_HARD_REG_BIT (fixed_reg_set, i);
880 SET_HARD_REG_BIT (call_used_reg_set, i);
881 SET_HARD_REG_BIT (call_fixed_reg_set, i);
883 reinit_regs ();
887 /* Structure used to record preferences of given pseudo. */
888 struct reg_pref
890 /* (enum reg_class) prefclass is the preferred class. May be
891 NO_REGS if no class is better than memory. */
892 char prefclass;
894 /* altclass is a register class that we should use for allocating
895 pseudo if no register in the preferred class is available.
896 If no register in this class is available, memory is preferred.
898 It might appear to be more general to have a bitmask of classes here,
899 but since it is recommended that there be a class corresponding to the
900 union of most major pair of classes, that generality is not required. */
901 char altclass;
903 /* allocnoclass is a register class that IRA uses for allocating
904 the pseudo. */
905 char allocnoclass;
908 /* Record preferences of each pseudo. This is available after RA is
909 run. */
910 static struct reg_pref *reg_pref;
912 /* Current size of reg_info. */
913 static int reg_info_size;
915 /* Return the reg_class in which pseudo reg number REGNO is best allocated.
916 This function is sometimes called before the info has been computed.
917 When that happens, just return GENERAL_REGS, which is innocuous. */
918 enum reg_class
919 reg_preferred_class (int regno)
921 if (reg_pref == 0)
922 return GENERAL_REGS;
924 return (enum reg_class) reg_pref[regno].prefclass;
927 enum reg_class
928 reg_alternate_class (int regno)
930 if (reg_pref == 0)
931 return ALL_REGS;
933 return (enum reg_class) reg_pref[regno].altclass;
936 /* Return the reg_class which is used by IRA for its allocation. */
937 enum reg_class
938 reg_allocno_class (int regno)
940 if (reg_pref == 0)
941 return NO_REGS;
943 return (enum reg_class) reg_pref[regno].allocnoclass;
948 /* Allocate space for reg info. */
949 static void
950 allocate_reg_info (void)
952 reg_info_size = max_reg_num ();
953 gcc_assert (! reg_pref && ! reg_renumber);
954 reg_renumber = XNEWVEC (short, reg_info_size);
955 reg_pref = XCNEWVEC (struct reg_pref, reg_info_size);
956 memset (reg_renumber, -1, reg_info_size * sizeof (short));
960 /* Resize reg info. The new elements will be uninitialized. Return
961 TRUE if new elements (for new pseudos) were added. */
962 bool
963 resize_reg_info (void)
965 int old;
967 if (reg_pref == NULL)
969 allocate_reg_info ();
970 return true;
972 if (reg_info_size == max_reg_num ())
973 return false;
974 old = reg_info_size;
975 reg_info_size = max_reg_num ();
976 gcc_assert (reg_pref && reg_renumber);
977 reg_renumber = XRESIZEVEC (short, reg_renumber, reg_info_size);
978 reg_pref = XRESIZEVEC (struct reg_pref, reg_pref, reg_info_size);
979 memset (reg_pref + old, -1,
980 (reg_info_size - old) * sizeof (struct reg_pref));
981 memset (reg_renumber + old, -1, (reg_info_size - old) * sizeof (short));
982 return true;
986 /* Free up the space allocated by allocate_reg_info. */
987 void
988 free_reg_info (void)
990 if (reg_pref)
992 free (reg_pref);
993 reg_pref = NULL;
996 if (reg_renumber)
998 free (reg_renumber);
999 reg_renumber = NULL;
1003 /* Initialize some global data for this pass. */
1004 static unsigned int
1005 reginfo_init (void)
1007 if (df)
1008 df_compute_regs_ever_live (true);
1010 /* This prevents dump_flow_info from losing if called
1011 before reginfo is run. */
1012 reg_pref = NULL;
1013 /* No more global register variables may be declared. */
1014 no_global_reg_vars = 1;
1015 return 1;
1018 struct rtl_opt_pass pass_reginfo_init =
1021 RTL_PASS,
1022 "reginfo", /* name */
1023 NULL, /* gate */
1024 reginfo_init, /* execute */
1025 NULL, /* sub */
1026 NULL, /* next */
1027 0, /* static_pass_number */
1028 TV_NONE, /* tv_id */
1029 0, /* properties_required */
1030 0, /* properties_provided */
1031 0, /* properties_destroyed */
1032 0, /* todo_flags_start */
1033 0 /* todo_flags_finish */
1039 /* Set up preferred, alternate, and cover classes for REGNO as
1040 PREFCLASS, ALTCLASS, and ALLOCNOCLASS. */
1041 void
1042 setup_reg_classes (int regno,
1043 enum reg_class prefclass, enum reg_class altclass,
1044 enum reg_class allocnoclass)
1046 if (reg_pref == NULL)
1047 return;
1048 gcc_assert (reg_info_size == max_reg_num ());
1049 reg_pref[regno].prefclass = prefclass;
1050 reg_pref[regno].altclass = altclass;
1051 reg_pref[regno].allocnoclass = allocnoclass;
1055 /* This is the `regscan' pass of the compiler, run just before cse and
1056 again just before loop. It finds the first and last use of each
1057 pseudo-register. */
1059 static void reg_scan_mark_refs (rtx, rtx);
1061 void
1062 reg_scan (rtx f, unsigned int nregs ATTRIBUTE_UNUSED)
1064 rtx insn;
1066 timevar_push (TV_REG_SCAN);
1068 for (insn = f; insn; insn = NEXT_INSN (insn))
1069 if (INSN_P (insn))
1071 reg_scan_mark_refs (PATTERN (insn), insn);
1072 if (REG_NOTES (insn))
1073 reg_scan_mark_refs (REG_NOTES (insn), insn);
1076 timevar_pop (TV_REG_SCAN);
1080 /* X is the expression to scan. INSN is the insn it appears in.
1081 NOTE_FLAG is nonzero if X is from INSN's notes rather than its body.
1082 We should only record information for REGs with numbers
1083 greater than or equal to MIN_REGNO. */
1084 static void
1085 reg_scan_mark_refs (rtx x, rtx insn)
1087 enum rtx_code code;
1088 rtx dest;
1089 rtx note;
1091 if (!x)
1092 return;
1093 code = GET_CODE (x);
1094 switch (code)
1096 case CONST:
1097 case CONST_INT:
1098 case CONST_DOUBLE:
1099 case CONST_FIXED:
1100 case CONST_VECTOR:
1101 case CC0:
1102 case PC:
1103 case SYMBOL_REF:
1104 case LABEL_REF:
1105 case ADDR_VEC:
1106 case ADDR_DIFF_VEC:
1107 case REG:
1108 return;
1110 case EXPR_LIST:
1111 if (XEXP (x, 0))
1112 reg_scan_mark_refs (XEXP (x, 0), insn);
1113 if (XEXP (x, 1))
1114 reg_scan_mark_refs (XEXP (x, 1), insn);
1115 break;
1117 case INSN_LIST:
1118 if (XEXP (x, 1))
1119 reg_scan_mark_refs (XEXP (x, 1), insn);
1120 break;
1122 case CLOBBER:
1123 if (MEM_P (XEXP (x, 0)))
1124 reg_scan_mark_refs (XEXP (XEXP (x, 0), 0), insn);
1125 break;
1127 case SET:
1128 /* Count a set of the destination if it is a register. */
1129 for (dest = SET_DEST (x);
1130 GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
1131 || GET_CODE (dest) == ZERO_EXTEND;
1132 dest = XEXP (dest, 0))
1135 /* If this is setting a pseudo from another pseudo or the sum of a
1136 pseudo and a constant integer and the other pseudo is known to be
1137 a pointer, set the destination to be a pointer as well.
1139 Likewise if it is setting the destination from an address or from a
1140 value equivalent to an address or to the sum of an address and
1141 something else.
1143 But don't do any of this if the pseudo corresponds to a user
1144 variable since it should have already been set as a pointer based
1145 on the type. */
1147 if (REG_P (SET_DEST (x))
1148 && REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER
1149 /* If the destination pseudo is set more than once, then other
1150 sets might not be to a pointer value (consider access to a
1151 union in two threads of control in the presence of global
1152 optimizations). So only set REG_POINTER on the destination
1153 pseudo if this is the only set of that pseudo. */
1154 && DF_REG_DEF_COUNT (REGNO (SET_DEST (x))) == 1
1155 && ! REG_USERVAR_P (SET_DEST (x))
1156 && ! REG_POINTER (SET_DEST (x))
1157 && ((REG_P (SET_SRC (x))
1158 && REG_POINTER (SET_SRC (x)))
1159 || ((GET_CODE (SET_SRC (x)) == PLUS
1160 || GET_CODE (SET_SRC (x)) == LO_SUM)
1161 && CONST_INT_P (XEXP (SET_SRC (x), 1))
1162 && REG_P (XEXP (SET_SRC (x), 0))
1163 && REG_POINTER (XEXP (SET_SRC (x), 0)))
1164 || GET_CODE (SET_SRC (x)) == CONST
1165 || GET_CODE (SET_SRC (x)) == SYMBOL_REF
1166 || GET_CODE (SET_SRC (x)) == LABEL_REF
1167 || (GET_CODE (SET_SRC (x)) == HIGH
1168 && (GET_CODE (XEXP (SET_SRC (x), 0)) == CONST
1169 || GET_CODE (XEXP (SET_SRC (x), 0)) == SYMBOL_REF
1170 || GET_CODE (XEXP (SET_SRC (x), 0)) == LABEL_REF))
1171 || ((GET_CODE (SET_SRC (x)) == PLUS
1172 || GET_CODE (SET_SRC (x)) == LO_SUM)
1173 && (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST
1174 || GET_CODE (XEXP (SET_SRC (x), 1)) == SYMBOL_REF
1175 || GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF))
1176 || ((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
1177 && (GET_CODE (XEXP (note, 0)) == CONST
1178 || GET_CODE (XEXP (note, 0)) == SYMBOL_REF
1179 || GET_CODE (XEXP (note, 0)) == LABEL_REF))))
1180 REG_POINTER (SET_DEST (x)) = 1;
1182 /* If this is setting a register from a register or from a simple
1183 conversion of a register, propagate REG_EXPR. */
1184 if (REG_P (dest) && !REG_ATTRS (dest))
1186 rtx src = SET_SRC (x);
1188 while (GET_CODE (src) == SIGN_EXTEND
1189 || GET_CODE (src) == ZERO_EXTEND
1190 || GET_CODE (src) == TRUNCATE
1191 || (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)))
1192 src = XEXP (src, 0);
1194 set_reg_attrs_from_value (dest, src);
1197 /* ... fall through ... */
1199 default:
1201 const char *fmt = GET_RTX_FORMAT (code);
1202 int i;
1203 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1205 if (fmt[i] == 'e')
1206 reg_scan_mark_refs (XEXP (x, i), insn);
1207 else if (fmt[i] == 'E' && XVEC (x, i) != 0)
1209 int j;
1210 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1211 reg_scan_mark_refs (XVECEXP (x, i, j), insn);
1219 /* Return nonzero if C1 is a subset of C2, i.e., if every register in C1
1220 is also in C2. */
1222 reg_class_subset_p (reg_class_t c1, reg_class_t c2)
1224 return (c1 == c2
1225 || c2 == ALL_REGS
1226 || hard_reg_set_subset_p (reg_class_contents[(int) c1],
1227 reg_class_contents[(int) c2]));
1230 /* Return nonzero if there is a register that is in both C1 and C2. */
1232 reg_classes_intersect_p (reg_class_t c1, reg_class_t c2)
1234 return (c1 == c2
1235 || c1 == ALL_REGS
1236 || c2 == ALL_REGS
1237 || hard_reg_set_intersect_p (reg_class_contents[(int) c1],
1238 reg_class_contents[(int) c2]));
1243 /* Passes for keeping and updating info about modes of registers
1244 inside subregisters. */
1246 #ifdef CANNOT_CHANGE_MODE_CLASS
1248 static bitmap invalid_mode_changes;
1250 static void
1251 record_subregs_of_mode (rtx subreg, bitmap subregs_of_mode)
1253 enum machine_mode mode;
1254 unsigned int regno;
1256 if (!REG_P (SUBREG_REG (subreg)))
1257 return;
1259 regno = REGNO (SUBREG_REG (subreg));
1260 mode = GET_MODE (subreg);
1262 if (regno < FIRST_PSEUDO_REGISTER)
1263 return;
1265 if (bitmap_set_bit (subregs_of_mode,
1266 regno * NUM_MACHINE_MODES + (unsigned int) mode))
1268 unsigned int rclass;
1269 for (rclass = 0; rclass < N_REG_CLASSES; rclass++)
1270 if (!bitmap_bit_p (invalid_mode_changes,
1271 regno * N_REG_CLASSES + rclass)
1272 && CANNOT_CHANGE_MODE_CLASS (PSEUDO_REGNO_MODE (regno),
1273 mode, (enum reg_class) rclass))
1274 bitmap_set_bit (invalid_mode_changes,
1275 regno * N_REG_CLASSES + rclass);
1279 /* Call record_subregs_of_mode for all the subregs in X. */
1280 static void
1281 find_subregs_of_mode (rtx x, bitmap subregs_of_mode)
1283 enum rtx_code code = GET_CODE (x);
1284 const char * const fmt = GET_RTX_FORMAT (code);
1285 int i;
1287 if (code == SUBREG)
1288 record_subregs_of_mode (x, subregs_of_mode);
1290 /* Time for some deep diving. */
1291 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1293 if (fmt[i] == 'e')
1294 find_subregs_of_mode (XEXP (x, i), subregs_of_mode);
1295 else if (fmt[i] == 'E')
1297 int j;
1298 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1299 find_subregs_of_mode (XVECEXP (x, i, j), subregs_of_mode);
1304 void
1305 init_subregs_of_mode (void)
1307 basic_block bb;
1308 rtx insn;
1309 bitmap_obstack srom_obstack;
1310 bitmap subregs_of_mode;
1312 gcc_assert (invalid_mode_changes == NULL);
1313 invalid_mode_changes = BITMAP_ALLOC (NULL);
1314 bitmap_obstack_initialize (&srom_obstack);
1315 subregs_of_mode = BITMAP_ALLOC (&srom_obstack);
1317 FOR_EACH_BB (bb)
1318 FOR_BB_INSNS (bb, insn)
1319 if (NONDEBUG_INSN_P (insn))
1320 find_subregs_of_mode (PATTERN (insn), subregs_of_mode);
1322 BITMAP_FREE (subregs_of_mode);
1323 bitmap_obstack_release (&srom_obstack);
1326 /* Return 1 if REGNO has had an invalid mode change in CLASS from FROM
1327 mode. */
1328 bool
1329 invalid_mode_change_p (unsigned int regno,
1330 enum reg_class rclass)
1332 return bitmap_bit_p (invalid_mode_changes,
1333 regno * N_REG_CLASSES + (unsigned) rclass);
1336 void
1337 finish_subregs_of_mode (void)
1339 BITMAP_FREE (invalid_mode_changes);
1341 #else
1342 void
1343 init_subregs_of_mode (void)
1346 void
1347 finish_subregs_of_mode (void)
1351 #endif /* CANNOT_CHANGE_MODE_CLASS */