remove need for store_values_directly
[official-gcc.git] / gcc / reginfo.c
blob6678688e105c2acc6559b041ebcf0099bfbb1488
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 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
465 else if (i == HARD_FRAME_POINTER_REGNUM)
467 #endif
468 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
469 else if (i == ARG_POINTER_REGNUM && fixed_regs[i])
471 #endif
472 else if (!PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
473 && i == (unsigned) PIC_OFFSET_TABLE_REGNUM && fixed_regs[i])
475 else if (CALL_REALLY_USED_REGNO_P (i))
477 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
478 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
482 COPY_HARD_REG_SET (call_fixed_reg_set, fixed_reg_set);
484 /* Preserve global registers if called more than once. */
485 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
487 if (global_regs[i])
489 fixed_regs[i] = call_used_regs[i] = 1;
490 SET_HARD_REG_BIT (fixed_reg_set, i);
491 SET_HARD_REG_BIT (call_used_reg_set, i);
492 SET_HARD_REG_BIT (call_fixed_reg_set, i);
496 memset (have_regs_of_mode, 0, sizeof (have_regs_of_mode));
497 memset (contains_reg_of_mode, 0, sizeof (contains_reg_of_mode));
498 for (m = 0; m < (unsigned int) MAX_MACHINE_MODE; m++)
500 HARD_REG_SET ok_regs;
501 CLEAR_HARD_REG_SET (ok_regs);
502 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
503 if (!fixed_regs [j] && HARD_REGNO_MODE_OK (j, (machine_mode) m))
504 SET_HARD_REG_BIT (ok_regs, j);
506 for (i = 0; i < N_REG_CLASSES; i++)
507 if ((targetm.class_max_nregs ((reg_class_t) i, (machine_mode) m)
508 <= reg_class_size[i])
509 && hard_reg_set_intersect_p (ok_regs, reg_class_contents[i]))
511 contains_reg_of_mode [i][m] = 1;
512 have_regs_of_mode [m] = 1;
517 /* Compute the table of register modes.
518 These values are used to record death information for individual registers
519 (as opposed to a multi-register mode).
520 This function might be invoked more than once, if the target has support
521 for changing register usage conventions on a per-function basis.
523 void
524 init_reg_modes_target (void)
526 int i, j;
528 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
529 for (j = 0; j < MAX_MACHINE_MODE; j++)
530 hard_regno_nregs[i][j] = HARD_REGNO_NREGS (i, (machine_mode)j);
532 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
534 reg_raw_mode[i] = choose_hard_reg_mode (i, 1, false);
536 /* If we couldn't find a valid mode, just use the previous mode
537 if it is suitable, otherwise fall back on word_mode. */
538 if (reg_raw_mode[i] == VOIDmode)
540 if (i > 0 && hard_regno_nregs[i][reg_raw_mode[i - 1]] == 1)
541 reg_raw_mode[i] = reg_raw_mode[i - 1];
542 else
543 reg_raw_mode[i] = word_mode;
548 /* Finish initializing the register sets and initialize the register modes.
549 This function might be invoked more than once, if the target has support
550 for changing register usage conventions on a per-function basis.
552 void
553 init_regs (void)
555 /* This finishes what was started by init_reg_sets, but couldn't be done
556 until after register usage was specified. */
557 init_reg_sets_1 ();
560 /* The same as previous function plus initializing IRA. */
561 void
562 reinit_regs (void)
564 init_regs ();
565 /* caller_save needs to be re-initialized. */
566 caller_save_initialized_p = false;
567 if (this_target_rtl->target_specific_initialized)
569 ira_init ();
570 recog_init ();
574 /* Initialize some fake stack-frame MEM references for use in
575 memory_move_secondary_cost. */
576 void
577 init_fake_stack_mems (void)
579 int i;
581 for (i = 0; i < MAX_MACHINE_MODE; i++)
582 top_of_stack[i] = gen_rtx_MEM ((machine_mode) i, stack_pointer_rtx);
586 /* Compute cost of moving data from a register of class FROM to one of
587 TO, using MODE. */
590 register_move_cost (machine_mode mode, reg_class_t from, reg_class_t to)
592 return targetm.register_move_cost (mode, from, to);
595 /* Compute cost of moving registers to/from memory. */
598 memory_move_cost (machine_mode mode, reg_class_t rclass, bool in)
600 return targetm.memory_move_cost (mode, rclass, in);
603 /* Compute extra cost of moving registers to/from memory due to reloads.
604 Only needed if secondary reloads are required for memory moves. */
606 memory_move_secondary_cost (machine_mode mode, reg_class_t rclass,
607 bool in)
609 reg_class_t altclass;
610 int partial_cost = 0;
611 /* We need a memory reference to feed to SECONDARY... macros. */
612 /* mem may be unused even if the SECONDARY_ macros are defined. */
613 rtx mem ATTRIBUTE_UNUSED = top_of_stack[(int) mode];
615 altclass = secondary_reload_class (in ? 1 : 0, rclass, mode, mem);
617 if (altclass == NO_REGS)
618 return 0;
620 if (in)
621 partial_cost = register_move_cost (mode, altclass, rclass);
622 else
623 partial_cost = register_move_cost (mode, rclass, altclass);
625 if (rclass == altclass)
626 /* This isn't simply a copy-to-temporary situation. Can't guess
627 what it is, so TARGET_MEMORY_MOVE_COST really ought not to be
628 calling here in that case.
630 I'm tempted to put in an assert here, but returning this will
631 probably only give poor estimates, which is what we would've
632 had before this code anyways. */
633 return partial_cost;
635 /* Check if the secondary reload register will also need a
636 secondary reload. */
637 return memory_move_secondary_cost (mode, altclass, in) + partial_cost;
640 /* Return a machine mode that is legitimate for hard reg REGNO and large
641 enough to save nregs. If we can't find one, return VOIDmode.
642 If CALL_SAVED is true, only consider modes that are call saved. */
643 machine_mode
644 choose_hard_reg_mode (unsigned int regno ATTRIBUTE_UNUSED,
645 unsigned int nregs, bool call_saved)
647 unsigned int /* machine_mode */ m;
648 machine_mode found_mode = VOIDmode, mode;
650 /* We first look for the largest integer mode that can be validly
651 held in REGNO. If none, we look for the largest floating-point mode.
652 If we still didn't find a valid mode, try CCmode. */
654 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
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_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_FLOAT);
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 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
682 mode != VOIDmode;
683 mode = GET_MODE_WIDER_MODE (mode))
684 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
685 && HARD_REGNO_MODE_OK (regno, mode)
686 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
687 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (found_mode))
688 found_mode = mode;
690 if (found_mode != VOIDmode)
691 return found_mode;
693 /* Iterate over all of the CCmodes. */
694 for (m = (unsigned int) CCmode; m < (unsigned int) NUM_MACHINE_MODES; ++m)
696 mode = (machine_mode) m;
697 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
698 && HARD_REGNO_MODE_OK (regno, mode)
699 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
700 return mode;
703 /* We can't find a mode valid for this register. */
704 return VOIDmode;
707 /* Specify the usage characteristics of the register named NAME.
708 It should be a fixed register if FIXED and a
709 call-used register if CALL_USED. */
710 void
711 fix_register (const char *name, int fixed, int call_used)
713 int i;
714 int reg, nregs;
716 /* Decode the name and update the primary form of
717 the register info. */
719 if ((reg = decode_reg_name_and_count (name, &nregs)) >= 0)
721 gcc_assert (nregs >= 1);
722 for (i = reg; i < reg + nregs; i++)
724 if ((i == STACK_POINTER_REGNUM
725 #ifdef HARD_FRAME_POINTER_REGNUM
726 || i == HARD_FRAME_POINTER_REGNUM
727 #else
728 || i == FRAME_POINTER_REGNUM
729 #endif
731 && (fixed == 0 || call_used == 0))
733 switch (fixed)
735 case 0:
736 switch (call_used)
738 case 0:
739 error ("can%'t use %qs as a call-saved register", name);
740 break;
742 case 1:
743 error ("can%'t use %qs as a call-used register", name);
744 break;
746 default:
747 gcc_unreachable ();
749 break;
751 case 1:
752 switch (call_used)
754 case 1:
755 error ("can%'t use %qs as a fixed register", name);
756 break;
758 case 0:
759 default:
760 gcc_unreachable ();
762 break;
764 default:
765 gcc_unreachable ();
768 else
770 fixed_regs[i] = fixed;
771 call_used_regs[i] = call_used;
772 #ifdef CALL_REALLY_USED_REGISTERS
773 if (fixed == 0)
774 call_really_used_regs[i] = call_used;
775 #endif
779 else
781 warning (0, "unknown register name: %s", name);
785 /* Mark register number I as global. */
786 void
787 globalize_reg (tree decl, int i)
789 location_t loc = DECL_SOURCE_LOCATION (decl);
791 #ifdef STACK_REGS
792 if (IN_RANGE (i, FIRST_STACK_REG, LAST_STACK_REG))
794 error ("stack register used for global register variable");
795 return;
797 #endif
799 if (fixed_regs[i] == 0 && no_global_reg_vars)
800 error_at (loc, "global register variable follows a function definition");
802 if (global_regs[i])
804 warning_at (loc, 0,
805 "register of %qD used for multiple global register variables",
806 decl);
807 inform (DECL_SOURCE_LOCATION (global_regs_decl[i]),
808 "conflicts with %qD", global_regs_decl[i]);
809 return;
812 if (call_used_regs[i] && ! fixed_regs[i])
813 warning_at (loc, 0, "call-clobbered register used for global register variable");
815 global_regs[i] = 1;
816 global_regs_decl[i] = decl;
818 /* If we're globalizing the frame pointer, we need to set the
819 appropriate regs_invalidated_by_call bit, even if it's already
820 set in fixed_regs. */
821 if (i != STACK_POINTER_REGNUM)
823 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
824 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
827 /* If already fixed, nothing else to do. */
828 if (fixed_regs[i])
829 return;
831 fixed_regs[i] = call_used_regs[i] = 1;
832 #ifdef CALL_REALLY_USED_REGISTERS
833 call_really_used_regs[i] = 1;
834 #endif
836 SET_HARD_REG_BIT (fixed_reg_set, i);
837 SET_HARD_REG_BIT (call_used_reg_set, i);
838 SET_HARD_REG_BIT (call_fixed_reg_set, i);
840 reinit_regs ();
844 /* Structure used to record preferences of given pseudo. */
845 struct reg_pref
847 /* (enum reg_class) prefclass is the preferred class. May be
848 NO_REGS if no class is better than memory. */
849 char prefclass;
851 /* altclass is a register class that we should use for allocating
852 pseudo if no register in the preferred class is available.
853 If no register in this class is available, memory is preferred.
855 It might appear to be more general to have a bitmask of classes here,
856 but since it is recommended that there be a class corresponding to the
857 union of most major pair of classes, that generality is not required. */
858 char altclass;
860 /* allocnoclass is a register class that IRA uses for allocating
861 the pseudo. */
862 char allocnoclass;
865 /* Record preferences of each pseudo. This is available after RA is
866 run. */
867 static struct reg_pref *reg_pref;
869 /* Current size of reg_info. */
870 static int reg_info_size;
871 /* Max_reg_num still last resize_reg_info call. */
872 static int max_regno_since_last_resize;
874 /* Return the reg_class in which pseudo reg number REGNO is best allocated.
875 This function is sometimes called before the info has been computed.
876 When that happens, just return GENERAL_REGS, which is innocuous. */
877 enum reg_class
878 reg_preferred_class (int regno)
880 if (reg_pref == 0)
881 return GENERAL_REGS;
883 gcc_assert (regno < reg_info_size);
884 return (enum reg_class) reg_pref[regno].prefclass;
887 enum reg_class
888 reg_alternate_class (int regno)
890 if (reg_pref == 0)
891 return ALL_REGS;
893 gcc_assert (regno < reg_info_size);
894 return (enum reg_class) reg_pref[regno].altclass;
897 /* Return the reg_class which is used by IRA for its allocation. */
898 enum reg_class
899 reg_allocno_class (int regno)
901 if (reg_pref == 0)
902 return NO_REGS;
904 gcc_assert (regno < reg_info_size);
905 return (enum reg_class) reg_pref[regno].allocnoclass;
910 /* Allocate space for reg info and initilize it. */
911 static void
912 allocate_reg_info (void)
914 int i;
916 max_regno_since_last_resize = max_reg_num ();
917 reg_info_size = max_regno_since_last_resize * 3 / 2 + 1;
918 gcc_assert (! reg_pref && ! reg_renumber);
919 reg_renumber = XNEWVEC (short, reg_info_size);
920 reg_pref = XCNEWVEC (struct reg_pref, reg_info_size);
921 memset (reg_renumber, -1, reg_info_size * sizeof (short));
922 for (i = 0; i < reg_info_size; i++)
924 reg_pref[i].prefclass = GENERAL_REGS;
925 reg_pref[i].altclass = ALL_REGS;
926 reg_pref[i].allocnoclass = GENERAL_REGS;
931 /* Resize reg info. The new elements will be initialized. Return TRUE
932 if new pseudos were added since the last call. */
933 bool
934 resize_reg_info (void)
936 int old, i;
937 bool change_p;
939 if (reg_pref == NULL)
941 allocate_reg_info ();
942 return true;
944 change_p = max_regno_since_last_resize != max_reg_num ();
945 max_regno_since_last_resize = max_reg_num ();
946 if (reg_info_size >= max_reg_num ())
947 return change_p;
948 old = reg_info_size;
949 reg_info_size = max_reg_num () * 3 / 2 + 1;
950 gcc_assert (reg_pref && reg_renumber);
951 reg_renumber = XRESIZEVEC (short, reg_renumber, reg_info_size);
952 reg_pref = XRESIZEVEC (struct reg_pref, reg_pref, reg_info_size);
953 memset (reg_pref + old, -1,
954 (reg_info_size - old) * sizeof (struct reg_pref));
955 memset (reg_renumber + old, -1, (reg_info_size - old) * sizeof (short));
956 for (i = old; i < reg_info_size; i++)
958 reg_pref[i].prefclass = GENERAL_REGS;
959 reg_pref[i].altclass = ALL_REGS;
960 reg_pref[i].allocnoclass = GENERAL_REGS;
962 return true;
966 /* Free up the space allocated by allocate_reg_info. */
967 void
968 free_reg_info (void)
970 if (reg_pref)
972 free (reg_pref);
973 reg_pref = NULL;
976 if (reg_renumber)
978 free (reg_renumber);
979 reg_renumber = NULL;
983 /* Initialize some global data for this pass. */
984 static unsigned int
985 reginfo_init (void)
987 if (df)
988 df_compute_regs_ever_live (true);
990 /* This prevents dump_reg_info from losing if called
991 before reginfo is run. */
992 reg_pref = NULL;
993 reg_info_size = max_regno_since_last_resize = 0;
994 /* No more global register variables may be declared. */
995 no_global_reg_vars = 1;
996 return 1;
999 namespace {
1001 const pass_data pass_data_reginfo_init =
1003 RTL_PASS, /* type */
1004 "reginfo", /* name */
1005 OPTGROUP_NONE, /* optinfo_flags */
1006 TV_NONE, /* tv_id */
1007 0, /* properties_required */
1008 0, /* properties_provided */
1009 0, /* properties_destroyed */
1010 0, /* todo_flags_start */
1011 0, /* todo_flags_finish */
1014 class pass_reginfo_init : public rtl_opt_pass
1016 public:
1017 pass_reginfo_init (gcc::context *ctxt)
1018 : rtl_opt_pass (pass_data_reginfo_init, ctxt)
1021 /* opt_pass methods: */
1022 virtual unsigned int execute (function *) { return reginfo_init (); }
1024 }; // class pass_reginfo_init
1026 } // anon namespace
1028 rtl_opt_pass *
1029 make_pass_reginfo_init (gcc::context *ctxt)
1031 return new pass_reginfo_init (ctxt);
1036 /* Set up preferred, alternate, and allocno classes for REGNO as
1037 PREFCLASS, ALTCLASS, and ALLOCNOCLASS. */
1038 void
1039 setup_reg_classes (int regno,
1040 enum reg_class prefclass, enum reg_class altclass,
1041 enum reg_class allocnoclass)
1043 if (reg_pref == NULL)
1044 return;
1045 gcc_assert (reg_info_size >= max_reg_num ());
1046 reg_pref[regno].prefclass = prefclass;
1047 reg_pref[regno].altclass = altclass;
1048 reg_pref[regno].allocnoclass = allocnoclass;
1052 /* This is the `regscan' pass of the compiler, run just before cse and
1053 again just before loop. It finds the first and last use of each
1054 pseudo-register. */
1056 static void reg_scan_mark_refs (rtx, rtx_insn *);
1058 void
1059 reg_scan (rtx_insn *f, unsigned int nregs ATTRIBUTE_UNUSED)
1061 rtx_insn *insn;
1063 timevar_push (TV_REG_SCAN);
1065 for (insn = f; insn; insn = NEXT_INSN (insn))
1066 if (INSN_P (insn))
1068 reg_scan_mark_refs (PATTERN (insn), insn);
1069 if (REG_NOTES (insn))
1070 reg_scan_mark_refs (REG_NOTES (insn), insn);
1073 timevar_pop (TV_REG_SCAN);
1077 /* X is the expression to scan. INSN is the insn it appears in.
1078 NOTE_FLAG is nonzero if X is from INSN's notes rather than its body.
1079 We should only record information for REGs with numbers
1080 greater than or equal to MIN_REGNO. */
1081 static void
1082 reg_scan_mark_refs (rtx x, rtx_insn *insn)
1084 enum rtx_code code;
1085 rtx dest;
1086 rtx note;
1088 if (!x)
1089 return;
1090 code = GET_CODE (x);
1091 switch (code)
1093 case CONST:
1094 CASE_CONST_ANY:
1095 case CC0:
1096 case PC:
1097 case SYMBOL_REF:
1098 case LABEL_REF:
1099 case ADDR_VEC:
1100 case ADDR_DIFF_VEC:
1101 case REG:
1102 return;
1104 case EXPR_LIST:
1105 if (XEXP (x, 0))
1106 reg_scan_mark_refs (XEXP (x, 0), insn);
1107 if (XEXP (x, 1))
1108 reg_scan_mark_refs (XEXP (x, 1), insn);
1109 break;
1111 case INSN_LIST:
1112 case INT_LIST:
1113 if (XEXP (x, 1))
1114 reg_scan_mark_refs (XEXP (x, 1), insn);
1115 break;
1117 case CLOBBER:
1118 if (MEM_P (XEXP (x, 0)))
1119 reg_scan_mark_refs (XEXP (XEXP (x, 0), 0), insn);
1120 break;
1122 case SET:
1123 /* Count a set of the destination if it is a register. */
1124 for (dest = SET_DEST (x);
1125 GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
1126 || GET_CODE (dest) == ZERO_EXTRACT;
1127 dest = XEXP (dest, 0))
1130 /* If this is setting a pseudo from another pseudo or the sum of a
1131 pseudo and a constant integer and the other pseudo is known to be
1132 a pointer, set the destination to be a pointer as well.
1134 Likewise if it is setting the destination from an address or from a
1135 value equivalent to an address or to the sum of an address and
1136 something else.
1138 But don't do any of this if the pseudo corresponds to a user
1139 variable since it should have already been set as a pointer based
1140 on the type. */
1142 if (REG_P (SET_DEST (x))
1143 && REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER
1144 /* If the destination pseudo is set more than once, then other
1145 sets might not be to a pointer value (consider access to a
1146 union in two threads of control in the presence of global
1147 optimizations). So only set REG_POINTER on the destination
1148 pseudo if this is the only set of that pseudo. */
1149 && DF_REG_DEF_COUNT (REGNO (SET_DEST (x))) == 1
1150 && ! REG_USERVAR_P (SET_DEST (x))
1151 && ! REG_POINTER (SET_DEST (x))
1152 && ((REG_P (SET_SRC (x))
1153 && REG_POINTER (SET_SRC (x)))
1154 || ((GET_CODE (SET_SRC (x)) == PLUS
1155 || GET_CODE (SET_SRC (x)) == LO_SUM)
1156 && CONST_INT_P (XEXP (SET_SRC (x), 1))
1157 && REG_P (XEXP (SET_SRC (x), 0))
1158 && REG_POINTER (XEXP (SET_SRC (x), 0)))
1159 || GET_CODE (SET_SRC (x)) == CONST
1160 || GET_CODE (SET_SRC (x)) == SYMBOL_REF
1161 || GET_CODE (SET_SRC (x)) == LABEL_REF
1162 || (GET_CODE (SET_SRC (x)) == HIGH
1163 && (GET_CODE (XEXP (SET_SRC (x), 0)) == CONST
1164 || GET_CODE (XEXP (SET_SRC (x), 0)) == SYMBOL_REF
1165 || GET_CODE (XEXP (SET_SRC (x), 0)) == LABEL_REF))
1166 || ((GET_CODE (SET_SRC (x)) == PLUS
1167 || GET_CODE (SET_SRC (x)) == LO_SUM)
1168 && (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST
1169 || GET_CODE (XEXP (SET_SRC (x), 1)) == SYMBOL_REF
1170 || GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF))
1171 || ((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
1172 && (GET_CODE (XEXP (note, 0)) == CONST
1173 || GET_CODE (XEXP (note, 0)) == SYMBOL_REF
1174 || GET_CODE (XEXP (note, 0)) == LABEL_REF))))
1175 REG_POINTER (SET_DEST (x)) = 1;
1177 /* If this is setting a register from a register or from a simple
1178 conversion of a register, propagate REG_EXPR. */
1179 if (REG_P (dest) && !REG_ATTRS (dest))
1180 set_reg_attrs_from_value (dest, SET_SRC (x));
1182 /* ... fall through ... */
1184 default:
1186 const char *fmt = GET_RTX_FORMAT (code);
1187 int i;
1188 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1190 if (fmt[i] == 'e')
1191 reg_scan_mark_refs (XEXP (x, i), insn);
1192 else if (fmt[i] == 'E' && XVEC (x, i) != 0)
1194 int j;
1195 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1196 reg_scan_mark_refs (XVECEXP (x, i, j), insn);
1204 /* Return nonzero if C1 is a subset of C2, i.e., if every register in C1
1205 is also in C2. */
1207 reg_class_subset_p (reg_class_t c1, reg_class_t c2)
1209 return (c1 == c2
1210 || c2 == ALL_REGS
1211 || hard_reg_set_subset_p (reg_class_contents[(int) c1],
1212 reg_class_contents[(int) c2]));
1215 /* Return nonzero if there is a register that is in both C1 and C2. */
1217 reg_classes_intersect_p (reg_class_t c1, reg_class_t c2)
1219 return (c1 == c2
1220 || c1 == ALL_REGS
1221 || c2 == ALL_REGS
1222 || hard_reg_set_intersect_p (reg_class_contents[(int) c1],
1223 reg_class_contents[(int) c2]));
1227 inline hashval_t
1228 simplifiable_subregs_hasher::hash (const simplifiable_subreg *value)
1230 return value->shape.unique_id ();
1233 inline bool
1234 simplifiable_subregs_hasher::equal (const simplifiable_subreg *value,
1235 const subreg_shape *compare)
1237 return value->shape == *compare;
1240 inline simplifiable_subreg::simplifiable_subreg (const subreg_shape &shape_in)
1241 : shape (shape_in)
1243 CLEAR_HARD_REG_SET (simplifiable_regs);
1246 /* Return the set of hard registers that are able to form the subreg
1247 described by SHAPE. */
1249 const HARD_REG_SET &
1250 simplifiable_subregs (const subreg_shape &shape)
1252 if (!this_target_hard_regs->x_simplifiable_subregs)
1253 this_target_hard_regs->x_simplifiable_subregs
1254 = new hash_table <simplifiable_subregs_hasher> (30);
1255 simplifiable_subreg **slot
1256 = (this_target_hard_regs->x_simplifiable_subregs
1257 ->find_slot_with_hash (&shape, shape.unique_id (), INSERT));
1259 if (!*slot)
1261 simplifiable_subreg *info = new simplifiable_subreg (shape);
1262 for (unsigned int i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1263 if (HARD_REGNO_MODE_OK (i, shape.inner_mode)
1264 && simplify_subreg_regno (i, shape.inner_mode, shape.offset,
1265 shape.outer_mode) >= 0)
1266 SET_HARD_REG_BIT (info->simplifiable_regs, i);
1267 *slot = info;
1269 return (*slot)->simplifiable_regs;
1272 /* Passes for keeping and updating info about modes of registers
1273 inside subregisters. */
1275 static HARD_REG_SET **valid_mode_changes;
1276 static obstack valid_mode_changes_obstack;
1278 static void
1279 record_subregs_of_mode (rtx subreg)
1281 unsigned int regno;
1283 if (!REG_P (SUBREG_REG (subreg)))
1284 return;
1286 regno = REGNO (SUBREG_REG (subreg));
1287 if (regno < FIRST_PSEUDO_REGISTER)
1288 return;
1290 if (valid_mode_changes[regno])
1291 AND_HARD_REG_SET (*valid_mode_changes[regno],
1292 simplifiable_subregs (shape_of_subreg (subreg)));
1293 else
1295 valid_mode_changes[regno]
1296 = XOBNEW (&valid_mode_changes_obstack, HARD_REG_SET);
1297 COPY_HARD_REG_SET (*valid_mode_changes[regno],
1298 simplifiable_subregs (shape_of_subreg (subreg)));
1302 /* Call record_subregs_of_mode for all the subregs in X. */
1303 static void
1304 find_subregs_of_mode (rtx x)
1306 enum rtx_code code = GET_CODE (x);
1307 const char * const fmt = GET_RTX_FORMAT (code);
1308 int i;
1310 if (code == SUBREG)
1311 record_subregs_of_mode (x);
1313 /* Time for some deep diving. */
1314 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1316 if (fmt[i] == 'e')
1317 find_subregs_of_mode (XEXP (x, i));
1318 else if (fmt[i] == 'E')
1320 int j;
1321 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1322 find_subregs_of_mode (XVECEXP (x, i, j));
1327 void
1328 init_subregs_of_mode (void)
1330 basic_block bb;
1331 rtx_insn *insn;
1333 gcc_obstack_init (&valid_mode_changes_obstack);
1334 valid_mode_changes = XCNEWVEC (HARD_REG_SET *, max_reg_num ());
1336 FOR_EACH_BB_FN (bb, cfun)
1337 FOR_BB_INSNS (bb, insn)
1338 if (NONDEBUG_INSN_P (insn))
1339 find_subregs_of_mode (PATTERN (insn));
1342 const HARD_REG_SET *
1343 valid_mode_changes_for_regno (unsigned int regno)
1345 return valid_mode_changes[regno];
1348 void
1349 finish_subregs_of_mode (void)
1351 XDELETEVEC (valid_mode_changes);
1352 obstack_free (&valid_mode_changes_obstack, NULL);
1355 /* Free all data attached to the structure. This isn't a destructor because
1356 we don't want to run on exit. */
1358 void
1359 target_hard_regs::finalize ()
1361 delete x_simplifiable_subregs;