OSDN Git Service

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