PR middle-end/66633
[official-gcc.git] / gcc / reginfo.c
blob09c2f33f1c0f863f21d21e1eea6d00cb1bc402da
1 /* Compute different info about registers.
2 Copyright (C) 1987-2015 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 "alias.h"
34 #include "symtab.h"
35 #include "tree.h"
36 #include "rtl.h"
37 #include "function.h"
38 #include "flags.h"
39 #include "insn-config.h"
40 #include "expmed.h"
41 #include "dojump.h"
42 #include "explow.h"
43 #include "calls.h"
44 #include "emit-rtl.h"
45 #include "varasm.h"
46 #include "stmt.h"
47 #include "expr.h"
48 #include "tm_p.h"
49 #include "predict.h"
50 #include "dominance.h"
51 #include "cfg.h"
52 #include "basic-block.h"
53 #include "regs.h"
54 #include "addresses.h"
55 #include "recog.h"
56 #include "reload.h"
57 #include "diagnostic-core.h"
58 #include "output.h"
59 #include "target.h"
60 #include "tree-pass.h"
61 #include "df.h"
62 #include "ira.h"
64 /* Maximum register number used in this function, plus one. */
66 int max_regno;
68 /* Used to cache the results of simplifiable_subregs. SHAPE is the input
69 parameter and SIMPLIFIABLE_REGS is the result. */
70 struct simplifiable_subreg
72 simplifiable_subreg (const subreg_shape &);
74 subreg_shape shape;
75 HARD_REG_SET simplifiable_regs;
78 struct target_hard_regs default_target_hard_regs;
79 struct target_regs default_target_regs;
80 #if SWITCHABLE_TARGET
81 struct target_hard_regs *this_target_hard_regs = &default_target_hard_regs;
82 struct target_regs *this_target_regs = &default_target_regs;
83 #endif
85 /* Data for initializing fixed_regs. */
86 static const char initial_fixed_regs[] = FIXED_REGISTERS;
88 /* Data for initializing call_used_regs. */
89 static const char initial_call_used_regs[] = CALL_USED_REGISTERS;
91 #ifdef CALL_REALLY_USED_REGISTERS
92 /* Data for initializing call_really_used_regs. */
93 static const char initial_call_really_used_regs[] = CALL_REALLY_USED_REGISTERS;
94 #endif
96 #ifdef CALL_REALLY_USED_REGISTERS
97 #define CALL_REALLY_USED_REGNO_P(X) call_really_used_regs[X]
98 #else
99 #define CALL_REALLY_USED_REGNO_P(X) call_used_regs[X]
100 #endif
102 /* Indexed by hard register number, contains 1 for registers
103 that are being used for global register decls.
104 These must be exempt from ordinary flow analysis
105 and are also considered fixed. */
106 char global_regs[FIRST_PSEUDO_REGISTER];
108 /* Declaration for the global register. */
109 tree global_regs_decl[FIRST_PSEUDO_REGISTER];
111 /* Same information as REGS_INVALIDATED_BY_CALL but in regset form to be used
112 in dataflow more conveniently. */
113 regset regs_invalidated_by_call_regset;
115 /* Same information as FIXED_REG_SET but in regset form. */
116 regset fixed_reg_set_regset;
118 /* The bitmap_obstack is used to hold some static variables that
119 should not be reset after each function is compiled. */
120 static bitmap_obstack persistent_obstack;
122 /* Used to initialize reg_alloc_order. */
123 #ifdef REG_ALLOC_ORDER
124 static int initial_reg_alloc_order[FIRST_PSEUDO_REGISTER] = REG_ALLOC_ORDER;
125 #endif
127 /* The same information, but as an array of unsigned ints. We copy from
128 these unsigned ints to the table above. We do this so the tm.h files
129 do not have to be aware of the wordsize for machines with <= 64 regs.
130 Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
131 #define N_REG_INTS \
132 ((FIRST_PSEUDO_REGISTER + (32 - 1)) / 32)
134 static const unsigned int_reg_class_contents[N_REG_CLASSES][N_REG_INTS]
135 = REG_CLASS_CONTENTS;
137 /* Array containing all of the register names. */
138 static const char *const initial_reg_names[] = REGISTER_NAMES;
140 /* Array containing all of the register class names. */
141 const char * reg_class_names[] = REG_CLASS_NAMES;
143 /* No more global register variables may be declared; true once
144 reginfo has been initialized. */
145 static int no_global_reg_vars = 0;
147 /* Given a register bitmap, turn on the bits in a HARD_REG_SET that
148 correspond to the hard registers, if any, set in that map. This
149 could be done far more efficiently by having all sorts of special-cases
150 with moving single words, but probably isn't worth the trouble. */
151 void
152 reg_set_to_hard_reg_set (HARD_REG_SET *to, const_bitmap from)
154 unsigned i;
155 bitmap_iterator bi;
157 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
159 if (i >= FIRST_PSEUDO_REGISTER)
160 return;
161 SET_HARD_REG_BIT (*to, i);
165 /* Function called only once per target_globals to initialize the
166 target_hard_regs structure. Once this is done, various switches
167 may override. */
168 void
169 init_reg_sets (void)
171 int i, j;
173 /* First copy the register information from the initial int form into
174 the regsets. */
176 for (i = 0; i < N_REG_CLASSES; i++)
178 CLEAR_HARD_REG_SET (reg_class_contents[i]);
180 /* Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
181 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
182 if (int_reg_class_contents[i][j / 32]
183 & ((unsigned) 1 << (j % 32)))
184 SET_HARD_REG_BIT (reg_class_contents[i], j);
187 /* Sanity check: make sure the target macros FIXED_REGISTERS and
188 CALL_USED_REGISTERS had the right number of initializers. */
189 gcc_assert (sizeof fixed_regs == sizeof initial_fixed_regs);
190 gcc_assert (sizeof call_used_regs == sizeof initial_call_used_regs);
191 #ifdef CALL_REALLY_USED_REGISTERS
192 gcc_assert (sizeof call_really_used_regs
193 == sizeof initial_call_really_used_regs);
194 #endif
195 #ifdef REG_ALLOC_ORDER
196 gcc_assert (sizeof reg_alloc_order == sizeof initial_reg_alloc_order);
197 #endif
198 gcc_assert (sizeof reg_names == sizeof initial_reg_names);
200 memcpy (fixed_regs, initial_fixed_regs, sizeof fixed_regs);
201 memcpy (call_used_regs, initial_call_used_regs, sizeof call_used_regs);
202 #ifdef CALL_REALLY_USED_REGISTERS
203 memcpy (call_really_used_regs, initial_call_really_used_regs,
204 sizeof call_really_used_regs);
205 #endif
206 #ifdef REG_ALLOC_ORDER
207 memcpy (reg_alloc_order, initial_reg_alloc_order, sizeof reg_alloc_order);
208 #endif
209 memcpy (reg_names, initial_reg_names, sizeof reg_names);
211 SET_HARD_REG_SET (accessible_reg_set);
212 SET_HARD_REG_SET (operand_reg_set);
215 /* We need to save copies of some of the register information which
216 can be munged by command-line switches so we can restore it during
217 subsequent back-end reinitialization. */
218 static char saved_fixed_regs[FIRST_PSEUDO_REGISTER];
219 static char saved_call_used_regs[FIRST_PSEUDO_REGISTER];
220 #ifdef CALL_REALLY_USED_REGISTERS
221 static char saved_call_really_used_regs[FIRST_PSEUDO_REGISTER];
222 #endif
223 static const char *saved_reg_names[FIRST_PSEUDO_REGISTER];
224 static HARD_REG_SET saved_accessible_reg_set;
225 static HARD_REG_SET saved_operand_reg_set;
227 /* Save the register information. */
228 void
229 save_register_info (void)
231 /* Sanity check: make sure the target macros FIXED_REGISTERS and
232 CALL_USED_REGISTERS had the right number of initializers. */
233 gcc_assert (sizeof fixed_regs == sizeof saved_fixed_regs);
234 gcc_assert (sizeof call_used_regs == sizeof saved_call_used_regs);
235 memcpy (saved_fixed_regs, fixed_regs, sizeof fixed_regs);
236 memcpy (saved_call_used_regs, call_used_regs, sizeof call_used_regs);
238 /* Likewise for call_really_used_regs. */
239 #ifdef CALL_REALLY_USED_REGISTERS
240 gcc_assert (sizeof call_really_used_regs
241 == sizeof saved_call_really_used_regs);
242 memcpy (saved_call_really_used_regs, call_really_used_regs,
243 sizeof call_really_used_regs);
244 #endif
246 /* And similarly for reg_names. */
247 gcc_assert (sizeof reg_names == sizeof saved_reg_names);
248 memcpy (saved_reg_names, reg_names, sizeof reg_names);
249 COPY_HARD_REG_SET (saved_accessible_reg_set, accessible_reg_set);
250 COPY_HARD_REG_SET (saved_operand_reg_set, operand_reg_set);
253 /* Restore the register information. */
254 static void
255 restore_register_info (void)
257 memcpy (fixed_regs, saved_fixed_regs, sizeof fixed_regs);
258 memcpy (call_used_regs, saved_call_used_regs, sizeof call_used_regs);
260 #ifdef CALL_REALLY_USED_REGISTERS
261 memcpy (call_really_used_regs, saved_call_really_used_regs,
262 sizeof call_really_used_regs);
263 #endif
265 memcpy (reg_names, saved_reg_names, sizeof reg_names);
266 COPY_HARD_REG_SET (accessible_reg_set, saved_accessible_reg_set);
267 COPY_HARD_REG_SET (operand_reg_set, saved_operand_reg_set);
270 /* After switches have been processed, which perhaps alter
271 `fixed_regs' and `call_used_regs', convert them to HARD_REG_SETs. */
272 static void
273 init_reg_sets_1 (void)
275 unsigned int i, j;
276 unsigned int /* machine_mode */ m;
278 restore_register_info ();
280 #ifdef REG_ALLOC_ORDER
281 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
282 inv_reg_alloc_order[reg_alloc_order[i]] = i;
283 #endif
285 /* Let the target tweak things if necessary. */
287 targetm.conditional_register_usage ();
289 /* Compute number of hard regs in each class. */
291 memset (reg_class_size, 0, sizeof reg_class_size);
292 for (i = 0; i < N_REG_CLASSES; i++)
294 bool any_nonfixed = false;
295 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
296 if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
298 reg_class_size[i]++;
299 if (!fixed_regs[j])
300 any_nonfixed = true;
302 class_only_fixed_regs[i] = !any_nonfixed;
305 /* Initialize the table of subunions.
306 reg_class_subunion[I][J] gets the largest-numbered reg-class
307 that is contained in the union of classes I and J. */
309 memset (reg_class_subunion, 0, sizeof reg_class_subunion);
310 for (i = 0; i < N_REG_CLASSES; i++)
312 for (j = 0; j < N_REG_CLASSES; j++)
314 HARD_REG_SET c;
315 int k;
317 COPY_HARD_REG_SET (c, reg_class_contents[i]);
318 IOR_HARD_REG_SET (c, reg_class_contents[j]);
319 for (k = 0; k < N_REG_CLASSES; k++)
320 if (hard_reg_set_subset_p (reg_class_contents[k], c)
321 && !hard_reg_set_subset_p (reg_class_contents[k],
322 reg_class_contents
323 [(int) reg_class_subunion[i][j]]))
324 reg_class_subunion[i][j] = (enum reg_class) k;
328 /* Initialize the table of superunions.
329 reg_class_superunion[I][J] gets the smallest-numbered reg-class
330 containing the union of classes I and J. */
332 memset (reg_class_superunion, 0, sizeof reg_class_superunion);
333 for (i = 0; i < N_REG_CLASSES; i++)
335 for (j = 0; j < N_REG_CLASSES; j++)
337 HARD_REG_SET c;
338 int k;
340 COPY_HARD_REG_SET (c, reg_class_contents[i]);
341 IOR_HARD_REG_SET (c, reg_class_contents[j]);
342 for (k = 0; k < N_REG_CLASSES; k++)
343 if (hard_reg_set_subset_p (c, reg_class_contents[k]))
344 break;
346 reg_class_superunion[i][j] = (enum reg_class) k;
350 /* Initialize the tables of subclasses and superclasses of each reg class.
351 First clear the whole table, then add the elements as they are found. */
353 for (i = 0; i < N_REG_CLASSES; i++)
355 for (j = 0; j < N_REG_CLASSES; j++)
356 reg_class_subclasses[i][j] = LIM_REG_CLASSES;
359 for (i = 0; i < N_REG_CLASSES; i++)
361 if (i == (int) NO_REGS)
362 continue;
364 for (j = i + 1; j < N_REG_CLASSES; j++)
365 if (hard_reg_set_subset_p (reg_class_contents[i],
366 reg_class_contents[j]))
368 /* Reg class I is a subclass of J.
369 Add J to the table of superclasses of I. */
370 enum reg_class *p;
372 /* Add I to the table of superclasses of J. */
373 p = &reg_class_subclasses[j][0];
374 while (*p != LIM_REG_CLASSES) p++;
375 *p = (enum reg_class) i;
379 /* Initialize "constant" tables. */
381 CLEAR_HARD_REG_SET (fixed_reg_set);
382 CLEAR_HARD_REG_SET (call_used_reg_set);
383 CLEAR_HARD_REG_SET (call_fixed_reg_set);
384 CLEAR_HARD_REG_SET (regs_invalidated_by_call);
385 if (!regs_invalidated_by_call_regset)
387 bitmap_obstack_initialize (&persistent_obstack);
388 regs_invalidated_by_call_regset = ALLOC_REG_SET (&persistent_obstack);
390 else
391 CLEAR_REG_SET (regs_invalidated_by_call_regset);
392 if (!fixed_reg_set_regset)
393 fixed_reg_set_regset = ALLOC_REG_SET (&persistent_obstack);
394 else
395 CLEAR_REG_SET (fixed_reg_set_regset);
397 AND_HARD_REG_SET (operand_reg_set, accessible_reg_set);
398 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
400 /* As a special exception, registers whose class is NO_REGS are
401 not accepted by `register_operand'. The reason for this change
402 is to allow the representation of special architecture artifacts
403 (such as a condition code register) without extending the rtl
404 definitions. Since registers of class NO_REGS cannot be used
405 as registers in any case where register classes are examined,
406 it is better to apply this exception in a target-independent way. */
407 if (REGNO_REG_CLASS (i) == NO_REGS)
408 CLEAR_HARD_REG_BIT (operand_reg_set, i);
410 /* If a register is too limited to be treated as a register operand,
411 then it should never be allocated to a pseudo. */
412 if (!TEST_HARD_REG_BIT (operand_reg_set, i))
414 fixed_regs[i] = 1;
415 call_used_regs[i] = 1;
418 /* call_used_regs must include fixed_regs. */
419 gcc_assert (!fixed_regs[i] || call_used_regs[i]);
420 #ifdef CALL_REALLY_USED_REGISTERS
421 /* call_used_regs must include call_really_used_regs. */
422 gcc_assert (!call_really_used_regs[i] || call_used_regs[i]);
423 #endif
425 if (fixed_regs[i])
427 SET_HARD_REG_BIT (fixed_reg_set, i);
428 SET_REGNO_REG_SET (fixed_reg_set_regset, i);
431 if (call_used_regs[i])
432 SET_HARD_REG_BIT (call_used_reg_set, i);
434 /* There are a couple of fixed registers that we know are safe to
435 exclude from being clobbered by calls:
437 The frame pointer is always preserved across calls. The arg
438 pointer is if it is fixed. The stack pointer usually is,
439 unless TARGET_RETURN_POPS_ARGS, in which case an explicit
440 CLOBBER will be present. If we are generating PIC code, the
441 PIC offset table register is preserved across calls, though the
442 target can override that. */
444 if (i == STACK_POINTER_REGNUM)
446 else if (global_regs[i])
448 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
449 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
451 else if (i == FRAME_POINTER_REGNUM)
453 else if (!HARD_FRAME_POINTER_IS_FRAME_POINTER
454 && i == HARD_FRAME_POINTER_REGNUM)
456 else if (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
457 && i == ARG_POINTER_REGNUM && fixed_regs[i])
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, (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, (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, (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 ((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 (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 (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 (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 machine_mode
631 choose_hard_reg_mode (unsigned int regno ATTRIBUTE_UNUSED,
632 unsigned int nregs, bool call_saved)
634 unsigned int /* machine_mode */ m;
635 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 = (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_EXTRACT;
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 simplifiable_subreg *value)
1217 return value->shape.unique_id ();
1220 inline bool
1221 simplifiable_subregs_hasher::equal (const simplifiable_subreg *value,
1222 const subreg_shape *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 const HARD_REG_SET *
1330 valid_mode_changes_for_regno (unsigned int regno)
1332 return valid_mode_changes[regno];
1335 void
1336 finish_subregs_of_mode (void)
1338 XDELETEVEC (valid_mode_changes);
1339 obstack_free (&valid_mode_changes_obstack, NULL);
1342 /* Free all data attached to the structure. This isn't a destructor because
1343 we don't want to run on exit. */
1345 void
1346 target_hard_regs::finalize ()
1348 delete x_simplifiable_subregs;