2003-03-15 Glen Nakamura <glen@imodulo.com>
[official-gcc.git] / gcc / regclass.c
blob01c08f3a07861dac6f392d82eb2ccdecff5ff6f2
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 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
23 /* This file contains two passes of the compiler: reg_scan and reg_class.
24 It also defines some tables of information about the hardware registers
25 and a function init_reg_sets to initialize the tables. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "hard-reg-set.h"
32 #include "rtl.h"
33 #include "expr.h"
34 #include "tm_p.h"
35 #include "flags.h"
36 #include "basic-block.h"
37 #include "regs.h"
38 #include "function.h"
39 #include "insn-config.h"
40 #include "recog.h"
41 #include "reload.h"
42 #include "real.h"
43 #include "toplev.h"
44 #include "output.h"
45 #include "ggc.h"
46 #include "timevar.h"
48 #ifndef REGISTER_MOVE_COST
49 #define REGISTER_MOVE_COST(m, x, y) 2
50 #endif
52 static void init_reg_sets_1 PARAMS ((void));
53 static void init_reg_modes PARAMS ((void));
54 static void init_reg_autoinc PARAMS ((void));
56 /* If we have auto-increment or auto-decrement and we can have secondary
57 reloads, we are not allowed to use classes requiring secondary
58 reloads for pseudos auto-incremented since reload can't handle it. */
60 #ifdef AUTO_INC_DEC
61 #if defined(SECONDARY_INPUT_RELOAD_CLASS) || defined(SECONDARY_OUTPUT_RELOAD_CLASS)
62 #define FORBIDDEN_INC_DEC_CLASSES
63 #endif
64 #endif
66 /* Register tables used by many passes. */
68 /* Indexed by hard register number, contains 1 for registers
69 that are fixed use (stack pointer, pc, frame pointer, etc.).
70 These are the registers that cannot be used to allocate
71 a pseudo reg for general use. */
73 char fixed_regs[FIRST_PSEUDO_REGISTER];
75 /* Same info as a HARD_REG_SET. */
77 HARD_REG_SET fixed_reg_set;
79 /* Data for initializing the above. */
81 static const char initial_fixed_regs[] = FIXED_REGISTERS;
83 /* Indexed by hard register number, contains 1 for registers
84 that are fixed use or are clobbered by function calls.
85 These are the registers that cannot be used to allocate
86 a pseudo reg whose life crosses calls unless we are able
87 to save/restore them across the calls. */
89 char call_used_regs[FIRST_PSEUDO_REGISTER];
91 /* Same info as a HARD_REG_SET. */
93 HARD_REG_SET call_used_reg_set;
95 /* HARD_REG_SET of registers we want to avoid caller saving. */
96 HARD_REG_SET losing_caller_save_reg_set;
98 /* Data for initializing the above. */
100 static const char initial_call_used_regs[] = CALL_USED_REGISTERS;
102 /* This is much like call_used_regs, except it doesn't have to
103 be a superset of FIXED_REGISTERS. This vector indicates
104 what is really call clobbered, and is used when defining
105 regs_invalidated_by_call. */
107 #ifdef CALL_REALLY_USED_REGISTERS
108 char call_really_used_regs[] = CALL_REALLY_USED_REGISTERS;
109 #endif
111 /* Indexed by hard register number, contains 1 for registers that are
112 fixed use or call used registers that cannot hold quantities across
113 calls even if we are willing to save and restore them. call fixed
114 registers are a subset of call used registers. */
116 char call_fixed_regs[FIRST_PSEUDO_REGISTER];
118 /* The same info as a HARD_REG_SET. */
120 HARD_REG_SET call_fixed_reg_set;
122 /* Number of non-fixed registers. */
124 int n_non_fixed_regs;
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 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 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. Unless
188 DEBUG_REGISTER_NAMES is defined, use the copy in print-rtl.c. */
190 #ifdef DEBUG_REGISTER_NAMES
191 const char * reg_names[] = REGISTER_NAMES;
192 #endif
194 /* For each hard register, the widest mode object that it can contain.
195 This will be a MODE_INT mode if the register can hold integers. Otherwise
196 it will be a MODE_FLOAT or a MODE_CC mode, whichever is valid for the
197 register. */
199 enum machine_mode reg_raw_mode[FIRST_PSEUDO_REGISTER];
201 /* 1 if class does contain register of given mode. */
203 static char contains_reg_of_mode [N_REG_CLASSES] [MAX_MACHINE_MODE];
205 /* Maximum cost of moving from a register in one class to a register in
206 another class. Based on REGISTER_MOVE_COST. */
208 static int move_cost[MAX_MACHINE_MODE][N_REG_CLASSES][N_REG_CLASSES];
210 /* Similar, but here we don't have to move if the first index is a subset
211 of the second so in that case the cost is zero. */
213 static int may_move_in_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 superset
216 of the second so in that case the cost is zero. */
218 static int may_move_out_cost[MAX_MACHINE_MODE][N_REG_CLASSES][N_REG_CLASSES];
220 #ifdef FORBIDDEN_INC_DEC_CLASSES
222 /* These are the classes that regs which are auto-incremented or decremented
223 cannot be put in. */
225 static int forbidden_inc_dec_class[N_REG_CLASSES];
227 /* Indexed by n, is nonzero if (REG n) is used in an auto-inc or auto-dec
228 context. */
230 static char *in_inc_dec;
232 #endif /* FORBIDDEN_INC_DEC_CLASSES */
234 #ifdef CANNOT_CHANGE_MODE_CLASS
235 /* All registers that have been subreged. Indexed by regno * MAX_MACHINE_MODE
236 + mode. */
237 bitmap_head subregs_of_mode;
238 #endif
240 /* Sample MEM values for use by memory_move_secondary_cost. */
242 static GTY(()) rtx top_of_stack[MAX_MACHINE_MODE];
244 /* Linked list of reg_info structures allocated for reg_n_info array.
245 Grouping all of the allocated structures together in one lump
246 means only one call to bzero to clear them, rather than n smaller
247 calls. */
248 struct reg_info_data {
249 struct reg_info_data *next; /* next set of reg_info structures */
250 size_t min_index; /* minimum index # */
251 size_t max_index; /* maximum index # */
252 char used_p; /* nonzero if this has been used previously */
253 reg_info data[1]; /* beginning of the reg_info data */
256 static struct reg_info_data *reg_info_head;
258 /* No more global register variables may be declared; true once
259 regclass has been initialized. */
261 static int no_global_reg_vars = 0;
264 /* Function called only once to initialize the above data on reg usage.
265 Once this is done, various switches may override. */
267 void
268 init_reg_sets ()
270 int i, j;
272 /* First copy the register information from the initial int form into
273 the regsets. */
275 for (i = 0; i < N_REG_CLASSES; i++)
277 CLEAR_HARD_REG_SET (reg_class_contents[i]);
279 /* Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
280 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
281 if (int_reg_class_contents[i][j / 32]
282 & ((unsigned) 1 << (j % 32)))
283 SET_HARD_REG_BIT (reg_class_contents[i], j);
286 memcpy (fixed_regs, initial_fixed_regs, sizeof fixed_regs);
287 memcpy (call_used_regs, initial_call_used_regs, sizeof call_used_regs);
288 memset (global_regs, 0, sizeof global_regs);
290 /* Do any additional initialization regsets may need. */
291 INIT_ONCE_REG_SET ();
293 #ifdef REG_ALLOC_ORDER
294 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
295 inv_reg_alloc_order[reg_alloc_order[i]] = i;
296 #endif
299 /* After switches have been processed, which perhaps alter
300 `fixed_regs' and `call_used_regs', convert them to HARD_REG_SETs. */
302 static void
303 init_reg_sets_1 ()
305 unsigned int i, j;
306 unsigned int /* enum machine_mode */ m;
307 char allocatable_regs_of_mode [MAX_MACHINE_MODE];
309 /* This macro allows the fixed or call-used registers
310 and the register classes to depend on target flags. */
312 #ifdef CONDITIONAL_REGISTER_USAGE
313 CONDITIONAL_REGISTER_USAGE;
314 #endif
316 /* Compute number of hard regs in each class. */
318 memset ((char *) reg_class_size, 0, sizeof reg_class_size);
319 for (i = 0; i < N_REG_CLASSES; i++)
320 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
321 if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
322 reg_class_size[i]++;
324 /* Initialize the table of subunions.
325 reg_class_subunion[I][J] gets the largest-numbered reg-class
326 that is contained in the union of classes I and J. */
328 for (i = 0; i < N_REG_CLASSES; i++)
330 for (j = 0; j < N_REG_CLASSES; j++)
332 #ifdef HARD_REG_SET
333 register /* Declare it register if it's a scalar. */
334 #endif
335 HARD_REG_SET c;
336 int k;
338 COPY_HARD_REG_SET (c, reg_class_contents[i]);
339 IOR_HARD_REG_SET (c, reg_class_contents[j]);
340 for (k = 0; k < N_REG_CLASSES; k++)
342 GO_IF_HARD_REG_SUBSET (reg_class_contents[k], c,
343 subclass1);
344 continue;
346 subclass1:
347 /* keep the largest subclass */ /* SPEE 900308 */
348 GO_IF_HARD_REG_SUBSET (reg_class_contents[k],
349 reg_class_contents[(int) reg_class_subunion[i][j]],
350 subclass2);
351 reg_class_subunion[i][j] = (enum reg_class) k;
352 subclass2:
358 /* Initialize the table of superunions.
359 reg_class_superunion[I][J] gets the smallest-numbered reg-class
360 containing the union of classes I and J. */
362 for (i = 0; i < N_REG_CLASSES; i++)
364 for (j = 0; j < N_REG_CLASSES; j++)
366 #ifdef HARD_REG_SET
367 register /* Declare it register if it's a scalar. */
368 #endif
369 HARD_REG_SET c;
370 int k;
372 COPY_HARD_REG_SET (c, reg_class_contents[i]);
373 IOR_HARD_REG_SET (c, reg_class_contents[j]);
374 for (k = 0; k < N_REG_CLASSES; k++)
375 GO_IF_HARD_REG_SUBSET (c, reg_class_contents[k], superclass);
377 superclass:
378 reg_class_superunion[i][j] = (enum reg_class) k;
382 /* Initialize the tables of subclasses and superclasses of each reg class.
383 First clear the whole table, then add the elements as they are found. */
385 for (i = 0; i < N_REG_CLASSES; i++)
387 for (j = 0; j < N_REG_CLASSES; j++)
389 reg_class_superclasses[i][j] = LIM_REG_CLASSES;
390 reg_class_subclasses[i][j] = LIM_REG_CLASSES;
394 for (i = 0; i < N_REG_CLASSES; i++)
396 if (i == (int) NO_REGS)
397 continue;
399 for (j = i + 1; j < N_REG_CLASSES; j++)
401 enum reg_class *p;
403 GO_IF_HARD_REG_SUBSET (reg_class_contents[i], reg_class_contents[j],
404 subclass);
405 continue;
406 subclass:
407 /* Reg class I is a subclass of J.
408 Add J to the table of superclasses of I. */
409 p = &reg_class_superclasses[i][0];
410 while (*p != LIM_REG_CLASSES) p++;
411 *p = (enum reg_class) j;
412 /* Add I to the table of superclasses of J. */
413 p = &reg_class_subclasses[j][0];
414 while (*p != LIM_REG_CLASSES) p++;
415 *p = (enum reg_class) i;
419 /* Initialize "constant" tables. */
421 CLEAR_HARD_REG_SET (fixed_reg_set);
422 CLEAR_HARD_REG_SET (call_used_reg_set);
423 CLEAR_HARD_REG_SET (call_fixed_reg_set);
424 CLEAR_HARD_REG_SET (regs_invalidated_by_call);
426 memcpy (call_fixed_regs, fixed_regs, sizeof call_fixed_regs);
428 n_non_fixed_regs = 0;
430 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
432 if (fixed_regs[i])
433 SET_HARD_REG_BIT (fixed_reg_set, i);
434 else
435 n_non_fixed_regs++;
437 if (call_used_regs[i])
438 SET_HARD_REG_BIT (call_used_reg_set, i);
439 if (call_fixed_regs[i])
440 SET_HARD_REG_BIT (call_fixed_reg_set, i);
441 if (CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (i)))
442 SET_HARD_REG_BIT (losing_caller_save_reg_set, i);
444 /* There are a couple of fixed registers that we know are safe to
445 exclude from being clobbered by calls:
447 The frame pointer is always preserved across calls. The arg pointer
448 is if it is fixed. The stack pointer usually is, unless
449 RETURN_POPS_ARGS, in which case an explicit CLOBBER will be present.
450 If we are generating PIC code, the PIC offset table register is
451 preserved across calls, though the target can override that. */
453 if (i == STACK_POINTER_REGNUM || i == FRAME_POINTER_REGNUM)
455 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
456 else if (i == HARD_FRAME_POINTER_REGNUM)
458 #endif
459 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
460 else if (i == ARG_POINTER_REGNUM && fixed_regs[i])
462 #endif
463 #ifndef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
464 else if (i == (unsigned) PIC_OFFSET_TABLE_REGNUM && fixed_regs[i])
466 #endif
467 else if (0
468 #ifdef CALL_REALLY_USED_REGISTERS
469 || call_really_used_regs[i]
470 #else
471 || call_used_regs[i]
472 #endif
473 || global_regs[i])
474 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
477 memset (contains_reg_of_mode, 0, sizeof (contains_reg_of_mode));
478 memset (allocatable_regs_of_mode, 0, sizeof (allocatable_regs_of_mode));
479 for (m = 0; m < (unsigned int) MAX_MACHINE_MODE; m++)
480 for (i = 0; i < N_REG_CLASSES; i++)
481 if ((unsigned) CLASS_MAX_NREGS (i, m) <= reg_class_size[i])
482 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
483 if (!fixed_regs [j] && TEST_HARD_REG_BIT (reg_class_contents[i], j)
484 && HARD_REGNO_MODE_OK (j, m))
486 contains_reg_of_mode [i][m] = 1;
487 allocatable_regs_of_mode [m] = 1;
488 break;
491 /* Initialize the move cost table. Find every subset of each class
492 and take the maximum cost of moving any subset to any other. */
494 for (m = 0; m < (unsigned int) MAX_MACHINE_MODE; m++)
495 if (allocatable_regs_of_mode [m])
497 for (i = 0; i < N_REG_CLASSES; i++)
498 if (contains_reg_of_mode [i][m])
499 for (j = 0; j < N_REG_CLASSES; j++)
501 int cost;
502 enum reg_class *p1, *p2;
504 if (!contains_reg_of_mode [j][m])
506 move_cost[m][i][j] = 65536;
507 may_move_in_cost[m][i][j] = 65536;
508 may_move_out_cost[m][i][j] = 65536;
510 else
512 cost = REGISTER_MOVE_COST (m, i, j);
514 for (p2 = &reg_class_subclasses[j][0];
515 *p2 != LIM_REG_CLASSES;
516 p2++)
517 if (*p2 != i && contains_reg_of_mode [*p2][m])
518 cost = MAX (cost, move_cost [m][i][*p2]);
520 for (p1 = &reg_class_subclasses[i][0];
521 *p1 != LIM_REG_CLASSES;
522 p1++)
523 if (*p1 != j && contains_reg_of_mode [*p1][m])
524 cost = MAX (cost, move_cost [m][*p1][j]);
526 move_cost[m][i][j] = cost;
528 if (reg_class_subset_p (i, j))
529 may_move_in_cost[m][i][j] = 0;
530 else
531 may_move_in_cost[m][i][j] = cost;
533 if (reg_class_subset_p (j, i))
534 may_move_out_cost[m][i][j] = 0;
535 else
536 may_move_out_cost[m][i][j] = cost;
539 else
540 for (j = 0; j < N_REG_CLASSES; j++)
542 move_cost[m][i][j] = 65536;
543 may_move_in_cost[m][i][j] = 65536;
544 may_move_out_cost[m][i][j] = 65536;
549 /* Compute the table of register modes.
550 These values are used to record death information for individual registers
551 (as opposed to a multi-register mode). */
553 static void
554 init_reg_modes ()
556 int i;
558 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
560 reg_raw_mode[i] = choose_hard_reg_mode (i, 1);
562 /* If we couldn't find a valid mode, just use the previous mode.
563 ??? One situation in which we need to do this is on the mips where
564 HARD_REGNO_NREGS (fpreg, [SD]Fmode) returns 2. Ideally we'd like
565 to use DF mode for the even registers and VOIDmode for the odd
566 (for the cpu models where the odd ones are inaccessible). */
567 if (reg_raw_mode[i] == VOIDmode)
568 reg_raw_mode[i] = i == 0 ? word_mode : reg_raw_mode[i-1];
572 /* Finish initializing the register sets and
573 initialize the register modes. */
575 void
576 init_regs ()
578 /* This finishes what was started by init_reg_sets, but couldn't be done
579 until after register usage was specified. */
580 init_reg_sets_1 ();
582 init_reg_modes ();
584 init_reg_autoinc ();
587 /* Initialize some fake stack-frame MEM references for use in
588 memory_move_secondary_cost. */
590 void
591 init_fake_stack_mems ()
593 #ifdef HAVE_SECONDARY_RELOADS
595 int i;
597 for (i = 0; i < MAX_MACHINE_MODE; i++)
598 top_of_stack[i] = gen_rtx_MEM (i, stack_pointer_rtx);
600 #endif
603 #ifdef HAVE_SECONDARY_RELOADS
605 /* Compute extra cost of moving registers to/from memory due to reloads.
606 Only needed if secondary reloads are required for memory moves. */
609 memory_move_secondary_cost (mode, class, in)
610 enum machine_mode mode;
611 enum reg_class class;
612 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 abort 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. */
665 enum machine_mode
666 choose_hard_reg_mode (regno, nregs)
667 unsigned int regno ATTRIBUTE_UNUSED;
668 unsigned int nregs;
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 found_mode = mode;
684 if (found_mode != VOIDmode)
685 return found_mode;
687 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
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 found_mode = mode;
694 if (found_mode != VOIDmode)
695 return found_mode;
697 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
698 mode != VOIDmode;
699 mode = GET_MODE_WIDER_MODE (mode))
700 if ((unsigned) HARD_REGNO_NREGS (regno, mode) == nregs
701 && HARD_REGNO_MODE_OK (regno, mode))
702 found_mode = mode;
704 if (found_mode != VOIDmode)
705 return found_mode;
707 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
708 mode != VOIDmode;
709 mode = GET_MODE_WIDER_MODE (mode))
710 if ((unsigned) HARD_REGNO_NREGS (regno, mode) == nregs
711 && HARD_REGNO_MODE_OK (regno, mode))
712 found_mode = mode;
714 if (found_mode != VOIDmode)
715 return found_mode;
717 /* Iterate over all of the CCmodes. */
718 for (m = (unsigned int) CCmode; m < (unsigned int) NUM_MACHINE_MODES; ++m)
720 mode = (enum machine_mode) m;
721 if ((unsigned) HARD_REGNO_NREGS (regno, mode) == nregs
722 && HARD_REGNO_MODE_OK (regno, mode))
723 return mode;
726 /* We can't find a mode valid for this register. */
727 return VOIDmode;
730 /* Specify the usage characteristics of the register named NAME.
731 It should be a fixed register if FIXED and a
732 call-used register if CALL_USED. */
734 void
735 fix_register (name, fixed, call_used)
736 const char *name;
737 int fixed, call_used;
739 int i;
741 /* Decode the name and update the primary form of
742 the register info. */
744 if ((i = decode_reg_name (name)) >= 0)
746 if ((i == STACK_POINTER_REGNUM
747 #ifdef HARD_FRAME_POINTER_REGNUM
748 || i == HARD_FRAME_POINTER_REGNUM
749 #else
750 || i == FRAME_POINTER_REGNUM
751 #endif
753 && (fixed == 0 || call_used == 0))
755 static const char * const what_option[2][2] = {
756 { "call-saved", "call-used" },
757 { "no-such-option", "fixed" }};
759 error ("can't use '%s' as a %s register", name,
760 what_option[fixed][call_used]);
762 else
764 fixed_regs[i] = fixed;
765 call_used_regs[i] = call_used;
766 #ifdef CALL_REALLY_USED_REGISTERS
767 if (fixed == 0)
768 call_really_used_regs[i] = call_used;
769 #endif
772 else
774 warning ("unknown register name: %s", name);
778 /* Mark register number I as global. */
780 void
781 globalize_reg (i)
782 int i;
784 if (fixed_regs[i] == 0 && no_global_reg_vars)
785 error ("global register variable follows a function definition");
787 if (global_regs[i])
789 warning ("register used for two global register variables");
790 return;
793 if (call_used_regs[i] && ! fixed_regs[i])
794 warning ("call-clobbered register used for global register variable");
796 global_regs[i] = 1;
798 /* If already fixed, nothing else to do. */
799 if (fixed_regs[i])
800 return;
802 fixed_regs[i] = call_used_regs[i] = call_fixed_regs[i] = 1;
803 n_non_fixed_regs--;
805 SET_HARD_REG_BIT (fixed_reg_set, i);
806 SET_HARD_REG_BIT (call_used_reg_set, i);
807 SET_HARD_REG_BIT (call_fixed_reg_set, i);
808 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
811 /* Now the data and code for the `regclass' pass, which happens
812 just before local-alloc. */
814 /* The `costs' struct records the cost of using a hard register of each class
815 and of using memory for each pseudo. We use this data to set up
816 register class preferences. */
818 struct costs
820 int cost[N_REG_CLASSES];
821 int mem_cost;
824 /* Structure used to record preferences of given pseudo. */
825 struct reg_pref
827 /* (enum reg_class) prefclass is the preferred class. */
828 char prefclass;
830 /* altclass is a register class that we should use for allocating
831 pseudo if no register in the preferred class is available.
832 If no register in this class is available, memory is preferred.
834 It might appear to be more general to have a bitmask of classes here,
835 but since it is recommended that there be a class corresponding to the
836 union of most major pair of classes, that generality is not required. */
837 char altclass;
840 /* Record the cost of each class for each pseudo. */
842 static struct costs *costs;
844 /* Initialized once, and used to initialize cost values for each insn. */
846 static struct costs init_cost;
848 /* Record preferences of each pseudo.
849 This is available after `regclass' is run. */
851 static struct reg_pref *reg_pref;
853 /* Allocated buffers for reg_pref. */
855 static struct reg_pref *reg_pref_buffer;
857 /* Frequency of executions of current insn. */
859 static int frequency;
861 static rtx scan_one_insn PARAMS ((rtx, int));
862 static void record_operand_costs PARAMS ((rtx, struct costs *, struct reg_pref *));
863 static void dump_regclass PARAMS ((FILE *));
864 static void record_reg_classes PARAMS ((int, int, rtx *, enum machine_mode *,
865 const char **, rtx,
866 struct costs *, struct reg_pref *));
867 static int copy_cost PARAMS ((rtx, enum machine_mode,
868 enum reg_class, int));
869 static void record_address_regs PARAMS ((rtx, enum reg_class, int));
870 #ifdef FORBIDDEN_INC_DEC_CLASSES
871 static int auto_inc_dec_reg_p PARAMS ((rtx, enum machine_mode));
872 #endif
873 static void reg_scan_mark_refs PARAMS ((rtx, rtx, int, unsigned int));
875 /* Return the reg_class in which pseudo reg number REGNO is best allocated.
876 This function is sometimes called before the info has been computed.
877 When that happens, just return GENERAL_REGS, which is innocuous. */
879 enum reg_class
880 reg_preferred_class (regno)
881 int regno;
883 if (reg_pref == 0)
884 return GENERAL_REGS;
885 return (enum reg_class) reg_pref[regno].prefclass;
888 enum reg_class
889 reg_alternate_class (regno)
890 int regno;
892 if (reg_pref == 0)
893 return ALL_REGS;
895 return (enum reg_class) reg_pref[regno].altclass;
898 /* Initialize some global data for this pass. */
900 void
901 regclass_init ()
903 int i;
905 init_cost.mem_cost = 10000;
906 for (i = 0; i < N_REG_CLASSES; i++)
907 init_cost.cost[i] = 10000;
909 /* This prevents dump_flow_info from losing if called
910 before regclass is run. */
911 reg_pref = NULL;
913 /* No more global register variables may be declared. */
914 no_global_reg_vars = 1;
917 /* Dump register costs. */
918 static void
919 dump_regclass (dump)
920 FILE *dump;
922 static const char *const reg_class_names[] = REG_CLASS_NAMES;
923 int i;
924 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
926 int /* enum reg_class */ class;
927 if (REG_N_REFS (i))
929 fprintf (dump, " Register %i costs:", i);
930 for (class = 0; class < (int) N_REG_CLASSES; class++)
931 if (contains_reg_of_mode [(enum reg_class) class][PSEUDO_REGNO_MODE (i)]
932 #ifdef FORBIDDEN_INC_DEC_CLASSES
933 && (!in_inc_dec[i]
934 || !forbidden_inc_dec_class[(enum reg_class) class])
935 #endif
936 #ifdef CANNOT_CHANGE_MODE_CLASS
937 && ! invalid_mode_change_p (i, (enum reg_class) class,
938 PSEUDO_REGNO_MODE (i))
939 #endif
941 fprintf (dump, " %s:%i", reg_class_names[class],
942 costs[i].cost[(enum reg_class) class]);
943 fprintf (dump, " MEM:%i\n", costs[i].mem_cost);
949 /* Calculate the costs of insn operands. */
951 static void
952 record_operand_costs (insn, op_costs, reg_pref)
953 rtx insn;
954 struct costs *op_costs;
955 struct reg_pref *reg_pref;
957 const char *constraints[MAX_RECOG_OPERANDS];
958 enum machine_mode modes[MAX_RECOG_OPERANDS];
959 int i;
961 for (i = 0; i < recog_data.n_operands; i++)
963 constraints[i] = recog_data.constraints[i];
964 modes[i] = recog_data.operand_mode[i];
967 /* If we get here, we are set up to record the costs of all the
968 operands for this insn. Start by initializing the costs.
969 Then handle any address registers. Finally record the desired
970 classes for any pseudos, doing it twice if some pair of
971 operands are commutative. */
973 for (i = 0; i < recog_data.n_operands; i++)
975 op_costs[i] = init_cost;
977 if (GET_CODE (recog_data.operand[i]) == SUBREG)
978 recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
980 if (GET_CODE (recog_data.operand[i]) == MEM)
981 record_address_regs (XEXP (recog_data.operand[i], 0),
982 MODE_BASE_REG_CLASS (modes[i]), frequency * 2);
983 else if (constraints[i][0] == 'p'
984 || EXTRA_ADDRESS_CONSTRAINT (constraints[i][0], constraints[i]))
985 record_address_regs (recog_data.operand[i],
986 MODE_BASE_REG_CLASS (modes[i]), frequency * 2);
989 /* Check for commutative in a separate loop so everything will
990 have been initialized. We must do this even if one operand
991 is a constant--see addsi3 in m68k.md. */
993 for (i = 0; i < (int) recog_data.n_operands - 1; i++)
994 if (constraints[i][0] == '%')
996 const char *xconstraints[MAX_RECOG_OPERANDS];
997 int j;
999 /* Handle commutative operands by swapping the constraints.
1000 We assume the modes are the same. */
1002 for (j = 0; j < recog_data.n_operands; j++)
1003 xconstraints[j] = constraints[j];
1005 xconstraints[i] = constraints[i+1];
1006 xconstraints[i+1] = constraints[i];
1007 record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
1008 recog_data.operand, modes,
1009 xconstraints, insn, op_costs, reg_pref);
1012 record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
1013 recog_data.operand, modes,
1014 constraints, insn, op_costs, reg_pref);
1017 /* Subroutine of regclass, processes one insn INSN. Scan it and record each
1018 time it would save code to put a certain register in a certain class.
1019 PASS, when nonzero, inhibits some optimizations which need only be done
1020 once.
1021 Return the last insn processed, so that the scan can be continued from
1022 there. */
1024 static rtx
1025 scan_one_insn (insn, pass)
1026 rtx insn;
1027 int pass;
1029 enum rtx_code code = GET_CODE (insn);
1030 enum rtx_code pat_code;
1031 rtx set, note;
1032 int i, j;
1033 struct costs op_costs[MAX_RECOG_OPERANDS];
1035 if (GET_RTX_CLASS (code) != 'i')
1036 return insn;
1038 pat_code = GET_CODE (PATTERN (insn));
1039 if (pat_code == USE
1040 || pat_code == CLOBBER
1041 || pat_code == ASM_INPUT
1042 || pat_code == ADDR_VEC
1043 || pat_code == ADDR_DIFF_VEC)
1044 return insn;
1046 set = single_set (insn);
1047 extract_insn (insn);
1049 /* If this insn loads a parameter from its stack slot, then
1050 it represents a savings, rather than a cost, if the
1051 parameter is stored in memory. Record this fact. */
1053 if (set != 0 && GET_CODE (SET_DEST (set)) == REG
1054 && GET_CODE (SET_SRC (set)) == MEM
1055 && (note = find_reg_note (insn, REG_EQUIV,
1056 NULL_RTX)) != 0
1057 && GET_CODE (XEXP (note, 0)) == MEM)
1059 costs[REGNO (SET_DEST (set))].mem_cost
1060 -= (MEMORY_MOVE_COST (GET_MODE (SET_DEST (set)),
1061 GENERAL_REGS, 1)
1062 * frequency);
1063 record_address_regs (XEXP (SET_SRC (set), 0),
1064 MODE_BASE_REG_CLASS (VOIDmode), frequency * 2);
1065 return insn;
1068 /* Improve handling of two-address insns such as
1069 (set X (ashift CONST Y)) where CONST must be made to
1070 match X. Change it into two insns: (set X CONST)
1071 (set X (ashift X Y)). If we left this for reloading, it
1072 would probably get three insns because X and Y might go
1073 in the same place. This prevents X and Y from receiving
1074 the same hard reg.
1076 We can only do this if the modes of operands 0 and 1
1077 (which might not be the same) are tieable and we only need
1078 do this during our first pass. */
1080 if (pass == 0 && optimize
1081 && recog_data.n_operands >= 3
1082 && recog_data.constraints[1][0] == '0'
1083 && recog_data.constraints[1][1] == 0
1084 && CONSTANT_P (recog_data.operand[1])
1085 && ! rtx_equal_p (recog_data.operand[0], recog_data.operand[1])
1086 && ! rtx_equal_p (recog_data.operand[0], recog_data.operand[2])
1087 && GET_CODE (recog_data.operand[0]) == REG
1088 && MODES_TIEABLE_P (GET_MODE (recog_data.operand[0]),
1089 recog_data.operand_mode[1]))
1091 rtx previnsn = prev_real_insn (insn);
1092 rtx dest
1093 = gen_lowpart (recog_data.operand_mode[1],
1094 recog_data.operand[0]);
1095 rtx newinsn
1096 = emit_insn_before (gen_move_insn (dest, recog_data.operand[1]), insn);
1098 /* If this insn was the start of a basic block,
1099 include the new insn in that block.
1100 We need not check for code_label here;
1101 while a basic block can start with a code_label,
1102 INSN could not be at the beginning of that block. */
1103 if (previnsn == 0 || GET_CODE (previnsn) == JUMP_INSN)
1105 basic_block b;
1106 FOR_EACH_BB (b)
1107 if (insn == b->head)
1108 b->head = newinsn;
1111 /* This makes one more setting of new insns's dest. */
1112 REG_N_SETS (REGNO (recog_data.operand[0]))++;
1113 REG_N_REFS (REGNO (recog_data.operand[0]))++;
1114 REG_FREQ (REGNO (recog_data.operand[0])) += frequency;
1116 *recog_data.operand_loc[1] = recog_data.operand[0];
1117 REG_N_REFS (REGNO (recog_data.operand[0]))++;
1118 REG_FREQ (REGNO (recog_data.operand[0])) += frequency;
1119 for (i = recog_data.n_dups - 1; i >= 0; i--)
1120 if (recog_data.dup_num[i] == 1)
1122 *recog_data.dup_loc[i] = recog_data.operand[0];
1123 REG_N_REFS (REGNO (recog_data.operand[0]))++;
1124 REG_FREQ (REGNO (recog_data.operand[0])) += frequency;
1127 return PREV_INSN (newinsn);
1130 record_operand_costs (insn, op_costs, reg_pref);
1132 /* Now add the cost for each operand to the total costs for
1133 its register. */
1135 for (i = 0; i < recog_data.n_operands; i++)
1136 if (GET_CODE (recog_data.operand[i]) == REG
1137 && REGNO (recog_data.operand[i]) >= FIRST_PSEUDO_REGISTER)
1139 int regno = REGNO (recog_data.operand[i]);
1140 struct costs *p = &costs[regno], *q = &op_costs[i];
1142 p->mem_cost += q->mem_cost * frequency;
1143 for (j = 0; j < N_REG_CLASSES; j++)
1144 p->cost[j] += q->cost[j] * frequency;
1147 return insn;
1150 /* Initialize information about which register classes can be used for
1151 pseudos that are auto-incremented or auto-decremented. */
1153 static void
1154 init_reg_autoinc ()
1156 #ifdef FORBIDDEN_INC_DEC_CLASSES
1157 int i;
1159 for (i = 0; i < N_REG_CLASSES; i++)
1161 rtx r = gen_rtx_raw_REG (VOIDmode, 0);
1162 enum machine_mode m;
1163 int j;
1165 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1166 if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
1168 REGNO (r) = j;
1170 for (m = VOIDmode; (int) m < (int) MAX_MACHINE_MODE;
1171 m = (enum machine_mode) ((int) m + 1))
1172 if (HARD_REGNO_MODE_OK (j, m))
1174 PUT_MODE (r, m);
1176 /* If a register is not directly suitable for an
1177 auto-increment or decrement addressing mode and
1178 requires secondary reloads, disallow its class from
1179 being used in such addresses. */
1181 if ((0
1182 #ifdef SECONDARY_RELOAD_CLASS
1183 || (SECONDARY_RELOAD_CLASS (MODE_BASE_REG_CLASS (VOIDmode), m, r)
1184 != NO_REGS)
1185 #else
1186 #ifdef SECONDARY_INPUT_RELOAD_CLASS
1187 || (SECONDARY_INPUT_RELOAD_CLASS (MODE_BASE_REG_CLASS (VOIDmode), m, r)
1188 != NO_REGS)
1189 #endif
1190 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
1191 || (SECONDARY_OUTPUT_RELOAD_CLASS (MODE_BASE_REG_CLASS (VOIDmode), m, r)
1192 != NO_REGS)
1193 #endif
1194 #endif
1196 && ! auto_inc_dec_reg_p (r, m))
1197 forbidden_inc_dec_class[i] = 1;
1201 #endif /* FORBIDDEN_INC_DEC_CLASSES */
1204 /* This is a pass of the compiler that scans all instructions
1205 and calculates the preferred class for each pseudo-register.
1206 This information can be accessed later by calling `reg_preferred_class'.
1207 This pass comes just before local register allocation. */
1209 void
1210 regclass (f, nregs, dump)
1211 rtx f;
1212 int nregs;
1213 FILE *dump;
1215 rtx insn;
1216 int i;
1217 int pass;
1219 init_recog ();
1221 costs = (struct costs *) xmalloc (nregs * sizeof (struct costs));
1223 #ifdef FORBIDDEN_INC_DEC_CLASSES
1225 in_inc_dec = (char *) xmalloc (nregs);
1227 #endif /* FORBIDDEN_INC_DEC_CLASSES */
1229 /* Normally we scan the insns once and determine the best class to use for
1230 each register. However, if -fexpensive_optimizations are on, we do so
1231 twice, the second time using the tentative best classes to guide the
1232 selection. */
1234 for (pass = 0; pass <= flag_expensive_optimizations; pass++)
1236 basic_block bb;
1238 if (dump)
1239 fprintf (dump, "\n\nPass %i\n\n",pass);
1240 /* Zero out our accumulation of the cost of each class for each reg. */
1242 memset ((char *) costs, 0, nregs * sizeof (struct costs));
1244 #ifdef FORBIDDEN_INC_DEC_CLASSES
1245 memset (in_inc_dec, 0, nregs);
1246 #endif
1248 /* Scan the instructions and record each time it would
1249 save code to put a certain register in a certain class. */
1251 if (!optimize)
1253 frequency = REG_FREQ_MAX;
1254 for (insn = f; insn; insn = NEXT_INSN (insn))
1255 insn = scan_one_insn (insn, pass);
1257 else
1258 FOR_EACH_BB (bb)
1260 /* Show that an insn inside a loop is likely to be executed three
1261 times more than insns outside a loop. This is much more
1262 aggressive than the assumptions made elsewhere and is being
1263 tried as an experiment. */
1264 frequency = REG_FREQ_FROM_BB (bb);
1265 for (insn = bb->head; ; insn = NEXT_INSN (insn))
1267 insn = scan_one_insn (insn, pass);
1268 if (insn == bb->end)
1269 break;
1273 /* Now for each register look at how desirable each class is
1274 and find which class is preferred. Store that in
1275 `prefclass'. Record in `altclass' the largest register
1276 class any of whose registers is better than memory. */
1278 if (pass == 0)
1279 reg_pref = reg_pref_buffer;
1281 if (dump)
1283 dump_regclass (dump);
1284 fprintf (dump,"\n");
1286 for (i = FIRST_PSEUDO_REGISTER; i < nregs; i++)
1288 int best_cost = (1 << (HOST_BITS_PER_INT - 2)) - 1;
1289 enum reg_class best = ALL_REGS, alt = NO_REGS;
1290 /* This is an enum reg_class, but we call it an int
1291 to save lots of casts. */
1292 int class;
1293 struct costs *p = &costs[i];
1295 /* In non-optimizing compilation REG_N_REFS is not initialized
1296 yet. */
1297 if (optimize && !REG_N_REFS (i) && !REG_N_SETS (i))
1298 continue;
1300 for (class = (int) ALL_REGS - 1; class > 0; class--)
1302 /* Ignore classes that are too small for this operand or
1303 invalid for an operand that was auto-incremented. */
1304 if (!contains_reg_of_mode [class][PSEUDO_REGNO_MODE (i)]
1305 #ifdef FORBIDDEN_INC_DEC_CLASSES
1306 || (in_inc_dec[i] && forbidden_inc_dec_class[class])
1307 #endif
1308 #ifdef CANNOT_CHANGE_MODE_CLASS
1309 || invalid_mode_change_p (i, (enum reg_class) class,
1310 PSEUDO_REGNO_MODE (i))
1311 #endif
1314 else if (p->cost[class] < best_cost)
1316 best_cost = p->cost[class];
1317 best = (enum reg_class) class;
1319 else if (p->cost[class] == best_cost)
1320 best = reg_class_subunion[(int) best][class];
1323 /* Record the alternate register class; i.e., a class for which
1324 every register in it is better than using memory. If adding a
1325 class would make a smaller class (i.e., no union of just those
1326 classes exists), skip that class. The major unions of classes
1327 should be provided as a register class. Don't do this if we
1328 will be doing it again later. */
1330 if ((pass == 1 || dump) || ! flag_expensive_optimizations)
1331 for (class = 0; class < N_REG_CLASSES; class++)
1332 if (p->cost[class] < p->mem_cost
1333 && (reg_class_size[(int) reg_class_subunion[(int) alt][class]]
1334 > reg_class_size[(int) alt])
1335 #ifdef FORBIDDEN_INC_DEC_CLASSES
1336 && ! (in_inc_dec[i] && forbidden_inc_dec_class[class])
1337 #endif
1338 #ifdef CANNOT_CHANGE_MODE_CLASS
1339 && ! invalid_mode_change_p (i, (enum reg_class) class,
1340 PSEUDO_REGNO_MODE (i))
1341 #endif
1343 alt = reg_class_subunion[(int) alt][class];
1345 /* If we don't add any classes, nothing to try. */
1346 if (alt == best)
1347 alt = NO_REGS;
1349 if (dump
1350 && (reg_pref[i].prefclass != (int) best
1351 || reg_pref[i].altclass != (int) alt))
1353 static const char *const reg_class_names[] = REG_CLASS_NAMES;
1354 fprintf (dump, " Register %i", i);
1355 if (alt == ALL_REGS || best == ALL_REGS)
1356 fprintf (dump, " pref %s\n", reg_class_names[(int) best]);
1357 else if (alt == NO_REGS)
1358 fprintf (dump, " pref %s or none\n", reg_class_names[(int) best]);
1359 else
1360 fprintf (dump, " pref %s, else %s\n",
1361 reg_class_names[(int) best],
1362 reg_class_names[(int) alt]);
1365 /* We cast to (int) because (char) hits bugs in some compilers. */
1366 reg_pref[i].prefclass = (int) best;
1367 reg_pref[i].altclass = (int) alt;
1371 #ifdef FORBIDDEN_INC_DEC_CLASSES
1372 free (in_inc_dec);
1373 #endif
1374 free (costs);
1377 /* Record the cost of using memory or registers of various classes for
1378 the operands in INSN.
1380 N_ALTS is the number of alternatives.
1382 N_OPS is the number of operands.
1384 OPS is an array of the operands.
1386 MODES are the modes of the operands, in case any are VOIDmode.
1388 CONSTRAINTS are the constraints to use for the operands. This array
1389 is modified by this procedure.
1391 This procedure works alternative by alternative. For each alternative
1392 we assume that we will be able to allocate all pseudos to their ideal
1393 register class and calculate the cost of using that alternative. Then
1394 we compute for each operand that is a pseudo-register, the cost of
1395 having the pseudo allocated to each register class and using it in that
1396 alternative. To this cost is added the cost of the alternative.
1398 The cost of each class for this insn is its lowest cost among all the
1399 alternatives. */
1401 static void
1402 record_reg_classes (n_alts, n_ops, ops, modes,
1403 constraints, insn, op_costs, reg_pref)
1404 int n_alts;
1405 int n_ops;
1406 rtx *ops;
1407 enum machine_mode *modes;
1408 const char **constraints;
1409 rtx insn;
1410 struct costs *op_costs;
1411 struct reg_pref *reg_pref;
1413 int alt;
1414 int i, j;
1415 rtx set;
1417 /* Process each alternative, each time minimizing an operand's cost with
1418 the cost for each operand in that alternative. */
1420 for (alt = 0; alt < n_alts; alt++)
1422 struct costs this_op_costs[MAX_RECOG_OPERANDS];
1423 int alt_fail = 0;
1424 int alt_cost = 0;
1425 enum reg_class classes[MAX_RECOG_OPERANDS];
1426 int allows_mem[MAX_RECOG_OPERANDS];
1427 int class;
1429 for (i = 0; i < n_ops; i++)
1431 const char *p = constraints[i];
1432 rtx op = ops[i];
1433 enum machine_mode mode = modes[i];
1434 int allows_addr = 0;
1435 int win = 0;
1436 unsigned char c;
1438 /* Initially show we know nothing about the register class. */
1439 classes[i] = NO_REGS;
1440 allows_mem[i] = 0;
1442 /* If this operand has no constraints at all, we can conclude
1443 nothing about it since anything is valid. */
1445 if (*p == 0)
1447 if (GET_CODE (op) == REG && REGNO (op) >= FIRST_PSEUDO_REGISTER)
1448 memset ((char *) &this_op_costs[i], 0, sizeof this_op_costs[i]);
1450 continue;
1453 /* If this alternative is only relevant when this operand
1454 matches a previous operand, we do different things depending
1455 on whether this operand is a pseudo-reg or not. We must process
1456 any modifiers for the operand before we can make this test. */
1458 while (*p == '%' || *p == '=' || *p == '+' || *p == '&')
1459 p++;
1461 if (p[0] >= '0' && p[0] <= '0' + i && (p[1] == ',' || p[1] == 0))
1463 /* Copy class and whether memory is allowed from the matching
1464 alternative. Then perform any needed cost computations
1465 and/or adjustments. */
1466 j = p[0] - '0';
1467 classes[i] = classes[j];
1468 allows_mem[i] = allows_mem[j];
1470 if (GET_CODE (op) != REG || REGNO (op) < FIRST_PSEUDO_REGISTER)
1472 /* If this matches the other operand, we have no added
1473 cost and we win. */
1474 if (rtx_equal_p (ops[j], op))
1475 win = 1;
1477 /* If we can put the other operand into a register, add to
1478 the cost of this alternative the cost to copy this
1479 operand to the register used for the other operand. */
1481 else if (classes[j] != NO_REGS)
1482 alt_cost += copy_cost (op, mode, classes[j], 1), win = 1;
1484 else if (GET_CODE (ops[j]) != REG
1485 || REGNO (ops[j]) < FIRST_PSEUDO_REGISTER)
1487 /* This op is a pseudo but the one it matches is not. */
1489 /* If we can't put the other operand into a register, this
1490 alternative can't be used. */
1492 if (classes[j] == NO_REGS)
1493 alt_fail = 1;
1495 /* Otherwise, add to the cost of this alternative the cost
1496 to copy the other operand to the register used for this
1497 operand. */
1499 else
1500 alt_cost += copy_cost (ops[j], mode, classes[j], 1);
1502 else
1504 /* The costs of this operand are not the same as the other
1505 operand since move costs are not symmetric. Moreover,
1506 if we cannot tie them, this alternative needs to do a
1507 copy, which is one instruction. */
1509 struct costs *pp = &this_op_costs[i];
1511 for (class = 0; class < N_REG_CLASSES; class++)
1512 pp->cost[class]
1513 = ((recog_data.operand_type[i] != OP_OUT
1514 ? may_move_in_cost[mode][class][(int) classes[i]]
1515 : 0)
1516 + (recog_data.operand_type[i] != OP_IN
1517 ? may_move_out_cost[mode][(int) classes[i]][class]
1518 : 0));
1520 /* If the alternative actually allows memory, make things
1521 a bit cheaper since we won't need an extra insn to
1522 load it. */
1524 pp->mem_cost
1525 = ((recog_data.operand_type[i] != OP_IN
1526 ? MEMORY_MOVE_COST (mode, classes[i], 0)
1527 : 0)
1528 + (recog_data.operand_type[i] != OP_OUT
1529 ? MEMORY_MOVE_COST (mode, classes[i], 1)
1530 : 0) - allows_mem[i]);
1532 /* If we have assigned a class to this register in our
1533 first pass, add a cost to this alternative corresponding
1534 to what we would add if this register were not in the
1535 appropriate class. */
1537 if (reg_pref)
1538 alt_cost
1539 += (may_move_in_cost[mode]
1540 [(unsigned char) reg_pref[REGNO (op)].prefclass]
1541 [(int) classes[i]]);
1543 if (REGNO (ops[i]) != REGNO (ops[j])
1544 && ! find_reg_note (insn, REG_DEAD, op))
1545 alt_cost += 2;
1547 /* This is in place of ordinary cost computation
1548 for this operand, so skip to the end of the
1549 alternative (should be just one character). */
1550 while (*p && *p++ != ',')
1553 constraints[i] = p;
1554 continue;
1558 /* Scan all the constraint letters. See if the operand matches
1559 any of the constraints. Collect the valid register classes
1560 and see if this operand accepts memory. */
1562 while ((c = *p))
1564 switch (c)
1566 case ',':
1567 break;
1568 case '*':
1569 /* Ignore the next letter for this pass. */
1570 c = *++p;
1571 break;
1573 case '?':
1574 alt_cost += 2;
1575 case '!': case '#': case '&':
1576 case '0': case '1': case '2': case '3': case '4':
1577 case '5': case '6': case '7': case '8': case '9':
1578 break;
1580 case 'p':
1581 allows_addr = 1;
1582 win = address_operand (op, GET_MODE (op));
1583 /* We know this operand is an address, so we want it to be
1584 allocated to a register that can be the base of an
1585 address, ie BASE_REG_CLASS. */
1586 classes[i]
1587 = reg_class_subunion[(int) classes[i]]
1588 [(int) MODE_BASE_REG_CLASS (VOIDmode)];
1589 break;
1591 case 'm': case 'o': case 'V':
1592 /* It doesn't seem worth distinguishing between offsettable
1593 and non-offsettable addresses here. */
1594 allows_mem[i] = 1;
1595 if (GET_CODE (op) == MEM)
1596 win = 1;
1597 break;
1599 case '<':
1600 if (GET_CODE (op) == MEM
1601 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
1602 || GET_CODE (XEXP (op, 0)) == POST_DEC))
1603 win = 1;
1604 break;
1606 case '>':
1607 if (GET_CODE (op) == MEM
1608 && (GET_CODE (XEXP (op, 0)) == PRE_INC
1609 || GET_CODE (XEXP (op, 0)) == POST_INC))
1610 win = 1;
1611 break;
1613 case 'E':
1614 case 'F':
1615 if (GET_CODE (op) == CONST_DOUBLE
1616 || (GET_CODE (op) == CONST_VECTOR
1617 && (GET_MODE_CLASS (GET_MODE (op))
1618 == MODE_VECTOR_FLOAT)))
1619 win = 1;
1620 break;
1622 case 'G':
1623 case 'H':
1624 if (GET_CODE (op) == CONST_DOUBLE
1625 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
1626 win = 1;
1627 break;
1629 case 's':
1630 if (GET_CODE (op) == CONST_INT
1631 || (GET_CODE (op) == CONST_DOUBLE
1632 && GET_MODE (op) == VOIDmode))
1633 break;
1634 case 'i':
1635 if (CONSTANT_P (op)
1636 #ifdef LEGITIMATE_PIC_OPERAND_P
1637 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))
1638 #endif
1640 win = 1;
1641 break;
1643 case 'n':
1644 if (GET_CODE (op) == CONST_INT
1645 || (GET_CODE (op) == CONST_DOUBLE
1646 && GET_MODE (op) == VOIDmode))
1647 win = 1;
1648 break;
1650 case 'I':
1651 case 'J':
1652 case 'K':
1653 case 'L':
1654 case 'M':
1655 case 'N':
1656 case 'O':
1657 case 'P':
1658 if (GET_CODE (op) == CONST_INT
1659 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
1660 win = 1;
1661 break;
1663 case 'X':
1664 win = 1;
1665 break;
1667 case 'g':
1668 if (GET_CODE (op) == MEM
1669 || (CONSTANT_P (op)
1670 #ifdef LEGITIMATE_PIC_OPERAND_P
1671 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))
1672 #endif
1674 win = 1;
1675 allows_mem[i] = 1;
1676 case 'r':
1677 classes[i]
1678 = reg_class_subunion[(int) classes[i]][(int) GENERAL_REGS];
1679 break;
1681 default:
1682 if (REG_CLASS_FROM_CONSTRAINT (c, p) != NO_REGS)
1683 classes[i]
1684 = reg_class_subunion[(int) classes[i]]
1685 [(int) REG_CLASS_FROM_CONSTRAINT (c, p)];
1686 #ifdef EXTRA_CONSTRAINT_STR
1687 else if (EXTRA_CONSTRAINT_STR (op, c, p))
1688 win = 1;
1690 if (EXTRA_MEMORY_CONSTRAINT (c, p))
1692 /* Every MEM can be reloaded to fit. */
1693 allows_mem[i] = 1;
1694 if (GET_CODE (op) == MEM)
1695 win = 1;
1697 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1699 /* Every address can be reloaded to fit. */
1700 allows_addr = 1;
1701 if (address_operand (op, GET_MODE (op)))
1702 win = 1;
1703 /* We know this operand is an address, so we want it to
1704 be allocated to a register that can be the base of an
1705 address, ie BASE_REG_CLASS. */
1706 classes[i]
1707 = reg_class_subunion[(int) classes[i]]
1708 [(int) MODE_BASE_REG_CLASS (VOIDmode)];
1710 #endif
1711 break;
1713 p += CONSTRAINT_LEN (c, p);
1714 if (c == ',')
1715 break;
1718 constraints[i] = p;
1720 /* How we account for this operand now depends on whether it is a
1721 pseudo register or not. If it is, we first check if any
1722 register classes are valid. If not, we ignore this alternative,
1723 since we want to assume that all pseudos get allocated for
1724 register preferencing. If some register class is valid, compute
1725 the costs of moving the pseudo into that class. */
1727 if (GET_CODE (op) == REG && REGNO (op) >= FIRST_PSEUDO_REGISTER)
1729 if (classes[i] == NO_REGS)
1731 /* We must always fail if the operand is a REG, but
1732 we did not find a suitable class.
1734 Otherwise we may perform an uninitialized read
1735 from this_op_costs after the `continue' statement
1736 below. */
1737 alt_fail = 1;
1739 else
1741 struct costs *pp = &this_op_costs[i];
1743 for (class = 0; class < N_REG_CLASSES; class++)
1744 pp->cost[class]
1745 = ((recog_data.operand_type[i] != OP_OUT
1746 ? may_move_in_cost[mode][class][(int) classes[i]]
1747 : 0)
1748 + (recog_data.operand_type[i] != OP_IN
1749 ? may_move_out_cost[mode][(int) classes[i]][class]
1750 : 0));
1752 /* If the alternative actually allows memory, make things
1753 a bit cheaper since we won't need an extra insn to
1754 load it. */
1756 pp->mem_cost
1757 = ((recog_data.operand_type[i] != OP_IN
1758 ? MEMORY_MOVE_COST (mode, classes[i], 0)
1759 : 0)
1760 + (recog_data.operand_type[i] != OP_OUT
1761 ? MEMORY_MOVE_COST (mode, classes[i], 1)
1762 : 0) - allows_mem[i]);
1764 /* If we have assigned a class to this register in our
1765 first pass, add a cost to this alternative corresponding
1766 to what we would add if this register were not in the
1767 appropriate class. */
1769 if (reg_pref)
1770 alt_cost
1771 += (may_move_in_cost[mode]
1772 [(unsigned char) reg_pref[REGNO (op)].prefclass]
1773 [(int) classes[i]]);
1777 /* Otherwise, if this alternative wins, either because we
1778 have already determined that or if we have a hard register of
1779 the proper class, there is no cost for this alternative. */
1781 else if (win
1782 || (GET_CODE (op) == REG
1783 && reg_fits_class_p (op, classes[i], 0, GET_MODE (op))))
1786 /* If registers are valid, the cost of this alternative includes
1787 copying the object to and/or from a register. */
1789 else if (classes[i] != NO_REGS)
1791 if (recog_data.operand_type[i] != OP_OUT)
1792 alt_cost += copy_cost (op, mode, classes[i], 1);
1794 if (recog_data.operand_type[i] != OP_IN)
1795 alt_cost += copy_cost (op, mode, classes[i], 0);
1798 /* The only other way this alternative can be used is if this is a
1799 constant that could be placed into memory. */
1801 else if (CONSTANT_P (op) && (allows_addr || allows_mem[i]))
1802 alt_cost += MEMORY_MOVE_COST (mode, classes[i], 1);
1803 else
1804 alt_fail = 1;
1807 if (alt_fail)
1808 continue;
1810 /* Finally, update the costs with the information we've calculated
1811 about this alternative. */
1813 for (i = 0; i < n_ops; i++)
1814 if (GET_CODE (ops[i]) == REG
1815 && REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
1817 struct costs *pp = &op_costs[i], *qq = &this_op_costs[i];
1818 int scale = 1 + (recog_data.operand_type[i] == OP_INOUT);
1820 pp->mem_cost = MIN (pp->mem_cost,
1821 (qq->mem_cost + alt_cost) * scale);
1823 for (class = 0; class < N_REG_CLASSES; class++)
1824 pp->cost[class] = MIN (pp->cost[class],
1825 (qq->cost[class] + alt_cost) * scale);
1829 /* If this insn is a single set copying operand 1 to operand 0
1830 and one operand is a pseudo with the other a hard reg or a pseudo
1831 that prefers a register that is in its own register class then
1832 we may want to adjust the cost of that register class to -1.
1834 Avoid the adjustment if the source does not die to avoid stressing of
1835 register allocator by preferrencing two coliding registers into single
1836 class.
1838 Also avoid the adjustment if a copy between registers of the class
1839 is expensive (ten times the cost of a default copy is considered
1840 arbitrarily expensive). This avoids losing when the preferred class
1841 is very expensive as the source of a copy instruction. */
1843 if ((set = single_set (insn)) != 0
1844 && ops[0] == SET_DEST (set) && ops[1] == SET_SRC (set)
1845 && GET_CODE (ops[0]) == REG && GET_CODE (ops[1]) == REG
1846 && find_regno_note (insn, REG_DEAD, REGNO (ops[1])))
1847 for (i = 0; i <= 1; i++)
1848 if (REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
1850 unsigned int regno = REGNO (ops[!i]);
1851 enum machine_mode mode = GET_MODE (ops[!i]);
1852 int class;
1853 unsigned int nr;
1855 if (regno >= FIRST_PSEUDO_REGISTER && reg_pref != 0)
1857 enum reg_class pref = reg_pref[regno].prefclass;
1859 if ((reg_class_size[(unsigned char) pref]
1860 == (unsigned) CLASS_MAX_NREGS (pref, mode))
1861 && REGISTER_MOVE_COST (mode, pref, pref) < 10 * 2)
1862 op_costs[i].cost[(unsigned char) pref] = -1;
1864 else if (regno < FIRST_PSEUDO_REGISTER)
1865 for (class = 0; class < N_REG_CLASSES; class++)
1866 if (TEST_HARD_REG_BIT (reg_class_contents[class], regno)
1867 && reg_class_size[class] == (unsigned) CLASS_MAX_NREGS (class, mode))
1869 if (reg_class_size[class] == 1)
1870 op_costs[i].cost[class] = -1;
1871 else
1873 for (nr = 0; nr < (unsigned) HARD_REGNO_NREGS (regno, mode); nr++)
1875 if (! TEST_HARD_REG_BIT (reg_class_contents[class],
1876 regno + nr))
1877 break;
1880 if (nr == (unsigned) HARD_REGNO_NREGS (regno,mode))
1881 op_costs[i].cost[class] = -1;
1887 /* Compute the cost of loading X into (if TO_P is nonzero) or from (if
1888 TO_P is zero) a register of class CLASS in mode MODE.
1890 X must not be a pseudo. */
1892 static int
1893 copy_cost (x, mode, class, to_p)
1894 rtx x;
1895 enum machine_mode mode ATTRIBUTE_UNUSED;
1896 enum reg_class class;
1897 int to_p ATTRIBUTE_UNUSED;
1899 #ifdef HAVE_SECONDARY_RELOADS
1900 enum reg_class secondary_class = NO_REGS;
1901 #endif
1903 /* If X is a SCRATCH, there is actually nothing to move since we are
1904 assuming optimal allocation. */
1906 if (GET_CODE (x) == SCRATCH)
1907 return 0;
1909 /* Get the class we will actually use for a reload. */
1910 class = PREFERRED_RELOAD_CLASS (x, class);
1912 #ifdef HAVE_SECONDARY_RELOADS
1913 /* If we need a secondary reload (we assume here that we are using
1914 the secondary reload as an intermediate, not a scratch register), the
1915 cost is that to load the input into the intermediate register, then
1916 to copy them. We use a special value of TO_P to avoid recursion. */
1918 #ifdef SECONDARY_INPUT_RELOAD_CLASS
1919 if (to_p == 1)
1920 secondary_class = SECONDARY_INPUT_RELOAD_CLASS (class, mode, x);
1921 #endif
1923 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
1924 if (! to_p)
1925 secondary_class = SECONDARY_OUTPUT_RELOAD_CLASS (class, mode, x);
1926 #endif
1928 if (secondary_class != NO_REGS)
1929 return (move_cost[mode][(int) secondary_class][(int) class]
1930 + copy_cost (x, mode, secondary_class, 2));
1931 #endif /* HAVE_SECONDARY_RELOADS */
1933 /* For memory, use the memory move cost, for (hard) registers, use the
1934 cost to move between the register classes, and use 2 for everything
1935 else (constants). */
1937 if (GET_CODE (x) == MEM || class == NO_REGS)
1938 return MEMORY_MOVE_COST (mode, class, to_p);
1940 else if (GET_CODE (x) == REG)
1941 return move_cost[mode][(int) REGNO_REG_CLASS (REGNO (x))][(int) class];
1943 else
1944 /* If this is a constant, we may eventually want to call rtx_cost here. */
1945 return COSTS_N_INSNS (1);
1948 /* Record the pseudo registers we must reload into hard registers
1949 in a subexpression of a memory address, X.
1951 CLASS is the class that the register needs to be in and is either
1952 BASE_REG_CLASS or INDEX_REG_CLASS.
1954 SCALE is twice the amount to multiply the cost by (it is twice so we
1955 can represent half-cost adjustments). */
1957 static void
1958 record_address_regs (x, class, scale)
1959 rtx x;
1960 enum reg_class class;
1961 int scale;
1963 enum rtx_code code = GET_CODE (x);
1965 switch (code)
1967 case CONST_INT:
1968 case CONST:
1969 case CC0:
1970 case PC:
1971 case SYMBOL_REF:
1972 case LABEL_REF:
1973 return;
1975 case PLUS:
1976 /* When we have an address that is a sum,
1977 we must determine whether registers are "base" or "index" regs.
1978 If there is a sum of two registers, we must choose one to be
1979 the "base". Luckily, we can use the REG_POINTER to make a good
1980 choice most of the time. We only need to do this on machines
1981 that can have two registers in an address and where the base
1982 and index register classes are different.
1984 ??? This code used to set REGNO_POINTER_FLAG in some cases, but
1985 that seems bogus since it should only be set when we are sure
1986 the register is being used as a pointer. */
1989 rtx arg0 = XEXP (x, 0);
1990 rtx arg1 = XEXP (x, 1);
1991 enum rtx_code code0 = GET_CODE (arg0);
1992 enum rtx_code code1 = GET_CODE (arg1);
1994 /* Look inside subregs. */
1995 if (code0 == SUBREG)
1996 arg0 = SUBREG_REG (arg0), code0 = GET_CODE (arg0);
1997 if (code1 == SUBREG)
1998 arg1 = SUBREG_REG (arg1), code1 = GET_CODE (arg1);
2000 /* If this machine only allows one register per address, it must
2001 be in the first operand. */
2003 if (MAX_REGS_PER_ADDRESS == 1)
2004 record_address_regs (arg0, class, scale);
2006 /* If index and base registers are the same on this machine, just
2007 record registers in any non-constant operands. We assume here,
2008 as well as in the tests below, that all addresses are in
2009 canonical form. */
2011 else if (INDEX_REG_CLASS == MODE_BASE_REG_CLASS (VOIDmode))
2013 record_address_regs (arg0, class, scale);
2014 if (! CONSTANT_P (arg1))
2015 record_address_regs (arg1, class, scale);
2018 /* If the second operand is a constant integer, it doesn't change
2019 what class the first operand must be. */
2021 else if (code1 == CONST_INT || code1 == CONST_DOUBLE)
2022 record_address_regs (arg0, class, scale);
2024 /* If the second operand is a symbolic constant, the first operand
2025 must be an index register. */
2027 else if (code1 == SYMBOL_REF || code1 == CONST || code1 == LABEL_REF)
2028 record_address_regs (arg0, INDEX_REG_CLASS, scale);
2030 /* If both operands are registers but one is already a hard register
2031 of index or base class, give the other the class that the hard
2032 register is not. */
2034 #ifdef REG_OK_FOR_BASE_P
2035 else if (code0 == REG && code1 == REG
2036 && REGNO (arg0) < FIRST_PSEUDO_REGISTER
2037 && (REG_OK_FOR_BASE_P (arg0) || REG_OK_FOR_INDEX_P (arg0)))
2038 record_address_regs (arg1,
2039 REG_OK_FOR_BASE_P (arg0)
2040 ? INDEX_REG_CLASS : MODE_BASE_REG_CLASS (VOIDmode),
2041 scale);
2042 else if (code0 == REG && code1 == REG
2043 && REGNO (arg1) < FIRST_PSEUDO_REGISTER
2044 && (REG_OK_FOR_BASE_P (arg1) || REG_OK_FOR_INDEX_P (arg1)))
2045 record_address_regs (arg0,
2046 REG_OK_FOR_BASE_P (arg1)
2047 ? INDEX_REG_CLASS : MODE_BASE_REG_CLASS (VOIDmode),
2048 scale);
2049 #endif
2051 /* If one operand is known to be a pointer, it must be the base
2052 with the other operand the index. Likewise if the other operand
2053 is a MULT. */
2055 else if ((code0 == REG && REG_POINTER (arg0))
2056 || code1 == MULT)
2058 record_address_regs (arg0, MODE_BASE_REG_CLASS (VOIDmode), scale);
2059 record_address_regs (arg1, INDEX_REG_CLASS, scale);
2061 else if ((code1 == REG && REG_POINTER (arg1))
2062 || code0 == MULT)
2064 record_address_regs (arg0, INDEX_REG_CLASS, scale);
2065 record_address_regs (arg1, MODE_BASE_REG_CLASS (VOIDmode), scale);
2068 /* Otherwise, count equal chances that each might be a base
2069 or index register. This case should be rare. */
2071 else
2073 record_address_regs (arg0, MODE_BASE_REG_CLASS (VOIDmode),
2074 scale / 2);
2075 record_address_regs (arg0, INDEX_REG_CLASS, scale / 2);
2076 record_address_regs (arg1, MODE_BASE_REG_CLASS (VOIDmode),
2077 scale / 2);
2078 record_address_regs (arg1, INDEX_REG_CLASS, scale / 2);
2081 break;
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. */
2086 case POST_MODIFY:
2087 case PRE_MODIFY:
2088 record_address_regs (XEXP (x, 0), MODE_BASE_REG_CLASS (VOIDmode),
2089 2 * scale);
2090 if (REG_P (XEXP (XEXP (x, 1), 1)))
2091 record_address_regs (XEXP (XEXP (x, 1), 1),
2092 INDEX_REG_CLASS, 2 * scale);
2093 break;
2095 case POST_INC:
2096 case PRE_INC:
2097 case POST_DEC:
2098 case PRE_DEC:
2099 /* Double the importance of a pseudo register that is incremented
2100 or decremented, since it would take two extra insns
2101 if it ends up in the wrong place. If the operand is a pseudo,
2102 show it is being used in an INC_DEC context. */
2104 #ifdef FORBIDDEN_INC_DEC_CLASSES
2105 if (GET_CODE (XEXP (x, 0)) == REG
2106 && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER)
2107 in_inc_dec[REGNO (XEXP (x, 0))] = 1;
2108 #endif
2110 record_address_regs (XEXP (x, 0), class, 2 * scale);
2111 break;
2113 case REG:
2115 struct costs *pp = &costs[REGNO (x)];
2116 int i;
2118 pp->mem_cost += (MEMORY_MOVE_COST (Pmode, class, 1) * scale) / 2;
2120 for (i = 0; i < N_REG_CLASSES; i++)
2121 pp->cost[i] += (may_move_in_cost[Pmode][i][(int) class] * scale) / 2;
2123 break;
2125 default:
2127 const char *fmt = GET_RTX_FORMAT (code);
2128 int i;
2129 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2130 if (fmt[i] == 'e')
2131 record_address_regs (XEXP (x, i), class, scale);
2136 #ifdef FORBIDDEN_INC_DEC_CLASSES
2138 /* Return 1 if REG is valid as an auto-increment memory reference
2139 to an object of MODE. */
2141 static int
2142 auto_inc_dec_reg_p (reg, mode)
2143 rtx reg;
2144 enum machine_mode mode;
2146 if (HAVE_POST_INCREMENT
2147 && memory_address_p (mode, gen_rtx_POST_INC (Pmode, reg)))
2148 return 1;
2150 if (HAVE_POST_DECREMENT
2151 && memory_address_p (mode, gen_rtx_POST_DEC (Pmode, reg)))
2152 return 1;
2154 if (HAVE_PRE_INCREMENT
2155 && memory_address_p (mode, gen_rtx_PRE_INC (Pmode, reg)))
2156 return 1;
2158 if (HAVE_PRE_DECREMENT
2159 && memory_address_p (mode, gen_rtx_PRE_DEC (Pmode, reg)))
2160 return 1;
2162 return 0;
2164 #endif
2166 static short *renumber;
2167 static size_t regno_allocated;
2168 static unsigned int reg_n_max;
2170 /* Allocate enough space to hold NUM_REGS registers for the tables used for
2171 reg_scan and flow_analysis that are indexed by the register number. If
2172 NEW_P is nonzero, initialize all of the registers, otherwise only
2173 initialize the new registers allocated. The same table is kept from
2174 function to function, only reallocating it when we need more room. If
2175 RENUMBER_P is nonzero, allocate the reg_renumber array also. */
2177 void
2178 allocate_reg_info (num_regs, new_p, renumber_p)
2179 size_t num_regs;
2180 int new_p;
2181 int renumber_p;
2183 size_t size_info;
2184 size_t size_renumber;
2185 size_t min = (new_p) ? 0 : reg_n_max;
2186 struct reg_info_data *reg_data;
2188 if (num_regs > regno_allocated)
2190 size_t old_allocated = regno_allocated;
2192 regno_allocated = num_regs + (num_regs / 20); /* add some slop space */
2193 size_renumber = regno_allocated * sizeof (short);
2195 if (!reg_n_info)
2197 VARRAY_REG_INIT (reg_n_info, regno_allocated, "reg_n_info");
2198 renumber = (short *) xmalloc (size_renumber);
2199 reg_pref_buffer = (struct reg_pref *) xmalloc (regno_allocated
2200 * sizeof (struct reg_pref));
2203 else
2205 VARRAY_GROW (reg_n_info, regno_allocated);
2207 if (new_p) /* if we're zapping everything, no need to realloc */
2209 free ((char *) renumber);
2210 free ((char *) reg_pref);
2211 renumber = (short *) xmalloc (size_renumber);
2212 reg_pref_buffer = (struct reg_pref *) xmalloc (regno_allocated
2213 * sizeof (struct reg_pref));
2216 else
2218 renumber = (short *) xrealloc ((char *) renumber, size_renumber);
2219 reg_pref_buffer = (struct reg_pref *) xrealloc ((char *) reg_pref_buffer,
2220 regno_allocated
2221 * sizeof (struct reg_pref));
2225 size_info = (regno_allocated - old_allocated) * sizeof (reg_info)
2226 + sizeof (struct reg_info_data) - sizeof (reg_info);
2227 reg_data = (struct reg_info_data *) xcalloc (size_info, 1);
2228 reg_data->min_index = old_allocated;
2229 reg_data->max_index = regno_allocated - 1;
2230 reg_data->next = reg_info_head;
2231 reg_info_head = reg_data;
2234 reg_n_max = num_regs;
2235 if (min < num_regs)
2237 /* Loop through each of the segments allocated for the actual
2238 reg_info pages, and set up the pointers, zero the pages, etc. */
2239 for (reg_data = reg_info_head;
2240 reg_data && reg_data->max_index >= min;
2241 reg_data = reg_data->next)
2243 size_t min_index = reg_data->min_index;
2244 size_t max_index = reg_data->max_index;
2245 size_t max = MIN (max_index, num_regs);
2246 size_t local_min = min - min_index;
2247 size_t i;
2249 if (reg_data->min_index > num_regs)
2250 continue;
2252 if (min < min_index)
2253 local_min = 0;
2254 if (!reg_data->used_p) /* page just allocated with calloc */
2255 reg_data->used_p = 1; /* no need to zero */
2256 else
2257 memset ((char *) &reg_data->data[local_min], 0,
2258 sizeof (reg_info) * (max - min_index - local_min + 1));
2260 for (i = min_index+local_min; i <= max; i++)
2262 VARRAY_REG (reg_n_info, i) = &reg_data->data[i-min_index];
2263 REG_BASIC_BLOCK (i) = REG_BLOCK_UNKNOWN;
2264 renumber[i] = -1;
2265 reg_pref_buffer[i].prefclass = (char) NO_REGS;
2266 reg_pref_buffer[i].altclass = (char) NO_REGS;
2271 /* If {pref,alt}class have already been allocated, update the pointers to
2272 the newly realloced ones. */
2273 if (reg_pref)
2274 reg_pref = reg_pref_buffer;
2276 if (renumber_p)
2277 reg_renumber = renumber;
2279 /* Tell the regset code about the new number of registers. */
2280 MAX_REGNO_REG_SET (num_regs, new_p, renumber_p);
2283 /* Free up the space allocated by allocate_reg_info. */
2284 void
2285 free_reg_info ()
2287 if (reg_n_info)
2289 struct reg_info_data *reg_data;
2290 struct reg_info_data *reg_next;
2292 VARRAY_FREE (reg_n_info);
2293 for (reg_data = reg_info_head; reg_data; reg_data = reg_next)
2295 reg_next = reg_data->next;
2296 free ((char *) reg_data);
2299 free (reg_pref_buffer);
2300 reg_pref_buffer = (struct reg_pref *) 0;
2301 reg_info_head = (struct reg_info_data *) 0;
2302 renumber = (short *) 0;
2304 regno_allocated = 0;
2305 reg_n_max = 0;
2308 /* This is the `regscan' pass of the compiler, run just before cse
2309 and again just before loop.
2311 It finds the first and last use of each pseudo-register
2312 and records them in the vectors regno_first_uid, regno_last_uid
2313 and counts the number of sets in the vector reg_n_sets.
2315 REPEAT is nonzero the second time this is called. */
2317 /* Maximum number of parallel sets and clobbers in any insn in this fn.
2318 Always at least 3, since the combiner could put that many together
2319 and we want this to remain correct for all the remaining passes.
2320 This corresponds to the maximum number of times note_stores will call
2321 a function for any insn. */
2323 int max_parallel;
2325 /* Used as a temporary to record the largest number of registers in
2326 PARALLEL in a SET_DEST. This is added to max_parallel. */
2328 static int max_set_parallel;
2330 void
2331 reg_scan (f, nregs, repeat)
2332 rtx f;
2333 unsigned int nregs;
2334 int repeat ATTRIBUTE_UNUSED;
2336 rtx insn;
2338 allocate_reg_info (nregs, TRUE, FALSE);
2339 max_parallel = 3;
2340 max_set_parallel = 0;
2342 timevar_push (TV_REG_SCAN);
2344 for (insn = f; insn; insn = NEXT_INSN (insn))
2345 if (GET_CODE (insn) == INSN
2346 || GET_CODE (insn) == CALL_INSN
2347 || GET_CODE (insn) == JUMP_INSN)
2349 if (GET_CODE (PATTERN (insn)) == PARALLEL
2350 && XVECLEN (PATTERN (insn), 0) > max_parallel)
2351 max_parallel = XVECLEN (PATTERN (insn), 0);
2352 reg_scan_mark_refs (PATTERN (insn), insn, 0, 0);
2354 if (REG_NOTES (insn))
2355 reg_scan_mark_refs (REG_NOTES (insn), insn, 1, 0);
2358 max_parallel += max_set_parallel;
2360 timevar_pop (TV_REG_SCAN);
2363 /* Update 'regscan' information by looking at the insns
2364 from FIRST to LAST. Some new REGs have been created,
2365 and any REG with number greater than OLD_MAX_REGNO is
2366 such a REG. We only update information for those. */
2368 void
2369 reg_scan_update (first, last, old_max_regno)
2370 rtx first;
2371 rtx last;
2372 unsigned int old_max_regno;
2374 rtx insn;
2376 allocate_reg_info (max_reg_num (), FALSE, FALSE);
2378 for (insn = first; insn != last; insn = NEXT_INSN (insn))
2379 if (GET_CODE (insn) == INSN
2380 || GET_CODE (insn) == CALL_INSN
2381 || GET_CODE (insn) == JUMP_INSN)
2383 if (GET_CODE (PATTERN (insn)) == PARALLEL
2384 && XVECLEN (PATTERN (insn), 0) > max_parallel)
2385 max_parallel = XVECLEN (PATTERN (insn), 0);
2386 reg_scan_mark_refs (PATTERN (insn), insn, 0, old_max_regno);
2388 if (REG_NOTES (insn))
2389 reg_scan_mark_refs (REG_NOTES (insn), insn, 1, old_max_regno);
2393 /* X is the expression to scan. INSN is the insn it appears in.
2394 NOTE_FLAG is nonzero if X is from INSN's notes rather than its body.
2395 We should only record information for REGs with numbers
2396 greater than or equal to MIN_REGNO. */
2398 static void
2399 reg_scan_mark_refs (x, insn, note_flag, min_regno)
2400 rtx x;
2401 rtx insn;
2402 int note_flag;
2403 unsigned int min_regno;
2405 enum rtx_code code;
2406 rtx dest;
2407 rtx note;
2409 if (!x)
2410 return;
2411 code = GET_CODE (x);
2412 switch (code)
2414 case CONST:
2415 case CONST_INT:
2416 case CONST_DOUBLE:
2417 case CONST_VECTOR:
2418 case CC0:
2419 case PC:
2420 case SYMBOL_REF:
2421 case LABEL_REF:
2422 case ADDR_VEC:
2423 case ADDR_DIFF_VEC:
2424 return;
2426 case REG:
2428 unsigned int regno = REGNO (x);
2430 if (regno >= min_regno)
2432 REGNO_LAST_NOTE_UID (regno) = INSN_UID (insn);
2433 if (!note_flag)
2434 REGNO_LAST_UID (regno) = INSN_UID (insn);
2435 if (REGNO_FIRST_UID (regno) == 0)
2436 REGNO_FIRST_UID (regno) = INSN_UID (insn);
2437 /* If we are called by reg_scan_update() (indicated by min_regno
2438 being set), we also need to update the reference count. */
2439 if (min_regno)
2440 REG_N_REFS (regno)++;
2443 break;
2445 case EXPR_LIST:
2446 if (XEXP (x, 0))
2447 reg_scan_mark_refs (XEXP (x, 0), insn, note_flag, min_regno);
2448 if (XEXP (x, 1))
2449 reg_scan_mark_refs (XEXP (x, 1), insn, note_flag, min_regno);
2450 break;
2452 case INSN_LIST:
2453 if (XEXP (x, 1))
2454 reg_scan_mark_refs (XEXP (x, 1), insn, note_flag, min_regno);
2455 break;
2457 case CLOBBER:
2459 rtx reg = XEXP (x, 0);
2460 if (REG_P (reg)
2461 && REGNO (reg) >= min_regno)
2463 REG_N_SETS (REGNO (reg))++;
2464 REG_N_REFS (REGNO (reg))++;
2467 break;
2469 case SET:
2470 /* Count a set of the destination if it is a register. */
2471 for (dest = SET_DEST (x);
2472 GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
2473 || GET_CODE (dest) == ZERO_EXTEND;
2474 dest = XEXP (dest, 0))
2477 /* For a PARALLEL, record the number of things (less the usual one for a
2478 SET) that are set. */
2479 if (GET_CODE (dest) == PARALLEL)
2480 max_set_parallel = MAX (max_set_parallel, XVECLEN (dest, 0) - 1);
2482 if (GET_CODE (dest) == REG
2483 && REGNO (dest) >= min_regno)
2485 REG_N_SETS (REGNO (dest))++;
2486 REG_N_REFS (REGNO (dest))++;
2489 /* If this is setting a pseudo from another pseudo or the sum of a
2490 pseudo and a constant integer and the other pseudo is known to be
2491 a pointer, set the destination to be a pointer as well.
2493 Likewise if it is setting the destination from an address or from a
2494 value equivalent to an address or to the sum of an address and
2495 something else.
2497 But don't do any of this if the pseudo corresponds to a user
2498 variable since it should have already been set as a pointer based
2499 on the type. */
2501 if (GET_CODE (SET_DEST (x)) == REG
2502 && REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER
2503 && REGNO (SET_DEST (x)) >= min_regno
2504 /* If the destination pseudo is set more than once, then other
2505 sets might not be to a pointer value (consider access to a
2506 union in two threads of control in the presence of global
2507 optimizations). So only set REG_POINTER on the destination
2508 pseudo if this is the only set of that pseudo. */
2509 && REG_N_SETS (REGNO (SET_DEST (x))) == 1
2510 && ! REG_USERVAR_P (SET_DEST (x))
2511 && ! REG_POINTER (SET_DEST (x))
2512 && ((GET_CODE (SET_SRC (x)) == REG
2513 && REG_POINTER (SET_SRC (x)))
2514 || ((GET_CODE (SET_SRC (x)) == PLUS
2515 || GET_CODE (SET_SRC (x)) == LO_SUM)
2516 && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2517 && GET_CODE (XEXP (SET_SRC (x), 0)) == REG
2518 && REG_POINTER (XEXP (SET_SRC (x), 0)))
2519 || GET_CODE (SET_SRC (x)) == CONST
2520 || GET_CODE (SET_SRC (x)) == SYMBOL_REF
2521 || GET_CODE (SET_SRC (x)) == LABEL_REF
2522 || (GET_CODE (SET_SRC (x)) == HIGH
2523 && (GET_CODE (XEXP (SET_SRC (x), 0)) == CONST
2524 || GET_CODE (XEXP (SET_SRC (x), 0)) == SYMBOL_REF
2525 || GET_CODE (XEXP (SET_SRC (x), 0)) == LABEL_REF))
2526 || ((GET_CODE (SET_SRC (x)) == PLUS
2527 || GET_CODE (SET_SRC (x)) == LO_SUM)
2528 && (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST
2529 || GET_CODE (XEXP (SET_SRC (x), 1)) == SYMBOL_REF
2530 || GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF))
2531 || ((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
2532 && (GET_CODE (XEXP (note, 0)) == CONST
2533 || GET_CODE (XEXP (note, 0)) == SYMBOL_REF
2534 || GET_CODE (XEXP (note, 0)) == LABEL_REF))))
2535 REG_POINTER (SET_DEST (x)) = 1;
2537 /* If this is setting a register from a register or from a simple
2538 conversion of a register, propagate REG_EXPR. */
2539 if (GET_CODE (dest) == REG)
2541 rtx src = SET_SRC (x);
2543 while (GET_CODE (src) == SIGN_EXTEND
2544 || GET_CODE (src) == ZERO_EXTEND
2545 || GET_CODE (src) == TRUNCATE
2546 || (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)))
2547 src = XEXP (src, 0);
2549 if (!REG_ATTRS (dest) && REG_P (src))
2550 REG_ATTRS (dest) = REG_ATTRS (src);
2551 if (!REG_ATTRS (dest) && GET_CODE (src) == MEM)
2552 set_reg_attrs_from_mem (dest, src);
2555 /* ... fall through ... */
2557 default:
2559 const char *fmt = GET_RTX_FORMAT (code);
2560 int i;
2561 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2563 if (fmt[i] == 'e')
2564 reg_scan_mark_refs (XEXP (x, i), insn, note_flag, min_regno);
2565 else if (fmt[i] == 'E' && XVEC (x, i) != 0)
2567 int j;
2568 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2569 reg_scan_mark_refs (XVECEXP (x, i, j), insn, note_flag, min_regno);
2576 /* Return nonzero if C1 is a subset of C2, i.e., if every register in C1
2577 is also in C2. */
2580 reg_class_subset_p (c1, c2)
2581 enum reg_class c1;
2582 enum reg_class c2;
2584 if (c1 == c2) return 1;
2586 if (c2 == ALL_REGS)
2587 win:
2588 return 1;
2589 GO_IF_HARD_REG_SUBSET (reg_class_contents[(int) c1],
2590 reg_class_contents[(int) c2],
2591 win);
2592 return 0;
2595 /* Return nonzero if there is a register that is in both C1 and C2. */
2598 reg_classes_intersect_p (c1, c2)
2599 enum reg_class c1;
2600 enum reg_class c2;
2602 #ifdef HARD_REG_SET
2603 register
2604 #endif
2605 HARD_REG_SET c;
2607 if (c1 == c2) return 1;
2609 if (c1 == ALL_REGS || c2 == ALL_REGS)
2610 return 1;
2612 COPY_HARD_REG_SET (c, reg_class_contents[(int) c1]);
2613 AND_HARD_REG_SET (c, reg_class_contents[(int) c2]);
2615 GO_IF_HARD_REG_SUBSET (c, reg_class_contents[(int) NO_REGS], lose);
2616 return 1;
2618 lose:
2619 return 0;
2622 /* Release any memory allocated by register sets. */
2624 void
2625 regset_release_memory ()
2627 bitmap_release_memory ();
2630 #ifdef CANNOT_CHANGE_MODE_CLASS
2631 /* Set bits in *USED which correspond to registers which can't change
2632 their mode from FROM to any mode in which REGNO was encountered. */
2634 void
2635 cannot_change_mode_set_regs (used, from, regno)
2636 HARD_REG_SET *used;
2637 enum machine_mode from;
2638 unsigned int regno;
2640 enum machine_mode to;
2641 int n, i;
2642 int start = regno * MAX_MACHINE_MODE;
2644 EXECUTE_IF_SET_IN_BITMAP (&subregs_of_mode, start, n,
2645 if (n >= MAX_MACHINE_MODE + start)
2646 return;
2647 to = n - start;
2648 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2649 if (! TEST_HARD_REG_BIT (*used, i)
2650 && REG_CANNOT_CHANGE_MODE_P (i, from, to))
2651 SET_HARD_REG_BIT (*used, i);
2655 /* Return 1 if REGNO has had an invalid mode change in CLASS from FROM
2656 mode. */
2658 bool
2659 invalid_mode_change_p (regno, class, from_mode)
2660 unsigned int regno;
2661 enum reg_class class;
2662 enum machine_mode from_mode;
2664 enum machine_mode to_mode;
2665 int n;
2666 int start = regno * MAX_MACHINE_MODE;
2668 EXECUTE_IF_SET_IN_BITMAP (&subregs_of_mode, start, n,
2669 if (n >= MAX_MACHINE_MODE + start)
2670 return 0;
2671 to_mode = n - start;
2672 if (CANNOT_CHANGE_MODE_CLASS (from_mode, to_mode, class))
2673 return 1;
2675 return 0;
2677 #endif /* CANNOT_CHANGE_MODE_CLASS */
2679 #include "gt-regclass.h"