Revise -mdisable-fpregs option and add new -msoft-mult option
[official-gcc.git] / gcc / reginfo.c
blob95547a870871203d9ccfce5cb7b3c8b4fceccbfa
1 /* Compute different info about registers.
2 Copyright (C) 1987-2021 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"
46 #include "function-abi.h"
48 /* Maximum register number used in this function, plus one. */
50 int max_regno;
52 /* Used to cache the results of simplifiable_subregs. SHAPE is the input
53 parameter and SIMPLIFIABLE_REGS is the result. */
54 class simplifiable_subreg
56 public:
57 simplifiable_subreg (const subreg_shape &);
59 subreg_shape shape;
60 HARD_REG_SET simplifiable_regs;
63 struct target_hard_regs default_target_hard_regs;
64 struct target_regs default_target_regs;
65 #if SWITCHABLE_TARGET
66 struct target_hard_regs *this_target_hard_regs = &default_target_hard_regs;
67 struct target_regs *this_target_regs = &default_target_regs;
68 #endif
70 #define call_used_regs \
71 (this_target_hard_regs->x_call_used_regs)
72 #define regs_invalidated_by_call \
73 (this_target_hard_regs->x_regs_invalidated_by_call)
75 /* Data for initializing fixed_regs. */
76 static const char initial_fixed_regs[] = FIXED_REGISTERS;
78 /* Data for initializing call_used_regs. */
79 #ifdef CALL_REALLY_USED_REGISTERS
80 #ifdef CALL_USED_REGISTERS
81 #error CALL_USED_REGISTERS and CALL_REALLY_USED_REGISTERS are both defined
82 #endif
83 static const char initial_call_used_regs[] = CALL_REALLY_USED_REGISTERS;
84 #else
85 static const char initial_call_used_regs[] = CALL_USED_REGISTERS;
86 #endif
88 /* Indexed by hard register number, contains 1 for registers
89 that are being used for global register decls.
90 These must be exempt from ordinary flow analysis
91 and are also considered fixed. */
92 char global_regs[FIRST_PSEUDO_REGISTER];
94 /* The set of global registers. */
95 HARD_REG_SET global_reg_set;
97 /* Declaration for the global register. */
98 tree global_regs_decl[FIRST_PSEUDO_REGISTER];
100 /* Used to initialize reg_alloc_order. */
101 #ifdef REG_ALLOC_ORDER
102 static int initial_reg_alloc_order[FIRST_PSEUDO_REGISTER] = REG_ALLOC_ORDER;
103 #endif
105 /* The same information, but as an array of unsigned ints. We copy from
106 these unsigned ints to the table above. We do this so the tm.h files
107 do not have to be aware of the wordsize for machines with <= 64 regs.
108 Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
109 #define N_REG_INTS \
110 ((FIRST_PSEUDO_REGISTER + (32 - 1)) / 32)
112 static const unsigned int_reg_class_contents[N_REG_CLASSES][N_REG_INTS]
113 = REG_CLASS_CONTENTS;
115 /* Array containing all of the register names. */
116 static const char *const initial_reg_names[] = REGISTER_NAMES;
118 /* Array containing all of the register class names. */
119 const char * reg_class_names[] = REG_CLASS_NAMES;
121 /* No more global register variables may be declared; true once
122 reginfo has been initialized. */
123 static int no_global_reg_vars = 0;
125 /* Given a register bitmap, turn on the bits in a HARD_REG_SET that
126 correspond to the hard registers, if any, set in that map. This
127 could be done far more efficiently by having all sorts of special-cases
128 with moving single words, but probably isn't worth the trouble. */
129 void
130 reg_set_to_hard_reg_set (HARD_REG_SET *to, const_bitmap from)
132 unsigned i;
133 bitmap_iterator bi;
135 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
137 if (i >= FIRST_PSEUDO_REGISTER)
138 return;
139 SET_HARD_REG_BIT (*to, i);
143 /* Function called only once per target_globals to initialize the
144 target_hard_regs structure. Once this is done, various switches
145 may override. */
146 void
147 init_reg_sets (void)
149 int i, j;
151 /* First copy the register information from the initial int form into
152 the regsets. */
154 for (i = 0; i < N_REG_CLASSES; i++)
156 CLEAR_HARD_REG_SET (reg_class_contents[i]);
158 /* Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
159 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
160 if (int_reg_class_contents[i][j / 32]
161 & ((unsigned) 1 << (j % 32)))
162 SET_HARD_REG_BIT (reg_class_contents[i], j);
165 /* Sanity check: make sure the target macros FIXED_REGISTERS and
166 CALL_USED_REGISTERS had the right number of initializers. */
167 gcc_assert (sizeof fixed_regs == sizeof initial_fixed_regs);
168 gcc_assert (sizeof call_used_regs == sizeof initial_call_used_regs);
169 #ifdef REG_ALLOC_ORDER
170 gcc_assert (sizeof reg_alloc_order == sizeof initial_reg_alloc_order);
171 #endif
172 gcc_assert (sizeof reg_names == sizeof initial_reg_names);
174 memcpy (fixed_regs, initial_fixed_regs, sizeof fixed_regs);
175 memcpy (call_used_regs, initial_call_used_regs, sizeof call_used_regs);
176 #ifdef REG_ALLOC_ORDER
177 memcpy (reg_alloc_order, initial_reg_alloc_order, sizeof reg_alloc_order);
178 #endif
179 memcpy (reg_names, initial_reg_names, sizeof reg_names);
181 SET_HARD_REG_SET (accessible_reg_set);
182 SET_HARD_REG_SET (operand_reg_set);
185 /* We need to save copies of some of the register information which
186 can be munged by command-line switches so we can restore it during
187 subsequent back-end reinitialization. */
188 static char saved_fixed_regs[FIRST_PSEUDO_REGISTER];
189 static char saved_call_used_regs[FIRST_PSEUDO_REGISTER];
190 static const char *saved_reg_names[FIRST_PSEUDO_REGISTER];
191 static HARD_REG_SET saved_accessible_reg_set;
192 static HARD_REG_SET saved_operand_reg_set;
194 /* Save the register information. */
195 void
196 save_register_info (void)
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 saved_fixed_regs);
201 gcc_assert (sizeof call_used_regs == sizeof saved_call_used_regs);
202 memcpy (saved_fixed_regs, fixed_regs, sizeof fixed_regs);
203 memcpy (saved_call_used_regs, call_used_regs, sizeof call_used_regs);
205 /* And similarly for reg_names. */
206 gcc_assert (sizeof reg_names == sizeof saved_reg_names);
207 memcpy (saved_reg_names, reg_names, sizeof reg_names);
208 saved_accessible_reg_set = accessible_reg_set;
209 saved_operand_reg_set = operand_reg_set;
212 /* Restore the register information. */
213 static void
214 restore_register_info (void)
216 memcpy (fixed_regs, saved_fixed_regs, sizeof fixed_regs);
217 memcpy (call_used_regs, saved_call_used_regs, sizeof call_used_regs);
219 memcpy (reg_names, saved_reg_names, sizeof reg_names);
220 accessible_reg_set = saved_accessible_reg_set;
221 operand_reg_set = saved_operand_reg_set;
224 /* After switches have been processed, which perhaps alter
225 `fixed_regs' and `call_used_regs', convert them to HARD_REG_SETs. */
226 static void
227 init_reg_sets_1 (void)
229 unsigned int i, j;
230 unsigned int /* machine_mode */ m;
232 restore_register_info ();
234 #ifdef REG_ALLOC_ORDER
235 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
236 inv_reg_alloc_order[reg_alloc_order[i]] = i;
237 #endif
239 /* Let the target tweak things if necessary. */
241 targetm.conditional_register_usage ();
243 /* Compute number of hard regs in each class. */
245 memset (reg_class_size, 0, sizeof reg_class_size);
246 for (i = 0; i < N_REG_CLASSES; i++)
248 bool any_nonfixed = false;
249 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
250 if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
252 reg_class_size[i]++;
253 if (!fixed_regs[j])
254 any_nonfixed = true;
256 class_only_fixed_regs[i] = !any_nonfixed;
259 /* Initialize the table of subunions.
260 reg_class_subunion[I][J] gets the largest-numbered reg-class
261 that is contained in the union of classes I and J. */
263 memset (reg_class_subunion, 0, sizeof reg_class_subunion);
264 for (i = 0; i < N_REG_CLASSES; i++)
266 for (j = 0; j < N_REG_CLASSES; j++)
268 HARD_REG_SET c;
269 int k;
271 c = reg_class_contents[i] | reg_class_contents[j];
272 for (k = 0; k < N_REG_CLASSES; k++)
273 if (hard_reg_set_subset_p (reg_class_contents[k], c)
274 && !hard_reg_set_subset_p (reg_class_contents[k],
275 reg_class_contents
276 [(int) reg_class_subunion[i][j]]))
277 reg_class_subunion[i][j] = (enum reg_class) k;
281 /* Initialize the table of superunions.
282 reg_class_superunion[I][J] gets the smallest-numbered reg-class
283 containing the union of classes I and J. */
285 memset (reg_class_superunion, 0, sizeof reg_class_superunion);
286 for (i = 0; i < N_REG_CLASSES; i++)
288 for (j = 0; j < N_REG_CLASSES; j++)
290 HARD_REG_SET c;
291 int k;
293 c = reg_class_contents[i] | reg_class_contents[j];
294 for (k = 0; k < N_REG_CLASSES; k++)
295 if (hard_reg_set_subset_p (c, reg_class_contents[k]))
296 break;
298 reg_class_superunion[i][j] = (enum reg_class) k;
302 /* Initialize the tables of subclasses and superclasses of each reg class.
303 First clear the whole table, then add the elements as they are found. */
305 for (i = 0; i < N_REG_CLASSES; i++)
307 for (j = 0; j < N_REG_CLASSES; j++)
308 reg_class_subclasses[i][j] = LIM_REG_CLASSES;
311 for (i = 0; i < N_REG_CLASSES; i++)
313 if (i == (int) NO_REGS)
314 continue;
316 for (j = i + 1; j < N_REG_CLASSES; j++)
317 if (hard_reg_set_subset_p (reg_class_contents[i],
318 reg_class_contents[j]))
320 /* Reg class I is a subclass of J.
321 Add J to the table of superclasses of I. */
322 enum reg_class *p;
324 /* Add I to the table of superclasses of J. */
325 p = &reg_class_subclasses[j][0];
326 while (*p != LIM_REG_CLASSES) p++;
327 *p = (enum reg_class) i;
331 /* Initialize "constant" tables. */
333 CLEAR_HARD_REG_SET (fixed_reg_set);
334 CLEAR_HARD_REG_SET (regs_invalidated_by_call);
336 operand_reg_set &= accessible_reg_set;
337 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
339 /* As a special exception, registers whose class is NO_REGS are
340 not accepted by `register_operand'. The reason for this change
341 is to allow the representation of special architecture artifacts
342 (such as a condition code register) without extending the rtl
343 definitions. Since registers of class NO_REGS cannot be used
344 as registers in any case where register classes are examined,
345 it is better to apply this exception in a target-independent way. */
346 if (REGNO_REG_CLASS (i) == NO_REGS)
347 CLEAR_HARD_REG_BIT (operand_reg_set, i);
349 /* If a register is too limited to be treated as a register operand,
350 then it should never be allocated to a pseudo. */
351 if (!TEST_HARD_REG_BIT (operand_reg_set, i))
352 fixed_regs[i] = 1;
354 if (fixed_regs[i])
355 SET_HARD_REG_BIT (fixed_reg_set, i);
357 /* There are a couple of fixed registers that we know are safe to
358 exclude from being clobbered by calls:
360 The frame pointer is always preserved across calls. The arg
361 pointer is if it is fixed. The stack pointer usually is,
362 unless TARGET_RETURN_POPS_ARGS, in which case an explicit
363 CLOBBER will be present. If we are generating PIC code, the
364 PIC offset table register is preserved across calls, though the
365 target can override that. */
367 if (i == STACK_POINTER_REGNUM)
369 else if (global_regs[i])
370 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
371 else if (i == FRAME_POINTER_REGNUM)
373 else if (!HARD_FRAME_POINTER_IS_FRAME_POINTER
374 && i == HARD_FRAME_POINTER_REGNUM)
376 else if (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
377 && i == ARG_POINTER_REGNUM && fixed_regs[i])
379 else if (!PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
380 && i == (unsigned) PIC_OFFSET_TABLE_REGNUM && fixed_regs[i])
382 else if (call_used_regs[i])
383 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
386 SET_HARD_REG_SET (savable_regs);
387 fixed_nonglobal_reg_set = fixed_reg_set;
389 /* Preserve global registers if called more than once. */
390 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
392 if (global_regs[i])
394 fixed_regs[i] = call_used_regs[i] = 1;
395 SET_HARD_REG_BIT (fixed_reg_set, i);
396 SET_HARD_REG_BIT (global_reg_set, i);
400 memset (have_regs_of_mode, 0, sizeof (have_regs_of_mode));
401 memset (contains_reg_of_mode, 0, sizeof (contains_reg_of_mode));
402 for (m = 0; m < (unsigned int) MAX_MACHINE_MODE; m++)
404 HARD_REG_SET ok_regs, ok_regs2;
405 CLEAR_HARD_REG_SET (ok_regs);
406 CLEAR_HARD_REG_SET (ok_regs2);
407 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
408 if (!TEST_HARD_REG_BIT (fixed_nonglobal_reg_set, j)
409 && targetm.hard_regno_mode_ok (j, (machine_mode) m))
411 SET_HARD_REG_BIT (ok_regs, j);
412 if (!fixed_regs[j])
413 SET_HARD_REG_BIT (ok_regs2, j);
416 for (i = 0; i < N_REG_CLASSES; i++)
417 if ((targetm.class_max_nregs ((reg_class_t) i, (machine_mode) m)
418 <= reg_class_size[i])
419 && hard_reg_set_intersect_p (ok_regs, reg_class_contents[i]))
421 contains_reg_of_mode[i][m] = 1;
422 if (hard_reg_set_intersect_p (ok_regs2, reg_class_contents[i]))
424 have_regs_of_mode[m] = 1;
425 contains_allocatable_reg_of_mode[i][m] = 1;
430 default_function_abi.initialize (0, regs_invalidated_by_call);
433 /* Compute the table of register modes.
434 These values are used to record death information for individual registers
435 (as opposed to a multi-register mode).
436 This function might be invoked more than once, if the target has support
437 for changing register usage conventions on a per-function basis.
439 void
440 init_reg_modes_target (void)
442 int i, j;
444 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
445 for (j = 0; j < MAX_MACHINE_MODE; j++)
446 this_target_regs->x_hard_regno_nregs[i][j]
447 = targetm.hard_regno_nregs (i, (machine_mode) j);
449 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
451 reg_raw_mode[i] = choose_hard_reg_mode (i, 1, NULL);
453 /* If we couldn't find a valid mode, just use the previous mode
454 if it is suitable, otherwise fall back on word_mode. */
455 if (reg_raw_mode[i] == VOIDmode)
457 if (i > 0 && hard_regno_nregs (i, reg_raw_mode[i - 1]) == 1)
458 reg_raw_mode[i] = reg_raw_mode[i - 1];
459 else
460 reg_raw_mode[i] = word_mode;
465 /* Finish initializing the register sets and initialize the register modes.
466 This function might be invoked more than once, if the target has support
467 for changing register usage conventions on a per-function basis.
469 void
470 init_regs (void)
472 /* This finishes what was started by init_reg_sets, but couldn't be done
473 until after register usage was specified. */
474 init_reg_sets_1 ();
477 /* The same as previous function plus initializing IRA. */
478 void
479 reinit_regs (void)
481 init_regs ();
482 /* caller_save needs to be re-initialized. */
483 caller_save_initialized_p = false;
484 if (this_target_rtl->target_specific_initialized)
486 ira_init ();
487 recog_init ();
491 /* Initialize some fake stack-frame MEM references for use in
492 memory_move_secondary_cost. */
493 void
494 init_fake_stack_mems (void)
496 int i;
498 for (i = 0; i < MAX_MACHINE_MODE; i++)
499 top_of_stack[i] = gen_rtx_MEM ((machine_mode) i, stack_pointer_rtx);
503 /* Compute cost of moving data from a register of class FROM to one of
504 TO, using MODE. */
507 register_move_cost (machine_mode mode, reg_class_t from, reg_class_t to)
509 return targetm.register_move_cost (mode, from, to);
512 /* Compute cost of moving registers to/from memory. */
515 memory_move_cost (machine_mode mode, reg_class_t rclass, bool in)
517 return targetm.memory_move_cost (mode, rclass, in);
520 /* Compute extra cost of moving registers to/from memory due to reloads.
521 Only needed if secondary reloads are required for memory moves. */
523 memory_move_secondary_cost (machine_mode mode, reg_class_t rclass,
524 bool in)
526 reg_class_t altclass;
527 int partial_cost = 0;
528 /* We need a memory reference to feed to SECONDARY... macros. */
529 /* mem may be unused even if the SECONDARY_ macros are defined. */
530 rtx mem ATTRIBUTE_UNUSED = top_of_stack[(int) mode];
532 altclass = secondary_reload_class (in ? 1 : 0, rclass, mode, mem);
534 if (altclass == NO_REGS)
535 return 0;
537 if (in)
538 partial_cost = register_move_cost (mode, altclass, rclass);
539 else
540 partial_cost = register_move_cost (mode, rclass, altclass);
542 if (rclass == altclass)
543 /* This isn't simply a copy-to-temporary situation. Can't guess
544 what it is, so TARGET_MEMORY_MOVE_COST really ought not to be
545 calling here in that case.
547 I'm tempted to put in an assert here, but returning this will
548 probably only give poor estimates, which is what we would've
549 had before this code anyways. */
550 return partial_cost;
552 /* Check if the secondary reload register will also need a
553 secondary reload. */
554 return memory_move_secondary_cost (mode, altclass, in) + partial_cost;
557 /* Return a machine mode that is legitimate for hard reg REGNO and large
558 enough to save nregs. If we can't find one, return VOIDmode.
559 If ABI is nonnull, only consider modes that are preserved across
560 calls that use ABI. */
561 machine_mode
562 choose_hard_reg_mode (unsigned int regno ATTRIBUTE_UNUSED,
563 unsigned int nregs, const predefined_function_abi *abi)
565 unsigned int /* machine_mode */ m;
566 machine_mode found_mode = VOIDmode, mode;
568 /* We first look for the largest integer mode that can be validly
569 held in REGNO. If none, we look for the largest floating-point mode.
570 If we still didn't find a valid mode, try CCmode.
572 The tests use maybe_gt rather than known_gt because we want (for example)
573 N V4SFs to win over plain V4SF even though N might be 1. */
574 FOR_EACH_MODE_IN_CLASS (mode, MODE_INT)
575 if (hard_regno_nregs (regno, mode) == nregs
576 && targetm.hard_regno_mode_ok (regno, mode)
577 && (!abi || !abi->clobbers_reg_p (mode, regno))
578 && maybe_gt (GET_MODE_SIZE (mode), GET_MODE_SIZE (found_mode)))
579 found_mode = mode;
581 FOR_EACH_MODE_IN_CLASS (mode, MODE_FLOAT)
582 if (hard_regno_nregs (regno, mode) == nregs
583 && targetm.hard_regno_mode_ok (regno, mode)
584 && (!abi || !abi->clobbers_reg_p (mode, regno))
585 && maybe_gt (GET_MODE_SIZE (mode), GET_MODE_SIZE (found_mode)))
586 found_mode = mode;
588 FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_FLOAT)
589 if (hard_regno_nregs (regno, mode) == nregs
590 && targetm.hard_regno_mode_ok (regno, mode)
591 && (!abi || !abi->clobbers_reg_p (mode, regno))
592 && maybe_gt (GET_MODE_SIZE (mode), GET_MODE_SIZE (found_mode)))
593 found_mode = mode;
595 FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_INT)
596 if (hard_regno_nregs (regno, mode) == nregs
597 && targetm.hard_regno_mode_ok (regno, mode)
598 && (!abi || !abi->clobbers_reg_p (mode, regno))
599 && maybe_gt (GET_MODE_SIZE (mode), GET_MODE_SIZE (found_mode)))
600 found_mode = mode;
602 if (found_mode != VOIDmode)
603 return found_mode;
605 /* Iterate over all of the CCmodes. */
606 for (m = (unsigned int) CCmode; m < (unsigned int) NUM_MACHINE_MODES; ++m)
608 mode = (machine_mode) m;
609 if (hard_regno_nregs (regno, mode) == nregs
610 && targetm.hard_regno_mode_ok (regno, mode)
611 && (!abi || !abi->clobbers_reg_p (mode, regno)))
612 return mode;
615 /* We can't find a mode valid for this register. */
616 return VOIDmode;
619 /* Specify the usage characteristics of the register named NAME.
620 It should be a fixed register if FIXED and a
621 call-used register if CALL_USED. */
622 void
623 fix_register (const char *name, int fixed, int call_used)
625 int i;
626 int reg, nregs;
628 /* Decode the name and update the primary form of
629 the register info. */
631 if ((reg = decode_reg_name_and_count (name, &nregs)) >= 0)
633 gcc_assert (nregs >= 1);
634 for (i = reg; i < reg + nregs; i++)
636 if ((i == STACK_POINTER_REGNUM
637 #ifdef HARD_FRAME_POINTER_REGNUM
638 || i == HARD_FRAME_POINTER_REGNUM
639 #else
640 || i == FRAME_POINTER_REGNUM
641 #endif
643 && (fixed == 0 || call_used == 0))
645 switch (fixed)
647 case 0:
648 switch (call_used)
650 case 0:
651 error ("cannot use %qs as a call-saved register", name);
652 break;
654 case 1:
655 error ("cannot use %qs as a call-used register", name);
656 break;
658 default:
659 gcc_unreachable ();
661 break;
663 case 1:
664 switch (call_used)
666 case 1:
667 error ("cannot use %qs as a fixed register", name);
668 break;
670 case 0:
671 default:
672 gcc_unreachable ();
674 break;
676 default:
677 gcc_unreachable ();
680 else
682 fixed_regs[i] = fixed;
683 #ifdef CALL_REALLY_USED_REGISTERS
684 if (fixed == 0)
685 call_used_regs[i] = call_used;
686 #else
687 call_used_regs[i] = call_used;
688 #endif
692 else
694 warning (0, "unknown register name: %s", name);
698 /* Mark register number I as global. */
699 void
700 globalize_reg (tree decl, int i)
702 location_t loc = DECL_SOURCE_LOCATION (decl);
704 #ifdef STACK_REGS
705 if (IN_RANGE (i, FIRST_STACK_REG, LAST_STACK_REG))
707 error ("stack register used for global register variable");
708 return;
710 #endif
712 if (fixed_regs[i] == 0 && no_global_reg_vars)
713 error_at (loc, "global register variable follows a function definition");
715 if (global_regs[i])
717 auto_diagnostic_group d;
718 warning_at (loc, 0,
719 "register of %qD used for multiple global register variables",
720 decl);
721 inform (DECL_SOURCE_LOCATION (global_regs_decl[i]),
722 "conflicts with %qD", global_regs_decl[i]);
723 return;
726 if (call_used_regs[i] && ! fixed_regs[i])
727 warning_at (loc, 0, "call-clobbered register used for global register variable");
729 global_regs[i] = 1;
730 global_regs_decl[i] = decl;
731 SET_HARD_REG_BIT (global_reg_set, i);
733 /* If we're globalizing the frame pointer, we need to set the
734 appropriate regs_invalidated_by_call bit, even if it's already
735 set in fixed_regs. */
736 if (i != STACK_POINTER_REGNUM)
738 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
739 for (unsigned int j = 0; j < NUM_ABI_IDS; ++j)
740 function_abis[j].add_full_reg_clobber (i);
743 /* If already fixed, nothing else to do. */
744 if (fixed_regs[i])
745 return;
747 fixed_regs[i] = call_used_regs[i] = 1;
749 SET_HARD_REG_BIT (fixed_reg_set, i);
751 reinit_regs ();
755 /* Structure used to record preferences of given pseudo. */
756 struct reg_pref
758 /* (enum reg_class) prefclass is the preferred class. May be
759 NO_REGS if no class is better than memory. */
760 char prefclass;
762 /* altclass is a register class that we should use for allocating
763 pseudo if no register in the preferred class is available.
764 If no register in this class is available, memory is preferred.
766 It might appear to be more general to have a bitmask of classes here,
767 but since it is recommended that there be a class corresponding to the
768 union of most major pair of classes, that generality is not required. */
769 char altclass;
771 /* allocnoclass is a register class that IRA uses for allocating
772 the pseudo. */
773 char allocnoclass;
776 /* Record preferences of each pseudo. This is available after RA is
777 run. */
778 static struct reg_pref *reg_pref;
780 /* Current size of reg_info. */
781 static int reg_info_size;
782 /* Max_reg_num still last resize_reg_info call. */
783 static int max_regno_since_last_resize;
785 /* Return the reg_class in which pseudo reg number REGNO is best allocated.
786 This function is sometimes called before the info has been computed.
787 When that happens, just return GENERAL_REGS, which is innocuous. */
788 enum reg_class
789 reg_preferred_class (int regno)
791 if (reg_pref == 0)
792 return GENERAL_REGS;
794 gcc_assert (regno < reg_info_size);
795 return (enum reg_class) reg_pref[regno].prefclass;
798 enum reg_class
799 reg_alternate_class (int regno)
801 if (reg_pref == 0)
802 return ALL_REGS;
804 gcc_assert (regno < reg_info_size);
805 return (enum reg_class) reg_pref[regno].altclass;
808 /* Return the reg_class which is used by IRA for its allocation. */
809 enum reg_class
810 reg_allocno_class (int regno)
812 if (reg_pref == 0)
813 return NO_REGS;
815 gcc_assert (regno < reg_info_size);
816 return (enum reg_class) reg_pref[regno].allocnoclass;
821 /* Allocate space for reg info and initilize it. */
822 static void
823 allocate_reg_info (void)
825 int i;
827 max_regno_since_last_resize = max_reg_num ();
828 reg_info_size = max_regno_since_last_resize * 3 / 2 + 1;
829 gcc_assert (! reg_pref && ! reg_renumber);
830 reg_renumber = XNEWVEC (short, reg_info_size);
831 reg_pref = XCNEWVEC (struct reg_pref, reg_info_size);
832 memset (reg_renumber, -1, reg_info_size * sizeof (short));
833 for (i = 0; i < reg_info_size; i++)
835 reg_pref[i].prefclass = GENERAL_REGS;
836 reg_pref[i].altclass = ALL_REGS;
837 reg_pref[i].allocnoclass = GENERAL_REGS;
842 /* Resize reg info. The new elements will be initialized. Return TRUE
843 if new pseudos were added since the last call. */
844 bool
845 resize_reg_info (void)
847 int old, i;
848 bool change_p;
850 if (reg_pref == NULL)
852 allocate_reg_info ();
853 return true;
855 change_p = max_regno_since_last_resize != max_reg_num ();
856 max_regno_since_last_resize = max_reg_num ();
857 if (reg_info_size >= max_reg_num ())
858 return change_p;
859 old = reg_info_size;
860 reg_info_size = max_reg_num () * 3 / 2 + 1;
861 gcc_assert (reg_pref && reg_renumber);
862 reg_renumber = XRESIZEVEC (short, reg_renumber, reg_info_size);
863 reg_pref = XRESIZEVEC (struct reg_pref, reg_pref, reg_info_size);
864 memset (reg_pref + old, -1,
865 (reg_info_size - old) * sizeof (struct reg_pref));
866 memset (reg_renumber + old, -1, (reg_info_size - old) * sizeof (short));
867 for (i = old; i < reg_info_size; i++)
869 reg_pref[i].prefclass = GENERAL_REGS;
870 reg_pref[i].altclass = ALL_REGS;
871 reg_pref[i].allocnoclass = GENERAL_REGS;
873 return true;
877 /* Free up the space allocated by allocate_reg_info. */
878 void
879 free_reg_info (void)
881 if (reg_pref)
883 free (reg_pref);
884 reg_pref = NULL;
887 if (reg_renumber)
889 free (reg_renumber);
890 reg_renumber = NULL;
894 /* Initialize some global data for this pass. */
895 static unsigned int
896 reginfo_init (void)
898 if (df)
899 df_compute_regs_ever_live (true);
901 /* This prevents dump_reg_info from losing if called
902 before reginfo is run. */
903 reg_pref = NULL;
904 reg_info_size = max_regno_since_last_resize = 0;
905 /* No more global register variables may be declared. */
906 no_global_reg_vars = 1;
907 return 1;
910 namespace {
912 const pass_data pass_data_reginfo_init =
914 RTL_PASS, /* type */
915 "reginfo", /* name */
916 OPTGROUP_NONE, /* optinfo_flags */
917 TV_NONE, /* tv_id */
918 0, /* properties_required */
919 0, /* properties_provided */
920 0, /* properties_destroyed */
921 0, /* todo_flags_start */
922 0, /* todo_flags_finish */
925 class pass_reginfo_init : public rtl_opt_pass
927 public:
928 pass_reginfo_init (gcc::context *ctxt)
929 : rtl_opt_pass (pass_data_reginfo_init, ctxt)
932 /* opt_pass methods: */
933 virtual unsigned int execute (function *) { return reginfo_init (); }
935 }; // class pass_reginfo_init
937 } // anon namespace
939 rtl_opt_pass *
940 make_pass_reginfo_init (gcc::context *ctxt)
942 return new pass_reginfo_init (ctxt);
947 /* Set up preferred, alternate, and allocno classes for REGNO as
948 PREFCLASS, ALTCLASS, and ALLOCNOCLASS. */
949 void
950 setup_reg_classes (int regno,
951 enum reg_class prefclass, enum reg_class altclass,
952 enum reg_class allocnoclass)
954 if (reg_pref == NULL)
955 return;
956 gcc_assert (reg_info_size >= max_reg_num ());
957 reg_pref[regno].prefclass = prefclass;
958 reg_pref[regno].altclass = altclass;
959 reg_pref[regno].allocnoclass = allocnoclass;
963 /* This is the `regscan' pass of the compiler, run just before cse and
964 again just before loop. It finds the first and last use of each
965 pseudo-register. */
967 static void reg_scan_mark_refs (rtx, rtx_insn *);
969 void
970 reg_scan (rtx_insn *f, unsigned int nregs ATTRIBUTE_UNUSED)
972 rtx_insn *insn;
974 timevar_push (TV_REG_SCAN);
976 for (insn = f; insn; insn = NEXT_INSN (insn))
977 if (INSN_P (insn))
979 reg_scan_mark_refs (PATTERN (insn), insn);
980 if (REG_NOTES (insn))
981 reg_scan_mark_refs (REG_NOTES (insn), insn);
984 timevar_pop (TV_REG_SCAN);
988 /* X is the expression to scan. INSN is the insn it appears in.
989 NOTE_FLAG is nonzero if X is from INSN's notes rather than its body.
990 We should only record information for REGs with numbers
991 greater than or equal to MIN_REGNO. */
992 static void
993 reg_scan_mark_refs (rtx x, rtx_insn *insn)
995 enum rtx_code code;
996 rtx dest;
997 rtx note;
999 if (!x)
1000 return;
1001 code = GET_CODE (x);
1002 switch (code)
1004 case CONST:
1005 CASE_CONST_ANY:
1006 case PC:
1007 case SYMBOL_REF:
1008 case LABEL_REF:
1009 case ADDR_VEC:
1010 case ADDR_DIFF_VEC:
1011 case REG:
1012 return;
1014 case EXPR_LIST:
1015 if (XEXP (x, 0))
1016 reg_scan_mark_refs (XEXP (x, 0), insn);
1017 if (XEXP (x, 1))
1018 reg_scan_mark_refs (XEXP (x, 1), insn);
1019 break;
1021 case INSN_LIST:
1022 case INT_LIST:
1023 if (XEXP (x, 1))
1024 reg_scan_mark_refs (XEXP (x, 1), insn);
1025 break;
1027 case CLOBBER:
1028 if (MEM_P (XEXP (x, 0)))
1029 reg_scan_mark_refs (XEXP (XEXP (x, 0), 0), insn);
1030 break;
1032 case SET:
1033 /* Count a set of the destination if it is a register. */
1034 for (dest = SET_DEST (x);
1035 GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
1036 || GET_CODE (dest) == ZERO_EXTRACT;
1037 dest = XEXP (dest, 0))
1040 /* If this is setting a pseudo from another pseudo or the sum of a
1041 pseudo and a constant integer and the other pseudo is known to be
1042 a pointer, set the destination to be a pointer as well.
1044 Likewise if it is setting the destination from an address or from a
1045 value equivalent to an address or to the sum of an address and
1046 something else.
1048 But don't do any of this if the pseudo corresponds to a user
1049 variable since it should have already been set as a pointer based
1050 on the type. */
1052 if (REG_P (SET_DEST (x))
1053 && REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER
1054 /* If the destination pseudo is set more than once, then other
1055 sets might not be to a pointer value (consider access to a
1056 union in two threads of control in the presence of global
1057 optimizations). So only set REG_POINTER on the destination
1058 pseudo if this is the only set of that pseudo. */
1059 && DF_REG_DEF_COUNT (REGNO (SET_DEST (x))) == 1
1060 && ! REG_USERVAR_P (SET_DEST (x))
1061 && ! REG_POINTER (SET_DEST (x))
1062 && ((REG_P (SET_SRC (x))
1063 && REG_POINTER (SET_SRC (x)))
1064 || ((GET_CODE (SET_SRC (x)) == PLUS
1065 || GET_CODE (SET_SRC (x)) == LO_SUM)
1066 && CONST_INT_P (XEXP (SET_SRC (x), 1))
1067 && REG_P (XEXP (SET_SRC (x), 0))
1068 && REG_POINTER (XEXP (SET_SRC (x), 0)))
1069 || GET_CODE (SET_SRC (x)) == CONST
1070 || GET_CODE (SET_SRC (x)) == SYMBOL_REF
1071 || GET_CODE (SET_SRC (x)) == LABEL_REF
1072 || (GET_CODE (SET_SRC (x)) == HIGH
1073 && (GET_CODE (XEXP (SET_SRC (x), 0)) == CONST
1074 || GET_CODE (XEXP (SET_SRC (x), 0)) == SYMBOL_REF
1075 || GET_CODE (XEXP (SET_SRC (x), 0)) == LABEL_REF))
1076 || ((GET_CODE (SET_SRC (x)) == PLUS
1077 || GET_CODE (SET_SRC (x)) == LO_SUM)
1078 && (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST
1079 || GET_CODE (XEXP (SET_SRC (x), 1)) == SYMBOL_REF
1080 || GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF))
1081 || ((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
1082 && (GET_CODE (XEXP (note, 0)) == CONST
1083 || GET_CODE (XEXP (note, 0)) == SYMBOL_REF
1084 || GET_CODE (XEXP (note, 0)) == LABEL_REF))))
1085 REG_POINTER (SET_DEST (x)) = 1;
1087 /* If this is setting a register from a register or from a simple
1088 conversion of a register, propagate REG_EXPR. */
1089 if (REG_P (dest) && !REG_ATTRS (dest))
1090 set_reg_attrs_from_value (dest, SET_SRC (x));
1092 /* fall through */
1094 default:
1096 const char *fmt = GET_RTX_FORMAT (code);
1097 int i;
1098 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1100 if (fmt[i] == 'e')
1101 reg_scan_mark_refs (XEXP (x, i), insn);
1102 else if (fmt[i] == 'E' && XVEC (x, i) != 0)
1104 int j;
1105 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1106 reg_scan_mark_refs (XVECEXP (x, i, j), insn);
1114 /* Return nonzero if C1 is a subset of C2, i.e., if every register in C1
1115 is also in C2. */
1117 reg_class_subset_p (reg_class_t c1, reg_class_t c2)
1119 return (c1 == c2
1120 || c2 == ALL_REGS
1121 || hard_reg_set_subset_p (reg_class_contents[(int) c1],
1122 reg_class_contents[(int) c2]));
1125 /* Return nonzero if there is a register that is in both C1 and C2. */
1127 reg_classes_intersect_p (reg_class_t c1, reg_class_t c2)
1129 return (c1 == c2
1130 || c1 == ALL_REGS
1131 || c2 == ALL_REGS
1132 || hard_reg_set_intersect_p (reg_class_contents[(int) c1],
1133 reg_class_contents[(int) c2]));
1137 inline hashval_t
1138 simplifiable_subregs_hasher::hash (const simplifiable_subreg *value)
1140 inchash::hash h;
1141 h.add_hwi (value->shape.unique_id ());
1142 return h.end ();
1145 inline bool
1146 simplifiable_subregs_hasher::equal (const simplifiable_subreg *value,
1147 const subreg_shape *compare)
1149 return value->shape == *compare;
1152 inline simplifiable_subreg::simplifiable_subreg (const subreg_shape &shape_in)
1153 : shape (shape_in)
1155 CLEAR_HARD_REG_SET (simplifiable_regs);
1158 /* Return the set of hard registers that are able to form the subreg
1159 described by SHAPE. */
1161 const HARD_REG_SET &
1162 simplifiable_subregs (const subreg_shape &shape)
1164 if (!this_target_hard_regs->x_simplifiable_subregs)
1165 this_target_hard_regs->x_simplifiable_subregs
1166 = new hash_table <simplifiable_subregs_hasher> (30);
1167 inchash::hash h;
1168 h.add_hwi (shape.unique_id ());
1169 simplifiable_subreg **slot
1170 = (this_target_hard_regs->x_simplifiable_subregs
1171 ->find_slot_with_hash (&shape, h.end (), INSERT));
1173 if (!*slot)
1175 simplifiable_subreg *info = new simplifiable_subreg (shape);
1176 for (unsigned int i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1177 if (targetm.hard_regno_mode_ok (i, shape.inner_mode)
1178 && simplify_subreg_regno (i, shape.inner_mode, shape.offset,
1179 shape.outer_mode) >= 0)
1180 SET_HARD_REG_BIT (info->simplifiable_regs, i);
1181 *slot = info;
1183 return (*slot)->simplifiable_regs;
1186 /* Passes for keeping and updating info about modes of registers
1187 inside subregisters. */
1189 static HARD_REG_SET **valid_mode_changes;
1190 static obstack valid_mode_changes_obstack;
1192 /* Restrict the choice of register for SUBREG_REG (SUBREG) based
1193 on information about SUBREG.
1195 If PARTIAL_DEF, SUBREG is a partial definition of a multipart inner
1196 register and we want to ensure that the other parts of the inner
1197 register are correctly preserved. If !PARTIAL_DEF we need to
1198 ensure that SUBREG itself can be formed. */
1200 static void
1201 record_subregs_of_mode (rtx subreg, bool partial_def)
1203 unsigned int regno;
1205 if (!REG_P (SUBREG_REG (subreg)))
1206 return;
1208 regno = REGNO (SUBREG_REG (subreg));
1209 if (regno < FIRST_PSEUDO_REGISTER)
1210 return;
1212 subreg_shape shape (shape_of_subreg (subreg));
1213 if (partial_def)
1215 /* The number of independently-accessible SHAPE.outer_mode values
1216 in SHAPE.inner_mode is GET_MODE_SIZE (SHAPE.inner_mode) / SIZE.
1217 We need to check that the assignment will preserve all the other
1218 SIZE-byte chunks in the inner register besides the one that
1219 includes SUBREG.
1221 In practice it is enough to check whether an equivalent
1222 SHAPE.inner_mode value in an adjacent SIZE-byte chunk can be formed.
1223 If the underlying registers are small enough, both subregs will
1224 be valid. If the underlying registers are too large, one of the
1225 subregs will be invalid.
1227 This relies on the fact that we've already been passed
1228 SUBREG with PARTIAL_DEF set to false.
1230 The size of the outer mode must ordered wrt the size of the
1231 inner mode's registers, since otherwise we wouldn't know at
1232 compile time how many registers the outer mode occupies. */
1233 poly_uint64 size = ordered_max (REGMODE_NATURAL_SIZE (shape.inner_mode),
1234 GET_MODE_SIZE (shape.outer_mode));
1235 gcc_checking_assert (known_lt (size, GET_MODE_SIZE (shape.inner_mode)));
1236 if (known_ge (shape.offset, size))
1237 shape.offset -= size;
1238 else
1239 shape.offset += size;
1242 if (valid_mode_changes[regno])
1243 *valid_mode_changes[regno] &= simplifiable_subregs (shape);
1244 else
1246 valid_mode_changes[regno]
1247 = XOBNEW (&valid_mode_changes_obstack, HARD_REG_SET);
1248 *valid_mode_changes[regno] = simplifiable_subregs (shape);
1252 /* Call record_subregs_of_mode for all the subregs in X. */
1253 static void
1254 find_subregs_of_mode (rtx x)
1256 enum rtx_code code = GET_CODE (x);
1257 const char * const fmt = GET_RTX_FORMAT (code);
1258 int i;
1260 if (code == SUBREG)
1261 record_subregs_of_mode (x, false);
1263 /* Time for some deep diving. */
1264 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1266 if (fmt[i] == 'e')
1267 find_subregs_of_mode (XEXP (x, i));
1268 else if (fmt[i] == 'E')
1270 int j;
1271 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1272 find_subregs_of_mode (XVECEXP (x, i, j));
1277 void
1278 init_subregs_of_mode (void)
1280 basic_block bb;
1281 rtx_insn *insn;
1283 gcc_obstack_init (&valid_mode_changes_obstack);
1284 valid_mode_changes = XCNEWVEC (HARD_REG_SET *, max_reg_num ());
1286 FOR_EACH_BB_FN (bb, cfun)
1287 FOR_BB_INSNS (bb, insn)
1288 if (NONDEBUG_INSN_P (insn))
1290 find_subregs_of_mode (PATTERN (insn));
1291 df_ref def;
1292 FOR_EACH_INSN_DEF (def, insn)
1293 if (DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL)
1294 && read_modify_subreg_p (DF_REF_REG (def)))
1295 record_subregs_of_mode (DF_REF_REG (def), true);
1299 const HARD_REG_SET *
1300 valid_mode_changes_for_regno (unsigned int regno)
1302 return valid_mode_changes[regno];
1305 void
1306 finish_subregs_of_mode (void)
1308 XDELETEVEC (valid_mode_changes);
1309 obstack_free (&valid_mode_changes_obstack, NULL);
1312 /* Free all data attached to the structure. This isn't a destructor because
1313 we don't want to run on exit. */
1315 void
1316 target_hard_regs::finalize ()
1318 delete x_simplifiable_subregs;