2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / reginfo.c
blobfde13c4206901e516919ead642398117df910b77
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 "input.h"
34 #include "alias.h"
35 #include "symtab.h"
36 #include "tree.h"
37 #include "rtl.h"
38 #include "function.h"
39 #include "flags.h"
40 #include "insn-config.h"
41 #include "expmed.h"
42 #include "dojump.h"
43 #include "explow.h"
44 #include "calls.h"
45 #include "emit-rtl.h"
46 #include "varasm.h"
47 #include "stmt.h"
48 #include "expr.h"
49 #include "tm_p.h"
50 #include "predict.h"
51 #include "dominance.h"
52 #include "cfg.h"
53 #include "basic-block.h"
54 #include "regs.h"
55 #include "addresses.h"
56 #include "recog.h"
57 #include "reload.h"
58 #include "diagnostic-core.h"
59 #include "output.h"
60 #include "target.h"
61 #include "tree-pass.h"
62 #include "df.h"
63 #include "ira.h"
65 /* Maximum register number used in this function, plus one. */
67 int max_regno;
69 /* Used to cache the results of simplifiable_subregs. SHAPE is the input
70 parameter and SIMPLIFIABLE_REGS is the result. */
71 struct simplifiable_subreg
73 simplifiable_subreg (const subreg_shape &);
75 subreg_shape shape;
76 HARD_REG_SET simplifiable_regs;
79 struct target_hard_regs default_target_hard_regs;
80 struct target_regs default_target_regs;
81 #if SWITCHABLE_TARGET
82 struct target_hard_regs *this_target_hard_regs = &default_target_hard_regs;
83 struct target_regs *this_target_regs = &default_target_regs;
84 #endif
86 /* Data for initializing fixed_regs. */
87 static const char initial_fixed_regs[] = FIXED_REGISTERS;
89 /* Data for initializing call_used_regs. */
90 static const char initial_call_used_regs[] = CALL_USED_REGISTERS;
92 #ifdef CALL_REALLY_USED_REGISTERS
93 /* Data for initializing call_really_used_regs. */
94 static const char initial_call_really_used_regs[] = CALL_REALLY_USED_REGISTERS;
95 #endif
97 #ifdef CALL_REALLY_USED_REGISTERS
98 #define CALL_REALLY_USED_REGNO_P(X) call_really_used_regs[X]
99 #else
100 #define CALL_REALLY_USED_REGNO_P(X) call_used_regs[X]
101 #endif
103 /* Indexed by hard register number, contains 1 for registers
104 that are being used for global register decls.
105 These must be exempt from ordinary flow analysis
106 and are also considered fixed. */
107 char global_regs[FIRST_PSEUDO_REGISTER];
109 /* Declaration for the global register. */
110 tree global_regs_decl[FIRST_PSEUDO_REGISTER];
112 /* Same information as REGS_INVALIDATED_BY_CALL but in regset form to be used
113 in dataflow more conveniently. */
114 regset regs_invalidated_by_call_regset;
116 /* Same information as FIXED_REG_SET but in regset form. */
117 regset fixed_reg_set_regset;
119 /* The bitmap_obstack is used to hold some static variables that
120 should not be reset after each function is compiled. */
121 static bitmap_obstack persistent_obstack;
123 /* Used to initialize reg_alloc_order. */
124 #ifdef REG_ALLOC_ORDER
125 static int initial_reg_alloc_order[FIRST_PSEUDO_REGISTER] = REG_ALLOC_ORDER;
126 #endif
128 /* The same information, but as an array of unsigned ints. We copy from
129 these unsigned ints to the table above. We do this so the tm.h files
130 do not have to be aware of the wordsize for machines with <= 64 regs.
131 Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
132 #define N_REG_INTS \
133 ((FIRST_PSEUDO_REGISTER + (32 - 1)) / 32)
135 static const unsigned int_reg_class_contents[N_REG_CLASSES][N_REG_INTS]
136 = REG_CLASS_CONTENTS;
138 /* Array containing all of the register names. */
139 static const char *const initial_reg_names[] = REGISTER_NAMES;
141 /* Array containing all of the register class names. */
142 const char * reg_class_names[] = REG_CLASS_NAMES;
144 /* No more global register variables may be declared; true once
145 reginfo has been initialized. */
146 static int no_global_reg_vars = 0;
148 /* Given a register bitmap, turn on the bits in a HARD_REG_SET that
149 correspond to the hard registers, if any, set in that map. This
150 could be done far more efficiently by having all sorts of special-cases
151 with moving single words, but probably isn't worth the trouble. */
152 void
153 reg_set_to_hard_reg_set (HARD_REG_SET *to, const_bitmap from)
155 unsigned i;
156 bitmap_iterator bi;
158 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
160 if (i >= FIRST_PSEUDO_REGISTER)
161 return;
162 SET_HARD_REG_BIT (*to, i);
166 /* Function called only once per target_globals to initialize the
167 target_hard_regs structure. Once this is done, various switches
168 may override. */
169 void
170 init_reg_sets (void)
172 int i, j;
174 /* First copy the register information from the initial int form into
175 the regsets. */
177 for (i = 0; i < N_REG_CLASSES; i++)
179 CLEAR_HARD_REG_SET (reg_class_contents[i]);
181 /* Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
182 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
183 if (int_reg_class_contents[i][j / 32]
184 & ((unsigned) 1 << (j % 32)))
185 SET_HARD_REG_BIT (reg_class_contents[i], j);
188 /* Sanity check: make sure the target macros FIXED_REGISTERS and
189 CALL_USED_REGISTERS had the right number of initializers. */
190 gcc_assert (sizeof fixed_regs == sizeof initial_fixed_regs);
191 gcc_assert (sizeof call_used_regs == sizeof initial_call_used_regs);
192 #ifdef CALL_REALLY_USED_REGISTERS
193 gcc_assert (sizeof call_really_used_regs
194 == sizeof initial_call_really_used_regs);
195 #endif
196 #ifdef REG_ALLOC_ORDER
197 gcc_assert (sizeof reg_alloc_order == sizeof initial_reg_alloc_order);
198 #endif
199 gcc_assert (sizeof reg_names == sizeof initial_reg_names);
201 memcpy (fixed_regs, initial_fixed_regs, sizeof fixed_regs);
202 memcpy (call_used_regs, initial_call_used_regs, sizeof call_used_regs);
203 #ifdef CALL_REALLY_USED_REGISTERS
204 memcpy (call_really_used_regs, initial_call_really_used_regs,
205 sizeof call_really_used_regs);
206 #endif
207 #ifdef REG_ALLOC_ORDER
208 memcpy (reg_alloc_order, initial_reg_alloc_order, sizeof reg_alloc_order);
209 #endif
210 memcpy (reg_names, initial_reg_names, sizeof reg_names);
212 SET_HARD_REG_SET (accessible_reg_set);
213 SET_HARD_REG_SET (operand_reg_set);
216 /* We need to save copies of some of the register information which
217 can be munged by command-line switches so we can restore it during
218 subsequent back-end reinitialization. */
219 static char saved_fixed_regs[FIRST_PSEUDO_REGISTER];
220 static char saved_call_used_regs[FIRST_PSEUDO_REGISTER];
221 #ifdef CALL_REALLY_USED_REGISTERS
222 static char saved_call_really_used_regs[FIRST_PSEUDO_REGISTER];
223 #endif
224 static const char *saved_reg_names[FIRST_PSEUDO_REGISTER];
225 static HARD_REG_SET saved_accessible_reg_set;
226 static HARD_REG_SET saved_operand_reg_set;
228 /* Save the register information. */
229 void
230 save_register_info (void)
232 /* Sanity check: make sure the target macros FIXED_REGISTERS and
233 CALL_USED_REGISTERS had the right number of initializers. */
234 gcc_assert (sizeof fixed_regs == sizeof saved_fixed_regs);
235 gcc_assert (sizeof call_used_regs == sizeof saved_call_used_regs);
236 memcpy (saved_fixed_regs, fixed_regs, sizeof fixed_regs);
237 memcpy (saved_call_used_regs, call_used_regs, sizeof call_used_regs);
239 /* Likewise for call_really_used_regs. */
240 #ifdef CALL_REALLY_USED_REGISTERS
241 gcc_assert (sizeof call_really_used_regs
242 == sizeof saved_call_really_used_regs);
243 memcpy (saved_call_really_used_regs, call_really_used_regs,
244 sizeof call_really_used_regs);
245 #endif
247 /* And similarly for reg_names. */
248 gcc_assert (sizeof reg_names == sizeof saved_reg_names);
249 memcpy (saved_reg_names, reg_names, sizeof reg_names);
250 COPY_HARD_REG_SET (saved_accessible_reg_set, accessible_reg_set);
251 COPY_HARD_REG_SET (saved_operand_reg_set, operand_reg_set);
254 /* Restore the register information. */
255 static void
256 restore_register_info (void)
258 memcpy (fixed_regs, saved_fixed_regs, sizeof fixed_regs);
259 memcpy (call_used_regs, saved_call_used_regs, sizeof call_used_regs);
261 #ifdef CALL_REALLY_USED_REGISTERS
262 memcpy (call_really_used_regs, saved_call_really_used_regs,
263 sizeof call_really_used_regs);
264 #endif
266 memcpy (reg_names, saved_reg_names, sizeof reg_names);
267 COPY_HARD_REG_SET (accessible_reg_set, saved_accessible_reg_set);
268 COPY_HARD_REG_SET (operand_reg_set, saved_operand_reg_set);
271 /* After switches have been processed, which perhaps alter
272 `fixed_regs' and `call_used_regs', convert them to HARD_REG_SETs. */
273 static void
274 init_reg_sets_1 (void)
276 unsigned int i, j;
277 unsigned int /* machine_mode */ m;
279 restore_register_info ();
281 #ifdef REG_ALLOC_ORDER
282 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
283 inv_reg_alloc_order[reg_alloc_order[i]] = i;
284 #endif
286 /* Let the target tweak things if necessary. */
288 targetm.conditional_register_usage ();
290 /* Compute number of hard regs in each class. */
292 memset (reg_class_size, 0, sizeof reg_class_size);
293 for (i = 0; i < N_REG_CLASSES; i++)
295 bool any_nonfixed = false;
296 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
297 if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
299 reg_class_size[i]++;
300 if (!fixed_regs[j])
301 any_nonfixed = true;
303 class_only_fixed_regs[i] = !any_nonfixed;
306 /* Initialize the table of subunions.
307 reg_class_subunion[I][J] gets the largest-numbered reg-class
308 that is contained in the union of classes I and J. */
310 memset (reg_class_subunion, 0, sizeof reg_class_subunion);
311 for (i = 0; i < N_REG_CLASSES; i++)
313 for (j = 0; j < N_REG_CLASSES; j++)
315 HARD_REG_SET c;
316 int k;
318 COPY_HARD_REG_SET (c, reg_class_contents[i]);
319 IOR_HARD_REG_SET (c, reg_class_contents[j]);
320 for (k = 0; k < N_REG_CLASSES; k++)
321 if (hard_reg_set_subset_p (reg_class_contents[k], c)
322 && !hard_reg_set_subset_p (reg_class_contents[k],
323 reg_class_contents
324 [(int) reg_class_subunion[i][j]]))
325 reg_class_subunion[i][j] = (enum reg_class) k;
329 /* Initialize the table of superunions.
330 reg_class_superunion[I][J] gets the smallest-numbered reg-class
331 containing the union of classes I and J. */
333 memset (reg_class_superunion, 0, sizeof reg_class_superunion);
334 for (i = 0; i < N_REG_CLASSES; i++)
336 for (j = 0; j < N_REG_CLASSES; j++)
338 HARD_REG_SET c;
339 int k;
341 COPY_HARD_REG_SET (c, reg_class_contents[i]);
342 IOR_HARD_REG_SET (c, reg_class_contents[j]);
343 for (k = 0; k < N_REG_CLASSES; k++)
344 if (hard_reg_set_subset_p (c, reg_class_contents[k]))
345 break;
347 reg_class_superunion[i][j] = (enum reg_class) k;
351 /* Initialize the tables of subclasses and superclasses of each reg class.
352 First clear the whole table, then add the elements as they are found. */
354 for (i = 0; i < N_REG_CLASSES; i++)
356 for (j = 0; j < N_REG_CLASSES; j++)
357 reg_class_subclasses[i][j] = LIM_REG_CLASSES;
360 for (i = 0; i < N_REG_CLASSES; i++)
362 if (i == (int) NO_REGS)
363 continue;
365 for (j = i + 1; j < N_REG_CLASSES; j++)
366 if (hard_reg_set_subset_p (reg_class_contents[i],
367 reg_class_contents[j]))
369 /* Reg class I is a subclass of J.
370 Add J to the table of superclasses of I. */
371 enum reg_class *p;
373 /* Add I to the table of superclasses of J. */
374 p = &reg_class_subclasses[j][0];
375 while (*p != LIM_REG_CLASSES) p++;
376 *p = (enum reg_class) i;
380 /* Initialize "constant" tables. */
382 CLEAR_HARD_REG_SET (fixed_reg_set);
383 CLEAR_HARD_REG_SET (call_used_reg_set);
384 CLEAR_HARD_REG_SET (call_fixed_reg_set);
385 CLEAR_HARD_REG_SET (regs_invalidated_by_call);
386 if (!regs_invalidated_by_call_regset)
388 bitmap_obstack_initialize (&persistent_obstack);
389 regs_invalidated_by_call_regset = ALLOC_REG_SET (&persistent_obstack);
391 else
392 CLEAR_REG_SET (regs_invalidated_by_call_regset);
393 if (!fixed_reg_set_regset)
394 fixed_reg_set_regset = ALLOC_REG_SET (&persistent_obstack);
395 else
396 CLEAR_REG_SET (fixed_reg_set_regset);
398 AND_HARD_REG_SET (operand_reg_set, accessible_reg_set);
399 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
401 /* As a special exception, registers whose class is NO_REGS are
402 not accepted by `register_operand'. The reason for this change
403 is to allow the representation of special architecture artifacts
404 (such as a condition code register) without extending the rtl
405 definitions. Since registers of class NO_REGS cannot be used
406 as registers in any case where register classes are examined,
407 it is better to apply this exception in a target-independent way. */
408 if (REGNO_REG_CLASS (i) == NO_REGS)
409 CLEAR_HARD_REG_BIT (operand_reg_set, i);
411 /* If a register is too limited to be treated as a register operand,
412 then it should never be allocated to a pseudo. */
413 if (!TEST_HARD_REG_BIT (operand_reg_set, i))
415 fixed_regs[i] = 1;
416 call_used_regs[i] = 1;
419 /* call_used_regs must include fixed_regs. */
420 gcc_assert (!fixed_regs[i] || call_used_regs[i]);
421 #ifdef CALL_REALLY_USED_REGISTERS
422 /* call_used_regs must include call_really_used_regs. */
423 gcc_assert (!call_really_used_regs[i] || call_used_regs[i]);
424 #endif
426 if (fixed_regs[i])
428 SET_HARD_REG_BIT (fixed_reg_set, i);
429 SET_REGNO_REG_SET (fixed_reg_set_regset, i);
432 if (call_used_regs[i])
433 SET_HARD_REG_BIT (call_used_reg_set, i);
435 /* There are a couple of fixed registers that we know are safe to
436 exclude from being clobbered by calls:
438 The frame pointer is always preserved across calls. The arg
439 pointer is if it is fixed. The stack pointer usually is,
440 unless TARGET_RETURN_POPS_ARGS, in which case an explicit
441 CLOBBER will be present. If we are generating PIC code, the
442 PIC offset table register is preserved across calls, though the
443 target can override that. */
445 if (i == STACK_POINTER_REGNUM)
447 else if (global_regs[i])
449 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
450 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
452 else if (i == FRAME_POINTER_REGNUM)
454 else if (!HARD_FRAME_POINTER_IS_FRAME_POINTER
455 && i == HARD_FRAME_POINTER_REGNUM)
457 else if (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
458 && i == ARG_POINTER_REGNUM && fixed_regs[i])
460 else if (!PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
461 && i == (unsigned) PIC_OFFSET_TABLE_REGNUM && fixed_regs[i])
463 else if (CALL_REALLY_USED_REGNO_P (i))
465 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
466 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
470 COPY_HARD_REG_SET (call_fixed_reg_set, fixed_reg_set);
472 /* Preserve global registers if called more than once. */
473 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
475 if (global_regs[i])
477 fixed_regs[i] = call_used_regs[i] = 1;
478 SET_HARD_REG_BIT (fixed_reg_set, i);
479 SET_HARD_REG_BIT (call_used_reg_set, i);
480 SET_HARD_REG_BIT (call_fixed_reg_set, i);
484 memset (have_regs_of_mode, 0, sizeof (have_regs_of_mode));
485 memset (contains_reg_of_mode, 0, sizeof (contains_reg_of_mode));
486 for (m = 0; m < (unsigned int) MAX_MACHINE_MODE; m++)
488 HARD_REG_SET ok_regs;
489 CLEAR_HARD_REG_SET (ok_regs);
490 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
491 if (!fixed_regs [j] && HARD_REGNO_MODE_OK (j, (machine_mode) m))
492 SET_HARD_REG_BIT (ok_regs, j);
494 for (i = 0; i < N_REG_CLASSES; i++)
495 if ((targetm.class_max_nregs ((reg_class_t) i, (machine_mode) m)
496 <= reg_class_size[i])
497 && hard_reg_set_intersect_p (ok_regs, reg_class_contents[i]))
499 contains_reg_of_mode [i][m] = 1;
500 have_regs_of_mode [m] = 1;
505 /* Compute the table of register modes.
506 These values are used to record death information for individual registers
507 (as opposed to a multi-register mode).
508 This function might be invoked more than once, if the target has support
509 for changing register usage conventions on a per-function basis.
511 void
512 init_reg_modes_target (void)
514 int i, j;
516 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
517 for (j = 0; j < MAX_MACHINE_MODE; j++)
518 hard_regno_nregs[i][j] = HARD_REGNO_NREGS (i, (machine_mode)j);
520 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
522 reg_raw_mode[i] = choose_hard_reg_mode (i, 1, false);
524 /* If we couldn't find a valid mode, just use the previous mode
525 if it is suitable, otherwise fall back on word_mode. */
526 if (reg_raw_mode[i] == VOIDmode)
528 if (i > 0 && hard_regno_nregs[i][reg_raw_mode[i - 1]] == 1)
529 reg_raw_mode[i] = reg_raw_mode[i - 1];
530 else
531 reg_raw_mode[i] = word_mode;
536 /* Finish initializing the register sets and initialize the register modes.
537 This function might be invoked more than once, if the target has support
538 for changing register usage conventions on a per-function basis.
540 void
541 init_regs (void)
543 /* This finishes what was started by init_reg_sets, but couldn't be done
544 until after register usage was specified. */
545 init_reg_sets_1 ();
548 /* The same as previous function plus initializing IRA. */
549 void
550 reinit_regs (void)
552 init_regs ();
553 /* caller_save needs to be re-initialized. */
554 caller_save_initialized_p = false;
555 if (this_target_rtl->target_specific_initialized)
557 ira_init ();
558 recog_init ();
562 /* Initialize some fake stack-frame MEM references for use in
563 memory_move_secondary_cost. */
564 void
565 init_fake_stack_mems (void)
567 int i;
569 for (i = 0; i < MAX_MACHINE_MODE; i++)
570 top_of_stack[i] = gen_rtx_MEM ((machine_mode) i, stack_pointer_rtx);
574 /* Compute cost of moving data from a register of class FROM to one of
575 TO, using MODE. */
578 register_move_cost (machine_mode mode, reg_class_t from, reg_class_t to)
580 return targetm.register_move_cost (mode, from, to);
583 /* Compute cost of moving registers to/from memory. */
586 memory_move_cost (machine_mode mode, reg_class_t rclass, bool in)
588 return targetm.memory_move_cost (mode, rclass, in);
591 /* Compute extra cost of moving registers to/from memory due to reloads.
592 Only needed if secondary reloads are required for memory moves. */
594 memory_move_secondary_cost (machine_mode mode, reg_class_t rclass,
595 bool in)
597 reg_class_t altclass;
598 int partial_cost = 0;
599 /* We need a memory reference to feed to SECONDARY... macros. */
600 /* mem may be unused even if the SECONDARY_ macros are defined. */
601 rtx mem ATTRIBUTE_UNUSED = top_of_stack[(int) mode];
603 altclass = secondary_reload_class (in ? 1 : 0, rclass, mode, mem);
605 if (altclass == NO_REGS)
606 return 0;
608 if (in)
609 partial_cost = register_move_cost (mode, altclass, rclass);
610 else
611 partial_cost = register_move_cost (mode, rclass, altclass);
613 if (rclass == altclass)
614 /* This isn't simply a copy-to-temporary situation. Can't guess
615 what it is, so TARGET_MEMORY_MOVE_COST really ought not to be
616 calling here in that case.
618 I'm tempted to put in an assert here, but returning this will
619 probably only give poor estimates, which is what we would've
620 had before this code anyways. */
621 return partial_cost;
623 /* Check if the secondary reload register will also need a
624 secondary reload. */
625 return memory_move_secondary_cost (mode, altclass, in) + partial_cost;
628 /* Return a machine mode that is legitimate for hard reg REGNO and large
629 enough to save nregs. If we can't find one, return VOIDmode.
630 If CALL_SAVED is true, only consider modes that are call saved. */
631 machine_mode
632 choose_hard_reg_mode (unsigned int regno ATTRIBUTE_UNUSED,
633 unsigned int nregs, bool call_saved)
635 unsigned int /* machine_mode */ m;
636 machine_mode found_mode = VOIDmode, mode;
638 /* We first look for the largest integer mode that can be validly
639 held in REGNO. If none, we look for the largest floating-point mode.
640 If we still didn't find a valid mode, try CCmode. */
642 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
643 mode != VOIDmode;
644 mode = GET_MODE_WIDER_MODE (mode))
645 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
646 && HARD_REGNO_MODE_OK (regno, mode)
647 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
648 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (found_mode))
649 found_mode = mode;
651 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
652 mode != VOIDmode;
653 mode = GET_MODE_WIDER_MODE (mode))
654 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
655 && HARD_REGNO_MODE_OK (regno, mode)
656 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
657 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (found_mode))
658 found_mode = mode;
660 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
661 mode != VOIDmode;
662 mode = GET_MODE_WIDER_MODE (mode))
663 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
664 && HARD_REGNO_MODE_OK (regno, mode)
665 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
666 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (found_mode))
667 found_mode = mode;
669 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
670 mode != VOIDmode;
671 mode = GET_MODE_WIDER_MODE (mode))
672 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
673 && HARD_REGNO_MODE_OK (regno, mode)
674 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
675 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (found_mode))
676 found_mode = mode;
678 if (found_mode != VOIDmode)
679 return found_mode;
681 /* Iterate over all of the CCmodes. */
682 for (m = (unsigned int) CCmode; m < (unsigned int) NUM_MACHINE_MODES; ++m)
684 mode = (machine_mode) m;
685 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
686 && HARD_REGNO_MODE_OK (regno, mode)
687 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
688 return mode;
691 /* We can't find a mode valid for this register. */
692 return VOIDmode;
695 /* Specify the usage characteristics of the register named NAME.
696 It should be a fixed register if FIXED and a
697 call-used register if CALL_USED. */
698 void
699 fix_register (const char *name, int fixed, int call_used)
701 int i;
702 int reg, nregs;
704 /* Decode the name and update the primary form of
705 the register info. */
707 if ((reg = decode_reg_name_and_count (name, &nregs)) >= 0)
709 gcc_assert (nregs >= 1);
710 for (i = reg; i < reg + nregs; i++)
712 if ((i == STACK_POINTER_REGNUM
713 #ifdef HARD_FRAME_POINTER_REGNUM
714 || i == HARD_FRAME_POINTER_REGNUM
715 #else
716 || i == FRAME_POINTER_REGNUM
717 #endif
719 && (fixed == 0 || call_used == 0))
721 switch (fixed)
723 case 0:
724 switch (call_used)
726 case 0:
727 error ("can%'t use %qs as a call-saved register", name);
728 break;
730 case 1:
731 error ("can%'t use %qs as a call-used register", name);
732 break;
734 default:
735 gcc_unreachable ();
737 break;
739 case 1:
740 switch (call_used)
742 case 1:
743 error ("can%'t use %qs as a fixed register", name);
744 break;
746 case 0:
747 default:
748 gcc_unreachable ();
750 break;
752 default:
753 gcc_unreachable ();
756 else
758 fixed_regs[i] = fixed;
759 call_used_regs[i] = call_used;
760 #ifdef CALL_REALLY_USED_REGISTERS
761 if (fixed == 0)
762 call_really_used_regs[i] = call_used;
763 #endif
767 else
769 warning (0, "unknown register name: %s", name);
773 /* Mark register number I as global. */
774 void
775 globalize_reg (tree decl, int i)
777 location_t loc = DECL_SOURCE_LOCATION (decl);
779 #ifdef STACK_REGS
780 if (IN_RANGE (i, FIRST_STACK_REG, LAST_STACK_REG))
782 error ("stack register used for global register variable");
783 return;
785 #endif
787 if (fixed_regs[i] == 0 && no_global_reg_vars)
788 error_at (loc, "global register variable follows a function definition");
790 if (global_regs[i])
792 warning_at (loc, 0,
793 "register of %qD used for multiple global register variables",
794 decl);
795 inform (DECL_SOURCE_LOCATION (global_regs_decl[i]),
796 "conflicts with %qD", global_regs_decl[i]);
797 return;
800 if (call_used_regs[i] && ! fixed_regs[i])
801 warning_at (loc, 0, "call-clobbered register used for global register variable");
803 global_regs[i] = 1;
804 global_regs_decl[i] = decl;
806 /* If we're globalizing the frame pointer, we need to set the
807 appropriate regs_invalidated_by_call bit, even if it's already
808 set in fixed_regs. */
809 if (i != STACK_POINTER_REGNUM)
811 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
812 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
815 /* If already fixed, nothing else to do. */
816 if (fixed_regs[i])
817 return;
819 fixed_regs[i] = call_used_regs[i] = 1;
820 #ifdef CALL_REALLY_USED_REGISTERS
821 call_really_used_regs[i] = 1;
822 #endif
824 SET_HARD_REG_BIT (fixed_reg_set, i);
825 SET_HARD_REG_BIT (call_used_reg_set, i);
826 SET_HARD_REG_BIT (call_fixed_reg_set, i);
828 reinit_regs ();
832 /* Structure used to record preferences of given pseudo. */
833 struct reg_pref
835 /* (enum reg_class) prefclass is the preferred class. May be
836 NO_REGS if no class is better than memory. */
837 char prefclass;
839 /* altclass is a register class that we should use for allocating
840 pseudo if no register in the preferred class is available.
841 If no register in this class is available, memory is preferred.
843 It might appear to be more general to have a bitmask of classes here,
844 but since it is recommended that there be a class corresponding to the
845 union of most major pair of classes, that generality is not required. */
846 char altclass;
848 /* allocnoclass is a register class that IRA uses for allocating
849 the pseudo. */
850 char allocnoclass;
853 /* Record preferences of each pseudo. This is available after RA is
854 run. */
855 static struct reg_pref *reg_pref;
857 /* Current size of reg_info. */
858 static int reg_info_size;
859 /* Max_reg_num still last resize_reg_info call. */
860 static int max_regno_since_last_resize;
862 /* Return the reg_class in which pseudo reg number REGNO is best allocated.
863 This function is sometimes called before the info has been computed.
864 When that happens, just return GENERAL_REGS, which is innocuous. */
865 enum reg_class
866 reg_preferred_class (int regno)
868 if (reg_pref == 0)
869 return GENERAL_REGS;
871 gcc_assert (regno < reg_info_size);
872 return (enum reg_class) reg_pref[regno].prefclass;
875 enum reg_class
876 reg_alternate_class (int regno)
878 if (reg_pref == 0)
879 return ALL_REGS;
881 gcc_assert (regno < reg_info_size);
882 return (enum reg_class) reg_pref[regno].altclass;
885 /* Return the reg_class which is used by IRA for its allocation. */
886 enum reg_class
887 reg_allocno_class (int regno)
889 if (reg_pref == 0)
890 return NO_REGS;
892 gcc_assert (regno < reg_info_size);
893 return (enum reg_class) reg_pref[regno].allocnoclass;
898 /* Allocate space for reg info and initilize it. */
899 static void
900 allocate_reg_info (void)
902 int i;
904 max_regno_since_last_resize = max_reg_num ();
905 reg_info_size = max_regno_since_last_resize * 3 / 2 + 1;
906 gcc_assert (! reg_pref && ! reg_renumber);
907 reg_renumber = XNEWVEC (short, reg_info_size);
908 reg_pref = XCNEWVEC (struct reg_pref, reg_info_size);
909 memset (reg_renumber, -1, reg_info_size * sizeof (short));
910 for (i = 0; i < reg_info_size; i++)
912 reg_pref[i].prefclass = GENERAL_REGS;
913 reg_pref[i].altclass = ALL_REGS;
914 reg_pref[i].allocnoclass = GENERAL_REGS;
919 /* Resize reg info. The new elements will be initialized. Return TRUE
920 if new pseudos were added since the last call. */
921 bool
922 resize_reg_info (void)
924 int old, i;
925 bool change_p;
927 if (reg_pref == NULL)
929 allocate_reg_info ();
930 return true;
932 change_p = max_regno_since_last_resize != max_reg_num ();
933 max_regno_since_last_resize = max_reg_num ();
934 if (reg_info_size >= max_reg_num ())
935 return change_p;
936 old = reg_info_size;
937 reg_info_size = max_reg_num () * 3 / 2 + 1;
938 gcc_assert (reg_pref && reg_renumber);
939 reg_renumber = XRESIZEVEC (short, reg_renumber, reg_info_size);
940 reg_pref = XRESIZEVEC (struct reg_pref, reg_pref, reg_info_size);
941 memset (reg_pref + old, -1,
942 (reg_info_size - old) * sizeof (struct reg_pref));
943 memset (reg_renumber + old, -1, (reg_info_size - old) * sizeof (short));
944 for (i = old; i < reg_info_size; i++)
946 reg_pref[i].prefclass = GENERAL_REGS;
947 reg_pref[i].altclass = ALL_REGS;
948 reg_pref[i].allocnoclass = GENERAL_REGS;
950 return true;
954 /* Free up the space allocated by allocate_reg_info. */
955 void
956 free_reg_info (void)
958 if (reg_pref)
960 free (reg_pref);
961 reg_pref = NULL;
964 if (reg_renumber)
966 free (reg_renumber);
967 reg_renumber = NULL;
971 /* Initialize some global data for this pass. */
972 static unsigned int
973 reginfo_init (void)
975 if (df)
976 df_compute_regs_ever_live (true);
978 /* This prevents dump_reg_info from losing if called
979 before reginfo is run. */
980 reg_pref = NULL;
981 reg_info_size = max_regno_since_last_resize = 0;
982 /* No more global register variables may be declared. */
983 no_global_reg_vars = 1;
984 return 1;
987 namespace {
989 const pass_data pass_data_reginfo_init =
991 RTL_PASS, /* type */
992 "reginfo", /* name */
993 OPTGROUP_NONE, /* optinfo_flags */
994 TV_NONE, /* tv_id */
995 0, /* properties_required */
996 0, /* properties_provided */
997 0, /* properties_destroyed */
998 0, /* todo_flags_start */
999 0, /* todo_flags_finish */
1002 class pass_reginfo_init : public rtl_opt_pass
1004 public:
1005 pass_reginfo_init (gcc::context *ctxt)
1006 : rtl_opt_pass (pass_data_reginfo_init, ctxt)
1009 /* opt_pass methods: */
1010 virtual unsigned int execute (function *) { return reginfo_init (); }
1012 }; // class pass_reginfo_init
1014 } // anon namespace
1016 rtl_opt_pass *
1017 make_pass_reginfo_init (gcc::context *ctxt)
1019 return new pass_reginfo_init (ctxt);
1024 /* Set up preferred, alternate, and allocno classes for REGNO as
1025 PREFCLASS, ALTCLASS, and ALLOCNOCLASS. */
1026 void
1027 setup_reg_classes (int regno,
1028 enum reg_class prefclass, enum reg_class altclass,
1029 enum reg_class allocnoclass)
1031 if (reg_pref == NULL)
1032 return;
1033 gcc_assert (reg_info_size >= max_reg_num ());
1034 reg_pref[regno].prefclass = prefclass;
1035 reg_pref[regno].altclass = altclass;
1036 reg_pref[regno].allocnoclass = allocnoclass;
1040 /* This is the `regscan' pass of the compiler, run just before cse and
1041 again just before loop. It finds the first and last use of each
1042 pseudo-register. */
1044 static void reg_scan_mark_refs (rtx, rtx_insn *);
1046 void
1047 reg_scan (rtx_insn *f, unsigned int nregs ATTRIBUTE_UNUSED)
1049 rtx_insn *insn;
1051 timevar_push (TV_REG_SCAN);
1053 for (insn = f; insn; insn = NEXT_INSN (insn))
1054 if (INSN_P (insn))
1056 reg_scan_mark_refs (PATTERN (insn), insn);
1057 if (REG_NOTES (insn))
1058 reg_scan_mark_refs (REG_NOTES (insn), insn);
1061 timevar_pop (TV_REG_SCAN);
1065 /* X is the expression to scan. INSN is the insn it appears in.
1066 NOTE_FLAG is nonzero if X is from INSN's notes rather than its body.
1067 We should only record information for REGs with numbers
1068 greater than or equal to MIN_REGNO. */
1069 static void
1070 reg_scan_mark_refs (rtx x, rtx_insn *insn)
1072 enum rtx_code code;
1073 rtx dest;
1074 rtx note;
1076 if (!x)
1077 return;
1078 code = GET_CODE (x);
1079 switch (code)
1081 case CONST:
1082 CASE_CONST_ANY:
1083 case CC0:
1084 case PC:
1085 case SYMBOL_REF:
1086 case LABEL_REF:
1087 case ADDR_VEC:
1088 case ADDR_DIFF_VEC:
1089 case REG:
1090 return;
1092 case EXPR_LIST:
1093 if (XEXP (x, 0))
1094 reg_scan_mark_refs (XEXP (x, 0), insn);
1095 if (XEXP (x, 1))
1096 reg_scan_mark_refs (XEXP (x, 1), insn);
1097 break;
1099 case INSN_LIST:
1100 case INT_LIST:
1101 if (XEXP (x, 1))
1102 reg_scan_mark_refs (XEXP (x, 1), insn);
1103 break;
1105 case CLOBBER:
1106 if (MEM_P (XEXP (x, 0)))
1107 reg_scan_mark_refs (XEXP (XEXP (x, 0), 0), insn);
1108 break;
1110 case SET:
1111 /* Count a set of the destination if it is a register. */
1112 for (dest = SET_DEST (x);
1113 GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
1114 || GET_CODE (dest) == ZERO_EXTRACT;
1115 dest = XEXP (dest, 0))
1118 /* If this is setting a pseudo from another pseudo or the sum of a
1119 pseudo and a constant integer and the other pseudo is known to be
1120 a pointer, set the destination to be a pointer as well.
1122 Likewise if it is setting the destination from an address or from a
1123 value equivalent to an address or to the sum of an address and
1124 something else.
1126 But don't do any of this if the pseudo corresponds to a user
1127 variable since it should have already been set as a pointer based
1128 on the type. */
1130 if (REG_P (SET_DEST (x))
1131 && REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER
1132 /* If the destination pseudo is set more than once, then other
1133 sets might not be to a pointer value (consider access to a
1134 union in two threads of control in the presence of global
1135 optimizations). So only set REG_POINTER on the destination
1136 pseudo if this is the only set of that pseudo. */
1137 && DF_REG_DEF_COUNT (REGNO (SET_DEST (x))) == 1
1138 && ! REG_USERVAR_P (SET_DEST (x))
1139 && ! REG_POINTER (SET_DEST (x))
1140 && ((REG_P (SET_SRC (x))
1141 && REG_POINTER (SET_SRC (x)))
1142 || ((GET_CODE (SET_SRC (x)) == PLUS
1143 || GET_CODE (SET_SRC (x)) == LO_SUM)
1144 && CONST_INT_P (XEXP (SET_SRC (x), 1))
1145 && REG_P (XEXP (SET_SRC (x), 0))
1146 && REG_POINTER (XEXP (SET_SRC (x), 0)))
1147 || GET_CODE (SET_SRC (x)) == CONST
1148 || GET_CODE (SET_SRC (x)) == SYMBOL_REF
1149 || GET_CODE (SET_SRC (x)) == LABEL_REF
1150 || (GET_CODE (SET_SRC (x)) == HIGH
1151 && (GET_CODE (XEXP (SET_SRC (x), 0)) == CONST
1152 || GET_CODE (XEXP (SET_SRC (x), 0)) == SYMBOL_REF
1153 || GET_CODE (XEXP (SET_SRC (x), 0)) == LABEL_REF))
1154 || ((GET_CODE (SET_SRC (x)) == PLUS
1155 || GET_CODE (SET_SRC (x)) == LO_SUM)
1156 && (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST
1157 || GET_CODE (XEXP (SET_SRC (x), 1)) == SYMBOL_REF
1158 || GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF))
1159 || ((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
1160 && (GET_CODE (XEXP (note, 0)) == CONST
1161 || GET_CODE (XEXP (note, 0)) == SYMBOL_REF
1162 || GET_CODE (XEXP (note, 0)) == LABEL_REF))))
1163 REG_POINTER (SET_DEST (x)) = 1;
1165 /* If this is setting a register from a register or from a simple
1166 conversion of a register, propagate REG_EXPR. */
1167 if (REG_P (dest) && !REG_ATTRS (dest))
1168 set_reg_attrs_from_value (dest, SET_SRC (x));
1170 /* ... fall through ... */
1172 default:
1174 const char *fmt = GET_RTX_FORMAT (code);
1175 int i;
1176 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1178 if (fmt[i] == 'e')
1179 reg_scan_mark_refs (XEXP (x, i), insn);
1180 else if (fmt[i] == 'E' && XVEC (x, i) != 0)
1182 int j;
1183 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1184 reg_scan_mark_refs (XVECEXP (x, i, j), insn);
1192 /* Return nonzero if C1 is a subset of C2, i.e., if every register in C1
1193 is also in C2. */
1195 reg_class_subset_p (reg_class_t c1, reg_class_t c2)
1197 return (c1 == c2
1198 || c2 == ALL_REGS
1199 || hard_reg_set_subset_p (reg_class_contents[(int) c1],
1200 reg_class_contents[(int) c2]));
1203 /* Return nonzero if there is a register that is in both C1 and C2. */
1205 reg_classes_intersect_p (reg_class_t c1, reg_class_t c2)
1207 return (c1 == c2
1208 || c1 == ALL_REGS
1209 || c2 == ALL_REGS
1210 || hard_reg_set_intersect_p (reg_class_contents[(int) c1],
1211 reg_class_contents[(int) c2]));
1215 inline hashval_t
1216 simplifiable_subregs_hasher::hash (const simplifiable_subreg *value)
1218 return value->shape.unique_id ();
1221 inline bool
1222 simplifiable_subregs_hasher::equal (const simplifiable_subreg *value,
1223 const subreg_shape *compare)
1225 return value->shape == *compare;
1228 inline simplifiable_subreg::simplifiable_subreg (const subreg_shape &shape_in)
1229 : shape (shape_in)
1231 CLEAR_HARD_REG_SET (simplifiable_regs);
1234 /* Return the set of hard registers that are able to form the subreg
1235 described by SHAPE. */
1237 const HARD_REG_SET &
1238 simplifiable_subregs (const subreg_shape &shape)
1240 if (!this_target_hard_regs->x_simplifiable_subregs)
1241 this_target_hard_regs->x_simplifiable_subregs
1242 = new hash_table <simplifiable_subregs_hasher> (30);
1243 simplifiable_subreg **slot
1244 = (this_target_hard_regs->x_simplifiable_subregs
1245 ->find_slot_with_hash (&shape, shape.unique_id (), INSERT));
1247 if (!*slot)
1249 simplifiable_subreg *info = new simplifiable_subreg (shape);
1250 for (unsigned int i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1251 if (HARD_REGNO_MODE_OK (i, shape.inner_mode)
1252 && simplify_subreg_regno (i, shape.inner_mode, shape.offset,
1253 shape.outer_mode) >= 0)
1254 SET_HARD_REG_BIT (info->simplifiable_regs, i);
1255 *slot = info;
1257 return (*slot)->simplifiable_regs;
1260 /* Passes for keeping and updating info about modes of registers
1261 inside subregisters. */
1263 static HARD_REG_SET **valid_mode_changes;
1264 static obstack valid_mode_changes_obstack;
1266 static void
1267 record_subregs_of_mode (rtx subreg)
1269 unsigned int regno;
1271 if (!REG_P (SUBREG_REG (subreg)))
1272 return;
1274 regno = REGNO (SUBREG_REG (subreg));
1275 if (regno < FIRST_PSEUDO_REGISTER)
1276 return;
1278 if (valid_mode_changes[regno])
1279 AND_HARD_REG_SET (*valid_mode_changes[regno],
1280 simplifiable_subregs (shape_of_subreg (subreg)));
1281 else
1283 valid_mode_changes[regno]
1284 = XOBNEW (&valid_mode_changes_obstack, HARD_REG_SET);
1285 COPY_HARD_REG_SET (*valid_mode_changes[regno],
1286 simplifiable_subregs (shape_of_subreg (subreg)));
1290 /* Call record_subregs_of_mode for all the subregs in X. */
1291 static void
1292 find_subregs_of_mode (rtx x)
1294 enum rtx_code code = GET_CODE (x);
1295 const char * const fmt = GET_RTX_FORMAT (code);
1296 int i;
1298 if (code == SUBREG)
1299 record_subregs_of_mode (x);
1301 /* Time for some deep diving. */
1302 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1304 if (fmt[i] == 'e')
1305 find_subregs_of_mode (XEXP (x, i));
1306 else if (fmt[i] == 'E')
1308 int j;
1309 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1310 find_subregs_of_mode (XVECEXP (x, i, j));
1315 void
1316 init_subregs_of_mode (void)
1318 basic_block bb;
1319 rtx_insn *insn;
1321 gcc_obstack_init (&valid_mode_changes_obstack);
1322 valid_mode_changes = XCNEWVEC (HARD_REG_SET *, max_reg_num ());
1324 FOR_EACH_BB_FN (bb, cfun)
1325 FOR_BB_INSNS (bb, insn)
1326 if (NONDEBUG_INSN_P (insn))
1327 find_subregs_of_mode (PATTERN (insn));
1330 const HARD_REG_SET *
1331 valid_mode_changes_for_regno (unsigned int regno)
1333 return valid_mode_changes[regno];
1336 void
1337 finish_subregs_of_mode (void)
1339 XDELETEVEC (valid_mode_changes);
1340 obstack_free (&valid_mode_changes_obstack, NULL);
1343 /* Free all data attached to the structure. This isn't a destructor because
1344 we don't want to run on exit. */
1346 void
1347 target_hard_regs::finalize ()
1349 delete x_simplifiable_subregs;