PR target/65871
[official-gcc.git] / gcc / reginfo.c
blobbc2f8de5d3ae41149099666f5ad39219ce0cfe74
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 "hash-set.h"
34 #include "machmode.h"
35 #include "vec.h"
36 #include "double-int.h"
37 #include "input.h"
38 #include "alias.h"
39 #include "symtab.h"
40 #include "wide-int.h"
41 #include "inchash.h"
42 #include "tree.h"
43 #include "rtl.h"
44 #include "hashtab.h"
45 #include "function.h"
46 #include "flags.h"
47 #include "statistics.h"
48 #include "real.h"
49 #include "fixed-value.h"
50 #include "insn-config.h"
51 #include "expmed.h"
52 #include "dojump.h"
53 #include "explow.h"
54 #include "calls.h"
55 #include "emit-rtl.h"
56 #include "varasm.h"
57 #include "stmt.h"
58 #include "expr.h"
59 #include "tm_p.h"
60 #include "predict.h"
61 #include "dominance.h"
62 #include "cfg.h"
63 #include "basic-block.h"
64 #include "regs.h"
65 #include "addresses.h"
66 #include "recog.h"
67 #include "reload.h"
68 #include "diagnostic-core.h"
69 #include "output.h"
70 #include "target.h"
71 #include "tree-pass.h"
72 #include "df.h"
73 #include "ira.h"
75 /* Maximum register number used in this function, plus one. */
77 int max_regno;
79 /* Used to cache the results of simplifiable_subregs. SHAPE is the input
80 parameter and SIMPLIFIABLE_REGS is the result. */
81 struct simplifiable_subreg
83 simplifiable_subreg (const subreg_shape &);
85 subreg_shape shape;
86 HARD_REG_SET simplifiable_regs;
89 struct target_hard_regs default_target_hard_regs;
90 struct target_regs default_target_regs;
91 #if SWITCHABLE_TARGET
92 struct target_hard_regs *this_target_hard_regs = &default_target_hard_regs;
93 struct target_regs *this_target_regs = &default_target_regs;
94 #endif
96 /* Data for initializing fixed_regs. */
97 static const char initial_fixed_regs[] = FIXED_REGISTERS;
99 /* Data for initializing call_used_regs. */
100 static const char initial_call_used_regs[] = CALL_USED_REGISTERS;
102 #ifdef CALL_REALLY_USED_REGISTERS
103 /* Data for initializing call_really_used_regs. */
104 static const char initial_call_really_used_regs[] = CALL_REALLY_USED_REGISTERS;
105 #endif
107 #ifdef CALL_REALLY_USED_REGISTERS
108 #define CALL_REALLY_USED_REGNO_P(X) call_really_used_regs[X]
109 #else
110 #define CALL_REALLY_USED_REGNO_P(X) call_used_regs[X]
111 #endif
113 /* Indexed by hard register number, contains 1 for registers
114 that are being used for global register decls.
115 These must be exempt from ordinary flow analysis
116 and are also considered fixed. */
117 char global_regs[FIRST_PSEUDO_REGISTER];
119 /* Declaration for the global register. */
120 tree global_regs_decl[FIRST_PSEUDO_REGISTER];
122 /* Same information as REGS_INVALIDATED_BY_CALL but in regset form to be used
123 in dataflow more conveniently. */
124 regset regs_invalidated_by_call_regset;
126 /* Same information as FIXED_REG_SET but in regset form. */
127 regset fixed_reg_set_regset;
129 /* The bitmap_obstack is used to hold some static variables that
130 should not be reset after each function is compiled. */
131 static bitmap_obstack persistent_obstack;
133 /* Used to initialize reg_alloc_order. */
134 #ifdef REG_ALLOC_ORDER
135 static int initial_reg_alloc_order[FIRST_PSEUDO_REGISTER] = REG_ALLOC_ORDER;
136 #endif
138 /* The same information, but as an array of unsigned ints. We copy from
139 these unsigned ints to the table above. We do this so the tm.h files
140 do not have to be aware of the wordsize for machines with <= 64 regs.
141 Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
142 #define N_REG_INTS \
143 ((FIRST_PSEUDO_REGISTER + (32 - 1)) / 32)
145 static const unsigned int_reg_class_contents[N_REG_CLASSES][N_REG_INTS]
146 = REG_CLASS_CONTENTS;
148 /* Array containing all of the register names. */
149 static const char *const initial_reg_names[] = REGISTER_NAMES;
151 /* Array containing all of the register class names. */
152 const char * reg_class_names[] = REG_CLASS_NAMES;
154 /* No more global register variables may be declared; true once
155 reginfo has been initialized. */
156 static int no_global_reg_vars = 0;
158 /* Given a register bitmap, turn on the bits in a HARD_REG_SET that
159 correspond to the hard registers, if any, set in that map. This
160 could be done far more efficiently by having all sorts of special-cases
161 with moving single words, but probably isn't worth the trouble. */
162 void
163 reg_set_to_hard_reg_set (HARD_REG_SET *to, const_bitmap from)
165 unsigned i;
166 bitmap_iterator bi;
168 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
170 if (i >= FIRST_PSEUDO_REGISTER)
171 return;
172 SET_HARD_REG_BIT (*to, i);
176 /* Function called only once per target_globals to initialize the
177 target_hard_regs structure. Once this is done, various switches
178 may override. */
179 void
180 init_reg_sets (void)
182 int i, j;
184 /* First copy the register information from the initial int form into
185 the regsets. */
187 for (i = 0; i < N_REG_CLASSES; i++)
189 CLEAR_HARD_REG_SET (reg_class_contents[i]);
191 /* Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
192 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
193 if (int_reg_class_contents[i][j / 32]
194 & ((unsigned) 1 << (j % 32)))
195 SET_HARD_REG_BIT (reg_class_contents[i], j);
198 /* Sanity check: make sure the target macros FIXED_REGISTERS and
199 CALL_USED_REGISTERS had the right number of initializers. */
200 gcc_assert (sizeof fixed_regs == sizeof initial_fixed_regs);
201 gcc_assert (sizeof call_used_regs == sizeof initial_call_used_regs);
202 #ifdef CALL_REALLY_USED_REGISTERS
203 gcc_assert (sizeof call_really_used_regs
204 == sizeof initial_call_really_used_regs);
205 #endif
206 #ifdef REG_ALLOC_ORDER
207 gcc_assert (sizeof reg_alloc_order == sizeof initial_reg_alloc_order);
208 #endif
209 gcc_assert (sizeof reg_names == sizeof initial_reg_names);
211 memcpy (fixed_regs, initial_fixed_regs, sizeof fixed_regs);
212 memcpy (call_used_regs, initial_call_used_regs, sizeof call_used_regs);
213 #ifdef CALL_REALLY_USED_REGISTERS
214 memcpy (call_really_used_regs, initial_call_really_used_regs,
215 sizeof call_really_used_regs);
216 #endif
217 #ifdef REG_ALLOC_ORDER
218 memcpy (reg_alloc_order, initial_reg_alloc_order, sizeof reg_alloc_order);
219 #endif
220 memcpy (reg_names, initial_reg_names, sizeof reg_names);
222 SET_HARD_REG_SET (accessible_reg_set);
223 SET_HARD_REG_SET (operand_reg_set);
226 /* We need to save copies of some of the register information which
227 can be munged by command-line switches so we can restore it during
228 subsequent back-end reinitialization. */
229 static char saved_fixed_regs[FIRST_PSEUDO_REGISTER];
230 static char saved_call_used_regs[FIRST_PSEUDO_REGISTER];
231 #ifdef CALL_REALLY_USED_REGISTERS
232 static char saved_call_really_used_regs[FIRST_PSEUDO_REGISTER];
233 #endif
234 static const char *saved_reg_names[FIRST_PSEUDO_REGISTER];
235 static HARD_REG_SET saved_accessible_reg_set;
236 static HARD_REG_SET saved_operand_reg_set;
238 /* Save the register information. */
239 void
240 save_register_info (void)
242 /* Sanity check: make sure the target macros FIXED_REGISTERS and
243 CALL_USED_REGISTERS had the right number of initializers. */
244 gcc_assert (sizeof fixed_regs == sizeof saved_fixed_regs);
245 gcc_assert (sizeof call_used_regs == sizeof saved_call_used_regs);
246 memcpy (saved_fixed_regs, fixed_regs, sizeof fixed_regs);
247 memcpy (saved_call_used_regs, call_used_regs, sizeof call_used_regs);
249 /* Likewise for call_really_used_regs. */
250 #ifdef CALL_REALLY_USED_REGISTERS
251 gcc_assert (sizeof call_really_used_regs
252 == sizeof saved_call_really_used_regs);
253 memcpy (saved_call_really_used_regs, call_really_used_regs,
254 sizeof call_really_used_regs);
255 #endif
257 /* And similarly for reg_names. */
258 gcc_assert (sizeof reg_names == sizeof saved_reg_names);
259 memcpy (saved_reg_names, reg_names, sizeof reg_names);
260 COPY_HARD_REG_SET (saved_accessible_reg_set, accessible_reg_set);
261 COPY_HARD_REG_SET (saved_operand_reg_set, operand_reg_set);
264 /* Restore the register information. */
265 static void
266 restore_register_info (void)
268 memcpy (fixed_regs, saved_fixed_regs, sizeof fixed_regs);
269 memcpy (call_used_regs, saved_call_used_regs, sizeof call_used_regs);
271 #ifdef CALL_REALLY_USED_REGISTERS
272 memcpy (call_really_used_regs, saved_call_really_used_regs,
273 sizeof call_really_used_regs);
274 #endif
276 memcpy (reg_names, saved_reg_names, sizeof reg_names);
277 COPY_HARD_REG_SET (accessible_reg_set, saved_accessible_reg_set);
278 COPY_HARD_REG_SET (operand_reg_set, saved_operand_reg_set);
281 /* After switches have been processed, which perhaps alter
282 `fixed_regs' and `call_used_regs', convert them to HARD_REG_SETs. */
283 static void
284 init_reg_sets_1 (void)
286 unsigned int i, j;
287 unsigned int /* machine_mode */ m;
289 restore_register_info ();
291 #ifdef REG_ALLOC_ORDER
292 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
293 inv_reg_alloc_order[reg_alloc_order[i]] = i;
294 #endif
296 /* Let the target tweak things if necessary. */
298 targetm.conditional_register_usage ();
300 /* Compute number of hard regs in each class. */
302 memset (reg_class_size, 0, sizeof reg_class_size);
303 for (i = 0; i < N_REG_CLASSES; i++)
305 bool any_nonfixed = false;
306 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
307 if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
309 reg_class_size[i]++;
310 if (!fixed_regs[j])
311 any_nonfixed = true;
313 class_only_fixed_regs[i] = !any_nonfixed;
316 /* Initialize the table of subunions.
317 reg_class_subunion[I][J] gets the largest-numbered reg-class
318 that is contained in the union of classes I and J. */
320 memset (reg_class_subunion, 0, sizeof reg_class_subunion);
321 for (i = 0; i < N_REG_CLASSES; i++)
323 for (j = 0; j < N_REG_CLASSES; j++)
325 HARD_REG_SET c;
326 int k;
328 COPY_HARD_REG_SET (c, reg_class_contents[i]);
329 IOR_HARD_REG_SET (c, reg_class_contents[j]);
330 for (k = 0; k < N_REG_CLASSES; k++)
331 if (hard_reg_set_subset_p (reg_class_contents[k], c)
332 && !hard_reg_set_subset_p (reg_class_contents[k],
333 reg_class_contents
334 [(int) reg_class_subunion[i][j]]))
335 reg_class_subunion[i][j] = (enum reg_class) k;
339 /* Initialize the table of superunions.
340 reg_class_superunion[I][J] gets the smallest-numbered reg-class
341 containing the union of classes I and J. */
343 memset (reg_class_superunion, 0, sizeof reg_class_superunion);
344 for (i = 0; i < N_REG_CLASSES; i++)
346 for (j = 0; j < N_REG_CLASSES; j++)
348 HARD_REG_SET c;
349 int k;
351 COPY_HARD_REG_SET (c, reg_class_contents[i]);
352 IOR_HARD_REG_SET (c, reg_class_contents[j]);
353 for (k = 0; k < N_REG_CLASSES; k++)
354 if (hard_reg_set_subset_p (c, reg_class_contents[k]))
355 break;
357 reg_class_superunion[i][j] = (enum reg_class) k;
361 /* Initialize the tables of subclasses and superclasses of each reg class.
362 First clear the whole table, then add the elements as they are found. */
364 for (i = 0; i < N_REG_CLASSES; i++)
366 for (j = 0; j < N_REG_CLASSES; j++)
367 reg_class_subclasses[i][j] = LIM_REG_CLASSES;
370 for (i = 0; i < N_REG_CLASSES; i++)
372 if (i == (int) NO_REGS)
373 continue;
375 for (j = i + 1; j < N_REG_CLASSES; j++)
376 if (hard_reg_set_subset_p (reg_class_contents[i],
377 reg_class_contents[j]))
379 /* Reg class I is a subclass of J.
380 Add J to the table of superclasses of I. */
381 enum reg_class *p;
383 /* Add I to the table of superclasses of J. */
384 p = &reg_class_subclasses[j][0];
385 while (*p != LIM_REG_CLASSES) p++;
386 *p = (enum reg_class) i;
390 /* Initialize "constant" tables. */
392 CLEAR_HARD_REG_SET (fixed_reg_set);
393 CLEAR_HARD_REG_SET (call_used_reg_set);
394 CLEAR_HARD_REG_SET (call_fixed_reg_set);
395 CLEAR_HARD_REG_SET (regs_invalidated_by_call);
396 if (!regs_invalidated_by_call_regset)
398 bitmap_obstack_initialize (&persistent_obstack);
399 regs_invalidated_by_call_regset = ALLOC_REG_SET (&persistent_obstack);
401 else
402 CLEAR_REG_SET (regs_invalidated_by_call_regset);
403 if (!fixed_reg_set_regset)
404 fixed_reg_set_regset = ALLOC_REG_SET (&persistent_obstack);
405 else
406 CLEAR_REG_SET (fixed_reg_set_regset);
408 AND_HARD_REG_SET (operand_reg_set, accessible_reg_set);
409 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
411 /* As a special exception, registers whose class is NO_REGS are
412 not accepted by `register_operand'. The reason for this change
413 is to allow the representation of special architecture artifacts
414 (such as a condition code register) without extending the rtl
415 definitions. Since registers of class NO_REGS cannot be used
416 as registers in any case where register classes are examined,
417 it is better to apply this exception in a target-independent way. */
418 if (REGNO_REG_CLASS (i) == NO_REGS)
419 CLEAR_HARD_REG_BIT (operand_reg_set, i);
421 /* If a register is too limited to be treated as a register operand,
422 then it should never be allocated to a pseudo. */
423 if (!TEST_HARD_REG_BIT (operand_reg_set, i))
425 fixed_regs[i] = 1;
426 call_used_regs[i] = 1;
429 /* call_used_regs must include fixed_regs. */
430 gcc_assert (!fixed_regs[i] || call_used_regs[i]);
431 #ifdef CALL_REALLY_USED_REGISTERS
432 /* call_used_regs must include call_really_used_regs. */
433 gcc_assert (!call_really_used_regs[i] || call_used_regs[i]);
434 #endif
436 if (fixed_regs[i])
438 SET_HARD_REG_BIT (fixed_reg_set, i);
439 SET_REGNO_REG_SET (fixed_reg_set_regset, i);
442 if (call_used_regs[i])
443 SET_HARD_REG_BIT (call_used_reg_set, i);
445 /* There are a couple of fixed registers that we know are safe to
446 exclude from being clobbered by calls:
448 The frame pointer is always preserved across calls. The arg
449 pointer is if it is fixed. The stack pointer usually is,
450 unless TARGET_RETURN_POPS_ARGS, in which case an explicit
451 CLOBBER will be present. If we are generating PIC code, the
452 PIC offset table register is preserved across calls, though the
453 target can override that. */
455 if (i == STACK_POINTER_REGNUM)
457 else if (global_regs[i])
459 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
460 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
462 else if (i == FRAME_POINTER_REGNUM)
464 else if (!HARD_FRAME_POINTER_IS_FRAME_POINTER
465 && i == HARD_FRAME_POINTER_REGNUM)
467 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
468 else if (i == ARG_POINTER_REGNUM && fixed_regs[i])
470 #endif
471 else if (!PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
472 && i == (unsigned) PIC_OFFSET_TABLE_REGNUM && fixed_regs[i])
474 else if (CALL_REALLY_USED_REGNO_P (i))
476 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
477 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
481 COPY_HARD_REG_SET (call_fixed_reg_set, fixed_reg_set);
483 /* Preserve global registers if called more than once. */
484 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
486 if (global_regs[i])
488 fixed_regs[i] = call_used_regs[i] = 1;
489 SET_HARD_REG_BIT (fixed_reg_set, i);
490 SET_HARD_REG_BIT (call_used_reg_set, i);
491 SET_HARD_REG_BIT (call_fixed_reg_set, i);
495 memset (have_regs_of_mode, 0, sizeof (have_regs_of_mode));
496 memset (contains_reg_of_mode, 0, sizeof (contains_reg_of_mode));
497 for (m = 0; m < (unsigned int) MAX_MACHINE_MODE; m++)
499 HARD_REG_SET ok_regs;
500 CLEAR_HARD_REG_SET (ok_regs);
501 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
502 if (!fixed_regs [j] && HARD_REGNO_MODE_OK (j, (machine_mode) m))
503 SET_HARD_REG_BIT (ok_regs, j);
505 for (i = 0; i < N_REG_CLASSES; i++)
506 if ((targetm.class_max_nregs ((reg_class_t) i, (machine_mode) m)
507 <= reg_class_size[i])
508 && hard_reg_set_intersect_p (ok_regs, reg_class_contents[i]))
510 contains_reg_of_mode [i][m] = 1;
511 have_regs_of_mode [m] = 1;
516 /* Compute the table of register modes.
517 These values are used to record death information for individual registers
518 (as opposed to a multi-register mode).
519 This function might be invoked more than once, if the target has support
520 for changing register usage conventions on a per-function basis.
522 void
523 init_reg_modes_target (void)
525 int i, j;
527 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
528 for (j = 0; j < MAX_MACHINE_MODE; j++)
529 hard_regno_nregs[i][j] = HARD_REGNO_NREGS (i, (machine_mode)j);
531 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
533 reg_raw_mode[i] = choose_hard_reg_mode (i, 1, false);
535 /* If we couldn't find a valid mode, just use the previous mode
536 if it is suitable, otherwise fall back on word_mode. */
537 if (reg_raw_mode[i] == VOIDmode)
539 if (i > 0 && hard_regno_nregs[i][reg_raw_mode[i - 1]] == 1)
540 reg_raw_mode[i] = reg_raw_mode[i - 1];
541 else
542 reg_raw_mode[i] = word_mode;
547 /* Finish initializing the register sets and initialize the register modes.
548 This function might be invoked more than once, if the target has support
549 for changing register usage conventions on a per-function basis.
551 void
552 init_regs (void)
554 /* This finishes what was started by init_reg_sets, but couldn't be done
555 until after register usage was specified. */
556 init_reg_sets_1 ();
559 /* The same as previous function plus initializing IRA. */
560 void
561 reinit_regs (void)
563 init_regs ();
564 /* caller_save needs to be re-initialized. */
565 caller_save_initialized_p = false;
566 if (this_target_rtl->target_specific_initialized)
568 ira_init ();
569 recog_init ();
573 /* Initialize some fake stack-frame MEM references for use in
574 memory_move_secondary_cost. */
575 void
576 init_fake_stack_mems (void)
578 int i;
580 for (i = 0; i < MAX_MACHINE_MODE; i++)
581 top_of_stack[i] = gen_rtx_MEM ((machine_mode) i, stack_pointer_rtx);
585 /* Compute cost of moving data from a register of class FROM to one of
586 TO, using MODE. */
589 register_move_cost (machine_mode mode, reg_class_t from, reg_class_t to)
591 return targetm.register_move_cost (mode, from, to);
594 /* Compute cost of moving registers to/from memory. */
597 memory_move_cost (machine_mode mode, reg_class_t rclass, bool in)
599 return targetm.memory_move_cost (mode, rclass, in);
602 /* Compute extra cost of moving registers to/from memory due to reloads.
603 Only needed if secondary reloads are required for memory moves. */
605 memory_move_secondary_cost (machine_mode mode, reg_class_t rclass,
606 bool in)
608 reg_class_t altclass;
609 int partial_cost = 0;
610 /* We need a memory reference to feed to SECONDARY... macros. */
611 /* mem may be unused even if the SECONDARY_ macros are defined. */
612 rtx mem ATTRIBUTE_UNUSED = top_of_stack[(int) mode];
614 altclass = secondary_reload_class (in ? 1 : 0, rclass, mode, mem);
616 if (altclass == NO_REGS)
617 return 0;
619 if (in)
620 partial_cost = register_move_cost (mode, altclass, rclass);
621 else
622 partial_cost = register_move_cost (mode, rclass, altclass);
624 if (rclass == altclass)
625 /* This isn't simply a copy-to-temporary situation. Can't guess
626 what it is, so TARGET_MEMORY_MOVE_COST really ought not to be
627 calling here in that case.
629 I'm tempted to put in an assert here, but returning this will
630 probably only give poor estimates, which is what we would've
631 had before this code anyways. */
632 return partial_cost;
634 /* Check if the secondary reload register will also need a
635 secondary reload. */
636 return memory_move_secondary_cost (mode, altclass, in) + partial_cost;
639 /* Return a machine mode that is legitimate for hard reg REGNO and large
640 enough to save nregs. If we can't find one, return VOIDmode.
641 If CALL_SAVED is true, only consider modes that are call saved. */
642 machine_mode
643 choose_hard_reg_mode (unsigned int regno ATTRIBUTE_UNUSED,
644 unsigned int nregs, bool call_saved)
646 unsigned int /* machine_mode */ m;
647 machine_mode found_mode = VOIDmode, mode;
649 /* We first look for the largest integer mode that can be validly
650 held in REGNO. If none, we look for the largest floating-point mode.
651 If we still didn't find a valid mode, try CCmode. */
653 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
654 mode != VOIDmode;
655 mode = GET_MODE_WIDER_MODE (mode))
656 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
657 && HARD_REGNO_MODE_OK (regno, mode)
658 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
659 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (found_mode))
660 found_mode = mode;
662 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
663 mode != VOIDmode;
664 mode = GET_MODE_WIDER_MODE (mode))
665 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
666 && HARD_REGNO_MODE_OK (regno, mode)
667 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
668 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (found_mode))
669 found_mode = mode;
671 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
672 mode != VOIDmode;
673 mode = GET_MODE_WIDER_MODE (mode))
674 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
675 && HARD_REGNO_MODE_OK (regno, mode)
676 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
677 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (found_mode))
678 found_mode = mode;
680 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
681 mode != VOIDmode;
682 mode = GET_MODE_WIDER_MODE (mode))
683 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
684 && HARD_REGNO_MODE_OK (regno, mode)
685 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
686 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (found_mode))
687 found_mode = mode;
689 if (found_mode != VOIDmode)
690 return found_mode;
692 /* Iterate over all of the CCmodes. */
693 for (m = (unsigned int) CCmode; m < (unsigned int) NUM_MACHINE_MODES; ++m)
695 mode = (machine_mode) m;
696 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
697 && HARD_REGNO_MODE_OK (regno, mode)
698 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
699 return mode;
702 /* We can't find a mode valid for this register. */
703 return VOIDmode;
706 /* Specify the usage characteristics of the register named NAME.
707 It should be a fixed register if FIXED and a
708 call-used register if CALL_USED. */
709 void
710 fix_register (const char *name, int fixed, int call_used)
712 int i;
713 int reg, nregs;
715 /* Decode the name and update the primary form of
716 the register info. */
718 if ((reg = decode_reg_name_and_count (name, &nregs)) >= 0)
720 gcc_assert (nregs >= 1);
721 for (i = reg; i < reg + nregs; i++)
723 if ((i == STACK_POINTER_REGNUM
724 #ifdef HARD_FRAME_POINTER_REGNUM
725 || i == HARD_FRAME_POINTER_REGNUM
726 #else
727 || i == FRAME_POINTER_REGNUM
728 #endif
730 && (fixed == 0 || call_used == 0))
732 switch (fixed)
734 case 0:
735 switch (call_used)
737 case 0:
738 error ("can%'t use %qs as a call-saved register", name);
739 break;
741 case 1:
742 error ("can%'t use %qs as a call-used register", name);
743 break;
745 default:
746 gcc_unreachable ();
748 break;
750 case 1:
751 switch (call_used)
753 case 1:
754 error ("can%'t use %qs as a fixed register", name);
755 break;
757 case 0:
758 default:
759 gcc_unreachable ();
761 break;
763 default:
764 gcc_unreachable ();
767 else
769 fixed_regs[i] = fixed;
770 call_used_regs[i] = call_used;
771 #ifdef CALL_REALLY_USED_REGISTERS
772 if (fixed == 0)
773 call_really_used_regs[i] = call_used;
774 #endif
778 else
780 warning (0, "unknown register name: %s", name);
784 /* Mark register number I as global. */
785 void
786 globalize_reg (tree decl, int i)
788 location_t loc = DECL_SOURCE_LOCATION (decl);
790 #ifdef STACK_REGS
791 if (IN_RANGE (i, FIRST_STACK_REG, LAST_STACK_REG))
793 error ("stack register used for global register variable");
794 return;
796 #endif
798 if (fixed_regs[i] == 0 && no_global_reg_vars)
799 error_at (loc, "global register variable follows a function definition");
801 if (global_regs[i])
803 warning_at (loc, 0,
804 "register of %qD used for multiple global register variables",
805 decl);
806 inform (DECL_SOURCE_LOCATION (global_regs_decl[i]),
807 "conflicts with %qD", global_regs_decl[i]);
808 return;
811 if (call_used_regs[i] && ! fixed_regs[i])
812 warning_at (loc, 0, "call-clobbered register used for global register variable");
814 global_regs[i] = 1;
815 global_regs_decl[i] = decl;
817 /* If we're globalizing the frame pointer, we need to set the
818 appropriate regs_invalidated_by_call bit, even if it's already
819 set in fixed_regs. */
820 if (i != STACK_POINTER_REGNUM)
822 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
823 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
826 /* If already fixed, nothing else to do. */
827 if (fixed_regs[i])
828 return;
830 fixed_regs[i] = call_used_regs[i] = 1;
831 #ifdef CALL_REALLY_USED_REGISTERS
832 call_really_used_regs[i] = 1;
833 #endif
835 SET_HARD_REG_BIT (fixed_reg_set, i);
836 SET_HARD_REG_BIT (call_used_reg_set, i);
837 SET_HARD_REG_BIT (call_fixed_reg_set, i);
839 reinit_regs ();
843 /* Structure used to record preferences of given pseudo. */
844 struct reg_pref
846 /* (enum reg_class) prefclass is the preferred class. May be
847 NO_REGS if no class is better than memory. */
848 char prefclass;
850 /* altclass is a register class that we should use for allocating
851 pseudo if no register in the preferred class is available.
852 If no register in this class is available, memory is preferred.
854 It might appear to be more general to have a bitmask of classes here,
855 but since it is recommended that there be a class corresponding to the
856 union of most major pair of classes, that generality is not required. */
857 char altclass;
859 /* allocnoclass is a register class that IRA uses for allocating
860 the pseudo. */
861 char allocnoclass;
864 /* Record preferences of each pseudo. This is available after RA is
865 run. */
866 static struct reg_pref *reg_pref;
868 /* Current size of reg_info. */
869 static int reg_info_size;
870 /* Max_reg_num still last resize_reg_info call. */
871 static int max_regno_since_last_resize;
873 /* Return the reg_class in which pseudo reg number REGNO is best allocated.
874 This function is sometimes called before the info has been computed.
875 When that happens, just return GENERAL_REGS, which is innocuous. */
876 enum reg_class
877 reg_preferred_class (int regno)
879 if (reg_pref == 0)
880 return GENERAL_REGS;
882 gcc_assert (regno < reg_info_size);
883 return (enum reg_class) reg_pref[regno].prefclass;
886 enum reg_class
887 reg_alternate_class (int regno)
889 if (reg_pref == 0)
890 return ALL_REGS;
892 gcc_assert (regno < reg_info_size);
893 return (enum reg_class) reg_pref[regno].altclass;
896 /* Return the reg_class which is used by IRA for its allocation. */
897 enum reg_class
898 reg_allocno_class (int regno)
900 if (reg_pref == 0)
901 return NO_REGS;
903 gcc_assert (regno < reg_info_size);
904 return (enum reg_class) reg_pref[regno].allocnoclass;
909 /* Allocate space for reg info and initilize it. */
910 static void
911 allocate_reg_info (void)
913 int i;
915 max_regno_since_last_resize = max_reg_num ();
916 reg_info_size = max_regno_since_last_resize * 3 / 2 + 1;
917 gcc_assert (! reg_pref && ! reg_renumber);
918 reg_renumber = XNEWVEC (short, reg_info_size);
919 reg_pref = XCNEWVEC (struct reg_pref, reg_info_size);
920 memset (reg_renumber, -1, reg_info_size * sizeof (short));
921 for (i = 0; i < reg_info_size; i++)
923 reg_pref[i].prefclass = GENERAL_REGS;
924 reg_pref[i].altclass = ALL_REGS;
925 reg_pref[i].allocnoclass = GENERAL_REGS;
930 /* Resize reg info. The new elements will be initialized. Return TRUE
931 if new pseudos were added since the last call. */
932 bool
933 resize_reg_info (void)
935 int old, i;
936 bool change_p;
938 if (reg_pref == NULL)
940 allocate_reg_info ();
941 return true;
943 change_p = max_regno_since_last_resize != max_reg_num ();
944 max_regno_since_last_resize = max_reg_num ();
945 if (reg_info_size >= max_reg_num ())
946 return change_p;
947 old = reg_info_size;
948 reg_info_size = max_reg_num () * 3 / 2 + 1;
949 gcc_assert (reg_pref && reg_renumber);
950 reg_renumber = XRESIZEVEC (short, reg_renumber, reg_info_size);
951 reg_pref = XRESIZEVEC (struct reg_pref, reg_pref, reg_info_size);
952 memset (reg_pref + old, -1,
953 (reg_info_size - old) * sizeof (struct reg_pref));
954 memset (reg_renumber + old, -1, (reg_info_size - old) * sizeof (short));
955 for (i = old; i < reg_info_size; i++)
957 reg_pref[i].prefclass = GENERAL_REGS;
958 reg_pref[i].altclass = ALL_REGS;
959 reg_pref[i].allocnoclass = GENERAL_REGS;
961 return true;
965 /* Free up the space allocated by allocate_reg_info. */
966 void
967 free_reg_info (void)
969 if (reg_pref)
971 free (reg_pref);
972 reg_pref = NULL;
975 if (reg_renumber)
977 free (reg_renumber);
978 reg_renumber = NULL;
982 /* Initialize some global data for this pass. */
983 static unsigned int
984 reginfo_init (void)
986 if (df)
987 df_compute_regs_ever_live (true);
989 /* This prevents dump_reg_info from losing if called
990 before reginfo is run. */
991 reg_pref = NULL;
992 reg_info_size = max_regno_since_last_resize = 0;
993 /* No more global register variables may be declared. */
994 no_global_reg_vars = 1;
995 return 1;
998 namespace {
1000 const pass_data pass_data_reginfo_init =
1002 RTL_PASS, /* type */
1003 "reginfo", /* name */
1004 OPTGROUP_NONE, /* optinfo_flags */
1005 TV_NONE, /* tv_id */
1006 0, /* properties_required */
1007 0, /* properties_provided */
1008 0, /* properties_destroyed */
1009 0, /* todo_flags_start */
1010 0, /* todo_flags_finish */
1013 class pass_reginfo_init : public rtl_opt_pass
1015 public:
1016 pass_reginfo_init (gcc::context *ctxt)
1017 : rtl_opt_pass (pass_data_reginfo_init, ctxt)
1020 /* opt_pass methods: */
1021 virtual unsigned int execute (function *) { return reginfo_init (); }
1023 }; // class pass_reginfo_init
1025 } // anon namespace
1027 rtl_opt_pass *
1028 make_pass_reginfo_init (gcc::context *ctxt)
1030 return new pass_reginfo_init (ctxt);
1035 /* Set up preferred, alternate, and allocno classes for REGNO as
1036 PREFCLASS, ALTCLASS, and ALLOCNOCLASS. */
1037 void
1038 setup_reg_classes (int regno,
1039 enum reg_class prefclass, enum reg_class altclass,
1040 enum reg_class allocnoclass)
1042 if (reg_pref == NULL)
1043 return;
1044 gcc_assert (reg_info_size >= max_reg_num ());
1045 reg_pref[regno].prefclass = prefclass;
1046 reg_pref[regno].altclass = altclass;
1047 reg_pref[regno].allocnoclass = allocnoclass;
1051 /* This is the `regscan' pass of the compiler, run just before cse and
1052 again just before loop. It finds the first and last use of each
1053 pseudo-register. */
1055 static void reg_scan_mark_refs (rtx, rtx_insn *);
1057 void
1058 reg_scan (rtx_insn *f, unsigned int nregs ATTRIBUTE_UNUSED)
1060 rtx_insn *insn;
1062 timevar_push (TV_REG_SCAN);
1064 for (insn = f; insn; insn = NEXT_INSN (insn))
1065 if (INSN_P (insn))
1067 reg_scan_mark_refs (PATTERN (insn), insn);
1068 if (REG_NOTES (insn))
1069 reg_scan_mark_refs (REG_NOTES (insn), insn);
1072 timevar_pop (TV_REG_SCAN);
1076 /* X is the expression to scan. INSN is the insn it appears in.
1077 NOTE_FLAG is nonzero if X is from INSN's notes rather than its body.
1078 We should only record information for REGs with numbers
1079 greater than or equal to MIN_REGNO. */
1080 static void
1081 reg_scan_mark_refs (rtx x, rtx_insn *insn)
1083 enum rtx_code code;
1084 rtx dest;
1085 rtx note;
1087 if (!x)
1088 return;
1089 code = GET_CODE (x);
1090 switch (code)
1092 case CONST:
1093 CASE_CONST_ANY:
1094 case CC0:
1095 case PC:
1096 case SYMBOL_REF:
1097 case LABEL_REF:
1098 case ADDR_VEC:
1099 case ADDR_DIFF_VEC:
1100 case REG:
1101 return;
1103 case EXPR_LIST:
1104 if (XEXP (x, 0))
1105 reg_scan_mark_refs (XEXP (x, 0), insn);
1106 if (XEXP (x, 1))
1107 reg_scan_mark_refs (XEXP (x, 1), insn);
1108 break;
1110 case INSN_LIST:
1111 case INT_LIST:
1112 if (XEXP (x, 1))
1113 reg_scan_mark_refs (XEXP (x, 1), insn);
1114 break;
1116 case CLOBBER:
1117 if (MEM_P (XEXP (x, 0)))
1118 reg_scan_mark_refs (XEXP (XEXP (x, 0), 0), insn);
1119 break;
1121 case SET:
1122 /* Count a set of the destination if it is a register. */
1123 for (dest = SET_DEST (x);
1124 GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
1125 || GET_CODE (dest) == ZERO_EXTRACT;
1126 dest = XEXP (dest, 0))
1129 /* If this is setting a pseudo from another pseudo or the sum of a
1130 pseudo and a constant integer and the other pseudo is known to be
1131 a pointer, set the destination to be a pointer as well.
1133 Likewise if it is setting the destination from an address or from a
1134 value equivalent to an address or to the sum of an address and
1135 something else.
1137 But don't do any of this if the pseudo corresponds to a user
1138 variable since it should have already been set as a pointer based
1139 on the type. */
1141 if (REG_P (SET_DEST (x))
1142 && REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER
1143 /* If the destination pseudo is set more than once, then other
1144 sets might not be to a pointer value (consider access to a
1145 union in two threads of control in the presence of global
1146 optimizations). So only set REG_POINTER on the destination
1147 pseudo if this is the only set of that pseudo. */
1148 && DF_REG_DEF_COUNT (REGNO (SET_DEST (x))) == 1
1149 && ! REG_USERVAR_P (SET_DEST (x))
1150 && ! REG_POINTER (SET_DEST (x))
1151 && ((REG_P (SET_SRC (x))
1152 && REG_POINTER (SET_SRC (x)))
1153 || ((GET_CODE (SET_SRC (x)) == PLUS
1154 || GET_CODE (SET_SRC (x)) == LO_SUM)
1155 && CONST_INT_P (XEXP (SET_SRC (x), 1))
1156 && REG_P (XEXP (SET_SRC (x), 0))
1157 && REG_POINTER (XEXP (SET_SRC (x), 0)))
1158 || GET_CODE (SET_SRC (x)) == CONST
1159 || GET_CODE (SET_SRC (x)) == SYMBOL_REF
1160 || GET_CODE (SET_SRC (x)) == LABEL_REF
1161 || (GET_CODE (SET_SRC (x)) == HIGH
1162 && (GET_CODE (XEXP (SET_SRC (x), 0)) == CONST
1163 || GET_CODE (XEXP (SET_SRC (x), 0)) == SYMBOL_REF
1164 || GET_CODE (XEXP (SET_SRC (x), 0)) == LABEL_REF))
1165 || ((GET_CODE (SET_SRC (x)) == PLUS
1166 || GET_CODE (SET_SRC (x)) == LO_SUM)
1167 && (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST
1168 || GET_CODE (XEXP (SET_SRC (x), 1)) == SYMBOL_REF
1169 || GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF))
1170 || ((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
1171 && (GET_CODE (XEXP (note, 0)) == CONST
1172 || GET_CODE (XEXP (note, 0)) == SYMBOL_REF
1173 || GET_CODE (XEXP (note, 0)) == LABEL_REF))))
1174 REG_POINTER (SET_DEST (x)) = 1;
1176 /* If this is setting a register from a register or from a simple
1177 conversion of a register, propagate REG_EXPR. */
1178 if (REG_P (dest) && !REG_ATTRS (dest))
1179 set_reg_attrs_from_value (dest, SET_SRC (x));
1181 /* ... fall through ... */
1183 default:
1185 const char *fmt = GET_RTX_FORMAT (code);
1186 int i;
1187 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1189 if (fmt[i] == 'e')
1190 reg_scan_mark_refs (XEXP (x, i), insn);
1191 else if (fmt[i] == 'E' && XVEC (x, i) != 0)
1193 int j;
1194 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1195 reg_scan_mark_refs (XVECEXP (x, i, j), insn);
1203 /* Return nonzero if C1 is a subset of C2, i.e., if every register in C1
1204 is also in C2. */
1206 reg_class_subset_p (reg_class_t c1, reg_class_t c2)
1208 return (c1 == c2
1209 || c2 == ALL_REGS
1210 || hard_reg_set_subset_p (reg_class_contents[(int) c1],
1211 reg_class_contents[(int) c2]));
1214 /* Return nonzero if there is a register that is in both C1 and C2. */
1216 reg_classes_intersect_p (reg_class_t c1, reg_class_t c2)
1218 return (c1 == c2
1219 || c1 == ALL_REGS
1220 || c2 == ALL_REGS
1221 || hard_reg_set_intersect_p (reg_class_contents[(int) c1],
1222 reg_class_contents[(int) c2]));
1226 inline hashval_t
1227 simplifiable_subregs_hasher::hash (const simplifiable_subreg *value)
1229 return value->shape.unique_id ();
1232 inline bool
1233 simplifiable_subregs_hasher::equal (const simplifiable_subreg *value,
1234 const subreg_shape *compare)
1236 return value->shape == *compare;
1239 inline simplifiable_subreg::simplifiable_subreg (const subreg_shape &shape_in)
1240 : shape (shape_in)
1242 CLEAR_HARD_REG_SET (simplifiable_regs);
1245 /* Return the set of hard registers that are able to form the subreg
1246 described by SHAPE. */
1248 const HARD_REG_SET &
1249 simplifiable_subregs (const subreg_shape &shape)
1251 if (!this_target_hard_regs->x_simplifiable_subregs)
1252 this_target_hard_regs->x_simplifiable_subregs
1253 = new hash_table <simplifiable_subregs_hasher> (30);
1254 simplifiable_subreg **slot
1255 = (this_target_hard_regs->x_simplifiable_subregs
1256 ->find_slot_with_hash (&shape, shape.unique_id (), INSERT));
1258 if (!*slot)
1260 simplifiable_subreg *info = new simplifiable_subreg (shape);
1261 for (unsigned int i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1262 if (HARD_REGNO_MODE_OK (i, shape.inner_mode)
1263 && simplify_subreg_regno (i, shape.inner_mode, shape.offset,
1264 shape.outer_mode) >= 0)
1265 SET_HARD_REG_BIT (info->simplifiable_regs, i);
1266 *slot = info;
1268 return (*slot)->simplifiable_regs;
1271 /* Passes for keeping and updating info about modes of registers
1272 inside subregisters. */
1274 static HARD_REG_SET **valid_mode_changes;
1275 static obstack valid_mode_changes_obstack;
1277 static void
1278 record_subregs_of_mode (rtx subreg)
1280 unsigned int regno;
1282 if (!REG_P (SUBREG_REG (subreg)))
1283 return;
1285 regno = REGNO (SUBREG_REG (subreg));
1286 if (regno < FIRST_PSEUDO_REGISTER)
1287 return;
1289 if (valid_mode_changes[regno])
1290 AND_HARD_REG_SET (*valid_mode_changes[regno],
1291 simplifiable_subregs (shape_of_subreg (subreg)));
1292 else
1294 valid_mode_changes[regno]
1295 = XOBNEW (&valid_mode_changes_obstack, HARD_REG_SET);
1296 COPY_HARD_REG_SET (*valid_mode_changes[regno],
1297 simplifiable_subregs (shape_of_subreg (subreg)));
1301 /* Call record_subregs_of_mode for all the subregs in X. */
1302 static void
1303 find_subregs_of_mode (rtx x)
1305 enum rtx_code code = GET_CODE (x);
1306 const char * const fmt = GET_RTX_FORMAT (code);
1307 int i;
1309 if (code == SUBREG)
1310 record_subregs_of_mode (x);
1312 /* Time for some deep diving. */
1313 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1315 if (fmt[i] == 'e')
1316 find_subregs_of_mode (XEXP (x, i));
1317 else if (fmt[i] == 'E')
1319 int j;
1320 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1321 find_subregs_of_mode (XVECEXP (x, i, j));
1326 void
1327 init_subregs_of_mode (void)
1329 basic_block bb;
1330 rtx_insn *insn;
1332 gcc_obstack_init (&valid_mode_changes_obstack);
1333 valid_mode_changes = XCNEWVEC (HARD_REG_SET *, max_reg_num ());
1335 FOR_EACH_BB_FN (bb, cfun)
1336 FOR_BB_INSNS (bb, insn)
1337 if (NONDEBUG_INSN_P (insn))
1338 find_subregs_of_mode (PATTERN (insn));
1341 const HARD_REG_SET *
1342 valid_mode_changes_for_regno (unsigned int regno)
1344 return valid_mode_changes[regno];
1347 void
1348 finish_subregs_of_mode (void)
1350 XDELETEVEC (valid_mode_changes);
1351 obstack_free (&valid_mode_changes_obstack, NULL);
1354 /* Free all data attached to the structure. This isn't a destructor because
1355 we don't want to run on exit. */
1357 void
1358 target_hard_regs::finalize ()
1360 delete x_simplifiable_subregs;