]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/reginfo.c
Update copyright years.
[thirdparty/gcc.git] / gcc / reginfo.c
1 /* Compute different info about registers.
2 Copyright (C) 1987-2021 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
20
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. */
27
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"
47
48 /* Maximum register number used in this function, plus one. */
49
50 int max_regno;
51
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
55 {
56 public:
57 simplifiable_subreg (const subreg_shape &);
58
59 subreg_shape shape;
60 HARD_REG_SET simplifiable_regs;
61 };
62 \f
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
69
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)
74
75 /* Data for initializing fixed_regs. */
76 static const char initial_fixed_regs[] = FIXED_REGISTERS;
77
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
87
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];
93
94 /* The set of global registers. */
95 HARD_REG_SET global_reg_set;
96
97 /* Declaration for the global register. */
98 tree global_regs_decl[FIRST_PSEUDO_REGISTER];
99
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
104
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)
111
112 static const unsigned int_reg_class_contents[N_REG_CLASSES][N_REG_INTS]
113 = REG_CLASS_CONTENTS;
114
115 /* Array containing all of the register names. */
116 static const char *const initial_reg_names[] = REGISTER_NAMES;
117
118 /* Array containing all of the register class names. */
119 const char * reg_class_names[] = REG_CLASS_NAMES;
120
121 /* No more global register variables may be declared; true once
122 reginfo has been initialized. */
123 static int no_global_reg_vars = 0;
124
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)
131 {
132 unsigned i;
133 bitmap_iterator bi;
134
135 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
136 {
137 if (i >= FIRST_PSEUDO_REGISTER)
138 return;
139 SET_HARD_REG_BIT (*to, i);
140 }
141 }
142
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)
148 {
149 int i, j;
150
151 /* First copy the register information from the initial int form into
152 the regsets. */
153
154 for (i = 0; i < N_REG_CLASSES; i++)
155 {
156 CLEAR_HARD_REG_SET (reg_class_contents[i]);
157
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);
163 }
164
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);
173
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);
180
181 SET_HARD_REG_SET (accessible_reg_set);
182 SET_HARD_REG_SET (operand_reg_set);
183 }
184
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;
193
194 /* Save the register information. */
195 void
196 save_register_info (void)
197 {
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);
204
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;
210 }
211
212 /* Restore the register information. */
213 static void
214 restore_register_info (void)
215 {
216 memcpy (fixed_regs, saved_fixed_regs, sizeof fixed_regs);
217 memcpy (call_used_regs, saved_call_used_regs, sizeof call_used_regs);
218
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;
222 }
223
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)
228 {
229 unsigned int i, j;
230 unsigned int /* machine_mode */ m;
231
232 restore_register_info ();
233
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
238
239 /* Let the target tweak things if necessary. */
240
241 targetm.conditional_register_usage ();
242
243 /* Compute number of hard regs in each class. */
244
245 memset (reg_class_size, 0, sizeof reg_class_size);
246 for (i = 0; i < N_REG_CLASSES; i++)
247 {
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))
251 {
252 reg_class_size[i]++;
253 if (!fixed_regs[j])
254 any_nonfixed = true;
255 }
256 class_only_fixed_regs[i] = !any_nonfixed;
257 }
258
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. */
262
263 memset (reg_class_subunion, 0, sizeof reg_class_subunion);
264 for (i = 0; i < N_REG_CLASSES; i++)
265 {
266 for (j = 0; j < N_REG_CLASSES; j++)
267 {
268 HARD_REG_SET c;
269 int k;
270
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;
278 }
279 }
280
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. */
284
285 memset (reg_class_superunion, 0, sizeof reg_class_superunion);
286 for (i = 0; i < N_REG_CLASSES; i++)
287 {
288 for (j = 0; j < N_REG_CLASSES; j++)
289 {
290 HARD_REG_SET c;
291 int k;
292
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;
297
298 reg_class_superunion[i][j] = (enum reg_class) k;
299 }
300 }
301
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. */
304
305 for (i = 0; i < N_REG_CLASSES; i++)
306 {
307 for (j = 0; j < N_REG_CLASSES; j++)
308 reg_class_subclasses[i][j] = LIM_REG_CLASSES;
309 }
310
311 for (i = 0; i < N_REG_CLASSES; i++)
312 {
313 if (i == (int) NO_REGS)
314 continue;
315
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]))
319 {
320 /* Reg class I is a subclass of J.
321 Add J to the table of superclasses of I. */
322 enum reg_class *p;
323
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;
328 }
329 }
330
331 /* Initialize "constant" tables. */
332
333 CLEAR_HARD_REG_SET (fixed_reg_set);
334 CLEAR_HARD_REG_SET (regs_invalidated_by_call);
335
336 operand_reg_set &= accessible_reg_set;
337 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
338 {
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);
348
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;
353
354 if (fixed_regs[i])
355 SET_HARD_REG_BIT (fixed_reg_set, i);
356
357 /* There are a couple of fixed registers that we know are safe to
358 exclude from being clobbered by calls:
359
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. */
366
367 if (i == STACK_POINTER_REGNUM)
368 ;
369 else if (global_regs[i])
370 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
371 else if (i == FRAME_POINTER_REGNUM)
372 ;
373 else if (!HARD_FRAME_POINTER_IS_FRAME_POINTER
374 && i == HARD_FRAME_POINTER_REGNUM)
375 ;
376 else if (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
377 && i == ARG_POINTER_REGNUM && fixed_regs[i])
378 ;
379 else if (!PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
380 && i == (unsigned) PIC_OFFSET_TABLE_REGNUM && fixed_regs[i])
381 ;
382 else if (call_used_regs[i])
383 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
384 }
385
386 SET_HARD_REG_SET (savable_regs);
387 fixed_nonglobal_reg_set = fixed_reg_set;
388
389 /* Preserve global registers if called more than once. */
390 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
391 {
392 if (global_regs[i])
393 {
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);
397 }
398 }
399
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++)
403 {
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))
410 {
411 SET_HARD_REG_BIT (ok_regs, j);
412 if (!fixed_regs[j])
413 SET_HARD_REG_BIT (ok_regs2, j);
414 }
415
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]))
420 {
421 contains_reg_of_mode[i][m] = 1;
422 if (hard_reg_set_intersect_p (ok_regs2, reg_class_contents[i]))
423 {
424 have_regs_of_mode[m] = 1;
425 contains_allocatable_reg_of_mode[i][m] = 1;
426 }
427 }
428 }
429
430 default_function_abi.initialize (0, regs_invalidated_by_call);
431 }
432
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.
438 */
439 void
440 init_reg_modes_target (void)
441 {
442 int i, j;
443
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);
448
449 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
450 {
451 reg_raw_mode[i] = choose_hard_reg_mode (i, 1, NULL);
452
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)
456 {
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;
461 }
462 }
463 }
464
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.
468 */
469 void
470 init_regs (void)
471 {
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 ();
475 }
476
477 /* The same as previous function plus initializing IRA. */
478 void
479 reinit_regs (void)
480 {
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)
485 {
486 ira_init ();
487 recog_init ();
488 }
489 }
490
491 /* Initialize some fake stack-frame MEM references for use in
492 memory_move_secondary_cost. */
493 void
494 init_fake_stack_mems (void)
495 {
496 int i;
497
498 for (i = 0; i < MAX_MACHINE_MODE; i++)
499 top_of_stack[i] = gen_rtx_MEM ((machine_mode) i, stack_pointer_rtx);
500 }
501
502
503 /* Compute cost of moving data from a register of class FROM to one of
504 TO, using MODE. */
505
506 int
507 register_move_cost (machine_mode mode, reg_class_t from, reg_class_t to)
508 {
509 return targetm.register_move_cost (mode, from, to);
510 }
511
512 /* Compute cost of moving registers to/from memory. */
513
514 int
515 memory_move_cost (machine_mode mode, reg_class_t rclass, bool in)
516 {
517 return targetm.memory_move_cost (mode, rclass, in);
518 }
519
520 /* Compute extra cost of moving registers to/from memory due to reloads.
521 Only needed if secondary reloads are required for memory moves. */
522 int
523 memory_move_secondary_cost (machine_mode mode, reg_class_t rclass,
524 bool in)
525 {
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];
531
532 altclass = secondary_reload_class (in ? 1 : 0, rclass, mode, mem);
533
534 if (altclass == NO_REGS)
535 return 0;
536
537 if (in)
538 partial_cost = register_move_cost (mode, altclass, rclass);
539 else
540 partial_cost = register_move_cost (mode, rclass, altclass);
541
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.
546
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;
551
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;
555 }
556
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)
564 {
565 unsigned int /* machine_mode */ m;
566 machine_mode found_mode = VOIDmode, mode;
567
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.
571
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;
580
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;
587
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;
594
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;
601
602 if (found_mode != VOIDmode)
603 return found_mode;
604
605 /* Iterate over all of the CCmodes. */
606 for (m = (unsigned int) CCmode; m < (unsigned int) NUM_MACHINE_MODES; ++m)
607 {
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;
613 }
614
615 /* We can't find a mode valid for this register. */
616 return VOIDmode;
617 }
618
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)
624 {
625 int i;
626 int reg, nregs;
627
628 /* Decode the name and update the primary form of
629 the register info. */
630
631 if ((reg = decode_reg_name_and_count (name, &nregs)) >= 0)
632 {
633 gcc_assert (nregs >= 1);
634 for (i = reg; i < reg + nregs; i++)
635 {
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
642 )
643 && (fixed == 0 || call_used == 0))
644 {
645 switch (fixed)
646 {
647 case 0:
648 switch (call_used)
649 {
650 case 0:
651 error ("cannot use %qs as a call-saved register", name);
652 break;
653
654 case 1:
655 error ("cannot use %qs as a call-used register", name);
656 break;
657
658 default:
659 gcc_unreachable ();
660 }
661 break;
662
663 case 1:
664 switch (call_used)
665 {
666 case 1:
667 error ("cannot use %qs as a fixed register", name);
668 break;
669
670 case 0:
671 default:
672 gcc_unreachable ();
673 }
674 break;
675
676 default:
677 gcc_unreachable ();
678 }
679 }
680 else
681 {
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
689 }
690 }
691 }
692 else
693 {
694 warning (0, "unknown register name: %s", name);
695 }
696 }
697
698 /* Mark register number I as global. */
699 void
700 globalize_reg (tree decl, int i)
701 {
702 location_t loc = DECL_SOURCE_LOCATION (decl);
703
704 #ifdef STACK_REGS
705 if (IN_RANGE (i, FIRST_STACK_REG, LAST_STACK_REG))
706 {
707 error ("stack register used for global register variable");
708 return;
709 }
710 #endif
711
712 if (fixed_regs[i] == 0 && no_global_reg_vars)
713 error_at (loc, "global register variable follows a function definition");
714
715 if (global_regs[i])
716 {
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;
724 }
725
726 if (call_used_regs[i] && ! fixed_regs[i])
727 warning_at (loc, 0, "call-clobbered register used for global register variable");
728
729 global_regs[i] = 1;
730 global_regs_decl[i] = decl;
731 SET_HARD_REG_BIT (global_reg_set, i);
732
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)
737 {
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);
741 }
742
743 /* If already fixed, nothing else to do. */
744 if (fixed_regs[i])
745 return;
746
747 fixed_regs[i] = call_used_regs[i] = 1;
748
749 SET_HARD_REG_BIT (fixed_reg_set, i);
750
751 reinit_regs ();
752 }
753 \f
754
755 /* Structure used to record preferences of given pseudo. */
756 struct reg_pref
757 {
758 /* (enum reg_class) prefclass is the preferred class. May be
759 NO_REGS if no class is better than memory. */
760 char prefclass;
761
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.
765
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;
770
771 /* allocnoclass is a register class that IRA uses for allocating
772 the pseudo. */
773 char allocnoclass;
774 };
775
776 /* Record preferences of each pseudo. This is available after RA is
777 run. */
778 static struct reg_pref *reg_pref;
779
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;
784
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)
790 {
791 if (reg_pref == 0)
792 return GENERAL_REGS;
793
794 gcc_assert (regno < reg_info_size);
795 return (enum reg_class) reg_pref[regno].prefclass;
796 }
797
798 enum reg_class
799 reg_alternate_class (int regno)
800 {
801 if (reg_pref == 0)
802 return ALL_REGS;
803
804 gcc_assert (regno < reg_info_size);
805 return (enum reg_class) reg_pref[regno].altclass;
806 }
807
808 /* Return the reg_class which is used by IRA for its allocation. */
809 enum reg_class
810 reg_allocno_class (int regno)
811 {
812 if (reg_pref == 0)
813 return NO_REGS;
814
815 gcc_assert (regno < reg_info_size);
816 return (enum reg_class) reg_pref[regno].allocnoclass;
817 }
818
819 \f
820
821 /* Allocate space for reg info and initilize it. */
822 static void
823 allocate_reg_info (void)
824 {
825 int i;
826
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++)
834 {
835 reg_pref[i].prefclass = GENERAL_REGS;
836 reg_pref[i].altclass = ALL_REGS;
837 reg_pref[i].allocnoclass = GENERAL_REGS;
838 }
839 }
840
841
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)
846 {
847 int old, i;
848 bool change_p;
849
850 if (reg_pref == NULL)
851 {
852 allocate_reg_info ();
853 return true;
854 }
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++)
868 {
869 reg_pref[i].prefclass = GENERAL_REGS;
870 reg_pref[i].altclass = ALL_REGS;
871 reg_pref[i].allocnoclass = GENERAL_REGS;
872 }
873 return true;
874 }
875
876
877 /* Free up the space allocated by allocate_reg_info. */
878 void
879 free_reg_info (void)
880 {
881 if (reg_pref)
882 {
883 free (reg_pref);
884 reg_pref = NULL;
885 }
886
887 if (reg_renumber)
888 {
889 free (reg_renumber);
890 reg_renumber = NULL;
891 }
892 }
893
894 /* Initialize some global data for this pass. */
895 static unsigned int
896 reginfo_init (void)
897 {
898 if (df)
899 df_compute_regs_ever_live (true);
900
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;
908 }
909
910 namespace {
911
912 const pass_data pass_data_reginfo_init =
913 {
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 */
923 };
924
925 class pass_reginfo_init : public rtl_opt_pass
926 {
927 public:
928 pass_reginfo_init (gcc::context *ctxt)
929 : rtl_opt_pass (pass_data_reginfo_init, ctxt)
930 {}
931
932 /* opt_pass methods: */
933 virtual unsigned int execute (function *) { return reginfo_init (); }
934
935 }; // class pass_reginfo_init
936
937 } // anon namespace
938
939 rtl_opt_pass *
940 make_pass_reginfo_init (gcc::context *ctxt)
941 {
942 return new pass_reginfo_init (ctxt);
943 }
944
945 \f
946
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)
953 {
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;
960 }
961
962 \f
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. */
966
967 static void reg_scan_mark_refs (rtx, rtx_insn *);
968
969 void
970 reg_scan (rtx_insn *f, unsigned int nregs ATTRIBUTE_UNUSED)
971 {
972 rtx_insn *insn;
973
974 timevar_push (TV_REG_SCAN);
975
976 for (insn = f; insn; insn = NEXT_INSN (insn))
977 if (INSN_P (insn))
978 {
979 reg_scan_mark_refs (PATTERN (insn), insn);
980 if (REG_NOTES (insn))
981 reg_scan_mark_refs (REG_NOTES (insn), insn);
982 }
983
984 timevar_pop (TV_REG_SCAN);
985 }
986
987
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)
994 {
995 enum rtx_code code;
996 rtx dest;
997 rtx note;
998
999 if (!x)
1000 return;
1001 code = GET_CODE (x);
1002 switch (code)
1003 {
1004 case CONST:
1005 CASE_CONST_ANY:
1006 case CC0:
1007 case PC:
1008 case SYMBOL_REF:
1009 case LABEL_REF:
1010 case ADDR_VEC:
1011 case ADDR_DIFF_VEC:
1012 case REG:
1013 return;
1014
1015 case EXPR_LIST:
1016 if (XEXP (x, 0))
1017 reg_scan_mark_refs (XEXP (x, 0), insn);
1018 if (XEXP (x, 1))
1019 reg_scan_mark_refs (XEXP (x, 1), insn);
1020 break;
1021
1022 case INSN_LIST:
1023 case INT_LIST:
1024 if (XEXP (x, 1))
1025 reg_scan_mark_refs (XEXP (x, 1), insn);
1026 break;
1027
1028 case CLOBBER:
1029 if (MEM_P (XEXP (x, 0)))
1030 reg_scan_mark_refs (XEXP (XEXP (x, 0), 0), insn);
1031 break;
1032
1033 case SET:
1034 /* Count a set of the destination if it is a register. */
1035 for (dest = SET_DEST (x);
1036 GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
1037 || GET_CODE (dest) == ZERO_EXTRACT;
1038 dest = XEXP (dest, 0))
1039 ;
1040
1041 /* If this is setting a pseudo from another pseudo or the sum of a
1042 pseudo and a constant integer and the other pseudo is known to be
1043 a pointer, set the destination to be a pointer as well.
1044
1045 Likewise if it is setting the destination from an address or from a
1046 value equivalent to an address or to the sum of an address and
1047 something else.
1048
1049 But don't do any of this if the pseudo corresponds to a user
1050 variable since it should have already been set as a pointer based
1051 on the type. */
1052
1053 if (REG_P (SET_DEST (x))
1054 && REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER
1055 /* If the destination pseudo is set more than once, then other
1056 sets might not be to a pointer value (consider access to a
1057 union in two threads of control in the presence of global
1058 optimizations). So only set REG_POINTER on the destination
1059 pseudo if this is the only set of that pseudo. */
1060 && DF_REG_DEF_COUNT (REGNO (SET_DEST (x))) == 1
1061 && ! REG_USERVAR_P (SET_DEST (x))
1062 && ! REG_POINTER (SET_DEST (x))
1063 && ((REG_P (SET_SRC (x))
1064 && REG_POINTER (SET_SRC (x)))
1065 || ((GET_CODE (SET_SRC (x)) == PLUS
1066 || GET_CODE (SET_SRC (x)) == LO_SUM)
1067 && CONST_INT_P (XEXP (SET_SRC (x), 1))
1068 && REG_P (XEXP (SET_SRC (x), 0))
1069 && REG_POINTER (XEXP (SET_SRC (x), 0)))
1070 || GET_CODE (SET_SRC (x)) == CONST
1071 || GET_CODE (SET_SRC (x)) == SYMBOL_REF
1072 || GET_CODE (SET_SRC (x)) == LABEL_REF
1073 || (GET_CODE (SET_SRC (x)) == HIGH
1074 && (GET_CODE (XEXP (SET_SRC (x), 0)) == CONST
1075 || GET_CODE (XEXP (SET_SRC (x), 0)) == SYMBOL_REF
1076 || GET_CODE (XEXP (SET_SRC (x), 0)) == LABEL_REF))
1077 || ((GET_CODE (SET_SRC (x)) == PLUS
1078 || GET_CODE (SET_SRC (x)) == LO_SUM)
1079 && (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST
1080 || GET_CODE (XEXP (SET_SRC (x), 1)) == SYMBOL_REF
1081 || GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF))
1082 || ((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
1083 && (GET_CODE (XEXP (note, 0)) == CONST
1084 || GET_CODE (XEXP (note, 0)) == SYMBOL_REF
1085 || GET_CODE (XEXP (note, 0)) == LABEL_REF))))
1086 REG_POINTER (SET_DEST (x)) = 1;
1087
1088 /* If this is setting a register from a register or from a simple
1089 conversion of a register, propagate REG_EXPR. */
1090 if (REG_P (dest) && !REG_ATTRS (dest))
1091 set_reg_attrs_from_value (dest, SET_SRC (x));
1092
1093 /* fall through */
1094
1095 default:
1096 {
1097 const char *fmt = GET_RTX_FORMAT (code);
1098 int i;
1099 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1100 {
1101 if (fmt[i] == 'e')
1102 reg_scan_mark_refs (XEXP (x, i), insn);
1103 else if (fmt[i] == 'E' && XVEC (x, i) != 0)
1104 {
1105 int j;
1106 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1107 reg_scan_mark_refs (XVECEXP (x, i, j), insn);
1108 }
1109 }
1110 }
1111 }
1112 }
1113 \f
1114
1115 /* Return nonzero if C1 is a subset of C2, i.e., if every register in C1
1116 is also in C2. */
1117 int
1118 reg_class_subset_p (reg_class_t c1, reg_class_t c2)
1119 {
1120 return (c1 == c2
1121 || c2 == ALL_REGS
1122 || hard_reg_set_subset_p (reg_class_contents[(int) c1],
1123 reg_class_contents[(int) c2]));
1124 }
1125
1126 /* Return nonzero if there is a register that is in both C1 and C2. */
1127 int
1128 reg_classes_intersect_p (reg_class_t c1, reg_class_t c2)
1129 {
1130 return (c1 == c2
1131 || c1 == ALL_REGS
1132 || c2 == ALL_REGS
1133 || hard_reg_set_intersect_p (reg_class_contents[(int) c1],
1134 reg_class_contents[(int) c2]));
1135 }
1136
1137 \f
1138 inline hashval_t
1139 simplifiable_subregs_hasher::hash (const simplifiable_subreg *value)
1140 {
1141 inchash::hash h;
1142 h.add_hwi (value->shape.unique_id ());
1143 return h.end ();
1144 }
1145
1146 inline bool
1147 simplifiable_subregs_hasher::equal (const simplifiable_subreg *value,
1148 const subreg_shape *compare)
1149 {
1150 return value->shape == *compare;
1151 }
1152
1153 inline simplifiable_subreg::simplifiable_subreg (const subreg_shape &shape_in)
1154 : shape (shape_in)
1155 {
1156 CLEAR_HARD_REG_SET (simplifiable_regs);
1157 }
1158
1159 /* Return the set of hard registers that are able to form the subreg
1160 described by SHAPE. */
1161
1162 const HARD_REG_SET &
1163 simplifiable_subregs (const subreg_shape &shape)
1164 {
1165 if (!this_target_hard_regs->x_simplifiable_subregs)
1166 this_target_hard_regs->x_simplifiable_subregs
1167 = new hash_table <simplifiable_subregs_hasher> (30);
1168 inchash::hash h;
1169 h.add_hwi (shape.unique_id ());
1170 simplifiable_subreg **slot
1171 = (this_target_hard_regs->x_simplifiable_subregs
1172 ->find_slot_with_hash (&shape, h.end (), INSERT));
1173
1174 if (!*slot)
1175 {
1176 simplifiable_subreg *info = new simplifiable_subreg (shape);
1177 for (unsigned int i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1178 if (targetm.hard_regno_mode_ok (i, shape.inner_mode)
1179 && simplify_subreg_regno (i, shape.inner_mode, shape.offset,
1180 shape.outer_mode) >= 0)
1181 SET_HARD_REG_BIT (info->simplifiable_regs, i);
1182 *slot = info;
1183 }
1184 return (*slot)->simplifiable_regs;
1185 }
1186
1187 /* Passes for keeping and updating info about modes of registers
1188 inside subregisters. */
1189
1190 static HARD_REG_SET **valid_mode_changes;
1191 static obstack valid_mode_changes_obstack;
1192
1193 /* Restrict the choice of register for SUBREG_REG (SUBREG) based
1194 on information about SUBREG.
1195
1196 If PARTIAL_DEF, SUBREG is a partial definition of a multipart inner
1197 register and we want to ensure that the other parts of the inner
1198 register are correctly preserved. If !PARTIAL_DEF we need to
1199 ensure that SUBREG itself can be formed. */
1200
1201 static void
1202 record_subregs_of_mode (rtx subreg, bool partial_def)
1203 {
1204 unsigned int regno;
1205
1206 if (!REG_P (SUBREG_REG (subreg)))
1207 return;
1208
1209 regno = REGNO (SUBREG_REG (subreg));
1210 if (regno < FIRST_PSEUDO_REGISTER)
1211 return;
1212
1213 subreg_shape shape (shape_of_subreg (subreg));
1214 if (partial_def)
1215 {
1216 /* The number of independently-accessible SHAPE.outer_mode values
1217 in SHAPE.inner_mode is GET_MODE_SIZE (SHAPE.inner_mode) / SIZE.
1218 We need to check that the assignment will preserve all the other
1219 SIZE-byte chunks in the inner register besides the one that
1220 includes SUBREG.
1221
1222 In practice it is enough to check whether an equivalent
1223 SHAPE.inner_mode value in an adjacent SIZE-byte chunk can be formed.
1224 If the underlying registers are small enough, both subregs will
1225 be valid. If the underlying registers are too large, one of the
1226 subregs will be invalid.
1227
1228 This relies on the fact that we've already been passed
1229 SUBREG with PARTIAL_DEF set to false.
1230
1231 The size of the outer mode must ordered wrt the size of the
1232 inner mode's registers, since otherwise we wouldn't know at
1233 compile time how many registers the outer mode occupies. */
1234 poly_uint64 size = ordered_max (REGMODE_NATURAL_SIZE (shape.inner_mode),
1235 GET_MODE_SIZE (shape.outer_mode));
1236 gcc_checking_assert (known_lt (size, GET_MODE_SIZE (shape.inner_mode)));
1237 if (known_ge (shape.offset, size))
1238 shape.offset -= size;
1239 else
1240 shape.offset += size;
1241 }
1242
1243 if (valid_mode_changes[regno])
1244 *valid_mode_changes[regno] &= simplifiable_subregs (shape);
1245 else
1246 {
1247 valid_mode_changes[regno]
1248 = XOBNEW (&valid_mode_changes_obstack, HARD_REG_SET);
1249 *valid_mode_changes[regno] = simplifiable_subregs (shape);
1250 }
1251 }
1252
1253 /* Call record_subregs_of_mode for all the subregs in X. */
1254 static void
1255 find_subregs_of_mode (rtx x)
1256 {
1257 enum rtx_code code = GET_CODE (x);
1258 const char * const fmt = GET_RTX_FORMAT (code);
1259 int i;
1260
1261 if (code == SUBREG)
1262 record_subregs_of_mode (x, false);
1263
1264 /* Time for some deep diving. */
1265 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1266 {
1267 if (fmt[i] == 'e')
1268 find_subregs_of_mode (XEXP (x, i));
1269 else if (fmt[i] == 'E')
1270 {
1271 int j;
1272 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1273 find_subregs_of_mode (XVECEXP (x, i, j));
1274 }
1275 }
1276 }
1277
1278 void
1279 init_subregs_of_mode (void)
1280 {
1281 basic_block bb;
1282 rtx_insn *insn;
1283
1284 gcc_obstack_init (&valid_mode_changes_obstack);
1285 valid_mode_changes = XCNEWVEC (HARD_REG_SET *, max_reg_num ());
1286
1287 FOR_EACH_BB_FN (bb, cfun)
1288 FOR_BB_INSNS (bb, insn)
1289 if (NONDEBUG_INSN_P (insn))
1290 {
1291 find_subregs_of_mode (PATTERN (insn));
1292 df_ref def;
1293 FOR_EACH_INSN_DEF (def, insn)
1294 if (DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL)
1295 && read_modify_subreg_p (DF_REF_REG (def)))
1296 record_subregs_of_mode (DF_REF_REG (def), true);
1297 }
1298 }
1299
1300 const HARD_REG_SET *
1301 valid_mode_changes_for_regno (unsigned int regno)
1302 {
1303 return valid_mode_changes[regno];
1304 }
1305
1306 void
1307 finish_subregs_of_mode (void)
1308 {
1309 XDELETEVEC (valid_mode_changes);
1310 obstack_free (&valid_mode_changes_obstack, NULL);
1311 }
1312
1313 /* Free all data attached to the structure. This isn't a destructor because
1314 we don't want to run on exit. */
1315
1316 void
1317 target_hard_regs::finalize ()
1318 {
1319 delete x_simplifiable_subregs;
1320 }