* Makefile.in (reg-stack.o): Don't depend on gt-reg-stack.h.
[official-gcc.git] / gcc / regclass.c
blob0b9fd321f215ebcd85468e4c74fb201d1dafe91a
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
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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
24 /* This file contains two passes of the compiler: reg_scan and reg_class.
25 It also defines some tables of information about the hardware registers
26 and a function init_reg_sets to initialize the tables. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "hard-reg-set.h"
33 #include "rtl.h"
34 #include "expr.h"
35 #include "tm_p.h"
36 #include "flags.h"
37 #include "basic-block.h"
38 #include "regs.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"
51 static void init_reg_sets_1 (void);
52 static void init_reg_autoinc (void);
54 /* If we have auto-increment or auto-decrement and we can have secondary
55 reloads, we are not allowed to use classes requiring secondary
56 reloads for pseudos auto-incremented since reload can't handle it. */
57 /* We leave it to target hooks to decide if we have secondary reloads, so
58 assume that we might have them. */
59 #if defined(AUTO_INC_DEC) /* */
60 #define FORBIDDEN_INC_DEC_CLASSES
61 #endif
63 /* Register tables used by many passes. */
65 /* Indexed by hard register number, contains 1 for registers
66 that are fixed use (stack pointer, pc, frame pointer, etc.).
67 These are the registers that cannot be used to allocate
68 a pseudo reg for general use. */
70 char fixed_regs[FIRST_PSEUDO_REGISTER];
72 /* Same info as a HARD_REG_SET. */
74 HARD_REG_SET fixed_reg_set;
76 /* Data for initializing the above. */
78 static const char initial_fixed_regs[] = FIXED_REGISTERS;
80 /* Indexed by hard register number, contains 1 for registers
81 that are fixed use or are clobbered by function calls.
82 These are the registers that cannot be used to allocate
83 a pseudo reg whose life crosses calls unless we are able
84 to save/restore them across the calls. */
86 char call_used_regs[FIRST_PSEUDO_REGISTER];
88 /* Same info as a HARD_REG_SET. */
90 HARD_REG_SET call_used_reg_set;
92 /* HARD_REG_SET of registers we want to avoid caller saving. */
93 HARD_REG_SET losing_caller_save_reg_set;
95 /* Data for initializing the above. */
97 static const char initial_call_used_regs[] = CALL_USED_REGISTERS;
99 /* This is much like call_used_regs, except it doesn't have to
100 be a superset of FIXED_REGISTERS. This vector indicates
101 what is really call clobbered, and is used when defining
102 regs_invalidated_by_call. */
104 #ifdef CALL_REALLY_USED_REGISTERS
105 char call_really_used_regs[] = CALL_REALLY_USED_REGISTERS;
106 #endif
108 #ifdef CALL_REALLY_USED_REGISTERS
109 #define CALL_REALLY_USED_REGNO_P(X) call_really_used_regs[X]
110 #else
111 #define CALL_REALLY_USED_REGNO_P(X) call_used_regs[X]
112 #endif
115 /* Indexed by hard register number, contains 1 for registers that are
116 fixed use or call used registers that cannot hold quantities across
117 calls even if we are willing to save and restore them. call fixed
118 registers are a subset of call used registers. */
120 char call_fixed_regs[FIRST_PSEUDO_REGISTER];
122 /* The same info as a HARD_REG_SET. */
124 HARD_REG_SET call_fixed_reg_set;
126 /* Indexed by hard register number, contains 1 for registers
127 that are being used for global register decls.
128 These must be exempt from ordinary flow analysis
129 and are also considered fixed. */
131 char global_regs[FIRST_PSEUDO_REGISTER];
133 /* Contains 1 for registers that are set or clobbered by calls. */
134 /* ??? Ideally, this would be just call_used_regs plus global_regs, but
135 for someone's bright idea to have call_used_regs strictly include
136 fixed_regs. Which leaves us guessing as to the set of fixed_regs
137 that are actually preserved. We know for sure that those associated
138 with the local stack frame are safe, but scant others. */
140 HARD_REG_SET regs_invalidated_by_call;
142 /* Table of register numbers in the order in which to try to use them. */
143 #ifdef REG_ALLOC_ORDER
144 int reg_alloc_order[FIRST_PSEUDO_REGISTER] = REG_ALLOC_ORDER;
146 /* The inverse of reg_alloc_order. */
147 int inv_reg_alloc_order[FIRST_PSEUDO_REGISTER];
148 #endif
150 /* For each reg class, a HARD_REG_SET saying which registers are in it. */
152 HARD_REG_SET reg_class_contents[N_REG_CLASSES];
154 /* The same information, but as an array of unsigned ints. We copy from
155 these unsigned ints to the table above. We do this so the tm.h files
156 do not have to be aware of the wordsize for machines with <= 64 regs.
157 Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
159 #define N_REG_INTS \
160 ((FIRST_PSEUDO_REGISTER + (32 - 1)) / 32)
162 static const unsigned int_reg_class_contents[N_REG_CLASSES][N_REG_INTS]
163 = REG_CLASS_CONTENTS;
165 /* For each reg class, number of regs it contains. */
167 unsigned int reg_class_size[N_REG_CLASSES];
169 /* For each reg class, table listing all the containing classes. */
171 static enum reg_class reg_class_superclasses[N_REG_CLASSES][N_REG_CLASSES];
173 /* For each reg class, table listing all the classes contained in it. */
175 static enum reg_class reg_class_subclasses[N_REG_CLASSES][N_REG_CLASSES];
177 /* For each pair of reg classes,
178 a largest reg class contained in their union. */
180 enum reg_class reg_class_subunion[N_REG_CLASSES][N_REG_CLASSES];
182 /* For each pair of reg classes,
183 the smallest reg class containing their union. */
185 enum reg_class reg_class_superunion[N_REG_CLASSES][N_REG_CLASSES];
187 /* Array containing all of the register names. */
189 const char * reg_names[] = REGISTER_NAMES;
191 /* Array containing all of the register class names. */
193 const char * reg_class_names[] = REG_CLASS_NAMES;
195 /* For each hard register, the widest mode object that it can contain.
196 This will be a MODE_INT mode if the register can hold integers. Otherwise
197 it will be a MODE_FLOAT or a MODE_CC mode, whichever is valid for the
198 register. */
200 enum machine_mode reg_raw_mode[FIRST_PSEUDO_REGISTER];
202 /* 1 if there is a register of given mode. */
204 bool have_regs_of_mode [MAX_MACHINE_MODE];
206 /* 1 if class does contain register of given mode. */
208 static char contains_reg_of_mode [N_REG_CLASSES] [MAX_MACHINE_MODE];
210 /* Maximum cost of moving from a register in one class to a register in
211 another class. Based on REGISTER_MOVE_COST. */
213 static int move_cost[MAX_MACHINE_MODE][N_REG_CLASSES][N_REG_CLASSES];
215 /* Similar, but here we don't have to move if the first index is a subset
216 of the second so in that case the cost is zero. */
218 static int may_move_in_cost[MAX_MACHINE_MODE][N_REG_CLASSES][N_REG_CLASSES];
220 /* Similar, but here we don't have to move if the first index is a superset
221 of the second so in that case the cost is zero. */
223 static int may_move_out_cost[MAX_MACHINE_MODE][N_REG_CLASSES][N_REG_CLASSES];
225 #ifdef FORBIDDEN_INC_DEC_CLASSES
227 /* These are the classes that regs which are auto-incremented or decremented
228 cannot be put in. */
230 static int forbidden_inc_dec_class[N_REG_CLASSES];
232 /* Indexed by n, is nonzero if (REG n) is used in an auto-inc or auto-dec
233 context. */
235 static char *in_inc_dec;
237 #endif /* FORBIDDEN_INC_DEC_CLASSES */
239 /* Sample MEM values for use by memory_move_secondary_cost. */
241 static GTY(()) rtx top_of_stack[MAX_MACHINE_MODE];
243 /* Linked list of reg_info structures allocated for reg_n_info array.
244 Grouping all of the allocated structures together in one lump
245 means only one call to bzero to clear them, rather than n smaller
246 calls. */
247 struct reg_info_data {
248 struct reg_info_data *next; /* next set of reg_info structures */
249 size_t min_index; /* minimum index # */
250 size_t max_index; /* maximum index # */
251 char used_p; /* nonzero if this has been used previously */
252 reg_info data[1]; /* beginning of the reg_info data */
255 static struct reg_info_data *reg_info_head;
257 /* No more global register variables may be declared; true once
258 regclass has been initialized. */
260 static int no_global_reg_vars = 0;
262 /* Specify number of hard registers given machine mode occupy. */
263 unsigned char hard_regno_nregs[FIRST_PSEUDO_REGISTER][MAX_MACHINE_MODE];
265 /* Function called only once to initialize the above data on reg usage.
266 Once this is done, various switches may override. */
268 void
269 init_reg_sets (void)
271 int i, j;
273 /* First copy the register information from the initial int form into
274 the regsets. */
276 for (i = 0; i < N_REG_CLASSES; i++)
278 CLEAR_HARD_REG_SET (reg_class_contents[i]);
280 /* Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
281 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
282 if (int_reg_class_contents[i][j / 32]
283 & ((unsigned) 1 << (j % 32)))
284 SET_HARD_REG_BIT (reg_class_contents[i], j);
287 /* Sanity check: make sure the target macros FIXED_REGISTERS and
288 CALL_USED_REGISTERS had the right number of initializers. */
289 gcc_assert (sizeof fixed_regs == sizeof initial_fixed_regs);
290 gcc_assert (sizeof call_used_regs == sizeof initial_call_used_regs);
292 memcpy (fixed_regs, initial_fixed_regs, sizeof fixed_regs);
293 memcpy (call_used_regs, initial_call_used_regs, sizeof call_used_regs);
294 memset (global_regs, 0, sizeof global_regs);
296 #ifdef REG_ALLOC_ORDER
297 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
298 inv_reg_alloc_order[reg_alloc_order[i]] = i;
299 #endif
302 /* After switches have been processed, which perhaps alter
303 `fixed_regs' and `call_used_regs', convert them to HARD_REG_SETs. */
305 static void
306 init_reg_sets_1 (void)
308 unsigned int i, j;
309 unsigned int /* enum machine_mode */ m;
311 /* This macro allows the fixed or call-used registers
312 and the register classes to depend on target flags. */
314 #ifdef CONDITIONAL_REGISTER_USAGE
315 CONDITIONAL_REGISTER_USAGE;
316 #endif
318 /* Compute number of hard regs in each class. */
320 memset (reg_class_size, 0, sizeof reg_class_size);
321 for (i = 0; i < N_REG_CLASSES; i++)
322 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
323 if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
324 reg_class_size[i]++;
326 /* Initialize the table of subunions.
327 reg_class_subunion[I][J] gets the largest-numbered reg-class
328 that is contained in the union of classes I and J. */
330 for (i = 0; i < N_REG_CLASSES; i++)
332 for (j = 0; j < N_REG_CLASSES; j++)
334 HARD_REG_SET c;
335 int k;
337 COPY_HARD_REG_SET (c, reg_class_contents[i]);
338 IOR_HARD_REG_SET (c, reg_class_contents[j]);
339 for (k = 0; k < N_REG_CLASSES; k++)
341 GO_IF_HARD_REG_SUBSET (reg_class_contents[k], c,
342 subclass1);
343 continue;
345 subclass1:
346 /* Keep the largest subclass. */ /* SPEE 900308 */
347 GO_IF_HARD_REG_SUBSET (reg_class_contents[k],
348 reg_class_contents[(int) reg_class_subunion[i][j]],
349 subclass2);
350 reg_class_subunion[i][j] = (enum reg_class) k;
351 subclass2:
357 /* Initialize the table of superunions.
358 reg_class_superunion[I][J] gets the smallest-numbered reg-class
359 containing the union of classes I and J. */
361 for (i = 0; i < N_REG_CLASSES; i++)
363 for (j = 0; j < N_REG_CLASSES; j++)
365 HARD_REG_SET c;
366 int k;
368 COPY_HARD_REG_SET (c, reg_class_contents[i]);
369 IOR_HARD_REG_SET (c, reg_class_contents[j]);
370 for (k = 0; k < N_REG_CLASSES; k++)
371 GO_IF_HARD_REG_SUBSET (c, reg_class_contents[k], superclass);
373 superclass:
374 reg_class_superunion[i][j] = (enum reg_class) k;
378 /* Initialize the tables of subclasses and superclasses of each reg class.
379 First clear the whole table, then add the elements as they are found. */
381 for (i = 0; i < N_REG_CLASSES; i++)
383 for (j = 0; j < N_REG_CLASSES; j++)
385 reg_class_superclasses[i][j] = LIM_REG_CLASSES;
386 reg_class_subclasses[i][j] = LIM_REG_CLASSES;
390 for (i = 0; i < N_REG_CLASSES; i++)
392 if (i == (int) NO_REGS)
393 continue;
395 for (j = i + 1; j < N_REG_CLASSES; j++)
397 enum reg_class *p;
399 GO_IF_HARD_REG_SUBSET (reg_class_contents[i], reg_class_contents[j],
400 subclass);
401 continue;
402 subclass:
403 /* Reg class I is a subclass of J.
404 Add J to the table of superclasses of I. */
405 p = &reg_class_superclasses[i][0];
406 while (*p != LIM_REG_CLASSES) p++;
407 *p = (enum reg_class) j;
408 /* Add I to the table of superclasses of J. */
409 p = &reg_class_subclasses[j][0];
410 while (*p != LIM_REG_CLASSES) p++;
411 *p = (enum reg_class) i;
415 /* Initialize "constant" tables. */
417 CLEAR_HARD_REG_SET (fixed_reg_set);
418 CLEAR_HARD_REG_SET (call_used_reg_set);
419 CLEAR_HARD_REG_SET (call_fixed_reg_set);
420 CLEAR_HARD_REG_SET (regs_invalidated_by_call);
422 memcpy (call_fixed_regs, fixed_regs, sizeof call_fixed_regs);
424 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
426 /* call_used_regs must include fixed_regs. */
427 gcc_assert (!fixed_regs[i] || call_used_regs[i]);
428 #ifdef CALL_REALLY_USED_REGISTERS
429 /* call_used_regs must include call_really_used_regs. */
430 gcc_assert (!call_really_used_regs[i] || call_used_regs[i]);
431 #endif
433 if (fixed_regs[i])
434 SET_HARD_REG_BIT (fixed_reg_set, i);
436 if (call_used_regs[i])
437 SET_HARD_REG_BIT (call_used_reg_set, i);
438 if (call_fixed_regs[i])
439 SET_HARD_REG_BIT (call_fixed_reg_set, i);
440 if (CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (i)))
441 SET_HARD_REG_BIT (losing_caller_save_reg_set, i);
443 /* There are a couple of fixed registers that we know are safe to
444 exclude from being clobbered by calls:
446 The frame pointer is always preserved across calls. The arg pointer
447 is if it is fixed. The stack pointer usually is, unless
448 RETURN_POPS_ARGS, in which case an explicit CLOBBER will be present.
449 If we are generating PIC code, the PIC offset table register is
450 preserved across calls, though the target can override that. */
452 if (i == STACK_POINTER_REGNUM)
454 else if (global_regs[i])
455 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
456 else if (i == FRAME_POINTER_REGNUM)
458 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
459 else if (i == HARD_FRAME_POINTER_REGNUM)
461 #endif
462 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
463 else if (i == ARG_POINTER_REGNUM && fixed_regs[i])
465 #endif
466 #ifndef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
467 else if (i == (unsigned) PIC_OFFSET_TABLE_REGNUM && fixed_regs[i])
469 #endif
470 else if (CALL_REALLY_USED_REGNO_P (i))
471 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
474 memset (have_regs_of_mode, 0, sizeof (have_regs_of_mode));
475 memset (contains_reg_of_mode, 0, sizeof (contains_reg_of_mode));
476 for (m = 0; m < (unsigned int) MAX_MACHINE_MODE; m++)
477 for (i = 0; i < N_REG_CLASSES; i++)
478 if ((unsigned) CLASS_MAX_NREGS (i, m) <= reg_class_size[i])
479 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
480 if (!fixed_regs [j] && TEST_HARD_REG_BIT (reg_class_contents[i], j)
481 && HARD_REGNO_MODE_OK (j, m))
483 contains_reg_of_mode [i][m] = 1;
484 have_regs_of_mode [m] = 1;
485 break;
488 /* Initialize the move cost table. Find every subset of each class
489 and take the maximum cost of moving any subset to any other. */
491 for (m = 0; m < (unsigned int) MAX_MACHINE_MODE; m++)
492 if (have_regs_of_mode [m])
494 for (i = 0; i < N_REG_CLASSES; i++)
495 if (contains_reg_of_mode [i][m])
496 for (j = 0; j < N_REG_CLASSES; j++)
498 int cost;
499 enum reg_class *p1, *p2;
501 if (!contains_reg_of_mode [j][m])
503 move_cost[m][i][j] = 65536;
504 may_move_in_cost[m][i][j] = 65536;
505 may_move_out_cost[m][i][j] = 65536;
507 else
509 cost = REGISTER_MOVE_COST (m, i, j);
511 for (p2 = &reg_class_subclasses[j][0];
512 *p2 != LIM_REG_CLASSES;
513 p2++)
514 if (*p2 != i && contains_reg_of_mode [*p2][m])
515 cost = MAX (cost, move_cost [m][i][*p2]);
517 for (p1 = &reg_class_subclasses[i][0];
518 *p1 != LIM_REG_CLASSES;
519 p1++)
520 if (*p1 != j && contains_reg_of_mode [*p1][m])
521 cost = MAX (cost, move_cost [m][*p1][j]);
523 move_cost[m][i][j] = cost;
525 if (reg_class_subset_p (i, j))
526 may_move_in_cost[m][i][j] = 0;
527 else
528 may_move_in_cost[m][i][j] = cost;
530 if (reg_class_subset_p (j, i))
531 may_move_out_cost[m][i][j] = 0;
532 else
533 may_move_out_cost[m][i][j] = cost;
536 else
537 for (j = 0; j < N_REG_CLASSES; j++)
539 move_cost[m][i][j] = 65536;
540 may_move_in_cost[m][i][j] = 65536;
541 may_move_out_cost[m][i][j] = 65536;
546 /* Compute the table of register modes.
547 These values are used to record death information for individual registers
548 (as opposed to a multi-register mode). */
550 void
551 init_reg_modes_once (void)
553 int i, j;
555 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
556 for (j = 0; j < MAX_MACHINE_MODE; j++)
557 hard_regno_nregs[i][j] = HARD_REGNO_NREGS(i, (enum machine_mode)j);
559 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
561 reg_raw_mode[i] = choose_hard_reg_mode (i, 1, false);
563 /* If we couldn't find a valid mode, just use the previous mode.
564 ??? One situation in which we need to do this is on the mips where
565 HARD_REGNO_NREGS (fpreg, [SD]Fmode) returns 2. Ideally we'd like
566 to use DF mode for the even registers and VOIDmode for the odd
567 (for the cpu models where the odd ones are inaccessible). */
568 if (reg_raw_mode[i] == VOIDmode)
569 reg_raw_mode[i] = i == 0 ? word_mode : reg_raw_mode[i-1];
573 /* Finish initializing the register sets and
574 initialize the register modes. */
576 void
577 init_regs (void)
579 /* This finishes what was started by init_reg_sets, but couldn't be done
580 until after register usage was specified. */
581 init_reg_sets_1 ();
583 init_reg_autoinc ();
586 /* Initialize some fake stack-frame MEM references for use in
587 memory_move_secondary_cost. */
589 void
590 init_fake_stack_mems (void)
593 int i;
595 for (i = 0; i < MAX_MACHINE_MODE; i++)
596 top_of_stack[i] = gen_rtx_MEM (i, stack_pointer_rtx);
601 /* Compute extra cost of moving registers to/from memory due to reloads.
602 Only needed if secondary reloads are required for memory moves. */
605 memory_move_secondary_cost (enum machine_mode mode, enum reg_class class, int in)
607 enum reg_class altclass;
608 int partial_cost = 0;
609 /* We need a memory reference to feed to SECONDARY... macros. */
610 /* mem may be unused even if the SECONDARY_ macros are defined. */
611 rtx mem ATTRIBUTE_UNUSED = top_of_stack[(int) mode];
614 altclass = secondary_reload_class (in ? 1 : 0, class, mode, mem);
616 if (altclass == NO_REGS)
617 return 0;
619 if (in)
620 partial_cost = REGISTER_MOVE_COST (mode, altclass, class);
621 else
622 partial_cost = REGISTER_MOVE_COST (mode, class, altclass);
624 if (class == altclass)
625 /* This isn't simply a copy-to-temporary situation. Can't guess
626 what it is, so MEMORY_MOVE_COST really ought not to be calling
627 here in that case.
629 I'm tempted to put in an assert here, but returning this will
630 probably only give poor estimates, which is what we would've
631 had before this code anyways. */
632 return partial_cost;
634 /* Check if the secondary reload register will also need a
635 secondary reload. */
636 return memory_move_secondary_cost (mode, altclass, in) + partial_cost;
639 /* Return a machine mode that is legitimate for hard reg REGNO and large
640 enough to save nregs. If we can't find one, return VOIDmode.
641 If CALL_SAVED is true, only consider modes that are call saved. */
643 enum machine_mode
644 choose_hard_reg_mode (unsigned int regno ATTRIBUTE_UNUSED,
645 unsigned int nregs, bool call_saved)
647 unsigned int /* enum machine_mode */ m;
648 enum machine_mode found_mode = VOIDmode, mode;
650 /* We first look for the largest integer mode that can be validly
651 held in REGNO. If none, we look for the largest floating-point mode.
652 If we still didn't find a valid mode, try CCmode. */
654 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
655 mode != VOIDmode;
656 mode = GET_MODE_WIDER_MODE (mode))
657 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
658 && HARD_REGNO_MODE_OK (regno, mode)
659 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
660 found_mode = mode;
662 if (found_mode != VOIDmode)
663 return found_mode;
665 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
666 mode != VOIDmode;
667 mode = GET_MODE_WIDER_MODE (mode))
668 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
669 && HARD_REGNO_MODE_OK (regno, mode)
670 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
671 found_mode = mode;
673 if (found_mode != VOIDmode)
674 return found_mode;
676 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
677 mode != VOIDmode;
678 mode = GET_MODE_WIDER_MODE (mode))
679 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
680 && HARD_REGNO_MODE_OK (regno, mode)
681 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
682 found_mode = mode;
684 if (found_mode != VOIDmode)
685 return found_mode;
687 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
688 mode != VOIDmode;
689 mode = GET_MODE_WIDER_MODE (mode))
690 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
691 && HARD_REGNO_MODE_OK (regno, mode)
692 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
693 found_mode = mode;
695 if (found_mode != VOIDmode)
696 return found_mode;
698 /* Iterate over all of the CCmodes. */
699 for (m = (unsigned int) CCmode; m < (unsigned int) NUM_MACHINE_MODES; ++m)
701 mode = (enum machine_mode) m;
702 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
703 && HARD_REGNO_MODE_OK (regno, mode)
704 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
705 return mode;
708 /* We can't find a mode valid for this register. */
709 return VOIDmode;
712 /* Specify the usage characteristics of the register named NAME.
713 It should be a fixed register if FIXED and a
714 call-used register if CALL_USED. */
716 void
717 fix_register (const char *name, int fixed, int call_used)
719 int i;
721 /* Decode the name and update the primary form of
722 the register info. */
724 if ((i = decode_reg_name (name)) >= 0)
726 if ((i == STACK_POINTER_REGNUM
727 #ifdef HARD_FRAME_POINTER_REGNUM
728 || i == HARD_FRAME_POINTER_REGNUM
729 #else
730 || i == FRAME_POINTER_REGNUM
731 #endif
733 && (fixed == 0 || call_used == 0))
735 static const char * const what_option[2][2] = {
736 { "call-saved", "call-used" },
737 { "no-such-option", "fixed" }};
739 error ("can't use '%s' as a %s register", name,
740 what_option[fixed][call_used]);
742 else
744 fixed_regs[i] = fixed;
745 call_used_regs[i] = call_used;
746 #ifdef CALL_REALLY_USED_REGISTERS
747 if (fixed == 0)
748 call_really_used_regs[i] = call_used;
749 #endif
752 else
754 warning (0, "unknown register name: %s", name);
758 /* Mark register number I as global. */
760 void
761 globalize_reg (int i)
763 if (fixed_regs[i] == 0 && no_global_reg_vars)
764 error ("global register variable follows a function definition");
766 if (global_regs[i])
768 warning (0, "register used for two global register variables");
769 return;
772 if (call_used_regs[i] && ! fixed_regs[i])
773 warning (0, "call-clobbered register used for global register variable");
775 global_regs[i] = 1;
777 /* If we're globalizing the frame pointer, we need to set the
778 appropriate regs_invalidated_by_call bit, even if it's already
779 set in fixed_regs. */
780 if (i != STACK_POINTER_REGNUM)
781 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
783 /* If already fixed, nothing else to do. */
784 if (fixed_regs[i])
785 return;
787 fixed_regs[i] = call_used_regs[i] = call_fixed_regs[i] = 1;
788 #ifdef CALL_REALLY_USED_REGISTERS
789 call_really_used_regs[i] = 1;
790 #endif
792 SET_HARD_REG_BIT (fixed_reg_set, i);
793 SET_HARD_REG_BIT (call_used_reg_set, i);
794 SET_HARD_REG_BIT (call_fixed_reg_set, i);
797 /* Now the data and code for the `regclass' pass, which happens
798 just before local-alloc. */
800 /* The `costs' struct records the cost of using a hard register of each class
801 and of using memory for each pseudo. We use this data to set up
802 register class preferences. */
804 struct costs
806 int cost[N_REG_CLASSES];
807 int mem_cost;
810 /* Structure used to record preferences of given pseudo. */
811 struct reg_pref
813 /* (enum reg_class) prefclass is the preferred class. */
814 char prefclass;
816 /* altclass is a register class that we should use for allocating
817 pseudo if no register in the preferred class is available.
818 If no register in this class is available, memory is preferred.
820 It might appear to be more general to have a bitmask of classes here,
821 but since it is recommended that there be a class corresponding to the
822 union of most major pair of classes, that generality is not required. */
823 char altclass;
826 /* Record the cost of each class for each pseudo. */
828 static struct costs *costs;
830 /* Initialized once, and used to initialize cost values for each insn. */
832 static struct costs init_cost;
834 /* Record preferences of each pseudo.
835 This is available after `regclass' is run. */
837 static struct reg_pref *reg_pref;
839 /* Allocated buffers for reg_pref. */
841 static struct reg_pref *reg_pref_buffer;
843 /* Frequency of executions of current insn. */
845 static int frequency;
847 static rtx scan_one_insn (rtx, int);
848 static void record_operand_costs (rtx, struct costs *, struct reg_pref *);
849 static void dump_regclass (FILE *);
850 static void record_reg_classes (int, int, rtx *, enum machine_mode *,
851 const char **, rtx, struct costs *,
852 struct reg_pref *);
853 static int copy_cost (rtx, enum machine_mode, enum reg_class, int,
854 secondary_reload_info *);
855 static void record_address_regs (rtx, enum reg_class, int);
856 #ifdef FORBIDDEN_INC_DEC_CLASSES
857 static int auto_inc_dec_reg_p (rtx, enum machine_mode);
858 #endif
859 static void reg_scan_mark_refs (rtx, rtx, int);
861 /* Return the reg_class in which pseudo reg number REGNO is best allocated.
862 This function is sometimes called before the info has been computed.
863 When that happens, just return GENERAL_REGS, which is innocuous. */
865 enum reg_class
866 reg_preferred_class (int regno)
868 if (reg_pref == 0)
869 return GENERAL_REGS;
870 return (enum reg_class) reg_pref[regno].prefclass;
873 enum reg_class
874 reg_alternate_class (int regno)
876 if (reg_pref == 0)
877 return ALL_REGS;
879 return (enum reg_class) reg_pref[regno].altclass;
882 /* Initialize some global data for this pass. */
884 void
885 regclass_init (void)
887 int i;
889 init_cost.mem_cost = 10000;
890 for (i = 0; i < N_REG_CLASSES; i++)
891 init_cost.cost[i] = 10000;
893 /* This prevents dump_flow_info from losing if called
894 before regclass is run. */
895 reg_pref = NULL;
897 /* No more global register variables may be declared. */
898 no_global_reg_vars = 1;
901 /* Dump register costs. */
902 static void
903 dump_regclass (FILE *dump)
905 int i;
906 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
908 int /* enum reg_class */ class;
909 if (REG_N_REFS (i))
911 fprintf (dump, " Register %i costs:", i);
912 for (class = 0; class < (int) N_REG_CLASSES; class++)
913 if (contains_reg_of_mode [(enum reg_class) class][PSEUDO_REGNO_MODE (i)]
914 #ifdef FORBIDDEN_INC_DEC_CLASSES
915 && (!in_inc_dec[i]
916 || !forbidden_inc_dec_class[(enum reg_class) class])
917 #endif
918 #ifdef CANNOT_CHANGE_MODE_CLASS
919 && ! invalid_mode_change_p (i, (enum reg_class) class,
920 PSEUDO_REGNO_MODE (i))
921 #endif
923 fprintf (dump, " %s:%i", reg_class_names[class],
924 costs[i].cost[(enum reg_class) class]);
925 fprintf (dump, " MEM:%i\n", costs[i].mem_cost);
931 /* Calculate the costs of insn operands. */
933 static void
934 record_operand_costs (rtx insn, struct costs *op_costs,
935 struct reg_pref *reg_pref)
937 const char *constraints[MAX_RECOG_OPERANDS];
938 enum machine_mode modes[MAX_RECOG_OPERANDS];
939 int i;
941 for (i = 0; i < recog_data.n_operands; i++)
943 constraints[i] = recog_data.constraints[i];
944 modes[i] = recog_data.operand_mode[i];
947 /* If we get here, we are set up to record the costs of all the
948 operands for this insn. Start by initializing the costs.
949 Then handle any address registers. Finally record the desired
950 classes for any pseudos, doing it twice if some pair of
951 operands are commutative. */
953 for (i = 0; i < recog_data.n_operands; i++)
955 op_costs[i] = init_cost;
957 if (GET_CODE (recog_data.operand[i]) == SUBREG)
958 recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
960 if (MEM_P (recog_data.operand[i]))
961 record_address_regs (XEXP (recog_data.operand[i], 0),
962 MODE_BASE_REG_CLASS (modes[i]), frequency * 2);
963 else if (constraints[i][0] == 'p'
964 || EXTRA_ADDRESS_CONSTRAINT (constraints[i][0], constraints[i]))
965 record_address_regs (recog_data.operand[i],
966 MODE_BASE_REG_CLASS (modes[i]), frequency * 2);
969 /* Check for commutative in a separate loop so everything will
970 have been initialized. We must do this even if one operand
971 is a constant--see addsi3 in m68k.md. */
973 for (i = 0; i < (int) recog_data.n_operands - 1; i++)
974 if (constraints[i][0] == '%')
976 const char *xconstraints[MAX_RECOG_OPERANDS];
977 int j;
979 /* Handle commutative operands by swapping the constraints.
980 We assume the modes are the same. */
982 for (j = 0; j < recog_data.n_operands; j++)
983 xconstraints[j] = constraints[j];
985 xconstraints[i] = constraints[i+1];
986 xconstraints[i+1] = constraints[i];
987 record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
988 recog_data.operand, modes,
989 xconstraints, insn, op_costs, reg_pref);
992 record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
993 recog_data.operand, modes,
994 constraints, insn, op_costs, reg_pref);
997 /* Subroutine of regclass, processes one insn INSN. Scan it and record each
998 time it would save code to put a certain register in a certain class.
999 PASS, when nonzero, inhibits some optimizations which need only be done
1000 once.
1001 Return the last insn processed, so that the scan can be continued from
1002 there. */
1004 static rtx
1005 scan_one_insn (rtx insn, int pass)
1007 enum rtx_code pat_code;
1008 rtx set, note;
1009 int i, j;
1010 struct costs op_costs[MAX_RECOG_OPERANDS];
1012 if (!INSN_P (insn))
1013 return insn;
1015 pat_code = GET_CODE (PATTERN (insn));
1016 if (pat_code == USE
1017 || pat_code == CLOBBER
1018 || pat_code == ASM_INPUT
1019 || pat_code == ADDR_VEC
1020 || pat_code == ADDR_DIFF_VEC)
1021 return insn;
1023 set = single_set (insn);
1024 extract_insn (insn);
1026 /* If this insn loads a parameter from its stack slot, then
1027 it represents a savings, rather than a cost, if the
1028 parameter is stored in memory. Record this fact. */
1030 if (set != 0 && REG_P (SET_DEST (set))
1031 && MEM_P (SET_SRC (set))
1032 && (note = find_reg_note (insn, REG_EQUIV,
1033 NULL_RTX)) != 0
1034 && MEM_P (XEXP (note, 0)))
1036 costs[REGNO (SET_DEST (set))].mem_cost
1037 -= (MEMORY_MOVE_COST (GET_MODE (SET_DEST (set)),
1038 GENERAL_REGS, 1)
1039 * frequency);
1040 record_address_regs (XEXP (SET_SRC (set), 0),
1041 MODE_BASE_REG_CLASS (VOIDmode), frequency * 2);
1042 return insn;
1045 /* Improve handling of two-address insns such as
1046 (set X (ashift CONST Y)) where CONST must be made to
1047 match X. Change it into two insns: (set X CONST)
1048 (set X (ashift X Y)). If we left this for reloading, it
1049 would probably get three insns because X and Y might go
1050 in the same place. This prevents X and Y from receiving
1051 the same hard reg.
1053 We can only do this if the modes of operands 0 and 1
1054 (which might not be the same) are tieable and we only need
1055 do this during our first pass. */
1057 if (pass == 0 && optimize
1058 && recog_data.n_operands >= 3
1059 && recog_data.constraints[1][0] == '0'
1060 && recog_data.constraints[1][1] == 0
1061 && CONSTANT_P (recog_data.operand[1])
1062 && ! rtx_equal_p (recog_data.operand[0], recog_data.operand[1])
1063 && ! rtx_equal_p (recog_data.operand[0], recog_data.operand[2])
1064 && REG_P (recog_data.operand[0])
1065 && MODES_TIEABLE_P (GET_MODE (recog_data.operand[0]),
1066 recog_data.operand_mode[1]))
1068 rtx previnsn = prev_real_insn (insn);
1069 rtx dest
1070 = gen_lowpart (recog_data.operand_mode[1],
1071 recog_data.operand[0]);
1072 rtx newinsn
1073 = emit_insn_before (gen_move_insn (dest, recog_data.operand[1]), insn);
1075 /* If this insn was the start of a basic block,
1076 include the new insn in that block.
1077 We need not check for code_label here;
1078 while a basic block can start with a code_label,
1079 INSN could not be at the beginning of that block. */
1080 if (previnsn == 0 || JUMP_P (previnsn))
1082 basic_block b;
1083 FOR_EACH_BB (b)
1084 if (insn == BB_HEAD (b))
1085 BB_HEAD (b) = newinsn;
1088 /* This makes one more setting of new insns's dest. */
1089 REG_N_SETS (REGNO (recog_data.operand[0]))++;
1090 REG_N_REFS (REGNO (recog_data.operand[0]))++;
1091 REG_FREQ (REGNO (recog_data.operand[0])) += frequency;
1093 *recog_data.operand_loc[1] = recog_data.operand[0];
1094 REG_N_REFS (REGNO (recog_data.operand[0]))++;
1095 REG_FREQ (REGNO (recog_data.operand[0])) += frequency;
1096 for (i = recog_data.n_dups - 1; i >= 0; i--)
1097 if (recog_data.dup_num[i] == 1)
1099 *recog_data.dup_loc[i] = recog_data.operand[0];
1100 REG_N_REFS (REGNO (recog_data.operand[0]))++;
1101 REG_FREQ (REGNO (recog_data.operand[0])) += frequency;
1104 return PREV_INSN (newinsn);
1107 record_operand_costs (insn, op_costs, reg_pref);
1109 /* Now add the cost for each operand to the total costs for
1110 its register. */
1112 for (i = 0; i < recog_data.n_operands; i++)
1113 if (REG_P (recog_data.operand[i])
1114 && REGNO (recog_data.operand[i]) >= FIRST_PSEUDO_REGISTER)
1116 int regno = REGNO (recog_data.operand[i]);
1117 struct costs *p = &costs[regno], *q = &op_costs[i];
1119 p->mem_cost += q->mem_cost * frequency;
1120 for (j = 0; j < N_REG_CLASSES; j++)
1121 p->cost[j] += q->cost[j] * frequency;
1124 return insn;
1127 /* Initialize information about which register classes can be used for
1128 pseudos that are auto-incremented or auto-decremented. */
1130 static void
1131 init_reg_autoinc (void)
1133 #ifdef FORBIDDEN_INC_DEC_CLASSES
1134 int i;
1136 for (i = 0; i < N_REG_CLASSES; i++)
1138 rtx r = gen_rtx_raw_REG (VOIDmode, 0);
1139 enum machine_mode m;
1140 int j;
1142 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1143 if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
1145 REGNO (r) = j;
1147 for (m = VOIDmode; (int) m < (int) MAX_MACHINE_MODE;
1148 m = (enum machine_mode) ((int) m + 1))
1149 if (HARD_REGNO_MODE_OK (j, m))
1151 enum reg_class base_class = MODE_BASE_REG_CLASS (VOIDmode);
1153 PUT_MODE (r, m);
1155 /* If a register is not directly suitable for an
1156 auto-increment or decrement addressing mode and
1157 requires secondary reloads, disallow its class from
1158 being used in such addresses. */
1160 if ((secondary_reload_class (1, base_class, m, r)
1161 || secondary_reload_class (1, base_class, m, r))
1162 && ! auto_inc_dec_reg_p (r, m))
1163 forbidden_inc_dec_class[i] = 1;
1167 #endif /* FORBIDDEN_INC_DEC_CLASSES */
1170 /* This is a pass of the compiler that scans all instructions
1171 and calculates the preferred class for each pseudo-register.
1172 This information can be accessed later by calling `reg_preferred_class'.
1173 This pass comes just before local register allocation. */
1175 void
1176 regclass (rtx f, int nregs)
1178 rtx insn;
1179 int i;
1180 int pass;
1182 init_recog ();
1184 costs = XNEWVEC (struct costs, nregs);
1186 #ifdef FORBIDDEN_INC_DEC_CLASSES
1188 in_inc_dec = XNEWVEC (char, nregs);
1190 #endif /* FORBIDDEN_INC_DEC_CLASSES */
1192 /* Normally we scan the insns once and determine the best class to use for
1193 each register. However, if -fexpensive_optimizations are on, we do so
1194 twice, the second time using the tentative best classes to guide the
1195 selection. */
1197 for (pass = 0; pass <= flag_expensive_optimizations; pass++)
1199 basic_block bb;
1201 if (dump_file)
1202 fprintf (dump_file, "\n\nPass %i\n\n",pass);
1203 /* Zero out our accumulation of the cost of each class for each reg. */
1205 memset (costs, 0, nregs * sizeof (struct costs));
1207 #ifdef FORBIDDEN_INC_DEC_CLASSES
1208 memset (in_inc_dec, 0, nregs);
1209 #endif
1211 /* Scan the instructions and record each time it would
1212 save code to put a certain register in a certain class. */
1214 if (!optimize)
1216 frequency = REG_FREQ_MAX;
1217 for (insn = f; insn; insn = NEXT_INSN (insn))
1218 insn = scan_one_insn (insn, pass);
1220 else
1221 FOR_EACH_BB (bb)
1223 /* Show that an insn inside a loop is likely to be executed three
1224 times more than insns outside a loop. This is much more
1225 aggressive than the assumptions made elsewhere and is being
1226 tried as an experiment. */
1227 frequency = REG_FREQ_FROM_BB (bb);
1228 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
1230 insn = scan_one_insn (insn, pass);
1231 if (insn == BB_END (bb))
1232 break;
1236 /* Now for each register look at how desirable each class is
1237 and find which class is preferred. Store that in
1238 `prefclass'. Record in `altclass' the largest register
1239 class any of whose registers is better than memory. */
1241 if (pass == 0)
1242 reg_pref = reg_pref_buffer;
1244 if (dump_file)
1246 dump_regclass (dump_file);
1247 fprintf (dump_file,"\n");
1249 for (i = FIRST_PSEUDO_REGISTER; i < nregs; i++)
1251 int best_cost = (1 << (HOST_BITS_PER_INT - 2)) - 1;
1252 enum reg_class best = ALL_REGS, alt = NO_REGS;
1253 /* This is an enum reg_class, but we call it an int
1254 to save lots of casts. */
1255 int class;
1256 struct costs *p = &costs[i];
1258 /* In non-optimizing compilation REG_N_REFS is not initialized
1259 yet. */
1260 if (optimize && !REG_N_REFS (i) && !REG_N_SETS (i))
1261 continue;
1263 for (class = (int) ALL_REGS - 1; class > 0; class--)
1265 /* Ignore classes that are too small for this operand or
1266 invalid for an operand that was auto-incremented. */
1267 if (!contains_reg_of_mode [class][PSEUDO_REGNO_MODE (i)]
1268 #ifdef FORBIDDEN_INC_DEC_CLASSES
1269 || (in_inc_dec[i] && forbidden_inc_dec_class[class])
1270 #endif
1271 #ifdef CANNOT_CHANGE_MODE_CLASS
1272 || invalid_mode_change_p (i, (enum reg_class) class,
1273 PSEUDO_REGNO_MODE (i))
1274 #endif
1277 else if (p->cost[class] < best_cost)
1279 best_cost = p->cost[class];
1280 best = (enum reg_class) class;
1282 else if (p->cost[class] == best_cost)
1283 best = reg_class_subunion[(int) best][class];
1286 /* Record the alternate register class; i.e., a class for which
1287 every register in it is better than using memory. If adding a
1288 class would make a smaller class (i.e., no union of just those
1289 classes exists), skip that class. The major unions of classes
1290 should be provided as a register class. Don't do this if we
1291 will be doing it again later. */
1293 if ((pass == 1 || dump_file) || ! flag_expensive_optimizations)
1294 for (class = 0; class < N_REG_CLASSES; class++)
1295 if (p->cost[class] < p->mem_cost
1296 && (reg_class_size[(int) reg_class_subunion[(int) alt][class]]
1297 > reg_class_size[(int) alt])
1298 #ifdef FORBIDDEN_INC_DEC_CLASSES
1299 && ! (in_inc_dec[i] && forbidden_inc_dec_class[class])
1300 #endif
1301 #ifdef CANNOT_CHANGE_MODE_CLASS
1302 && ! invalid_mode_change_p (i, (enum reg_class) class,
1303 PSEUDO_REGNO_MODE (i))
1304 #endif
1306 alt = reg_class_subunion[(int) alt][class];
1308 /* If we don't add any classes, nothing to try. */
1309 if (alt == best)
1310 alt = NO_REGS;
1312 if (dump_file
1313 && (reg_pref[i].prefclass != (int) best
1314 || reg_pref[i].altclass != (int) alt))
1316 fprintf (dump_file, " Register %i", i);
1317 if (alt == ALL_REGS || best == ALL_REGS)
1318 fprintf (dump_file, " pref %s\n", reg_class_names[(int) best]);
1319 else if (alt == NO_REGS)
1320 fprintf (dump_file, " pref %s or none\n", reg_class_names[(int) best]);
1321 else
1322 fprintf (dump_file, " pref %s, else %s\n",
1323 reg_class_names[(int) best],
1324 reg_class_names[(int) alt]);
1327 /* We cast to (int) because (char) hits bugs in some compilers. */
1328 reg_pref[i].prefclass = (int) best;
1329 reg_pref[i].altclass = (int) alt;
1333 #ifdef FORBIDDEN_INC_DEC_CLASSES
1334 free (in_inc_dec);
1335 #endif
1336 free (costs);
1339 /* Record the cost of using memory or registers of various classes for
1340 the operands in INSN.
1342 N_ALTS is the number of alternatives.
1344 N_OPS is the number of operands.
1346 OPS is an array of the operands.
1348 MODES are the modes of the operands, in case any are VOIDmode.
1350 CONSTRAINTS are the constraints to use for the operands. This array
1351 is modified by this procedure.
1353 This procedure works alternative by alternative. For each alternative
1354 we assume that we will be able to allocate all pseudos to their ideal
1355 register class and calculate the cost of using that alternative. Then
1356 we compute for each operand that is a pseudo-register, the cost of
1357 having the pseudo allocated to each register class and using it in that
1358 alternative. To this cost is added the cost of the alternative.
1360 The cost of each class for this insn is its lowest cost among all the
1361 alternatives. */
1363 static void
1364 record_reg_classes (int n_alts, int n_ops, rtx *ops,
1365 enum machine_mode *modes, const char **constraints,
1366 rtx insn, struct costs *op_costs,
1367 struct reg_pref *reg_pref)
1369 int alt;
1370 int i, j;
1371 rtx set;
1373 /* Process each alternative, each time minimizing an operand's cost with
1374 the cost for each operand in that alternative. */
1376 for (alt = 0; alt < n_alts; alt++)
1378 struct costs this_op_costs[MAX_RECOG_OPERANDS];
1379 int alt_fail = 0;
1380 int alt_cost = 0;
1381 enum reg_class classes[MAX_RECOG_OPERANDS];
1382 int allows_mem[MAX_RECOG_OPERANDS];
1383 int class;
1385 for (i = 0; i < n_ops; i++)
1387 const char *p = constraints[i];
1388 rtx op = ops[i];
1389 enum machine_mode mode = modes[i];
1390 int allows_addr = 0;
1391 int win = 0;
1392 unsigned char c;
1394 /* Initially show we know nothing about the register class. */
1395 classes[i] = NO_REGS;
1396 allows_mem[i] = 0;
1398 /* If this operand has no constraints at all, we can conclude
1399 nothing about it since anything is valid. */
1401 if (*p == 0)
1403 if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
1404 memset (&this_op_costs[i], 0, sizeof this_op_costs[i]);
1406 continue;
1409 /* If this alternative is only relevant when this operand
1410 matches a previous operand, we do different things depending
1411 on whether this operand is a pseudo-reg or not. We must process
1412 any modifiers for the operand before we can make this test. */
1414 while (*p == '%' || *p == '=' || *p == '+' || *p == '&')
1415 p++;
1417 if (p[0] >= '0' && p[0] <= '0' + i && (p[1] == ',' || p[1] == 0))
1419 /* Copy class and whether memory is allowed from the matching
1420 alternative. Then perform any needed cost computations
1421 and/or adjustments. */
1422 j = p[0] - '0';
1423 classes[i] = classes[j];
1424 allows_mem[i] = allows_mem[j];
1426 if (!REG_P (op) || REGNO (op) < FIRST_PSEUDO_REGISTER)
1428 /* If this matches the other operand, we have no added
1429 cost and we win. */
1430 if (rtx_equal_p (ops[j], op))
1431 win = 1;
1433 /* If we can put the other operand into a register, add to
1434 the cost of this alternative the cost to copy this
1435 operand to the register used for the other operand. */
1437 else if (classes[j] != NO_REGS)
1439 alt_cost += copy_cost (op, mode, classes[j], 1, NULL);
1440 win = 1;
1443 else if (!REG_P (ops[j])
1444 || REGNO (ops[j]) < FIRST_PSEUDO_REGISTER)
1446 /* This op is a pseudo but the one it matches is not. */
1448 /* If we can't put the other operand into a register, this
1449 alternative can't be used. */
1451 if (classes[j] == NO_REGS)
1452 alt_fail = 1;
1454 /* Otherwise, add to the cost of this alternative the cost
1455 to copy the other operand to the register used for this
1456 operand. */
1458 else
1459 alt_cost += copy_cost (ops[j], mode, classes[j], 1, NULL);
1461 else
1463 /* The costs of this operand are not the same as the other
1464 operand since move costs are not symmetric. Moreover,
1465 if we cannot tie them, this alternative needs to do a
1466 copy, which is one instruction. */
1468 struct costs *pp = &this_op_costs[i];
1470 for (class = 0; class < N_REG_CLASSES; class++)
1471 pp->cost[class]
1472 = ((recog_data.operand_type[i] != OP_OUT
1473 ? may_move_in_cost[mode][class][(int) classes[i]]
1474 : 0)
1475 + (recog_data.operand_type[i] != OP_IN
1476 ? may_move_out_cost[mode][(int) classes[i]][class]
1477 : 0));
1479 /* If the alternative actually allows memory, make things
1480 a bit cheaper since we won't need an extra insn to
1481 load it. */
1483 pp->mem_cost
1484 = ((recog_data.operand_type[i] != OP_IN
1485 ? MEMORY_MOVE_COST (mode, classes[i], 0)
1486 : 0)
1487 + (recog_data.operand_type[i] != OP_OUT
1488 ? MEMORY_MOVE_COST (mode, classes[i], 1)
1489 : 0) - allows_mem[i]);
1491 /* If we have assigned a class to this register in our
1492 first pass, add a cost to this alternative corresponding
1493 to what we would add if this register were not in the
1494 appropriate class. */
1496 if (reg_pref)
1497 alt_cost
1498 += (may_move_in_cost[mode]
1499 [(unsigned char) reg_pref[REGNO (op)].prefclass]
1500 [(int) classes[i]]);
1502 if (REGNO (ops[i]) != REGNO (ops[j])
1503 && ! find_reg_note (insn, REG_DEAD, op))
1504 alt_cost += 2;
1506 /* This is in place of ordinary cost computation
1507 for this operand, so skip to the end of the
1508 alternative (should be just one character). */
1509 while (*p && *p++ != ',')
1512 constraints[i] = p;
1513 continue;
1517 /* Scan all the constraint letters. See if the operand matches
1518 any of the constraints. Collect the valid register classes
1519 and see if this operand accepts memory. */
1521 while ((c = *p))
1523 switch (c)
1525 case ',':
1526 break;
1527 case '*':
1528 /* Ignore the next letter for this pass. */
1529 c = *++p;
1530 break;
1532 case '?':
1533 alt_cost += 2;
1534 case '!': case '#': case '&':
1535 case '0': case '1': case '2': case '3': case '4':
1536 case '5': case '6': case '7': case '8': case '9':
1537 break;
1539 case 'p':
1540 allows_addr = 1;
1541 win = address_operand (op, GET_MODE (op));
1542 /* We know this operand is an address, so we want it to be
1543 allocated to a register that can be the base of an
1544 address, i.e. BASE_REG_CLASS. */
1545 classes[i]
1546 = reg_class_subunion[(int) classes[i]]
1547 [(int) MODE_BASE_REG_CLASS (VOIDmode)];
1548 break;
1550 case 'm': case 'o': case 'V':
1551 /* It doesn't seem worth distinguishing between offsettable
1552 and non-offsettable addresses here. */
1553 allows_mem[i] = 1;
1554 if (MEM_P (op))
1555 win = 1;
1556 break;
1558 case '<':
1559 if (MEM_P (op)
1560 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
1561 || GET_CODE (XEXP (op, 0)) == POST_DEC))
1562 win = 1;
1563 break;
1565 case '>':
1566 if (MEM_P (op)
1567 && (GET_CODE (XEXP (op, 0)) == PRE_INC
1568 || GET_CODE (XEXP (op, 0)) == POST_INC))
1569 win = 1;
1570 break;
1572 case 'E':
1573 case 'F':
1574 if (GET_CODE (op) == CONST_DOUBLE
1575 || (GET_CODE (op) == CONST_VECTOR
1576 && (GET_MODE_CLASS (GET_MODE (op))
1577 == MODE_VECTOR_FLOAT)))
1578 win = 1;
1579 break;
1581 case 'G':
1582 case 'H':
1583 if (GET_CODE (op) == CONST_DOUBLE
1584 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
1585 win = 1;
1586 break;
1588 case 's':
1589 if (GET_CODE (op) == CONST_INT
1590 || (GET_CODE (op) == CONST_DOUBLE
1591 && GET_MODE (op) == VOIDmode))
1592 break;
1593 case 'i':
1594 if (CONSTANT_P (op)
1595 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op)))
1596 win = 1;
1597 break;
1599 case 'n':
1600 if (GET_CODE (op) == CONST_INT
1601 || (GET_CODE (op) == CONST_DOUBLE
1602 && GET_MODE (op) == VOIDmode))
1603 win = 1;
1604 break;
1606 case 'I':
1607 case 'J':
1608 case 'K':
1609 case 'L':
1610 case 'M':
1611 case 'N':
1612 case 'O':
1613 case 'P':
1614 if (GET_CODE (op) == CONST_INT
1615 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
1616 win = 1;
1617 break;
1619 case 'X':
1620 win = 1;
1621 break;
1623 case 'g':
1624 if (MEM_P (op)
1625 || (CONSTANT_P (op)
1626 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))))
1627 win = 1;
1628 allows_mem[i] = 1;
1629 case 'r':
1630 classes[i]
1631 = reg_class_subunion[(int) classes[i]][(int) GENERAL_REGS];
1632 break;
1634 default:
1635 if (REG_CLASS_FROM_CONSTRAINT (c, p) != NO_REGS)
1636 classes[i]
1637 = reg_class_subunion[(int) classes[i]]
1638 [(int) REG_CLASS_FROM_CONSTRAINT (c, p)];
1639 #ifdef EXTRA_CONSTRAINT_STR
1640 else if (EXTRA_CONSTRAINT_STR (op, c, p))
1641 win = 1;
1643 if (EXTRA_MEMORY_CONSTRAINT (c, p))
1645 /* Every MEM can be reloaded to fit. */
1646 allows_mem[i] = 1;
1647 if (MEM_P (op))
1648 win = 1;
1650 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1652 /* Every address can be reloaded to fit. */
1653 allows_addr = 1;
1654 if (address_operand (op, GET_MODE (op)))
1655 win = 1;
1656 /* We know this operand is an address, so we want it to
1657 be allocated to a register that can be the base of an
1658 address, i.e. BASE_REG_CLASS. */
1659 classes[i]
1660 = reg_class_subunion[(int) classes[i]]
1661 [(int) MODE_BASE_REG_CLASS (VOIDmode)];
1663 #endif
1664 break;
1666 p += CONSTRAINT_LEN (c, p);
1667 if (c == ',')
1668 break;
1671 constraints[i] = p;
1673 /* How we account for this operand now depends on whether it is a
1674 pseudo register or not. If it is, we first check if any
1675 register classes are valid. If not, we ignore this alternative,
1676 since we want to assume that all pseudos get allocated for
1677 register preferencing. If some register class is valid, compute
1678 the costs of moving the pseudo into that class. */
1680 if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
1682 if (classes[i] == NO_REGS)
1684 /* We must always fail if the operand is a REG, but
1685 we did not find a suitable class.
1687 Otherwise we may perform an uninitialized read
1688 from this_op_costs after the `continue' statement
1689 below. */
1690 alt_fail = 1;
1692 else
1694 struct costs *pp = &this_op_costs[i];
1696 for (class = 0; class < N_REG_CLASSES; class++)
1697 pp->cost[class]
1698 = ((recog_data.operand_type[i] != OP_OUT
1699 ? may_move_in_cost[mode][class][(int) classes[i]]
1700 : 0)
1701 + (recog_data.operand_type[i] != OP_IN
1702 ? may_move_out_cost[mode][(int) classes[i]][class]
1703 : 0));
1705 /* If the alternative actually allows memory, make things
1706 a bit cheaper since we won't need an extra insn to
1707 load it. */
1709 pp->mem_cost
1710 = ((recog_data.operand_type[i] != OP_IN
1711 ? MEMORY_MOVE_COST (mode, classes[i], 0)
1712 : 0)
1713 + (recog_data.operand_type[i] != OP_OUT
1714 ? MEMORY_MOVE_COST (mode, classes[i], 1)
1715 : 0) - allows_mem[i]);
1717 /* If we have assigned a class to this register in our
1718 first pass, add a cost to this alternative corresponding
1719 to what we would add if this register were not in the
1720 appropriate class. */
1722 if (reg_pref)
1723 alt_cost
1724 += (may_move_in_cost[mode]
1725 [(unsigned char) reg_pref[REGNO (op)].prefclass]
1726 [(int) classes[i]]);
1730 /* Otherwise, if this alternative wins, either because we
1731 have already determined that or if we have a hard register of
1732 the proper class, there is no cost for this alternative. */
1734 else if (win
1735 || (REG_P (op)
1736 && reg_fits_class_p (op, classes[i], 0, GET_MODE (op))))
1739 /* If registers are valid, the cost of this alternative includes
1740 copying the object to and/or from a register. */
1742 else if (classes[i] != NO_REGS)
1744 if (recog_data.operand_type[i] != OP_OUT)
1745 alt_cost += copy_cost (op, mode, classes[i], 1, NULL);
1747 if (recog_data.operand_type[i] != OP_IN)
1748 alt_cost += copy_cost (op, mode, classes[i], 0, NULL);
1751 /* The only other way this alternative can be used is if this is a
1752 constant that could be placed into memory. */
1754 else if (CONSTANT_P (op) && (allows_addr || allows_mem[i]))
1755 alt_cost += MEMORY_MOVE_COST (mode, classes[i], 1);
1756 else
1757 alt_fail = 1;
1760 if (alt_fail)
1761 continue;
1763 /* Finally, update the costs with the information we've calculated
1764 about this alternative. */
1766 for (i = 0; i < n_ops; i++)
1767 if (REG_P (ops[i])
1768 && REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
1770 struct costs *pp = &op_costs[i], *qq = &this_op_costs[i];
1771 int scale = 1 + (recog_data.operand_type[i] == OP_INOUT);
1773 pp->mem_cost = MIN (pp->mem_cost,
1774 (qq->mem_cost + alt_cost) * scale);
1776 for (class = 0; class < N_REG_CLASSES; class++)
1777 pp->cost[class] = MIN (pp->cost[class],
1778 (qq->cost[class] + alt_cost) * scale);
1782 /* If this insn is a single set copying operand 1 to operand 0
1783 and one operand is a pseudo with the other a hard reg or a pseudo
1784 that prefers a register that is in its own register class then
1785 we may want to adjust the cost of that register class to -1.
1787 Avoid the adjustment if the source does not die to avoid stressing of
1788 register allocator by preferrencing two colliding registers into single
1789 class.
1791 Also avoid the adjustment if a copy between registers of the class
1792 is expensive (ten times the cost of a default copy is considered
1793 arbitrarily expensive). This avoids losing when the preferred class
1794 is very expensive as the source of a copy instruction. */
1796 if ((set = single_set (insn)) != 0
1797 && ops[0] == SET_DEST (set) && ops[1] == SET_SRC (set)
1798 && REG_P (ops[0]) && REG_P (ops[1])
1799 && find_regno_note (insn, REG_DEAD, REGNO (ops[1])))
1800 for (i = 0; i <= 1; i++)
1801 if (REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
1803 unsigned int regno = REGNO (ops[!i]);
1804 enum machine_mode mode = GET_MODE (ops[!i]);
1805 int class;
1806 unsigned int nr;
1808 if (regno >= FIRST_PSEUDO_REGISTER && reg_pref != 0)
1810 enum reg_class pref = reg_pref[regno].prefclass;
1812 if ((reg_class_size[(unsigned char) pref]
1813 == (unsigned) CLASS_MAX_NREGS (pref, mode))
1814 && REGISTER_MOVE_COST (mode, pref, pref) < 10 * 2)
1815 op_costs[i].cost[(unsigned char) pref] = -1;
1817 else if (regno < FIRST_PSEUDO_REGISTER)
1818 for (class = 0; class < N_REG_CLASSES; class++)
1819 if (TEST_HARD_REG_BIT (reg_class_contents[class], regno)
1820 && reg_class_size[class] == (unsigned) CLASS_MAX_NREGS (class, mode))
1822 if (reg_class_size[class] == 1)
1823 op_costs[i].cost[class] = -1;
1824 else
1826 for (nr = 0; nr < (unsigned) hard_regno_nregs[regno][mode]; nr++)
1828 if (! TEST_HARD_REG_BIT (reg_class_contents[class],
1829 regno + nr))
1830 break;
1833 if (nr == (unsigned) hard_regno_nregs[regno][mode])
1834 op_costs[i].cost[class] = -1;
1840 /* Compute the cost of loading X into (if TO_P is nonzero) or from (if
1841 TO_P is zero) a register of class CLASS in mode MODE.
1843 X must not be a pseudo. */
1845 static int
1846 copy_cost (rtx x, enum machine_mode mode, enum reg_class class, int to_p,
1847 secondary_reload_info *prev_sri)
1849 enum reg_class secondary_class = NO_REGS;
1850 secondary_reload_info sri;
1852 /* If X is a SCRATCH, there is actually nothing to move since we are
1853 assuming optimal allocation. */
1855 if (GET_CODE (x) == SCRATCH)
1856 return 0;
1858 /* Get the class we will actually use for a reload. */
1859 class = PREFERRED_RELOAD_CLASS (x, class);
1861 /* If we need a secondary reload for an intermediate, the
1862 cost is that to load the input into the intermediate register, then
1863 to copy it. */
1865 sri.prev_sri = prev_sri;
1866 sri.extra_cost = 0;
1867 secondary_class = targetm.secondary_reload (to_p, x, class, mode, &sri);
1869 if (secondary_class != NO_REGS)
1870 return (move_cost[mode][(int) secondary_class][(int) class]
1871 + sri.extra_cost
1872 + copy_cost (x, mode, secondary_class, to_p, &sri));
1874 /* For memory, use the memory move cost, for (hard) registers, use the
1875 cost to move between the register classes, and use 2 for everything
1876 else (constants). */
1878 if (MEM_P (x) || class == NO_REGS)
1879 return sri.extra_cost + MEMORY_MOVE_COST (mode, class, to_p);
1881 else if (REG_P (x))
1882 return (sri.extra_cost
1883 + move_cost[mode][(int) REGNO_REG_CLASS (REGNO (x))][(int) class]);
1885 else
1886 /* If this is a constant, we may eventually want to call rtx_cost here. */
1887 return sri.extra_cost + COSTS_N_INSNS (1);
1890 /* Record the pseudo registers we must reload into hard registers
1891 in a subexpression of a memory address, X.
1893 CLASS is the class that the register needs to be in and is either
1894 BASE_REG_CLASS or INDEX_REG_CLASS.
1896 SCALE is twice the amount to multiply the cost by (it is twice so we
1897 can represent half-cost adjustments). */
1899 static void
1900 record_address_regs (rtx x, enum reg_class class, int scale)
1902 enum rtx_code code = GET_CODE (x);
1904 switch (code)
1906 case CONST_INT:
1907 case CONST:
1908 case CC0:
1909 case PC:
1910 case SYMBOL_REF:
1911 case LABEL_REF:
1912 return;
1914 case PLUS:
1915 /* When we have an address that is a sum,
1916 we must determine whether registers are "base" or "index" regs.
1917 If there is a sum of two registers, we must choose one to be
1918 the "base". Luckily, we can use the REG_POINTER to make a good
1919 choice most of the time. We only need to do this on machines
1920 that can have two registers in an address and where the base
1921 and index register classes are different.
1923 ??? This code used to set REGNO_POINTER_FLAG in some cases, but
1924 that seems bogus since it should only be set when we are sure
1925 the register is being used as a pointer. */
1928 rtx arg0 = XEXP (x, 0);
1929 rtx arg1 = XEXP (x, 1);
1930 enum rtx_code code0 = GET_CODE (arg0);
1931 enum rtx_code code1 = GET_CODE (arg1);
1933 /* Look inside subregs. */
1934 if (code0 == SUBREG)
1935 arg0 = SUBREG_REG (arg0), code0 = GET_CODE (arg0);
1936 if (code1 == SUBREG)
1937 arg1 = SUBREG_REG (arg1), code1 = GET_CODE (arg1);
1939 /* If this machine only allows one register per address, it must
1940 be in the first operand. */
1942 if (MAX_REGS_PER_ADDRESS == 1)
1943 record_address_regs (arg0, class, scale);
1945 /* If index and base registers are the same on this machine, just
1946 record registers in any non-constant operands. We assume here,
1947 as well as in the tests below, that all addresses are in
1948 canonical form. */
1950 else if (INDEX_REG_CLASS == MODE_BASE_REG_CLASS (VOIDmode))
1952 record_address_regs (arg0, class, scale);
1953 if (! CONSTANT_P (arg1))
1954 record_address_regs (arg1, class, scale);
1957 /* If the second operand is a constant integer, it doesn't change
1958 what class the first operand must be. */
1960 else if (code1 == CONST_INT || code1 == CONST_DOUBLE)
1961 record_address_regs (arg0, class, scale);
1963 /* If the second operand is a symbolic constant, the first operand
1964 must be an index register. */
1966 else if (code1 == SYMBOL_REF || code1 == CONST || code1 == LABEL_REF)
1967 record_address_regs (arg0, INDEX_REG_CLASS, scale);
1969 /* If both operands are registers but one is already a hard register
1970 of index or reg-base class, give the other the class that the
1971 hard register is not. */
1973 else if (code0 == REG && code1 == REG
1974 && REGNO (arg0) < FIRST_PSEUDO_REGISTER
1975 && (REG_MODE_OK_FOR_REG_BASE_P (arg0, VOIDmode)
1976 || REG_OK_FOR_INDEX_P (arg0)))
1977 record_address_regs (arg1,
1978 REG_MODE_OK_FOR_REG_BASE_P (arg0, VOIDmode)
1979 ? INDEX_REG_CLASS
1980 : MODE_BASE_REG_REG_CLASS (VOIDmode),
1981 scale);
1982 else if (code0 == REG && code1 == REG
1983 && REGNO (arg1) < FIRST_PSEUDO_REGISTER
1984 && (REG_MODE_OK_FOR_REG_BASE_P (arg1, VOIDmode)
1985 || REG_OK_FOR_INDEX_P (arg1)))
1986 record_address_regs (arg0,
1987 REG_MODE_OK_FOR_REG_BASE_P (arg1, VOIDmode)
1988 ? INDEX_REG_CLASS
1989 : MODE_BASE_REG_REG_CLASS (VOIDmode),
1990 scale);
1992 /* If one operand is known to be a pointer, it must be the base
1993 with the other operand the index. Likewise if the other operand
1994 is a MULT. */
1996 else if ((code0 == REG && REG_POINTER (arg0))
1997 || code1 == MULT)
1999 record_address_regs (arg0, MODE_BASE_REG_REG_CLASS (VOIDmode),
2000 scale);
2001 record_address_regs (arg1, INDEX_REG_CLASS, scale);
2003 else if ((code1 == REG && REG_POINTER (arg1))
2004 || code0 == MULT)
2006 record_address_regs (arg0, INDEX_REG_CLASS, scale);
2007 record_address_regs (arg1, MODE_BASE_REG_REG_CLASS (VOIDmode),
2008 scale);
2011 /* Otherwise, count equal chances that each might be a base
2012 or index register. This case should be rare. */
2014 else
2016 record_address_regs (arg0, MODE_BASE_REG_REG_CLASS (VOIDmode),
2017 scale / 2);
2018 record_address_regs (arg0, INDEX_REG_CLASS, scale / 2);
2019 record_address_regs (arg1, MODE_BASE_REG_REG_CLASS (VOIDmode),
2020 scale / 2);
2021 record_address_regs (arg1, INDEX_REG_CLASS, scale / 2);
2024 break;
2026 /* Double the importance of a pseudo register that is incremented
2027 or decremented, since it would take two extra insns
2028 if it ends up in the wrong place. */
2029 case POST_MODIFY:
2030 case PRE_MODIFY:
2031 record_address_regs (XEXP (x, 0), MODE_BASE_REG_CLASS (VOIDmode),
2032 2 * scale);
2033 if (REG_P (XEXP (XEXP (x, 1), 1)))
2034 record_address_regs (XEXP (XEXP (x, 1), 1),
2035 INDEX_REG_CLASS, 2 * scale);
2036 break;
2038 case POST_INC:
2039 case PRE_INC:
2040 case POST_DEC:
2041 case PRE_DEC:
2042 /* Double the importance of a pseudo register that is incremented
2043 or decremented, since it would take two extra insns
2044 if it ends up in the wrong place. If the operand is a pseudo,
2045 show it is being used in an INC_DEC context. */
2047 #ifdef FORBIDDEN_INC_DEC_CLASSES
2048 if (REG_P (XEXP (x, 0))
2049 && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER)
2050 in_inc_dec[REGNO (XEXP (x, 0))] = 1;
2051 #endif
2053 record_address_regs (XEXP (x, 0), class, 2 * scale);
2054 break;
2056 case REG:
2058 struct costs *pp = &costs[REGNO (x)];
2059 int i;
2061 pp->mem_cost += (MEMORY_MOVE_COST (Pmode, class, 1) * scale) / 2;
2063 for (i = 0; i < N_REG_CLASSES; i++)
2064 pp->cost[i] += (may_move_in_cost[Pmode][i][(int) class] * scale) / 2;
2066 break;
2068 default:
2070 const char *fmt = GET_RTX_FORMAT (code);
2071 int i;
2072 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2073 if (fmt[i] == 'e')
2074 record_address_regs (XEXP (x, i), class, scale);
2079 #ifdef FORBIDDEN_INC_DEC_CLASSES
2081 /* Return 1 if REG is valid as an auto-increment memory reference
2082 to an object of MODE. */
2084 static int
2085 auto_inc_dec_reg_p (rtx reg, enum machine_mode mode)
2087 if (HAVE_POST_INCREMENT
2088 && memory_address_p (mode, gen_rtx_POST_INC (Pmode, reg)))
2089 return 1;
2091 if (HAVE_POST_DECREMENT
2092 && memory_address_p (mode, gen_rtx_POST_DEC (Pmode, reg)))
2093 return 1;
2095 if (HAVE_PRE_INCREMENT
2096 && memory_address_p (mode, gen_rtx_PRE_INC (Pmode, reg)))
2097 return 1;
2099 if (HAVE_PRE_DECREMENT
2100 && memory_address_p (mode, gen_rtx_PRE_DEC (Pmode, reg)))
2101 return 1;
2103 return 0;
2105 #endif
2107 static short *renumber;
2108 static size_t regno_allocated;
2109 static unsigned int reg_n_max;
2111 /* Allocate enough space to hold NUM_REGS registers for the tables used for
2112 reg_scan and flow_analysis that are indexed by the register number. If
2113 NEW_P is nonzero, initialize all of the registers, otherwise only
2114 initialize the new registers allocated. The same table is kept from
2115 function to function, only reallocating it when we need more room. If
2116 RENUMBER_P is nonzero, allocate the reg_renumber array also. */
2118 void
2119 allocate_reg_info (size_t num_regs, int new_p, int renumber_p)
2121 size_t size_info;
2122 size_t size_renumber;
2123 size_t min = (new_p) ? 0 : reg_n_max;
2124 struct reg_info_data *reg_data;
2126 if (num_regs > regno_allocated)
2128 size_t old_allocated = regno_allocated;
2130 regno_allocated = num_regs + (num_regs / 20); /* Add some slop space. */
2131 size_renumber = regno_allocated * sizeof (short);
2133 if (!reg_n_info)
2135 VARRAY_REG_INIT (reg_n_info, regno_allocated, "reg_n_info");
2136 renumber = xmalloc (size_renumber);
2137 reg_pref_buffer = XNEWVEC (struct reg_pref, regno_allocated);
2139 else
2141 VARRAY_GROW (reg_n_info, regno_allocated);
2143 if (new_p) /* If we're zapping everything, no need to realloc. */
2145 free ((char *) renumber);
2146 free ((char *) reg_pref);
2147 renumber = xmalloc (size_renumber);
2148 reg_pref_buffer = XNEWVEC (struct reg_pref, regno_allocated);
2151 else
2153 renumber = xrealloc (renumber, size_renumber);
2154 reg_pref_buffer = (struct reg_pref *) xrealloc (reg_pref_buffer,
2155 regno_allocated
2156 * sizeof (struct reg_pref));
2160 size_info = (regno_allocated - old_allocated) * sizeof (reg_info)
2161 + sizeof (struct reg_info_data) - sizeof (reg_info);
2162 reg_data = xcalloc (size_info, 1);
2163 reg_data->min_index = old_allocated;
2164 reg_data->max_index = regno_allocated - 1;
2165 reg_data->next = reg_info_head;
2166 reg_info_head = reg_data;
2169 reg_n_max = num_regs;
2170 if (min < num_regs)
2172 /* Loop through each of the segments allocated for the actual
2173 reg_info pages, and set up the pointers, zero the pages, etc. */
2174 for (reg_data = reg_info_head;
2175 reg_data && reg_data->max_index >= min;
2176 reg_data = reg_data->next)
2178 size_t min_index = reg_data->min_index;
2179 size_t max_index = reg_data->max_index;
2180 size_t max = MIN (max_index, num_regs);
2181 size_t local_min = min - min_index;
2182 size_t i;
2184 if (reg_data->min_index > num_regs)
2185 continue;
2187 if (min < min_index)
2188 local_min = 0;
2189 if (!reg_data->used_p) /* page just allocated with calloc */
2190 reg_data->used_p = 1; /* no need to zero */
2191 else
2192 memset (&reg_data->data[local_min], 0,
2193 sizeof (reg_info) * (max - min_index - local_min + 1));
2195 for (i = min_index+local_min; i <= max; i++)
2197 VARRAY_REG (reg_n_info, i) = &reg_data->data[i-min_index];
2198 REG_BASIC_BLOCK (i) = REG_BLOCK_UNKNOWN;
2199 renumber[i] = -1;
2200 reg_pref_buffer[i].prefclass = (char) NO_REGS;
2201 reg_pref_buffer[i].altclass = (char) NO_REGS;
2206 /* If {pref,alt}class have already been allocated, update the pointers to
2207 the newly realloced ones. */
2208 if (reg_pref)
2209 reg_pref = reg_pref_buffer;
2211 if (renumber_p)
2212 reg_renumber = renumber;
2215 /* Free up the space allocated by allocate_reg_info. */
2216 void
2217 free_reg_info (void)
2219 if (reg_n_info)
2221 struct reg_info_data *reg_data;
2222 struct reg_info_data *reg_next;
2224 VARRAY_FREE (reg_n_info);
2225 for (reg_data = reg_info_head; reg_data; reg_data = reg_next)
2227 reg_next = reg_data->next;
2228 free ((char *) reg_data);
2231 free (reg_pref_buffer);
2232 reg_pref_buffer = (struct reg_pref *) 0;
2233 reg_info_head = (struct reg_info_data *) 0;
2234 renumber = (short *) 0;
2236 regno_allocated = 0;
2237 reg_n_max = 0;
2240 /* This is the `regscan' pass of the compiler, run just before cse
2241 and again just before loop.
2243 It finds the first and last use of each pseudo-register
2244 and records them in the vectors regno_first_uid, regno_last_uid
2245 and counts the number of sets in the vector reg_n_sets.
2247 REPEAT is nonzero the second time this is called. */
2249 /* Maximum number of parallel sets and clobbers in any insn in this fn.
2250 Always at least 3, since the combiner could put that many together
2251 and we want this to remain correct for all the remaining passes.
2252 This corresponds to the maximum number of times note_stores will call
2253 a function for any insn. */
2255 int max_parallel;
2257 /* Used as a temporary to record the largest number of registers in
2258 PARALLEL in a SET_DEST. This is added to max_parallel. */
2260 static int max_set_parallel;
2262 void
2263 reg_scan (rtx f, unsigned int nregs)
2265 rtx insn;
2267 timevar_push (TV_REG_SCAN);
2269 allocate_reg_info (nregs, TRUE, FALSE);
2270 max_parallel = 3;
2271 max_set_parallel = 0;
2273 for (insn = f; insn; insn = NEXT_INSN (insn))
2274 if (INSN_P (insn))
2276 rtx pat = PATTERN (insn);
2277 if (GET_CODE (pat) == PARALLEL
2278 && XVECLEN (pat, 0) > max_parallel)
2279 max_parallel = XVECLEN (pat, 0);
2280 reg_scan_mark_refs (pat, insn, 0);
2282 if (REG_NOTES (insn))
2283 reg_scan_mark_refs (REG_NOTES (insn), insn, 1);
2286 max_parallel += max_set_parallel;
2288 timevar_pop (TV_REG_SCAN);
2291 /* X is the expression to scan. INSN is the insn it appears in.
2292 NOTE_FLAG is nonzero if X is from INSN's notes rather than its body. */
2294 static void
2295 reg_scan_mark_refs (rtx x, rtx insn, int note_flag)
2297 enum rtx_code code;
2298 rtx dest;
2299 rtx note;
2301 if (!x)
2302 return;
2303 code = GET_CODE (x);
2304 switch (code)
2306 case CONST:
2307 case CONST_INT:
2308 case CONST_DOUBLE:
2309 case CONST_VECTOR:
2310 case CC0:
2311 case PC:
2312 case SYMBOL_REF:
2313 case LABEL_REF:
2314 case ADDR_VEC:
2315 case ADDR_DIFF_VEC:
2316 return;
2318 case REG:
2320 unsigned int regno = REGNO (x);
2322 if (!note_flag)
2323 REGNO_LAST_UID (regno) = INSN_UID (insn);
2324 if (REGNO_FIRST_UID (regno) == 0)
2325 REGNO_FIRST_UID (regno) = INSN_UID (insn);
2327 break;
2329 case EXPR_LIST:
2330 if (XEXP (x, 0))
2331 reg_scan_mark_refs (XEXP (x, 0), insn, note_flag);
2332 if (XEXP (x, 1))
2333 reg_scan_mark_refs (XEXP (x, 1), insn, note_flag);
2334 break;
2336 case INSN_LIST:
2337 if (XEXP (x, 1))
2338 reg_scan_mark_refs (XEXP (x, 1), insn, note_flag);
2339 break;
2341 case CLOBBER:
2343 rtx reg = XEXP (x, 0);
2344 if (REG_P (reg))
2346 REG_N_SETS (REGNO (reg))++;
2347 REG_N_REFS (REGNO (reg))++;
2349 else if (MEM_P (reg))
2350 reg_scan_mark_refs (XEXP (reg, 0), insn, note_flag);
2352 break;
2354 case SET:
2355 /* Count a set of the destination if it is a register. */
2356 for (dest = SET_DEST (x);
2357 GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
2358 || GET_CODE (dest) == ZERO_EXTEND;
2359 dest = XEXP (dest, 0))
2362 /* For a PARALLEL, record the number of things (less the usual one for a
2363 SET) that are set. */
2364 if (GET_CODE (dest) == PARALLEL)
2365 max_set_parallel = MAX (max_set_parallel, XVECLEN (dest, 0) - 1);
2367 if (REG_P (dest))
2369 REG_N_SETS (REGNO (dest))++;
2370 REG_N_REFS (REGNO (dest))++;
2373 /* If this is setting a pseudo from another pseudo or the sum of a
2374 pseudo and a constant integer and the other pseudo is known to be
2375 a pointer, set the destination to be a pointer as well.
2377 Likewise if it is setting the destination from an address or from a
2378 value equivalent to an address or to the sum of an address and
2379 something else.
2381 But don't do any of this if the pseudo corresponds to a user
2382 variable since it should have already been set as a pointer based
2383 on the type. */
2385 if (REG_P (SET_DEST (x))
2386 && REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER
2387 /* If the destination pseudo is set more than once, then other
2388 sets might not be to a pointer value (consider access to a
2389 union in two threads of control in the presence of global
2390 optimizations). So only set REG_POINTER on the destination
2391 pseudo if this is the only set of that pseudo. */
2392 && REG_N_SETS (REGNO (SET_DEST (x))) == 1
2393 && ! REG_USERVAR_P (SET_DEST (x))
2394 && ! REG_POINTER (SET_DEST (x))
2395 && ((REG_P (SET_SRC (x))
2396 && REG_POINTER (SET_SRC (x)))
2397 || ((GET_CODE (SET_SRC (x)) == PLUS
2398 || GET_CODE (SET_SRC (x)) == LO_SUM)
2399 && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2400 && REG_P (XEXP (SET_SRC (x), 0))
2401 && REG_POINTER (XEXP (SET_SRC (x), 0)))
2402 || GET_CODE (SET_SRC (x)) == CONST
2403 || GET_CODE (SET_SRC (x)) == SYMBOL_REF
2404 || GET_CODE (SET_SRC (x)) == LABEL_REF
2405 || (GET_CODE (SET_SRC (x)) == HIGH
2406 && (GET_CODE (XEXP (SET_SRC (x), 0)) == CONST
2407 || GET_CODE (XEXP (SET_SRC (x), 0)) == SYMBOL_REF
2408 || GET_CODE (XEXP (SET_SRC (x), 0)) == LABEL_REF))
2409 || ((GET_CODE (SET_SRC (x)) == PLUS
2410 || GET_CODE (SET_SRC (x)) == LO_SUM)
2411 && (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST
2412 || GET_CODE (XEXP (SET_SRC (x), 1)) == SYMBOL_REF
2413 || GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF))
2414 || ((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
2415 && (GET_CODE (XEXP (note, 0)) == CONST
2416 || GET_CODE (XEXP (note, 0)) == SYMBOL_REF
2417 || GET_CODE (XEXP (note, 0)) == LABEL_REF))))
2418 REG_POINTER (SET_DEST (x)) = 1;
2420 /* If this is setting a register from a register or from a simple
2421 conversion of a register, propagate REG_EXPR. */
2422 if (REG_P (dest))
2424 rtx src = SET_SRC (x);
2426 while (GET_CODE (src) == SIGN_EXTEND
2427 || GET_CODE (src) == ZERO_EXTEND
2428 || GET_CODE (src) == TRUNCATE
2429 || (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)))
2430 src = XEXP (src, 0);
2432 if (!REG_ATTRS (dest) && REG_P (src))
2433 REG_ATTRS (dest) = REG_ATTRS (src);
2434 if (!REG_ATTRS (dest) && MEM_P (src))
2435 set_reg_attrs_from_mem (dest, src);
2438 /* ... fall through ... */
2440 default:
2442 const char *fmt = GET_RTX_FORMAT (code);
2443 int i;
2444 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2446 if (fmt[i] == 'e')
2447 reg_scan_mark_refs (XEXP (x, i), insn, note_flag);
2448 else if (fmt[i] == 'E' && XVEC (x, i) != 0)
2450 int j;
2451 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2452 reg_scan_mark_refs (XVECEXP (x, i, j), insn, note_flag);
2459 /* Return nonzero if C1 is a subset of C2, i.e., if every register in C1
2460 is also in C2. */
2463 reg_class_subset_p (enum reg_class c1, enum reg_class c2)
2465 if (c1 == c2) return 1;
2467 if (c2 == ALL_REGS)
2468 win:
2469 return 1;
2470 GO_IF_HARD_REG_SUBSET (reg_class_contents[(int) c1],
2471 reg_class_contents[(int) c2],
2472 win);
2473 return 0;
2476 /* Return nonzero if there is a register that is in both C1 and C2. */
2479 reg_classes_intersect_p (enum reg_class c1, enum reg_class c2)
2481 HARD_REG_SET c;
2483 if (c1 == c2) return 1;
2485 if (c1 == ALL_REGS || c2 == ALL_REGS)
2486 return 1;
2488 COPY_HARD_REG_SET (c, reg_class_contents[(int) c1]);
2489 AND_HARD_REG_SET (c, reg_class_contents[(int) c2]);
2491 GO_IF_HARD_REG_SUBSET (c, reg_class_contents[(int) NO_REGS], lose);
2492 return 1;
2494 lose:
2495 return 0;
2498 #ifdef CANNOT_CHANGE_MODE_CLASS
2500 struct subregs_of_mode_node
2502 unsigned int block;
2503 unsigned char modes[MAX_MACHINE_MODE];
2506 static htab_t subregs_of_mode;
2508 static hashval_t
2509 som_hash (const void *x)
2511 const struct subregs_of_mode_node *a = x;
2512 return a->block;
2515 static int
2516 som_eq (const void *x, const void *y)
2518 const struct subregs_of_mode_node *a = x;
2519 const struct subregs_of_mode_node *b = y;
2520 return a->block == b->block;
2523 void
2524 init_subregs_of_mode (void)
2526 if (subregs_of_mode)
2527 htab_empty (subregs_of_mode);
2528 else
2529 subregs_of_mode = htab_create (100, som_hash, som_eq, free);
2532 void
2533 record_subregs_of_mode (rtx subreg)
2535 struct subregs_of_mode_node dummy, *node;
2536 enum machine_mode mode;
2537 unsigned int regno;
2538 void **slot;
2540 if (!REG_P (SUBREG_REG (subreg)))
2541 return;
2543 regno = REGNO (SUBREG_REG (subreg));
2544 mode = GET_MODE (subreg);
2546 if (regno < FIRST_PSEUDO_REGISTER)
2547 return;
2549 dummy.block = regno & -8;
2550 slot = htab_find_slot_with_hash (subregs_of_mode, &dummy,
2551 dummy.block, INSERT);
2552 node = *slot;
2553 if (node == NULL)
2555 node = XCNEW (struct subregs_of_mode_node);
2556 node->block = regno & -8;
2557 *slot = node;
2560 node->modes[mode] |= 1 << (regno & 7);
2563 /* Set bits in *USED which correspond to registers which can't change
2564 their mode from FROM to any mode in which REGNO was encountered. */
2566 void
2567 cannot_change_mode_set_regs (HARD_REG_SET *used, enum machine_mode from,
2568 unsigned int regno)
2570 struct subregs_of_mode_node dummy, *node;
2571 enum machine_mode to;
2572 unsigned char mask;
2573 unsigned int i;
2575 dummy.block = regno & -8;
2576 node = htab_find_with_hash (subregs_of_mode, &dummy, dummy.block);
2577 if (node == NULL)
2578 return;
2580 mask = 1 << (regno & 7);
2581 for (to = VOIDmode; to < NUM_MACHINE_MODES; to++)
2582 if (node->modes[to] & mask)
2583 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2584 if (!TEST_HARD_REG_BIT (*used, i)
2585 && REG_CANNOT_CHANGE_MODE_P (i, from, to))
2586 SET_HARD_REG_BIT (*used, i);
2589 /* Return 1 if REGNO has had an invalid mode change in CLASS from FROM
2590 mode. */
2592 bool
2593 invalid_mode_change_p (unsigned int regno, enum reg_class class,
2594 enum machine_mode from)
2596 struct subregs_of_mode_node dummy, *node;
2597 enum machine_mode to;
2598 unsigned char mask;
2600 dummy.block = regno & -8;
2601 node = htab_find_with_hash (subregs_of_mode, &dummy, dummy.block);
2602 if (node == NULL)
2603 return false;
2605 mask = 1 << (regno & 7);
2606 for (to = VOIDmode; to < NUM_MACHINE_MODES; to++)
2607 if (node->modes[to] & mask)
2608 if (CANNOT_CHANGE_MODE_CLASS (from, to, class))
2609 return true;
2611 return false;
2613 #endif /* CANNOT_CHANGE_MODE_CLASS */
2615 #include "gt-regclass.h"