2014-10-17 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / reginfo.c
blobae15e7d21194036029a8fd4387da50c1af322808
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 "hashtab.h"
42 #include "hash-set.h"
43 #include "vec.h"
44 #include "machmode.h"
45 #include "input.h"
46 #include "function.h"
47 #include "insn-config.h"
48 #include "recog.h"
49 #include "reload.h"
50 #include "diagnostic-core.h"
51 #include "output.h"
52 #include "target.h"
53 #include "tree-pass.h"
54 #include "df.h"
55 #include "ira.h"
57 /* Maximum register number used in this function, plus one. */
59 int max_regno;
61 /* Used to cache the results of simplifiable_subregs. SHAPE is the input
62 parameter and SIMPLIFIABLE_REGS is the result. */
63 struct simplifiable_subreg
65 simplifiable_subreg (const subreg_shape &);
67 subreg_shape shape;
68 HARD_REG_SET simplifiable_regs;
71 struct simplifiable_subregs_hasher : typed_noop_remove <simplifiable_subreg>
73 typedef simplifiable_subreg value_type;
74 typedef subreg_shape compare_type;
76 static inline hashval_t hash (const value_type *);
77 static inline bool equal (const value_type *, const compare_type *);
80 struct target_hard_regs default_target_hard_regs;
81 struct target_regs default_target_regs;
82 #if SWITCHABLE_TARGET
83 struct target_hard_regs *this_target_hard_regs = &default_target_hard_regs;
84 struct target_regs *this_target_regs = &default_target_regs;
85 #endif
87 /* Data for initializing fixed_regs. */
88 static const char initial_fixed_regs[] = FIXED_REGISTERS;
90 /* Data for initializing call_used_regs. */
91 static const char initial_call_used_regs[] = CALL_USED_REGISTERS;
93 #ifdef CALL_REALLY_USED_REGISTERS
94 /* Data for initializing call_really_used_regs. */
95 static const char initial_call_really_used_regs[] = CALL_REALLY_USED_REGISTERS;
96 #endif
98 #ifdef CALL_REALLY_USED_REGISTERS
99 #define CALL_REALLY_USED_REGNO_P(X) call_really_used_regs[X]
100 #else
101 #define CALL_REALLY_USED_REGNO_P(X) call_used_regs[X]
102 #endif
104 /* Indexed by hard register number, contains 1 for registers
105 that are being used for global register decls.
106 These must be exempt from ordinary flow analysis
107 and are also considered fixed. */
108 char global_regs[FIRST_PSEUDO_REGISTER];
110 /* Declaration for the global register. */
111 tree global_regs_decl[FIRST_PSEUDO_REGISTER];
113 /* Same information as REGS_INVALIDATED_BY_CALL but in regset form to be used
114 in dataflow more conveniently. */
115 regset regs_invalidated_by_call_regset;
117 /* Same information as FIXED_REG_SET but in regset form. */
118 regset fixed_reg_set_regset;
120 /* The bitmap_obstack is used to hold some static variables that
121 should not be reset after each function is compiled. */
122 static bitmap_obstack persistent_obstack;
124 /* Used to initialize reg_alloc_order. */
125 #ifdef REG_ALLOC_ORDER
126 static int initial_reg_alloc_order[FIRST_PSEUDO_REGISTER] = REG_ALLOC_ORDER;
127 #endif
129 /* The same information, but as an array of unsigned ints. We copy from
130 these unsigned ints to the table above. We do this so the tm.h files
131 do not have to be aware of the wordsize for machines with <= 64 regs.
132 Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
133 #define N_REG_INTS \
134 ((FIRST_PSEUDO_REGISTER + (32 - 1)) / 32)
136 static const unsigned int_reg_class_contents[N_REG_CLASSES][N_REG_INTS]
137 = REG_CLASS_CONTENTS;
139 /* Array containing all of the register names. */
140 static const char *const initial_reg_names[] = REGISTER_NAMES;
142 /* Array containing all of the register class names. */
143 const char * reg_class_names[] = REG_CLASS_NAMES;
145 /* No more global register variables may be declared; true once
146 reginfo has been initialized. */
147 static int no_global_reg_vars = 0;
149 /* Given a register bitmap, turn on the bits in a HARD_REG_SET that
150 correspond to the hard registers, if any, set in that map. This
151 could be done far more efficiently by having all sorts of special-cases
152 with moving single words, but probably isn't worth the trouble. */
153 void
154 reg_set_to_hard_reg_set (HARD_REG_SET *to, const_bitmap from)
156 unsigned i;
157 bitmap_iterator bi;
159 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
161 if (i >= FIRST_PSEUDO_REGISTER)
162 return;
163 SET_HARD_REG_BIT (*to, i);
167 /* Function called only once per target_globals to initialize the
168 target_hard_regs structure. Once this is done, various switches
169 may override. */
170 void
171 init_reg_sets (void)
173 int i, j;
175 /* First copy the register information from the initial int form into
176 the regsets. */
178 for (i = 0; i < N_REG_CLASSES; i++)
180 CLEAR_HARD_REG_SET (reg_class_contents[i]);
182 /* Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
183 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
184 if (int_reg_class_contents[i][j / 32]
185 & ((unsigned) 1 << (j % 32)))
186 SET_HARD_REG_BIT (reg_class_contents[i], j);
189 /* Sanity check: make sure the target macros FIXED_REGISTERS and
190 CALL_USED_REGISTERS had the right number of initializers. */
191 gcc_assert (sizeof fixed_regs == sizeof initial_fixed_regs);
192 gcc_assert (sizeof call_used_regs == sizeof initial_call_used_regs);
193 #ifdef CALL_REALLY_USED_REGISTERS
194 gcc_assert (sizeof call_really_used_regs
195 == sizeof initial_call_really_used_regs);
196 #endif
197 #ifdef REG_ALLOC_ORDER
198 gcc_assert (sizeof reg_alloc_order == sizeof initial_reg_alloc_order);
199 #endif
200 gcc_assert (sizeof reg_names == sizeof initial_reg_names);
202 memcpy (fixed_regs, initial_fixed_regs, sizeof fixed_regs);
203 memcpy (call_used_regs, initial_call_used_regs, sizeof call_used_regs);
204 #ifdef CALL_REALLY_USED_REGISTERS
205 memcpy (call_really_used_regs, initial_call_really_used_regs,
206 sizeof call_really_used_regs);
207 #endif
208 #ifdef REG_ALLOC_ORDER
209 memcpy (reg_alloc_order, initial_reg_alloc_order, sizeof reg_alloc_order);
210 #endif
211 memcpy (reg_names, initial_reg_names, sizeof reg_names);
213 SET_HARD_REG_SET (accessible_reg_set);
214 SET_HARD_REG_SET (operand_reg_set);
217 /* We need to save copies of some of the register information which
218 can be munged by command-line switches so we can restore it during
219 subsequent back-end reinitialization. */
220 static char saved_fixed_regs[FIRST_PSEUDO_REGISTER];
221 static char saved_call_used_regs[FIRST_PSEUDO_REGISTER];
222 #ifdef CALL_REALLY_USED_REGISTERS
223 static char saved_call_really_used_regs[FIRST_PSEUDO_REGISTER];
224 #endif
225 static const char *saved_reg_names[FIRST_PSEUDO_REGISTER];
226 static HARD_REG_SET saved_accessible_reg_set;
227 static HARD_REG_SET saved_operand_reg_set;
229 /* Save the register information. */
230 void
231 save_register_info (void)
233 /* Sanity check: make sure the target macros FIXED_REGISTERS and
234 CALL_USED_REGISTERS had the right number of initializers. */
235 gcc_assert (sizeof fixed_regs == sizeof saved_fixed_regs);
236 gcc_assert (sizeof call_used_regs == sizeof saved_call_used_regs);
237 memcpy (saved_fixed_regs, fixed_regs, sizeof fixed_regs);
238 memcpy (saved_call_used_regs, call_used_regs, sizeof call_used_regs);
240 /* Likewise for call_really_used_regs. */
241 #ifdef CALL_REALLY_USED_REGISTERS
242 gcc_assert (sizeof call_really_used_regs
243 == sizeof saved_call_really_used_regs);
244 memcpy (saved_call_really_used_regs, call_really_used_regs,
245 sizeof call_really_used_regs);
246 #endif
248 /* And similarly for reg_names. */
249 gcc_assert (sizeof reg_names == sizeof saved_reg_names);
250 memcpy (saved_reg_names, reg_names, sizeof reg_names);
251 COPY_HARD_REG_SET (saved_accessible_reg_set, accessible_reg_set);
252 COPY_HARD_REG_SET (saved_operand_reg_set, operand_reg_set);
255 /* Restore the register information. */
256 static void
257 restore_register_info (void)
259 memcpy (fixed_regs, saved_fixed_regs, sizeof fixed_regs);
260 memcpy (call_used_regs, saved_call_used_regs, sizeof call_used_regs);
262 #ifdef CALL_REALLY_USED_REGISTERS
263 memcpy (call_really_used_regs, saved_call_really_used_regs,
264 sizeof call_really_used_regs);
265 #endif
267 memcpy (reg_names, saved_reg_names, sizeof reg_names);
268 COPY_HARD_REG_SET (accessible_reg_set, saved_accessible_reg_set);
269 COPY_HARD_REG_SET (operand_reg_set, saved_operand_reg_set);
272 /* After switches have been processed, which perhaps alter
273 `fixed_regs' and `call_used_regs', convert them to HARD_REG_SETs. */
274 static void
275 init_reg_sets_1 (void)
277 unsigned int i, j;
278 unsigned int /* enum machine_mode */ m;
280 restore_register_info ();
282 #ifdef REG_ALLOC_ORDER
283 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
284 inv_reg_alloc_order[reg_alloc_order[i]] = i;
285 #endif
287 /* Let the target tweak things if necessary. */
289 targetm.conditional_register_usage ();
291 /* Compute number of hard regs in each class. */
293 memset (reg_class_size, 0, sizeof reg_class_size);
294 for (i = 0; i < N_REG_CLASSES; i++)
296 bool any_nonfixed = false;
297 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
298 if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
300 reg_class_size[i]++;
301 if (!fixed_regs[j])
302 any_nonfixed = true;
304 class_only_fixed_regs[i] = !any_nonfixed;
307 /* Initialize the table of subunions.
308 reg_class_subunion[I][J] gets the largest-numbered reg-class
309 that is contained in the union of classes I and J. */
311 memset (reg_class_subunion, 0, sizeof reg_class_subunion);
312 for (i = 0; i < N_REG_CLASSES; i++)
314 for (j = 0; j < N_REG_CLASSES; j++)
316 HARD_REG_SET c;
317 int k;
319 COPY_HARD_REG_SET (c, reg_class_contents[i]);
320 IOR_HARD_REG_SET (c, reg_class_contents[j]);
321 for (k = 0; k < N_REG_CLASSES; k++)
322 if (hard_reg_set_subset_p (reg_class_contents[k], c)
323 && !hard_reg_set_subset_p (reg_class_contents[k],
324 reg_class_contents
325 [(int) reg_class_subunion[i][j]]))
326 reg_class_subunion[i][j] = (enum reg_class) k;
330 /* Initialize the table of superunions.
331 reg_class_superunion[I][J] gets the smallest-numbered reg-class
332 containing the union of classes I and J. */
334 memset (reg_class_superunion, 0, sizeof reg_class_superunion);
335 for (i = 0; i < N_REG_CLASSES; i++)
337 for (j = 0; j < N_REG_CLASSES; j++)
339 HARD_REG_SET c;
340 int k;
342 COPY_HARD_REG_SET (c, reg_class_contents[i]);
343 IOR_HARD_REG_SET (c, reg_class_contents[j]);
344 for (k = 0; k < N_REG_CLASSES; k++)
345 if (hard_reg_set_subset_p (c, reg_class_contents[k]))
346 break;
348 reg_class_superunion[i][j] = (enum reg_class) k;
352 /* Initialize the tables of subclasses and superclasses of each reg class.
353 First clear the whole table, then add the elements as they are found. */
355 for (i = 0; i < N_REG_CLASSES; i++)
357 for (j = 0; j < N_REG_CLASSES; j++)
358 reg_class_subclasses[i][j] = LIM_REG_CLASSES;
361 for (i = 0; i < N_REG_CLASSES; i++)
363 if (i == (int) NO_REGS)
364 continue;
366 for (j = i + 1; j < N_REG_CLASSES; j++)
367 if (hard_reg_set_subset_p (reg_class_contents[i],
368 reg_class_contents[j]))
370 /* Reg class I is a subclass of J.
371 Add J to the table of superclasses of I. */
372 enum reg_class *p;
374 /* Add I to the table of superclasses of J. */
375 p = &reg_class_subclasses[j][0];
376 while (*p != LIM_REG_CLASSES) p++;
377 *p = (enum reg_class) i;
381 /* Initialize "constant" tables. */
383 CLEAR_HARD_REG_SET (fixed_reg_set);
384 CLEAR_HARD_REG_SET (call_used_reg_set);
385 CLEAR_HARD_REG_SET (call_fixed_reg_set);
386 CLEAR_HARD_REG_SET (regs_invalidated_by_call);
387 if (!regs_invalidated_by_call_regset)
389 bitmap_obstack_initialize (&persistent_obstack);
390 regs_invalidated_by_call_regset = ALLOC_REG_SET (&persistent_obstack);
392 else
393 CLEAR_REG_SET (regs_invalidated_by_call_regset);
394 if (!fixed_reg_set_regset)
395 fixed_reg_set_regset = ALLOC_REG_SET (&persistent_obstack);
396 else
397 CLEAR_REG_SET (fixed_reg_set_regset);
399 AND_HARD_REG_SET (operand_reg_set, accessible_reg_set);
400 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
402 /* As a special exception, registers whose class is NO_REGS are
403 not accepted by `register_operand'. The reason for this change
404 is to allow the representation of special architecture artifacts
405 (such as a condition code register) without extending the rtl
406 definitions. Since registers of class NO_REGS cannot be used
407 as registers in any case where register classes are examined,
408 it is better to apply this exception in a target-independent way. */
409 if (REGNO_REG_CLASS (i) == NO_REGS)
410 CLEAR_HARD_REG_BIT (operand_reg_set, i);
412 /* If a register is too limited to be treated as a register operand,
413 then it should never be allocated to a pseudo. */
414 if (!TEST_HARD_REG_BIT (operand_reg_set, i))
416 fixed_regs[i] = 1;
417 call_used_regs[i] = 1;
420 /* call_used_regs must include fixed_regs. */
421 gcc_assert (!fixed_regs[i] || call_used_regs[i]);
422 #ifdef CALL_REALLY_USED_REGISTERS
423 /* call_used_regs must include call_really_used_regs. */
424 gcc_assert (!call_really_used_regs[i] || call_used_regs[i]);
425 #endif
427 if (fixed_regs[i])
429 SET_HARD_REG_BIT (fixed_reg_set, i);
430 SET_REGNO_REG_SET (fixed_reg_set_regset, i);
433 if (call_used_regs[i])
434 SET_HARD_REG_BIT (call_used_reg_set, i);
436 /* There are a couple of fixed registers that we know are safe to
437 exclude from being clobbered by calls:
439 The frame pointer is always preserved across calls. The arg
440 pointer is if it is fixed. The stack pointer usually is,
441 unless TARGET_RETURN_POPS_ARGS, in which case an explicit
442 CLOBBER will be present. If we are generating PIC code, the
443 PIC offset table register is preserved across calls, though the
444 target can override that. */
446 if (i == STACK_POINTER_REGNUM)
448 else if (global_regs[i])
450 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
451 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
453 else if (i == FRAME_POINTER_REGNUM)
455 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
456 else if (i == HARD_FRAME_POINTER_REGNUM)
458 #endif
459 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
460 else if (i == ARG_POINTER_REGNUM && fixed_regs[i])
462 #endif
463 else if (!PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
464 && i == (unsigned) PIC_OFFSET_TABLE_REGNUM && fixed_regs[i])
466 else if (CALL_REALLY_USED_REGNO_P (i))
468 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
469 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
473 COPY_HARD_REG_SET (call_fixed_reg_set, fixed_reg_set);
475 /* Preserve global registers if called more than once. */
476 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
478 if (global_regs[i])
480 fixed_regs[i] = call_used_regs[i] = 1;
481 SET_HARD_REG_BIT (fixed_reg_set, i);
482 SET_HARD_REG_BIT (call_used_reg_set, i);
483 SET_HARD_REG_BIT (call_fixed_reg_set, i);
487 memset (have_regs_of_mode, 0, sizeof (have_regs_of_mode));
488 memset (contains_reg_of_mode, 0, sizeof (contains_reg_of_mode));
489 for (m = 0; m < (unsigned int) MAX_MACHINE_MODE; m++)
491 HARD_REG_SET ok_regs;
492 CLEAR_HARD_REG_SET (ok_regs);
493 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
494 if (!fixed_regs [j] && HARD_REGNO_MODE_OK (j, (enum machine_mode) m))
495 SET_HARD_REG_BIT (ok_regs, j);
497 for (i = 0; i < N_REG_CLASSES; i++)
498 if ((targetm.class_max_nregs ((reg_class_t) i, (enum machine_mode) m)
499 <= reg_class_size[i])
500 && hard_reg_set_intersect_p (ok_regs, reg_class_contents[i]))
502 contains_reg_of_mode [i][m] = 1;
503 have_regs_of_mode [m] = 1;
508 /* Compute the table of register modes.
509 These values are used to record death information for individual registers
510 (as opposed to a multi-register mode).
511 This function might be invoked more than once, if the target has support
512 for changing register usage conventions on a per-function basis.
514 void
515 init_reg_modes_target (void)
517 int i, j;
519 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
520 for (j = 0; j < MAX_MACHINE_MODE; j++)
521 hard_regno_nregs[i][j] = HARD_REGNO_NREGS (i, (enum machine_mode)j);
523 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
525 reg_raw_mode[i] = choose_hard_reg_mode (i, 1, false);
527 /* If we couldn't find a valid mode, just use the previous mode
528 if it is suitable, otherwise fall back on word_mode. */
529 if (reg_raw_mode[i] == VOIDmode)
531 if (i > 0 && hard_regno_nregs[i][reg_raw_mode[i - 1]] == 1)
532 reg_raw_mode[i] = reg_raw_mode[i - 1];
533 else
534 reg_raw_mode[i] = word_mode;
539 /* Finish initializing the register sets and initialize the register modes.
540 This function might be invoked more than once, if the target has support
541 for changing register usage conventions on a per-function basis.
543 void
544 init_regs (void)
546 /* This finishes what was started by init_reg_sets, but couldn't be done
547 until after register usage was specified. */
548 init_reg_sets_1 ();
551 /* The same as previous function plus initializing IRA. */
552 void
553 reinit_regs (void)
555 init_regs ();
556 /* caller_save needs to be re-initialized. */
557 caller_save_initialized_p = false;
558 if (this_target_rtl->target_specific_initialized)
560 ira_init ();
561 recog_init ();
565 /* Initialize some fake stack-frame MEM references for use in
566 memory_move_secondary_cost. */
567 void
568 init_fake_stack_mems (void)
570 int i;
572 for (i = 0; i < MAX_MACHINE_MODE; i++)
573 top_of_stack[i] = gen_rtx_MEM ((enum machine_mode) i, stack_pointer_rtx);
577 /* Compute cost of moving data from a register of class FROM to one of
578 TO, using MODE. */
581 register_move_cost (enum machine_mode mode, reg_class_t from, reg_class_t to)
583 return targetm.register_move_cost (mode, from, to);
586 /* Compute cost of moving registers to/from memory. */
589 memory_move_cost (enum machine_mode mode, reg_class_t rclass, bool in)
591 return targetm.memory_move_cost (mode, rclass, in);
594 /* Compute extra cost of moving registers to/from memory due to reloads.
595 Only needed if secondary reloads are required for memory moves. */
597 memory_move_secondary_cost (enum machine_mode mode, reg_class_t rclass,
598 bool in)
600 reg_class_t altclass;
601 int partial_cost = 0;
602 /* We need a memory reference to feed to SECONDARY... macros. */
603 /* mem may be unused even if the SECONDARY_ macros are defined. */
604 rtx mem ATTRIBUTE_UNUSED = top_of_stack[(int) mode];
606 altclass = secondary_reload_class (in ? 1 : 0, rclass, mode, mem);
608 if (altclass == NO_REGS)
609 return 0;
611 if (in)
612 partial_cost = register_move_cost (mode, altclass, rclass);
613 else
614 partial_cost = register_move_cost (mode, rclass, altclass);
616 if (rclass == altclass)
617 /* This isn't simply a copy-to-temporary situation. Can't guess
618 what it is, so TARGET_MEMORY_MOVE_COST really ought not to be
619 calling here in that case.
621 I'm tempted to put in an assert here, but returning this will
622 probably only give poor estimates, which is what we would've
623 had before this code anyways. */
624 return partial_cost;
626 /* Check if the secondary reload register will also need a
627 secondary reload. */
628 return memory_move_secondary_cost (mode, altclass, in) + partial_cost;
631 /* Return a machine mode that is legitimate for hard reg REGNO and large
632 enough to save nregs. If we can't find one, return VOIDmode.
633 If CALL_SAVED is true, only consider modes that are call saved. */
634 enum machine_mode
635 choose_hard_reg_mode (unsigned int regno ATTRIBUTE_UNUSED,
636 unsigned int nregs, bool call_saved)
638 unsigned int /* enum machine_mode */ m;
639 enum machine_mode found_mode = VOIDmode, mode;
641 /* We first look for the largest integer mode that can be validly
642 held in REGNO. If none, we look for the largest floating-point mode.
643 If we still didn't find a valid mode, try CCmode. */
645 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
646 mode != VOIDmode;
647 mode = GET_MODE_WIDER_MODE (mode))
648 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
649 && HARD_REGNO_MODE_OK (regno, mode)
650 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
651 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (found_mode))
652 found_mode = mode;
654 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
655 mode != VOIDmode;
656 mode = GET_MODE_WIDER_MODE (mode))
657 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
658 && HARD_REGNO_MODE_OK (regno, mode)
659 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
660 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (found_mode))
661 found_mode = mode;
663 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
664 mode != VOIDmode;
665 mode = GET_MODE_WIDER_MODE (mode))
666 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
667 && HARD_REGNO_MODE_OK (regno, mode)
668 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
669 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (found_mode))
670 found_mode = mode;
672 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
673 mode != VOIDmode;
674 mode = GET_MODE_WIDER_MODE (mode))
675 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
676 && HARD_REGNO_MODE_OK (regno, mode)
677 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
678 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (found_mode))
679 found_mode = mode;
681 if (found_mode != VOIDmode)
682 return found_mode;
684 /* Iterate over all of the CCmodes. */
685 for (m = (unsigned int) CCmode; m < (unsigned int) NUM_MACHINE_MODES; ++m)
687 mode = (enum machine_mode) m;
688 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
689 && HARD_REGNO_MODE_OK (regno, mode)
690 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
691 return mode;
694 /* We can't find a mode valid for this register. */
695 return VOIDmode;
698 /* Specify the usage characteristics of the register named NAME.
699 It should be a fixed register if FIXED and a
700 call-used register if CALL_USED. */
701 void
702 fix_register (const char *name, int fixed, int call_used)
704 int i;
705 int reg, nregs;
707 /* Decode the name and update the primary form of
708 the register info. */
710 if ((reg = decode_reg_name_and_count (name, &nregs)) >= 0)
712 gcc_assert (nregs >= 1);
713 for (i = reg; i < reg + nregs; i++)
715 if ((i == STACK_POINTER_REGNUM
716 #ifdef HARD_FRAME_POINTER_REGNUM
717 || i == HARD_FRAME_POINTER_REGNUM
718 #else
719 || i == FRAME_POINTER_REGNUM
720 #endif
722 && (fixed == 0 || call_used == 0))
724 switch (fixed)
726 case 0:
727 switch (call_used)
729 case 0:
730 error ("can%'t use %qs as a call-saved register", name);
731 break;
733 case 1:
734 error ("can%'t use %qs as a call-used register", name);
735 break;
737 default:
738 gcc_unreachable ();
740 break;
742 case 1:
743 switch (call_used)
745 case 1:
746 error ("can%'t use %qs as a fixed register", name);
747 break;
749 case 0:
750 default:
751 gcc_unreachable ();
753 break;
755 default:
756 gcc_unreachable ();
759 else
761 fixed_regs[i] = fixed;
762 call_used_regs[i] = call_used;
763 #ifdef CALL_REALLY_USED_REGISTERS
764 if (fixed == 0)
765 call_really_used_regs[i] = call_used;
766 #endif
770 else
772 warning (0, "unknown register name: %s", name);
776 /* Mark register number I as global. */
777 void
778 globalize_reg (tree decl, int i)
780 location_t loc = DECL_SOURCE_LOCATION (decl);
782 #ifdef STACK_REGS
783 if (IN_RANGE (i, FIRST_STACK_REG, LAST_STACK_REG))
785 error ("stack register used for global register variable");
786 return;
788 #endif
790 if (fixed_regs[i] == 0 && no_global_reg_vars)
791 error_at (loc, "global register variable follows a function definition");
793 if (global_regs[i])
795 warning_at (loc, 0,
796 "register of %qD used for multiple global register variables",
797 decl);
798 inform (DECL_SOURCE_LOCATION (global_regs_decl[i]),
799 "conflicts with %qD", global_regs_decl[i]);
800 return;
803 if (call_used_regs[i] && ! fixed_regs[i])
804 warning_at (loc, 0, "call-clobbered register used for global register variable");
806 global_regs[i] = 1;
807 global_regs_decl[i] = decl;
809 /* If we're globalizing the frame pointer, we need to set the
810 appropriate regs_invalidated_by_call bit, even if it's already
811 set in fixed_regs. */
812 if (i != STACK_POINTER_REGNUM)
814 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
815 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
818 /* If already fixed, nothing else to do. */
819 if (fixed_regs[i])
820 return;
822 fixed_regs[i] = call_used_regs[i] = 1;
823 #ifdef CALL_REALLY_USED_REGISTERS
824 call_really_used_regs[i] = 1;
825 #endif
827 SET_HARD_REG_BIT (fixed_reg_set, i);
828 SET_HARD_REG_BIT (call_used_reg_set, i);
829 SET_HARD_REG_BIT (call_fixed_reg_set, i);
831 reinit_regs ();
835 /* Structure used to record preferences of given pseudo. */
836 struct reg_pref
838 /* (enum reg_class) prefclass is the preferred class. May be
839 NO_REGS if no class is better than memory. */
840 char prefclass;
842 /* altclass is a register class that we should use for allocating
843 pseudo if no register in the preferred class is available.
844 If no register in this class is available, memory is preferred.
846 It might appear to be more general to have a bitmask of classes here,
847 but since it is recommended that there be a class corresponding to the
848 union of most major pair of classes, that generality is not required. */
849 char altclass;
851 /* allocnoclass is a register class that IRA uses for allocating
852 the pseudo. */
853 char allocnoclass;
856 /* Record preferences of each pseudo. This is available after RA is
857 run. */
858 static struct reg_pref *reg_pref;
860 /* Current size of reg_info. */
861 static int reg_info_size;
862 /* Max_reg_num still last resize_reg_info call. */
863 static int max_regno_since_last_resize;
865 /* Return the reg_class in which pseudo reg number REGNO is best allocated.
866 This function is sometimes called before the info has been computed.
867 When that happens, just return GENERAL_REGS, which is innocuous. */
868 enum reg_class
869 reg_preferred_class (int regno)
871 if (reg_pref == 0)
872 return GENERAL_REGS;
874 gcc_assert (regno < reg_info_size);
875 return (enum reg_class) reg_pref[regno].prefclass;
878 enum reg_class
879 reg_alternate_class (int regno)
881 if (reg_pref == 0)
882 return ALL_REGS;
884 gcc_assert (regno < reg_info_size);
885 return (enum reg_class) reg_pref[regno].altclass;
888 /* Return the reg_class which is used by IRA for its allocation. */
889 enum reg_class
890 reg_allocno_class (int regno)
892 if (reg_pref == 0)
893 return NO_REGS;
895 gcc_assert (regno < reg_info_size);
896 return (enum reg_class) reg_pref[regno].allocnoclass;
901 /* Allocate space for reg info and initilize it. */
902 static void
903 allocate_reg_info (void)
905 int i;
907 max_regno_since_last_resize = max_reg_num ();
908 reg_info_size = max_regno_since_last_resize * 3 / 2 + 1;
909 gcc_assert (! reg_pref && ! reg_renumber);
910 reg_renumber = XNEWVEC (short, reg_info_size);
911 reg_pref = XCNEWVEC (struct reg_pref, reg_info_size);
912 memset (reg_renumber, -1, reg_info_size * sizeof (short));
913 for (i = 0; i < reg_info_size; i++)
915 reg_pref[i].prefclass = GENERAL_REGS;
916 reg_pref[i].altclass = ALL_REGS;
917 reg_pref[i].allocnoclass = GENERAL_REGS;
922 /* Resize reg info. The new elements will be initialized. Return TRUE
923 if new pseudos were added since the last call. */
924 bool
925 resize_reg_info (void)
927 int old, i;
928 bool change_p;
930 if (reg_pref == NULL)
932 allocate_reg_info ();
933 return true;
935 change_p = max_regno_since_last_resize != max_reg_num ();
936 max_regno_since_last_resize = max_reg_num ();
937 if (reg_info_size >= max_reg_num ())
938 return change_p;
939 old = reg_info_size;
940 reg_info_size = max_reg_num () * 3 / 2 + 1;
941 gcc_assert (reg_pref && reg_renumber);
942 reg_renumber = XRESIZEVEC (short, reg_renumber, reg_info_size);
943 reg_pref = XRESIZEVEC (struct reg_pref, reg_pref, reg_info_size);
944 memset (reg_pref + old, -1,
945 (reg_info_size - old) * sizeof (struct reg_pref));
946 memset (reg_renumber + old, -1, (reg_info_size - old) * sizeof (short));
947 for (i = old; i < reg_info_size; i++)
949 reg_pref[i].prefclass = GENERAL_REGS;
950 reg_pref[i].altclass = ALL_REGS;
951 reg_pref[i].allocnoclass = GENERAL_REGS;
953 return true;
957 /* Free up the space allocated by allocate_reg_info. */
958 void
959 free_reg_info (void)
961 if (reg_pref)
963 free (reg_pref);
964 reg_pref = NULL;
967 if (reg_renumber)
969 free (reg_renumber);
970 reg_renumber = NULL;
974 /* Initialize some global data for this pass. */
975 static unsigned int
976 reginfo_init (void)
978 if (df)
979 df_compute_regs_ever_live (true);
981 /* This prevents dump_reg_info from losing if called
982 before reginfo is run. */
983 reg_pref = NULL;
984 reg_info_size = max_regno_since_last_resize = 0;
985 /* No more global register variables may be declared. */
986 no_global_reg_vars = 1;
987 return 1;
990 namespace {
992 const pass_data pass_data_reginfo_init =
994 RTL_PASS, /* type */
995 "reginfo", /* name */
996 OPTGROUP_NONE, /* optinfo_flags */
997 TV_NONE, /* tv_id */
998 0, /* properties_required */
999 0, /* properties_provided */
1000 0, /* properties_destroyed */
1001 0, /* todo_flags_start */
1002 0, /* todo_flags_finish */
1005 class pass_reginfo_init : public rtl_opt_pass
1007 public:
1008 pass_reginfo_init (gcc::context *ctxt)
1009 : rtl_opt_pass (pass_data_reginfo_init, ctxt)
1012 /* opt_pass methods: */
1013 virtual unsigned int execute (function *) { return reginfo_init (); }
1015 }; // class pass_reginfo_init
1017 } // anon namespace
1019 rtl_opt_pass *
1020 make_pass_reginfo_init (gcc::context *ctxt)
1022 return new pass_reginfo_init (ctxt);
1027 /* Set up preferred, alternate, and allocno classes for REGNO as
1028 PREFCLASS, ALTCLASS, and ALLOCNOCLASS. */
1029 void
1030 setup_reg_classes (int regno,
1031 enum reg_class prefclass, enum reg_class altclass,
1032 enum reg_class allocnoclass)
1034 if (reg_pref == NULL)
1035 return;
1036 gcc_assert (reg_info_size >= max_reg_num ());
1037 reg_pref[regno].prefclass = prefclass;
1038 reg_pref[regno].altclass = altclass;
1039 reg_pref[regno].allocnoclass = allocnoclass;
1043 /* This is the `regscan' pass of the compiler, run just before cse and
1044 again just before loop. It finds the first and last use of each
1045 pseudo-register. */
1047 static void reg_scan_mark_refs (rtx, rtx_insn *);
1049 void
1050 reg_scan (rtx_insn *f, unsigned int nregs ATTRIBUTE_UNUSED)
1052 rtx_insn *insn;
1054 timevar_push (TV_REG_SCAN);
1056 for (insn = f; insn; insn = NEXT_INSN (insn))
1057 if (INSN_P (insn))
1059 reg_scan_mark_refs (PATTERN (insn), insn);
1060 if (REG_NOTES (insn))
1061 reg_scan_mark_refs (REG_NOTES (insn), insn);
1064 timevar_pop (TV_REG_SCAN);
1068 /* X is the expression to scan. INSN is the insn it appears in.
1069 NOTE_FLAG is nonzero if X is from INSN's notes rather than its body.
1070 We should only record information for REGs with numbers
1071 greater than or equal to MIN_REGNO. */
1072 static void
1073 reg_scan_mark_refs (rtx x, rtx_insn *insn)
1075 enum rtx_code code;
1076 rtx dest;
1077 rtx note;
1079 if (!x)
1080 return;
1081 code = GET_CODE (x);
1082 switch (code)
1084 case CONST:
1085 CASE_CONST_ANY:
1086 case CC0:
1087 case PC:
1088 case SYMBOL_REF:
1089 case LABEL_REF:
1090 case ADDR_VEC:
1091 case ADDR_DIFF_VEC:
1092 case REG:
1093 return;
1095 case EXPR_LIST:
1096 if (XEXP (x, 0))
1097 reg_scan_mark_refs (XEXP (x, 0), insn);
1098 if (XEXP (x, 1))
1099 reg_scan_mark_refs (XEXP (x, 1), insn);
1100 break;
1102 case INSN_LIST:
1103 case INT_LIST:
1104 if (XEXP (x, 1))
1105 reg_scan_mark_refs (XEXP (x, 1), insn);
1106 break;
1108 case CLOBBER:
1109 if (MEM_P (XEXP (x, 0)))
1110 reg_scan_mark_refs (XEXP (XEXP (x, 0), 0), insn);
1111 break;
1113 case SET:
1114 /* Count a set of the destination if it is a register. */
1115 for (dest = SET_DEST (x);
1116 GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
1117 || GET_CODE (dest) == ZERO_EXTEND;
1118 dest = XEXP (dest, 0))
1121 /* If this is setting a pseudo from another pseudo or the sum of a
1122 pseudo and a constant integer and the other pseudo is known to be
1123 a pointer, set the destination to be a pointer as well.
1125 Likewise if it is setting the destination from an address or from a
1126 value equivalent to an address or to the sum of an address and
1127 something else.
1129 But don't do any of this if the pseudo corresponds to a user
1130 variable since it should have already been set as a pointer based
1131 on the type. */
1133 if (REG_P (SET_DEST (x))
1134 && REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER
1135 /* If the destination pseudo is set more than once, then other
1136 sets might not be to a pointer value (consider access to a
1137 union in two threads of control in the presence of global
1138 optimizations). So only set REG_POINTER on the destination
1139 pseudo if this is the only set of that pseudo. */
1140 && DF_REG_DEF_COUNT (REGNO (SET_DEST (x))) == 1
1141 && ! REG_USERVAR_P (SET_DEST (x))
1142 && ! REG_POINTER (SET_DEST (x))
1143 && ((REG_P (SET_SRC (x))
1144 && REG_POINTER (SET_SRC (x)))
1145 || ((GET_CODE (SET_SRC (x)) == PLUS
1146 || GET_CODE (SET_SRC (x)) == LO_SUM)
1147 && CONST_INT_P (XEXP (SET_SRC (x), 1))
1148 && REG_P (XEXP (SET_SRC (x), 0))
1149 && REG_POINTER (XEXP (SET_SRC (x), 0)))
1150 || GET_CODE (SET_SRC (x)) == CONST
1151 || GET_CODE (SET_SRC (x)) == SYMBOL_REF
1152 || GET_CODE (SET_SRC (x)) == LABEL_REF
1153 || (GET_CODE (SET_SRC (x)) == HIGH
1154 && (GET_CODE (XEXP (SET_SRC (x), 0)) == CONST
1155 || GET_CODE (XEXP (SET_SRC (x), 0)) == SYMBOL_REF
1156 || GET_CODE (XEXP (SET_SRC (x), 0)) == LABEL_REF))
1157 || ((GET_CODE (SET_SRC (x)) == PLUS
1158 || GET_CODE (SET_SRC (x)) == LO_SUM)
1159 && (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST
1160 || GET_CODE (XEXP (SET_SRC (x), 1)) == SYMBOL_REF
1161 || GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF))
1162 || ((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
1163 && (GET_CODE (XEXP (note, 0)) == CONST
1164 || GET_CODE (XEXP (note, 0)) == SYMBOL_REF
1165 || GET_CODE (XEXP (note, 0)) == LABEL_REF))))
1166 REG_POINTER (SET_DEST (x)) = 1;
1168 /* If this is setting a register from a register or from a simple
1169 conversion of a register, propagate REG_EXPR. */
1170 if (REG_P (dest) && !REG_ATTRS (dest))
1171 set_reg_attrs_from_value (dest, SET_SRC (x));
1173 /* ... fall through ... */
1175 default:
1177 const char *fmt = GET_RTX_FORMAT (code);
1178 int i;
1179 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1181 if (fmt[i] == 'e')
1182 reg_scan_mark_refs (XEXP (x, i), insn);
1183 else if (fmt[i] == 'E' && XVEC (x, i) != 0)
1185 int j;
1186 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1187 reg_scan_mark_refs (XVECEXP (x, i, j), insn);
1195 /* Return nonzero if C1 is a subset of C2, i.e., if every register in C1
1196 is also in C2. */
1198 reg_class_subset_p (reg_class_t c1, reg_class_t c2)
1200 return (c1 == c2
1201 || c2 == ALL_REGS
1202 || hard_reg_set_subset_p (reg_class_contents[(int) c1],
1203 reg_class_contents[(int) c2]));
1206 /* Return nonzero if there is a register that is in both C1 and C2. */
1208 reg_classes_intersect_p (reg_class_t c1, reg_class_t c2)
1210 return (c1 == c2
1211 || c1 == ALL_REGS
1212 || c2 == ALL_REGS
1213 || hard_reg_set_intersect_p (reg_class_contents[(int) c1],
1214 reg_class_contents[(int) c2]));
1218 inline hashval_t
1219 simplifiable_subregs_hasher::hash (const value_type *value)
1221 return value->shape.unique_id ();
1224 inline bool
1225 simplifiable_subregs_hasher::equal (const value_type *value,
1226 const compare_type *compare)
1228 return value->shape == *compare;
1231 inline simplifiable_subreg::simplifiable_subreg (const subreg_shape &shape_in)
1232 : shape (shape_in)
1234 CLEAR_HARD_REG_SET (simplifiable_regs);
1237 /* Return the set of hard registers that are able to form the subreg
1238 described by SHAPE. */
1240 const HARD_REG_SET &
1241 simplifiable_subregs (const subreg_shape &shape)
1243 if (!this_target_hard_regs->x_simplifiable_subregs)
1244 this_target_hard_regs->x_simplifiable_subregs
1245 = new hash_table <simplifiable_subregs_hasher> (30);
1246 simplifiable_subreg **slot
1247 = (this_target_hard_regs->x_simplifiable_subregs
1248 ->find_slot_with_hash (&shape, shape.unique_id (), INSERT));
1250 if (!*slot)
1252 simplifiable_subreg *info = new simplifiable_subreg (shape);
1253 for (unsigned int i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1254 if (HARD_REGNO_MODE_OK (i, shape.inner_mode)
1255 && simplify_subreg_regno (i, shape.inner_mode, shape.offset,
1256 shape.outer_mode) >= 0)
1257 SET_HARD_REG_BIT (info->simplifiable_regs, i);
1258 *slot = info;
1260 return (*slot)->simplifiable_regs;
1263 /* Passes for keeping and updating info about modes of registers
1264 inside subregisters. */
1266 static HARD_REG_SET **valid_mode_changes;
1267 static obstack valid_mode_changes_obstack;
1269 static void
1270 record_subregs_of_mode (rtx subreg)
1272 unsigned int regno;
1274 if (!REG_P (SUBREG_REG (subreg)))
1275 return;
1277 regno = REGNO (SUBREG_REG (subreg));
1278 if (regno < FIRST_PSEUDO_REGISTER)
1279 return;
1281 if (valid_mode_changes[regno])
1282 AND_HARD_REG_SET (*valid_mode_changes[regno],
1283 simplifiable_subregs (shape_of_subreg (subreg)));
1284 else
1286 valid_mode_changes[regno]
1287 = XOBNEW (&valid_mode_changes_obstack, HARD_REG_SET);
1288 COPY_HARD_REG_SET (*valid_mode_changes[regno],
1289 simplifiable_subregs (shape_of_subreg (subreg)));
1293 /* Call record_subregs_of_mode for all the subregs in X. */
1294 static void
1295 find_subregs_of_mode (rtx x)
1297 enum rtx_code code = GET_CODE (x);
1298 const char * const fmt = GET_RTX_FORMAT (code);
1299 int i;
1301 if (code == SUBREG)
1302 record_subregs_of_mode (x);
1304 /* Time for some deep diving. */
1305 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1307 if (fmt[i] == 'e')
1308 find_subregs_of_mode (XEXP (x, i));
1309 else if (fmt[i] == 'E')
1311 int j;
1312 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1313 find_subregs_of_mode (XVECEXP (x, i, j));
1318 void
1319 init_subregs_of_mode (void)
1321 basic_block bb;
1322 rtx_insn *insn;
1324 gcc_obstack_init (&valid_mode_changes_obstack);
1325 valid_mode_changes = XCNEWVEC (HARD_REG_SET *, max_reg_num ());
1327 FOR_EACH_BB_FN (bb, cfun)
1328 FOR_BB_INSNS (bb, insn)
1329 if (NONDEBUG_INSN_P (insn))
1330 find_subregs_of_mode (PATTERN (insn));
1333 /* Return 1 if REGNO has had an invalid mode change in CLASS from FROM
1334 mode. */
1335 bool
1336 invalid_mode_change_p (unsigned int regno, enum reg_class rclass)
1338 return (valid_mode_changes[regno]
1339 && !hard_reg_set_intersect_p (reg_class_contents[rclass],
1340 *valid_mode_changes[regno]));
1343 void
1344 finish_subregs_of_mode (void)
1346 XDELETEVEC (valid_mode_changes);
1347 obstack_finish (&valid_mode_changes_obstack);
1350 /* Free all data attached to the structure. This isn't a destructor because
1351 we don't want to run on exit. */
1353 void
1354 target_hard_regs::finalize ()
1356 delete x_simplifiable_subregs;