Fix bootstrap/PR63632
[official-gcc.git] / gcc / lra-eliminations.c
blob3133e9aa2e2274aa39e447e4bae119a8c51402ff
1 /* Code for RTL register eliminations.
2 Copyright (C) 2010-2014 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
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.
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.
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/>. */
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.
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.
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).
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:
50 fp + 42 => sp + 42
54 #include "config.h"
55 #include "system.h"
56 #include "coretypes.h"
57 #include "tm.h"
58 #include "hard-reg-set.h"
59 #include "rtl.h"
60 #include "tm_p.h"
61 #include "regs.h"
62 #include "insn-config.h"
63 #include "insn-codes.h"
64 #include "recog.h"
65 #include "output.h"
66 #include "addresses.h"
67 #include "target.h"
68 #include "hashtab.h"
69 #include "hash-set.h"
70 #include "vec.h"
71 #include "machmode.h"
72 #include "input.h"
73 #include "function.h"
74 #include "expr.h"
75 #include "basic-block.h"
76 #include "except.h"
77 #include "optabs.h"
78 #include "df.h"
79 #include "ira.h"
80 #include "rtl-error.h"
81 #include "lra-int.h"
83 /* This structure is used to record information about hard register
84 eliminations. */
85 struct lra_elim_table
87 /* Hard register number to be eliminated. */
88 int from;
89 /* Hard register number used as replacement. */
90 int to;
91 /* Difference between values of the two hard registers above on
92 previous iteration. */
93 HOST_WIDE_INT previous_offset;
94 /* Difference between the values on the current iteration. */
95 HOST_WIDE_INT offset;
96 /* Nonzero if this elimination can be done. */
97 bool can_eliminate;
98 /* CAN_ELIMINATE since the last check. */
99 bool prev_can_eliminate;
100 /* REG rtx for the register to be eliminated. We cannot simply
101 compare the number since we might then spuriously replace a hard
102 register corresponding to a pseudo assigned to the reg to be
103 eliminated. */
104 rtx from_rtx;
105 /* REG rtx for the replacement. */
106 rtx to_rtx;
109 /* The elimination table. Each array entry describes one possible way
110 of eliminating a register in favor of another. If there is more
111 than one way of eliminating a particular register, the most
112 preferred should be specified first. */
113 static struct lra_elim_table *reg_eliminate = 0;
115 /* This is an intermediate structure to initialize the table. It has
116 exactly the members provided by ELIMINABLE_REGS. */
117 static const struct elim_table_1
119 const int from;
120 const int to;
121 } reg_eliminate_1[] =
123 /* If a set of eliminable hard registers was specified, define the
124 table from it. Otherwise, default to the normal case of the frame
125 pointer being replaced by the stack pointer. */
127 #ifdef ELIMINABLE_REGS
128 ELIMINABLE_REGS;
129 #else
130 {{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}};
131 #endif
133 #define NUM_ELIMINABLE_REGS ARRAY_SIZE (reg_eliminate_1)
135 /* Print info about elimination table to file F. */
136 static void
137 print_elim_table (FILE *f)
139 struct lra_elim_table *ep;
141 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
142 fprintf (f, "%s eliminate %d to %d (offset=" HOST_WIDE_INT_PRINT_DEC
143 ", prev_offset=" HOST_WIDE_INT_PRINT_DEC ")\n",
144 ep->can_eliminate ? "Can" : "Can't",
145 ep->from, ep->to, ep->offset, ep->previous_offset);
148 /* Print info about elimination table to stderr. */
149 void
150 lra_debug_elim_table (void)
152 print_elim_table (stderr);
155 /* Setup possibility of elimination in elimination table element EP to
156 VALUE. Setup FRAME_POINTER_NEEDED if elimination from frame
157 pointer to stack pointer is not possible anymore. */
158 static void
159 setup_can_eliminate (struct lra_elim_table *ep, bool value)
161 ep->can_eliminate = ep->prev_can_eliminate = value;
162 if (! value
163 && ep->from == FRAME_POINTER_REGNUM && ep->to == STACK_POINTER_REGNUM)
164 frame_pointer_needed = 1;
167 /* Map: eliminable "from" register -> its current elimination,
168 or NULL if none. The elimination table may contain more than
169 one elimination for the same hard register, but this map specifies
170 the one that we are currently using. */
171 static struct lra_elim_table *elimination_map[FIRST_PSEUDO_REGISTER];
173 /* When an eliminable hard register becomes not eliminable, we use the
174 following special structure to restore original offsets for the
175 register. */
176 static struct lra_elim_table self_elim_table;
178 /* Offsets should be used to restore original offsets for eliminable
179 hard register which just became not eliminable. Zero,
180 otherwise. */
181 static HOST_WIDE_INT self_elim_offsets[FIRST_PSEUDO_REGISTER];
183 /* Map: hard regno -> RTL presentation. RTL presentations of all
184 potentially eliminable hard registers are stored in the map. */
185 static rtx eliminable_reg_rtx[FIRST_PSEUDO_REGISTER];
187 /* Set up ELIMINATION_MAP of the currently used eliminations. */
188 static void
189 setup_elimination_map (void)
191 int i;
192 struct lra_elim_table *ep;
194 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
195 elimination_map[i] = NULL;
196 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
197 if (ep->can_eliminate && elimination_map[ep->from] == NULL)
198 elimination_map[ep->from] = ep;
203 /* Compute the sum of X and Y, making canonicalizations assumed in an
204 address, namely: sum constant integers, surround the sum of two
205 constants with a CONST, put the constant as the second operand, and
206 group the constant on the outermost sum.
208 This routine assumes both inputs are already in canonical form. */
209 static rtx
210 form_sum (rtx x, rtx y)
212 rtx tem;
213 enum machine_mode mode = GET_MODE (x);
215 if (mode == VOIDmode)
216 mode = GET_MODE (y);
218 if (mode == VOIDmode)
219 mode = Pmode;
221 if (CONST_INT_P (x))
222 return plus_constant (mode, y, INTVAL (x));
223 else if (CONST_INT_P (y))
224 return plus_constant (mode, x, INTVAL (y));
225 else if (CONSTANT_P (x))
226 tem = x, x = y, y = tem;
228 if (GET_CODE (x) == PLUS && CONSTANT_P (XEXP (x, 1)))
229 return form_sum (XEXP (x, 0), form_sum (XEXP (x, 1), y));
231 /* Note that if the operands of Y are specified in the opposite
232 order in the recursive calls below, infinite recursion will
233 occur. */
234 if (GET_CODE (y) == PLUS && CONSTANT_P (XEXP (y, 1)))
235 return form_sum (form_sum (x, XEXP (y, 0)), XEXP (y, 1));
237 /* If both constant, encapsulate sum. Otherwise, just form sum. A
238 constant will have been placed second. */
239 if (CONSTANT_P (x) && CONSTANT_P (y))
241 if (GET_CODE (x) == CONST)
242 x = XEXP (x, 0);
243 if (GET_CODE (y) == CONST)
244 y = XEXP (y, 0);
246 return gen_rtx_CONST (VOIDmode, gen_rtx_PLUS (mode, x, y));
249 return gen_rtx_PLUS (mode, x, y);
252 /* Return the current substitution hard register of the elimination of
253 HARD_REGNO. If HARD_REGNO is not eliminable, return itself. */
255 lra_get_elimination_hard_regno (int hard_regno)
257 struct lra_elim_table *ep;
259 if (hard_regno < 0 || hard_regno >= FIRST_PSEUDO_REGISTER)
260 return hard_regno;
261 if ((ep = elimination_map[hard_regno]) == NULL)
262 return hard_regno;
263 return ep->to;
266 /* Return elimination which will be used for hard reg REG, NULL
267 otherwise. */
268 static struct lra_elim_table *
269 get_elimination (rtx reg)
271 int hard_regno;
272 struct lra_elim_table *ep;
273 HOST_WIDE_INT offset;
275 lra_assert (REG_P (reg));
276 if ((hard_regno = REGNO (reg)) < 0 || hard_regno >= FIRST_PSEUDO_REGISTER)
277 return NULL;
278 if ((ep = elimination_map[hard_regno]) != NULL)
279 return ep->from_rtx != reg ? NULL : ep;
280 if ((offset = self_elim_offsets[hard_regno]) == 0)
281 return NULL;
282 /* This is an iteration to restore offsets just after HARD_REGNO
283 stopped to be eliminable. */
284 self_elim_table.from = self_elim_table.to = hard_regno;
285 self_elim_table.from_rtx
286 = self_elim_table.to_rtx
287 = eliminable_reg_rtx[hard_regno];
288 lra_assert (self_elim_table.from_rtx != NULL);
289 self_elim_table.offset = offset;
290 return &self_elim_table;
293 /* Scan X and replace any eliminable registers (such as fp) with a
294 replacement (such as sp) if SUBST_P, plus an offset. The offset is
295 a change in the offset between the eliminable register and its
296 substitution if UPDATE_P, or the full offset if FULL_P, or
297 otherwise zero. If FULL_P, we also use the SP offsets for
298 elimination to SP.
300 MEM_MODE is the mode of an enclosing MEM. We need this to know how
301 much to adjust a register for, e.g., PRE_DEC. Also, if we are
302 inside a MEM, we are allowed to replace a sum of a hard register
303 and the constant zero with the hard register, which we cannot do
304 outside a MEM. In addition, we need to record the fact that a
305 hard register is referenced outside a MEM.
307 If we make full substitution to SP for non-null INSN, add the insn
308 sp offset. */
310 lra_eliminate_regs_1 (rtx_insn *insn, rtx x, enum machine_mode mem_mode,
311 bool subst_p, bool update_p, bool full_p)
313 enum rtx_code code = GET_CODE (x);
314 struct lra_elim_table *ep;
315 rtx new_rtx;
316 int i, j;
317 const char *fmt;
318 int copied = 0;
320 gcc_assert (!update_p || !full_p);
321 if (! current_function_decl)
322 return x;
324 switch (code)
326 CASE_CONST_ANY:
327 case CONST:
328 case SYMBOL_REF:
329 case CODE_LABEL:
330 case PC:
331 case CC0:
332 case ASM_INPUT:
333 case ADDR_VEC:
334 case ADDR_DIFF_VEC:
335 case RETURN:
336 return x;
338 case REG:
339 /* First handle the case where we encounter a bare hard register
340 that is eliminable. Replace it with a PLUS. */
341 if ((ep = get_elimination (x)) != NULL)
343 rtx to = subst_p ? ep->to_rtx : ep->from_rtx;
345 if (update_p)
346 return plus_constant (Pmode, to, ep->offset - ep->previous_offset);
347 else if (full_p)
348 return plus_constant (Pmode, to,
349 ep->offset
350 - (insn != NULL_RTX
351 && ep->to_rtx == stack_pointer_rtx
352 ? lra_get_insn_recog_data (insn)->sp_offset
353 : 0));
354 else
355 return to;
357 return x;
359 case PLUS:
360 /* If this is the sum of an eliminable register and a constant, rework
361 the sum. */
362 if (REG_P (XEXP (x, 0)) && CONSTANT_P (XEXP (x, 1)))
364 if ((ep = get_elimination (XEXP (x, 0))) != NULL)
366 HOST_WIDE_INT offset;
367 rtx to = subst_p ? ep->to_rtx : ep->from_rtx;
369 if (! update_p && ! full_p)
370 return gen_rtx_PLUS (Pmode, to, XEXP (x, 1));
372 offset = (update_p
373 ? ep->offset - ep->previous_offset : ep->offset);
374 if (full_p && insn != NULL_RTX && ep->to_rtx == stack_pointer_rtx)
375 offset -= lra_get_insn_recog_data (insn)->sp_offset;
376 if (CONST_INT_P (XEXP (x, 1))
377 && INTVAL (XEXP (x, 1)) == -offset)
378 return to;
379 else
380 return gen_rtx_PLUS (Pmode, to,
381 plus_constant (Pmode,
382 XEXP (x, 1), offset));
385 /* If the hard register is not eliminable, we are done since
386 the other operand is a constant. */
387 return x;
390 /* If this is part of an address, we want to bring any constant
391 to the outermost PLUS. We will do this by doing hard
392 register replacement in our operands and seeing if a constant
393 shows up in one of them.
395 Note that there is no risk of modifying the structure of the
396 insn, since we only get called for its operands, thus we are
397 either modifying the address inside a MEM, or something like
398 an address operand of a load-address insn. */
401 rtx new0 = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
402 subst_p, update_p, full_p);
403 rtx new1 = lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
404 subst_p, update_p, full_p);
406 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
407 return form_sum (new0, new1);
409 return x;
411 case MULT:
412 /* If this is the product of an eliminable hard register and a
413 constant, apply the distribute law and move the constant out
414 so that we have (plus (mult ..) ..). This is needed in order
415 to keep load-address insns valid. This case is pathological.
416 We ignore the possibility of overflow here. */
417 if (REG_P (XEXP (x, 0)) && CONST_INT_P (XEXP (x, 1))
418 && (ep = get_elimination (XEXP (x, 0))) != NULL)
420 rtx to = subst_p ? ep->to_rtx : ep->from_rtx;
422 if (update_p)
423 return
424 plus_constant (Pmode,
425 gen_rtx_MULT (Pmode, to, XEXP (x, 1)),
426 (ep->offset - ep->previous_offset)
427 * INTVAL (XEXP (x, 1)));
428 else if (full_p)
430 HOST_WIDE_INT offset = ep->offset;
432 if (insn != NULL_RTX && ep->to_rtx == stack_pointer_rtx)
433 offset -= lra_get_insn_recog_data (insn)->sp_offset;
434 return
435 plus_constant (Pmode,
436 gen_rtx_MULT (Pmode, to, XEXP (x, 1)),
437 offset * INTVAL (XEXP (x, 1)));
439 else
440 return gen_rtx_MULT (Pmode, to, XEXP (x, 1));
443 /* ... fall through ... */
445 case CALL:
446 case COMPARE:
447 /* See comments before PLUS about handling MINUS. */
448 case MINUS:
449 case DIV: case UDIV:
450 case MOD: case UMOD:
451 case AND: case IOR: case XOR:
452 case ROTATERT: case ROTATE:
453 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
454 case NE: case EQ:
455 case GE: case GT: case GEU: case GTU:
456 case LE: case LT: case LEU: case LTU:
458 rtx new0 = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
459 subst_p, update_p, full_p);
460 rtx new1 = XEXP (x, 1)
461 ? lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
462 subst_p, update_p, full_p) : 0;
464 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
465 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
467 return x;
469 case EXPR_LIST:
470 /* If we have something in XEXP (x, 0), the usual case,
471 eliminate it. */
472 if (XEXP (x, 0))
474 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
475 subst_p, update_p, full_p);
476 if (new_rtx != XEXP (x, 0))
478 /* If this is a REG_DEAD note, it is not valid anymore.
479 Using the eliminated version could result in creating a
480 REG_DEAD note for the stack or frame pointer. */
481 if (REG_NOTE_KIND (x) == REG_DEAD)
482 return (XEXP (x, 1)
483 ? lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
484 subst_p, update_p, full_p)
485 : NULL_RTX);
487 x = alloc_reg_note (REG_NOTE_KIND (x), new_rtx, XEXP (x, 1));
491 /* ... fall through ... */
493 case INSN_LIST:
494 case INT_LIST:
495 /* Now do eliminations in the rest of the chain. If this was
496 an EXPR_LIST, this might result in allocating more memory than is
497 strictly needed, but it simplifies the code. */
498 if (XEXP (x, 1))
500 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
501 subst_p, update_p, full_p);
502 if (new_rtx != XEXP (x, 1))
503 return
504 gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x),
505 XEXP (x, 0), new_rtx);
507 return x;
509 case PRE_INC:
510 case POST_INC:
511 case PRE_DEC:
512 case POST_DEC:
513 /* We do not support elimination of a register that is modified.
514 elimination_effects has already make sure that this does not
515 happen. */
516 return x;
518 case PRE_MODIFY:
519 case POST_MODIFY:
520 /* We do not support elimination of a hard register that is
521 modified. LRA has already make sure that this does not
522 happen. The only remaining case we need to consider here is
523 that the increment value may be an eliminable register. */
524 if (GET_CODE (XEXP (x, 1)) == PLUS
525 && XEXP (XEXP (x, 1), 0) == XEXP (x, 0))
527 rtx new_rtx = lra_eliminate_regs_1 (insn, XEXP (XEXP (x, 1), 1),
528 mem_mode,
529 subst_p, update_p, full_p);
531 if (new_rtx != XEXP (XEXP (x, 1), 1))
532 return gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (x, 0),
533 gen_rtx_PLUS (GET_MODE (x),
534 XEXP (x, 0), new_rtx));
536 return x;
538 case STRICT_LOW_PART:
539 case NEG: case NOT:
540 case SIGN_EXTEND: case ZERO_EXTEND:
541 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
542 case FLOAT: case FIX:
543 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
544 case ABS:
545 case SQRT:
546 case FFS:
547 case CLZ:
548 case CTZ:
549 case POPCOUNT:
550 case PARITY:
551 case BSWAP:
552 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
553 subst_p, update_p, full_p);
554 if (new_rtx != XEXP (x, 0))
555 return gen_rtx_fmt_e (code, GET_MODE (x), new_rtx);
556 return x;
558 case SUBREG:
559 new_rtx = lra_eliminate_regs_1 (insn, SUBREG_REG (x), mem_mode,
560 subst_p, update_p, full_p);
562 if (new_rtx != SUBREG_REG (x))
564 int x_size = GET_MODE_SIZE (GET_MODE (x));
565 int new_size = GET_MODE_SIZE (GET_MODE (new_rtx));
567 if (MEM_P (new_rtx) && x_size <= new_size)
569 SUBREG_REG (x) = new_rtx;
570 alter_subreg (&x, false);
571 return x;
573 else if (! subst_p)
575 /* LRA can transform subregs itself. So don't call
576 simplify_gen_subreg until LRA transformations are
577 finished. Function simplify_gen_subreg can do
578 non-trivial transformations (like truncation) which
579 might make LRA work to fail. */
580 SUBREG_REG (x) = new_rtx;
581 return x;
583 else
584 return simplify_gen_subreg (GET_MODE (x), new_rtx,
585 GET_MODE (new_rtx), SUBREG_BYTE (x));
588 return x;
590 case MEM:
591 /* Our only special processing is to pass the mode of the MEM to our
592 recursive call and copy the flags. While we are here, handle this
593 case more efficiently. */
594 return
595 replace_equiv_address_nv
597 lra_eliminate_regs_1 (insn, XEXP (x, 0), GET_MODE (x),
598 subst_p, update_p, full_p));
600 case USE:
601 /* Handle insn_list USE that a call to a pure function may generate. */
602 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 0), VOIDmode,
603 subst_p, update_p, full_p);
604 if (new_rtx != XEXP (x, 0))
605 return gen_rtx_USE (GET_MODE (x), new_rtx);
606 return x;
608 case CLOBBER:
609 case SET:
610 gcc_unreachable ();
612 default:
613 break;
616 /* Process each of our operands recursively. If any have changed, make a
617 copy of the rtx. */
618 fmt = GET_RTX_FORMAT (code);
619 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
621 if (*fmt == 'e')
623 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, i), mem_mode,
624 subst_p, update_p, full_p);
625 if (new_rtx != XEXP (x, i) && ! copied)
627 x = shallow_copy_rtx (x);
628 copied = 1;
630 XEXP (x, i) = new_rtx;
632 else if (*fmt == 'E')
634 int copied_vec = 0;
635 for (j = 0; j < XVECLEN (x, i); j++)
637 new_rtx = lra_eliminate_regs_1 (insn, XVECEXP (x, i, j), mem_mode,
638 subst_p, update_p, full_p);
639 if (new_rtx != XVECEXP (x, i, j) && ! copied_vec)
641 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
642 XVEC (x, i)->elem);
643 if (! copied)
645 x = shallow_copy_rtx (x);
646 copied = 1;
648 XVEC (x, i) = new_v;
649 copied_vec = 1;
651 XVECEXP (x, i, j) = new_rtx;
656 return x;
659 /* This function is used externally in subsequent passes of GCC. It
660 always does a full elimination of X. */
662 lra_eliminate_regs (rtx x, enum machine_mode mem_mode,
663 rtx insn ATTRIBUTE_UNUSED)
665 return lra_eliminate_regs_1 (NULL, x, mem_mode, true, false, true);
668 /* Stack pointer offset before the current insn relative to one at the
669 func start. RTL insns can change SP explicitly. We keep the
670 changes from one insn to another through this variable. */
671 static HOST_WIDE_INT curr_sp_change;
673 /* Scan rtx X for references to elimination source or target registers
674 in contexts that would prevent the elimination from happening.
675 Update the table of eliminables to reflect the changed state.
676 MEM_MODE is the mode of an enclosing MEM rtx, or VOIDmode if not
677 within a MEM. */
678 static void
679 mark_not_eliminable (rtx x, enum machine_mode mem_mode)
681 enum rtx_code code = GET_CODE (x);
682 struct lra_elim_table *ep;
683 int i, j;
684 const char *fmt;
686 switch (code)
688 case PRE_INC:
689 case POST_INC:
690 case PRE_DEC:
691 case POST_DEC:
692 case POST_MODIFY:
693 case PRE_MODIFY:
694 if (XEXP (x, 0) == stack_pointer_rtx
695 && ((code != PRE_MODIFY && code != POST_MODIFY)
696 || (GET_CODE (XEXP (x, 1)) == PLUS
697 && XEXP (x, 0) == XEXP (XEXP (x, 1), 0)
698 && CONST_INT_P (XEXP (XEXP (x, 1), 1)))))
700 int size = GET_MODE_SIZE (mem_mode);
702 #ifdef PUSH_ROUNDING
703 /* If more bytes than MEM_MODE are pushed, account for
704 them. */
705 size = PUSH_ROUNDING (size);
706 #endif
707 if (code == PRE_DEC || code == POST_DEC)
708 curr_sp_change -= size;
709 else if (code == PRE_INC || code == POST_INC)
710 curr_sp_change += size;
711 else if (code == PRE_MODIFY || code == POST_MODIFY)
712 curr_sp_change += INTVAL (XEXP (XEXP (x, 1), 1));
714 else if (REG_P (XEXP (x, 0))
715 && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER)
717 /* If we modify the source of an elimination rule, disable
718 it. Do the same if it is the destination and not the
719 hard frame register. */
720 for (ep = reg_eliminate;
721 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
722 ep++)
723 if (ep->from_rtx == XEXP (x, 0)
724 || (ep->to_rtx == XEXP (x, 0)
725 && ep->to_rtx != hard_frame_pointer_rtx))
726 setup_can_eliminate (ep, false);
728 return;
730 case USE:
731 if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
732 /* If using a hard register that is the source of an eliminate
733 we still think can be performed, note it cannot be
734 performed since we don't know how this hard register is
735 used. */
736 for (ep = reg_eliminate;
737 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
738 ep++)
739 if (ep->from_rtx == XEXP (x, 0)
740 && ep->to_rtx != hard_frame_pointer_rtx)
741 setup_can_eliminate (ep, false);
742 return;
744 case CLOBBER:
745 if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
746 /* If clobbering a hard register that is the replacement
747 register for an elimination we still think can be
748 performed, note that it cannot be performed. Otherwise, we
749 need not be concerned about it. */
750 for (ep = reg_eliminate;
751 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
752 ep++)
753 if (ep->to_rtx == XEXP (x, 0)
754 && ep->to_rtx != hard_frame_pointer_rtx)
755 setup_can_eliminate (ep, false);
756 return;
758 case SET:
759 if (SET_DEST (x) == stack_pointer_rtx
760 && GET_CODE (SET_SRC (x)) == PLUS
761 && XEXP (SET_SRC (x), 0) == SET_DEST (x)
762 && CONST_INT_P (XEXP (SET_SRC (x), 1)))
764 curr_sp_change += INTVAL (XEXP (SET_SRC (x), 1));
765 return;
767 if (! REG_P (SET_DEST (x))
768 || REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER)
769 mark_not_eliminable (SET_DEST (x), mem_mode);
770 else
772 /* See if this is setting the replacement hard register for
773 an elimination.
775 If DEST is the hard frame pointer, we do nothing because
776 we assume that all assignments to the frame pointer are
777 for non-local gotos and are being done at a time when
778 they are valid and do not disturb anything else. Some
779 machines want to eliminate a fake argument pointer (or
780 even a fake frame pointer) with either the real frame
781 pointer or the stack pointer. Assignments to the hard
782 frame pointer must not prevent this elimination. */
783 for (ep = reg_eliminate;
784 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
785 ep++)
786 if (ep->to_rtx == SET_DEST (x)
787 && SET_DEST (x) != hard_frame_pointer_rtx)
788 setup_can_eliminate (ep, false);
791 mark_not_eliminable (SET_SRC (x), mem_mode);
792 return;
794 case MEM:
795 /* Our only special processing is to pass the mode of the MEM to
796 our recursive call. */
797 mark_not_eliminable (XEXP (x, 0), GET_MODE (x));
798 return;
800 default:
801 break;
804 fmt = GET_RTX_FORMAT (code);
805 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
807 if (*fmt == 'e')
808 mark_not_eliminable (XEXP (x, i), mem_mode);
809 else if (*fmt == 'E')
810 for (j = 0; j < XVECLEN (x, i); j++)
811 mark_not_eliminable (XVECEXP (x, i, j), mem_mode);
817 #ifdef HARD_FRAME_POINTER_REGNUM
819 /* Find offset equivalence note for reg WHAT in INSN and return the
820 found elmination offset. If the note is not found, return NULL.
821 Remove the found note. */
822 static rtx
823 remove_reg_equal_offset_note (rtx insn, rtx what)
825 rtx link, *link_loc;
827 for (link_loc = &REG_NOTES (insn);
828 (link = *link_loc) != NULL_RTX;
829 link_loc = &XEXP (link, 1))
830 if (REG_NOTE_KIND (link) == REG_EQUAL
831 && GET_CODE (XEXP (link, 0)) == PLUS
832 && XEXP (XEXP (link, 0), 0) == what
833 && CONST_INT_P (XEXP (XEXP (link, 0), 1)))
835 *link_loc = XEXP (link, 1);
836 return XEXP (XEXP (link, 0), 1);
838 return NULL_RTX;
841 #endif
843 /* Scan INSN and eliminate all eliminable hard registers in it.
845 If REPLACE_P is true, do the replacement destructively. Also
846 delete the insn as dead it if it is setting an eliminable register.
848 If REPLACE_P is false, just update the offsets while keeping the
849 base register the same. If FIRST_P, use the sp offset for
850 elimination to sp. Attach the note about used elimination for
851 insns setting frame pointer to update elimination easy (without
852 parsing already generated elimination insns to find offset
853 previously used) in future. */
855 static void
856 eliminate_regs_in_insn (rtx_insn *insn, bool replace_p, bool first_p)
858 int icode = recog_memoized (insn);
859 rtx old_set = single_set (insn);
860 bool validate_p;
861 int i;
862 rtx substed_operand[MAX_RECOG_OPERANDS];
863 rtx orig_operand[MAX_RECOG_OPERANDS];
864 struct lra_elim_table *ep;
865 rtx plus_src, plus_cst_src;
866 lra_insn_recog_data_t id;
867 struct lra_static_insn_data *static_id;
869 if (icode < 0 && asm_noperands (PATTERN (insn)) < 0 && ! DEBUG_INSN_P (insn))
871 lra_assert (GET_CODE (PATTERN (insn)) == USE
872 || GET_CODE (PATTERN (insn)) == CLOBBER
873 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
874 return;
877 /* Check for setting an eliminable register. */
878 if (old_set != 0 && REG_P (SET_DEST (old_set))
879 && (ep = get_elimination (SET_DEST (old_set))) != NULL)
881 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
882 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
884 bool delete_p = replace_p;
886 #ifdef HARD_FRAME_POINTER_REGNUM
887 if (ep->from == FRAME_POINTER_REGNUM
888 && ep->to == HARD_FRAME_POINTER_REGNUM)
889 /* If this is setting the frame pointer register to the
890 hardware frame pointer register and this is an
891 elimination that will be done (tested above), this
892 insn is really adjusting the frame pointer downward
893 to compensate for the adjustment done before a
894 nonlocal goto. */
896 rtx src = SET_SRC (old_set);
897 rtx off = remove_reg_equal_offset_note (insn, ep->to_rtx);
899 if (off != NULL_RTX
900 || src == ep->to_rtx
901 || (GET_CODE (src) == PLUS
902 && XEXP (src, 0) == ep->to_rtx
903 && CONST_INT_P (XEXP (src, 1))))
905 HOST_WIDE_INT offset;
907 if (replace_p)
909 SET_DEST (old_set) = ep->to_rtx;
910 lra_update_insn_recog_data (insn);
911 return;
913 offset = (off != NULL_RTX ? INTVAL (off)
914 : src == ep->to_rtx ? 0 : INTVAL (XEXP (src, 1)));
915 offset -= (ep->offset - ep->previous_offset);
916 src = plus_constant (Pmode, ep->to_rtx, offset);
918 /* First see if this insn remains valid when we
919 make the change. If not, keep the INSN_CODE
920 the same and let the constraint pass fit it
921 up. */
922 validate_change (insn, &SET_SRC (old_set), src, 1);
923 validate_change (insn, &SET_DEST (old_set),
924 ep->from_rtx, 1);
925 if (! apply_change_group ())
927 SET_SRC (old_set) = src;
928 SET_DEST (old_set) = ep->from_rtx;
930 lra_update_insn_recog_data (insn);
931 /* Add offset note for future updates. */
932 add_reg_note (insn, REG_EQUAL, src);
933 return;
936 #endif
938 /* This insn isn't serving a useful purpose. We delete it
939 when REPLACE is set. */
940 if (delete_p)
941 lra_delete_dead_insn (insn);
942 return;
946 /* We allow one special case which happens to work on all machines we
947 currently support: a single set with the source or a REG_EQUAL
948 note being a PLUS of an eliminable register and a constant. */
949 plus_src = plus_cst_src = 0;
950 if (old_set && REG_P (SET_DEST (old_set)))
952 if (GET_CODE (SET_SRC (old_set)) == PLUS)
953 plus_src = SET_SRC (old_set);
954 /* First see if the source is of the form (plus (...) CST). */
955 if (plus_src
956 && CONST_INT_P (XEXP (plus_src, 1)))
957 plus_cst_src = plus_src;
958 /* Check that the first operand of the PLUS is a hard reg or
959 the lowpart subreg of one. */
960 if (plus_cst_src)
962 rtx reg = XEXP (plus_cst_src, 0);
964 if (GET_CODE (reg) == SUBREG && subreg_lowpart_p (reg))
965 reg = SUBREG_REG (reg);
967 if (!REG_P (reg) || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
968 plus_cst_src = 0;
971 if (plus_cst_src)
973 rtx reg = XEXP (plus_cst_src, 0);
974 HOST_WIDE_INT offset = INTVAL (XEXP (plus_cst_src, 1));
976 if (GET_CODE (reg) == SUBREG)
977 reg = SUBREG_REG (reg);
979 if (REG_P (reg) && (ep = get_elimination (reg)) != NULL)
981 rtx to_rtx = replace_p ? ep->to_rtx : ep->from_rtx;
983 if (! replace_p)
985 offset += (ep->offset - ep->previous_offset);
986 if (first_p && ep->to_rtx == stack_pointer_rtx)
987 offset -= lra_get_insn_recog_data (insn)->sp_offset;
988 offset = trunc_int_for_mode (offset, GET_MODE (plus_cst_src));
991 if (GET_CODE (XEXP (plus_cst_src, 0)) == SUBREG)
992 to_rtx = gen_lowpart (GET_MODE (XEXP (plus_cst_src, 0)), to_rtx);
993 /* If we have a nonzero offset, and the source is already a
994 simple REG, the following transformation would increase
995 the cost of the insn by replacing a simple REG with (plus
996 (reg sp) CST). So try only when we already had a PLUS
997 before. */
998 if (offset == 0 || plus_src)
1000 rtx new_src = plus_constant (GET_MODE (to_rtx), to_rtx, offset);
1002 old_set = single_set (insn);
1004 /* First see if this insn remains valid when we make the
1005 change. If not, try to replace the whole pattern
1006 with a simple set (this may help if the original insn
1007 was a PARALLEL that was only recognized as single_set
1008 due to REG_UNUSED notes). If this isn't valid
1009 either, keep the INSN_CODE the same and let the
1010 constraint pass fix it up. */
1011 if (! validate_change (insn, &SET_SRC (old_set), new_src, 0))
1013 rtx new_pat = gen_rtx_SET (VOIDmode,
1014 SET_DEST (old_set), new_src);
1016 if (! validate_change (insn, &PATTERN (insn), new_pat, 0))
1017 SET_SRC (old_set) = new_src;
1019 lra_update_insn_recog_data (insn);
1020 /* This can't have an effect on elimination offsets, so skip
1021 right to the end. */
1022 return;
1027 /* Eliminate all eliminable registers occurring in operands that
1028 can be handled by the constraint pass. */
1029 id = lra_get_insn_recog_data (insn);
1030 static_id = id->insn_static_data;
1031 validate_p = false;
1032 for (i = 0; i < static_id->n_operands; i++)
1034 orig_operand[i] = *id->operand_loc[i];
1035 substed_operand[i] = *id->operand_loc[i];
1037 /* For an asm statement, every operand is eliminable. */
1038 if (icode < 0 || insn_data[icode].operand[i].eliminable)
1040 /* Check for setting a hard register that we know about. */
1041 if (static_id->operand[i].type != OP_IN
1042 && REG_P (orig_operand[i]))
1044 /* If we are assigning to a hard register that can be
1045 eliminated, it must be as part of a PARALLEL, since
1046 the code above handles single SETs. This reg can not
1047 be longer eliminated -- it is forced by
1048 mark_not_eliminable. */
1049 for (ep = reg_eliminate;
1050 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
1051 ep++)
1052 lra_assert (ep->from_rtx != orig_operand[i]
1053 || ! ep->can_eliminate);
1056 /* Companion to the above plus substitution, we can allow
1057 invariants as the source of a plain move. */
1058 substed_operand[i]
1059 = lra_eliminate_regs_1 (insn, *id->operand_loc[i], VOIDmode,
1060 replace_p, ! replace_p && ! first_p,
1061 first_p);
1062 if (substed_operand[i] != orig_operand[i])
1063 validate_p = true;
1067 if (! validate_p)
1068 return;
1070 /* Substitute the operands; the new values are in the substed_operand
1071 array. */
1072 for (i = 0; i < static_id->n_operands; i++)
1073 *id->operand_loc[i] = substed_operand[i];
1074 for (i = 0; i < static_id->n_dups; i++)
1075 *id->dup_loc[i] = substed_operand[(int) static_id->dup_num[i]];
1077 /* If we had a move insn but now we don't, re-recognize it.
1078 This will cause spurious re-recognition if the old move had a
1079 PARALLEL since the new one still will, but we can't call
1080 single_set without having put new body into the insn and the
1081 re-recognition won't hurt in this rare case. */
1082 id = lra_update_insn_recog_data (insn);
1083 static_id = id->insn_static_data;
1086 /* Spill pseudos which are assigned to hard registers in SET. Add
1087 affected insns for processing in the subsequent constraint
1088 pass. */
1089 static void
1090 spill_pseudos (HARD_REG_SET set)
1092 int i;
1093 bitmap_head to_process;
1094 rtx_insn *insn;
1096 if (hard_reg_set_empty_p (set))
1097 return;
1098 if (lra_dump_file != NULL)
1100 fprintf (lra_dump_file, " Spilling non-eliminable hard regs:");
1101 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1102 if (TEST_HARD_REG_BIT (set, i))
1103 fprintf (lra_dump_file, " %d", i);
1104 fprintf (lra_dump_file, "\n");
1106 bitmap_initialize (&to_process, &reg_obstack);
1107 for (i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
1108 if (lra_reg_info[i].nrefs != 0 && reg_renumber[i] >= 0
1109 && overlaps_hard_reg_set_p (set,
1110 PSEUDO_REGNO_MODE (i), reg_renumber[i]))
1112 if (lra_dump_file != NULL)
1113 fprintf (lra_dump_file, " Spilling r%d(%d)\n",
1114 i, reg_renumber[i]);
1115 reg_renumber[i] = -1;
1116 bitmap_ior_into (&to_process, &lra_reg_info[i].insn_bitmap);
1118 IOR_HARD_REG_SET (lra_no_alloc_regs, set);
1119 for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
1120 if (bitmap_bit_p (&to_process, INSN_UID (insn)))
1122 lra_push_insn (insn);
1123 lra_set_used_insn_alternative (insn, -1);
1125 bitmap_clear (&to_process);
1128 /* Update all offsets and possibility for elimination on eliminable
1129 registers. Spill pseudos assigned to registers which are
1130 uneliminable, update LRA_NO_ALLOC_REGS and ELIMINABLE_REG_SET. Add
1131 insns to INSNS_WITH_CHANGED_OFFSETS containing eliminable hard
1132 registers whose offsets should be changed. Return true if any
1133 elimination offset changed. */
1134 static bool
1135 update_reg_eliminate (bitmap insns_with_changed_offsets)
1137 bool prev, result;
1138 struct lra_elim_table *ep, *ep1;
1139 HARD_REG_SET temp_hard_reg_set;
1141 /* Clear self elimination offsets. */
1142 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1143 self_elim_offsets[ep->from] = 0;
1144 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1146 /* If it is a currently used elimination: update the previous
1147 offset. */
1148 if (elimination_map[ep->from] == ep)
1149 ep->previous_offset = ep->offset;
1151 prev = ep->prev_can_eliminate;
1152 setup_can_eliminate (ep, targetm.can_eliminate (ep->from, ep->to));
1153 if (ep->can_eliminate && ! prev)
1155 /* It is possible that not eliminable register becomes
1156 eliminable because we took other reasons into account to
1157 set up eliminable regs in the initial set up. Just
1158 ignore new eliminable registers. */
1159 setup_can_eliminate (ep, false);
1160 continue;
1162 if (ep->can_eliminate != prev && elimination_map[ep->from] == ep)
1164 /* We cannot use this elimination anymore -- find another
1165 one. */
1166 if (lra_dump_file != NULL)
1167 fprintf (lra_dump_file,
1168 " Elimination %d to %d is not possible anymore\n",
1169 ep->from, ep->to);
1170 /* If after processing RTL we decides that SP can be used as
1171 a result of elimination, it can not be changed. */
1172 gcc_assert (ep->to_rtx != stack_pointer_rtx);
1173 /* Mark that is not eliminable anymore. */
1174 elimination_map[ep->from] = NULL;
1175 for (ep1 = ep + 1; ep1 < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep1++)
1176 if (ep1->can_eliminate && ep1->from == ep->from)
1177 break;
1178 if (ep1 < &reg_eliminate[NUM_ELIMINABLE_REGS])
1180 if (lra_dump_file != NULL)
1181 fprintf (lra_dump_file, " Using elimination %d to %d now\n",
1182 ep1->from, ep1->to);
1183 lra_assert (ep1->previous_offset == 0);
1184 ep1->previous_offset = ep->offset;
1186 else
1188 /* There is no elimination anymore just use the hard
1189 register `from' itself. Setup self elimination
1190 offset to restore the original offset values. */
1191 if (lra_dump_file != NULL)
1192 fprintf (lra_dump_file, " %d is not eliminable at all\n",
1193 ep->from);
1194 self_elim_offsets[ep->from] = -ep->offset;
1195 if (ep->offset != 0)
1196 bitmap_ior_into (insns_with_changed_offsets,
1197 &lra_reg_info[ep->from].insn_bitmap);
1201 #ifdef ELIMINABLE_REGS
1202 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->offset);
1203 #else
1204 INITIAL_FRAME_POINTER_OFFSET (ep->offset);
1205 #endif
1207 setup_elimination_map ();
1208 result = false;
1209 CLEAR_HARD_REG_SET (temp_hard_reg_set);
1210 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1211 if (elimination_map[ep->from] == NULL)
1212 SET_HARD_REG_BIT (temp_hard_reg_set, ep->from);
1213 else if (elimination_map[ep->from] == ep)
1215 /* Prevent the hard register into which we eliminate from
1216 the usage for pseudos. */
1217 if (ep->from != ep->to)
1218 SET_HARD_REG_BIT (temp_hard_reg_set, ep->to);
1219 if (ep->previous_offset != ep->offset)
1221 bitmap_ior_into (insns_with_changed_offsets,
1222 &lra_reg_info[ep->from].insn_bitmap);
1224 /* Update offset when the eliminate offset have been
1225 changed. */
1226 lra_update_reg_val_offset (lra_reg_info[ep->from].val,
1227 ep->offset - ep->previous_offset);
1228 result = true;
1231 IOR_HARD_REG_SET (lra_no_alloc_regs, temp_hard_reg_set);
1232 AND_COMPL_HARD_REG_SET (eliminable_regset, temp_hard_reg_set);
1233 spill_pseudos (temp_hard_reg_set);
1234 return result;
1237 /* Initialize the table of hard registers to eliminate.
1238 Pre-condition: global flag frame_pointer_needed has been set before
1239 calling this function. */
1240 static void
1241 init_elim_table (void)
1243 struct lra_elim_table *ep;
1244 #ifdef ELIMINABLE_REGS
1245 bool value_p;
1246 const struct elim_table_1 *ep1;
1247 #endif
1249 if (!reg_eliminate)
1250 reg_eliminate = XCNEWVEC (struct lra_elim_table, NUM_ELIMINABLE_REGS);
1252 memset (self_elim_offsets, 0, sizeof (self_elim_offsets));
1253 /* Initiate member values which will be never changed. */
1254 self_elim_table.can_eliminate = self_elim_table.prev_can_eliminate = true;
1255 self_elim_table.previous_offset = 0;
1256 #ifdef ELIMINABLE_REGS
1257 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
1258 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
1260 ep->offset = ep->previous_offset = 0;
1261 ep->from = ep1->from;
1262 ep->to = ep1->to;
1263 value_p = (targetm.can_eliminate (ep->from, ep->to)
1264 && ! (ep->to == STACK_POINTER_REGNUM
1265 && frame_pointer_needed
1266 && (! SUPPORTS_STACK_ALIGNMENT
1267 || ! stack_realign_fp)));
1268 setup_can_eliminate (ep, value_p);
1270 #else
1271 reg_eliminate[0].offset = reg_eliminate[0].previous_offset = 0;
1272 reg_eliminate[0].from = reg_eliminate_1[0].from;
1273 reg_eliminate[0].to = reg_eliminate_1[0].to;
1274 setup_can_eliminate (&reg_eliminate[0], ! frame_pointer_needed);
1275 #endif
1277 /* Build the FROM and TO REG rtx's. Note that code in gen_rtx_REG
1278 will cause, e.g., gen_rtx_REG (Pmode, STACK_POINTER_REGNUM) to
1279 equal stack_pointer_rtx. We depend on this. Threfore we switch
1280 off that we are in LRA temporarily. */
1281 lra_in_progress = 0;
1282 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1284 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
1285 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
1286 eliminable_reg_rtx[ep->from] = ep->from_rtx;
1288 lra_in_progress = 1;
1291 /* Function for initialization of elimination once per function. It
1292 sets up sp offset for each insn. */
1293 static void
1294 init_elimination (void)
1296 bool stop_to_sp_elimination_p;
1297 basic_block bb;
1298 rtx_insn *insn;
1299 struct lra_elim_table *ep;
1301 init_elim_table ();
1302 FOR_EACH_BB_FN (bb, cfun)
1304 curr_sp_change = 0;
1305 stop_to_sp_elimination_p = false;
1306 FOR_BB_INSNS (bb, insn)
1307 if (INSN_P (insn))
1309 lra_get_insn_recog_data (insn)->sp_offset = curr_sp_change;
1310 if (NONDEBUG_INSN_P (insn))
1312 mark_not_eliminable (PATTERN (insn), VOIDmode);
1313 if (curr_sp_change != 0
1314 && find_reg_note (insn, REG_LABEL_OPERAND, NULL_RTX))
1315 stop_to_sp_elimination_p = true;
1318 if (! frame_pointer_needed
1319 && (curr_sp_change != 0 || stop_to_sp_elimination_p)
1320 && bb->succs && bb->succs->length () != 0)
1321 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1322 if (ep->to == STACK_POINTER_REGNUM)
1323 setup_can_eliminate (ep, false);
1325 setup_elimination_map ();
1328 /* Eliminate hard reg given by its location LOC. */
1329 void
1330 lra_eliminate_reg_if_possible (rtx *loc)
1332 int regno;
1333 struct lra_elim_table *ep;
1335 lra_assert (REG_P (*loc));
1336 if ((regno = REGNO (*loc)) >= FIRST_PSEUDO_REGISTER
1337 || ! TEST_HARD_REG_BIT (lra_no_alloc_regs, regno))
1338 return;
1339 if ((ep = get_elimination (*loc)) != NULL)
1340 *loc = ep->to_rtx;
1343 /* Do (final if FINAL_P or first if FIRST_P) elimination in INSN. Add
1344 the insn for subsequent processing in the constraint pass, update
1345 the insn info. */
1346 static void
1347 process_insn_for_elimination (rtx_insn *insn, bool final_p, bool first_p)
1349 eliminate_regs_in_insn (insn, final_p, first_p);
1350 if (! final_p)
1352 /* Check that insn changed its code. This is a case when a move
1353 insn becomes an add insn and we do not want to process the
1354 insn as a move anymore. */
1355 int icode = recog (PATTERN (insn), insn, 0);
1357 if (icode >= 0 && icode != INSN_CODE (insn))
1359 INSN_CODE (insn) = icode;
1360 lra_update_insn_recog_data (insn);
1362 lra_update_insn_regno_info (insn);
1363 lra_push_insn (insn);
1364 lra_set_used_insn_alternative (insn, -1);
1368 /* Entry function to do final elimination if FINAL_P or to update
1369 elimination register offsets (FIRST_P if we are doing it the first
1370 time). */
1371 void
1372 lra_eliminate (bool final_p, bool first_p)
1374 unsigned int uid;
1375 bitmap_head insns_with_changed_offsets;
1376 bitmap_iterator bi;
1377 struct lra_elim_table *ep;
1379 gcc_assert (! final_p || ! first_p);
1381 timevar_push (TV_LRA_ELIMINATE);
1383 if (first_p)
1384 init_elimination ();
1386 bitmap_initialize (&insns_with_changed_offsets, &reg_obstack);
1387 if (final_p)
1389 #ifdef ENABLE_CHECKING
1390 update_reg_eliminate (&insns_with_changed_offsets);
1391 if (! bitmap_empty_p (&insns_with_changed_offsets))
1392 gcc_unreachable ();
1393 #endif
1394 /* We change eliminable hard registers in insns so we should do
1395 this for all insns containing any eliminable hard
1396 register. */
1397 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1398 if (elimination_map[ep->from] != NULL)
1399 bitmap_ior_into (&insns_with_changed_offsets,
1400 &lra_reg_info[ep->from].insn_bitmap);
1402 else if (! update_reg_eliminate (&insns_with_changed_offsets))
1403 goto lra_eliminate_done;
1404 if (lra_dump_file != NULL)
1406 fprintf (lra_dump_file, "New elimination table:\n");
1407 print_elim_table (lra_dump_file);
1409 EXECUTE_IF_SET_IN_BITMAP (&insns_with_changed_offsets, 0, uid, bi)
1410 /* A dead insn can be deleted in process_insn_for_elimination. */
1411 if (lra_insn_recog_data[uid] != NULL)
1412 process_insn_for_elimination (lra_insn_recog_data[uid]->insn,
1413 final_p, first_p);
1414 bitmap_clear (&insns_with_changed_offsets);
1416 lra_eliminate_done:
1417 timevar_pop (TV_LRA_ELIMINATE);