* config/i386/i386.c (ix86_expand_fp_absneg_operator): Use elt_mode
[official-gcc.git] / gcc / regclass.c
blob3fd8281e3e50d8ed1da4832b71dfc9eefa566fc0
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
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, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, 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"
50 static void init_reg_sets_1 (void);
51 static void init_reg_autoinc (void);
53 /* If we have auto-increment or auto-decrement and we can have secondary
54 reloads, we are not allowed to use classes requiring secondary
55 reloads for pseudos auto-incremented since reload can't handle it. */
57 #ifdef AUTO_INC_DEC
58 #if defined(SECONDARY_INPUT_RELOAD_CLASS) || defined(SECONDARY_OUTPUT_RELOAD_CLASS)
59 #define FORBIDDEN_INC_DEC_CLASSES
60 #endif
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 /* Number of non-fixed registers. */
128 int n_non_fixed_regs;
130 /* Indexed by hard register number, contains 1 for registers
131 that are being used for global register decls.
132 These must be exempt from ordinary flow analysis
133 and are also considered fixed. */
135 char global_regs[FIRST_PSEUDO_REGISTER];
137 /* Contains 1 for registers that are set or clobbered by calls. */
138 /* ??? Ideally, this would be just call_used_regs plus global_regs, but
139 for someone's bright idea to have call_used_regs strictly include
140 fixed_regs. Which leaves us guessing as to the set of fixed_regs
141 that are actually preserved. We know for sure that those associated
142 with the local stack frame are safe, but scant others. */
144 HARD_REG_SET regs_invalidated_by_call;
146 /* Table of register numbers in the order in which to try to use them. */
147 #ifdef REG_ALLOC_ORDER
148 int reg_alloc_order[FIRST_PSEUDO_REGISTER] = REG_ALLOC_ORDER;
150 /* The inverse of reg_alloc_order. */
151 int inv_reg_alloc_order[FIRST_PSEUDO_REGISTER];
152 #endif
154 /* For each reg class, a HARD_REG_SET saying which registers are in it. */
156 HARD_REG_SET reg_class_contents[N_REG_CLASSES];
158 /* The same information, but as an array of unsigned ints. We copy from
159 these unsigned ints to the table above. We do this so the tm.h files
160 do not have to be aware of the wordsize for machines with <= 64 regs.
161 Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
163 #define N_REG_INTS \
164 ((FIRST_PSEUDO_REGISTER + (32 - 1)) / 32)
166 static const unsigned int_reg_class_contents[N_REG_CLASSES][N_REG_INTS]
167 = REG_CLASS_CONTENTS;
169 /* For each reg class, number of regs it contains. */
171 unsigned int reg_class_size[N_REG_CLASSES];
173 /* For each reg class, table listing all the containing classes. */
175 enum reg_class reg_class_superclasses[N_REG_CLASSES][N_REG_CLASSES];
177 /* For each reg class, table listing all the classes contained in it. */
179 enum reg_class reg_class_subclasses[N_REG_CLASSES][N_REG_CLASSES];
181 /* For each pair of reg classes,
182 a largest reg class contained in their union. */
184 enum reg_class reg_class_subunion[N_REG_CLASSES][N_REG_CLASSES];
186 /* For each pair of reg classes,
187 the smallest reg class containing their union. */
189 enum reg_class reg_class_superunion[N_REG_CLASSES][N_REG_CLASSES];
191 /* Array containing all of the register names. */
193 const char * reg_names[] = REGISTER_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 n_non_fixed_regs = 0;
426 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
428 /* call_used_regs must include fixed_regs. */
429 gcc_assert (!fixed_regs[i] || call_used_regs[i]);
430 #ifdef CALL_REALLY_USED_REGISTERS
431 /* call_used_regs must include call_really_used_regs. */
432 gcc_assert (!call_really_used_regs[i] || call_used_regs[i]);
433 #endif
435 if (fixed_regs[i])
436 SET_HARD_REG_BIT (fixed_reg_set, i);
437 else
438 n_non_fixed_regs++;
440 if (call_used_regs[i])
441 SET_HARD_REG_BIT (call_used_reg_set, i);
442 if (call_fixed_regs[i])
443 SET_HARD_REG_BIT (call_fixed_reg_set, i);
444 if (CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (i)))
445 SET_HARD_REG_BIT (losing_caller_save_reg_set, i);
447 /* There are a couple of fixed registers that we know are safe to
448 exclude from being clobbered by calls:
450 The frame pointer is always preserved across calls. The arg pointer
451 is if it is fixed. The stack pointer usually is, unless
452 RETURN_POPS_ARGS, in which case an explicit CLOBBER will be present.
453 If we are generating PIC code, the PIC offset table register is
454 preserved across calls, though the target can override that. */
456 if (i == STACK_POINTER_REGNUM)
458 else if (global_regs[i])
459 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
460 else if (i == FRAME_POINTER_REGNUM)
462 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
463 else if (i == HARD_FRAME_POINTER_REGNUM)
465 #endif
466 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
467 else if (i == ARG_POINTER_REGNUM && fixed_regs[i])
469 #endif
470 #ifndef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
471 else if (i == (unsigned) PIC_OFFSET_TABLE_REGNUM && fixed_regs[i])
473 #endif
474 else if (CALL_REALLY_USED_REGNO_P (i))
475 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
478 memset (have_regs_of_mode, 0, sizeof (have_regs_of_mode));
479 memset (contains_reg_of_mode, 0, sizeof (contains_reg_of_mode));
480 for (m = 0; m < (unsigned int) MAX_MACHINE_MODE; m++)
481 for (i = 0; i < N_REG_CLASSES; i++)
482 if ((unsigned) CLASS_MAX_NREGS (i, m) <= reg_class_size[i])
483 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
484 if (!fixed_regs [j] && TEST_HARD_REG_BIT (reg_class_contents[i], j)
485 && HARD_REGNO_MODE_OK (j, m))
487 contains_reg_of_mode [i][m] = 1;
488 have_regs_of_mode [m] = 1;
489 break;
492 /* Initialize the move cost table. Find every subset of each class
493 and take the maximum cost of moving any subset to any other. */
495 for (m = 0; m < (unsigned int) MAX_MACHINE_MODE; m++)
496 if (have_regs_of_mode [m])
498 for (i = 0; i < N_REG_CLASSES; i++)
499 if (contains_reg_of_mode [i][m])
500 for (j = 0; j < N_REG_CLASSES; j++)
502 int cost;
503 enum reg_class *p1, *p2;
505 if (!contains_reg_of_mode [j][m])
507 move_cost[m][i][j] = 65536;
508 may_move_in_cost[m][i][j] = 65536;
509 may_move_out_cost[m][i][j] = 65536;
511 else
513 cost = REGISTER_MOVE_COST (m, i, j);
515 for (p2 = &reg_class_subclasses[j][0];
516 *p2 != LIM_REG_CLASSES;
517 p2++)
518 if (*p2 != i && contains_reg_of_mode [*p2][m])
519 cost = MAX (cost, move_cost [m][i][*p2]);
521 for (p1 = &reg_class_subclasses[i][0];
522 *p1 != LIM_REG_CLASSES;
523 p1++)
524 if (*p1 != j && contains_reg_of_mode [*p1][m])
525 cost = MAX (cost, move_cost [m][*p1][j]);
527 move_cost[m][i][j] = cost;
529 if (reg_class_subset_p (i, j))
530 may_move_in_cost[m][i][j] = 0;
531 else
532 may_move_in_cost[m][i][j] = cost;
534 if (reg_class_subset_p (j, i))
535 may_move_out_cost[m][i][j] = 0;
536 else
537 may_move_out_cost[m][i][j] = cost;
540 else
541 for (j = 0; j < N_REG_CLASSES; j++)
543 move_cost[m][i][j] = 65536;
544 may_move_in_cost[m][i][j] = 65536;
545 may_move_out_cost[m][i][j] = 65536;
550 /* Compute the table of register modes.
551 These values are used to record death information for individual registers
552 (as opposed to a multi-register mode). */
554 void
555 init_reg_modes_once (void)
557 int i, j;
559 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
560 for (j = 0; j < MAX_MACHINE_MODE; j++)
561 hard_regno_nregs[i][j] = HARD_REGNO_NREGS(i, (enum machine_mode)j);
563 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
565 reg_raw_mode[i] = choose_hard_reg_mode (i, 1, false);
567 /* If we couldn't find a valid mode, just use the previous mode.
568 ??? One situation in which we need to do this is on the mips where
569 HARD_REGNO_NREGS (fpreg, [SD]Fmode) returns 2. Ideally we'd like
570 to use DF mode for the even registers and VOIDmode for the odd
571 (for the cpu models where the odd ones are inaccessible). */
572 if (reg_raw_mode[i] == VOIDmode)
573 reg_raw_mode[i] = i == 0 ? word_mode : reg_raw_mode[i-1];
577 /* Finish initializing the register sets and
578 initialize the register modes. */
580 void
581 init_regs (void)
583 /* This finishes what was started by init_reg_sets, but couldn't be done
584 until after register usage was specified. */
585 init_reg_sets_1 ();
587 init_reg_autoinc ();
590 /* Initialize some fake stack-frame MEM references for use in
591 memory_move_secondary_cost. */
593 void
594 init_fake_stack_mems (void)
596 #ifdef HAVE_SECONDARY_RELOADS
598 int i;
600 for (i = 0; i < MAX_MACHINE_MODE; i++)
601 top_of_stack[i] = gen_rtx_MEM (i, stack_pointer_rtx);
603 #endif
606 #ifdef HAVE_SECONDARY_RELOADS
608 /* Compute extra cost of moving registers to/from memory due to reloads.
609 Only needed if secondary reloads are required for memory moves. */
612 memory_move_secondary_cost (enum machine_mode mode, enum reg_class class, int in)
614 enum reg_class altclass;
615 int partial_cost = 0;
616 /* We need a memory reference to feed to SECONDARY... macros. */
617 /* mem may be unused even if the SECONDARY_ macros are defined. */
618 rtx mem ATTRIBUTE_UNUSED = top_of_stack[(int) mode];
621 if (in)
623 #ifdef SECONDARY_INPUT_RELOAD_CLASS
624 altclass = SECONDARY_INPUT_RELOAD_CLASS (class, mode, mem);
625 #else
626 altclass = NO_REGS;
627 #endif
629 else
631 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
632 altclass = SECONDARY_OUTPUT_RELOAD_CLASS (class, mode, mem);
633 #else
634 altclass = NO_REGS;
635 #endif
638 if (altclass == NO_REGS)
639 return 0;
641 if (in)
642 partial_cost = REGISTER_MOVE_COST (mode, altclass, class);
643 else
644 partial_cost = REGISTER_MOVE_COST (mode, class, altclass);
646 if (class == altclass)
647 /* This isn't simply a copy-to-temporary situation. Can't guess
648 what it is, so MEMORY_MOVE_COST really ought not to be calling
649 here in that case.
651 I'm tempted to put in an assert here, but returning this will
652 probably only give poor estimates, which is what we would've
653 had before this code anyways. */
654 return partial_cost;
656 /* Check if the secondary reload register will also need a
657 secondary reload. */
658 return memory_move_secondary_cost (mode, altclass, in) + partial_cost;
660 #endif
662 /* Return a machine mode that is legitimate for hard reg REGNO and large
663 enough to save nregs. If we can't find one, return VOIDmode.
664 If CALL_SAVED is true, only consider modes that are call saved. */
666 enum machine_mode
667 choose_hard_reg_mode (unsigned int regno ATTRIBUTE_UNUSED,
668 unsigned int nregs, bool call_saved)
670 unsigned int /* enum machine_mode */ m;
671 enum machine_mode found_mode = VOIDmode, mode;
673 /* We first look for the largest integer mode that can be validly
674 held in REGNO. If none, we look for the largest floating-point mode.
675 If we still didn't find a valid mode, try CCmode. */
677 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
678 mode != VOIDmode;
679 mode = GET_MODE_WIDER_MODE (mode))
680 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
681 && HARD_REGNO_MODE_OK (regno, mode)
682 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
683 found_mode = mode;
685 if (found_mode != VOIDmode)
686 return found_mode;
688 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
689 mode != VOIDmode;
690 mode = GET_MODE_WIDER_MODE (mode))
691 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
692 && HARD_REGNO_MODE_OK (regno, mode)
693 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
694 found_mode = mode;
696 if (found_mode != VOIDmode)
697 return found_mode;
699 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
700 mode != VOIDmode;
701 mode = GET_MODE_WIDER_MODE (mode))
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 found_mode = mode;
707 if (found_mode != VOIDmode)
708 return found_mode;
710 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
711 mode != VOIDmode;
712 mode = GET_MODE_WIDER_MODE (mode))
713 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
714 && HARD_REGNO_MODE_OK (regno, mode)
715 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
716 found_mode = mode;
718 if (found_mode != VOIDmode)
719 return found_mode;
721 /* Iterate over all of the CCmodes. */
722 for (m = (unsigned int) CCmode; m < (unsigned int) NUM_MACHINE_MODES; ++m)
724 mode = (enum machine_mode) m;
725 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
726 && HARD_REGNO_MODE_OK (regno, mode)
727 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
728 return mode;
731 /* We can't find a mode valid for this register. */
732 return VOIDmode;
735 /* Specify the usage characteristics of the register named NAME.
736 It should be a fixed register if FIXED and a
737 call-used register if CALL_USED. */
739 void
740 fix_register (const char *name, int fixed, int call_used)
742 int i;
744 /* Decode the name and update the primary form of
745 the register info. */
747 if ((i = decode_reg_name (name)) >= 0)
749 if ((i == STACK_POINTER_REGNUM
750 #ifdef HARD_FRAME_POINTER_REGNUM
751 || i == HARD_FRAME_POINTER_REGNUM
752 #else
753 || i == FRAME_POINTER_REGNUM
754 #endif
756 && (fixed == 0 || call_used == 0))
758 static const char * const what_option[2][2] = {
759 { "call-saved", "call-used" },
760 { "no-such-option", "fixed" }};
762 error ("can't use '%s' as a %s register", name,
763 what_option[fixed][call_used]);
765 else
767 fixed_regs[i] = fixed;
768 call_used_regs[i] = call_used;
769 #ifdef CALL_REALLY_USED_REGISTERS
770 if (fixed == 0)
771 call_really_used_regs[i] = call_used;
772 #endif
775 else
777 warning ("unknown register name: %s", name);
781 /* Mark register number I as global. */
783 void
784 globalize_reg (int i)
786 if (fixed_regs[i] == 0 && no_global_reg_vars)
787 error ("global register variable follows a function definition");
789 if (global_regs[i])
791 warning ("register used for two global register variables");
792 return;
795 if (call_used_regs[i] && ! fixed_regs[i])
796 warning ("call-clobbered register used for global register variable");
798 global_regs[i] = 1;
800 /* If we're globalizing the frame pointer, we need to set the
801 appropriate regs_invalidated_by_call bit, even if it's already
802 set in fixed_regs. */
803 if (i != STACK_POINTER_REGNUM)
804 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
806 /* If already fixed, nothing else to do. */
807 if (fixed_regs[i])
808 return;
810 fixed_regs[i] = call_used_regs[i] = call_fixed_regs[i] = 1;
811 #ifdef CALL_REALLY_USED_REGISTERS
812 call_really_used_regs[i] = 1;
813 #endif
814 n_non_fixed_regs--;
816 SET_HARD_REG_BIT (fixed_reg_set, i);
817 SET_HARD_REG_BIT (call_used_reg_set, i);
818 SET_HARD_REG_BIT (call_fixed_reg_set, i);
821 /* Now the data and code for the `regclass' pass, which happens
822 just before local-alloc. */
824 /* The `costs' struct records the cost of using a hard register of each class
825 and of using memory for each pseudo. We use this data to set up
826 register class preferences. */
828 struct costs
830 int cost[N_REG_CLASSES];
831 int mem_cost;
834 /* Structure used to record preferences of given pseudo. */
835 struct reg_pref
837 /* (enum reg_class) prefclass is the preferred class. */
838 char prefclass;
840 /* altclass is a register class that we should use for allocating
841 pseudo if no register in the preferred class is available.
842 If no register in this class is available, memory is preferred.
844 It might appear to be more general to have a bitmask of classes here,
845 but since it is recommended that there be a class corresponding to the
846 union of most major pair of classes, that generality is not required. */
847 char altclass;
850 /* Record the cost of each class for each pseudo. */
852 static struct costs *costs;
854 /* Initialized once, and used to initialize cost values for each insn. */
856 static struct costs init_cost;
858 /* Record preferences of each pseudo.
859 This is available after `regclass' is run. */
861 static struct reg_pref *reg_pref;
863 /* Allocated buffers for reg_pref. */
865 static struct reg_pref *reg_pref_buffer;
867 /* Frequency of executions of current insn. */
869 static int frequency;
871 static rtx scan_one_insn (rtx, int);
872 static void record_operand_costs (rtx, struct costs *, struct reg_pref *);
873 static void dump_regclass (FILE *);
874 static void record_reg_classes (int, int, rtx *, enum machine_mode *,
875 const char **, rtx, struct costs *,
876 struct reg_pref *);
877 static int copy_cost (rtx, enum machine_mode, enum reg_class, int);
878 static void record_address_regs (rtx, enum reg_class, int);
879 #ifdef FORBIDDEN_INC_DEC_CLASSES
880 static int auto_inc_dec_reg_p (rtx, enum machine_mode);
881 #endif
882 static void reg_scan_mark_refs (rtx, rtx, int, unsigned int);
884 /* Return the reg_class in which pseudo reg number REGNO is best allocated.
885 This function is sometimes called before the info has been computed.
886 When that happens, just return GENERAL_REGS, which is innocuous. */
888 enum reg_class
889 reg_preferred_class (int regno)
891 if (reg_pref == 0)
892 return GENERAL_REGS;
893 return (enum reg_class) reg_pref[regno].prefclass;
896 enum reg_class
897 reg_alternate_class (int regno)
899 if (reg_pref == 0)
900 return ALL_REGS;
902 return (enum reg_class) reg_pref[regno].altclass;
905 /* Initialize some global data for this pass. */
907 void
908 regclass_init (void)
910 int i;
912 init_cost.mem_cost = 10000;
913 for (i = 0; i < N_REG_CLASSES; i++)
914 init_cost.cost[i] = 10000;
916 /* This prevents dump_flow_info from losing if called
917 before regclass is run. */
918 reg_pref = NULL;
920 /* No more global register variables may be declared. */
921 no_global_reg_vars = 1;
924 /* Dump register costs. */
925 static void
926 dump_regclass (FILE *dump)
928 static const char *const reg_class_names[] = REG_CLASS_NAMES;
929 int i;
930 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
932 int /* enum reg_class */ class;
933 if (REG_N_REFS (i))
935 fprintf (dump, " Register %i costs:", i);
936 for (class = 0; class < (int) N_REG_CLASSES; class++)
937 if (contains_reg_of_mode [(enum reg_class) class][PSEUDO_REGNO_MODE (i)]
938 #ifdef FORBIDDEN_INC_DEC_CLASSES
939 && (!in_inc_dec[i]
940 || !forbidden_inc_dec_class[(enum reg_class) class])
941 #endif
942 #ifdef CANNOT_CHANGE_MODE_CLASS
943 && ! invalid_mode_change_p (i, (enum reg_class) class,
944 PSEUDO_REGNO_MODE (i))
945 #endif
947 fprintf (dump, " %s:%i", reg_class_names[class],
948 costs[i].cost[(enum reg_class) class]);
949 fprintf (dump, " MEM:%i\n", costs[i].mem_cost);
955 /* Calculate the costs of insn operands. */
957 static void
958 record_operand_costs (rtx insn, struct costs *op_costs,
959 struct reg_pref *reg_pref)
961 const char *constraints[MAX_RECOG_OPERANDS];
962 enum machine_mode modes[MAX_RECOG_OPERANDS];
963 int i;
965 for (i = 0; i < recog_data.n_operands; i++)
967 constraints[i] = recog_data.constraints[i];
968 modes[i] = recog_data.operand_mode[i];
971 /* If we get here, we are set up to record the costs of all the
972 operands for this insn. Start by initializing the costs.
973 Then handle any address registers. Finally record the desired
974 classes for any pseudos, doing it twice if some pair of
975 operands are commutative. */
977 for (i = 0; i < recog_data.n_operands; i++)
979 op_costs[i] = init_cost;
981 if (GET_CODE (recog_data.operand[i]) == SUBREG)
982 recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
984 if (MEM_P (recog_data.operand[i]))
985 record_address_regs (XEXP (recog_data.operand[i], 0),
986 MODE_BASE_REG_CLASS (modes[i]), frequency * 2);
987 else if (constraints[i][0] == 'p'
988 || EXTRA_ADDRESS_CONSTRAINT (constraints[i][0], constraints[i]))
989 record_address_regs (recog_data.operand[i],
990 MODE_BASE_REG_CLASS (modes[i]), frequency * 2);
993 /* Check for commutative in a separate loop so everything will
994 have been initialized. We must do this even if one operand
995 is a constant--see addsi3 in m68k.md. */
997 for (i = 0; i < (int) recog_data.n_operands - 1; i++)
998 if (constraints[i][0] == '%')
1000 const char *xconstraints[MAX_RECOG_OPERANDS];
1001 int j;
1003 /* Handle commutative operands by swapping the constraints.
1004 We assume the modes are the same. */
1006 for (j = 0; j < recog_data.n_operands; j++)
1007 xconstraints[j] = constraints[j];
1009 xconstraints[i] = constraints[i+1];
1010 xconstraints[i+1] = constraints[i];
1011 record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
1012 recog_data.operand, modes,
1013 xconstraints, insn, op_costs, reg_pref);
1016 record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
1017 recog_data.operand, modes,
1018 constraints, insn, op_costs, reg_pref);
1021 /* Subroutine of regclass, processes one insn INSN. Scan it and record each
1022 time it would save code to put a certain register in a certain class.
1023 PASS, when nonzero, inhibits some optimizations which need only be done
1024 once.
1025 Return the last insn processed, so that the scan can be continued from
1026 there. */
1028 static rtx
1029 scan_one_insn (rtx insn, int pass)
1031 enum rtx_code pat_code;
1032 rtx set, note;
1033 int i, j;
1034 struct costs op_costs[MAX_RECOG_OPERANDS];
1036 if (!INSN_P (insn))
1037 return insn;
1039 pat_code = GET_CODE (PATTERN (insn));
1040 if (pat_code == USE
1041 || pat_code == CLOBBER
1042 || pat_code == ASM_INPUT
1043 || pat_code == ADDR_VEC
1044 || pat_code == ADDR_DIFF_VEC)
1045 return insn;
1047 set = single_set (insn);
1048 extract_insn (insn);
1050 /* If this insn loads a parameter from its stack slot, then
1051 it represents a savings, rather than a cost, if the
1052 parameter is stored in memory. Record this fact. */
1054 if (set != 0 && REG_P (SET_DEST (set))
1055 && MEM_P (SET_SRC (set))
1056 && (note = find_reg_note (insn, REG_EQUIV,
1057 NULL_RTX)) != 0
1058 && MEM_P (XEXP (note, 0)))
1060 costs[REGNO (SET_DEST (set))].mem_cost
1061 -= (MEMORY_MOVE_COST (GET_MODE (SET_DEST (set)),
1062 GENERAL_REGS, 1)
1063 * frequency);
1064 record_address_regs (XEXP (SET_SRC (set), 0),
1065 MODE_BASE_REG_CLASS (VOIDmode), frequency * 2);
1066 return insn;
1069 /* Improve handling of two-address insns such as
1070 (set X (ashift CONST Y)) where CONST must be made to
1071 match X. Change it into two insns: (set X CONST)
1072 (set X (ashift X Y)). If we left this for reloading, it
1073 would probably get three insns because X and Y might go
1074 in the same place. This prevents X and Y from receiving
1075 the same hard reg.
1077 We can only do this if the modes of operands 0 and 1
1078 (which might not be the same) are tieable and we only need
1079 do this during our first pass. */
1081 if (pass == 0 && optimize
1082 && recog_data.n_operands >= 3
1083 && recog_data.constraints[1][0] == '0'
1084 && recog_data.constraints[1][1] == 0
1085 && CONSTANT_P (recog_data.operand[1])
1086 && ! rtx_equal_p (recog_data.operand[0], recog_data.operand[1])
1087 && ! rtx_equal_p (recog_data.operand[0], recog_data.operand[2])
1088 && REG_P (recog_data.operand[0])
1089 && MODES_TIEABLE_P (GET_MODE (recog_data.operand[0]),
1090 recog_data.operand_mode[1]))
1092 rtx previnsn = prev_real_insn (insn);
1093 rtx dest
1094 = gen_lowpart (recog_data.operand_mode[1],
1095 recog_data.operand[0]);
1096 rtx newinsn
1097 = emit_insn_before (gen_move_insn (dest, recog_data.operand[1]), insn);
1099 /* If this insn was the start of a basic block,
1100 include the new insn in that block.
1101 We need not check for code_label here;
1102 while a basic block can start with a code_label,
1103 INSN could not be at the beginning of that block. */
1104 if (previnsn == 0 || JUMP_P (previnsn))
1106 basic_block b;
1107 FOR_EACH_BB (b)
1108 if (insn == BB_HEAD (b))
1109 BB_HEAD (b) = newinsn;
1112 /* This makes one more setting of new insns's dest. */
1113 REG_N_SETS (REGNO (recog_data.operand[0]))++;
1114 REG_N_REFS (REGNO (recog_data.operand[0]))++;
1115 REG_FREQ (REGNO (recog_data.operand[0])) += frequency;
1117 *recog_data.operand_loc[1] = recog_data.operand[0];
1118 REG_N_REFS (REGNO (recog_data.operand[0]))++;
1119 REG_FREQ (REGNO (recog_data.operand[0])) += frequency;
1120 for (i = recog_data.n_dups - 1; i >= 0; i--)
1121 if (recog_data.dup_num[i] == 1)
1123 *recog_data.dup_loc[i] = recog_data.operand[0];
1124 REG_N_REFS (REGNO (recog_data.operand[0]))++;
1125 REG_FREQ (REGNO (recog_data.operand[0])) += frequency;
1128 return PREV_INSN (newinsn);
1131 record_operand_costs (insn, op_costs, reg_pref);
1133 /* Now add the cost for each operand to the total costs for
1134 its register. */
1136 for (i = 0; i < recog_data.n_operands; i++)
1137 if (REG_P (recog_data.operand[i])
1138 && REGNO (recog_data.operand[i]) >= FIRST_PSEUDO_REGISTER)
1140 int regno = REGNO (recog_data.operand[i]);
1141 struct costs *p = &costs[regno], *q = &op_costs[i];
1143 p->mem_cost += q->mem_cost * frequency;
1144 for (j = 0; j < N_REG_CLASSES; j++)
1145 p->cost[j] += q->cost[j] * frequency;
1148 return insn;
1151 /* Initialize information about which register classes can be used for
1152 pseudos that are auto-incremented or auto-decremented. */
1154 static void
1155 init_reg_autoinc (void)
1157 #ifdef FORBIDDEN_INC_DEC_CLASSES
1158 int i;
1160 for (i = 0; i < N_REG_CLASSES; i++)
1162 rtx r = gen_rtx_raw_REG (VOIDmode, 0);
1163 enum machine_mode m;
1164 int j;
1166 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1167 if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
1169 REGNO (r) = j;
1171 for (m = VOIDmode; (int) m < (int) MAX_MACHINE_MODE;
1172 m = (enum machine_mode) ((int) m + 1))
1173 if (HARD_REGNO_MODE_OK (j, m))
1175 PUT_MODE (r, m);
1177 /* If a register is not directly suitable for an
1178 auto-increment or decrement addressing mode and
1179 requires secondary reloads, disallow its class from
1180 being used in such addresses. */
1182 if ((0
1183 #ifdef SECONDARY_RELOAD_CLASS
1184 || (SECONDARY_RELOAD_CLASS (MODE_BASE_REG_CLASS (VOIDmode), m, r)
1185 != NO_REGS)
1186 #else
1187 #ifdef SECONDARY_INPUT_RELOAD_CLASS
1188 || (SECONDARY_INPUT_RELOAD_CLASS (MODE_BASE_REG_CLASS (VOIDmode), m, r)
1189 != NO_REGS)
1190 #endif
1191 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
1192 || (SECONDARY_OUTPUT_RELOAD_CLASS (MODE_BASE_REG_CLASS (VOIDmode), m, r)
1193 != NO_REGS)
1194 #endif
1195 #endif
1197 && ! auto_inc_dec_reg_p (r, m))
1198 forbidden_inc_dec_class[i] = 1;
1202 #endif /* FORBIDDEN_INC_DEC_CLASSES */
1205 /* This is a pass of the compiler that scans all instructions
1206 and calculates the preferred class for each pseudo-register.
1207 This information can be accessed later by calling `reg_preferred_class'.
1208 This pass comes just before local register allocation. */
1210 void
1211 regclass (rtx f, int nregs, FILE *dump)
1213 rtx insn;
1214 int i;
1215 int pass;
1217 init_recog ();
1219 costs = xmalloc (nregs * sizeof (struct costs));
1221 #ifdef FORBIDDEN_INC_DEC_CLASSES
1223 in_inc_dec = xmalloc (nregs);
1225 #endif /* FORBIDDEN_INC_DEC_CLASSES */
1227 /* Normally we scan the insns once and determine the best class to use for
1228 each register. However, if -fexpensive_optimizations are on, we do so
1229 twice, the second time using the tentative best classes to guide the
1230 selection. */
1232 for (pass = 0; pass <= flag_expensive_optimizations; pass++)
1234 basic_block bb;
1236 if (dump)
1237 fprintf (dump, "\n\nPass %i\n\n",pass);
1238 /* Zero out our accumulation of the cost of each class for each reg. */
1240 memset (costs, 0, nregs * sizeof (struct costs));
1242 #ifdef FORBIDDEN_INC_DEC_CLASSES
1243 memset (in_inc_dec, 0, nregs);
1244 #endif
1246 /* Scan the instructions and record each time it would
1247 save code to put a certain register in a certain class. */
1249 if (!optimize)
1251 frequency = REG_FREQ_MAX;
1252 for (insn = f; insn; insn = NEXT_INSN (insn))
1253 insn = scan_one_insn (insn, pass);
1255 else
1256 FOR_EACH_BB (bb)
1258 /* Show that an insn inside a loop is likely to be executed three
1259 times more than insns outside a loop. This is much more
1260 aggressive than the assumptions made elsewhere and is being
1261 tried as an experiment. */
1262 frequency = REG_FREQ_FROM_BB (bb);
1263 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
1265 insn = scan_one_insn (insn, pass);
1266 if (insn == BB_END (bb))
1267 break;
1271 /* Now for each register look at how desirable each class is
1272 and find which class is preferred. Store that in
1273 `prefclass'. Record in `altclass' the largest register
1274 class any of whose registers is better than memory. */
1276 if (pass == 0)
1277 reg_pref = reg_pref_buffer;
1279 if (dump)
1281 dump_regclass (dump);
1282 fprintf (dump,"\n");
1284 for (i = FIRST_PSEUDO_REGISTER; i < nregs; i++)
1286 int best_cost = (1 << (HOST_BITS_PER_INT - 2)) - 1;
1287 enum reg_class best = ALL_REGS, alt = NO_REGS;
1288 /* This is an enum reg_class, but we call it an int
1289 to save lots of casts. */
1290 int class;
1291 struct costs *p = &costs[i];
1293 /* In non-optimizing compilation REG_N_REFS is not initialized
1294 yet. */
1295 if (optimize && !REG_N_REFS (i) && !REG_N_SETS (i))
1296 continue;
1298 for (class = (int) ALL_REGS - 1; class > 0; class--)
1300 /* Ignore classes that are too small for this operand or
1301 invalid for an operand that was auto-incremented. */
1302 if (!contains_reg_of_mode [class][PSEUDO_REGNO_MODE (i)]
1303 #ifdef FORBIDDEN_INC_DEC_CLASSES
1304 || (in_inc_dec[i] && forbidden_inc_dec_class[class])
1305 #endif
1306 #ifdef CANNOT_CHANGE_MODE_CLASS
1307 || invalid_mode_change_p (i, (enum reg_class) class,
1308 PSEUDO_REGNO_MODE (i))
1309 #endif
1312 else if (p->cost[class] < best_cost)
1314 best_cost = p->cost[class];
1315 best = (enum reg_class) class;
1317 else if (p->cost[class] == best_cost)
1318 best = reg_class_subunion[(int) best][class];
1321 /* Record the alternate register class; i.e., a class for which
1322 every register in it is better than using memory. If adding a
1323 class would make a smaller class (i.e., no union of just those
1324 classes exists), skip that class. The major unions of classes
1325 should be provided as a register class. Don't do this if we
1326 will be doing it again later. */
1328 if ((pass == 1 || dump) || ! flag_expensive_optimizations)
1329 for (class = 0; class < N_REG_CLASSES; class++)
1330 if (p->cost[class] < p->mem_cost
1331 && (reg_class_size[(int) reg_class_subunion[(int) alt][class]]
1332 > reg_class_size[(int) alt])
1333 #ifdef FORBIDDEN_INC_DEC_CLASSES
1334 && ! (in_inc_dec[i] && forbidden_inc_dec_class[class])
1335 #endif
1336 #ifdef CANNOT_CHANGE_MODE_CLASS
1337 && ! invalid_mode_change_p (i, (enum reg_class) class,
1338 PSEUDO_REGNO_MODE (i))
1339 #endif
1341 alt = reg_class_subunion[(int) alt][class];
1343 /* If we don't add any classes, nothing to try. */
1344 if (alt == best)
1345 alt = NO_REGS;
1347 if (dump
1348 && (reg_pref[i].prefclass != (int) best
1349 || reg_pref[i].altclass != (int) alt))
1351 static const char *const reg_class_names[] = REG_CLASS_NAMES;
1352 fprintf (dump, " Register %i", i);
1353 if (alt == ALL_REGS || best == ALL_REGS)
1354 fprintf (dump, " pref %s\n", reg_class_names[(int) best]);
1355 else if (alt == NO_REGS)
1356 fprintf (dump, " pref %s or none\n", reg_class_names[(int) best]);
1357 else
1358 fprintf (dump, " pref %s, else %s\n",
1359 reg_class_names[(int) best],
1360 reg_class_names[(int) alt]);
1363 /* We cast to (int) because (char) hits bugs in some compilers. */
1364 reg_pref[i].prefclass = (int) best;
1365 reg_pref[i].altclass = (int) alt;
1369 #ifdef FORBIDDEN_INC_DEC_CLASSES
1370 free (in_inc_dec);
1371 #endif
1372 free (costs);
1375 /* Record the cost of using memory or registers of various classes for
1376 the operands in INSN.
1378 N_ALTS is the number of alternatives.
1380 N_OPS is the number of operands.
1382 OPS is an array of the operands.
1384 MODES are the modes of the operands, in case any are VOIDmode.
1386 CONSTRAINTS are the constraints to use for the operands. This array
1387 is modified by this procedure.
1389 This procedure works alternative by alternative. For each alternative
1390 we assume that we will be able to allocate all pseudos to their ideal
1391 register class and calculate the cost of using that alternative. Then
1392 we compute for each operand that is a pseudo-register, the cost of
1393 having the pseudo allocated to each register class and using it in that
1394 alternative. To this cost is added the cost of the alternative.
1396 The cost of each class for this insn is its lowest cost among all the
1397 alternatives. */
1399 static void
1400 record_reg_classes (int n_alts, int n_ops, rtx *ops,
1401 enum machine_mode *modes, const char **constraints,
1402 rtx insn, struct costs *op_costs,
1403 struct reg_pref *reg_pref)
1405 int alt;
1406 int i, j;
1407 rtx set;
1409 /* Process each alternative, each time minimizing an operand's cost with
1410 the cost for each operand in that alternative. */
1412 for (alt = 0; alt < n_alts; alt++)
1414 struct costs this_op_costs[MAX_RECOG_OPERANDS];
1415 int alt_fail = 0;
1416 int alt_cost = 0;
1417 enum reg_class classes[MAX_RECOG_OPERANDS];
1418 int allows_mem[MAX_RECOG_OPERANDS];
1419 int class;
1421 for (i = 0; i < n_ops; i++)
1423 const char *p = constraints[i];
1424 rtx op = ops[i];
1425 enum machine_mode mode = modes[i];
1426 int allows_addr = 0;
1427 int win = 0;
1428 unsigned char c;
1430 /* Initially show we know nothing about the register class. */
1431 classes[i] = NO_REGS;
1432 allows_mem[i] = 0;
1434 /* If this operand has no constraints at all, we can conclude
1435 nothing about it since anything is valid. */
1437 if (*p == 0)
1439 if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
1440 memset (&this_op_costs[i], 0, sizeof this_op_costs[i]);
1442 continue;
1445 /* If this alternative is only relevant when this operand
1446 matches a previous operand, we do different things depending
1447 on whether this operand is a pseudo-reg or not. We must process
1448 any modifiers for the operand before we can make this test. */
1450 while (*p == '%' || *p == '=' || *p == '+' || *p == '&')
1451 p++;
1453 if (p[0] >= '0' && p[0] <= '0' + i && (p[1] == ',' || p[1] == 0))
1455 /* Copy class and whether memory is allowed from the matching
1456 alternative. Then perform any needed cost computations
1457 and/or adjustments. */
1458 j = p[0] - '0';
1459 classes[i] = classes[j];
1460 allows_mem[i] = allows_mem[j];
1462 if (!REG_P (op) || REGNO (op) < FIRST_PSEUDO_REGISTER)
1464 /* If this matches the other operand, we have no added
1465 cost and we win. */
1466 if (rtx_equal_p (ops[j], op))
1467 win = 1;
1469 /* If we can put the other operand into a register, add to
1470 the cost of this alternative the cost to copy this
1471 operand to the register used for the other operand. */
1473 else if (classes[j] != NO_REGS)
1474 alt_cost += copy_cost (op, mode, classes[j], 1), win = 1;
1476 else if (!REG_P (ops[j])
1477 || REGNO (ops[j]) < FIRST_PSEUDO_REGISTER)
1479 /* This op is a pseudo but the one it matches is not. */
1481 /* If we can't put the other operand into a register, this
1482 alternative can't be used. */
1484 if (classes[j] == NO_REGS)
1485 alt_fail = 1;
1487 /* Otherwise, add to the cost of this alternative the cost
1488 to copy the other operand to the register used for this
1489 operand. */
1491 else
1492 alt_cost += copy_cost (ops[j], mode, classes[j], 1);
1494 else
1496 /* The costs of this operand are not the same as the other
1497 operand since move costs are not symmetric. Moreover,
1498 if we cannot tie them, this alternative needs to do a
1499 copy, which is one instruction. */
1501 struct costs *pp = &this_op_costs[i];
1503 for (class = 0; class < N_REG_CLASSES; class++)
1504 pp->cost[class]
1505 = ((recog_data.operand_type[i] != OP_OUT
1506 ? may_move_in_cost[mode][class][(int) classes[i]]
1507 : 0)
1508 + (recog_data.operand_type[i] != OP_IN
1509 ? may_move_out_cost[mode][(int) classes[i]][class]
1510 : 0));
1512 /* If the alternative actually allows memory, make things
1513 a bit cheaper since we won't need an extra insn to
1514 load it. */
1516 pp->mem_cost
1517 = ((recog_data.operand_type[i] != OP_IN
1518 ? MEMORY_MOVE_COST (mode, classes[i], 0)
1519 : 0)
1520 + (recog_data.operand_type[i] != OP_OUT
1521 ? MEMORY_MOVE_COST (mode, classes[i], 1)
1522 : 0) - allows_mem[i]);
1524 /* If we have assigned a class to this register in our
1525 first pass, add a cost to this alternative corresponding
1526 to what we would add if this register were not in the
1527 appropriate class. */
1529 if (reg_pref)
1530 alt_cost
1531 += (may_move_in_cost[mode]
1532 [(unsigned char) reg_pref[REGNO (op)].prefclass]
1533 [(int) classes[i]]);
1535 if (REGNO (ops[i]) != REGNO (ops[j])
1536 && ! find_reg_note (insn, REG_DEAD, op))
1537 alt_cost += 2;
1539 /* This is in place of ordinary cost computation
1540 for this operand, so skip to the end of the
1541 alternative (should be just one character). */
1542 while (*p && *p++ != ',')
1545 constraints[i] = p;
1546 continue;
1550 /* Scan all the constraint letters. See if the operand matches
1551 any of the constraints. Collect the valid register classes
1552 and see if this operand accepts memory. */
1554 while ((c = *p))
1556 switch (c)
1558 case ',':
1559 break;
1560 case '*':
1561 /* Ignore the next letter for this pass. */
1562 c = *++p;
1563 break;
1565 case '?':
1566 alt_cost += 2;
1567 case '!': case '#': case '&':
1568 case '0': case '1': case '2': case '3': case '4':
1569 case '5': case '6': case '7': case '8': case '9':
1570 break;
1572 case 'p':
1573 allows_addr = 1;
1574 win = address_operand (op, GET_MODE (op));
1575 /* We know this operand is an address, so we want it to be
1576 allocated to a register that can be the base of an
1577 address, i.e. BASE_REG_CLASS. */
1578 classes[i]
1579 = reg_class_subunion[(int) classes[i]]
1580 [(int) MODE_BASE_REG_CLASS (VOIDmode)];
1581 break;
1583 case 'm': case 'o': case 'V':
1584 /* It doesn't seem worth distinguishing between offsettable
1585 and non-offsettable addresses here. */
1586 allows_mem[i] = 1;
1587 if (MEM_P (op))
1588 win = 1;
1589 break;
1591 case '<':
1592 if (MEM_P (op)
1593 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
1594 || GET_CODE (XEXP (op, 0)) == POST_DEC))
1595 win = 1;
1596 break;
1598 case '>':
1599 if (MEM_P (op)
1600 && (GET_CODE (XEXP (op, 0)) == PRE_INC
1601 || GET_CODE (XEXP (op, 0)) == POST_INC))
1602 win = 1;
1603 break;
1605 case 'E':
1606 case 'F':
1607 if (GET_CODE (op) == CONST_DOUBLE
1608 || (GET_CODE (op) == CONST_VECTOR
1609 && (GET_MODE_CLASS (GET_MODE (op))
1610 == MODE_VECTOR_FLOAT)))
1611 win = 1;
1612 break;
1614 case 'G':
1615 case 'H':
1616 if (GET_CODE (op) == CONST_DOUBLE
1617 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
1618 win = 1;
1619 break;
1621 case 's':
1622 if (GET_CODE (op) == CONST_INT
1623 || (GET_CODE (op) == CONST_DOUBLE
1624 && GET_MODE (op) == VOIDmode))
1625 break;
1626 case 'i':
1627 if (CONSTANT_P (op)
1628 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op)))
1629 win = 1;
1630 break;
1632 case 'n':
1633 if (GET_CODE (op) == CONST_INT
1634 || (GET_CODE (op) == CONST_DOUBLE
1635 && GET_MODE (op) == VOIDmode))
1636 win = 1;
1637 break;
1639 case 'I':
1640 case 'J':
1641 case 'K':
1642 case 'L':
1643 case 'M':
1644 case 'N':
1645 case 'O':
1646 case 'P':
1647 if (GET_CODE (op) == CONST_INT
1648 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
1649 win = 1;
1650 break;
1652 case 'X':
1653 win = 1;
1654 break;
1656 case 'g':
1657 if (MEM_P (op)
1658 || (CONSTANT_P (op)
1659 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))))
1660 win = 1;
1661 allows_mem[i] = 1;
1662 case 'r':
1663 classes[i]
1664 = reg_class_subunion[(int) classes[i]][(int) GENERAL_REGS];
1665 break;
1667 default:
1668 if (REG_CLASS_FROM_CONSTRAINT (c, p) != NO_REGS)
1669 classes[i]
1670 = reg_class_subunion[(int) classes[i]]
1671 [(int) REG_CLASS_FROM_CONSTRAINT (c, p)];
1672 #ifdef EXTRA_CONSTRAINT_STR
1673 else if (EXTRA_CONSTRAINT_STR (op, c, p))
1674 win = 1;
1676 if (EXTRA_MEMORY_CONSTRAINT (c, p))
1678 /* Every MEM can be reloaded to fit. */
1679 allows_mem[i] = 1;
1680 if (MEM_P (op))
1681 win = 1;
1683 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1685 /* Every address can be reloaded to fit. */
1686 allows_addr = 1;
1687 if (address_operand (op, GET_MODE (op)))
1688 win = 1;
1689 /* We know this operand is an address, so we want it to
1690 be allocated to a register that can be the base of an
1691 address, i.e. BASE_REG_CLASS. */
1692 classes[i]
1693 = reg_class_subunion[(int) classes[i]]
1694 [(int) MODE_BASE_REG_CLASS (VOIDmode)];
1696 #endif
1697 break;
1699 p += CONSTRAINT_LEN (c, p);
1700 if (c == ',')
1701 break;
1704 constraints[i] = p;
1706 /* How we account for this operand now depends on whether it is a
1707 pseudo register or not. If it is, we first check if any
1708 register classes are valid. If not, we ignore this alternative,
1709 since we want to assume that all pseudos get allocated for
1710 register preferencing. If some register class is valid, compute
1711 the costs of moving the pseudo into that class. */
1713 if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
1715 if (classes[i] == NO_REGS)
1717 /* We must always fail if the operand is a REG, but
1718 we did not find a suitable class.
1720 Otherwise we may perform an uninitialized read
1721 from this_op_costs after the `continue' statement
1722 below. */
1723 alt_fail = 1;
1725 else
1727 struct costs *pp = &this_op_costs[i];
1729 for (class = 0; class < N_REG_CLASSES; class++)
1730 pp->cost[class]
1731 = ((recog_data.operand_type[i] != OP_OUT
1732 ? may_move_in_cost[mode][class][(int) classes[i]]
1733 : 0)
1734 + (recog_data.operand_type[i] != OP_IN
1735 ? may_move_out_cost[mode][(int) classes[i]][class]
1736 : 0));
1738 /* If the alternative actually allows memory, make things
1739 a bit cheaper since we won't need an extra insn to
1740 load it. */
1742 pp->mem_cost
1743 = ((recog_data.operand_type[i] != OP_IN
1744 ? MEMORY_MOVE_COST (mode, classes[i], 0)
1745 : 0)
1746 + (recog_data.operand_type[i] != OP_OUT
1747 ? MEMORY_MOVE_COST (mode, classes[i], 1)
1748 : 0) - allows_mem[i]);
1750 /* If we have assigned a class to this register in our
1751 first pass, add a cost to this alternative corresponding
1752 to what we would add if this register were not in the
1753 appropriate class. */
1755 if (reg_pref)
1756 alt_cost
1757 += (may_move_in_cost[mode]
1758 [(unsigned char) reg_pref[REGNO (op)].prefclass]
1759 [(int) classes[i]]);
1763 /* Otherwise, if this alternative wins, either because we
1764 have already determined that or if we have a hard register of
1765 the proper class, there is no cost for this alternative. */
1767 else if (win
1768 || (REG_P (op)
1769 && reg_fits_class_p (op, classes[i], 0, GET_MODE (op))))
1772 /* If registers are valid, the cost of this alternative includes
1773 copying the object to and/or from a register. */
1775 else if (classes[i] != NO_REGS)
1777 if (recog_data.operand_type[i] != OP_OUT)
1778 alt_cost += copy_cost (op, mode, classes[i], 1);
1780 if (recog_data.operand_type[i] != OP_IN)
1781 alt_cost += copy_cost (op, mode, classes[i], 0);
1784 /* The only other way this alternative can be used is if this is a
1785 constant that could be placed into memory. */
1787 else if (CONSTANT_P (op) && (allows_addr || allows_mem[i]))
1788 alt_cost += MEMORY_MOVE_COST (mode, classes[i], 1);
1789 else
1790 alt_fail = 1;
1793 if (alt_fail)
1794 continue;
1796 /* Finally, update the costs with the information we've calculated
1797 about this alternative. */
1799 for (i = 0; i < n_ops; i++)
1800 if (REG_P (ops[i])
1801 && REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
1803 struct costs *pp = &op_costs[i], *qq = &this_op_costs[i];
1804 int scale = 1 + (recog_data.operand_type[i] == OP_INOUT);
1806 pp->mem_cost = MIN (pp->mem_cost,
1807 (qq->mem_cost + alt_cost) * scale);
1809 for (class = 0; class < N_REG_CLASSES; class++)
1810 pp->cost[class] = MIN (pp->cost[class],
1811 (qq->cost[class] + alt_cost) * scale);
1815 /* If this insn is a single set copying operand 1 to operand 0
1816 and one operand is a pseudo with the other a hard reg or a pseudo
1817 that prefers a register that is in its own register class then
1818 we may want to adjust the cost of that register class to -1.
1820 Avoid the adjustment if the source does not die to avoid stressing of
1821 register allocator by preferrencing two colliding registers into single
1822 class.
1824 Also avoid the adjustment if a copy between registers of the class
1825 is expensive (ten times the cost of a default copy is considered
1826 arbitrarily expensive). This avoids losing when the preferred class
1827 is very expensive as the source of a copy instruction. */
1829 if ((set = single_set (insn)) != 0
1830 && ops[0] == SET_DEST (set) && ops[1] == SET_SRC (set)
1831 && REG_P (ops[0]) && REG_P (ops[1])
1832 && find_regno_note (insn, REG_DEAD, REGNO (ops[1])))
1833 for (i = 0; i <= 1; i++)
1834 if (REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
1836 unsigned int regno = REGNO (ops[!i]);
1837 enum machine_mode mode = GET_MODE (ops[!i]);
1838 int class;
1839 unsigned int nr;
1841 if (regno >= FIRST_PSEUDO_REGISTER && reg_pref != 0)
1843 enum reg_class pref = reg_pref[regno].prefclass;
1845 if ((reg_class_size[(unsigned char) pref]
1846 == (unsigned) CLASS_MAX_NREGS (pref, mode))
1847 && REGISTER_MOVE_COST (mode, pref, pref) < 10 * 2)
1848 op_costs[i].cost[(unsigned char) pref] = -1;
1850 else if (regno < FIRST_PSEUDO_REGISTER)
1851 for (class = 0; class < N_REG_CLASSES; class++)
1852 if (TEST_HARD_REG_BIT (reg_class_contents[class], regno)
1853 && reg_class_size[class] == (unsigned) CLASS_MAX_NREGS (class, mode))
1855 if (reg_class_size[class] == 1)
1856 op_costs[i].cost[class] = -1;
1857 else
1859 for (nr = 0; nr < (unsigned) hard_regno_nregs[regno][mode]; nr++)
1861 if (! TEST_HARD_REG_BIT (reg_class_contents[class],
1862 regno + nr))
1863 break;
1866 if (nr == (unsigned) hard_regno_nregs[regno][mode])
1867 op_costs[i].cost[class] = -1;
1873 /* Compute the cost of loading X into (if TO_P is nonzero) or from (if
1874 TO_P is zero) a register of class CLASS in mode MODE.
1876 X must not be a pseudo. */
1878 static int
1879 copy_cost (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED,
1880 enum reg_class class, int to_p ATTRIBUTE_UNUSED)
1882 #ifdef HAVE_SECONDARY_RELOADS
1883 enum reg_class secondary_class = NO_REGS;
1884 #endif
1886 /* If X is a SCRATCH, there is actually nothing to move since we are
1887 assuming optimal allocation. */
1889 if (GET_CODE (x) == SCRATCH)
1890 return 0;
1892 /* Get the class we will actually use for a reload. */
1893 class = PREFERRED_RELOAD_CLASS (x, class);
1895 #ifdef HAVE_SECONDARY_RELOADS
1896 /* If we need a secondary reload (we assume here that we are using
1897 the secondary reload as an intermediate, not a scratch register), the
1898 cost is that to load the input into the intermediate register, then
1899 to copy them. We use a special value of TO_P to avoid recursion. */
1901 #ifdef SECONDARY_INPUT_RELOAD_CLASS
1902 if (to_p == 1)
1903 secondary_class = SECONDARY_INPUT_RELOAD_CLASS (class, mode, x);
1904 #endif
1906 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
1907 if (! to_p)
1908 secondary_class = SECONDARY_OUTPUT_RELOAD_CLASS (class, mode, x);
1909 #endif
1911 if (secondary_class != NO_REGS)
1912 return (move_cost[mode][(int) secondary_class][(int) class]
1913 + copy_cost (x, mode, secondary_class, 2));
1914 #endif /* HAVE_SECONDARY_RELOADS */
1916 /* For memory, use the memory move cost, for (hard) registers, use the
1917 cost to move between the register classes, and use 2 for everything
1918 else (constants). */
1920 if (MEM_P (x) || class == NO_REGS)
1921 return MEMORY_MOVE_COST (mode, class, to_p);
1923 else if (REG_P (x))
1924 return move_cost[mode][(int) REGNO_REG_CLASS (REGNO (x))][(int) class];
1926 else
1927 /* If this is a constant, we may eventually want to call rtx_cost here. */
1928 return COSTS_N_INSNS (1);
1931 /* Record the pseudo registers we must reload into hard registers
1932 in a subexpression of a memory address, X.
1934 CLASS is the class that the register needs to be in and is either
1935 BASE_REG_CLASS or INDEX_REG_CLASS.
1937 SCALE is twice the amount to multiply the cost by (it is twice so we
1938 can represent half-cost adjustments). */
1940 static void
1941 record_address_regs (rtx x, enum reg_class class, int scale)
1943 enum rtx_code code = GET_CODE (x);
1945 switch (code)
1947 case CONST_INT:
1948 case CONST:
1949 case CC0:
1950 case PC:
1951 case SYMBOL_REF:
1952 case LABEL_REF:
1953 return;
1955 case PLUS:
1956 /* When we have an address that is a sum,
1957 we must determine whether registers are "base" or "index" regs.
1958 If there is a sum of two registers, we must choose one to be
1959 the "base". Luckily, we can use the REG_POINTER to make a good
1960 choice most of the time. We only need to do this on machines
1961 that can have two registers in an address and where the base
1962 and index register classes are different.
1964 ??? This code used to set REGNO_POINTER_FLAG in some cases, but
1965 that seems bogus since it should only be set when we are sure
1966 the register is being used as a pointer. */
1969 rtx arg0 = XEXP (x, 0);
1970 rtx arg1 = XEXP (x, 1);
1971 enum rtx_code code0 = GET_CODE (arg0);
1972 enum rtx_code code1 = GET_CODE (arg1);
1974 /* Look inside subregs. */
1975 if (code0 == SUBREG)
1976 arg0 = SUBREG_REG (arg0), code0 = GET_CODE (arg0);
1977 if (code1 == SUBREG)
1978 arg1 = SUBREG_REG (arg1), code1 = GET_CODE (arg1);
1980 /* If this machine only allows one register per address, it must
1981 be in the first operand. */
1983 if (MAX_REGS_PER_ADDRESS == 1)
1984 record_address_regs (arg0, class, scale);
1986 /* If index and base registers are the same on this machine, just
1987 record registers in any non-constant operands. We assume here,
1988 as well as in the tests below, that all addresses are in
1989 canonical form. */
1991 else if (INDEX_REG_CLASS == MODE_BASE_REG_CLASS (VOIDmode))
1993 record_address_regs (arg0, class, scale);
1994 if (! CONSTANT_P (arg1))
1995 record_address_regs (arg1, class, scale);
1998 /* If the second operand is a constant integer, it doesn't change
1999 what class the first operand must be. */
2001 else if (code1 == CONST_INT || code1 == CONST_DOUBLE)
2002 record_address_regs (arg0, class, scale);
2004 /* If the second operand is a symbolic constant, the first operand
2005 must be an index register. */
2007 else if (code1 == SYMBOL_REF || code1 == CONST || code1 == LABEL_REF)
2008 record_address_regs (arg0, INDEX_REG_CLASS, scale);
2010 /* If both operands are registers but one is already a hard register
2011 of index or reg-base class, give the other the class that the
2012 hard register is not. */
2014 else if (code0 == REG && code1 == REG
2015 && REGNO (arg0) < FIRST_PSEUDO_REGISTER
2016 && (REG_MODE_OK_FOR_REG_BASE_P (arg0, VOIDmode)
2017 || REG_OK_FOR_INDEX_P (arg0)))
2018 record_address_regs (arg1,
2019 REG_MODE_OK_FOR_REG_BASE_P (arg0, VOIDmode)
2020 ? INDEX_REG_CLASS
2021 : MODE_BASE_REG_REG_CLASS (VOIDmode),
2022 scale);
2023 else if (code0 == REG && code1 == REG
2024 && REGNO (arg1) < FIRST_PSEUDO_REGISTER
2025 && (REG_MODE_OK_FOR_REG_BASE_P (arg1, VOIDmode)
2026 || REG_OK_FOR_INDEX_P (arg1)))
2027 record_address_regs (arg0,
2028 REG_MODE_OK_FOR_REG_BASE_P (arg1, VOIDmode)
2029 ? INDEX_REG_CLASS
2030 : MODE_BASE_REG_REG_CLASS (VOIDmode),
2031 scale);
2033 /* If one operand is known to be a pointer, it must be the base
2034 with the other operand the index. Likewise if the other operand
2035 is a MULT. */
2037 else if ((code0 == REG && REG_POINTER (arg0))
2038 || code1 == MULT)
2040 record_address_regs (arg0, MODE_BASE_REG_REG_CLASS (VOIDmode),
2041 scale);
2042 record_address_regs (arg1, INDEX_REG_CLASS, scale);
2044 else if ((code1 == REG && REG_POINTER (arg1))
2045 || code0 == MULT)
2047 record_address_regs (arg0, INDEX_REG_CLASS, scale);
2048 record_address_regs (arg1, MODE_BASE_REG_REG_CLASS (VOIDmode),
2049 scale);
2052 /* Otherwise, count equal chances that each might be a base
2053 or index register. This case should be rare. */
2055 else
2057 record_address_regs (arg0, MODE_BASE_REG_REG_CLASS (VOIDmode),
2058 scale / 2);
2059 record_address_regs (arg0, INDEX_REG_CLASS, scale / 2);
2060 record_address_regs (arg1, MODE_BASE_REG_REG_CLASS (VOIDmode),
2061 scale / 2);
2062 record_address_regs (arg1, INDEX_REG_CLASS, scale / 2);
2065 break;
2067 /* Double the importance of a pseudo register that is incremented
2068 or decremented, since it would take two extra insns
2069 if it ends up in the wrong place. */
2070 case POST_MODIFY:
2071 case PRE_MODIFY:
2072 record_address_regs (XEXP (x, 0), MODE_BASE_REG_CLASS (VOIDmode),
2073 2 * scale);
2074 if (REG_P (XEXP (XEXP (x, 1), 1)))
2075 record_address_regs (XEXP (XEXP (x, 1), 1),
2076 INDEX_REG_CLASS, 2 * scale);
2077 break;
2079 case POST_INC:
2080 case PRE_INC:
2081 case POST_DEC:
2082 case PRE_DEC:
2083 /* Double the importance of a pseudo register that is incremented
2084 or decremented, since it would take two extra insns
2085 if it ends up in the wrong place. If the operand is a pseudo,
2086 show it is being used in an INC_DEC context. */
2088 #ifdef FORBIDDEN_INC_DEC_CLASSES
2089 if (REG_P (XEXP (x, 0))
2090 && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER)
2091 in_inc_dec[REGNO (XEXP (x, 0))] = 1;
2092 #endif
2094 record_address_regs (XEXP (x, 0), class, 2 * scale);
2095 break;
2097 case REG:
2099 struct costs *pp = &costs[REGNO (x)];
2100 int i;
2102 pp->mem_cost += (MEMORY_MOVE_COST (Pmode, class, 1) * scale) / 2;
2104 for (i = 0; i < N_REG_CLASSES; i++)
2105 pp->cost[i] += (may_move_in_cost[Pmode][i][(int) class] * scale) / 2;
2107 break;
2109 default:
2111 const char *fmt = GET_RTX_FORMAT (code);
2112 int i;
2113 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2114 if (fmt[i] == 'e')
2115 record_address_regs (XEXP (x, i), class, scale);
2120 #ifdef FORBIDDEN_INC_DEC_CLASSES
2122 /* Return 1 if REG is valid as an auto-increment memory reference
2123 to an object of MODE. */
2125 static int
2126 auto_inc_dec_reg_p (rtx reg, enum machine_mode mode)
2128 if (HAVE_POST_INCREMENT
2129 && memory_address_p (mode, gen_rtx_POST_INC (Pmode, reg)))
2130 return 1;
2132 if (HAVE_POST_DECREMENT
2133 && memory_address_p (mode, gen_rtx_POST_DEC (Pmode, reg)))
2134 return 1;
2136 if (HAVE_PRE_INCREMENT
2137 && memory_address_p (mode, gen_rtx_PRE_INC (Pmode, reg)))
2138 return 1;
2140 if (HAVE_PRE_DECREMENT
2141 && memory_address_p (mode, gen_rtx_PRE_DEC (Pmode, reg)))
2142 return 1;
2144 return 0;
2146 #endif
2148 static short *renumber;
2149 static size_t regno_allocated;
2150 static unsigned int reg_n_max;
2152 /* Allocate enough space to hold NUM_REGS registers for the tables used for
2153 reg_scan and flow_analysis that are indexed by the register number. If
2154 NEW_P is nonzero, initialize all of the registers, otherwise only
2155 initialize the new registers allocated. The same table is kept from
2156 function to function, only reallocating it when we need more room. If
2157 RENUMBER_P is nonzero, allocate the reg_renumber array also. */
2159 void
2160 allocate_reg_info (size_t num_regs, int new_p, int renumber_p)
2162 size_t size_info;
2163 size_t size_renumber;
2164 size_t min = (new_p) ? 0 : reg_n_max;
2165 struct reg_info_data *reg_data;
2167 if (num_regs > regno_allocated)
2169 size_t old_allocated = regno_allocated;
2171 regno_allocated = num_regs + (num_regs / 20); /* Add some slop space. */
2172 size_renumber = regno_allocated * sizeof (short);
2174 if (!reg_n_info)
2176 VARRAY_REG_INIT (reg_n_info, regno_allocated, "reg_n_info");
2177 renumber = xmalloc (size_renumber);
2178 reg_pref_buffer = xmalloc (regno_allocated
2179 * sizeof (struct reg_pref));
2181 else
2183 VARRAY_GROW (reg_n_info, regno_allocated);
2185 if (new_p) /* If we're zapping everything, no need to realloc. */
2187 free ((char *) renumber);
2188 free ((char *) reg_pref);
2189 renumber = xmalloc (size_renumber);
2190 reg_pref_buffer = xmalloc (regno_allocated
2191 * sizeof (struct reg_pref));
2194 else
2196 renumber = xrealloc (renumber, size_renumber);
2197 reg_pref_buffer = xrealloc (reg_pref_buffer,
2198 regno_allocated
2199 * sizeof (struct reg_pref));
2203 size_info = (regno_allocated - old_allocated) * sizeof (reg_info)
2204 + sizeof (struct reg_info_data) - sizeof (reg_info);
2205 reg_data = xcalloc (size_info, 1);
2206 reg_data->min_index = old_allocated;
2207 reg_data->max_index = regno_allocated - 1;
2208 reg_data->next = reg_info_head;
2209 reg_info_head = reg_data;
2212 reg_n_max = num_regs;
2213 if (min < num_regs)
2215 /* Loop through each of the segments allocated for the actual
2216 reg_info pages, and set up the pointers, zero the pages, etc. */
2217 for (reg_data = reg_info_head;
2218 reg_data && reg_data->max_index >= min;
2219 reg_data = reg_data->next)
2221 size_t min_index = reg_data->min_index;
2222 size_t max_index = reg_data->max_index;
2223 size_t max = MIN (max_index, num_regs);
2224 size_t local_min = min - min_index;
2225 size_t i;
2227 if (reg_data->min_index > num_regs)
2228 continue;
2230 if (min < min_index)
2231 local_min = 0;
2232 if (!reg_data->used_p) /* page just allocated with calloc */
2233 reg_data->used_p = 1; /* no need to zero */
2234 else
2235 memset (&reg_data->data[local_min], 0,
2236 sizeof (reg_info) * (max - min_index - local_min + 1));
2238 for (i = min_index+local_min; i <= max; i++)
2240 VARRAY_REG (reg_n_info, i) = &reg_data->data[i-min_index];
2241 REG_BASIC_BLOCK (i) = REG_BLOCK_UNKNOWN;
2242 renumber[i] = -1;
2243 reg_pref_buffer[i].prefclass = (char) NO_REGS;
2244 reg_pref_buffer[i].altclass = (char) NO_REGS;
2249 /* If {pref,alt}class have already been allocated, update the pointers to
2250 the newly realloced ones. */
2251 if (reg_pref)
2252 reg_pref = reg_pref_buffer;
2254 if (renumber_p)
2255 reg_renumber = renumber;
2258 /* Free up the space allocated by allocate_reg_info. */
2259 void
2260 free_reg_info (void)
2262 if (reg_n_info)
2264 struct reg_info_data *reg_data;
2265 struct reg_info_data *reg_next;
2267 VARRAY_FREE (reg_n_info);
2268 for (reg_data = reg_info_head; reg_data; reg_data = reg_next)
2270 reg_next = reg_data->next;
2271 free ((char *) reg_data);
2274 free (reg_pref_buffer);
2275 reg_pref_buffer = (struct reg_pref *) 0;
2276 reg_info_head = (struct reg_info_data *) 0;
2277 renumber = (short *) 0;
2279 regno_allocated = 0;
2280 reg_n_max = 0;
2283 /* This is the `regscan' pass of the compiler, run just before cse
2284 and again just before loop.
2286 It finds the first and last use of each pseudo-register
2287 and records them in the vectors regno_first_uid, regno_last_uid
2288 and counts the number of sets in the vector reg_n_sets.
2290 REPEAT is nonzero the second time this is called. */
2292 /* Maximum number of parallel sets and clobbers in any insn in this fn.
2293 Always at least 3, since the combiner could put that many together
2294 and we want this to remain correct for all the remaining passes.
2295 This corresponds to the maximum number of times note_stores will call
2296 a function for any insn. */
2298 int max_parallel;
2300 /* Used as a temporary to record the largest number of registers in
2301 PARALLEL in a SET_DEST. This is added to max_parallel. */
2303 static int max_set_parallel;
2305 void
2306 reg_scan (rtx f, unsigned int nregs, int repeat ATTRIBUTE_UNUSED)
2308 rtx insn;
2310 timevar_push (TV_REG_SCAN);
2312 allocate_reg_info (nregs, TRUE, FALSE);
2313 max_parallel = 3;
2314 max_set_parallel = 0;
2316 for (insn = f; insn; insn = NEXT_INSN (insn))
2317 if (INSN_P (insn))
2319 rtx pat = PATTERN (insn);
2320 if (GET_CODE (pat) == PARALLEL
2321 && XVECLEN (pat, 0) > max_parallel)
2322 max_parallel = XVECLEN (pat, 0);
2323 reg_scan_mark_refs (pat, insn, 0, 0);
2325 if (REG_NOTES (insn))
2326 reg_scan_mark_refs (REG_NOTES (insn), insn, 1, 0);
2329 max_parallel += max_set_parallel;
2331 timevar_pop (TV_REG_SCAN);
2334 /* Update 'regscan' information by looking at the insns
2335 from FIRST to LAST. Some new REGs have been created,
2336 and any REG with number greater than OLD_MAX_REGNO is
2337 such a REG. We only update information for those. */
2339 void
2340 reg_scan_update (rtx first, rtx last, unsigned int old_max_regno)
2342 rtx insn;
2344 allocate_reg_info (max_reg_num (), FALSE, FALSE);
2346 for (insn = first; insn != last; insn = NEXT_INSN (insn))
2347 if (INSN_P (insn))
2349 rtx pat = PATTERN (insn);
2350 if (GET_CODE (pat) == PARALLEL
2351 && XVECLEN (pat, 0) > max_parallel)
2352 max_parallel = XVECLEN (pat, 0);
2353 reg_scan_mark_refs (pat, insn, 0, old_max_regno);
2355 if (REG_NOTES (insn))
2356 reg_scan_mark_refs (REG_NOTES (insn), insn, 1, old_max_regno);
2360 /* X is the expression to scan. INSN is the insn it appears in.
2361 NOTE_FLAG is nonzero if X is from INSN's notes rather than its body.
2362 We should only record information for REGs with numbers
2363 greater than or equal to MIN_REGNO. */
2365 static void
2366 reg_scan_mark_refs (rtx x, rtx insn, int note_flag, unsigned int min_regno)
2368 enum rtx_code code;
2369 rtx dest;
2370 rtx note;
2372 if (!x)
2373 return;
2374 code = GET_CODE (x);
2375 switch (code)
2377 case CONST:
2378 case CONST_INT:
2379 case CONST_DOUBLE:
2380 case CONST_VECTOR:
2381 case CC0:
2382 case PC:
2383 case SYMBOL_REF:
2384 case LABEL_REF:
2385 case ADDR_VEC:
2386 case ADDR_DIFF_VEC:
2387 return;
2389 case REG:
2391 unsigned int regno = REGNO (x);
2393 if (regno >= min_regno)
2395 if (!note_flag)
2396 REGNO_LAST_UID (regno) = INSN_UID (insn);
2397 if (REGNO_FIRST_UID (regno) == 0)
2398 REGNO_FIRST_UID (regno) = INSN_UID (insn);
2399 /* If we are called by reg_scan_update() (indicated by min_regno
2400 being set), we also need to update the reference count. */
2401 if (min_regno)
2402 REG_N_REFS (regno)++;
2405 break;
2407 case EXPR_LIST:
2408 if (XEXP (x, 0))
2409 reg_scan_mark_refs (XEXP (x, 0), insn, note_flag, min_regno);
2410 if (XEXP (x, 1))
2411 reg_scan_mark_refs (XEXP (x, 1), insn, note_flag, min_regno);
2412 break;
2414 case INSN_LIST:
2415 if (XEXP (x, 1))
2416 reg_scan_mark_refs (XEXP (x, 1), insn, note_flag, min_regno);
2417 break;
2419 case CLOBBER:
2421 rtx reg = XEXP (x, 0);
2422 if (REG_P (reg)
2423 && REGNO (reg) >= min_regno)
2425 REG_N_SETS (REGNO (reg))++;
2426 REG_N_REFS (REGNO (reg))++;
2428 else if (MEM_P (reg))
2429 reg_scan_mark_refs (XEXP (reg, 0), insn, note_flag, min_regno);
2431 break;
2433 case SET:
2434 /* Count a set of the destination if it is a register. */
2435 for (dest = SET_DEST (x);
2436 GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
2437 || GET_CODE (dest) == ZERO_EXTEND;
2438 dest = XEXP (dest, 0))
2441 /* For a PARALLEL, record the number of things (less the usual one for a
2442 SET) that are set. */
2443 if (GET_CODE (dest) == PARALLEL)
2444 max_set_parallel = MAX (max_set_parallel, XVECLEN (dest, 0) - 1);
2446 if (REG_P (dest)
2447 && REGNO (dest) >= min_regno)
2449 REG_N_SETS (REGNO (dest))++;
2450 REG_N_REFS (REGNO (dest))++;
2453 /* If this is setting a pseudo from another pseudo or the sum of a
2454 pseudo and a constant integer and the other pseudo is known to be
2455 a pointer, set the destination to be a pointer as well.
2457 Likewise if it is setting the destination from an address or from a
2458 value equivalent to an address or to the sum of an address and
2459 something else.
2461 But don't do any of this if the pseudo corresponds to a user
2462 variable since it should have already been set as a pointer based
2463 on the type. */
2465 if (REG_P (SET_DEST (x))
2466 && REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER
2467 && REGNO (SET_DEST (x)) >= min_regno
2468 /* If the destination pseudo is set more than once, then other
2469 sets might not be to a pointer value (consider access to a
2470 union in two threads of control in the presence of global
2471 optimizations). So only set REG_POINTER on the destination
2472 pseudo if this is the only set of that pseudo. */
2473 && REG_N_SETS (REGNO (SET_DEST (x))) == 1
2474 && ! REG_USERVAR_P (SET_DEST (x))
2475 && ! REG_POINTER (SET_DEST (x))
2476 && ((REG_P (SET_SRC (x))
2477 && REG_POINTER (SET_SRC (x)))
2478 || ((GET_CODE (SET_SRC (x)) == PLUS
2479 || GET_CODE (SET_SRC (x)) == LO_SUM)
2480 && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2481 && REG_P (XEXP (SET_SRC (x), 0))
2482 && REG_POINTER (XEXP (SET_SRC (x), 0)))
2483 || GET_CODE (SET_SRC (x)) == CONST
2484 || GET_CODE (SET_SRC (x)) == SYMBOL_REF
2485 || GET_CODE (SET_SRC (x)) == LABEL_REF
2486 || (GET_CODE (SET_SRC (x)) == HIGH
2487 && (GET_CODE (XEXP (SET_SRC (x), 0)) == CONST
2488 || GET_CODE (XEXP (SET_SRC (x), 0)) == SYMBOL_REF
2489 || GET_CODE (XEXP (SET_SRC (x), 0)) == LABEL_REF))
2490 || ((GET_CODE (SET_SRC (x)) == PLUS
2491 || GET_CODE (SET_SRC (x)) == LO_SUM)
2492 && (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST
2493 || GET_CODE (XEXP (SET_SRC (x), 1)) == SYMBOL_REF
2494 || GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF))
2495 || ((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
2496 && (GET_CODE (XEXP (note, 0)) == CONST
2497 || GET_CODE (XEXP (note, 0)) == SYMBOL_REF
2498 || GET_CODE (XEXP (note, 0)) == LABEL_REF))))
2499 REG_POINTER (SET_DEST (x)) = 1;
2501 /* If this is setting a register from a register or from a simple
2502 conversion of a register, propagate REG_EXPR. */
2503 if (REG_P (dest))
2505 rtx src = SET_SRC (x);
2507 while (GET_CODE (src) == SIGN_EXTEND
2508 || GET_CODE (src) == ZERO_EXTEND
2509 || GET_CODE (src) == TRUNCATE
2510 || (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)))
2511 src = XEXP (src, 0);
2513 if (!REG_ATTRS (dest) && REG_P (src))
2514 REG_ATTRS (dest) = REG_ATTRS (src);
2515 if (!REG_ATTRS (dest) && MEM_P (src))
2516 set_reg_attrs_from_mem (dest, src);
2519 /* ... fall through ... */
2521 default:
2523 const char *fmt = GET_RTX_FORMAT (code);
2524 int i;
2525 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2527 if (fmt[i] == 'e')
2528 reg_scan_mark_refs (XEXP (x, i), insn, note_flag, min_regno);
2529 else if (fmt[i] == 'E' && XVEC (x, i) != 0)
2531 int j;
2532 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2533 reg_scan_mark_refs (XVECEXP (x, i, j), insn, note_flag, min_regno);
2540 /* Return nonzero if C1 is a subset of C2, i.e., if every register in C1
2541 is also in C2. */
2544 reg_class_subset_p (enum reg_class c1, enum reg_class c2)
2546 if (c1 == c2) return 1;
2548 if (c2 == ALL_REGS)
2549 win:
2550 return 1;
2551 GO_IF_HARD_REG_SUBSET (reg_class_contents[(int) c1],
2552 reg_class_contents[(int) c2],
2553 win);
2554 return 0;
2557 /* Return nonzero if there is a register that is in both C1 and C2. */
2560 reg_classes_intersect_p (enum reg_class c1, enum reg_class c2)
2562 HARD_REG_SET c;
2564 if (c1 == c2) return 1;
2566 if (c1 == ALL_REGS || c2 == ALL_REGS)
2567 return 1;
2569 COPY_HARD_REG_SET (c, reg_class_contents[(int) c1]);
2570 AND_HARD_REG_SET (c, reg_class_contents[(int) c2]);
2572 GO_IF_HARD_REG_SUBSET (c, reg_class_contents[(int) NO_REGS], lose);
2573 return 1;
2575 lose:
2576 return 0;
2579 #ifdef CANNOT_CHANGE_MODE_CLASS
2581 struct subregs_of_mode_node
2583 unsigned int block;
2584 unsigned char modes[MAX_MACHINE_MODE];
2587 static htab_t subregs_of_mode;
2589 static hashval_t
2590 som_hash (const void *x)
2592 const struct subregs_of_mode_node *a = x;
2593 return a->block;
2596 static int
2597 som_eq (const void *x, const void *y)
2599 const struct subregs_of_mode_node *a = x;
2600 const struct subregs_of_mode_node *b = y;
2601 return a->block == b->block;
2604 void
2605 init_subregs_of_mode (void)
2607 if (subregs_of_mode)
2608 htab_empty (subregs_of_mode);
2609 else
2610 subregs_of_mode = htab_create (100, som_hash, som_eq, free);
2613 void
2614 record_subregs_of_mode (rtx subreg)
2616 struct subregs_of_mode_node dummy, *node;
2617 enum machine_mode mode;
2618 unsigned int regno;
2619 void **slot;
2621 if (!REG_P (SUBREG_REG (subreg)))
2622 return;
2624 regno = REGNO (SUBREG_REG (subreg));
2625 mode = GET_MODE (subreg);
2627 if (regno < FIRST_PSEUDO_REGISTER)
2628 return;
2630 dummy.block = regno & -8;
2631 slot = htab_find_slot_with_hash (subregs_of_mode, &dummy,
2632 dummy.block, INSERT);
2633 node = *slot;
2634 if (node == NULL)
2636 node = xcalloc (1, sizeof (*node));
2637 node->block = regno & -8;
2638 *slot = node;
2641 node->modes[mode] |= 1 << (regno & 7);
2644 /* Set bits in *USED which correspond to registers which can't change
2645 their mode from FROM to any mode in which REGNO was encountered. */
2647 void
2648 cannot_change_mode_set_regs (HARD_REG_SET *used, enum machine_mode from,
2649 unsigned int regno)
2651 struct subregs_of_mode_node dummy, *node;
2652 enum machine_mode to;
2653 unsigned char mask;
2654 unsigned int i;
2656 dummy.block = regno & -8;
2657 node = htab_find_with_hash (subregs_of_mode, &dummy, dummy.block);
2658 if (node == NULL)
2659 return;
2661 mask = 1 << (regno & 7);
2662 for (to = VOIDmode; to < NUM_MACHINE_MODES; to++)
2663 if (node->modes[to] & mask)
2664 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2665 if (!TEST_HARD_REG_BIT (*used, i)
2666 && REG_CANNOT_CHANGE_MODE_P (i, from, to))
2667 SET_HARD_REG_BIT (*used, i);
2670 /* Return 1 if REGNO has had an invalid mode change in CLASS from FROM
2671 mode. */
2673 bool
2674 invalid_mode_change_p (unsigned int regno, enum reg_class class,
2675 enum machine_mode from)
2677 struct subregs_of_mode_node dummy, *node;
2678 enum machine_mode to;
2679 unsigned char mask;
2681 dummy.block = regno & -8;
2682 node = htab_find_with_hash (subregs_of_mode, &dummy, dummy.block);
2683 if (node == NULL)
2684 return false;
2686 mask = 1 << (regno & 7);
2687 for (to = VOIDmode; to < NUM_MACHINE_MODES; to++)
2688 if (node->modes[to] & mask)
2689 if (CANNOT_CHANGE_MODE_CLASS (from, to, class))
2690 return true;
2692 return false;
2694 #endif /* CANNOT_CHANGE_MODE_CLASS */
2696 #include "gt-regclass.h"