1 /* Compute different info about registers.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996
3 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This file contains regscan pass of the compiler and passes for
24 dealing with info about modes of pseudo-registers inside
25 subregisters. It also defines some tables of information about the
26 hardware registers, function init_reg_sets to initialize the
27 tables, and other auxiliary functions to deal with info about
28 registers and their classes. */
32 #include "coretypes.h"
34 #include "hard-reg-set.h"
39 #include "basic-block.h"
41 #include "addresses.h"
43 #include "insn-config.h"
53 #include "tree-pass.h"
57 /* Maximum register number used in this function, plus one. */
62 /* Register tables used by many passes. */
64 /* Indexed by hard register number, contains 1 for registers
65 that are fixed use (stack pointer, pc, frame pointer, etc.).
66 These are the registers that cannot be used to allocate
67 a pseudo reg for general use. */
68 char fixed_regs
[FIRST_PSEUDO_REGISTER
];
70 /* Same info as a HARD_REG_SET. */
71 HARD_REG_SET fixed_reg_set
;
73 /* Data for initializing the above. */
74 static const char initial_fixed_regs
[] = FIXED_REGISTERS
;
76 /* Indexed by hard register number, contains 1 for registers
77 that are fixed use or are clobbered by function calls.
78 These are the registers that cannot be used to allocate
79 a pseudo reg whose life crosses calls unless we are able
80 to save/restore them across the calls. */
81 char call_used_regs
[FIRST_PSEUDO_REGISTER
];
83 /* Same info as a HARD_REG_SET. */
84 HARD_REG_SET call_used_reg_set
;
86 /* Data for initializing the above. */
87 static const char initial_call_used_regs
[] = CALL_USED_REGISTERS
;
89 /* This is much like call_used_regs, except it doesn't have to
90 be a superset of FIXED_REGISTERS. This vector indicates
91 what is really call clobbered, and is used when defining
92 regs_invalidated_by_call. */
93 #ifdef CALL_REALLY_USED_REGISTERS
94 char call_really_used_regs
[] = CALL_REALLY_USED_REGISTERS
;
97 #ifdef CALL_REALLY_USED_REGISTERS
98 #define CALL_REALLY_USED_REGNO_P(X) call_really_used_regs[X]
100 #define CALL_REALLY_USED_REGNO_P(X) call_used_regs[X]
104 /* Indexed by hard register number, contains 1 for registers that are
105 fixed use or call used registers that cannot hold quantities across
106 calls even if we are willing to save and restore them. call fixed
107 registers are a subset of call used registers. */
108 char call_fixed_regs
[FIRST_PSEUDO_REGISTER
];
110 /* The same info as a HARD_REG_SET. */
111 HARD_REG_SET call_fixed_reg_set
;
113 /* Indexed by hard register number, contains 1 for registers
114 that are being used for global register decls.
115 These must be exempt from ordinary flow analysis
116 and are also considered fixed. */
117 char global_regs
[FIRST_PSEUDO_REGISTER
];
119 /* Contains 1 for registers that are set or clobbered by calls. */
120 /* ??? Ideally, this would be just call_used_regs plus global_regs, but
121 for someone's bright idea to have call_used_regs strictly include
122 fixed_regs. Which leaves us guessing as to the set of fixed_regs
123 that are actually preserved. We know for sure that those associated
124 with the local stack frame are safe, but scant others. */
125 HARD_REG_SET regs_invalidated_by_call
;
127 /* Same information as REGS_INVALIDATED_BY_CALL but in regset form to be used
128 in dataflow more conveniently. */
129 regset regs_invalidated_by_call_regset
;
131 /* The bitmap_obstack is used to hold some static variables that
132 should not be reset after each function is compiled. */
133 static bitmap_obstack persistent_obstack
;
135 /* Table of register numbers in the order in which to try to use them. */
136 #ifdef REG_ALLOC_ORDER
137 int reg_alloc_order
[FIRST_PSEUDO_REGISTER
] = REG_ALLOC_ORDER
;
139 /* The inverse of reg_alloc_order. */
140 int inv_reg_alloc_order
[FIRST_PSEUDO_REGISTER
];
143 /* For each reg class, a HARD_REG_SET saying which registers are in it. */
144 HARD_REG_SET reg_class_contents
[N_REG_CLASSES
];
146 /* The same information, but as an array of unsigned ints. We copy from
147 these unsigned ints to the table above. We do this so the tm.h files
148 do not have to be aware of the wordsize for machines with <= 64 regs.
149 Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
151 ((FIRST_PSEUDO_REGISTER + (32 - 1)) / 32)
153 static const unsigned int_reg_class_contents
[N_REG_CLASSES
][N_REG_INTS
]
154 = REG_CLASS_CONTENTS
;
156 /* For each reg class, number of regs it contains. */
157 unsigned int reg_class_size
[N_REG_CLASSES
];
159 /* For each reg class, table listing all the classes contained in it. */
160 enum reg_class reg_class_subclasses
[N_REG_CLASSES
][N_REG_CLASSES
];
162 /* For each pair of reg classes,
163 a largest reg class contained in their union. */
164 enum reg_class reg_class_subunion
[N_REG_CLASSES
][N_REG_CLASSES
];
166 /* For each pair of reg classes,
167 the smallest reg class containing their union. */
168 enum reg_class reg_class_superunion
[N_REG_CLASSES
][N_REG_CLASSES
];
170 /* Array containing all of the register names. */
171 const char * reg_names
[] = REGISTER_NAMES
;
173 /* Array containing all of the register class names. */
174 const char * reg_class_names
[] = REG_CLASS_NAMES
;
176 /* For each hard register, the widest mode object that it can contain.
177 This will be a MODE_INT mode if the register can hold integers. Otherwise
178 it will be a MODE_FLOAT or a MODE_CC mode, whichever is valid for the
180 enum machine_mode reg_raw_mode
[FIRST_PSEUDO_REGISTER
];
182 /* 1 if there is a register of given mode. */
183 bool have_regs_of_mode
[MAX_MACHINE_MODE
];
185 /* 1 if class does contain register of given mode. */
186 char contains_reg_of_mode
[N_REG_CLASSES
] [MAX_MACHINE_MODE
];
188 /* Maximum cost of moving from a register in one class to a register in
189 another class. Based on REGISTER_MOVE_COST. */
190 move_table
*move_cost
[MAX_MACHINE_MODE
];
192 /* Similar, but here we don't have to move if the first index is a subset
193 of the second so in that case the cost is zero. */
194 move_table
*may_move_in_cost
[MAX_MACHINE_MODE
];
196 /* Similar, but here we don't have to move if the first index is a superset
197 of the second so in that case the cost is zero. */
198 move_table
*may_move_out_cost
[MAX_MACHINE_MODE
];
200 /* Keep track of the last mode we initialized move costs for. */
201 static int last_mode_for_init_move_cost
;
203 /* Sample MEM values for use by memory_move_secondary_cost. */
204 static GTY(()) rtx top_of_stack
[MAX_MACHINE_MODE
];
206 /* No more global register variables may be declared; true once
207 reginfo has been initialized. */
208 static int no_global_reg_vars
= 0;
210 /* Specify number of hard registers given machine mode occupy. */
211 unsigned char hard_regno_nregs
[FIRST_PSEUDO_REGISTER
][MAX_MACHINE_MODE
];
213 /* Given a register bitmap, turn on the bits in a HARD_REG_SET that
214 correspond to the hard registers, if any, set in that map. This
215 could be done far more efficiently by having all sorts of special-cases
216 with moving single words, but probably isn't worth the trouble. */
218 reg_set_to_hard_reg_set (HARD_REG_SET
*to
, const_bitmap from
)
223 EXECUTE_IF_SET_IN_BITMAP (from
, 0, i
, bi
)
225 if (i
>= FIRST_PSEUDO_REGISTER
)
227 SET_HARD_REG_BIT (*to
, i
);
231 /* Function called only once to initialize the above data on reg usage.
232 Once this is done, various switches may override. */
238 /* First copy the register information from the initial int form into
241 for (i
= 0; i
< N_REG_CLASSES
; i
++)
243 CLEAR_HARD_REG_SET (reg_class_contents
[i
]);
245 /* Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
246 for (j
= 0; j
< FIRST_PSEUDO_REGISTER
; j
++)
247 if (int_reg_class_contents
[i
][j
/ 32]
248 & ((unsigned) 1 << (j
% 32)))
249 SET_HARD_REG_BIT (reg_class_contents
[i
], j
);
252 /* Sanity check: make sure the target macros FIXED_REGISTERS and
253 CALL_USED_REGISTERS had the right number of initializers. */
254 gcc_assert (sizeof fixed_regs
== sizeof initial_fixed_regs
);
255 gcc_assert (sizeof call_used_regs
== sizeof initial_call_used_regs
);
257 memcpy (fixed_regs
, initial_fixed_regs
, sizeof fixed_regs
);
258 memcpy (call_used_regs
, initial_call_used_regs
, sizeof call_used_regs
);
259 memset (global_regs
, 0, sizeof global_regs
);
262 /* Initialize may_move_cost and friends for mode M. */
264 init_move_cost (enum machine_mode m
)
266 static unsigned short last_move_cost
[N_REG_CLASSES
][N_REG_CLASSES
];
267 bool all_match
= true;
270 gcc_assert (have_regs_of_mode
[m
]);
271 for (i
= 0; i
< N_REG_CLASSES
; i
++)
272 if (contains_reg_of_mode
[i
][m
])
273 for (j
= 0; j
< N_REG_CLASSES
; j
++)
276 if (!contains_reg_of_mode
[j
][m
])
280 cost
= REGISTER_MOVE_COST (m
, i
, j
);
281 gcc_assert (cost
< 65535);
283 all_match
&= (last_move_cost
[i
][j
] == cost
);
284 last_move_cost
[i
][j
] = cost
;
286 if (all_match
&& last_mode_for_init_move_cost
!= -1)
288 move_cost
[m
] = move_cost
[last_mode_for_init_move_cost
];
289 may_move_in_cost
[m
] = may_move_in_cost
[last_mode_for_init_move_cost
];
290 may_move_out_cost
[m
] = may_move_out_cost
[last_mode_for_init_move_cost
];
293 last_mode_for_init_move_cost
= m
;
294 move_cost
[m
] = (move_table
*)xmalloc (sizeof (move_table
)
296 may_move_in_cost
[m
] = (move_table
*)xmalloc (sizeof (move_table
)
298 may_move_out_cost
[m
] = (move_table
*)xmalloc (sizeof (move_table
)
300 for (i
= 0; i
< N_REG_CLASSES
; i
++)
301 if (contains_reg_of_mode
[i
][m
])
302 for (j
= 0; j
< N_REG_CLASSES
; j
++)
305 enum reg_class
*p1
, *p2
;
307 if (last_move_cost
[i
][j
] == 65535)
309 move_cost
[m
][i
][j
] = 65535;
310 may_move_in_cost
[m
][i
][j
] = 65535;
311 may_move_out_cost
[m
][i
][j
] = 65535;
315 cost
= last_move_cost
[i
][j
];
317 for (p2
= ®_class_subclasses
[j
][0];
318 *p2
!= LIM_REG_CLASSES
; p2
++)
319 if (*p2
!= i
&& contains_reg_of_mode
[*p2
][m
])
320 cost
= MAX (cost
, move_cost
[m
][i
][*p2
]);
322 for (p1
= ®_class_subclasses
[i
][0];
323 *p1
!= LIM_REG_CLASSES
; p1
++)
324 if (*p1
!= j
&& contains_reg_of_mode
[*p1
][m
])
325 cost
= MAX (cost
, move_cost
[m
][*p1
][j
]);
327 gcc_assert (cost
<= 65535);
328 move_cost
[m
][i
][j
] = cost
;
330 if (reg_class_subset_p (i
, j
))
331 may_move_in_cost
[m
][i
][j
] = 0;
333 may_move_in_cost
[m
][i
][j
] = cost
;
335 if (reg_class_subset_p (j
, i
))
336 may_move_out_cost
[m
][i
][j
] = 0;
338 may_move_out_cost
[m
][i
][j
] = cost
;
342 for (j
= 0; j
< N_REG_CLASSES
; j
++)
344 move_cost
[m
][i
][j
] = 65535;
345 may_move_in_cost
[m
][i
][j
] = 65535;
346 may_move_out_cost
[m
][i
][j
] = 65535;
350 /* We need to save copies of some of the register information which
351 can be munged by command-line switches so we can restore it during
352 subsequent back-end reinitialization. */
353 static char saved_fixed_regs
[FIRST_PSEUDO_REGISTER
];
354 static char saved_call_used_regs
[FIRST_PSEUDO_REGISTER
];
355 #ifdef CALL_REALLY_USED_REGISTERS
356 static char saved_call_really_used_regs
[FIRST_PSEUDO_REGISTER
];
358 static const char *saved_reg_names
[FIRST_PSEUDO_REGISTER
];
360 /* Save the register information. */
362 save_register_info (void)
364 /* Sanity check: make sure the target macros FIXED_REGISTERS and
365 CALL_USED_REGISTERS had the right number of initializers. */
366 gcc_assert (sizeof fixed_regs
== sizeof saved_fixed_regs
);
367 gcc_assert (sizeof call_used_regs
== sizeof saved_call_used_regs
);
368 memcpy (saved_fixed_regs
, fixed_regs
, sizeof fixed_regs
);
369 memcpy (saved_call_used_regs
, call_used_regs
, sizeof call_used_regs
);
371 /* Likewise for call_really_used_regs. */
372 #ifdef CALL_REALLY_USED_REGISTERS
373 gcc_assert (sizeof call_really_used_regs
374 == sizeof saved_call_really_used_regs
);
375 memcpy (saved_call_really_used_regs
, call_really_used_regs
,
376 sizeof call_really_used_regs
);
379 /* And similarly for reg_names. */
380 gcc_assert (sizeof reg_names
== sizeof saved_reg_names
);
381 memcpy (saved_reg_names
, reg_names
, sizeof reg_names
);
384 /* Restore the register information. */
386 restore_register_info (void)
388 memcpy (fixed_regs
, saved_fixed_regs
, sizeof fixed_regs
);
389 memcpy (call_used_regs
, saved_call_used_regs
, sizeof call_used_regs
);
391 #ifdef CALL_REALLY_USED_REGISTERS
392 memcpy (call_really_used_regs
, saved_call_really_used_regs
,
393 sizeof call_really_used_regs
);
396 memcpy (reg_names
, saved_reg_names
, sizeof reg_names
);
399 /* After switches have been processed, which perhaps alter
400 `fixed_regs' and `call_used_regs', convert them to HARD_REG_SETs. */
402 init_reg_sets_1 (void)
405 unsigned int /* enum machine_mode */ m
;
407 restore_register_info ();
409 #ifdef REG_ALLOC_ORDER
410 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
411 inv_reg_alloc_order
[reg_alloc_order
[i
]] = i
;
414 /* This macro allows the fixed or call-used registers
415 and the register classes to depend on target flags. */
417 #ifdef CONDITIONAL_REGISTER_USAGE
418 CONDITIONAL_REGISTER_USAGE
;
421 /* Compute number of hard regs in each class. */
423 memset (reg_class_size
, 0, sizeof reg_class_size
);
424 for (i
= 0; i
< N_REG_CLASSES
; i
++)
425 for (j
= 0; j
< FIRST_PSEUDO_REGISTER
; j
++)
426 if (TEST_HARD_REG_BIT (reg_class_contents
[i
], j
))
429 /* Initialize the table of subunions.
430 reg_class_subunion[I][J] gets the largest-numbered reg-class
431 that is contained in the union of classes I and J. */
433 memset (reg_class_subunion
, 0, sizeof reg_class_subunion
);
434 for (i
= 0; i
< N_REG_CLASSES
; i
++)
436 for (j
= 0; j
< N_REG_CLASSES
; j
++)
441 COPY_HARD_REG_SET (c
, reg_class_contents
[i
]);
442 IOR_HARD_REG_SET (c
, reg_class_contents
[j
]);
443 for (k
= 0; k
< N_REG_CLASSES
; k
++)
444 if (hard_reg_set_subset_p (reg_class_contents
[k
], c
)
445 && !hard_reg_set_subset_p (reg_class_contents
[k
],
447 [(int) reg_class_subunion
[i
][j
]]))
448 reg_class_subunion
[i
][j
] = (enum reg_class
) k
;
452 /* Initialize the table of superunions.
453 reg_class_superunion[I][J] gets the smallest-numbered reg-class
454 containing the union of classes I and J. */
456 memset (reg_class_superunion
, 0, sizeof reg_class_superunion
);
457 for (i
= 0; i
< N_REG_CLASSES
; i
++)
459 for (j
= 0; j
< N_REG_CLASSES
; j
++)
464 COPY_HARD_REG_SET (c
, reg_class_contents
[i
]);
465 IOR_HARD_REG_SET (c
, reg_class_contents
[j
]);
466 for (k
= 0; k
< N_REG_CLASSES
; k
++)
467 if (hard_reg_set_subset_p (c
, reg_class_contents
[k
]))
470 reg_class_superunion
[i
][j
] = (enum reg_class
) k
;
474 /* Initialize the tables of subclasses and superclasses of each reg class.
475 First clear the whole table, then add the elements as they are found. */
477 for (i
= 0; i
< N_REG_CLASSES
; i
++)
479 for (j
= 0; j
< N_REG_CLASSES
; j
++)
480 reg_class_subclasses
[i
][j
] = LIM_REG_CLASSES
;
483 for (i
= 0; i
< N_REG_CLASSES
; i
++)
485 if (i
== (int) NO_REGS
)
488 for (j
= i
+ 1; j
< N_REG_CLASSES
; j
++)
489 if (hard_reg_set_subset_p (reg_class_contents
[i
],
490 reg_class_contents
[j
]))
492 /* Reg class I is a subclass of J.
493 Add J to the table of superclasses of I. */
496 /* Add I to the table of superclasses of J. */
497 p
= ®_class_subclasses
[j
][0];
498 while (*p
!= LIM_REG_CLASSES
) p
++;
499 *p
= (enum reg_class
) i
;
503 /* Initialize "constant" tables. */
505 CLEAR_HARD_REG_SET (fixed_reg_set
);
506 CLEAR_HARD_REG_SET (call_used_reg_set
);
507 CLEAR_HARD_REG_SET (call_fixed_reg_set
);
508 CLEAR_HARD_REG_SET (regs_invalidated_by_call
);
509 if (!regs_invalidated_by_call_regset
)
511 bitmap_obstack_initialize (&persistent_obstack
);
512 regs_invalidated_by_call_regset
= ALLOC_REG_SET (&persistent_obstack
);
515 CLEAR_REG_SET (regs_invalidated_by_call_regset
);
517 memcpy (call_fixed_regs
, fixed_regs
, sizeof call_fixed_regs
);
519 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
521 /* call_used_regs must include fixed_regs. */
522 gcc_assert (!fixed_regs
[i
] || call_used_regs
[i
]);
523 #ifdef CALL_REALLY_USED_REGISTERS
524 /* call_used_regs must include call_really_used_regs. */
525 gcc_assert (!call_really_used_regs
[i
] || call_used_regs
[i
]);
529 SET_HARD_REG_BIT (fixed_reg_set
, i
);
531 if (call_used_regs
[i
])
532 SET_HARD_REG_BIT (call_used_reg_set
, i
);
533 if (call_fixed_regs
[i
])
534 SET_HARD_REG_BIT (call_fixed_reg_set
, i
);
536 /* There are a couple of fixed registers that we know are safe to
537 exclude from being clobbered by calls:
539 The frame pointer is always preserved across calls. The arg pointer
540 is if it is fixed. The stack pointer usually is, unless
541 RETURN_POPS_ARGS, in which case an explicit CLOBBER will be present.
542 If we are generating PIC code, the PIC offset table register is
543 preserved across calls, though the target can override that. */
545 if (i
== STACK_POINTER_REGNUM
)
547 else if (global_regs
[i
])
549 SET_HARD_REG_BIT (regs_invalidated_by_call
, i
);
550 SET_REGNO_REG_SET (regs_invalidated_by_call_regset
, i
);
552 else if (i
== FRAME_POINTER_REGNUM
)
554 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
555 else if (i
== HARD_FRAME_POINTER_REGNUM
)
558 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
559 else if (i
== ARG_POINTER_REGNUM
&& fixed_regs
[i
])
562 #ifndef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
563 else if (i
== (unsigned) PIC_OFFSET_TABLE_REGNUM
&& fixed_regs
[i
])
566 else if (CALL_REALLY_USED_REGNO_P (i
))
568 SET_HARD_REG_BIT (regs_invalidated_by_call
, i
);
569 SET_REGNO_REG_SET (regs_invalidated_by_call_regset
, i
);
573 /* Preserve global registers if called more than once. */
574 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
578 fixed_regs
[i
] = call_used_regs
[i
] = call_fixed_regs
[i
] = 1;
579 SET_HARD_REG_BIT (fixed_reg_set
, i
);
580 SET_HARD_REG_BIT (call_used_reg_set
, i
);
581 SET_HARD_REG_BIT (call_fixed_reg_set
, i
);
585 memset (have_regs_of_mode
, 0, sizeof (have_regs_of_mode
));
586 memset (contains_reg_of_mode
, 0, sizeof (contains_reg_of_mode
));
587 for (m
= 0; m
< (unsigned int) MAX_MACHINE_MODE
; m
++)
589 HARD_REG_SET ok_regs
;
590 CLEAR_HARD_REG_SET (ok_regs
);
591 for (j
= 0; j
< FIRST_PSEUDO_REGISTER
; j
++)
592 if (!fixed_regs
[j
] && HARD_REGNO_MODE_OK (j
, m
))
593 SET_HARD_REG_BIT (ok_regs
, j
);
595 for (i
= 0; i
< N_REG_CLASSES
; i
++)
596 if ((unsigned) CLASS_MAX_NREGS (i
, m
) <= reg_class_size
[i
]
597 && hard_reg_set_intersect_p (ok_regs
, reg_class_contents
[i
]))
599 contains_reg_of_mode
[i
][m
] = 1;
600 have_regs_of_mode
[m
] = 1;
604 /* Reset move_cost and friends, making sure we only free shared
605 table entries once. */
606 for (i
= 0; i
< MAX_MACHINE_MODE
; i
++)
609 for (j
= 0; j
< i
&& move_cost
[i
] != move_cost
[j
]; j
++)
614 free (may_move_in_cost
[i
]);
615 free (may_move_out_cost
[i
]);
618 memset (move_cost
, 0, sizeof move_cost
);
619 memset (may_move_in_cost
, 0, sizeof may_move_in_cost
);
620 memset (may_move_out_cost
, 0, sizeof may_move_out_cost
);
621 last_mode_for_init_move_cost
= -1;
624 /* Compute the table of register modes.
625 These values are used to record death information for individual registers
626 (as opposed to a multi-register mode).
627 This function might be invoked more than once, if the target has support
628 for changing register usage conventions on a per-function basis.
631 init_reg_modes_target (void)
635 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
636 for (j
= 0; j
< MAX_MACHINE_MODE
; j
++)
637 hard_regno_nregs
[i
][j
] = HARD_REGNO_NREGS(i
, (enum machine_mode
)j
);
639 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
641 reg_raw_mode
[i
] = choose_hard_reg_mode (i
, 1, false);
643 /* If we couldn't find a valid mode, just use the previous mode.
644 ??? One situation in which we need to do this is on the mips where
645 HARD_REGNO_NREGS (fpreg, [SD]Fmode) returns 2. Ideally we'd like
646 to use DF mode for the even registers and VOIDmode for the odd
647 (for the cpu models where the odd ones are inaccessible). */
648 if (reg_raw_mode
[i
] == VOIDmode
)
649 reg_raw_mode
[i
] = i
== 0 ? word_mode
: reg_raw_mode
[i
-1];
653 /* Finish initializing the register sets and initialize the register modes.
654 This function might be invoked more than once, if the target has support
655 for changing register usage conventions on a per-function basis.
660 /* This finishes what was started by init_reg_sets, but couldn't be done
661 until after register usage was specified. */
665 /* The same as previous function plus initializing IRA. */
673 /* Initialize some fake stack-frame MEM references for use in
674 memory_move_secondary_cost. */
676 init_fake_stack_mems (void)
680 for (i
= 0; i
< MAX_MACHINE_MODE
; i
++)
681 top_of_stack
[i
] = gen_rtx_MEM (i
, stack_pointer_rtx
);
685 /* Compute extra cost of moving registers to/from memory due to reloads.
686 Only needed if secondary reloads are required for memory moves. */
688 memory_move_secondary_cost (enum machine_mode mode
, enum reg_class rclass
,
691 enum reg_class altclass
;
692 int partial_cost
= 0;
693 /* We need a memory reference to feed to SECONDARY... macros. */
694 /* mem may be unused even if the SECONDARY_ macros are defined. */
695 rtx mem ATTRIBUTE_UNUSED
= top_of_stack
[(int) mode
];
697 altclass
= secondary_reload_class (in
? 1 : 0, rclass
, mode
, mem
);
699 if (altclass
== NO_REGS
)
703 partial_cost
= REGISTER_MOVE_COST (mode
, altclass
, rclass
);
705 partial_cost
= REGISTER_MOVE_COST (mode
, rclass
, altclass
);
707 if (rclass
== altclass
)
708 /* This isn't simply a copy-to-temporary situation. Can't guess
709 what it is, so MEMORY_MOVE_COST really ought not to be calling
712 I'm tempted to put in an assert here, but returning this will
713 probably only give poor estimates, which is what we would've
714 had before this code anyways. */
717 /* Check if the secondary reload register will also need a
719 return memory_move_secondary_cost (mode
, altclass
, in
) + partial_cost
;
722 /* Return a machine mode that is legitimate for hard reg REGNO and large
723 enough to save nregs. If we can't find one, return VOIDmode.
724 If CALL_SAVED is true, only consider modes that are call saved. */
726 choose_hard_reg_mode (unsigned int regno ATTRIBUTE_UNUSED
,
727 unsigned int nregs
, bool call_saved
)
729 unsigned int /* enum machine_mode */ m
;
730 enum machine_mode found_mode
= VOIDmode
, mode
;
732 /* We first look for the largest integer mode that can be validly
733 held in REGNO. If none, we look for the largest floating-point mode.
734 If we still didn't find a valid mode, try CCmode. */
736 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
738 mode
= GET_MODE_WIDER_MODE (mode
))
739 if ((unsigned) hard_regno_nregs
[regno
][mode
] == nregs
740 && HARD_REGNO_MODE_OK (regno
, mode
)
741 && (! call_saved
|| ! HARD_REGNO_CALL_PART_CLOBBERED (regno
, mode
)))
744 if (found_mode
!= VOIDmode
)
747 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_FLOAT
);
749 mode
= GET_MODE_WIDER_MODE (mode
))
750 if ((unsigned) hard_regno_nregs
[regno
][mode
] == nregs
751 && HARD_REGNO_MODE_OK (regno
, mode
)
752 && (! call_saved
|| ! HARD_REGNO_CALL_PART_CLOBBERED (regno
, mode
)))
755 if (found_mode
!= VOIDmode
)
758 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT
);
760 mode
= GET_MODE_WIDER_MODE (mode
))
761 if ((unsigned) hard_regno_nregs
[regno
][mode
] == nregs
762 && HARD_REGNO_MODE_OK (regno
, mode
)
763 && (! call_saved
|| ! HARD_REGNO_CALL_PART_CLOBBERED (regno
, mode
)))
766 if (found_mode
!= VOIDmode
)
769 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT
);
771 mode
= GET_MODE_WIDER_MODE (mode
))
772 if ((unsigned) hard_regno_nregs
[regno
][mode
] == nregs
773 && HARD_REGNO_MODE_OK (regno
, mode
)
774 && (! call_saved
|| ! HARD_REGNO_CALL_PART_CLOBBERED (regno
, mode
)))
777 if (found_mode
!= VOIDmode
)
780 /* Iterate over all of the CCmodes. */
781 for (m
= (unsigned int) CCmode
; m
< (unsigned int) NUM_MACHINE_MODES
; ++m
)
783 mode
= (enum machine_mode
) m
;
784 if ((unsigned) hard_regno_nregs
[regno
][mode
] == nregs
785 && HARD_REGNO_MODE_OK (regno
, mode
)
786 && (! call_saved
|| ! HARD_REGNO_CALL_PART_CLOBBERED (regno
, mode
)))
790 /* We can't find a mode valid for this register. */
794 /* Specify the usage characteristics of the register named NAME.
795 It should be a fixed register if FIXED and a
796 call-used register if CALL_USED. */
798 fix_register (const char *name
, int fixed
, int call_used
)
802 /* Decode the name and update the primary form of
803 the register info. */
805 if ((i
= decode_reg_name (name
)) >= 0)
807 if ((i
== STACK_POINTER_REGNUM
808 #ifdef HARD_FRAME_POINTER_REGNUM
809 || i
== HARD_FRAME_POINTER_REGNUM
811 || i
== FRAME_POINTER_REGNUM
814 && (fixed
== 0 || call_used
== 0))
816 static const char * const what_option
[2][2] = {
817 { "call-saved", "call-used" },
818 { "no-such-option", "fixed" }};
820 error ("can't use '%s' as a %s register", name
,
821 what_option
[fixed
][call_used
]);
825 fixed_regs
[i
] = fixed
;
826 call_used_regs
[i
] = call_used
;
827 #ifdef CALL_REALLY_USED_REGISTERS
829 call_really_used_regs
[i
] = call_used
;
835 warning (0, "unknown register name: %s", name
);
839 /* Mark register number I as global. */
841 globalize_reg (int i
)
843 if (fixed_regs
[i
] == 0 && no_global_reg_vars
)
844 error ("global register variable follows a function definition");
848 warning (0, "register used for two global register variables");
852 if (call_used_regs
[i
] && ! fixed_regs
[i
])
853 warning (0, "call-clobbered register used for global register variable");
857 /* If we're globalizing the frame pointer, we need to set the
858 appropriate regs_invalidated_by_call bit, even if it's already
859 set in fixed_regs. */
860 if (i
!= STACK_POINTER_REGNUM
)
862 SET_HARD_REG_BIT (regs_invalidated_by_call
, i
);
863 SET_REGNO_REG_SET (regs_invalidated_by_call_regset
, i
);
866 /* If already fixed, nothing else to do. */
870 fixed_regs
[i
] = call_used_regs
[i
] = call_fixed_regs
[i
] = 1;
871 #ifdef CALL_REALLY_USED_REGISTERS
872 call_really_used_regs
[i
] = 1;
875 SET_HARD_REG_BIT (fixed_reg_set
, i
);
876 SET_HARD_REG_BIT (call_used_reg_set
, i
);
877 SET_HARD_REG_BIT (call_fixed_reg_set
, i
);
881 /* Structure used to record preferences of given pseudo. */
884 /* (enum reg_class) prefclass is the preferred class. May be
885 NO_REGS if no class is better than memory. */
888 /* altclass is a register class that we should use for allocating
889 pseudo if no register in the preferred class is available.
890 If no register in this class is available, memory is preferred.
892 It might appear to be more general to have a bitmask of classes here,
893 but since it is recommended that there be a class corresponding to the
894 union of most major pair of classes, that generality is not required. */
898 /* Record preferences of each pseudo. This is available after RA is
900 static struct reg_pref
*reg_pref
;
902 /* Return the reg_class in which pseudo reg number REGNO is best allocated.
903 This function is sometimes called before the info has been computed.
904 When that happens, just return GENERAL_REGS, which is innocuous. */
906 reg_preferred_class (int regno
)
911 return (enum reg_class
) reg_pref
[regno
].prefclass
;
915 reg_alternate_class (int regno
)
920 return (enum reg_class
) reg_pref
[regno
].altclass
;
923 /* Initialize some global data for this pass. */
928 df_compute_regs_ever_live (true);
930 /* This prevents dump_flow_info from losing if called
931 before reginfo is run. */
934 /* No more global register variables may be declared. */
935 no_global_reg_vars
= 1;
939 struct rtl_opt_pass pass_reginfo_init
=
943 "reginfo", /* name */
945 reginfo_init
, /* execute */
948 0, /* static_pass_number */
950 0, /* properties_required */
951 0, /* properties_provided */
952 0, /* properties_destroyed */
953 0, /* todo_flags_start */
954 0 /* todo_flags_finish */
960 /* Allocate space for reg info. */
962 allocate_reg_info (void)
964 int size
= max_reg_num ();
966 gcc_assert (! reg_pref
&& ! reg_renumber
);
967 reg_renumber
= XNEWVEC (short, size
);
968 reg_pref
= XCNEWVEC (struct reg_pref
, size
);
969 memset (reg_renumber
, -1, size
* sizeof (short));
973 /* Resize reg info. The new elements will be uninitialized. */
975 resize_reg_info (void)
977 int size
= max_reg_num ();
979 gcc_assert (reg_pref
&& reg_renumber
);
980 reg_renumber
= XRESIZEVEC (short, reg_renumber
, size
);
981 reg_pref
= XRESIZEVEC (struct reg_pref
, reg_pref
, size
);
985 /* Free up the space allocated by allocate_reg_info. */
1005 /* Set up preferred and alternate classes for REGNO as PREFCLASS and
1008 setup_reg_classes (int regno
,
1009 enum reg_class prefclass
, enum reg_class altclass
)
1011 if (reg_pref
== NULL
)
1013 reg_pref
[regno
].prefclass
= prefclass
;
1014 reg_pref
[regno
].altclass
= altclass
;
1018 /* This is the `regscan' pass of the compiler, run just before cse and
1019 again just before loop. It finds the first and last use of each
1022 static void reg_scan_mark_refs (rtx
, rtx
);
1025 reg_scan (rtx f
, unsigned int nregs ATTRIBUTE_UNUSED
)
1029 timevar_push (TV_REG_SCAN
);
1031 for (insn
= f
; insn
; insn
= NEXT_INSN (insn
))
1034 reg_scan_mark_refs (PATTERN (insn
), insn
);
1035 if (REG_NOTES (insn
))
1036 reg_scan_mark_refs (REG_NOTES (insn
), insn
);
1039 timevar_pop (TV_REG_SCAN
);
1043 /* X is the expression to scan. INSN is the insn it appears in.
1044 NOTE_FLAG is nonzero if X is from INSN's notes rather than its body.
1045 We should only record information for REGs with numbers
1046 greater than or equal to MIN_REGNO. */
1048 reg_scan_mark_refs (rtx x
, rtx insn
)
1056 code
= GET_CODE (x
);
1075 reg_scan_mark_refs (XEXP (x
, 0), insn
);
1077 reg_scan_mark_refs (XEXP (x
, 1), insn
);
1082 reg_scan_mark_refs (XEXP (x
, 1), insn
);
1086 if (MEM_P (XEXP (x
, 0)))
1087 reg_scan_mark_refs (XEXP (XEXP (x
, 0), 0), insn
);
1091 /* Count a set of the destination if it is a register. */
1092 for (dest
= SET_DEST (x
);
1093 GET_CODE (dest
) == SUBREG
|| GET_CODE (dest
) == STRICT_LOW_PART
1094 || GET_CODE (dest
) == ZERO_EXTEND
;
1095 dest
= XEXP (dest
, 0))
1098 /* If this is setting a pseudo from another pseudo or the sum of a
1099 pseudo and a constant integer and the other pseudo is known to be
1100 a pointer, set the destination to be a pointer as well.
1102 Likewise if it is setting the destination from an address or from a
1103 value equivalent to an address or to the sum of an address and
1106 But don't do any of this if the pseudo corresponds to a user
1107 variable since it should have already been set as a pointer based
1110 if (REG_P (SET_DEST (x
))
1111 && REGNO (SET_DEST (x
)) >= FIRST_PSEUDO_REGISTER
1112 /* If the destination pseudo is set more than once, then other
1113 sets might not be to a pointer value (consider access to a
1114 union in two threads of control in the presence of global
1115 optimizations). So only set REG_POINTER on the destination
1116 pseudo if this is the only set of that pseudo. */
1117 && DF_REG_DEF_COUNT (REGNO (SET_DEST (x
))) == 1
1118 && ! REG_USERVAR_P (SET_DEST (x
))
1119 && ! REG_POINTER (SET_DEST (x
))
1120 && ((REG_P (SET_SRC (x
))
1121 && REG_POINTER (SET_SRC (x
)))
1122 || ((GET_CODE (SET_SRC (x
)) == PLUS
1123 || GET_CODE (SET_SRC (x
)) == LO_SUM
)
1124 && GET_CODE (XEXP (SET_SRC (x
), 1)) == CONST_INT
1125 && REG_P (XEXP (SET_SRC (x
), 0))
1126 && REG_POINTER (XEXP (SET_SRC (x
), 0)))
1127 || GET_CODE (SET_SRC (x
)) == CONST
1128 || GET_CODE (SET_SRC (x
)) == SYMBOL_REF
1129 || GET_CODE (SET_SRC (x
)) == LABEL_REF
1130 || (GET_CODE (SET_SRC (x
)) == HIGH
1131 && (GET_CODE (XEXP (SET_SRC (x
), 0)) == CONST
1132 || GET_CODE (XEXP (SET_SRC (x
), 0)) == SYMBOL_REF
1133 || GET_CODE (XEXP (SET_SRC (x
), 0)) == LABEL_REF
))
1134 || ((GET_CODE (SET_SRC (x
)) == PLUS
1135 || GET_CODE (SET_SRC (x
)) == LO_SUM
)
1136 && (GET_CODE (XEXP (SET_SRC (x
), 1)) == CONST
1137 || GET_CODE (XEXP (SET_SRC (x
), 1)) == SYMBOL_REF
1138 || GET_CODE (XEXP (SET_SRC (x
), 1)) == LABEL_REF
))
1139 || ((note
= find_reg_note (insn
, REG_EQUAL
, 0)) != 0
1140 && (GET_CODE (XEXP (note
, 0)) == CONST
1141 || GET_CODE (XEXP (note
, 0)) == SYMBOL_REF
1142 || GET_CODE (XEXP (note
, 0)) == LABEL_REF
))))
1143 REG_POINTER (SET_DEST (x
)) = 1;
1145 /* If this is setting a register from a register or from a simple
1146 conversion of a register, propagate REG_EXPR. */
1147 if (REG_P (dest
) && !REG_ATTRS (dest
))
1149 rtx src
= SET_SRC (x
);
1151 while (GET_CODE (src
) == SIGN_EXTEND
1152 || GET_CODE (src
) == ZERO_EXTEND
1153 || GET_CODE (src
) == TRUNCATE
1154 || (GET_CODE (src
) == SUBREG
&& subreg_lowpart_p (src
)))
1155 src
= XEXP (src
, 0);
1157 set_reg_attrs_from_value (dest
, src
);
1160 /* ... fall through ... */
1164 const char *fmt
= GET_RTX_FORMAT (code
);
1166 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1169 reg_scan_mark_refs (XEXP (x
, i
), insn
);
1170 else if (fmt
[i
] == 'E' && XVEC (x
, i
) != 0)
1173 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
1174 reg_scan_mark_refs (XVECEXP (x
, i
, j
), insn
);
1182 /* Return nonzero if C1 is a subset of C2, i.e., if every register in C1
1185 reg_class_subset_p (enum reg_class c1
, enum reg_class c2
)
1189 || hard_reg_set_subset_p (reg_class_contents
[(int) c1
],
1190 reg_class_contents
[(int) c2
]));
1193 /* Return nonzero if there is a register that is in both C1 and C2. */
1195 reg_classes_intersect_p (enum reg_class c1
, enum reg_class c2
)
1200 || hard_reg_set_intersect_p (reg_class_contents
[(int) c1
],
1201 reg_class_contents
[(int) c2
]));
1206 /* Passes for keeping and updating info about modes of registers
1207 inside subregisters. */
1209 #ifdef CANNOT_CHANGE_MODE_CLASS
1211 struct subregs_of_mode_node
1214 unsigned char modes
[MAX_MACHINE_MODE
];
1217 static htab_t subregs_of_mode
;
1220 som_hash (const void *x
)
1222 const struct subregs_of_mode_node
*const a
=
1223 (const struct subregs_of_mode_node
*) x
;
1228 som_eq (const void *x
, const void *y
)
1230 const struct subregs_of_mode_node
*const a
=
1231 (const struct subregs_of_mode_node
*) x
;
1232 const struct subregs_of_mode_node
*const b
=
1233 (const struct subregs_of_mode_node
*) y
;
1234 return a
->block
== b
->block
;
1238 record_subregs_of_mode (rtx subreg
)
1240 struct subregs_of_mode_node dummy
, *node
;
1241 enum machine_mode mode
;
1245 if (!REG_P (SUBREG_REG (subreg
)))
1248 regno
= REGNO (SUBREG_REG (subreg
));
1249 mode
= GET_MODE (subreg
);
1251 if (regno
< FIRST_PSEUDO_REGISTER
)
1254 dummy
.block
= regno
& -8;
1255 slot
= htab_find_slot_with_hash (subregs_of_mode
, &dummy
,
1256 dummy
.block
, INSERT
);
1257 node
= (struct subregs_of_mode_node
*) *slot
;
1260 node
= XCNEW (struct subregs_of_mode_node
);
1261 node
->block
= regno
& -8;
1265 node
->modes
[mode
] |= 1 << (regno
& 7);
1268 /* Call record_subregs_of_mode for all the subregs in X. */
1270 find_subregs_of_mode (rtx x
)
1272 enum rtx_code code
= GET_CODE (x
);
1273 const char * const fmt
= GET_RTX_FORMAT (code
);
1277 record_subregs_of_mode (x
);
1279 /* Time for some deep diving. */
1280 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1283 find_subregs_of_mode (XEXP (x
, i
));
1284 else if (fmt
[i
] == 'E')
1287 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
1288 find_subregs_of_mode (XVECEXP (x
, i
, j
));
1294 init_subregs_of_mode (void)
1299 if (subregs_of_mode
)
1300 htab_empty (subregs_of_mode
);
1302 subregs_of_mode
= htab_create (100, som_hash
, som_eq
, free
);
1305 FOR_BB_INSNS (bb
, insn
)
1307 find_subregs_of_mode (PATTERN (insn
));
1312 /* Set bits in *USED which correspond to registers which can't change
1313 their mode from FROM to any mode in which REGNO was
1316 cannot_change_mode_set_regs (HARD_REG_SET
*used
, enum machine_mode from
,
1319 struct subregs_of_mode_node dummy
, *node
;
1320 enum machine_mode to
;
1324 gcc_assert (subregs_of_mode
);
1325 dummy
.block
= regno
& -8;
1326 node
= (struct subregs_of_mode_node
*)
1327 htab_find_with_hash (subregs_of_mode
, &dummy
, dummy
.block
);
1331 mask
= 1 << (regno
& 7);
1332 for (to
= VOIDmode
; to
< NUM_MACHINE_MODES
; to
++)
1333 if (node
->modes
[to
] & mask
)
1334 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
1335 if (!TEST_HARD_REG_BIT (*used
, i
)
1336 && REG_CANNOT_CHANGE_MODE_P (i
, from
, to
))
1337 SET_HARD_REG_BIT (*used
, i
);
1340 /* Return 1 if REGNO has had an invalid mode change in CLASS from FROM
1343 invalid_mode_change_p (unsigned int regno
,
1344 enum reg_class rclass ATTRIBUTE_UNUSED
,
1345 enum machine_mode from
)
1347 struct subregs_of_mode_node dummy
, *node
;
1348 enum machine_mode to
;
1351 gcc_assert (subregs_of_mode
);
1352 dummy
.block
= regno
& -8;
1353 node
= (struct subregs_of_mode_node
*)
1354 htab_find_with_hash (subregs_of_mode
, &dummy
, dummy
.block
);
1358 mask
= 1 << (regno
& 7);
1359 for (to
= VOIDmode
; to
< NUM_MACHINE_MODES
; to
++)
1360 if (node
->modes
[to
] & mask
)
1361 if (CANNOT_CHANGE_MODE_CLASS (from
, to
, rclass
))
1368 finish_subregs_of_mode (void)
1370 htab_delete (subregs_of_mode
);
1371 subregs_of_mode
= 0;
1376 init_subregs_of_mode (void)
1381 finish_subregs_of_mode (void)
1386 #endif /* CANNOT_CHANGE_MODE_CLASS */
1389 gate_subregs_of_mode_init (void)
1391 #ifdef CANNOT_CHANGE_MODE_CLASS
1398 struct rtl_opt_pass pass_subregs_of_mode_init
=
1402 "subregs_of_mode_init", /* name */
1403 gate_subregs_of_mode_init
, /* gate */
1404 init_subregs_of_mode
, /* execute */
1407 0, /* static_pass_number */
1409 0, /* properties_required */
1410 0, /* properties_provided */
1411 0, /* properties_destroyed */
1412 0, /* todo_flags_start */
1413 0 /* todo_flags_finish */
1417 struct rtl_opt_pass pass_subregs_of_mode_finish
=
1421 "subregs_of_mode_finish", /* name */
1422 gate_subregs_of_mode_init
, /* gate */
1423 finish_subregs_of_mode
, /* execute */
1426 0, /* static_pass_number */
1428 0, /* properties_required */
1429 0, /* properties_provided */
1430 0, /* properties_destroyed */
1431 0, /* todo_flags_start */
1432 0 /* todo_flags_finish */
1437 #include "gt-reginfo.h"