OSDN Git Service

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