OSDN Git Service

90th Cygnus<->FSF quick merge
[pf3gnuchains/gcc-fork.git] / gcc / local-alloc.c
1 /* Allocate registers within a basic block, for GNU compiler.
2    Copyright (C) 1987, 88, 91, 93, 94, 95, 1996 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 /* Allocation of hard register numbers to pseudo registers is done in
23    two passes.  In this pass we consider only regs that are born and
24    die once within one basic block.  We do this one basic block at a
25    time.  Then the next pass allocates the registers that remain.
26    Two passes are used because this pass uses methods that work only
27    on linear code, but that do a better job than the general methods
28    used in global_alloc, and more quickly too.
29
30    The assignments made are recorded in the vector reg_renumber
31    whose space is allocated here.  The rtl code itself is not altered.
32
33    We assign each instruction in the basic block a number
34    which is its order from the beginning of the block.
35    Then we can represent the lifetime of a pseudo register with
36    a pair of numbers, and check for conflicts easily.
37    We can record the availability of hard registers with a
38    HARD_REG_SET for each instruction.  The HARD_REG_SET
39    contains 0 or 1 for each hard reg.
40
41    To avoid register shuffling, we tie registers together when one
42    dies by being copied into another, or dies in an instruction that
43    does arithmetic to produce another.  The tied registers are
44    allocated as one.  Registers with different reg class preferences
45    can never be tied unless the class preferred by one is a subclass
46    of the one preferred by the other.
47
48    Tying is represented with "quantity numbers".
49    A non-tied register is given a new quantity number.
50    Tied registers have the same quantity number.
51    
52    We have provision to exempt registers, even when they are contained
53    within the block, that can be tied to others that are not contained in it.
54    This is so that global_alloc could process them both and tie them then.
55    But this is currently disabled since tying in global_alloc is not
56    yet implemented.  */
57
58 /* Pseudos allocated here cannot be reallocated by global.c if the hard
59    register is used as a spill register.  So we don't allocate such pseudos
60    here if their preferred class is likely to be used by spills.  */
61
62 #include <stdio.h>
63 #include "config.h"
64 #include "rtl.h"
65 #include "flags.h"
66 #include "basic-block.h"
67 #include "regs.h"
68 #include "hard-reg-set.h"
69 #include "insn-config.h"
70 #include "recog.h"
71 #include "output.h"
72 \f
73 /* Next quantity number available for allocation.  */
74
75 static int next_qty;
76
77 /* In all the following vectors indexed by quantity number.  */
78
79 /* Element Q is the hard reg number chosen for quantity Q,
80    or -1 if none was found.  */
81
82 static short *qty_phys_reg;
83
84 /* We maintain two hard register sets that indicate suggested hard registers
85    for each quantity.  The first, qty_phys_copy_sugg, contains hard registers
86    that are tied to the quantity by a simple copy.  The second contains all
87    hard registers that are tied to the quantity via an arithmetic operation.
88
89    The former register set is given priority for allocation.  This tends to
90    eliminate copy insns.  */
91
92 /* Element Q is a set of hard registers that are suggested for quantity Q by
93    copy insns.  */
94
95 static HARD_REG_SET *qty_phys_copy_sugg;
96
97 /* Element Q is a set of hard registers that are suggested for quantity Q by
98    arithmetic insns.  */
99
100 static HARD_REG_SET *qty_phys_sugg;
101
102 /* Element Q is the number of suggested registers in qty_phys_copy_sugg.  */
103
104 static short *qty_phys_num_copy_sugg;
105
106 /* Element Q is the number of suggested registers in qty_phys_sugg.  */
107
108 static short *qty_phys_num_sugg;
109
110 /* Element Q is the number of refs to quantity Q.  */
111
112 static int *qty_n_refs;
113
114 /* Element Q is a reg class contained in (smaller than) the
115    preferred classes of all the pseudo regs that are tied in quantity Q.
116    This is the preferred class for allocating that quantity.  */
117
118 static enum reg_class *qty_min_class;
119
120 /* Insn number (counting from head of basic block)
121    where quantity Q was born.  -1 if birth has not been recorded.  */
122
123 static int *qty_birth;
124
125 /* Insn number (counting from head of basic block)
126    where quantity Q died.  Due to the way tying is done,
127    and the fact that we consider in this pass only regs that die but once,
128    a quantity can die only once.  Each quantity's life span
129    is a set of consecutive insns.  -1 if death has not been recorded.  */
130
131 static int *qty_death;
132
133 /* Number of words needed to hold the data in quantity Q.
134    This depends on its machine mode.  It is used for these purposes:
135    1. It is used in computing the relative importances of qtys,
136       which determines the order in which we look for regs for them.
137    2. It is used in rules that prevent tying several registers of
138       different sizes in a way that is geometrically impossible
139       (see combine_regs).  */
140
141 static int *qty_size;
142
143 /* This holds the mode of the registers that are tied to qty Q,
144    or VOIDmode if registers with differing modes are tied together.  */
145
146 static enum machine_mode *qty_mode;
147
148 /* Number of times a reg tied to qty Q lives across a CALL_INSN.  */
149
150 static int *qty_n_calls_crossed;
151
152 /* Register class within which we allocate qty Q if we can't get
153    its preferred class.  */
154
155 static enum reg_class *qty_alternate_class;
156
157 /* Element Q is the SCRATCH expression for which this quantity is being
158    allocated or 0 if this quantity is allocating registers.  */
159
160 static rtx *qty_scratch_rtx;
161
162 /* Element Q is nonzero if this quantity has been used in a SUBREG
163    that changes its size.  */
164
165 static char *qty_changes_size;
166
167 /* Element Q is the register number of one pseudo register whose
168    reg_qty value is Q, or -1 is this quantity is for a SCRATCH.  This
169    register should be the head of the chain maintained in reg_next_in_qty.  */
170
171 static int *qty_first_reg;
172
173 /* If (REG N) has been assigned a quantity number, is a register number
174    of another register assigned the same quantity number, or -1 for the
175    end of the chain.  qty_first_reg point to the head of this chain.  */
176
177 static int *reg_next_in_qty;
178
179 /* reg_qty[N] (where N is a pseudo reg number) is the qty number of that reg
180    if it is >= 0,
181    of -1 if this register cannot be allocated by local-alloc,
182    or -2 if not known yet.
183
184    Note that if we see a use or death of pseudo register N with
185    reg_qty[N] == -2, register N must be local to the current block.  If
186    it were used in more than one block, we would have reg_qty[N] == -1.
187    This relies on the fact that if reg_basic_block[N] is >= 0, register N
188    will not appear in any other block.  We save a considerable number of
189    tests by exploiting this.
190
191    If N is < FIRST_PSEUDO_REGISTER, reg_qty[N] is undefined and should not
192    be referenced.  */
193
194 static int *reg_qty;
195
196 /* The offset (in words) of register N within its quantity.
197    This can be nonzero if register N is SImode, and has been tied
198    to a subreg of a DImode register.  */
199
200 static char *reg_offset;
201
202 /* Vector of substitutions of register numbers,
203    used to map pseudo regs into hardware regs.
204    This is set up as a result of register allocation.
205    Element N is the hard reg assigned to pseudo reg N,
206    or is -1 if no hard reg was assigned.
207    If N is a hard reg number, element N is N.  */
208
209 short *reg_renumber;
210
211 /* Set of hard registers live at the current point in the scan
212    of the instructions in a basic block.  */
213
214 static HARD_REG_SET regs_live;
215
216 /* Each set of hard registers indicates registers live at a particular
217    point in the basic block.  For N even, regs_live_at[N] says which
218    hard registers are needed *after* insn N/2 (i.e., they may not
219    conflict with the outputs of insn N/2 or the inputs of insn N/2 + 1.
220
221    If an object is to conflict with the inputs of insn J but not the
222    outputs of insn J + 1, we say it is born at index J*2 - 1.  Similarly,
223    if it is to conflict with the outputs of insn J but not the inputs of
224    insn J + 1, it is said to die at index J*2 + 1.  */
225
226 static HARD_REG_SET *regs_live_at;
227
228 int *scratch_block;
229 rtx *scratch_list;
230 int scratch_list_length;
231 static int scratch_index;
232
233 /* Communicate local vars `insn_number' and `insn'
234    from `block_alloc' to `reg_is_set', `wipe_dead_reg', and `alloc_qty'.  */
235 static int this_insn_number;
236 static rtx this_insn;
237
238 /* Used to communicate changes made by update_equiv_regs to
239    memref_referenced_p.  reg_equiv_replacement is set for any REG_EQUIV note
240    found or created, so that we can keep track of what memory accesses might
241    be created later, e.g. by reload.  */
242
243 static rtx *reg_equiv_replacement;
244
245 static void alloc_qty           PROTO((int, enum machine_mode, int, int));
246 static void alloc_qty_for_scratch PROTO((rtx, int, rtx, int, int));
247 static void validate_equiv_mem_from_store PROTO((rtx, rtx));
248 static int validate_equiv_mem   PROTO((rtx, rtx, rtx));
249 static int memref_referenced_p  PROTO((rtx, rtx));
250 static int memref_used_between_p PROTO((rtx, rtx, rtx));
251 static void optimize_reg_copy_1 PROTO((rtx, rtx, rtx));
252 static void optimize_reg_copy_2 PROTO((rtx, rtx, rtx));
253 static void update_equiv_regs   PROTO((void));
254 static void block_alloc         PROTO((int));
255 static int qty_sugg_compare     PROTO((int, int));
256 static int qty_sugg_compare_1   PROTO((const GENERIC_PTR, const GENERIC_PTR));
257 static int qty_compare          PROTO((int, int));
258 static int qty_compare_1        PROTO((const GENERIC_PTR, const GENERIC_PTR));
259 static int combine_regs         PROTO((rtx, rtx, int, int, rtx, int));
260 static int reg_meets_class_p    PROTO((int, enum reg_class));
261 static int reg_classes_overlap_p PROTO((enum reg_class, enum reg_class,
262                                         int));
263 static void update_qty_class    PROTO((int, int));
264 static void reg_is_set          PROTO((rtx, rtx));
265 static void reg_is_born         PROTO((rtx, int));
266 static void wipe_dead_reg       PROTO((rtx, int));
267 static int find_free_reg        PROTO((enum reg_class, enum machine_mode,
268                                        int, int, int, int, int));
269 static void mark_life           PROTO((int, enum machine_mode, int));
270 static void post_mark_life      PROTO((int, enum machine_mode, int, int, int));
271 static int no_conflict_p        PROTO((rtx, rtx, rtx));
272 static int requires_inout       PROTO((char *));
273 \f
274 /* Allocate a new quantity (new within current basic block)
275    for register number REGNO which is born at index BIRTH
276    within the block.  MODE and SIZE are info on reg REGNO.  */
277
278 static void
279 alloc_qty (regno, mode, size, birth)
280      int regno;
281      enum machine_mode mode;
282      int size, birth;
283 {
284   register int qty = next_qty++;
285
286   reg_qty[regno] = qty;
287   reg_offset[regno] = 0;
288   reg_next_in_qty[regno] = -1;
289
290   qty_first_reg[qty] = regno;
291   qty_size[qty] = size;
292   qty_mode[qty] = mode;
293   qty_birth[qty] = birth;
294   qty_n_calls_crossed[qty] = reg_n_calls_crossed[regno];
295   qty_min_class[qty] = reg_preferred_class (regno);
296   qty_alternate_class[qty] = reg_alternate_class (regno);
297   qty_n_refs[qty] = reg_n_refs[regno];
298   qty_changes_size[qty] = reg_changes_size[regno];
299 }
300 \f
301 /* Similar to `alloc_qty', but allocates a quantity for a SCRATCH rtx
302    used as operand N in INSN.  We assume here that the SCRATCH is used in
303    a CLOBBER.  */
304
305 static void
306 alloc_qty_for_scratch (scratch, n, insn, insn_code_num, insn_number)
307      rtx scratch;
308      int n;
309      rtx insn;
310      int insn_code_num, insn_number;
311 {
312   register int qty;
313   enum reg_class class;
314   char *p, c;
315   int i;
316
317 #ifdef REGISTER_CONSTRAINTS
318   /* If we haven't yet computed which alternative will be used, do so now.
319      Then set P to the constraints for that alternative.  */
320   if (which_alternative == -1)
321     if (! constrain_operands (insn_code_num, 0))
322       return;
323
324   for (p = insn_operand_constraint[insn_code_num][n], i = 0;
325        *p && i < which_alternative; p++)
326     if (*p == ',')
327       i++;
328
329   /* Compute the class required for this SCRATCH.  If we don't need a
330      register, the class will remain NO_REGS.  If we guessed the alternative
331      number incorrectly, reload will fix things up for us.  */
332
333   class = NO_REGS;
334   while ((c = *p++) != '\0' && c != ',')
335     switch (c)
336       {
337       case '=':  case '+':  case '?':
338       case '#':  case '&':  case '!':
339       case '*':  case '%':  
340       case '0':  case '1':  case '2':  case '3':  case '4':
341       case 'm':  case '<':  case '>':  case 'V':  case 'o':
342       case 'E':  case 'F':  case 'G':  case 'H':
343       case 's':  case 'i':  case 'n':
344       case 'I':  case 'J':  case 'K':  case 'L':
345       case 'M':  case 'N':  case 'O':  case 'P':
346 #ifdef EXTRA_CONSTRAINT
347       case 'Q':  case 'R':  case 'S':  case 'T':  case 'U':
348 #endif
349       case 'p':
350         /* These don't say anything we care about.  */
351         break;
352
353       case 'X':
354         /* We don't need to allocate this SCRATCH.  */
355         return;
356
357       case 'g': case 'r':
358         class = reg_class_subunion[(int) class][(int) GENERAL_REGS];
359         break;
360
361       default:
362         class
363           = reg_class_subunion[(int) class][(int) REG_CLASS_FROM_LETTER (c)];
364         break;
365       }
366
367   if (class == NO_REGS)
368     return;
369
370 #else /* REGISTER_CONSTRAINTS */
371
372   class = GENERAL_REGS;
373 #endif
374   
375
376   qty = next_qty++;
377
378   qty_first_reg[qty] = -1;
379   qty_scratch_rtx[qty] = scratch;
380   qty_size[qty] = GET_MODE_SIZE (GET_MODE (scratch));
381   qty_mode[qty] = GET_MODE (scratch);
382   qty_birth[qty] = 2 * insn_number - 1;
383   qty_death[qty] = 2 * insn_number + 1;
384   qty_n_calls_crossed[qty] = 0;
385   qty_min_class[qty] = class;
386   qty_alternate_class[qty] = NO_REGS;
387   qty_n_refs[qty] = 1;
388   qty_changes_size[qty] = 0;
389 }
390 \f
391 /* Main entry point of this file.  */
392
393 void
394 local_alloc ()
395 {
396   register int b, i;
397   int max_qty;
398
399   /* Leaf functions and non-leaf functions have different needs.
400      If defined, let the machine say what kind of ordering we
401      should use.  */
402 #ifdef ORDER_REGS_FOR_LOCAL_ALLOC
403   ORDER_REGS_FOR_LOCAL_ALLOC;
404 #endif
405
406   /* Promote REG_EQUAL notes to REG_EQUIV notes and adjust status of affected
407      registers.  */
408   update_equiv_regs ();
409
410   /* This sets the maximum number of quantities we can have.  Quantity
411      numbers start at zero and we can have one for each pseudo plus the
412      number of SCRATCHes in the largest block, in the worst case.  */
413   max_qty = (max_regno - FIRST_PSEUDO_REGISTER) + max_scratch;
414
415   /* Allocate vectors of temporary data.
416      See the declarations of these variables, above,
417      for what they mean.  */
418
419   /* There can be up to MAX_SCRATCH * N_BASIC_BLOCKS SCRATCHes to allocate.
420      Instead of allocating this much memory from now until the end of
421      reload, only allocate space for MAX_QTY SCRATCHes.  If there are more
422      reload will allocate them.  */
423
424   scratch_list_length = max_qty;
425   scratch_list = (rtx *) xmalloc (scratch_list_length * sizeof (rtx));
426   bzero ((char *) scratch_list, scratch_list_length * sizeof (rtx));
427   scratch_block = (int *) xmalloc (scratch_list_length * sizeof (int));
428   bzero ((char *) scratch_block, scratch_list_length * sizeof (int));
429   scratch_index = 0;
430
431   qty_phys_reg = (short *) alloca (max_qty * sizeof (short));
432   qty_phys_copy_sugg
433     = (HARD_REG_SET *) alloca (max_qty * sizeof (HARD_REG_SET));
434   qty_phys_num_copy_sugg = (short *) alloca (max_qty * sizeof (short));
435   qty_phys_sugg = (HARD_REG_SET *) alloca (max_qty * sizeof (HARD_REG_SET));
436   qty_phys_num_sugg = (short *) alloca (max_qty * sizeof (short));
437   qty_birth = (int *) alloca (max_qty * sizeof (int));
438   qty_death = (int *) alloca (max_qty * sizeof (int));
439   qty_scratch_rtx = (rtx *) alloca (max_qty * sizeof (rtx));
440   qty_first_reg = (int *) alloca (max_qty * sizeof (int));
441   qty_size = (int *) alloca (max_qty * sizeof (int));
442   qty_mode
443     = (enum machine_mode *) alloca (max_qty * sizeof (enum machine_mode));
444   qty_n_calls_crossed = (int *) alloca (max_qty * sizeof (int));
445   qty_min_class
446     = (enum reg_class *) alloca (max_qty * sizeof (enum reg_class));
447   qty_alternate_class
448     = (enum reg_class *) alloca (max_qty * sizeof (enum reg_class));
449   qty_n_refs = (int *) alloca (max_qty * sizeof (int));
450   qty_changes_size = (char *) alloca (max_qty * sizeof (char));
451
452   reg_qty = (int *) alloca (max_regno * sizeof (int));
453   reg_offset = (char *) alloca (max_regno * sizeof (char));
454   reg_next_in_qty = (int *) alloca (max_regno * sizeof (int));
455
456   reg_renumber = (short *) oballoc (max_regno * sizeof (short));
457   for (i = 0; i < max_regno; i++)
458     reg_renumber[i] = -1;
459
460   /* Determine which pseudo-registers can be allocated by local-alloc.
461      In general, these are the registers used only in a single block and
462      which only die once.  However, if a register's preferred class has only
463      a few entries, don't allocate this register here unless it is preferred
464      or nothing since retry_global_alloc won't be able to move it to
465      GENERAL_REGS if a reload register of this class is needed.
466
467      We need not be concerned with which block actually uses the register
468      since we will never see it outside that block.  */
469
470   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
471     {
472       if (reg_basic_block[i] >= 0 && reg_n_deaths[i] == 1
473           && (reg_alternate_class (i) == NO_REGS
474               || ! CLASS_LIKELY_SPILLED_P (reg_preferred_class (i))))
475         reg_qty[i] = -2;
476       else
477         reg_qty[i] = -1;
478     }
479
480   /* Force loop below to initialize entire quantity array.  */
481   next_qty = max_qty;
482
483   /* Allocate each block's local registers, block by block.  */
484
485   for (b = 0; b < n_basic_blocks; b++)
486     {
487       /* NEXT_QTY indicates which elements of the `qty_...'
488          vectors might need to be initialized because they were used
489          for the previous block; it is set to the entire array before
490          block 0.  Initialize those, with explicit loop if there are few,
491          else with bzero and bcopy.  Do not initialize vectors that are
492          explicit set by `alloc_qty'.  */
493
494       if (next_qty < 6)
495         {
496           for (i = 0; i < next_qty; i++)
497             {
498               qty_scratch_rtx[i] = 0;
499               CLEAR_HARD_REG_SET (qty_phys_copy_sugg[i]);
500               qty_phys_num_copy_sugg[i] = 0;
501               CLEAR_HARD_REG_SET (qty_phys_sugg[i]);
502               qty_phys_num_sugg[i] = 0;
503             }
504         }
505       else
506         {
507 #define CLEAR(vector)  \
508           bzero ((char *) (vector), (sizeof (*(vector))) * next_qty);
509
510           CLEAR (qty_scratch_rtx);
511           CLEAR (qty_phys_copy_sugg);
512           CLEAR (qty_phys_num_copy_sugg);
513           CLEAR (qty_phys_sugg);
514           CLEAR (qty_phys_num_sugg);
515         }
516
517       next_qty = 0;
518
519       block_alloc (b);
520 #ifdef USE_C_ALLOCA
521       alloca (0);
522 #endif
523     }
524 }
525 \f
526 /* Depth of loops we are in while in update_equiv_regs.  */
527 static int loop_depth;
528
529 /* Used for communication between the following two functions: contains
530    a MEM that we wish to ensure remains unchanged.  */
531 static rtx equiv_mem;
532
533 /* Set nonzero if EQUIV_MEM is modified.  */
534 static int equiv_mem_modified;
535
536 /* If EQUIV_MEM is modified by modifying DEST, indicate that it is modified.
537    Called via note_stores.  */
538
539 static void
540 validate_equiv_mem_from_store (dest, set)
541      rtx dest;
542      rtx set;
543 {
544   if ((GET_CODE (dest) == REG
545        && reg_overlap_mentioned_p (dest, equiv_mem))
546       || (GET_CODE (dest) == MEM
547           && true_dependence (dest, equiv_mem)))
548     equiv_mem_modified = 1;
549 }
550
551 /* Verify that no store between START and the death of REG invalidates
552    MEMREF.  MEMREF is invalidated by modifying a register used in MEMREF,
553    by storing into an overlapping memory location, or with a non-const
554    CALL_INSN.
555
556    Return 1 if MEMREF remains valid.  */
557
558 static int
559 validate_equiv_mem (start, reg, memref)
560      rtx start;
561      rtx reg;
562      rtx memref;
563 {
564   rtx insn;
565   rtx note;
566
567   equiv_mem = memref;
568   equiv_mem_modified = 0;
569
570   /* If the memory reference has side effects or is volatile, it isn't a
571      valid equivalence.  */
572   if (side_effects_p (memref))
573     return 0;
574
575   for (insn = start; insn && ! equiv_mem_modified; insn = NEXT_INSN (insn))
576     {
577       if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
578         continue;
579
580       if (find_reg_note (insn, REG_DEAD, reg))
581         return 1;
582
583       if (GET_CODE (insn) == CALL_INSN && ! RTX_UNCHANGING_P (memref)
584           && ! CONST_CALL_P (insn))
585         return 0;
586
587       note_stores (PATTERN (insn), validate_equiv_mem_from_store);
588
589       /* If a register mentioned in MEMREF is modified via an
590          auto-increment, we lose the equivalence.  Do the same if one
591          dies; although we could extend the life, it doesn't seem worth
592          the trouble.  */
593
594       for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
595         if ((REG_NOTE_KIND (note) == REG_INC
596              || REG_NOTE_KIND (note) == REG_DEAD)
597             && GET_CODE (XEXP (note, 0)) == REG
598             && reg_overlap_mentioned_p (XEXP (note, 0), memref))
599           return 0;
600     }
601
602   return 0;
603 }
604 \f
605 /* TRUE if X references a memory location that would be affected by a store
606    to MEMREF.  */
607
608 static int
609 memref_referenced_p (memref, x)
610      rtx x;
611      rtx memref;
612 {
613   int i, j;
614   char *fmt;
615   enum rtx_code code = GET_CODE (x);
616
617   switch (code)
618     {
619     case CONST_INT:
620     case CONST:
621     case LABEL_REF:
622     case SYMBOL_REF:
623     case CONST_DOUBLE:
624     case PC:
625     case CC0:
626     case HIGH:
627     case LO_SUM:
628       return 0;
629
630     case REG:
631       return (reg_equiv_replacement[REGNO (x)]
632               && memref_referenced_p (memref,
633                                       reg_equiv_replacement[REGNO (x)]));
634
635     case MEM:
636       if (true_dependence (memref, x))
637         return 1;
638       break;
639
640     case SET:
641       /* If we are setting a MEM, it doesn't count (its address does), but any
642          other SET_DEST that has a MEM in it is referencing the MEM.  */
643       if (GET_CODE (SET_DEST (x)) == MEM)
644         {
645           if (memref_referenced_p (memref, XEXP (SET_DEST (x), 0)))
646             return 1;
647         }
648       else if (memref_referenced_p (memref, SET_DEST (x)))
649         return 1;
650
651       return memref_referenced_p (memref, SET_SRC (x));
652     }
653
654   fmt = GET_RTX_FORMAT (code);
655   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
656     switch (fmt[i])
657       {
658       case 'e':
659         if (memref_referenced_p (memref, XEXP (x, i)))
660           return 1;
661         break;
662       case 'E':
663         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
664           if (memref_referenced_p (memref, XVECEXP (x, i, j)))
665             return 1;
666         break;
667       }
668
669   return 0;
670 }
671
672 /* TRUE if some insn in the range (START, END] references a memory location
673    that would be affected by a store to MEMREF.  */
674
675 static int
676 memref_used_between_p (memref, start, end)
677      rtx memref;
678      rtx start;
679      rtx end;
680 {
681   rtx insn;
682
683   for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
684        insn = NEXT_INSN (insn))
685     if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
686         && memref_referenced_p (memref, PATTERN (insn)))
687       return 1;
688
689   return 0;
690 }
691 \f
692 /* INSN is a copy from SRC to DEST, both registers, and SRC does not die
693    in INSN.
694
695    Search forward to see if SRC dies before either it or DEST is modified,
696    but don't scan past the end of a basic block.  If so, we can replace SRC
697    with DEST and let SRC die in INSN. 
698
699    This will reduce the number of registers live in that range and may enable
700    DEST to be tied to SRC, thus often saving one register in addition to a
701    register-register copy.  */
702
703 static void
704 optimize_reg_copy_1 (insn, dest, src)
705      rtx insn;
706      rtx dest;
707      rtx src;
708 {
709   rtx p, q;
710   rtx note;
711   rtx dest_death = 0;
712   int sregno = REGNO (src);
713   int dregno = REGNO (dest);
714
715   if (sregno == dregno
716 #ifdef SMALL_REGISTER_CLASSES
717       /* We don't want to mess with hard regs if register classes are small. */
718       || (SMALL_REGISTER_CLASSES
719           && (sregno < FIRST_PSEUDO_REGISTER
720               || dregno < FIRST_PSEUDO_REGISTER))
721 #endif
722       /* We don't see all updates to SP if they are in an auto-inc memory
723          reference, so we must disallow this optimization on them.  */
724       || sregno == STACK_POINTER_REGNUM || dregno == STACK_POINTER_REGNUM)
725     return;
726
727   for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
728     {
729       if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
730           || (GET_CODE (p) == NOTE
731               && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
732                   || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
733         break;
734
735       if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
736         continue;
737
738       if (reg_set_p (src, p) || reg_set_p (dest, p)
739           /* Don't change a USE of a register.  */
740           || (GET_CODE (PATTERN (p)) == USE
741               && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
742         break;
743
744       /* See if all of SRC dies in P.  This test is slightly more
745          conservative than it needs to be.  */
746       if ((note = find_regno_note (p, REG_DEAD, sregno)) != 0
747           && GET_MODE (XEXP (note, 0)) == GET_MODE (src))
748         {
749           int failed = 0;
750           int length = 0;
751           int d_length = 0;
752           int n_calls = 0;
753           int d_n_calls = 0;
754
755           /* We can do the optimization.  Scan forward from INSN again,
756              replacing regs as we go.  Set FAILED if a replacement can't
757              be done.  In that case, we can't move the death note for SRC.
758              This should be rare.  */
759
760           /* Set to stop at next insn.  */
761           for (q = next_real_insn (insn);
762                q != next_real_insn (p);
763                q = next_real_insn (q))
764             {
765               if (reg_overlap_mentioned_p (src, PATTERN (q)))
766                 {
767                   /* If SRC is a hard register, we might miss some
768                      overlapping registers with validate_replace_rtx,
769                      so we would have to undo it.  We can't if DEST is
770                      present in the insn, so fail in that combination
771                      of cases.  */
772                   if (sregno < FIRST_PSEUDO_REGISTER
773                       && reg_mentioned_p (dest, PATTERN (q)))
774                     failed = 1;
775
776                   /* Replace all uses and make sure that the register
777                      isn't still present.  */
778                   else if (validate_replace_rtx (src, dest, q)
779                            && (sregno >= FIRST_PSEUDO_REGISTER
780                                || ! reg_overlap_mentioned_p (src,
781                                                              PATTERN (q))))
782                     {
783                       /* We assume that a register is used exactly once per
784                          insn in the updates below.  If this is not correct,
785                          no great harm is done.  */
786                       if (sregno >= FIRST_PSEUDO_REGISTER)
787                         reg_n_refs[sregno] -= loop_depth;
788                       if (dregno >= FIRST_PSEUDO_REGISTER)
789                         reg_n_refs[dregno] += loop_depth;
790                     }
791                   else
792                     {
793                       validate_replace_rtx (dest, src, q);
794                       failed = 1;
795                     }
796                 }
797
798               /* Count the insns and CALL_INSNs passed.  If we passed the
799                  death note of DEST, show increased live length.  */
800               length++;
801               if (dest_death)
802                 d_length++;
803
804               /* If the insn in which SRC dies is a CALL_INSN, don't count it
805                  as a call that has been crossed.  Otherwise, count it.  */
806               if (q != p && GET_CODE (q) == CALL_INSN)
807                 {
808                   n_calls++;
809                   if (dest_death)
810                     d_n_calls++;
811                 }
812
813               /* If DEST dies here, remove the death note and save it for
814                  later.  Make sure ALL of DEST dies here; again, this is
815                  overly conservative.  */
816               if (dest_death == 0
817                   && (dest_death = find_regno_note (q, REG_DEAD, dregno)) != 0
818                   && GET_MODE (XEXP (dest_death, 0)) == GET_MODE (dest))
819                 remove_note (q, dest_death);
820             }
821
822           if (! failed)
823             {
824               if (sregno >= FIRST_PSEUDO_REGISTER)
825                 {
826                   if (reg_live_length[sregno] >= 0)
827                     {
828                       reg_live_length[sregno] -= length;
829                       /* reg_live_length is only an approximation after
830                          combine if sched is not run, so make sure that we
831                          still have a reasonable value.  */
832                       if (reg_live_length[sregno] < 2)
833                         reg_live_length[sregno] = 2;
834                     }
835
836                   reg_n_calls_crossed[sregno] -= n_calls;
837                 }
838
839               if (dregno >= FIRST_PSEUDO_REGISTER)
840                 {
841                   if (reg_live_length[dregno] >= 0)
842                     reg_live_length[dregno] += d_length;
843
844                   reg_n_calls_crossed[dregno] += d_n_calls;
845                 }
846
847               /* Move death note of SRC from P to INSN.  */
848               remove_note (p, note);
849               XEXP (note, 1) = REG_NOTES (insn);
850               REG_NOTES (insn) = note;
851             }
852
853           /* Put death note of DEST on P if we saw it die.  */
854           if (dest_death)
855             {
856               XEXP (dest_death, 1) = REG_NOTES (p);
857               REG_NOTES (p) = dest_death;
858             }
859
860           return;
861         }
862
863       /* If SRC is a hard register which is set or killed in some other
864          way, we can't do this optimization.  */
865       else if (sregno < FIRST_PSEUDO_REGISTER
866                && dead_or_set_p (p, src))
867         break;
868     }
869 }
870 \f
871 /* INSN is a copy of SRC to DEST, in which SRC dies.  See if we now have
872    a sequence of insns that modify DEST followed by an insn that sets
873    SRC to DEST in which DEST dies, with no prior modification of DEST.
874    (There is no need to check if the insns in between actually modify
875    DEST.  We should not have cases where DEST is not modified, but
876    the optimization is safe if no such modification is detected.)
877    In that case, we can replace all uses of DEST, starting with INSN and
878    ending with the set of SRC to DEST, with SRC.  We do not do this
879    optimization if a CALL_INSN is crossed unless SRC already crosses a
880    call or if DEST dies before the copy back to SRC.
881
882    It is assumed that DEST and SRC are pseudos; it is too complicated to do
883    this for hard registers since the substitutions we may make might fail.  */
884
885 static void
886 optimize_reg_copy_2 (insn, dest, src)
887      rtx insn;
888      rtx dest;
889      rtx src;
890 {
891   rtx p, q;
892   rtx set;
893   int sregno = REGNO (src);
894   int dregno = REGNO (dest);
895
896   for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
897     {
898       if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
899           || (GET_CODE (p) == NOTE
900               && (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG
901                   || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)))
902         break;
903
904       if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
905         continue;
906
907       set = single_set (p);
908       if (set && SET_SRC (set) == dest && SET_DEST (set) == src
909           && find_reg_note (p, REG_DEAD, dest))
910         {
911           /* We can do the optimization.  Scan forward from INSN again,
912              replacing regs as we go.  */
913
914           /* Set to stop at next insn.  */
915           for (q = insn; q != NEXT_INSN (p); q = NEXT_INSN (q))
916             if (GET_RTX_CLASS (GET_CODE (q)) == 'i')
917               {
918                 if (reg_mentioned_p (dest, PATTERN (q)))
919                   {
920                     PATTERN (q) = replace_rtx (PATTERN (q), dest, src);
921
922                     /* We assume that a register is used exactly once per
923                        insn in the updates below.  If this is not correct,
924                        no great harm is done.  */
925                     reg_n_refs[dregno] -= loop_depth;
926                     reg_n_refs[sregno] += loop_depth;
927                   }
928
929
930               if (GET_CODE (q) == CALL_INSN)
931                 {
932                   reg_n_calls_crossed[dregno]--;
933                   reg_n_calls_crossed[sregno]++;
934                 }
935               }
936
937           remove_note (p, find_reg_note (p, REG_DEAD, dest));
938           reg_n_deaths[dregno]--;
939           remove_note (insn, find_reg_note (insn, REG_DEAD, src));
940           reg_n_deaths[sregno]--;
941           return;
942         }
943
944       if (reg_set_p (src, p)
945           || find_reg_note (p, REG_DEAD, dest)
946           || (GET_CODE (p) == CALL_INSN && reg_n_calls_crossed[sregno] == 0))
947         break;
948     }
949 }
950 \f             
951 /* Find registers that are equivalent to a single value throughout the
952    compilation (either because they can be referenced in memory or are set once
953    from a single constant).  Lower their priority for a register.
954
955    If such a register is only referenced once, try substituting its value
956    into the using insn.  If it succeeds, we can eliminate the register
957    completely.  */
958
959 static void
960 update_equiv_regs ()
961 {
962   rtx *reg_equiv_init_insn = (rtx *) alloca (max_regno * sizeof (rtx *));
963   /* Set when an attempt should be made to replace a register with the
964      associated reg_equiv_replacement entry at the end of this function.  */
965   char *reg_equiv_replace
966     = (char *) alloca (max_regno * sizeof *reg_equiv_replace);
967   rtx insn;
968
969   reg_equiv_replacement = (rtx *) alloca (max_regno * sizeof (rtx *));
970
971   bzero ((char *) reg_equiv_init_insn, max_regno * sizeof (rtx *));
972   bzero ((char *) reg_equiv_replacement, max_regno * sizeof (rtx *));
973   bzero ((char *) reg_equiv_replace, max_regno * sizeof *reg_equiv_replace);
974
975   init_alias_analysis ();
976
977   loop_depth = 1;
978
979   /* Scan the insns and find which registers have equivalences.  Do this
980      in a separate scan of the insns because (due to -fcse-follow-jumps)
981      a register can be set below its use.  */
982   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
983     {
984       rtx note;
985       rtx set = single_set (insn);
986       rtx dest, src;
987       int regno;
988
989       if (GET_CODE (insn) == NOTE)
990         {
991           if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
992             loop_depth++;
993           else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
994             loop_depth--;
995         }
996
997       /* If this insn contains more (or less) than a single SET, ignore it.  */
998       if (set == 0)
999         continue;
1000
1001       dest = SET_DEST (set);
1002       src = SET_SRC (set);
1003
1004       /* If this sets a MEM to the contents of a REG that is only used
1005          in a single basic block, see if the register is always equivalent
1006          to that memory location and if moving the store from INSN to the
1007          insn that set REG is safe.  If so, put a REG_EQUIV note on the
1008          initializing insn.  */
1009
1010       if (GET_CODE (dest) == MEM && GET_CODE (SET_SRC (set)) == REG
1011           && (regno = REGNO (SET_SRC (set))) >= FIRST_PSEUDO_REGISTER
1012           && reg_basic_block[regno] >= 0
1013           && reg_equiv_init_insn[regno] != 0
1014           && validate_equiv_mem (reg_equiv_init_insn[regno], SET_SRC (set),
1015                                  dest)
1016           && ! memref_used_between_p (SET_DEST (set),
1017                                       reg_equiv_init_insn[regno], insn))
1018         REG_NOTES (reg_equiv_init_insn[regno])
1019           = gen_rtx (EXPR_LIST, REG_EQUIV, dest,
1020                      REG_NOTES (reg_equiv_init_insn[regno]));
1021
1022       /* If this is a register-register copy where SRC is not dead, see if we
1023          can optimize it.  */
1024       if (flag_expensive_optimizations && GET_CODE (dest) == REG
1025           && GET_CODE (SET_SRC (set)) == REG
1026           && ! find_reg_note (insn, REG_DEAD, SET_SRC (set)))
1027         optimize_reg_copy_1 (insn, dest, SET_SRC (set));
1028
1029       /* Similarly for a pseudo-pseudo copy when SRC is dead.  */
1030       else if (flag_expensive_optimizations && GET_CODE (dest) == REG
1031                && REGNO (dest) >= FIRST_PSEUDO_REGISTER
1032                && GET_CODE (SET_SRC (set)) == REG
1033                && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER
1034                && find_reg_note (insn, REG_DEAD, SET_SRC (set)))
1035         optimize_reg_copy_2 (insn, dest, SET_SRC (set));
1036
1037       /* Otherwise, we only handle the case of a pseudo register being set
1038          once and only if neither the source nor the destination are
1039          in a register class that's likely to be spilled.  */
1040       if (GET_CODE (dest) != REG
1041           || (regno = REGNO (dest)) < FIRST_PSEUDO_REGISTER
1042           || reg_n_sets[regno] != 1
1043           || CLASS_LIKELY_SPILLED_P (reg_preferred_class (REGNO (dest)))
1044           || (GET_CODE (src) == REG
1045               && REGNO (src) >= FIRST_PSEUDO_REGISTER
1046               && CLASS_LIKELY_SPILLED_P (reg_preferred_class (REGNO (src)))))
1047         continue;
1048
1049       note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
1050
1051       /* Record this insn as initializing this register.  */
1052       reg_equiv_init_insn[regno] = insn;
1053
1054       /* If this register is known to be equal to a constant, record that
1055          it is always equivalent to the constant.  */
1056       if (note && CONSTANT_P (XEXP (note, 0)))
1057         PUT_MODE (note, (enum machine_mode) REG_EQUIV);
1058
1059       /* If this insn introduces a "constant" register, decrease the priority
1060          of that register.  Record this insn if the register is only used once
1061          more and the equivalence value is the same as our source.
1062
1063          The latter condition is checked for two reasons:  First, it is an
1064          indication that it may be more efficient to actually emit the insn
1065          as written (if no registers are available, reload will substitute
1066          the equivalence).  Secondly, it avoids problems with any registers
1067          dying in this insn whose death notes would be missed.
1068
1069          If we don't have a REG_EQUIV note, see if this insn is loading
1070          a register used only in one basic block from a MEM.  If so, and the
1071          MEM remains unchanged for the life of the register, add a REG_EQUIV
1072          note.  */
1073          
1074       note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
1075
1076       if (note == 0 && reg_basic_block[regno] >= 0
1077           && GET_CODE (SET_SRC (set)) == MEM
1078           && validate_equiv_mem (insn, dest, SET_SRC (set)))
1079         REG_NOTES (insn) = note = gen_rtx (EXPR_LIST, REG_EQUIV, SET_SRC (set),
1080                                            REG_NOTES (insn));
1081
1082       if (note)
1083         {
1084           int regno = REGNO (dest);
1085
1086           reg_equiv_replacement[regno] = XEXP (note, 0);
1087
1088           /* Don't mess with things live during setjmp.  */
1089           if (reg_live_length[regno] >= 0)
1090             {
1091               /* Note that the statement below does not affect the priority
1092                  in local-alloc!  */
1093               reg_live_length[regno] *= 2;
1094
1095
1096               /* If the register is referenced exactly twice, meaning it is
1097                  set once and used once, indicate that the reference may be
1098                  replaced by the equivalence we computed above.  If the
1099                  register is only used in one basic block, this can't succeed
1100                  or combine would have done it.
1101
1102                  It would be nice to use "loop_depth * 2" in the compare
1103                  below.  Unfortunately, LOOP_DEPTH need not be constant within
1104                  a basic block so this would be too complicated.
1105
1106                  This case normally occurs when a parameter is read from
1107                  memory and then used exactly once, not in a loop.  */
1108
1109                 if (reg_n_refs[regno] == 2
1110                     && reg_basic_block[regno] < 0
1111                     && rtx_equal_p (XEXP (note, 0), SET_SRC (set)))
1112                   reg_equiv_replace[regno] = 1;
1113             }
1114         }
1115     }
1116
1117   /* Now scan all regs killed in an insn to see if any of them are registers
1118      only used that once.  If so, see if we can replace the reference with
1119      the equivalent from.  If we can, delete the initializing reference
1120      and this register will go away.  */
1121   for (insn = next_active_insn (get_insns ());
1122        insn;
1123        insn = next_active_insn (insn))
1124     {
1125       rtx link;
1126
1127       for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1128         if (REG_NOTE_KIND (link) == REG_DEAD
1129             /* Make sure this insn still refers to the register.  */
1130             && reg_mentioned_p (XEXP (link, 0), PATTERN (insn)))
1131           {
1132             int regno = REGNO (XEXP (link, 0));
1133
1134             if (reg_equiv_replace[regno]
1135                 && validate_replace_rtx (regno_reg_rtx[regno],
1136                                          reg_equiv_replacement[regno], insn))
1137               {
1138                 rtx equiv_insn = reg_equiv_init_insn[regno];
1139
1140                 remove_death (regno, insn);
1141                 reg_n_refs[regno] = 0;
1142                 PUT_CODE (equiv_insn, NOTE);
1143                 NOTE_LINE_NUMBER (equiv_insn) = NOTE_INSN_DELETED;
1144                 NOTE_SOURCE_FILE (equiv_insn) = 0;
1145               }
1146           }
1147     }
1148 }
1149 \f
1150 /* Allocate hard regs to the pseudo regs used only within block number B.
1151    Only the pseudos that die but once can be handled.  */
1152
1153 static void
1154 block_alloc (b)
1155      int b;
1156 {
1157   register int i, q;
1158   register rtx insn;
1159   rtx note;
1160   int insn_number = 0;
1161   int insn_count = 0;
1162   int max_uid = get_max_uid ();
1163   int *qty_order;
1164   int no_conflict_combined_regno = -1;
1165   /* Counter to prevent allocating more SCRATCHes than can be stored
1166      in SCRATCH_LIST.  */
1167   int scratches_allocated = scratch_index;
1168
1169   /* Count the instructions in the basic block.  */
1170
1171   insn = basic_block_end[b];
1172   while (1)
1173     {
1174       if (GET_CODE (insn) != NOTE)
1175         if (++insn_count > max_uid)
1176           abort ();
1177       if (insn == basic_block_head[b])
1178         break;
1179       insn = PREV_INSN (insn);
1180     }
1181
1182   /* +2 to leave room for a post_mark_life at the last insn and for
1183      the birth of a CLOBBER in the first insn.  */
1184   regs_live_at = (HARD_REG_SET *) alloca ((2 * insn_count + 2)
1185                                           * sizeof (HARD_REG_SET));
1186   bzero ((char *) regs_live_at, (2 * insn_count + 2) * sizeof (HARD_REG_SET));
1187
1188   /* Initialize table of hardware registers currently live.  */
1189
1190 #ifdef HARD_REG_SET
1191   regs_live = *basic_block_live_at_start[b];
1192 #else
1193   COPY_HARD_REG_SET (regs_live, basic_block_live_at_start[b]);
1194 #endif
1195
1196   /* This loop scans the instructions of the basic block
1197      and assigns quantities to registers.
1198      It computes which registers to tie.  */
1199
1200   insn = basic_block_head[b];
1201   while (1)
1202     {
1203       register rtx body = PATTERN (insn);
1204
1205       if (GET_CODE (insn) != NOTE)
1206         insn_number++;
1207
1208       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1209         {
1210           register rtx link, set;
1211           register int win = 0;
1212           register rtx r0, r1;
1213           int combined_regno = -1;
1214           int i;
1215           int insn_code_number = recog_memoized (insn);
1216
1217           this_insn_number = insn_number;
1218           this_insn = insn;
1219
1220           if (insn_code_number >= 0)
1221             insn_extract (insn);
1222           which_alternative = -1;
1223
1224           /* Is this insn suitable for tying two registers?
1225              If so, try doing that.
1226              Suitable insns are those with at least two operands and where
1227              operand 0 is an output that is a register that is not
1228              earlyclobber.
1229
1230              We can tie operand 0 with some operand that dies in this insn.
1231              First look for operands that are required to be in the same
1232              register as operand 0.  If we find such, only try tying that
1233              operand or one that can be put into that operand if the
1234              operation is commutative.  If we don't find an operand
1235              that is required to be in the same register as operand 0,
1236              we can tie with any operand.
1237
1238              Subregs in place of regs are also ok.
1239
1240              If tying is done, WIN is set nonzero.  */
1241
1242           if (insn_code_number >= 0
1243 #ifdef REGISTER_CONSTRAINTS
1244               && insn_n_operands[insn_code_number] > 1
1245               && insn_operand_constraint[insn_code_number][0][0] == '='
1246               && insn_operand_constraint[insn_code_number][0][1] != '&'
1247 #else
1248               && GET_CODE (PATTERN (insn)) == SET
1249               && rtx_equal_p (SET_DEST (PATTERN (insn)), recog_operand[0])
1250 #endif
1251               )
1252             {
1253 #ifdef REGISTER_CONSTRAINTS
1254               /* If non-negative, is an operand that must match operand 0.  */
1255               int must_match_0 = -1;
1256               /* Counts number of alternatives that require a match with
1257                  operand 0.  */
1258               int n_matching_alts = 0;
1259
1260               for (i = 1; i < insn_n_operands[insn_code_number]; i++)
1261                 {
1262                   char *p = insn_operand_constraint[insn_code_number][i];
1263                   int this_match = (requires_inout (p));
1264
1265                   n_matching_alts += this_match;
1266                   if (this_match == insn_n_alternatives[insn_code_number])
1267                     must_match_0 = i;
1268                 }
1269 #endif
1270
1271               r0 = recog_operand[0];
1272               for (i = 1; i < insn_n_operands[insn_code_number]; i++)
1273                 {
1274 #ifdef REGISTER_CONSTRAINTS
1275                   /* Skip this operand if we found an operand that
1276                      must match operand 0 and this operand isn't it
1277                      and can't be made to be it by commutativity.  */
1278
1279                   if (must_match_0 >= 0 && i != must_match_0
1280                       && ! (i == must_match_0 + 1
1281                             && insn_operand_constraint[insn_code_number][i-1][0] == '%')
1282                       && ! (i == must_match_0 - 1
1283                             && insn_operand_constraint[insn_code_number][i][0] == '%'))
1284                     continue;
1285
1286                   /* Likewise if each alternative has some operand that
1287                      must match operand zero.  In that case, skip any 
1288                      operand that doesn't list operand 0 since we know that
1289                      the operand always conflicts with operand 0.  We
1290                      ignore commutatity in this case to keep things simple.  */
1291                   if (n_matching_alts == insn_n_alternatives[insn_code_number]
1292                       && (0 == requires_inout
1293                           (insn_operand_constraint[insn_code_number][i])))
1294                     continue;
1295 #endif
1296
1297                   r1 = recog_operand[i];
1298
1299                   /* If the operand is an address, find a register in it.
1300                      There may be more than one register, but we only try one
1301                      of them.  */
1302                   if (
1303 #ifdef REGISTER_CONSTRAINTS
1304                       insn_operand_constraint[insn_code_number][i][0] == 'p'
1305 #else
1306                       insn_operand_address_p[insn_code_number][i]
1307 #endif
1308                       )
1309                     while (GET_CODE (r1) == PLUS || GET_CODE (r1) == MULT)
1310                       r1 = XEXP (r1, 0);
1311
1312                   if (GET_CODE (r0) == REG || GET_CODE (r0) == SUBREG)
1313                     {
1314                       /* We have two priorities for hard register preferences.
1315                          If we have a move insn or an insn whose first input
1316                          can only be in the same register as the output, give
1317                          priority to an equivalence found from that insn.  */
1318                       int may_save_copy
1319                         = ((SET_DEST (body) == r0 && SET_SRC (body) == r1)
1320 #ifdef REGISTER_CONSTRAINTS
1321                            || (r1 == recog_operand[i] && must_match_0 >= 0)
1322 #endif
1323                            );
1324                       
1325                       if (GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG)
1326                         win = combine_regs (r1, r0, may_save_copy,
1327                                             insn_number, insn, 0);
1328                     }
1329                   if (win)
1330                     break;
1331                 }
1332             }
1333
1334           /* Recognize an insn sequence with an ultimate result
1335              which can safely overlap one of the inputs.
1336              The sequence begins with a CLOBBER of its result,
1337              and ends with an insn that copies the result to itself
1338              and has a REG_EQUAL note for an equivalent formula.
1339              That note indicates what the inputs are.
1340              The result and the input can overlap if each insn in
1341              the sequence either doesn't mention the input
1342              or has a REG_NO_CONFLICT note to inhibit the conflict.
1343
1344              We do the combining test at the CLOBBER so that the
1345              destination register won't have had a quantity number
1346              assigned, since that would prevent combining.  */
1347
1348           if (GET_CODE (PATTERN (insn)) == CLOBBER
1349               && (r0 = XEXP (PATTERN (insn), 0),
1350                   GET_CODE (r0) == REG)
1351               && (link = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0
1352               && XEXP (link, 0) != 0
1353               && GET_CODE (XEXP (link, 0)) == INSN
1354               && (set = single_set (XEXP (link, 0))) != 0
1355               && SET_DEST (set) == r0 && SET_SRC (set) == r0
1356               && (note = find_reg_note (XEXP (link, 0), REG_EQUAL,
1357                                         NULL_RTX)) != 0)
1358             {
1359               if (r1 = XEXP (note, 0), GET_CODE (r1) == REG
1360                   /* Check that we have such a sequence.  */
1361                   && no_conflict_p (insn, r0, r1))
1362                 win = combine_regs (r1, r0, 1, insn_number, insn, 1);
1363               else if (GET_RTX_FORMAT (GET_CODE (XEXP (note, 0)))[0] == 'e'
1364                        && (r1 = XEXP (XEXP (note, 0), 0),
1365                            GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG)
1366                        && no_conflict_p (insn, r0, r1))
1367                 win = combine_regs (r1, r0, 0, insn_number, insn, 1);
1368
1369               /* Here we care if the operation to be computed is
1370                  commutative.  */
1371               else if ((GET_CODE (XEXP (note, 0)) == EQ
1372                         || GET_CODE (XEXP (note, 0)) == NE
1373                         || GET_RTX_CLASS (GET_CODE (XEXP (note, 0))) == 'c')
1374                        && (r1 = XEXP (XEXP (note, 0), 1),
1375                            (GET_CODE (r1) == REG || GET_CODE (r1) == SUBREG))
1376                        && no_conflict_p (insn, r0, r1))
1377                 win = combine_regs (r1, r0, 0, insn_number, insn, 1);
1378
1379               /* If we did combine something, show the register number
1380                  in question so that we know to ignore its death.  */
1381               if (win)
1382                 no_conflict_combined_regno = REGNO (r1);
1383             }
1384
1385           /* If registers were just tied, set COMBINED_REGNO
1386              to the number of the register used in this insn
1387              that was tied to the register set in this insn.
1388              This register's qty should not be "killed".  */
1389
1390           if (win)
1391             {
1392               while (GET_CODE (r1) == SUBREG)
1393                 r1 = SUBREG_REG (r1);
1394               combined_regno = REGNO (r1);
1395             }
1396
1397           /* Mark the death of everything that dies in this instruction,
1398              except for anything that was just combined.  */
1399
1400           for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1401             if (REG_NOTE_KIND (link) == REG_DEAD
1402                 && GET_CODE (XEXP (link, 0)) == REG
1403                 && combined_regno != REGNO (XEXP (link, 0))
1404                 && (no_conflict_combined_regno != REGNO (XEXP (link, 0))
1405                     || ! find_reg_note (insn, REG_NO_CONFLICT, XEXP (link, 0))))
1406               wipe_dead_reg (XEXP (link, 0), 0);
1407
1408           /* Allocate qty numbers for all registers local to this block
1409              that are born (set) in this instruction.
1410              A pseudo that already has a qty is not changed.  */
1411
1412           note_stores (PATTERN (insn), reg_is_set);
1413
1414           /* If anything is set in this insn and then unused, mark it as dying
1415              after this insn, so it will conflict with our outputs.  This
1416              can't match with something that combined, and it doesn't matter
1417              if it did.  Do this after the calls to reg_is_set since these
1418              die after, not during, the current insn.  */
1419
1420           for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1421             if (REG_NOTE_KIND (link) == REG_UNUSED
1422                 && GET_CODE (XEXP (link, 0)) == REG)
1423               wipe_dead_reg (XEXP (link, 0), 1);
1424
1425           /* Allocate quantities for any SCRATCH operands of this insn.  */
1426
1427           if (insn_code_number >= 0)
1428             for (i = 0; i < insn_n_operands[insn_code_number]; i++)
1429               if (GET_CODE (recog_operand[i]) == SCRATCH
1430                   && scratches_allocated++ < scratch_list_length)
1431                 alloc_qty_for_scratch (recog_operand[i], i, insn,
1432                                        insn_code_number, insn_number);
1433
1434           /* If this is an insn that has a REG_RETVAL note pointing at a 
1435              CLOBBER insn, we have reached the end of a REG_NO_CONFLICT
1436              block, so clear any register number that combined within it.  */
1437           if ((note = find_reg_note (insn, REG_RETVAL, NULL_RTX)) != 0
1438               && GET_CODE (XEXP (note, 0)) == INSN
1439               && GET_CODE (PATTERN (XEXP (note, 0))) == CLOBBER)
1440             no_conflict_combined_regno = -1;
1441         }
1442
1443       /* Set the registers live after INSN_NUMBER.  Note that we never
1444          record the registers live before the block's first insn, since no
1445          pseudos we care about are live before that insn.  */
1446
1447       IOR_HARD_REG_SET (regs_live_at[2 * insn_number], regs_live);
1448       IOR_HARD_REG_SET (regs_live_at[2 * insn_number + 1], regs_live);
1449
1450       if (insn == basic_block_end[b])
1451         break;
1452
1453       insn = NEXT_INSN (insn);
1454     }
1455
1456   /* Now every register that is local to this basic block
1457      should have been given a quantity, or else -1 meaning ignore it.
1458      Every quantity should have a known birth and death.  
1459
1460      Order the qtys so we assign them registers in order of the
1461      number of suggested registers they need so we allocate those with
1462      the most restrictive needs first.  */
1463
1464   qty_order = (int *) alloca (next_qty * sizeof (int));
1465   for (i = 0; i < next_qty; i++)
1466     qty_order[i] = i;
1467
1468 #define EXCHANGE(I1, I2)  \
1469   { i = qty_order[I1]; qty_order[I1] = qty_order[I2]; qty_order[I2] = i; }
1470
1471   switch (next_qty)
1472     {
1473     case 3:
1474       /* Make qty_order[2] be the one to allocate last.  */
1475       if (qty_sugg_compare (0, 1) > 0)
1476         EXCHANGE (0, 1);
1477       if (qty_sugg_compare (1, 2) > 0)
1478         EXCHANGE (2, 1);
1479
1480       /* ... Fall through ...  */
1481     case 2:
1482       /* Put the best one to allocate in qty_order[0].  */
1483       if (qty_sugg_compare (0, 1) > 0)
1484         EXCHANGE (0, 1);
1485
1486       /* ... Fall through ...  */
1487
1488     case 1:
1489     case 0:
1490       /* Nothing to do here.  */
1491       break;
1492
1493     default:
1494       qsort (qty_order, next_qty, sizeof (int), qty_sugg_compare_1);
1495     }
1496
1497   /* Try to put each quantity in a suggested physical register, if it has one.
1498      This may cause registers to be allocated that otherwise wouldn't be, but
1499      this seems acceptable in local allocation (unlike global allocation).  */
1500   for (i = 0; i < next_qty; i++)
1501     {
1502       q = qty_order[i];
1503       if (qty_phys_num_sugg[q] != 0 || qty_phys_num_copy_sugg[q] != 0)
1504         qty_phys_reg[q] = find_free_reg (qty_min_class[q], qty_mode[q], q,
1505                                          0, 1, qty_birth[q], qty_death[q]);
1506       else
1507         qty_phys_reg[q] = -1;
1508     }
1509
1510   /* Order the qtys so we assign them registers in order of 
1511      decreasing length of life.  Normally call qsort, but if we 
1512      have only a very small number of quantities, sort them ourselves.  */
1513
1514   for (i = 0; i < next_qty; i++)
1515     qty_order[i] = i;
1516
1517 #define EXCHANGE(I1, I2)  \
1518   { i = qty_order[I1]; qty_order[I1] = qty_order[I2]; qty_order[I2] = i; }
1519
1520   switch (next_qty)
1521     {
1522     case 3:
1523       /* Make qty_order[2] be the one to allocate last.  */
1524       if (qty_compare (0, 1) > 0)
1525         EXCHANGE (0, 1);
1526       if (qty_compare (1, 2) > 0)
1527         EXCHANGE (2, 1);
1528
1529       /* ... Fall through ...  */
1530     case 2:
1531       /* Put the best one to allocate in qty_order[0].  */
1532       if (qty_compare (0, 1) > 0)
1533         EXCHANGE (0, 1);
1534
1535       /* ... Fall through ...  */
1536
1537     case 1:
1538     case 0:
1539       /* Nothing to do here.  */
1540       break;
1541
1542     default:
1543       qsort (qty_order, next_qty, sizeof (int), qty_compare_1);
1544     }
1545
1546   /* Now for each qty that is not a hardware register,
1547      look for a hardware register to put it in.
1548      First try the register class that is cheapest for this qty,
1549      if there is more than one class.  */
1550
1551   for (i = 0; i < next_qty; i++)
1552     {
1553       q = qty_order[i];
1554       if (qty_phys_reg[q] < 0)
1555         {
1556           if (N_REG_CLASSES > 1)
1557             {
1558               qty_phys_reg[q] = find_free_reg (qty_min_class[q], 
1559                                                qty_mode[q], q, 0, 0,
1560                                                qty_birth[q], qty_death[q]);
1561               if (qty_phys_reg[q] >= 0)
1562                 continue;
1563             }
1564
1565           if (qty_alternate_class[q] != NO_REGS)
1566             qty_phys_reg[q] = find_free_reg (qty_alternate_class[q],
1567                                              qty_mode[q], q, 0, 0,
1568                                              qty_birth[q], qty_death[q]);
1569         }
1570     }
1571
1572   /* Now propagate the register assignments
1573      to the pseudo regs belonging to the qtys.  */
1574
1575   for (q = 0; q < next_qty; q++)
1576     if (qty_phys_reg[q] >= 0)
1577       {
1578         for (i = qty_first_reg[q]; i >= 0; i = reg_next_in_qty[i])
1579           reg_renumber[i] = qty_phys_reg[q] + reg_offset[i];
1580         if (qty_scratch_rtx[q])
1581           {
1582             if (GET_CODE (qty_scratch_rtx[q]) == REG)
1583               abort ();
1584             PUT_CODE (qty_scratch_rtx[q], REG);
1585             REGNO (qty_scratch_rtx[q]) = qty_phys_reg[q];
1586
1587             scratch_block[scratch_index] = b;
1588             scratch_list[scratch_index++] = qty_scratch_rtx[q];
1589
1590             /* Must clear the USED field, because it will have been set by
1591                copy_rtx_if_shared, but the leaf_register code expects that
1592                it is zero in all REG rtx.  copy_rtx_if_shared does not set the
1593                used bit for REGs, but does for SCRATCHes.  */
1594             qty_scratch_rtx[q]->used = 0;
1595           }
1596       }
1597 }
1598 \f
1599 /* Compare two quantities' priority for getting real registers.
1600    We give shorter-lived quantities higher priority.
1601    Quantities with more references are also preferred, as are quantities that
1602    require multiple registers.  This is the identical prioritization as
1603    done by global-alloc.
1604
1605    We used to give preference to registers with *longer* lives, but using
1606    the same algorithm in both local- and global-alloc can speed up execution
1607    of some programs by as much as a factor of three!  */
1608
1609 /* Note that the quotient will never be bigger than
1610    the value of floor_log2 times the maximum number of
1611    times a register can occur in one insn (surely less than 100).
1612    Multiplying this by 10000 can't overflow.
1613    QTY_CMP_PRI is also used by qty_sugg_compare.  */
1614
1615 #define QTY_CMP_PRI(q)          \
1616   ((int) (((double) (floor_log2 (qty_n_refs[q]) * qty_n_refs[q] * qty_size[q]) \
1617           / (qty_death[q] - qty_birth[q])) * 10000))
1618
1619 static int
1620 qty_compare (q1, q2)
1621      int q1, q2;
1622 {
1623   return QTY_CMP_PRI (q2) - QTY_CMP_PRI (q1);
1624 }
1625
1626 static int
1627 qty_compare_1 (q1p, q2p)
1628      const GENERIC_PTR q1p;
1629      const GENERIC_PTR q2p;
1630 {
1631   register int q1 = *(int *)q1p, q2 = *(int *)q2p;
1632   register int tem = QTY_CMP_PRI (q2) - QTY_CMP_PRI (q1);
1633
1634   if (tem != 0)
1635     return tem;
1636
1637   /* If qtys are equally good, sort by qty number,
1638      so that the results of qsort leave nothing to chance.  */
1639   return q1 - q2;
1640 }
1641 \f
1642 /* Compare two quantities' priority for getting real registers.  This version
1643    is called for quantities that have suggested hard registers.  First priority
1644    goes to quantities that have copy preferences, then to those that have
1645    normal preferences.  Within those groups, quantities with the lower
1646    number of preferences have the highest priority.  Of those, we use the same
1647    algorithm as above.  */
1648
1649 #define QTY_CMP_SUGG(q)         \
1650   (qty_phys_num_copy_sugg[q]            \
1651     ? qty_phys_num_copy_sugg[q] \
1652     : qty_phys_num_sugg[q] * FIRST_PSEUDO_REGISTER)
1653
1654 static int
1655 qty_sugg_compare (q1, q2)
1656      int q1, q2;
1657 {
1658   register int tem = QTY_CMP_SUGG (q1) - QTY_CMP_SUGG (q2);
1659
1660   if (tem != 0)
1661     return tem;
1662   
1663   return QTY_CMP_PRI (q2) - QTY_CMP_PRI (q1);
1664 }
1665
1666 static int
1667 qty_sugg_compare_1 (q1p, q2p)
1668      const GENERIC_PTR q1p;
1669      const GENERIC_PTR q2p;
1670 {
1671   register int q1 = *(int *)q1p, q2 = *(int *)q2p;
1672   register int tem = QTY_CMP_SUGG (q1) - QTY_CMP_SUGG (q2);
1673
1674   if (tem != 0)
1675     return tem;
1676
1677   tem = QTY_CMP_PRI (q2) - QTY_CMP_PRI (q1);
1678   if (tem != 0)
1679     return tem;
1680
1681   /* If qtys are equally good, sort by qty number,
1682      so that the results of qsort leave nothing to chance.  */
1683   return q1 - q2;
1684 }
1685
1686 #undef QTY_CMP_SUGG
1687 #undef QTY_CMP_PRI
1688 \f
1689 /* Attempt to combine the two registers (rtx's) USEDREG and SETREG.
1690    Returns 1 if have done so, or 0 if cannot.
1691
1692    Combining registers means marking them as having the same quantity
1693    and adjusting the offsets within the quantity if either of
1694    them is a SUBREG).
1695
1696    We don't actually combine a hard reg with a pseudo; instead
1697    we just record the hard reg as the suggestion for the pseudo's quantity.
1698    If we really combined them, we could lose if the pseudo lives
1699    across an insn that clobbers the hard reg (eg, movstr).
1700
1701    ALREADY_DEAD is non-zero if USEDREG is known to be dead even though
1702    there is no REG_DEAD note on INSN.  This occurs during the processing
1703    of REG_NO_CONFLICT blocks.
1704
1705    MAY_SAVE_COPYCOPY is non-zero if this insn is simply copying USEDREG to
1706    SETREG or if the input and output must share a register.
1707    In that case, we record a hard reg suggestion in QTY_PHYS_COPY_SUGG.
1708    
1709    There are elaborate checks for the validity of combining.  */
1710
1711    
1712 static int
1713 combine_regs (usedreg, setreg, may_save_copy, insn_number, insn, already_dead)
1714      rtx usedreg, setreg;
1715      int may_save_copy;
1716      int insn_number;
1717      rtx insn;
1718      int already_dead;
1719 {
1720   register int ureg, sreg;
1721   register int offset = 0;
1722   int usize, ssize;
1723   register int sqty;
1724
1725   /* Determine the numbers and sizes of registers being used.  If a subreg
1726      is present that does not change the entire register, don't consider
1727      this a copy insn.  */
1728
1729   while (GET_CODE (usedreg) == SUBREG)
1730     {
1731       if (GET_MODE_SIZE (GET_MODE (SUBREG_REG (usedreg))) > UNITS_PER_WORD)
1732         may_save_copy = 0;
1733       offset += SUBREG_WORD (usedreg);
1734       usedreg = SUBREG_REG (usedreg);
1735     }
1736   if (GET_CODE (usedreg) != REG)
1737     return 0;
1738   ureg = REGNO (usedreg);
1739   usize = REG_SIZE (usedreg);
1740
1741   while (GET_CODE (setreg) == SUBREG)
1742     {
1743       if (GET_MODE_SIZE (GET_MODE (SUBREG_REG (setreg))) > UNITS_PER_WORD)
1744         may_save_copy = 0;
1745       offset -= SUBREG_WORD (setreg);
1746       setreg = SUBREG_REG (setreg);
1747     }
1748   if (GET_CODE (setreg) != REG)
1749     return 0;
1750   sreg = REGNO (setreg);
1751   ssize = REG_SIZE (setreg);
1752
1753   /* If UREG is a pseudo-register that hasn't already been assigned a
1754      quantity number, it means that it is not local to this block or dies
1755      more than once.  In either event, we can't do anything with it.  */
1756   if ((ureg >= FIRST_PSEUDO_REGISTER && reg_qty[ureg] < 0)
1757       /* Do not combine registers unless one fits within the other.  */
1758       || (offset > 0 && usize + offset > ssize)
1759       || (offset < 0 && usize + offset < ssize)
1760       /* Do not combine with a smaller already-assigned object
1761          if that smaller object is already combined with something bigger.  */
1762       || (ssize > usize && ureg >= FIRST_PSEUDO_REGISTER
1763           && usize < qty_size[reg_qty[ureg]])
1764       /* Can't combine if SREG is not a register we can allocate.  */
1765       || (sreg >= FIRST_PSEUDO_REGISTER && reg_qty[sreg] == -1)
1766       /* Don't combine with a pseudo mentioned in a REG_NO_CONFLICT note.
1767          These have already been taken care of.  This probably wouldn't
1768          combine anyway, but don't take any chances.  */
1769       || (ureg >= FIRST_PSEUDO_REGISTER
1770           && find_reg_note (insn, REG_NO_CONFLICT, usedreg))
1771       /* Don't tie something to itself.  In most cases it would make no
1772          difference, but it would screw up if the reg being tied to itself
1773          also dies in this insn.  */
1774       || ureg == sreg
1775       /* Don't try to connect two different hardware registers.  */
1776       || (ureg < FIRST_PSEUDO_REGISTER && sreg < FIRST_PSEUDO_REGISTER)
1777       /* Don't connect two different machine modes if they have different
1778          implications as to which registers may be used.  */
1779       || !MODES_TIEABLE_P (GET_MODE (usedreg), GET_MODE (setreg)))
1780     return 0;
1781
1782   /* Now, if UREG is a hard reg and SREG is a pseudo, record the hard reg in
1783      qty_phys_sugg for the pseudo instead of tying them.
1784
1785      Return "failure" so that the lifespan of UREG is terminated here;
1786      that way the two lifespans will be disjoint and nothing will prevent
1787      the pseudo reg from being given this hard reg.  */
1788
1789   if (ureg < FIRST_PSEUDO_REGISTER)
1790     {
1791       /* Allocate a quantity number so we have a place to put our
1792          suggestions.  */
1793       if (reg_qty[sreg] == -2)
1794         reg_is_born (setreg, 2 * insn_number);
1795
1796       if (reg_qty[sreg] >= 0)
1797         {
1798           if (may_save_copy
1799               && ! TEST_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[sreg]], ureg))
1800             {
1801               SET_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[sreg]], ureg);
1802               qty_phys_num_copy_sugg[reg_qty[sreg]]++;
1803             }
1804           else if (! TEST_HARD_REG_BIT (qty_phys_sugg[reg_qty[sreg]], ureg))
1805             {
1806               SET_HARD_REG_BIT (qty_phys_sugg[reg_qty[sreg]], ureg);
1807               qty_phys_num_sugg[reg_qty[sreg]]++;
1808             }
1809         }
1810       return 0;
1811     }
1812
1813   /* Similarly for SREG a hard register and UREG a pseudo register.  */
1814
1815   if (sreg < FIRST_PSEUDO_REGISTER)
1816     {
1817       if (may_save_copy
1818           && ! TEST_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[ureg]], sreg))
1819         {
1820           SET_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[ureg]], sreg);
1821           qty_phys_num_copy_sugg[reg_qty[ureg]]++;
1822         }
1823       else if (! TEST_HARD_REG_BIT (qty_phys_sugg[reg_qty[ureg]], sreg))
1824         {
1825           SET_HARD_REG_BIT (qty_phys_sugg[reg_qty[ureg]], sreg);
1826           qty_phys_num_sugg[reg_qty[ureg]]++;
1827         }
1828       return 0;
1829     }
1830
1831   /* At this point we know that SREG and UREG are both pseudos.
1832      Do nothing if SREG already has a quantity or is a register that we
1833      don't allocate.  */
1834   if (reg_qty[sreg] >= -1
1835       /* If we are not going to let any regs live across calls,
1836          don't tie a call-crossing reg to a non-call-crossing reg.  */
1837       || (current_function_has_nonlocal_label
1838           && ((reg_n_calls_crossed[ureg] > 0)
1839               != (reg_n_calls_crossed[sreg] > 0))))
1840     return 0;
1841
1842   /* We don't already know about SREG, so tie it to UREG
1843      if this is the last use of UREG, provided the classes they want
1844      are compatible.  */
1845
1846   if ((already_dead || find_regno_note (insn, REG_DEAD, ureg))
1847       && reg_meets_class_p (sreg, qty_min_class[reg_qty[ureg]]))
1848     {
1849       /* Add SREG to UREG's quantity.  */
1850       sqty = reg_qty[ureg];
1851       reg_qty[sreg] = sqty;
1852       reg_offset[sreg] = reg_offset[ureg] + offset;
1853       reg_next_in_qty[sreg] = qty_first_reg[sqty];
1854       qty_first_reg[sqty] = sreg;
1855
1856       /* If SREG's reg class is smaller, set qty_min_class[SQTY].  */
1857       update_qty_class (sqty, sreg);
1858
1859       /* Update info about quantity SQTY.  */
1860       qty_n_calls_crossed[sqty] += reg_n_calls_crossed[sreg];
1861       qty_n_refs[sqty] += reg_n_refs[sreg];
1862       if (usize < ssize)
1863         {
1864           register int i;
1865
1866           for (i = qty_first_reg[sqty]; i >= 0; i = reg_next_in_qty[i])
1867             reg_offset[i] -= offset;
1868
1869           qty_size[sqty] = ssize;
1870           qty_mode[sqty] = GET_MODE (setreg);
1871         }
1872     }
1873   else
1874     return 0;
1875
1876   return 1;
1877 }
1878 \f
1879 /* Return 1 if the preferred class of REG allows it to be tied
1880    to a quantity or register whose class is CLASS.
1881    True if REG's reg class either contains or is contained in CLASS.  */
1882
1883 static int
1884 reg_meets_class_p (reg, class)
1885      int reg;
1886      enum reg_class class;
1887 {
1888   register enum reg_class rclass = reg_preferred_class (reg);
1889   return (reg_class_subset_p (rclass, class)
1890           || reg_class_subset_p (class, rclass));
1891 }
1892
1893 /* Return 1 if the two specified classes have registers in common.
1894    If CALL_SAVED, then consider only call-saved registers.  */
1895
1896 static int
1897 reg_classes_overlap_p (c1, c2, call_saved)
1898      register enum reg_class c1;
1899      register enum reg_class c2;
1900      int call_saved;
1901 {
1902   HARD_REG_SET c;
1903   int i;
1904
1905   COPY_HARD_REG_SET (c, reg_class_contents[(int) c1]);
1906   AND_HARD_REG_SET (c, reg_class_contents[(int) c2]);
1907
1908   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1909     if (TEST_HARD_REG_BIT (c, i)
1910         && (! call_saved || ! call_used_regs[i]))
1911       return 1;
1912
1913   return 0;
1914 }
1915
1916 /* Update the class of QTY assuming that REG is being tied to it.  */
1917
1918 static void
1919 update_qty_class (qty, reg)
1920      int qty;
1921      int reg;
1922 {
1923   enum reg_class rclass = reg_preferred_class (reg);
1924   if (reg_class_subset_p (rclass, qty_min_class[qty]))
1925     qty_min_class[qty] = rclass;
1926
1927   rclass = reg_alternate_class (reg);
1928   if (reg_class_subset_p (rclass, qty_alternate_class[qty]))
1929     qty_alternate_class[qty] = rclass;
1930
1931   if (reg_changes_size[reg])
1932     qty_changes_size[qty] = 1;
1933 }
1934 \f
1935 /* Handle something which alters the value of an rtx REG.
1936
1937    REG is whatever is set or clobbered.  SETTER is the rtx that
1938    is modifying the register.
1939
1940    If it is not really a register, we do nothing.
1941    The file-global variables `this_insn' and `this_insn_number'
1942    carry info from `block_alloc'.  */
1943
1944 static void
1945 reg_is_set (reg, setter)
1946      rtx reg;
1947      rtx setter;
1948 {
1949   /* Note that note_stores will only pass us a SUBREG if it is a SUBREG of
1950      a hard register.  These may actually not exist any more.  */
1951
1952   if (GET_CODE (reg) != SUBREG
1953       && GET_CODE (reg) != REG)
1954     return;
1955
1956   /* Mark this register as being born.  If it is used in a CLOBBER, mark
1957      it as being born halfway between the previous insn and this insn so that
1958      it conflicts with our inputs but not the outputs of the previous insn.  */
1959
1960   reg_is_born (reg, 2 * this_insn_number - (GET_CODE (setter) == CLOBBER));
1961 }
1962 \f
1963 /* Handle beginning of the life of register REG.
1964    BIRTH is the index at which this is happening.  */
1965
1966 static void
1967 reg_is_born (reg, birth)
1968      rtx reg;
1969      int birth;
1970 {
1971   register int regno;
1972      
1973   if (GET_CODE (reg) == SUBREG)
1974     regno = REGNO (SUBREG_REG (reg)) + SUBREG_WORD (reg);
1975   else
1976     regno = REGNO (reg);
1977
1978   if (regno < FIRST_PSEUDO_REGISTER)
1979     {
1980       mark_life (regno, GET_MODE (reg), 1);
1981
1982       /* If the register was to have been born earlier that the present
1983          insn, mark it as live where it is actually born.  */
1984       if (birth < 2 * this_insn_number)
1985         post_mark_life (regno, GET_MODE (reg), 1, birth, 2 * this_insn_number);
1986     }
1987   else
1988     {
1989       if (reg_qty[regno] == -2)
1990         alloc_qty (regno, GET_MODE (reg), PSEUDO_REGNO_SIZE (regno), birth);
1991
1992       /* If this register has a quantity number, show that it isn't dead.  */
1993       if (reg_qty[regno] >= 0)
1994         qty_death[reg_qty[regno]] = -1;
1995     }
1996 }
1997
1998 /* Record the death of REG in the current insn.  If OUTPUT_P is non-zero,
1999    REG is an output that is dying (i.e., it is never used), otherwise it
2000    is an input (the normal case).
2001    If OUTPUT_P is 1, then we extend the life past the end of this insn.  */
2002
2003 static void
2004 wipe_dead_reg (reg, output_p)
2005      register rtx reg;
2006      int output_p;
2007 {
2008   register int regno = REGNO (reg);
2009
2010   /* If this insn has multiple results,
2011      and the dead reg is used in one of the results,
2012      extend its life to after this insn,
2013      so it won't get allocated together with any other result of this insn.  */
2014   if (GET_CODE (PATTERN (this_insn)) == PARALLEL
2015       && !single_set (this_insn))
2016     {
2017       int i;
2018       for (i = XVECLEN (PATTERN (this_insn), 0) - 1; i >= 0; i--)
2019         {
2020           rtx set = XVECEXP (PATTERN (this_insn), 0, i);
2021           if (GET_CODE (set) == SET
2022               && GET_CODE (SET_DEST (set)) != REG
2023               && !rtx_equal_p (reg, SET_DEST (set))
2024               && reg_overlap_mentioned_p (reg, SET_DEST (set)))
2025             output_p = 1;
2026         }
2027     }
2028
2029   /* If this register is used in an auto-increment address, then extend its
2030      life to after this insn, so that it won't get allocated together with
2031      the result of this insn.  */
2032   if (! output_p && find_regno_note (this_insn, REG_INC, regno))
2033     output_p = 1;
2034
2035   if (regno < FIRST_PSEUDO_REGISTER)
2036     {
2037       mark_life (regno, GET_MODE (reg), 0);
2038
2039       /* If a hard register is dying as an output, mark it as in use at
2040          the beginning of this insn (the above statement would cause this
2041          not to happen).  */
2042       if (output_p)
2043         post_mark_life (regno, GET_MODE (reg), 1,
2044                         2 * this_insn_number, 2 * this_insn_number+ 1);
2045     }
2046
2047   else if (reg_qty[regno] >= 0)
2048     qty_death[reg_qty[regno]] = 2 * this_insn_number + output_p;
2049 }
2050 \f
2051 /* Find a block of SIZE words of hard regs in reg_class CLASS
2052    that can hold something of machine-mode MODE
2053      (but actually we test only the first of the block for holding MODE)
2054    and still free between insn BORN_INDEX and insn DEAD_INDEX,
2055    and return the number of the first of them.
2056    Return -1 if such a block cannot be found. 
2057    If QTY crosses calls, insist on a register preserved by calls,
2058    unless ACCEPT_CALL_CLOBBERED is nonzero.
2059
2060    If JUST_TRY_SUGGESTED is non-zero, only try to see if the suggested
2061    register is available.  If not, return -1.  */
2062
2063 static int
2064 find_free_reg (class, mode, qty, accept_call_clobbered, just_try_suggested,
2065                born_index, dead_index)
2066      enum reg_class class;
2067      enum machine_mode mode;
2068      int qty;
2069      int accept_call_clobbered;
2070      int just_try_suggested;
2071      int born_index, dead_index;
2072 {
2073   register int i, ins;
2074 #ifdef HARD_REG_SET
2075   register              /* Declare it register if it's a scalar.  */
2076 #endif
2077     HARD_REG_SET used, first_used;
2078 #ifdef ELIMINABLE_REGS
2079   static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
2080 #endif
2081
2082   /* Validate our parameters.  */
2083   if (born_index < 0 || born_index > dead_index)
2084     abort ();
2085
2086   /* Don't let a pseudo live in a reg across a function call
2087      if we might get a nonlocal goto.  */
2088   if (current_function_has_nonlocal_label
2089       && qty_n_calls_crossed[qty] > 0)
2090     return -1;
2091
2092   if (accept_call_clobbered)
2093     COPY_HARD_REG_SET (used, call_fixed_reg_set);
2094   else if (qty_n_calls_crossed[qty] == 0)
2095     COPY_HARD_REG_SET (used, fixed_reg_set);
2096   else
2097     COPY_HARD_REG_SET (used, call_used_reg_set);
2098
2099   if (accept_call_clobbered)
2100     IOR_HARD_REG_SET (used, losing_caller_save_reg_set);
2101
2102   for (ins = born_index; ins < dead_index; ins++)
2103     IOR_HARD_REG_SET (used, regs_live_at[ins]);
2104
2105   IOR_COMPL_HARD_REG_SET (used, reg_class_contents[(int) class]);
2106
2107   /* Don't use the frame pointer reg in local-alloc even if
2108      we may omit the frame pointer, because if we do that and then we
2109      need a frame pointer, reload won't know how to move the pseudo
2110      to another hard reg.  It can move only regs made by global-alloc.
2111
2112      This is true of any register that can be eliminated.  */
2113 #ifdef ELIMINABLE_REGS
2114   for (i = 0; i < sizeof eliminables / sizeof eliminables[0]; i++)
2115     SET_HARD_REG_BIT (used, eliminables[i].from);
2116 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2117   /* If FRAME_POINTER_REGNUM is not a real register, then protect the one
2118      that it might be eliminated into.  */
2119   SET_HARD_REG_BIT (used, HARD_FRAME_POINTER_REGNUM);
2120 #endif
2121 #else
2122   SET_HARD_REG_BIT (used, FRAME_POINTER_REGNUM);
2123 #endif
2124
2125 #ifdef CLASS_CANNOT_CHANGE_SIZE
2126   if (qty_changes_size[qty])
2127     IOR_HARD_REG_SET (used,
2128                       reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE]);
2129 #endif
2130
2131   /* Normally, the registers that can be used for the first register in
2132      a multi-register quantity are the same as those that can be used for
2133      subsequent registers.  However, if just trying suggested registers,
2134      restrict our consideration to them.  If there are copy-suggested
2135      register, try them.  Otherwise, try the arithmetic-suggested
2136      registers.  */
2137   COPY_HARD_REG_SET (first_used, used);
2138
2139   if (just_try_suggested)
2140     {
2141       if (qty_phys_num_copy_sugg[qty] != 0)
2142         IOR_COMPL_HARD_REG_SET (first_used, qty_phys_copy_sugg[qty]);
2143       else
2144         IOR_COMPL_HARD_REG_SET (first_used, qty_phys_sugg[qty]);
2145     }
2146
2147   /* If all registers are excluded, we can't do anything.  */
2148   GO_IF_HARD_REG_SUBSET (reg_class_contents[(int) ALL_REGS], first_used, fail);
2149
2150   /* If at least one would be suitable, test each hard reg.  */
2151
2152   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2153     {
2154 #ifdef REG_ALLOC_ORDER
2155       int regno = reg_alloc_order[i];
2156 #else
2157       int regno = i;
2158 #endif
2159       if (! TEST_HARD_REG_BIT (first_used, regno)
2160           && HARD_REGNO_MODE_OK (regno, mode))
2161         {
2162           register int j;
2163           register int size1 = HARD_REGNO_NREGS (regno, mode);
2164           for (j = 1; j < size1 && ! TEST_HARD_REG_BIT (used, regno + j); j++);
2165           if (j == size1)
2166             {
2167               /* Mark that this register is in use between its birth and death
2168                  insns.  */
2169               post_mark_life (regno, mode, 1, born_index, dead_index);
2170               return regno;
2171             }
2172 #ifndef REG_ALLOC_ORDER
2173           i += j;               /* Skip starting points we know will lose */
2174 #endif
2175         }
2176     }
2177
2178  fail:
2179
2180   /* If we are just trying suggested register, we have just tried copy-
2181      suggested registers, and there are arithmetic-suggested registers,
2182      try them.  */
2183   
2184   /* If it would be profitable to allocate a call-clobbered register
2185      and save and restore it around calls, do that.  */
2186   if (just_try_suggested && qty_phys_num_copy_sugg[qty] != 0
2187       && qty_phys_num_sugg[qty] != 0)
2188     {
2189       /* Don't try the copy-suggested regs again.  */
2190       qty_phys_num_copy_sugg[qty] = 0;
2191       return find_free_reg (class, mode, qty, accept_call_clobbered, 1,
2192                             born_index, dead_index);
2193     }
2194
2195   /* We need not check to see if the current function has nonlocal
2196      labels because we don't put any pseudos that are live over calls in
2197      registers in that case.  */
2198
2199   if (! accept_call_clobbered
2200       && flag_caller_saves
2201       && ! just_try_suggested
2202       && qty_n_calls_crossed[qty] != 0
2203       && CALLER_SAVE_PROFITABLE (qty_n_refs[qty], qty_n_calls_crossed[qty]))
2204     {
2205       i = find_free_reg (class, mode, qty, 1, 0, born_index, dead_index);
2206       if (i >= 0)
2207         caller_save_needed = 1;
2208       return i;
2209     }
2210   return -1;
2211 }
2212 \f
2213 /* Mark that REGNO with machine-mode MODE is live starting from the current
2214    insn (if LIFE is non-zero) or dead starting at the current insn (if LIFE
2215    is zero).  */
2216
2217 static void
2218 mark_life (regno, mode, life)
2219      register int regno;
2220      enum machine_mode mode;
2221      int life;
2222 {
2223   register int j = HARD_REGNO_NREGS (regno, mode);
2224   if (life)
2225     while (--j >= 0)
2226       SET_HARD_REG_BIT (regs_live, regno + j);
2227   else
2228     while (--j >= 0)
2229       CLEAR_HARD_REG_BIT (regs_live, regno + j);
2230 }
2231
2232 /* Mark register number REGNO (with machine-mode MODE) as live (if LIFE
2233    is non-zero) or dead (if LIFE is zero) from insn number BIRTH (inclusive)
2234    to insn number DEATH (exclusive).  */
2235
2236 static void
2237 post_mark_life (regno, mode, life, birth, death)
2238      int regno;
2239      enum machine_mode mode;
2240      int life, birth, death;
2241 {
2242   register int j = HARD_REGNO_NREGS (regno, mode);
2243 #ifdef HARD_REG_SET
2244   register              /* Declare it register if it's a scalar.  */
2245 #endif
2246     HARD_REG_SET this_reg;
2247
2248   CLEAR_HARD_REG_SET (this_reg);
2249   while (--j >= 0)
2250     SET_HARD_REG_BIT (this_reg, regno + j);
2251
2252   if (life)
2253     while (birth < death)
2254       {
2255         IOR_HARD_REG_SET (regs_live_at[birth], this_reg);
2256         birth++;
2257       }
2258   else
2259     while (birth < death)
2260       {
2261         AND_COMPL_HARD_REG_SET (regs_live_at[birth], this_reg);
2262         birth++;
2263       }
2264 }
2265 \f
2266 /* INSN is the CLOBBER insn that starts a REG_NO_NOCONFLICT block, R0
2267    is the register being clobbered, and R1 is a register being used in
2268    the equivalent expression.
2269
2270    If R1 dies in the block and has a REG_NO_CONFLICT note on every insn
2271    in which it is used, return 1.
2272
2273    Otherwise, return 0.  */
2274
2275 static int
2276 no_conflict_p (insn, r0, r1)
2277      rtx insn, r0, r1;
2278 {
2279   int ok = 0;
2280   rtx note = find_reg_note (insn, REG_LIBCALL, NULL_RTX);
2281   rtx p, last;
2282
2283   /* If R1 is a hard register, return 0 since we handle this case
2284      when we scan the insns that actually use it.  */
2285
2286   if (note == 0
2287       || (GET_CODE (r1) == REG && REGNO (r1) < FIRST_PSEUDO_REGISTER)
2288       || (GET_CODE (r1) == SUBREG && GET_CODE (SUBREG_REG (r1)) == REG
2289           && REGNO (SUBREG_REG (r1)) < FIRST_PSEUDO_REGISTER))
2290     return 0;
2291
2292   last = XEXP (note, 0);
2293
2294   for (p = NEXT_INSN (insn); p && p != last; p = NEXT_INSN (p))
2295     if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
2296       {
2297         if (find_reg_note (p, REG_DEAD, r1))
2298           ok = 1;
2299
2300         if (reg_mentioned_p (r1, PATTERN (p))
2301             && ! find_reg_note (p, REG_NO_CONFLICT, r1))
2302           return 0;
2303       }
2304       
2305   return ok;
2306 }
2307 \f
2308 #ifdef REGISTER_CONSTRAINTS
2309
2310 /* Return the number of alternatives for which the constraint string P
2311    indicates that the operand must be equal to operand 0 and that no register
2312    is acceptable.  */
2313
2314 static int
2315 requires_inout (p)
2316      char *p;
2317 {
2318   char c;
2319   int found_zero = 0;
2320   int reg_allowed = 0;
2321   int num_matching_alts = 0;
2322
2323   while (c = *p++)
2324     switch (c)
2325       {
2326       case '=':  case '+':  case '?':
2327       case '#':  case '&':  case '!':
2328       case '*':  case '%':
2329       case '1':  case '2':  case '3':  case '4':
2330       case 'm':  case '<':  case '>':  case 'V':  case 'o':
2331       case 'E':  case 'F':  case 'G':  case 'H':
2332       case 's':  case 'i':  case 'n':
2333       case 'I':  case 'J':  case 'K':  case 'L':
2334       case 'M':  case 'N':  case 'O':  case 'P':
2335 #ifdef EXTRA_CONSTRAINT
2336       case 'Q':  case 'R':  case 'S':  case 'T':  case 'U':
2337 #endif
2338       case 'X':
2339         /* These don't say anything we care about.  */
2340         break;
2341
2342       case ',':
2343         if (found_zero && ! reg_allowed)
2344           num_matching_alts++;
2345
2346         found_zero = reg_allowed = 0;
2347         break;
2348
2349       case '0':
2350         found_zero = 1;
2351         break;
2352
2353       case 'p':
2354       case 'g': case 'r':
2355       default:
2356         reg_allowed = 1;
2357         break;
2358       }
2359
2360   if (found_zero && ! reg_allowed)
2361     num_matching_alts++;
2362
2363   return num_matching_alts;
2364 }
2365 #endif /* REGISTER_CONSTRAINTS */
2366 \f
2367 void
2368 dump_local_alloc (file)
2369      FILE *file;
2370 {
2371   register int i;
2372   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
2373     if (reg_renumber[i] != -1)
2374       fprintf (file, ";; Register %d in %d.\n", i, reg_renumber[i]);
2375 }