OSDN Git Service

* pa.h (ASM_OUTPUT_SECTION_NAME): Fix typo.
[pf3gnuchains/gcc-fork.git] / gcc / regclass.c
1 /* Compute register class preferences for pseudo-registers.
2    Copyright (C) 1987, 88, 91, 92, 93, 94, 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
351 /* After switches have been processed, which perhaps alter
352    `fixed_regs' and `call_used_regs', convert them to HARD_REG_SETs.  */
353
354 static void
355 init_reg_sets_1 ()
356 {
357   register int i;
358
359   /* This macro allows the fixed or call-used registers
360      to depend on target flags.  */
361
362 #ifdef CONDITIONAL_REGISTER_USAGE
363   CONDITIONAL_REGISTER_USAGE;
364 #endif
365
366   /* Initialize "constant" tables.  */
367
368   CLEAR_HARD_REG_SET (fixed_reg_set);
369   CLEAR_HARD_REG_SET (call_used_reg_set);
370   CLEAR_HARD_REG_SET (call_fixed_reg_set);
371
372   bcopy (fixed_regs, call_fixed_regs, sizeof call_fixed_regs);
373
374   n_non_fixed_regs = 0;
375
376   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
377     {
378       if (fixed_regs[i])
379         SET_HARD_REG_BIT (fixed_reg_set, i);
380       else
381         n_non_fixed_regs++;
382
383       if (call_used_regs[i])
384         SET_HARD_REG_BIT (call_used_reg_set, i);
385       if (call_fixed_regs[i])
386         SET_HARD_REG_BIT (call_fixed_reg_set, i);
387       if (CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (i)))
388         SET_HARD_REG_BIT (losing_caller_save_reg_set, i);
389     }
390 }
391
392 /* Compute the table of register modes.
393    These values are used to record death information for individual registers
394    (as opposed to a multi-register mode).  */
395
396 static void
397 init_reg_modes ()
398 {
399   register int i;
400
401   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
402     {
403       reg_raw_mode[i] = choose_hard_reg_mode (i, 1);
404
405       /* If we couldn't find a valid mode, fall back to `word_mode'.
406          ??? We assume `word_mode' has already been initialized.
407          ??? One situation in which we need to do this is on the mips where
408          HARD_REGNO_NREGS (fpreg, [SD]Fmode) returns 2.  Ideally we'd like
409          to use DF mode for the even registers and VOIDmode for the odd
410          (for the cpu models where the odd ones are inaccessible).  */
411       if (reg_raw_mode[i] == VOIDmode)
412         reg_raw_mode[i] = word_mode;
413     }
414 }
415
416 /* Finish initializing the register sets and
417    initialize the register modes.  */
418
419 void
420 init_regs ()
421 {
422   /* This finishes what was started by init_reg_sets, but couldn't be done
423      until after register usage was specified.  */
424   if (!output_bytecode)
425     init_reg_sets_1 ();
426
427   init_reg_modes ();
428 }
429
430 /* Return a machine mode that is legitimate for hard reg REGNO and large
431    enough to save nregs.  If we can't find one, return VOIDmode.  */
432
433 enum machine_mode
434 choose_hard_reg_mode (regno, nregs)
435      int regno;
436      int nregs;
437 {
438   enum machine_mode found_mode = VOIDmode, mode;
439
440   /* We first look for the largest integer mode that can be validly
441      held in REGNO.  If none, we look for the largest floating-point mode.
442      If we still didn't find a valid mode, try CCmode.  */
443
444   for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
445        mode != VOIDmode;
446        mode = GET_MODE_WIDER_MODE (mode))
447     if (HARD_REGNO_NREGS (regno, mode) == nregs
448         && HARD_REGNO_MODE_OK (regno, mode))
449       found_mode = mode;
450
451   if (found_mode != VOIDmode)
452     return found_mode;
453
454   for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
455        mode != VOIDmode;
456        mode = GET_MODE_WIDER_MODE (mode))
457     if (HARD_REGNO_NREGS (regno, mode) == nregs
458         && HARD_REGNO_MODE_OK (regno, mode))
459       found_mode = mode;
460
461   if (found_mode != VOIDmode)
462     return found_mode;
463
464   if (HARD_REGNO_NREGS (regno, CCmode) == nregs
465       && HARD_REGNO_MODE_OK (regno, CCmode))
466     return CCmode;
467
468   /* We can't find a mode valid for this register.  */
469   return VOIDmode;
470 }
471
472 /* Specify the usage characteristics of the register named NAME.
473    It should be a fixed register if FIXED and a
474    call-used register if CALL_USED.  */
475
476 void
477 fix_register (name, fixed, call_used)
478      char *name;
479      int fixed, call_used;
480 {
481   int i;
482
483   if (output_bytecode)
484     {
485       warning ("request to mark `%s' as %s ignored by bytecode compiler",
486                name, call_used ? "call-used" : "fixed");
487       return;
488     }
489
490   /* Decode the name and update the primary form of
491      the register info.  */
492
493   if ((i = decode_reg_name (name)) >= 0)
494     {
495       fixed_regs[i] = fixed;
496       call_used_regs[i] = call_used;
497     }
498   else
499     {
500       warning ("unknown register name: %s", name);
501     }
502 }
503
504 /* Mark register number I as global.  */
505
506 void
507 globalize_reg (i)
508      int i;
509 {
510   if (global_regs[i])
511     {
512       warning ("register used for two global register variables");
513       return;
514     }
515
516   if (call_used_regs[i] && ! fixed_regs[i])
517     warning ("call-clobbered register used for global register variable");
518
519   global_regs[i] = 1;
520
521   /* If already fixed, nothing else to do.  */
522   if (fixed_regs[i])
523     return;
524
525   fixed_regs[i] = call_used_regs[i] = call_fixed_regs[i] = 1;
526   n_non_fixed_regs--;
527
528   SET_HARD_REG_BIT (fixed_reg_set, i);
529   SET_HARD_REG_BIT (call_used_reg_set, i);
530   SET_HARD_REG_BIT (call_fixed_reg_set, i);
531 }
532 \f
533 /* Now the data and code for the `regclass' pass, which happens
534    just before local-alloc.  */
535
536 /* The `costs' struct records the cost of using a hard register of each class
537    and of using memory for each pseudo.  We use this data to set up
538    register class preferences.  */
539
540 struct costs
541 {
542   int cost[N_REG_CLASSES];
543   int mem_cost;
544 };
545
546 /* Record the cost of each class for each pseudo.  */
547
548 static struct costs *costs;
549
550 /* Record the same data by operand number, accumulated for each alternative
551    in an insn.  The contribution to a pseudo is that of the minimum-cost
552    alternative.  */
553
554 static struct costs op_costs[MAX_RECOG_OPERANDS];
555
556 /* (enum reg_class) prefclass[R] is the preferred class for pseudo number R.
557    This is available after `regclass' is run.  */
558
559 static char *prefclass;
560
561 /* altclass[R] is a register class that we should use for allocating
562    pseudo number R if no register in the preferred class is available.
563    If no register in this class is available, memory is preferred.
564
565    It might appear to be more general to have a bitmask of classes here,
566    but since it is recommended that there be a class corresponding to the
567    union of most major pair of classes, that generality is not required. 
568
569    This is available after `regclass' is run.  */
570
571 static char *altclass;
572
573 /* Record the depth of loops that we are in.  */
574
575 static int loop_depth;
576
577 /* Account for the fact that insns within a loop are executed very commonly,
578    but don't keep doing this as loops go too deep.  */
579
580 static int loop_cost;
581
582 static void record_reg_classes  PROTO((int, int, rtx *, enum machine_mode *,
583                                        char **, rtx));
584 static int copy_cost            PROTO((rtx, enum machine_mode, 
585                                        enum reg_class, int));
586 static void record_address_regs PROTO((rtx, enum reg_class, int));
587 static auto_inc_dec_reg_p       PROTO((rtx, enum machine_mode));
588 static void reg_scan_mark_refs  PROTO((rtx, rtx, int));
589
590 /* Return the reg_class in which pseudo reg number REGNO is best allocated.
591    This function is sometimes called before the info has been computed.
592    When that happens, just return GENERAL_REGS, which is innocuous.  */
593
594 enum reg_class
595 reg_preferred_class (regno)
596      int regno;
597 {
598   if (prefclass == 0)
599     return GENERAL_REGS;
600   return (enum reg_class) prefclass[regno];
601 }
602
603 enum reg_class
604 reg_alternate_class (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   enum reg_class secondary_class = NO_REGS;
1412
1413   /* If X is a SCRATCH, there is actually nothing to move since we are
1414      assuming optimal allocation.  */
1415
1416   if (GET_CODE (x) == SCRATCH)
1417     return 0;
1418
1419   /* Get the class we will actually use for a reload.  */
1420   class = PREFERRED_RELOAD_CLASS (x, class);
1421
1422 #ifdef HAVE_SECONDARY_RELOADS
1423   /* If we need a secondary reload (we assume here that we are using 
1424      the secondary reload as an intermediate, not a scratch register), the
1425      cost is that to load the input into the intermediate register, then
1426      to copy them.  We use a special value of TO_P to avoid recursion.  */
1427
1428 #ifdef SECONDARY_INPUT_RELOAD_CLASS
1429   if (to_p == 1)
1430     secondary_class = SECONDARY_INPUT_RELOAD_CLASS (class, mode, x);
1431 #endif
1432
1433 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
1434   if (! to_p)
1435     secondary_class = SECONDARY_OUTPUT_RELOAD_CLASS (class, mode, x);
1436 #endif
1437
1438   if (secondary_class != NO_REGS)
1439     return (move_cost[(int) secondary_class][(int) class]
1440             + copy_cost (x, mode, secondary_class, 2));
1441 #endif  /* HAVE_SECONDARY_RELOADS */
1442
1443   /* For memory, use the memory move cost, for (hard) registers, use the
1444      cost to move between the register classes, and use 2 for everything
1445      else (constants).  */
1446
1447   if (GET_CODE (x) == MEM || class == NO_REGS)
1448     return MEMORY_MOVE_COST (mode);
1449
1450   else if (GET_CODE (x) == REG)
1451     return move_cost[(int) REGNO_REG_CLASS (REGNO (x))][(int) class];
1452
1453   else
1454     /* If this is a constant, we may eventually want to call rtx_cost here.  */
1455     return 2;
1456 }
1457 \f
1458 /* Record the pseudo registers we must reload into hard registers
1459    in a subexpression of a memory address, X.
1460
1461    CLASS is the class that the register needs to be in and is either
1462    BASE_REG_CLASS or INDEX_REG_CLASS.
1463
1464    SCALE is twice the amount to multiply the cost by (it is twice so we
1465    can represent half-cost adjustments).  */
1466
1467 static void
1468 record_address_regs (x, class, scale)
1469      rtx x;
1470      enum reg_class class;
1471      int scale;
1472 {
1473   register enum rtx_code code = GET_CODE (x);
1474
1475   switch (code)
1476     {
1477     case CONST_INT:
1478     case CONST:
1479     case CC0:
1480     case PC:
1481     case SYMBOL_REF:
1482     case LABEL_REF:
1483       return;
1484
1485     case PLUS:
1486       /* When we have an address that is a sum,
1487          we must determine whether registers are "base" or "index" regs.
1488          If there is a sum of two registers, we must choose one to be
1489          the "base".  Luckily, we can use the REGNO_POINTER_FLAG
1490          to make a good choice most of the time.  We only need to do this
1491          on machines that can have two registers in an address and where
1492          the base and index register classes are different.
1493
1494          ??? This code used to set REGNO_POINTER_FLAG in some cases, but
1495          that seems bogus since it should only be set when we are sure
1496          the register is being used as a pointer.  */
1497
1498       {
1499         rtx arg0 = XEXP (x, 0);
1500         rtx arg1 = XEXP (x, 1);
1501         register enum rtx_code code0 = GET_CODE (arg0);
1502         register enum rtx_code code1 = GET_CODE (arg1);
1503
1504         /* Look inside subregs.  */
1505         if (code0 == SUBREG)
1506           arg0 = SUBREG_REG (arg0), code0 = GET_CODE (arg0);
1507         if (code1 == SUBREG)
1508           arg1 = SUBREG_REG (arg1), code1 = GET_CODE (arg1);
1509
1510         /* If this machine only allows one register per address, it must
1511            be in the first operand.  */
1512
1513         if (MAX_REGS_PER_ADDRESS == 1)
1514           record_address_regs (arg0, class, scale);
1515
1516         /* If index and base registers are the same on this machine, just
1517            record registers in any non-constant operands.  We assume here,
1518            as well as in the tests below, that all addresses are in 
1519            canonical form.  */
1520
1521         else if (INDEX_REG_CLASS == BASE_REG_CLASS)
1522           {
1523             record_address_regs (arg0, class, scale);
1524             if (! CONSTANT_P (arg1))
1525               record_address_regs (arg1, class, scale);
1526           }
1527
1528         /* If the second operand is a constant integer, it doesn't change
1529            what class the first operand must be.  */
1530
1531         else if (code1 == CONST_INT || code1 == CONST_DOUBLE)
1532           record_address_regs (arg0, class, scale);
1533
1534         /* If the second operand is a symbolic constant, the first operand
1535            must be an index register.  */
1536
1537         else if (code1 == SYMBOL_REF || code1 == CONST || code1 == LABEL_REF)
1538           record_address_regs (arg0, INDEX_REG_CLASS, scale);
1539
1540         /* If this the sum of two registers where the first is known to be a 
1541            pointer, it must be a base register with the second an index.  */
1542
1543         else if (code0 == REG && code1 == REG
1544                  && REGNO_POINTER_FLAG (REGNO (arg0)))
1545           {
1546             record_address_regs (arg0, BASE_REG_CLASS, scale);
1547             record_address_regs (arg1, INDEX_REG_CLASS, scale);
1548           }
1549
1550         /* If this is the sum of two registers and neither is known to
1551            be a pointer, count equal chances that each might be a base
1552            or index register.  This case should be rare.  */
1553
1554         else if (code0 == REG && code1 == REG
1555                  && ! REGNO_POINTER_FLAG (REGNO (arg0))
1556                  && ! REGNO_POINTER_FLAG (REGNO (arg1)))
1557           {
1558             record_address_regs (arg0, BASE_REG_CLASS, scale / 2);
1559             record_address_regs (arg0, INDEX_REG_CLASS, scale / 2);
1560             record_address_regs (arg1, BASE_REG_CLASS, scale / 2);
1561             record_address_regs (arg1, INDEX_REG_CLASS, scale / 2);
1562           }
1563
1564         /* In all other cases, the first operand is an index and the
1565            second is the base.  */
1566
1567         else
1568           {
1569             record_address_regs (arg0, INDEX_REG_CLASS, scale);
1570             record_address_regs (arg1, BASE_REG_CLASS, scale);
1571           }
1572       }
1573       break;
1574
1575     case POST_INC:
1576     case PRE_INC:
1577     case POST_DEC:
1578     case PRE_DEC:
1579       /* Double the importance of a pseudo register that is incremented
1580          or decremented, since it would take two extra insns
1581          if it ends up in the wrong place.  If the operand is a pseudo,
1582          show it is being used in an INC_DEC context.  */
1583
1584 #ifdef FORBIDDEN_INC_DEC_CLASSES
1585       if (GET_CODE (XEXP (x, 0)) == REG
1586           && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER)
1587         in_inc_dec[REGNO (XEXP (x, 0))] = 1;
1588 #endif
1589
1590       record_address_regs (XEXP (x, 0), class, 2 * scale);
1591       break;
1592
1593     case REG:
1594       {
1595         register struct costs *pp = &costs[REGNO (x)];
1596         register int i;
1597
1598         pp->mem_cost += (MEMORY_MOVE_COST (Pmode) * scale) / 2;
1599
1600         for (i = 0; i < N_REG_CLASSES; i++)
1601           pp->cost[i] += (may_move_cost[i][(int) class] * scale) / 2;
1602       }
1603       break;
1604
1605     default:
1606       {
1607         register char *fmt = GET_RTX_FORMAT (code);
1608         register int i;
1609         for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1610           if (fmt[i] == 'e')
1611             record_address_regs (XEXP (x, i), class, scale);
1612       }
1613     }
1614 }
1615 \f
1616 #ifdef FORBIDDEN_INC_DEC_CLASSES
1617
1618 /* Return 1 if REG is valid as an auto-increment memory reference
1619    to an object of MODE.  */
1620
1621 static 
1622 auto_inc_dec_reg_p (reg, mode)
1623      rtx reg;
1624      enum machine_mode mode;
1625 {
1626 #ifdef HAVE_POST_INCREMENT
1627   if (memory_address_p (mode, gen_rtx (POST_INC, Pmode, reg)))
1628     return 1;
1629 #endif
1630
1631 #ifdef HAVE_POST_DECREMENT
1632   if (memory_address_p (mode, gen_rtx (POST_DEC, Pmode, reg)))
1633     return 1;
1634 #endif
1635
1636 #ifdef HAVE_PRE_INCREMENT
1637   if (memory_address_p (mode, gen_rtx (PRE_INC, Pmode, reg)))
1638     return 1;
1639 #endif
1640
1641 #ifdef HAVE_PRE_DECREMENT
1642   if (memory_address_p (mode, gen_rtx (PRE_DEC, Pmode, reg)))
1643     return 1;
1644 #endif
1645
1646   return 0;
1647 }
1648 #endif
1649
1650 #endif /* REGISTER_CONSTRAINTS */
1651 \f
1652 /* Allocate enough space to hold NUM_REGS registers for the tables used for
1653    reg_scan and flow_analysis that are indexed by the register number.  If
1654    NEW_P is non zero, initialize all of the registers, otherwise only
1655    initialize the new registers allocated.  The same table is kept from
1656    function to function, only reallocating it when we need more room.  If
1657    RENUMBER_P is non zero, allocate the reg_renumber array also.  */
1658
1659 void
1660 allocate_reg_info (num_regs, new_p, renumber_p)
1661      int num_regs;
1662      int new_p;
1663      int renumber_p;
1664 {
1665   static int regno_allocated = 0;
1666   static int regno_max = 0;
1667   static short *renumber = (short *)0;
1668   int i;
1669   int size_info;
1670   int size_renumber;
1671   int min = (new_p) ? 0 : regno_max;
1672
1673   /* If this message come up, and you want to fix it, then all of the tables
1674      like reg_renumber, etc. that use short will have to be found and lengthed
1675      to int or HOST_WIDE_INT.  */
1676
1677   /* Free up all storage allocated */
1678   if (num_regs < 0)
1679     {
1680       if (reg_n_info)
1681         {
1682           free ((char *)reg_n_info);
1683           free ((char *)renumber);
1684           reg_n_info = (reg_info *)0;
1685           renumber = (short *)0;
1686         }
1687       regno_allocated = 0;
1688       regno_max = 0;
1689       return;
1690     }
1691
1692   if (num_regs > regno_allocated)
1693     {
1694       regno_allocated = num_regs + (num_regs / 20);     /* add some slop space */
1695       size_info = regno_allocated * sizeof (reg_info);
1696       size_renumber = regno_allocated * sizeof (short);
1697
1698       if (!reg_n_info)
1699         {
1700           reg_n_info = (reg_info *) xmalloc (size_info);
1701           renumber = (short *) xmalloc (size_renumber);
1702         }
1703
1704       else if (new_p)           /* if we're zapping everything, no need to realloc */
1705         {
1706           free ((char *)reg_n_info);
1707           free ((char *)renumber);
1708           reg_n_info = (reg_info *) xmalloc (size_info);
1709           renumber = (short *) xmalloc (size_renumber);
1710         }
1711
1712       else
1713         {
1714           reg_n_info = (reg_info *) xrealloc ((char *)reg_n_info, size_info);
1715           renumber = (short *) xrealloc ((char *)renumber, size_renumber);
1716         }
1717     }
1718
1719   if (min < num_regs)
1720     {
1721       bzero ((char *) &reg_n_info[min], (num_regs - min) * sizeof (reg_info));
1722       for (i = min; i < num_regs; i++)
1723         {
1724           REG_BASIC_BLOCK (i) = REG_BLOCK_UNKNOWN;
1725           renumber[i] = -1;
1726         }
1727     }
1728
1729   if (renumber_p)
1730     reg_renumber = renumber;
1731
1732   regno_max = num_regs;
1733 }
1734
1735 \f
1736 /* This is the `regscan' pass of the compiler, run just before cse
1737    and again just before loop.
1738
1739    It finds the first and last use of each pseudo-register
1740    and records them in the vectors regno_first_uid, regno_last_uid
1741    and counts the number of sets in the vector reg_n_sets.
1742
1743    REPEAT is nonzero the second time this is called.  */
1744
1745 /* Maximum number of parallel sets and clobbers in any insn in this fn.
1746    Always at least 3, since the combiner could put that many together
1747    and we want this to remain correct for all the remaining passes.  */
1748
1749 int max_parallel;
1750
1751 void
1752 reg_scan (f, nregs, repeat)
1753      rtx f;
1754      int nregs;
1755      int repeat;
1756 {
1757   register rtx insn;
1758
1759   allocate_reg_info (nregs, TRUE, FALSE);
1760   max_parallel = 3;
1761
1762   for (insn = f; insn; insn = NEXT_INSN (insn))
1763     if (GET_CODE (insn) == INSN
1764         || GET_CODE (insn) == CALL_INSN
1765         || GET_CODE (insn) == JUMP_INSN)
1766       {
1767         if (GET_CODE (PATTERN (insn)) == PARALLEL
1768             && XVECLEN (PATTERN (insn), 0) > max_parallel)
1769           max_parallel = XVECLEN (PATTERN (insn), 0);
1770         reg_scan_mark_refs (PATTERN (insn), insn, 0);
1771
1772         if (REG_NOTES (insn))
1773           reg_scan_mark_refs (REG_NOTES (insn), insn, 1);
1774       }
1775 }
1776
1777 /* X is the expression to scan.  INSN is the insn it appears in.
1778    NOTE_FLAG is nonzero if X is from INSN's notes rather than its body.  */
1779
1780 static void
1781 reg_scan_mark_refs (x, insn, note_flag)
1782      rtx x;
1783      rtx insn;
1784      int note_flag;
1785 {
1786   register enum rtx_code code = GET_CODE (x);
1787   register rtx dest;
1788   register rtx note;
1789
1790   switch (code)
1791     {
1792     case CONST_INT:
1793     case CONST:
1794     case CONST_DOUBLE:
1795     case CC0:
1796     case PC:
1797     case SYMBOL_REF:
1798     case LABEL_REF:
1799     case ADDR_VEC:
1800     case ADDR_DIFF_VEC:
1801       return;
1802
1803     case REG:
1804       {
1805         register int regno = REGNO (x);
1806
1807         REGNO_LAST_NOTE_UID (regno) = INSN_UID (insn);
1808         if (!note_flag)
1809           REGNO_LAST_UID (regno) = INSN_UID (insn);
1810         if (REGNO_FIRST_UID (regno) == 0)
1811           REGNO_FIRST_UID (regno) = INSN_UID (insn);
1812       }
1813       break;
1814
1815     case EXPR_LIST:
1816       if (XEXP (x, 0))
1817         reg_scan_mark_refs (XEXP (x, 0), insn, note_flag);
1818       if (XEXP (x, 1))
1819         reg_scan_mark_refs (XEXP (x, 1), insn, note_flag);
1820       break;
1821
1822     case INSN_LIST:
1823       if (XEXP (x, 1))
1824         reg_scan_mark_refs (XEXP (x, 1), insn, note_flag);
1825       break;
1826
1827     case SET:
1828       /* Count a set of the destination if it is a register.  */
1829       for (dest = SET_DEST (x);
1830            GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
1831            || GET_CODE (dest) == ZERO_EXTEND;
1832            dest = XEXP (dest, 0))
1833         ;
1834
1835       if (GET_CODE (dest) == REG)
1836         REG_N_SETS (REGNO (dest))++;
1837
1838       /* If this is setting a pseudo from another pseudo or the sum of a
1839          pseudo and a constant integer and the other pseudo is known to be
1840          a pointer, set the destination to be a pointer as well.
1841
1842          Likewise if it is setting the destination from an address or from a
1843          value equivalent to an address or to the sum of an address and
1844          something else.
1845                      
1846          But don't do any of this if the pseudo corresponds to a user
1847          variable since it should have already been set as a pointer based
1848          on the type.  */
1849
1850       if (GET_CODE (SET_DEST (x)) == REG
1851           && REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER
1852           && ! REG_USERVAR_P (SET_DEST (x))
1853           && ! REGNO_POINTER_FLAG (REGNO (SET_DEST (x)))
1854           && ((GET_CODE (SET_SRC (x)) == REG
1855                && REGNO_POINTER_FLAG (REGNO (SET_SRC (x))))
1856               || ((GET_CODE (SET_SRC (x)) == PLUS
1857                    || GET_CODE (SET_SRC (x)) == LO_SUM)
1858                   && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
1859                   && GET_CODE (XEXP (SET_SRC (x), 0)) == REG
1860                   && REGNO_POINTER_FLAG (REGNO (XEXP (SET_SRC (x), 0))))
1861               || GET_CODE (SET_SRC (x)) == CONST
1862               || GET_CODE (SET_SRC (x)) == SYMBOL_REF
1863               || GET_CODE (SET_SRC (x)) == LABEL_REF
1864               || (GET_CODE (SET_SRC (x)) == HIGH
1865                   && (GET_CODE (XEXP (SET_SRC (x), 0)) == CONST
1866                       || GET_CODE (XEXP (SET_SRC (x), 0)) == SYMBOL_REF
1867                       || GET_CODE (XEXP (SET_SRC (x), 0)) == LABEL_REF))
1868               || ((GET_CODE (SET_SRC (x)) == PLUS
1869                    || GET_CODE (SET_SRC (x)) == LO_SUM)
1870                   && (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST
1871                       || GET_CODE (XEXP (SET_SRC (x), 1)) == SYMBOL_REF
1872                       || GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF))
1873               || ((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
1874                   && (GET_CODE (XEXP (note, 0)) == CONST
1875                       || GET_CODE (XEXP (note, 0)) == SYMBOL_REF
1876                       || GET_CODE (XEXP (note, 0)) == LABEL_REF))))
1877         REGNO_POINTER_FLAG (REGNO (SET_DEST (x))) = 1;
1878
1879       /* ... fall through ...  */
1880
1881     default:
1882       {
1883         register char *fmt = GET_RTX_FORMAT (code);
1884         register int i;
1885         for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1886           {
1887             if (fmt[i] == 'e')
1888               reg_scan_mark_refs (XEXP (x, i), insn, note_flag);
1889             else if (fmt[i] == 'E' && XVEC (x, i) != 0)
1890               {
1891                 register int j;
1892                 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1893                   reg_scan_mark_refs (XVECEXP (x, i, j), insn, note_flag);
1894               }
1895           }
1896       }
1897     }
1898 }
1899 \f
1900 /* Return nonzero if C1 is a subset of C2, i.e., if every register in C1
1901    is also in C2.  */
1902
1903 int
1904 reg_class_subset_p (c1, c2)
1905      register enum reg_class c1;
1906      register enum reg_class c2;
1907 {
1908   if (c1 == c2) return 1;
1909
1910   if (c2 == ALL_REGS)
1911   win:
1912     return 1;
1913   GO_IF_HARD_REG_SUBSET (reg_class_contents[(int)c1],
1914                          reg_class_contents[(int)c2],
1915                          win);
1916   return 0;
1917 }
1918
1919 /* Return nonzero if there is a register that is in both C1 and C2.  */
1920
1921 int
1922 reg_classes_intersect_p (c1, c2)
1923      register enum reg_class c1;
1924      register enum reg_class c2;
1925 {
1926 #ifdef HARD_REG_SET
1927   register
1928 #endif
1929     HARD_REG_SET c;
1930
1931   if (c1 == c2) return 1;
1932
1933   if (c1 == ALL_REGS || c2 == ALL_REGS)
1934     return 1;
1935
1936   COPY_HARD_REG_SET (c, reg_class_contents[(int) c1]);
1937   AND_HARD_REG_SET (c, reg_class_contents[(int) c2]);
1938
1939   GO_IF_HARD_REG_SUBSET (c, reg_class_contents[(int) NO_REGS], lose);
1940   return 1;
1941
1942  lose:
1943   return 0;
1944 }
1945