OSDN Git Service

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