Backport from GCC mainline.
[platform/upstream/linaro-gcc.git] / gcc / lra-eliminations.c
1 /* Code for RTL register eliminations.
2    Copyright (C) 2010-2016 Free Software Foundation, Inc.
3    Contributed by Vladimir Makarov <vmakarov@redhat.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 /* Eliminable registers (like a soft argument or frame pointer) are
22    widely used in RTL.  These eliminable registers should be replaced
23    by real hard registers (like the stack pointer or hard frame
24    pointer) plus some offset.  The offsets usually change whenever the
25    stack is expanded.  We know the final offsets only at the very end
26    of LRA.
27
28    Within LRA, we usually keep the RTL in such a state that the
29    eliminable registers can be replaced by just the corresponding hard
30    register (without any offset).  To achieve this we should add the
31    initial elimination offset at the beginning of LRA and update the
32    offsets whenever the stack is expanded.  We need to do this before
33    every constraint pass because the choice of offset often affects
34    whether a particular address or memory constraint is satisfied.
35
36    We keep RTL code at most time in such state that the virtual
37    registers can be changed by just the corresponding hard registers
38    (with zero offsets) and we have the right RTL code.  To achieve this
39    we should add initial offset at the beginning of LRA work and update
40    offsets after each stack expanding.  But actually we update virtual
41    registers to the same virtual registers + corresponding offsets
42    before every constraint pass because it affects constraint
43    satisfaction (e.g. an address displacement became too big for some
44    target).
45
46    The final change of eliminable registers to the corresponding hard
47    registers are done at the very end of LRA when there were no change
48    in offsets anymore:
49
50                      fp + 42     =>     sp + 42
51
52 */
53
54 #include "config.h"
55 #include "system.h"
56 #include "coretypes.h"
57 #include "backend.h"
58 #include "target.h"
59 #include "rtl.h"
60 #include "tree.h"
61 #include "df.h"
62 #include "tm_p.h"
63 #include "optabs.h"
64 #include "regs.h"
65 #include "ira.h"
66 #include "recog.h"
67 #include "output.h"
68 #include "rtl-error.h"
69 #include "lra-int.h"
70
71 /* This structure is used to record information about hard register
72    eliminations.  */
73 struct lra_elim_table
74 {
75   /* Hard register number to be eliminated.  */
76   int from;
77   /* Hard register number used as replacement.  */
78   int to;
79   /* Difference between values of the two hard registers above on
80      previous iteration.  */
81   HOST_WIDE_INT previous_offset;
82   /* Difference between the values on the current iteration.  */
83   HOST_WIDE_INT offset;
84   /* Nonzero if this elimination can be done.  */
85   bool can_eliminate;
86   /* CAN_ELIMINATE since the last check.  */
87   bool prev_can_eliminate;
88   /* REG rtx for the register to be eliminated.  We cannot simply
89      compare the number since we might then spuriously replace a hard
90      register corresponding to a pseudo assigned to the reg to be
91      eliminated.  */
92   rtx from_rtx;
93   /* REG rtx for the replacement.  */
94   rtx to_rtx;
95 };
96
97 /* The elimination table.  Each array entry describes one possible way
98    of eliminating a register in favor of another.  If there is more
99    than one way of eliminating a particular register, the most
100    preferred should be specified first.  */
101 static struct lra_elim_table *reg_eliminate = 0;
102
103 /* This is an intermediate structure to initialize the table.  It has
104    exactly the members provided by ELIMINABLE_REGS.  */
105 static const struct elim_table_1
106 {
107   const int from;
108   const int to;
109 } reg_eliminate_1[] =
110
111 /* If a set of eliminable hard registers was specified, define the
112    table from it.  Otherwise, default to the normal case of the frame
113    pointer being replaced by the stack pointer.  */
114
115 #ifdef ELIMINABLE_REGS
116   ELIMINABLE_REGS;
117 #else
118   {{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}};
119 #endif
120
121 #define NUM_ELIMINABLE_REGS ARRAY_SIZE (reg_eliminate_1)
122
123 /* Print info about elimination table to file F.  */
124 static void
125 print_elim_table (FILE *f)
126 {
127   struct lra_elim_table *ep;
128
129   for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
130     fprintf (f, "%s eliminate %d to %d (offset=" HOST_WIDE_INT_PRINT_DEC
131              ", prev_offset=" HOST_WIDE_INT_PRINT_DEC ")\n",
132              ep->can_eliminate ? "Can" : "Can't",
133              ep->from, ep->to, ep->offset, ep->previous_offset);
134 }
135
136 /* Print info about elimination table to stderr.  */
137 void
138 lra_debug_elim_table (void)
139 {
140   print_elim_table (stderr);
141 }
142
143 /* Setup possibility of elimination in elimination table element EP to
144    VALUE.  Setup FRAME_POINTER_NEEDED if elimination from frame
145    pointer to stack pointer is not possible anymore.  */
146 static void
147 setup_can_eliminate (struct lra_elim_table *ep, bool value)
148 {
149   ep->can_eliminate = ep->prev_can_eliminate = value;
150   if (! value
151       && ep->from == FRAME_POINTER_REGNUM && ep->to == STACK_POINTER_REGNUM)
152     frame_pointer_needed = 1;
153   if (!frame_pointer_needed)
154     REGNO_POINTER_ALIGN (HARD_FRAME_POINTER_REGNUM) = 0;
155 }
156
157 /* Map: eliminable "from" register -> its current elimination,
158    or NULL if none.  The elimination table may contain more than
159    one elimination for the same hard register, but this map specifies
160    the one that we are currently using.  */
161 static struct lra_elim_table *elimination_map[FIRST_PSEUDO_REGISTER];
162
163 /* When an eliminable hard register becomes not eliminable, we use the
164    following special structure to restore original offsets for the
165    register.  */
166 static struct lra_elim_table self_elim_table;
167
168 /* Offsets should be used to restore original offsets for eliminable
169    hard register which just became not eliminable.  Zero,
170    otherwise.  */
171 static HOST_WIDE_INT self_elim_offsets[FIRST_PSEUDO_REGISTER];
172
173 /* Map: hard regno -> RTL presentation.  RTL presentations of all
174    potentially eliminable hard registers are stored in the map.  */
175 static rtx eliminable_reg_rtx[FIRST_PSEUDO_REGISTER];
176
177 /* Set up ELIMINATION_MAP of the currently used eliminations.  */
178 static void
179 setup_elimination_map (void)
180 {
181   int i;
182   struct lra_elim_table *ep;
183
184   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
185     elimination_map[i] = NULL;
186   for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
187     if (ep->can_eliminate && elimination_map[ep->from] == NULL)
188       elimination_map[ep->from] = ep;
189 }
190
191 \f
192
193 /* Compute the sum of X and Y, making canonicalizations assumed in an
194    address, namely: sum constant integers, surround the sum of two
195    constants with a CONST, put the constant as the second operand, and
196    group the constant on the outermost sum.
197
198    This routine assumes both inputs are already in canonical form.  */
199 static rtx
200 form_sum (rtx x, rtx y)
201 {
202   machine_mode mode = GET_MODE (x);
203
204   if (mode == VOIDmode)
205     mode = GET_MODE (y);
206
207   if (mode == VOIDmode)
208     mode = Pmode;
209
210   if (CONST_INT_P (x))
211     return plus_constant (mode, y, INTVAL (x));
212   else if (CONST_INT_P (y))
213     return plus_constant (mode, x, INTVAL (y));
214   else if (CONSTANT_P (x))
215     std::swap (x, y);
216
217   if (GET_CODE (x) == PLUS && CONSTANT_P (XEXP (x, 1)))
218     return form_sum (XEXP (x, 0), form_sum (XEXP (x, 1), y));
219
220   /* Note that if the operands of Y are specified in the opposite
221      order in the recursive calls below, infinite recursion will
222      occur.  */
223   if (GET_CODE (y) == PLUS && CONSTANT_P (XEXP (y, 1)))
224     return form_sum (form_sum (x, XEXP (y, 0)), XEXP (y, 1));
225
226   /* If both constant, encapsulate sum.  Otherwise, just form sum.  A
227      constant will have been placed second.  */
228   if (CONSTANT_P (x) && CONSTANT_P (y))
229     {
230       if (GET_CODE (x) == CONST)
231         x = XEXP (x, 0);
232       if (GET_CODE (y) == CONST)
233         y = XEXP (y, 0);
234
235       return gen_rtx_CONST (VOIDmode, gen_rtx_PLUS (mode, x, y));
236     }
237
238   return gen_rtx_PLUS (mode, x, y);
239 }
240
241 /* Return the current substitution hard register of the elimination of
242    HARD_REGNO.  If HARD_REGNO is not eliminable, return itself.  */
243 int
244 lra_get_elimination_hard_regno (int hard_regno)
245 {
246   struct lra_elim_table *ep;
247
248   if (hard_regno < 0 || hard_regno >= FIRST_PSEUDO_REGISTER)
249     return hard_regno;
250   if ((ep = elimination_map[hard_regno]) == NULL)
251     return hard_regno;
252   return ep->to;
253 }
254
255 /* Return elimination which will be used for hard reg REG, NULL
256    otherwise.  */
257 static struct lra_elim_table *
258 get_elimination (rtx reg)
259 {
260   int hard_regno;
261   struct lra_elim_table *ep;
262   HOST_WIDE_INT offset;
263
264   lra_assert (REG_P (reg));
265   if ((hard_regno = REGNO (reg)) < 0 || hard_regno >= FIRST_PSEUDO_REGISTER)
266     return NULL;
267   if ((ep = elimination_map[hard_regno]) != NULL)
268     return ep->from_rtx != reg ? NULL : ep;
269   if ((offset = self_elim_offsets[hard_regno]) == 0)
270     return NULL;
271   /* This is an iteration to restore offsets just after HARD_REGNO
272      stopped to be eliminable.  */
273   self_elim_table.from = self_elim_table.to = hard_regno;
274   self_elim_table.from_rtx
275     = self_elim_table.to_rtx
276     = eliminable_reg_rtx[hard_regno];
277   lra_assert (self_elim_table.from_rtx != NULL);
278   self_elim_table.offset = offset;
279   return &self_elim_table;
280 }
281
282 /* Transform (subreg (plus reg const)) to (plus (subreg reg) const)
283    when it is possible.  Return X or the transformation result if the
284    transformation is done.  */
285 static rtx
286 move_plus_up (rtx x)
287 {
288   rtx subreg_reg;
289   enum machine_mode x_mode, subreg_reg_mode;
290   
291   if (GET_CODE (x) != SUBREG || !subreg_lowpart_p (x))
292     return x;
293   subreg_reg = SUBREG_REG (x);
294   x_mode = GET_MODE (x);
295   subreg_reg_mode = GET_MODE (subreg_reg);
296   if (GET_CODE (x) == SUBREG && GET_CODE (subreg_reg) == PLUS
297       && GET_MODE_SIZE (x_mode) <= GET_MODE_SIZE (subreg_reg_mode)
298       && CONSTANT_P (XEXP (subreg_reg, 1))
299       && GET_MODE_CLASS (x_mode) == MODE_INT
300       && GET_MODE_CLASS (subreg_reg_mode) == MODE_INT)
301     {
302       rtx cst = simplify_subreg (x_mode, XEXP (subreg_reg, 1), subreg_reg_mode,
303                                  subreg_lowpart_offset (x_mode,
304                                                         subreg_reg_mode));
305       if (cst && CONSTANT_P (cst))
306         return gen_rtx_PLUS (x_mode, lowpart_subreg (x_mode,
307                                                      XEXP (subreg_reg, 0),
308                                                      subreg_reg_mode), cst);
309     }
310   return x;
311 }
312
313 /* Scan X and replace any eliminable registers (such as fp) with a
314    replacement (such as sp) if SUBST_P, plus an offset.  The offset is
315    a change in the offset between the eliminable register and its
316    substitution if UPDATE_P, or the full offset if FULL_P, or
317    otherwise zero.  If FULL_P, we also use the SP offsets for
318    elimination to SP.  If UPDATE_P, use UPDATE_SP_OFFSET for updating
319    offsets of register elimnable to SP.  If UPDATE_SP_OFFSET is
320    non-zero, don't use difference of the offset and the previous
321    offset.
322
323    MEM_MODE is the mode of an enclosing MEM.  We need this to know how
324    much to adjust a register for, e.g., PRE_DEC.  Also, if we are
325    inside a MEM, we are allowed to replace a sum of a hard register
326    and the constant zero with the hard register, which we cannot do
327    outside a MEM.  In addition, we need to record the fact that a
328    hard register is referenced outside a MEM.
329
330    If we make full substitution to SP for non-null INSN, add the insn
331    sp offset.  */
332 rtx
333 lra_eliminate_regs_1 (rtx_insn *insn, rtx x, machine_mode mem_mode,
334                       bool subst_p, bool update_p,
335                       HOST_WIDE_INT update_sp_offset, bool full_p)
336 {
337   enum rtx_code code = GET_CODE (x);
338   struct lra_elim_table *ep;
339   rtx new_rtx;
340   int i, j;
341   const char *fmt;
342   int copied = 0;
343
344   lra_assert (!update_p || !full_p);
345   lra_assert (update_sp_offset == 0 || (!subst_p && update_p && !full_p));
346   if (! current_function_decl)
347     return x;
348
349   switch (code)
350     {
351     CASE_CONST_ANY:
352     case CONST:
353     case SYMBOL_REF:
354     case CODE_LABEL:
355     case PC:
356     case CC0:
357     case ASM_INPUT:
358     case ADDR_VEC:
359     case ADDR_DIFF_VEC:
360     case RETURN:
361       return x;
362
363     case REG:
364       /* First handle the case where we encounter a bare hard register
365          that is eliminable.  Replace it with a PLUS.  */
366       if ((ep = get_elimination (x)) != NULL)
367         {
368           rtx to = subst_p ? ep->to_rtx : ep->from_rtx;
369
370           if (update_sp_offset != 0)
371             {
372               if (ep->to_rtx == stack_pointer_rtx)
373                 return plus_constant (Pmode, to, update_sp_offset);
374               return to;
375             }
376           else if (update_p)
377             return plus_constant (Pmode, to, ep->offset - ep->previous_offset);
378           else if (full_p)
379             return plus_constant (Pmode, to,
380                                   ep->offset
381                                   - (insn != NULL_RTX
382                                      && ep->to_rtx == stack_pointer_rtx
383                                      ? lra_get_insn_recog_data (insn)->sp_offset
384                                      : 0));
385           else
386             return to;
387         }
388       return x;
389
390     case PLUS:
391       /* If this is the sum of an eliminable register and a constant, rework
392          the sum.  */
393       if (REG_P (XEXP (x, 0)) && CONSTANT_P (XEXP (x, 1)))
394         {
395           if ((ep = get_elimination (XEXP (x, 0))) != NULL)
396             {
397               HOST_WIDE_INT offset;
398               rtx to = subst_p ? ep->to_rtx : ep->from_rtx;
399
400               if (! update_p && ! full_p)
401                 return gen_rtx_PLUS (Pmode, to, XEXP (x, 1));
402               
403               if (update_sp_offset != 0)
404                 offset = ep->to_rtx == stack_pointer_rtx ? update_sp_offset : 0;
405               else
406                 offset = (update_p
407                           ? ep->offset - ep->previous_offset : ep->offset);
408               if (full_p && insn != NULL_RTX && ep->to_rtx == stack_pointer_rtx)
409                 offset -= lra_get_insn_recog_data (insn)->sp_offset;
410               if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) == -offset)
411                 return to;
412               else
413                 return gen_rtx_PLUS (Pmode, to,
414                                      plus_constant (Pmode,
415                                                     XEXP (x, 1), offset));
416             }
417
418           /* If the hard register is not eliminable, we are done since
419              the other operand is a constant.  */
420           return x;
421         }
422
423       /* If this is part of an address, we want to bring any constant
424          to the outermost PLUS.  We will do this by doing hard
425          register replacement in our operands and seeing if a constant
426          shows up in one of them.
427
428          Note that there is no risk of modifying the structure of the
429          insn, since we only get called for its operands, thus we are
430          either modifying the address inside a MEM, or something like
431          an address operand of a load-address insn.  */
432
433       {
434         rtx new0 = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
435                                          subst_p, update_p,
436                                          update_sp_offset, full_p);
437         rtx new1 = lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
438                                          subst_p, update_p,
439                                          update_sp_offset, full_p);
440
441         new0 = move_plus_up (new0);
442         new1 = move_plus_up (new1);
443         if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
444           return form_sum (new0, new1);
445       }
446       return x;
447
448     case MULT:
449       /* If this is the product of an eliminable hard register and a
450          constant, apply the distribute law and move the constant out
451          so that we have (plus (mult ..) ..).  This is needed in order
452          to keep load-address insns valid.  This case is pathological.
453          We ignore the possibility of overflow here.  */
454       if (REG_P (XEXP (x, 0)) && CONST_INT_P (XEXP (x, 1))
455           && (ep = get_elimination (XEXP (x, 0))) != NULL)
456         {
457           rtx to = subst_p ? ep->to_rtx : ep->from_rtx;
458
459           if (update_sp_offset != 0)
460             {
461               if (ep->to_rtx == stack_pointer_rtx)
462                 return plus_constant (Pmode,
463                                       gen_rtx_MULT (Pmode, to, XEXP (x, 1)),
464                                       update_sp_offset * INTVAL (XEXP (x, 1)));
465               return gen_rtx_MULT (Pmode, to, XEXP (x, 1));
466             }
467           else if (update_p)
468             return plus_constant (Pmode,
469                                   gen_rtx_MULT (Pmode, to, XEXP (x, 1)),
470                                   (ep->offset - ep->previous_offset)
471                                   * INTVAL (XEXP (x, 1)));
472           else if (full_p)
473             {
474               HOST_WIDE_INT offset = ep->offset;
475
476               if (insn != NULL_RTX && ep->to_rtx == stack_pointer_rtx)
477                 offset -= lra_get_insn_recog_data (insn)->sp_offset;
478               return
479                 plus_constant (Pmode,
480                                gen_rtx_MULT (Pmode, to, XEXP (x, 1)),
481                                offset * INTVAL (XEXP (x, 1)));
482             }
483           else
484             return gen_rtx_MULT (Pmode, to, XEXP (x, 1));
485         }
486
487       /* ... fall through ...  */
488
489     case CALL:
490     case COMPARE:
491     /* See comments before PLUS about handling MINUS.  */
492     case MINUS:
493     case DIV:      case UDIV:
494     case MOD:      case UMOD:
495     case AND:      case IOR:      case XOR:
496     case ROTATERT: case ROTATE:
497     case ASHIFTRT: case LSHIFTRT: case ASHIFT:
498     case NE:       case EQ:
499     case GE:       case GT:       case GEU:    case GTU:
500     case LE:       case LT:       case LEU:    case LTU:
501       {
502         rtx new0 = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
503                                          subst_p, update_p, 
504                                          update_sp_offset, full_p);
505         rtx new1 = XEXP (x, 1)
506                    ? lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
507                                            subst_p, update_p,
508                                            update_sp_offset, full_p) : 0;
509
510         if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
511           return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
512       }
513       return x;
514
515     case EXPR_LIST:
516       /* If we have something in XEXP (x, 0), the usual case,
517          eliminate it.  */
518       if (XEXP (x, 0))
519         {
520           new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
521                                           subst_p, update_p,
522                                           update_sp_offset, full_p);
523           if (new_rtx != XEXP (x, 0))
524             {
525               /* If this is a REG_DEAD note, it is not valid anymore.
526                  Using the eliminated version could result in creating a
527                  REG_DEAD note for the stack or frame pointer.  */
528               if (REG_NOTE_KIND (x) == REG_DEAD)
529                 return (XEXP (x, 1)
530                         ? lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
531                                                 subst_p, update_p,
532                                                 update_sp_offset, full_p)
533                         : NULL_RTX);
534
535               x = alloc_reg_note (REG_NOTE_KIND (x), new_rtx, XEXP (x, 1));
536             }
537         }
538
539       /* ... fall through ...  */
540
541     case INSN_LIST:
542     case INT_LIST:
543       /* Now do eliminations in the rest of the chain.  If this was
544          an EXPR_LIST, this might result in allocating more memory than is
545          strictly needed, but it simplifies the code.  */
546       if (XEXP (x, 1))
547         {
548           new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
549                                           subst_p, update_p,
550                                           update_sp_offset, full_p);
551           if (new_rtx != XEXP (x, 1))
552             return
553               gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x),
554                               XEXP (x, 0), new_rtx);
555         }
556       return x;
557
558     case PRE_INC:
559     case POST_INC:
560     case PRE_DEC:
561     case POST_DEC:
562       /* We do not support elimination of a register that is modified.
563          elimination_effects has already make sure that this does not
564          happen.  */
565       return x;
566
567     case PRE_MODIFY:
568     case POST_MODIFY:
569       /* We do not support elimination of a hard register that is
570          modified.  LRA has already make sure that this does not
571          happen. The only remaining case we need to consider here is
572          that the increment value may be an eliminable register.  */
573       if (GET_CODE (XEXP (x, 1)) == PLUS
574           && XEXP (XEXP (x, 1), 0) == XEXP (x, 0))
575         {
576           rtx new_rtx = lra_eliminate_regs_1 (insn, XEXP (XEXP (x, 1), 1),
577                                               mem_mode, subst_p, update_p,
578                                               update_sp_offset, full_p);
579
580           if (new_rtx != XEXP (XEXP (x, 1), 1))
581             return gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (x, 0),
582                                    gen_rtx_PLUS (GET_MODE (x),
583                                                  XEXP (x, 0), new_rtx));
584         }
585       return x;
586
587     case STRICT_LOW_PART:
588     case NEG:          case NOT:
589     case SIGN_EXTEND:  case ZERO_EXTEND:
590     case TRUNCATE:     case FLOAT_EXTEND: case FLOAT_TRUNCATE:
591     case FLOAT:        case FIX:
592     case UNSIGNED_FIX: case UNSIGNED_FLOAT:
593     case ABS:
594     case SQRT:
595     case FFS:
596     case CLZ:
597     case CTZ:
598     case POPCOUNT:
599     case PARITY:
600     case BSWAP:
601       new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
602                                       subst_p, update_p,
603                                       update_sp_offset, full_p);
604       if (new_rtx != XEXP (x, 0))
605         return gen_rtx_fmt_e (code, GET_MODE (x), new_rtx);
606       return x;
607
608     case SUBREG:
609       new_rtx = lra_eliminate_regs_1 (insn, SUBREG_REG (x), mem_mode,
610                                       subst_p, update_p,
611                                       update_sp_offset, full_p);
612
613       if (new_rtx != SUBREG_REG (x))
614         {
615           int x_size = GET_MODE_SIZE (GET_MODE (x));
616           int new_size = GET_MODE_SIZE (GET_MODE (new_rtx));
617
618           if (MEM_P (new_rtx) && x_size <= new_size)
619             {
620               SUBREG_REG (x) = new_rtx;
621               alter_subreg (&x, false);
622               return x;
623             }
624           else if (! subst_p)
625             {
626               /* LRA can transform subregs itself.  So don't call
627                  simplify_gen_subreg until LRA transformations are
628                  finished.  Function simplify_gen_subreg can do
629                  non-trivial transformations (like truncation) which
630                  might make LRA work to fail.  */
631               SUBREG_REG (x) = new_rtx;
632               return x;
633             }
634           else
635             return simplify_gen_subreg (GET_MODE (x), new_rtx,
636                                         GET_MODE (new_rtx), SUBREG_BYTE (x));
637         }
638
639       return x;
640
641     case MEM:
642       /* Our only special processing is to pass the mode of the MEM to our
643          recursive call and copy the flags.  While we are here, handle this
644          case more efficiently.  */
645       return
646         replace_equiv_address_nv
647         (x,
648          lra_eliminate_regs_1 (insn, XEXP (x, 0), GET_MODE (x),
649                                subst_p, update_p, update_sp_offset, full_p));
650
651     case USE:
652       /* Handle insn_list USE that a call to a pure function may generate.  */
653       new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 0), VOIDmode,
654                                       subst_p, update_p, update_sp_offset, full_p);
655       if (new_rtx != XEXP (x, 0))
656         return gen_rtx_USE (GET_MODE (x), new_rtx);
657       return x;
658
659     case CLOBBER:
660     case SET:
661       gcc_unreachable ();
662
663     default:
664       break;
665     }
666
667   /* Process each of our operands recursively.  If any have changed, make a
668      copy of the rtx.  */
669   fmt = GET_RTX_FORMAT (code);
670   for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
671     {
672       if (*fmt == 'e')
673         {
674           new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, i), mem_mode,
675                                           subst_p, update_p,
676                                           update_sp_offset, full_p);
677           if (new_rtx != XEXP (x, i) && ! copied)
678             {
679               x = shallow_copy_rtx (x);
680               copied = 1;
681             }
682           XEXP (x, i) = new_rtx;
683         }
684       else if (*fmt == 'E')
685         {
686           int copied_vec = 0;
687           for (j = 0; j < XVECLEN (x, i); j++)
688             {
689               new_rtx = lra_eliminate_regs_1 (insn, XVECEXP (x, i, j), mem_mode,
690                                               subst_p, update_p,
691                                               update_sp_offset, full_p);
692               if (new_rtx != XVECEXP (x, i, j) && ! copied_vec)
693                 {
694                   rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
695                                              XVEC (x, i)->elem);
696                   if (! copied)
697                     {
698                       x = shallow_copy_rtx (x);
699                       copied = 1;
700                     }
701                   XVEC (x, i) = new_v;
702                   copied_vec = 1;
703                 }
704               XVECEXP (x, i, j) = new_rtx;
705             }
706         }
707     }
708
709   return x;
710 }
711
712 /* This function is used externally in subsequent passes of GCC.  It
713    always does a full elimination of X.  */
714 rtx
715 lra_eliminate_regs (rtx x, machine_mode mem_mode,
716                     rtx insn ATTRIBUTE_UNUSED)
717 {
718   return lra_eliminate_regs_1 (NULL, x, mem_mode, true, false, 0, true);
719 }
720
721 /* Stack pointer offset before the current insn relative to one at the
722    func start.  RTL insns can change SP explicitly.  We keep the
723    changes from one insn to another through this variable.  */
724 static HOST_WIDE_INT curr_sp_change;
725
726 /* Scan rtx X for references to elimination source or target registers
727    in contexts that would prevent the elimination from happening.
728    Update the table of eliminables to reflect the changed state.
729    MEM_MODE is the mode of an enclosing MEM rtx, or VOIDmode if not
730    within a MEM.  */
731 static void
732 mark_not_eliminable (rtx x, machine_mode mem_mode)
733 {
734   enum rtx_code code = GET_CODE (x);
735   struct lra_elim_table *ep;
736   int i, j;
737   const char *fmt;
738
739   switch (code)
740     {
741     case PRE_INC:
742     case POST_INC:
743     case PRE_DEC:
744     case POST_DEC:
745     case POST_MODIFY:
746     case PRE_MODIFY:
747       if (XEXP (x, 0) == stack_pointer_rtx
748           && ((code != PRE_MODIFY && code != POST_MODIFY)
749               || (GET_CODE (XEXP (x, 1)) == PLUS
750                   && XEXP (x, 0) == XEXP (XEXP (x, 1), 0)
751                   && CONST_INT_P (XEXP (XEXP (x, 1), 1)))))
752         {
753           int size = GET_MODE_SIZE (mem_mode);
754           
755 #ifdef PUSH_ROUNDING
756           /* If more bytes than MEM_MODE are pushed, account for
757              them.  */
758           size = PUSH_ROUNDING (size);
759 #endif
760           if (code == PRE_DEC || code == POST_DEC)
761             curr_sp_change -= size;
762           else if (code == PRE_INC || code == POST_INC)
763             curr_sp_change += size;
764           else if (code == PRE_MODIFY || code == POST_MODIFY)
765             curr_sp_change += INTVAL (XEXP (XEXP (x, 1), 1));
766         }
767       else if (REG_P (XEXP (x, 0))
768                && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER)
769         {
770           /* If we modify the source of an elimination rule, disable
771              it.  Do the same if it is the destination and not the
772              hard frame register.  */
773           for (ep = reg_eliminate;
774                ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
775                ep++)
776             if (ep->from_rtx == XEXP (x, 0)
777                 || (ep->to_rtx == XEXP (x, 0)
778                     && ep->to_rtx != hard_frame_pointer_rtx))
779               setup_can_eliminate (ep, false);
780         }
781       return;
782
783     case USE:
784       if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
785         /* If using a hard register that is the source of an eliminate
786            we still think can be performed, note it cannot be
787            performed since we don't know how this hard register is
788            used.  */
789         for (ep = reg_eliminate;
790              ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
791              ep++)
792           if (ep->from_rtx == XEXP (x, 0)
793               && ep->to_rtx != hard_frame_pointer_rtx)
794             setup_can_eliminate (ep, false);
795       return;
796
797     case CLOBBER:
798       if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
799         /* If clobbering a hard register that is the replacement
800            register for an elimination we still think can be
801            performed, note that it cannot be performed.  Otherwise, we
802            need not be concerned about it.  */
803         for (ep = reg_eliminate;
804              ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
805              ep++)
806           if (ep->to_rtx == XEXP (x, 0)
807               && ep->to_rtx != hard_frame_pointer_rtx)
808             setup_can_eliminate (ep, false);
809       return;
810
811     case SET:
812       if (SET_DEST (x) == stack_pointer_rtx
813           && GET_CODE (SET_SRC (x)) == PLUS
814           && XEXP (SET_SRC (x), 0) == SET_DEST (x)
815           && CONST_INT_P (XEXP (SET_SRC (x), 1)))
816         {
817           curr_sp_change += INTVAL (XEXP (SET_SRC (x), 1));
818           return;
819         }
820       if (! REG_P (SET_DEST (x))
821           || REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER)
822         mark_not_eliminable (SET_DEST (x), mem_mode);
823       else
824         {
825           /* See if this is setting the replacement hard register for
826              an elimination.
827              
828              If DEST is the hard frame pointer, we do nothing because
829              we assume that all assignments to the frame pointer are
830              for non-local gotos and are being done at a time when
831              they are valid and do not disturb anything else.  Some
832              machines want to eliminate a fake argument pointer (or
833              even a fake frame pointer) with either the real frame
834              pointer or the stack pointer.  Assignments to the hard
835              frame pointer must not prevent this elimination.  */
836           for (ep = reg_eliminate;
837                ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
838                ep++)
839             if (ep->to_rtx == SET_DEST (x)
840                 && SET_DEST (x) != hard_frame_pointer_rtx)
841               setup_can_eliminate (ep, false);
842         }
843       
844       mark_not_eliminable (SET_SRC (x), mem_mode);
845       return;
846
847     case MEM:
848       /* Our only special processing is to pass the mode of the MEM to
849          our recursive call.  */
850       mark_not_eliminable (XEXP (x, 0), GET_MODE (x));
851       return;
852
853     default:
854       break;
855     }
856
857   fmt = GET_RTX_FORMAT (code);
858   for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
859     {
860       if (*fmt == 'e')
861         mark_not_eliminable (XEXP (x, i), mem_mode);
862       else if (*fmt == 'E')
863         for (j = 0; j < XVECLEN (x, i); j++)
864           mark_not_eliminable (XVECEXP (x, i, j), mem_mode);
865     }
866 }
867
868 \f
869
870 #ifdef HARD_FRAME_POINTER_REGNUM
871
872 /* Find offset equivalence note for reg WHAT in INSN and return the
873    found elmination offset.  If the note is not found, return NULL.
874    Remove the found note.  */
875 static rtx
876 remove_reg_equal_offset_note (rtx_insn *insn, rtx what)
877 {
878   rtx link, *link_loc;
879
880   for (link_loc = &REG_NOTES (insn);
881        (link = *link_loc) != NULL_RTX;
882        link_loc = &XEXP (link, 1))
883     if (REG_NOTE_KIND (link) == REG_EQUAL
884         && GET_CODE (XEXP (link, 0)) == PLUS
885         && XEXP (XEXP (link, 0), 0) == what
886         && CONST_INT_P (XEXP (XEXP (link, 0), 1)))
887       {
888         *link_loc = XEXP (link, 1);
889         return XEXP (XEXP (link, 0), 1);
890       }
891   return NULL_RTX;
892 }
893
894 #endif
895
896 /* Scan INSN and eliminate all eliminable hard registers in it.
897
898    If REPLACE_P is true, do the replacement destructively.  Also
899    delete the insn as dead it if it is setting an eliminable register.
900
901    If REPLACE_P is false, just update the offsets while keeping the
902    base register the same.  If FIRST_P, use the sp offset for
903    elimination to sp.  Otherwise, use UPDATE_SP_OFFSET for this.  If
904    UPDATE_SP_OFFSET is non-zero, don't use difference of the offset
905    and the previous offset.  Attach the note about used elimination
906    for insns setting frame pointer to update elimination easy (without
907    parsing already generated elimination insns to find offset
908    previously used) in future.  */
909
910 void
911 eliminate_regs_in_insn (rtx_insn *insn, bool replace_p, bool first_p,
912                         HOST_WIDE_INT update_sp_offset)
913 {
914   int icode = recog_memoized (insn);
915   rtx old_set = single_set (insn);
916   bool validate_p;
917   int i;
918   rtx substed_operand[MAX_RECOG_OPERANDS];
919   rtx orig_operand[MAX_RECOG_OPERANDS];
920   struct lra_elim_table *ep;
921   rtx plus_src, plus_cst_src;
922   lra_insn_recog_data_t id;
923   struct lra_static_insn_data *static_id;
924
925   if (icode < 0 && asm_noperands (PATTERN (insn)) < 0 && ! DEBUG_INSN_P (insn))
926     {
927       lra_assert (GET_CODE (PATTERN (insn)) == USE
928                   || GET_CODE (PATTERN (insn)) == CLOBBER
929                   || GET_CODE (PATTERN (insn)) == ASM_INPUT);
930       return;
931     }
932
933   /* Check for setting an eliminable register.  */
934   if (old_set != 0 && REG_P (SET_DEST (old_set))
935       && (ep = get_elimination (SET_DEST (old_set))) != NULL)
936     {
937       for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
938         if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
939           {
940             bool delete_p = replace_p;
941             
942 #ifdef HARD_FRAME_POINTER_REGNUM
943             if (ep->from == FRAME_POINTER_REGNUM
944                 && ep->to == HARD_FRAME_POINTER_REGNUM)
945               /* If this is setting the frame pointer register to the
946                  hardware frame pointer register and this is an
947                  elimination that will be done (tested above), this
948                  insn is really adjusting the frame pointer downward
949                  to compensate for the adjustment done before a
950                  nonlocal goto.  */
951               {
952                 rtx src = SET_SRC (old_set);
953                 rtx off = remove_reg_equal_offset_note (insn, ep->to_rtx);
954                 
955                 /* We should never process such insn with non-zero
956                    UPDATE_SP_OFFSET.  */
957                 lra_assert (update_sp_offset == 0);
958                 
959                 if (off != NULL_RTX
960                     || src == ep->to_rtx
961                     || (GET_CODE (src) == PLUS
962                         && XEXP (src, 0) == ep->to_rtx
963                         && CONST_INT_P (XEXP (src, 1))))
964                   {
965                     HOST_WIDE_INT offset;
966                     
967                     if (replace_p)
968                       {
969                         SET_DEST (old_set) = ep->to_rtx;
970                         lra_update_insn_recog_data (insn);
971                         return;
972                       }
973                     offset = (off != NULL_RTX ? INTVAL (off)
974                               : src == ep->to_rtx ? 0 : INTVAL (XEXP (src, 1)));
975                     offset -= (ep->offset - ep->previous_offset);
976                     src = plus_constant (Pmode, ep->to_rtx, offset);
977                     
978                     /* First see if this insn remains valid when we
979                        make the change.  If not, keep the INSN_CODE
980                        the same and let the constraint pass fit it
981                        up.  */
982                     validate_change (insn, &SET_SRC (old_set), src, 1);
983                     validate_change (insn, &SET_DEST (old_set),
984                                      ep->from_rtx, 1);
985                     if (! apply_change_group ())
986                       {
987                         SET_SRC (old_set) = src;
988                         SET_DEST (old_set) = ep->from_rtx;
989                       }
990                     lra_update_insn_recog_data (insn);
991                     /* Add offset note for future updates.  */
992                     add_reg_note (insn, REG_EQUAL, src);
993                     return;
994                   }
995               }
996 #endif
997             
998             /* This insn isn't serving a useful purpose.  We delete it
999                when REPLACE is set.  */
1000             if (delete_p)
1001               lra_delete_dead_insn (insn);
1002             return;
1003           }
1004     }
1005
1006   /* We allow one special case which happens to work on all machines we
1007      currently support: a single set with the source or a REG_EQUAL
1008      note being a PLUS of an eliminable register and a constant.  */
1009   plus_src = plus_cst_src = 0;
1010   if (old_set && REG_P (SET_DEST (old_set)))
1011     {
1012       if (GET_CODE (SET_SRC (old_set)) == PLUS)
1013         plus_src = SET_SRC (old_set);
1014       /* First see if the source is of the form (plus (...) CST).  */
1015       if (plus_src
1016           && CONST_INT_P (XEXP (plus_src, 1)))
1017         plus_cst_src = plus_src;
1018       /* Check that the first operand of the PLUS is a hard reg or
1019          the lowpart subreg of one.  */
1020       if (plus_cst_src)
1021         {
1022           rtx reg = XEXP (plus_cst_src, 0);
1023
1024           if (GET_CODE (reg) == SUBREG && subreg_lowpart_p (reg))
1025             reg = SUBREG_REG (reg);
1026
1027           if (!REG_P (reg) || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
1028             plus_cst_src = 0;
1029         }
1030     }
1031   if (plus_cst_src)
1032     {
1033       rtx reg = XEXP (plus_cst_src, 0);
1034       HOST_WIDE_INT offset = INTVAL (XEXP (plus_cst_src, 1));
1035
1036       if (GET_CODE (reg) == SUBREG)
1037         reg = SUBREG_REG (reg);
1038
1039       if (REG_P (reg) && (ep = get_elimination (reg)) != NULL)
1040         {
1041           rtx to_rtx = replace_p ? ep->to_rtx : ep->from_rtx;
1042
1043           if (! replace_p)
1044             {
1045               if (update_sp_offset == 0)
1046                 offset += (ep->offset - ep->previous_offset);
1047               if (ep->to_rtx == stack_pointer_rtx)
1048                 {
1049                   if (first_p)
1050                     offset -= lra_get_insn_recog_data (insn)->sp_offset;
1051                   else
1052                     offset += update_sp_offset;
1053                 }
1054               offset = trunc_int_for_mode (offset, GET_MODE (plus_cst_src));
1055             }
1056
1057           if (GET_CODE (XEXP (plus_cst_src, 0)) == SUBREG)
1058             to_rtx = gen_lowpart (GET_MODE (XEXP (plus_cst_src, 0)), to_rtx);
1059           /* If we have a nonzero offset, and the source is already a
1060              simple REG, the following transformation would increase
1061              the cost of the insn by replacing a simple REG with (plus
1062              (reg sp) CST).  So try only when we already had a PLUS
1063              before.  */
1064           if (offset == 0 || plus_src)
1065             {
1066               rtx new_src = plus_constant (GET_MODE (to_rtx), to_rtx, offset);
1067
1068               old_set = single_set (insn);
1069
1070               /* First see if this insn remains valid when we make the
1071                  change.  If not, try to replace the whole pattern
1072                  with a simple set (this may help if the original insn
1073                  was a PARALLEL that was only recognized as single_set
1074                  due to REG_UNUSED notes).  If this isn't valid
1075                  either, keep the INSN_CODE the same and let the
1076                  constraint pass fix it up.  */
1077               if (! validate_change (insn, &SET_SRC (old_set), new_src, 0))
1078                 {
1079                   rtx new_pat = gen_rtx_SET (SET_DEST (old_set), new_src);
1080
1081                   if (! validate_change (insn, &PATTERN (insn), new_pat, 0))
1082                     SET_SRC (old_set) = new_src;
1083                 }
1084               lra_update_insn_recog_data (insn);
1085               /* This can't have an effect on elimination offsets, so skip
1086                  right to the end.  */
1087               return;
1088             }
1089         }
1090     }
1091
1092   /* Eliminate all eliminable registers occurring in operands that
1093      can be handled by the constraint pass.  */
1094   id = lra_get_insn_recog_data (insn);
1095   static_id = id->insn_static_data;
1096   validate_p = false;
1097   for (i = 0; i < static_id->n_operands; i++)
1098     {
1099       orig_operand[i] = *id->operand_loc[i];
1100       substed_operand[i] = *id->operand_loc[i];
1101
1102       /* For an asm statement, every operand is eliminable.  */
1103       if (icode < 0 || insn_data[icode].operand[i].eliminable)
1104         {
1105           /* Check for setting a hard register that we know about.  */
1106           if (static_id->operand[i].type != OP_IN
1107               && REG_P (orig_operand[i]))
1108             {
1109               /* If we are assigning to a hard register that can be
1110                  eliminated, it must be as part of a PARALLEL, since
1111                  the code above handles single SETs.  This reg can not
1112                  be longer eliminated -- it is forced by
1113                  mark_not_eliminable.  */
1114               for (ep = reg_eliminate;
1115                    ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
1116                    ep++)
1117                 lra_assert (ep->from_rtx != orig_operand[i]
1118                             || ! ep->can_eliminate);
1119             }
1120
1121           /* Companion to the above plus substitution, we can allow
1122              invariants as the source of a plain move.  */
1123           substed_operand[i]
1124             = lra_eliminate_regs_1 (insn, *id->operand_loc[i], VOIDmode,
1125                                     replace_p, ! replace_p && ! first_p,
1126                                     update_sp_offset, first_p);
1127           if (substed_operand[i] != orig_operand[i])
1128             validate_p = true;
1129         }
1130     }
1131
1132   if (! validate_p)
1133     return;
1134
1135   /* Substitute the operands; the new values are in the substed_operand
1136      array.  */
1137   for (i = 0; i < static_id->n_operands; i++)
1138     *id->operand_loc[i] = substed_operand[i];
1139   for (i = 0; i < static_id->n_dups; i++)
1140     *id->dup_loc[i] = substed_operand[(int) static_id->dup_num[i]];
1141
1142   /* If we had a move insn but now we don't, re-recognize it.
1143      This will cause spurious re-recognition if the old move had a
1144      PARALLEL since the new one still will, but we can't call
1145      single_set without having put new body into the insn and the
1146      re-recognition won't hurt in this rare case.  */
1147   id = lra_update_insn_recog_data (insn);
1148   static_id = id->insn_static_data;
1149 }
1150
1151 /* Spill pseudos which are assigned to hard registers in SET.  Add
1152    affected insns for processing in the subsequent constraint
1153    pass.  */
1154 static void
1155 spill_pseudos (HARD_REG_SET set)
1156 {
1157   int i;
1158   bitmap_head to_process;
1159   rtx_insn *insn;
1160
1161   if (hard_reg_set_empty_p (set))
1162     return;
1163   if (lra_dump_file != NULL)
1164     {
1165       fprintf (lra_dump_file, "    Spilling non-eliminable hard regs:");
1166       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1167         if (TEST_HARD_REG_BIT (set, i))
1168           fprintf (lra_dump_file, " %d", i);
1169       fprintf (lra_dump_file, "\n");
1170     }
1171   bitmap_initialize (&to_process, &reg_obstack);
1172   for (i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
1173     if (lra_reg_info[i].nrefs != 0 && reg_renumber[i] >= 0
1174         && overlaps_hard_reg_set_p (set,
1175                                     PSEUDO_REGNO_MODE (i), reg_renumber[i]))
1176       {
1177         if (lra_dump_file != NULL)
1178           fprintf (lra_dump_file, "      Spilling r%d(%d)\n",
1179                    i, reg_renumber[i]);
1180         reg_renumber[i] = -1;
1181         bitmap_ior_into (&to_process, &lra_reg_info[i].insn_bitmap);
1182       }
1183   IOR_HARD_REG_SET (lra_no_alloc_regs, set);
1184   for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
1185     if (bitmap_bit_p (&to_process, INSN_UID (insn)))
1186       {
1187         lra_push_insn (insn);
1188         lra_set_used_insn_alternative (insn, -1);
1189       }
1190   bitmap_clear (&to_process);
1191 }
1192
1193 /* Update all offsets and possibility for elimination on eliminable
1194    registers.  Spill pseudos assigned to registers which are
1195    uneliminable, update LRA_NO_ALLOC_REGS and ELIMINABLE_REG_SET.  Add
1196    insns to INSNS_WITH_CHANGED_OFFSETS containing eliminable hard
1197    registers whose offsets should be changed.  Return true if any
1198    elimination offset changed.  */
1199 static bool
1200 update_reg_eliminate (bitmap insns_with_changed_offsets)
1201 {
1202   bool prev, result;
1203   struct lra_elim_table *ep, *ep1;
1204   HARD_REG_SET temp_hard_reg_set;
1205
1206   /* Clear self elimination offsets.  */
1207   for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1208     self_elim_offsets[ep->from] = 0;
1209   for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1210     {
1211       /* If it is a currently used elimination: update the previous
1212          offset.  */
1213       if (elimination_map[ep->from] == ep)
1214         ep->previous_offset = ep->offset;
1215
1216       prev = ep->prev_can_eliminate;
1217       setup_can_eliminate (ep, targetm.can_eliminate (ep->from, ep->to));
1218       if (ep->can_eliminate && ! prev)
1219         {
1220           /* It is possible that not eliminable register becomes
1221              eliminable because we took other reasons into account to
1222              set up eliminable regs in the initial set up.  Just
1223              ignore new eliminable registers.  */
1224           setup_can_eliminate (ep, false);
1225           continue;
1226         }
1227       if (ep->can_eliminate != prev && elimination_map[ep->from] == ep)
1228         {
1229           /* We cannot use this elimination anymore -- find another
1230              one.  */
1231           if (lra_dump_file != NULL)
1232             fprintf (lra_dump_file,
1233                      "  Elimination %d to %d is not possible anymore\n",
1234                      ep->from, ep->to);
1235           /* If after processing RTL we decides that SP can be used as
1236              a result of elimination, it can not be changed.  */
1237           gcc_assert ((ep->to_rtx != stack_pointer_rtx)
1238                       || (ep->from < FIRST_PSEUDO_REGISTER
1239                           && fixed_regs [ep->from]));
1240           /* Mark that is not eliminable anymore.  */
1241           elimination_map[ep->from] = NULL;
1242           for (ep1 = ep + 1; ep1 < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep1++)
1243             if (ep1->can_eliminate && ep1->from == ep->from)
1244               break;
1245           if (ep1 < &reg_eliminate[NUM_ELIMINABLE_REGS])
1246             {
1247               if (lra_dump_file != NULL)
1248                 fprintf (lra_dump_file, "    Using elimination %d to %d now\n",
1249                          ep1->from, ep1->to);
1250               lra_assert (ep1->previous_offset == 0);
1251               ep1->previous_offset = ep->offset;
1252             }
1253           else
1254             {
1255               /* There is no elimination anymore just use the hard
1256                  register `from' itself.  Setup self elimination
1257                  offset to restore the original offset values.  */
1258               if (lra_dump_file != NULL)
1259                 fprintf (lra_dump_file, "    %d is not eliminable at all\n",
1260                          ep->from);
1261               self_elim_offsets[ep->from] = -ep->offset;
1262               if (ep->offset != 0)
1263                 bitmap_ior_into (insns_with_changed_offsets,
1264                                  &lra_reg_info[ep->from].insn_bitmap);
1265             }
1266         }
1267
1268 #ifdef ELIMINABLE_REGS
1269       INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->offset);
1270 #else
1271       INITIAL_FRAME_POINTER_OFFSET (ep->offset);
1272 #endif
1273     }
1274   setup_elimination_map ();
1275   result = false;
1276   CLEAR_HARD_REG_SET (temp_hard_reg_set);
1277   for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1278     if (elimination_map[ep->from] == NULL)
1279       SET_HARD_REG_BIT (temp_hard_reg_set, ep->from);
1280     else if (elimination_map[ep->from] == ep)
1281       {
1282         /* Prevent the hard register into which we eliminate from
1283            the usage for pseudos.  */
1284         if (ep->from != ep->to)
1285           SET_HARD_REG_BIT (temp_hard_reg_set, ep->to);
1286         if (ep->previous_offset != ep->offset)
1287           {
1288             bitmap_ior_into (insns_with_changed_offsets,
1289                              &lra_reg_info[ep->from].insn_bitmap);
1290
1291             /* Update offset when the eliminate offset have been
1292                changed.  */
1293             lra_update_reg_val_offset (lra_reg_info[ep->from].val,
1294                                        ep->offset - ep->previous_offset);
1295             result = true;
1296           }
1297       }
1298   IOR_HARD_REG_SET (lra_no_alloc_regs, temp_hard_reg_set);
1299   AND_COMPL_HARD_REG_SET (eliminable_regset, temp_hard_reg_set);
1300   spill_pseudos (temp_hard_reg_set);
1301   return result;
1302 }
1303
1304 /* Initialize the table of hard registers to eliminate.
1305    Pre-condition: global flag frame_pointer_needed has been set before
1306    calling this function.  */
1307 static void
1308 init_elim_table (void)
1309 {
1310   struct lra_elim_table *ep;
1311 #ifdef ELIMINABLE_REGS
1312   bool value_p;
1313   const struct elim_table_1 *ep1;
1314 #endif
1315
1316   if (!reg_eliminate)
1317     reg_eliminate = XCNEWVEC (struct lra_elim_table, NUM_ELIMINABLE_REGS);
1318
1319   memset (self_elim_offsets, 0, sizeof (self_elim_offsets));
1320   /* Initiate member values which will be never changed.  */
1321   self_elim_table.can_eliminate = self_elim_table.prev_can_eliminate = true;
1322   self_elim_table.previous_offset = 0;
1323 #ifdef ELIMINABLE_REGS
1324   for (ep = reg_eliminate, ep1 = reg_eliminate_1;
1325        ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
1326     {
1327       ep->offset = ep->previous_offset = 0;
1328       ep->from = ep1->from;
1329       ep->to = ep1->to;
1330       value_p = (targetm.can_eliminate (ep->from, ep->to)
1331                  && ! (ep->to == STACK_POINTER_REGNUM
1332                        && frame_pointer_needed
1333                        && (! SUPPORTS_STACK_ALIGNMENT
1334                            || ! stack_realign_fp)));
1335       setup_can_eliminate (ep, value_p);
1336     }
1337 #else
1338   reg_eliminate[0].offset = reg_eliminate[0].previous_offset = 0;
1339   reg_eliminate[0].from = reg_eliminate_1[0].from;
1340   reg_eliminate[0].to = reg_eliminate_1[0].to;
1341   setup_can_eliminate (&reg_eliminate[0], ! frame_pointer_needed);
1342 #endif
1343
1344   /* Build the FROM and TO REG rtx's.  Note that code in gen_rtx_REG
1345      will cause, e.g., gen_rtx_REG (Pmode, STACK_POINTER_REGNUM) to
1346      equal stack_pointer_rtx.  We depend on this. Threfore we switch
1347      off that we are in LRA temporarily.  */
1348   lra_in_progress = 0;
1349   for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1350     {
1351       ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
1352       ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
1353       eliminable_reg_rtx[ep->from] = ep->from_rtx;
1354     }
1355   lra_in_progress = 1;
1356 }
1357
1358 /* Function for initialization of elimination once per function.  It
1359    sets up sp offset for each insn.  */
1360 static void
1361 init_elimination (void)
1362 {
1363   bool stop_to_sp_elimination_p;
1364   basic_block bb;
1365   rtx_insn *insn;
1366   struct lra_elim_table *ep;
1367
1368   init_elim_table ();
1369   FOR_EACH_BB_FN (bb, cfun)
1370     {
1371       curr_sp_change = 0;
1372       stop_to_sp_elimination_p = false;
1373       FOR_BB_INSNS (bb, insn)
1374         if (INSN_P (insn))
1375           {
1376             lra_get_insn_recog_data (insn)->sp_offset = curr_sp_change;
1377             if (NONDEBUG_INSN_P (insn))
1378               {
1379                 mark_not_eliminable (PATTERN (insn), VOIDmode);
1380                 if (curr_sp_change != 0
1381                     && find_reg_note (insn, REG_LABEL_OPERAND, NULL_RTX))
1382                   stop_to_sp_elimination_p = true;
1383               }
1384           }
1385       if (! frame_pointer_needed
1386           && (curr_sp_change != 0 || stop_to_sp_elimination_p)
1387           && bb->succs && bb->succs->length () != 0)
1388         for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1389           if (ep->to == STACK_POINTER_REGNUM)
1390             setup_can_eliminate (ep, false);
1391     }
1392   setup_elimination_map ();
1393 }
1394
1395 /* Eliminate hard reg given by its location LOC.  */
1396 void
1397 lra_eliminate_reg_if_possible (rtx *loc)
1398 {
1399   int regno;
1400   struct lra_elim_table *ep;
1401
1402   lra_assert (REG_P (*loc));
1403   if ((regno = REGNO (*loc)) >= FIRST_PSEUDO_REGISTER
1404       || ! TEST_HARD_REG_BIT (lra_no_alloc_regs, regno))
1405     return;
1406   if ((ep = get_elimination (*loc)) != NULL)
1407     *loc = ep->to_rtx;
1408 }
1409
1410 /* Do (final if FINAL_P or first if FIRST_P) elimination in INSN.  Add
1411    the insn for subsequent processing in the constraint pass, update
1412    the insn info.  */
1413 static void
1414 process_insn_for_elimination (rtx_insn *insn, bool final_p, bool first_p)
1415 {
1416   eliminate_regs_in_insn (insn, final_p, first_p, 0);
1417   if (! final_p)
1418     {
1419       /* Check that insn changed its code.  This is a case when a move
1420          insn becomes an add insn and we do not want to process the
1421          insn as a move anymore.  */
1422       int icode = recog (PATTERN (insn), insn, 0);
1423
1424       if (icode >= 0 && icode != INSN_CODE (insn))
1425         {
1426           INSN_CODE (insn) = icode;
1427           lra_update_insn_recog_data (insn);
1428         }
1429       lra_update_insn_regno_info (insn);
1430       lra_push_insn (insn);
1431       lra_set_used_insn_alternative (insn, -1);
1432     }
1433 }
1434
1435 /* Entry function to do final elimination if FINAL_P or to update
1436    elimination register offsets (FIRST_P if we are doing it the first
1437    time).  */
1438 void
1439 lra_eliminate (bool final_p, bool first_p)
1440 {
1441   unsigned int uid;
1442   bitmap_head insns_with_changed_offsets;
1443   bitmap_iterator bi;
1444   struct lra_elim_table *ep;
1445
1446   gcc_assert (! final_p || ! first_p);
1447
1448   timevar_push (TV_LRA_ELIMINATE);
1449
1450   if (first_p)
1451     init_elimination ();
1452
1453   bitmap_initialize (&insns_with_changed_offsets, &reg_obstack);
1454   if (final_p)
1455     {
1456       if (flag_checking)
1457         {
1458           update_reg_eliminate (&insns_with_changed_offsets);
1459           gcc_assert (bitmap_empty_p (&insns_with_changed_offsets));
1460         }
1461       /* We change eliminable hard registers in insns so we should do
1462          this for all insns containing any eliminable hard
1463          register.  */
1464       for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1465         if (elimination_map[ep->from] != NULL)
1466           bitmap_ior_into (&insns_with_changed_offsets,
1467                            &lra_reg_info[ep->from].insn_bitmap);
1468     }
1469   else if (! update_reg_eliminate (&insns_with_changed_offsets))
1470     goto lra_eliminate_done;
1471   if (lra_dump_file != NULL)
1472     {
1473       fprintf (lra_dump_file, "New elimination table:\n");
1474       print_elim_table (lra_dump_file);
1475     }
1476   EXECUTE_IF_SET_IN_BITMAP (&insns_with_changed_offsets, 0, uid, bi)
1477     /* A dead insn can be deleted in process_insn_for_elimination.  */
1478     if (lra_insn_recog_data[uid] != NULL)
1479       process_insn_for_elimination (lra_insn_recog_data[uid]->insn,
1480                                     final_p, first_p);
1481   bitmap_clear (&insns_with_changed_offsets);
1482
1483 lra_eliminate_done:
1484   timevar_pop (TV_LRA_ELIMINATE);
1485 }