OSDN Git Service

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