Fix typo.
[official-gcc.git] / gcc / regclass.c
blobf31ccd15e4026ddf4213c65b25abf6caeb6ed67c
1 /* Compute register class preferences for pseudo-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
11 version.
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
16 for more details.
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 two passes of the compiler: reg_scan and reg_class.
24 It also defines some tables of information about the hardware registers
25 and a function init_reg_sets to initialize the tables. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "hard-reg-set.h"
32 #include "rtl.h"
33 #include "expr.h"
34 #include "tm_p.h"
35 #include "flags.h"
36 #include "basic-block.h"
37 #include "regs.h"
38 #include "addresses.h"
39 #include "function.h"
40 #include "insn-config.h"
41 #include "recog.h"
42 #include "reload.h"
43 #include "real.h"
44 #include "toplev.h"
45 #include "output.h"
46 #include "ggc.h"
47 #include "timevar.h"
48 #include "hashtab.h"
49 #include "target.h"
50 #include "tree-pass.h"
51 #include "df.h"
53 /* Maximum register number used in this function, plus one. */
55 int max_regno;
57 static void init_reg_sets_1 (void);
58 static void init_reg_autoinc (void);
60 /* If we have auto-increment or auto-decrement and we can have secondary
61 reloads, we are not allowed to use classes requiring secondary
62 reloads for pseudos auto-incremented since reload can't handle it. */
63 /* We leave it to target hooks to decide if we have secondary reloads, so
64 assume that we might have them. */
65 #if defined(AUTO_INC_DEC) /* */
66 #define FORBIDDEN_INC_DEC_CLASSES
67 #endif
69 /* Register tables used by many passes. */
71 /* Indexed by hard register number, contains 1 for registers
72 that are fixed use (stack pointer, pc, frame pointer, etc.).
73 These are the registers that cannot be used to allocate
74 a pseudo reg for general use. */
76 char fixed_regs[FIRST_PSEUDO_REGISTER];
78 /* Same info as a HARD_REG_SET. */
80 HARD_REG_SET fixed_reg_set;
82 /* Data for initializing the above. */
84 static const char initial_fixed_regs[] = FIXED_REGISTERS;
86 /* Indexed by hard register number, contains 1 for registers
87 that are fixed use or are clobbered by function calls.
88 These are the registers that cannot be used to allocate
89 a pseudo reg whose life crosses calls unless we are able
90 to save/restore them across the calls. */
92 char call_used_regs[FIRST_PSEUDO_REGISTER];
94 /* Same info as a HARD_REG_SET. */
96 HARD_REG_SET call_used_reg_set;
98 /* HARD_REG_SET of registers we want to avoid caller saving. */
99 HARD_REG_SET losing_caller_save_reg_set;
101 /* Data for initializing the above. */
103 static const char initial_call_used_regs[] = CALL_USED_REGISTERS;
105 /* This is much like call_used_regs, except it doesn't have to
106 be a superset of FIXED_REGISTERS. This vector indicates
107 what is really call clobbered, and is used when defining
108 regs_invalidated_by_call. */
110 #ifdef CALL_REALLY_USED_REGISTERS
111 char call_really_used_regs[] = CALL_REALLY_USED_REGISTERS;
112 #endif
114 #ifdef CALL_REALLY_USED_REGISTERS
115 #define CALL_REALLY_USED_REGNO_P(X) call_really_used_regs[X]
116 #else
117 #define CALL_REALLY_USED_REGNO_P(X) call_used_regs[X]
118 #endif
121 /* Indexed by hard register number, contains 1 for registers that are
122 fixed use or call used registers that cannot hold quantities across
123 calls even if we are willing to save and restore them. call fixed
124 registers are a subset of call used registers. */
126 char call_fixed_regs[FIRST_PSEUDO_REGISTER];
128 /* The same info as a HARD_REG_SET. */
130 HARD_REG_SET call_fixed_reg_set;
132 /* Indexed by hard register number, contains 1 for registers
133 that are being used for global register decls.
134 These must be exempt from ordinary flow analysis
135 and are also considered fixed. */
137 char global_regs[FIRST_PSEUDO_REGISTER];
139 /* Contains 1 for registers that are set or clobbered by calls. */
140 /* ??? Ideally, this would be just call_used_regs plus global_regs, but
141 for someone's bright idea to have call_used_regs strictly include
142 fixed_regs. Which leaves us guessing as to the set of fixed_regs
143 that are actually preserved. We know for sure that those associated
144 with the local stack frame are safe, but scant others. */
146 HARD_REG_SET regs_invalidated_by_call;
148 /* Same information as REGS_INVALIDATED_BY_CALL but in regset form to be used
149 in dataflow more conveniently. */
151 regset regs_invalidated_by_call_regset;
153 /* The bitmap_obstack is used to hold some static variables that
154 should not be reset after each function is compiled. */
156 static bitmap_obstack persistent_obstack;
158 /* Table of register numbers in the order in which to try to use them. */
159 #ifdef REG_ALLOC_ORDER
160 int reg_alloc_order[FIRST_PSEUDO_REGISTER] = REG_ALLOC_ORDER;
162 /* The inverse of reg_alloc_order. */
163 int inv_reg_alloc_order[FIRST_PSEUDO_REGISTER];
164 #endif
166 /* For each reg class, a HARD_REG_SET saying which registers are in it. */
168 HARD_REG_SET reg_class_contents[N_REG_CLASSES];
170 /* The same information, but as an array of unsigned ints. We copy from
171 these unsigned ints to the table above. We do this so the tm.h files
172 do not have to be aware of the wordsize for machines with <= 64 regs.
173 Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
175 #define N_REG_INTS \
176 ((FIRST_PSEUDO_REGISTER + (32 - 1)) / 32)
178 static const unsigned int_reg_class_contents[N_REG_CLASSES][N_REG_INTS]
179 = REG_CLASS_CONTENTS;
181 /* For each reg class, number of regs it contains. */
183 unsigned int reg_class_size[N_REG_CLASSES];
185 /* For each reg class, table listing all the containing classes. */
187 static enum reg_class reg_class_superclasses[N_REG_CLASSES][N_REG_CLASSES];
189 /* For each reg class, table listing all the classes contained in it. */
191 enum reg_class reg_class_subclasses[N_REG_CLASSES][N_REG_CLASSES];
193 /* For each pair of reg classes,
194 a largest reg class contained in their union. */
196 enum reg_class reg_class_subunion[N_REG_CLASSES][N_REG_CLASSES];
198 /* For each pair of reg classes,
199 the smallest reg class containing their union. */
201 enum reg_class reg_class_superunion[N_REG_CLASSES][N_REG_CLASSES];
203 /* Array containing all of the register names. */
205 const char * reg_names[] = REGISTER_NAMES;
207 /* Array containing all of the register class names. */
209 const char * reg_class_names[] = REG_CLASS_NAMES;
211 /* For each hard register, the widest mode object that it can contain.
212 This will be a MODE_INT mode if the register can hold integers. Otherwise
213 it will be a MODE_FLOAT or a MODE_CC mode, whichever is valid for the
214 register. */
216 enum machine_mode reg_raw_mode[FIRST_PSEUDO_REGISTER];
218 /* 1 if there is a register of given mode. */
220 bool have_regs_of_mode [MAX_MACHINE_MODE];
222 /* 1 if class does contain register of given mode. */
224 char contains_reg_of_mode [N_REG_CLASSES] [MAX_MACHINE_MODE];
226 /* Maximum cost of moving from a register in one class to a register in
227 another class. Based on REGISTER_MOVE_COST. */
229 move_table *move_cost[MAX_MACHINE_MODE];
231 /* Similar, but here we don't have to move if the first index is a subset
232 of the second so in that case the cost is zero. */
234 move_table *may_move_in_cost[MAX_MACHINE_MODE];
236 /* Similar, but here we don't have to move if the first index is a superset
237 of the second so in that case the cost is zero. */
239 move_table *may_move_out_cost[MAX_MACHINE_MODE];
241 /* Keep track of the last mode we initialized move costs for. */
242 static int last_mode_for_init_move_cost;
244 #ifdef FORBIDDEN_INC_DEC_CLASSES
246 /* These are the classes that regs which are auto-incremented or decremented
247 cannot be put in. */
249 static int forbidden_inc_dec_class[N_REG_CLASSES];
251 /* Indexed by n, is nonzero if (REG n) is used in an auto-inc or auto-dec
252 context. */
254 static char *in_inc_dec;
256 #endif /* FORBIDDEN_INC_DEC_CLASSES */
258 /* Sample MEM values for use by memory_move_secondary_cost. */
260 static GTY(()) rtx top_of_stack[MAX_MACHINE_MODE];
262 /* No more global register variables may be declared; true once
263 regclass has been initialized. */
265 static int no_global_reg_vars = 0;
267 /* Specify number of hard registers given machine mode occupy. */
268 unsigned char hard_regno_nregs[FIRST_PSEUDO_REGISTER][MAX_MACHINE_MODE];
270 /* Given a register bitmap, turn on the bits in a HARD_REG_SET that
271 correspond to the hard registers, if any, set in that map. This
272 could be done far more efficiently by having all sorts of special-cases
273 with moving single words, but probably isn't worth the trouble. */
275 void
276 reg_set_to_hard_reg_set (HARD_REG_SET *to, const_bitmap from)
278 unsigned i;
279 bitmap_iterator bi;
281 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
283 if (i >= FIRST_PSEUDO_REGISTER)
284 return;
285 SET_HARD_REG_BIT (*to, i);
290 /* Function called only once to initialize the above data on reg usage.
291 Once this is done, various switches may override. */
293 void
294 init_reg_sets (void)
296 int i, j;
298 /* First copy the register information from the initial int form into
299 the regsets. */
301 for (i = 0; i < N_REG_CLASSES; i++)
303 CLEAR_HARD_REG_SET (reg_class_contents[i]);
305 /* Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
306 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
307 if (int_reg_class_contents[i][j / 32]
308 & ((unsigned) 1 << (j % 32)))
309 SET_HARD_REG_BIT (reg_class_contents[i], j);
312 /* Sanity check: make sure the target macros FIXED_REGISTERS and
313 CALL_USED_REGISTERS had the right number of initializers. */
314 gcc_assert (sizeof fixed_regs == sizeof initial_fixed_regs);
315 gcc_assert (sizeof call_used_regs == sizeof initial_call_used_regs);
317 memcpy (fixed_regs, initial_fixed_regs, sizeof fixed_regs);
318 memcpy (call_used_regs, initial_call_used_regs, sizeof call_used_regs);
319 memset (global_regs, 0, sizeof global_regs);
322 /* Initialize may_move_cost and friends for mode M. */
324 void
325 init_move_cost (enum machine_mode m)
327 static unsigned short last_move_cost[N_REG_CLASSES][N_REG_CLASSES];
328 bool all_match = true;
329 unsigned int i, j;
331 gcc_assert (have_regs_of_mode[m]);
332 for (i = 0; i < N_REG_CLASSES; i++)
333 if (contains_reg_of_mode[i][m])
334 for (j = 0; j < N_REG_CLASSES; j++)
336 int cost;
337 if (!contains_reg_of_mode[j][m])
338 cost = 65535;
339 else
341 cost = REGISTER_MOVE_COST (m, i, j);
342 gcc_assert (cost < 65535);
344 all_match &= (last_move_cost[i][j] == cost);
345 last_move_cost[i][j] = cost;
347 if (all_match && last_mode_for_init_move_cost != -1)
349 move_cost[m] = move_cost[last_mode_for_init_move_cost];
350 may_move_in_cost[m] = may_move_in_cost[last_mode_for_init_move_cost];
351 may_move_out_cost[m] = may_move_out_cost[last_mode_for_init_move_cost];
352 return;
354 last_mode_for_init_move_cost = m;
355 move_cost[m] = (move_table *)xmalloc (sizeof (move_table)
356 * N_REG_CLASSES);
357 may_move_in_cost[m] = (move_table *)xmalloc (sizeof (move_table)
358 * N_REG_CLASSES);
359 may_move_out_cost[m] = (move_table *)xmalloc (sizeof (move_table)
360 * N_REG_CLASSES);
361 for (i = 0; i < N_REG_CLASSES; i++)
362 if (contains_reg_of_mode[i][m])
363 for (j = 0; j < N_REG_CLASSES; j++)
365 int cost;
366 enum reg_class *p1, *p2;
368 if (last_move_cost[i][j] == 65535)
370 move_cost[m][i][j] = 65535;
371 may_move_in_cost[m][i][j] = 65535;
372 may_move_out_cost[m][i][j] = 65535;
374 else
376 cost = last_move_cost[i][j];
378 for (p2 = &reg_class_subclasses[j][0];
379 *p2 != LIM_REG_CLASSES; p2++)
380 if (*p2 != i && contains_reg_of_mode[*p2][m])
381 cost = MAX (cost, move_cost[m][i][*p2]);
383 for (p1 = &reg_class_subclasses[i][0];
384 *p1 != LIM_REG_CLASSES; p1++)
385 if (*p1 != j && contains_reg_of_mode[*p1][m])
386 cost = MAX (cost, move_cost[m][*p1][j]);
388 gcc_assert (cost <= 65535);
389 move_cost[m][i][j] = cost;
391 if (reg_class_subset_p (i, j))
392 may_move_in_cost[m][i][j] = 0;
393 else
394 may_move_in_cost[m][i][j] = cost;
396 if (reg_class_subset_p (j, i))
397 may_move_out_cost[m][i][j] = 0;
398 else
399 may_move_out_cost[m][i][j] = cost;
402 else
403 for (j = 0; j < N_REG_CLASSES; j++)
405 move_cost[m][i][j] = 65535;
406 may_move_in_cost[m][i][j] = 65535;
407 may_move_out_cost[m][i][j] = 65535;
411 /* We need to save copies of some of the register information which
412 can be munged by command-line switches so we can restore it during
413 subsequent back-end reinitialization. */
415 static char saved_fixed_regs[FIRST_PSEUDO_REGISTER];
416 static char saved_call_used_regs[FIRST_PSEUDO_REGISTER];
417 #ifdef CALL_REALLY_USED_REGISTERS
418 static char saved_call_really_used_regs[FIRST_PSEUDO_REGISTER];
419 #endif
420 static const char *saved_reg_names[FIRST_PSEUDO_REGISTER];
422 /* Save the register information. */
424 void
425 save_register_info (void)
427 /* Sanity check: make sure the target macros FIXED_REGISTERS and
428 CALL_USED_REGISTERS had the right number of initializers. */
429 gcc_assert (sizeof fixed_regs == sizeof saved_fixed_regs);
430 gcc_assert (sizeof call_used_regs == sizeof saved_call_used_regs);
431 memcpy (saved_fixed_regs, fixed_regs, sizeof fixed_regs);
432 memcpy (saved_call_used_regs, call_used_regs, sizeof call_used_regs);
434 /* Likewise for call_really_used_regs. */
435 #ifdef CALL_REALLY_USED_REGISTERS
436 gcc_assert (sizeof call_really_used_regs
437 == sizeof saved_call_really_used_regs);
438 memcpy (saved_call_really_used_regs, call_really_used_regs,
439 sizeof call_really_used_regs);
440 #endif
442 /* And similarly for reg_names. */
443 gcc_assert (sizeof reg_names == sizeof saved_reg_names);
444 memcpy (saved_reg_names, reg_names, sizeof reg_names);
447 /* Restore the register information. */
449 static void
450 restore_register_info (void)
452 memcpy (fixed_regs, saved_fixed_regs, sizeof fixed_regs);
453 memcpy (call_used_regs, saved_call_used_regs, sizeof call_used_regs);
455 #ifdef CALL_REALLY_USED_REGISTERS
456 memcpy (call_really_used_regs, saved_call_really_used_regs,
457 sizeof call_really_used_regs);
458 #endif
460 memcpy (reg_names, saved_reg_names, sizeof reg_names);
463 /* After switches have been processed, which perhaps alter
464 `fixed_regs' and `call_used_regs', convert them to HARD_REG_SETs. */
466 static void
467 init_reg_sets_1 (void)
469 unsigned int i, j;
470 unsigned int /* enum machine_mode */ m;
472 restore_register_info ();
474 #ifdef REG_ALLOC_ORDER
475 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
476 inv_reg_alloc_order[reg_alloc_order[i]] = i;
477 #endif
479 /* This macro allows the fixed or call-used registers
480 and the register classes to depend on target flags. */
482 #ifdef CONDITIONAL_REGISTER_USAGE
483 CONDITIONAL_REGISTER_USAGE;
484 #endif
486 /* Compute number of hard regs in each class. */
488 memset (reg_class_size, 0, sizeof reg_class_size);
489 for (i = 0; i < N_REG_CLASSES; i++)
490 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
491 if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
492 reg_class_size[i]++;
494 /* Initialize the table of subunions.
495 reg_class_subunion[I][J] gets the largest-numbered reg-class
496 that is contained in the union of classes I and J. */
498 memset (reg_class_subunion, 0, sizeof reg_class_subunion);
499 for (i = 0; i < N_REG_CLASSES; i++)
501 for (j = 0; j < N_REG_CLASSES; j++)
503 HARD_REG_SET c;
504 int k;
506 COPY_HARD_REG_SET (c, reg_class_contents[i]);
507 IOR_HARD_REG_SET (c, reg_class_contents[j]);
508 for (k = 0; k < N_REG_CLASSES; k++)
509 if (hard_reg_set_subset_p (reg_class_contents[k], c)
510 && !hard_reg_set_subset_p (reg_class_contents[k],
511 reg_class_contents
512 [(int) reg_class_subunion[i][j]]))
513 reg_class_subunion[i][j] = (enum reg_class) k;
517 /* Initialize the table of superunions.
518 reg_class_superunion[I][J] gets the smallest-numbered reg-class
519 containing the union of classes I and J. */
521 memset (reg_class_superunion, 0, sizeof reg_class_superunion);
522 for (i = 0; i < N_REG_CLASSES; i++)
524 for (j = 0; j < N_REG_CLASSES; j++)
526 HARD_REG_SET c;
527 int k;
529 COPY_HARD_REG_SET (c, reg_class_contents[i]);
530 IOR_HARD_REG_SET (c, reg_class_contents[j]);
531 for (k = 0; k < N_REG_CLASSES; k++)
532 if (hard_reg_set_subset_p (c, reg_class_contents[k]))
533 break;
535 reg_class_superunion[i][j] = (enum reg_class) k;
539 /* Initialize the tables of subclasses and superclasses of each reg class.
540 First clear the whole table, then add the elements as they are found. */
542 for (i = 0; i < N_REG_CLASSES; i++)
544 for (j = 0; j < N_REG_CLASSES; j++)
546 reg_class_superclasses[i][j] = LIM_REG_CLASSES;
547 reg_class_subclasses[i][j] = LIM_REG_CLASSES;
551 for (i = 0; i < N_REG_CLASSES; i++)
553 if (i == (int) NO_REGS)
554 continue;
556 for (j = i + 1; j < N_REG_CLASSES; j++)
557 if (hard_reg_set_subset_p (reg_class_contents[i],
558 reg_class_contents[j]))
560 /* Reg class I is a subclass of J.
561 Add J to the table of superclasses of I. */
562 enum reg_class *p;
564 p = &reg_class_superclasses[i][0];
565 while (*p != LIM_REG_CLASSES) p++;
566 *p = (enum reg_class) j;
567 /* Add I to the table of superclasses of J. */
568 p = &reg_class_subclasses[j][0];
569 while (*p != LIM_REG_CLASSES) p++;
570 *p = (enum reg_class) i;
574 /* Initialize "constant" tables. */
576 CLEAR_HARD_REG_SET (fixed_reg_set);
577 CLEAR_HARD_REG_SET (call_used_reg_set);
578 CLEAR_HARD_REG_SET (call_fixed_reg_set);
579 CLEAR_HARD_REG_SET (regs_invalidated_by_call);
580 CLEAR_HARD_REG_SET (losing_caller_save_reg_set);
581 if (!regs_invalidated_by_call_regset)
583 bitmap_obstack_initialize (&persistent_obstack);
584 regs_invalidated_by_call_regset = ALLOC_REG_SET (&persistent_obstack);
586 else
587 CLEAR_REG_SET (regs_invalidated_by_call_regset);
589 memcpy (call_fixed_regs, fixed_regs, sizeof call_fixed_regs);
591 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
593 /* call_used_regs must include fixed_regs. */
594 gcc_assert (!fixed_regs[i] || call_used_regs[i]);
595 #ifdef CALL_REALLY_USED_REGISTERS
596 /* call_used_regs must include call_really_used_regs. */
597 gcc_assert (!call_really_used_regs[i] || call_used_regs[i]);
598 #endif
600 if (fixed_regs[i])
601 SET_HARD_REG_BIT (fixed_reg_set, i);
603 if (call_used_regs[i])
604 SET_HARD_REG_BIT (call_used_reg_set, i);
605 if (call_fixed_regs[i])
606 SET_HARD_REG_BIT (call_fixed_reg_set, i);
607 if (CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (i)))
608 SET_HARD_REG_BIT (losing_caller_save_reg_set, i);
610 /* There are a couple of fixed registers that we know are safe to
611 exclude from being clobbered by calls:
613 The frame pointer is always preserved across calls. The arg pointer
614 is if it is fixed. The stack pointer usually is, unless
615 RETURN_POPS_ARGS, in which case an explicit CLOBBER will be present.
616 If we are generating PIC code, the PIC offset table register is
617 preserved across calls, though the target can override that. */
619 if (i == STACK_POINTER_REGNUM)
621 else if (global_regs[i])
623 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
624 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
626 else if (i == FRAME_POINTER_REGNUM)
628 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
629 else if (i == HARD_FRAME_POINTER_REGNUM)
631 #endif
632 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
633 else if (i == ARG_POINTER_REGNUM && fixed_regs[i])
635 #endif
636 #ifndef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
637 else if (i == (unsigned) PIC_OFFSET_TABLE_REGNUM && fixed_regs[i])
639 #endif
640 else if (CALL_REALLY_USED_REGNO_P (i))
642 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
643 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
647 /* Preserve global registers if called more than once. */
648 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
650 if (global_regs[i])
652 fixed_regs[i] = call_used_regs[i] = call_fixed_regs[i] = 1;
653 SET_HARD_REG_BIT (fixed_reg_set, i);
654 SET_HARD_REG_BIT (call_used_reg_set, i);
655 SET_HARD_REG_BIT (call_fixed_reg_set, i);
659 memset (have_regs_of_mode, 0, sizeof (have_regs_of_mode));
660 memset (contains_reg_of_mode, 0, sizeof (contains_reg_of_mode));
661 for (m = 0; m < (unsigned int) MAX_MACHINE_MODE; m++)
663 HARD_REG_SET ok_regs;
664 CLEAR_HARD_REG_SET (ok_regs);
665 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
666 if (!fixed_regs [j] && HARD_REGNO_MODE_OK (j, m))
667 SET_HARD_REG_BIT (ok_regs, j);
669 for (i = 0; i < N_REG_CLASSES; i++)
670 if ((unsigned) CLASS_MAX_NREGS (i, m) <= reg_class_size[i]
671 && hard_reg_set_intersect_p (ok_regs, reg_class_contents[i]))
673 contains_reg_of_mode [i][m] = 1;
674 have_regs_of_mode [m] = 1;
678 /* Reset move_cost and friends, making sure we only free shared
679 table entries once. */
680 for (i = 0; i < MAX_MACHINE_MODE; i++)
681 if (move_cost[i])
683 for (j = 0; j < i && move_cost[i] != move_cost[j]; j++)
685 if (i == j)
687 free (move_cost[i]);
688 free (may_move_in_cost[i]);
689 free (may_move_out_cost[i]);
692 memset (move_cost, 0, sizeof move_cost);
693 memset (may_move_in_cost, 0, sizeof may_move_in_cost);
694 memset (may_move_out_cost, 0, sizeof may_move_out_cost);
695 last_mode_for_init_move_cost = -1;
698 /* Compute the table of register modes.
699 These values are used to record death information for individual registers
700 (as opposed to a multi-register mode).
701 This function might be invoked more than once, if the target has support
702 for changing register usage conventions on a per-function basis.
705 void
706 init_reg_modes_target (void)
708 int i, j;
710 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
711 for (j = 0; j < MAX_MACHINE_MODE; j++)
712 hard_regno_nregs[i][j] = HARD_REGNO_NREGS(i, (enum machine_mode)j);
714 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
716 reg_raw_mode[i] = choose_hard_reg_mode (i, 1, false);
718 /* If we couldn't find a valid mode, just use the previous mode.
719 ??? One situation in which we need to do this is on the mips where
720 HARD_REGNO_NREGS (fpreg, [SD]Fmode) returns 2. Ideally we'd like
721 to use DF mode for the even registers and VOIDmode for the odd
722 (for the cpu models where the odd ones are inaccessible). */
723 if (reg_raw_mode[i] == VOIDmode)
724 reg_raw_mode[i] = i == 0 ? word_mode : reg_raw_mode[i-1];
728 /* Finish initializing the register sets and initialize the register modes.
729 This function might be invoked more than once, if the target has support
730 for changing register usage conventions on a per-function basis.
733 void
734 init_regs (void)
736 /* This finishes what was started by init_reg_sets, but couldn't be done
737 until after register usage was specified. */
738 init_reg_sets_1 ();
740 init_reg_autoinc ();
743 /* Initialize some fake stack-frame MEM references for use in
744 memory_move_secondary_cost. */
746 void
747 init_fake_stack_mems (void)
750 int i;
752 for (i = 0; i < MAX_MACHINE_MODE; i++)
753 top_of_stack[i] = gen_rtx_MEM (i, stack_pointer_rtx);
758 /* Compute extra cost of moving registers to/from memory due to reloads.
759 Only needed if secondary reloads are required for memory moves. */
762 memory_move_secondary_cost (enum machine_mode mode, enum reg_class rclass, int in)
764 enum reg_class altclass;
765 int partial_cost = 0;
766 /* We need a memory reference to feed to SECONDARY... macros. */
767 /* mem may be unused even if the SECONDARY_ macros are defined. */
768 rtx mem ATTRIBUTE_UNUSED = top_of_stack[(int) mode];
771 altclass = secondary_reload_class (in ? 1 : 0, rclass, mode, mem);
773 if (altclass == NO_REGS)
774 return 0;
776 if (in)
777 partial_cost = REGISTER_MOVE_COST (mode, altclass, rclass);
778 else
779 partial_cost = REGISTER_MOVE_COST (mode, rclass, altclass);
781 if (rclass == altclass)
782 /* This isn't simply a copy-to-temporary situation. Can't guess
783 what it is, so MEMORY_MOVE_COST really ought not to be calling
784 here in that case.
786 I'm tempted to put in an assert here, but returning this will
787 probably only give poor estimates, which is what we would've
788 had before this code anyways. */
789 return partial_cost;
791 /* Check if the secondary reload register will also need a
792 secondary reload. */
793 return memory_move_secondary_cost (mode, altclass, in) + partial_cost;
796 /* Return a machine mode that is legitimate for hard reg REGNO and large
797 enough to save nregs. If we can't find one, return VOIDmode.
798 If CALL_SAVED is true, only consider modes that are call saved. */
800 enum machine_mode
801 choose_hard_reg_mode (unsigned int regno ATTRIBUTE_UNUSED,
802 unsigned int nregs, bool call_saved)
804 unsigned int /* enum machine_mode */ m;
805 enum machine_mode found_mode = VOIDmode, mode;
807 /* We first look for the largest integer mode that can be validly
808 held in REGNO. If none, we look for the largest floating-point mode.
809 If we still didn't find a valid mode, try CCmode. */
811 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
812 mode != VOIDmode;
813 mode = GET_MODE_WIDER_MODE (mode))
814 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
815 && HARD_REGNO_MODE_OK (regno, mode)
816 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
817 found_mode = mode;
819 if (found_mode != VOIDmode)
820 return found_mode;
822 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
823 mode != VOIDmode;
824 mode = GET_MODE_WIDER_MODE (mode))
825 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
826 && HARD_REGNO_MODE_OK (regno, mode)
827 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
828 found_mode = mode;
830 if (found_mode != VOIDmode)
831 return found_mode;
833 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
834 mode != VOIDmode;
835 mode = GET_MODE_WIDER_MODE (mode))
836 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
837 && HARD_REGNO_MODE_OK (regno, mode)
838 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
839 found_mode = mode;
841 if (found_mode != VOIDmode)
842 return found_mode;
844 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
845 mode != VOIDmode;
846 mode = GET_MODE_WIDER_MODE (mode))
847 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
848 && HARD_REGNO_MODE_OK (regno, mode)
849 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
850 found_mode = mode;
852 if (found_mode != VOIDmode)
853 return found_mode;
855 /* Iterate over all of the CCmodes. */
856 for (m = (unsigned int) CCmode; m < (unsigned int) NUM_MACHINE_MODES; ++m)
858 mode = (enum machine_mode) m;
859 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
860 && HARD_REGNO_MODE_OK (regno, mode)
861 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
862 return mode;
865 /* We can't find a mode valid for this register. */
866 return VOIDmode;
869 /* Specify the usage characteristics of the register named NAME.
870 It should be a fixed register if FIXED and a
871 call-used register if CALL_USED. */
873 void
874 fix_register (const char *name, int fixed, int call_used)
876 int i;
878 /* Decode the name and update the primary form of
879 the register info. */
881 if ((i = decode_reg_name (name)) >= 0)
883 if ((i == STACK_POINTER_REGNUM
884 #ifdef HARD_FRAME_POINTER_REGNUM
885 || i == HARD_FRAME_POINTER_REGNUM
886 #else
887 || i == FRAME_POINTER_REGNUM
888 #endif
890 && (fixed == 0 || call_used == 0))
892 static const char * const what_option[2][2] = {
893 { "call-saved", "call-used" },
894 { "no-such-option", "fixed" }};
896 error ("can't use '%s' as a %s register", name,
897 what_option[fixed][call_used]);
899 else
901 fixed_regs[i] = fixed;
902 call_used_regs[i] = call_used;
903 #ifdef CALL_REALLY_USED_REGISTERS
904 if (fixed == 0)
905 call_really_used_regs[i] = call_used;
906 #endif
909 else
911 warning (0, "unknown register name: %s", name);
915 /* Mark register number I as global. */
917 void
918 globalize_reg (int i)
920 if (fixed_regs[i] == 0 && no_global_reg_vars)
921 error ("global register variable follows a function definition");
923 if (global_regs[i])
925 warning (0, "register used for two global register variables");
926 return;
929 if (call_used_regs[i] && ! fixed_regs[i])
930 warning (0, "call-clobbered register used for global register variable");
932 global_regs[i] = 1;
934 /* If we're globalizing the frame pointer, we need to set the
935 appropriate regs_invalidated_by_call bit, even if it's already
936 set in fixed_regs. */
937 if (i != STACK_POINTER_REGNUM)
939 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
940 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
943 /* If already fixed, nothing else to do. */
944 if (fixed_regs[i])
945 return;
947 fixed_regs[i] = call_used_regs[i] = call_fixed_regs[i] = 1;
948 #ifdef CALL_REALLY_USED_REGISTERS
949 call_really_used_regs[i] = 1;
950 #endif
952 SET_HARD_REG_BIT (fixed_reg_set, i);
953 SET_HARD_REG_BIT (call_used_reg_set, i);
954 SET_HARD_REG_BIT (call_fixed_reg_set, i);
957 /* Now the data and code for the `regclass' pass, which happens
958 just before local-alloc. */
960 /* The `costs' struct records the cost of using a hard register of each class
961 and of using memory for each pseudo. We use this data to set up
962 register class preferences. */
964 struct costs
966 int cost[N_REG_CLASSES];
967 int mem_cost;
970 /* Structure used to record preferences of given pseudo. */
971 struct reg_pref
973 /* (enum reg_class) prefclass is the preferred class. May be
974 NO_REGS if no class is better than memory. */
975 char prefclass;
977 /* altclass is a register class that we should use for allocating
978 pseudo if no register in the preferred class is available.
979 If no register in this class is available, memory is preferred.
981 It might appear to be more general to have a bitmask of classes here,
982 but since it is recommended that there be a class corresponding to the
983 union of most major pair of classes, that generality is not required. */
984 char altclass;
987 /* Record the cost of each class for each pseudo. */
989 static struct costs *costs;
991 /* Initialized once, and used to initialize cost values for each insn. */
993 static struct costs init_cost;
995 /* Record preferences of each pseudo.
996 This is available after `regclass' is run. */
998 static struct reg_pref *reg_pref;
1000 /* Frequency of executions of current insn. */
1002 static int frequency;
1004 static rtx scan_one_insn (rtx, int);
1005 static void record_operand_costs (rtx, struct costs *, struct reg_pref *);
1006 static void dump_regclass (FILE *);
1007 static void record_reg_classes (int, int, rtx *, enum machine_mode *,
1008 const char **, rtx, struct costs *,
1009 struct reg_pref *);
1010 static int copy_cost (rtx, enum machine_mode, enum reg_class, int,
1011 secondary_reload_info *);
1012 static void record_address_regs (enum machine_mode, rtx, int, enum rtx_code,
1013 enum rtx_code, int);
1014 #ifdef FORBIDDEN_INC_DEC_CLASSES
1015 static int auto_inc_dec_reg_p (rtx, enum machine_mode);
1016 #endif
1017 static void reg_scan_mark_refs (rtx, rtx);
1019 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudo registers. */
1021 static inline bool
1022 ok_for_index_p_nonstrict (rtx reg)
1024 unsigned regno = REGNO (reg);
1025 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
1028 /* A version of regno_ok_for_base_p for use during regclass, when all pseudos
1029 should count as OK. Arguments as for regno_ok_for_base_p. */
1031 static inline bool
1032 ok_for_base_p_nonstrict (rtx reg, enum machine_mode mode,
1033 enum rtx_code outer_code, enum rtx_code index_code)
1035 unsigned regno = REGNO (reg);
1036 if (regno >= FIRST_PSEUDO_REGISTER)
1037 return true;
1039 return ok_for_base_p_1 (regno, mode, outer_code, index_code);
1042 /* Return the reg_class in which pseudo reg number REGNO is best allocated.
1043 This function is sometimes called before the info has been computed.
1044 When that happens, just return GENERAL_REGS, which is innocuous. */
1046 enum reg_class
1047 reg_preferred_class (int regno)
1049 if (reg_pref == 0)
1050 return GENERAL_REGS;
1052 return (enum reg_class) reg_pref[regno].prefclass;
1055 enum reg_class
1056 reg_alternate_class (int regno)
1058 if (reg_pref == 0)
1059 return ALL_REGS;
1061 return (enum reg_class) reg_pref[regno].altclass;
1064 /* Initialize some global data for this pass. */
1066 static unsigned int
1067 regclass_init (void)
1069 int i;
1071 if (df)
1072 df_compute_regs_ever_live (true);
1074 init_cost.mem_cost = 10000;
1075 for (i = 0; i < N_REG_CLASSES; i++)
1076 init_cost.cost[i] = 10000;
1078 /* This prevents dump_flow_info from losing if called
1079 before regclass is run. */
1080 reg_pref = NULL;
1082 /* No more global register variables may be declared. */
1083 no_global_reg_vars = 1;
1084 return 1;
1087 struct rtl_opt_pass pass_regclass_init =
1090 RTL_PASS,
1091 "regclass", /* name */
1092 NULL, /* gate */
1093 regclass_init, /* execute */
1094 NULL, /* sub */
1095 NULL, /* next */
1096 0, /* static_pass_number */
1097 0, /* tv_id */
1098 0, /* properties_required */
1099 0, /* properties_provided */
1100 0, /* properties_destroyed */
1101 0, /* todo_flags_start */
1102 0 /* todo_flags_finish */
1108 /* Dump register costs. */
1109 static void
1110 dump_regclass (FILE *dump)
1112 int i;
1113 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1115 int /* enum reg_class */ rclass;
1116 if (REG_N_REFS (i))
1118 fprintf (dump, " Register %i costs:", i);
1119 for (rclass = 0; rclass < (int) N_REG_CLASSES; rclass++)
1120 if (contains_reg_of_mode [(enum reg_class) rclass][PSEUDO_REGNO_MODE (i)]
1121 #ifdef FORBIDDEN_INC_DEC_CLASSES
1122 && (!in_inc_dec[i]
1123 || !forbidden_inc_dec_class[(enum reg_class) rclass])
1124 #endif
1125 #ifdef CANNOT_CHANGE_MODE_CLASS
1126 && ! invalid_mode_change_p (i, (enum reg_class) rclass,
1127 PSEUDO_REGNO_MODE (i))
1128 #endif
1130 fprintf (dump, " %s:%i", reg_class_names[rclass],
1131 costs[i].cost[(enum reg_class) rclass]);
1132 fprintf (dump, " MEM:%i\n", costs[i].mem_cost);
1138 /* Calculate the costs of insn operands. */
1140 static void
1141 record_operand_costs (rtx insn, struct costs *op_costs,
1142 struct reg_pref *reg_pref)
1144 const char *constraints[MAX_RECOG_OPERANDS];
1145 enum machine_mode modes[MAX_RECOG_OPERANDS];
1146 int i;
1148 for (i = 0; i < recog_data.n_operands; i++)
1150 constraints[i] = recog_data.constraints[i];
1151 modes[i] = recog_data.operand_mode[i];
1154 /* If we get here, we are set up to record the costs of all the
1155 operands for this insn. Start by initializing the costs.
1156 Then handle any address registers. Finally record the desired
1157 classes for any pseudos, doing it twice if some pair of
1158 operands are commutative. */
1160 for (i = 0; i < recog_data.n_operands; i++)
1162 op_costs[i] = init_cost;
1164 if (GET_CODE (recog_data.operand[i]) == SUBREG)
1165 recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
1167 if (MEM_P (recog_data.operand[i]))
1168 record_address_regs (GET_MODE (recog_data.operand[i]),
1169 XEXP (recog_data.operand[i], 0),
1170 0, MEM, SCRATCH, frequency * 2);
1171 else if (recog_data.alternative_enabled_p[0]
1172 && (constraints[i][0] == 'p'
1173 || EXTRA_ADDRESS_CONSTRAINT (constraints[i][0], constraints[i])))
1174 record_address_regs (VOIDmode, recog_data.operand[i], 0, ADDRESS,
1175 SCRATCH, frequency * 2);
1178 /* Check for commutative in a separate loop so everything will
1179 have been initialized. We must do this even if one operand
1180 is a constant--see addsi3 in m68k.md. */
1182 for (i = 0; i < (int) recog_data.n_operands - 1; i++)
1183 if (constraints[i][0] == '%')
1185 const char *xconstraints[MAX_RECOG_OPERANDS];
1186 int j;
1188 /* Handle commutative operands by swapping the constraints.
1189 We assume the modes are the same. */
1191 for (j = 0; j < recog_data.n_operands; j++)
1192 xconstraints[j] = constraints[j];
1194 xconstraints[i] = constraints[i+1];
1195 xconstraints[i+1] = constraints[i];
1196 record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
1197 recog_data.operand, modes,
1198 xconstraints, insn, op_costs, reg_pref);
1201 record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
1202 recog_data.operand, modes,
1203 constraints, insn, op_costs, reg_pref);
1206 /* Subroutine of regclass, processes one insn INSN. Scan it and record each
1207 time it would save code to put a certain register in a certain class.
1208 PASS, when nonzero, inhibits some optimizations which need only be done
1209 once.
1210 Return the last insn processed, so that the scan can be continued from
1211 there. */
1213 static rtx
1214 scan_one_insn (rtx insn, int pass ATTRIBUTE_UNUSED)
1216 enum rtx_code pat_code;
1217 rtx set, note;
1218 int i, j;
1219 struct costs op_costs[MAX_RECOG_OPERANDS];
1221 if (!INSN_P (insn))
1222 return insn;
1224 pat_code = GET_CODE (PATTERN (insn));
1225 if (pat_code == USE
1226 || pat_code == CLOBBER
1227 || pat_code == ASM_INPUT
1228 || pat_code == ADDR_VEC
1229 || pat_code == ADDR_DIFF_VEC)
1230 return insn;
1232 set = single_set (insn);
1233 extract_insn (insn);
1235 /* If this insn loads a parameter from its stack slot, then
1236 it represents a savings, rather than a cost, if the
1237 parameter is stored in memory. Record this fact. */
1239 if (set != 0 && REG_P (SET_DEST (set))
1240 && MEM_P (SET_SRC (set))
1241 && (note = find_reg_note (insn, REG_EQUIV,
1242 NULL_RTX)) != 0
1243 && MEM_P (XEXP (note, 0)))
1245 costs[REGNO (SET_DEST (set))].mem_cost
1246 -= (MEMORY_MOVE_COST (GET_MODE (SET_DEST (set)),
1247 GENERAL_REGS, 1)
1248 * frequency);
1249 record_address_regs (GET_MODE (SET_SRC (set)), XEXP (SET_SRC (set), 0),
1250 0, MEM, SCRATCH, frequency * 2);
1251 return insn;
1254 record_operand_costs (insn, op_costs, reg_pref);
1256 /* Now add the cost for each operand to the total costs for
1257 its register. */
1259 for (i = 0; i < recog_data.n_operands; i++)
1260 if (REG_P (recog_data.operand[i])
1261 && REGNO (recog_data.operand[i]) >= FIRST_PSEUDO_REGISTER)
1263 int regno = REGNO (recog_data.operand[i]);
1264 struct costs *p = &costs[regno], *q = &op_costs[i];
1266 p->mem_cost += q->mem_cost * frequency;
1267 for (j = 0; j < N_REG_CLASSES; j++)
1268 p->cost[j] += q->cost[j] * frequency;
1271 return insn;
1274 /* Initialize information about which register classes can be used for
1275 pseudos that are auto-incremented or auto-decremented. */
1277 static void
1278 init_reg_autoinc (void)
1280 #ifdef FORBIDDEN_INC_DEC_CLASSES
1281 int i;
1283 memset (forbidden_inc_dec_class, 0, sizeof forbidden_inc_dec_class);
1284 for (i = 0; i < N_REG_CLASSES; i++)
1286 rtx r = gen_rtx_raw_REG (VOIDmode, 0);
1287 enum machine_mode m;
1288 int j;
1290 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1291 if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
1293 SET_REGNO (r, j);
1295 for (m = VOIDmode; (int) m < (int) MAX_MACHINE_MODE;
1296 m = (enum machine_mode) ((int) m + 1))
1297 if (HARD_REGNO_MODE_OK (j, m))
1299 /* ??? There are two assumptions here; that the base class does not
1300 depend on the exact outer code (POST_INC vs. PRE_INC etc.), and
1301 that it does not depend on the machine mode of the memory
1302 reference. */
1303 enum reg_class base_class
1304 = base_reg_class (VOIDmode, POST_INC, SCRATCH);
1306 PUT_MODE (r, m);
1308 /* If a register is not directly suitable for an
1309 auto-increment or decrement addressing mode and
1310 requires secondary reloads, disallow its class from
1311 being used in such addresses. */
1313 if ((secondary_reload_class (0, base_class, m, r)
1314 || secondary_reload_class (1, base_class, m, r))
1315 && ! auto_inc_dec_reg_p (r, m))
1316 forbidden_inc_dec_class[i] = 1;
1320 #endif /* FORBIDDEN_INC_DEC_CLASSES */
1323 /* This is a pass of the compiler that scans all instructions
1324 and calculates the preferred class for each pseudo-register.
1325 This information can be accessed later by calling `reg_preferred_class'.
1326 This pass comes just before local register allocation. */
1328 void
1329 regclass (rtx f, int nregs)
1331 rtx insn;
1332 int i;
1333 int pass;
1334 max_regno = max_reg_num ();
1336 init_recog ();
1338 reg_renumber = XNEWVEC (short, max_regno);
1339 reg_pref = XCNEWVEC (struct reg_pref, max_regno);
1340 memset (reg_renumber, -1, max_regno * sizeof (short));
1342 costs = XNEWVEC (struct costs, nregs);
1344 #ifdef FORBIDDEN_INC_DEC_CLASSES
1346 in_inc_dec = XNEWVEC (char, nregs);
1348 #endif /* FORBIDDEN_INC_DEC_CLASSES */
1350 /* Normally we scan the insns once and determine the best class to use for
1351 each register. However, if -fexpensive_optimizations are on, we do so
1352 twice, the second time using the tentative best classes to guide the
1353 selection. */
1355 for (pass = 0; pass <= flag_expensive_optimizations; pass++)
1357 basic_block bb;
1359 if (dump_file)
1360 fprintf (dump_file, "\n\nPass %i\n\n",pass);
1361 /* Zero out our accumulation of the cost of each class for each reg. */
1363 memset (costs, 0, nregs * sizeof (struct costs));
1365 #ifdef FORBIDDEN_INC_DEC_CLASSES
1366 memset (in_inc_dec, 0, nregs);
1367 #endif
1369 /* Scan the instructions and record each time it would
1370 save code to put a certain register in a certain class. */
1372 if (!optimize)
1374 frequency = REG_FREQ_MAX;
1375 for (insn = f; insn; insn = NEXT_INSN (insn))
1376 insn = scan_one_insn (insn, pass);
1378 else
1379 FOR_EACH_BB (bb)
1381 /* Show that an insn inside a loop is likely to be executed three
1382 times more than insns outside a loop. This is much more
1383 aggressive than the assumptions made elsewhere and is being
1384 tried as an experiment. */
1385 frequency = REG_FREQ_FROM_BB (bb);
1386 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
1388 insn = scan_one_insn (insn, pass);
1389 if (insn == BB_END (bb))
1390 break;
1394 /* Now for each register look at how desirable each class is
1395 and find which class is preferred. Store that in
1396 `prefclass'. Record in `altclass' the largest register
1397 class any of whose registers is better than memory. */
1399 if (dump_file)
1401 dump_regclass (dump_file);
1402 fprintf (dump_file,"\n");
1404 for (i = FIRST_PSEUDO_REGISTER; i < nregs; i++)
1406 int best_cost = (1 << (HOST_BITS_PER_INT - 2)) - 1;
1407 enum reg_class best = ALL_REGS, alt = NO_REGS;
1408 /* This is an enum reg_class, but we call it an int
1409 to save lots of casts. */
1410 int rclass;
1411 struct costs *p = &costs[i];
1413 if (regno_reg_rtx[i] == NULL)
1414 continue;
1416 /* In non-optimizing compilation REG_N_REFS is not initialized
1417 yet. */
1418 if (optimize && !REG_N_REFS (i) && !REG_N_SETS (i))
1419 continue;
1421 for (rclass = (int) ALL_REGS - 1; rclass > 0; rclass--)
1423 /* Ignore classes that are too small for this operand or
1424 invalid for an operand that was auto-incremented. */
1425 if (!contains_reg_of_mode [rclass][PSEUDO_REGNO_MODE (i)]
1426 #ifdef FORBIDDEN_INC_DEC_CLASSES
1427 || (in_inc_dec[i] && forbidden_inc_dec_class[rclass])
1428 #endif
1429 #ifdef CANNOT_CHANGE_MODE_CLASS
1430 || invalid_mode_change_p (i, (enum reg_class) rclass,
1431 PSEUDO_REGNO_MODE (i))
1432 #endif
1435 else if (p->cost[rclass] < best_cost)
1437 best_cost = p->cost[rclass];
1438 best = (enum reg_class) rclass;
1440 else if (p->cost[rclass] == best_cost)
1441 best = reg_class_subunion[(int) best][rclass];
1444 /* If no register class is better than memory, use memory. */
1445 if (p->mem_cost < best_cost)
1446 best = NO_REGS;
1448 /* Record the alternate register class; i.e., a class for which
1449 every register in it is better than using memory. If adding a
1450 class would make a smaller class (i.e., no union of just those
1451 classes exists), skip that class. The major unions of classes
1452 should be provided as a register class. Don't do this if we
1453 will be doing it again later. */
1455 if ((pass == 1 || dump_file) || ! flag_expensive_optimizations)
1456 for (rclass = 0; rclass < N_REG_CLASSES; rclass++)
1457 if (p->cost[rclass] < p->mem_cost
1458 && (reg_class_size[(int) reg_class_subunion[(int) alt][rclass]]
1459 > reg_class_size[(int) alt])
1460 #ifdef FORBIDDEN_INC_DEC_CLASSES
1461 && ! (in_inc_dec[i] && forbidden_inc_dec_class[rclass])
1462 #endif
1463 #ifdef CANNOT_CHANGE_MODE_CLASS
1464 && ! invalid_mode_change_p (i, (enum reg_class) rclass,
1465 PSEUDO_REGNO_MODE (i))
1466 #endif
1468 alt = reg_class_subunion[(int) alt][rclass];
1470 /* If we don't add any classes, nothing to try. */
1471 if (alt == best)
1472 alt = NO_REGS;
1474 if (dump_file
1475 && (reg_pref[i].prefclass != (int) best
1476 || reg_pref[i].altclass != (int) alt))
1478 fprintf (dump_file, " Register %i", i);
1479 if (alt == ALL_REGS || best == ALL_REGS)
1480 fprintf (dump_file, " pref %s\n", reg_class_names[(int) best]);
1481 else if (alt == NO_REGS)
1482 fprintf (dump_file, " pref %s or none\n", reg_class_names[(int) best]);
1483 else
1484 fprintf (dump_file, " pref %s, else %s\n",
1485 reg_class_names[(int) best],
1486 reg_class_names[(int) alt]);
1489 /* We cast to (int) because (char) hits bugs in some compilers. */
1490 reg_pref[i].prefclass = (int) best;
1491 reg_pref[i].altclass = (int) alt;
1495 #ifdef FORBIDDEN_INC_DEC_CLASSES
1496 free (in_inc_dec);
1497 #endif
1498 free (costs);
1501 /* Record the cost of using memory or registers of various classes for
1502 the operands in INSN.
1504 N_ALTS is the number of alternatives.
1506 N_OPS is the number of operands.
1508 OPS is an array of the operands.
1510 MODES are the modes of the operands, in case any are VOIDmode.
1512 CONSTRAINTS are the constraints to use for the operands. This array
1513 is modified by this procedure.
1515 This procedure works alternative by alternative. For each alternative
1516 we assume that we will be able to allocate all pseudos to their ideal
1517 register class and calculate the cost of using that alternative. Then
1518 we compute for each operand that is a pseudo-register, the cost of
1519 having the pseudo allocated to each register class and using it in that
1520 alternative. To this cost is added the cost of the alternative.
1522 The cost of each class for this insn is its lowest cost among all the
1523 alternatives. */
1525 static void
1526 record_reg_classes (int n_alts, int n_ops, rtx *ops,
1527 enum machine_mode *modes, const char **constraints,
1528 rtx insn, struct costs *op_costs,
1529 struct reg_pref *reg_pref)
1531 int alt;
1532 int i, j;
1533 rtx set;
1535 /* Process each alternative, each time minimizing an operand's cost with
1536 the cost for each operand in that alternative. */
1538 for (alt = 0; alt < n_alts; alt++)
1540 struct costs this_op_costs[MAX_RECOG_OPERANDS];
1541 int alt_fail = 0;
1542 int alt_cost = 0;
1543 enum reg_class classes[MAX_RECOG_OPERANDS];
1544 int allows_mem[MAX_RECOG_OPERANDS];
1545 int rclass;
1547 for (i = 0; i < n_ops; i++)
1549 const char *p = constraints[i];
1550 rtx op = ops[i];
1551 enum machine_mode mode = modes[i];
1552 int allows_addr = 0;
1553 int win = 0;
1554 unsigned char c;
1556 /* Initially show we know nothing about the register class. */
1557 classes[i] = NO_REGS;
1558 allows_mem[i] = 0;
1560 /* If this operand has no constraints at all, we can conclude
1561 nothing about it since anything is valid. */
1563 if (*p == 0)
1565 if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
1566 memset (&this_op_costs[i], 0, sizeof this_op_costs[i]);
1568 continue;
1571 /* If this alternative is only relevant when this operand
1572 matches a previous operand, we do different things depending
1573 on whether this operand is a pseudo-reg or not. We must process
1574 any modifiers for the operand before we can make this test. */
1576 while (*p == '%' || *p == '=' || *p == '+' || *p == '&')
1577 p++;
1579 if (p[0] >= '0' && p[0] <= '0' + i && (p[1] == ',' || p[1] == 0))
1581 /* Copy class and whether memory is allowed from the matching
1582 alternative. Then perform any needed cost computations
1583 and/or adjustments. */
1584 j = p[0] - '0';
1585 classes[i] = classes[j];
1586 allows_mem[i] = allows_mem[j];
1588 if (!REG_P (op) || REGNO (op) < FIRST_PSEUDO_REGISTER)
1590 /* If this matches the other operand, we have no added
1591 cost and we win. */
1592 if (rtx_equal_p (ops[j], op))
1593 win = 1;
1595 /* If we can put the other operand into a register, add to
1596 the cost of this alternative the cost to copy this
1597 operand to the register used for the other operand. */
1599 else if (classes[j] != NO_REGS)
1601 alt_cost += copy_cost (op, mode, classes[j], 1, NULL);
1602 win = 1;
1605 else if (!REG_P (ops[j])
1606 || REGNO (ops[j]) < FIRST_PSEUDO_REGISTER)
1608 /* This op is a pseudo but the one it matches is not. */
1610 /* If we can't put the other operand into a register, this
1611 alternative can't be used. */
1613 if (classes[j] == NO_REGS)
1614 alt_fail = 1;
1616 /* Otherwise, add to the cost of this alternative the cost
1617 to copy the other operand to the register used for this
1618 operand. */
1620 else
1621 alt_cost += copy_cost (ops[j], mode, classes[j], 1, NULL);
1623 else
1625 /* The costs of this operand are not the same as the other
1626 operand since move costs are not symmetric. Moreover,
1627 if we cannot tie them, this alternative needs to do a
1628 copy, which is one instruction. */
1630 struct costs *pp = &this_op_costs[i];
1631 move_table *intable = NULL;
1632 move_table *outtable = NULL;
1633 int op_class = (int) classes[i];
1635 if (!move_cost[mode])
1636 init_move_cost (mode);
1637 intable = may_move_in_cost[mode];
1638 outtable = may_move_out_cost[mode];
1640 /* The loop is performance critical, so unswitch it manually.
1642 switch (recog_data.operand_type[i])
1644 case OP_INOUT:
1645 for (rclass = 0; rclass < N_REG_CLASSES; rclass++)
1646 pp->cost[rclass] = (intable[rclass][op_class]
1647 + outtable[op_class][rclass]);
1648 break;
1649 case OP_IN:
1650 for (rclass = 0; rclass < N_REG_CLASSES; rclass++)
1651 pp->cost[rclass] = intable[rclass][op_class];
1652 break;
1653 case OP_OUT:
1654 for (rclass = 0; rclass < N_REG_CLASSES; rclass++)
1655 pp->cost[rclass] = outtable[op_class][rclass];
1656 break;
1659 /* If the alternative actually allows memory, make things
1660 a bit cheaper since we won't need an extra insn to
1661 load it. */
1663 pp->mem_cost
1664 = ((recog_data.operand_type[i] != OP_IN
1665 ? MEMORY_MOVE_COST (mode, classes[i], 0)
1666 : 0)
1667 + (recog_data.operand_type[i] != OP_OUT
1668 ? MEMORY_MOVE_COST (mode, classes[i], 1)
1669 : 0) - allows_mem[i]);
1671 /* If we have assigned a class to this register in our
1672 first pass, add a cost to this alternative corresponding
1673 to what we would add if this register were not in the
1674 appropriate class. */
1676 if (reg_pref && reg_pref[REGNO (op)].prefclass != NO_REGS)
1677 alt_cost
1678 += (may_move_in_cost[mode]
1679 [(unsigned char) reg_pref[REGNO (op)].prefclass]
1680 [(int) classes[i]]);
1682 if (REGNO (ops[i]) != REGNO (ops[j])
1683 && ! find_reg_note (insn, REG_DEAD, op))
1684 alt_cost += 2;
1686 /* This is in place of ordinary cost computation
1687 for this operand, so skip to the end of the
1688 alternative (should be just one character). */
1689 while (*p && *p++ != ',')
1692 constraints[i] = p;
1693 continue;
1697 /* Scan all the constraint letters. See if the operand matches
1698 any of the constraints. Collect the valid register classes
1699 and see if this operand accepts memory. */
1701 while ((c = *p))
1703 switch (c)
1705 case ',':
1706 break;
1707 case '*':
1708 /* Ignore the next letter for this pass. */
1709 c = *++p;
1710 break;
1712 case '?':
1713 alt_cost += 2;
1714 case '!': case '#': case '&':
1715 case '0': case '1': case '2': case '3': case '4':
1716 case '5': case '6': case '7': case '8': case '9':
1717 break;
1719 case 'p':
1720 allows_addr = 1;
1721 win = address_operand (op, GET_MODE (op));
1722 /* We know this operand is an address, so we want it to be
1723 allocated to a register that can be the base of an
1724 address, i.e. BASE_REG_CLASS. */
1725 classes[i]
1726 = reg_class_subunion[(int) classes[i]]
1727 [(int) base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
1728 break;
1730 case TARGET_MEM_CONSTRAINT: case 'o': case 'V':
1731 /* It doesn't seem worth distinguishing between offsettable
1732 and non-offsettable addresses here. */
1733 allows_mem[i] = 1;
1734 if (MEM_P (op))
1735 win = 1;
1736 break;
1738 case '<':
1739 if (MEM_P (op)
1740 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
1741 || GET_CODE (XEXP (op, 0)) == POST_DEC))
1742 win = 1;
1743 break;
1745 case '>':
1746 if (MEM_P (op)
1747 && (GET_CODE (XEXP (op, 0)) == PRE_INC
1748 || GET_CODE (XEXP (op, 0)) == POST_INC))
1749 win = 1;
1750 break;
1752 case 'E':
1753 case 'F':
1754 if (GET_CODE (op) == CONST_DOUBLE
1755 || (GET_CODE (op) == CONST_VECTOR
1756 && (GET_MODE_CLASS (GET_MODE (op))
1757 == MODE_VECTOR_FLOAT)))
1758 win = 1;
1759 break;
1761 case 'G':
1762 case 'H':
1763 if (GET_CODE (op) == CONST_DOUBLE
1764 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
1765 win = 1;
1766 break;
1768 case 's':
1769 if (GET_CODE (op) == CONST_INT
1770 || (GET_CODE (op) == CONST_DOUBLE
1771 && GET_MODE (op) == VOIDmode))
1772 break;
1773 case 'i':
1774 if (CONSTANT_P (op)
1775 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op)))
1776 win = 1;
1777 break;
1779 case 'n':
1780 if (GET_CODE (op) == CONST_INT
1781 || (GET_CODE (op) == CONST_DOUBLE
1782 && GET_MODE (op) == VOIDmode))
1783 win = 1;
1784 break;
1786 case 'I':
1787 case 'J':
1788 case 'K':
1789 case 'L':
1790 case 'M':
1791 case 'N':
1792 case 'O':
1793 case 'P':
1794 if (GET_CODE (op) == CONST_INT
1795 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
1796 win = 1;
1797 break;
1799 case 'X':
1800 win = 1;
1801 break;
1803 case 'g':
1804 if (MEM_P (op)
1805 || (CONSTANT_P (op)
1806 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))))
1807 win = 1;
1808 allows_mem[i] = 1;
1809 case 'r':
1810 classes[i]
1811 = reg_class_subunion[(int) classes[i]][(int) GENERAL_REGS];
1812 break;
1814 default:
1815 if (REG_CLASS_FROM_CONSTRAINT (c, p) != NO_REGS)
1816 classes[i]
1817 = reg_class_subunion[(int) classes[i]]
1818 [(int) REG_CLASS_FROM_CONSTRAINT (c, p)];
1819 #ifdef EXTRA_CONSTRAINT_STR
1820 else if (EXTRA_CONSTRAINT_STR (op, c, p))
1821 win = 1;
1823 if (EXTRA_MEMORY_CONSTRAINT (c, p))
1825 /* Every MEM can be reloaded to fit. */
1826 allows_mem[i] = 1;
1827 if (MEM_P (op))
1828 win = 1;
1830 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1832 /* Every address can be reloaded to fit. */
1833 allows_addr = 1;
1834 if (address_operand (op, GET_MODE (op)))
1835 win = 1;
1836 /* We know this operand is an address, so we want it to
1837 be allocated to a register that can be the base of an
1838 address, i.e. BASE_REG_CLASS. */
1839 classes[i]
1840 = reg_class_subunion[(int) classes[i]]
1841 [(int) base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
1843 #endif
1844 break;
1846 p += CONSTRAINT_LEN (c, p);
1847 if (c == ',')
1848 break;
1851 constraints[i] = p;
1853 /* How we account for this operand now depends on whether it is a
1854 pseudo register or not. If it is, we first check if any
1855 register classes are valid. If not, we ignore this alternative,
1856 since we want to assume that all pseudos get allocated for
1857 register preferencing. If some register class is valid, compute
1858 the costs of moving the pseudo into that class. */
1860 if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
1862 if (classes[i] == NO_REGS)
1864 /* We must always fail if the operand is a REG, but
1865 we did not find a suitable class.
1867 Otherwise we may perform an uninitialized read
1868 from this_op_costs after the `continue' statement
1869 below. */
1870 alt_fail = 1;
1872 else
1874 struct costs *pp = &this_op_costs[i];
1875 move_table *intable = NULL;
1876 move_table *outtable = NULL;
1877 int op_class = (int) classes[i];
1879 if (!move_cost[mode])
1880 init_move_cost (mode);
1881 intable = may_move_in_cost[mode];
1882 outtable = may_move_out_cost[mode];
1884 /* The loop is performance critical, so unswitch it manually.
1886 switch (recog_data.operand_type[i])
1888 case OP_INOUT:
1889 for (rclass = 0; rclass < N_REG_CLASSES; rclass++)
1890 pp->cost[rclass] = (intable[rclass][op_class]
1891 + outtable[op_class][rclass]);
1892 break;
1893 case OP_IN:
1894 for (rclass = 0; rclass < N_REG_CLASSES; rclass++)
1895 pp->cost[rclass] = intable[rclass][op_class];
1896 break;
1897 case OP_OUT:
1898 for (rclass = 0; rclass < N_REG_CLASSES; rclass++)
1899 pp->cost[rclass] = outtable[op_class][rclass];
1900 break;
1903 /* If the alternative actually allows memory, make things
1904 a bit cheaper since we won't need an extra insn to
1905 load it. */
1907 pp->mem_cost
1908 = ((recog_data.operand_type[i] != OP_IN
1909 ? MEMORY_MOVE_COST (mode, classes[i], 0)
1910 : 0)
1911 + (recog_data.operand_type[i] != OP_OUT
1912 ? MEMORY_MOVE_COST (mode, classes[i], 1)
1913 : 0) - allows_mem[i]);
1915 /* If we have assigned a class to this register in our
1916 first pass, add a cost to this alternative corresponding
1917 to what we would add if this register were not in the
1918 appropriate class. */
1920 if (reg_pref && reg_pref[REGNO (op)].prefclass != NO_REGS)
1921 alt_cost
1922 += (may_move_in_cost[mode]
1923 [(unsigned char) reg_pref[REGNO (op)].prefclass]
1924 [(int) classes[i]]);
1928 /* Otherwise, if this alternative wins, either because we
1929 have already determined that or if we have a hard register of
1930 the proper class, there is no cost for this alternative. */
1932 else if (win
1933 || (REG_P (op)
1934 && reg_fits_class_p (op, classes[i], 0, GET_MODE (op))))
1937 /* If registers are valid, the cost of this alternative includes
1938 copying the object to and/or from a register. */
1940 else if (classes[i] != NO_REGS)
1942 if (recog_data.operand_type[i] != OP_OUT)
1943 alt_cost += copy_cost (op, mode, classes[i], 1, NULL);
1945 if (recog_data.operand_type[i] != OP_IN)
1946 alt_cost += copy_cost (op, mode, classes[i], 0, NULL);
1949 /* The only other way this alternative can be used is if this is a
1950 constant that could be placed into memory. */
1952 else if (CONSTANT_P (op) && (allows_addr || allows_mem[i]))
1953 alt_cost += MEMORY_MOVE_COST (mode, classes[i], 1);
1954 else
1955 alt_fail = 1;
1958 if (alt_fail)
1959 continue;
1961 if (!recog_data.alternative_enabled_p[alt])
1962 continue;
1964 /* Finally, update the costs with the information we've calculated
1965 about this alternative. */
1967 for (i = 0; i < n_ops; i++)
1968 if (REG_P (ops[i])
1969 && REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
1971 struct costs *pp = &op_costs[i], *qq = &this_op_costs[i];
1972 int scale = 1 + (recog_data.operand_type[i] == OP_INOUT);
1974 pp->mem_cost = MIN (pp->mem_cost,
1975 (qq->mem_cost + alt_cost) * scale);
1977 for (rclass = 0; rclass < N_REG_CLASSES; rclass++)
1978 pp->cost[rclass] = MIN (pp->cost[rclass],
1979 (qq->cost[rclass] + alt_cost) * scale);
1983 /* If this insn is a single set copying operand 1 to operand 0
1984 and one operand is a pseudo with the other a hard reg or a pseudo
1985 that prefers a register that is in its own register class then
1986 we may want to adjust the cost of that register class to -1.
1988 Avoid the adjustment if the source does not die to avoid stressing of
1989 register allocator by preferencing two colliding registers into single
1990 class.
1992 Also avoid the adjustment if a copy between registers of the class
1993 is expensive (ten times the cost of a default copy is considered
1994 arbitrarily expensive). This avoids losing when the preferred class
1995 is very expensive as the source of a copy instruction. */
1997 if ((set = single_set (insn)) != 0
1998 && ops[0] == SET_DEST (set) && ops[1] == SET_SRC (set)
1999 && REG_P (ops[0]) && REG_P (ops[1])
2000 && find_regno_note (insn, REG_DEAD, REGNO (ops[1])))
2001 for (i = 0; i <= 1; i++)
2002 if (REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
2004 unsigned int regno = REGNO (ops[!i]);
2005 enum machine_mode mode = GET_MODE (ops[!i]);
2006 int rclass;
2008 if (regno >= FIRST_PSEUDO_REGISTER && reg_pref != 0
2009 && reg_pref[regno].prefclass != NO_REGS)
2011 enum reg_class pref = reg_pref[regno].prefclass;
2013 if ((reg_class_size[(unsigned char) pref]
2014 == (unsigned) CLASS_MAX_NREGS (pref, mode))
2015 && REGISTER_MOVE_COST (mode, pref, pref) < 10 * 2)
2016 op_costs[i].cost[(unsigned char) pref] = -1;
2018 else if (regno < FIRST_PSEUDO_REGISTER)
2019 for (rclass = 0; rclass < N_REG_CLASSES; rclass++)
2020 if (TEST_HARD_REG_BIT (reg_class_contents[rclass], regno)
2021 && reg_class_size[rclass] == (unsigned) CLASS_MAX_NREGS (rclass, mode))
2023 if (reg_class_size[rclass] == 1)
2024 op_costs[i].cost[rclass] = -1;
2025 else if (in_hard_reg_set_p (reg_class_contents[rclass],
2026 mode, regno))
2027 op_costs[i].cost[rclass] = -1;
2032 /* Compute the cost of loading X into (if TO_P is nonzero) or from (if
2033 TO_P is zero) a register of class CLASS in mode MODE.
2035 X must not be a pseudo. */
2037 static int
2038 copy_cost (rtx x, enum machine_mode mode, enum reg_class rclass, int to_p,
2039 secondary_reload_info *prev_sri)
2041 enum reg_class secondary_class = NO_REGS;
2042 secondary_reload_info sri;
2044 /* If X is a SCRATCH, there is actually nothing to move since we are
2045 assuming optimal allocation. */
2047 if (GET_CODE (x) == SCRATCH)
2048 return 0;
2050 /* Get the class we will actually use for a reload. */
2051 rclass = PREFERRED_RELOAD_CLASS (x, rclass);
2053 /* If we need a secondary reload for an intermediate, the
2054 cost is that to load the input into the intermediate register, then
2055 to copy it. */
2057 sri.prev_sri = prev_sri;
2058 sri.extra_cost = 0;
2059 secondary_class = targetm.secondary_reload (to_p, x, rclass, mode, &sri);
2061 if (!move_cost[mode])
2062 init_move_cost (mode);
2064 if (secondary_class != NO_REGS)
2065 return (move_cost[mode][(int) secondary_class][(int) rclass]
2066 + sri.extra_cost
2067 + copy_cost (x, mode, secondary_class, to_p, &sri));
2069 /* For memory, use the memory move cost, for (hard) registers, use the
2070 cost to move between the register classes, and use 2 for everything
2071 else (constants). */
2073 if (MEM_P (x) || rclass == NO_REGS)
2074 return sri.extra_cost + MEMORY_MOVE_COST (mode, rclass, to_p);
2076 else if (REG_P (x))
2077 return (sri.extra_cost
2078 + move_cost[mode][(int) REGNO_REG_CLASS (REGNO (x))][(int) rclass]);
2080 else
2081 /* If this is a constant, we may eventually want to call rtx_cost here. */
2082 return sri.extra_cost + COSTS_N_INSNS (1);
2085 /* Record the pseudo registers we must reload into hard registers
2086 in a subexpression of a memory address, X.
2088 If CONTEXT is 0, we are looking at the base part of an address, otherwise we
2089 are looking at the index part.
2091 MODE is the mode of the memory reference; OUTER_CODE and INDEX_CODE
2092 give the context that the rtx appears in. These three arguments are
2093 passed down to base_reg_class.
2095 SCALE is twice the amount to multiply the cost by (it is twice so we
2096 can represent half-cost adjustments). */
2098 static void
2099 record_address_regs (enum machine_mode mode, rtx x, int context,
2100 enum rtx_code outer_code, enum rtx_code index_code,
2101 int scale)
2103 enum rtx_code code = GET_CODE (x);
2104 enum reg_class rclass;
2106 if (context == 1)
2107 rclass = INDEX_REG_CLASS;
2108 else
2109 rclass = base_reg_class (mode, outer_code, index_code);
2111 switch (code)
2113 case CONST_INT:
2114 case CONST:
2115 case CC0:
2116 case PC:
2117 case SYMBOL_REF:
2118 case LABEL_REF:
2119 return;
2121 case PLUS:
2122 /* When we have an address that is a sum,
2123 we must determine whether registers are "base" or "index" regs.
2124 If there is a sum of two registers, we must choose one to be
2125 the "base". Luckily, we can use the REG_POINTER to make a good
2126 choice most of the time. We only need to do this on machines
2127 that can have two registers in an address and where the base
2128 and index register classes are different.
2130 ??? This code used to set REGNO_POINTER_FLAG in some cases, but
2131 that seems bogus since it should only be set when we are sure
2132 the register is being used as a pointer. */
2135 rtx arg0 = XEXP (x, 0);
2136 rtx arg1 = XEXP (x, 1);
2137 enum rtx_code code0 = GET_CODE (arg0);
2138 enum rtx_code code1 = GET_CODE (arg1);
2140 /* Look inside subregs. */
2141 if (code0 == SUBREG)
2142 arg0 = SUBREG_REG (arg0), code0 = GET_CODE (arg0);
2143 if (code1 == SUBREG)
2144 arg1 = SUBREG_REG (arg1), code1 = GET_CODE (arg1);
2146 /* If this machine only allows one register per address, it must
2147 be in the first operand. */
2149 if (MAX_REGS_PER_ADDRESS == 1)
2150 record_address_regs (mode, arg0, 0, PLUS, code1, scale);
2152 /* If index and base registers are the same on this machine, just
2153 record registers in any non-constant operands. We assume here,
2154 as well as in the tests below, that all addresses are in
2155 canonical form. */
2157 else if (INDEX_REG_CLASS == base_reg_class (VOIDmode, PLUS, SCRATCH))
2159 record_address_regs (mode, arg0, context, PLUS, code1, scale);
2160 if (! CONSTANT_P (arg1))
2161 record_address_regs (mode, arg1, context, PLUS, code0, scale);
2164 /* If the second operand is a constant integer, it doesn't change
2165 what class the first operand must be. */
2167 else if (code1 == CONST_INT || code1 == CONST_DOUBLE)
2168 record_address_regs (mode, arg0, context, PLUS, code1, scale);
2170 /* If the second operand is a symbolic constant, the first operand
2171 must be an index register. */
2173 else if (code1 == SYMBOL_REF || code1 == CONST || code1 == LABEL_REF)
2174 record_address_regs (mode, arg0, 1, PLUS, code1, scale);
2176 /* If both operands are registers but one is already a hard register
2177 of index or reg-base class, give the other the class that the
2178 hard register is not. */
2180 else if (code0 == REG && code1 == REG
2181 && REGNO (arg0) < FIRST_PSEUDO_REGISTER
2182 && (ok_for_base_p_nonstrict (arg0, mode, PLUS, REG)
2183 || ok_for_index_p_nonstrict (arg0)))
2184 record_address_regs (mode, arg1,
2185 ok_for_base_p_nonstrict (arg0, mode, PLUS, REG)
2186 ? 1 : 0,
2187 PLUS, REG, scale);
2188 else if (code0 == REG && code1 == REG
2189 && REGNO (arg1) < FIRST_PSEUDO_REGISTER
2190 && (ok_for_base_p_nonstrict (arg1, mode, PLUS, REG)
2191 || ok_for_index_p_nonstrict (arg1)))
2192 record_address_regs (mode, arg0,
2193 ok_for_base_p_nonstrict (arg1, mode, PLUS, REG)
2194 ? 1 : 0,
2195 PLUS, REG, scale);
2197 /* If one operand is known to be a pointer, it must be the base
2198 with the other operand the index. Likewise if the other operand
2199 is a MULT. */
2201 else if ((code0 == REG && REG_POINTER (arg0))
2202 || code1 == MULT)
2204 record_address_regs (mode, arg0, 0, PLUS, code1, scale);
2205 record_address_regs (mode, arg1, 1, PLUS, code0, scale);
2207 else if ((code1 == REG && REG_POINTER (arg1))
2208 || code0 == MULT)
2210 record_address_regs (mode, arg0, 1, PLUS, code1, scale);
2211 record_address_regs (mode, arg1, 0, PLUS, code0, scale);
2214 /* Otherwise, count equal chances that each might be a base
2215 or index register. This case should be rare. */
2217 else
2219 record_address_regs (mode, arg0, 0, PLUS, code1, scale / 2);
2220 record_address_regs (mode, arg0, 1, PLUS, code1, scale / 2);
2221 record_address_regs (mode, arg1, 0, PLUS, code0, scale / 2);
2222 record_address_regs (mode, arg1, 1, PLUS, code0, scale / 2);
2225 break;
2227 /* Double the importance of a pseudo register that is incremented
2228 or decremented, since it would take two extra insns
2229 if it ends up in the wrong place. */
2230 case POST_MODIFY:
2231 case PRE_MODIFY:
2232 record_address_regs (mode, XEXP (x, 0), 0, code,
2233 GET_CODE (XEXP (XEXP (x, 1), 1)), 2 * scale);
2234 if (REG_P (XEXP (XEXP (x, 1), 1)))
2235 record_address_regs (mode, XEXP (XEXP (x, 1), 1), 1, code, REG,
2236 2 * scale);
2237 break;
2239 case POST_INC:
2240 case PRE_INC:
2241 case POST_DEC:
2242 case PRE_DEC:
2243 /* Double the importance of a pseudo register that is incremented
2244 or decremented, since it would take two extra insns
2245 if it ends up in the wrong place. If the operand is a pseudo,
2246 show it is being used in an INC_DEC context. */
2248 #ifdef FORBIDDEN_INC_DEC_CLASSES
2249 if (REG_P (XEXP (x, 0))
2250 && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER)
2251 in_inc_dec[REGNO (XEXP (x, 0))] = 1;
2252 #endif
2254 record_address_regs (mode, XEXP (x, 0), 0, code, SCRATCH, 2 * scale);
2255 break;
2257 case REG:
2259 struct costs *pp = &costs[REGNO (x)];
2260 int i;
2262 pp->mem_cost += (MEMORY_MOVE_COST (Pmode, rclass, 1) * scale) / 2;
2264 if (!move_cost[Pmode])
2265 init_move_cost (Pmode);
2266 for (i = 0; i < N_REG_CLASSES; i++)
2267 pp->cost[i] += (may_move_in_cost[Pmode][i][(int) rclass] * scale) / 2;
2269 break;
2271 default:
2273 const char *fmt = GET_RTX_FORMAT (code);
2274 int i;
2275 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2276 if (fmt[i] == 'e')
2277 record_address_regs (mode, XEXP (x, i), context, code, SCRATCH,
2278 scale);
2283 #ifdef FORBIDDEN_INC_DEC_CLASSES
2285 /* Return 1 if REG is valid as an auto-increment memory reference
2286 to an object of MODE. */
2288 static int
2289 auto_inc_dec_reg_p (rtx reg, enum machine_mode mode)
2291 if (HAVE_POST_INCREMENT
2292 && memory_address_p (mode, gen_rtx_POST_INC (Pmode, reg)))
2293 return 1;
2295 if (HAVE_POST_DECREMENT
2296 && memory_address_p (mode, gen_rtx_POST_DEC (Pmode, reg)))
2297 return 1;
2299 if (HAVE_PRE_INCREMENT
2300 && memory_address_p (mode, gen_rtx_PRE_INC (Pmode, reg)))
2301 return 1;
2303 if (HAVE_PRE_DECREMENT
2304 && memory_address_p (mode, gen_rtx_PRE_DEC (Pmode, reg)))
2305 return 1;
2307 return 0;
2309 #endif
2312 /* Allocate space for reg info. */
2313 void
2314 allocate_reg_info (void)
2316 int size = max_reg_num ();
2318 gcc_assert (! reg_pref && ! reg_renumber);
2319 reg_renumber = XNEWVEC (short, size);
2320 reg_pref = XCNEWVEC (struct reg_pref, size);
2321 memset (reg_renumber, -1, size * sizeof (short));
2325 /* Resize reg info. The new elements will be uninitialized. */
2326 void
2327 resize_reg_info (void)
2329 int size = max_reg_num ();
2331 gcc_assert (reg_pref && reg_renumber);
2332 reg_renumber = XRESIZEVEC (short, reg_renumber, size);
2333 reg_pref = XRESIZEVEC (struct reg_pref, reg_pref, size);
2337 /* Free up the space allocated by allocate_reg_info. */
2338 void
2339 free_reg_info (void)
2341 if (reg_pref)
2343 free (reg_pref);
2344 reg_pref = NULL;
2347 if (reg_renumber)
2349 free (reg_renumber);
2350 reg_renumber = NULL;
2357 /* Set up preferred and alternate classes for REGNO as PREFCLASS and
2358 ALTCLASS. */
2359 void
2360 setup_reg_classes (int regno,
2361 enum reg_class prefclass, enum reg_class altclass)
2363 if (reg_pref == NULL)
2364 return;
2365 reg_pref[regno].prefclass = prefclass;
2366 reg_pref[regno].altclass = altclass;
2370 /* This is the `regscan' pass of the compiler, run just before cse and
2371 again just before loop. It finds the first and last use of each
2372 pseudo-register. */
2374 void
2375 reg_scan (rtx f, unsigned int nregs ATTRIBUTE_UNUSED)
2377 rtx insn;
2379 timevar_push (TV_REG_SCAN);
2381 for (insn = f; insn; insn = NEXT_INSN (insn))
2382 if (INSN_P (insn))
2384 reg_scan_mark_refs (PATTERN (insn), insn);
2385 if (REG_NOTES (insn))
2386 reg_scan_mark_refs (REG_NOTES (insn), insn);
2389 timevar_pop (TV_REG_SCAN);
2393 /* X is the expression to scan. INSN is the insn it appears in.
2394 NOTE_FLAG is nonzero if X is from INSN's notes rather than its body.
2395 We should only record information for REGs with numbers
2396 greater than or equal to MIN_REGNO. */
2398 static void
2399 reg_scan_mark_refs (rtx x, rtx insn)
2401 enum rtx_code code;
2402 rtx dest;
2403 rtx note;
2405 if (!x)
2406 return;
2407 code = GET_CODE (x);
2408 switch (code)
2410 case CONST:
2411 case CONST_INT:
2412 case CONST_DOUBLE:
2413 case CONST_FIXED:
2414 case CONST_VECTOR:
2415 case CC0:
2416 case PC:
2417 case SYMBOL_REF:
2418 case LABEL_REF:
2419 case ADDR_VEC:
2420 case ADDR_DIFF_VEC:
2421 case REG:
2422 return;
2424 case EXPR_LIST:
2425 if (XEXP (x, 0))
2426 reg_scan_mark_refs (XEXP (x, 0), insn);
2427 if (XEXP (x, 1))
2428 reg_scan_mark_refs (XEXP (x, 1), insn);
2429 break;
2431 case INSN_LIST:
2432 if (XEXP (x, 1))
2433 reg_scan_mark_refs (XEXP (x, 1), insn);
2434 break;
2436 case CLOBBER:
2437 if (MEM_P (XEXP (x, 0)))
2438 reg_scan_mark_refs (XEXP (XEXP (x, 0), 0), insn);
2439 break;
2441 case SET:
2442 /* Count a set of the destination if it is a register. */
2443 for (dest = SET_DEST (x);
2444 GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
2445 || GET_CODE (dest) == ZERO_EXTEND;
2446 dest = XEXP (dest, 0))
2449 /* If this is setting a pseudo from another pseudo or the sum of a
2450 pseudo and a constant integer and the other pseudo is known to be
2451 a pointer, set the destination to be a pointer as well.
2453 Likewise if it is setting the destination from an address or from a
2454 value equivalent to an address or to the sum of an address and
2455 something else.
2457 But don't do any of this if the pseudo corresponds to a user
2458 variable since it should have already been set as a pointer based
2459 on the type. */
2461 if (REG_P (SET_DEST (x))
2462 && REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER
2463 /* If the destination pseudo is set more than once, then other
2464 sets might not be to a pointer value (consider access to a
2465 union in two threads of control in the presence of global
2466 optimizations). So only set REG_POINTER on the destination
2467 pseudo if this is the only set of that pseudo. */
2468 && DF_REG_DEF_COUNT (REGNO (SET_DEST (x))) == 1
2469 && ! REG_USERVAR_P (SET_DEST (x))
2470 && ! REG_POINTER (SET_DEST (x))
2471 && ((REG_P (SET_SRC (x))
2472 && REG_POINTER (SET_SRC (x)))
2473 || ((GET_CODE (SET_SRC (x)) == PLUS
2474 || GET_CODE (SET_SRC (x)) == LO_SUM)
2475 && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2476 && REG_P (XEXP (SET_SRC (x), 0))
2477 && REG_POINTER (XEXP (SET_SRC (x), 0)))
2478 || GET_CODE (SET_SRC (x)) == CONST
2479 || GET_CODE (SET_SRC (x)) == SYMBOL_REF
2480 || GET_CODE (SET_SRC (x)) == LABEL_REF
2481 || (GET_CODE (SET_SRC (x)) == HIGH
2482 && (GET_CODE (XEXP (SET_SRC (x), 0)) == CONST
2483 || GET_CODE (XEXP (SET_SRC (x), 0)) == SYMBOL_REF
2484 || GET_CODE (XEXP (SET_SRC (x), 0)) == LABEL_REF))
2485 || ((GET_CODE (SET_SRC (x)) == PLUS
2486 || GET_CODE (SET_SRC (x)) == LO_SUM)
2487 && (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST
2488 || GET_CODE (XEXP (SET_SRC (x), 1)) == SYMBOL_REF
2489 || GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF))
2490 || ((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
2491 && (GET_CODE (XEXP (note, 0)) == CONST
2492 || GET_CODE (XEXP (note, 0)) == SYMBOL_REF
2493 || GET_CODE (XEXP (note, 0)) == LABEL_REF))))
2494 REG_POINTER (SET_DEST (x)) = 1;
2496 /* If this is setting a register from a register or from a simple
2497 conversion of a register, propagate REG_EXPR. */
2498 if (REG_P (dest) && !REG_ATTRS (dest))
2500 rtx src = SET_SRC (x);
2502 while (GET_CODE (src) == SIGN_EXTEND
2503 || GET_CODE (src) == ZERO_EXTEND
2504 || GET_CODE (src) == TRUNCATE
2505 || (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)))
2506 src = XEXP (src, 0);
2508 set_reg_attrs_from_value (dest, src);
2511 /* ... fall through ... */
2513 default:
2515 const char *fmt = GET_RTX_FORMAT (code);
2516 int i;
2517 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2519 if (fmt[i] == 'e')
2520 reg_scan_mark_refs (XEXP (x, i), insn);
2521 else if (fmt[i] == 'E' && XVEC (x, i) != 0)
2523 int j;
2524 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2525 reg_scan_mark_refs (XVECEXP (x, i, j), insn);
2532 /* Return nonzero if C1 is a subset of C2, i.e., if every register in C1
2533 is also in C2. */
2536 reg_class_subset_p (enum reg_class c1, enum reg_class c2)
2538 return (c1 == c2
2539 || c2 == ALL_REGS
2540 || hard_reg_set_subset_p (reg_class_contents[(int) c1],
2541 reg_class_contents[(int) c2]));
2544 /* Return nonzero if there is a register that is in both C1 and C2. */
2547 reg_classes_intersect_p (enum reg_class c1, enum reg_class c2)
2549 return (c1 == c2
2550 || c1 == ALL_REGS
2551 || c2 == ALL_REGS
2552 || hard_reg_set_intersect_p (reg_class_contents[(int) c1],
2553 reg_class_contents[(int) c2]));
2556 #ifdef CANNOT_CHANGE_MODE_CLASS
2558 struct subregs_of_mode_node
2560 unsigned int block;
2561 unsigned char modes[MAX_MACHINE_MODE];
2564 static htab_t subregs_of_mode;
2566 static hashval_t
2567 som_hash (const void *x)
2569 const struct subregs_of_mode_node *const a =
2570 (const struct subregs_of_mode_node *) x;
2571 return a->block;
2574 static int
2575 som_eq (const void *x, const void *y)
2577 const struct subregs_of_mode_node *const a =
2578 (const struct subregs_of_mode_node *) x;
2579 const struct subregs_of_mode_node *const b =
2580 (const struct subregs_of_mode_node *) y;
2581 return a->block == b->block;
2585 static void
2586 record_subregs_of_mode (rtx subreg)
2588 struct subregs_of_mode_node dummy, *node;
2589 enum machine_mode mode;
2590 unsigned int regno;
2591 void **slot;
2593 if (!REG_P (SUBREG_REG (subreg)))
2594 return;
2596 regno = REGNO (SUBREG_REG (subreg));
2597 mode = GET_MODE (subreg);
2599 if (regno < FIRST_PSEUDO_REGISTER)
2600 return;
2602 dummy.block = regno & -8;
2603 slot = htab_find_slot_with_hash (subregs_of_mode, &dummy,
2604 dummy.block, INSERT);
2605 node = (struct subregs_of_mode_node *) *slot;
2606 if (node == NULL)
2608 node = XCNEW (struct subregs_of_mode_node);
2609 node->block = regno & -8;
2610 *slot = node;
2613 node->modes[mode] |= 1 << (regno & 7);
2617 /* Call record_subregs_of_mode for all the subregs in X. */
2619 static void
2620 find_subregs_of_mode (rtx x)
2622 enum rtx_code code = GET_CODE (x);
2623 const char * const fmt = GET_RTX_FORMAT (code);
2624 int i;
2626 if (code == SUBREG)
2627 record_subregs_of_mode (x);
2629 /* Time for some deep diving. */
2630 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2632 if (fmt[i] == 'e')
2633 find_subregs_of_mode (XEXP (x, i));
2634 else if (fmt[i] == 'E')
2636 int j;
2637 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2638 find_subregs_of_mode (XVECEXP (x, i, j));
2643 static unsigned int
2644 init_subregs_of_mode (void)
2646 basic_block bb;
2647 rtx insn;
2649 if (subregs_of_mode)
2650 htab_empty (subregs_of_mode);
2651 else
2652 subregs_of_mode = htab_create (100, som_hash, som_eq, free);
2654 FOR_EACH_BB (bb)
2655 FOR_BB_INSNS (bb, insn)
2656 if (INSN_P (insn))
2657 find_subregs_of_mode (PATTERN (insn));
2659 return 0;
2663 /* Set bits in *USED which correspond to registers which can't change
2664 their mode from FROM to any mode in which REGNO was encountered. */
2666 void
2667 cannot_change_mode_set_regs (HARD_REG_SET *used, enum machine_mode from,
2668 unsigned int regno)
2670 struct subregs_of_mode_node dummy, *node;
2671 enum machine_mode to;
2672 unsigned char mask;
2673 unsigned int i;
2675 gcc_assert (subregs_of_mode);
2676 dummy.block = regno & -8;
2677 node = (struct subregs_of_mode_node *)
2678 htab_find_with_hash (subregs_of_mode, &dummy, dummy.block);
2679 if (node == NULL)
2680 return;
2682 mask = 1 << (regno & 7);
2683 for (to = VOIDmode; to < NUM_MACHINE_MODES; to++)
2684 if (node->modes[to] & mask)
2685 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2686 if (!TEST_HARD_REG_BIT (*used, i)
2687 && REG_CANNOT_CHANGE_MODE_P (i, from, to))
2688 SET_HARD_REG_BIT (*used, i);
2691 /* Return 1 if REGNO has had an invalid mode change in CLASS from FROM
2692 mode. */
2694 bool
2695 invalid_mode_change_p (unsigned int regno,
2696 enum reg_class rclass ATTRIBUTE_UNUSED,
2697 enum machine_mode from)
2699 struct subregs_of_mode_node dummy, *node;
2700 enum machine_mode to;
2701 unsigned char mask;
2703 gcc_assert (subregs_of_mode);
2704 dummy.block = regno & -8;
2705 node = (struct subregs_of_mode_node *)
2706 htab_find_with_hash (subregs_of_mode, &dummy, dummy.block);
2707 if (node == NULL)
2708 return false;
2710 mask = 1 << (regno & 7);
2711 for (to = VOIDmode; to < NUM_MACHINE_MODES; to++)
2712 if (node->modes[to] & mask)
2713 if (CANNOT_CHANGE_MODE_CLASS (from, to, rclass))
2714 return true;
2716 return false;
2719 static unsigned int
2720 finish_subregs_of_mode (void)
2722 htab_delete (subregs_of_mode);
2723 subregs_of_mode = 0;
2724 return 0;
2726 #else
2727 static unsigned int
2728 init_subregs_of_mode (void)
2730 return 0;
2732 static unsigned int
2733 finish_subregs_of_mode (void)
2735 return 0;
2738 #endif /* CANNOT_CHANGE_MODE_CLASS */
2740 static bool
2741 gate_subregs_of_mode_init (void)
2743 #ifdef CANNOT_CHANGE_MODE_CLASS
2744 return true;
2745 #else
2746 return false;
2747 #endif
2750 struct rtl_opt_pass pass_subregs_of_mode_init =
2753 RTL_PASS,
2754 "subregs_of_mode_init", /* name */
2755 gate_subregs_of_mode_init, /* gate */
2756 init_subregs_of_mode, /* execute */
2757 NULL, /* sub */
2758 NULL, /* next */
2759 0, /* static_pass_number */
2760 0, /* tv_id */
2761 0, /* properties_required */
2762 0, /* properties_provided */
2763 0, /* properties_destroyed */
2764 0, /* todo_flags_start */
2765 0 /* todo_flags_finish */
2769 struct rtl_opt_pass pass_subregs_of_mode_finish =
2772 RTL_PASS,
2773 "subregs_of_mode_finish", /* name */
2774 gate_subregs_of_mode_init, /* gate */
2775 finish_subregs_of_mode, /* execute */
2776 NULL, /* sub */
2777 NULL, /* next */
2778 0, /* static_pass_number */
2779 0, /* tv_id */
2780 0, /* properties_required */
2781 0, /* properties_provided */
2782 0, /* properties_destroyed */
2783 0, /* todo_flags_start */
2784 0 /* todo_flags_finish */
2790 #include "gt-regclass.h"