OSDN Git Service

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