OSDN Git Service

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