ipa-utils.h (polymorphic_call_context): Add metdhos dump, debug and clear_outer_type.
[official-gcc.git] / gcc / reginfo.c
blob226152278b4a03a03883830476d3bc1ab3f5bad5
1 /* Compute different info about registers.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file contains regscan pass of the compiler and passes for
22 dealing with info about modes of pseudo-registers inside
23 subregisters. It also defines some tables of information about the
24 hardware registers, function init_reg_sets to initialize the
25 tables, and other auxiliary functions to deal with info about
26 registers and their classes. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "hard-reg-set.h"
33 #include "tree.h"
34 #include "rtl.h"
35 #include "expr.h"
36 #include "tm_p.h"
37 #include "flags.h"
38 #include "basic-block.h"
39 #include "regs.h"
40 #include "addresses.h"
41 #include "function.h"
42 #include "insn-config.h"
43 #include "recog.h"
44 #include "reload.h"
45 #include "diagnostic-core.h"
46 #include "output.h"
47 #include "hashtab.h"
48 #include "target.h"
49 #include "tree-pass.h"
50 #include "df.h"
51 #include "ira.h"
53 /* Maximum register number used in this function, plus one. */
55 int max_regno;
57 /* Used to cache the results of simplifiable_subregs. SHAPE is the input
58 parameter and SIMPLIFIABLE_REGS is the result. */
59 struct simplifiable_subreg
61 simplifiable_subreg (const subreg_shape &);
63 subreg_shape shape;
64 HARD_REG_SET simplifiable_regs;
67 struct simplifiable_subregs_hasher : typed_noop_remove <simplifiable_subreg>
69 typedef simplifiable_subreg value_type;
70 typedef subreg_shape compare_type;
72 static inline hashval_t hash (const value_type *);
73 static inline bool equal (const value_type *, const compare_type *);
76 struct target_hard_regs default_target_hard_regs;
77 struct target_regs default_target_regs;
78 #if SWITCHABLE_TARGET
79 struct target_hard_regs *this_target_hard_regs = &default_target_hard_regs;
80 struct target_regs *this_target_regs = &default_target_regs;
81 #endif
83 /* Data for initializing fixed_regs. */
84 static const char initial_fixed_regs[] = FIXED_REGISTERS;
86 /* Data for initializing call_used_regs. */
87 static const char initial_call_used_regs[] = CALL_USED_REGISTERS;
89 #ifdef CALL_REALLY_USED_REGISTERS
90 /* Data for initializing call_really_used_regs. */
91 static const char initial_call_really_used_regs[] = CALL_REALLY_USED_REGISTERS;
92 #endif
94 #ifdef CALL_REALLY_USED_REGISTERS
95 #define CALL_REALLY_USED_REGNO_P(X) call_really_used_regs[X]
96 #else
97 #define CALL_REALLY_USED_REGNO_P(X) call_used_regs[X]
98 #endif
100 /* Indexed by hard register number, contains 1 for registers
101 that are being used for global register decls.
102 These must be exempt from ordinary flow analysis
103 and are also considered fixed. */
104 char global_regs[FIRST_PSEUDO_REGISTER];
106 /* Declaration for the global register. */
107 tree global_regs_decl[FIRST_PSEUDO_REGISTER];
109 /* Same information as REGS_INVALIDATED_BY_CALL but in regset form to be used
110 in dataflow more conveniently. */
111 regset regs_invalidated_by_call_regset;
113 /* Same information as FIXED_REG_SET but in regset form. */
114 regset fixed_reg_set_regset;
116 /* The bitmap_obstack is used to hold some static variables that
117 should not be reset after each function is compiled. */
118 static bitmap_obstack persistent_obstack;
120 /* Used to initialize reg_alloc_order. */
121 #ifdef REG_ALLOC_ORDER
122 static int initial_reg_alloc_order[FIRST_PSEUDO_REGISTER] = REG_ALLOC_ORDER;
123 #endif
125 /* The same information, but as an array of unsigned ints. We copy from
126 these unsigned ints to the table above. We do this so the tm.h files
127 do not have to be aware of the wordsize for machines with <= 64 regs.
128 Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
129 #define N_REG_INTS \
130 ((FIRST_PSEUDO_REGISTER + (32 - 1)) / 32)
132 static const unsigned int_reg_class_contents[N_REG_CLASSES][N_REG_INTS]
133 = REG_CLASS_CONTENTS;
135 /* Array containing all of the register names. */
136 static const char *const initial_reg_names[] = REGISTER_NAMES;
138 /* Array containing all of the register class names. */
139 const char * reg_class_names[] = REG_CLASS_NAMES;
141 /* No more global register variables may be declared; true once
142 reginfo has been initialized. */
143 static int no_global_reg_vars = 0;
145 /* Given a register bitmap, turn on the bits in a HARD_REG_SET that
146 correspond to the hard registers, if any, set in that map. This
147 could be done far more efficiently by having all sorts of special-cases
148 with moving single words, but probably isn't worth the trouble. */
149 void
150 reg_set_to_hard_reg_set (HARD_REG_SET *to, const_bitmap from)
152 unsigned i;
153 bitmap_iterator bi;
155 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
157 if (i >= FIRST_PSEUDO_REGISTER)
158 return;
159 SET_HARD_REG_BIT (*to, i);
163 /* Function called only once per target_globals to initialize the
164 target_hard_regs structure. Once this is done, various switches
165 may override. */
166 void
167 init_reg_sets (void)
169 int i, j;
171 /* First copy the register information from the initial int form into
172 the regsets. */
174 for (i = 0; i < N_REG_CLASSES; i++)
176 CLEAR_HARD_REG_SET (reg_class_contents[i]);
178 /* Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
179 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
180 if (int_reg_class_contents[i][j / 32]
181 & ((unsigned) 1 << (j % 32)))
182 SET_HARD_REG_BIT (reg_class_contents[i], j);
185 /* Sanity check: make sure the target macros FIXED_REGISTERS and
186 CALL_USED_REGISTERS had the right number of initializers. */
187 gcc_assert (sizeof fixed_regs == sizeof initial_fixed_regs);
188 gcc_assert (sizeof call_used_regs == sizeof initial_call_used_regs);
189 #ifdef CALL_REALLY_USED_REGISTERS
190 gcc_assert (sizeof call_really_used_regs
191 == sizeof initial_call_really_used_regs);
192 #endif
193 #ifdef REG_ALLOC_ORDER
194 gcc_assert (sizeof reg_alloc_order == sizeof initial_reg_alloc_order);
195 #endif
196 gcc_assert (sizeof reg_names == sizeof initial_reg_names);
198 memcpy (fixed_regs, initial_fixed_regs, sizeof fixed_regs);
199 memcpy (call_used_regs, initial_call_used_regs, sizeof call_used_regs);
200 #ifdef CALL_REALLY_USED_REGISTERS
201 memcpy (call_really_used_regs, initial_call_really_used_regs,
202 sizeof call_really_used_regs);
203 #endif
204 #ifdef REG_ALLOC_ORDER
205 memcpy (reg_alloc_order, initial_reg_alloc_order, sizeof reg_alloc_order);
206 #endif
207 memcpy (reg_names, initial_reg_names, sizeof reg_names);
209 SET_HARD_REG_SET (accessible_reg_set);
210 SET_HARD_REG_SET (operand_reg_set);
213 /* We need to save copies of some of the register information which
214 can be munged by command-line switches so we can restore it during
215 subsequent back-end reinitialization. */
216 static char saved_fixed_regs[FIRST_PSEUDO_REGISTER];
217 static char saved_call_used_regs[FIRST_PSEUDO_REGISTER];
218 #ifdef CALL_REALLY_USED_REGISTERS
219 static char saved_call_really_used_regs[FIRST_PSEUDO_REGISTER];
220 #endif
221 static const char *saved_reg_names[FIRST_PSEUDO_REGISTER];
222 static HARD_REG_SET saved_accessible_reg_set;
223 static HARD_REG_SET saved_operand_reg_set;
225 /* Save the register information. */
226 void
227 save_register_info (void)
229 /* Sanity check: make sure the target macros FIXED_REGISTERS and
230 CALL_USED_REGISTERS had the right number of initializers. */
231 gcc_assert (sizeof fixed_regs == sizeof saved_fixed_regs);
232 gcc_assert (sizeof call_used_regs == sizeof saved_call_used_regs);
233 memcpy (saved_fixed_regs, fixed_regs, sizeof fixed_regs);
234 memcpy (saved_call_used_regs, call_used_regs, sizeof call_used_regs);
236 /* Likewise for call_really_used_regs. */
237 #ifdef CALL_REALLY_USED_REGISTERS
238 gcc_assert (sizeof call_really_used_regs
239 == sizeof saved_call_really_used_regs);
240 memcpy (saved_call_really_used_regs, call_really_used_regs,
241 sizeof call_really_used_regs);
242 #endif
244 /* And similarly for reg_names. */
245 gcc_assert (sizeof reg_names == sizeof saved_reg_names);
246 memcpy (saved_reg_names, reg_names, sizeof reg_names);
247 COPY_HARD_REG_SET (saved_accessible_reg_set, accessible_reg_set);
248 COPY_HARD_REG_SET (saved_operand_reg_set, operand_reg_set);
251 /* Restore the register information. */
252 static void
253 restore_register_info (void)
255 memcpy (fixed_regs, saved_fixed_regs, sizeof fixed_regs);
256 memcpy (call_used_regs, saved_call_used_regs, sizeof call_used_regs);
258 #ifdef CALL_REALLY_USED_REGISTERS
259 memcpy (call_really_used_regs, saved_call_really_used_regs,
260 sizeof call_really_used_regs);
261 #endif
263 memcpy (reg_names, saved_reg_names, sizeof reg_names);
264 COPY_HARD_REG_SET (accessible_reg_set, saved_accessible_reg_set);
265 COPY_HARD_REG_SET (operand_reg_set, saved_operand_reg_set);
268 /* After switches have been processed, which perhaps alter
269 `fixed_regs' and `call_used_regs', convert them to HARD_REG_SETs. */
270 static void
271 init_reg_sets_1 (void)
273 unsigned int i, j;
274 unsigned int /* enum machine_mode */ m;
276 restore_register_info ();
278 #ifdef REG_ALLOC_ORDER
279 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
280 inv_reg_alloc_order[reg_alloc_order[i]] = i;
281 #endif
283 /* Let the target tweak things if necessary. */
285 targetm.conditional_register_usage ();
287 /* Compute number of hard regs in each class. */
289 memset (reg_class_size, 0, sizeof reg_class_size);
290 for (i = 0; i < N_REG_CLASSES; i++)
292 bool any_nonfixed = false;
293 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
294 if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
296 reg_class_size[i]++;
297 if (!fixed_regs[j])
298 any_nonfixed = true;
300 class_only_fixed_regs[i] = !any_nonfixed;
303 /* Initialize the table of subunions.
304 reg_class_subunion[I][J] gets the largest-numbered reg-class
305 that is contained in the union of classes I and J. */
307 memset (reg_class_subunion, 0, sizeof reg_class_subunion);
308 for (i = 0; i < N_REG_CLASSES; i++)
310 for (j = 0; j < N_REG_CLASSES; j++)
312 HARD_REG_SET c;
313 int k;
315 COPY_HARD_REG_SET (c, reg_class_contents[i]);
316 IOR_HARD_REG_SET (c, reg_class_contents[j]);
317 for (k = 0; k < N_REG_CLASSES; k++)
318 if (hard_reg_set_subset_p (reg_class_contents[k], c)
319 && !hard_reg_set_subset_p (reg_class_contents[k],
320 reg_class_contents
321 [(int) reg_class_subunion[i][j]]))
322 reg_class_subunion[i][j] = (enum reg_class) k;
326 /* Initialize the table of superunions.
327 reg_class_superunion[I][J] gets the smallest-numbered reg-class
328 containing the union of classes I and J. */
330 memset (reg_class_superunion, 0, sizeof reg_class_superunion);
331 for (i = 0; i < N_REG_CLASSES; i++)
333 for (j = 0; j < N_REG_CLASSES; j++)
335 HARD_REG_SET c;
336 int k;
338 COPY_HARD_REG_SET (c, reg_class_contents[i]);
339 IOR_HARD_REG_SET (c, reg_class_contents[j]);
340 for (k = 0; k < N_REG_CLASSES; k++)
341 if (hard_reg_set_subset_p (c, reg_class_contents[k]))
342 break;
344 reg_class_superunion[i][j] = (enum reg_class) k;
348 /* Initialize the tables of subclasses and superclasses of each reg class.
349 First clear the whole table, then add the elements as they are found. */
351 for (i = 0; i < N_REG_CLASSES; i++)
353 for (j = 0; j < N_REG_CLASSES; j++)
354 reg_class_subclasses[i][j] = LIM_REG_CLASSES;
357 for (i = 0; i < N_REG_CLASSES; i++)
359 if (i == (int) NO_REGS)
360 continue;
362 for (j = i + 1; j < N_REG_CLASSES; j++)
363 if (hard_reg_set_subset_p (reg_class_contents[i],
364 reg_class_contents[j]))
366 /* Reg class I is a subclass of J.
367 Add J to the table of superclasses of I. */
368 enum reg_class *p;
370 /* Add I to the table of superclasses of J. */
371 p = &reg_class_subclasses[j][0];
372 while (*p != LIM_REG_CLASSES) p++;
373 *p = (enum reg_class) i;
377 /* Initialize "constant" tables. */
379 CLEAR_HARD_REG_SET (fixed_reg_set);
380 CLEAR_HARD_REG_SET (call_used_reg_set);
381 CLEAR_HARD_REG_SET (call_fixed_reg_set);
382 CLEAR_HARD_REG_SET (regs_invalidated_by_call);
383 if (!regs_invalidated_by_call_regset)
385 bitmap_obstack_initialize (&persistent_obstack);
386 regs_invalidated_by_call_regset = ALLOC_REG_SET (&persistent_obstack);
388 else
389 CLEAR_REG_SET (regs_invalidated_by_call_regset);
390 if (!fixed_reg_set_regset)
391 fixed_reg_set_regset = ALLOC_REG_SET (&persistent_obstack);
392 else
393 CLEAR_REG_SET (fixed_reg_set_regset);
395 AND_HARD_REG_SET (operand_reg_set, accessible_reg_set);
396 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
398 /* As a special exception, registers whose class is NO_REGS are
399 not accepted by `register_operand'. The reason for this change
400 is to allow the representation of special architecture artifacts
401 (such as a condition code register) without extending the rtl
402 definitions. Since registers of class NO_REGS cannot be used
403 as registers in any case where register classes are examined,
404 it is better to apply this exception in a target-independent way. */
405 if (REGNO_REG_CLASS (i) == NO_REGS)
406 CLEAR_HARD_REG_BIT (operand_reg_set, i);
408 /* If a register is too limited to be treated as a register operand,
409 then it should never be allocated to a pseudo. */
410 if (!TEST_HARD_REG_BIT (operand_reg_set, i))
412 fixed_regs[i] = 1;
413 call_used_regs[i] = 1;
416 /* call_used_regs must include fixed_regs. */
417 gcc_assert (!fixed_regs[i] || call_used_regs[i]);
418 #ifdef CALL_REALLY_USED_REGISTERS
419 /* call_used_regs must include call_really_used_regs. */
420 gcc_assert (!call_really_used_regs[i] || call_used_regs[i]);
421 #endif
423 if (fixed_regs[i])
425 SET_HARD_REG_BIT (fixed_reg_set, i);
426 SET_REGNO_REG_SET (fixed_reg_set_regset, i);
429 if (call_used_regs[i])
430 SET_HARD_REG_BIT (call_used_reg_set, i);
432 /* There are a couple of fixed registers that we know are safe to
433 exclude from being clobbered by calls:
435 The frame pointer is always preserved across calls. The arg
436 pointer is if it is fixed. The stack pointer usually is,
437 unless TARGET_RETURN_POPS_ARGS, in which case an explicit
438 CLOBBER will be present. If we are generating PIC code, the
439 PIC offset table register is preserved across calls, though the
440 target can override that. */
442 if (i == STACK_POINTER_REGNUM)
444 else if (global_regs[i])
446 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
447 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
449 else if (i == FRAME_POINTER_REGNUM)
451 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
452 else if (i == HARD_FRAME_POINTER_REGNUM)
454 #endif
455 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
456 else if (i == ARG_POINTER_REGNUM && fixed_regs[i])
458 #endif
459 else if (!PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
460 && i == (unsigned) PIC_OFFSET_TABLE_REGNUM && fixed_regs[i])
462 else if (CALL_REALLY_USED_REGNO_P (i))
464 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
465 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
469 COPY_HARD_REG_SET (call_fixed_reg_set, fixed_reg_set);
471 /* Preserve global registers if called more than once. */
472 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
474 if (global_regs[i])
476 fixed_regs[i] = call_used_regs[i] = 1;
477 SET_HARD_REG_BIT (fixed_reg_set, i);
478 SET_HARD_REG_BIT (call_used_reg_set, i);
479 SET_HARD_REG_BIT (call_fixed_reg_set, i);
483 memset (have_regs_of_mode, 0, sizeof (have_regs_of_mode));
484 memset (contains_reg_of_mode, 0, sizeof (contains_reg_of_mode));
485 for (m = 0; m < (unsigned int) MAX_MACHINE_MODE; m++)
487 HARD_REG_SET ok_regs;
488 CLEAR_HARD_REG_SET (ok_regs);
489 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
490 if (!fixed_regs [j] && HARD_REGNO_MODE_OK (j, (enum machine_mode) m))
491 SET_HARD_REG_BIT (ok_regs, j);
493 for (i = 0; i < N_REG_CLASSES; i++)
494 if ((targetm.class_max_nregs ((reg_class_t) i, (enum machine_mode) m)
495 <= reg_class_size[i])
496 && hard_reg_set_intersect_p (ok_regs, reg_class_contents[i]))
498 contains_reg_of_mode [i][m] = 1;
499 have_regs_of_mode [m] = 1;
504 /* Compute the table of register modes.
505 These values are used to record death information for individual registers
506 (as opposed to a multi-register mode).
507 This function might be invoked more than once, if the target has support
508 for changing register usage conventions on a per-function basis.
510 void
511 init_reg_modes_target (void)
513 int i, j;
515 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
516 for (j = 0; j < MAX_MACHINE_MODE; j++)
517 hard_regno_nregs[i][j] = HARD_REGNO_NREGS (i, (enum machine_mode)j);
519 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
521 reg_raw_mode[i] = choose_hard_reg_mode (i, 1, false);
523 /* If we couldn't find a valid mode, just use the previous mode
524 if it is suitable, otherwise fall back on word_mode. */
525 if (reg_raw_mode[i] == VOIDmode)
527 if (i > 0 && hard_regno_nregs[i][reg_raw_mode[i - 1]] == 1)
528 reg_raw_mode[i] = reg_raw_mode[i - 1];
529 else
530 reg_raw_mode[i] = word_mode;
535 /* Finish initializing the register sets and initialize the register modes.
536 This function might be invoked more than once, if the target has support
537 for changing register usage conventions on a per-function basis.
539 void
540 init_regs (void)
542 /* This finishes what was started by init_reg_sets, but couldn't be done
543 until after register usage was specified. */
544 init_reg_sets_1 ();
547 /* The same as previous function plus initializing IRA. */
548 void
549 reinit_regs (void)
551 init_regs ();
552 /* caller_save needs to be re-initialized. */
553 caller_save_initialized_p = false;
554 if (this_target_rtl->target_specific_initialized)
556 ira_init ();
557 recog_init ();
561 /* Initialize some fake stack-frame MEM references for use in
562 memory_move_secondary_cost. */
563 void
564 init_fake_stack_mems (void)
566 int i;
568 for (i = 0; i < MAX_MACHINE_MODE; i++)
569 top_of_stack[i] = gen_rtx_MEM ((enum machine_mode) i, stack_pointer_rtx);
573 /* Compute cost of moving data from a register of class FROM to one of
574 TO, using MODE. */
577 register_move_cost (enum machine_mode mode, reg_class_t from, reg_class_t to)
579 return targetm.register_move_cost (mode, from, to);
582 /* Compute cost of moving registers to/from memory. */
585 memory_move_cost (enum machine_mode mode, reg_class_t rclass, bool in)
587 return targetm.memory_move_cost (mode, rclass, in);
590 /* Compute extra cost of moving registers to/from memory due to reloads.
591 Only needed if secondary reloads are required for memory moves. */
593 memory_move_secondary_cost (enum machine_mode mode, reg_class_t rclass,
594 bool in)
596 reg_class_t altclass;
597 int partial_cost = 0;
598 /* We need a memory reference to feed to SECONDARY... macros. */
599 /* mem may be unused even if the SECONDARY_ macros are defined. */
600 rtx mem ATTRIBUTE_UNUSED = top_of_stack[(int) mode];
602 altclass = secondary_reload_class (in ? 1 : 0, rclass, mode, mem);
604 if (altclass == NO_REGS)
605 return 0;
607 if (in)
608 partial_cost = register_move_cost (mode, altclass, rclass);
609 else
610 partial_cost = register_move_cost (mode, rclass, altclass);
612 if (rclass == altclass)
613 /* This isn't simply a copy-to-temporary situation. Can't guess
614 what it is, so TARGET_MEMORY_MOVE_COST really ought not to be
615 calling here in that case.
617 I'm tempted to put in an assert here, but returning this will
618 probably only give poor estimates, which is what we would've
619 had before this code anyways. */
620 return partial_cost;
622 /* Check if the secondary reload register will also need a
623 secondary reload. */
624 return memory_move_secondary_cost (mode, altclass, in) + partial_cost;
627 /* Return a machine mode that is legitimate for hard reg REGNO and large
628 enough to save nregs. If we can't find one, return VOIDmode.
629 If CALL_SAVED is true, only consider modes that are call saved. */
630 enum machine_mode
631 choose_hard_reg_mode (unsigned int regno ATTRIBUTE_UNUSED,
632 unsigned int nregs, bool call_saved)
634 unsigned int /* enum machine_mode */ m;
635 enum machine_mode found_mode = VOIDmode, mode;
637 /* We first look for the largest integer mode that can be validly
638 held in REGNO. If none, we look for the largest floating-point mode.
639 If we still didn't find a valid mode, try CCmode. */
641 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
642 mode != VOIDmode;
643 mode = GET_MODE_WIDER_MODE (mode))
644 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
645 && HARD_REGNO_MODE_OK (regno, mode)
646 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
647 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (found_mode))
648 found_mode = mode;
650 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
651 mode != VOIDmode;
652 mode = GET_MODE_WIDER_MODE (mode))
653 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
654 && HARD_REGNO_MODE_OK (regno, mode)
655 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
656 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (found_mode))
657 found_mode = mode;
659 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
660 mode != VOIDmode;
661 mode = GET_MODE_WIDER_MODE (mode))
662 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
663 && HARD_REGNO_MODE_OK (regno, mode)
664 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
665 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (found_mode))
666 found_mode = mode;
668 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
669 mode != VOIDmode;
670 mode = GET_MODE_WIDER_MODE (mode))
671 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
672 && HARD_REGNO_MODE_OK (regno, mode)
673 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
674 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (found_mode))
675 found_mode = mode;
677 if (found_mode != VOIDmode)
678 return found_mode;
680 /* Iterate over all of the CCmodes. */
681 for (m = (unsigned int) CCmode; m < (unsigned int) NUM_MACHINE_MODES; ++m)
683 mode = (enum machine_mode) m;
684 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
685 && HARD_REGNO_MODE_OK (regno, mode)
686 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
687 return mode;
690 /* We can't find a mode valid for this register. */
691 return VOIDmode;
694 /* Specify the usage characteristics of the register named NAME.
695 It should be a fixed register if FIXED and a
696 call-used register if CALL_USED. */
697 void
698 fix_register (const char *name, int fixed, int call_used)
700 int i;
701 int reg, nregs;
703 /* Decode the name and update the primary form of
704 the register info. */
706 if ((reg = decode_reg_name_and_count (name, &nregs)) >= 0)
708 gcc_assert (nregs >= 1);
709 for (i = reg; i < reg + nregs; i++)
711 if ((i == STACK_POINTER_REGNUM
712 #ifdef HARD_FRAME_POINTER_REGNUM
713 || i == HARD_FRAME_POINTER_REGNUM
714 #else
715 || i == FRAME_POINTER_REGNUM
716 #endif
718 && (fixed == 0 || call_used == 0))
720 switch (fixed)
722 case 0:
723 switch (call_used)
725 case 0:
726 error ("can%'t use %qs as a call-saved register", name);
727 break;
729 case 1:
730 error ("can%'t use %qs as a call-used register", name);
731 break;
733 default:
734 gcc_unreachable ();
736 break;
738 case 1:
739 switch (call_used)
741 case 1:
742 error ("can%'t use %qs as a fixed register", name);
743 break;
745 case 0:
746 default:
747 gcc_unreachable ();
749 break;
751 default:
752 gcc_unreachable ();
755 else
757 fixed_regs[i] = fixed;
758 call_used_regs[i] = call_used;
759 #ifdef CALL_REALLY_USED_REGISTERS
760 if (fixed == 0)
761 call_really_used_regs[i] = call_used;
762 #endif
766 else
768 warning (0, "unknown register name: %s", name);
772 /* Mark register number I as global. */
773 void
774 globalize_reg (tree decl, int i)
776 location_t loc = DECL_SOURCE_LOCATION (decl);
778 #ifdef STACK_REGS
779 if (IN_RANGE (i, FIRST_STACK_REG, LAST_STACK_REG))
781 error ("stack register used for global register variable");
782 return;
784 #endif
786 if (fixed_regs[i] == 0 && no_global_reg_vars)
787 error_at (loc, "global register variable follows a function definition");
789 if (global_regs[i])
791 warning_at (loc, 0,
792 "register of %qD used for multiple global register variables",
793 decl);
794 inform (DECL_SOURCE_LOCATION (global_regs_decl[i]),
795 "conflicts with %qD", global_regs_decl[i]);
796 return;
799 if (call_used_regs[i] && ! fixed_regs[i])
800 warning_at (loc, 0, "call-clobbered register used for global register variable");
802 global_regs[i] = 1;
803 global_regs_decl[i] = decl;
805 /* If we're globalizing the frame pointer, we need to set the
806 appropriate regs_invalidated_by_call bit, even if it's already
807 set in fixed_regs. */
808 if (i != STACK_POINTER_REGNUM)
810 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
811 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
814 /* If already fixed, nothing else to do. */
815 if (fixed_regs[i])
816 return;
818 fixed_regs[i] = call_used_regs[i] = 1;
819 #ifdef CALL_REALLY_USED_REGISTERS
820 call_really_used_regs[i] = 1;
821 #endif
823 SET_HARD_REG_BIT (fixed_reg_set, i);
824 SET_HARD_REG_BIT (call_used_reg_set, i);
825 SET_HARD_REG_BIT (call_fixed_reg_set, i);
827 reinit_regs ();
831 /* Structure used to record preferences of given pseudo. */
832 struct reg_pref
834 /* (enum reg_class) prefclass is the preferred class. May be
835 NO_REGS if no class is better than memory. */
836 char prefclass;
838 /* altclass is a register class that we should use for allocating
839 pseudo if no register in the preferred class is available.
840 If no register in this class is available, memory is preferred.
842 It might appear to be more general to have a bitmask of classes here,
843 but since it is recommended that there be a class corresponding to the
844 union of most major pair of classes, that generality is not required. */
845 char altclass;
847 /* allocnoclass is a register class that IRA uses for allocating
848 the pseudo. */
849 char allocnoclass;
852 /* Record preferences of each pseudo. This is available after RA is
853 run. */
854 static struct reg_pref *reg_pref;
856 /* Current size of reg_info. */
857 static int reg_info_size;
858 /* Max_reg_num still last resize_reg_info call. */
859 static int max_regno_since_last_resize;
861 /* Return the reg_class in which pseudo reg number REGNO is best allocated.
862 This function is sometimes called before the info has been computed.
863 When that happens, just return GENERAL_REGS, which is innocuous. */
864 enum reg_class
865 reg_preferred_class (int regno)
867 if (reg_pref == 0)
868 return GENERAL_REGS;
870 gcc_assert (regno < reg_info_size);
871 return (enum reg_class) reg_pref[regno].prefclass;
874 enum reg_class
875 reg_alternate_class (int regno)
877 if (reg_pref == 0)
878 return ALL_REGS;
880 gcc_assert (regno < reg_info_size);
881 return (enum reg_class) reg_pref[regno].altclass;
884 /* Return the reg_class which is used by IRA for its allocation. */
885 enum reg_class
886 reg_allocno_class (int regno)
888 if (reg_pref == 0)
889 return NO_REGS;
891 gcc_assert (regno < reg_info_size);
892 return (enum reg_class) reg_pref[regno].allocnoclass;
897 /* Allocate space for reg info and initilize it. */
898 static void
899 allocate_reg_info (void)
901 int i;
903 max_regno_since_last_resize = max_reg_num ();
904 reg_info_size = max_regno_since_last_resize * 3 / 2 + 1;
905 gcc_assert (! reg_pref && ! reg_renumber);
906 reg_renumber = XNEWVEC (short, reg_info_size);
907 reg_pref = XCNEWVEC (struct reg_pref, reg_info_size);
908 memset (reg_renumber, -1, reg_info_size * sizeof (short));
909 for (i = 0; i < reg_info_size; i++)
911 reg_pref[i].prefclass = GENERAL_REGS;
912 reg_pref[i].altclass = ALL_REGS;
913 reg_pref[i].allocnoclass = GENERAL_REGS;
918 /* Resize reg info. The new elements will be initialized. Return TRUE
919 if new pseudos were added since the last call. */
920 bool
921 resize_reg_info (void)
923 int old, i;
924 bool change_p;
926 if (reg_pref == NULL)
928 allocate_reg_info ();
929 return true;
931 change_p = max_regno_since_last_resize != max_reg_num ();
932 max_regno_since_last_resize = max_reg_num ();
933 if (reg_info_size >= max_reg_num ())
934 return change_p;
935 old = reg_info_size;
936 reg_info_size = max_reg_num () * 3 / 2 + 1;
937 gcc_assert (reg_pref && reg_renumber);
938 reg_renumber = XRESIZEVEC (short, reg_renumber, reg_info_size);
939 reg_pref = XRESIZEVEC (struct reg_pref, reg_pref, reg_info_size);
940 memset (reg_pref + old, -1,
941 (reg_info_size - old) * sizeof (struct reg_pref));
942 memset (reg_renumber + old, -1, (reg_info_size - old) * sizeof (short));
943 for (i = old; i < reg_info_size; i++)
945 reg_pref[i].prefclass = GENERAL_REGS;
946 reg_pref[i].altclass = ALL_REGS;
947 reg_pref[i].allocnoclass = GENERAL_REGS;
949 return true;
953 /* Free up the space allocated by allocate_reg_info. */
954 void
955 free_reg_info (void)
957 if (reg_pref)
959 free (reg_pref);
960 reg_pref = NULL;
963 if (reg_renumber)
965 free (reg_renumber);
966 reg_renumber = NULL;
970 /* Initialize some global data for this pass. */
971 static unsigned int
972 reginfo_init (void)
974 if (df)
975 df_compute_regs_ever_live (true);
977 /* This prevents dump_reg_info from losing if called
978 before reginfo is run. */
979 reg_pref = NULL;
980 reg_info_size = max_regno_since_last_resize = 0;
981 /* No more global register variables may be declared. */
982 no_global_reg_vars = 1;
983 return 1;
986 namespace {
988 const pass_data pass_data_reginfo_init =
990 RTL_PASS, /* type */
991 "reginfo", /* name */
992 OPTGROUP_NONE, /* optinfo_flags */
993 TV_NONE, /* tv_id */
994 0, /* properties_required */
995 0, /* properties_provided */
996 0, /* properties_destroyed */
997 0, /* todo_flags_start */
998 0, /* todo_flags_finish */
1001 class pass_reginfo_init : public rtl_opt_pass
1003 public:
1004 pass_reginfo_init (gcc::context *ctxt)
1005 : rtl_opt_pass (pass_data_reginfo_init, ctxt)
1008 /* opt_pass methods: */
1009 virtual unsigned int execute (function *) { return reginfo_init (); }
1011 }; // class pass_reginfo_init
1013 } // anon namespace
1015 rtl_opt_pass *
1016 make_pass_reginfo_init (gcc::context *ctxt)
1018 return new pass_reginfo_init (ctxt);
1023 /* Set up preferred, alternate, and allocno classes for REGNO as
1024 PREFCLASS, ALTCLASS, and ALLOCNOCLASS. */
1025 void
1026 setup_reg_classes (int regno,
1027 enum reg_class prefclass, enum reg_class altclass,
1028 enum reg_class allocnoclass)
1030 if (reg_pref == NULL)
1031 return;
1032 gcc_assert (reg_info_size >= max_reg_num ());
1033 reg_pref[regno].prefclass = prefclass;
1034 reg_pref[regno].altclass = altclass;
1035 reg_pref[regno].allocnoclass = allocnoclass;
1039 /* This is the `regscan' pass of the compiler, run just before cse and
1040 again just before loop. It finds the first and last use of each
1041 pseudo-register. */
1043 static void reg_scan_mark_refs (rtx, rtx_insn *);
1045 void
1046 reg_scan (rtx_insn *f, unsigned int nregs ATTRIBUTE_UNUSED)
1048 rtx_insn *insn;
1050 timevar_push (TV_REG_SCAN);
1052 for (insn = f; insn; insn = NEXT_INSN (insn))
1053 if (INSN_P (insn))
1055 reg_scan_mark_refs (PATTERN (insn), insn);
1056 if (REG_NOTES (insn))
1057 reg_scan_mark_refs (REG_NOTES (insn), insn);
1060 timevar_pop (TV_REG_SCAN);
1064 /* X is the expression to scan. INSN is the insn it appears in.
1065 NOTE_FLAG is nonzero if X is from INSN's notes rather than its body.
1066 We should only record information for REGs with numbers
1067 greater than or equal to MIN_REGNO. */
1068 static void
1069 reg_scan_mark_refs (rtx x, rtx_insn *insn)
1071 enum rtx_code code;
1072 rtx dest;
1073 rtx note;
1075 if (!x)
1076 return;
1077 code = GET_CODE (x);
1078 switch (code)
1080 case CONST:
1081 CASE_CONST_ANY:
1082 case CC0:
1083 case PC:
1084 case SYMBOL_REF:
1085 case LABEL_REF:
1086 case ADDR_VEC:
1087 case ADDR_DIFF_VEC:
1088 case REG:
1089 return;
1091 case EXPR_LIST:
1092 if (XEXP (x, 0))
1093 reg_scan_mark_refs (XEXP (x, 0), insn);
1094 if (XEXP (x, 1))
1095 reg_scan_mark_refs (XEXP (x, 1), insn);
1096 break;
1098 case INSN_LIST:
1099 case INT_LIST:
1100 if (XEXP (x, 1))
1101 reg_scan_mark_refs (XEXP (x, 1), insn);
1102 break;
1104 case CLOBBER:
1105 if (MEM_P (XEXP (x, 0)))
1106 reg_scan_mark_refs (XEXP (XEXP (x, 0), 0), insn);
1107 break;
1109 case SET:
1110 /* Count a set of the destination if it is a register. */
1111 for (dest = SET_DEST (x);
1112 GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
1113 || GET_CODE (dest) == ZERO_EXTEND;
1114 dest = XEXP (dest, 0))
1117 /* If this is setting a pseudo from another pseudo or the sum of a
1118 pseudo and a constant integer and the other pseudo is known to be
1119 a pointer, set the destination to be a pointer as well.
1121 Likewise if it is setting the destination from an address or from a
1122 value equivalent to an address or to the sum of an address and
1123 something else.
1125 But don't do any of this if the pseudo corresponds to a user
1126 variable since it should have already been set as a pointer based
1127 on the type. */
1129 if (REG_P (SET_DEST (x))
1130 && REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER
1131 /* If the destination pseudo is set more than once, then other
1132 sets might not be to a pointer value (consider access to a
1133 union in two threads of control in the presence of global
1134 optimizations). So only set REG_POINTER on the destination
1135 pseudo if this is the only set of that pseudo. */
1136 && DF_REG_DEF_COUNT (REGNO (SET_DEST (x))) == 1
1137 && ! REG_USERVAR_P (SET_DEST (x))
1138 && ! REG_POINTER (SET_DEST (x))
1139 && ((REG_P (SET_SRC (x))
1140 && REG_POINTER (SET_SRC (x)))
1141 || ((GET_CODE (SET_SRC (x)) == PLUS
1142 || GET_CODE (SET_SRC (x)) == LO_SUM)
1143 && CONST_INT_P (XEXP (SET_SRC (x), 1))
1144 && REG_P (XEXP (SET_SRC (x), 0))
1145 && REG_POINTER (XEXP (SET_SRC (x), 0)))
1146 || GET_CODE (SET_SRC (x)) == CONST
1147 || GET_CODE (SET_SRC (x)) == SYMBOL_REF
1148 || GET_CODE (SET_SRC (x)) == LABEL_REF
1149 || (GET_CODE (SET_SRC (x)) == HIGH
1150 && (GET_CODE (XEXP (SET_SRC (x), 0)) == CONST
1151 || GET_CODE (XEXP (SET_SRC (x), 0)) == SYMBOL_REF
1152 || GET_CODE (XEXP (SET_SRC (x), 0)) == LABEL_REF))
1153 || ((GET_CODE (SET_SRC (x)) == PLUS
1154 || GET_CODE (SET_SRC (x)) == LO_SUM)
1155 && (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST
1156 || GET_CODE (XEXP (SET_SRC (x), 1)) == SYMBOL_REF
1157 || GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF))
1158 || ((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
1159 && (GET_CODE (XEXP (note, 0)) == CONST
1160 || GET_CODE (XEXP (note, 0)) == SYMBOL_REF
1161 || GET_CODE (XEXP (note, 0)) == LABEL_REF))))
1162 REG_POINTER (SET_DEST (x)) = 1;
1164 /* If this is setting a register from a register or from a simple
1165 conversion of a register, propagate REG_EXPR. */
1166 if (REG_P (dest) && !REG_ATTRS (dest))
1167 set_reg_attrs_from_value (dest, SET_SRC (x));
1169 /* ... fall through ... */
1171 default:
1173 const char *fmt = GET_RTX_FORMAT (code);
1174 int i;
1175 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1177 if (fmt[i] == 'e')
1178 reg_scan_mark_refs (XEXP (x, i), insn);
1179 else if (fmt[i] == 'E' && XVEC (x, i) != 0)
1181 int j;
1182 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1183 reg_scan_mark_refs (XVECEXP (x, i, j), insn);
1191 /* Return nonzero if C1 is a subset of C2, i.e., if every register in C1
1192 is also in C2. */
1194 reg_class_subset_p (reg_class_t c1, reg_class_t c2)
1196 return (c1 == c2
1197 || c2 == ALL_REGS
1198 || hard_reg_set_subset_p (reg_class_contents[(int) c1],
1199 reg_class_contents[(int) c2]));
1202 /* Return nonzero if there is a register that is in both C1 and C2. */
1204 reg_classes_intersect_p (reg_class_t c1, reg_class_t c2)
1206 return (c1 == c2
1207 || c1 == ALL_REGS
1208 || c2 == ALL_REGS
1209 || hard_reg_set_intersect_p (reg_class_contents[(int) c1],
1210 reg_class_contents[(int) c2]));
1214 inline hashval_t
1215 simplifiable_subregs_hasher::hash (const value_type *value)
1217 return value->shape.unique_id ();
1220 inline bool
1221 simplifiable_subregs_hasher::equal (const value_type *value,
1222 const compare_type *compare)
1224 return value->shape == *compare;
1227 inline simplifiable_subreg::simplifiable_subreg (const subreg_shape &shape_in)
1228 : shape (shape_in)
1230 CLEAR_HARD_REG_SET (simplifiable_regs);
1233 /* Return the set of hard registers that are able to form the subreg
1234 described by SHAPE. */
1236 const HARD_REG_SET &
1237 simplifiable_subregs (const subreg_shape &shape)
1239 if (!this_target_hard_regs->x_simplifiable_subregs)
1240 this_target_hard_regs->x_simplifiable_subregs
1241 = new hash_table <simplifiable_subregs_hasher> (30);
1242 simplifiable_subreg **slot
1243 = (this_target_hard_regs->x_simplifiable_subregs
1244 ->find_slot_with_hash (&shape, shape.unique_id (), INSERT));
1246 if (!*slot)
1248 simplifiable_subreg *info = new simplifiable_subreg (shape);
1249 for (unsigned int i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1250 if (HARD_REGNO_MODE_OK (i, shape.inner_mode)
1251 && simplify_subreg_regno (i, shape.inner_mode, shape.offset,
1252 shape.outer_mode) >= 0)
1253 SET_HARD_REG_BIT (info->simplifiable_regs, i);
1254 *slot = info;
1256 return (*slot)->simplifiable_regs;
1259 /* Passes for keeping and updating info about modes of registers
1260 inside subregisters. */
1262 static HARD_REG_SET **valid_mode_changes;
1263 static obstack valid_mode_changes_obstack;
1265 static void
1266 record_subregs_of_mode (rtx subreg)
1268 unsigned int regno;
1270 if (!REG_P (SUBREG_REG (subreg)))
1271 return;
1273 regno = REGNO (SUBREG_REG (subreg));
1274 if (regno < FIRST_PSEUDO_REGISTER)
1275 return;
1277 if (valid_mode_changes[regno])
1278 AND_HARD_REG_SET (*valid_mode_changes[regno],
1279 simplifiable_subregs (shape_of_subreg (subreg)));
1280 else
1282 valid_mode_changes[regno]
1283 = XOBNEW (&valid_mode_changes_obstack, HARD_REG_SET);
1284 COPY_HARD_REG_SET (*valid_mode_changes[regno],
1285 simplifiable_subregs (shape_of_subreg (subreg)));
1289 /* Call record_subregs_of_mode for all the subregs in X. */
1290 static void
1291 find_subregs_of_mode (rtx x)
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);
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));
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));
1314 void
1315 init_subregs_of_mode (void)
1317 basic_block bb;
1318 rtx_insn *insn;
1320 gcc_obstack_init (&valid_mode_changes_obstack);
1321 valid_mode_changes = XCNEWVEC (HARD_REG_SET *, max_reg_num ());
1323 FOR_EACH_BB_FN (bb, cfun)
1324 FOR_BB_INSNS (bb, insn)
1325 if (NONDEBUG_INSN_P (insn))
1326 find_subregs_of_mode (PATTERN (insn));
1329 /* Return 1 if REGNO has had an invalid mode change in CLASS from FROM
1330 mode. */
1331 bool
1332 invalid_mode_change_p (unsigned int regno, enum reg_class rclass)
1334 return (valid_mode_changes[regno]
1335 && !hard_reg_set_intersect_p (reg_class_contents[rclass],
1336 *valid_mode_changes[regno]));
1339 void
1340 finish_subregs_of_mode (void)
1342 XDELETEVEC (valid_mode_changes);
1343 obstack_finish (&valid_mode_changes_obstack);
1346 /* Free all data attached to the structure. This isn't a destructor because
1347 we don't want to run on exit. */
1349 void
1350 target_hard_regs::finalize ()
1352 delete x_simplifiable_subregs;