OSDN Git Service

Major cutover to using system.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 "recog.h"
71 #include "output.h"
72 \f
73 /* Next quantity number available for allocation.  */
74
75 static int next_qty;
76
77 /* In all the following vectors indexed by quantity number.  */
78
79 /* Element Q is the hard reg number chosen for quantity Q,
80    or -1 if none was found.  */
81
82 static short *qty_phys_reg;
83
84 /* We maintain two hard register sets that indicate suggested hard registers
85    for each quantity.  The first, qty_phys_copy_sugg, contains hard registers
86    that are tied to the quantity by a simple copy.  The second contains all
87    hard registers that are tied to the quantity via an arithmetic operation.
88
89    The former register set is given priority for allocation.  This tends to
90    eliminate copy insns.  */
91
92 /* Element Q is a set of hard registers that are suggested for quantity Q by
93    copy insns.  */
94
95 static HARD_REG_SET *qty_phys_copy_sugg;
96
97 /* Element Q is a set of hard registers that are suggested for quantity Q by
98    arithmetic insns.  */
99
100 static HARD_REG_SET *qty_phys_sugg;
101
102 /* Element Q is the number of suggested registers in qty_phys_copy_sugg.  */
103
104 static short *qty_phys_num_copy_sugg;
105
106 /* Element Q is the number of suggested registers in qty_phys_sugg.  */
107
108 static short *qty_phys_num_sugg;
109
110 /* Element Q is the number of refs to quantity Q.  */
111
112 static int *qty_n_refs;
113
114 /* Element Q is a reg class contained in (smaller than) the
115    preferred classes of all the pseudo regs that are tied in quantity Q.
116    This is the preferred class for allocating that quantity.  */
117
118 static enum reg_class *qty_min_class;
119
120 /* Insn number (counting from head of basic block)
121    where quantity Q was born.  -1 if birth has not been recorded.  */
122
123 static int *qty_birth;
124
125 /* Insn number (counting from head of basic block)
126    where quantity Q died.  Due to the way tying is done,
127    and the fact that we consider in this pass only regs that die but once,
128    a quantity can die only once.  Each quantity's life span
129    is a set of consecutive insns.  -1 if death has not been recorded.  */
130
131 static int *qty_death;
132
133 /* Number of words needed to hold the data in quantity Q.
134    This depends on its machine mode.  It is used for these purposes:
135    1. It is used in computing the relative importances of qtys,
136       which determines the order in which we look for regs for them.
137    2. It is used in rules that prevent tying several registers of
138       different sizes in a way that is geometrically impossible
139       (see combine_regs).  */
140
141 static int *qty_size;
142
143 /* This holds the mode of the registers that are tied to qty Q,
144    or VOIDmode if registers with differing modes are tied together.  */
145
146 static enum machine_mode *qty_mode;
147
148 /* Number of times a reg tied to qty Q lives across a CALL_INSN.  */
149
150 static int *qty_n_calls_crossed;
151
152 /* Register class within which we allocate qty Q if we can't get
153    its preferred class.  */
154
155 static enum reg_class *qty_alternate_class;
156
157 /* Element Q is the SCRATCH expression for which this quantity is being
158    allocated or 0 if this quantity is allocating registers.  */
159
160 static rtx *qty_scratch_rtx;
161
162 /* Element Q is nonzero if this quantity has been used in a SUBREG
163    that changes its size.  */
164
165 static char *qty_changes_size;
166
167 /* Element Q is the register number of one pseudo register whose
168    reg_qty value is Q, or -1 is this quantity is for a SCRATCH.  This
169    register should be the head of the chain maintained in reg_next_in_qty.  */
170
171 static int *qty_first_reg;
172
173 /* If (REG N) has been assigned a quantity number, is a register number
174    of another register assigned the same quantity number, or -1 for the
175    end of the chain.  qty_first_reg point to the head of this chain.  */
176
177 static int *reg_next_in_qty;
178
179 /* reg_qty[N] (where N is a pseudo reg number) is the qty number of that reg
180    if it is >= 0,
181    of -1 if this register cannot be allocated by local-alloc,
182    or -2 if not known yet.
183
184    Note that if we see a use or death of pseudo register N with
185    reg_qty[N] == -2, register N must be local to the current block.  If
186    it were used in more than one block, we would have reg_qty[N] == -1.
187    This relies on the fact that if reg_basic_block[N] is >= 0, register N
188    will not appear in any other block.  We save a considerable number of
189    tests by exploiting this.
190
191    If N is < FIRST_PSEUDO_REGISTER, reg_qty[N] is undefined and should not
192    be referenced.  */
193
194 static int *reg_qty;
195
196 /* The offset (in words) of register N within its quantity.
197    This can be nonzero if register N is SImode, and has been tied
198    to a subreg of a DImode register.  */
199
200 static char *reg_offset;
201
202 /* Vector of substitutions of register numbers,
203    used to map pseudo regs into hardware regs.
204    This is set up as a result of register allocation.
205    Element N is the hard reg assigned to pseudo reg N,
206    or is -1 if no hard reg was assigned.
207    If N is a hard reg number, element N is N.  */
208
209 short *reg_renumber;
210
211 /* Set of hard registers live at the current point in the scan
212    of the instructions in a basic block.  */
213
214 static HARD_REG_SET regs_live;
215
216 /* Each set of hard registers indicates registers live at a particular
217    point in the basic block.  For N even, regs_live_at[N] says which
218    hard registers are needed *after* insn N/2 (i.e., they may not
219    conflict with the outputs of insn N/2 or the inputs of insn N/2 + 1.
220
221    If an object is to conflict with the inputs of insn J but not the
222    outputs of insn J + 1, we say it is born at index J*2 - 1.  Similarly,
223    if it is to conflict with the outputs of insn J but not the inputs of
224    insn J + 1, it is said to die at index J*2 + 1.  */
225
226 static HARD_REG_SET *regs_live_at;
227
228 int *scratch_block;
229 rtx *scratch_list;
230 int scratch_list_length;
231 static int scratch_index;
232
233 /* Communicate local vars `insn_number' and `insn'
234    from `block_alloc' to `reg_is_set', `wipe_dead_reg', and `alloc_qty'.  */
235 static int this_insn_number;
236 static rtx this_insn;
237
238 /* Used to communicate changes made by update_equiv_regs to
239    memref_referenced_p.  reg_equiv_replacement is set for any REG_EQUIV note
240    found or created, so that we can keep track of what memory accesses might
241    be created later, e.g. by reload.  */
242
243 static rtx *reg_equiv_replacement;
244
245 static void alloc_qty           PROTO((int, enum machine_mode, int, int));
246 static void alloc_qty_for_scratch PROTO((rtx, int, rtx, int, int));
247 static void validate_equiv_mem_from_store PROTO((rtx, rtx));
248 static int validate_equiv_mem   PROTO((rtx, rtx, rtx));
249 static int contains_replace_regs PROTO((rtx, char *));
250 static int memref_referenced_p  PROTO((rtx, rtx));
251 static int memref_used_between_p PROTO((rtx, rtx, rtx));
252 static void update_equiv_regs   PROTO((void));
253 static void block_alloc         PROTO((int));
254 static int qty_sugg_compare     PROTO((int, int));
255 static int qty_sugg_compare_1   PROTO((const GENERIC_PTR, const GENERIC_PTR));
256 static int qty_compare          PROTO((int, int));
257 static int qty_compare_1        PROTO((const GENERIC_PTR, const GENERIC_PTR));
258 static int combine_regs         PROTO((rtx, rtx, int, int, rtx, int));
259 static int reg_meets_class_p    PROTO((int, enum reg_class));
260 static int reg_classes_overlap_p PROTO((enum reg_class, enum reg_class,
261                                         int));
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           if (N_REG_CLASSES > 1)
1420             {
1421               qty_phys_reg[q] = find_free_reg (qty_min_class[q], 
1422                                                qty_mode[q], q, 0, 0,
1423                                                qty_birth[q], qty_death[q]);
1424               if (qty_phys_reg[q] >= 0)
1425                 continue;
1426             }
1427
1428           if (qty_alternate_class[q] != NO_REGS)
1429             qty_phys_reg[q] = find_free_reg (qty_alternate_class[q],
1430                                              qty_mode[q], q, 0, 0,
1431                                              qty_birth[q], qty_death[q]);
1432         }
1433     }
1434
1435   /* Now propagate the register assignments
1436      to the pseudo regs belonging to the qtys.  */
1437
1438   for (q = 0; q < next_qty; q++)
1439     if (qty_phys_reg[q] >= 0)
1440       {
1441         for (i = qty_first_reg[q]; i >= 0; i = reg_next_in_qty[i])
1442           reg_renumber[i] = qty_phys_reg[q] + reg_offset[i];
1443         if (qty_scratch_rtx[q])
1444           {
1445             if (GET_CODE (qty_scratch_rtx[q]) == REG)
1446               abort ();
1447             qty_scratch_rtx[q] = gen_rtx_REG (GET_MODE (qty_scratch_rtx[q]),
1448                                               qty_phys_reg[q]);
1449             scratch_block[scratch_index] = b;
1450             scratch_list[scratch_index++] = qty_scratch_rtx[q];
1451
1452           }
1453       }
1454 }
1455 \f
1456 /* Compare two quantities' priority for getting real registers.
1457    We give shorter-lived quantities higher priority.
1458    Quantities with more references are also preferred, as are quantities that
1459    require multiple registers.  This is the identical prioritization as
1460    done by global-alloc.
1461
1462    We used to give preference to registers with *longer* lives, but using
1463    the same algorithm in both local- and global-alloc can speed up execution
1464    of some programs by as much as a factor of three!  */
1465
1466 /* Note that the quotient will never be bigger than
1467    the value of floor_log2 times the maximum number of
1468    times a register can occur in one insn (surely less than 100).
1469    Multiplying this by 10000 can't overflow.
1470    QTY_CMP_PRI is also used by qty_sugg_compare.  */
1471
1472 #define QTY_CMP_PRI(q)          \
1473   ((int) (((double) (floor_log2 (qty_n_refs[q]) * qty_n_refs[q] * qty_size[q]) \
1474           / (qty_death[q] - qty_birth[q])) * 10000))
1475
1476 static int
1477 qty_compare (q1, q2)
1478      int q1, q2;
1479 {
1480   return QTY_CMP_PRI (q2) - QTY_CMP_PRI (q1);
1481 }
1482
1483 static int
1484 qty_compare_1 (q1p, q2p)
1485      const GENERIC_PTR q1p;
1486      const GENERIC_PTR q2p;
1487 {
1488   register int q1 = *(int *)q1p, q2 = *(int *)q2p;
1489   register int tem = QTY_CMP_PRI (q2) - QTY_CMP_PRI (q1);
1490
1491   if (tem != 0)
1492     return tem;
1493
1494   /* If qtys are equally good, sort by qty number,
1495      so that the results of qsort leave nothing to chance.  */
1496   return q1 - q2;
1497 }
1498 \f
1499 /* Compare two quantities' priority for getting real registers.  This version
1500    is called for quantities that have suggested hard registers.  First priority
1501    goes to quantities that have copy preferences, then to those that have
1502    normal preferences.  Within those groups, quantities with the lower
1503    number of preferences have the highest priority.  Of those, we use the same
1504    algorithm as above.  */
1505
1506 #define QTY_CMP_SUGG(q)         \
1507   (qty_phys_num_copy_sugg[q]            \
1508     ? qty_phys_num_copy_sugg[q] \
1509     : qty_phys_num_sugg[q] * FIRST_PSEUDO_REGISTER)
1510
1511 static int
1512 qty_sugg_compare (q1, q2)
1513      int q1, q2;
1514 {
1515   register int tem = QTY_CMP_SUGG (q1) - QTY_CMP_SUGG (q2);
1516
1517   if (tem != 0)
1518     return tem;
1519   
1520   return QTY_CMP_PRI (q2) - QTY_CMP_PRI (q1);
1521 }
1522
1523 static int
1524 qty_sugg_compare_1 (q1p, q2p)
1525      const GENERIC_PTR q1p;
1526      const GENERIC_PTR q2p;
1527 {
1528   register int q1 = *(int *)q1p, q2 = *(int *)q2p;
1529   register int tem = QTY_CMP_SUGG (q1) - QTY_CMP_SUGG (q2);
1530
1531   if (tem != 0)
1532     return tem;
1533
1534   tem = QTY_CMP_PRI (q2) - QTY_CMP_PRI (q1);
1535   if (tem != 0)
1536     return tem;
1537
1538   /* If qtys are equally good, sort by qty number,
1539      so that the results of qsort leave nothing to chance.  */
1540   return q1 - q2;
1541 }
1542
1543 #undef QTY_CMP_SUGG
1544 #undef QTY_CMP_PRI
1545 \f
1546 /* Attempt to combine the two registers (rtx's) USEDREG and SETREG.
1547    Returns 1 if have done so, or 0 if cannot.
1548
1549    Combining registers means marking them as having the same quantity
1550    and adjusting the offsets within the quantity if either of
1551    them is a SUBREG).
1552
1553    We don't actually combine a hard reg with a pseudo; instead
1554    we just record the hard reg as the suggestion for the pseudo's quantity.
1555    If we really combined them, we could lose if the pseudo lives
1556    across an insn that clobbers the hard reg (eg, movstr).
1557
1558    ALREADY_DEAD is non-zero if USEDREG is known to be dead even though
1559    there is no REG_DEAD note on INSN.  This occurs during the processing
1560    of REG_NO_CONFLICT blocks.
1561
1562    MAY_SAVE_COPYCOPY is non-zero if this insn is simply copying USEDREG to
1563    SETREG or if the input and output must share a register.
1564    In that case, we record a hard reg suggestion in QTY_PHYS_COPY_SUGG.
1565    
1566    There are elaborate checks for the validity of combining.  */
1567
1568    
1569 static int
1570 combine_regs (usedreg, setreg, may_save_copy, insn_number, insn, already_dead)
1571      rtx usedreg, setreg;
1572      int may_save_copy;
1573      int insn_number;
1574      rtx insn;
1575      int already_dead;
1576 {
1577   register int ureg, sreg;
1578   register int offset = 0;
1579   int usize, ssize;
1580   register int sqty;
1581
1582   /* Determine the numbers and sizes of registers being used.  If a subreg
1583      is present that does not change the entire register, don't consider
1584      this a copy insn.  */
1585
1586   while (GET_CODE (usedreg) == SUBREG)
1587     {
1588       if (GET_MODE_SIZE (GET_MODE (SUBREG_REG (usedreg))) > UNITS_PER_WORD)
1589         may_save_copy = 0;
1590       offset += SUBREG_WORD (usedreg);
1591       usedreg = SUBREG_REG (usedreg);
1592     }
1593   if (GET_CODE (usedreg) != REG)
1594     return 0;
1595   ureg = REGNO (usedreg);
1596   usize = REG_SIZE (usedreg);
1597
1598   while (GET_CODE (setreg) == SUBREG)
1599     {
1600       if (GET_MODE_SIZE (GET_MODE (SUBREG_REG (setreg))) > UNITS_PER_WORD)
1601         may_save_copy = 0;
1602       offset -= SUBREG_WORD (setreg);
1603       setreg = SUBREG_REG (setreg);
1604     }
1605   if (GET_CODE (setreg) != REG)
1606     return 0;
1607   sreg = REGNO (setreg);
1608   ssize = REG_SIZE (setreg);
1609
1610   /* If UREG is a pseudo-register that hasn't already been assigned a
1611      quantity number, it means that it is not local to this block or dies
1612      more than once.  In either event, we can't do anything with it.  */
1613   if ((ureg >= FIRST_PSEUDO_REGISTER && reg_qty[ureg] < 0)
1614       /* Do not combine registers unless one fits within the other.  */
1615       || (offset > 0 && usize + offset > ssize)
1616       || (offset < 0 && usize + offset < ssize)
1617       /* Do not combine with a smaller already-assigned object
1618          if that smaller object is already combined with something bigger.  */
1619       || (ssize > usize && ureg >= FIRST_PSEUDO_REGISTER
1620           && usize < qty_size[reg_qty[ureg]])
1621       /* Can't combine if SREG is not a register we can allocate.  */
1622       || (sreg >= FIRST_PSEUDO_REGISTER && reg_qty[sreg] == -1)
1623       /* Don't combine with a pseudo mentioned in a REG_NO_CONFLICT note.
1624          These have already been taken care of.  This probably wouldn't
1625          combine anyway, but don't take any chances.  */
1626       || (ureg >= FIRST_PSEUDO_REGISTER
1627           && find_reg_note (insn, REG_NO_CONFLICT, usedreg))
1628       /* Don't tie something to itself.  In most cases it would make no
1629          difference, but it would screw up if the reg being tied to itself
1630          also dies in this insn.  */
1631       || ureg == sreg
1632       /* Don't try to connect two different hardware registers.  */
1633       || (ureg < FIRST_PSEUDO_REGISTER && sreg < FIRST_PSEUDO_REGISTER)
1634       /* Don't connect two different machine modes if they have different
1635          implications as to which registers may be used.  */
1636       || !MODES_TIEABLE_P (GET_MODE (usedreg), GET_MODE (setreg)))
1637     return 0;
1638
1639   /* Now, if UREG is a hard reg and SREG is a pseudo, record the hard reg in
1640      qty_phys_sugg for the pseudo instead of tying them.
1641
1642      Return "failure" so that the lifespan of UREG is terminated here;
1643      that way the two lifespans will be disjoint and nothing will prevent
1644      the pseudo reg from being given this hard reg.  */
1645
1646   if (ureg < FIRST_PSEUDO_REGISTER)
1647     {
1648       /* Allocate a quantity number so we have a place to put our
1649          suggestions.  */
1650       if (reg_qty[sreg] == -2)
1651         reg_is_born (setreg, 2 * insn_number);
1652
1653       if (reg_qty[sreg] >= 0)
1654         {
1655           if (may_save_copy
1656               && ! TEST_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[sreg]], ureg))
1657             {
1658               SET_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[sreg]], ureg);
1659               qty_phys_num_copy_sugg[reg_qty[sreg]]++;
1660             }
1661           else if (! TEST_HARD_REG_BIT (qty_phys_sugg[reg_qty[sreg]], ureg))
1662             {
1663               SET_HARD_REG_BIT (qty_phys_sugg[reg_qty[sreg]], ureg);
1664               qty_phys_num_sugg[reg_qty[sreg]]++;
1665             }
1666         }
1667       return 0;
1668     }
1669
1670   /* Similarly for SREG a hard register and UREG a pseudo register.  */
1671
1672   if (sreg < FIRST_PSEUDO_REGISTER)
1673     {
1674       if (may_save_copy
1675           && ! TEST_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[ureg]], sreg))
1676         {
1677           SET_HARD_REG_BIT (qty_phys_copy_sugg[reg_qty[ureg]], sreg);
1678           qty_phys_num_copy_sugg[reg_qty[ureg]]++;
1679         }
1680       else if (! TEST_HARD_REG_BIT (qty_phys_sugg[reg_qty[ureg]], sreg))
1681         {
1682           SET_HARD_REG_BIT (qty_phys_sugg[reg_qty[ureg]], sreg);
1683           qty_phys_num_sugg[reg_qty[ureg]]++;
1684         }
1685       return 0;
1686     }
1687
1688   /* At this point we know that SREG and UREG are both pseudos.
1689      Do nothing if SREG already has a quantity or is a register that we
1690      don't allocate.  */
1691   if (reg_qty[sreg] >= -1
1692       /* If we are not going to let any regs live across calls,
1693          don't tie a call-crossing reg to a non-call-crossing reg.  */
1694       || (current_function_has_nonlocal_label
1695           && ((REG_N_CALLS_CROSSED (ureg) > 0)
1696               != (REG_N_CALLS_CROSSED (sreg) > 0))))
1697     return 0;
1698
1699   /* We don't already know about SREG, so tie it to UREG
1700      if this is the last use of UREG, provided the classes they want
1701      are compatible.  */
1702
1703   if ((already_dead || find_regno_note (insn, REG_DEAD, ureg))
1704       && reg_meets_class_p (sreg, qty_min_class[reg_qty[ureg]]))
1705     {
1706       /* Add SREG to UREG's quantity.  */
1707       sqty = reg_qty[ureg];
1708       reg_qty[sreg] = sqty;
1709       reg_offset[sreg] = reg_offset[ureg] + offset;
1710       reg_next_in_qty[sreg] = qty_first_reg[sqty];
1711       qty_first_reg[sqty] = sreg;
1712
1713       /* If SREG's reg class is smaller, set qty_min_class[SQTY].  */
1714       update_qty_class (sqty, sreg);
1715
1716       /* Update info about quantity SQTY.  */
1717       qty_n_calls_crossed[sqty] += REG_N_CALLS_CROSSED (sreg);
1718       qty_n_refs[sqty] += REG_N_REFS (sreg);
1719       if (usize < ssize)
1720         {
1721           register int i;
1722
1723           for (i = qty_first_reg[sqty]; i >= 0; i = reg_next_in_qty[i])
1724             reg_offset[i] -= offset;
1725
1726           qty_size[sqty] = ssize;
1727           qty_mode[sqty] = GET_MODE (setreg);
1728         }
1729     }
1730   else
1731     return 0;
1732
1733   return 1;
1734 }
1735 \f
1736 /* Return 1 if the preferred class of REG allows it to be tied
1737    to a quantity or register whose class is CLASS.
1738    True if REG's reg class either contains or is contained in CLASS.  */
1739
1740 static int
1741 reg_meets_class_p (reg, class)
1742      int reg;
1743      enum reg_class class;
1744 {
1745   register enum reg_class rclass = reg_preferred_class (reg);
1746   return (reg_class_subset_p (rclass, class)
1747           || reg_class_subset_p (class, rclass));
1748 }
1749
1750 /* Return 1 if the two specified classes have registers in common.
1751    If CALL_SAVED, then consider only call-saved registers.  */
1752
1753 static int
1754 reg_classes_overlap_p (c1, c2, call_saved)
1755      register enum reg_class c1;
1756      register enum reg_class c2;
1757      int call_saved;
1758 {
1759   HARD_REG_SET c;
1760   int i;
1761
1762   COPY_HARD_REG_SET (c, reg_class_contents[(int) c1]);
1763   AND_HARD_REG_SET (c, reg_class_contents[(int) c2]);
1764
1765   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1766     if (TEST_HARD_REG_BIT (c, i)
1767         && (! call_saved || ! call_used_regs[i]))
1768       return 1;
1769
1770   return 0;
1771 }
1772
1773 /* Update the class of QTY assuming that REG is being tied to it.  */
1774
1775 static void
1776 update_qty_class (qty, reg)
1777      int qty;
1778      int reg;
1779 {
1780   enum reg_class rclass = reg_preferred_class (reg);
1781   if (reg_class_subset_p (rclass, qty_min_class[qty]))
1782     qty_min_class[qty] = rclass;
1783
1784   rclass = reg_alternate_class (reg);
1785   if (reg_class_subset_p (rclass, qty_alternate_class[qty]))
1786     qty_alternate_class[qty] = rclass;
1787
1788   if (REG_CHANGES_SIZE (reg))
1789     qty_changes_size[qty] = 1;
1790 }
1791 \f
1792 /* Handle something which alters the value of an rtx REG.
1793
1794    REG is whatever is set or clobbered.  SETTER is the rtx that
1795    is modifying the register.
1796
1797    If it is not really a register, we do nothing.
1798    The file-global variables `this_insn' and `this_insn_number'
1799    carry info from `block_alloc'.  */
1800
1801 static void
1802 reg_is_set (reg, setter)
1803      rtx reg;
1804      rtx setter;
1805 {
1806   /* Note that note_stores will only pass us a SUBREG if it is a SUBREG of
1807      a hard register.  These may actually not exist any more.  */
1808
1809   if (GET_CODE (reg) != SUBREG
1810       && GET_CODE (reg) != REG)
1811     return;
1812
1813   /* Mark this register as being born.  If it is used in a CLOBBER, mark
1814      it as being born halfway between the previous insn and this insn so that
1815      it conflicts with our inputs but not the outputs of the previous insn.  */
1816
1817   reg_is_born (reg, 2 * this_insn_number - (GET_CODE (setter) == CLOBBER));
1818 }
1819 \f
1820 /* Handle beginning of the life of register REG.
1821    BIRTH is the index at which this is happening.  */
1822
1823 static void
1824 reg_is_born (reg, birth)
1825      rtx reg;
1826      int birth;
1827 {
1828   register int regno;
1829      
1830   if (GET_CODE (reg) == SUBREG)
1831     regno = REGNO (SUBREG_REG (reg)) + SUBREG_WORD (reg);
1832   else
1833     regno = REGNO (reg);
1834
1835   if (regno < FIRST_PSEUDO_REGISTER)
1836     {
1837       mark_life (regno, GET_MODE (reg), 1);
1838
1839       /* If the register was to have been born earlier that the present
1840          insn, mark it as live where it is actually born.  */
1841       if (birth < 2 * this_insn_number)
1842         post_mark_life (regno, GET_MODE (reg), 1, birth, 2 * this_insn_number);
1843     }
1844   else
1845     {
1846       if (reg_qty[regno] == -2)
1847         alloc_qty (regno, GET_MODE (reg), PSEUDO_REGNO_SIZE (regno), birth);
1848
1849       /* If this register has a quantity number, show that it isn't dead.  */
1850       if (reg_qty[regno] >= 0)
1851         qty_death[reg_qty[regno]] = -1;
1852     }
1853 }
1854
1855 /* Record the death of REG in the current insn.  If OUTPUT_P is non-zero,
1856    REG is an output that is dying (i.e., it is never used), otherwise it
1857    is an input (the normal case).
1858    If OUTPUT_P is 1, then we extend the life past the end of this insn.  */
1859
1860 static void
1861 wipe_dead_reg (reg, output_p)
1862      register rtx reg;
1863      int output_p;
1864 {
1865   register int regno = REGNO (reg);
1866
1867   /* If this insn has multiple results,
1868      and the dead reg is used in one of the results,
1869      extend its life to after this insn,
1870      so it won't get allocated together with any other result of this insn.  */
1871   if (GET_CODE (PATTERN (this_insn)) == PARALLEL
1872       && !single_set (this_insn))
1873     {
1874       int i;
1875       for (i = XVECLEN (PATTERN (this_insn), 0) - 1; i >= 0; i--)
1876         {
1877           rtx set = XVECEXP (PATTERN (this_insn), 0, i);
1878           if (GET_CODE (set) == SET
1879               && GET_CODE (SET_DEST (set)) != REG
1880               && !rtx_equal_p (reg, SET_DEST (set))
1881               && reg_overlap_mentioned_p (reg, SET_DEST (set)))
1882             output_p = 1;
1883         }
1884     }
1885
1886   /* If this register is used in an auto-increment address, then extend its
1887      life to after this insn, so that it won't get allocated together with
1888      the result of this insn.  */
1889   if (! output_p && find_regno_note (this_insn, REG_INC, regno))
1890     output_p = 1;
1891
1892   if (regno < FIRST_PSEUDO_REGISTER)
1893     {
1894       mark_life (regno, GET_MODE (reg), 0);
1895
1896       /* If a hard register is dying as an output, mark it as in use at
1897          the beginning of this insn (the above statement would cause this
1898          not to happen).  */
1899       if (output_p)
1900         post_mark_life (regno, GET_MODE (reg), 1,
1901                         2 * this_insn_number, 2 * this_insn_number+ 1);
1902     }
1903
1904   else if (reg_qty[regno] >= 0)
1905     qty_death[reg_qty[regno]] = 2 * this_insn_number + output_p;
1906 }
1907 \f
1908 /* Find a block of SIZE words of hard regs in reg_class CLASS
1909    that can hold something of machine-mode MODE
1910      (but actually we test only the first of the block for holding MODE)
1911    and still free between insn BORN_INDEX and insn DEAD_INDEX,
1912    and return the number of the first of them.
1913    Return -1 if such a block cannot be found. 
1914    If QTY crosses calls, insist on a register preserved by calls,
1915    unless ACCEPT_CALL_CLOBBERED is nonzero.
1916
1917    If JUST_TRY_SUGGESTED is non-zero, only try to see if the suggested
1918    register is available.  If not, return -1.  */
1919
1920 static int
1921 find_free_reg (class, mode, qty, accept_call_clobbered, just_try_suggested,
1922                born_index, dead_index)
1923      enum reg_class class;
1924      enum machine_mode mode;
1925      int qty;
1926      int accept_call_clobbered;
1927      int just_try_suggested;
1928      int born_index, dead_index;
1929 {
1930   register int i, ins;
1931 #ifdef HARD_REG_SET
1932   register              /* Declare it register if it's a scalar.  */
1933 #endif
1934     HARD_REG_SET used, first_used;
1935 #ifdef ELIMINABLE_REGS
1936   static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
1937 #endif
1938
1939   /* Validate our parameters.  */
1940   if (born_index < 0 || born_index > dead_index)
1941     abort ();
1942
1943   /* Don't let a pseudo live in a reg across a function call
1944      if we might get a nonlocal goto.  */
1945   if (current_function_has_nonlocal_label
1946       && qty_n_calls_crossed[qty] > 0)
1947     return -1;
1948
1949   if (accept_call_clobbered)
1950     COPY_HARD_REG_SET (used, call_fixed_reg_set);
1951   else if (qty_n_calls_crossed[qty] == 0)
1952     COPY_HARD_REG_SET (used, fixed_reg_set);
1953   else
1954     COPY_HARD_REG_SET (used, call_used_reg_set);
1955
1956   if (accept_call_clobbered)
1957     IOR_HARD_REG_SET (used, losing_caller_save_reg_set);
1958
1959   for (ins = born_index; ins < dead_index; ins++)
1960     IOR_HARD_REG_SET (used, regs_live_at[ins]);
1961
1962   IOR_COMPL_HARD_REG_SET (used, reg_class_contents[(int) class]);
1963
1964   /* Don't use the frame pointer reg in local-alloc even if
1965      we may omit the frame pointer, because if we do that and then we
1966      need a frame pointer, reload won't know how to move the pseudo
1967      to another hard reg.  It can move only regs made by global-alloc.
1968
1969      This is true of any register that can be eliminated.  */
1970 #ifdef ELIMINABLE_REGS
1971   for (i = 0; i < sizeof eliminables / sizeof eliminables[0]; i++)
1972     SET_HARD_REG_BIT (used, eliminables[i].from);
1973 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
1974   /* If FRAME_POINTER_REGNUM is not a real register, then protect the one
1975      that it might be eliminated into.  */
1976   SET_HARD_REG_BIT (used, HARD_FRAME_POINTER_REGNUM);
1977 #endif
1978 #else
1979   SET_HARD_REG_BIT (used, FRAME_POINTER_REGNUM);
1980 #endif
1981
1982 #ifdef CLASS_CANNOT_CHANGE_SIZE
1983   if (qty_changes_size[qty])
1984     IOR_HARD_REG_SET (used,
1985                       reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE]);
1986 #endif
1987
1988   /* Normally, the registers that can be used for the first register in
1989      a multi-register quantity are the same as those that can be used for
1990      subsequent registers.  However, if just trying suggested registers,
1991      restrict our consideration to them.  If there are copy-suggested
1992      register, try them.  Otherwise, try the arithmetic-suggested
1993      registers.  */
1994   COPY_HARD_REG_SET (first_used, used);
1995
1996   if (just_try_suggested)
1997     {
1998       if (qty_phys_num_copy_sugg[qty] != 0)
1999         IOR_COMPL_HARD_REG_SET (first_used, qty_phys_copy_sugg[qty]);
2000       else
2001         IOR_COMPL_HARD_REG_SET (first_used, qty_phys_sugg[qty]);
2002     }
2003
2004   /* If all registers are excluded, we can't do anything.  */
2005   GO_IF_HARD_REG_SUBSET (reg_class_contents[(int) ALL_REGS], first_used, fail);
2006
2007   /* If at least one would be suitable, test each hard reg.  */
2008
2009   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2010     {
2011 #ifdef REG_ALLOC_ORDER
2012       int regno = reg_alloc_order[i];
2013 #else
2014       int regno = i;
2015 #endif
2016       if (! TEST_HARD_REG_BIT (first_used, regno)
2017           && HARD_REGNO_MODE_OK (regno, mode))
2018         {
2019           register int j;
2020           register int size1 = HARD_REGNO_NREGS (regno, mode);
2021           for (j = 1; j < size1 && ! TEST_HARD_REG_BIT (used, regno + j); j++);
2022           if (j == size1)
2023             {
2024               /* Mark that this register is in use between its birth and death
2025                  insns.  */
2026               post_mark_life (regno, mode, 1, born_index, dead_index);
2027               return regno;
2028             }
2029 #ifndef REG_ALLOC_ORDER
2030           i += j;               /* Skip starting points we know will lose */
2031 #endif
2032         }
2033     }
2034
2035  fail:
2036
2037   /* If we are just trying suggested register, we have just tried copy-
2038      suggested registers, and there are arithmetic-suggested registers,
2039      try them.  */
2040   
2041   /* If it would be profitable to allocate a call-clobbered register
2042      and save and restore it around calls, do that.  */
2043   if (just_try_suggested && qty_phys_num_copy_sugg[qty] != 0
2044       && qty_phys_num_sugg[qty] != 0)
2045     {
2046       /* Don't try the copy-suggested regs again.  */
2047       qty_phys_num_copy_sugg[qty] = 0;
2048       return find_free_reg (class, mode, qty, accept_call_clobbered, 1,
2049                             born_index, dead_index);
2050     }
2051
2052   /* We need not check to see if the current function has nonlocal
2053      labels because we don't put any pseudos that are live over calls in
2054      registers in that case.  */
2055
2056   if (! accept_call_clobbered
2057       && flag_caller_saves
2058       && ! just_try_suggested
2059       && qty_n_calls_crossed[qty] != 0
2060       && CALLER_SAVE_PROFITABLE (qty_n_refs[qty], qty_n_calls_crossed[qty]))
2061     {
2062       i = find_free_reg (class, mode, qty, 1, 0, born_index, dead_index);
2063       if (i >= 0)
2064         caller_save_needed = 1;
2065       return i;
2066     }
2067   return -1;
2068 }
2069 \f
2070 /* Mark that REGNO with machine-mode MODE is live starting from the current
2071    insn (if LIFE is non-zero) or dead starting at the current insn (if LIFE
2072    is zero).  */
2073
2074 static void
2075 mark_life (regno, mode, life)
2076      register int regno;
2077      enum machine_mode mode;
2078      int life;
2079 {
2080   register int j = HARD_REGNO_NREGS (regno, mode);
2081   if (life)
2082     while (--j >= 0)
2083       SET_HARD_REG_BIT (regs_live, regno + j);
2084   else
2085     while (--j >= 0)
2086       CLEAR_HARD_REG_BIT (regs_live, regno + j);
2087 }
2088
2089 /* Mark register number REGNO (with machine-mode MODE) as live (if LIFE
2090    is non-zero) or dead (if LIFE is zero) from insn number BIRTH (inclusive)
2091    to insn number DEATH (exclusive).  */
2092
2093 static void
2094 post_mark_life (regno, mode, life, birth, death)
2095      int regno;
2096      enum machine_mode mode;
2097      int life, birth, death;
2098 {
2099   register int j = HARD_REGNO_NREGS (regno, mode);
2100 #ifdef HARD_REG_SET
2101   register              /* Declare it register if it's a scalar.  */
2102 #endif
2103     HARD_REG_SET this_reg;
2104
2105   CLEAR_HARD_REG_SET (this_reg);
2106   while (--j >= 0)
2107     SET_HARD_REG_BIT (this_reg, regno + j);
2108
2109   if (life)
2110     while (birth < death)
2111       {
2112         IOR_HARD_REG_SET (regs_live_at[birth], this_reg);
2113         birth++;
2114       }
2115   else
2116     while (birth < death)
2117       {
2118         AND_COMPL_HARD_REG_SET (regs_live_at[birth], this_reg);
2119         birth++;
2120       }
2121 }
2122 \f
2123 /* INSN is the CLOBBER insn that starts a REG_NO_NOCONFLICT block, R0
2124    is the register being clobbered, and R1 is a register being used in
2125    the equivalent expression.
2126
2127    If R1 dies in the block and has a REG_NO_CONFLICT note on every insn
2128    in which it is used, return 1.
2129
2130    Otherwise, return 0.  */
2131
2132 static int
2133 no_conflict_p (insn, r0, r1)
2134      rtx insn, r0, r1;
2135 {
2136   int ok = 0;
2137   rtx note = find_reg_note (insn, REG_LIBCALL, NULL_RTX);
2138   rtx p, last;
2139
2140   /* If R1 is a hard register, return 0 since we handle this case
2141      when we scan the insns that actually use it.  */
2142
2143   if (note == 0
2144       || (GET_CODE (r1) == REG && REGNO (r1) < FIRST_PSEUDO_REGISTER)
2145       || (GET_CODE (r1) == SUBREG && GET_CODE (SUBREG_REG (r1)) == REG
2146           && REGNO (SUBREG_REG (r1)) < FIRST_PSEUDO_REGISTER))
2147     return 0;
2148
2149   last = XEXP (note, 0);
2150
2151   for (p = NEXT_INSN (insn); p && p != last; p = NEXT_INSN (p))
2152     if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
2153       {
2154         if (find_reg_note (p, REG_DEAD, r1))
2155           ok = 1;
2156
2157         /* There must be a REG_NO_CONFLICT note on every insn, otherwise
2158            some earlier optimization pass has inserted instructions into
2159            the sequence, and it is not safe to perform this optimization.
2160            Note that emit_no_conflict_block always ensures that this is
2161            true when these sequences are created.  */
2162         if (! find_reg_note (p, REG_NO_CONFLICT, r1))
2163           return 0;
2164       }
2165       
2166   return ok;
2167 }
2168 \f
2169 #ifdef REGISTER_CONSTRAINTS
2170
2171 /* Return the number of alternatives for which the constraint string P
2172    indicates that the operand must be equal to operand 0 and that no register
2173    is acceptable.  */
2174
2175 static int
2176 requires_inout (p)
2177      char *p;
2178 {
2179   char c;
2180   int found_zero = 0;
2181   int reg_allowed = 0;
2182   int num_matching_alts = 0;
2183
2184   while ((c = *p++))
2185     switch (c)
2186       {
2187       case '=':  case '+':  case '?':
2188       case '#':  case '&':  case '!':
2189       case '*':  case '%':
2190       case '1':  case '2':  case '3':  case '4':
2191       case 'm':  case '<':  case '>':  case 'V':  case 'o':
2192       case 'E':  case 'F':  case 'G':  case 'H':
2193       case 's':  case 'i':  case 'n':
2194       case 'I':  case 'J':  case 'K':  case 'L':
2195       case 'M':  case 'N':  case 'O':  case 'P':
2196 #ifdef EXTRA_CONSTRAINT
2197       case 'Q':  case 'R':  case 'S':  case 'T':  case 'U':
2198 #endif
2199       case 'X':
2200         /* These don't say anything we care about.  */
2201         break;
2202
2203       case ',':
2204         if (found_zero && ! reg_allowed)
2205           num_matching_alts++;
2206
2207         found_zero = reg_allowed = 0;
2208         break;
2209
2210       case '0':
2211         found_zero = 1;
2212         break;
2213
2214       case 'p':
2215       case 'g': case 'r':
2216       default:
2217         reg_allowed = 1;
2218         break;
2219       }
2220
2221   if (found_zero && ! reg_allowed)
2222     num_matching_alts++;
2223
2224   return num_matching_alts;
2225 }
2226 #endif /* REGISTER_CONSTRAINTS */
2227 \f
2228 void
2229 dump_local_alloc (file)
2230      FILE *file;
2231 {
2232   register int i;
2233   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
2234     if (reg_renumber[i] != -1)
2235       fprintf (file, ";; Register %d in %d.\n", i, reg_renumber[i]);
2236 }