2016-10-22 Thomas Koenig <tkoenig@gcc.gnu.org>
[official-gcc.git] / gcc / reginfo.c
blob31715593f1aca64c86eba73bf38321bd9faf007e
1 /* Compute different info about registers.
2 Copyright (C) 1987-2016 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 "target.h"
33 #include "rtl.h"
34 #include "tree.h"
35 #include "df.h"
36 #include "memmodel.h"
37 #include "tm_p.h"
38 #include "insn-config.h"
39 #include "regs.h"
40 #include "ira.h"
41 #include "recog.h"
42 #include "diagnostic-core.h"
43 #include "reload.h"
44 #include "output.h"
45 #include "tree-pass.h"
47 /* Maximum register number used in this function, plus one. */
49 int max_regno;
51 /* Used to cache the results of simplifiable_subregs. SHAPE is the input
52 parameter and SIMPLIFIABLE_REGS is the result. */
53 struct simplifiable_subreg
55 simplifiable_subreg (const subreg_shape &);
57 subreg_shape shape;
58 HARD_REG_SET simplifiable_regs;
61 struct target_hard_regs default_target_hard_regs;
62 struct target_regs default_target_regs;
63 #if SWITCHABLE_TARGET
64 struct target_hard_regs *this_target_hard_regs = &default_target_hard_regs;
65 struct target_regs *this_target_regs = &default_target_regs;
66 #endif
68 /* Data for initializing fixed_regs. */
69 static const char initial_fixed_regs[] = FIXED_REGISTERS;
71 /* Data for initializing call_used_regs. */
72 static const char initial_call_used_regs[] = CALL_USED_REGISTERS;
74 #ifdef CALL_REALLY_USED_REGISTERS
75 /* Data for initializing call_really_used_regs. */
76 static const char initial_call_really_used_regs[] = CALL_REALLY_USED_REGISTERS;
77 #endif
79 #ifdef CALL_REALLY_USED_REGISTERS
80 #define CALL_REALLY_USED_REGNO_P(X) call_really_used_regs[X]
81 #else
82 #define CALL_REALLY_USED_REGNO_P(X) call_used_regs[X]
83 #endif
85 /* Indexed by hard register number, contains 1 for registers
86 that are being used for global register decls.
87 These must be exempt from ordinary flow analysis
88 and are also considered fixed. */
89 char global_regs[FIRST_PSEUDO_REGISTER];
91 /* Declaration for the global register. */
92 tree global_regs_decl[FIRST_PSEUDO_REGISTER];
94 /* Same information as REGS_INVALIDATED_BY_CALL but in regset form to be used
95 in dataflow more conveniently. */
96 regset regs_invalidated_by_call_regset;
98 /* Same information as FIXED_REG_SET but in regset form. */
99 regset fixed_reg_set_regset;
101 /* The bitmap_obstack is used to hold some static variables that
102 should not be reset after each function is compiled. */
103 static bitmap_obstack persistent_obstack;
105 /* Used to initialize reg_alloc_order. */
106 #ifdef REG_ALLOC_ORDER
107 static int initial_reg_alloc_order[FIRST_PSEUDO_REGISTER] = REG_ALLOC_ORDER;
108 #endif
110 /* The same information, but as an array of unsigned ints. We copy from
111 these unsigned ints to the table above. We do this so the tm.h files
112 do not have to be aware of the wordsize for machines with <= 64 regs.
113 Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
114 #define N_REG_INTS \
115 ((FIRST_PSEUDO_REGISTER + (32 - 1)) / 32)
117 static const unsigned int_reg_class_contents[N_REG_CLASSES][N_REG_INTS]
118 = REG_CLASS_CONTENTS;
120 /* Array containing all of the register names. */
121 static const char *const initial_reg_names[] = REGISTER_NAMES;
123 /* Array containing all of the register class names. */
124 const char * reg_class_names[] = REG_CLASS_NAMES;
126 /* No more global register variables may be declared; true once
127 reginfo has been initialized. */
128 static int no_global_reg_vars = 0;
130 /* Given a register bitmap, turn on the bits in a HARD_REG_SET that
131 correspond to the hard registers, if any, set in that map. This
132 could be done far more efficiently by having all sorts of special-cases
133 with moving single words, but probably isn't worth the trouble. */
134 void
135 reg_set_to_hard_reg_set (HARD_REG_SET *to, const_bitmap from)
137 unsigned i;
138 bitmap_iterator bi;
140 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
142 if (i >= FIRST_PSEUDO_REGISTER)
143 return;
144 SET_HARD_REG_BIT (*to, i);
148 /* Function called only once per target_globals to initialize the
149 target_hard_regs structure. Once this is done, various switches
150 may override. */
151 void
152 init_reg_sets (void)
154 int i, j;
156 /* First copy the register information from the initial int form into
157 the regsets. */
159 for (i = 0; i < N_REG_CLASSES; i++)
161 CLEAR_HARD_REG_SET (reg_class_contents[i]);
163 /* Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
164 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
165 if (int_reg_class_contents[i][j / 32]
166 & ((unsigned) 1 << (j % 32)))
167 SET_HARD_REG_BIT (reg_class_contents[i], j);
170 /* Sanity check: make sure the target macros FIXED_REGISTERS and
171 CALL_USED_REGISTERS had the right number of initializers. */
172 gcc_assert (sizeof fixed_regs == sizeof initial_fixed_regs);
173 gcc_assert (sizeof call_used_regs == sizeof initial_call_used_regs);
174 #ifdef CALL_REALLY_USED_REGISTERS
175 gcc_assert (sizeof call_really_used_regs
176 == sizeof initial_call_really_used_regs);
177 #endif
178 #ifdef REG_ALLOC_ORDER
179 gcc_assert (sizeof reg_alloc_order == sizeof initial_reg_alloc_order);
180 #endif
181 gcc_assert (sizeof reg_names == sizeof initial_reg_names);
183 memcpy (fixed_regs, initial_fixed_regs, sizeof fixed_regs);
184 memcpy (call_used_regs, initial_call_used_regs, sizeof call_used_regs);
185 #ifdef CALL_REALLY_USED_REGISTERS
186 memcpy (call_really_used_regs, initial_call_really_used_regs,
187 sizeof call_really_used_regs);
188 #endif
189 #ifdef REG_ALLOC_ORDER
190 memcpy (reg_alloc_order, initial_reg_alloc_order, sizeof reg_alloc_order);
191 #endif
192 memcpy (reg_names, initial_reg_names, sizeof reg_names);
194 SET_HARD_REG_SET (accessible_reg_set);
195 SET_HARD_REG_SET (operand_reg_set);
198 /* We need to save copies of some of the register information which
199 can be munged by command-line switches so we can restore it during
200 subsequent back-end reinitialization. */
201 static char saved_fixed_regs[FIRST_PSEUDO_REGISTER];
202 static char saved_call_used_regs[FIRST_PSEUDO_REGISTER];
203 #ifdef CALL_REALLY_USED_REGISTERS
204 static char saved_call_really_used_regs[FIRST_PSEUDO_REGISTER];
205 #endif
206 static const char *saved_reg_names[FIRST_PSEUDO_REGISTER];
207 static HARD_REG_SET saved_accessible_reg_set;
208 static HARD_REG_SET saved_operand_reg_set;
210 /* Save the register information. */
211 void
212 save_register_info (void)
214 /* Sanity check: make sure the target macros FIXED_REGISTERS and
215 CALL_USED_REGISTERS had the right number of initializers. */
216 gcc_assert (sizeof fixed_regs == sizeof saved_fixed_regs);
217 gcc_assert (sizeof call_used_regs == sizeof saved_call_used_regs);
218 memcpy (saved_fixed_regs, fixed_regs, sizeof fixed_regs);
219 memcpy (saved_call_used_regs, call_used_regs, sizeof call_used_regs);
221 /* Likewise for call_really_used_regs. */
222 #ifdef CALL_REALLY_USED_REGISTERS
223 gcc_assert (sizeof call_really_used_regs
224 == sizeof saved_call_really_used_regs);
225 memcpy (saved_call_really_used_regs, call_really_used_regs,
226 sizeof call_really_used_regs);
227 #endif
229 /* And similarly for reg_names. */
230 gcc_assert (sizeof reg_names == sizeof saved_reg_names);
231 memcpy (saved_reg_names, reg_names, sizeof reg_names);
232 COPY_HARD_REG_SET (saved_accessible_reg_set, accessible_reg_set);
233 COPY_HARD_REG_SET (saved_operand_reg_set, operand_reg_set);
236 /* Restore the register information. */
237 static void
238 restore_register_info (void)
240 memcpy (fixed_regs, saved_fixed_regs, sizeof fixed_regs);
241 memcpy (call_used_regs, saved_call_used_regs, sizeof call_used_regs);
243 #ifdef CALL_REALLY_USED_REGISTERS
244 memcpy (call_really_used_regs, saved_call_really_used_regs,
245 sizeof call_really_used_regs);
246 #endif
248 memcpy (reg_names, saved_reg_names, sizeof reg_names);
249 COPY_HARD_REG_SET (accessible_reg_set, saved_accessible_reg_set);
250 COPY_HARD_REG_SET (operand_reg_set, saved_operand_reg_set);
253 /* After switches have been processed, which perhaps alter
254 `fixed_regs' and `call_used_regs', convert them to HARD_REG_SETs. */
255 static void
256 init_reg_sets_1 (void)
258 unsigned int i, j;
259 unsigned int /* machine_mode */ m;
261 restore_register_info ();
263 #ifdef REG_ALLOC_ORDER
264 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
265 inv_reg_alloc_order[reg_alloc_order[i]] = i;
266 #endif
268 /* Let the target tweak things if necessary. */
270 targetm.conditional_register_usage ();
272 /* Compute number of hard regs in each class. */
274 memset (reg_class_size, 0, sizeof reg_class_size);
275 for (i = 0; i < N_REG_CLASSES; i++)
277 bool any_nonfixed = false;
278 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
279 if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
281 reg_class_size[i]++;
282 if (!fixed_regs[j])
283 any_nonfixed = true;
285 class_only_fixed_regs[i] = !any_nonfixed;
288 /* Initialize the table of subunions.
289 reg_class_subunion[I][J] gets the largest-numbered reg-class
290 that is contained in the union of classes I and J. */
292 memset (reg_class_subunion, 0, sizeof reg_class_subunion);
293 for (i = 0; i < N_REG_CLASSES; i++)
295 for (j = 0; j < N_REG_CLASSES; j++)
297 HARD_REG_SET c;
298 int k;
300 COPY_HARD_REG_SET (c, reg_class_contents[i]);
301 IOR_HARD_REG_SET (c, reg_class_contents[j]);
302 for (k = 0; k < N_REG_CLASSES; k++)
303 if (hard_reg_set_subset_p (reg_class_contents[k], c)
304 && !hard_reg_set_subset_p (reg_class_contents[k],
305 reg_class_contents
306 [(int) reg_class_subunion[i][j]]))
307 reg_class_subunion[i][j] = (enum reg_class) k;
311 /* Initialize the table of superunions.
312 reg_class_superunion[I][J] gets the smallest-numbered reg-class
313 containing the union of classes I and J. */
315 memset (reg_class_superunion, 0, sizeof reg_class_superunion);
316 for (i = 0; i < N_REG_CLASSES; i++)
318 for (j = 0; j < N_REG_CLASSES; j++)
320 HARD_REG_SET c;
321 int k;
323 COPY_HARD_REG_SET (c, reg_class_contents[i]);
324 IOR_HARD_REG_SET (c, reg_class_contents[j]);
325 for (k = 0; k < N_REG_CLASSES; k++)
326 if (hard_reg_set_subset_p (c, reg_class_contents[k]))
327 break;
329 reg_class_superunion[i][j] = (enum reg_class) k;
333 /* Initialize the tables of subclasses and superclasses of each reg class.
334 First clear the whole table, then add the elements as they are found. */
336 for (i = 0; i < N_REG_CLASSES; i++)
338 for (j = 0; j < N_REG_CLASSES; j++)
339 reg_class_subclasses[i][j] = LIM_REG_CLASSES;
342 for (i = 0; i < N_REG_CLASSES; i++)
344 if (i == (int) NO_REGS)
345 continue;
347 for (j = i + 1; j < N_REG_CLASSES; j++)
348 if (hard_reg_set_subset_p (reg_class_contents[i],
349 reg_class_contents[j]))
351 /* Reg class I is a subclass of J.
352 Add J to the table of superclasses of I. */
353 enum reg_class *p;
355 /* Add I to the table of superclasses of J. */
356 p = &reg_class_subclasses[j][0];
357 while (*p != LIM_REG_CLASSES) p++;
358 *p = (enum reg_class) i;
362 /* Initialize "constant" tables. */
364 CLEAR_HARD_REG_SET (fixed_reg_set);
365 CLEAR_HARD_REG_SET (call_used_reg_set);
366 CLEAR_HARD_REG_SET (call_fixed_reg_set);
367 CLEAR_HARD_REG_SET (regs_invalidated_by_call);
368 if (!regs_invalidated_by_call_regset)
370 bitmap_obstack_initialize (&persistent_obstack);
371 regs_invalidated_by_call_regset = ALLOC_REG_SET (&persistent_obstack);
373 else
374 CLEAR_REG_SET (regs_invalidated_by_call_regset);
375 if (!fixed_reg_set_regset)
376 fixed_reg_set_regset = ALLOC_REG_SET (&persistent_obstack);
377 else
378 CLEAR_REG_SET (fixed_reg_set_regset);
380 AND_HARD_REG_SET (operand_reg_set, accessible_reg_set);
381 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
383 /* As a special exception, registers whose class is NO_REGS are
384 not accepted by `register_operand'. The reason for this change
385 is to allow the representation of special architecture artifacts
386 (such as a condition code register) without extending the rtl
387 definitions. Since registers of class NO_REGS cannot be used
388 as registers in any case where register classes are examined,
389 it is better to apply this exception in a target-independent way. */
390 if (REGNO_REG_CLASS (i) == NO_REGS)
391 CLEAR_HARD_REG_BIT (operand_reg_set, i);
393 /* If a register is too limited to be treated as a register operand,
394 then it should never be allocated to a pseudo. */
395 if (!TEST_HARD_REG_BIT (operand_reg_set, i))
397 fixed_regs[i] = 1;
398 call_used_regs[i] = 1;
401 /* call_used_regs must include fixed_regs. */
402 gcc_assert (!fixed_regs[i] || call_used_regs[i]);
403 #ifdef CALL_REALLY_USED_REGISTERS
404 /* call_used_regs must include call_really_used_regs. */
405 gcc_assert (!call_really_used_regs[i] || call_used_regs[i]);
406 #endif
408 if (fixed_regs[i])
410 SET_HARD_REG_BIT (fixed_reg_set, i);
411 SET_REGNO_REG_SET (fixed_reg_set_regset, i);
414 if (call_used_regs[i])
415 SET_HARD_REG_BIT (call_used_reg_set, i);
417 /* There are a couple of fixed registers that we know are safe to
418 exclude from being clobbered by calls:
420 The frame pointer is always preserved across calls. The arg
421 pointer is if it is fixed. The stack pointer usually is,
422 unless TARGET_RETURN_POPS_ARGS, in which case an explicit
423 CLOBBER will be present. If we are generating PIC code, the
424 PIC offset table register is preserved across calls, though the
425 target can override that. */
427 if (i == STACK_POINTER_REGNUM)
429 else if (global_regs[i])
431 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
432 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
434 else if (i == FRAME_POINTER_REGNUM)
436 else if (!HARD_FRAME_POINTER_IS_FRAME_POINTER
437 && i == HARD_FRAME_POINTER_REGNUM)
439 else if (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
440 && i == ARG_POINTER_REGNUM && fixed_regs[i])
442 else if (!PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
443 && i == (unsigned) PIC_OFFSET_TABLE_REGNUM && fixed_regs[i])
445 else if (CALL_REALLY_USED_REGNO_P (i))
447 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
448 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
452 COPY_HARD_REG_SET (call_fixed_reg_set, fixed_reg_set);
453 COPY_HARD_REG_SET (fixed_nonglobal_reg_set, fixed_reg_set);
455 /* Preserve global registers if called more than once. */
456 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
458 if (global_regs[i])
460 fixed_regs[i] = call_used_regs[i] = 1;
461 SET_HARD_REG_BIT (fixed_reg_set, i);
462 SET_HARD_REG_BIT (call_used_reg_set, i);
463 SET_HARD_REG_BIT (call_fixed_reg_set, i);
467 memset (have_regs_of_mode, 0, sizeof (have_regs_of_mode));
468 memset (contains_reg_of_mode, 0, sizeof (contains_reg_of_mode));
469 for (m = 0; m < (unsigned int) MAX_MACHINE_MODE; m++)
471 HARD_REG_SET ok_regs;
472 CLEAR_HARD_REG_SET (ok_regs);
473 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
474 if (!fixed_regs [j] && HARD_REGNO_MODE_OK (j, (machine_mode) m))
475 SET_HARD_REG_BIT (ok_regs, j);
477 for (i = 0; i < N_REG_CLASSES; i++)
478 if ((targetm.class_max_nregs ((reg_class_t) i, (machine_mode) m)
479 <= reg_class_size[i])
480 && hard_reg_set_intersect_p (ok_regs, reg_class_contents[i]))
482 contains_reg_of_mode [i][m] = 1;
483 have_regs_of_mode [m] = 1;
488 /* Compute the table of register modes.
489 These values are used to record death information for individual registers
490 (as opposed to a multi-register mode).
491 This function might be invoked more than once, if the target has support
492 for changing register usage conventions on a per-function basis.
494 void
495 init_reg_modes_target (void)
497 int i, j;
499 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
500 for (j = 0; j < MAX_MACHINE_MODE; j++)
501 hard_regno_nregs[i][j] = HARD_REGNO_NREGS (i, (machine_mode)j);
503 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
505 reg_raw_mode[i] = choose_hard_reg_mode (i, 1, false);
507 /* If we couldn't find a valid mode, just use the previous mode
508 if it is suitable, otherwise fall back on word_mode. */
509 if (reg_raw_mode[i] == VOIDmode)
511 if (i > 0 && hard_regno_nregs[i][reg_raw_mode[i - 1]] == 1)
512 reg_raw_mode[i] = reg_raw_mode[i - 1];
513 else
514 reg_raw_mode[i] = word_mode;
519 /* Finish initializing the register sets and initialize the register modes.
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_regs (void)
526 /* This finishes what was started by init_reg_sets, but couldn't be done
527 until after register usage was specified. */
528 init_reg_sets_1 ();
531 /* The same as previous function plus initializing IRA. */
532 void
533 reinit_regs (void)
535 init_regs ();
536 /* caller_save needs to be re-initialized. */
537 caller_save_initialized_p = false;
538 if (this_target_rtl->target_specific_initialized)
540 ira_init ();
541 recog_init ();
545 /* Initialize some fake stack-frame MEM references for use in
546 memory_move_secondary_cost. */
547 void
548 init_fake_stack_mems (void)
550 int i;
552 for (i = 0; i < MAX_MACHINE_MODE; i++)
553 top_of_stack[i] = gen_rtx_MEM ((machine_mode) i, stack_pointer_rtx);
557 /* Compute cost of moving data from a register of class FROM to one of
558 TO, using MODE. */
561 register_move_cost (machine_mode mode, reg_class_t from, reg_class_t to)
563 return targetm.register_move_cost (mode, from, to);
566 /* Compute cost of moving registers to/from memory. */
569 memory_move_cost (machine_mode mode, reg_class_t rclass, bool in)
571 return targetm.memory_move_cost (mode, rclass, in);
574 /* Compute extra cost of moving registers to/from memory due to reloads.
575 Only needed if secondary reloads are required for memory moves. */
577 memory_move_secondary_cost (machine_mode mode, reg_class_t rclass,
578 bool in)
580 reg_class_t altclass;
581 int partial_cost = 0;
582 /* We need a memory reference to feed to SECONDARY... macros. */
583 /* mem may be unused even if the SECONDARY_ macros are defined. */
584 rtx mem ATTRIBUTE_UNUSED = top_of_stack[(int) mode];
586 altclass = secondary_reload_class (in ? 1 : 0, rclass, mode, mem);
588 if (altclass == NO_REGS)
589 return 0;
591 if (in)
592 partial_cost = register_move_cost (mode, altclass, rclass);
593 else
594 partial_cost = register_move_cost (mode, rclass, altclass);
596 if (rclass == altclass)
597 /* This isn't simply a copy-to-temporary situation. Can't guess
598 what it is, so TARGET_MEMORY_MOVE_COST really ought not to be
599 calling here in that case.
601 I'm tempted to put in an assert here, but returning this will
602 probably only give poor estimates, which is what we would've
603 had before this code anyways. */
604 return partial_cost;
606 /* Check if the secondary reload register will also need a
607 secondary reload. */
608 return memory_move_secondary_cost (mode, altclass, in) + partial_cost;
611 /* Return a machine mode that is legitimate for hard reg REGNO and large
612 enough to save nregs. If we can't find one, return VOIDmode.
613 If CALL_SAVED is true, only consider modes that are call saved. */
614 machine_mode
615 choose_hard_reg_mode (unsigned int regno ATTRIBUTE_UNUSED,
616 unsigned int nregs, bool call_saved)
618 unsigned int /* machine_mode */ m;
619 machine_mode found_mode = VOIDmode, mode;
621 /* We first look for the largest integer mode that can be validly
622 held in REGNO. If none, we look for the largest floating-point mode.
623 If we still didn't find a valid mode, try CCmode. */
625 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
626 mode != VOIDmode;
627 mode = GET_MODE_WIDER_MODE (mode))
628 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
629 && HARD_REGNO_MODE_OK (regno, mode)
630 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode))
631 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (found_mode))
632 found_mode = mode;
634 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
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_VECTOR_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_INT);
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 if (found_mode != VOIDmode)
662 return found_mode;
664 /* Iterate over all of the CCmodes. */
665 for (m = (unsigned int) CCmode; m < (unsigned int) NUM_MACHINE_MODES; ++m)
667 mode = (machine_mode) m;
668 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
669 && HARD_REGNO_MODE_OK (regno, mode)
670 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
671 return mode;
674 /* We can't find a mode valid for this register. */
675 return VOIDmode;
678 /* Specify the usage characteristics of the register named NAME.
679 It should be a fixed register if FIXED and a
680 call-used register if CALL_USED. */
681 void
682 fix_register (const char *name, int fixed, int call_used)
684 int i;
685 int reg, nregs;
687 /* Decode the name and update the primary form of
688 the register info. */
690 if ((reg = decode_reg_name_and_count (name, &nregs)) >= 0)
692 gcc_assert (nregs >= 1);
693 for (i = reg; i < reg + nregs; i++)
695 if ((i == STACK_POINTER_REGNUM
696 #ifdef HARD_FRAME_POINTER_REGNUM
697 || i == HARD_FRAME_POINTER_REGNUM
698 #else
699 || i == FRAME_POINTER_REGNUM
700 #endif
702 && (fixed == 0 || call_used == 0))
704 switch (fixed)
706 case 0:
707 switch (call_used)
709 case 0:
710 error ("can%'t use %qs as a call-saved register", name);
711 break;
713 case 1:
714 error ("can%'t use %qs as a call-used register", name);
715 break;
717 default:
718 gcc_unreachable ();
720 break;
722 case 1:
723 switch (call_used)
725 case 1:
726 error ("can%'t use %qs as a fixed register", name);
727 break;
729 case 0:
730 default:
731 gcc_unreachable ();
733 break;
735 default:
736 gcc_unreachable ();
739 else
741 fixed_regs[i] = fixed;
742 call_used_regs[i] = call_used;
743 #ifdef CALL_REALLY_USED_REGISTERS
744 if (fixed == 0)
745 call_really_used_regs[i] = call_used;
746 #endif
750 else
752 warning (0, "unknown register name: %s", name);
756 /* Mark register number I as global. */
757 void
758 globalize_reg (tree decl, int i)
760 location_t loc = DECL_SOURCE_LOCATION (decl);
762 #ifdef STACK_REGS
763 if (IN_RANGE (i, FIRST_STACK_REG, LAST_STACK_REG))
765 error ("stack register used for global register variable");
766 return;
768 #endif
770 if (fixed_regs[i] == 0 && no_global_reg_vars)
771 error_at (loc, "global register variable follows a function definition");
773 if (global_regs[i])
775 warning_at (loc, 0,
776 "register of %qD used for multiple global register variables",
777 decl);
778 inform (DECL_SOURCE_LOCATION (global_regs_decl[i]),
779 "conflicts with %qD", global_regs_decl[i]);
780 return;
783 if (call_used_regs[i] && ! fixed_regs[i])
784 warning_at (loc, 0, "call-clobbered register used for global register variable");
786 global_regs[i] = 1;
787 global_regs_decl[i] = decl;
789 /* If we're globalizing the frame pointer, we need to set the
790 appropriate regs_invalidated_by_call bit, even if it's already
791 set in fixed_regs. */
792 if (i != STACK_POINTER_REGNUM)
794 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
795 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
798 /* If already fixed, nothing else to do. */
799 if (fixed_regs[i])
800 return;
802 fixed_regs[i] = call_used_regs[i] = 1;
803 #ifdef CALL_REALLY_USED_REGISTERS
804 call_really_used_regs[i] = 1;
805 #endif
807 SET_HARD_REG_BIT (fixed_reg_set, i);
808 SET_HARD_REG_BIT (call_used_reg_set, i);
809 SET_HARD_REG_BIT (call_fixed_reg_set, i);
811 reinit_regs ();
815 /* Structure used to record preferences of given pseudo. */
816 struct reg_pref
818 /* (enum reg_class) prefclass is the preferred class. May be
819 NO_REGS if no class is better than memory. */
820 char prefclass;
822 /* altclass is a register class that we should use for allocating
823 pseudo if no register in the preferred class is available.
824 If no register in this class is available, memory is preferred.
826 It might appear to be more general to have a bitmask of classes here,
827 but since it is recommended that there be a class corresponding to the
828 union of most major pair of classes, that generality is not required. */
829 char altclass;
831 /* allocnoclass is a register class that IRA uses for allocating
832 the pseudo. */
833 char allocnoclass;
836 /* Record preferences of each pseudo. This is available after RA is
837 run. */
838 static struct reg_pref *reg_pref;
840 /* Current size of reg_info. */
841 static int reg_info_size;
842 /* Max_reg_num still last resize_reg_info call. */
843 static int max_regno_since_last_resize;
845 /* Return the reg_class in which pseudo reg number REGNO is best allocated.
846 This function is sometimes called before the info has been computed.
847 When that happens, just return GENERAL_REGS, which is innocuous. */
848 enum reg_class
849 reg_preferred_class (int regno)
851 if (reg_pref == 0)
852 return GENERAL_REGS;
854 gcc_assert (regno < reg_info_size);
855 return (enum reg_class) reg_pref[regno].prefclass;
858 enum reg_class
859 reg_alternate_class (int regno)
861 if (reg_pref == 0)
862 return ALL_REGS;
864 gcc_assert (regno < reg_info_size);
865 return (enum reg_class) reg_pref[regno].altclass;
868 /* Return the reg_class which is used by IRA for its allocation. */
869 enum reg_class
870 reg_allocno_class (int regno)
872 if (reg_pref == 0)
873 return NO_REGS;
875 gcc_assert (regno < reg_info_size);
876 return (enum reg_class) reg_pref[regno].allocnoclass;
881 /* Allocate space for reg info and initilize it. */
882 static void
883 allocate_reg_info (void)
885 int i;
887 max_regno_since_last_resize = max_reg_num ();
888 reg_info_size = max_regno_since_last_resize * 3 / 2 + 1;
889 gcc_assert (! reg_pref && ! reg_renumber);
890 reg_renumber = XNEWVEC (short, reg_info_size);
891 reg_pref = XCNEWVEC (struct reg_pref, reg_info_size);
892 memset (reg_renumber, -1, reg_info_size * sizeof (short));
893 for (i = 0; i < reg_info_size; i++)
895 reg_pref[i].prefclass = GENERAL_REGS;
896 reg_pref[i].altclass = ALL_REGS;
897 reg_pref[i].allocnoclass = GENERAL_REGS;
902 /* Resize reg info. The new elements will be initialized. Return TRUE
903 if new pseudos were added since the last call. */
904 bool
905 resize_reg_info (void)
907 int old, i;
908 bool change_p;
910 if (reg_pref == NULL)
912 allocate_reg_info ();
913 return true;
915 change_p = max_regno_since_last_resize != max_reg_num ();
916 max_regno_since_last_resize = max_reg_num ();
917 if (reg_info_size >= max_reg_num ())
918 return change_p;
919 old = reg_info_size;
920 reg_info_size = max_reg_num () * 3 / 2 + 1;
921 gcc_assert (reg_pref && reg_renumber);
922 reg_renumber = XRESIZEVEC (short, reg_renumber, reg_info_size);
923 reg_pref = XRESIZEVEC (struct reg_pref, reg_pref, reg_info_size);
924 memset (reg_pref + old, -1,
925 (reg_info_size - old) * sizeof (struct reg_pref));
926 memset (reg_renumber + old, -1, (reg_info_size - old) * sizeof (short));
927 for (i = old; i < reg_info_size; i++)
929 reg_pref[i].prefclass = GENERAL_REGS;
930 reg_pref[i].altclass = ALL_REGS;
931 reg_pref[i].allocnoclass = GENERAL_REGS;
933 return true;
937 /* Free up the space allocated by allocate_reg_info. */
938 void
939 free_reg_info (void)
941 if (reg_pref)
943 free (reg_pref);
944 reg_pref = NULL;
947 if (reg_renumber)
949 free (reg_renumber);
950 reg_renumber = NULL;
954 /* Initialize some global data for this pass. */
955 static unsigned int
956 reginfo_init (void)
958 if (df)
959 df_compute_regs_ever_live (true);
961 /* This prevents dump_reg_info from losing if called
962 before reginfo is run. */
963 reg_pref = NULL;
964 reg_info_size = max_regno_since_last_resize = 0;
965 /* No more global register variables may be declared. */
966 no_global_reg_vars = 1;
967 return 1;
970 namespace {
972 const pass_data pass_data_reginfo_init =
974 RTL_PASS, /* type */
975 "reginfo", /* name */
976 OPTGROUP_NONE, /* optinfo_flags */
977 TV_NONE, /* tv_id */
978 0, /* properties_required */
979 0, /* properties_provided */
980 0, /* properties_destroyed */
981 0, /* todo_flags_start */
982 0, /* todo_flags_finish */
985 class pass_reginfo_init : public rtl_opt_pass
987 public:
988 pass_reginfo_init (gcc::context *ctxt)
989 : rtl_opt_pass (pass_data_reginfo_init, ctxt)
992 /* opt_pass methods: */
993 virtual unsigned int execute (function *) { return reginfo_init (); }
995 }; // class pass_reginfo_init
997 } // anon namespace
999 rtl_opt_pass *
1000 make_pass_reginfo_init (gcc::context *ctxt)
1002 return new pass_reginfo_init (ctxt);
1007 /* Set up preferred, alternate, and allocno classes for REGNO as
1008 PREFCLASS, ALTCLASS, and ALLOCNOCLASS. */
1009 void
1010 setup_reg_classes (int regno,
1011 enum reg_class prefclass, enum reg_class altclass,
1012 enum reg_class allocnoclass)
1014 if (reg_pref == NULL)
1015 return;
1016 gcc_assert (reg_info_size >= max_reg_num ());
1017 reg_pref[regno].prefclass = prefclass;
1018 reg_pref[regno].altclass = altclass;
1019 reg_pref[regno].allocnoclass = allocnoclass;
1023 /* This is the `regscan' pass of the compiler, run just before cse and
1024 again just before loop. It finds the first and last use of each
1025 pseudo-register. */
1027 static void reg_scan_mark_refs (rtx, rtx_insn *);
1029 void
1030 reg_scan (rtx_insn *f, unsigned int nregs ATTRIBUTE_UNUSED)
1032 rtx_insn *insn;
1034 timevar_push (TV_REG_SCAN);
1036 for (insn = f; insn; insn = NEXT_INSN (insn))
1037 if (INSN_P (insn))
1039 reg_scan_mark_refs (PATTERN (insn), insn);
1040 if (REG_NOTES (insn))
1041 reg_scan_mark_refs (REG_NOTES (insn), insn);
1044 timevar_pop (TV_REG_SCAN);
1048 /* X is the expression to scan. INSN is the insn it appears in.
1049 NOTE_FLAG is nonzero if X is from INSN's notes rather than its body.
1050 We should only record information for REGs with numbers
1051 greater than or equal to MIN_REGNO. */
1052 static void
1053 reg_scan_mark_refs (rtx x, rtx_insn *insn)
1055 enum rtx_code code;
1056 rtx dest;
1057 rtx note;
1059 if (!x)
1060 return;
1061 code = GET_CODE (x);
1062 switch (code)
1064 case CONST:
1065 CASE_CONST_ANY:
1066 case CC0:
1067 case PC:
1068 case SYMBOL_REF:
1069 case LABEL_REF:
1070 case ADDR_VEC:
1071 case ADDR_DIFF_VEC:
1072 case REG:
1073 return;
1075 case EXPR_LIST:
1076 if (XEXP (x, 0))
1077 reg_scan_mark_refs (XEXP (x, 0), insn);
1078 if (XEXP (x, 1))
1079 reg_scan_mark_refs (XEXP (x, 1), insn);
1080 break;
1082 case INSN_LIST:
1083 case INT_LIST:
1084 if (XEXP (x, 1))
1085 reg_scan_mark_refs (XEXP (x, 1), insn);
1086 break;
1088 case CLOBBER:
1089 if (MEM_P (XEXP (x, 0)))
1090 reg_scan_mark_refs (XEXP (XEXP (x, 0), 0), insn);
1091 break;
1093 case SET:
1094 /* Count a set of the destination if it is a register. */
1095 for (dest = SET_DEST (x);
1096 GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
1097 || GET_CODE (dest) == ZERO_EXTRACT;
1098 dest = XEXP (dest, 0))
1101 /* If this is setting a pseudo from another pseudo or the sum of a
1102 pseudo and a constant integer and the other pseudo is known to be
1103 a pointer, set the destination to be a pointer as well.
1105 Likewise if it is setting the destination from an address or from a
1106 value equivalent to an address or to the sum of an address and
1107 something else.
1109 But don't do any of this if the pseudo corresponds to a user
1110 variable since it should have already been set as a pointer based
1111 on the type. */
1113 if (REG_P (SET_DEST (x))
1114 && REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER
1115 /* If the destination pseudo is set more than once, then other
1116 sets might not be to a pointer value (consider access to a
1117 union in two threads of control in the presence of global
1118 optimizations). So only set REG_POINTER on the destination
1119 pseudo if this is the only set of that pseudo. */
1120 && DF_REG_DEF_COUNT (REGNO (SET_DEST (x))) == 1
1121 && ! REG_USERVAR_P (SET_DEST (x))
1122 && ! REG_POINTER (SET_DEST (x))
1123 && ((REG_P (SET_SRC (x))
1124 && REG_POINTER (SET_SRC (x)))
1125 || ((GET_CODE (SET_SRC (x)) == PLUS
1126 || GET_CODE (SET_SRC (x)) == LO_SUM)
1127 && CONST_INT_P (XEXP (SET_SRC (x), 1))
1128 && REG_P (XEXP (SET_SRC (x), 0))
1129 && REG_POINTER (XEXP (SET_SRC (x), 0)))
1130 || GET_CODE (SET_SRC (x)) == CONST
1131 || GET_CODE (SET_SRC (x)) == SYMBOL_REF
1132 || GET_CODE (SET_SRC (x)) == LABEL_REF
1133 || (GET_CODE (SET_SRC (x)) == HIGH
1134 && (GET_CODE (XEXP (SET_SRC (x), 0)) == CONST
1135 || GET_CODE (XEXP (SET_SRC (x), 0)) == SYMBOL_REF
1136 || GET_CODE (XEXP (SET_SRC (x), 0)) == LABEL_REF))
1137 || ((GET_CODE (SET_SRC (x)) == PLUS
1138 || GET_CODE (SET_SRC (x)) == LO_SUM)
1139 && (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST
1140 || GET_CODE (XEXP (SET_SRC (x), 1)) == SYMBOL_REF
1141 || GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF))
1142 || ((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
1143 && (GET_CODE (XEXP (note, 0)) == CONST
1144 || GET_CODE (XEXP (note, 0)) == SYMBOL_REF
1145 || GET_CODE (XEXP (note, 0)) == LABEL_REF))))
1146 REG_POINTER (SET_DEST (x)) = 1;
1148 /* If this is setting a register from a register or from a simple
1149 conversion of a register, propagate REG_EXPR. */
1150 if (REG_P (dest) && !REG_ATTRS (dest))
1151 set_reg_attrs_from_value (dest, SET_SRC (x));
1153 /* fall through */
1155 default:
1157 const char *fmt = GET_RTX_FORMAT (code);
1158 int i;
1159 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1161 if (fmt[i] == 'e')
1162 reg_scan_mark_refs (XEXP (x, i), insn);
1163 else if (fmt[i] == 'E' && XVEC (x, i) != 0)
1165 int j;
1166 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1167 reg_scan_mark_refs (XVECEXP (x, i, j), insn);
1175 /* Return nonzero if C1 is a subset of C2, i.e., if every register in C1
1176 is also in C2. */
1178 reg_class_subset_p (reg_class_t c1, reg_class_t c2)
1180 return (c1 == c2
1181 || c2 == ALL_REGS
1182 || hard_reg_set_subset_p (reg_class_contents[(int) c1],
1183 reg_class_contents[(int) c2]));
1186 /* Return nonzero if there is a register that is in both C1 and C2. */
1188 reg_classes_intersect_p (reg_class_t c1, reg_class_t c2)
1190 return (c1 == c2
1191 || c1 == ALL_REGS
1192 || c2 == ALL_REGS
1193 || hard_reg_set_intersect_p (reg_class_contents[(int) c1],
1194 reg_class_contents[(int) c2]));
1198 inline hashval_t
1199 simplifiable_subregs_hasher::hash (const simplifiable_subreg *value)
1201 return value->shape.unique_id ();
1204 inline bool
1205 simplifiable_subregs_hasher::equal (const simplifiable_subreg *value,
1206 const subreg_shape *compare)
1208 return value->shape == *compare;
1211 inline simplifiable_subreg::simplifiable_subreg (const subreg_shape &shape_in)
1212 : shape (shape_in)
1214 CLEAR_HARD_REG_SET (simplifiable_regs);
1217 /* Return the set of hard registers that are able to form the subreg
1218 described by SHAPE. */
1220 const HARD_REG_SET &
1221 simplifiable_subregs (const subreg_shape &shape)
1223 if (!this_target_hard_regs->x_simplifiable_subregs)
1224 this_target_hard_regs->x_simplifiable_subregs
1225 = new hash_table <simplifiable_subregs_hasher> (30);
1226 simplifiable_subreg **slot
1227 = (this_target_hard_regs->x_simplifiable_subregs
1228 ->find_slot_with_hash (&shape, shape.unique_id (), INSERT));
1230 if (!*slot)
1232 simplifiable_subreg *info = new simplifiable_subreg (shape);
1233 for (unsigned int i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1234 if (HARD_REGNO_MODE_OK (i, shape.inner_mode)
1235 && simplify_subreg_regno (i, shape.inner_mode, shape.offset,
1236 shape.outer_mode) >= 0)
1237 SET_HARD_REG_BIT (info->simplifiable_regs, i);
1238 *slot = info;
1240 return (*slot)->simplifiable_regs;
1243 /* Passes for keeping and updating info about modes of registers
1244 inside subregisters. */
1246 static HARD_REG_SET **valid_mode_changes;
1247 static obstack valid_mode_changes_obstack;
1249 /* Restrict the choice of register for SUBREG_REG (SUBREG) based
1250 on information about SUBREG.
1252 If PARTIAL_DEF, SUBREG is a partial definition of a multipart inner
1253 register and we want to ensure that the other parts of the inner
1254 register are correctly preserved. If !PARTIAL_DEF we need to
1255 ensure that SUBREG itself can be formed. */
1257 static void
1258 record_subregs_of_mode (rtx subreg, bool partial_def)
1260 unsigned int regno;
1262 if (!REG_P (SUBREG_REG (subreg)))
1263 return;
1265 regno = REGNO (SUBREG_REG (subreg));
1266 if (regno < FIRST_PSEUDO_REGISTER)
1267 return;
1269 subreg_shape shape (shape_of_subreg (subreg));
1270 if (partial_def)
1272 /* The number of independently-accessible SHAPE.outer_mode values
1273 in SHAPE.inner_mode is GET_MODE_SIZE (SHAPE.inner_mode) / SIZE.
1274 We need to check that the assignment will preserve all the other
1275 SIZE-byte chunks in the inner register besides the one that
1276 includes SUBREG.
1278 In practice it is enough to check whether an equivalent
1279 SHAPE.inner_mode value in an adjacent SIZE-byte chunk can be formed.
1280 If the underlying registers are small enough, both subregs will
1281 be valid. If the underlying registers are too large, one of the
1282 subregs will be invalid.
1284 This relies on the fact that we've already been passed
1285 SUBREG with PARTIAL_DEF set to false. */
1286 unsigned int size = MAX (REGMODE_NATURAL_SIZE (shape.inner_mode),
1287 GET_MODE_SIZE (shape.outer_mode));
1288 gcc_checking_assert (size < GET_MODE_SIZE (shape.inner_mode));
1289 if (shape.offset >= size)
1290 shape.offset -= size;
1291 else
1292 shape.offset += size;
1295 if (valid_mode_changes[regno])
1296 AND_HARD_REG_SET (*valid_mode_changes[regno],
1297 simplifiable_subregs (shape));
1298 else
1300 valid_mode_changes[regno]
1301 = XOBNEW (&valid_mode_changes_obstack, HARD_REG_SET);
1302 COPY_HARD_REG_SET (*valid_mode_changes[regno],
1303 simplifiable_subregs (shape));
1307 /* Call record_subregs_of_mode for all the subregs in X. */
1308 static void
1309 find_subregs_of_mode (rtx x)
1311 enum rtx_code code = GET_CODE (x);
1312 const char * const fmt = GET_RTX_FORMAT (code);
1313 int i;
1315 if (code == SUBREG)
1316 record_subregs_of_mode (x, false);
1318 /* Time for some deep diving. */
1319 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1321 if (fmt[i] == 'e')
1322 find_subregs_of_mode (XEXP (x, i));
1323 else if (fmt[i] == 'E')
1325 int j;
1326 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1327 find_subregs_of_mode (XVECEXP (x, i, j));
1332 void
1333 init_subregs_of_mode (void)
1335 basic_block bb;
1336 rtx_insn *insn;
1338 gcc_obstack_init (&valid_mode_changes_obstack);
1339 valid_mode_changes = XCNEWVEC (HARD_REG_SET *, max_reg_num ());
1341 FOR_EACH_BB_FN (bb, cfun)
1342 FOR_BB_INSNS (bb, insn)
1343 if (NONDEBUG_INSN_P (insn))
1345 find_subregs_of_mode (PATTERN (insn));
1346 df_ref def;
1347 FOR_EACH_INSN_DEF (def, insn)
1348 if (DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL)
1349 && df_read_modify_subreg_p (DF_REF_REG (def)))
1350 record_subregs_of_mode (DF_REF_REG (def), true);
1354 const HARD_REG_SET *
1355 valid_mode_changes_for_regno (unsigned int regno)
1357 return valid_mode_changes[regno];
1360 void
1361 finish_subregs_of_mode (void)
1363 XDELETEVEC (valid_mode_changes);
1364 obstack_free (&valid_mode_changes_obstack, NULL);
1367 /* Free all data attached to the structure. This isn't a destructor because
1368 we don't want to run on exit. */
1370 void
1371 target_hard_regs::finalize ()
1373 delete x_simplifiable_subregs;