2008-06-04 Xinliang David Li <davidxl@google.com>
[official-gcc.git] / gcc / regclass.c
blob8b9e86b1411d5ca39a322fa8cfd84d83a002c5f9
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"
53 /* Maximum register number used in this function, plus one. */
55 int max_regno;
57 static void init_reg_sets_1 (void);
58 static void init_reg_autoinc (void);
60 /* If we have auto-increment or auto-decrement and we can have secondary
61 reloads, we are not allowed to use classes requiring secondary
62 reloads for pseudos auto-incremented since reload can't handle it. */
63 /* We leave it to target hooks to decide if we have secondary reloads, so
64 assume that we might have them. */
65 #if defined(AUTO_INC_DEC) /* */
66 #define FORBIDDEN_INC_DEC_CLASSES
67 #endif
69 /* Register tables used by many passes. */
71 /* Indexed by hard register number, contains 1 for registers
72 that are fixed use (stack pointer, pc, frame pointer, etc.).
73 These are the registers that cannot be used to allocate
74 a pseudo reg for general use. */
76 char fixed_regs[FIRST_PSEUDO_REGISTER];
78 /* Same info as a HARD_REG_SET. */
80 HARD_REG_SET fixed_reg_set;
82 /* Data for initializing the above. */
84 static const char initial_fixed_regs[] = FIXED_REGISTERS;
86 /* Indexed by hard register number, contains 1 for registers
87 that are fixed use or are clobbered by function calls.
88 These are the registers that cannot be used to allocate
89 a pseudo reg whose life crosses calls unless we are able
90 to save/restore them across the calls. */
92 char call_used_regs[FIRST_PSEUDO_REGISTER];
94 /* Same info as a HARD_REG_SET. */
96 HARD_REG_SET call_used_reg_set;
98 /* HARD_REG_SET of registers we want to avoid caller saving. */
99 HARD_REG_SET losing_caller_save_reg_set;
101 /* Data for initializing the above. */
103 static const char initial_call_used_regs[] = CALL_USED_REGISTERS;
105 /* This is much like call_used_regs, except it doesn't have to
106 be a superset of FIXED_REGISTERS. This vector indicates
107 what is really call clobbered, and is used when defining
108 regs_invalidated_by_call. */
110 #ifdef CALL_REALLY_USED_REGISTERS
111 char call_really_used_regs[] = CALL_REALLY_USED_REGISTERS;
112 #endif
114 #ifdef CALL_REALLY_USED_REGISTERS
115 #define CALL_REALLY_USED_REGNO_P(X) call_really_used_regs[X]
116 #else
117 #define CALL_REALLY_USED_REGNO_P(X) call_used_regs[X]
118 #endif
121 /* Indexed by hard register number, contains 1 for registers that are
122 fixed use or call used registers that cannot hold quantities across
123 calls even if we are willing to save and restore them. call fixed
124 registers are a subset of call used registers. */
126 char call_fixed_regs[FIRST_PSEUDO_REGISTER];
128 /* The same info as a HARD_REG_SET. */
130 HARD_REG_SET call_fixed_reg_set;
132 /* Indexed by hard register number, contains 1 for registers
133 that are being used for global register decls.
134 These must be exempt from ordinary flow analysis
135 and are also considered fixed. */
137 char global_regs[FIRST_PSEUDO_REGISTER];
139 /* Contains 1 for registers that are set or clobbered by calls. */
140 /* ??? Ideally, this would be just call_used_regs plus global_regs, but
141 for someone's bright idea to have call_used_regs strictly include
142 fixed_regs. Which leaves us guessing as to the set of fixed_regs
143 that are actually preserved. We know for sure that those associated
144 with the local stack frame are safe, but scant others. */
146 HARD_REG_SET regs_invalidated_by_call;
148 /* Table of register numbers in the order in which to try to use them. */
149 #ifdef REG_ALLOC_ORDER
150 int reg_alloc_order[FIRST_PSEUDO_REGISTER] = REG_ALLOC_ORDER;
152 /* The inverse of reg_alloc_order. */
153 int inv_reg_alloc_order[FIRST_PSEUDO_REGISTER];
154 #endif
156 /* For each reg class, a HARD_REG_SET saying which registers are in it. */
158 HARD_REG_SET reg_class_contents[N_REG_CLASSES];
160 /* The same information, but as an array of unsigned ints. We copy from
161 these unsigned ints to the table above. We do this so the tm.h files
162 do not have to be aware of the wordsize for machines with <= 64 regs.
163 Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
165 #define N_REG_INTS \
166 ((FIRST_PSEUDO_REGISTER + (32 - 1)) / 32)
168 static const unsigned int_reg_class_contents[N_REG_CLASSES][N_REG_INTS]
169 = REG_CLASS_CONTENTS;
171 /* For each reg class, number of regs it contains. */
173 unsigned int reg_class_size[N_REG_CLASSES];
175 /* For each reg class, table listing all the containing classes. */
177 static enum reg_class reg_class_superclasses[N_REG_CLASSES][N_REG_CLASSES];
179 /* For each reg class, table listing all the classes contained in it. */
181 static enum reg_class reg_class_subclasses[N_REG_CLASSES][N_REG_CLASSES];
183 /* For each pair of reg classes,
184 a largest reg class contained in their union. */
186 enum reg_class reg_class_subunion[N_REG_CLASSES][N_REG_CLASSES];
188 /* For each pair of reg classes,
189 the smallest reg class containing their union. */
191 enum reg_class reg_class_superunion[N_REG_CLASSES][N_REG_CLASSES];
193 /* Array containing all of the register names. */
195 const char * reg_names[] = REGISTER_NAMES;
197 /* Array containing all of the register class names. */
199 const char * reg_class_names[] = REG_CLASS_NAMES;
201 /* For each hard register, the widest mode object that it can contain.
202 This will be a MODE_INT mode if the register can hold integers. Otherwise
203 it will be a MODE_FLOAT or a MODE_CC mode, whichever is valid for the
204 register. */
206 enum machine_mode reg_raw_mode[FIRST_PSEUDO_REGISTER];
208 /* 1 if there is a register of given mode. */
210 bool have_regs_of_mode [MAX_MACHINE_MODE];
212 /* 1 if class does contain register of given mode. */
214 static char contains_reg_of_mode [N_REG_CLASSES] [MAX_MACHINE_MODE];
216 typedef unsigned short move_table[N_REG_CLASSES];
218 /* Maximum cost of moving from a register in one class to a register in
219 another class. Based on REGISTER_MOVE_COST. */
221 static move_table *move_cost[MAX_MACHINE_MODE];
223 /* Similar, but here we don't have to move if the first index is a subset
224 of the second so in that case the cost is zero. */
226 static move_table *may_move_in_cost[MAX_MACHINE_MODE];
228 /* Similar, but here we don't have to move if the first index is a superset
229 of the second so in that case the cost is zero. */
231 static move_table *may_move_out_cost[MAX_MACHINE_MODE];
233 /* Keep track of the last mode we initialized move costs for. */
234 static int last_mode_for_init_move_cost;
236 #ifdef FORBIDDEN_INC_DEC_CLASSES
238 /* These are the classes that regs which are auto-incremented or decremented
239 cannot be put in. */
241 static int forbidden_inc_dec_class[N_REG_CLASSES];
243 /* Indexed by n, is nonzero if (REG n) is used in an auto-inc or auto-dec
244 context. */
246 static char *in_inc_dec;
248 #endif /* FORBIDDEN_INC_DEC_CLASSES */
250 /* Sample MEM values for use by memory_move_secondary_cost. */
252 static GTY(()) rtx top_of_stack[MAX_MACHINE_MODE];
254 /* No more global register variables may be declared; true once
255 regclass has been initialized. */
257 static int no_global_reg_vars = 0;
259 /* Specify number of hard registers given machine mode occupy. */
260 unsigned char hard_regno_nregs[FIRST_PSEUDO_REGISTER][MAX_MACHINE_MODE];
262 /* Given a register bitmap, turn on the bits in a HARD_REG_SET that
263 correspond to the hard registers, if any, set in that map. This
264 could be done far more efficiently by having all sorts of special-cases
265 with moving single words, but probably isn't worth the trouble. */
267 void
268 reg_set_to_hard_reg_set (HARD_REG_SET *to, const_bitmap from)
270 unsigned i;
271 bitmap_iterator bi;
273 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
275 if (i >= FIRST_PSEUDO_REGISTER)
276 return;
277 SET_HARD_REG_BIT (*to, i);
282 /* Function called only once to initialize the above data on reg usage.
283 Once this is done, various switches may override. */
285 void
286 init_reg_sets (void)
288 int i, j;
290 /* First copy the register information from the initial int form into
291 the regsets. */
293 for (i = 0; i < N_REG_CLASSES; i++)
295 CLEAR_HARD_REG_SET (reg_class_contents[i]);
297 /* Note that we hard-code 32 here, not HOST_BITS_PER_INT. */
298 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
299 if (int_reg_class_contents[i][j / 32]
300 & ((unsigned) 1 << (j % 32)))
301 SET_HARD_REG_BIT (reg_class_contents[i], j);
304 /* Sanity check: make sure the target macros FIXED_REGISTERS and
305 CALL_USED_REGISTERS had the right number of initializers. */
306 gcc_assert (sizeof fixed_regs == sizeof initial_fixed_regs);
307 gcc_assert (sizeof call_used_regs == sizeof initial_call_used_regs);
309 memcpy (fixed_regs, initial_fixed_regs, sizeof fixed_regs);
310 memcpy (call_used_regs, initial_call_used_regs, sizeof call_used_regs);
311 memset (global_regs, 0, sizeof global_regs);
314 /* Initialize may_move_cost and friends for mode M. */
316 static void
317 init_move_cost (enum machine_mode m)
319 static unsigned short last_move_cost[N_REG_CLASSES][N_REG_CLASSES];
320 bool all_match = true;
321 unsigned int i, j;
323 gcc_assert (have_regs_of_mode[m]);
324 for (i = 0; i < N_REG_CLASSES; i++)
325 if (contains_reg_of_mode[i][m])
326 for (j = 0; j < N_REG_CLASSES; j++)
328 int cost;
329 if (!contains_reg_of_mode[j][m])
330 cost = 65535;
331 else
333 cost = REGISTER_MOVE_COST (m, i, j);
334 gcc_assert (cost < 65535);
336 all_match &= (last_move_cost[i][j] == cost);
337 last_move_cost[i][j] = cost;
339 if (all_match && last_mode_for_init_move_cost != -1)
341 move_cost[m] = move_cost[last_mode_for_init_move_cost];
342 may_move_in_cost[m] = may_move_in_cost[last_mode_for_init_move_cost];
343 may_move_out_cost[m] = may_move_out_cost[last_mode_for_init_move_cost];
344 return;
346 last_mode_for_init_move_cost = m;
347 move_cost[m] = (move_table *)xmalloc (sizeof (move_table)
348 * N_REG_CLASSES);
349 may_move_in_cost[m] = (move_table *)xmalloc (sizeof (move_table)
350 * N_REG_CLASSES);
351 may_move_out_cost[m] = (move_table *)xmalloc (sizeof (move_table)
352 * N_REG_CLASSES);
353 for (i = 0; i < N_REG_CLASSES; i++)
354 if (contains_reg_of_mode[i][m])
355 for (j = 0; j < N_REG_CLASSES; j++)
357 int cost;
358 enum reg_class *p1, *p2;
360 if (last_move_cost[i][j] == 65535)
362 move_cost[m][i][j] = 65535;
363 may_move_in_cost[m][i][j] = 65535;
364 may_move_out_cost[m][i][j] = 65535;
366 else
368 cost = last_move_cost[i][j];
370 for (p2 = &reg_class_subclasses[j][0];
371 *p2 != LIM_REG_CLASSES; p2++)
372 if (*p2 != i && contains_reg_of_mode[*p2][m])
373 cost = MAX (cost, move_cost[m][i][*p2]);
375 for (p1 = &reg_class_subclasses[i][0];
376 *p1 != LIM_REG_CLASSES; p1++)
377 if (*p1 != j && contains_reg_of_mode[*p1][m])
378 cost = MAX (cost, move_cost[m][*p1][j]);
380 gcc_assert (cost <= 65535);
381 move_cost[m][i][j] = cost;
383 if (reg_class_subset_p (i, j))
384 may_move_in_cost[m][i][j] = 0;
385 else
386 may_move_in_cost[m][i][j] = cost;
388 if (reg_class_subset_p (j, i))
389 may_move_out_cost[m][i][j] = 0;
390 else
391 may_move_out_cost[m][i][j] = cost;
394 else
395 for (j = 0; j < N_REG_CLASSES; j++)
397 move_cost[m][i][j] = 65535;
398 may_move_in_cost[m][i][j] = 65535;
399 may_move_out_cost[m][i][j] = 65535;
403 /* We need to save copies of some of the register information which
404 can be munged by command-line switches so we can restore it during
405 subsequent back-end reinitialization. */
407 static char saved_fixed_regs[FIRST_PSEUDO_REGISTER];
408 static char saved_call_used_regs[FIRST_PSEUDO_REGISTER];
409 #ifdef CALL_REALLY_USED_REGISTERS
410 static char saved_call_really_used_regs[FIRST_PSEUDO_REGISTER];
411 #endif
412 static const char *saved_reg_names[FIRST_PSEUDO_REGISTER];
414 /* Save the register information. */
416 void
417 save_register_info (void)
419 /* Sanity check: make sure the target macros FIXED_REGISTERS and
420 CALL_USED_REGISTERS had the right number of initializers. */
421 gcc_assert (sizeof fixed_regs == sizeof saved_fixed_regs);
422 gcc_assert (sizeof call_used_regs == sizeof saved_call_used_regs);
423 memcpy (saved_fixed_regs, fixed_regs, sizeof fixed_regs);
424 memcpy (saved_call_used_regs, call_used_regs, sizeof call_used_regs);
426 /* Likewise for call_really_used_regs. */
427 #ifdef CALL_REALLY_USED_REGISTERS
428 gcc_assert (sizeof call_really_used_regs
429 == sizeof saved_call_really_used_regs);
430 memcpy (saved_call_really_used_regs, call_really_used_regs,
431 sizeof call_really_used_regs);
432 #endif
434 /* And similarly for reg_names. */
435 gcc_assert (sizeof reg_names == sizeof saved_reg_names);
436 memcpy (saved_reg_names, reg_names, sizeof reg_names);
439 /* Restore the register information. */
441 static void
442 restore_register_info (void)
444 memcpy (fixed_regs, saved_fixed_regs, sizeof fixed_regs);
445 memcpy (call_used_regs, saved_call_used_regs, sizeof call_used_regs);
447 #ifdef CALL_REALLY_USED_REGISTERS
448 memcpy (call_really_used_regs, saved_call_really_used_regs,
449 sizeof call_really_used_regs);
450 #endif
452 memcpy (reg_names, saved_reg_names, sizeof reg_names);
455 /* After switches have been processed, which perhaps alter
456 `fixed_regs' and `call_used_regs', convert them to HARD_REG_SETs. */
458 static void
459 init_reg_sets_1 (void)
461 unsigned int i, j;
462 unsigned int /* enum machine_mode */ m;
464 restore_register_info ();
466 #ifdef REG_ALLOC_ORDER
467 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
468 inv_reg_alloc_order[reg_alloc_order[i]] = i;
469 #endif
471 /* This macro allows the fixed or call-used registers
472 and the register classes to depend on target flags. */
474 #ifdef CONDITIONAL_REGISTER_USAGE
475 CONDITIONAL_REGISTER_USAGE;
476 #endif
478 /* Compute number of hard regs in each class. */
480 memset (reg_class_size, 0, sizeof reg_class_size);
481 for (i = 0; i < N_REG_CLASSES; i++)
482 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
483 if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
484 reg_class_size[i]++;
486 /* Initialize the table of subunions.
487 reg_class_subunion[I][J] gets the largest-numbered reg-class
488 that is contained in the union of classes I and J. */
490 memset (reg_class_subunion, 0, sizeof reg_class_subunion);
491 for (i = 0; i < N_REG_CLASSES; i++)
493 for (j = 0; j < N_REG_CLASSES; j++)
495 HARD_REG_SET c;
496 int k;
498 COPY_HARD_REG_SET (c, reg_class_contents[i]);
499 IOR_HARD_REG_SET (c, reg_class_contents[j]);
500 for (k = 0; k < N_REG_CLASSES; k++)
501 if (hard_reg_set_subset_p (reg_class_contents[k], c)
502 && !hard_reg_set_subset_p (reg_class_contents[k],
503 reg_class_contents
504 [(int) reg_class_subunion[i][j]]))
505 reg_class_subunion[i][j] = (enum reg_class) k;
509 /* Initialize the table of superunions.
510 reg_class_superunion[I][J] gets the smallest-numbered reg-class
511 containing the union of classes I and J. */
513 memset (reg_class_superunion, 0, sizeof reg_class_superunion);
514 for (i = 0; i < N_REG_CLASSES; i++)
516 for (j = 0; j < N_REG_CLASSES; j++)
518 HARD_REG_SET c;
519 int k;
521 COPY_HARD_REG_SET (c, reg_class_contents[i]);
522 IOR_HARD_REG_SET (c, reg_class_contents[j]);
523 for (k = 0; k < N_REG_CLASSES; k++)
524 if (hard_reg_set_subset_p (c, reg_class_contents[k]))
525 break;
527 reg_class_superunion[i][j] = (enum reg_class) k;
531 /* Initialize the tables of subclasses and superclasses of each reg class.
532 First clear the whole table, then add the elements as they are found. */
534 for (i = 0; i < N_REG_CLASSES; i++)
536 for (j = 0; j < N_REG_CLASSES; j++)
538 reg_class_superclasses[i][j] = LIM_REG_CLASSES;
539 reg_class_subclasses[i][j] = LIM_REG_CLASSES;
543 for (i = 0; i < N_REG_CLASSES; i++)
545 if (i == (int) NO_REGS)
546 continue;
548 for (j = i + 1; j < N_REG_CLASSES; j++)
549 if (hard_reg_set_subset_p (reg_class_contents[i],
550 reg_class_contents[j]))
552 /* Reg class I is a subclass of J.
553 Add J to the table of superclasses of I. */
554 enum reg_class *p;
556 p = &reg_class_superclasses[i][0];
557 while (*p != LIM_REG_CLASSES) p++;
558 *p = (enum reg_class) j;
559 /* Add I to the table of superclasses of J. */
560 p = &reg_class_subclasses[j][0];
561 while (*p != LIM_REG_CLASSES) p++;
562 *p = (enum reg_class) i;
566 /* Initialize "constant" tables. */
568 CLEAR_HARD_REG_SET (fixed_reg_set);
569 CLEAR_HARD_REG_SET (call_used_reg_set);
570 CLEAR_HARD_REG_SET (call_fixed_reg_set);
571 CLEAR_HARD_REG_SET (regs_invalidated_by_call);
572 CLEAR_HARD_REG_SET (losing_caller_save_reg_set);
574 memcpy (call_fixed_regs, fixed_regs, sizeof call_fixed_regs);
576 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
578 /* call_used_regs must include fixed_regs. */
579 gcc_assert (!fixed_regs[i] || call_used_regs[i]);
580 #ifdef CALL_REALLY_USED_REGISTERS
581 /* call_used_regs must include call_really_used_regs. */
582 gcc_assert (!call_really_used_regs[i] || call_used_regs[i]);
583 #endif
585 if (fixed_regs[i])
586 SET_HARD_REG_BIT (fixed_reg_set, i);
588 if (call_used_regs[i])
589 SET_HARD_REG_BIT (call_used_reg_set, i);
590 if (call_fixed_regs[i])
591 SET_HARD_REG_BIT (call_fixed_reg_set, i);
592 if (CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (i)))
593 SET_HARD_REG_BIT (losing_caller_save_reg_set, i);
595 /* There are a couple of fixed registers that we know are safe to
596 exclude from being clobbered by calls:
598 The frame pointer is always preserved across calls. The arg pointer
599 is if it is fixed. The stack pointer usually is, unless
600 RETURN_POPS_ARGS, in which case an explicit CLOBBER will be present.
601 If we are generating PIC code, the PIC offset table register is
602 preserved across calls, though the target can override that. */
604 if (i == STACK_POINTER_REGNUM)
606 else if (global_regs[i])
607 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
608 else if (i == FRAME_POINTER_REGNUM)
610 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
611 else if (i == HARD_FRAME_POINTER_REGNUM)
613 #endif
614 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
615 else if (i == ARG_POINTER_REGNUM && fixed_regs[i])
617 #endif
618 #ifndef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
619 else if (i == (unsigned) PIC_OFFSET_TABLE_REGNUM && fixed_regs[i])
621 #endif
622 else if (CALL_REALLY_USED_REGNO_P (i))
623 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
626 /* Preserve global registers if called more than once. */
627 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
629 if (global_regs[i])
631 fixed_regs[i] = call_used_regs[i] = call_fixed_regs[i] = 1;
632 SET_HARD_REG_BIT (fixed_reg_set, i);
633 SET_HARD_REG_BIT (call_used_reg_set, i);
634 SET_HARD_REG_BIT (call_fixed_reg_set, i);
638 memset (have_regs_of_mode, 0, sizeof (have_regs_of_mode));
639 memset (contains_reg_of_mode, 0, sizeof (contains_reg_of_mode));
640 for (m = 0; m < (unsigned int) MAX_MACHINE_MODE; m++)
642 HARD_REG_SET ok_regs;
643 CLEAR_HARD_REG_SET (ok_regs);
644 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
645 if (!fixed_regs [j] && HARD_REGNO_MODE_OK (j, m))
646 SET_HARD_REG_BIT (ok_regs, j);
648 for (i = 0; i < N_REG_CLASSES; i++)
649 if ((unsigned) CLASS_MAX_NREGS (i, m) <= reg_class_size[i]
650 && hard_reg_set_intersect_p (ok_regs, reg_class_contents[i]))
652 contains_reg_of_mode [i][m] = 1;
653 have_regs_of_mode [m] = 1;
657 /* Reset move_cost and friends, making sure we only free shared
658 table entries once. */
659 for (i = 0; i < MAX_MACHINE_MODE; i++)
660 if (move_cost[i])
662 for (j = 0; j < i && move_cost[i] != move_cost[j]; j++)
664 if (i == j)
666 free (move_cost[i]);
667 free (may_move_in_cost[i]);
668 free (may_move_out_cost[i]);
671 memset (move_cost, 0, sizeof move_cost);
672 memset (may_move_in_cost, 0, sizeof may_move_in_cost);
673 memset (may_move_out_cost, 0, sizeof may_move_out_cost);
674 last_mode_for_init_move_cost = -1;
677 /* Compute the table of register modes.
678 These values are used to record death information for individual registers
679 (as opposed to a multi-register mode).
680 This function might be invoked more than once, if the target has support
681 for changing register usage conventions on a per-function basis.
684 void
685 init_reg_modes_target (void)
687 int i, j;
689 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
690 for (j = 0; j < MAX_MACHINE_MODE; j++)
691 hard_regno_nregs[i][j] = HARD_REGNO_NREGS(i, (enum machine_mode)j);
693 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
695 reg_raw_mode[i] = choose_hard_reg_mode (i, 1, false);
697 /* If we couldn't find a valid mode, just use the previous mode.
698 ??? One situation in which we need to do this is on the mips where
699 HARD_REGNO_NREGS (fpreg, [SD]Fmode) returns 2. Ideally we'd like
700 to use DF mode for the even registers and VOIDmode for the odd
701 (for the cpu models where the odd ones are inaccessible). */
702 if (reg_raw_mode[i] == VOIDmode)
703 reg_raw_mode[i] = i == 0 ? word_mode : reg_raw_mode[i-1];
707 /* Finish initializing the register sets and initialize the register modes.
708 This function might be invoked more than once, if the target has support
709 for changing register usage conventions on a per-function basis.
712 void
713 init_regs (void)
715 /* This finishes what was started by init_reg_sets, but couldn't be done
716 until after register usage was specified. */
717 init_reg_sets_1 ();
719 init_reg_autoinc ();
722 /* Initialize some fake stack-frame MEM references for use in
723 memory_move_secondary_cost. */
725 void
726 init_fake_stack_mems (void)
729 int i;
731 for (i = 0; i < MAX_MACHINE_MODE; i++)
732 top_of_stack[i] = gen_rtx_MEM (i, stack_pointer_rtx);
737 /* Compute extra cost of moving registers to/from memory due to reloads.
738 Only needed if secondary reloads are required for memory moves. */
741 memory_move_secondary_cost (enum machine_mode mode, enum reg_class class, int in)
743 enum reg_class altclass;
744 int partial_cost = 0;
745 /* We need a memory reference to feed to SECONDARY... macros. */
746 /* mem may be unused even if the SECONDARY_ macros are defined. */
747 rtx mem ATTRIBUTE_UNUSED = top_of_stack[(int) mode];
750 altclass = secondary_reload_class (in ? 1 : 0, class, mode, mem);
752 if (altclass == NO_REGS)
753 return 0;
755 if (in)
756 partial_cost = REGISTER_MOVE_COST (mode, altclass, class);
757 else
758 partial_cost = REGISTER_MOVE_COST (mode, class, altclass);
760 if (class == altclass)
761 /* This isn't simply a copy-to-temporary situation. Can't guess
762 what it is, so MEMORY_MOVE_COST really ought not to be calling
763 here in that case.
765 I'm tempted to put in an assert here, but returning this will
766 probably only give poor estimates, which is what we would've
767 had before this code anyways. */
768 return partial_cost;
770 /* Check if the secondary reload register will also need a
771 secondary reload. */
772 return memory_move_secondary_cost (mode, altclass, in) + partial_cost;
775 /* Return a machine mode that is legitimate for hard reg REGNO and large
776 enough to save nregs. If we can't find one, return VOIDmode.
777 If CALL_SAVED is true, only consider modes that are call saved. */
779 enum machine_mode
780 choose_hard_reg_mode (unsigned int regno ATTRIBUTE_UNUSED,
781 unsigned int nregs, bool call_saved)
783 unsigned int /* enum machine_mode */ m;
784 enum machine_mode found_mode = VOIDmode, mode;
786 /* We first look for the largest integer mode that can be validly
787 held in REGNO. If none, we look for the largest floating-point mode.
788 If we still didn't find a valid mode, try CCmode. */
790 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
791 mode != VOIDmode;
792 mode = GET_MODE_WIDER_MODE (mode))
793 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
794 && HARD_REGNO_MODE_OK (regno, mode)
795 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
796 found_mode = mode;
798 if (found_mode != VOIDmode)
799 return found_mode;
801 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
802 mode != VOIDmode;
803 mode = GET_MODE_WIDER_MODE (mode))
804 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
805 && HARD_REGNO_MODE_OK (regno, mode)
806 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
807 found_mode = mode;
809 if (found_mode != VOIDmode)
810 return found_mode;
812 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT);
813 mode != VOIDmode;
814 mode = GET_MODE_WIDER_MODE (mode))
815 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
816 && HARD_REGNO_MODE_OK (regno, mode)
817 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
818 found_mode = mode;
820 if (found_mode != VOIDmode)
821 return found_mode;
823 for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_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 /* Iterate over all of the CCmodes. */
835 for (m = (unsigned int) CCmode; m < (unsigned int) NUM_MACHINE_MODES; ++m)
837 mode = (enum machine_mode) m;
838 if ((unsigned) hard_regno_nregs[regno][mode] == nregs
839 && HARD_REGNO_MODE_OK (regno, mode)
840 && (! call_saved || ! HARD_REGNO_CALL_PART_CLOBBERED (regno, mode)))
841 return mode;
844 /* We can't find a mode valid for this register. */
845 return VOIDmode;
848 /* Specify the usage characteristics of the register named NAME.
849 It should be a fixed register if FIXED and a
850 call-used register if CALL_USED. */
852 void
853 fix_register (const char *name, int fixed, int call_used)
855 int i;
857 /* Decode the name and update the primary form of
858 the register info. */
860 if ((i = decode_reg_name (name)) >= 0)
862 if ((i == STACK_POINTER_REGNUM
863 #ifdef HARD_FRAME_POINTER_REGNUM
864 || i == HARD_FRAME_POINTER_REGNUM
865 #else
866 || i == FRAME_POINTER_REGNUM
867 #endif
869 && (fixed == 0 || call_used == 0))
871 static const char * const what_option[2][2] = {
872 { "call-saved", "call-used" },
873 { "no-such-option", "fixed" }};
875 error ("can't use '%s' as a %s register", name,
876 what_option[fixed][call_used]);
878 else
880 fixed_regs[i] = fixed;
881 call_used_regs[i] = call_used;
882 #ifdef CALL_REALLY_USED_REGISTERS
883 if (fixed == 0)
884 call_really_used_regs[i] = call_used;
885 #endif
888 else
890 warning (0, "unknown register name: %s", name);
894 /* Mark register number I as global. */
896 void
897 globalize_reg (int i)
899 if (fixed_regs[i] == 0 && no_global_reg_vars)
900 error ("global register variable follows a function definition");
902 if (global_regs[i])
904 warning (0, "register used for two global register variables");
905 return;
908 if (call_used_regs[i] && ! fixed_regs[i])
909 warning (0, "call-clobbered register used for global register variable");
911 global_regs[i] = 1;
913 /* If we're globalizing the frame pointer, we need to set the
914 appropriate regs_invalidated_by_call bit, even if it's already
915 set in fixed_regs. */
916 if (i != STACK_POINTER_REGNUM)
917 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
919 /* If already fixed, nothing else to do. */
920 if (fixed_regs[i])
921 return;
923 fixed_regs[i] = call_used_regs[i] = call_fixed_regs[i] = 1;
924 #ifdef CALL_REALLY_USED_REGISTERS
925 call_really_used_regs[i] = 1;
926 #endif
928 SET_HARD_REG_BIT (fixed_reg_set, i);
929 SET_HARD_REG_BIT (call_used_reg_set, i);
930 SET_HARD_REG_BIT (call_fixed_reg_set, i);
933 /* Now the data and code for the `regclass' pass, which happens
934 just before local-alloc. */
936 /* The `costs' struct records the cost of using a hard register of each class
937 and of using memory for each pseudo. We use this data to set up
938 register class preferences. */
940 struct costs
942 int cost[N_REG_CLASSES];
943 int mem_cost;
946 /* Structure used to record preferences of given pseudo. */
947 struct reg_pref
949 /* (enum reg_class) prefclass is the preferred class. May be
950 NO_REGS if no class is better than memory. */
951 char prefclass;
953 /* altclass is a register class that we should use for allocating
954 pseudo if no register in the preferred class is available.
955 If no register in this class is available, memory is preferred.
957 It might appear to be more general to have a bitmask of classes here,
958 but since it is recommended that there be a class corresponding to the
959 union of most major pair of classes, that generality is not required. */
960 char altclass;
963 /* Record the cost of each class for each pseudo. */
965 static struct costs *costs;
967 /* Initialized once, and used to initialize cost values for each insn. */
969 static struct costs init_cost;
971 /* Record preferences of each pseudo.
972 This is available after `regclass' is run. */
974 static struct reg_pref *reg_pref;
976 /* Frequency of executions of current insn. */
978 static int frequency;
980 static rtx scan_one_insn (rtx, int);
981 static void record_operand_costs (rtx, struct costs *, struct reg_pref *);
982 static void dump_regclass (FILE *);
983 static void record_reg_classes (int, int, rtx *, enum machine_mode *,
984 const char **, rtx, struct costs *,
985 struct reg_pref *);
986 static int copy_cost (rtx, enum machine_mode, enum reg_class, int,
987 secondary_reload_info *);
988 static void record_address_regs (enum machine_mode, rtx, int, enum rtx_code,
989 enum rtx_code, int);
990 #ifdef FORBIDDEN_INC_DEC_CLASSES
991 static int auto_inc_dec_reg_p (rtx, enum machine_mode);
992 #endif
993 static void reg_scan_mark_refs (rtx, rtx);
995 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudo registers. */
997 static inline bool
998 ok_for_index_p_nonstrict (rtx reg)
1000 unsigned regno = REGNO (reg);
1001 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
1004 /* A version of regno_ok_for_base_p for use during regclass, when all pseudos
1005 should count as OK. Arguments as for regno_ok_for_base_p. */
1007 static inline bool
1008 ok_for_base_p_nonstrict (rtx reg, enum machine_mode mode,
1009 enum rtx_code outer_code, enum rtx_code index_code)
1011 unsigned regno = REGNO (reg);
1012 if (regno >= FIRST_PSEUDO_REGISTER)
1013 return true;
1015 return ok_for_base_p_1 (regno, mode, outer_code, index_code);
1018 /* Return the reg_class in which pseudo reg number REGNO is best allocated.
1019 This function is sometimes called before the info has been computed.
1020 When that happens, just return GENERAL_REGS, which is innocuous. */
1022 enum reg_class
1023 reg_preferred_class (int regno)
1025 if (reg_pref == 0)
1026 return GENERAL_REGS;
1027 return (enum reg_class) reg_pref[regno].prefclass;
1030 enum reg_class
1031 reg_alternate_class (int regno)
1033 if (reg_pref == 0)
1034 return ALL_REGS;
1036 return (enum reg_class) reg_pref[regno].altclass;
1039 /* Initialize some global data for this pass. */
1041 static unsigned int
1042 regclass_init (void)
1044 int i;
1046 if (df)
1047 df_compute_regs_ever_live (true);
1049 init_cost.mem_cost = 10000;
1050 for (i = 0; i < N_REG_CLASSES; i++)
1051 init_cost.cost[i] = 10000;
1053 /* This prevents dump_flow_info from losing if called
1054 before regclass is run. */
1055 reg_pref = NULL;
1057 /* No more global register variables may be declared. */
1058 no_global_reg_vars = 1;
1059 return 1;
1062 struct rtl_opt_pass pass_regclass_init =
1065 RTL_PASS,
1066 "regclass", /* name */
1067 NULL, /* gate */
1068 regclass_init, /* execute */
1069 NULL, /* sub */
1070 NULL, /* next */
1071 0, /* static_pass_number */
1072 0, /* tv_id */
1073 0, /* properties_required */
1074 0, /* properties_provided */
1075 0, /* properties_destroyed */
1076 0, /* todo_flags_start */
1077 0 /* todo_flags_finish */
1083 /* Dump register costs. */
1084 static void
1085 dump_regclass (FILE *dump)
1087 int i;
1088 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1090 int /* enum reg_class */ class;
1091 if (REG_N_REFS (i))
1093 fprintf (dump, " Register %i costs:", i);
1094 for (class = 0; class < (int) N_REG_CLASSES; class++)
1095 if (contains_reg_of_mode [(enum reg_class) class][PSEUDO_REGNO_MODE (i)]
1096 #ifdef FORBIDDEN_INC_DEC_CLASSES
1097 && (!in_inc_dec[i]
1098 || !forbidden_inc_dec_class[(enum reg_class) class])
1099 #endif
1100 #ifdef CANNOT_CHANGE_MODE_CLASS
1101 && ! invalid_mode_change_p (i, (enum reg_class) class,
1102 PSEUDO_REGNO_MODE (i))
1103 #endif
1105 fprintf (dump, " %s:%i", reg_class_names[class],
1106 costs[i].cost[(enum reg_class) class]);
1107 fprintf (dump, " MEM:%i\n", costs[i].mem_cost);
1113 /* Calculate the costs of insn operands. */
1115 static void
1116 record_operand_costs (rtx insn, struct costs *op_costs,
1117 struct reg_pref *reg_pref)
1119 const char *constraints[MAX_RECOG_OPERANDS];
1120 enum machine_mode modes[MAX_RECOG_OPERANDS];
1121 int i;
1123 for (i = 0; i < recog_data.n_operands; i++)
1125 constraints[i] = recog_data.constraints[i];
1126 modes[i] = recog_data.operand_mode[i];
1129 /* If we get here, we are set up to record the costs of all the
1130 operands for this insn. Start by initializing the costs.
1131 Then handle any address registers. Finally record the desired
1132 classes for any pseudos, doing it twice if some pair of
1133 operands are commutative. */
1135 for (i = 0; i < recog_data.n_operands; i++)
1137 op_costs[i] = init_cost;
1139 if (GET_CODE (recog_data.operand[i]) == SUBREG)
1140 recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
1142 if (MEM_P (recog_data.operand[i]))
1143 record_address_regs (GET_MODE (recog_data.operand[i]),
1144 XEXP (recog_data.operand[i], 0),
1145 0, MEM, SCRATCH, frequency * 2);
1146 else if (recog_data.alternative_enabled_p[0]
1147 && (constraints[i][0] == 'p'
1148 || EXTRA_ADDRESS_CONSTRAINT (constraints[i][0], constraints[i])))
1149 record_address_regs (VOIDmode, recog_data.operand[i], 0, ADDRESS,
1150 SCRATCH, frequency * 2);
1153 /* Check for commutative in a separate loop so everything will
1154 have been initialized. We must do this even if one operand
1155 is a constant--see addsi3 in m68k.md. */
1157 for (i = 0; i < (int) recog_data.n_operands - 1; i++)
1158 if (constraints[i][0] == '%')
1160 const char *xconstraints[MAX_RECOG_OPERANDS];
1161 int j;
1163 /* Handle commutative operands by swapping the constraints.
1164 We assume the modes are the same. */
1166 for (j = 0; j < recog_data.n_operands; j++)
1167 xconstraints[j] = constraints[j];
1169 xconstraints[i] = constraints[i+1];
1170 xconstraints[i+1] = constraints[i];
1171 record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
1172 recog_data.operand, modes,
1173 xconstraints, insn, op_costs, reg_pref);
1176 record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
1177 recog_data.operand, modes,
1178 constraints, insn, op_costs, reg_pref);
1181 /* Subroutine of regclass, processes one insn INSN. Scan it and record each
1182 time it would save code to put a certain register in a certain class.
1183 PASS, when nonzero, inhibits some optimizations which need only be done
1184 once.
1185 Return the last insn processed, so that the scan can be continued from
1186 there. */
1188 static rtx
1189 scan_one_insn (rtx insn, int pass ATTRIBUTE_UNUSED)
1191 enum rtx_code pat_code;
1192 rtx set, note;
1193 int i, j;
1194 struct costs op_costs[MAX_RECOG_OPERANDS];
1196 if (!INSN_P (insn))
1197 return insn;
1199 pat_code = GET_CODE (PATTERN (insn));
1200 if (pat_code == USE
1201 || pat_code == CLOBBER
1202 || pat_code == ASM_INPUT
1203 || pat_code == ADDR_VEC
1204 || pat_code == ADDR_DIFF_VEC)
1205 return insn;
1207 set = single_set (insn);
1208 extract_insn (insn);
1210 /* If this insn loads a parameter from its stack slot, then
1211 it represents a savings, rather than a cost, if the
1212 parameter is stored in memory. Record this fact. */
1214 if (set != 0 && REG_P (SET_DEST (set))
1215 && MEM_P (SET_SRC (set))
1216 && (note = find_reg_note (insn, REG_EQUIV,
1217 NULL_RTX)) != 0
1218 && MEM_P (XEXP (note, 0)))
1220 costs[REGNO (SET_DEST (set))].mem_cost
1221 -= (MEMORY_MOVE_COST (GET_MODE (SET_DEST (set)),
1222 GENERAL_REGS, 1)
1223 * frequency);
1224 record_address_regs (GET_MODE (SET_SRC (set)), XEXP (SET_SRC (set), 0),
1225 0, MEM, SCRATCH, frequency * 2);
1226 return insn;
1229 record_operand_costs (insn, op_costs, reg_pref);
1231 /* Now add the cost for each operand to the total costs for
1232 its register. */
1234 for (i = 0; i < recog_data.n_operands; i++)
1235 if (REG_P (recog_data.operand[i])
1236 && REGNO (recog_data.operand[i]) >= FIRST_PSEUDO_REGISTER)
1238 int regno = REGNO (recog_data.operand[i]);
1239 struct costs *p = &costs[regno], *q = &op_costs[i];
1241 p->mem_cost += q->mem_cost * frequency;
1242 for (j = 0; j < N_REG_CLASSES; j++)
1243 p->cost[j] += q->cost[j] * frequency;
1246 return insn;
1249 /* Initialize information about which register classes can be used for
1250 pseudos that are auto-incremented or auto-decremented. */
1252 static void
1253 init_reg_autoinc (void)
1255 #ifdef FORBIDDEN_INC_DEC_CLASSES
1256 int i;
1258 memset (forbidden_inc_dec_class, 0, sizeof forbidden_inc_dec_class);
1259 for (i = 0; i < N_REG_CLASSES; i++)
1261 rtx r = gen_rtx_raw_REG (VOIDmode, 0);
1262 enum machine_mode m;
1263 int j;
1265 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1266 if (TEST_HARD_REG_BIT (reg_class_contents[i], j))
1268 SET_REGNO (r, j);
1270 for (m = VOIDmode; (int) m < (int) MAX_MACHINE_MODE;
1271 m = (enum machine_mode) ((int) m + 1))
1272 if (HARD_REGNO_MODE_OK (j, m))
1274 /* ??? There are two assumptions here; that the base class does not
1275 depend on the exact outer code (POST_INC vs. PRE_INC etc.), and
1276 that it does not depend on the machine mode of the memory
1277 reference. */
1278 enum reg_class base_class
1279 = base_reg_class (VOIDmode, POST_INC, SCRATCH);
1281 PUT_MODE (r, m);
1283 /* If a register is not directly suitable for an
1284 auto-increment or decrement addressing mode and
1285 requires secondary reloads, disallow its class from
1286 being used in such addresses. */
1288 if ((secondary_reload_class (0, base_class, m, r)
1289 || secondary_reload_class (1, base_class, m, r))
1290 && ! auto_inc_dec_reg_p (r, m))
1291 forbidden_inc_dec_class[i] = 1;
1295 #endif /* FORBIDDEN_INC_DEC_CLASSES */
1298 /* This is a pass of the compiler that scans all instructions
1299 and calculates the preferred class for each pseudo-register.
1300 This information can be accessed later by calling `reg_preferred_class'.
1301 This pass comes just before local register allocation. */
1303 void
1304 regclass (rtx f, int nregs)
1306 rtx insn;
1307 int i;
1308 int pass;
1309 max_regno = max_reg_num ();
1311 init_recog ();
1313 reg_renumber = xmalloc (max_regno * sizeof (short));
1314 reg_pref = XCNEWVEC (struct reg_pref, max_regno);
1315 memset (reg_renumber, -1, max_regno * sizeof (short));
1317 costs = XNEWVEC (struct costs, nregs);
1319 #ifdef FORBIDDEN_INC_DEC_CLASSES
1321 in_inc_dec = XNEWVEC (char, nregs);
1323 #endif /* FORBIDDEN_INC_DEC_CLASSES */
1325 /* Normally we scan the insns once and determine the best class to use for
1326 each register. However, if -fexpensive_optimizations are on, we do so
1327 twice, the second time using the tentative best classes to guide the
1328 selection. */
1330 for (pass = 0; pass <= flag_expensive_optimizations; pass++)
1332 basic_block bb;
1334 if (dump_file)
1335 fprintf (dump_file, "\n\nPass %i\n\n",pass);
1336 /* Zero out our accumulation of the cost of each class for each reg. */
1338 memset (costs, 0, nregs * sizeof (struct costs));
1340 #ifdef FORBIDDEN_INC_DEC_CLASSES
1341 memset (in_inc_dec, 0, nregs);
1342 #endif
1344 /* Scan the instructions and record each time it would
1345 save code to put a certain register in a certain class. */
1347 if (!optimize)
1349 frequency = REG_FREQ_MAX;
1350 for (insn = f; insn; insn = NEXT_INSN (insn))
1351 insn = scan_one_insn (insn, pass);
1353 else
1354 FOR_EACH_BB (bb)
1356 /* Show that an insn inside a loop is likely to be executed three
1357 times more than insns outside a loop. This is much more
1358 aggressive than the assumptions made elsewhere and is being
1359 tried as an experiment. */
1360 frequency = REG_FREQ_FROM_BB (bb);
1361 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
1363 insn = scan_one_insn (insn, pass);
1364 if (insn == BB_END (bb))
1365 break;
1369 /* Now for each register look at how desirable each class is
1370 and find which class is preferred. Store that in
1371 `prefclass'. Record in `altclass' the largest register
1372 class any of whose registers is better than memory. */
1374 if (dump_file)
1376 dump_regclass (dump_file);
1377 fprintf (dump_file,"\n");
1379 for (i = FIRST_PSEUDO_REGISTER; i < nregs; i++)
1381 int best_cost = (1 << (HOST_BITS_PER_INT - 2)) - 1;
1382 enum reg_class best = ALL_REGS, alt = NO_REGS;
1383 /* This is an enum reg_class, but we call it an int
1384 to save lots of casts. */
1385 int class;
1386 struct costs *p = &costs[i];
1388 if (regno_reg_rtx[i] == NULL)
1389 continue;
1391 /* In non-optimizing compilation REG_N_REFS is not initialized
1392 yet. */
1393 if (optimize && !REG_N_REFS (i) && !REG_N_SETS (i))
1394 continue;
1396 for (class = (int) ALL_REGS - 1; class > 0; class--)
1398 /* Ignore classes that are too small for this operand or
1399 invalid for an operand that was auto-incremented. */
1400 if (!contains_reg_of_mode [class][PSEUDO_REGNO_MODE (i)]
1401 #ifdef FORBIDDEN_INC_DEC_CLASSES
1402 || (in_inc_dec[i] && forbidden_inc_dec_class[class])
1403 #endif
1404 #ifdef CANNOT_CHANGE_MODE_CLASS
1405 || invalid_mode_change_p (i, (enum reg_class) class,
1406 PSEUDO_REGNO_MODE (i))
1407 #endif
1410 else if (p->cost[class] < best_cost)
1412 best_cost = p->cost[class];
1413 best = (enum reg_class) class;
1415 else if (p->cost[class] == best_cost)
1416 best = reg_class_subunion[(int) best][class];
1419 /* If no register class is better than memory, use memory. */
1420 if (p->mem_cost < best_cost)
1421 best = NO_REGS;
1423 /* Record the alternate register class; i.e., a class for which
1424 every register in it is better than using memory. If adding a
1425 class would make a smaller class (i.e., no union of just those
1426 classes exists), skip that class. The major unions of classes
1427 should be provided as a register class. Don't do this if we
1428 will be doing it again later. */
1430 if ((pass == 1 || dump_file) || ! flag_expensive_optimizations)
1431 for (class = 0; class < N_REG_CLASSES; class++)
1432 if (p->cost[class] < p->mem_cost
1433 && (reg_class_size[(int) reg_class_subunion[(int) alt][class]]
1434 > reg_class_size[(int) alt])
1435 #ifdef FORBIDDEN_INC_DEC_CLASSES
1436 && ! (in_inc_dec[i] && forbidden_inc_dec_class[class])
1437 #endif
1438 #ifdef CANNOT_CHANGE_MODE_CLASS
1439 && ! invalid_mode_change_p (i, (enum reg_class) class,
1440 PSEUDO_REGNO_MODE (i))
1441 #endif
1443 alt = reg_class_subunion[(int) alt][class];
1445 /* If we don't add any classes, nothing to try. */
1446 if (alt == best)
1447 alt = NO_REGS;
1449 if (dump_file
1450 && (reg_pref[i].prefclass != (int) best
1451 || reg_pref[i].altclass != (int) alt))
1453 fprintf (dump_file, " Register %i", i);
1454 if (alt == ALL_REGS || best == ALL_REGS)
1455 fprintf (dump_file, " pref %s\n", reg_class_names[(int) best]);
1456 else if (alt == NO_REGS)
1457 fprintf (dump_file, " pref %s or none\n", reg_class_names[(int) best]);
1458 else
1459 fprintf (dump_file, " pref %s, else %s\n",
1460 reg_class_names[(int) best],
1461 reg_class_names[(int) alt]);
1464 /* We cast to (int) because (char) hits bugs in some compilers. */
1465 reg_pref[i].prefclass = (int) best;
1466 reg_pref[i].altclass = (int) alt;
1470 #ifdef FORBIDDEN_INC_DEC_CLASSES
1471 free (in_inc_dec);
1472 #endif
1473 free (costs);
1476 /* Record the cost of using memory or registers of various classes for
1477 the operands in INSN.
1479 N_ALTS is the number of alternatives.
1481 N_OPS is the number of operands.
1483 OPS is an array of the operands.
1485 MODES are the modes of the operands, in case any are VOIDmode.
1487 CONSTRAINTS are the constraints to use for the operands. This array
1488 is modified by this procedure.
1490 This procedure works alternative by alternative. For each alternative
1491 we assume that we will be able to allocate all pseudos to their ideal
1492 register class and calculate the cost of using that alternative. Then
1493 we compute for each operand that is a pseudo-register, the cost of
1494 having the pseudo allocated to each register class and using it in that
1495 alternative. To this cost is added the cost of the alternative.
1497 The cost of each class for this insn is its lowest cost among all the
1498 alternatives. */
1500 static void
1501 record_reg_classes (int n_alts, int n_ops, rtx *ops,
1502 enum machine_mode *modes, const char **constraints,
1503 rtx insn, struct costs *op_costs,
1504 struct reg_pref *reg_pref)
1506 int alt;
1507 int i, j;
1508 rtx set;
1510 /* Process each alternative, each time minimizing an operand's cost with
1511 the cost for each operand in that alternative. */
1513 for (alt = 0; alt < n_alts; alt++)
1515 struct costs this_op_costs[MAX_RECOG_OPERANDS];
1516 int alt_fail = 0;
1517 int alt_cost = 0;
1518 enum reg_class classes[MAX_RECOG_OPERANDS];
1519 int allows_mem[MAX_RECOG_OPERANDS];
1520 int class;
1522 for (i = 0; i < n_ops; i++)
1524 const char *p = constraints[i];
1525 rtx op = ops[i];
1526 enum machine_mode mode = modes[i];
1527 int allows_addr = 0;
1528 int win = 0;
1529 unsigned char c;
1531 /* Initially show we know nothing about the register class. */
1532 classes[i] = NO_REGS;
1533 allows_mem[i] = 0;
1535 /* If this operand has no constraints at all, we can conclude
1536 nothing about it since anything is valid. */
1538 if (*p == 0)
1540 if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
1541 memset (&this_op_costs[i], 0, sizeof this_op_costs[i]);
1543 continue;
1546 /* If this alternative is only relevant when this operand
1547 matches a previous operand, we do different things depending
1548 on whether this operand is a pseudo-reg or not. We must process
1549 any modifiers for the operand before we can make this test. */
1551 while (*p == '%' || *p == '=' || *p == '+' || *p == '&')
1552 p++;
1554 if (p[0] >= '0' && p[0] <= '0' + i && (p[1] == ',' || p[1] == 0))
1556 /* Copy class and whether memory is allowed from the matching
1557 alternative. Then perform any needed cost computations
1558 and/or adjustments. */
1559 j = p[0] - '0';
1560 classes[i] = classes[j];
1561 allows_mem[i] = allows_mem[j];
1563 if (!REG_P (op) || REGNO (op) < FIRST_PSEUDO_REGISTER)
1565 /* If this matches the other operand, we have no added
1566 cost and we win. */
1567 if (rtx_equal_p (ops[j], op))
1568 win = 1;
1570 /* If we can put the other operand into a register, add to
1571 the cost of this alternative the cost to copy this
1572 operand to the register used for the other operand. */
1574 else if (classes[j] != NO_REGS)
1576 alt_cost += copy_cost (op, mode, classes[j], 1, NULL);
1577 win = 1;
1580 else if (!REG_P (ops[j])
1581 || REGNO (ops[j]) < FIRST_PSEUDO_REGISTER)
1583 /* This op is a pseudo but the one it matches is not. */
1585 /* If we can't put the other operand into a register, this
1586 alternative can't be used. */
1588 if (classes[j] == NO_REGS)
1589 alt_fail = 1;
1591 /* Otherwise, add to the cost of this alternative the cost
1592 to copy the other operand to the register used for this
1593 operand. */
1595 else
1596 alt_cost += copy_cost (ops[j], mode, classes[j], 1, NULL);
1598 else
1600 /* The costs of this operand are not the same as the other
1601 operand since move costs are not symmetric. Moreover,
1602 if we cannot tie them, this alternative needs to do a
1603 copy, which is one instruction. */
1605 struct costs *pp = &this_op_costs[i];
1606 move_table *intable = NULL;
1607 move_table *outtable = NULL;
1608 int op_class = (int) classes[i];
1610 if (!move_cost[mode])
1611 init_move_cost (mode);
1612 intable = may_move_in_cost[mode];
1613 outtable = may_move_out_cost[mode];
1615 /* The loop is performance critical, so unswitch it manually.
1617 switch (recog_data.operand_type[i])
1619 case OP_INOUT:
1620 for (class = 0; class < N_REG_CLASSES; class++)
1621 pp->cost[class] = (intable[class][op_class]
1622 + outtable[op_class][class]);
1623 break;
1624 case OP_IN:
1625 for (class = 0; class < N_REG_CLASSES; class++)
1626 pp->cost[class] = intable[class][op_class];
1627 break;
1628 case OP_OUT:
1629 for (class = 0; class < N_REG_CLASSES; class++)
1630 pp->cost[class] = outtable[op_class][class];
1631 break;
1634 /* If the alternative actually allows memory, make things
1635 a bit cheaper since we won't need an extra insn to
1636 load it. */
1638 pp->mem_cost
1639 = ((recog_data.operand_type[i] != OP_IN
1640 ? MEMORY_MOVE_COST (mode, classes[i], 0)
1641 : 0)
1642 + (recog_data.operand_type[i] != OP_OUT
1643 ? MEMORY_MOVE_COST (mode, classes[i], 1)
1644 : 0) - allows_mem[i]);
1646 /* If we have assigned a class to this register in our
1647 first pass, add a cost to this alternative corresponding
1648 to what we would add if this register were not in the
1649 appropriate class. */
1651 if (reg_pref && reg_pref[REGNO (op)].prefclass != NO_REGS)
1652 alt_cost
1653 += (may_move_in_cost[mode]
1654 [(unsigned char) reg_pref[REGNO (op)].prefclass]
1655 [(int) classes[i]]);
1657 if (REGNO (ops[i]) != REGNO (ops[j])
1658 && ! find_reg_note (insn, REG_DEAD, op))
1659 alt_cost += 2;
1661 /* This is in place of ordinary cost computation
1662 for this operand, so skip to the end of the
1663 alternative (should be just one character). */
1664 while (*p && *p++ != ',')
1667 constraints[i] = p;
1668 continue;
1672 /* Scan all the constraint letters. See if the operand matches
1673 any of the constraints. Collect the valid register classes
1674 and see if this operand accepts memory. */
1676 while ((c = *p))
1678 switch (c)
1680 case ',':
1681 break;
1682 case '*':
1683 /* Ignore the next letter for this pass. */
1684 c = *++p;
1685 break;
1687 case '?':
1688 alt_cost += 2;
1689 case '!': case '#': case '&':
1690 case '0': case '1': case '2': case '3': case '4':
1691 case '5': case '6': case '7': case '8': case '9':
1692 break;
1694 case 'p':
1695 allows_addr = 1;
1696 win = address_operand (op, GET_MODE (op));
1697 /* We know this operand is an address, so we want it to be
1698 allocated to a register that can be the base of an
1699 address, i.e. BASE_REG_CLASS. */
1700 classes[i]
1701 = reg_class_subunion[(int) classes[i]]
1702 [(int) base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
1703 break;
1705 case TARGET_MEM_CONSTRAINT: case 'o': case 'V':
1706 /* It doesn't seem worth distinguishing between offsettable
1707 and non-offsettable addresses here. */
1708 allows_mem[i] = 1;
1709 if (MEM_P (op))
1710 win = 1;
1711 break;
1713 case '<':
1714 if (MEM_P (op)
1715 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
1716 || GET_CODE (XEXP (op, 0)) == POST_DEC))
1717 win = 1;
1718 break;
1720 case '>':
1721 if (MEM_P (op)
1722 && (GET_CODE (XEXP (op, 0)) == PRE_INC
1723 || GET_CODE (XEXP (op, 0)) == POST_INC))
1724 win = 1;
1725 break;
1727 case 'E':
1728 case 'F':
1729 if (GET_CODE (op) == CONST_DOUBLE
1730 || (GET_CODE (op) == CONST_VECTOR
1731 && (GET_MODE_CLASS (GET_MODE (op))
1732 == MODE_VECTOR_FLOAT)))
1733 win = 1;
1734 break;
1736 case 'G':
1737 case 'H':
1738 if (GET_CODE (op) == CONST_DOUBLE
1739 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
1740 win = 1;
1741 break;
1743 case 's':
1744 if (GET_CODE (op) == CONST_INT
1745 || (GET_CODE (op) == CONST_DOUBLE
1746 && GET_MODE (op) == VOIDmode))
1747 break;
1748 case 'i':
1749 if (CONSTANT_P (op)
1750 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op)))
1751 win = 1;
1752 break;
1754 case 'n':
1755 if (GET_CODE (op) == CONST_INT
1756 || (GET_CODE (op) == CONST_DOUBLE
1757 && GET_MODE (op) == VOIDmode))
1758 win = 1;
1759 break;
1761 case 'I':
1762 case 'J':
1763 case 'K':
1764 case 'L':
1765 case 'M':
1766 case 'N':
1767 case 'O':
1768 case 'P':
1769 if (GET_CODE (op) == CONST_INT
1770 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
1771 win = 1;
1772 break;
1774 case 'X':
1775 win = 1;
1776 break;
1778 case 'g':
1779 if (MEM_P (op)
1780 || (CONSTANT_P (op)
1781 && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))))
1782 win = 1;
1783 allows_mem[i] = 1;
1784 case 'r':
1785 classes[i]
1786 = reg_class_subunion[(int) classes[i]][(int) GENERAL_REGS];
1787 break;
1789 default:
1790 if (REG_CLASS_FROM_CONSTRAINT (c, p) != NO_REGS)
1791 classes[i]
1792 = reg_class_subunion[(int) classes[i]]
1793 [(int) REG_CLASS_FROM_CONSTRAINT (c, p)];
1794 #ifdef EXTRA_CONSTRAINT_STR
1795 else if (EXTRA_CONSTRAINT_STR (op, c, p))
1796 win = 1;
1798 if (EXTRA_MEMORY_CONSTRAINT (c, p))
1800 /* Every MEM can be reloaded to fit. */
1801 allows_mem[i] = 1;
1802 if (MEM_P (op))
1803 win = 1;
1805 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1807 /* Every address can be reloaded to fit. */
1808 allows_addr = 1;
1809 if (address_operand (op, GET_MODE (op)))
1810 win = 1;
1811 /* We know this operand is an address, so we want it to
1812 be allocated to a register that can be the base of an
1813 address, i.e. BASE_REG_CLASS. */
1814 classes[i]
1815 = reg_class_subunion[(int) classes[i]]
1816 [(int) base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
1818 #endif
1819 break;
1821 p += CONSTRAINT_LEN (c, p);
1822 if (c == ',')
1823 break;
1826 constraints[i] = p;
1828 /* How we account for this operand now depends on whether it is a
1829 pseudo register or not. If it is, we first check if any
1830 register classes are valid. If not, we ignore this alternative,
1831 since we want to assume that all pseudos get allocated for
1832 register preferencing. If some register class is valid, compute
1833 the costs of moving the pseudo into that class. */
1835 if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
1837 if (classes[i] == NO_REGS)
1839 /* We must always fail if the operand is a REG, but
1840 we did not find a suitable class.
1842 Otherwise we may perform an uninitialized read
1843 from this_op_costs after the `continue' statement
1844 below. */
1845 alt_fail = 1;
1847 else
1849 struct costs *pp = &this_op_costs[i];
1850 move_table *intable = NULL;
1851 move_table *outtable = NULL;
1852 int op_class = (int) classes[i];
1854 if (!move_cost[mode])
1855 init_move_cost (mode);
1856 intable = may_move_in_cost[mode];
1857 outtable = may_move_out_cost[mode];
1859 /* The loop is performance critical, so unswitch it manually.
1861 switch (recog_data.operand_type[i])
1863 case OP_INOUT:
1864 for (class = 0; class < N_REG_CLASSES; class++)
1865 pp->cost[class] = (intable[class][op_class]
1866 + outtable[op_class][class]);
1867 break;
1868 case OP_IN:
1869 for (class = 0; class < N_REG_CLASSES; class++)
1870 pp->cost[class] = intable[class][op_class];
1871 break;
1872 case OP_OUT:
1873 for (class = 0; class < N_REG_CLASSES; class++)
1874 pp->cost[class] = outtable[op_class][class];
1875 break;
1878 /* If the alternative actually allows memory, make things
1879 a bit cheaper since we won't need an extra insn to
1880 load it. */
1882 pp->mem_cost
1883 = ((recog_data.operand_type[i] != OP_IN
1884 ? MEMORY_MOVE_COST (mode, classes[i], 0)
1885 : 0)
1886 + (recog_data.operand_type[i] != OP_OUT
1887 ? MEMORY_MOVE_COST (mode, classes[i], 1)
1888 : 0) - allows_mem[i]);
1890 /* If we have assigned a class to this register in our
1891 first pass, add a cost to this alternative corresponding
1892 to what we would add if this register were not in the
1893 appropriate class. */
1895 if (reg_pref && reg_pref[REGNO (op)].prefclass != NO_REGS)
1896 alt_cost
1897 += (may_move_in_cost[mode]
1898 [(unsigned char) reg_pref[REGNO (op)].prefclass]
1899 [(int) classes[i]]);
1903 /* Otherwise, if this alternative wins, either because we
1904 have already determined that or if we have a hard register of
1905 the proper class, there is no cost for this alternative. */
1907 else if (win
1908 || (REG_P (op)
1909 && reg_fits_class_p (op, classes[i], 0, GET_MODE (op))))
1912 /* If registers are valid, the cost of this alternative includes
1913 copying the object to and/or from a register. */
1915 else if (classes[i] != NO_REGS)
1917 if (recog_data.operand_type[i] != OP_OUT)
1918 alt_cost += copy_cost (op, mode, classes[i], 1, NULL);
1920 if (recog_data.operand_type[i] != OP_IN)
1921 alt_cost += copy_cost (op, mode, classes[i], 0, NULL);
1924 /* The only other way this alternative can be used is if this is a
1925 constant that could be placed into memory. */
1927 else if (CONSTANT_P (op) && (allows_addr || allows_mem[i]))
1928 alt_cost += MEMORY_MOVE_COST (mode, classes[i], 1);
1929 else
1930 alt_fail = 1;
1933 if (alt_fail)
1934 continue;
1936 if (!recog_data.alternative_enabled_p[alt])
1937 continue;
1939 /* Finally, update the costs with the information we've calculated
1940 about this alternative. */
1942 for (i = 0; i < n_ops; i++)
1943 if (REG_P (ops[i])
1944 && REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
1946 struct costs *pp = &op_costs[i], *qq = &this_op_costs[i];
1947 int scale = 1 + (recog_data.operand_type[i] == OP_INOUT);
1949 pp->mem_cost = MIN (pp->mem_cost,
1950 (qq->mem_cost + alt_cost) * scale);
1952 for (class = 0; class < N_REG_CLASSES; class++)
1953 pp->cost[class] = MIN (pp->cost[class],
1954 (qq->cost[class] + alt_cost) * scale);
1958 /* If this insn is a single set copying operand 1 to operand 0
1959 and one operand is a pseudo with the other a hard reg or a pseudo
1960 that prefers a register that is in its own register class then
1961 we may want to adjust the cost of that register class to -1.
1963 Avoid the adjustment if the source does not die to avoid stressing of
1964 register allocator by preferrencing two colliding registers into single
1965 class.
1967 Also avoid the adjustment if a copy between registers of the class
1968 is expensive (ten times the cost of a default copy is considered
1969 arbitrarily expensive). This avoids losing when the preferred class
1970 is very expensive as the source of a copy instruction. */
1972 if ((set = single_set (insn)) != 0
1973 && ops[0] == SET_DEST (set) && ops[1] == SET_SRC (set)
1974 && REG_P (ops[0]) && REG_P (ops[1])
1975 && find_regno_note (insn, REG_DEAD, REGNO (ops[1])))
1976 for (i = 0; i <= 1; i++)
1977 if (REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
1979 unsigned int regno = REGNO (ops[!i]);
1980 enum machine_mode mode = GET_MODE (ops[!i]);
1981 int class;
1983 if (regno >= FIRST_PSEUDO_REGISTER && reg_pref != 0
1984 && reg_pref[regno].prefclass != NO_REGS)
1986 enum reg_class pref = reg_pref[regno].prefclass;
1988 if ((reg_class_size[(unsigned char) pref]
1989 == (unsigned) CLASS_MAX_NREGS (pref, mode))
1990 && REGISTER_MOVE_COST (mode, pref, pref) < 10 * 2)
1991 op_costs[i].cost[(unsigned char) pref] = -1;
1993 else if (regno < FIRST_PSEUDO_REGISTER)
1994 for (class = 0; class < N_REG_CLASSES; class++)
1995 if (TEST_HARD_REG_BIT (reg_class_contents[class], regno)
1996 && reg_class_size[class] == (unsigned) CLASS_MAX_NREGS (class, mode))
1998 if (reg_class_size[class] == 1)
1999 op_costs[i].cost[class] = -1;
2000 else if (in_hard_reg_set_p (reg_class_contents[class],
2001 mode, regno))
2002 op_costs[i].cost[class] = -1;
2007 /* Compute the cost of loading X into (if TO_P is nonzero) or from (if
2008 TO_P is zero) a register of class CLASS in mode MODE.
2010 X must not be a pseudo. */
2012 static int
2013 copy_cost (rtx x, enum machine_mode mode, enum reg_class class, int to_p,
2014 secondary_reload_info *prev_sri)
2016 enum reg_class secondary_class = NO_REGS;
2017 secondary_reload_info sri;
2019 /* If X is a SCRATCH, there is actually nothing to move since we are
2020 assuming optimal allocation. */
2022 if (GET_CODE (x) == SCRATCH)
2023 return 0;
2025 /* Get the class we will actually use for a reload. */
2026 class = PREFERRED_RELOAD_CLASS (x, class);
2028 /* If we need a secondary reload for an intermediate, the
2029 cost is that to load the input into the intermediate register, then
2030 to copy it. */
2032 sri.prev_sri = prev_sri;
2033 sri.extra_cost = 0;
2034 secondary_class = targetm.secondary_reload (to_p, x, class, mode, &sri);
2036 if (!move_cost[mode])
2037 init_move_cost (mode);
2039 if (secondary_class != NO_REGS)
2040 return (move_cost[mode][(int) secondary_class][(int) class]
2041 + sri.extra_cost
2042 + copy_cost (x, mode, secondary_class, to_p, &sri));
2044 /* For memory, use the memory move cost, for (hard) registers, use the
2045 cost to move between the register classes, and use 2 for everything
2046 else (constants). */
2048 if (MEM_P (x) || class == NO_REGS)
2049 return sri.extra_cost + MEMORY_MOVE_COST (mode, class, to_p);
2051 else if (REG_P (x))
2052 return (sri.extra_cost
2053 + move_cost[mode][(int) REGNO_REG_CLASS (REGNO (x))][(int) class]);
2055 else
2056 /* If this is a constant, we may eventually want to call rtx_cost here. */
2057 return sri.extra_cost + COSTS_N_INSNS (1);
2060 /* Record the pseudo registers we must reload into hard registers
2061 in a subexpression of a memory address, X.
2063 If CONTEXT is 0, we are looking at the base part of an address, otherwise we
2064 are looking at the index part.
2066 MODE is the mode of the memory reference; OUTER_CODE and INDEX_CODE
2067 give the context that the rtx appears in. These three arguments are
2068 passed down to base_reg_class.
2070 SCALE is twice the amount to multiply the cost by (it is twice so we
2071 can represent half-cost adjustments). */
2073 static void
2074 record_address_regs (enum machine_mode mode, rtx x, int context,
2075 enum rtx_code outer_code, enum rtx_code index_code,
2076 int scale)
2078 enum rtx_code code = GET_CODE (x);
2079 enum reg_class class;
2081 if (context == 1)
2082 class = INDEX_REG_CLASS;
2083 else
2084 class = base_reg_class (mode, outer_code, index_code);
2086 switch (code)
2088 case CONST_INT:
2089 case CONST:
2090 case CC0:
2091 case PC:
2092 case SYMBOL_REF:
2093 case LABEL_REF:
2094 return;
2096 case PLUS:
2097 /* When we have an address that is a sum,
2098 we must determine whether registers are "base" or "index" regs.
2099 If there is a sum of two registers, we must choose one to be
2100 the "base". Luckily, we can use the REG_POINTER to make a good
2101 choice most of the time. We only need to do this on machines
2102 that can have two registers in an address and where the base
2103 and index register classes are different.
2105 ??? This code used to set REGNO_POINTER_FLAG in some cases, but
2106 that seems bogus since it should only be set when we are sure
2107 the register is being used as a pointer. */
2110 rtx arg0 = XEXP (x, 0);
2111 rtx arg1 = XEXP (x, 1);
2112 enum rtx_code code0 = GET_CODE (arg0);
2113 enum rtx_code code1 = GET_CODE (arg1);
2115 /* Look inside subregs. */
2116 if (code0 == SUBREG)
2117 arg0 = SUBREG_REG (arg0), code0 = GET_CODE (arg0);
2118 if (code1 == SUBREG)
2119 arg1 = SUBREG_REG (arg1), code1 = GET_CODE (arg1);
2121 /* If this machine only allows one register per address, it must
2122 be in the first operand. */
2124 if (MAX_REGS_PER_ADDRESS == 1)
2125 record_address_regs (mode, arg0, 0, PLUS, code1, scale);
2127 /* If index and base registers are the same on this machine, just
2128 record registers in any non-constant operands. We assume here,
2129 as well as in the tests below, that all addresses are in
2130 canonical form. */
2132 else if (INDEX_REG_CLASS == base_reg_class (VOIDmode, PLUS, SCRATCH))
2134 record_address_regs (mode, arg0, context, PLUS, code1, scale);
2135 if (! CONSTANT_P (arg1))
2136 record_address_regs (mode, arg1, context, PLUS, code0, scale);
2139 /* If the second operand is a constant integer, it doesn't change
2140 what class the first operand must be. */
2142 else if (code1 == CONST_INT || code1 == CONST_DOUBLE)
2143 record_address_regs (mode, arg0, context, PLUS, code1, scale);
2145 /* If the second operand is a symbolic constant, the first operand
2146 must be an index register. */
2148 else if (code1 == SYMBOL_REF || code1 == CONST || code1 == LABEL_REF)
2149 record_address_regs (mode, arg0, 1, PLUS, code1, scale);
2151 /* If both operands are registers but one is already a hard register
2152 of index or reg-base class, give the other the class that the
2153 hard register is not. */
2155 else if (code0 == REG && code1 == REG
2156 && REGNO (arg0) < FIRST_PSEUDO_REGISTER
2157 && (ok_for_base_p_nonstrict (arg0, mode, PLUS, REG)
2158 || ok_for_index_p_nonstrict (arg0)))
2159 record_address_regs (mode, arg1,
2160 ok_for_base_p_nonstrict (arg0, mode, PLUS, REG)
2161 ? 1 : 0,
2162 PLUS, REG, scale);
2163 else if (code0 == REG && code1 == REG
2164 && REGNO (arg1) < FIRST_PSEUDO_REGISTER
2165 && (ok_for_base_p_nonstrict (arg1, mode, PLUS, REG)
2166 || ok_for_index_p_nonstrict (arg1)))
2167 record_address_regs (mode, arg0,
2168 ok_for_base_p_nonstrict (arg1, mode, PLUS, REG)
2169 ? 1 : 0,
2170 PLUS, REG, scale);
2172 /* If one operand is known to be a pointer, it must be the base
2173 with the other operand the index. Likewise if the other operand
2174 is a MULT. */
2176 else if ((code0 == REG && REG_POINTER (arg0))
2177 || code1 == MULT)
2179 record_address_regs (mode, arg0, 0, PLUS, code1, scale);
2180 record_address_regs (mode, arg1, 1, PLUS, code0, scale);
2182 else if ((code1 == REG && REG_POINTER (arg1))
2183 || code0 == MULT)
2185 record_address_regs (mode, arg0, 1, PLUS, code1, scale);
2186 record_address_regs (mode, arg1, 0, PLUS, code0, scale);
2189 /* Otherwise, count equal chances that each might be a base
2190 or index register. This case should be rare. */
2192 else
2194 record_address_regs (mode, arg0, 0, PLUS, code1, scale / 2);
2195 record_address_regs (mode, arg0, 1, PLUS, code1, scale / 2);
2196 record_address_regs (mode, arg1, 0, PLUS, code0, scale / 2);
2197 record_address_regs (mode, arg1, 1, PLUS, code0, scale / 2);
2200 break;
2202 /* Double the importance of a pseudo register that is incremented
2203 or decremented, since it would take two extra insns
2204 if it ends up in the wrong place. */
2205 case POST_MODIFY:
2206 case PRE_MODIFY:
2207 record_address_regs (mode, XEXP (x, 0), 0, code,
2208 GET_CODE (XEXP (XEXP (x, 1), 1)), 2 * scale);
2209 if (REG_P (XEXP (XEXP (x, 1), 1)))
2210 record_address_regs (mode, XEXP (XEXP (x, 1), 1), 1, code, REG,
2211 2 * scale);
2212 break;
2214 case POST_INC:
2215 case PRE_INC:
2216 case POST_DEC:
2217 case PRE_DEC:
2218 /* Double the importance of a pseudo register that is incremented
2219 or decremented, since it would take two extra insns
2220 if it ends up in the wrong place. If the operand is a pseudo,
2221 show it is being used in an INC_DEC context. */
2223 #ifdef FORBIDDEN_INC_DEC_CLASSES
2224 if (REG_P (XEXP (x, 0))
2225 && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER)
2226 in_inc_dec[REGNO (XEXP (x, 0))] = 1;
2227 #endif
2229 record_address_regs (mode, XEXP (x, 0), 0, code, SCRATCH, 2 * scale);
2230 break;
2232 case REG:
2234 struct costs *pp = &costs[REGNO (x)];
2235 int i;
2237 pp->mem_cost += (MEMORY_MOVE_COST (Pmode, class, 1) * scale) / 2;
2239 if (!move_cost[Pmode])
2240 init_move_cost (Pmode);
2241 for (i = 0; i < N_REG_CLASSES; i++)
2242 pp->cost[i] += (may_move_in_cost[Pmode][i][(int) class] * scale) / 2;
2244 break;
2246 default:
2248 const char *fmt = GET_RTX_FORMAT (code);
2249 int i;
2250 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2251 if (fmt[i] == 'e')
2252 record_address_regs (mode, XEXP (x, i), context, code, SCRATCH,
2253 scale);
2258 #ifdef FORBIDDEN_INC_DEC_CLASSES
2260 /* Return 1 if REG is valid as an auto-increment memory reference
2261 to an object of MODE. */
2263 static int
2264 auto_inc_dec_reg_p (rtx reg, enum machine_mode mode)
2266 if (HAVE_POST_INCREMENT
2267 && memory_address_p (mode, gen_rtx_POST_INC (Pmode, reg)))
2268 return 1;
2270 if (HAVE_POST_DECREMENT
2271 && memory_address_p (mode, gen_rtx_POST_DEC (Pmode, reg)))
2272 return 1;
2274 if (HAVE_PRE_INCREMENT
2275 && memory_address_p (mode, gen_rtx_PRE_INC (Pmode, reg)))
2276 return 1;
2278 if (HAVE_PRE_DECREMENT
2279 && memory_address_p (mode, gen_rtx_PRE_DEC (Pmode, reg)))
2280 return 1;
2282 return 0;
2284 #endif
2286 /* Free up the space allocated by allocate_reg_info. */
2287 void
2288 free_reg_info (void)
2290 if (reg_pref)
2292 free (reg_pref);
2293 reg_pref = NULL;
2296 if (reg_renumber)
2298 free (reg_renumber);
2299 reg_renumber = NULL;
2304 /* This is the `regscan' pass of the compiler, run just before cse and
2305 again just before loop. It finds the first and last use of each
2306 pseudo-register. */
2308 void
2309 reg_scan (rtx f, unsigned int nregs ATTRIBUTE_UNUSED)
2311 rtx insn;
2313 timevar_push (TV_REG_SCAN);
2315 for (insn = f; insn; insn = NEXT_INSN (insn))
2316 if (INSN_P (insn))
2318 reg_scan_mark_refs (PATTERN (insn), insn);
2319 if (REG_NOTES (insn))
2320 reg_scan_mark_refs (REG_NOTES (insn), insn);
2323 timevar_pop (TV_REG_SCAN);
2327 /* X is the expression to scan. INSN is the insn it appears in.
2328 NOTE_FLAG is nonzero if X is from INSN's notes rather than its body.
2329 We should only record information for REGs with numbers
2330 greater than or equal to MIN_REGNO. */
2332 static void
2333 reg_scan_mark_refs (rtx x, rtx insn)
2335 enum rtx_code code;
2336 rtx dest;
2337 rtx note;
2339 if (!x)
2340 return;
2341 code = GET_CODE (x);
2342 switch (code)
2344 case CONST:
2345 case CONST_INT:
2346 case CONST_DOUBLE:
2347 case CONST_FIXED:
2348 case CONST_VECTOR:
2349 case CC0:
2350 case PC:
2351 case SYMBOL_REF:
2352 case LABEL_REF:
2353 case ADDR_VEC:
2354 case ADDR_DIFF_VEC:
2355 case REG:
2356 return;
2358 case EXPR_LIST:
2359 if (XEXP (x, 0))
2360 reg_scan_mark_refs (XEXP (x, 0), insn);
2361 if (XEXP (x, 1))
2362 reg_scan_mark_refs (XEXP (x, 1), insn);
2363 break;
2365 case INSN_LIST:
2366 if (XEXP (x, 1))
2367 reg_scan_mark_refs (XEXP (x, 1), insn);
2368 break;
2370 case CLOBBER:
2371 if (MEM_P (XEXP (x, 0)))
2372 reg_scan_mark_refs (XEXP (XEXP (x, 0), 0), insn);
2373 break;
2375 case SET:
2376 /* Count a set of the destination if it is a register. */
2377 for (dest = SET_DEST (x);
2378 GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
2379 || GET_CODE (dest) == ZERO_EXTEND;
2380 dest = XEXP (dest, 0))
2383 /* If this is setting a pseudo from another pseudo or the sum of a
2384 pseudo and a constant integer and the other pseudo is known to be
2385 a pointer, set the destination to be a pointer as well.
2387 Likewise if it is setting the destination from an address or from a
2388 value equivalent to an address or to the sum of an address and
2389 something else.
2391 But don't do any of this if the pseudo corresponds to a user
2392 variable since it should have already been set as a pointer based
2393 on the type. */
2395 if (REG_P (SET_DEST (x))
2396 && REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER
2397 /* If the destination pseudo is set more than once, then other
2398 sets might not be to a pointer value (consider access to a
2399 union in two threads of control in the presence of global
2400 optimizations). So only set REG_POINTER on the destination
2401 pseudo if this is the only set of that pseudo. */
2402 && DF_REG_DEF_COUNT (REGNO (SET_DEST (x))) == 1
2403 && ! REG_USERVAR_P (SET_DEST (x))
2404 && ! REG_POINTER (SET_DEST (x))
2405 && ((REG_P (SET_SRC (x))
2406 && REG_POINTER (SET_SRC (x)))
2407 || ((GET_CODE (SET_SRC (x)) == PLUS
2408 || GET_CODE (SET_SRC (x)) == LO_SUM)
2409 && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2410 && REG_P (XEXP (SET_SRC (x), 0))
2411 && REG_POINTER (XEXP (SET_SRC (x), 0)))
2412 || GET_CODE (SET_SRC (x)) == CONST
2413 || GET_CODE (SET_SRC (x)) == SYMBOL_REF
2414 || GET_CODE (SET_SRC (x)) == LABEL_REF
2415 || (GET_CODE (SET_SRC (x)) == HIGH
2416 && (GET_CODE (XEXP (SET_SRC (x), 0)) == CONST
2417 || GET_CODE (XEXP (SET_SRC (x), 0)) == SYMBOL_REF
2418 || GET_CODE (XEXP (SET_SRC (x), 0)) == LABEL_REF))
2419 || ((GET_CODE (SET_SRC (x)) == PLUS
2420 || GET_CODE (SET_SRC (x)) == LO_SUM)
2421 && (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST
2422 || GET_CODE (XEXP (SET_SRC (x), 1)) == SYMBOL_REF
2423 || GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF))
2424 || ((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
2425 && (GET_CODE (XEXP (note, 0)) == CONST
2426 || GET_CODE (XEXP (note, 0)) == SYMBOL_REF
2427 || GET_CODE (XEXP (note, 0)) == LABEL_REF))))
2428 REG_POINTER (SET_DEST (x)) = 1;
2430 /* If this is setting a register from a register or from a simple
2431 conversion of a register, propagate REG_EXPR. */
2432 if (REG_P (dest) && !REG_ATTRS (dest))
2434 rtx src = SET_SRC (x);
2436 while (GET_CODE (src) == SIGN_EXTEND
2437 || GET_CODE (src) == ZERO_EXTEND
2438 || GET_CODE (src) == TRUNCATE
2439 || (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)))
2440 src = XEXP (src, 0);
2442 set_reg_attrs_from_value (dest, src);
2445 /* ... fall through ... */
2447 default:
2449 const char *fmt = GET_RTX_FORMAT (code);
2450 int i;
2451 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2453 if (fmt[i] == 'e')
2454 reg_scan_mark_refs (XEXP (x, i), insn);
2455 else if (fmt[i] == 'E' && XVEC (x, i) != 0)
2457 int j;
2458 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2459 reg_scan_mark_refs (XVECEXP (x, i, j), insn);
2466 /* Return nonzero if C1 is a subset of C2, i.e., if every register in C1
2467 is also in C2. */
2470 reg_class_subset_p (enum reg_class c1, enum reg_class c2)
2472 return (c1 == c2
2473 || c2 == ALL_REGS
2474 || hard_reg_set_subset_p (reg_class_contents[(int) c1],
2475 reg_class_contents[(int) c2]));
2478 /* Return nonzero if there is a register that is in both C1 and C2. */
2481 reg_classes_intersect_p (enum reg_class c1, enum reg_class c2)
2483 return (c1 == c2
2484 || c1 == ALL_REGS
2485 || c2 == ALL_REGS
2486 || hard_reg_set_intersect_p (reg_class_contents[(int) c1],
2487 reg_class_contents[(int) c2]));
2490 #ifdef CANNOT_CHANGE_MODE_CLASS
2492 struct subregs_of_mode_node
2494 unsigned int block;
2495 unsigned char modes[MAX_MACHINE_MODE];
2498 static htab_t subregs_of_mode;
2500 static hashval_t
2501 som_hash (const void *x)
2503 const struct subregs_of_mode_node *a = x;
2504 return a->block;
2507 static int
2508 som_eq (const void *x, const void *y)
2510 const struct subregs_of_mode_node *a = x;
2511 const struct subregs_of_mode_node *b = y;
2512 return a->block == b->block;
2516 static void
2517 record_subregs_of_mode (rtx subreg)
2519 struct subregs_of_mode_node dummy, *node;
2520 enum machine_mode mode;
2521 unsigned int regno;
2522 void **slot;
2524 if (!REG_P (SUBREG_REG (subreg)))
2525 return;
2527 regno = REGNO (SUBREG_REG (subreg));
2528 mode = GET_MODE (subreg);
2530 if (regno < FIRST_PSEUDO_REGISTER)
2531 return;
2533 dummy.block = regno & -8;
2534 slot = htab_find_slot_with_hash (subregs_of_mode, &dummy,
2535 dummy.block, INSERT);
2536 node = *slot;
2537 if (node == NULL)
2539 node = XCNEW (struct subregs_of_mode_node);
2540 node->block = regno & -8;
2541 *slot = node;
2544 node->modes[mode] |= 1 << (regno & 7);
2548 /* Call record_subregs_of_mode for all the subregs in X. */
2550 static void
2551 find_subregs_of_mode (rtx x)
2553 enum rtx_code code = GET_CODE (x);
2554 const char * const fmt = GET_RTX_FORMAT (code);
2555 int i;
2557 if (code == SUBREG)
2558 record_subregs_of_mode (x);
2560 /* Time for some deep diving. */
2561 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2563 if (fmt[i] == 'e')
2564 find_subregs_of_mode (XEXP (x, i));
2565 else if (fmt[i] == 'E')
2567 int j;
2568 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2569 find_subregs_of_mode (XVECEXP (x, i, j));
2574 static unsigned int
2575 init_subregs_of_mode (void)
2577 basic_block bb;
2578 rtx insn;
2580 if (subregs_of_mode)
2581 htab_empty (subregs_of_mode);
2582 else
2583 subregs_of_mode = htab_create (100, som_hash, som_eq, free);
2585 FOR_EACH_BB (bb)
2586 FOR_BB_INSNS (bb, insn)
2587 if (INSN_P (insn))
2588 find_subregs_of_mode (PATTERN (insn));
2590 return 0;
2594 /* Set bits in *USED which correspond to registers which can't change
2595 their mode from FROM to any mode in which REGNO was encountered. */
2597 void
2598 cannot_change_mode_set_regs (HARD_REG_SET *used, enum machine_mode from,
2599 unsigned int regno)
2601 struct subregs_of_mode_node dummy, *node;
2602 enum machine_mode to;
2603 unsigned char mask;
2604 unsigned int i;
2606 gcc_assert (subregs_of_mode);
2607 dummy.block = regno & -8;
2608 node = htab_find_with_hash (subregs_of_mode, &dummy, dummy.block);
2609 if (node == NULL)
2610 return;
2612 mask = 1 << (regno & 7);
2613 for (to = VOIDmode; to < NUM_MACHINE_MODES; to++)
2614 if (node->modes[to] & mask)
2615 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2616 if (!TEST_HARD_REG_BIT (*used, i)
2617 && REG_CANNOT_CHANGE_MODE_P (i, from, to))
2618 SET_HARD_REG_BIT (*used, i);
2621 /* Return 1 if REGNO has had an invalid mode change in CLASS from FROM
2622 mode. */
2624 bool
2625 invalid_mode_change_p (unsigned int regno,
2626 enum reg_class class ATTRIBUTE_UNUSED,
2627 enum machine_mode from)
2629 struct subregs_of_mode_node dummy, *node;
2630 enum machine_mode to;
2631 unsigned char mask;
2633 gcc_assert (subregs_of_mode);
2634 dummy.block = regno & -8;
2635 node = htab_find_with_hash (subregs_of_mode, &dummy, dummy.block);
2636 if (node == NULL)
2637 return false;
2639 mask = 1 << (regno & 7);
2640 for (to = VOIDmode; to < NUM_MACHINE_MODES; to++)
2641 if (node->modes[to] & mask)
2642 if (CANNOT_CHANGE_MODE_CLASS (from, to, class))
2643 return true;
2645 return false;
2648 static unsigned int
2649 finish_subregs_of_mode (void)
2651 htab_delete (subregs_of_mode);
2652 subregs_of_mode = 0;
2653 return 0;
2655 #else
2656 static unsigned int
2657 init_subregs_of_mode (void)
2659 return 0;
2661 static unsigned int
2662 finish_subregs_of_mode (void)
2664 return 0;
2667 #endif /* CANNOT_CHANGE_MODE_CLASS */
2669 static bool
2670 gate_subregs_of_mode_init (void)
2672 #ifdef CANNOT_CHANGE_MODE_CLASS
2673 return true;
2674 #else
2675 return false;
2676 #endif
2679 struct rtl_opt_pass pass_subregs_of_mode_init =
2682 RTL_PASS,
2683 "subregs_of_mode_init", /* name */
2684 gate_subregs_of_mode_init, /* gate */
2685 init_subregs_of_mode, /* execute */
2686 NULL, /* sub */
2687 NULL, /* next */
2688 0, /* static_pass_number */
2689 0, /* tv_id */
2690 0, /* properties_required */
2691 0, /* properties_provided */
2692 0, /* properties_destroyed */
2693 0, /* todo_flags_start */
2694 0 /* todo_flags_finish */
2698 struct rtl_opt_pass pass_subregs_of_mode_finish =
2701 RTL_PASS,
2702 "subregs_of_mode_finish", /* name */
2703 gate_subregs_of_mode_init, /* gate */
2704 finish_subregs_of_mode, /* execute */
2705 NULL, /* sub */
2706 NULL, /* next */
2707 0, /* static_pass_number */
2708 0, /* tv_id */
2709 0, /* properties_required */
2710 0, /* properties_provided */
2711 0, /* properties_destroyed */
2712 0, /* todo_flags_start */
2713 0 /* todo_flags_finish */
2719 #include "gt-regclass.h"