2015-09-29 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / reginfo.c
blob9e855c2c1f7caf622d96f5e740dad12438dc62fe
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 "backend.h"
32 #include "tree.h"
33 #include "rtl.h"
34 #include "df.h"
35 #include "alias.h"
36 #include "flags.h"
37 #include "insn-config.h"
38 #include "expmed.h"
39 #include "dojump.h"
40 #include "explow.h"
41 #include "calls.h"
42 #include "emit-rtl.h"
43 #include "varasm.h"
44 #include "stmt.h"
45 #include "expr.h"
46 #include "tm_p.h"
47 #include "regs.h"
48 #include "addresses.h"
49 #include "recog.h"
50 #include "reload.h"
51 #include "diagnostic-core.h"
52 #include "output.h"
53 #include "target.h"
54 #include "tree-pass.h"
55 #include "ira.h"
57 /* Maximum register number used in this function, plus one. */
59 int max_regno;
61 /* Used to cache the results of simplifiable_subregs. SHAPE is the input
62 parameter and SIMPLIFIABLE_REGS is the result. */
63 struct simplifiable_subreg
65 simplifiable_subreg (const subreg_shape &);
67 subreg_shape shape;
68 HARD_REG_SET simplifiable_regs;
71 struct target_hard_regs default_target_hard_regs;
72 struct target_regs default_target_regs;
73 #if SWITCHABLE_TARGET
74 struct target_hard_regs *this_target_hard_regs = &default_target_hard_regs;
75 struct target_regs *this_target_regs = &default_target_regs;
76 #endif
78 /* Data for initializing fixed_regs. */
79 static const char initial_fixed_regs[] = FIXED_REGISTERS;
81 /* Data for initializing call_used_regs. */
82 static const char initial_call_used_regs[] = CALL_USED_REGISTERS;
84 #ifdef CALL_REALLY_USED_REGISTERS
85 /* Data for initializing call_really_used_regs. */
86 static const char initial_call_really_used_regs[] = CALL_REALLY_USED_REGISTERS;
87 #endif
89 #ifdef CALL_REALLY_USED_REGISTERS
90 #define CALL_REALLY_USED_REGNO_P(X) call_really_used_regs[X]
91 #else
92 #define CALL_REALLY_USED_REGNO_P(X) call_used_regs[X]
93 #endif
95 /* Indexed by hard register number, contains 1 for registers
96 that are being used for global register decls.
97 These must be exempt from ordinary flow analysis
98 and are also considered fixed. */
99 char global_regs[FIRST_PSEUDO_REGISTER];
101 /* Declaration for the global register. */
102 tree global_regs_decl[FIRST_PSEUDO_REGISTER];
104 /* Same information as REGS_INVALIDATED_BY_CALL but in regset form to be used
105 in dataflow more conveniently. */
106 regset regs_invalidated_by_call_regset;
108 /* Same information as FIXED_REG_SET but in regset form. */
109 regset fixed_reg_set_regset;
111 /* The bitmap_obstack is used to hold some static variables that
112 should not be reset after each function is compiled. */
113 static bitmap_obstack persistent_obstack;
115 /* Used to initialize reg_alloc_order. */
116 #ifdef REG_ALLOC_ORDER
117 static int initial_reg_alloc_order[FIRST_PSEUDO_REGISTER] = REG_ALLOC_ORDER;
118 #endif
120 /* The same information, but as an array of unsigned ints. We copy from
121 these unsigned ints to the table above. We do this so the tm.h files
122 do not have to be aware of the wordsize for machines with <= 64 regs.
123 Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
124 #define N_REG_INTS \
125 ((FIRST_PSEUDO_REGISTER + (32 - 1)) / 32)
127 static const unsigned int_reg_class_contents[N_REG_CLASSES][N_REG_INTS]
128 = REG_CLASS_CONTENTS;
130 /* Array containing all of the register names. */
131 static const char *const initial_reg_names[] = REGISTER_NAMES;
133 /* Array containing all of the register class names. */
134 const char * reg_class_names[] = REG_CLASS_NAMES;
136 /* No more global register variables may be declared; true once
137 reginfo has been initialized. */
138 static int no_global_reg_vars = 0;
140 /* Given a register bitmap, turn on the bits in a HARD_REG_SET that
141 correspond to the hard registers, if any, set in that map. This
142 could be done far more efficiently by having all sorts of special-cases
143 with moving single words, but probably isn't worth the trouble. */
144 void
145 reg_set_to_hard_reg_set (HARD_REG_SET *to, const_bitmap from)
147 unsigned i;
148 bitmap_iterator bi;
150 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
152 if (i >= FIRST_PSEUDO_REGISTER)
153 return;
154 SET_HARD_REG_BIT (*to, i);
158 /* Function called only once per target_globals to initialize the
159 target_hard_regs structure. Once this is done, various switches
160 may override. */
161 void
162 init_reg_sets (void)
164 int i, j;
166 /* First copy the register information from the initial int form into
167 the regsets. */
169 for (i = 0; i < N_REG_CLASSES; i++)
171 CLEAR_HARD_REG_SET (reg_class_contents[i]);
173 /* Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
174 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
175 if (int_reg_class_contents[i][j / 32]
176 & ((unsigned) 1 << (j % 32)))
177 SET_HARD_REG_BIT (reg_class_contents[i], j);
180 /* Sanity check: make sure the target macros FIXED_REGISTERS and
181 CALL_USED_REGISTERS had the right number of initializers. */
182 gcc_assert (sizeof fixed_regs == sizeof initial_fixed_regs);
183 gcc_assert (sizeof call_used_regs == sizeof initial_call_used_regs);
184 #ifdef CALL_REALLY_USED_REGISTERS
185 gcc_assert (sizeof call_really_used_regs
186 == sizeof initial_call_really_used_regs);
187 #endif
188 #ifdef REG_ALLOC_ORDER
189 gcc_assert (sizeof reg_alloc_order == sizeof initial_reg_alloc_order);
190 #endif
191 gcc_assert (sizeof reg_names == sizeof initial_reg_names);
193 memcpy (fixed_regs, initial_fixed_regs, sizeof fixed_regs);
194 memcpy (call_used_regs, initial_call_used_regs, sizeof call_used_regs);
195 #ifdef CALL_REALLY_USED_REGISTERS
196 memcpy (call_really_used_regs, initial_call_really_used_regs,
197 sizeof call_really_used_regs);
198 #endif
199 #ifdef REG_ALLOC_ORDER
200 memcpy (reg_alloc_order, initial_reg_alloc_order, sizeof reg_alloc_order);
201 #endif
202 memcpy (reg_names, initial_reg_names, sizeof reg_names);
204 SET_HARD_REG_SET (accessible_reg_set);
205 SET_HARD_REG_SET (operand_reg_set);
208 /* We need to save copies of some of the register information which
209 can be munged by command-line switches so we can restore it during
210 subsequent back-end reinitialization. */
211 static char saved_fixed_regs[FIRST_PSEUDO_REGISTER];
212 static char saved_call_used_regs[FIRST_PSEUDO_REGISTER];
213 #ifdef CALL_REALLY_USED_REGISTERS
214 static char saved_call_really_used_regs[FIRST_PSEUDO_REGISTER];
215 #endif
216 static const char *saved_reg_names[FIRST_PSEUDO_REGISTER];
217 static HARD_REG_SET saved_accessible_reg_set;
218 static HARD_REG_SET saved_operand_reg_set;
220 /* Save the register information. */
221 void
222 save_register_info (void)
224 /* Sanity check: make sure the target macros FIXED_REGISTERS and
225 CALL_USED_REGISTERS had the right number of initializers. */
226 gcc_assert (sizeof fixed_regs == sizeof saved_fixed_regs);
227 gcc_assert (sizeof call_used_regs == sizeof saved_call_used_regs);
228 memcpy (saved_fixed_regs, fixed_regs, sizeof fixed_regs);
229 memcpy (saved_call_used_regs, call_used_regs, sizeof call_used_regs);
231 /* Likewise for call_really_used_regs. */
232 #ifdef CALL_REALLY_USED_REGISTERS
233 gcc_assert (sizeof call_really_used_regs
234 == sizeof saved_call_really_used_regs);
235 memcpy (saved_call_really_used_regs, call_really_used_regs,
236 sizeof call_really_used_regs);
237 #endif
239 /* And similarly for reg_names. */
240 gcc_assert (sizeof reg_names == sizeof saved_reg_names);
241 memcpy (saved_reg_names, reg_names, sizeof reg_names);
242 COPY_HARD_REG_SET (saved_accessible_reg_set, accessible_reg_set);
243 COPY_HARD_REG_SET (saved_operand_reg_set, operand_reg_set);
246 /* Restore the register information. */
247 static void
248 restore_register_info (void)
250 memcpy (fixed_regs, saved_fixed_regs, sizeof fixed_regs);
251 memcpy (call_used_regs, saved_call_used_regs, sizeof call_used_regs);
253 #ifdef CALL_REALLY_USED_REGISTERS
254 memcpy (call_really_used_regs, saved_call_really_used_regs,
255 sizeof call_really_used_regs);
256 #endif
258 memcpy (reg_names, saved_reg_names, sizeof reg_names);
259 COPY_HARD_REG_SET (accessible_reg_set, saved_accessible_reg_set);
260 COPY_HARD_REG_SET (operand_reg_set, saved_operand_reg_set);
263 /* After switches have been processed, which perhaps alter
264 `fixed_regs' and `call_used_regs', convert them to HARD_REG_SETs. */
265 static void
266 init_reg_sets_1 (void)
268 unsigned int i, j;
269 unsigned int /* machine_mode */ m;
271 restore_register_info ();
273 #ifdef REG_ALLOC_ORDER
274 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
275 inv_reg_alloc_order[reg_alloc_order[i]] = i;
276 #endif
278 /* Let the target tweak things if necessary. */
280 targetm.conditional_register_usage ();
282 /* Compute number of hard regs in each class. */
284 memset (reg_class_size, 0, sizeof reg_class_size);
285 for (i = 0; i < N_REG_CLASSES; i++)
287 bool any_nonfixed = false;
288 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
289 if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
291 reg_class_size[i]++;
292 if (!fixed_regs[j])
293 any_nonfixed = true;
295 class_only_fixed_regs[i] = !any_nonfixed;
298 /* Initialize the table of subunions.
299 reg_class_subunion[I][J] gets the largest-numbered reg-class
300 that is contained in the union of classes I and J. */
302 memset (reg_class_subunion, 0, sizeof reg_class_subunion);
303 for (i = 0; i < N_REG_CLASSES; i++)
305 for (j = 0; j < N_REG_CLASSES; j++)
307 HARD_REG_SET c;
308 int k;
310 COPY_HARD_REG_SET (c, reg_class_contents[i]);
311 IOR_HARD_REG_SET (c, reg_class_contents[j]);
312 for (k = 0; k < N_REG_CLASSES; k++)
313 if (hard_reg_set_subset_p (reg_class_contents[k], c)
314 && !hard_reg_set_subset_p (reg_class_contents[k],
315 reg_class_contents
316 [(int) reg_class_subunion[i][j]]))
317 reg_class_subunion[i][j] = (enum reg_class) k;
321 /* Initialize the table of superunions.
322 reg_class_superunion[I][J] gets the smallest-numbered reg-class
323 containing the union of classes I and J. */
325 memset (reg_class_superunion, 0, sizeof reg_class_superunion);
326 for (i = 0; i < N_REG_CLASSES; i++)
328 for (j = 0; j < N_REG_CLASSES; j++)
330 HARD_REG_SET c;
331 int k;
333 COPY_HARD_REG_SET (c, reg_class_contents[i]);
334 IOR_HARD_REG_SET (c, reg_class_contents[j]);
335 for (k = 0; k < N_REG_CLASSES; k++)
336 if (hard_reg_set_subset_p (c, reg_class_contents[k]))
337 break;
339 reg_class_superunion[i][j] = (enum reg_class) k;
343 /* Initialize the tables of subclasses and superclasses of each reg class.
344 First clear the whole table, then add the elements as they are found. */
346 for (i = 0; i < N_REG_CLASSES; i++)
348 for (j = 0; j < N_REG_CLASSES; j++)
349 reg_class_subclasses[i][j] = LIM_REG_CLASSES;
352 for (i = 0; i < N_REG_CLASSES; i++)
354 if (i == (int) NO_REGS)
355 continue;
357 for (j = i + 1; j < N_REG_CLASSES; j++)
358 if (hard_reg_set_subset_p (reg_class_contents[i],
359 reg_class_contents[j]))
361 /* Reg class I is a subclass of J.
362 Add J to the table of superclasses of I. */
363 enum reg_class *p;
365 /* Add I to the table of superclasses of J. */
366 p = &reg_class_subclasses[j][0];
367 while (*p != LIM_REG_CLASSES) p++;
368 *p = (enum reg_class) i;
372 /* Initialize "constant" tables. */
374 CLEAR_HARD_REG_SET (fixed_reg_set);
375 CLEAR_HARD_REG_SET (call_used_reg_set);
376 CLEAR_HARD_REG_SET (call_fixed_reg_set);
377 CLEAR_HARD_REG_SET (regs_invalidated_by_call);
378 if (!regs_invalidated_by_call_regset)
380 bitmap_obstack_initialize (&persistent_obstack);
381 regs_invalidated_by_call_regset = ALLOC_REG_SET (&persistent_obstack);
383 else
384 CLEAR_REG_SET (regs_invalidated_by_call_regset);
385 if (!fixed_reg_set_regset)
386 fixed_reg_set_regset = ALLOC_REG_SET (&persistent_obstack);
387 else
388 CLEAR_REG_SET (fixed_reg_set_regset);
390 AND_HARD_REG_SET (operand_reg_set, accessible_reg_set);
391 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
393 /* As a special exception, registers whose class is NO_REGS are
394 not accepted by `register_operand'. The reason for this change
395 is to allow the representation of special architecture artifacts
396 (such as a condition code register) without extending the rtl
397 definitions. Since registers of class NO_REGS cannot be used
398 as registers in any case where register classes are examined,
399 it is better to apply this exception in a target-independent way. */
400 if (REGNO_REG_CLASS (i) == NO_REGS)
401 CLEAR_HARD_REG_BIT (operand_reg_set, i);
403 /* If a register is too limited to be treated as a register operand,
404 then it should never be allocated to a pseudo. */
405 if (!TEST_HARD_REG_BIT (operand_reg_set, i))
407 fixed_regs[i] = 1;
408 call_used_regs[i] = 1;
411 /* call_used_regs must include fixed_regs. */
412 gcc_assert (!fixed_regs[i] || call_used_regs[i]);
413 #ifdef CALL_REALLY_USED_REGISTERS
414 /* call_used_regs must include call_really_used_regs. */
415 gcc_assert (!call_really_used_regs[i] || call_used_regs[i]);
416 #endif
418 if (fixed_regs[i])
420 SET_HARD_REG_BIT (fixed_reg_set, i);
421 SET_REGNO_REG_SET (fixed_reg_set_regset, i);
424 if (call_used_regs[i])
425 SET_HARD_REG_BIT (call_used_reg_set, i);
427 /* There are a couple of fixed registers that we know are safe to
428 exclude from being clobbered by calls:
430 The frame pointer is always preserved across calls. The arg
431 pointer is if it is fixed. The stack pointer usually is,
432 unless TARGET_RETURN_POPS_ARGS, in which case an explicit
433 CLOBBER will be present. If we are generating PIC code, the
434 PIC offset table register is preserved across calls, though the
435 target can override that. */
437 if (i == STACK_POINTER_REGNUM)
439 else if (global_regs[i])
441 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
442 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
444 else if (i == FRAME_POINTER_REGNUM)
446 else if (!HARD_FRAME_POINTER_IS_FRAME_POINTER
447 && i == HARD_FRAME_POINTER_REGNUM)
449 else if (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
450 && i == ARG_POINTER_REGNUM && fixed_regs[i])
452 else if (!PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
453 && i == (unsigned) PIC_OFFSET_TABLE_REGNUM && fixed_regs[i])
455 else if (CALL_REALLY_USED_REGNO_P (i))
457 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
458 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
462 COPY_HARD_REG_SET (call_fixed_reg_set, fixed_reg_set);
464 /* Preserve global registers if called more than once. */
465 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
467 if (global_regs[i])
469 fixed_regs[i] = call_used_regs[i] = 1;
470 SET_HARD_REG_BIT (fixed_reg_set, i);
471 SET_HARD_REG_BIT (call_used_reg_set, i);
472 SET_HARD_REG_BIT (call_fixed_reg_set, i);
476 memset (have_regs_of_mode, 0, sizeof (have_regs_of_mode));
477 memset (contains_reg_of_mode, 0, sizeof (contains_reg_of_mode));
478 for (m = 0; m < (unsigned int) MAX_MACHINE_MODE; m++)
480 HARD_REG_SET ok_regs;
481 CLEAR_HARD_REG_SET (ok_regs);
482 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
483 if (!fixed_regs [j] && HARD_REGNO_MODE_OK (j, (machine_mode) m))
484 SET_HARD_REG_BIT (ok_regs, j);
486 for (i = 0; i < N_REG_CLASSES; i++)
487 if ((targetm.class_max_nregs ((reg_class_t) i, (machine_mode) m)
488 <= reg_class_size[i])
489 && hard_reg_set_intersect_p (ok_regs, reg_class_contents[i]))
491 contains_reg_of_mode [i][m] = 1;
492 have_regs_of_mode [m] = 1;
497 /* Compute the table of register modes.
498 These values are used to record death information for individual registers
499 (as opposed to a multi-register mode).
500 This function might be invoked more than once, if the target has support
501 for changing register usage conventions on a per-function basis.
503 void
504 init_reg_modes_target (void)
506 int i, j;
508 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
509 for (j = 0; j < MAX_MACHINE_MODE; j++)
510 hard_regno_nregs[i][j] = HARD_REGNO_NREGS (i, (machine_mode)j);
512 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
514 reg_raw_mode[i] = choose_hard_reg_mode (i, 1, false);
516 /* If we couldn't find a valid mode, just use the previous mode
517 if it is suitable, otherwise fall back on word_mode. */
518 if (reg_raw_mode[i] == VOIDmode)
520 if (i > 0 && hard_regno_nregs[i][reg_raw_mode[i - 1]] == 1)
521 reg_raw_mode[i] = reg_raw_mode[i - 1];
522 else
523 reg_raw_mode[i] = word_mode;
528 /* Finish initializing the register sets and initialize the register modes.
529 This function might be invoked more than once, if the target has support
530 for changing register usage conventions on a per-function basis.
532 void
533 init_regs (void)
535 /* This finishes what was started by init_reg_sets, but couldn't be done
536 until after register usage was specified. */
537 init_reg_sets_1 ();
540 /* The same as previous function plus initializing IRA. */
541 void
542 reinit_regs (void)
544 init_regs ();
545 /* caller_save needs to be re-initialized. */
546 caller_save_initialized_p = false;
547 if (this_target_rtl->target_specific_initialized)
549 ira_init ();
550 recog_init ();
554 /* Initialize some fake stack-frame MEM references for use in
555 memory_move_secondary_cost. */
556 void
557 init_fake_stack_mems (void)
559 int i;
561 for (i = 0; i < MAX_MACHINE_MODE; i++)
562 top_of_stack[i] = gen_rtx_MEM ((machine_mode) i, stack_pointer_rtx);
566 /* Compute cost of moving data from a register of class FROM to one of
567 TO, using MODE. */
570 register_move_cost (machine_mode mode, reg_class_t from, reg_class_t to)
572 return targetm.register_move_cost (mode, from, to);
575 /* Compute cost of moving registers to/from memory. */
578 memory_move_cost (machine_mode mode, reg_class_t rclass, bool in)
580 return targetm.memory_move_cost (mode, rclass, in);
583 /* Compute extra cost of moving registers to/from memory due to reloads.
584 Only needed if secondary reloads are required for memory moves. */
586 memory_move_secondary_cost (machine_mode mode, reg_class_t rclass,
587 bool in)
589 reg_class_t altclass;
590 int partial_cost = 0;
591 /* We need a memory reference to feed to SECONDARY... macros. */
592 /* mem may be unused even if the SECONDARY_ macros are defined. */
593 rtx mem ATTRIBUTE_UNUSED = top_of_stack[(int) mode];
595 altclass = secondary_reload_class (in ? 1 : 0, rclass, mode, mem);
597 if (altclass == NO_REGS)
598 return 0;
600 if (in)
601 partial_cost = register_move_cost (mode, altclass, rclass);
602 else
603 partial_cost = register_move_cost (mode, rclass, altclass);
605 if (rclass == altclass)
606 /* This isn't simply a copy-to-temporary situation. Can't guess
607 what it is, so TARGET_MEMORY_MOVE_COST really ought not to be
608 calling here in that case.
610 I'm tempted to put in an assert here, but returning this will
611 probably only give poor estimates, which is what we would've
612 had before this code anyways. */
613 return partial_cost;
615 /* Check if the secondary reload register will also need a
616 secondary reload. */
617 return memory_move_secondary_cost (mode, altclass, in) + partial_cost;
620 /* Return a machine mode that is legitimate for hard reg REGNO and large
621 enough to save nregs. If we can't find one, return VOIDmode.
622 If CALL_SAVED is true, only consider modes that are call saved. */
623 machine_mode
624 choose_hard_reg_mode (unsigned int regno ATTRIBUTE_UNUSED,
625 unsigned int nregs, bool call_saved)
627 unsigned int /* machine_mode */ m;
628 machine_mode found_mode = VOIDmode, mode;
630 /* We first look for the largest integer mode that can be validly
631 held in REGNO. If none, we look for the largest floating-point mode.
632 If we still didn't find a valid mode, try CCmode. */
634 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
635 mode != VOIDmode;
636 mode = GET_MODE_WIDER_MODE (mode))
637 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
638 && HARD_REGNO_MODE_OK (regno, mode)
639 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
640 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (found_mode))
641 found_mode = mode;
643 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
644 mode != VOIDmode;
645 mode = GET_MODE_WIDER_MODE (mode))
646 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
647 && HARD_REGNO_MODE_OK (regno, mode)
648 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
649 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (found_mode))
650 found_mode = mode;
652 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
653 mode != VOIDmode;
654 mode = GET_MODE_WIDER_MODE (mode))
655 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
656 && HARD_REGNO_MODE_OK (regno, mode)
657 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
658 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (found_mode))
659 found_mode = mode;
661 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
662 mode != VOIDmode;
663 mode = GET_MODE_WIDER_MODE (mode))
664 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
665 && HARD_REGNO_MODE_OK (regno, mode)
666 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
667 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (found_mode))
668 found_mode = mode;
670 if (found_mode != VOIDmode)
671 return found_mode;
673 /* Iterate over all of the CCmodes. */
674 for (m = (unsigned int) CCmode; m < (unsigned int) NUM_MACHINE_MODES; ++m)
676 mode = (machine_mode) m;
677 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
678 && HARD_REGNO_MODE_OK (regno, mode)
679 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
680 return mode;
683 /* We can't find a mode valid for this register. */
684 return VOIDmode;
687 /* Specify the usage characteristics of the register named NAME.
688 It should be a fixed register if FIXED and a
689 call-used register if CALL_USED. */
690 void
691 fix_register (const char *name, int fixed, int call_used)
693 int i;
694 int reg, nregs;
696 /* Decode the name and update the primary form of
697 the register info. */
699 if ((reg = decode_reg_name_and_count (name, &nregs)) >= 0)
701 gcc_assert (nregs >= 1);
702 for (i = reg; i < reg + nregs; i++)
704 if ((i == STACK_POINTER_REGNUM
705 #ifdef HARD_FRAME_POINTER_REGNUM
706 || i == HARD_FRAME_POINTER_REGNUM
707 #else
708 || i == FRAME_POINTER_REGNUM
709 #endif
711 && (fixed == 0 || call_used == 0))
713 switch (fixed)
715 case 0:
716 switch (call_used)
718 case 0:
719 error ("can%'t use %qs as a call-saved register", name);
720 break;
722 case 1:
723 error ("can%'t use %qs as a call-used register", name);
724 break;
726 default:
727 gcc_unreachable ();
729 break;
731 case 1:
732 switch (call_used)
734 case 1:
735 error ("can%'t use %qs as a fixed register", name);
736 break;
738 case 0:
739 default:
740 gcc_unreachable ();
742 break;
744 default:
745 gcc_unreachable ();
748 else
750 fixed_regs[i] = fixed;
751 call_used_regs[i] = call_used;
752 #ifdef CALL_REALLY_USED_REGISTERS
753 if (fixed == 0)
754 call_really_used_regs[i] = call_used;
755 #endif
759 else
761 warning (0, "unknown register name: %s", name);
765 /* Mark register number I as global. */
766 void
767 globalize_reg (tree decl, int i)
769 location_t loc = DECL_SOURCE_LOCATION (decl);
771 #ifdef STACK_REGS
772 if (IN_RANGE (i, FIRST_STACK_REG, LAST_STACK_REG))
774 error ("stack register used for global register variable");
775 return;
777 #endif
779 if (fixed_regs[i] == 0 && no_global_reg_vars)
780 error_at (loc, "global register variable follows a function definition");
782 if (global_regs[i])
784 warning_at (loc, 0,
785 "register of %qD used for multiple global register variables",
786 decl);
787 inform (DECL_SOURCE_LOCATION (global_regs_decl[i]),
788 "conflicts with %qD", global_regs_decl[i]);
789 return;
792 if (call_used_regs[i] && ! fixed_regs[i])
793 warning_at (loc, 0, "call-clobbered register used for global register variable");
795 global_regs[i] = 1;
796 global_regs_decl[i] = decl;
798 /* If we're globalizing the frame pointer, we need to set the
799 appropriate regs_invalidated_by_call bit, even if it's already
800 set in fixed_regs. */
801 if (i != STACK_POINTER_REGNUM)
803 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
804 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
807 /* If already fixed, nothing else to do. */
808 if (fixed_regs[i])
809 return;
811 fixed_regs[i] = call_used_regs[i] = 1;
812 #ifdef CALL_REALLY_USED_REGISTERS
813 call_really_used_regs[i] = 1;
814 #endif
816 SET_HARD_REG_BIT (fixed_reg_set, i);
817 SET_HARD_REG_BIT (call_used_reg_set, i);
818 SET_HARD_REG_BIT (call_fixed_reg_set, i);
820 reinit_regs ();
824 /* Structure used to record preferences of given pseudo. */
825 struct reg_pref
827 /* (enum reg_class) prefclass is the preferred class. May be
828 NO_REGS if no class is better than memory. */
829 char prefclass;
831 /* altclass is a register class that we should use for allocating
832 pseudo if no register in the preferred class is available.
833 If no register in this class is available, memory is preferred.
835 It might appear to be more general to have a bitmask of classes here,
836 but since it is recommended that there be a class corresponding to the
837 union of most major pair of classes, that generality is not required. */
838 char altclass;
840 /* allocnoclass is a register class that IRA uses for allocating
841 the pseudo. */
842 char allocnoclass;
845 /* Record preferences of each pseudo. This is available after RA is
846 run. */
847 static struct reg_pref *reg_pref;
849 /* Current size of reg_info. */
850 static int reg_info_size;
851 /* Max_reg_num still last resize_reg_info call. */
852 static int max_regno_since_last_resize;
854 /* Return the reg_class in which pseudo reg number REGNO is best allocated.
855 This function is sometimes called before the info has been computed.
856 When that happens, just return GENERAL_REGS, which is innocuous. */
857 enum reg_class
858 reg_preferred_class (int regno)
860 if (reg_pref == 0)
861 return GENERAL_REGS;
863 gcc_assert (regno < reg_info_size);
864 return (enum reg_class) reg_pref[regno].prefclass;
867 enum reg_class
868 reg_alternate_class (int regno)
870 if (reg_pref == 0)
871 return ALL_REGS;
873 gcc_assert (regno < reg_info_size);
874 return (enum reg_class) reg_pref[regno].altclass;
877 /* Return the reg_class which is used by IRA for its allocation. */
878 enum reg_class
879 reg_allocno_class (int regno)
881 if (reg_pref == 0)
882 return NO_REGS;
884 gcc_assert (regno < reg_info_size);
885 return (enum reg_class) reg_pref[regno].allocnoclass;
890 /* Allocate space for reg info and initilize it. */
891 static void
892 allocate_reg_info (void)
894 int i;
896 max_regno_since_last_resize = max_reg_num ();
897 reg_info_size = max_regno_since_last_resize * 3 / 2 + 1;
898 gcc_assert (! reg_pref && ! reg_renumber);
899 reg_renumber = XNEWVEC (short, reg_info_size);
900 reg_pref = XCNEWVEC (struct reg_pref, reg_info_size);
901 memset (reg_renumber, -1, reg_info_size * sizeof (short));
902 for (i = 0; i < reg_info_size; i++)
904 reg_pref[i].prefclass = GENERAL_REGS;
905 reg_pref[i].altclass = ALL_REGS;
906 reg_pref[i].allocnoclass = GENERAL_REGS;
911 /* Resize reg info. The new elements will be initialized. Return TRUE
912 if new pseudos were added since the last call. */
913 bool
914 resize_reg_info (void)
916 int old, i;
917 bool change_p;
919 if (reg_pref == NULL)
921 allocate_reg_info ();
922 return true;
924 change_p = max_regno_since_last_resize != max_reg_num ();
925 max_regno_since_last_resize = max_reg_num ();
926 if (reg_info_size >= max_reg_num ())
927 return change_p;
928 old = reg_info_size;
929 reg_info_size = max_reg_num () * 3 / 2 + 1;
930 gcc_assert (reg_pref && reg_renumber);
931 reg_renumber = XRESIZEVEC (short, reg_renumber, reg_info_size);
932 reg_pref = XRESIZEVEC (struct reg_pref, reg_pref, reg_info_size);
933 memset (reg_pref + old, -1,
934 (reg_info_size - old) * sizeof (struct reg_pref));
935 memset (reg_renumber + old, -1, (reg_info_size - old) * sizeof (short));
936 for (i = old; i < reg_info_size; i++)
938 reg_pref[i].prefclass = GENERAL_REGS;
939 reg_pref[i].altclass = ALL_REGS;
940 reg_pref[i].allocnoclass = GENERAL_REGS;
942 return true;
946 /* Free up the space allocated by allocate_reg_info. */
947 void
948 free_reg_info (void)
950 if (reg_pref)
952 free (reg_pref);
953 reg_pref = NULL;
956 if (reg_renumber)
958 free (reg_renumber);
959 reg_renumber = NULL;
963 /* Initialize some global data for this pass. */
964 static unsigned int
965 reginfo_init (void)
967 if (df)
968 df_compute_regs_ever_live (true);
970 /* This prevents dump_reg_info from losing if called
971 before reginfo is run. */
972 reg_pref = NULL;
973 reg_info_size = max_regno_since_last_resize = 0;
974 /* No more global register variables may be declared. */
975 no_global_reg_vars = 1;
976 return 1;
979 namespace {
981 const pass_data pass_data_reginfo_init =
983 RTL_PASS, /* type */
984 "reginfo", /* name */
985 OPTGROUP_NONE, /* optinfo_flags */
986 TV_NONE, /* tv_id */
987 0, /* properties_required */
988 0, /* properties_provided */
989 0, /* properties_destroyed */
990 0, /* todo_flags_start */
991 0, /* todo_flags_finish */
994 class pass_reginfo_init : public rtl_opt_pass
996 public:
997 pass_reginfo_init (gcc::context *ctxt)
998 : rtl_opt_pass (pass_data_reginfo_init, ctxt)
1001 /* opt_pass methods: */
1002 virtual unsigned int execute (function *) { return reginfo_init (); }
1004 }; // class pass_reginfo_init
1006 } // anon namespace
1008 rtl_opt_pass *
1009 make_pass_reginfo_init (gcc::context *ctxt)
1011 return new pass_reginfo_init (ctxt);
1016 /* Set up preferred, alternate, and allocno classes for REGNO as
1017 PREFCLASS, ALTCLASS, and ALLOCNOCLASS. */
1018 void
1019 setup_reg_classes (int regno,
1020 enum reg_class prefclass, enum reg_class altclass,
1021 enum reg_class allocnoclass)
1023 if (reg_pref == NULL)
1024 return;
1025 gcc_assert (reg_info_size >= max_reg_num ());
1026 reg_pref[regno].prefclass = prefclass;
1027 reg_pref[regno].altclass = altclass;
1028 reg_pref[regno].allocnoclass = allocnoclass;
1032 /* This is the `regscan' pass of the compiler, run just before cse and
1033 again just before loop. It finds the first and last use of each
1034 pseudo-register. */
1036 static void reg_scan_mark_refs (rtx, rtx_insn *);
1038 void
1039 reg_scan (rtx_insn *f, unsigned int nregs ATTRIBUTE_UNUSED)
1041 rtx_insn *insn;
1043 timevar_push (TV_REG_SCAN);
1045 for (insn = f; insn; insn = NEXT_INSN (insn))
1046 if (INSN_P (insn))
1048 reg_scan_mark_refs (PATTERN (insn), insn);
1049 if (REG_NOTES (insn))
1050 reg_scan_mark_refs (REG_NOTES (insn), insn);
1053 timevar_pop (TV_REG_SCAN);
1057 /* X is the expression to scan. INSN is the insn it appears in.
1058 NOTE_FLAG is nonzero if X is from INSN's notes rather than its body.
1059 We should only record information for REGs with numbers
1060 greater than or equal to MIN_REGNO. */
1061 static void
1062 reg_scan_mark_refs (rtx x, rtx_insn *insn)
1064 enum rtx_code code;
1065 rtx dest;
1066 rtx note;
1068 if (!x)
1069 return;
1070 code = GET_CODE (x);
1071 switch (code)
1073 case CONST:
1074 CASE_CONST_ANY:
1075 case CC0:
1076 case PC:
1077 case SYMBOL_REF:
1078 case LABEL_REF:
1079 case ADDR_VEC:
1080 case ADDR_DIFF_VEC:
1081 case REG:
1082 return;
1084 case EXPR_LIST:
1085 if (XEXP (x, 0))
1086 reg_scan_mark_refs (XEXP (x, 0), insn);
1087 if (XEXP (x, 1))
1088 reg_scan_mark_refs (XEXP (x, 1), insn);
1089 break;
1091 case INSN_LIST:
1092 case INT_LIST:
1093 if (XEXP (x, 1))
1094 reg_scan_mark_refs (XEXP (x, 1), insn);
1095 break;
1097 case CLOBBER:
1098 if (MEM_P (XEXP (x, 0)))
1099 reg_scan_mark_refs (XEXP (XEXP (x, 0), 0), insn);
1100 break;
1102 case SET:
1103 /* Count a set of the destination if it is a register. */
1104 for (dest = SET_DEST (x);
1105 GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
1106 || GET_CODE (dest) == ZERO_EXTRACT;
1107 dest = XEXP (dest, 0))
1110 /* If this is setting a pseudo from another pseudo or the sum of a
1111 pseudo and a constant integer and the other pseudo is known to be
1112 a pointer, set the destination to be a pointer as well.
1114 Likewise if it is setting the destination from an address or from a
1115 value equivalent to an address or to the sum of an address and
1116 something else.
1118 But don't do any of this if the pseudo corresponds to a user
1119 variable since it should have already been set as a pointer based
1120 on the type. */
1122 if (REG_P (SET_DEST (x))
1123 && REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER
1124 /* If the destination pseudo is set more than once, then other
1125 sets might not be to a pointer value (consider access to a
1126 union in two threads of control in the presence of global
1127 optimizations). So only set REG_POINTER on the destination
1128 pseudo if this is the only set of that pseudo. */
1129 && DF_REG_DEF_COUNT (REGNO (SET_DEST (x))) == 1
1130 && ! REG_USERVAR_P (SET_DEST (x))
1131 && ! REG_POINTER (SET_DEST (x))
1132 && ((REG_P (SET_SRC (x))
1133 && REG_POINTER (SET_SRC (x)))
1134 || ((GET_CODE (SET_SRC (x)) == PLUS
1135 || GET_CODE (SET_SRC (x)) == LO_SUM)
1136 && CONST_INT_P (XEXP (SET_SRC (x), 1))
1137 && REG_P (XEXP (SET_SRC (x), 0))
1138 && REG_POINTER (XEXP (SET_SRC (x), 0)))
1139 || GET_CODE (SET_SRC (x)) == CONST
1140 || GET_CODE (SET_SRC (x)) == SYMBOL_REF
1141 || GET_CODE (SET_SRC (x)) == LABEL_REF
1142 || (GET_CODE (SET_SRC (x)) == HIGH
1143 && (GET_CODE (XEXP (SET_SRC (x), 0)) == CONST
1144 || GET_CODE (XEXP (SET_SRC (x), 0)) == SYMBOL_REF
1145 || GET_CODE (XEXP (SET_SRC (x), 0)) == LABEL_REF))
1146 || ((GET_CODE (SET_SRC (x)) == PLUS
1147 || GET_CODE (SET_SRC (x)) == LO_SUM)
1148 && (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST
1149 || GET_CODE (XEXP (SET_SRC (x), 1)) == SYMBOL_REF
1150 || GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF))
1151 || ((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
1152 && (GET_CODE (XEXP (note, 0)) == CONST
1153 || GET_CODE (XEXP (note, 0)) == SYMBOL_REF
1154 || GET_CODE (XEXP (note, 0)) == LABEL_REF))))
1155 REG_POINTER (SET_DEST (x)) = 1;
1157 /* If this is setting a register from a register or from a simple
1158 conversion of a register, propagate REG_EXPR. */
1159 if (REG_P (dest) && !REG_ATTRS (dest))
1160 set_reg_attrs_from_value (dest, SET_SRC (x));
1162 /* ... fall through ... */
1164 default:
1166 const char *fmt = GET_RTX_FORMAT (code);
1167 int i;
1168 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1170 if (fmt[i] == 'e')
1171 reg_scan_mark_refs (XEXP (x, i), insn);
1172 else if (fmt[i] == 'E' && XVEC (x, i) != 0)
1174 int j;
1175 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1176 reg_scan_mark_refs (XVECEXP (x, i, j), insn);
1184 /* Return nonzero if C1 is a subset of C2, i.e., if every register in C1
1185 is also in C2. */
1187 reg_class_subset_p (reg_class_t c1, reg_class_t c2)
1189 return (c1 == c2
1190 || c2 == ALL_REGS
1191 || hard_reg_set_subset_p (reg_class_contents[(int) c1],
1192 reg_class_contents[(int) c2]));
1195 /* Return nonzero if there is a register that is in both C1 and C2. */
1197 reg_classes_intersect_p (reg_class_t c1, reg_class_t c2)
1199 return (c1 == c2
1200 || c1 == ALL_REGS
1201 || c2 == ALL_REGS
1202 || hard_reg_set_intersect_p (reg_class_contents[(int) c1],
1203 reg_class_contents[(int) c2]));
1207 inline hashval_t
1208 simplifiable_subregs_hasher::hash (const simplifiable_subreg *value)
1210 return value->shape.unique_id ();
1213 inline bool
1214 simplifiable_subregs_hasher::equal (const simplifiable_subreg *value,
1215 const subreg_shape *compare)
1217 return value->shape == *compare;
1220 inline simplifiable_subreg::simplifiable_subreg (const subreg_shape &shape_in)
1221 : shape (shape_in)
1223 CLEAR_HARD_REG_SET (simplifiable_regs);
1226 /* Return the set of hard registers that are able to form the subreg
1227 described by SHAPE. */
1229 const HARD_REG_SET &
1230 simplifiable_subregs (const subreg_shape &shape)
1232 if (!this_target_hard_regs->x_simplifiable_subregs)
1233 this_target_hard_regs->x_simplifiable_subregs
1234 = new hash_table <simplifiable_subregs_hasher> (30);
1235 simplifiable_subreg **slot
1236 = (this_target_hard_regs->x_simplifiable_subregs
1237 ->find_slot_with_hash (&shape, shape.unique_id (), INSERT));
1239 if (!*slot)
1241 simplifiable_subreg *info = new simplifiable_subreg (shape);
1242 for (unsigned int i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1243 if (HARD_REGNO_MODE_OK (i, shape.inner_mode)
1244 && simplify_subreg_regno (i, shape.inner_mode, shape.offset,
1245 shape.outer_mode) >= 0)
1246 SET_HARD_REG_BIT (info->simplifiable_regs, i);
1247 *slot = info;
1249 return (*slot)->simplifiable_regs;
1252 /* Passes for keeping and updating info about modes of registers
1253 inside subregisters. */
1255 static HARD_REG_SET **valid_mode_changes;
1256 static obstack valid_mode_changes_obstack;
1258 static void
1259 record_subregs_of_mode (rtx subreg)
1261 unsigned int regno;
1263 if (!REG_P (SUBREG_REG (subreg)))
1264 return;
1266 regno = REGNO (SUBREG_REG (subreg));
1267 if (regno < FIRST_PSEUDO_REGISTER)
1268 return;
1270 if (valid_mode_changes[regno])
1271 AND_HARD_REG_SET (*valid_mode_changes[regno],
1272 simplifiable_subregs (shape_of_subreg (subreg)));
1273 else
1275 valid_mode_changes[regno]
1276 = XOBNEW (&valid_mode_changes_obstack, HARD_REG_SET);
1277 COPY_HARD_REG_SET (*valid_mode_changes[regno],
1278 simplifiable_subregs (shape_of_subreg (subreg)));
1282 /* Call record_subregs_of_mode for all the subregs in X. */
1283 static void
1284 find_subregs_of_mode (rtx x)
1286 enum rtx_code code = GET_CODE (x);
1287 const char * const fmt = GET_RTX_FORMAT (code);
1288 int i;
1290 if (code == SUBREG)
1291 record_subregs_of_mode (x);
1293 /* Time for some deep diving. */
1294 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1296 if (fmt[i] == 'e')
1297 find_subregs_of_mode (XEXP (x, i));
1298 else if (fmt[i] == 'E')
1300 int j;
1301 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1302 find_subregs_of_mode (XVECEXP (x, i, j));
1307 void
1308 init_subregs_of_mode (void)
1310 basic_block bb;
1311 rtx_insn *insn;
1313 gcc_obstack_init (&valid_mode_changes_obstack);
1314 valid_mode_changes = XCNEWVEC (HARD_REG_SET *, max_reg_num ());
1316 FOR_EACH_BB_FN (bb, cfun)
1317 FOR_BB_INSNS (bb, insn)
1318 if (NONDEBUG_INSN_P (insn))
1319 find_subregs_of_mode (PATTERN (insn));
1322 const HARD_REG_SET *
1323 valid_mode_changes_for_regno (unsigned int regno)
1325 return valid_mode_changes[regno];
1328 void
1329 finish_subregs_of_mode (void)
1331 XDELETEVEC (valid_mode_changes);
1332 obstack_free (&valid_mode_changes_obstack, NULL);
1335 /* Free all data attached to the structure. This isn't a destructor because
1336 we don't want to run on exit. */
1338 void
1339 target_hard_regs::finalize ()
1341 delete x_simplifiable_subregs;