OSDN Git Service

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