* arm.c (arm_compute_initial_elimination_offset): If optimizing for
[official-gcc.git] / gcc / regclass.c
blobea6c86b6c578e7366f67823fd6b1c90abf45abd4
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"
47 #ifndef REGISTER_MOVE_COST
48 #define REGISTER_MOVE_COST(m, x, y) 2
49 #endif
51 static void init_reg_sets_1 PARAMS ((void));
52 static void init_reg_modes PARAMS ((void));
53 static void init_reg_autoinc PARAMS ((void));
55 /* If we have auto-increment or auto-decrement and we can have secondary
56 reloads, we are not allowed to use classes requiring secondary
57 reloads for pseudos auto-incremented since reload can't handle it. */
59 #ifdef AUTO_INC_DEC
60 #if defined(SECONDARY_INPUT_RELOAD_CLASS) || defined(SECONDARY_OUTPUT_RELOAD_CLASS)
61 #define FORBIDDEN_INC_DEC_CLASSES
62 #endif
63 #endif
65 /* Register tables used by many passes. */
67 /* Indexed by hard register number, contains 1 for registers
68 that are fixed use (stack pointer, pc, frame pointer, etc.).
69 These are the registers that cannot be used to allocate
70 a pseudo reg for general use. */
72 char fixed_regs[FIRST_PSEUDO_REGISTER];
74 /* Same info as a HARD_REG_SET. */
76 HARD_REG_SET fixed_reg_set;
78 /* Data for initializing the above. */
80 static const char initial_fixed_regs[] = FIXED_REGISTERS;
82 /* Indexed by hard register number, contains 1 for registers
83 that are fixed use or are clobbered by function calls.
84 These are the registers that cannot be used to allocate
85 a pseudo reg whose life crosses calls unless we are able
86 to save/restore them across the calls. */
88 char call_used_regs[FIRST_PSEUDO_REGISTER];
90 /* Same info as a HARD_REG_SET. */
92 HARD_REG_SET call_used_reg_set;
94 /* HARD_REG_SET of registers we want to avoid caller saving. */
95 HARD_REG_SET losing_caller_save_reg_set;
97 /* Data for initializing the above. */
99 static const char initial_call_used_regs[] = CALL_USED_REGISTERS;
101 /* This is much like call_used_regs, except it doesn't have to
102 be a superset of FIXED_REGISTERS. This vector indicates
103 what is really call clobbered, and is used when defining
104 regs_invalidated_by_call. */
106 #ifdef CALL_REALLY_USED_REGISTERS
107 char call_really_used_regs[] = CALL_REALLY_USED_REGISTERS;
108 #endif
110 /* Indexed by hard register number, contains 1 for registers that are
111 fixed use or call used registers that cannot hold quantities across
112 calls even if we are willing to save and restore them. call fixed
113 registers are a subset of call used registers. */
115 char call_fixed_regs[FIRST_PSEUDO_REGISTER];
117 /* The same info as a HARD_REG_SET. */
119 HARD_REG_SET call_fixed_reg_set;
121 /* Number of non-fixed registers. */
123 int n_non_fixed_regs;
125 /* Indexed by hard register number, contains 1 for registers
126 that are being used for global register decls.
127 These must be exempt from ordinary flow analysis
128 and are also considered fixed. */
130 char global_regs[FIRST_PSEUDO_REGISTER];
132 /* Contains 1 for registers that are set or clobbered by calls. */
133 /* ??? Ideally, this would be just call_used_regs plus global_regs, but
134 for someone's bright idea to have call_used_regs strictly include
135 fixed_regs. Which leaves us guessing as to the set of fixed_regs
136 that are actually preserved. We know for sure that those associated
137 with the local stack frame are safe, but scant others. */
139 HARD_REG_SET regs_invalidated_by_call;
141 /* Table of register numbers in the order in which to try to use them. */
142 #ifdef REG_ALLOC_ORDER
143 int reg_alloc_order[FIRST_PSEUDO_REGISTER] = REG_ALLOC_ORDER;
145 /* The inverse of reg_alloc_order. */
146 int inv_reg_alloc_order[FIRST_PSEUDO_REGISTER];
147 #endif
149 /* For each reg class, a HARD_REG_SET saying which registers are in it. */
151 HARD_REG_SET reg_class_contents[N_REG_CLASSES];
153 /* The same information, but as an array of unsigned ints. We copy from
154 these unsigned ints to the table above. We do this so the tm.h files
155 do not have to be aware of the wordsize for machines with <= 64 regs.
156 Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
158 #define N_REG_INTS \
159 ((FIRST_PSEUDO_REGISTER + (32 - 1)) / 32)
161 static const unsigned int_reg_class_contents[N_REG_CLASSES][N_REG_INTS]
162 = REG_CLASS_CONTENTS;
164 /* For each reg class, number of regs it contains. */
166 unsigned int reg_class_size[N_REG_CLASSES];
168 /* For each reg class, table listing all the containing classes. */
170 enum reg_class reg_class_superclasses[N_REG_CLASSES][N_REG_CLASSES];
172 /* For each reg class, table listing all the classes contained in it. */
174 enum reg_class reg_class_subclasses[N_REG_CLASSES][N_REG_CLASSES];
176 /* For each pair of reg classes,
177 a largest reg class contained in their union. */
179 enum reg_class reg_class_subunion[N_REG_CLASSES][N_REG_CLASSES];
181 /* For each pair of reg classes,
182 the smallest reg class containing their union. */
184 enum reg_class reg_class_superunion[N_REG_CLASSES][N_REG_CLASSES];
186 /* Array containing all of the register names. Unless
187 DEBUG_REGISTER_NAMES is defined, use the copy in print-rtl.c. */
189 #ifdef DEBUG_REGISTER_NAMES
190 const char * reg_names[] = REGISTER_NAMES;
191 #endif
193 /* For each hard register, the widest mode object that it can contain.
194 This will be a MODE_INT mode if the register can hold integers. Otherwise
195 it will be a MODE_FLOAT or a MODE_CC mode, whichever is valid for the
196 register. */
198 enum machine_mode reg_raw_mode[FIRST_PSEUDO_REGISTER];
200 /* 1 if class does contain register of given mode. */
202 static char contains_reg_of_mode [N_REG_CLASSES] [MAX_MACHINE_MODE];
204 /* Maximum cost of moving from a register in one class to a register in
205 another class. Based on REGISTER_MOVE_COST. */
207 static int move_cost[MAX_MACHINE_MODE][N_REG_CLASSES][N_REG_CLASSES];
209 /* Similar, but here we don't have to move if the first index is a subset
210 of the second so in that case the cost is zero. */
212 static int may_move_in_cost[MAX_MACHINE_MODE][N_REG_CLASSES][N_REG_CLASSES];
214 /* Similar, but here we don't have to move if the first index is a superset
215 of the second so in that case the cost is zero. */
217 static int may_move_out_cost[MAX_MACHINE_MODE][N_REG_CLASSES][N_REG_CLASSES];
219 #ifdef FORBIDDEN_INC_DEC_CLASSES
221 /* These are the classes that regs which are auto-incremented or decremented
222 cannot be put in. */
224 static int forbidden_inc_dec_class[N_REG_CLASSES];
226 /* Indexed by n, is nonzero if (REG n) is used in an auto-inc or auto-dec
227 context. */
229 static char *in_inc_dec;
231 #endif /* FORBIDDEN_INC_DEC_CLASSES */
233 #ifdef CANNOT_CHANGE_MODE_CLASS
234 /* All registers that have been subreged. Indexed by mode, where each
235 entry is a regset of registers. */
236 regset_head subregs_of_mode [NUM_MACHINE_MODES];
237 #endif
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;
263 /* Function called only once to initialize the above data on reg usage.
264 Once this is done, various switches may override. */
266 void
267 init_reg_sets ()
269 int i, j;
271 /* First copy the register information from the initial int form into
272 the regsets. */
274 for (i = 0; i < N_REG_CLASSES; i++)
276 CLEAR_HARD_REG_SET (reg_class_contents[i]);
278 /* Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
279 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
280 if (int_reg_class_contents[i][j / 32]
281 & ((unsigned) 1 << (j % 32)))
282 SET_HARD_REG_BIT (reg_class_contents[i], j);
285 memcpy (fixed_regs, initial_fixed_regs, sizeof fixed_regs);
286 memcpy (call_used_regs, initial_call_used_regs, sizeof call_used_regs);
287 memset (global_regs, 0, sizeof global_regs);
289 /* Do any additional initialization regsets may need. */
290 INIT_ONCE_REG_SET ();
292 #ifdef REG_ALLOC_ORDER
293 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
294 inv_reg_alloc_order[reg_alloc_order[i]] = i;
295 #endif
298 /* After switches have been processed, which perhaps alter
299 `fixed_regs' and `call_used_regs', convert them to HARD_REG_SETs. */
301 static void
302 init_reg_sets_1 ()
304 unsigned int i, j;
305 unsigned int /* enum machine_mode */ m;
306 char allocatable_regs_of_mode [MAX_MACHINE_MODE];
308 /* This macro allows the fixed or call-used registers
309 and the register classes to depend on target flags. */
311 #ifdef CONDITIONAL_REGISTER_USAGE
312 CONDITIONAL_REGISTER_USAGE;
313 #endif
315 /* Compute number of hard regs in each class. */
317 memset ((char *) reg_class_size, 0, sizeof reg_class_size);
318 for (i = 0; i < N_REG_CLASSES; i++)
319 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
320 if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
321 reg_class_size[i]++;
323 /* Initialize the table of subunions.
324 reg_class_subunion[I][J] gets the largest-numbered reg-class
325 that is contained in the union of classes I and J. */
327 for (i = 0; i < N_REG_CLASSES; i++)
329 for (j = 0; j < N_REG_CLASSES; j++)
331 #ifdef HARD_REG_SET
332 register /* Declare it register if it's a scalar. */
333 #endif
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 #ifdef HARD_REG_SET
366 register /* Declare it register if it's a scalar. */
367 #endif
368 HARD_REG_SET c;
369 int k;
371 COPY_HARD_REG_SET (c, reg_class_contents[i]);
372 IOR_HARD_REG_SET (c, reg_class_contents[j]);
373 for (k = 0; k < N_REG_CLASSES; k++)
374 GO_IF_HARD_REG_SUBSET (c, reg_class_contents[k], superclass);
376 superclass:
377 reg_class_superunion[i][j] = (enum reg_class) k;
381 /* Initialize the tables of subclasses and superclasses of each reg class.
382 First clear the whole table, then add the elements as they are found. */
384 for (i = 0; i < N_REG_CLASSES; i++)
386 for (j = 0; j < N_REG_CLASSES; j++)
388 reg_class_superclasses[i][j] = LIM_REG_CLASSES;
389 reg_class_subclasses[i][j] = LIM_REG_CLASSES;
393 for (i = 0; i < N_REG_CLASSES; i++)
395 if (i == (int) NO_REGS)
396 continue;
398 for (j = i + 1; j < N_REG_CLASSES; j++)
400 enum reg_class *p;
402 GO_IF_HARD_REG_SUBSET (reg_class_contents[i], reg_class_contents[j],
403 subclass);
404 continue;
405 subclass:
406 /* Reg class I is a subclass of J.
407 Add J to the table of superclasses of I. */
408 p = &reg_class_superclasses[i][0];
409 while (*p != LIM_REG_CLASSES) p++;
410 *p = (enum reg_class) j;
411 /* Add I to the table of superclasses of J. */
412 p = &reg_class_subclasses[j][0];
413 while (*p != LIM_REG_CLASSES) p++;
414 *p = (enum reg_class) i;
418 /* Initialize "constant" tables. */
420 CLEAR_HARD_REG_SET (fixed_reg_set);
421 CLEAR_HARD_REG_SET (call_used_reg_set);
422 CLEAR_HARD_REG_SET (call_fixed_reg_set);
423 CLEAR_HARD_REG_SET (regs_invalidated_by_call);
425 memcpy (call_fixed_regs, fixed_regs, sizeof call_fixed_regs);
427 n_non_fixed_regs = 0;
429 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
431 if (fixed_regs[i])
432 SET_HARD_REG_BIT (fixed_reg_set, i);
433 else
434 n_non_fixed_regs++;
436 if (call_used_regs[i])
437 SET_HARD_REG_BIT (call_used_reg_set, i);
438 if (call_fixed_regs[i])
439 SET_HARD_REG_BIT (call_fixed_reg_set, i);
440 if (CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (i)))
441 SET_HARD_REG_BIT (losing_caller_save_reg_set, i);
443 /* There are a couple of fixed registers that we know are safe to
444 exclude from being clobbered by calls:
446 The frame pointer is always preserved across calls. The arg pointer
447 is if it is fixed. The stack pointer usually is, unless
448 RETURN_POPS_ARGS, in which case an explicit CLOBBER will be present.
449 If we are generating PIC code, the PIC offset table register is
450 preserved across calls, though the target can override that. */
452 if (i == STACK_POINTER_REGNUM || i == FRAME_POINTER_REGNUM)
454 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
455 else if (i == HARD_FRAME_POINTER_REGNUM)
457 #endif
458 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
459 else if (i == ARG_POINTER_REGNUM && fixed_regs[i])
461 #endif
462 #ifndef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
463 else if (i == (unsigned) PIC_OFFSET_TABLE_REGNUM && fixed_regs[i])
465 #endif
466 else if (0
467 #ifdef CALL_REALLY_USED_REGISTERS
468 || call_really_used_regs[i]
469 #else
470 || call_used_regs[i]
471 #endif
472 || global_regs[i])
473 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
476 memset (contains_reg_of_mode, 0, sizeof (contains_reg_of_mode));
477 memset (allocatable_regs_of_mode, 0, sizeof (allocatable_regs_of_mode));
478 for (m = 0; m < (unsigned int) MAX_MACHINE_MODE; m++)
479 for (i = 0; i < N_REG_CLASSES; i++)
480 if ((unsigned) CLASS_MAX_NREGS (i, m) <= reg_class_size[i])
481 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
482 if (!fixed_regs [j] && TEST_HARD_REG_BIT (reg_class_contents[i], j)
483 && HARD_REGNO_MODE_OK (j, m))
485 contains_reg_of_mode [i][m] = 1;
486 allocatable_regs_of_mode [m] = 1;
487 break;
490 /* Initialize the move cost table. Find every subset of each class
491 and take the maximum cost of moving any subset to any other. */
493 for (m = 0; m < (unsigned int) MAX_MACHINE_MODE; m++)
494 if (allocatable_regs_of_mode [m])
496 for (i = 0; i < N_REG_CLASSES; i++)
497 if (contains_reg_of_mode [i][m])
498 for (j = 0; j < N_REG_CLASSES; j++)
500 int cost;
501 enum reg_class *p1, *p2;
503 if (!contains_reg_of_mode [j][m])
505 move_cost[m][i][j] = 65536;
506 may_move_in_cost[m][i][j] = 65536;
507 may_move_out_cost[m][i][j] = 65536;
509 else
511 cost = REGISTER_MOVE_COST (m, i, j);
513 for (p2 = &reg_class_subclasses[j][0];
514 *p2 != LIM_REG_CLASSES;
515 p2++)
516 if (*p2 != i && contains_reg_of_mode [*p2][m])
517 cost = MAX (cost, move_cost [m][i][*p2]);
519 for (p1 = &reg_class_subclasses[i][0];
520 *p1 != LIM_REG_CLASSES;
521 p1++)
522 if (*p1 != j && contains_reg_of_mode [*p1][m])
523 cost = MAX (cost, move_cost [m][*p1][j]);
525 move_cost[m][i][j] = cost;
527 if (reg_class_subset_p (i, j))
528 may_move_in_cost[m][i][j] = 0;
529 else
530 may_move_in_cost[m][i][j] = cost;
532 if (reg_class_subset_p (j, i))
533 may_move_out_cost[m][i][j] = 0;
534 else
535 may_move_out_cost[m][i][j] = cost;
538 else
539 for (j = 0; j < N_REG_CLASSES; j++)
541 move_cost[m][i][j] = 65536;
542 may_move_in_cost[m][i][j] = 65536;
543 may_move_out_cost[m][i][j] = 65536;
548 /* Compute the table of register modes.
549 These values are used to record death information for individual registers
550 (as opposed to a multi-register mode). */
552 static void
553 init_reg_modes ()
555 int i;
557 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
559 reg_raw_mode[i] = choose_hard_reg_mode (i, 1);
561 /* If we couldn't find a valid mode, just use the previous mode.
562 ??? One situation in which we need to do this is on the mips where
563 HARD_REGNO_NREGS (fpreg, [SD]Fmode) returns 2. Ideally we'd like
564 to use DF mode for the even registers and VOIDmode for the odd
565 (for the cpu models where the odd ones are inaccessible). */
566 if (reg_raw_mode[i] == VOIDmode)
567 reg_raw_mode[i] = i == 0 ? word_mode : reg_raw_mode[i-1];
571 /* Finish initializing the register sets and
572 initialize the register modes. */
574 void
575 init_regs ()
577 /* This finishes what was started by init_reg_sets, but couldn't be done
578 until after register usage was specified. */
579 init_reg_sets_1 ();
581 init_reg_modes ();
583 init_reg_autoinc ();
586 /* Initialize some fake stack-frame MEM references for use in
587 memory_move_secondary_cost. */
589 void
590 init_fake_stack_mems ()
592 #ifdef HAVE_SECONDARY_RELOADS
594 int i;
596 for (i = 0; i < MAX_MACHINE_MODE; i++)
597 top_of_stack[i] = gen_rtx_MEM (i, stack_pointer_rtx);
599 #endif
602 #ifdef HAVE_SECONDARY_RELOADS
604 /* Compute extra cost of moving registers to/from memory due to reloads.
605 Only needed if secondary reloads are required for memory moves. */
608 memory_move_secondary_cost (mode, class, in)
609 enum machine_mode mode;
610 enum reg_class class;
611 int in;
613 enum reg_class altclass;
614 int partial_cost = 0;
615 /* We need a memory reference to feed to SECONDARY... macros. */
616 /* mem may be unused even if the SECONDARY_ macros are defined. */
617 rtx mem ATTRIBUTE_UNUSED = top_of_stack[(int) mode];
620 if (in)
622 #ifdef SECONDARY_INPUT_RELOAD_CLASS
623 altclass = SECONDARY_INPUT_RELOAD_CLASS (class, mode, mem);
624 #else
625 altclass = NO_REGS;
626 #endif
628 else
630 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
631 altclass = SECONDARY_OUTPUT_RELOAD_CLASS (class, mode, mem);
632 #else
633 altclass = NO_REGS;
634 #endif
637 if (altclass == NO_REGS)
638 return 0;
640 if (in)
641 partial_cost = REGISTER_MOVE_COST (mode, altclass, class);
642 else
643 partial_cost = REGISTER_MOVE_COST (mode, class, altclass);
645 if (class == altclass)
646 /* This isn't simply a copy-to-temporary situation. Can't guess
647 what it is, so MEMORY_MOVE_COST really ought not to be calling
648 here in that case.
650 I'm tempted to put in an abort here, but returning this will
651 probably only give poor estimates, which is what we would've
652 had before this code anyways. */
653 return partial_cost;
655 /* Check if the secondary reload register will also need a
656 secondary reload. */
657 return memory_move_secondary_cost (mode, altclass, in) + partial_cost;
659 #endif
661 /* Return a machine mode that is legitimate for hard reg REGNO and large
662 enough to save nregs. If we can't find one, return VOIDmode. */
664 enum machine_mode
665 choose_hard_reg_mode (regno, nregs)
666 unsigned int regno ATTRIBUTE_UNUSED;
667 unsigned int nregs;
669 unsigned int /* enum machine_mode */ m;
670 enum machine_mode found_mode = VOIDmode, mode;
672 /* We first look for the largest integer mode that can be validly
673 held in REGNO. If none, we look for the largest floating-point mode.
674 If we still didn't find a valid mode, try CCmode. */
676 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
677 mode != VOIDmode;
678 mode = GET_MODE_WIDER_MODE (mode))
679 if ((unsigned) HARD_REGNO_NREGS (regno, mode) == nregs
680 && HARD_REGNO_MODE_OK (regno, mode))
681 found_mode = mode;
683 if (found_mode != VOIDmode)
684 return found_mode;
686 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
687 mode != VOIDmode;
688 mode = GET_MODE_WIDER_MODE (mode))
689 if ((unsigned) HARD_REGNO_NREGS (regno, mode) == nregs
690 && HARD_REGNO_MODE_OK (regno, mode))
691 found_mode = mode;
693 if (found_mode != VOIDmode)
694 return found_mode;
696 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
697 mode != VOIDmode;
698 mode = GET_MODE_WIDER_MODE (mode))
699 if ((unsigned) HARD_REGNO_NREGS (regno, mode) == nregs
700 && HARD_REGNO_MODE_OK (regno, mode))
701 found_mode = mode;
703 if (found_mode != VOIDmode)
704 return found_mode;
706 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
707 mode != VOIDmode;
708 mode = GET_MODE_WIDER_MODE (mode))
709 if ((unsigned) HARD_REGNO_NREGS (regno, mode) == nregs
710 && HARD_REGNO_MODE_OK (regno, mode))
711 found_mode = mode;
713 if (found_mode != VOIDmode)
714 return found_mode;
716 /* Iterate over all of the CCmodes. */
717 for (m = (unsigned int) CCmode; m < (unsigned int) NUM_MACHINE_MODES; ++m)
719 mode = (enum machine_mode) m;
720 if ((unsigned) HARD_REGNO_NREGS (regno, mode) == nregs
721 && HARD_REGNO_MODE_OK (regno, mode))
722 return mode;
725 /* We can't find a mode valid for this register. */
726 return VOIDmode;
729 /* Specify the usage characteristics of the register named NAME.
730 It should be a fixed register if FIXED and a
731 call-used register if CALL_USED. */
733 void
734 fix_register (name, fixed, call_used)
735 const char *name;
736 int fixed, call_used;
738 int i;
740 /* Decode the name and update the primary form of
741 the register info. */
743 if ((i = decode_reg_name (name)) >= 0)
745 if ((i == STACK_POINTER_REGNUM
746 #ifdef HARD_FRAME_POINTER_REGNUM
747 || i == HARD_FRAME_POINTER_REGNUM
748 #else
749 || i == FRAME_POINTER_REGNUM
750 #endif
752 && (fixed == 0 || call_used == 0))
754 static const char * const what_option[2][2] = {
755 { "call-saved", "call-used" },
756 { "no-such-option", "fixed" }};
758 error ("can't use '%s' as a %s register", name,
759 what_option[fixed][call_used]);
761 else
763 fixed_regs[i] = fixed;
764 call_used_regs[i] = call_used;
765 #ifdef CALL_REALLY_USED_REGISTERS
766 if (fixed == 0)
767 call_really_used_regs[i] = call_used;
768 #endif
771 else
773 warning ("unknown register name: %s", name);
777 /* Mark register number I as global. */
779 void
780 globalize_reg (i)
781 int i;
783 if (fixed_regs[i] == 0 && no_global_reg_vars)
784 error ("global register variable follows a function definition");
786 if (global_regs[i])
788 warning ("register used for two global register variables");
789 return;
792 if (call_used_regs[i] && ! fixed_regs[i])
793 warning ("call-clobbered register used for global register variable");
795 global_regs[i] = 1;
797 /* If already fixed, nothing else to do. */
798 if (fixed_regs[i])
799 return;
801 fixed_regs[i] = call_used_regs[i] = call_fixed_regs[i] = 1;
802 n_non_fixed_regs--;
804 SET_HARD_REG_BIT (fixed_reg_set, i);
805 SET_HARD_REG_BIT (call_used_reg_set, i);
806 SET_HARD_REG_BIT (call_fixed_reg_set, i);
807 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
810 /* Now the data and code for the `regclass' pass, which happens
811 just before local-alloc. */
813 /* The `costs' struct records the cost of using a hard register of each class
814 and of using memory for each pseudo. We use this data to set up
815 register class preferences. */
817 struct costs
819 int cost[N_REG_CLASSES];
820 int mem_cost;
823 /* Structure used to record preferences of given pseudo. */
824 struct reg_pref
826 /* (enum reg_class) prefclass is the preferred class. */
827 char prefclass;
829 /* altclass is a register class that we should use for allocating
830 pseudo if no register in the preferred class is available.
831 If no register in this class is available, memory is preferred.
833 It might appear to be more general to have a bitmask of classes here,
834 but since it is recommended that there be a class corresponding to the
835 union of most major pair of classes, that generality is not required. */
836 char altclass;
839 /* Record the cost of each class for each pseudo. */
841 static struct costs *costs;
843 /* Initialized once, and used to initialize cost values for each insn. */
845 static struct costs init_cost;
847 /* Record preferences of each pseudo.
848 This is available after `regclass' is run. */
850 static struct reg_pref *reg_pref;
852 /* Allocated buffers for reg_pref. */
854 static struct reg_pref *reg_pref_buffer;
856 /* Frequency of executions of current insn. */
858 static int frequency;
860 static rtx scan_one_insn PARAMS ((rtx, int));
861 static void record_operand_costs PARAMS ((rtx, struct costs *, struct reg_pref *));
862 static void dump_regclass PARAMS ((FILE *));
863 static void record_reg_classes PARAMS ((int, int, rtx *, enum machine_mode *,
864 const char **, rtx,
865 struct costs *, struct reg_pref *));
866 static int copy_cost PARAMS ((rtx, enum machine_mode,
867 enum reg_class, int));
868 static void record_address_regs PARAMS ((rtx, enum reg_class, int));
869 #ifdef FORBIDDEN_INC_DEC_CLASSES
870 static int auto_inc_dec_reg_p PARAMS ((rtx, enum machine_mode));
871 #endif
872 static void reg_scan_mark_refs PARAMS ((rtx, rtx, int, unsigned int));
874 /* Return the reg_class in which pseudo reg number REGNO is best allocated.
875 This function is sometimes called before the info has been computed.
876 When that happens, just return GENERAL_REGS, which is innocuous. */
878 enum reg_class
879 reg_preferred_class (regno)
880 int regno;
882 if (reg_pref == 0)
883 return GENERAL_REGS;
884 return (enum reg_class) reg_pref[regno].prefclass;
887 enum reg_class
888 reg_alternate_class (regno)
889 int regno;
891 if (reg_pref == 0)
892 return ALL_REGS;
894 return (enum reg_class) reg_pref[regno].altclass;
897 /* Initialize some global data for this pass. */
899 void
900 regclass_init ()
902 int i;
904 init_cost.mem_cost = 10000;
905 for (i = 0; i < N_REG_CLASSES; i++)
906 init_cost.cost[i] = 10000;
908 /* This prevents dump_flow_info from losing if called
909 before regclass is run. */
910 reg_pref = NULL;
912 /* No more global register variables may be declared. */
913 no_global_reg_vars = 1;
916 /* Dump register costs. */
917 static void
918 dump_regclass (dump)
919 FILE *dump;
921 static const char *const reg_class_names[] = REG_CLASS_NAMES;
922 int i;
923 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
925 int /* enum reg_class */ class;
926 if (REG_N_REFS (i))
928 fprintf (dump, " Register %i costs:", i);
929 for (class = 0; class < (int) N_REG_CLASSES; class++)
930 if (contains_reg_of_mode [(enum reg_class) class][PSEUDO_REGNO_MODE (i)]
931 #ifdef FORBIDDEN_INC_DEC_CLASSES
932 && (!in_inc_dec[i]
933 || !forbidden_inc_dec_class[(enum reg_class) class])
934 #endif
935 #ifdef CANNOT_CHANGE_MODE_CLASS
936 && ! invalid_mode_change_p (i, (enum reg_class) class,
937 PSEUDO_REGNO_MODE (i))
938 #endif
940 fprintf (dump, " %s:%i", reg_class_names[class],
941 costs[i].cost[(enum reg_class) class]);
942 fprintf (dump, " MEM:%i\n", costs[i].mem_cost);
948 /* Calculate the costs of insn operands. */
950 static void
951 record_operand_costs (insn, op_costs, reg_pref)
952 rtx insn;
953 struct costs *op_costs;
954 struct reg_pref *reg_pref;
956 const char *constraints[MAX_RECOG_OPERANDS];
957 enum machine_mode modes[MAX_RECOG_OPERANDS];
958 int i;
960 for (i = 0; i < recog_data.n_operands; i++)
962 constraints[i] = recog_data.constraints[i];
963 modes[i] = recog_data.operand_mode[i];
966 /* If we get here, we are set up to record the costs of all the
967 operands for this insn. Start by initializing the costs.
968 Then handle any address registers. Finally record the desired
969 classes for any pseudos, doing it twice if some pair of
970 operands are commutative. */
972 for (i = 0; i < recog_data.n_operands; i++)
974 op_costs[i] = init_cost;
976 if (GET_CODE (recog_data.operand[i]) == SUBREG)
977 recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
979 if (GET_CODE (recog_data.operand[i]) == MEM)
980 record_address_regs (XEXP (recog_data.operand[i], 0),
981 MODE_BASE_REG_CLASS (modes[i]), frequency * 2);
982 else if (constraints[i][0] == 'p'
983 || EXTRA_ADDRESS_CONSTRAINT (constraints[i][0], constraints[i]))
984 record_address_regs (recog_data.operand[i],
985 MODE_BASE_REG_CLASS (modes[i]), frequency * 2);
988 /* Check for commutative in a separate loop so everything will
989 have been initialized. We must do this even if one operand
990 is a constant--see addsi3 in m68k.md. */
992 for (i = 0; i < (int) recog_data.n_operands - 1; i++)
993 if (constraints[i][0] == '%')
995 const char *xconstraints[MAX_RECOG_OPERANDS];
996 int j;
998 /* Handle commutative operands by swapping the constraints.
999 We assume the modes are the same. */
1001 for (j = 0; j < recog_data.n_operands; j++)
1002 xconstraints[j] = constraints[j];
1004 xconstraints[i] = constraints[i+1];
1005 xconstraints[i+1] = constraints[i];
1006 record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
1007 recog_data.operand, modes,
1008 xconstraints, insn, op_costs, reg_pref);
1011 record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
1012 recog_data.operand, modes,
1013 constraints, insn, op_costs, reg_pref);
1016 /* Subroutine of regclass, processes one insn INSN. Scan it and record each
1017 time it would save code to put a certain register in a certain class.
1018 PASS, when nonzero, inhibits some optimizations which need only be done
1019 once.
1020 Return the last insn processed, so that the scan can be continued from
1021 there. */
1023 static rtx
1024 scan_one_insn (insn, pass)
1025 rtx insn;
1026 int pass;
1028 enum rtx_code code = GET_CODE (insn);
1029 enum rtx_code pat_code;
1030 rtx set, note;
1031 int i, j;
1032 struct costs op_costs[MAX_RECOG_OPERANDS];
1034 if (GET_RTX_CLASS (code) != 'i')
1035 return insn;
1037 pat_code = GET_CODE (PATTERN (insn));
1038 if (pat_code == USE
1039 || pat_code == CLOBBER
1040 || pat_code == ASM_INPUT
1041 || pat_code == ADDR_VEC
1042 || pat_code == ADDR_DIFF_VEC)
1043 return insn;
1045 set = single_set (insn);
1046 extract_insn (insn);
1048 /* If this insn loads a parameter from its stack slot, then
1049 it represents a savings, rather than a cost, if the
1050 parameter is stored in memory. Record this fact. */
1052 if (set != 0 && GET_CODE (SET_DEST (set)) == REG
1053 && GET_CODE (SET_SRC (set)) == MEM
1054 && (note = find_reg_note (insn, REG_EQUIV,
1055 NULL_RTX)) != 0
1056 && GET_CODE (XEXP (note, 0)) == MEM)
1058 costs[REGNO (SET_DEST (set))].mem_cost
1059 -= (MEMORY_MOVE_COST (GET_MODE (SET_DEST (set)),
1060 GENERAL_REGS, 1)
1061 * frequency);
1062 record_address_regs (XEXP (SET_SRC (set), 0),
1063 MODE_BASE_REG_CLASS (VOIDmode), frequency * 2);
1064 return insn;
1067 /* Improve handling of two-address insns such as
1068 (set X (ashift CONST Y)) where CONST must be made to
1069 match X. Change it into two insns: (set X CONST)
1070 (set X (ashift X Y)). If we left this for reloading, it
1071 would probably get three insns because X and Y might go
1072 in the same place. This prevents X and Y from receiving
1073 the same hard reg.
1075 We can only do this if the modes of operands 0 and 1
1076 (which might not be the same) are tieable and we only need
1077 do this during our first pass. */
1079 if (pass == 0 && optimize
1080 && recog_data.n_operands >= 3
1081 && recog_data.constraints[1][0] == '0'
1082 && recog_data.constraints[1][1] == 0
1083 && CONSTANT_P (recog_data.operand[1])
1084 && ! rtx_equal_p (recog_data.operand[0], recog_data.operand[1])
1085 && ! rtx_equal_p (recog_data.operand[0], recog_data.operand[2])
1086 && GET_CODE (recog_data.operand[0]) == REG
1087 && MODES_TIEABLE_P (GET_MODE (recog_data.operand[0]),
1088 recog_data.operand_mode[1]))
1090 rtx previnsn = prev_real_insn (insn);
1091 rtx dest
1092 = gen_lowpart (recog_data.operand_mode[1],
1093 recog_data.operand[0]);
1094 rtx newinsn
1095 = emit_insn_before (gen_move_insn (dest, recog_data.operand[1]), insn);
1097 /* If this insn was the start of a basic block,
1098 include the new insn in that block.
1099 We need not check for code_label here;
1100 while a basic block can start with a code_label,
1101 INSN could not be at the beginning of that block. */
1102 if (previnsn == 0 || GET_CODE (previnsn) == JUMP_INSN)
1104 basic_block b;
1105 FOR_EACH_BB (b)
1106 if (insn == b->head)
1107 b->head = newinsn;
1110 /* This makes one more setting of new insns's dest. */
1111 REG_N_SETS (REGNO (recog_data.operand[0]))++;
1112 REG_N_REFS (REGNO (recog_data.operand[0]))++;
1113 REG_FREQ (REGNO (recog_data.operand[0])) += frequency;
1115 *recog_data.operand_loc[1] = recog_data.operand[0];
1116 REG_N_REFS (REGNO (recog_data.operand[0]))++;
1117 REG_FREQ (REGNO (recog_data.operand[0])) += frequency;
1118 for (i = recog_data.n_dups - 1; i >= 0; i--)
1119 if (recog_data.dup_num[i] == 1)
1121 *recog_data.dup_loc[i] = recog_data.operand[0];
1122 REG_N_REFS (REGNO (recog_data.operand[0]))++;
1123 REG_FREQ (REGNO (recog_data.operand[0])) += frequency;
1126 return PREV_INSN (newinsn);
1129 record_operand_costs (insn, op_costs, reg_pref);
1131 /* Now add the cost for each operand to the total costs for
1132 its register. */
1134 for (i = 0; i < recog_data.n_operands; i++)
1135 if (GET_CODE (recog_data.operand[i]) == REG
1136 && REGNO (recog_data.operand[i]) >= FIRST_PSEUDO_REGISTER)
1138 int regno = REGNO (recog_data.operand[i]);
1139 struct costs *p = &costs[regno], *q = &op_costs[i];
1141 p->mem_cost += q->mem_cost * frequency;
1142 for (j = 0; j < N_REG_CLASSES; j++)
1143 p->cost[j] += q->cost[j] * frequency;
1146 return insn;
1149 /* Initialize information about which register classes can be used for
1150 pseudos that are auto-incremented or auto-decremented. */
1152 static void
1153 init_reg_autoinc ()
1155 #ifdef FORBIDDEN_INC_DEC_CLASSES
1156 int i;
1158 for (i = 0; i < N_REG_CLASSES; i++)
1160 rtx r = gen_rtx_raw_REG (VOIDmode, 0);
1161 enum machine_mode m;
1162 int j;
1164 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1165 if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
1167 REGNO (r) = j;
1169 for (m = VOIDmode; (int) m < (int) MAX_MACHINE_MODE;
1170 m = (enum machine_mode) ((int) m + 1))
1171 if (HARD_REGNO_MODE_OK (j, m))
1173 PUT_MODE (r, m);
1175 /* If a register is not directly suitable for an
1176 auto-increment or decrement addressing mode and
1177 requires secondary reloads, disallow its class from
1178 being used in such addresses. */
1180 if ((0
1181 #ifdef SECONDARY_RELOAD_CLASS
1182 || (SECONDARY_RELOAD_CLASS (MODE_BASE_REG_CLASS (VOIDmode), m, r)
1183 != NO_REGS)
1184 #else
1185 #ifdef SECONDARY_INPUT_RELOAD_CLASS
1186 || (SECONDARY_INPUT_RELOAD_CLASS (MODE_BASE_REG_CLASS (VOIDmode), m, r)
1187 != NO_REGS)
1188 #endif
1189 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
1190 || (SECONDARY_OUTPUT_RELOAD_CLASS (MODE_BASE_REG_CLASS (VOIDmode), m, r)
1191 != NO_REGS)
1192 #endif
1193 #endif
1195 && ! auto_inc_dec_reg_p (r, m))
1196 forbidden_inc_dec_class[i] = 1;
1200 #endif /* FORBIDDEN_INC_DEC_CLASSES */
1203 /* This is a pass of the compiler that scans all instructions
1204 and calculates the preferred class for each pseudo-register.
1205 This information can be accessed later by calling `reg_preferred_class'.
1206 This pass comes just before local register allocation. */
1208 void
1209 regclass (f, nregs, dump)
1210 rtx f;
1211 int nregs;
1212 FILE *dump;
1214 rtx insn;
1215 int i;
1216 int pass;
1218 init_recog ();
1220 costs = (struct costs *) xmalloc (nregs * sizeof (struct costs));
1222 #ifdef FORBIDDEN_INC_DEC_CLASSES
1224 in_inc_dec = (char *) xmalloc (nregs);
1226 #endif /* FORBIDDEN_INC_DEC_CLASSES */
1228 /* Normally we scan the insns once and determine the best class to use for
1229 each register. However, if -fexpensive_optimizations are on, we do so
1230 twice, the second time using the tentative best classes to guide the
1231 selection. */
1233 for (pass = 0; pass <= flag_expensive_optimizations; pass++)
1235 basic_block bb;
1237 if (dump)
1238 fprintf (dump, "\n\nPass %i\n\n",pass);
1239 /* Zero out our accumulation of the cost of each class for each reg. */
1241 memset ((char *) costs, 0, nregs * sizeof (struct costs));
1243 #ifdef FORBIDDEN_INC_DEC_CLASSES
1244 memset (in_inc_dec, 0, nregs);
1245 #endif
1247 /* Scan the instructions and record each time it would
1248 save code to put a certain register in a certain class. */
1250 if (!optimize)
1252 frequency = REG_FREQ_MAX;
1253 for (insn = f; insn; insn = NEXT_INSN (insn))
1254 insn = scan_one_insn (insn, pass);
1256 else
1257 FOR_EACH_BB (bb)
1259 /* Show that an insn inside a loop is likely to be executed three
1260 times more than insns outside a loop. This is much more
1261 aggressive than the assumptions made elsewhere and is being
1262 tried as an experiment. */
1263 frequency = REG_FREQ_FROM_BB (bb);
1264 for (insn = bb->head; ; insn = NEXT_INSN (insn))
1266 insn = scan_one_insn (insn, pass);
1267 if (insn == bb->end)
1268 break;
1272 /* Now for each register look at how desirable each class is
1273 and find which class is preferred. Store that in
1274 `prefclass'. Record in `altclass' the largest register
1275 class any of whose registers is better than memory. */
1277 if (pass == 0)
1278 reg_pref = reg_pref_buffer;
1280 if (dump)
1282 dump_regclass (dump);
1283 fprintf (dump,"\n");
1285 for (i = FIRST_PSEUDO_REGISTER; i < nregs; i++)
1287 int best_cost = (1 << (HOST_BITS_PER_INT - 2)) - 1;
1288 enum reg_class best = ALL_REGS, alt = NO_REGS;
1289 /* This is an enum reg_class, but we call it an int
1290 to save lots of casts. */
1291 int class;
1292 struct costs *p = &costs[i];
1294 /* In non-optimizing compilation REG_N_REFS is not initialized
1295 yet. */
1296 if (optimize && !REG_N_REFS (i) && !REG_N_SETS (i))
1297 continue;
1299 for (class = (int) ALL_REGS - 1; class > 0; class--)
1301 /* Ignore classes that are too small for this operand or
1302 invalid for an operand that was auto-incremented. */
1303 if (!contains_reg_of_mode [class][PSEUDO_REGNO_MODE (i)]
1304 #ifdef FORBIDDEN_INC_DEC_CLASSES
1305 || (in_inc_dec[i] && forbidden_inc_dec_class[class])
1306 #endif
1307 #ifdef CANNOT_CHANGE_MODE_CLASS
1308 || invalid_mode_change_p (i, (enum reg_class) class,
1309 PSEUDO_REGNO_MODE (i))
1310 #endif
1313 else if (p->cost[class] < best_cost)
1315 best_cost = p->cost[class];
1316 best = (enum reg_class) class;
1318 else if (p->cost[class] == best_cost)
1319 best = reg_class_subunion[(int) best][class];
1322 /* Record the alternate register class; i.e., a class for which
1323 every register in it is better than using memory. If adding a
1324 class would make a smaller class (i.e., no union of just those
1325 classes exists), skip that class. The major unions of classes
1326 should be provided as a register class. Don't do this if we
1327 will be doing it again later. */
1329 if ((pass == 1 || dump) || ! flag_expensive_optimizations)
1330 for (class = 0; class < N_REG_CLASSES; class++)
1331 if (p->cost[class] < p->mem_cost
1332 && (reg_class_size[(int) reg_class_subunion[(int) alt][class]]
1333 > reg_class_size[(int) alt])
1334 #ifdef FORBIDDEN_INC_DEC_CLASSES
1335 && ! (in_inc_dec[i] && forbidden_inc_dec_class[class])
1336 #endif
1337 #ifdef CANNOT_CHANGE_MODE_CLASS
1338 && ! invalid_mode_change_p (i, (enum reg_class) class,
1339 PSEUDO_REGNO_MODE (i))
1340 #endif
1342 alt = reg_class_subunion[(int) alt][class];
1344 /* If we don't add any classes, nothing to try. */
1345 if (alt == best)
1346 alt = NO_REGS;
1348 if (dump
1349 && (reg_pref[i].prefclass != (int) best
1350 || reg_pref[i].altclass != (int) alt))
1352 static const char *const reg_class_names[] = REG_CLASS_NAMES;
1353 fprintf (dump, " Register %i", i);
1354 if (alt == ALL_REGS || best == ALL_REGS)
1355 fprintf (dump, " pref %s\n", reg_class_names[(int) best]);
1356 else if (alt == NO_REGS)
1357 fprintf (dump, " pref %s or none\n", reg_class_names[(int) best]);
1358 else
1359 fprintf (dump, " pref %s, else %s\n",
1360 reg_class_names[(int) best],
1361 reg_class_names[(int) alt]);
1364 /* We cast to (int) because (char) hits bugs in some compilers. */
1365 reg_pref[i].prefclass = (int) best;
1366 reg_pref[i].altclass = (int) alt;
1370 #ifdef FORBIDDEN_INC_DEC_CLASSES
1371 free (in_inc_dec);
1372 #endif
1373 free (costs);
1376 /* Record the cost of using memory or registers of various classes for
1377 the operands in INSN.
1379 N_ALTS is the number of alternatives.
1381 N_OPS is the number of operands.
1383 OPS is an array of the operands.
1385 MODES are the modes of the operands, in case any are VOIDmode.
1387 CONSTRAINTS are the constraints to use for the operands. This array
1388 is modified by this procedure.
1390 This procedure works alternative by alternative. For each alternative
1391 we assume that we will be able to allocate all pseudos to their ideal
1392 register class and calculate the cost of using that alternative. Then
1393 we compute for each operand that is a pseudo-register, the cost of
1394 having the pseudo allocated to each register class and using it in that
1395 alternative. To this cost is added the cost of the alternative.
1397 The cost of each class for this insn is its lowest cost among all the
1398 alternatives. */
1400 static void
1401 record_reg_classes (n_alts, n_ops, ops, modes,
1402 constraints, insn, op_costs, reg_pref)
1403 int n_alts;
1404 int n_ops;
1405 rtx *ops;
1406 enum machine_mode *modes;
1407 const char **constraints;
1408 rtx insn;
1409 struct costs *op_costs;
1410 struct reg_pref *reg_pref;
1412 int alt;
1413 int i, j;
1414 rtx set;
1416 /* Process each alternative, each time minimizing an operand's cost with
1417 the cost for each operand in that alternative. */
1419 for (alt = 0; alt < n_alts; alt++)
1421 struct costs this_op_costs[MAX_RECOG_OPERANDS];
1422 int alt_fail = 0;
1423 int alt_cost = 0;
1424 enum reg_class classes[MAX_RECOG_OPERANDS];
1425 int allows_mem[MAX_RECOG_OPERANDS];
1426 int class;
1428 for (i = 0; i < n_ops; i++)
1430 const char *p = constraints[i];
1431 rtx op = ops[i];
1432 enum machine_mode mode = modes[i];
1433 int allows_addr = 0;
1434 int win = 0;
1435 unsigned char c;
1437 /* Initially show we know nothing about the register class. */
1438 classes[i] = NO_REGS;
1439 allows_mem[i] = 0;
1441 /* If this operand has no constraints at all, we can conclude
1442 nothing about it since anything is valid. */
1444 if (*p == 0)
1446 if (GET_CODE (op) == REG && REGNO (op) >= FIRST_PSEUDO_REGISTER)
1447 memset ((char *) &this_op_costs[i], 0, sizeof this_op_costs[i]);
1449 continue;
1452 /* If this alternative is only relevant when this operand
1453 matches a previous operand, we do different things depending
1454 on whether this operand is a pseudo-reg or not. We must process
1455 any modifiers for the operand before we can make this test. */
1457 while (*p == '%' || *p == '=' || *p == '+' || *p == '&')
1458 p++;
1460 if (p[0] >= '0' && p[0] <= '0' + i && (p[1] == ',' || p[1] == 0))
1462 /* Copy class and whether memory is allowed from the matching
1463 alternative. Then perform any needed cost computations
1464 and/or adjustments. */
1465 j = p[0] - '0';
1466 classes[i] = classes[j];
1467 allows_mem[i] = allows_mem[j];
1469 if (GET_CODE (op) != REG || REGNO (op) < FIRST_PSEUDO_REGISTER)
1471 /* If this matches the other operand, we have no added
1472 cost and we win. */
1473 if (rtx_equal_p (ops[j], op))
1474 win = 1;
1476 /* If we can put the other operand into a register, add to
1477 the cost of this alternative the cost to copy this
1478 operand to the register used for the other operand. */
1480 else if (classes[j] != NO_REGS)
1481 alt_cost += copy_cost (op, mode, classes[j], 1), win = 1;
1483 else if (GET_CODE (ops[j]) != REG
1484 || REGNO (ops[j]) < FIRST_PSEUDO_REGISTER)
1486 /* This op is a pseudo but the one it matches is not. */
1488 /* If we can't put the other operand into a register, this
1489 alternative can't be used. */
1491 if (classes[j] == NO_REGS)
1492 alt_fail = 1;
1494 /* Otherwise, add to the cost of this alternative the cost
1495 to copy the other operand to the register used for this
1496 operand. */
1498 else
1499 alt_cost += copy_cost (ops[j], mode, classes[j], 1);
1501 else
1503 /* The costs of this operand are not the same as the other
1504 operand since move costs are not symmetric. Moreover,
1505 if we cannot tie them, this alternative needs to do a
1506 copy, which is one instruction. */
1508 struct costs *pp = &this_op_costs[i];
1510 for (class = 0; class < N_REG_CLASSES; class++)
1511 pp->cost[class]
1512 = ((recog_data.operand_type[i] != OP_OUT
1513 ? may_move_in_cost[mode][class][(int) classes[i]]
1514 : 0)
1515 + (recog_data.operand_type[i] != OP_IN
1516 ? may_move_out_cost[mode][(int) classes[i]][class]
1517 : 0));
1519 /* If the alternative actually allows memory, make things
1520 a bit cheaper since we won't need an extra insn to
1521 load it. */
1523 pp->mem_cost
1524 = ((recog_data.operand_type[i] != OP_IN
1525 ? MEMORY_MOVE_COST (mode, classes[i], 0)
1526 : 0)
1527 + (recog_data.operand_type[i] != OP_OUT
1528 ? MEMORY_MOVE_COST (mode, classes[i], 1)
1529 : 0) - allows_mem[i]);
1531 /* If we have assigned a class to this register in our
1532 first pass, add a cost to this alternative corresponding
1533 to what we would add if this register were not in the
1534 appropriate class. */
1536 if (reg_pref)
1537 alt_cost
1538 += (may_move_in_cost[mode]
1539 [(unsigned char) reg_pref[REGNO (op)].prefclass]
1540 [(int) classes[i]]);
1542 if (REGNO (ops[i]) != REGNO (ops[j])
1543 && ! find_reg_note (insn, REG_DEAD, op))
1544 alt_cost += 2;
1546 /* This is in place of ordinary cost computation
1547 for this operand, so skip to the end of the
1548 alternative (should be just one character). */
1549 while (*p && *p++ != ',')
1552 constraints[i] = p;
1553 continue;
1557 /* Scan all the constraint letters. See if the operand matches
1558 any of the constraints. Collect the valid register classes
1559 and see if this operand accepts memory. */
1561 while ((c = *p))
1563 switch (c)
1565 case ',':
1566 break;
1567 case '*':
1568 /* Ignore the next letter for this pass. */
1569 c = *++p;
1570 break;
1572 case '?':
1573 alt_cost += 2;
1574 case '!': case '#': case '&':
1575 case '0': case '1': case '2': case '3': case '4':
1576 case '5': case '6': case '7': case '8': case '9':
1577 break;
1579 case 'p':
1580 allows_addr = 1;
1581 win = address_operand (op, GET_MODE (op));
1582 /* We know this operand is an address, so we want it to be
1583 allocated to a register that can be the base of an
1584 address, ie BASE_REG_CLASS. */
1585 classes[i]
1586 = reg_class_subunion[(int) classes[i]]
1587 [(int) MODE_BASE_REG_CLASS (VOIDmode)];
1588 break;
1590 case 'm': case 'o': case 'V':
1591 /* It doesn't seem worth distinguishing between offsettable
1592 and non-offsettable addresses here. */
1593 allows_mem[i] = 1;
1594 if (GET_CODE (op) == MEM)
1595 win = 1;
1596 break;
1598 case '<':
1599 if (GET_CODE (op) == MEM
1600 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
1601 || GET_CODE (XEXP (op, 0)) == POST_DEC))
1602 win = 1;
1603 break;
1605 case '>':
1606 if (GET_CODE (op) == MEM
1607 && (GET_CODE (XEXP (op, 0)) == PRE_INC
1608 || GET_CODE (XEXP (op, 0)) == POST_INC))
1609 win = 1;
1610 break;
1612 case 'E':
1613 case 'F':
1614 if (GET_CODE (op) == CONST_DOUBLE
1615 || (GET_CODE (op) == CONST_VECTOR
1616 && (GET_MODE_CLASS (GET_MODE (op))
1617 == MODE_VECTOR_FLOAT)))
1618 win = 1;
1619 break;
1621 case 'G':
1622 case 'H':
1623 if (GET_CODE (op) == CONST_DOUBLE
1624 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
1625 win = 1;
1626 break;
1628 case 's':
1629 if (GET_CODE (op) == CONST_INT
1630 || (GET_CODE (op) == CONST_DOUBLE
1631 && GET_MODE (op) == VOIDmode))
1632 break;
1633 case 'i':
1634 if (CONSTANT_P (op)
1635 #ifdef LEGITIMATE_PIC_OPERAND_P
1636 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))
1637 #endif
1639 win = 1;
1640 break;
1642 case 'n':
1643 if (GET_CODE (op) == CONST_INT
1644 || (GET_CODE (op) == CONST_DOUBLE
1645 && GET_MODE (op) == VOIDmode))
1646 win = 1;
1647 break;
1649 case 'I':
1650 case 'J':
1651 case 'K':
1652 case 'L':
1653 case 'M':
1654 case 'N':
1655 case 'O':
1656 case 'P':
1657 if (GET_CODE (op) == CONST_INT
1658 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
1659 win = 1;
1660 break;
1662 case 'X':
1663 win = 1;
1664 break;
1666 case 'g':
1667 if (GET_CODE (op) == MEM
1668 || (CONSTANT_P (op)
1669 #ifdef LEGITIMATE_PIC_OPERAND_P
1670 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))
1671 #endif
1673 win = 1;
1674 allows_mem[i] = 1;
1675 case 'r':
1676 classes[i]
1677 = reg_class_subunion[(int) classes[i]][(int) GENERAL_REGS];
1678 break;
1680 default:
1681 if (REG_CLASS_FROM_CONSTRAINT (c, p) != NO_REGS)
1682 classes[i]
1683 = reg_class_subunion[(int) classes[i]]
1684 [(int) REG_CLASS_FROM_CONSTRAINT (c, p)];
1685 #ifdef EXTRA_CONSTRAINT_STR
1686 else if (EXTRA_CONSTRAINT_STR (op, c, p))
1687 win = 1;
1689 if (EXTRA_MEMORY_CONSTRAINT (c, p))
1691 /* Every MEM can be reloaded to fit. */
1692 allows_mem[i] = 1;
1693 if (GET_CODE (op) == MEM)
1694 win = 1;
1696 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1698 /* Every address can be reloaded to fit. */
1699 allows_addr = 1;
1700 if (address_operand (op, GET_MODE (op)))
1701 win = 1;
1702 /* We know this operand is an address, so we want it to
1703 be allocated to a register that can be the base of an
1704 address, ie BASE_REG_CLASS. */
1705 classes[i]
1706 = reg_class_subunion[(int) classes[i]]
1707 [(int) MODE_BASE_REG_CLASS (VOIDmode)];
1709 #endif
1710 break;
1712 p += CONSTRAINT_LEN (c, p);
1713 if (c == ',')
1714 break;
1717 constraints[i] = p;
1719 /* How we account for this operand now depends on whether it is a
1720 pseudo register or not. If it is, we first check if any
1721 register classes are valid. If not, we ignore this alternative,
1722 since we want to assume that all pseudos get allocated for
1723 register preferencing. If some register class is valid, compute
1724 the costs of moving the pseudo into that class. */
1726 if (GET_CODE (op) == REG && REGNO (op) >= FIRST_PSEUDO_REGISTER)
1728 if (classes[i] == NO_REGS)
1730 /* We must always fail if the operand is a REG, but
1731 we did not find a suitable class.
1733 Otherwise we may perform an uninitialized read
1734 from this_op_costs after the `continue' statement
1735 below. */
1736 alt_fail = 1;
1738 else
1740 struct costs *pp = &this_op_costs[i];
1742 for (class = 0; class < N_REG_CLASSES; class++)
1743 pp->cost[class]
1744 = ((recog_data.operand_type[i] != OP_OUT
1745 ? may_move_in_cost[mode][class][(int) classes[i]]
1746 : 0)
1747 + (recog_data.operand_type[i] != OP_IN
1748 ? may_move_out_cost[mode][(int) classes[i]][class]
1749 : 0));
1751 /* If the alternative actually allows memory, make things
1752 a bit cheaper since we won't need an extra insn to
1753 load it. */
1755 pp->mem_cost
1756 = ((recog_data.operand_type[i] != OP_IN
1757 ? MEMORY_MOVE_COST (mode, classes[i], 0)
1758 : 0)
1759 + (recog_data.operand_type[i] != OP_OUT
1760 ? MEMORY_MOVE_COST (mode, classes[i], 1)
1761 : 0) - allows_mem[i]);
1763 /* If we have assigned a class to this register in our
1764 first pass, add a cost to this alternative corresponding
1765 to what we would add if this register were not in the
1766 appropriate class. */
1768 if (reg_pref)
1769 alt_cost
1770 += (may_move_in_cost[mode]
1771 [(unsigned char) reg_pref[REGNO (op)].prefclass]
1772 [(int) classes[i]]);
1776 /* Otherwise, if this alternative wins, either because we
1777 have already determined that or if we have a hard register of
1778 the proper class, there is no cost for this alternative. */
1780 else if (win
1781 || (GET_CODE (op) == REG
1782 && reg_fits_class_p (op, classes[i], 0, GET_MODE (op))))
1785 /* If registers are valid, the cost of this alternative includes
1786 copying the object to and/or from a register. */
1788 else if (classes[i] != NO_REGS)
1790 if (recog_data.operand_type[i] != OP_OUT)
1791 alt_cost += copy_cost (op, mode, classes[i], 1);
1793 if (recog_data.operand_type[i] != OP_IN)
1794 alt_cost += copy_cost (op, mode, classes[i], 0);
1797 /* The only other way this alternative can be used is if this is a
1798 constant that could be placed into memory. */
1800 else if (CONSTANT_P (op) && (allows_addr || allows_mem[i]))
1801 alt_cost += MEMORY_MOVE_COST (mode, classes[i], 1);
1802 else
1803 alt_fail = 1;
1806 if (alt_fail)
1807 continue;
1809 /* Finally, update the costs with the information we've calculated
1810 about this alternative. */
1812 for (i = 0; i < n_ops; i++)
1813 if (GET_CODE (ops[i]) == REG
1814 && REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
1816 struct costs *pp = &op_costs[i], *qq = &this_op_costs[i];
1817 int scale = 1 + (recog_data.operand_type[i] == OP_INOUT);
1819 pp->mem_cost = MIN (pp->mem_cost,
1820 (qq->mem_cost + alt_cost) * scale);
1822 for (class = 0; class < N_REG_CLASSES; class++)
1823 pp->cost[class] = MIN (pp->cost[class],
1824 (qq->cost[class] + alt_cost) * scale);
1828 /* If this insn is a single set copying operand 1 to operand 0
1829 and one operand is a pseudo with the other a hard reg or a pseudo
1830 that prefers a register that is in its own register class then
1831 we may want to adjust the cost of that register class to -1.
1833 Avoid the adjustment if the source does not die to avoid stressing of
1834 register allocator by preferrencing two coliding registers into single
1835 class.
1837 Also avoid the adjustment if a copy between registers of the class
1838 is expensive (ten times the cost of a default copy is considered
1839 arbitrarily expensive). This avoids losing when the preferred class
1840 is very expensive as the source of a copy instruction. */
1842 if ((set = single_set (insn)) != 0
1843 && ops[0] == SET_DEST (set) && ops[1] == SET_SRC (set)
1844 && GET_CODE (ops[0]) == REG && GET_CODE (ops[1]) == REG
1845 && find_regno_note (insn, REG_DEAD, REGNO (ops[1])))
1846 for (i = 0; i <= 1; i++)
1847 if (REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
1849 unsigned int regno = REGNO (ops[!i]);
1850 enum machine_mode mode = GET_MODE (ops[!i]);
1851 int class;
1852 unsigned int nr;
1854 if (regno >= FIRST_PSEUDO_REGISTER && reg_pref != 0)
1856 enum reg_class pref = reg_pref[regno].prefclass;
1858 if ((reg_class_size[(unsigned char) pref]
1859 == (unsigned) CLASS_MAX_NREGS (pref, mode))
1860 && REGISTER_MOVE_COST (mode, pref, pref) < 10 * 2)
1861 op_costs[i].cost[(unsigned char) pref] = -1;
1863 else if (regno < FIRST_PSEUDO_REGISTER)
1864 for (class = 0; class < N_REG_CLASSES; class++)
1865 if (TEST_HARD_REG_BIT (reg_class_contents[class], regno)
1866 && reg_class_size[class] == (unsigned) CLASS_MAX_NREGS (class, mode))
1868 if (reg_class_size[class] == 1)
1869 op_costs[i].cost[class] = -1;
1870 else
1872 for (nr = 0; nr < (unsigned) HARD_REGNO_NREGS (regno, mode); nr++)
1874 if (! TEST_HARD_REG_BIT (reg_class_contents[class],
1875 regno + nr))
1876 break;
1879 if (nr == (unsigned) HARD_REGNO_NREGS (regno,mode))
1880 op_costs[i].cost[class] = -1;
1886 /* Compute the cost of loading X into (if TO_P is nonzero) or from (if
1887 TO_P is zero) a register of class CLASS in mode MODE.
1889 X must not be a pseudo. */
1891 static int
1892 copy_cost (x, mode, class, to_p)
1893 rtx x;
1894 enum machine_mode mode ATTRIBUTE_UNUSED;
1895 enum reg_class class;
1896 int to_p ATTRIBUTE_UNUSED;
1898 #ifdef HAVE_SECONDARY_RELOADS
1899 enum reg_class secondary_class = NO_REGS;
1900 #endif
1902 /* If X is a SCRATCH, there is actually nothing to move since we are
1903 assuming optimal allocation. */
1905 if (GET_CODE (x) == SCRATCH)
1906 return 0;
1908 /* Get the class we will actually use for a reload. */
1909 class = PREFERRED_RELOAD_CLASS (x, class);
1911 #ifdef HAVE_SECONDARY_RELOADS
1912 /* If we need a secondary reload (we assume here that we are using
1913 the secondary reload as an intermediate, not a scratch register), the
1914 cost is that to load the input into the intermediate register, then
1915 to copy them. We use a special value of TO_P to avoid recursion. */
1917 #ifdef SECONDARY_INPUT_RELOAD_CLASS
1918 if (to_p == 1)
1919 secondary_class = SECONDARY_INPUT_RELOAD_CLASS (class, mode, x);
1920 #endif
1922 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
1923 if (! to_p)
1924 secondary_class = SECONDARY_OUTPUT_RELOAD_CLASS (class, mode, x);
1925 #endif
1927 if (secondary_class != NO_REGS)
1928 return (move_cost[mode][(int) secondary_class][(int) class]
1929 + copy_cost (x, mode, secondary_class, 2));
1930 #endif /* HAVE_SECONDARY_RELOADS */
1932 /* For memory, use the memory move cost, for (hard) registers, use the
1933 cost to move between the register classes, and use 2 for everything
1934 else (constants). */
1936 if (GET_CODE (x) == MEM || class == NO_REGS)
1937 return MEMORY_MOVE_COST (mode, class, to_p);
1939 else if (GET_CODE (x) == REG)
1940 return move_cost[mode][(int) REGNO_REG_CLASS (REGNO (x))][(int) class];
1942 else
1943 /* If this is a constant, we may eventually want to call rtx_cost here. */
1944 return COSTS_N_INSNS (1);
1947 /* Record the pseudo registers we must reload into hard registers
1948 in a subexpression of a memory address, X.
1950 CLASS is the class that the register needs to be in and is either
1951 BASE_REG_CLASS or INDEX_REG_CLASS.
1953 SCALE is twice the amount to multiply the cost by (it is twice so we
1954 can represent half-cost adjustments). */
1956 static void
1957 record_address_regs (x, class, scale)
1958 rtx x;
1959 enum reg_class class;
1960 int scale;
1962 enum rtx_code code = GET_CODE (x);
1964 switch (code)
1966 case CONST_INT:
1967 case CONST:
1968 case CC0:
1969 case PC:
1970 case SYMBOL_REF:
1971 case LABEL_REF:
1972 return;
1974 case PLUS:
1975 /* When we have an address that is a sum,
1976 we must determine whether registers are "base" or "index" regs.
1977 If there is a sum of two registers, we must choose one to be
1978 the "base". Luckily, we can use the REG_POINTER to make a good
1979 choice most of the time. We only need to do this on machines
1980 that can have two registers in an address and where the base
1981 and index register classes are different.
1983 ??? This code used to set REGNO_POINTER_FLAG in some cases, but
1984 that seems bogus since it should only be set when we are sure
1985 the register is being used as a pointer. */
1988 rtx arg0 = XEXP (x, 0);
1989 rtx arg1 = XEXP (x, 1);
1990 enum rtx_code code0 = GET_CODE (arg0);
1991 enum rtx_code code1 = GET_CODE (arg1);
1993 /* Look inside subregs. */
1994 if (code0 == SUBREG)
1995 arg0 = SUBREG_REG (arg0), code0 = GET_CODE (arg0);
1996 if (code1 == SUBREG)
1997 arg1 = SUBREG_REG (arg1), code1 = GET_CODE (arg1);
1999 /* If this machine only allows one register per address, it must
2000 be in the first operand. */
2002 if (MAX_REGS_PER_ADDRESS == 1)
2003 record_address_regs (arg0, class, scale);
2005 /* If index and base registers are the same on this machine, just
2006 record registers in any non-constant operands. We assume here,
2007 as well as in the tests below, that all addresses are in
2008 canonical form. */
2010 else if (INDEX_REG_CLASS == MODE_BASE_REG_CLASS (VOIDmode))
2012 record_address_regs (arg0, class, scale);
2013 if (! CONSTANT_P (arg1))
2014 record_address_regs (arg1, class, scale);
2017 /* If the second operand is a constant integer, it doesn't change
2018 what class the first operand must be. */
2020 else if (code1 == CONST_INT || code1 == CONST_DOUBLE)
2021 record_address_regs (arg0, class, scale);
2023 /* If the second operand is a symbolic constant, the first operand
2024 must be an index register. */
2026 else if (code1 == SYMBOL_REF || code1 == CONST || code1 == LABEL_REF)
2027 record_address_regs (arg0, INDEX_REG_CLASS, scale);
2029 /* If both operands are registers but one is already a hard register
2030 of index or base class, give the other the class that the hard
2031 register is not. */
2033 #ifdef REG_OK_FOR_BASE_P
2034 else if (code0 == REG && code1 == REG
2035 && REGNO (arg0) < FIRST_PSEUDO_REGISTER
2036 && (REG_OK_FOR_BASE_P (arg0) || REG_OK_FOR_INDEX_P (arg0)))
2037 record_address_regs (arg1,
2038 REG_OK_FOR_BASE_P (arg0)
2039 ? INDEX_REG_CLASS : MODE_BASE_REG_CLASS (VOIDmode),
2040 scale);
2041 else if (code0 == REG && code1 == REG
2042 && REGNO (arg1) < FIRST_PSEUDO_REGISTER
2043 && (REG_OK_FOR_BASE_P (arg1) || REG_OK_FOR_INDEX_P (arg1)))
2044 record_address_regs (arg0,
2045 REG_OK_FOR_BASE_P (arg1)
2046 ? INDEX_REG_CLASS : MODE_BASE_REG_CLASS (VOIDmode),
2047 scale);
2048 #endif
2050 /* If one operand is known to be a pointer, it must be the base
2051 with the other operand the index. Likewise if the other operand
2052 is a MULT. */
2054 else if ((code0 == REG && REG_POINTER (arg0))
2055 || code1 == MULT)
2057 record_address_regs (arg0, MODE_BASE_REG_CLASS (VOIDmode), scale);
2058 record_address_regs (arg1, INDEX_REG_CLASS, scale);
2060 else if ((code1 == REG && REG_POINTER (arg1))
2061 || code0 == MULT)
2063 record_address_regs (arg0, INDEX_REG_CLASS, scale);
2064 record_address_regs (arg1, MODE_BASE_REG_CLASS (VOIDmode), scale);
2067 /* Otherwise, count equal chances that each might be a base
2068 or index register. This case should be rare. */
2070 else
2072 record_address_regs (arg0, MODE_BASE_REG_CLASS (VOIDmode),
2073 scale / 2);
2074 record_address_regs (arg0, INDEX_REG_CLASS, scale / 2);
2075 record_address_regs (arg1, MODE_BASE_REG_CLASS (VOIDmode),
2076 scale / 2);
2077 record_address_regs (arg1, INDEX_REG_CLASS, scale / 2);
2080 break;
2082 /* Double the importance of a pseudo register that is incremented
2083 or decremented, since it would take two extra insns
2084 if it ends up in the wrong place. */
2085 case POST_MODIFY:
2086 case PRE_MODIFY:
2087 record_address_regs (XEXP (x, 0), MODE_BASE_REG_CLASS (VOIDmode),
2088 2 * scale);
2089 if (REG_P (XEXP (XEXP (x, 1), 1)))
2090 record_address_regs (XEXP (XEXP (x, 1), 1),
2091 INDEX_REG_CLASS, 2 * scale);
2092 break;
2094 case POST_INC:
2095 case PRE_INC:
2096 case POST_DEC:
2097 case PRE_DEC:
2098 /* Double the importance of a pseudo register that is incremented
2099 or decremented, since it would take two extra insns
2100 if it ends up in the wrong place. If the operand is a pseudo,
2101 show it is being used in an INC_DEC context. */
2103 #ifdef FORBIDDEN_INC_DEC_CLASSES
2104 if (GET_CODE (XEXP (x, 0)) == REG
2105 && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER)
2106 in_inc_dec[REGNO (XEXP (x, 0))] = 1;
2107 #endif
2109 record_address_regs (XEXP (x, 0), class, 2 * scale);
2110 break;
2112 case REG:
2114 struct costs *pp = &costs[REGNO (x)];
2115 int i;
2117 pp->mem_cost += (MEMORY_MOVE_COST (Pmode, class, 1) * scale) / 2;
2119 for (i = 0; i < N_REG_CLASSES; i++)
2120 pp->cost[i] += (may_move_in_cost[Pmode][i][(int) class] * scale) / 2;
2122 break;
2124 default:
2126 const char *fmt = GET_RTX_FORMAT (code);
2127 int i;
2128 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2129 if (fmt[i] == 'e')
2130 record_address_regs (XEXP (x, i), class, scale);
2135 #ifdef FORBIDDEN_INC_DEC_CLASSES
2137 /* Return 1 if REG is valid as an auto-increment memory reference
2138 to an object of MODE. */
2140 static int
2141 auto_inc_dec_reg_p (reg, mode)
2142 rtx reg;
2143 enum machine_mode mode;
2145 if (HAVE_POST_INCREMENT
2146 && memory_address_p (mode, gen_rtx_POST_INC (Pmode, reg)))
2147 return 1;
2149 if (HAVE_POST_DECREMENT
2150 && memory_address_p (mode, gen_rtx_POST_DEC (Pmode, reg)))
2151 return 1;
2153 if (HAVE_PRE_INCREMENT
2154 && memory_address_p (mode, gen_rtx_PRE_INC (Pmode, reg)))
2155 return 1;
2157 if (HAVE_PRE_DECREMENT
2158 && memory_address_p (mode, gen_rtx_PRE_DEC (Pmode, reg)))
2159 return 1;
2161 return 0;
2163 #endif
2165 static short *renumber;
2166 static size_t regno_allocated;
2167 static unsigned int reg_n_max;
2169 /* Allocate enough space to hold NUM_REGS registers for the tables used for
2170 reg_scan and flow_analysis that are indexed by the register number. If
2171 NEW_P is nonzero, initialize all of the registers, otherwise only
2172 initialize the new registers allocated. The same table is kept from
2173 function to function, only reallocating it when we need more room. If
2174 RENUMBER_P is nonzero, allocate the reg_renumber array also. */
2176 void
2177 allocate_reg_info (num_regs, new_p, renumber_p)
2178 size_t num_regs;
2179 int new_p;
2180 int renumber_p;
2182 size_t size_info;
2183 size_t size_renumber;
2184 size_t min = (new_p) ? 0 : reg_n_max;
2185 struct reg_info_data *reg_data;
2187 if (num_regs > regno_allocated)
2189 size_t old_allocated = regno_allocated;
2191 regno_allocated = num_regs + (num_regs / 20); /* add some slop space */
2192 size_renumber = regno_allocated * sizeof (short);
2194 if (!reg_n_info)
2196 VARRAY_REG_INIT (reg_n_info, regno_allocated, "reg_n_info");
2197 renumber = (short *) xmalloc (size_renumber);
2198 reg_pref_buffer = (struct reg_pref *) xmalloc (regno_allocated
2199 * sizeof (struct reg_pref));
2202 else
2204 VARRAY_GROW (reg_n_info, regno_allocated);
2206 if (new_p) /* if we're zapping everything, no need to realloc */
2208 free ((char *) renumber);
2209 free ((char *) reg_pref);
2210 renumber = (short *) xmalloc (size_renumber);
2211 reg_pref_buffer = (struct reg_pref *) xmalloc (regno_allocated
2212 * sizeof (struct reg_pref));
2215 else
2217 renumber = (short *) xrealloc ((char *) renumber, size_renumber);
2218 reg_pref_buffer = (struct reg_pref *) xrealloc ((char *) reg_pref_buffer,
2219 regno_allocated
2220 * sizeof (struct reg_pref));
2224 size_info = (regno_allocated - old_allocated) * sizeof (reg_info)
2225 + sizeof (struct reg_info_data) - sizeof (reg_info);
2226 reg_data = (struct reg_info_data *) xcalloc (size_info, 1);
2227 reg_data->min_index = old_allocated;
2228 reg_data->max_index = regno_allocated - 1;
2229 reg_data->next = reg_info_head;
2230 reg_info_head = reg_data;
2233 reg_n_max = num_regs;
2234 if (min < num_regs)
2236 /* Loop through each of the segments allocated for the actual
2237 reg_info pages, and set up the pointers, zero the pages, etc. */
2238 for (reg_data = reg_info_head;
2239 reg_data && reg_data->max_index >= min;
2240 reg_data = reg_data->next)
2242 size_t min_index = reg_data->min_index;
2243 size_t max_index = reg_data->max_index;
2244 size_t max = MIN (max_index, num_regs);
2245 size_t local_min = min - min_index;
2246 size_t i;
2248 if (reg_data->min_index > num_regs)
2249 continue;
2251 if (min < min_index)
2252 local_min = 0;
2253 if (!reg_data->used_p) /* page just allocated with calloc */
2254 reg_data->used_p = 1; /* no need to zero */
2255 else
2256 memset ((char *) &reg_data->data[local_min], 0,
2257 sizeof (reg_info) * (max - min_index - local_min + 1));
2259 for (i = min_index+local_min; i <= max; i++)
2261 VARRAY_REG (reg_n_info, i) = &reg_data->data[i-min_index];
2262 REG_BASIC_BLOCK (i) = REG_BLOCK_UNKNOWN;
2263 renumber[i] = -1;
2264 reg_pref_buffer[i].prefclass = (char) NO_REGS;
2265 reg_pref_buffer[i].altclass = (char) NO_REGS;
2270 /* If {pref,alt}class have already been allocated, update the pointers to
2271 the newly realloced ones. */
2272 if (reg_pref)
2273 reg_pref = reg_pref_buffer;
2275 if (renumber_p)
2276 reg_renumber = renumber;
2278 /* Tell the regset code about the new number of registers. */
2279 MAX_REGNO_REG_SET (num_regs, new_p, renumber_p);
2282 /* Free up the space allocated by allocate_reg_info. */
2283 void
2284 free_reg_info ()
2286 if (reg_n_info)
2288 struct reg_info_data *reg_data;
2289 struct reg_info_data *reg_next;
2291 VARRAY_FREE (reg_n_info);
2292 for (reg_data = reg_info_head; reg_data; reg_data = reg_next)
2294 reg_next = reg_data->next;
2295 free ((char *) reg_data);
2298 free (reg_pref_buffer);
2299 reg_pref_buffer = (struct reg_pref *) 0;
2300 reg_info_head = (struct reg_info_data *) 0;
2301 renumber = (short *) 0;
2303 regno_allocated = 0;
2304 reg_n_max = 0;
2307 /* This is the `regscan' pass of the compiler, run just before cse
2308 and again just before loop.
2310 It finds the first and last use of each pseudo-register
2311 and records them in the vectors regno_first_uid, regno_last_uid
2312 and counts the number of sets in the vector reg_n_sets.
2314 REPEAT is nonzero the second time this is called. */
2316 /* Maximum number of parallel sets and clobbers in any insn in this fn.
2317 Always at least 3, since the combiner could put that many together
2318 and we want this to remain correct for all the remaining passes.
2319 This corresponds to the maximum number of times note_stores will call
2320 a function for any insn. */
2322 int max_parallel;
2324 /* Used as a temporary to record the largest number of registers in
2325 PARALLEL in a SET_DEST. This is added to max_parallel. */
2327 static int max_set_parallel;
2329 void
2330 reg_scan (f, nregs, repeat)
2331 rtx f;
2332 unsigned int nregs;
2333 int repeat ATTRIBUTE_UNUSED;
2335 rtx insn;
2337 allocate_reg_info (nregs, TRUE, FALSE);
2338 max_parallel = 3;
2339 max_set_parallel = 0;
2341 for (insn = f; insn; insn = NEXT_INSN (insn))
2342 if (GET_CODE (insn) == INSN
2343 || GET_CODE (insn) == CALL_INSN
2344 || GET_CODE (insn) == JUMP_INSN)
2346 if (GET_CODE (PATTERN (insn)) == PARALLEL
2347 && XVECLEN (PATTERN (insn), 0) > max_parallel)
2348 max_parallel = XVECLEN (PATTERN (insn), 0);
2349 reg_scan_mark_refs (PATTERN (insn), insn, 0, 0);
2351 if (REG_NOTES (insn))
2352 reg_scan_mark_refs (REG_NOTES (insn), insn, 1, 0);
2355 max_parallel += max_set_parallel;
2358 /* Update 'regscan' information by looking at the insns
2359 from FIRST to LAST. Some new REGs have been created,
2360 and any REG with number greater than OLD_MAX_REGNO is
2361 such a REG. We only update information for those. */
2363 void
2364 reg_scan_update (first, last, old_max_regno)
2365 rtx first;
2366 rtx last;
2367 unsigned int old_max_regno;
2369 rtx insn;
2371 allocate_reg_info (max_reg_num (), FALSE, FALSE);
2373 for (insn = first; insn != last; insn = NEXT_INSN (insn))
2374 if (GET_CODE (insn) == INSN
2375 || GET_CODE (insn) == CALL_INSN
2376 || GET_CODE (insn) == JUMP_INSN)
2378 if (GET_CODE (PATTERN (insn)) == PARALLEL
2379 && XVECLEN (PATTERN (insn), 0) > max_parallel)
2380 max_parallel = XVECLEN (PATTERN (insn), 0);
2381 reg_scan_mark_refs (PATTERN (insn), insn, 0, old_max_regno);
2383 if (REG_NOTES (insn))
2384 reg_scan_mark_refs (REG_NOTES (insn), insn, 1, old_max_regno);
2388 /* X is the expression to scan. INSN is the insn it appears in.
2389 NOTE_FLAG is nonzero if X is from INSN's notes rather than its body.
2390 We should only record information for REGs with numbers
2391 greater than or equal to MIN_REGNO. */
2393 static void
2394 reg_scan_mark_refs (x, insn, note_flag, min_regno)
2395 rtx x;
2396 rtx insn;
2397 int note_flag;
2398 unsigned int min_regno;
2400 enum rtx_code code;
2401 rtx dest;
2402 rtx note;
2404 if (!x)
2405 return;
2406 code = GET_CODE (x);
2407 switch (code)
2409 case CONST:
2410 case CONST_INT:
2411 case CONST_DOUBLE:
2412 case CONST_VECTOR:
2413 case CC0:
2414 case PC:
2415 case SYMBOL_REF:
2416 case LABEL_REF:
2417 case ADDR_VEC:
2418 case ADDR_DIFF_VEC:
2419 return;
2421 case REG:
2423 unsigned int regno = REGNO (x);
2425 if (regno >= min_regno)
2427 REGNO_LAST_NOTE_UID (regno) = INSN_UID (insn);
2428 if (!note_flag)
2429 REGNO_LAST_UID (regno) = INSN_UID (insn);
2430 if (REGNO_FIRST_UID (regno) == 0)
2431 REGNO_FIRST_UID (regno) = INSN_UID (insn);
2432 /* If we are called by reg_scan_update() (indicated by min_regno
2433 being set), we also need to update the reference count. */
2434 if (min_regno)
2435 REG_N_REFS (regno)++;
2438 break;
2440 case EXPR_LIST:
2441 if (XEXP (x, 0))
2442 reg_scan_mark_refs (XEXP (x, 0), insn, note_flag, min_regno);
2443 if (XEXP (x, 1))
2444 reg_scan_mark_refs (XEXP (x, 1), insn, note_flag, min_regno);
2445 break;
2447 case INSN_LIST:
2448 if (XEXP (x, 1))
2449 reg_scan_mark_refs (XEXP (x, 1), insn, note_flag, min_regno);
2450 break;
2452 case CLOBBER:
2454 rtx reg = XEXP (x, 0);
2455 if (REG_P (reg)
2456 && REGNO (reg) >= min_regno)
2458 REG_N_SETS (REGNO (reg))++;
2459 REG_N_REFS (REGNO (reg))++;
2462 break;
2464 case SET:
2465 /* Count a set of the destination if it is a register. */
2466 for (dest = SET_DEST (x);
2467 GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
2468 || GET_CODE (dest) == ZERO_EXTEND;
2469 dest = XEXP (dest, 0))
2472 /* For a PARALLEL, record the number of things (less the usual one for a
2473 SET) that are set. */
2474 if (GET_CODE (dest) == PARALLEL)
2475 max_set_parallel = MAX (max_set_parallel, XVECLEN (dest, 0) - 1);
2477 if (GET_CODE (dest) == REG
2478 && REGNO (dest) >= min_regno)
2480 REG_N_SETS (REGNO (dest))++;
2481 REG_N_REFS (REGNO (dest))++;
2484 /* If this is setting a pseudo from another pseudo or the sum of a
2485 pseudo and a constant integer and the other pseudo is known to be
2486 a pointer, set the destination to be a pointer as well.
2488 Likewise if it is setting the destination from an address or from a
2489 value equivalent to an address or to the sum of an address and
2490 something else.
2492 But don't do any of this if the pseudo corresponds to a user
2493 variable since it should have already been set as a pointer based
2494 on the type. */
2496 if (GET_CODE (SET_DEST (x)) == REG
2497 && REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER
2498 && REGNO (SET_DEST (x)) >= min_regno
2499 /* If the destination pseudo is set more than once, then other
2500 sets might not be to a pointer value (consider access to a
2501 union in two threads of control in the presence of global
2502 optimizations). So only set REG_POINTER on the destination
2503 pseudo if this is the only set of that pseudo. */
2504 && REG_N_SETS (REGNO (SET_DEST (x))) == 1
2505 && ! REG_USERVAR_P (SET_DEST (x))
2506 && ! REG_POINTER (SET_DEST (x))
2507 && ((GET_CODE (SET_SRC (x)) == REG
2508 && REG_POINTER (SET_SRC (x)))
2509 || ((GET_CODE (SET_SRC (x)) == PLUS
2510 || GET_CODE (SET_SRC (x)) == LO_SUM)
2511 && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2512 && GET_CODE (XEXP (SET_SRC (x), 0)) == REG
2513 && REG_POINTER (XEXP (SET_SRC (x), 0)))
2514 || GET_CODE (SET_SRC (x)) == CONST
2515 || GET_CODE (SET_SRC (x)) == SYMBOL_REF
2516 || GET_CODE (SET_SRC (x)) == LABEL_REF
2517 || (GET_CODE (SET_SRC (x)) == HIGH
2518 && (GET_CODE (XEXP (SET_SRC (x), 0)) == CONST
2519 || GET_CODE (XEXP (SET_SRC (x), 0)) == SYMBOL_REF
2520 || GET_CODE (XEXP (SET_SRC (x), 0)) == LABEL_REF))
2521 || ((GET_CODE (SET_SRC (x)) == PLUS
2522 || GET_CODE (SET_SRC (x)) == LO_SUM)
2523 && (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST
2524 || GET_CODE (XEXP (SET_SRC (x), 1)) == SYMBOL_REF
2525 || GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF))
2526 || ((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
2527 && (GET_CODE (XEXP (note, 0)) == CONST
2528 || GET_CODE (XEXP (note, 0)) == SYMBOL_REF
2529 || GET_CODE (XEXP (note, 0)) == LABEL_REF))))
2530 REG_POINTER (SET_DEST (x)) = 1;
2532 /* If this is setting a register from a register or from a simple
2533 conversion of a register, propagate REG_EXPR. */
2534 if (GET_CODE (dest) == REG)
2536 rtx src = SET_SRC (x);
2538 while (GET_CODE (src) == SIGN_EXTEND
2539 || GET_CODE (src) == ZERO_EXTEND
2540 || GET_CODE (src) == TRUNCATE
2541 || (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)))
2542 src = XEXP (src, 0);
2544 if (!REG_ATTRS (dest) && REG_P (src))
2545 REG_ATTRS (dest) = REG_ATTRS (src);
2546 if (!REG_ATTRS (dest) && GET_CODE (src) == MEM)
2547 set_reg_attrs_from_mem (dest, src);
2550 /* ... fall through ... */
2552 default:
2554 const char *fmt = GET_RTX_FORMAT (code);
2555 int i;
2556 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2558 if (fmt[i] == 'e')
2559 reg_scan_mark_refs (XEXP (x, i), insn, note_flag, min_regno);
2560 else if (fmt[i] == 'E' && XVEC (x, i) != 0)
2562 int j;
2563 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2564 reg_scan_mark_refs (XVECEXP (x, i, j), insn, note_flag, min_regno);
2571 /* Return nonzero if C1 is a subset of C2, i.e., if every register in C1
2572 is also in C2. */
2575 reg_class_subset_p (c1, c2)
2576 enum reg_class c1;
2577 enum reg_class c2;
2579 if (c1 == c2) return 1;
2581 if (c2 == ALL_REGS)
2582 win:
2583 return 1;
2584 GO_IF_HARD_REG_SUBSET (reg_class_contents[(int) c1],
2585 reg_class_contents[(int) c2],
2586 win);
2587 return 0;
2590 /* Return nonzero if there is a register that is in both C1 and C2. */
2593 reg_classes_intersect_p (c1, c2)
2594 enum reg_class c1;
2595 enum reg_class c2;
2597 #ifdef HARD_REG_SET
2598 register
2599 #endif
2600 HARD_REG_SET c;
2602 if (c1 == c2) return 1;
2604 if (c1 == ALL_REGS || c2 == ALL_REGS)
2605 return 1;
2607 COPY_HARD_REG_SET (c, reg_class_contents[(int) c1]);
2608 AND_HARD_REG_SET (c, reg_class_contents[(int) c2]);
2610 GO_IF_HARD_REG_SUBSET (c, reg_class_contents[(int) NO_REGS], lose);
2611 return 1;
2613 lose:
2614 return 0;
2617 /* Release any memory allocated by register sets. */
2619 void
2620 regset_release_memory ()
2622 bitmap_release_memory ();
2625 #ifdef CANNOT_CHANGE_MODE_CLASS
2626 /* Set bits in *USED which correspond to registers which can't change
2627 their mode from FROM to any mode in which REGNO was encountered. */
2629 void
2630 cannot_change_mode_set_regs (used, from, regno)
2631 HARD_REG_SET *used;
2632 enum machine_mode from;
2633 unsigned int regno;
2635 enum machine_mode to;
2636 enum reg_class class;
2638 for (to = VOIDmode; to < MAX_MACHINE_MODE; ++to)
2639 if (REGNO_REG_SET_P (&subregs_of_mode[to], regno))
2641 class = CANNOT_CHANGE_MODE_CLASS (from, to);
2642 if (class != NO_REGS)
2643 IOR_HARD_REG_SET (*used, reg_class_contents [(int) class]);
2647 /* Return 1 if REGNO has had an invalid mode change in CLASS from FROM
2648 mode. */
2650 bool
2651 invalid_mode_change_p (regno, class, from_mode)
2652 unsigned int regno;
2653 enum reg_class class;
2654 enum machine_mode from_mode;
2656 enum machine_mode to_mode;
2658 for (to_mode = 0; to_mode < NUM_MACHINE_MODES; ++to_mode)
2659 if (REGNO_REG_SET_P (&subregs_of_mode[(int) to_mode], regno)
2660 && reg_classes_intersect_p
2661 (class, CANNOT_CHANGE_MODE_CLASS (from_mode, to_mode)))
2662 return 1;
2663 return 0;
2665 #endif /* CANNOT_CHANGE_MODE_CLASS */
2667 #include "gt-regclass.h"