GCC Middle and Back End API Reference
hard-reg-set.h
Go to the documentation of this file.
1/* Sets (bit vectors) of hard registers, and operations on them.
2 Copyright (C) 1987-2025 Free Software Foundation, Inc.
3
4This file is part of GCC
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20#ifndef GCC_HARD_REG_SET_H
21#define GCC_HARD_REG_SET_H
22
23#include "array-traits.h"
24
25/* Define the type of a set of hard registers. */
26
27/* HARD_REG_ELT_TYPE is a typedef of the unsigned integral type which
28 will be used for hard reg sets, either alone or in an array.
29
30 If HARD_REG_SET is a macro, its definition is HARD_REG_ELT_TYPE,
31 and it has enough bits to represent all the target machine's hard
32 registers. Otherwise, it is a typedef for a suitably sized array
33 of HARD_REG_ELT_TYPEs. HARD_REG_SET_LONGS is defined as how many.
34
35 Note that lots of code assumes that the first part of a regset is
36 the same format as a HARD_REG_SET. To help make sure this is true,
37 we only try the widest fast integer mode (HOST_WIDEST_FAST_INT)
38 instead of all the smaller types. This approach loses only if
39 there are very few registers and then only in the few cases where
40 we have an array of HARD_REG_SETs, so it needn't be as complex as
41 it used to be. */
42
44
45#if FIRST_PSEUDO_REGISTER <= HOST_BITS_PER_WIDEST_FAST_INT
46
49
50#else
51
52#define HARD_REG_SET_LONGS \
53 ((FIRST_PSEUDO_REGISTER + HOST_BITS_PER_WIDEST_FAST_INT - 1) \
54 / HOST_BITS_PER_WIDEST_FAST_INT)
55
56struct HARD_REG_SET
57{
59 operator~ () const
60 {
61 HARD_REG_SET res;
62 for (unsigned int i = 0; i < ARRAY_SIZE (elts); ++i)
63 res.elts[i] = ~elts[i];
64 return res;
65 }
66
68 operator& (const HARD_REG_SET &other) const
69 {
70 HARD_REG_SET res;
71 for (unsigned int i = 0; i < ARRAY_SIZE (elts); ++i)
72 res.elts[i] = elts[i] & other.elts[i];
73 return res;
74 }
75
77 operator&= (const HARD_REG_SET &other)
78 {
79 for (unsigned int i = 0; i < ARRAY_SIZE (elts); ++i)
80 elts[i] &= other.elts[i];
81 return *this;
82 }
83
85 operator| (const HARD_REG_SET &other) const
86 {
87 HARD_REG_SET res;
88 for (unsigned int i = 0; i < ARRAY_SIZE (elts); ++i)
89 res.elts[i] = elts[i] | other.elts[i];
90 return res;
91 }
92
94 operator|= (const HARD_REG_SET &other)
95 {
96 for (unsigned int i = 0; i < ARRAY_SIZE (elts); ++i)
97 elts[i] |= other.elts[i];
98 return *this;
99 }
100
101 bool
102 operator== (const HARD_REG_SET &other) const
103 {
104 HARD_REG_ELT_TYPE bad = 0;
105 for (unsigned int i = 0; i < ARRAY_SIZE (elts); ++i)
106 bad |= (elts[i] ^ other.elts[i]);
107 return bad == 0;
108 }
109
110 bool
111 operator!= (const HARD_REG_SET &other) const
112 {
113 return !operator== (other);
114 }
115
116 HARD_REG_ELT_TYPE elts[HARD_REG_SET_LONGS];
117};
118typedef const HARD_REG_SET &const_hard_reg_set;
119
120template<>
122{
124 static const bool has_constant_size = true;
125 static const size_t constant_size = HARD_REG_SET_LONGS;
126 static const element_type *base (const HARD_REG_SET &x) { return x.elts; }
127 static size_t size (const HARD_REG_SET &) { return HARD_REG_SET_LONGS; }
128};
129
130#endif
131
132/* HARD_REG_SET wrapped into a structure, to make it possible to
133 use HARD_REG_SET even in APIs that should not include
134 hard-reg-set.h. */
139
140/* HARD_CONST is used to cast a constant to the appropriate type
141 for use with a HARD_REG_SET. */
142
143#define HARD_CONST(X) ((HARD_REG_ELT_TYPE) (X))
144
145/* Define macros SET_HARD_REG_BIT, CLEAR_HARD_REG_BIT and TEST_HARD_REG_BIT
146 to set, clear or test one bit in a hard reg set of type HARD_REG_SET.
147 All three take two arguments: the set and the register number.
148
149 In the case where sets are arrays of longs, the first argument
150 is actually a pointer to a long.
151
152 Define two macros for initializing a set:
153 CLEAR_HARD_REG_SET and SET_HARD_REG_SET.
154 These take just one argument.
155
156 Also define:
157
158 hard_reg_set_subset_p (X, Y), which returns true if X is a subset of Y.
159 hard_reg_set_intersect_p (X, Y), which returns true if X and Y intersect.
160 hard_reg_set_empty_p (X), which returns true if X is empty. */
161
162#define UHOST_BITS_PER_WIDE_INT ((unsigned) HOST_BITS_PER_WIDEST_FAST_INT)
163
164#if FIRST_PSEUDO_REGISTER <= HOST_BITS_PER_WIDEST_FAST_INT
165
166#define SET_HARD_REG_BIT(SET, BIT) \
167 ((SET) |= HARD_CONST (1) << (BIT))
168#define CLEAR_HARD_REG_BIT(SET, BIT) \
169 ((SET) &= ~(HARD_CONST (1) << (BIT)))
170#define TEST_HARD_REG_BIT(SET, BIT) \
171 (!!((SET) & (HARD_CONST (1) << (BIT))))
172
173#define CLEAR_HARD_REG_SET(TO) ((TO) = HARD_CONST (0))
174#define SET_HARD_REG_SET(TO) ((TO) = ~ HARD_CONST (0))
175
176inline bool
181
182inline bool
187
188inline bool
190{
191 return x == HARD_CONST (0);
192}
193
194inline int
199
200#else
201
202inline void
203SET_HARD_REG_BIT (HARD_REG_SET &set, unsigned int bit)
204{
205 set.elts[bit / UHOST_BITS_PER_WIDE_INT]
206 |= HARD_CONST (1) << (bit % UHOST_BITS_PER_WIDE_INT);
207}
208
209inline void
210CLEAR_HARD_REG_BIT (HARD_REG_SET &set, unsigned int bit)
211{
212 set.elts[bit / UHOST_BITS_PER_WIDE_INT]
213 &= ~(HARD_CONST (1) << (bit % UHOST_BITS_PER_WIDE_INT));
214}
215
216inline bool
217TEST_HARD_REG_BIT (const_hard_reg_set set, unsigned int bit)
218{
219 return (set.elts[bit / UHOST_BITS_PER_WIDE_INT]
220 & (HARD_CONST (1) << (bit % UHOST_BITS_PER_WIDE_INT)));
221}
222
223inline void
225{
226 for (unsigned int i = 0; i < ARRAY_SIZE (set.elts); ++i)
227 set.elts[i] = 0;
228}
229
230inline void
232{
233 for (unsigned int i = 0; i < ARRAY_SIZE (set.elts); ++i)
234 set.elts[i] = -1;
235}
236
237inline bool
239{
240 HARD_REG_ELT_TYPE bad = 0;
241 for (unsigned int i = 0; i < ARRAY_SIZE (x.elts); ++i)
242 bad |= (x.elts[i] & ~y.elts[i]);
243 return bad == 0;
244}
245
246inline bool
248{
249 HARD_REG_ELT_TYPE good = 0;
250 for (unsigned int i = 0; i < ARRAY_SIZE (x.elts); ++i)
251 good |= (x.elts[i] & y.elts[i]);
252 return good != 0;
253}
254
255inline bool
257{
258 HARD_REG_ELT_TYPE bad = 0;
259 for (unsigned int i = 0; i < ARRAY_SIZE (x.elts); ++i)
260 bad |= x.elts[i];
261 return bad == 0;
262}
263
264inline int
266{
267 int count = 0;
268 for (unsigned int i = 0; i < ARRAY_SIZE (x.elts); ++i)
269 count += popcount_hwi (x.elts[i]);
270 return count;
271}
272#endif
273
274/* Iterator for hard register sets. */
275
277{
278 /* Pointer to the current element. */
280
281 /* The length of the set. */
282 unsigned short length;
283
284 /* Word within the current element. */
285 unsigned short word_no;
286
287 /* Contents of the actually processed word. When finding next bit
288 it is shifted right, so that the actual bit is always the least
289 significant bit of ACTUAL. */
291};
292
293#define HARD_REG_ELT_BITS UHOST_BITS_PER_WIDE_INT
294
295/* The implementation of the iterator functions is fully analogous to
296 the bitmap iterators. */
297inline void
299 unsigned min, unsigned *regno)
300{
301#ifdef HARD_REG_SET_LONGS
302 iter->pelt = set.elts;
303 iter->length = HARD_REG_SET_LONGS;
304#else
305 iter->pelt = &set;
306 iter->length = 1;
307#endif
308 iter->word_no = min / HARD_REG_ELT_BITS;
309 if (iter->word_no < iter->length)
310 {
311 iter->bits = iter->pelt[iter->word_no];
312 iter->bits >>= min % HARD_REG_ELT_BITS;
313
314 /* This is required for correct search of the next bit. */
315 min += !iter->bits;
316 }
317 *regno = min;
318}
319
320inline bool
322{
323 while (1)
324 {
325 /* Return false when we're advanced past the end of the set. */
326 if (iter->word_no >= iter->length)
327 return false;
328
329 if (iter->bits)
330 {
331 /* Find the correct bit and return it. */
332 while (!(iter->bits & 1))
333 {
334 iter->bits >>= 1;
335 *regno += 1;
336 }
337 return (*regno < FIRST_PSEUDO_REGISTER);
338 }
339
340 /* Round to the beginning of the next word. */
341 *regno = (*regno + HARD_REG_ELT_BITS - 1);
342 *regno -= *regno % HARD_REG_ELT_BITS;
343
344 /* Find the next non-zero word. */
345 while (++iter->word_no < iter->length)
346 {
347 iter->bits = iter->pelt[iter->word_no];
348 if (iter->bits)
349 break;
350 *regno += HARD_REG_ELT_BITS;
351 }
352 }
353}
354
355inline void
357{
358 iter->bits >>= 1;
359 *regno += 1;
360}
361
362#define EXECUTE_IF_SET_IN_HARD_REG_SET(SET, MIN, REGNUM, ITER) \
363 for (hard_reg_set_iter_init (&(ITER), (SET), (MIN), &(REGNUM)); \
364 hard_reg_set_iter_set (&(ITER), &(REGNUM)); \
365 hard_reg_set_iter_next (&(ITER), &(REGNUM)))
366
367
368/* Define some standard sets of registers. */
369
370/* Indexed by hard register number, contains 1 for registers
371 that are being used for global register decls.
372 These must be exempt from ordinary flow analysis
373 and are also considered fixed. */
374
375extern char global_regs[FIRST_PSEUDO_REGISTER];
376
378
380class subreg_shape;
381
382struct simplifiable_subregs_hasher : nofree_ptr_hash <simplifiable_subreg>
383{
385
386 static inline hashval_t hash (const simplifiable_subreg *);
387 static inline bool equal (const simplifiable_subreg *, const subreg_shape *);
388};
389
391 void finalize ();
392
393 /* The set of registers that actually exist on the current target. */
395
396 /* The set of registers that should be considered to be register
397 operands. It is a subset of x_accessible_reg_set. */
399
400 /* Indexed by hard register number, contains 1 for registers
401 that are fixed use (stack pointer, pc, frame pointer, etc.;.
402 These are the registers that cannot be used to allocate
403 a pseudo reg whose life does not cross calls. */
404 char x_fixed_regs[FIRST_PSEUDO_REGISTER];
405
406 /* The same info as a HARD_REG_SET. */
408
409 /* Indexed by hard register number, contains 1 for registers
410 that are fixed use or are clobbered by function calls.
411 These are the registers that cannot be used to allocate
412 a pseudo reg whose life crosses calls. */
413 char x_call_used_regs[FIRST_PSEUDO_REGISTER];
414
415 /* For targets that use reload rather than LRA, this is the set
416 of registers that we are able to save and restore around calls
417 (i.e. those for which we know a suitable mode and set of
418 load/store instructions exist). For LRA targets it contains
419 all registers.
420
421 This is legacy information and should be removed if all targets
422 switch to LRA. */
424
425 /* Contains registers that are fixed use -- i.e. in fixed_reg_set -- but
426 only if they are not merely part of that set because they are global
427 regs. Global regs that are not otherwise fixed can still take part
428 in register allocation. */
430
431 /* Contains 1 for registers that are set or clobbered by calls. */
432 /* ??? Ideally, this would be just call_used_regs plus global_regs, but
433 for someone's bright idea to have call_used_regs strictly include
434 fixed_regs. Which leaves us guessing as to the set of fixed_regs
435 that are actually preserved. We know for sure that those associated
436 with the local stack frame are safe, but scant others. */
438
439 /* The set of registers that are used by EH_RETURN_DATA_REGNO. */
441
442 /* Table of register numbers in the order in which to try to use them. */
443 int x_reg_alloc_order[FIRST_PSEUDO_REGISTER];
444
445 /* The inverse of reg_alloc_order. */
446 int x_inv_reg_alloc_order[FIRST_PSEUDO_REGISTER];
447
448 /* For each reg class, a HARD_REG_SET saying which registers are in it. */
450
451 /* For each reg class, a boolean saying whether the class contains only
452 fixed registers. */
453 bool x_class_only_fixed_regs[N_REG_CLASSES];
454
455 /* For each reg class, number of regs it contains. */
456 unsigned int x_reg_class_size[N_REG_CLASSES];
457
458 /* For each reg class, table listing all the classes contained in it. */
459 enum reg_class x_reg_class_subclasses[N_REG_CLASSES][N_REG_CLASSES];
460
461 /* For each pair of reg classes,
462 a largest reg class contained in their union. */
463 enum reg_class x_reg_class_subunion[N_REG_CLASSES][N_REG_CLASSES];
464
465 /* For each pair of reg classes,
466 the smallest reg class that contains their union. */
467 enum reg_class x_reg_class_superunion[N_REG_CLASSES][N_REG_CLASSES];
468
469 /* Vector indexed by hardware reg giving its name. */
470 const char *x_reg_names[FIRST_PSEUDO_REGISTER];
471
472 /* Records which registers can form a particular subreg, with the subreg
473 being identified by its outer mode, inner mode and offset. */
474 hash_table <simplifiable_subregs_hasher> *x_simplifiable_subregs;
475};
476
478#if SWITCHABLE_TARGET
480#else
481#define this_target_hard_regs (&default_target_hard_regs)
482#endif
483
484#define accessible_reg_set \
485 (this_target_hard_regs->x_accessible_reg_set)
486#define operand_reg_set \
487 (this_target_hard_regs->x_operand_reg_set)
488#define fixed_regs \
489 (this_target_hard_regs->x_fixed_regs)
490#define fixed_reg_set \
491 (this_target_hard_regs->x_fixed_reg_set)
492#define fixed_nonglobal_reg_set \
493 (this_target_hard_regs->x_fixed_nonglobal_reg_set)
494#ifdef IN_TARGET_CODE
495#define call_used_regs \
496 (this_target_hard_regs->x_call_used_regs)
497#endif
498#define savable_regs \
499 (this_target_hard_regs->x_savable_regs)
500#ifdef IN_TARGET_CODE
501#define regs_invalidated_by_call \
502 (this_target_hard_regs->x_regs_invalidated_by_call)
503#define call_used_or_fixed_regs \
504 (regs_invalidated_by_call | fixed_reg_set)
505#endif
506#define eh_return_data_regs \
507 (this_target_hard_regs->x_eh_return_data_regs)
508#define reg_alloc_order \
509 (this_target_hard_regs->x_reg_alloc_order)
510#define inv_reg_alloc_order \
511 (this_target_hard_regs->x_inv_reg_alloc_order)
512#define reg_class_contents \
513 (this_target_hard_regs->x_reg_class_contents)
514#define class_only_fixed_regs \
515 (this_target_hard_regs->x_class_only_fixed_regs)
516#define reg_class_size \
517 (this_target_hard_regs->x_reg_class_size)
518#define reg_class_subclasses \
519 (this_target_hard_regs->x_reg_class_subclasses)
520#define reg_class_subunion \
521 (this_target_hard_regs->x_reg_class_subunion)
522#define reg_class_superunion \
523 (this_target_hard_regs->x_reg_class_superunion)
524#define reg_names \
525 (this_target_hard_regs->x_reg_names)
526
527/* Vector indexed by reg class giving its name. */
528
529extern const char * reg_class_names[];
530
531/* Given a hard REGN a FROM mode and a TO mode, return true if
532 REGN can change from mode FROM to mode TO. */
533#define REG_CAN_CHANGE_MODE_P(REGN, FROM, TO) \
534 (targetm.can_change_mode_class (FROM, TO, REGNO_REG_CLASS (REGN)))
535
536#ifdef IN_TARGET_CODE
537/* Return true if register REGNO is either fixed or call-used
538 (aka call-clobbered). */
539
540inline bool
541call_used_or_fixed_reg_p (unsigned int regno)
542{
543 return fixed_regs[regno] || this_target_hard_regs->x_call_used_regs[regno];
544}
545#endif
546
547#endif /* ! GCC_HARD_REG_SET_H */
Definition reginfo.cc:55
Definition rtl.h:2130
static unsigned int count[debug_counter_number_of_counters]
Definition dbgcnt.cc:50
bool operator==(const nowarn_spec_t &lhs, const nowarn_spec_t &rhs)
Definition diagnostic-spec.h:131
bool operator!=(const nowarn_spec_t &lhs, const nowarn_spec_t &rhs)
Definition diagnostic-spec.h:139
nowarn_spec_t operator|(const nowarn_spec_t &lhs, const nowarn_spec_t &rhs)
Definition diagnostic-spec.h:115
nowarn_spec_t operator&(const nowarn_spec_t &lhs, const nowarn_spec_t &rhs)
Definition diagnostic-spec.h:123
dump_flags_t & operator&=(dump_flags_t &lhs, dump_flags_t rhs)
Definition dumpfile.h:240
dump_flags_t & operator|=(dump_flags_t &lhs, dump_flags_t rhs)
Definition dumpfile.h:232
dump_flags_t operator~(dump_flags_t flags)
Definition dumpfile.h:226
#define SET_HARD_REG_SET(TO)
Definition hard-reg-set.h:174
bool hard_reg_set_subset_p(const_hard_reg_set x, const_hard_reg_set y)
Definition hard-reg-set.h:177
#define HARD_REG_ELT_BITS
Definition hard-reg-set.h:293
#define this_target_hard_regs
Definition hard-reg-set.h:481
unsigned HOST_WIDEST_FAST_INT HARD_REG_ELT_TYPE
Definition hard-reg-set.h:43
bool hard_reg_set_iter_set(hard_reg_set_iterator *iter, unsigned *regno)
Definition hard-reg-set.h:321
#define TEST_HARD_REG_BIT(SET, BIT)
Definition hard-reg-set.h:170
#define CLEAR_HARD_REG_BIT(SET, BIT)
Definition hard-reg-set.h:168
char global_regs[FIRST_PSEUDO_REGISTER]
Definition reginfo.cc:92
HARD_REG_ELT_TYPE HARD_REG_SET
Definition hard-reg-set.h:47
bool hard_reg_set_intersect_p(const_hard_reg_set x, const_hard_reg_set y)
Definition hard-reg-set.h:183
#define HARD_CONST(X)
Definition hard-reg-set.h:143
#define fixed_regs
Definition hard-reg-set.h:488
void hard_reg_set_iter_next(hard_reg_set_iterator *iter, unsigned *regno)
Definition hard-reg-set.h:356
int hard_reg_set_popcount(const_hard_reg_set x)
Definition hard-reg-set.h:195
#define CLEAR_HARD_REG_SET(TO)
Definition hard-reg-set.h:173
void hard_reg_set_iter_init(hard_reg_set_iterator *iter, const_hard_reg_set set, unsigned min, unsigned *regno)
Definition hard-reg-set.h:298
const char * reg_class_names[]
Definition reginfo.cc:119
bool hard_reg_set_empty_p(const_hard_reg_set x)
Definition hard-reg-set.h:189
#define UHOST_BITS_PER_WIDE_INT
Definition hard-reg-set.h:162
HARD_REG_SET global_reg_set
Definition reginfo.cc:95
#define SET_HARD_REG_BIT(SET, BIT)
Definition hard-reg-set.h:166
struct target_hard_regs default_target_hard_regs
Definition reginfo.cc:63
const HARD_REG_SET const_hard_reg_set
Definition hard-reg-set.h:48
int popcount_hwi(unsigned HOST_WIDE_INT x)
Definition hwint.cc:111
#define HOST_WIDEST_FAST_INT
Definition hwint.h:154
i
Definition poly-int.h:776
Definition array-traits.h:35
Definition hard-reg-set.h:136
HARD_REG_SET set
Definition hard-reg-set.h:137
Definition hard-reg-set.h:277
unsigned short length
Definition hard-reg-set.h:282
const HARD_REG_ELT_TYPE * pelt
Definition hard-reg-set.h:279
unsigned short word_no
Definition hard-reg-set.h:285
HARD_REG_ELT_TYPE bits
Definition hard-reg-set.h:290
Definition hash-traits.h:303
static const size_t constant_size
Definition array-traits.h:29
T element_type
Definition array-traits.h:27
static const bool has_constant_size
Definition array-traits.h:28
static const T * base(const T &x)
Definition array-traits.h:30
static size_t size(const T &)
Definition array-traits.h:31
Definition cse.cc:4127
Definition hard-reg-set.h:383
static hashval_t hash(const simplifiable_subreg *)
Definition reginfo.cc:1176
static bool equal(const simplifiable_subreg *, const subreg_shape *)
Definition reginfo.cc:1184
const subreg_shape * compare_type
Definition hard-reg-set.h:384
Definition hard-reg-set.h:390
int x_reg_alloc_order[FIRST_PSEUDO_REGISTER]
Definition hard-reg-set.h:443
HARD_REG_SET x_fixed_nonglobal_reg_set
Definition hard-reg-set.h:429
char x_call_used_regs[FIRST_PSEUDO_REGISTER]
Definition hard-reg-set.h:413
char x_fixed_regs[FIRST_PSEUDO_REGISTER]
Definition hard-reg-set.h:404
int x_inv_reg_alloc_order[FIRST_PSEUDO_REGISTER]
Definition hard-reg-set.h:446
enum reg_class x_reg_class_subunion[N_REG_CLASSES][N_REG_CLASSES]
Definition hard-reg-set.h:463
bool x_class_only_fixed_regs[N_REG_CLASSES]
Definition hard-reg-set.h:453
unsigned int x_reg_class_size[N_REG_CLASSES]
Definition hard-reg-set.h:456
HARD_REG_SET x_reg_class_contents[N_REG_CLASSES]
Definition hard-reg-set.h:449
const char * x_reg_names[FIRST_PSEUDO_REGISTER]
Definition hard-reg-set.h:470
HARD_REG_SET x_accessible_reg_set
Definition hard-reg-set.h:394
hash_table< simplifiable_subregs_hasher > * x_simplifiable_subregs
Definition hard-reg-set.h:474
HARD_REG_SET x_savable_regs
Definition hard-reg-set.h:423
HARD_REG_SET x_eh_return_data_regs
Definition hard-reg-set.h:440
enum reg_class x_reg_class_subclasses[N_REG_CLASSES][N_REG_CLASSES]
Definition hard-reg-set.h:459
HARD_REG_SET x_operand_reg_set
Definition hard-reg-set.h:398
enum reg_class x_reg_class_superunion[N_REG_CLASSES][N_REG_CLASSES]
Definition hard-reg-set.h:467
void finalize()
Definition reginfo.cc:1354
HARD_REG_SET x_fixed_reg_set
Definition hard-reg-set.h:407
HARD_REG_SET x_regs_invalidated_by_call
Definition hard-reg-set.h:437
const T2 & y
Definition wide-int.h:3870