import of gcc-2.8
[official-gcc.git] / gcc / regclass.c
blobe307e03ff21b3ad9ee4de57b7012c4269b43ad1c
1 /* Compute register class preferences for pseudo-registers.
2 Copyright (C) 1987, 88, 91-96, 1997 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* This file contains two passes of the compiler: reg_scan and reg_class.
23 It also defines some tables of information about the hardware registers
24 and a function init_reg_sets to initialize the tables. */
26 #include "config.h"
27 #include <stdio.h>
28 #include "rtl.h"
29 #include "hard-reg-set.h"
30 #include "flags.h"
31 #include "basic-block.h"
32 #include "regs.h"
33 #include "insn-config.h"
34 #include "recog.h"
35 #include "reload.h"
36 #include "real.h"
37 #include "bytecode.h"
39 #ifndef REGISTER_MOVE_COST
40 #define REGISTER_MOVE_COST(x, y) 2
41 #endif
43 #ifndef MEMORY_MOVE_COST
44 #define MEMORY_MOVE_COST(x) 4
45 #endif
47 /* If we have auto-increment or auto-decrement and we can have secondary
48 reloads, we are not allowed to use classes requiring secondary
49 reloads for pseudos auto-incremented since reload can't handle it. */
51 #ifdef AUTO_INC_DEC
52 #if defined(SECONDARY_INPUT_RELOAD_CLASS) || defined(SECONDARY_OUTPUT_RELOAD_CLASS)
53 #define FORBIDDEN_INC_DEC_CLASSES
54 #endif
55 #endif
57 /* Register tables used by many passes. */
59 /* Indexed by hard register number, contains 1 for registers
60 that are fixed use (stack pointer, pc, frame pointer, etc.).
61 These are the registers that cannot be used to allocate
62 a pseudo reg whose life does not cross calls. */
64 char fixed_regs[FIRST_PSEUDO_REGISTER];
66 /* Same info as a HARD_REG_SET. */
68 HARD_REG_SET fixed_reg_set;
70 /* Data for initializing the above. */
72 static char initial_fixed_regs[] = FIXED_REGISTERS;
74 /* Indexed by hard register number, contains 1 for registers
75 that are fixed use or are clobbered by function calls.
76 These are the registers that cannot be used to allocate
77 a pseudo reg whose life crosses calls. */
79 char call_used_regs[FIRST_PSEUDO_REGISTER];
81 /* Same info as a HARD_REG_SET. */
83 HARD_REG_SET call_used_reg_set;
85 /* HARD_REG_SET of registers we want to avoid caller saving. */
86 HARD_REG_SET losing_caller_save_reg_set;
88 /* Data for initializing the above. */
90 static char initial_call_used_regs[] = CALL_USED_REGISTERS;
92 /* Indexed by hard register number, contains 1 for registers that are
93 fixed use -- i.e. in fixed_regs -- or a function value return register
94 or STRUCT_VALUE_REGNUM or STATIC_CHAIN_REGNUM. These are the
95 registers that cannot hold quantities across calls even if we are
96 willing to save and restore them. */
98 char call_fixed_regs[FIRST_PSEUDO_REGISTER];
100 /* The same info as a HARD_REG_SET. */
102 HARD_REG_SET call_fixed_reg_set;
104 /* Number of non-fixed registers. */
106 int n_non_fixed_regs;
108 /* Indexed by hard register number, contains 1 for registers
109 that are being used for global register decls.
110 These must be exempt from ordinary flow analysis
111 and are also considered fixed. */
113 char global_regs[FIRST_PSEUDO_REGISTER];
115 /* Table of register numbers in the order in which to try to use them. */
116 #ifdef REG_ALLOC_ORDER
117 int reg_alloc_order[FIRST_PSEUDO_REGISTER] = REG_ALLOC_ORDER;
118 #endif
120 /* For each reg class, a HARD_REG_SET saying which registers are in it. */
122 HARD_REG_SET reg_class_contents[N_REG_CLASSES];
124 /* The same information, but as an array of unsigned ints. We copy from
125 these unsigned ints to the table above. We do this so the tm.h files
126 do not have to be aware of the wordsize for machines with <= 64 regs. */
128 #define N_REG_INTS \
129 ((FIRST_PSEUDO_REGISTER + (HOST_BITS_PER_INT - 1)) / HOST_BITS_PER_INT)
131 static unsigned int_reg_class_contents[N_REG_CLASSES][N_REG_INTS]
132 = REG_CLASS_CONTENTS;
134 /* For each reg class, number of regs it contains. */
136 int reg_class_size[N_REG_CLASSES];
138 /* For each reg class, table listing all the containing classes. */
140 enum reg_class reg_class_superclasses[N_REG_CLASSES][N_REG_CLASSES];
142 /* For each reg class, table listing all the classes contained in it. */
144 enum reg_class reg_class_subclasses[N_REG_CLASSES][N_REG_CLASSES];
146 /* For each pair of reg classes,
147 a largest reg class contained in their union. */
149 enum reg_class reg_class_subunion[N_REG_CLASSES][N_REG_CLASSES];
151 /* For each pair of reg classes,
152 the smallest reg class containing their union. */
154 enum reg_class reg_class_superunion[N_REG_CLASSES][N_REG_CLASSES];
156 /* Array containing all of the register names */
158 char *reg_names[] = REGISTER_NAMES;
160 /* For each hard register, the widest mode object that it can contain.
161 This will be a MODE_INT mode if the register can hold integers. Otherwise
162 it will be a MODE_FLOAT or a MODE_CC mode, whichever is valid for the
163 register. */
165 enum machine_mode reg_raw_mode[FIRST_PSEUDO_REGISTER];
167 /* Maximum cost of moving from a register in one class to a register in
168 another class. Based on REGISTER_MOVE_COST. */
170 static int move_cost[N_REG_CLASSES][N_REG_CLASSES];
172 /* Similar, but here we don't have to move if the first index is a subset
173 of the second so in that case the cost is zero. */
175 static int may_move_cost[N_REG_CLASSES][N_REG_CLASSES];
177 #ifdef FORBIDDEN_INC_DEC_CLASSES
179 /* These are the classes that regs which are auto-incremented or decremented
180 cannot be put in. */
182 static int forbidden_inc_dec_class[N_REG_CLASSES];
184 /* Indexed by n, is non-zero if (REG n) is used in an auto-inc or auto-dec
185 context. */
187 static char *in_inc_dec;
189 #endif /* FORBIDDEN_INC_DEC_CLASSES */
191 /* Function called only once to initialize the above data on reg usage.
192 Once this is done, various switches may override. */
194 void
195 init_reg_sets ()
197 register int i, j;
199 /* First copy the register information from the initial int form into
200 the regsets. */
202 for (i = 0; i < N_REG_CLASSES; i++)
204 CLEAR_HARD_REG_SET (reg_class_contents[i]);
206 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
207 if (int_reg_class_contents[i][j / HOST_BITS_PER_INT]
208 & ((unsigned) 1 << (j % HOST_BITS_PER_INT)))
209 SET_HARD_REG_BIT (reg_class_contents[i], j);
212 bcopy (initial_fixed_regs, fixed_regs, sizeof fixed_regs);
213 bcopy (initial_call_used_regs, call_used_regs, sizeof call_used_regs);
214 bzero (global_regs, sizeof global_regs);
216 /* Compute number of hard regs in each class. */
218 bzero ((char *) reg_class_size, sizeof reg_class_size);
219 for (i = 0; i < N_REG_CLASSES; i++)
220 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
221 if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
222 reg_class_size[i]++;
224 /* Initialize the table of subunions.
225 reg_class_subunion[I][J] gets the largest-numbered reg-class
226 that is contained in the union of classes I and J. */
228 for (i = 0; i < N_REG_CLASSES; i++)
230 for (j = 0; j < N_REG_CLASSES; j++)
232 #ifdef HARD_REG_SET
233 register /* Declare it register if it's a scalar. */
234 #endif
235 HARD_REG_SET c;
236 register int k;
238 COPY_HARD_REG_SET (c, reg_class_contents[i]);
239 IOR_HARD_REG_SET (c, reg_class_contents[j]);
240 for (k = 0; k < N_REG_CLASSES; k++)
242 GO_IF_HARD_REG_SUBSET (reg_class_contents[k], c,
243 subclass1);
244 continue;
246 subclass1:
247 /* keep the largest subclass */ /* SPEE 900308 */
248 GO_IF_HARD_REG_SUBSET (reg_class_contents[k],
249 reg_class_contents[(int) reg_class_subunion[i][j]],
250 subclass2);
251 reg_class_subunion[i][j] = (enum reg_class) k;
252 subclass2:
258 /* Initialize the table of superunions.
259 reg_class_superunion[I][J] gets the smallest-numbered reg-class
260 containing the union of classes I and J. */
262 for (i = 0; i < N_REG_CLASSES; i++)
264 for (j = 0; j < N_REG_CLASSES; j++)
266 #ifdef HARD_REG_SET
267 register /* Declare it register if it's a scalar. */
268 #endif
269 HARD_REG_SET c;
270 register int k;
272 COPY_HARD_REG_SET (c, reg_class_contents[i]);
273 IOR_HARD_REG_SET (c, reg_class_contents[j]);
274 for (k = 0; k < N_REG_CLASSES; k++)
275 GO_IF_HARD_REG_SUBSET (c, reg_class_contents[k], superclass);
277 superclass:
278 reg_class_superunion[i][j] = (enum reg_class) k;
282 /* Initialize the tables of subclasses and superclasses of each reg class.
283 First clear the whole table, then add the elements as they are found. */
285 for (i = 0; i < N_REG_CLASSES; i++)
287 for (j = 0; j < N_REG_CLASSES; j++)
289 reg_class_superclasses[i][j] = LIM_REG_CLASSES;
290 reg_class_subclasses[i][j] = LIM_REG_CLASSES;
294 for (i = 0; i < N_REG_CLASSES; i++)
296 if (i == (int) NO_REGS)
297 continue;
299 for (j = i + 1; j < N_REG_CLASSES; j++)
301 enum reg_class *p;
303 GO_IF_HARD_REG_SUBSET (reg_class_contents[i], reg_class_contents[j],
304 subclass);
305 continue;
306 subclass:
307 /* Reg class I is a subclass of J.
308 Add J to the table of superclasses of I. */
309 p = &reg_class_superclasses[i][0];
310 while (*p != LIM_REG_CLASSES) p++;
311 *p = (enum reg_class) j;
312 /* Add I to the table of superclasses of J. */
313 p = &reg_class_subclasses[j][0];
314 while (*p != LIM_REG_CLASSES) p++;
315 *p = (enum reg_class) i;
319 /* Initialize the move cost table. Find every subset of each class
320 and take the maximum cost of moving any subset to any other. */
322 for (i = 0; i < N_REG_CLASSES; i++)
323 for (j = 0; j < N_REG_CLASSES; j++)
325 int cost = i == j ? 2 : REGISTER_MOVE_COST (i, j);
326 enum reg_class *p1, *p2;
328 for (p2 = &reg_class_subclasses[j][0]; *p2 != LIM_REG_CLASSES; p2++)
329 if (*p2 != i)
330 cost = MAX (cost, REGISTER_MOVE_COST (i, *p2));
332 for (p1 = &reg_class_subclasses[i][0]; *p1 != LIM_REG_CLASSES; p1++)
334 if (*p1 != j)
335 cost = MAX (cost, REGISTER_MOVE_COST (*p1, j));
337 for (p2 = &reg_class_subclasses[j][0];
338 *p2 != LIM_REG_CLASSES; p2++)
339 if (*p1 != *p2)
340 cost = MAX (cost, REGISTER_MOVE_COST (*p1, *p2));
343 move_cost[i][j] = cost;
345 if (reg_class_subset_p (i, j))
346 cost = 0;
348 may_move_cost[i][j] = cost;
351 /* Do any additional initialization regsets may need */
352 INIT_ONCE_REG_SET ();
355 /* After switches have been processed, which perhaps alter
356 `fixed_regs' and `call_used_regs', convert them to HARD_REG_SETs. */
358 static void
359 init_reg_sets_1 ()
361 register int i;
363 /* This macro allows the fixed or call-used registers
364 to depend on target flags. */
366 #ifdef CONDITIONAL_REGISTER_USAGE
367 CONDITIONAL_REGISTER_USAGE;
368 #endif
370 /* Initialize "constant" tables. */
372 CLEAR_HARD_REG_SET (fixed_reg_set);
373 CLEAR_HARD_REG_SET (call_used_reg_set);
374 CLEAR_HARD_REG_SET (call_fixed_reg_set);
376 bcopy (fixed_regs, call_fixed_regs, sizeof call_fixed_regs);
378 n_non_fixed_regs = 0;
380 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
382 if (fixed_regs[i])
383 SET_HARD_REG_BIT (fixed_reg_set, i);
384 else
385 n_non_fixed_regs++;
387 if (call_used_regs[i])
388 SET_HARD_REG_BIT (call_used_reg_set, i);
389 if (call_fixed_regs[i])
390 SET_HARD_REG_BIT (call_fixed_reg_set, i);
391 if (CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (i)))
392 SET_HARD_REG_BIT (losing_caller_save_reg_set, i);
396 /* Compute the table of register modes.
397 These values are used to record death information for individual registers
398 (as opposed to a multi-register mode). */
400 static void
401 init_reg_modes ()
403 register int i;
405 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
407 reg_raw_mode[i] = choose_hard_reg_mode (i, 1);
409 /* If we couldn't find a valid mode, fall back to `word_mode'.
410 ??? We assume `word_mode' has already been initialized.
411 ??? One situation in which we need to do this is on the mips where
412 HARD_REGNO_NREGS (fpreg, [SD]Fmode) returns 2. Ideally we'd like
413 to use DF mode for the even registers and VOIDmode for the odd
414 (for the cpu models where the odd ones are inaccessible). */
415 if (reg_raw_mode[i] == VOIDmode)
416 reg_raw_mode[i] = word_mode;
420 /* Finish initializing the register sets and
421 initialize the register modes. */
423 void
424 init_regs ()
426 /* This finishes what was started by init_reg_sets, but couldn't be done
427 until after register usage was specified. */
428 if (!output_bytecode)
429 init_reg_sets_1 ();
431 init_reg_modes ();
434 /* Return a machine mode that is legitimate for hard reg REGNO and large
435 enough to save nregs. If we can't find one, return VOIDmode. */
437 enum machine_mode
438 choose_hard_reg_mode (regno, nregs)
439 int regno;
440 int nregs;
442 enum machine_mode found_mode = VOIDmode, mode;
444 /* We first look for the largest integer mode that can be validly
445 held in REGNO. If none, we look for the largest floating-point mode.
446 If we still didn't find a valid mode, try CCmode. */
448 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
449 mode != VOIDmode;
450 mode = GET_MODE_WIDER_MODE (mode))
451 if (HARD_REGNO_NREGS (regno, mode) == nregs
452 && HARD_REGNO_MODE_OK (regno, mode))
453 found_mode = mode;
455 if (found_mode != VOIDmode)
456 return found_mode;
458 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
459 mode != VOIDmode;
460 mode = GET_MODE_WIDER_MODE (mode))
461 if (HARD_REGNO_NREGS (regno, mode) == nregs
462 && HARD_REGNO_MODE_OK (regno, mode))
463 found_mode = mode;
465 if (found_mode != VOIDmode)
466 return found_mode;
468 if (HARD_REGNO_NREGS (regno, CCmode) == nregs
469 && HARD_REGNO_MODE_OK (regno, CCmode))
470 return CCmode;
472 /* We can't find a mode valid for this register. */
473 return VOIDmode;
476 /* Specify the usage characteristics of the register named NAME.
477 It should be a fixed register if FIXED and a
478 call-used register if CALL_USED. */
480 void
481 fix_register (name, fixed, call_used)
482 char *name;
483 int fixed, call_used;
485 int i;
487 if (output_bytecode)
489 warning ("request to mark `%s' as %s ignored by bytecode compiler",
490 name, call_used ? "call-used" : "fixed");
491 return;
494 /* Decode the name and update the primary form of
495 the register info. */
497 if ((i = decode_reg_name (name)) >= 0)
499 fixed_regs[i] = fixed;
500 call_used_regs[i] = call_used;
502 else
504 warning ("unknown register name: %s", name);
508 /* Mark register number I as global. */
510 void
511 globalize_reg (i)
512 int i;
514 if (global_regs[i])
516 warning ("register used for two global register variables");
517 return;
520 if (call_used_regs[i] && ! fixed_regs[i])
521 warning ("call-clobbered register used for global register variable");
523 global_regs[i] = 1;
525 /* If already fixed, nothing else to do. */
526 if (fixed_regs[i])
527 return;
529 fixed_regs[i] = call_used_regs[i] = call_fixed_regs[i] = 1;
530 n_non_fixed_regs--;
532 SET_HARD_REG_BIT (fixed_reg_set, i);
533 SET_HARD_REG_BIT (call_used_reg_set, i);
534 SET_HARD_REG_BIT (call_fixed_reg_set, i);
537 /* Now the data and code for the `regclass' pass, which happens
538 just before local-alloc. */
540 /* The `costs' struct records the cost of using a hard register of each class
541 and of using memory for each pseudo. We use this data to set up
542 register class preferences. */
544 struct costs
546 int cost[N_REG_CLASSES];
547 int mem_cost;
550 /* Record the cost of each class for each pseudo. */
552 static struct costs *costs;
554 /* Record the same data by operand number, accumulated for each alternative
555 in an insn. The contribution to a pseudo is that of the minimum-cost
556 alternative. */
558 static struct costs op_costs[MAX_RECOG_OPERANDS];
560 /* (enum reg_class) prefclass[R] is the preferred class for pseudo number R.
561 This is available after `regclass' is run. */
563 static char *prefclass;
565 /* altclass[R] is a register class that we should use for allocating
566 pseudo number R if no register in the preferred class is available.
567 If no register in this class is available, memory is preferred.
569 It might appear to be more general to have a bitmask of classes here,
570 but since it is recommended that there be a class corresponding to the
571 union of most major pair of classes, that generality is not required.
573 This is available after `regclass' is run. */
575 static char *altclass;
577 /* Record the depth of loops that we are in. */
579 static int loop_depth;
581 /* Account for the fact that insns within a loop are executed very commonly,
582 but don't keep doing this as loops go too deep. */
584 static int loop_cost;
586 static void record_reg_classes PROTO((int, int, rtx *, enum machine_mode *,
587 char **, rtx));
588 static int copy_cost PROTO((rtx, enum machine_mode,
589 enum reg_class, int));
590 static void record_address_regs PROTO((rtx, enum reg_class, int));
591 static auto_inc_dec_reg_p PROTO((rtx, enum machine_mode));
592 static void reg_scan_mark_refs PROTO((rtx, rtx, int));
594 /* Return the reg_class in which pseudo reg number REGNO is best allocated.
595 This function is sometimes called before the info has been computed.
596 When that happens, just return GENERAL_REGS, which is innocuous. */
598 enum reg_class
599 reg_preferred_class (regno)
600 int regno;
602 if (prefclass == 0)
603 return GENERAL_REGS;
604 return (enum reg_class) prefclass[regno];
607 enum reg_class
608 reg_alternate_class (regno)
610 if (prefclass == 0)
611 return ALL_REGS;
613 return (enum reg_class) altclass[regno];
616 /* This prevents dump_flow_info from losing if called
617 before regclass is run. */
619 void
620 regclass_init ()
622 prefclass = 0;
625 /* This is a pass of the compiler that scans all instructions
626 and calculates the preferred class for each pseudo-register.
627 This information can be accessed later by calling `reg_preferred_class'.
628 This pass comes just before local register allocation. */
630 void
631 regclass (f, nregs)
632 rtx f;
633 int nregs;
635 #ifdef REGISTER_CONSTRAINTS
636 register rtx insn;
637 register int i, j;
638 struct costs init_cost;
639 rtx set;
640 int pass;
642 init_recog ();
644 costs = (struct costs *) alloca (nregs * sizeof (struct costs));
646 #ifdef FORBIDDEN_INC_DEC_CLASSES
648 in_inc_dec = (char *) alloca (nregs);
650 /* Initialize information about which register classes can be used for
651 pseudos that are auto-incremented or auto-decremented. It would
652 seem better to put this in init_reg_sets, but we need to be able
653 to allocate rtx, which we can't do that early. */
655 for (i = 0; i < N_REG_CLASSES; i++)
657 rtx r = gen_rtx (REG, VOIDmode, 0);
658 enum machine_mode m;
660 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
661 if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
663 REGNO (r) = j;
665 for (m = VOIDmode; (int) m < (int) MAX_MACHINE_MODE;
666 m = (enum machine_mode) ((int) m + 1))
667 if (HARD_REGNO_MODE_OK (j, m))
669 PUT_MODE (r, m);
671 /* If a register is not directly suitable for an
672 auto-increment or decrement addressing mode and
673 requires secondary reloads, disallow its class from
674 being used in such addresses. */
676 if ((0
677 #ifdef SECONDARY_RELOAD_CLASS
678 || (SECONDARY_RELOAD_CLASS (BASE_REG_CLASS, m, r)
679 != NO_REGS)
680 #else
681 #ifdef SECONDARY_INPUT_RELOAD_CLASS
682 || (SECONDARY_INPUT_RELOAD_CLASS (BASE_REG_CLASS, m, r)
683 != NO_REGS)
684 #endif
685 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
686 || (SECONDARY_OUTPUT_RELOAD_CLASS (BASE_REG_CLASS, m, r)
687 != NO_REGS)
688 #endif
689 #endif
691 && ! auto_inc_dec_reg_p (r, m))
692 forbidden_inc_dec_class[i] = 1;
696 #endif /* FORBIDDEN_INC_DEC_CLASSES */
698 init_cost.mem_cost = 10000;
699 for (i = 0; i < N_REG_CLASSES; i++)
700 init_cost.cost[i] = 10000;
702 /* Normally we scan the insns once and determine the best class to use for
703 each register. However, if -fexpensive_optimizations are on, we do so
704 twice, the second time using the tentative best classes to guide the
705 selection. */
707 for (pass = 0; pass <= flag_expensive_optimizations; pass++)
709 /* Zero out our accumulation of the cost of each class for each reg. */
711 bzero ((char *) costs, nregs * sizeof (struct costs));
713 #ifdef FORBIDDEN_INC_DEC_CLASSES
714 bzero (in_inc_dec, nregs);
715 #endif
717 loop_depth = 0, loop_cost = 1;
719 /* Scan the instructions and record each time it would
720 save code to put a certain register in a certain class. */
722 for (insn = f; insn; insn = NEXT_INSN (insn))
724 char *constraints[MAX_RECOG_OPERANDS];
725 enum machine_mode modes[MAX_RECOG_OPERANDS];
726 int nalternatives;
727 int noperands;
729 /* Show that an insn inside a loop is likely to be executed three
730 times more than insns outside a loop. This is much more aggressive
731 than the assumptions made elsewhere and is being tried as an
732 experiment. */
734 if (GET_CODE (insn) == NOTE
735 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
736 loop_depth++, loop_cost = 1 << (2 * MIN (loop_depth, 5));
737 else if (GET_CODE (insn) == NOTE
738 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
739 loop_depth--, loop_cost = 1 << (2 * MIN (loop_depth, 5));
741 else if ((GET_CODE (insn) == INSN
742 && GET_CODE (PATTERN (insn)) != USE
743 && GET_CODE (PATTERN (insn)) != CLOBBER
744 && GET_CODE (PATTERN (insn)) != ASM_INPUT)
745 || (GET_CODE (insn) == JUMP_INSN
746 && GET_CODE (PATTERN (insn)) != ADDR_VEC
747 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
748 || GET_CODE (insn) == CALL_INSN)
750 if (GET_CODE (insn) == INSN
751 && (noperands = asm_noperands (PATTERN (insn))) >= 0)
753 decode_asm_operands (PATTERN (insn), recog_operand, NULL_PTR,
754 constraints, modes);
755 nalternatives = (noperands == 0 ? 0
756 : n_occurrences (',', constraints[0]) + 1);
758 else
760 int insn_code_number = recog_memoized (insn);
761 rtx note;
763 set = single_set (insn);
764 insn_extract (insn);
766 nalternatives = insn_n_alternatives[insn_code_number];
767 noperands = insn_n_operands[insn_code_number];
769 /* If this insn loads a parameter from its stack slot, then
770 it represents a savings, rather than a cost, if the
771 parameter is stored in memory. Record this fact. */
773 if (set != 0 && GET_CODE (SET_DEST (set)) == REG
774 && GET_CODE (SET_SRC (set)) == MEM
775 && (note = find_reg_note (insn, REG_EQUIV,
776 NULL_RTX)) != 0
777 && GET_CODE (XEXP (note, 0)) == MEM)
779 costs[REGNO (SET_DEST (set))].mem_cost
780 -= (MEMORY_MOVE_COST (GET_MODE (SET_DEST (set)))
781 * loop_cost);
782 record_address_regs (XEXP (SET_SRC (set), 0),
783 BASE_REG_CLASS, loop_cost * 2);
784 continue;
787 /* Improve handling of two-address insns such as
788 (set X (ashift CONST Y)) where CONST must be made to
789 match X. Change it into two insns: (set X CONST)
790 (set X (ashift X Y)). If we left this for reloading, it
791 would probably get three insns because X and Y might go
792 in the same place. This prevents X and Y from receiving
793 the same hard reg.
795 We can only do this if the modes of operands 0 and 1
796 (which might not be the same) are tieable and we only need
797 do this during our first pass. */
799 if (pass == 0 && optimize
800 && noperands >= 3
801 && insn_operand_constraint[insn_code_number][1][0] == '0'
802 && insn_operand_constraint[insn_code_number][1][1] == 0
803 && CONSTANT_P (recog_operand[1])
804 && ! rtx_equal_p (recog_operand[0], recog_operand[1])
805 && ! rtx_equal_p (recog_operand[0], recog_operand[2])
806 && GET_CODE (recog_operand[0]) == REG
807 && MODES_TIEABLE_P (GET_MODE (recog_operand[0]),
808 insn_operand_mode[insn_code_number][1]))
810 rtx previnsn = prev_real_insn (insn);
811 rtx dest
812 = gen_lowpart (insn_operand_mode[insn_code_number][1],
813 recog_operand[0]);
814 rtx newinsn
815 = emit_insn_before (gen_move_insn (dest,
816 recog_operand[1]),
817 insn);
819 /* If this insn was the start of a basic block,
820 include the new insn in that block.
821 We need not check for code_label here;
822 while a basic block can start with a code_label,
823 INSN could not be at the beginning of that block. */
824 if (previnsn == 0 || GET_CODE (previnsn) == JUMP_INSN)
826 int b;
827 for (b = 0; b < n_basic_blocks; b++)
828 if (insn == basic_block_head[b])
829 basic_block_head[b] = newinsn;
832 /* This makes one more setting of new insns's dest. */
833 REG_N_SETS (REGNO (recog_operand[0]))++;
835 *recog_operand_loc[1] = recog_operand[0];
836 for (i = insn_n_dups[insn_code_number] - 1; i >= 0; i--)
837 if (recog_dup_num[i] == 1)
838 *recog_dup_loc[i] = recog_operand[0];
840 insn = PREV_INSN (newinsn);
841 continue;
844 for (i = 0; i < noperands; i++)
846 constraints[i]
847 = insn_operand_constraint[insn_code_number][i];
848 modes[i] = insn_operand_mode[insn_code_number][i];
852 /* If we get here, we are set up to record the costs of all the
853 operands for this insn. Start by initializing the costs.
854 Then handle any address registers. Finally record the desired
855 classes for any pseudos, doing it twice if some pair of
856 operands are commutative. */
858 for (i = 0; i < noperands; i++)
860 op_costs[i] = init_cost;
862 if (GET_CODE (recog_operand[i]) == SUBREG)
863 recog_operand[i] = SUBREG_REG (recog_operand[i]);
865 if (GET_CODE (recog_operand[i]) == MEM)
866 record_address_regs (XEXP (recog_operand[i], 0),
867 BASE_REG_CLASS, loop_cost * 2);
868 else if (constraints[i][0] == 'p')
869 record_address_regs (recog_operand[i],
870 BASE_REG_CLASS, loop_cost * 2);
873 /* Check for commutative in a separate loop so everything will
874 have been initialized. We must do this even if one operand
875 is a constant--see addsi3 in m68k.md. */
877 for (i = 0; i < noperands - 1; i++)
878 if (constraints[i][0] == '%')
880 char *xconstraints[MAX_RECOG_OPERANDS];
881 int j;
883 /* Handle commutative operands by swapping the constraints.
884 We assume the modes are the same. */
886 for (j = 0; j < noperands; j++)
887 xconstraints[j] = constraints[j];
889 xconstraints[i] = constraints[i+1];
890 xconstraints[i+1] = constraints[i];
891 record_reg_classes (nalternatives, noperands,
892 recog_operand, modes, xconstraints,
893 insn);
896 record_reg_classes (nalternatives, noperands, recog_operand,
897 modes, constraints, insn);
899 /* Now add the cost for each operand to the total costs for
900 its register. */
902 for (i = 0; i < noperands; i++)
903 if (GET_CODE (recog_operand[i]) == REG
904 && REGNO (recog_operand[i]) >= FIRST_PSEUDO_REGISTER)
906 int regno = REGNO (recog_operand[i]);
907 struct costs *p = &costs[regno], *q = &op_costs[i];
909 p->mem_cost += q->mem_cost * loop_cost;
910 for (j = 0; j < N_REG_CLASSES; j++)
911 p->cost[j] += q->cost[j] * loop_cost;
916 /* Now for each register look at how desirable each class is
917 and find which class is preferred. Store that in
918 `prefclass[REGNO]'. Record in `altclass[REGNO]' the largest register
919 class any of whose registers is better than memory. */
921 if (pass == 0)
923 prefclass = (char *) oballoc (nregs);
924 altclass = (char *) oballoc (nregs);
927 for (i = FIRST_PSEUDO_REGISTER; i < nregs; i++)
929 register int best_cost = (1 << (HOST_BITS_PER_INT - 2)) - 1;
930 enum reg_class best = ALL_REGS, alt = NO_REGS;
931 /* This is an enum reg_class, but we call it an int
932 to save lots of casts. */
933 register int class;
934 register struct costs *p = &costs[i];
936 for (class = (int) ALL_REGS - 1; class > 0; class--)
938 /* Ignore classes that are too small for this operand or
939 invalid for a operand that was auto-incremented. */
940 if (CLASS_MAX_NREGS (class, PSEUDO_REGNO_MODE (i))
941 > reg_class_size[class]
942 #ifdef FORBIDDEN_INC_DEC_CLASSES
943 || (in_inc_dec[i] && forbidden_inc_dec_class[class])
944 #endif
947 else if (p->cost[class] < best_cost)
949 best_cost = p->cost[class];
950 best = (enum reg_class) class;
952 else if (p->cost[class] == best_cost)
953 best = reg_class_subunion[(int)best][class];
956 /* Record the alternate register class; i.e., a class for which
957 every register in it is better than using memory. If adding a
958 class would make a smaller class (i.e., no union of just those
959 classes exists), skip that class. The major unions of classes
960 should be provided as a register class. Don't do this if we
961 will be doing it again later. */
963 if (pass == 1 || ! flag_expensive_optimizations)
964 for (class = 0; class < N_REG_CLASSES; class++)
965 if (p->cost[class] < p->mem_cost
966 && (reg_class_size[(int) reg_class_subunion[(int) alt][class]]
967 > reg_class_size[(int) alt])
968 #ifdef FORBIDDEN_INC_DEC_CLASSES
969 && ! (in_inc_dec[i] && forbidden_inc_dec_class[class])
970 #endif
972 alt = reg_class_subunion[(int) alt][class];
974 /* If we don't add any classes, nothing to try. */
975 if (alt == best)
976 alt = NO_REGS;
978 /* We cast to (int) because (char) hits bugs in some compilers. */
979 prefclass[i] = (int) best;
980 altclass[i] = (int) alt;
983 #endif /* REGISTER_CONSTRAINTS */
986 #ifdef REGISTER_CONSTRAINTS
988 /* Record the cost of using memory or registers of various classes for
989 the operands in INSN.
991 N_ALTS is the number of alternatives.
993 N_OPS is the number of operands.
995 OPS is an array of the operands.
997 MODES are the modes of the operands, in case any are VOIDmode.
999 CONSTRAINTS are the constraints to use for the operands. This array
1000 is modified by this procedure.
1002 This procedure works alternative by alternative. For each alternative
1003 we assume that we will be able to allocate all pseudos to their ideal
1004 register class and calculate the cost of using that alternative. Then
1005 we compute for each operand that is a pseudo-register, the cost of
1006 having the pseudo allocated to each register class and using it in that
1007 alternative. To this cost is added the cost of the alternative.
1009 The cost of each class for this insn is its lowest cost among all the
1010 alternatives. */
1012 static void
1013 record_reg_classes (n_alts, n_ops, ops, modes, constraints, insn)
1014 int n_alts;
1015 int n_ops;
1016 rtx *ops;
1017 enum machine_mode *modes;
1018 char **constraints;
1019 rtx insn;
1021 int alt;
1022 enum op_type {OP_READ, OP_WRITE, OP_READ_WRITE} op_types[MAX_RECOG_OPERANDS];
1023 int i, j;
1024 rtx set;
1026 /* By default, each operand is an input operand. */
1028 for (i = 0; i < n_ops; i++)
1029 op_types[i] = OP_READ;
1031 /* Process each alternative, each time minimizing an operand's cost with
1032 the cost for each operand in that alternative. */
1034 for (alt = 0; alt < n_alts; alt++)
1036 struct costs this_op_costs[MAX_RECOG_OPERANDS];
1037 int alt_fail = 0;
1038 int alt_cost = 0;
1039 enum reg_class classes[MAX_RECOG_OPERANDS];
1040 int class;
1042 for (i = 0; i < n_ops; i++)
1044 char *p = constraints[i];
1045 rtx op = ops[i];
1046 enum machine_mode mode = modes[i];
1047 int allows_mem = 0;
1048 int win = 0;
1049 char c;
1051 /* If this operand has no constraints at all, we can conclude
1052 nothing about it since anything is valid. */
1054 if (*p == 0)
1056 if (GET_CODE (op) == REG && REGNO (op) >= FIRST_PSEUDO_REGISTER)
1057 bzero ((char *) &this_op_costs[i], sizeof this_op_costs[i]);
1059 continue;
1062 if (*p == '%')
1063 p++;
1065 /* If this alternative is only relevant when this operand
1066 matches a previous operand, we do different things depending
1067 on whether this operand is a pseudo-reg or not. */
1069 if (p[0] >= '0' && p[0] <= '0' + i && (p[1] == ',' || p[1] == 0))
1071 j = p[0] - '0';
1072 classes[i] = classes[j];
1074 if (GET_CODE (op) != REG || REGNO (op) < FIRST_PSEUDO_REGISTER)
1076 /* If this matches the other operand, we have no added
1077 cost and we win. */
1078 if (rtx_equal_p (ops[j], op))
1079 win = 1;
1081 /* If we can put the other operand into a register, add to
1082 the cost of this alternative the cost to copy this
1083 operand to the register used for the other operand. */
1085 else if (classes[j] != NO_REGS)
1086 alt_cost += copy_cost (op, mode, classes[j], 1), win = 1;
1088 else if (GET_CODE (ops[j]) != REG
1089 || REGNO (ops[j]) < FIRST_PSEUDO_REGISTER)
1091 /* This op is a pseudo but the one it matches is not. */
1093 /* If we can't put the other operand into a register, this
1094 alternative can't be used. */
1096 if (classes[j] == NO_REGS)
1097 alt_fail = 1;
1099 /* Otherwise, add to the cost of this alternative the cost
1100 to copy the other operand to the register used for this
1101 operand. */
1103 else
1104 alt_cost += copy_cost (ops[j], mode, classes[j], 1);
1106 else
1108 /* The costs of this operand are the same as that of the
1109 other operand. However, if we cannot tie them, this
1110 alternative needs to do a copy, which is one
1111 instruction. */
1113 this_op_costs[i] = this_op_costs[j];
1114 if (REGNO (ops[i]) != REGNO (ops[j])
1115 && ! find_reg_note (insn, REG_DEAD, op))
1116 alt_cost += 2;
1118 /* This is in place of ordinary cost computation
1119 for this operand, so skip to the end of the
1120 alternative (should be just one character). */
1121 while (*p && *p++ != ',')
1124 constraints[i] = p;
1125 continue;
1129 /* Scan all the constraint letters. See if the operand matches
1130 any of the constraints. Collect the valid register classes
1131 and see if this operand accepts memory. */
1133 classes[i] = NO_REGS;
1134 while (*p && (c = *p++) != ',')
1135 switch (c)
1137 case '=':
1138 op_types[i] = OP_WRITE;
1139 break;
1141 case '+':
1142 op_types[i] = OP_READ_WRITE;
1143 break;
1145 case '*':
1146 /* Ignore the next letter for this pass. */
1147 p++;
1148 break;
1150 case '%':
1151 case '?': case '!': case '#':
1152 case '&':
1153 case '0': case '1': case '2': case '3': case '4':
1154 case 'p':
1155 break;
1157 case 'm': case 'o': case 'V':
1158 /* It doesn't seem worth distinguishing between offsettable
1159 and non-offsettable addresses here. */
1160 allows_mem = 1;
1161 if (GET_CODE (op) == MEM)
1162 win = 1;
1163 break;
1165 case '<':
1166 if (GET_CODE (op) == MEM
1167 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
1168 || GET_CODE (XEXP (op, 0)) == POST_DEC))
1169 win = 1;
1170 break;
1172 case '>':
1173 if (GET_CODE (op) == MEM
1174 && (GET_CODE (XEXP (op, 0)) == PRE_INC
1175 || GET_CODE (XEXP (op, 0)) == POST_INC))
1176 win = 1;
1177 break;
1179 case 'E':
1180 #ifndef REAL_ARITHMETIC
1181 /* Match any floating double constant, but only if
1182 we can examine the bits of it reliably. */
1183 if ((HOST_FLOAT_FORMAT != TARGET_FLOAT_FORMAT
1184 || HOST_BITS_PER_WIDE_INT != BITS_PER_WORD)
1185 && GET_MODE (op) != VOIDmode && ! flag_pretend_float)
1186 break;
1187 #endif
1188 if (GET_CODE (op) == CONST_DOUBLE)
1189 win = 1;
1190 break;
1192 case 'F':
1193 if (GET_CODE (op) == CONST_DOUBLE)
1194 win = 1;
1195 break;
1197 case 'G':
1198 case 'H':
1199 if (GET_CODE (op) == CONST_DOUBLE
1200 && CONST_DOUBLE_OK_FOR_LETTER_P (op, c))
1201 win = 1;
1202 break;
1204 case 's':
1205 if (GET_CODE (op) == CONST_INT
1206 || (GET_CODE (op) == CONST_DOUBLE
1207 && GET_MODE (op) == VOIDmode))
1208 break;
1209 case 'i':
1210 if (CONSTANT_P (op)
1211 #ifdef LEGITIMATE_PIC_OPERAND_P
1212 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))
1213 #endif
1215 win = 1;
1216 break;
1218 case 'n':
1219 if (GET_CODE (op) == CONST_INT
1220 || (GET_CODE (op) == CONST_DOUBLE
1221 && GET_MODE (op) == VOIDmode))
1222 win = 1;
1223 break;
1225 case 'I':
1226 case 'J':
1227 case 'K':
1228 case 'L':
1229 case 'M':
1230 case 'N':
1231 case 'O':
1232 case 'P':
1233 if (GET_CODE (op) == CONST_INT
1234 && CONST_OK_FOR_LETTER_P (INTVAL (op), c))
1235 win = 1;
1236 break;
1238 case 'X':
1239 win = 1;
1240 break;
1242 #ifdef EXTRA_CONSTRAINT
1243 case 'Q':
1244 case 'R':
1245 case 'S':
1246 case 'T':
1247 case 'U':
1248 if (EXTRA_CONSTRAINT (op, c))
1249 win = 1;
1250 break;
1251 #endif
1253 case 'g':
1254 if (GET_CODE (op) == MEM
1255 || (CONSTANT_P (op)
1256 #ifdef LEGITIMATE_PIC_OPERAND_P
1257 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))
1258 #endif
1260 win = 1;
1261 allows_mem = 1;
1262 case 'r':
1263 classes[i]
1264 = reg_class_subunion[(int) classes[i]][(int) GENERAL_REGS];
1265 break;
1267 default:
1268 classes[i]
1269 = reg_class_subunion[(int) classes[i]]
1270 [(int) REG_CLASS_FROM_LETTER (c)];
1273 constraints[i] = p;
1275 /* How we account for this operand now depends on whether it is a
1276 pseudo register or not. If it is, we first check if any
1277 register classes are valid. If not, we ignore this alternative,
1278 since we want to assume that all pseudos get allocated for
1279 register preferencing. If some register class is valid, compute
1280 the costs of moving the pseudo into that class. */
1282 if (GET_CODE (op) == REG && REGNO (op) >= FIRST_PSEUDO_REGISTER)
1284 if (classes[i] == NO_REGS)
1285 alt_fail = 1;
1286 else
1288 struct costs *pp = &this_op_costs[i];
1290 for (class = 0; class < N_REG_CLASSES; class++)
1291 pp->cost[class] = may_move_cost[class][(int) classes[i]];
1293 /* If the alternative actually allows memory, make things
1294 a bit cheaper since we won't need an extra insn to
1295 load it. */
1297 pp->mem_cost = MEMORY_MOVE_COST (mode) - allows_mem;
1299 /* If we have assigned a class to this register in our
1300 first pass, add a cost to this alternative corresponding
1301 to what we would add if this register were not in the
1302 appropriate class. */
1304 if (prefclass)
1305 alt_cost
1306 += may_move_cost[prefclass[REGNO (op)]][(int) classes[i]];
1310 /* Otherwise, if this alternative wins, either because we
1311 have already determined that or if we have a hard register of
1312 the proper class, there is no cost for this alternative. */
1314 else if (win
1315 || (GET_CODE (op) == REG
1316 && reg_fits_class_p (op, classes[i], 0, GET_MODE (op))))
1319 /* If registers are valid, the cost of this alternative includes
1320 copying the object to and/or from a register. */
1322 else if (classes[i] != NO_REGS)
1324 if (op_types[i] != OP_WRITE)
1325 alt_cost += copy_cost (op, mode, classes[i], 1);
1327 if (op_types[i] != OP_READ)
1328 alt_cost += copy_cost (op, mode, classes[i], 0);
1331 /* The only other way this alternative can be used is if this is a
1332 constant that could be placed into memory. */
1334 else if (CONSTANT_P (op) && allows_mem)
1335 alt_cost += MEMORY_MOVE_COST (mode);
1336 else
1337 alt_fail = 1;
1340 if (alt_fail)
1341 continue;
1343 /* Finally, update the costs with the information we've calculated
1344 about this alternative. */
1346 for (i = 0; i < n_ops; i++)
1347 if (GET_CODE (ops[i]) == REG
1348 && REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
1350 struct costs *pp = &op_costs[i], *qq = &this_op_costs[i];
1351 int scale = 1 + (op_types[i] == OP_READ_WRITE);
1353 pp->mem_cost = MIN (pp->mem_cost,
1354 (qq->mem_cost + alt_cost) * scale);
1356 for (class = 0; class < N_REG_CLASSES; class++)
1357 pp->cost[class] = MIN (pp->cost[class],
1358 (qq->cost[class] + alt_cost) * scale);
1362 /* If this insn is a single set copying operand 1 to operand 0
1363 and one is a pseudo with the other a hard reg that is in its
1364 own register class, set the cost of that register class to -1. */
1366 if ((set = single_set (insn)) != 0
1367 && ops[0] == SET_DEST (set) && ops[1] == SET_SRC (set)
1368 && GET_CODE (ops[0]) == REG && GET_CODE (ops[1]) == REG)
1369 for (i = 0; i <= 1; i++)
1370 if (REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
1372 int regno = REGNO (ops[!i]);
1373 enum machine_mode mode = GET_MODE (ops[!i]);
1374 int class;
1375 int nr;
1377 if (regno >= FIRST_PSEUDO_REGISTER && prefclass != 0
1378 && (reg_class_size[prefclass[regno]]
1379 == CLASS_MAX_NREGS (prefclass[regno], mode)))
1380 op_costs[i].cost[prefclass[regno]] = -1;
1381 else if (regno < FIRST_PSEUDO_REGISTER)
1382 for (class = 0; class < N_REG_CLASSES; class++)
1383 if (TEST_HARD_REG_BIT (reg_class_contents[class], regno)
1384 && reg_class_size[class] == CLASS_MAX_NREGS (class, mode))
1386 if (reg_class_size[class] == 1)
1387 op_costs[i].cost[class] = -1;
1388 else
1390 for (nr = 0; nr < HARD_REGNO_NREGS(regno, mode); nr++)
1392 if (!TEST_HARD_REG_BIT (reg_class_contents[class], regno + nr))
1393 break;
1396 if (nr == HARD_REGNO_NREGS(regno,mode))
1397 op_costs[i].cost[class] = -1;
1403 /* Compute the cost of loading X into (if TO_P is non-zero) or from (if
1404 TO_P is zero) a register of class CLASS in mode MODE.
1406 X must not be a pseudo. */
1408 static int
1409 copy_cost (x, mode, class, to_p)
1410 rtx x;
1411 enum machine_mode mode;
1412 enum reg_class class;
1413 int to_p;
1415 enum reg_class secondary_class = NO_REGS;
1417 /* If X is a SCRATCH, there is actually nothing to move since we are
1418 assuming optimal allocation. */
1420 if (GET_CODE (x) == SCRATCH)
1421 return 0;
1423 /* Get the class we will actually use for a reload. */
1424 class = PREFERRED_RELOAD_CLASS (x, class);
1426 #ifdef HAVE_SECONDARY_RELOADS
1427 /* If we need a secondary reload (we assume here that we are using
1428 the secondary reload as an intermediate, not a scratch register), the
1429 cost is that to load the input into the intermediate register, then
1430 to copy them. We use a special value of TO_P to avoid recursion. */
1432 #ifdef SECONDARY_INPUT_RELOAD_CLASS
1433 if (to_p == 1)
1434 secondary_class = SECONDARY_INPUT_RELOAD_CLASS (class, mode, x);
1435 #endif
1437 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
1438 if (! to_p)
1439 secondary_class = SECONDARY_OUTPUT_RELOAD_CLASS (class, mode, x);
1440 #endif
1442 if (secondary_class != NO_REGS)
1443 return (move_cost[(int) secondary_class][(int) class]
1444 + copy_cost (x, mode, secondary_class, 2));
1445 #endif /* HAVE_SECONDARY_RELOADS */
1447 /* For memory, use the memory move cost, for (hard) registers, use the
1448 cost to move between the register classes, and use 2 for everything
1449 else (constants). */
1451 if (GET_CODE (x) == MEM || class == NO_REGS)
1452 return MEMORY_MOVE_COST (mode);
1454 else if (GET_CODE (x) == REG)
1455 return move_cost[(int) REGNO_REG_CLASS (REGNO (x))][(int) class];
1457 else
1458 /* If this is a constant, we may eventually want to call rtx_cost here. */
1459 return 2;
1462 /* Record the pseudo registers we must reload into hard registers
1463 in a subexpression of a memory address, X.
1465 CLASS is the class that the register needs to be in and is either
1466 BASE_REG_CLASS or INDEX_REG_CLASS.
1468 SCALE is twice the amount to multiply the cost by (it is twice so we
1469 can represent half-cost adjustments). */
1471 static void
1472 record_address_regs (x, class, scale)
1473 rtx x;
1474 enum reg_class class;
1475 int scale;
1477 register enum rtx_code code = GET_CODE (x);
1479 switch (code)
1481 case CONST_INT:
1482 case CONST:
1483 case CC0:
1484 case PC:
1485 case SYMBOL_REF:
1486 case LABEL_REF:
1487 return;
1489 case PLUS:
1490 /* When we have an address that is a sum,
1491 we must determine whether registers are "base" or "index" regs.
1492 If there is a sum of two registers, we must choose one to be
1493 the "base". Luckily, we can use the REGNO_POINTER_FLAG
1494 to make a good choice most of the time. We only need to do this
1495 on machines that can have two registers in an address and where
1496 the base and index register classes are different.
1498 ??? This code used to set REGNO_POINTER_FLAG in some cases, but
1499 that seems bogus since it should only be set when we are sure
1500 the register is being used as a pointer. */
1503 rtx arg0 = XEXP (x, 0);
1504 rtx arg1 = XEXP (x, 1);
1505 register enum rtx_code code0 = GET_CODE (arg0);
1506 register enum rtx_code code1 = GET_CODE (arg1);
1508 /* Look inside subregs. */
1509 if (code0 == SUBREG)
1510 arg0 = SUBREG_REG (arg0), code0 = GET_CODE (arg0);
1511 if (code1 == SUBREG)
1512 arg1 = SUBREG_REG (arg1), code1 = GET_CODE (arg1);
1514 /* If this machine only allows one register per address, it must
1515 be in the first operand. */
1517 if (MAX_REGS_PER_ADDRESS == 1)
1518 record_address_regs (arg0, class, scale);
1520 /* If index and base registers are the same on this machine, just
1521 record registers in any non-constant operands. We assume here,
1522 as well as in the tests below, that all addresses are in
1523 canonical form. */
1525 else if (INDEX_REG_CLASS == BASE_REG_CLASS)
1527 record_address_regs (arg0, class, scale);
1528 if (! CONSTANT_P (arg1))
1529 record_address_regs (arg1, class, scale);
1532 /* If the second operand is a constant integer, it doesn't change
1533 what class the first operand must be. */
1535 else if (code1 == CONST_INT || code1 == CONST_DOUBLE)
1536 record_address_regs (arg0, class, scale);
1538 /* If the second operand is a symbolic constant, the first operand
1539 must be an index register. */
1541 else if (code1 == SYMBOL_REF || code1 == CONST || code1 == LABEL_REF)
1542 record_address_regs (arg0, INDEX_REG_CLASS, scale);
1544 /* If both operands are registers but one is already a hard register
1545 of index or base class, give the other the class that the hard
1546 register is not. */
1548 else if (code0 == REG && code1 == REG
1549 && REGNO (arg0) < FIRST_PSEUDO_REGISTER
1550 && (REG_OK_FOR_BASE_P (arg0) || REG_OK_FOR_INDEX_P (arg0)))
1551 record_address_regs (arg1,
1552 REG_OK_FOR_BASE_P (arg0)
1553 ? INDEX_REG_CLASS : BASE_REG_CLASS,
1554 scale);
1555 else if (code0 == REG && code1 == REG
1556 && REGNO (arg1) < FIRST_PSEUDO_REGISTER
1557 && (REG_OK_FOR_BASE_P (arg1) || REG_OK_FOR_INDEX_P (arg1)))
1558 record_address_regs (arg0,
1559 REG_OK_FOR_BASE_P (arg1)
1560 ? INDEX_REG_CLASS : BASE_REG_CLASS,
1561 scale);
1563 /* If one operand is known to be a pointer, it must be the base
1564 with the other operand the index. Likewise if the other operand
1565 is a MULT. */
1567 else if ((code0 == REG && REGNO_POINTER_FLAG (REGNO (arg0)))
1568 || code1 == MULT)
1570 record_address_regs (arg0, BASE_REG_CLASS, scale);
1571 record_address_regs (arg1, INDEX_REG_CLASS, scale);
1573 else if ((code1 == REG && REGNO_POINTER_FLAG (REGNO (arg1)))
1574 || code0 == MULT)
1576 record_address_regs (arg0, INDEX_REG_CLASS, scale);
1577 record_address_regs (arg1, BASE_REG_CLASS, scale);
1580 /* Otherwise, count equal chances that each might be a base
1581 or index register. This case should be rare. */
1583 else
1585 record_address_regs (arg0, BASE_REG_CLASS, scale / 2);
1586 record_address_regs (arg0, INDEX_REG_CLASS, scale / 2);
1587 record_address_regs (arg1, BASE_REG_CLASS, scale / 2);
1588 record_address_regs (arg1, INDEX_REG_CLASS, scale / 2);
1591 break;
1593 case POST_INC:
1594 case PRE_INC:
1595 case POST_DEC:
1596 case PRE_DEC:
1597 /* Double the importance of a pseudo register that is incremented
1598 or decremented, since it would take two extra insns
1599 if it ends up in the wrong place. If the operand is a pseudo,
1600 show it is being used in an INC_DEC context. */
1602 #ifdef FORBIDDEN_INC_DEC_CLASSES
1603 if (GET_CODE (XEXP (x, 0)) == REG
1604 && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER)
1605 in_inc_dec[REGNO (XEXP (x, 0))] = 1;
1606 #endif
1608 record_address_regs (XEXP (x, 0), class, 2 * scale);
1609 break;
1611 case REG:
1613 register struct costs *pp = &costs[REGNO (x)];
1614 register int i;
1616 pp->mem_cost += (MEMORY_MOVE_COST (Pmode) * scale) / 2;
1618 for (i = 0; i < N_REG_CLASSES; i++)
1619 pp->cost[i] += (may_move_cost[i][(int) class] * scale) / 2;
1621 break;
1623 default:
1625 register char *fmt = GET_RTX_FORMAT (code);
1626 register int i;
1627 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1628 if (fmt[i] == 'e')
1629 record_address_regs (XEXP (x, i), class, scale);
1634 #ifdef FORBIDDEN_INC_DEC_CLASSES
1636 /* Return 1 if REG is valid as an auto-increment memory reference
1637 to an object of MODE. */
1639 static
1640 auto_inc_dec_reg_p (reg, mode)
1641 rtx reg;
1642 enum machine_mode mode;
1644 #ifdef HAVE_POST_INCREMENT
1645 if (memory_address_p (mode, gen_rtx (POST_INC, Pmode, reg)))
1646 return 1;
1647 #endif
1649 #ifdef HAVE_POST_DECREMENT
1650 if (memory_address_p (mode, gen_rtx (POST_DEC, Pmode, reg)))
1651 return 1;
1652 #endif
1654 #ifdef HAVE_PRE_INCREMENT
1655 if (memory_address_p (mode, gen_rtx (PRE_INC, Pmode, reg)))
1656 return 1;
1657 #endif
1659 #ifdef HAVE_PRE_DECREMENT
1660 if (memory_address_p (mode, gen_rtx (PRE_DEC, Pmode, reg)))
1661 return 1;
1662 #endif
1664 return 0;
1666 #endif
1668 #endif /* REGISTER_CONSTRAINTS */
1670 /* Allocate enough space to hold NUM_REGS registers for the tables used for
1671 reg_scan and flow_analysis that are indexed by the register number. If
1672 NEW_P is non zero, initialize all of the registers, otherwise only
1673 initialize the new registers allocated. The same table is kept from
1674 function to function, only reallocating it when we need more room. If
1675 RENUMBER_P is non zero, allocate the reg_renumber array also. */
1677 void
1678 allocate_reg_info (num_regs, new_p, renumber_p)
1679 int num_regs;
1680 int new_p;
1681 int renumber_p;
1683 static int regno_allocated = 0;
1684 static int regno_max = 0;
1685 static short *renumber = (short *)0;
1686 int i;
1687 int size_info;
1688 int size_renumber;
1689 int min = (new_p) ? 0 : regno_max;
1691 /* If this message come up, and you want to fix it, then all of the tables
1692 like reg_renumber, etc. that use short will have to be found and lengthed
1693 to int or HOST_WIDE_INT. */
1695 /* Free up all storage allocated */
1696 if (num_regs < 0)
1698 if (reg_n_info)
1700 free ((char *)reg_n_info);
1701 free ((char *)renumber);
1702 reg_n_info = (reg_info *)0;
1703 renumber = (short *)0;
1705 regno_allocated = 0;
1706 regno_max = 0;
1707 return;
1710 if (num_regs > regno_allocated)
1712 regno_allocated = num_regs + (num_regs / 20); /* add some slop space */
1713 size_info = regno_allocated * sizeof (reg_info);
1714 size_renumber = regno_allocated * sizeof (short);
1716 if (!reg_n_info)
1718 reg_n_info = (reg_info *) xmalloc (size_info);
1719 renumber = (short *) xmalloc (size_renumber);
1722 else if (new_p) /* if we're zapping everything, no need to realloc */
1724 free ((char *)reg_n_info);
1725 free ((char *)renumber);
1726 reg_n_info = (reg_info *) xmalloc (size_info);
1727 renumber = (short *) xmalloc (size_renumber);
1730 else
1732 reg_n_info = (reg_info *) xrealloc ((char *)reg_n_info, size_info);
1733 renumber = (short *) xrealloc ((char *)renumber, size_renumber);
1737 if (min < num_regs)
1739 bzero ((char *) &reg_n_info[min], (num_regs - min) * sizeof (reg_info));
1740 for (i = min; i < num_regs; i++)
1742 REG_BASIC_BLOCK (i) = REG_BLOCK_UNKNOWN;
1743 renumber[i] = -1;
1747 if (renumber_p)
1748 reg_renumber = renumber;
1750 /* Tell the regset code about the new number of registers */
1751 MAX_REGNO_REG_SET (num_regs, new_p, renumber_p);
1753 regno_max = num_regs;
1757 /* This is the `regscan' pass of the compiler, run just before cse
1758 and again just before loop.
1760 It finds the first and last use of each pseudo-register
1761 and records them in the vectors regno_first_uid, regno_last_uid
1762 and counts the number of sets in the vector reg_n_sets.
1764 REPEAT is nonzero the second time this is called. */
1766 /* Maximum number of parallel sets and clobbers in any insn in this fn.
1767 Always at least 3, since the combiner could put that many together
1768 and we want this to remain correct for all the remaining passes. */
1770 int max_parallel;
1772 void
1773 reg_scan (f, nregs, repeat)
1774 rtx f;
1775 int nregs;
1776 int repeat;
1778 register rtx insn;
1780 allocate_reg_info (nregs, TRUE, FALSE);
1781 max_parallel = 3;
1783 for (insn = f; insn; insn = NEXT_INSN (insn))
1784 if (GET_CODE (insn) == INSN
1785 || GET_CODE (insn) == CALL_INSN
1786 || GET_CODE (insn) == JUMP_INSN)
1788 if (GET_CODE (PATTERN (insn)) == PARALLEL
1789 && XVECLEN (PATTERN (insn), 0) > max_parallel)
1790 max_parallel = XVECLEN (PATTERN (insn), 0);
1791 reg_scan_mark_refs (PATTERN (insn), insn, 0);
1793 if (REG_NOTES (insn))
1794 reg_scan_mark_refs (REG_NOTES (insn), insn, 1);
1798 /* X is the expression to scan. INSN is the insn it appears in.
1799 NOTE_FLAG is nonzero if X is from INSN's notes rather than its body. */
1801 static void
1802 reg_scan_mark_refs (x, insn, note_flag)
1803 rtx x;
1804 rtx insn;
1805 int note_flag;
1807 register enum rtx_code code = GET_CODE (x);
1808 register rtx dest;
1809 register rtx note;
1811 switch (code)
1813 case CONST_INT:
1814 case CONST:
1815 case CONST_DOUBLE:
1816 case CC0:
1817 case PC:
1818 case SYMBOL_REF:
1819 case LABEL_REF:
1820 case ADDR_VEC:
1821 case ADDR_DIFF_VEC:
1822 return;
1824 case REG:
1826 register int regno = REGNO (x);
1828 REGNO_LAST_NOTE_UID (regno) = INSN_UID (insn);
1829 if (!note_flag)
1830 REGNO_LAST_UID (regno) = INSN_UID (insn);
1831 if (REGNO_FIRST_UID (regno) == 0)
1832 REGNO_FIRST_UID (regno) = INSN_UID (insn);
1834 break;
1836 case EXPR_LIST:
1837 if (XEXP (x, 0))
1838 reg_scan_mark_refs (XEXP (x, 0), insn, note_flag);
1839 if (XEXP (x, 1))
1840 reg_scan_mark_refs (XEXP (x, 1), insn, note_flag);
1841 break;
1843 case INSN_LIST:
1844 if (XEXP (x, 1))
1845 reg_scan_mark_refs (XEXP (x, 1), insn, note_flag);
1846 break;
1848 case SET:
1849 /* Count a set of the destination if it is a register. */
1850 for (dest = SET_DEST (x);
1851 GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
1852 || GET_CODE (dest) == ZERO_EXTEND;
1853 dest = XEXP (dest, 0))
1856 if (GET_CODE (dest) == REG)
1857 REG_N_SETS (REGNO (dest))++;
1859 /* If this is setting a pseudo from another pseudo or the sum of a
1860 pseudo and a constant integer and the other pseudo is known to be
1861 a pointer, set the destination to be a pointer as well.
1863 Likewise if it is setting the destination from an address or from a
1864 value equivalent to an address or to the sum of an address and
1865 something else.
1867 But don't do any of this if the pseudo corresponds to a user
1868 variable since it should have already been set as a pointer based
1869 on the type. */
1871 if (GET_CODE (SET_DEST (x)) == REG
1872 && REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER
1873 && ! REG_USERVAR_P (SET_DEST (x))
1874 && ! REGNO_POINTER_FLAG (REGNO (SET_DEST (x)))
1875 && ((GET_CODE (SET_SRC (x)) == REG
1876 && REGNO_POINTER_FLAG (REGNO (SET_SRC (x))))
1877 || ((GET_CODE (SET_SRC (x)) == PLUS
1878 || GET_CODE (SET_SRC (x)) == LO_SUM)
1879 && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
1880 && GET_CODE (XEXP (SET_SRC (x), 0)) == REG
1881 && REGNO_POINTER_FLAG (REGNO (XEXP (SET_SRC (x), 0))))
1882 || GET_CODE (SET_SRC (x)) == CONST
1883 || GET_CODE (SET_SRC (x)) == SYMBOL_REF
1884 || GET_CODE (SET_SRC (x)) == LABEL_REF
1885 || (GET_CODE (SET_SRC (x)) == HIGH
1886 && (GET_CODE (XEXP (SET_SRC (x), 0)) == CONST
1887 || GET_CODE (XEXP (SET_SRC (x), 0)) == SYMBOL_REF
1888 || GET_CODE (XEXP (SET_SRC (x), 0)) == LABEL_REF))
1889 || ((GET_CODE (SET_SRC (x)) == PLUS
1890 || GET_CODE (SET_SRC (x)) == LO_SUM)
1891 && (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST
1892 || GET_CODE (XEXP (SET_SRC (x), 1)) == SYMBOL_REF
1893 || GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF))
1894 || ((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
1895 && (GET_CODE (XEXP (note, 0)) == CONST
1896 || GET_CODE (XEXP (note, 0)) == SYMBOL_REF
1897 || GET_CODE (XEXP (note, 0)) == LABEL_REF))))
1898 REGNO_POINTER_FLAG (REGNO (SET_DEST (x))) = 1;
1900 /* ... fall through ... */
1902 default:
1904 register char *fmt = GET_RTX_FORMAT (code);
1905 register int i;
1906 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1908 if (fmt[i] == 'e')
1909 reg_scan_mark_refs (XEXP (x, i), insn, note_flag);
1910 else if (fmt[i] == 'E' && XVEC (x, i) != 0)
1912 register int j;
1913 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1914 reg_scan_mark_refs (XVECEXP (x, i, j), insn, note_flag);
1921 /* Return nonzero if C1 is a subset of C2, i.e., if every register in C1
1922 is also in C2. */
1925 reg_class_subset_p (c1, c2)
1926 register enum reg_class c1;
1927 register enum reg_class c2;
1929 if (c1 == c2) return 1;
1931 if (c2 == ALL_REGS)
1932 win:
1933 return 1;
1934 GO_IF_HARD_REG_SUBSET (reg_class_contents[(int)c1],
1935 reg_class_contents[(int)c2],
1936 win);
1937 return 0;
1940 /* Return nonzero if there is a register that is in both C1 and C2. */
1943 reg_classes_intersect_p (c1, c2)
1944 register enum reg_class c1;
1945 register enum reg_class c2;
1947 #ifdef HARD_REG_SET
1948 register
1949 #endif
1950 HARD_REG_SET c;
1952 if (c1 == c2) return 1;
1954 if (c1 == ALL_REGS || c2 == ALL_REGS)
1955 return 1;
1957 COPY_HARD_REG_SET (c, reg_class_contents[(int) c1]);
1958 AND_HARD_REG_SET (c, reg_class_contents[(int) c2]);
1960 GO_IF_HARD_REG_SUBSET (c, reg_class_contents[(int) NO_REGS], lose);
1961 return 1;
1963 lose:
1964 return 0;
1967 /* Release any memory allocated by register sets. */
1969 void
1970 regset_release_memory ()
1972 if (basic_block_live_at_start)
1974 free_regset_vector (basic_block_live_at_start, n_basic_blocks);
1975 basic_block_live_at_start = 0;
1978 FREE_REG_SET (regs_live_at_setjmp);
1979 bitmap_release_memory ();