2009-01-19 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / regclass.c
blob2f665d874a3a5d3068dade460e81d952ba8b7197
1 /* Compute register class preferences for pseudo-registers.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996
3 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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 "addresses.h"
39 #include "function.h"
40 #include "insn-config.h"
41 #include "recog.h"
42 #include "reload.h"
43 #include "real.h"
44 #include "toplev.h"
45 #include "output.h"
46 #include "ggc.h"
47 #include "timevar.h"
48 #include "hashtab.h"
49 #include "target.h"
50 #include "tree-pass.h"
51 #include "df.h"
52 #include "ira.h"
54 /* Maximum register number used in this function, plus one. */
56 int max_regno;
58 static void init_reg_sets_1 (void);
59 static void init_reg_autoinc (void);
61 /* If we have auto-increment or auto-decrement and we can have secondary
62 reloads, we are not allowed to use classes requiring secondary
63 reloads for pseudos auto-incremented since reload can't handle it. */
64 /* We leave it to target hooks to decide if we have secondary reloads, so
65 assume that we might have them. */
66 #if defined(AUTO_INC_DEC) /* */
67 #define FORBIDDEN_INC_DEC_CLASSES
68 #endif
70 /* Register tables used by many passes. */
72 /* Indexed by hard register number, contains 1 for registers
73 that are fixed use (stack pointer, pc, frame pointer, etc.).
74 These are the registers that cannot be used to allocate
75 a pseudo reg for general use. */
77 char fixed_regs[FIRST_PSEUDO_REGISTER];
79 /* Same info as a HARD_REG_SET. */
81 HARD_REG_SET fixed_reg_set;
83 /* Data for initializing the above. */
85 static const char initial_fixed_regs[] = FIXED_REGISTERS;
87 /* Indexed by hard register number, contains 1 for registers
88 that are fixed use or are clobbered by function calls.
89 These are the registers that cannot be used to allocate
90 a pseudo reg whose life crosses calls unless we are able
91 to save/restore them across the calls. */
93 char call_used_regs[FIRST_PSEUDO_REGISTER];
95 /* Same info as a HARD_REG_SET. */
97 HARD_REG_SET call_used_reg_set;
99 /* HARD_REG_SET of registers we want to avoid caller saving. */
100 HARD_REG_SET losing_caller_save_reg_set;
102 /* Data for initializing the above. */
104 static const char initial_call_used_regs[] = CALL_USED_REGISTERS;
106 /* This is much like call_used_regs, except it doesn't have to
107 be a superset of FIXED_REGISTERS. This vector indicates
108 what is really call clobbered, and is used when defining
109 regs_invalidated_by_call. */
111 #ifdef CALL_REALLY_USED_REGISTERS
112 char call_really_used_regs[] = CALL_REALLY_USED_REGISTERS;
113 #endif
115 #ifdef CALL_REALLY_USED_REGISTERS
116 #define CALL_REALLY_USED_REGNO_P(X) call_really_used_regs[X]
117 #else
118 #define CALL_REALLY_USED_REGNO_P(X) call_used_regs[X]
119 #endif
122 /* Indexed by hard register number, contains 1 for registers that are
123 fixed use or call used registers that cannot hold quantities across
124 calls even if we are willing to save and restore them. call fixed
125 registers are a subset of call used registers. */
127 char call_fixed_regs[FIRST_PSEUDO_REGISTER];
129 /* The same info as a HARD_REG_SET. */
131 HARD_REG_SET call_fixed_reg_set;
133 /* Indexed by hard register number, contains 1 for registers
134 that are being used for global register decls.
135 These must be exempt from ordinary flow analysis
136 and are also considered fixed. */
138 char global_regs[FIRST_PSEUDO_REGISTER];
140 /* Contains 1 for registers that are set or clobbered by calls. */
141 /* ??? Ideally, this would be just call_used_regs plus global_regs, but
142 for someone's bright idea to have call_used_regs strictly include
143 fixed_regs. Which leaves us guessing as to the set of fixed_regs
144 that are actually preserved. We know for sure that those associated
145 with the local stack frame are safe, but scant others. */
147 HARD_REG_SET regs_invalidated_by_call;
149 /* Same information as REGS_INVALIDATED_BY_CALL but in regset form to be used
150 in dataflow more conveniently. */
152 regset regs_invalidated_by_call_regset;
154 /* The bitmap_obstack is used to hold some static variables that
155 should not be reset after each function is compiled. */
157 static bitmap_obstack persistent_obstack;
159 /* Table of register numbers in the order in which to try to use them. */
160 #ifdef REG_ALLOC_ORDER
161 int reg_alloc_order[FIRST_PSEUDO_REGISTER] = REG_ALLOC_ORDER;
163 /* The inverse of reg_alloc_order. */
164 int inv_reg_alloc_order[FIRST_PSEUDO_REGISTER];
165 #endif
167 /* For each reg class, a HARD_REG_SET saying which registers are in it. */
169 HARD_REG_SET reg_class_contents[N_REG_CLASSES];
171 /* The same information, but as an array of unsigned ints. We copy from
172 these unsigned ints to the table above. We do this so the tm.h files
173 do not have to be aware of the wordsize for machines with <= 64 regs.
174 Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
176 #define N_REG_INTS \
177 ((FIRST_PSEUDO_REGISTER + (32 - 1)) / 32)
179 static const unsigned int_reg_class_contents[N_REG_CLASSES][N_REG_INTS]
180 = REG_CLASS_CONTENTS;
182 /* For each reg class, number of regs it contains. */
184 unsigned int reg_class_size[N_REG_CLASSES];
186 /* For each reg class, table listing all the containing classes. */
188 static enum reg_class reg_class_superclasses[N_REG_CLASSES][N_REG_CLASSES];
190 /* For each reg class, table listing all the classes contained in it. */
192 enum reg_class reg_class_subclasses[N_REG_CLASSES][N_REG_CLASSES];
194 /* For each pair of reg classes,
195 a largest reg class contained in their union. */
197 enum reg_class reg_class_subunion[N_REG_CLASSES][N_REG_CLASSES];
199 /* For each pair of reg classes,
200 the smallest reg class containing their union. */
202 enum reg_class reg_class_superunion[N_REG_CLASSES][N_REG_CLASSES];
204 /* Array containing all of the register names. */
206 const char * reg_names[] = REGISTER_NAMES;
208 /* Array containing all of the register class names. */
210 const char * reg_class_names[] = REG_CLASS_NAMES;
212 /* For each hard register, the widest mode object that it can contain.
213 This will be a MODE_INT mode if the register can hold integers. Otherwise
214 it will be a MODE_FLOAT or a MODE_CC mode, whichever is valid for the
215 register. */
217 enum machine_mode reg_raw_mode[FIRST_PSEUDO_REGISTER];
219 /* 1 if there is a register of given mode. */
221 bool have_regs_of_mode [MAX_MACHINE_MODE];
223 /* 1 if class does contain register of given mode. */
225 char contains_reg_of_mode [N_REG_CLASSES] [MAX_MACHINE_MODE];
227 /* Maximum cost of moving from a register in one class to a register in
228 another class. Based on REGISTER_MOVE_COST. */
230 move_table *move_cost[MAX_MACHINE_MODE];
232 /* Similar, but here we don't have to move if the first index is a subset
233 of the second so in that case the cost is zero. */
235 move_table *may_move_in_cost[MAX_MACHINE_MODE];
237 /* Similar, but here we don't have to move if the first index is a superset
238 of the second so in that case the cost is zero. */
240 move_table *may_move_out_cost[MAX_MACHINE_MODE];
242 /* Keep track of the last mode we initialized move costs for. */
243 static int last_mode_for_init_move_cost;
245 #ifdef FORBIDDEN_INC_DEC_CLASSES
247 /* These are the classes that regs which are auto-incremented or decremented
248 cannot be put in. */
250 static int forbidden_inc_dec_class[N_REG_CLASSES];
252 /* Indexed by n, is nonzero if (REG n) is used in an auto-inc or auto-dec
253 context. */
255 static char *in_inc_dec;
257 #endif /* FORBIDDEN_INC_DEC_CLASSES */
259 /* Sample MEM values for use by memory_move_secondary_cost. */
261 static GTY(()) rtx top_of_stack[MAX_MACHINE_MODE];
263 /* No more global register variables may be declared; true once
264 regclass has been initialized. */
266 static int no_global_reg_vars = 0;
268 /* Specify number of hard registers given machine mode occupy. */
269 unsigned char hard_regno_nregs[FIRST_PSEUDO_REGISTER][MAX_MACHINE_MODE];
271 /* Given a register bitmap, turn on the bits in a HARD_REG_SET that
272 correspond to the hard registers, if any, set in that map. This
273 could be done far more efficiently by having all sorts of special-cases
274 with moving single words, but probably isn't worth the trouble. */
276 void
277 reg_set_to_hard_reg_set (HARD_REG_SET *to, const_bitmap from)
279 unsigned i;
280 bitmap_iterator bi;
282 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
284 if (i >= FIRST_PSEUDO_REGISTER)
285 return;
286 SET_HARD_REG_BIT (*to, i);
291 /* Function called only once to initialize the above data on reg usage.
292 Once this is done, various switches may override. */
294 void
295 init_reg_sets (void)
297 int i, j;
299 /* First copy the register information from the initial int form into
300 the regsets. */
302 for (i = 0; i < N_REG_CLASSES; i++)
304 CLEAR_HARD_REG_SET (reg_class_contents[i]);
306 /* Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
307 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
308 if (int_reg_class_contents[i][j / 32]
309 & ((unsigned) 1 << (j % 32)))
310 SET_HARD_REG_BIT (reg_class_contents[i], j);
313 /* Sanity check: make sure the target macros FIXED_REGISTERS and
314 CALL_USED_REGISTERS had the right number of initializers. */
315 gcc_assert (sizeof fixed_regs == sizeof initial_fixed_regs);
316 gcc_assert (sizeof call_used_regs == sizeof initial_call_used_regs);
318 memcpy (fixed_regs, initial_fixed_regs, sizeof fixed_regs);
319 memcpy (call_used_regs, initial_call_used_regs, sizeof call_used_regs);
320 memset (global_regs, 0, sizeof global_regs);
323 /* Initialize may_move_cost and friends for mode M. */
325 void
326 init_move_cost (enum machine_mode m)
328 static unsigned short last_move_cost[N_REG_CLASSES][N_REG_CLASSES];
329 bool all_match = true;
330 unsigned int i, j;
332 gcc_assert (have_regs_of_mode[m]);
333 for (i = 0; i < N_REG_CLASSES; i++)
334 if (contains_reg_of_mode[i][m])
335 for (j = 0; j < N_REG_CLASSES; j++)
337 int cost;
338 if (!contains_reg_of_mode[j][m])
339 cost = 65535;
340 else
342 cost = REGISTER_MOVE_COST (m, i, j);
343 gcc_assert (cost < 65535);
345 all_match &= (last_move_cost[i][j] == cost);
346 last_move_cost[i][j] = cost;
348 if (all_match && last_mode_for_init_move_cost != -1)
350 move_cost[m] = move_cost[last_mode_for_init_move_cost];
351 may_move_in_cost[m] = may_move_in_cost[last_mode_for_init_move_cost];
352 may_move_out_cost[m] = may_move_out_cost[last_mode_for_init_move_cost];
353 return;
355 last_mode_for_init_move_cost = m;
356 move_cost[m] = (move_table *)xmalloc (sizeof (move_table)
357 * N_REG_CLASSES);
358 may_move_in_cost[m] = (move_table *)xmalloc (sizeof (move_table)
359 * N_REG_CLASSES);
360 may_move_out_cost[m] = (move_table *)xmalloc (sizeof (move_table)
361 * N_REG_CLASSES);
362 for (i = 0; i < N_REG_CLASSES; i++)
363 if (contains_reg_of_mode[i][m])
364 for (j = 0; j < N_REG_CLASSES; j++)
366 int cost;
367 enum reg_class *p1, *p2;
369 if (last_move_cost[i][j] == 65535)
371 move_cost[m][i][j] = 65535;
372 may_move_in_cost[m][i][j] = 65535;
373 may_move_out_cost[m][i][j] = 65535;
375 else
377 cost = last_move_cost[i][j];
379 for (p2 = &reg_class_subclasses[j][0];
380 *p2 != LIM_REG_CLASSES; p2++)
381 if (*p2 != i && contains_reg_of_mode[*p2][m])
382 cost = MAX (cost, move_cost[m][i][*p2]);
384 for (p1 = &reg_class_subclasses[i][0];
385 *p1 != LIM_REG_CLASSES; p1++)
386 if (*p1 != j && contains_reg_of_mode[*p1][m])
387 cost = MAX (cost, move_cost[m][*p1][j]);
389 gcc_assert (cost <= 65535);
390 move_cost[m][i][j] = cost;
392 if (reg_class_subset_p (i, j))
393 may_move_in_cost[m][i][j] = 0;
394 else
395 may_move_in_cost[m][i][j] = cost;
397 if (reg_class_subset_p (j, i))
398 may_move_out_cost[m][i][j] = 0;
399 else
400 may_move_out_cost[m][i][j] = cost;
403 else
404 for (j = 0; j < N_REG_CLASSES; j++)
406 move_cost[m][i][j] = 65535;
407 may_move_in_cost[m][i][j] = 65535;
408 may_move_out_cost[m][i][j] = 65535;
412 /* We need to save copies of some of the register information which
413 can be munged by command-line switches so we can restore it during
414 subsequent back-end reinitialization. */
416 static char saved_fixed_regs[FIRST_PSEUDO_REGISTER];
417 static char saved_call_used_regs[FIRST_PSEUDO_REGISTER];
418 #ifdef CALL_REALLY_USED_REGISTERS
419 static char saved_call_really_used_regs[FIRST_PSEUDO_REGISTER];
420 #endif
421 static const char *saved_reg_names[FIRST_PSEUDO_REGISTER];
423 /* Save the register information. */
425 void
426 save_register_info (void)
428 /* Sanity check: make sure the target macros FIXED_REGISTERS and
429 CALL_USED_REGISTERS had the right number of initializers. */
430 gcc_assert (sizeof fixed_regs == sizeof saved_fixed_regs);
431 gcc_assert (sizeof call_used_regs == sizeof saved_call_used_regs);
432 memcpy (saved_fixed_regs, fixed_regs, sizeof fixed_regs);
433 memcpy (saved_call_used_regs, call_used_regs, sizeof call_used_regs);
435 /* Likewise for call_really_used_regs. */
436 #ifdef CALL_REALLY_USED_REGISTERS
437 gcc_assert (sizeof call_really_used_regs
438 == sizeof saved_call_really_used_regs);
439 memcpy (saved_call_really_used_regs, call_really_used_regs,
440 sizeof call_really_used_regs);
441 #endif
443 /* And similarly for reg_names. */
444 gcc_assert (sizeof reg_names == sizeof saved_reg_names);
445 memcpy (saved_reg_names, reg_names, sizeof reg_names);
448 /* Restore the register information. */
450 static void
451 restore_register_info (void)
453 memcpy (fixed_regs, saved_fixed_regs, sizeof fixed_regs);
454 memcpy (call_used_regs, saved_call_used_regs, sizeof call_used_regs);
456 #ifdef CALL_REALLY_USED_REGISTERS
457 memcpy (call_really_used_regs, saved_call_really_used_regs,
458 sizeof call_really_used_regs);
459 #endif
461 memcpy (reg_names, saved_reg_names, sizeof reg_names);
464 /* After switches have been processed, which perhaps alter
465 `fixed_regs' and `call_used_regs', convert them to HARD_REG_SETs. */
467 static void
468 init_reg_sets_1 (void)
470 unsigned int i, j;
471 unsigned int /* enum machine_mode */ m;
473 restore_register_info ();
475 #ifdef REG_ALLOC_ORDER
476 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
477 inv_reg_alloc_order[reg_alloc_order[i]] = i;
478 #endif
480 /* This macro allows the fixed or call-used registers
481 and the register classes to depend on target flags. */
483 #ifdef CONDITIONAL_REGISTER_USAGE
484 CONDITIONAL_REGISTER_USAGE;
485 #endif
487 /* Compute number of hard regs in each class. */
489 memset (reg_class_size, 0, sizeof reg_class_size);
490 for (i = 0; i < N_REG_CLASSES; i++)
491 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
492 if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
493 reg_class_size[i]++;
495 /* Initialize the table of subunions.
496 reg_class_subunion[I][J] gets the largest-numbered reg-class
497 that is contained in the union of classes I and J. */
499 memset (reg_class_subunion, 0, sizeof reg_class_subunion);
500 for (i = 0; i < N_REG_CLASSES; i++)
502 for (j = 0; j < N_REG_CLASSES; j++)
504 HARD_REG_SET c;
505 int k;
507 COPY_HARD_REG_SET (c, reg_class_contents[i]);
508 IOR_HARD_REG_SET (c, reg_class_contents[j]);
509 for (k = 0; k < N_REG_CLASSES; k++)
510 if (hard_reg_set_subset_p (reg_class_contents[k], c)
511 && !hard_reg_set_subset_p (reg_class_contents[k],
512 reg_class_contents
513 [(int) reg_class_subunion[i][j]]))
514 reg_class_subunion[i][j] = (enum reg_class) k;
518 /* Initialize the table of superunions.
519 reg_class_superunion[I][J] gets the smallest-numbered reg-class
520 containing the union of classes I and J. */
522 memset (reg_class_superunion, 0, sizeof reg_class_superunion);
523 for (i = 0; i < N_REG_CLASSES; i++)
525 for (j = 0; j < N_REG_CLASSES; j++)
527 HARD_REG_SET c;
528 int k;
530 COPY_HARD_REG_SET (c, reg_class_contents[i]);
531 IOR_HARD_REG_SET (c, reg_class_contents[j]);
532 for (k = 0; k < N_REG_CLASSES; k++)
533 if (hard_reg_set_subset_p (c, reg_class_contents[k]))
534 break;
536 reg_class_superunion[i][j] = (enum reg_class) k;
540 /* Initialize the tables of subclasses and superclasses of each reg class.
541 First clear the whole table, then add the elements as they are found. */
543 for (i = 0; i < N_REG_CLASSES; i++)
545 for (j = 0; j < N_REG_CLASSES; j++)
547 reg_class_superclasses[i][j] = LIM_REG_CLASSES;
548 reg_class_subclasses[i][j] = LIM_REG_CLASSES;
552 for (i = 0; i < N_REG_CLASSES; i++)
554 if (i == (int) NO_REGS)
555 continue;
557 for (j = i + 1; j < N_REG_CLASSES; j++)
558 if (hard_reg_set_subset_p (reg_class_contents[i],
559 reg_class_contents[j]))
561 /* Reg class I is a subclass of J.
562 Add J to the table of superclasses of I. */
563 enum reg_class *p;
565 p = &reg_class_superclasses[i][0];
566 while (*p != LIM_REG_CLASSES) p++;
567 *p = (enum reg_class) j;
568 /* Add I to the table of superclasses of J. */
569 p = &reg_class_subclasses[j][0];
570 while (*p != LIM_REG_CLASSES) p++;
571 *p = (enum reg_class) i;
575 /* Initialize "constant" tables. */
577 CLEAR_HARD_REG_SET (fixed_reg_set);
578 CLEAR_HARD_REG_SET (call_used_reg_set);
579 CLEAR_HARD_REG_SET (call_fixed_reg_set);
580 CLEAR_HARD_REG_SET (regs_invalidated_by_call);
581 CLEAR_HARD_REG_SET (losing_caller_save_reg_set);
582 if (!regs_invalidated_by_call_regset)
584 bitmap_obstack_initialize (&persistent_obstack);
585 regs_invalidated_by_call_regset = ALLOC_REG_SET (&persistent_obstack);
587 else
588 CLEAR_REG_SET (regs_invalidated_by_call_regset);
590 memcpy (call_fixed_regs, fixed_regs, sizeof call_fixed_regs);
592 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
594 /* call_used_regs must include fixed_regs. */
595 gcc_assert (!fixed_regs[i] || call_used_regs[i]);
596 #ifdef CALL_REALLY_USED_REGISTERS
597 /* call_used_regs must include call_really_used_regs. */
598 gcc_assert (!call_really_used_regs[i] || call_used_regs[i]);
599 #endif
601 if (fixed_regs[i])
602 SET_HARD_REG_BIT (fixed_reg_set, i);
604 if (call_used_regs[i])
605 SET_HARD_REG_BIT (call_used_reg_set, i);
606 if (call_fixed_regs[i])
607 SET_HARD_REG_BIT (call_fixed_reg_set, i);
608 if (CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (i)))
609 SET_HARD_REG_BIT (losing_caller_save_reg_set, i);
611 /* There are a couple of fixed registers that we know are safe to
612 exclude from being clobbered by calls:
614 The frame pointer is always preserved across calls. The arg pointer
615 is if it is fixed. The stack pointer usually is, unless
616 RETURN_POPS_ARGS, in which case an explicit CLOBBER will be present.
617 If we are generating PIC code, the PIC offset table register is
618 preserved across calls, though the target can override that. */
620 if (i == STACK_POINTER_REGNUM)
622 else if (global_regs[i])
624 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
625 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
627 else if (i == FRAME_POINTER_REGNUM)
629 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
630 else if (i == HARD_FRAME_POINTER_REGNUM)
632 #endif
633 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
634 else if (i == ARG_POINTER_REGNUM && fixed_regs[i])
636 #endif
637 #ifndef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
638 else if (i == (unsigned) PIC_OFFSET_TABLE_REGNUM && fixed_regs[i])
640 #endif
641 else if (CALL_REALLY_USED_REGNO_P (i))
643 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
644 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
648 /* Preserve global registers if called more than once. */
649 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
651 if (global_regs[i])
653 fixed_regs[i] = call_used_regs[i] = call_fixed_regs[i] = 1;
654 SET_HARD_REG_BIT (fixed_reg_set, i);
655 SET_HARD_REG_BIT (call_used_reg_set, i);
656 SET_HARD_REG_BIT (call_fixed_reg_set, i);
660 memset (have_regs_of_mode, 0, sizeof (have_regs_of_mode));
661 memset (contains_reg_of_mode, 0, sizeof (contains_reg_of_mode));
662 for (m = 0; m < (unsigned int) MAX_MACHINE_MODE; m++)
664 HARD_REG_SET ok_regs;
665 CLEAR_HARD_REG_SET (ok_regs);
666 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
667 if (!fixed_regs [j] && HARD_REGNO_MODE_OK (j, m))
668 SET_HARD_REG_BIT (ok_regs, j);
670 for (i = 0; i < N_REG_CLASSES; i++)
671 if ((unsigned) CLASS_MAX_NREGS (i, m) <= reg_class_size[i]
672 && hard_reg_set_intersect_p (ok_regs, reg_class_contents[i]))
674 contains_reg_of_mode [i][m] = 1;
675 have_regs_of_mode [m] = 1;
679 /* Reset move_cost and friends, making sure we only free shared
680 table entries once. */
681 for (i = 0; i < MAX_MACHINE_MODE; i++)
682 if (move_cost[i])
684 for (j = 0; j < i && move_cost[i] != move_cost[j]; j++)
686 if (i == j)
688 free (move_cost[i]);
689 free (may_move_in_cost[i]);
690 free (may_move_out_cost[i]);
693 memset (move_cost, 0, sizeof move_cost);
694 memset (may_move_in_cost, 0, sizeof may_move_in_cost);
695 memset (may_move_out_cost, 0, sizeof may_move_out_cost);
696 last_mode_for_init_move_cost = -1;
699 /* Compute the table of register modes.
700 These values are used to record death information for individual registers
701 (as opposed to a multi-register mode).
702 This function might be invoked more than once, if the target has support
703 for changing register usage conventions on a per-function basis.
706 void
707 init_reg_modes_target (void)
709 int i, j;
711 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
712 for (j = 0; j < MAX_MACHINE_MODE; j++)
713 hard_regno_nregs[i][j] = HARD_REGNO_NREGS(i, (enum machine_mode)j);
715 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
717 reg_raw_mode[i] = choose_hard_reg_mode (i, 1, false);
719 /* If we couldn't find a valid mode, just use the previous mode.
720 ??? One situation in which we need to do this is on the mips where
721 HARD_REGNO_NREGS (fpreg, [SD]Fmode) returns 2. Ideally we'd like
722 to use DF mode for the even registers and VOIDmode for the odd
723 (for the cpu models where the odd ones are inaccessible). */
724 if (reg_raw_mode[i] == VOIDmode)
725 reg_raw_mode[i] = i == 0 ? word_mode : reg_raw_mode[i-1];
729 /* Finish initializing the register sets and initialize the register modes.
730 This function might be invoked more than once, if the target has support
731 for changing register usage conventions on a per-function basis.
734 void
735 init_regs (void)
737 /* This finishes what was started by init_reg_sets, but couldn't be done
738 until after register usage was specified. */
739 init_reg_sets_1 ();
741 init_reg_autoinc ();
744 /* The same as previous function plus initializing IRA if it is
745 necessary. */
746 void
747 reinit_regs (void)
749 init_regs ();
751 if (flag_ira)
752 ira_init ();
755 /* Initialize some fake stack-frame MEM references for use in
756 memory_move_secondary_cost. */
758 void
759 init_fake_stack_mems (void)
762 int i;
764 for (i = 0; i < MAX_MACHINE_MODE; i++)
765 top_of_stack[i] = gen_rtx_MEM (i, stack_pointer_rtx);
770 /* Compute extra cost of moving registers to/from memory due to reloads.
771 Only needed if secondary reloads are required for memory moves. */
774 memory_move_secondary_cost (enum machine_mode mode, enum reg_class rclass, int in)
776 enum reg_class altclass;
777 int partial_cost = 0;
778 /* We need a memory reference to feed to SECONDARY... macros. */
779 /* mem may be unused even if the SECONDARY_ macros are defined. */
780 rtx mem ATTRIBUTE_UNUSED = top_of_stack[(int) mode];
783 altclass = secondary_reload_class (in ? 1 : 0, rclass, mode, mem);
785 if (altclass == NO_REGS)
786 return 0;
788 if (in)
789 partial_cost = REGISTER_MOVE_COST (mode, altclass, rclass);
790 else
791 partial_cost = REGISTER_MOVE_COST (mode, rclass, altclass);
793 if (rclass == altclass)
794 /* This isn't simply a copy-to-temporary situation. Can't guess
795 what it is, so MEMORY_MOVE_COST really ought not to be calling
796 here in that case.
798 I'm tempted to put in an assert here, but returning this will
799 probably only give poor estimates, which is what we would've
800 had before this code anyways. */
801 return partial_cost;
803 /* Check if the secondary reload register will also need a
804 secondary reload. */
805 return memory_move_secondary_cost (mode, altclass, in) + partial_cost;
808 /* Return a machine mode that is legitimate for hard reg REGNO and large
809 enough to save nregs. If we can't find one, return VOIDmode.
810 If CALL_SAVED is true, only consider modes that are call saved. */
812 enum machine_mode
813 choose_hard_reg_mode (unsigned int regno ATTRIBUTE_UNUSED,
814 unsigned int nregs, bool call_saved)
816 unsigned int /* enum machine_mode */ m;
817 enum machine_mode found_mode = VOIDmode, mode;
819 /* We first look for the largest integer mode that can be validly
820 held in REGNO. If none, we look for the largest floating-point mode.
821 If we still didn't find a valid mode, try CCmode. */
823 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
824 mode != VOIDmode;
825 mode = GET_MODE_WIDER_MODE (mode))
826 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
827 && HARD_REGNO_MODE_OK (regno, mode)
828 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
829 found_mode = mode;
831 if (found_mode != VOIDmode)
832 return found_mode;
834 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
835 mode != VOIDmode;
836 mode = GET_MODE_WIDER_MODE (mode))
837 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
838 && HARD_REGNO_MODE_OK (regno, mode)
839 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
840 found_mode = mode;
842 if (found_mode != VOIDmode)
843 return found_mode;
845 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
846 mode != VOIDmode;
847 mode = GET_MODE_WIDER_MODE (mode))
848 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
849 && HARD_REGNO_MODE_OK (regno, mode)
850 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
851 found_mode = mode;
853 if (found_mode != VOIDmode)
854 return found_mode;
856 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT);
857 mode != VOIDmode;
858 mode = GET_MODE_WIDER_MODE (mode))
859 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
860 && HARD_REGNO_MODE_OK (regno, mode)
861 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
862 found_mode = mode;
864 if (found_mode != VOIDmode)
865 return found_mode;
867 /* Iterate over all of the CCmodes. */
868 for (m = (unsigned int) CCmode; m < (unsigned int) NUM_MACHINE_MODES; ++m)
870 mode = (enum machine_mode) m;
871 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
872 && HARD_REGNO_MODE_OK (regno, mode)
873 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
874 return mode;
877 /* We can't find a mode valid for this register. */
878 return VOIDmode;
881 /* Specify the usage characteristics of the register named NAME.
882 It should be a fixed register if FIXED and a
883 call-used register if CALL_USED. */
885 void
886 fix_register (const char *name, int fixed, int call_used)
888 int i;
890 /* Decode the name and update the primary form of
891 the register info. */
893 if ((i = decode_reg_name (name)) >= 0)
895 if ((i == STACK_POINTER_REGNUM
896 #ifdef HARD_FRAME_POINTER_REGNUM
897 || i == HARD_FRAME_POINTER_REGNUM
898 #else
899 || i == FRAME_POINTER_REGNUM
900 #endif
902 && (fixed == 0 || call_used == 0))
904 static const char * const what_option[2][2] = {
905 { "call-saved", "call-used" },
906 { "no-such-option", "fixed" }};
908 error ("can't use '%s' as a %s register", name,
909 what_option[fixed][call_used]);
911 else
913 fixed_regs[i] = fixed;
914 call_used_regs[i] = call_used;
915 #ifdef CALL_REALLY_USED_REGISTERS
916 if (fixed == 0)
917 call_really_used_regs[i] = call_used;
918 #endif
921 else
923 warning (0, "unknown register name: %s", name);
927 /* Mark register number I as global. */
929 void
930 globalize_reg (int i)
932 if (fixed_regs[i] == 0 && no_global_reg_vars)
933 error ("global register variable follows a function definition");
935 if (global_regs[i])
937 warning (0, "register used for two global register variables");
938 return;
941 if (call_used_regs[i] && ! fixed_regs[i])
942 warning (0, "call-clobbered register used for global register variable");
944 global_regs[i] = 1;
946 /* If we're globalizing the frame pointer, we need to set the
947 appropriate regs_invalidated_by_call bit, even if it's already
948 set in fixed_regs. */
949 if (i != STACK_POINTER_REGNUM)
951 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
952 SET_REGNO_REG_SET (regs_invalidated_by_call_regset, i);
955 /* If already fixed, nothing else to do. */
956 if (fixed_regs[i])
957 return;
959 fixed_regs[i] = call_used_regs[i] = call_fixed_regs[i] = 1;
960 #ifdef CALL_REALLY_USED_REGISTERS
961 call_really_used_regs[i] = 1;
962 #endif
964 SET_HARD_REG_BIT (fixed_reg_set, i);
965 SET_HARD_REG_BIT (call_used_reg_set, i);
966 SET_HARD_REG_BIT (call_fixed_reg_set, i);
969 /* Now the data and code for the `regclass' pass, which happens
970 just before local-alloc. */
972 /* The `costs' struct records the cost of using a hard register of each class
973 and of using memory for each pseudo. We use this data to set up
974 register class preferences. */
976 struct costs
978 int cost[N_REG_CLASSES];
979 int mem_cost;
982 /* Structure used to record preferences of given pseudo. */
983 struct reg_pref
985 /* (enum reg_class) prefclass is the preferred class. May be
986 NO_REGS if no class is better than memory. */
987 char prefclass;
989 /* altclass is a register class that we should use for allocating
990 pseudo if no register in the preferred class is available.
991 If no register in this class is available, memory is preferred.
993 It might appear to be more general to have a bitmask of classes here,
994 but since it is recommended that there be a class corresponding to the
995 union of most major pair of classes, that generality is not required. */
996 char altclass;
999 /* Record the cost of each class for each pseudo. */
1001 static struct costs *costs;
1003 /* Initialized once, and used to initialize cost values for each insn. */
1005 static struct costs init_cost;
1007 /* Record preferences of each pseudo.
1008 This is available after `regclass' is run. */
1010 static struct reg_pref *reg_pref;
1012 /* Frequency of executions of current insn. */
1014 static int frequency;
1016 static rtx scan_one_insn (rtx, int);
1017 static void record_operand_costs (rtx, struct costs *, struct reg_pref *);
1018 static void dump_regclass (FILE *);
1019 static void record_reg_classes (int, int, rtx *, enum machine_mode *,
1020 const char **, rtx, struct costs *,
1021 struct reg_pref *);
1022 static int copy_cost (rtx, enum machine_mode, enum reg_class, int,
1023 secondary_reload_info *);
1024 static void record_address_regs (enum machine_mode, rtx, int, enum rtx_code,
1025 enum rtx_code, int);
1026 #ifdef FORBIDDEN_INC_DEC_CLASSES
1027 static int auto_inc_dec_reg_p (rtx, enum machine_mode);
1028 #endif
1029 static void reg_scan_mark_refs (rtx, rtx);
1031 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudo registers. */
1033 static inline bool
1034 ok_for_index_p_nonstrict (rtx reg)
1036 unsigned regno = REGNO (reg);
1037 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
1040 /* A version of regno_ok_for_base_p for use during regclass, when all pseudos
1041 should count as OK. Arguments as for regno_ok_for_base_p. */
1043 static inline bool
1044 ok_for_base_p_nonstrict (rtx reg, enum machine_mode mode,
1045 enum rtx_code outer_code, enum rtx_code index_code)
1047 unsigned regno = REGNO (reg);
1048 if (regno >= FIRST_PSEUDO_REGISTER)
1049 return true;
1051 return ok_for_base_p_1 (regno, mode, outer_code, index_code);
1054 /* Return the reg_class in which pseudo reg number REGNO is best allocated.
1055 This function is sometimes called before the info has been computed.
1056 When that happens, just return GENERAL_REGS, which is innocuous. */
1058 enum reg_class
1059 reg_preferred_class (int regno)
1061 if (reg_pref == 0)
1062 return GENERAL_REGS;
1064 return (enum reg_class) reg_pref[regno].prefclass;
1067 enum reg_class
1068 reg_alternate_class (int regno)
1070 if (reg_pref == 0)
1071 return ALL_REGS;
1073 return (enum reg_class) reg_pref[regno].altclass;
1076 /* Initialize some global data for this pass. */
1078 static unsigned int
1079 regclass_init (void)
1081 int i;
1083 if (df)
1084 df_compute_regs_ever_live (true);
1086 init_cost.mem_cost = 10000;
1087 for (i = 0; i < N_REG_CLASSES; i++)
1088 init_cost.cost[i] = 10000;
1090 /* This prevents dump_flow_info from losing if called
1091 before regclass is run. */
1092 reg_pref = NULL;
1094 /* No more global register variables may be declared. */
1095 no_global_reg_vars = 1;
1096 return 1;
1099 struct rtl_opt_pass pass_regclass_init =
1102 RTL_PASS,
1103 "regclass", /* name */
1104 NULL, /* gate */
1105 regclass_init, /* execute */
1106 NULL, /* sub */
1107 NULL, /* next */
1108 0, /* static_pass_number */
1109 0, /* tv_id */
1110 0, /* properties_required */
1111 0, /* properties_provided */
1112 0, /* properties_destroyed */
1113 0, /* todo_flags_start */
1114 0 /* todo_flags_finish */
1120 /* Dump register costs. */
1121 static void
1122 dump_regclass (FILE *dump)
1124 int i;
1125 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1127 int /* enum reg_class */ rclass;
1128 if (REG_N_REFS (i))
1130 fprintf (dump, " Register %i costs:", i);
1131 for (rclass = 0; rclass < (int) N_REG_CLASSES; rclass++)
1132 if (contains_reg_of_mode [(enum reg_class) rclass][PSEUDO_REGNO_MODE (i)]
1133 #ifdef FORBIDDEN_INC_DEC_CLASSES
1134 && (!in_inc_dec[i]
1135 || !forbidden_inc_dec_class[(enum reg_class) rclass])
1136 #endif
1137 #ifdef CANNOT_CHANGE_MODE_CLASS
1138 && ! invalid_mode_change_p (i, (enum reg_class) rclass,
1139 PSEUDO_REGNO_MODE (i))
1140 #endif
1142 fprintf (dump, " %s:%i", reg_class_names[rclass],
1143 costs[i].cost[(enum reg_class) rclass]);
1144 fprintf (dump, " MEM:%i\n", costs[i].mem_cost);
1150 /* Calculate the costs of insn operands. */
1152 static void
1153 record_operand_costs (rtx insn, struct costs *op_costs,
1154 struct reg_pref *reg_pref)
1156 const char *constraints[MAX_RECOG_OPERANDS];
1157 enum machine_mode modes[MAX_RECOG_OPERANDS];
1158 int i;
1160 for (i = 0; i < recog_data.n_operands; i++)
1162 constraints[i] = recog_data.constraints[i];
1163 modes[i] = recog_data.operand_mode[i];
1166 /* If we get here, we are set up to record the costs of all the
1167 operands for this insn. Start by initializing the costs.
1168 Then handle any address registers. Finally record the desired
1169 classes for any pseudos, doing it twice if some pair of
1170 operands are commutative. */
1172 for (i = 0; i < recog_data.n_operands; i++)
1174 op_costs[i] = init_cost;
1176 if (GET_CODE (recog_data.operand[i]) == SUBREG)
1177 recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
1179 if (MEM_P (recog_data.operand[i]))
1180 record_address_regs (GET_MODE (recog_data.operand[i]),
1181 XEXP (recog_data.operand[i], 0),
1182 0, MEM, SCRATCH, frequency * 2);
1183 else if (recog_data.alternative_enabled_p[0]
1184 && (constraints[i][0] == 'p'
1185 || EXTRA_ADDRESS_CONSTRAINT (constraints[i][0], constraints[i])))
1186 record_address_regs (VOIDmode, recog_data.operand[i], 0, ADDRESS,
1187 SCRATCH, frequency * 2);
1190 /* Check for commutative in a separate loop so everything will
1191 have been initialized. We must do this even if one operand
1192 is a constant--see addsi3 in m68k.md. */
1194 for (i = 0; i < (int) recog_data.n_operands - 1; i++)
1195 if (constraints[i][0] == '%')
1197 const char *xconstraints[MAX_RECOG_OPERANDS];
1198 int j;
1200 /* Handle commutative operands by swapping the constraints.
1201 We assume the modes are the same. */
1203 for (j = 0; j < recog_data.n_operands; j++)
1204 xconstraints[j] = constraints[j];
1206 xconstraints[i] = constraints[i+1];
1207 xconstraints[i+1] = constraints[i];
1208 record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
1209 recog_data.operand, modes,
1210 xconstraints, insn, op_costs, reg_pref);
1213 record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
1214 recog_data.operand, modes,
1215 constraints, insn, op_costs, reg_pref);
1218 /* Subroutine of regclass, processes one insn INSN. Scan it and record each
1219 time it would save code to put a certain register in a certain class.
1220 PASS, when nonzero, inhibits some optimizations which need only be done
1221 once.
1222 Return the last insn processed, so that the scan can be continued from
1223 there. */
1225 static rtx
1226 scan_one_insn (rtx insn, int pass ATTRIBUTE_UNUSED)
1228 enum rtx_code pat_code;
1229 rtx set, note;
1230 int i, j;
1231 struct costs op_costs[MAX_RECOG_OPERANDS];
1233 if (!INSN_P (insn))
1234 return insn;
1236 pat_code = GET_CODE (PATTERN (insn));
1237 if (pat_code == USE
1238 || pat_code == CLOBBER
1239 || pat_code == ASM_INPUT
1240 || pat_code == ADDR_VEC
1241 || pat_code == ADDR_DIFF_VEC)
1242 return insn;
1244 set = single_set (insn);
1245 extract_insn (insn);
1247 /* If this insn loads a parameter from its stack slot, then
1248 it represents a savings, rather than a cost, if the
1249 parameter is stored in memory. Record this fact. */
1251 if (set != 0 && REG_P (SET_DEST (set))
1252 && MEM_P (SET_SRC (set))
1253 && (note = find_reg_note (insn, REG_EQUIV,
1254 NULL_RTX)) != 0
1255 && MEM_P (XEXP (note, 0)))
1257 costs[REGNO (SET_DEST (set))].mem_cost
1258 -= (MEMORY_MOVE_COST (GET_MODE (SET_DEST (set)),
1259 GENERAL_REGS, 1)
1260 * frequency);
1261 record_address_regs (GET_MODE (SET_SRC (set)), XEXP (SET_SRC (set), 0),
1262 0, MEM, SCRATCH, frequency * 2);
1263 return insn;
1266 record_operand_costs (insn, op_costs, reg_pref);
1268 /* Now add the cost for each operand to the total costs for
1269 its register. */
1271 for (i = 0; i < recog_data.n_operands; i++)
1272 if (REG_P (recog_data.operand[i])
1273 && REGNO (recog_data.operand[i]) >= FIRST_PSEUDO_REGISTER)
1275 int regno = REGNO (recog_data.operand[i]);
1276 struct costs *p = &costs[regno], *q = &op_costs[i];
1278 p->mem_cost += q->mem_cost * frequency;
1279 for (j = 0; j < N_REG_CLASSES; j++)
1280 p->cost[j] += q->cost[j] * frequency;
1283 return insn;
1286 /* Initialize information about which register classes can be used for
1287 pseudos that are auto-incremented or auto-decremented. */
1289 static void
1290 init_reg_autoinc (void)
1292 #ifdef FORBIDDEN_INC_DEC_CLASSES
1293 int i;
1295 memset (forbidden_inc_dec_class, 0, sizeof forbidden_inc_dec_class);
1296 for (i = 0; i < N_REG_CLASSES; i++)
1298 rtx r = gen_rtx_raw_REG (VOIDmode, 0);
1299 enum machine_mode m;
1300 int j;
1302 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1303 if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
1305 SET_REGNO (r, j);
1307 for (m = VOIDmode; (int) m < (int) MAX_MACHINE_MODE;
1308 m = (enum machine_mode) ((int) m + 1))
1309 if (HARD_REGNO_MODE_OK (j, m))
1311 /* ??? There are two assumptions here; that the base class does not
1312 depend on the exact outer code (POST_INC vs. PRE_INC etc.), and
1313 that it does not depend on the machine mode of the memory
1314 reference. */
1315 enum reg_class base_class
1316 = base_reg_class (VOIDmode, POST_INC, SCRATCH);
1318 PUT_MODE (r, m);
1320 /* If a register is not directly suitable for an
1321 auto-increment or decrement addressing mode and
1322 requires secondary reloads, disallow its class from
1323 being used in such addresses. */
1325 if ((secondary_reload_class (0, base_class, m, r)
1326 || secondary_reload_class (1, base_class, m, r))
1327 && ! auto_inc_dec_reg_p (r, m))
1328 forbidden_inc_dec_class[i] = 1;
1332 #endif /* FORBIDDEN_INC_DEC_CLASSES */
1335 /* This is a pass of the compiler that scans all instructions
1336 and calculates the preferred class for each pseudo-register.
1337 This information can be accessed later by calling `reg_preferred_class'.
1338 This pass comes just before local register allocation. */
1340 void
1341 regclass (rtx f, int nregs)
1343 rtx insn;
1344 int i;
1345 int pass;
1346 max_regno = max_reg_num ();
1348 init_recog ();
1350 reg_renumber = XNEWVEC (short, max_regno);
1351 reg_pref = XCNEWVEC (struct reg_pref, max_regno);
1352 memset (reg_renumber, -1, max_regno * sizeof (short));
1354 costs = XNEWVEC (struct costs, nregs);
1356 #ifdef FORBIDDEN_INC_DEC_CLASSES
1358 in_inc_dec = XNEWVEC (char, nregs);
1360 #endif /* FORBIDDEN_INC_DEC_CLASSES */
1362 /* Normally we scan the insns once and determine the best class to use for
1363 each register. However, if -fexpensive_optimizations are on, we do so
1364 twice, the second time using the tentative best classes to guide the
1365 selection. */
1367 for (pass = 0; pass <= flag_expensive_optimizations; pass++)
1369 basic_block bb;
1371 if (dump_file)
1372 fprintf (dump_file, "\n\nPass %i\n\n",pass);
1373 /* Zero out our accumulation of the cost of each class for each reg. */
1375 memset (costs, 0, nregs * sizeof (struct costs));
1377 #ifdef FORBIDDEN_INC_DEC_CLASSES
1378 memset (in_inc_dec, 0, nregs);
1379 #endif
1381 /* Scan the instructions and record each time it would
1382 save code to put a certain register in a certain class. */
1384 if (!optimize)
1386 frequency = REG_FREQ_MAX;
1387 for (insn = f; insn; insn = NEXT_INSN (insn))
1388 insn = scan_one_insn (insn, pass);
1390 else
1391 FOR_EACH_BB (bb)
1393 /* Show that an insn inside a loop is likely to be executed three
1394 times more than insns outside a loop. This is much more
1395 aggressive than the assumptions made elsewhere and is being
1396 tried as an experiment. */
1397 frequency = REG_FREQ_FROM_BB (bb);
1398 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
1400 insn = scan_one_insn (insn, pass);
1401 if (insn == BB_END (bb))
1402 break;
1406 /* Now for each register look at how desirable each class is
1407 and find which class is preferred. Store that in
1408 `prefclass'. Record in `altclass' the largest register
1409 class any of whose registers is better than memory. */
1411 if (dump_file)
1413 dump_regclass (dump_file);
1414 fprintf (dump_file,"\n");
1416 for (i = FIRST_PSEUDO_REGISTER; i < nregs; i++)
1418 int best_cost = (1 << (HOST_BITS_PER_INT - 2)) - 1;
1419 enum reg_class best = ALL_REGS, alt = NO_REGS;
1420 /* This is an enum reg_class, but we call it an int
1421 to save lots of casts. */
1422 int rclass;
1423 struct costs *p = &costs[i];
1425 if (regno_reg_rtx[i] == NULL)
1426 continue;
1428 /* In non-optimizing compilation REG_N_REFS is not initialized
1429 yet. */
1430 if (optimize && !REG_N_REFS (i) && !REG_N_SETS (i))
1431 continue;
1433 for (rclass = (int) ALL_REGS - 1; rclass > 0; rclass--)
1435 /* Ignore classes that are too small for this operand or
1436 invalid for an operand that was auto-incremented. */
1437 if (!contains_reg_of_mode [rclass][PSEUDO_REGNO_MODE (i)]
1438 #ifdef FORBIDDEN_INC_DEC_CLASSES
1439 || (in_inc_dec[i] && forbidden_inc_dec_class[rclass])
1440 #endif
1441 #ifdef CANNOT_CHANGE_MODE_CLASS
1442 || invalid_mode_change_p (i, (enum reg_class) rclass,
1443 PSEUDO_REGNO_MODE (i))
1444 #endif
1447 else if (p->cost[rclass] < best_cost)
1449 best_cost = p->cost[rclass];
1450 best = (enum reg_class) rclass;
1452 else if (p->cost[rclass] == best_cost)
1453 best = reg_class_subunion[(int) best][rclass];
1456 /* If no register class is better than memory, use memory. */
1457 if (p->mem_cost < best_cost)
1458 best = NO_REGS;
1460 /* Record the alternate register class; i.e., a class for which
1461 every register in it is better than using memory. If adding a
1462 class would make a smaller class (i.e., no union of just those
1463 classes exists), skip that class. The major unions of classes
1464 should be provided as a register class. Don't do this if we
1465 will be doing it again later. */
1467 if ((pass == 1 || dump_file) || ! flag_expensive_optimizations)
1468 for (rclass = 0; rclass < N_REG_CLASSES; rclass++)
1469 if (p->cost[rclass] < p->mem_cost
1470 && (reg_class_size[(int) reg_class_subunion[(int) alt][rclass]]
1471 > reg_class_size[(int) alt])
1472 #ifdef FORBIDDEN_INC_DEC_CLASSES
1473 && ! (in_inc_dec[i] && forbidden_inc_dec_class[rclass])
1474 #endif
1475 #ifdef CANNOT_CHANGE_MODE_CLASS
1476 && ! invalid_mode_change_p (i, (enum reg_class) rclass,
1477 PSEUDO_REGNO_MODE (i))
1478 #endif
1480 alt = reg_class_subunion[(int) alt][rclass];
1482 /* If we don't add any classes, nothing to try. */
1483 if (alt == best)
1484 alt = NO_REGS;
1486 if (dump_file
1487 && (reg_pref[i].prefclass != (int) best
1488 || reg_pref[i].altclass != (int) alt))
1490 fprintf (dump_file, " Register %i", i);
1491 if (alt == ALL_REGS || best == ALL_REGS)
1492 fprintf (dump_file, " pref %s\n", reg_class_names[(int) best]);
1493 else if (alt == NO_REGS)
1494 fprintf (dump_file, " pref %s or none\n", reg_class_names[(int) best]);
1495 else
1496 fprintf (dump_file, " pref %s, else %s\n",
1497 reg_class_names[(int) best],
1498 reg_class_names[(int) alt]);
1501 /* We cast to (int) because (char) hits bugs in some compilers. */
1502 reg_pref[i].prefclass = (int) best;
1503 reg_pref[i].altclass = (int) alt;
1507 #ifdef FORBIDDEN_INC_DEC_CLASSES
1508 free (in_inc_dec);
1509 #endif
1510 free (costs);
1513 /* Record the cost of using memory or registers of various classes for
1514 the operands in INSN.
1516 N_ALTS is the number of alternatives.
1518 N_OPS is the number of operands.
1520 OPS is an array of the operands.
1522 MODES are the modes of the operands, in case any are VOIDmode.
1524 CONSTRAINTS are the constraints to use for the operands. This array
1525 is modified by this procedure.
1527 This procedure works alternative by alternative. For each alternative
1528 we assume that we will be able to allocate all pseudos to their ideal
1529 register class and calculate the cost of using that alternative. Then
1530 we compute for each operand that is a pseudo-register, the cost of
1531 having the pseudo allocated to each register class and using it in that
1532 alternative. To this cost is added the cost of the alternative.
1534 The cost of each class for this insn is its lowest cost among all the
1535 alternatives. */
1537 static void
1538 record_reg_classes (int n_alts, int n_ops, rtx *ops,
1539 enum machine_mode *modes, const char **constraints,
1540 rtx insn, struct costs *op_costs,
1541 struct reg_pref *reg_pref)
1543 int alt;
1544 int i, j;
1545 rtx set;
1547 /* Process each alternative, each time minimizing an operand's cost with
1548 the cost for each operand in that alternative. */
1550 for (alt = 0; alt < n_alts; alt++)
1552 struct costs this_op_costs[MAX_RECOG_OPERANDS];
1553 int alt_fail = 0;
1554 int alt_cost = 0;
1555 enum reg_class classes[MAX_RECOG_OPERANDS];
1556 int allows_mem[MAX_RECOG_OPERANDS];
1557 int rclass;
1559 for (i = 0; i < n_ops; i++)
1561 const char *p = constraints[i];
1562 rtx op = ops[i];
1563 enum machine_mode mode = modes[i];
1564 int allows_addr = 0;
1565 int win = 0;
1566 unsigned char c;
1568 /* Initially show we know nothing about the register class. */
1569 classes[i] = NO_REGS;
1570 allows_mem[i] = 0;
1572 /* If this operand has no constraints at all, we can conclude
1573 nothing about it since anything is valid. */
1575 if (*p == 0)
1577 if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
1578 memset (&this_op_costs[i], 0, sizeof this_op_costs[i]);
1580 continue;
1583 /* If this alternative is only relevant when this operand
1584 matches a previous operand, we do different things depending
1585 on whether this operand is a pseudo-reg or not. We must process
1586 any modifiers for the operand before we can make this test. */
1588 while (*p == '%' || *p == '=' || *p == '+' || *p == '&')
1589 p++;
1591 if (p[0] >= '0' && p[0] <= '0' + i && (p[1] == ',' || p[1] == 0))
1593 /* Copy class and whether memory is allowed from the matching
1594 alternative. Then perform any needed cost computations
1595 and/or adjustments. */
1596 j = p[0] - '0';
1597 classes[i] = classes[j];
1598 allows_mem[i] = allows_mem[j];
1600 if (!REG_P (op) || REGNO (op) < FIRST_PSEUDO_REGISTER)
1602 /* If this matches the other operand, we have no added
1603 cost and we win. */
1604 if (rtx_equal_p (ops[j], op))
1605 win = 1;
1607 /* If we can put the other operand into a register, add to
1608 the cost of this alternative the cost to copy this
1609 operand to the register used for the other operand. */
1611 else if (classes[j] != NO_REGS)
1613 alt_cost += copy_cost (op, mode, classes[j], 1, NULL);
1614 win = 1;
1617 else if (!REG_P (ops[j])
1618 || REGNO (ops[j]) < FIRST_PSEUDO_REGISTER)
1620 /* This op is a pseudo but the one it matches is not. */
1622 /* If we can't put the other operand into a register, this
1623 alternative can't be used. */
1625 if (classes[j] == NO_REGS)
1626 alt_fail = 1;
1628 /* Otherwise, add to the cost of this alternative the cost
1629 to copy the other operand to the register used for this
1630 operand. */
1632 else
1633 alt_cost += copy_cost (ops[j], mode, classes[j], 1, NULL);
1635 else
1637 /* The costs of this operand are not the same as the other
1638 operand since move costs are not symmetric. Moreover,
1639 if we cannot tie them, this alternative needs to do a
1640 copy, which is one instruction. */
1642 struct costs *pp = &this_op_costs[i];
1643 move_table *intable = NULL;
1644 move_table *outtable = NULL;
1645 int op_class = (int) classes[i];
1647 if (!move_cost[mode])
1648 init_move_cost (mode);
1649 intable = may_move_in_cost[mode];
1650 outtable = may_move_out_cost[mode];
1652 /* The loop is performance critical, so unswitch it manually.
1654 switch (recog_data.operand_type[i])
1656 case OP_INOUT:
1657 for (rclass = 0; rclass < N_REG_CLASSES; rclass++)
1658 pp->cost[rclass] = (intable[rclass][op_class]
1659 + outtable[op_class][rclass]);
1660 break;
1661 case OP_IN:
1662 for (rclass = 0; rclass < N_REG_CLASSES; rclass++)
1663 pp->cost[rclass] = intable[rclass][op_class];
1664 break;
1665 case OP_OUT:
1666 for (rclass = 0; rclass < N_REG_CLASSES; rclass++)
1667 pp->cost[rclass] = outtable[op_class][rclass];
1668 break;
1671 /* If the alternative actually allows memory, make things
1672 a bit cheaper since we won't need an extra insn to
1673 load it. */
1675 pp->mem_cost
1676 = ((recog_data.operand_type[i] != OP_IN
1677 ? MEMORY_MOVE_COST (mode, classes[i], 0)
1678 : 0)
1679 + (recog_data.operand_type[i] != OP_OUT
1680 ? MEMORY_MOVE_COST (mode, classes[i], 1)
1681 : 0) - allows_mem[i]);
1683 /* If we have assigned a class to this register in our
1684 first pass, add a cost to this alternative corresponding
1685 to what we would add if this register were not in the
1686 appropriate class. */
1688 if (reg_pref && reg_pref[REGNO (op)].prefclass != NO_REGS)
1689 alt_cost
1690 += (may_move_in_cost[mode]
1691 [(unsigned char) reg_pref[REGNO (op)].prefclass]
1692 [(int) classes[i]]);
1694 if (REGNO (ops[i]) != REGNO (ops[j])
1695 && ! find_reg_note (insn, REG_DEAD, op))
1696 alt_cost += 2;
1698 /* This is in place of ordinary cost computation
1699 for this operand, so skip to the end of the
1700 alternative (should be just one character). */
1701 while (*p && *p++ != ',')
1704 constraints[i] = p;
1705 continue;
1709 /* Scan all the constraint letters. See if the operand matches
1710 any of the constraints. Collect the valid register classes
1711 and see if this operand accepts memory. */
1713 while ((c = *p))
1715 switch (c)
1717 case ',':
1718 break;
1719 case '*':
1720 /* Ignore the next letter for this pass. */
1721 c = *++p;
1722 break;
1724 case '?':
1725 alt_cost += 2;
1726 case '!': case '#': case '&':
1727 case '0': case '1': case '2': case '3': case '4':
1728 case '5': case '6': case '7': case '8': case '9':
1729 break;
1731 case 'p':
1732 allows_addr = 1;
1733 win = address_operand (op, GET_MODE (op));
1734 /* We know this operand is an address, so we want it to be
1735 allocated to a register that can be the base of an
1736 address, i.e. BASE_REG_CLASS. */
1737 classes[i]
1738 = reg_class_subunion[(int) classes[i]]
1739 [(int) base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
1740 break;
1742 case TARGET_MEM_CONSTRAINT: case 'o': case 'V':
1743 /* It doesn't seem worth distinguishing between offsettable
1744 and non-offsettable addresses here. */
1745 allows_mem[i] = 1;
1746 if (MEM_P (op))
1747 win = 1;
1748 break;
1750 case '<':
1751 if (MEM_P (op)
1752 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
1753 || GET_CODE (XEXP (op, 0)) == POST_DEC))
1754 win = 1;
1755 break;
1757 case '>':
1758 if (MEM_P (op)
1759 && (GET_CODE (XEXP (op, 0)) == PRE_INC
1760 || GET_CODE (XEXP (op, 0)) == POST_INC))
1761 win = 1;
1762 break;
1764 case 'E':
1765 case 'F':
1766 if (GET_CODE (op) == CONST_DOUBLE
1767 || (GET_CODE (op) == CONST_VECTOR
1768 && (GET_MODE_CLASS (GET_MODE (op))
1769 == MODE_VECTOR_FLOAT)))
1770 win = 1;
1771 break;
1773 case 'G':
1774 case 'H':
1775 if (GET_CODE (op) == CONST_DOUBLE
1776 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
1777 win = 1;
1778 break;
1780 case 's':
1781 if (GET_CODE (op) == CONST_INT
1782 || (GET_CODE (op) == CONST_DOUBLE
1783 && GET_MODE (op) == VOIDmode))
1784 break;
1785 case 'i':
1786 if (CONSTANT_P (op)
1787 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op)))
1788 win = 1;
1789 break;
1791 case 'n':
1792 if (GET_CODE (op) == CONST_INT
1793 || (GET_CODE (op) == CONST_DOUBLE
1794 && GET_MODE (op) == VOIDmode))
1795 win = 1;
1796 break;
1798 case 'I':
1799 case 'J':
1800 case 'K':
1801 case 'L':
1802 case 'M':
1803 case 'N':
1804 case 'O':
1805 case 'P':
1806 if (GET_CODE (op) == CONST_INT
1807 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
1808 win = 1;
1809 break;
1811 case 'X':
1812 win = 1;
1813 break;
1815 case 'g':
1816 if (MEM_P (op)
1817 || (CONSTANT_P (op)
1818 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))))
1819 win = 1;
1820 allows_mem[i] = 1;
1821 case 'r':
1822 classes[i]
1823 = reg_class_subunion[(int) classes[i]][(int) GENERAL_REGS];
1824 break;
1826 default:
1827 if (REG_CLASS_FROM_CONSTRAINT (c, p) != NO_REGS)
1828 classes[i]
1829 = reg_class_subunion[(int) classes[i]]
1830 [(int) REG_CLASS_FROM_CONSTRAINT (c, p)];
1831 #ifdef EXTRA_CONSTRAINT_STR
1832 else if (EXTRA_CONSTRAINT_STR (op, c, p))
1833 win = 1;
1835 if (EXTRA_MEMORY_CONSTRAINT (c, p))
1837 /* Every MEM can be reloaded to fit. */
1838 allows_mem[i] = 1;
1839 if (MEM_P (op))
1840 win = 1;
1842 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1844 /* Every address can be reloaded to fit. */
1845 allows_addr = 1;
1846 if (address_operand (op, GET_MODE (op)))
1847 win = 1;
1848 /* We know this operand is an address, so we want it to
1849 be allocated to a register that can be the base of an
1850 address, i.e. BASE_REG_CLASS. */
1851 classes[i]
1852 = reg_class_subunion[(int) classes[i]]
1853 [(int) base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
1855 #endif
1856 break;
1858 p += CONSTRAINT_LEN (c, p);
1859 if (c == ',')
1860 break;
1863 constraints[i] = p;
1865 /* How we account for this operand now depends on whether it is a
1866 pseudo register or not. If it is, we first check if any
1867 register classes are valid. If not, we ignore this alternative,
1868 since we want to assume that all pseudos get allocated for
1869 register preferencing. If some register class is valid, compute
1870 the costs of moving the pseudo into that class. */
1872 if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
1874 if (classes[i] == NO_REGS)
1876 /* We must always fail if the operand is a REG, but
1877 we did not find a suitable class.
1879 Otherwise we may perform an uninitialized read
1880 from this_op_costs after the `continue' statement
1881 below. */
1882 alt_fail = 1;
1884 else
1886 struct costs *pp = &this_op_costs[i];
1887 move_table *intable = NULL;
1888 move_table *outtable = NULL;
1889 int op_class = (int) classes[i];
1891 if (!move_cost[mode])
1892 init_move_cost (mode);
1893 intable = may_move_in_cost[mode];
1894 outtable = may_move_out_cost[mode];
1896 /* The loop is performance critical, so unswitch it manually.
1898 switch (recog_data.operand_type[i])
1900 case OP_INOUT:
1901 for (rclass = 0; rclass < N_REG_CLASSES; rclass++)
1902 pp->cost[rclass] = (intable[rclass][op_class]
1903 + outtable[op_class][rclass]);
1904 break;
1905 case OP_IN:
1906 for (rclass = 0; rclass < N_REG_CLASSES; rclass++)
1907 pp->cost[rclass] = intable[rclass][op_class];
1908 break;
1909 case OP_OUT:
1910 for (rclass = 0; rclass < N_REG_CLASSES; rclass++)
1911 pp->cost[rclass] = outtable[op_class][rclass];
1912 break;
1915 /* If the alternative actually allows memory, make things
1916 a bit cheaper since we won't need an extra insn to
1917 load it. */
1919 pp->mem_cost
1920 = ((recog_data.operand_type[i] != OP_IN
1921 ? MEMORY_MOVE_COST (mode, classes[i], 0)
1922 : 0)
1923 + (recog_data.operand_type[i] != OP_OUT
1924 ? MEMORY_MOVE_COST (mode, classes[i], 1)
1925 : 0) - allows_mem[i]);
1927 /* If we have assigned a class to this register in our
1928 first pass, add a cost to this alternative corresponding
1929 to what we would add if this register were not in the
1930 appropriate class. */
1932 if (reg_pref && reg_pref[REGNO (op)].prefclass != NO_REGS)
1933 alt_cost
1934 += (may_move_in_cost[mode]
1935 [(unsigned char) reg_pref[REGNO (op)].prefclass]
1936 [(int) classes[i]]);
1940 /* Otherwise, if this alternative wins, either because we
1941 have already determined that or if we have a hard register of
1942 the proper class, there is no cost for this alternative. */
1944 else if (win
1945 || (REG_P (op)
1946 && reg_fits_class_p (op, classes[i], 0, GET_MODE (op))))
1949 /* If registers are valid, the cost of this alternative includes
1950 copying the object to and/or from a register. */
1952 else if (classes[i] != NO_REGS)
1954 if (recog_data.operand_type[i] != OP_OUT)
1955 alt_cost += copy_cost (op, mode, classes[i], 1, NULL);
1957 if (recog_data.operand_type[i] != OP_IN)
1958 alt_cost += copy_cost (op, mode, classes[i], 0, NULL);
1961 /* The only other way this alternative can be used is if this is a
1962 constant that could be placed into memory. */
1964 else if (CONSTANT_P (op) && (allows_addr || allows_mem[i]))
1965 alt_cost += MEMORY_MOVE_COST (mode, classes[i], 1);
1966 else
1967 alt_fail = 1;
1970 if (alt_fail)
1971 continue;
1973 if (!recog_data.alternative_enabled_p[alt])
1974 continue;
1976 /* Finally, update the costs with the information we've calculated
1977 about this alternative. */
1979 for (i = 0; i < n_ops; i++)
1980 if (REG_P (ops[i])
1981 && REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
1983 struct costs *pp = &op_costs[i], *qq = &this_op_costs[i];
1984 int scale = 1 + (recog_data.operand_type[i] == OP_INOUT);
1986 pp->mem_cost = MIN (pp->mem_cost,
1987 (qq->mem_cost + alt_cost) * scale);
1989 for (rclass = 0; rclass < N_REG_CLASSES; rclass++)
1990 pp->cost[rclass] = MIN (pp->cost[rclass],
1991 (qq->cost[rclass] + alt_cost) * scale);
1995 /* If this insn is a single set copying operand 1 to operand 0
1996 and one operand is a pseudo with the other a hard reg or a pseudo
1997 that prefers a register that is in its own register class then
1998 we may want to adjust the cost of that register class to -1.
2000 Avoid the adjustment if the source does not die to avoid stressing of
2001 register allocator by preferencing two colliding registers into single
2002 class.
2004 Also avoid the adjustment if a copy between registers of the class
2005 is expensive (ten times the cost of a default copy is considered
2006 arbitrarily expensive). This avoids losing when the preferred class
2007 is very expensive as the source of a copy instruction. */
2009 if ((set = single_set (insn)) != 0
2010 && ops[0] == SET_DEST (set) && ops[1] == SET_SRC (set)
2011 && REG_P (ops[0]) && REG_P (ops[1])
2012 && find_regno_note (insn, REG_DEAD, REGNO (ops[1])))
2013 for (i = 0; i <= 1; i++)
2014 if (REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
2016 unsigned int regno = REGNO (ops[!i]);
2017 enum machine_mode mode = GET_MODE (ops[!i]);
2018 int rclass;
2020 if (regno >= FIRST_PSEUDO_REGISTER && reg_pref != 0
2021 && reg_pref[regno].prefclass != NO_REGS)
2023 enum reg_class pref = reg_pref[regno].prefclass;
2025 if ((reg_class_size[(unsigned char) pref]
2026 == (unsigned) CLASS_MAX_NREGS (pref, mode))
2027 && REGISTER_MOVE_COST (mode, pref, pref) < 10 * 2)
2028 op_costs[i].cost[(unsigned char) pref] = -1;
2030 else if (regno < FIRST_PSEUDO_REGISTER)
2031 for (rclass = 0; rclass < N_REG_CLASSES; rclass++)
2032 if (TEST_HARD_REG_BIT (reg_class_contents[rclass], regno)
2033 && reg_class_size[rclass] == (unsigned) CLASS_MAX_NREGS (rclass, mode))
2035 if (reg_class_size[rclass] == 1)
2036 op_costs[i].cost[rclass] = -1;
2037 else if (in_hard_reg_set_p (reg_class_contents[rclass],
2038 mode, regno))
2039 op_costs[i].cost[rclass] = -1;
2044 /* Compute the cost of loading X into (if TO_P is nonzero) or from (if
2045 TO_P is zero) a register of class CLASS in mode MODE.
2047 X must not be a pseudo. */
2049 static int
2050 copy_cost (rtx x, enum machine_mode mode, enum reg_class rclass, int to_p,
2051 secondary_reload_info *prev_sri)
2053 enum reg_class secondary_class = NO_REGS;
2054 secondary_reload_info sri;
2056 /* If X is a SCRATCH, there is actually nothing to move since we are
2057 assuming optimal allocation. */
2059 if (GET_CODE (x) == SCRATCH)
2060 return 0;
2062 /* Get the class we will actually use for a reload. */
2063 rclass = PREFERRED_RELOAD_CLASS (x, rclass);
2065 /* If we need a secondary reload for an intermediate, the
2066 cost is that to load the input into the intermediate register, then
2067 to copy it. */
2069 sri.prev_sri = prev_sri;
2070 sri.extra_cost = 0;
2071 secondary_class = targetm.secondary_reload (to_p, x, rclass, mode, &sri);
2073 if (!move_cost[mode])
2074 init_move_cost (mode);
2076 if (secondary_class != NO_REGS)
2077 return (move_cost[mode][(int) secondary_class][(int) rclass]
2078 + sri.extra_cost
2079 + copy_cost (x, mode, secondary_class, to_p, &sri));
2081 /* For memory, use the memory move cost, for (hard) registers, use the
2082 cost to move between the register classes, and use 2 for everything
2083 else (constants). */
2085 if (MEM_P (x) || rclass == NO_REGS)
2086 return sri.extra_cost + MEMORY_MOVE_COST (mode, rclass, to_p);
2088 else if (REG_P (x))
2089 return (sri.extra_cost
2090 + move_cost[mode][(int) REGNO_REG_CLASS (REGNO (x))][(int) rclass]);
2092 else
2093 /* If this is a constant, we may eventually want to call rtx_cost here. */
2094 return sri.extra_cost + COSTS_N_INSNS (1);
2097 /* Record the pseudo registers we must reload into hard registers
2098 in a subexpression of a memory address, X.
2100 If CONTEXT is 0, we are looking at the base part of an address, otherwise we
2101 are looking at the index part.
2103 MODE is the mode of the memory reference; OUTER_CODE and INDEX_CODE
2104 give the context that the rtx appears in. These three arguments are
2105 passed down to base_reg_class.
2107 SCALE is twice the amount to multiply the cost by (it is twice so we
2108 can represent half-cost adjustments). */
2110 static void
2111 record_address_regs (enum machine_mode mode, rtx x, int context,
2112 enum rtx_code outer_code, enum rtx_code index_code,
2113 int scale)
2115 enum rtx_code code = GET_CODE (x);
2116 enum reg_class rclass;
2118 if (context == 1)
2119 rclass = INDEX_REG_CLASS;
2120 else
2121 rclass = base_reg_class (mode, outer_code, index_code);
2123 switch (code)
2125 case CONST_INT:
2126 case CONST:
2127 case CC0:
2128 case PC:
2129 case SYMBOL_REF:
2130 case LABEL_REF:
2131 return;
2133 case PLUS:
2134 /* When we have an address that is a sum,
2135 we must determine whether registers are "base" or "index" regs.
2136 If there is a sum of two registers, we must choose one to be
2137 the "base". Luckily, we can use the REG_POINTER to make a good
2138 choice most of the time. We only need to do this on machines
2139 that can have two registers in an address and where the base
2140 and index register classes are different.
2142 ??? This code used to set REGNO_POINTER_FLAG in some cases, but
2143 that seems bogus since it should only be set when we are sure
2144 the register is being used as a pointer. */
2147 rtx arg0 = XEXP (x, 0);
2148 rtx arg1 = XEXP (x, 1);
2149 enum rtx_code code0 = GET_CODE (arg0);
2150 enum rtx_code code1 = GET_CODE (arg1);
2152 /* Look inside subregs. */
2153 if (code0 == SUBREG)
2154 arg0 = SUBREG_REG (arg0), code0 = GET_CODE (arg0);
2155 if (code1 == SUBREG)
2156 arg1 = SUBREG_REG (arg1), code1 = GET_CODE (arg1);
2158 /* If this machine only allows one register per address, it must
2159 be in the first operand. */
2161 if (MAX_REGS_PER_ADDRESS == 1)
2162 record_address_regs (mode, arg0, 0, PLUS, code1, scale);
2164 /* If index and base registers are the same on this machine, just
2165 record registers in any non-constant operands. We assume here,
2166 as well as in the tests below, that all addresses are in
2167 canonical form. */
2169 else if (INDEX_REG_CLASS == base_reg_class (VOIDmode, PLUS, SCRATCH))
2171 record_address_regs (mode, arg0, context, PLUS, code1, scale);
2172 if (! CONSTANT_P (arg1))
2173 record_address_regs (mode, arg1, context, PLUS, code0, scale);
2176 /* If the second operand is a constant integer, it doesn't change
2177 what class the first operand must be. */
2179 else if (code1 == CONST_INT || code1 == CONST_DOUBLE)
2180 record_address_regs (mode, arg0, context, PLUS, code1, scale);
2182 /* If the second operand is a symbolic constant, the first operand
2183 must be an index register. */
2185 else if (code1 == SYMBOL_REF || code1 == CONST || code1 == LABEL_REF)
2186 record_address_regs (mode, arg0, 1, PLUS, code1, scale);
2188 /* If both operands are registers but one is already a hard register
2189 of index or reg-base class, give the other the class that the
2190 hard register is not. */
2192 else if (code0 == REG && code1 == REG
2193 && REGNO (arg0) < FIRST_PSEUDO_REGISTER
2194 && (ok_for_base_p_nonstrict (arg0, mode, PLUS, REG)
2195 || ok_for_index_p_nonstrict (arg0)))
2196 record_address_regs (mode, arg1,
2197 ok_for_base_p_nonstrict (arg0, mode, PLUS, REG)
2198 ? 1 : 0,
2199 PLUS, REG, scale);
2200 else if (code0 == REG && code1 == REG
2201 && REGNO (arg1) < FIRST_PSEUDO_REGISTER
2202 && (ok_for_base_p_nonstrict (arg1, mode, PLUS, REG)
2203 || ok_for_index_p_nonstrict (arg1)))
2204 record_address_regs (mode, arg0,
2205 ok_for_base_p_nonstrict (arg1, mode, PLUS, REG)
2206 ? 1 : 0,
2207 PLUS, REG, scale);
2209 /* If one operand is known to be a pointer, it must be the base
2210 with the other operand the index. Likewise if the other operand
2211 is a MULT. */
2213 else if ((code0 == REG && REG_POINTER (arg0))
2214 || code1 == MULT)
2216 record_address_regs (mode, arg0, 0, PLUS, code1, scale);
2217 record_address_regs (mode, arg1, 1, PLUS, code0, scale);
2219 else if ((code1 == REG && REG_POINTER (arg1))
2220 || code0 == MULT)
2222 record_address_regs (mode, arg0, 1, PLUS, code1, scale);
2223 record_address_regs (mode, arg1, 0, PLUS, code0, scale);
2226 /* Otherwise, count equal chances that each might be a base
2227 or index register. This case should be rare. */
2229 else
2231 record_address_regs (mode, arg0, 0, PLUS, code1, scale / 2);
2232 record_address_regs (mode, arg0, 1, PLUS, code1, scale / 2);
2233 record_address_regs (mode, arg1, 0, PLUS, code0, scale / 2);
2234 record_address_regs (mode, arg1, 1, PLUS, code0, scale / 2);
2237 break;
2239 /* Double the importance of a pseudo register that is incremented
2240 or decremented, since it would take two extra insns
2241 if it ends up in the wrong place. */
2242 case POST_MODIFY:
2243 case PRE_MODIFY:
2244 record_address_regs (mode, XEXP (x, 0), 0, code,
2245 GET_CODE (XEXP (XEXP (x, 1), 1)), 2 * scale);
2246 if (REG_P (XEXP (XEXP (x, 1), 1)))
2247 record_address_regs (mode, XEXP (XEXP (x, 1), 1), 1, code, REG,
2248 2 * scale);
2249 break;
2251 case POST_INC:
2252 case PRE_INC:
2253 case POST_DEC:
2254 case PRE_DEC:
2255 /* Double the importance of a pseudo register that is incremented
2256 or decremented, since it would take two extra insns
2257 if it ends up in the wrong place. If the operand is a pseudo,
2258 show it is being used in an INC_DEC context. */
2260 #ifdef FORBIDDEN_INC_DEC_CLASSES
2261 if (REG_P (XEXP (x, 0))
2262 && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER)
2263 in_inc_dec[REGNO (XEXP (x, 0))] = 1;
2264 #endif
2266 record_address_regs (mode, XEXP (x, 0), 0, code, SCRATCH, 2 * scale);
2267 break;
2269 case REG:
2271 struct costs *pp = &costs[REGNO (x)];
2272 int i;
2274 pp->mem_cost += (MEMORY_MOVE_COST (Pmode, rclass, 1) * scale) / 2;
2276 if (!move_cost[Pmode])
2277 init_move_cost (Pmode);
2278 for (i = 0; i < N_REG_CLASSES; i++)
2279 pp->cost[i] += (may_move_in_cost[Pmode][i][(int) rclass] * scale) / 2;
2281 break;
2283 default:
2285 const char *fmt = GET_RTX_FORMAT (code);
2286 int i;
2287 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2288 if (fmt[i] == 'e')
2289 record_address_regs (mode, XEXP (x, i), context, code, SCRATCH,
2290 scale);
2295 #ifdef FORBIDDEN_INC_DEC_CLASSES
2297 /* Return 1 if REG is valid as an auto-increment memory reference
2298 to an object of MODE. */
2300 static int
2301 auto_inc_dec_reg_p (rtx reg, enum machine_mode mode)
2303 if (HAVE_POST_INCREMENT
2304 && memory_address_p (mode, gen_rtx_POST_INC (Pmode, reg)))
2305 return 1;
2307 if (HAVE_POST_DECREMENT
2308 && memory_address_p (mode, gen_rtx_POST_DEC (Pmode, reg)))
2309 return 1;
2311 if (HAVE_PRE_INCREMENT
2312 && memory_address_p (mode, gen_rtx_PRE_INC (Pmode, reg)))
2313 return 1;
2315 if (HAVE_PRE_DECREMENT
2316 && memory_address_p (mode, gen_rtx_PRE_DEC (Pmode, reg)))
2317 return 1;
2319 return 0;
2321 #endif
2324 /* Allocate space for reg info. */
2325 void
2326 allocate_reg_info (void)
2328 int size = max_reg_num ();
2330 gcc_assert (! reg_pref && ! reg_renumber);
2331 reg_renumber = XNEWVEC (short, size);
2332 reg_pref = XCNEWVEC (struct reg_pref, size);
2333 memset (reg_renumber, -1, size * sizeof (short));
2337 /* Resize reg info. The new elements will be uninitialized. */
2338 void
2339 resize_reg_info (void)
2341 int size = max_reg_num ();
2343 gcc_assert (reg_pref && reg_renumber);
2344 reg_renumber = XRESIZEVEC (short, reg_renumber, size);
2345 reg_pref = XRESIZEVEC (struct reg_pref, reg_pref, size);
2349 /* Free up the space allocated by allocate_reg_info. */
2350 void
2351 free_reg_info (void)
2353 if (reg_pref)
2355 free (reg_pref);
2356 reg_pref = NULL;
2359 if (reg_renumber)
2361 free (reg_renumber);
2362 reg_renumber = NULL;
2369 /* Set up preferred and alternate classes for REGNO as PREFCLASS and
2370 ALTCLASS. */
2371 void
2372 setup_reg_classes (int regno,
2373 enum reg_class prefclass, enum reg_class altclass)
2375 if (reg_pref == NULL)
2376 return;
2377 reg_pref[regno].prefclass = prefclass;
2378 reg_pref[regno].altclass = altclass;
2382 /* This is the `regscan' pass of the compiler, run just before cse and
2383 again just before loop. It finds the first and last use of each
2384 pseudo-register. */
2386 void
2387 reg_scan (rtx f, unsigned int nregs ATTRIBUTE_UNUSED)
2389 rtx insn;
2391 timevar_push (TV_REG_SCAN);
2393 for (insn = f; insn; insn = NEXT_INSN (insn))
2394 if (INSN_P (insn))
2396 reg_scan_mark_refs (PATTERN (insn), insn);
2397 if (REG_NOTES (insn))
2398 reg_scan_mark_refs (REG_NOTES (insn), insn);
2401 timevar_pop (TV_REG_SCAN);
2405 /* X is the expression to scan. INSN is the insn it appears in.
2406 NOTE_FLAG is nonzero if X is from INSN's notes rather than its body.
2407 We should only record information for REGs with numbers
2408 greater than or equal to MIN_REGNO. */
2410 static void
2411 reg_scan_mark_refs (rtx x, rtx insn)
2413 enum rtx_code code;
2414 rtx dest;
2415 rtx note;
2417 if (!x)
2418 return;
2419 code = GET_CODE (x);
2420 switch (code)
2422 case CONST:
2423 case CONST_INT:
2424 case CONST_DOUBLE:
2425 case CONST_FIXED:
2426 case CONST_VECTOR:
2427 case CC0:
2428 case PC:
2429 case SYMBOL_REF:
2430 case LABEL_REF:
2431 case ADDR_VEC:
2432 case ADDR_DIFF_VEC:
2433 case REG:
2434 return;
2436 case EXPR_LIST:
2437 if (XEXP (x, 0))
2438 reg_scan_mark_refs (XEXP (x, 0), insn);
2439 if (XEXP (x, 1))
2440 reg_scan_mark_refs (XEXP (x, 1), insn);
2441 break;
2443 case INSN_LIST:
2444 if (XEXP (x, 1))
2445 reg_scan_mark_refs (XEXP (x, 1), insn);
2446 break;
2448 case CLOBBER:
2449 if (MEM_P (XEXP (x, 0)))
2450 reg_scan_mark_refs (XEXP (XEXP (x, 0), 0), insn);
2451 break;
2453 case SET:
2454 /* Count a set of the destination if it is a register. */
2455 for (dest = SET_DEST (x);
2456 GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
2457 || GET_CODE (dest) == ZERO_EXTEND;
2458 dest = XEXP (dest, 0))
2461 /* If this is setting a pseudo from another pseudo or the sum of a
2462 pseudo and a constant integer and the other pseudo is known to be
2463 a pointer, set the destination to be a pointer as well.
2465 Likewise if it is setting the destination from an address or from a
2466 value equivalent to an address or to the sum of an address and
2467 something else.
2469 But don't do any of this if the pseudo corresponds to a user
2470 variable since it should have already been set as a pointer based
2471 on the type. */
2473 if (REG_P (SET_DEST (x))
2474 && REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER
2475 /* If the destination pseudo is set more than once, then other
2476 sets might not be to a pointer value (consider access to a
2477 union in two threads of control in the presence of global
2478 optimizations). So only set REG_POINTER on the destination
2479 pseudo if this is the only set of that pseudo. */
2480 && DF_REG_DEF_COUNT (REGNO (SET_DEST (x))) == 1
2481 && ! REG_USERVAR_P (SET_DEST (x))
2482 && ! REG_POINTER (SET_DEST (x))
2483 && ((REG_P (SET_SRC (x))
2484 && REG_POINTER (SET_SRC (x)))
2485 || ((GET_CODE (SET_SRC (x)) == PLUS
2486 || GET_CODE (SET_SRC (x)) == LO_SUM)
2487 && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2488 && REG_P (XEXP (SET_SRC (x), 0))
2489 && REG_POINTER (XEXP (SET_SRC (x), 0)))
2490 || GET_CODE (SET_SRC (x)) == CONST
2491 || GET_CODE (SET_SRC (x)) == SYMBOL_REF
2492 || GET_CODE (SET_SRC (x)) == LABEL_REF
2493 || (GET_CODE (SET_SRC (x)) == HIGH
2494 && (GET_CODE (XEXP (SET_SRC (x), 0)) == CONST
2495 || GET_CODE (XEXP (SET_SRC (x), 0)) == SYMBOL_REF
2496 || GET_CODE (XEXP (SET_SRC (x), 0)) == LABEL_REF))
2497 || ((GET_CODE (SET_SRC (x)) == PLUS
2498 || GET_CODE (SET_SRC (x)) == LO_SUM)
2499 && (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST
2500 || GET_CODE (XEXP (SET_SRC (x), 1)) == SYMBOL_REF
2501 || GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF))
2502 || ((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
2503 && (GET_CODE (XEXP (note, 0)) == CONST
2504 || GET_CODE (XEXP (note, 0)) == SYMBOL_REF
2505 || GET_CODE (XEXP (note, 0)) == LABEL_REF))))
2506 REG_POINTER (SET_DEST (x)) = 1;
2508 /* If this is setting a register from a register or from a simple
2509 conversion of a register, propagate REG_EXPR. */
2510 if (REG_P (dest) && !REG_ATTRS (dest))
2512 rtx src = SET_SRC (x);
2514 while (GET_CODE (src) == SIGN_EXTEND
2515 || GET_CODE (src) == ZERO_EXTEND
2516 || GET_CODE (src) == TRUNCATE
2517 || (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)))
2518 src = XEXP (src, 0);
2520 set_reg_attrs_from_value (dest, src);
2523 /* ... fall through ... */
2525 default:
2527 const char *fmt = GET_RTX_FORMAT (code);
2528 int i;
2529 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2531 if (fmt[i] == 'e')
2532 reg_scan_mark_refs (XEXP (x, i), insn);
2533 else if (fmt[i] == 'E' && XVEC (x, i) != 0)
2535 int j;
2536 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2537 reg_scan_mark_refs (XVECEXP (x, i, j), insn);
2544 /* Return nonzero if C1 is a subset of C2, i.e., if every register in C1
2545 is also in C2. */
2548 reg_class_subset_p (enum reg_class c1, enum reg_class c2)
2550 return (c1 == c2
2551 || c2 == ALL_REGS
2552 || hard_reg_set_subset_p (reg_class_contents[(int) c1],
2553 reg_class_contents[(int) c2]));
2556 /* Return nonzero if there is a register that is in both C1 and C2. */
2559 reg_classes_intersect_p (enum reg_class c1, enum reg_class c2)
2561 return (c1 == c2
2562 || c1 == ALL_REGS
2563 || c2 == ALL_REGS
2564 || hard_reg_set_intersect_p (reg_class_contents[(int) c1],
2565 reg_class_contents[(int) c2]));
2568 #ifdef CANNOT_CHANGE_MODE_CLASS
2570 struct subregs_of_mode_node
2572 unsigned int block;
2573 unsigned char modes[MAX_MACHINE_MODE];
2576 static htab_t subregs_of_mode;
2578 static hashval_t
2579 som_hash (const void *x)
2581 const struct subregs_of_mode_node *const a =
2582 (const struct subregs_of_mode_node *) x;
2583 return a->block;
2586 static int
2587 som_eq (const void *x, const void *y)
2589 const struct subregs_of_mode_node *const a =
2590 (const struct subregs_of_mode_node *) x;
2591 const struct subregs_of_mode_node *const b =
2592 (const struct subregs_of_mode_node *) y;
2593 return a->block == b->block;
2597 static void
2598 record_subregs_of_mode (rtx subreg)
2600 struct subregs_of_mode_node dummy, *node;
2601 enum machine_mode mode;
2602 unsigned int regno;
2603 void **slot;
2605 if (!REG_P (SUBREG_REG (subreg)))
2606 return;
2608 regno = REGNO (SUBREG_REG (subreg));
2609 mode = GET_MODE (subreg);
2611 if (regno < FIRST_PSEUDO_REGISTER)
2612 return;
2614 dummy.block = regno & -8;
2615 slot = htab_find_slot_with_hash (subregs_of_mode, &dummy,
2616 dummy.block, INSERT);
2617 node = (struct subregs_of_mode_node *) *slot;
2618 if (node == NULL)
2620 node = XCNEW (struct subregs_of_mode_node);
2621 node->block = regno & -8;
2622 *slot = node;
2625 node->modes[mode] |= 1 << (regno & 7);
2629 /* Call record_subregs_of_mode for all the subregs in X. */
2631 static void
2632 find_subregs_of_mode (rtx x)
2634 enum rtx_code code = GET_CODE (x);
2635 const char * const fmt = GET_RTX_FORMAT (code);
2636 int i;
2638 if (code == SUBREG)
2639 record_subregs_of_mode (x);
2641 /* Time for some deep diving. */
2642 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2644 if (fmt[i] == 'e')
2645 find_subregs_of_mode (XEXP (x, i));
2646 else if (fmt[i] == 'E')
2648 int j;
2649 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2650 find_subregs_of_mode (XVECEXP (x, i, j));
2655 static unsigned int
2656 init_subregs_of_mode (void)
2658 basic_block bb;
2659 rtx insn;
2661 if (subregs_of_mode)
2662 htab_empty (subregs_of_mode);
2663 else
2664 subregs_of_mode = htab_create (100, som_hash, som_eq, free);
2666 FOR_EACH_BB (bb)
2667 FOR_BB_INSNS (bb, insn)
2668 if (INSN_P (insn))
2669 find_subregs_of_mode (PATTERN (insn));
2671 return 0;
2675 /* Set bits in *USED which correspond to registers which can't change
2676 their mode from FROM to any mode in which REGNO was encountered. */
2678 void
2679 cannot_change_mode_set_regs (HARD_REG_SET *used, enum machine_mode from,
2680 unsigned int regno)
2682 struct subregs_of_mode_node dummy, *node;
2683 enum machine_mode to;
2684 unsigned char mask;
2685 unsigned int i;
2687 gcc_assert (subregs_of_mode);
2688 dummy.block = regno & -8;
2689 node = (struct subregs_of_mode_node *)
2690 htab_find_with_hash (subregs_of_mode, &dummy, dummy.block);
2691 if (node == NULL)
2692 return;
2694 mask = 1 << (regno & 7);
2695 for (to = VOIDmode; to < NUM_MACHINE_MODES; to++)
2696 if (node->modes[to] & mask)
2697 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2698 if (!TEST_HARD_REG_BIT (*used, i)
2699 && REG_CANNOT_CHANGE_MODE_P (i, from, to))
2700 SET_HARD_REG_BIT (*used, i);
2703 /* Return 1 if REGNO has had an invalid mode change in CLASS from FROM
2704 mode. */
2706 bool
2707 invalid_mode_change_p (unsigned int regno,
2708 enum reg_class rclass ATTRIBUTE_UNUSED,
2709 enum machine_mode from)
2711 struct subregs_of_mode_node dummy, *node;
2712 enum machine_mode to;
2713 unsigned char mask;
2715 gcc_assert (subregs_of_mode);
2716 dummy.block = regno & -8;
2717 node = (struct subregs_of_mode_node *)
2718 htab_find_with_hash (subregs_of_mode, &dummy, dummy.block);
2719 if (node == NULL)
2720 return false;
2722 mask = 1 << (regno & 7);
2723 for (to = VOIDmode; to < NUM_MACHINE_MODES; to++)
2724 if (node->modes[to] & mask)
2725 if (CANNOT_CHANGE_MODE_CLASS (from, to, rclass))
2726 return true;
2728 return false;
2731 static unsigned int
2732 finish_subregs_of_mode (void)
2734 htab_delete (subregs_of_mode);
2735 subregs_of_mode = 0;
2736 return 0;
2738 #else
2739 static unsigned int
2740 init_subregs_of_mode (void)
2742 return 0;
2744 static unsigned int
2745 finish_subregs_of_mode (void)
2747 return 0;
2750 #endif /* CANNOT_CHANGE_MODE_CLASS */
2752 static bool
2753 gate_subregs_of_mode_init (void)
2755 #ifdef CANNOT_CHANGE_MODE_CLASS
2756 return true;
2757 #else
2758 return false;
2759 #endif
2762 struct rtl_opt_pass pass_subregs_of_mode_init =
2765 RTL_PASS,
2766 "subregs_of_mode_init", /* name */
2767 gate_subregs_of_mode_init, /* gate */
2768 init_subregs_of_mode, /* execute */
2769 NULL, /* sub */
2770 NULL, /* next */
2771 0, /* static_pass_number */
2772 0, /* tv_id */
2773 0, /* properties_required */
2774 0, /* properties_provided */
2775 0, /* properties_destroyed */
2776 0, /* todo_flags_start */
2777 0 /* todo_flags_finish */
2781 struct rtl_opt_pass pass_subregs_of_mode_finish =
2784 RTL_PASS,
2785 "subregs_of_mode_finish", /* name */
2786 gate_subregs_of_mode_init, /* gate */
2787 finish_subregs_of_mode, /* execute */
2788 NULL, /* sub */
2789 NULL, /* next */
2790 0, /* static_pass_number */
2791 0, /* tv_id */
2792 0, /* properties_required */
2793 0, /* properties_provided */
2794 0, /* properties_destroyed */
2795 0, /* todo_flags_start */
2796 0 /* todo_flags_finish */
2802 #include "gt-regclass.h"