2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / lra-eliminations.c
blobd8cbb478a48ea38c585eff9134477345a24c5bac
1 /* Code for RTL register eliminations.
2 Copyright (C) 2010-2015 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 "input.h"
69 #include "function.h"
70 #include "symtab.h"
71 #include "flags.h"
72 #include "alias.h"
73 #include "tree.h"
74 #include "expmed.h"
75 #include "dojump.h"
76 #include "explow.h"
77 #include "calls.h"
78 #include "emit-rtl.h"
79 #include "varasm.h"
80 #include "stmt.h"
81 #include "expr.h"
82 #include "predict.h"
83 #include "dominance.h"
84 #include "cfg.h"
85 #include "basic-block.h"
86 #include "except.h"
87 #include "optabs.h"
88 #include "df.h"
89 #include "ira.h"
90 #include "rtl-error.h"
91 #include "lra-int.h"
93 /* This structure is used to record information about hard register
94 eliminations. */
95 struct lra_elim_table
97 /* Hard register number to be eliminated. */
98 int from;
99 /* Hard register number used as replacement. */
100 int to;
101 /* Difference between values of the two hard registers above on
102 previous iteration. */
103 HOST_WIDE_INT previous_offset;
104 /* Difference between the values on the current iteration. */
105 HOST_WIDE_INT offset;
106 /* Nonzero if this elimination can be done. */
107 bool can_eliminate;
108 /* CAN_ELIMINATE since the last check. */
109 bool prev_can_eliminate;
110 /* REG rtx for the register to be eliminated. We cannot simply
111 compare the number since we might then spuriously replace a hard
112 register corresponding to a pseudo assigned to the reg to be
113 eliminated. */
114 rtx from_rtx;
115 /* REG rtx for the replacement. */
116 rtx to_rtx;
119 /* The elimination table. Each array entry describes one possible way
120 of eliminating a register in favor of another. If there is more
121 than one way of eliminating a particular register, the most
122 preferred should be specified first. */
123 static struct lra_elim_table *reg_eliminate = 0;
125 /* This is an intermediate structure to initialize the table. It has
126 exactly the members provided by ELIMINABLE_REGS. */
127 static const struct elim_table_1
129 const int from;
130 const int to;
131 } reg_eliminate_1[] =
133 /* If a set of eliminable hard registers was specified, define the
134 table from it. Otherwise, default to the normal case of the frame
135 pointer being replaced by the stack pointer. */
137 #ifdef ELIMINABLE_REGS
138 ELIMINABLE_REGS;
139 #else
140 {{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}};
141 #endif
143 #define NUM_ELIMINABLE_REGS ARRAY_SIZE (reg_eliminate_1)
145 /* Print info about elimination table to file F. */
146 static void
147 print_elim_table (FILE *f)
149 struct lra_elim_table *ep;
151 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
152 fprintf (f, "%s eliminate %d to %d (offset=" HOST_WIDE_INT_PRINT_DEC
153 ", prev_offset=" HOST_WIDE_INT_PRINT_DEC ")\n",
154 ep->can_eliminate ? "Can" : "Can't",
155 ep->from, ep->to, ep->offset, ep->previous_offset);
158 /* Print info about elimination table to stderr. */
159 void
160 lra_debug_elim_table (void)
162 print_elim_table (stderr);
165 /* Setup possibility of elimination in elimination table element EP to
166 VALUE. Setup FRAME_POINTER_NEEDED if elimination from frame
167 pointer to stack pointer is not possible anymore. */
168 static void
169 setup_can_eliminate (struct lra_elim_table *ep, bool value)
171 ep->can_eliminate = ep->prev_can_eliminate = value;
172 if (! value
173 && ep->from == FRAME_POINTER_REGNUM && ep->to == STACK_POINTER_REGNUM)
174 frame_pointer_needed = 1;
175 if (!frame_pointer_needed)
176 REGNO_POINTER_ALIGN (HARD_FRAME_POINTER_REGNUM) = 0;
179 /* Map: eliminable "from" register -> its current elimination,
180 or NULL if none. The elimination table may contain more than
181 one elimination for the same hard register, but this map specifies
182 the one that we are currently using. */
183 static struct lra_elim_table *elimination_map[FIRST_PSEUDO_REGISTER];
185 /* When an eliminable hard register becomes not eliminable, we use the
186 following special structure to restore original offsets for the
187 register. */
188 static struct lra_elim_table self_elim_table;
190 /* Offsets should be used to restore original offsets for eliminable
191 hard register which just became not eliminable. Zero,
192 otherwise. */
193 static HOST_WIDE_INT self_elim_offsets[FIRST_PSEUDO_REGISTER];
195 /* Map: hard regno -> RTL presentation. RTL presentations of all
196 potentially eliminable hard registers are stored in the map. */
197 static rtx eliminable_reg_rtx[FIRST_PSEUDO_REGISTER];
199 /* Set up ELIMINATION_MAP of the currently used eliminations. */
200 static void
201 setup_elimination_map (void)
203 int i;
204 struct lra_elim_table *ep;
206 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
207 elimination_map[i] = NULL;
208 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
209 if (ep->can_eliminate && elimination_map[ep->from] == NULL)
210 elimination_map[ep->from] = ep;
215 /* Compute the sum of X and Y, making canonicalizations assumed in an
216 address, namely: sum constant integers, surround the sum of two
217 constants with a CONST, put the constant as the second operand, and
218 group the constant on the outermost sum.
220 This routine assumes both inputs are already in canonical form. */
221 static rtx
222 form_sum (rtx x, rtx y)
224 rtx tem;
225 machine_mode mode = GET_MODE (x);
227 if (mode == VOIDmode)
228 mode = GET_MODE (y);
230 if (mode == VOIDmode)
231 mode = Pmode;
233 if (CONST_INT_P (x))
234 return plus_constant (mode, y, INTVAL (x));
235 else if (CONST_INT_P (y))
236 return plus_constant (mode, x, INTVAL (y));
237 else if (CONSTANT_P (x))
238 tem = x, x = y, y = tem;
240 if (GET_CODE (x) == PLUS && CONSTANT_P (XEXP (x, 1)))
241 return form_sum (XEXP (x, 0), form_sum (XEXP (x, 1), y));
243 /* Note that if the operands of Y are specified in the opposite
244 order in the recursive calls below, infinite recursion will
245 occur. */
246 if (GET_CODE (y) == PLUS && CONSTANT_P (XEXP (y, 1)))
247 return form_sum (form_sum (x, XEXP (y, 0)), XEXP (y, 1));
249 /* If both constant, encapsulate sum. Otherwise, just form sum. A
250 constant will have been placed second. */
251 if (CONSTANT_P (x) && CONSTANT_P (y))
253 if (GET_CODE (x) == CONST)
254 x = XEXP (x, 0);
255 if (GET_CODE (y) == CONST)
256 y = XEXP (y, 0);
258 return gen_rtx_CONST (VOIDmode, gen_rtx_PLUS (mode, x, y));
261 return gen_rtx_PLUS (mode, x, y);
264 /* Return the current substitution hard register of the elimination of
265 HARD_REGNO. If HARD_REGNO is not eliminable, return itself. */
267 lra_get_elimination_hard_regno (int hard_regno)
269 struct lra_elim_table *ep;
271 if (hard_regno < 0 || hard_regno >= FIRST_PSEUDO_REGISTER)
272 return hard_regno;
273 if ((ep = elimination_map[hard_regno]) == NULL)
274 return hard_regno;
275 return ep->to;
278 /* Return elimination which will be used for hard reg REG, NULL
279 otherwise. */
280 static struct lra_elim_table *
281 get_elimination (rtx reg)
283 int hard_regno;
284 struct lra_elim_table *ep;
285 HOST_WIDE_INT offset;
287 lra_assert (REG_P (reg));
288 if ((hard_regno = REGNO (reg)) < 0 || hard_regno >= FIRST_PSEUDO_REGISTER)
289 return NULL;
290 if ((ep = elimination_map[hard_regno]) != NULL)
291 return ep->from_rtx != reg ? NULL : ep;
292 if ((offset = self_elim_offsets[hard_regno]) == 0)
293 return NULL;
294 /* This is an iteration to restore offsets just after HARD_REGNO
295 stopped to be eliminable. */
296 self_elim_table.from = self_elim_table.to = hard_regno;
297 self_elim_table.from_rtx
298 = self_elim_table.to_rtx
299 = eliminable_reg_rtx[hard_regno];
300 lra_assert (self_elim_table.from_rtx != NULL);
301 self_elim_table.offset = offset;
302 return &self_elim_table;
305 /* Scan X and replace any eliminable registers (such as fp) with a
306 replacement (such as sp) if SUBST_P, plus an offset. The offset is
307 a change in the offset between the eliminable register and its
308 substitution if UPDATE_P, or the full offset if FULL_P, or
309 otherwise zero. If FULL_P, we also use the SP offsets for
310 elimination to SP. If UPDATE_P, use UPDATE_SP_OFFSET for updating
311 offsets of register elimnable to SP. If UPDATE_SP_OFFSET is
312 non-zero, don't use difference of the offset and the previous
313 offset.
315 MEM_MODE is the mode of an enclosing MEM. We need this to know how
316 much to adjust a register for, e.g., PRE_DEC. Also, if we are
317 inside a MEM, we are allowed to replace a sum of a hard register
318 and the constant zero with the hard register, which we cannot do
319 outside a MEM. In addition, we need to record the fact that a
320 hard register is referenced outside a MEM.
322 If we make full substitution to SP for non-null INSN, add the insn
323 sp offset. */
325 lra_eliminate_regs_1 (rtx_insn *insn, rtx x, machine_mode mem_mode,
326 bool subst_p, bool update_p,
327 HOST_WIDE_INT update_sp_offset, bool full_p)
329 enum rtx_code code = GET_CODE (x);
330 struct lra_elim_table *ep;
331 rtx new_rtx;
332 int i, j;
333 const char *fmt;
334 int copied = 0;
336 lra_assert (!update_p || !full_p);
337 lra_assert (update_sp_offset == 0 || (!subst_p && update_p && !full_p));
338 if (! current_function_decl)
339 return x;
341 switch (code)
343 CASE_CONST_ANY:
344 case CONST:
345 case SYMBOL_REF:
346 case CODE_LABEL:
347 case PC:
348 case CC0:
349 case ASM_INPUT:
350 case ADDR_VEC:
351 case ADDR_DIFF_VEC:
352 case RETURN:
353 return x;
355 case REG:
356 /* First handle the case where we encounter a bare hard register
357 that is eliminable. Replace it with a PLUS. */
358 if ((ep = get_elimination (x)) != NULL)
360 rtx to = subst_p ? ep->to_rtx : ep->from_rtx;
362 if (update_sp_offset != 0)
364 if (ep->to_rtx == stack_pointer_rtx)
365 return plus_constant (Pmode, to, update_sp_offset);
366 return to;
368 else if (update_p)
369 return plus_constant (Pmode, to, ep->offset - ep->previous_offset);
370 else if (full_p)
371 return plus_constant (Pmode, to,
372 ep->offset
373 - (insn != NULL_RTX
374 && ep->to_rtx == stack_pointer_rtx
375 ? lra_get_insn_recog_data (insn)->sp_offset
376 : 0));
377 else
378 return to;
380 return x;
382 case PLUS:
383 /* If this is the sum of an eliminable register and a constant, rework
384 the sum. */
385 if (REG_P (XEXP (x, 0)) && CONSTANT_P (XEXP (x, 1)))
387 if ((ep = get_elimination (XEXP (x, 0))) != NULL)
389 HOST_WIDE_INT offset;
390 rtx to = subst_p ? ep->to_rtx : ep->from_rtx;
392 if (! update_p && ! full_p)
393 return gen_rtx_PLUS (Pmode, to, XEXP (x, 1));
395 if (update_sp_offset != 0)
396 offset = ep->to_rtx == stack_pointer_rtx ? update_sp_offset : 0;
397 else
398 offset = (update_p
399 ? ep->offset - ep->previous_offset : ep->offset);
400 if (full_p && insn != NULL_RTX && ep->to_rtx == stack_pointer_rtx)
401 offset -= lra_get_insn_recog_data (insn)->sp_offset;
402 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) == -offset)
403 return to;
404 else
405 return gen_rtx_PLUS (Pmode, to,
406 plus_constant (Pmode,
407 XEXP (x, 1), offset));
410 /* If the hard register is not eliminable, we are done since
411 the other operand is a constant. */
412 return x;
415 /* If this is part of an address, we want to bring any constant
416 to the outermost PLUS. We will do this by doing hard
417 register replacement in our operands and seeing if a constant
418 shows up in one of them.
420 Note that there is no risk of modifying the structure of the
421 insn, since we only get called for its operands, thus we are
422 either modifying the address inside a MEM, or something like
423 an address operand of a load-address insn. */
426 rtx new0 = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
427 subst_p, update_p,
428 update_sp_offset, full_p);
429 rtx new1 = lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
430 subst_p, update_p,
431 update_sp_offset, full_p);
433 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
434 return form_sum (new0, new1);
436 return x;
438 case MULT:
439 /* If this is the product of an eliminable hard register and a
440 constant, apply the distribute law and move the constant out
441 so that we have (plus (mult ..) ..). This is needed in order
442 to keep load-address insns valid. This case is pathological.
443 We ignore the possibility of overflow here. */
444 if (REG_P (XEXP (x, 0)) && CONST_INT_P (XEXP (x, 1))
445 && (ep = get_elimination (XEXP (x, 0))) != NULL)
447 rtx to = subst_p ? ep->to_rtx : ep->from_rtx;
449 if (update_sp_offset != 0)
451 if (ep->to_rtx == stack_pointer_rtx)
452 return plus_constant (Pmode,
453 gen_rtx_MULT (Pmode, to, XEXP (x, 1)),
454 update_sp_offset * INTVAL (XEXP (x, 1)));
455 return gen_rtx_MULT (Pmode, to, XEXP (x, 1));
457 else if (update_p)
458 return plus_constant (Pmode,
459 gen_rtx_MULT (Pmode, to, XEXP (x, 1)),
460 (ep->offset - ep->previous_offset)
461 * INTVAL (XEXP (x, 1)));
462 else if (full_p)
464 HOST_WIDE_INT offset = ep->offset;
466 if (insn != NULL_RTX && ep->to_rtx == stack_pointer_rtx)
467 offset -= lra_get_insn_recog_data (insn)->sp_offset;
468 return
469 plus_constant (Pmode,
470 gen_rtx_MULT (Pmode, to, XEXP (x, 1)),
471 offset * INTVAL (XEXP (x, 1)));
473 else
474 return gen_rtx_MULT (Pmode, to, XEXP (x, 1));
477 /* ... fall through ... */
479 case CALL:
480 case COMPARE:
481 /* See comments before PLUS about handling MINUS. */
482 case MINUS:
483 case DIV: case UDIV:
484 case MOD: case UMOD:
485 case AND: case IOR: case XOR:
486 case ROTATERT: case ROTATE:
487 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
488 case NE: case EQ:
489 case GE: case GT: case GEU: case GTU:
490 case LE: case LT: case LEU: case LTU:
492 rtx new0 = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
493 subst_p, update_p,
494 update_sp_offset, full_p);
495 rtx new1 = XEXP (x, 1)
496 ? lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
497 subst_p, update_p,
498 update_sp_offset, full_p) : 0;
500 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
501 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
503 return x;
505 case EXPR_LIST:
506 /* If we have something in XEXP (x, 0), the usual case,
507 eliminate it. */
508 if (XEXP (x, 0))
510 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
511 subst_p, update_p,
512 update_sp_offset, full_p);
513 if (new_rtx != XEXP (x, 0))
515 /* If this is a REG_DEAD note, it is not valid anymore.
516 Using the eliminated version could result in creating a
517 REG_DEAD note for the stack or frame pointer. */
518 if (REG_NOTE_KIND (x) == REG_DEAD)
519 return (XEXP (x, 1)
520 ? lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
521 subst_p, update_p,
522 update_sp_offset, full_p)
523 : NULL_RTX);
525 x = alloc_reg_note (REG_NOTE_KIND (x), new_rtx, XEXP (x, 1));
529 /* ... fall through ... */
531 case INSN_LIST:
532 case INT_LIST:
533 /* Now do eliminations in the rest of the chain. If this was
534 an EXPR_LIST, this might result in allocating more memory than is
535 strictly needed, but it simplifies the code. */
536 if (XEXP (x, 1))
538 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
539 subst_p, update_p,
540 update_sp_offset, full_p);
541 if (new_rtx != XEXP (x, 1))
542 return
543 gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x),
544 XEXP (x, 0), new_rtx);
546 return x;
548 case PRE_INC:
549 case POST_INC:
550 case PRE_DEC:
551 case POST_DEC:
552 /* We do not support elimination of a register that is modified.
553 elimination_effects has already make sure that this does not
554 happen. */
555 return x;
557 case PRE_MODIFY:
558 case POST_MODIFY:
559 /* We do not support elimination of a hard register that is
560 modified. LRA has already make sure that this does not
561 happen. The only remaining case we need to consider here is
562 that the increment value may be an eliminable register. */
563 if (GET_CODE (XEXP (x, 1)) == PLUS
564 && XEXP (XEXP (x, 1), 0) == XEXP (x, 0))
566 rtx new_rtx = lra_eliminate_regs_1 (insn, XEXP (XEXP (x, 1), 1),
567 mem_mode, subst_p, update_p,
568 update_sp_offset, full_p);
570 if (new_rtx != XEXP (XEXP (x, 1), 1))
571 return gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (x, 0),
572 gen_rtx_PLUS (GET_MODE (x),
573 XEXP (x, 0), new_rtx));
575 return x;
577 case STRICT_LOW_PART:
578 case NEG: case NOT:
579 case SIGN_EXTEND: case ZERO_EXTEND:
580 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
581 case FLOAT: case FIX:
582 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
583 case ABS:
584 case SQRT:
585 case FFS:
586 case CLZ:
587 case CTZ:
588 case POPCOUNT:
589 case PARITY:
590 case BSWAP:
591 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
592 subst_p, update_p,
593 update_sp_offset, full_p);
594 if (new_rtx != XEXP (x, 0))
595 return gen_rtx_fmt_e (code, GET_MODE (x), new_rtx);
596 return x;
598 case SUBREG:
599 new_rtx = lra_eliminate_regs_1 (insn, SUBREG_REG (x), mem_mode,
600 subst_p, update_p,
601 update_sp_offset, full_p);
603 if (new_rtx != SUBREG_REG (x))
605 int x_size = GET_MODE_SIZE (GET_MODE (x));
606 int new_size = GET_MODE_SIZE (GET_MODE (new_rtx));
608 if (MEM_P (new_rtx) && x_size <= new_size)
610 SUBREG_REG (x) = new_rtx;
611 alter_subreg (&x, false);
612 return x;
614 else if (! subst_p)
616 /* LRA can transform subregs itself. So don't call
617 simplify_gen_subreg until LRA transformations are
618 finished. Function simplify_gen_subreg can do
619 non-trivial transformations (like truncation) which
620 might make LRA work to fail. */
621 SUBREG_REG (x) = new_rtx;
622 return x;
624 else
625 return simplify_gen_subreg (GET_MODE (x), new_rtx,
626 GET_MODE (new_rtx), SUBREG_BYTE (x));
629 return x;
631 case MEM:
632 /* Our only special processing is to pass the mode of the MEM to our
633 recursive call and copy the flags. While we are here, handle this
634 case more efficiently. */
635 return
636 replace_equiv_address_nv
638 lra_eliminate_regs_1 (insn, XEXP (x, 0), GET_MODE (x),
639 subst_p, update_p, update_sp_offset, full_p));
641 case USE:
642 /* Handle insn_list USE that a call to a pure function may generate. */
643 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 0), VOIDmode,
644 subst_p, update_p, update_sp_offset, full_p);
645 if (new_rtx != XEXP (x, 0))
646 return gen_rtx_USE (GET_MODE (x), new_rtx);
647 return x;
649 case CLOBBER:
650 case SET:
651 gcc_unreachable ();
653 default:
654 break;
657 /* Process each of our operands recursively. If any have changed, make a
658 copy of the rtx. */
659 fmt = GET_RTX_FORMAT (code);
660 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
662 if (*fmt == 'e')
664 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, i), mem_mode,
665 subst_p, update_p,
666 update_sp_offset, full_p);
667 if (new_rtx != XEXP (x, i) && ! copied)
669 x = shallow_copy_rtx (x);
670 copied = 1;
672 XEXP (x, i) = new_rtx;
674 else if (*fmt == 'E')
676 int copied_vec = 0;
677 for (j = 0; j < XVECLEN (x, i); j++)
679 new_rtx = lra_eliminate_regs_1 (insn, XVECEXP (x, i, j), mem_mode,
680 subst_p, update_p,
681 update_sp_offset, full_p);
682 if (new_rtx != XVECEXP (x, i, j) && ! copied_vec)
684 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
685 XVEC (x, i)->elem);
686 if (! copied)
688 x = shallow_copy_rtx (x);
689 copied = 1;
691 XVEC (x, i) = new_v;
692 copied_vec = 1;
694 XVECEXP (x, i, j) = new_rtx;
699 return x;
702 /* This function is used externally in subsequent passes of GCC. It
703 always does a full elimination of X. */
705 lra_eliminate_regs (rtx x, machine_mode mem_mode,
706 rtx insn ATTRIBUTE_UNUSED)
708 return lra_eliminate_regs_1 (NULL, x, mem_mode, true, false, 0, true);
711 /* Stack pointer offset before the current insn relative to one at the
712 func start. RTL insns can change SP explicitly. We keep the
713 changes from one insn to another through this variable. */
714 static HOST_WIDE_INT curr_sp_change;
716 /* Scan rtx X for references to elimination source or target registers
717 in contexts that would prevent the elimination from happening.
718 Update the table of eliminables to reflect the changed state.
719 MEM_MODE is the mode of an enclosing MEM rtx, or VOIDmode if not
720 within a MEM. */
721 static void
722 mark_not_eliminable (rtx x, machine_mode mem_mode)
724 enum rtx_code code = GET_CODE (x);
725 struct lra_elim_table *ep;
726 int i, j;
727 const char *fmt;
729 switch (code)
731 case PRE_INC:
732 case POST_INC:
733 case PRE_DEC:
734 case POST_DEC:
735 case POST_MODIFY:
736 case PRE_MODIFY:
737 if (XEXP (x, 0) == stack_pointer_rtx
738 && ((code != PRE_MODIFY && code != POST_MODIFY)
739 || (GET_CODE (XEXP (x, 1)) == PLUS
740 && XEXP (x, 0) == XEXP (XEXP (x, 1), 0)
741 && CONST_INT_P (XEXP (XEXP (x, 1), 1)))))
743 int size = GET_MODE_SIZE (mem_mode);
745 #ifdef PUSH_ROUNDING
746 /* If more bytes than MEM_MODE are pushed, account for
747 them. */
748 size = PUSH_ROUNDING (size);
749 #endif
750 if (code == PRE_DEC || code == POST_DEC)
751 curr_sp_change -= size;
752 else if (code == PRE_INC || code == POST_INC)
753 curr_sp_change += size;
754 else if (code == PRE_MODIFY || code == POST_MODIFY)
755 curr_sp_change += INTVAL (XEXP (XEXP (x, 1), 1));
757 else if (REG_P (XEXP (x, 0))
758 && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER)
760 /* If we modify the source of an elimination rule, disable
761 it. Do the same if it is the destination and not the
762 hard frame register. */
763 for (ep = reg_eliminate;
764 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
765 ep++)
766 if (ep->from_rtx == XEXP (x, 0)
767 || (ep->to_rtx == XEXP (x, 0)
768 && ep->to_rtx != hard_frame_pointer_rtx))
769 setup_can_eliminate (ep, false);
771 return;
773 case USE:
774 if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
775 /* If using a hard register that is the source of an eliminate
776 we still think can be performed, note it cannot be
777 performed since we don't know how this hard register is
778 used. */
779 for (ep = reg_eliminate;
780 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
781 ep++)
782 if (ep->from_rtx == XEXP (x, 0)
783 && ep->to_rtx != hard_frame_pointer_rtx)
784 setup_can_eliminate (ep, false);
785 return;
787 case CLOBBER:
788 if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
789 /* If clobbering a hard register that is the replacement
790 register for an elimination we still think can be
791 performed, note that it cannot be performed. Otherwise, we
792 need not be concerned about it. */
793 for (ep = reg_eliminate;
794 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
795 ep++)
796 if (ep->to_rtx == XEXP (x, 0)
797 && ep->to_rtx != hard_frame_pointer_rtx)
798 setup_can_eliminate (ep, false);
799 return;
801 case SET:
802 if (SET_DEST (x) == stack_pointer_rtx
803 && GET_CODE (SET_SRC (x)) == PLUS
804 && XEXP (SET_SRC (x), 0) == SET_DEST (x)
805 && CONST_INT_P (XEXP (SET_SRC (x), 1)))
807 curr_sp_change += INTVAL (XEXP (SET_SRC (x), 1));
808 return;
810 if (! REG_P (SET_DEST (x))
811 || REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER)
812 mark_not_eliminable (SET_DEST (x), mem_mode);
813 else
815 /* See if this is setting the replacement hard register for
816 an elimination.
818 If DEST is the hard frame pointer, we do nothing because
819 we assume that all assignments to the frame pointer are
820 for non-local gotos and are being done at a time when
821 they are valid and do not disturb anything else. Some
822 machines want to eliminate a fake argument pointer (or
823 even a fake frame pointer) with either the real frame
824 pointer or the stack pointer. Assignments to the hard
825 frame pointer must not prevent this elimination. */
826 for (ep = reg_eliminate;
827 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
828 ep++)
829 if (ep->to_rtx == SET_DEST (x)
830 && SET_DEST (x) != hard_frame_pointer_rtx)
831 setup_can_eliminate (ep, false);
834 mark_not_eliminable (SET_SRC (x), mem_mode);
835 return;
837 case MEM:
838 /* Our only special processing is to pass the mode of the MEM to
839 our recursive call. */
840 mark_not_eliminable (XEXP (x, 0), GET_MODE (x));
841 return;
843 default:
844 break;
847 fmt = GET_RTX_FORMAT (code);
848 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
850 if (*fmt == 'e')
851 mark_not_eliminable (XEXP (x, i), mem_mode);
852 else if (*fmt == 'E')
853 for (j = 0; j < XVECLEN (x, i); j++)
854 mark_not_eliminable (XVECEXP (x, i, j), mem_mode);
860 #ifdef HARD_FRAME_POINTER_REGNUM
862 /* Find offset equivalence note for reg WHAT in INSN and return the
863 found elmination offset. If the note is not found, return NULL.
864 Remove the found note. */
865 static rtx
866 remove_reg_equal_offset_note (rtx_insn *insn, rtx what)
868 rtx link, *link_loc;
870 for (link_loc = &REG_NOTES (insn);
871 (link = *link_loc) != NULL_RTX;
872 link_loc = &XEXP (link, 1))
873 if (REG_NOTE_KIND (link) == REG_EQUAL
874 && GET_CODE (XEXP (link, 0)) == PLUS
875 && XEXP (XEXP (link, 0), 0) == what
876 && CONST_INT_P (XEXP (XEXP (link, 0), 1)))
878 *link_loc = XEXP (link, 1);
879 return XEXP (XEXP (link, 0), 1);
881 return NULL_RTX;
884 #endif
886 /* Scan INSN and eliminate all eliminable hard registers in it.
888 If REPLACE_P is true, do the replacement destructively. Also
889 delete the insn as dead it if it is setting an eliminable register.
891 If REPLACE_P is false, just update the offsets while keeping the
892 base register the same. If FIRST_P, use the sp offset for
893 elimination to sp. Otherwise, use UPDATE_SP_OFFSET for this. If
894 UPDATE_SP_OFFSET is non-zero, don't use difference of the offset
895 and the previous offset. Attach the note about used elimination
896 for insns setting frame pointer to update elimination easy (without
897 parsing already generated elimination insns to find offset
898 previously used) in future. */
900 void
901 eliminate_regs_in_insn (rtx_insn *insn, bool replace_p, bool first_p,
902 HOST_WIDE_INT update_sp_offset)
904 int icode = recog_memoized (insn);
905 rtx old_set = single_set (insn);
906 bool validate_p;
907 int i;
908 rtx substed_operand[MAX_RECOG_OPERANDS];
909 rtx orig_operand[MAX_RECOG_OPERANDS];
910 struct lra_elim_table *ep;
911 rtx plus_src, plus_cst_src;
912 lra_insn_recog_data_t id;
913 struct lra_static_insn_data *static_id;
915 if (icode < 0 && asm_noperands (PATTERN (insn)) < 0 && ! DEBUG_INSN_P (insn))
917 lra_assert (GET_CODE (PATTERN (insn)) == USE
918 || GET_CODE (PATTERN (insn)) == CLOBBER
919 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
920 return;
923 /* Check for setting an eliminable register. */
924 if (old_set != 0 && REG_P (SET_DEST (old_set))
925 && (ep = get_elimination (SET_DEST (old_set))) != NULL)
927 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
928 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
930 bool delete_p = replace_p;
932 #ifdef HARD_FRAME_POINTER_REGNUM
933 if (ep->from == FRAME_POINTER_REGNUM
934 && ep->to == HARD_FRAME_POINTER_REGNUM)
935 /* If this is setting the frame pointer register to the
936 hardware frame pointer register and this is an
937 elimination that will be done (tested above), this
938 insn is really adjusting the frame pointer downward
939 to compensate for the adjustment done before a
940 nonlocal goto. */
942 rtx src = SET_SRC (old_set);
943 rtx off = remove_reg_equal_offset_note (insn, ep->to_rtx);
945 /* We should never process such insn with non-zero
946 UPDATE_SP_OFFSET. */
947 lra_assert (update_sp_offset == 0);
949 if (off != NULL_RTX
950 || src == ep->to_rtx
951 || (GET_CODE (src) == PLUS
952 && XEXP (src, 0) == ep->to_rtx
953 && CONST_INT_P (XEXP (src, 1))))
955 HOST_WIDE_INT offset;
957 if (replace_p)
959 SET_DEST (old_set) = ep->to_rtx;
960 lra_update_insn_recog_data (insn);
961 return;
963 offset = (off != NULL_RTX ? INTVAL (off)
964 : src == ep->to_rtx ? 0 : INTVAL (XEXP (src, 1)));
965 offset -= (ep->offset - ep->previous_offset);
966 src = plus_constant (Pmode, ep->to_rtx, offset);
968 /* First see if this insn remains valid when we
969 make the change. If not, keep the INSN_CODE
970 the same and let the constraint pass fit it
971 up. */
972 validate_change (insn, &SET_SRC (old_set), src, 1);
973 validate_change (insn, &SET_DEST (old_set),
974 ep->from_rtx, 1);
975 if (! apply_change_group ())
977 SET_SRC (old_set) = src;
978 SET_DEST (old_set) = ep->from_rtx;
980 lra_update_insn_recog_data (insn);
981 /* Add offset note for future updates. */
982 add_reg_note (insn, REG_EQUAL, src);
983 return;
986 #endif
988 /* This insn isn't serving a useful purpose. We delete it
989 when REPLACE is set. */
990 if (delete_p)
991 lra_delete_dead_insn (insn);
992 return;
996 /* We allow one special case which happens to work on all machines we
997 currently support: a single set with the source or a REG_EQUAL
998 note being a PLUS of an eliminable register and a constant. */
999 plus_src = plus_cst_src = 0;
1000 if (old_set && REG_P (SET_DEST (old_set)))
1002 if (GET_CODE (SET_SRC (old_set)) == PLUS)
1003 plus_src = SET_SRC (old_set);
1004 /* First see if the source is of the form (plus (...) CST). */
1005 if (plus_src
1006 && CONST_INT_P (XEXP (plus_src, 1)))
1007 plus_cst_src = plus_src;
1008 /* Check that the first operand of the PLUS is a hard reg or
1009 the lowpart subreg of one. */
1010 if (plus_cst_src)
1012 rtx reg = XEXP (plus_cst_src, 0);
1014 if (GET_CODE (reg) == SUBREG && subreg_lowpart_p (reg))
1015 reg = SUBREG_REG (reg);
1017 if (!REG_P (reg) || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
1018 plus_cst_src = 0;
1021 if (plus_cst_src)
1023 rtx reg = XEXP (plus_cst_src, 0);
1024 HOST_WIDE_INT offset = INTVAL (XEXP (plus_cst_src, 1));
1026 if (GET_CODE (reg) == SUBREG)
1027 reg = SUBREG_REG (reg);
1029 if (REG_P (reg) && (ep = get_elimination (reg)) != NULL)
1031 rtx to_rtx = replace_p ? ep->to_rtx : ep->from_rtx;
1033 if (! replace_p)
1035 if (update_sp_offset == 0)
1036 offset += (ep->offset - ep->previous_offset);
1037 if (ep->to_rtx == stack_pointer_rtx)
1039 if (first_p)
1040 offset -= lra_get_insn_recog_data (insn)->sp_offset;
1041 else
1042 offset += update_sp_offset;
1044 offset = trunc_int_for_mode (offset, GET_MODE (plus_cst_src));
1047 if (GET_CODE (XEXP (plus_cst_src, 0)) == SUBREG)
1048 to_rtx = gen_lowpart (GET_MODE (XEXP (plus_cst_src, 0)), to_rtx);
1049 /* If we have a nonzero offset, and the source is already a
1050 simple REG, the following transformation would increase
1051 the cost of the insn by replacing a simple REG with (plus
1052 (reg sp) CST). So try only when we already had a PLUS
1053 before. */
1054 if (offset == 0 || plus_src)
1056 rtx new_src = plus_constant (GET_MODE (to_rtx), to_rtx, offset);
1058 old_set = single_set (insn);
1060 /* First see if this insn remains valid when we make the
1061 change. If not, try to replace the whole pattern
1062 with a simple set (this may help if the original insn
1063 was a PARALLEL that was only recognized as single_set
1064 due to REG_UNUSED notes). If this isn't valid
1065 either, keep the INSN_CODE the same and let the
1066 constraint pass fix it up. */
1067 if (! validate_change (insn, &SET_SRC (old_set), new_src, 0))
1069 rtx new_pat = gen_rtx_SET (SET_DEST (old_set), new_src);
1071 if (! validate_change (insn, &PATTERN (insn), new_pat, 0))
1072 SET_SRC (old_set) = new_src;
1074 lra_update_insn_recog_data (insn);
1075 /* This can't have an effect on elimination offsets, so skip
1076 right to the end. */
1077 return;
1082 /* Eliminate all eliminable registers occurring in operands that
1083 can be handled by the constraint pass. */
1084 id = lra_get_insn_recog_data (insn);
1085 static_id = id->insn_static_data;
1086 validate_p = false;
1087 for (i = 0; i < static_id->n_operands; i++)
1089 orig_operand[i] = *id->operand_loc[i];
1090 substed_operand[i] = *id->operand_loc[i];
1092 /* For an asm statement, every operand is eliminable. */
1093 if (icode < 0 || insn_data[icode].operand[i].eliminable)
1095 /* Check for setting a hard register that we know about. */
1096 if (static_id->operand[i].type != OP_IN
1097 && REG_P (orig_operand[i]))
1099 /* If we are assigning to a hard register that can be
1100 eliminated, it must be as part of a PARALLEL, since
1101 the code above handles single SETs. This reg can not
1102 be longer eliminated -- it is forced by
1103 mark_not_eliminable. */
1104 for (ep = reg_eliminate;
1105 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
1106 ep++)
1107 lra_assert (ep->from_rtx != orig_operand[i]
1108 || ! ep->can_eliminate);
1111 /* Companion to the above plus substitution, we can allow
1112 invariants as the source of a plain move. */
1113 substed_operand[i]
1114 = lra_eliminate_regs_1 (insn, *id->operand_loc[i], VOIDmode,
1115 replace_p, ! replace_p && ! first_p,
1116 update_sp_offset, first_p);
1117 if (substed_operand[i] != orig_operand[i])
1118 validate_p = true;
1122 if (! validate_p)
1123 return;
1125 /* Substitute the operands; the new values are in the substed_operand
1126 array. */
1127 for (i = 0; i < static_id->n_operands; i++)
1128 *id->operand_loc[i] = substed_operand[i];
1129 for (i = 0; i < static_id->n_dups; i++)
1130 *id->dup_loc[i] = substed_operand[(int) static_id->dup_num[i]];
1132 /* If we had a move insn but now we don't, re-recognize it.
1133 This will cause spurious re-recognition if the old move had a
1134 PARALLEL since the new one still will, but we can't call
1135 single_set without having put new body into the insn and the
1136 re-recognition won't hurt in this rare case. */
1137 id = lra_update_insn_recog_data (insn);
1138 static_id = id->insn_static_data;
1141 /* Spill pseudos which are assigned to hard registers in SET. Add
1142 affected insns for processing in the subsequent constraint
1143 pass. */
1144 static void
1145 spill_pseudos (HARD_REG_SET set)
1147 int i;
1148 bitmap_head to_process;
1149 rtx_insn *insn;
1151 if (hard_reg_set_empty_p (set))
1152 return;
1153 if (lra_dump_file != NULL)
1155 fprintf (lra_dump_file, " Spilling non-eliminable hard regs:");
1156 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1157 if (TEST_HARD_REG_BIT (set, i))
1158 fprintf (lra_dump_file, " %d", i);
1159 fprintf (lra_dump_file, "\n");
1161 bitmap_initialize (&to_process, &reg_obstack);
1162 for (i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
1163 if (lra_reg_info[i].nrefs != 0 && reg_renumber[i] >= 0
1164 && overlaps_hard_reg_set_p (set,
1165 PSEUDO_REGNO_MODE (i), reg_renumber[i]))
1167 if (lra_dump_file != NULL)
1168 fprintf (lra_dump_file, " Spilling r%d(%d)\n",
1169 i, reg_renumber[i]);
1170 reg_renumber[i] = -1;
1171 bitmap_ior_into (&to_process, &lra_reg_info[i].insn_bitmap);
1173 IOR_HARD_REG_SET (lra_no_alloc_regs, set);
1174 for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
1175 if (bitmap_bit_p (&to_process, INSN_UID (insn)))
1177 lra_push_insn (insn);
1178 lra_set_used_insn_alternative (insn, -1);
1180 bitmap_clear (&to_process);
1183 /* Update all offsets and possibility for elimination on eliminable
1184 registers. Spill pseudos assigned to registers which are
1185 uneliminable, update LRA_NO_ALLOC_REGS and ELIMINABLE_REG_SET. Add
1186 insns to INSNS_WITH_CHANGED_OFFSETS containing eliminable hard
1187 registers whose offsets should be changed. Return true if any
1188 elimination offset changed. */
1189 static bool
1190 update_reg_eliminate (bitmap insns_with_changed_offsets)
1192 bool prev, result;
1193 struct lra_elim_table *ep, *ep1;
1194 HARD_REG_SET temp_hard_reg_set;
1196 /* Clear self elimination offsets. */
1197 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1198 self_elim_offsets[ep->from] = 0;
1199 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1201 /* If it is a currently used elimination: update the previous
1202 offset. */
1203 if (elimination_map[ep->from] == ep)
1204 ep->previous_offset = ep->offset;
1206 prev = ep->prev_can_eliminate;
1207 setup_can_eliminate (ep, targetm.can_eliminate (ep->from, ep->to));
1208 if (ep->can_eliminate && ! prev)
1210 /* It is possible that not eliminable register becomes
1211 eliminable because we took other reasons into account to
1212 set up eliminable regs in the initial set up. Just
1213 ignore new eliminable registers. */
1214 setup_can_eliminate (ep, false);
1215 continue;
1217 if (ep->can_eliminate != prev && elimination_map[ep->from] == ep)
1219 /* We cannot use this elimination anymore -- find another
1220 one. */
1221 if (lra_dump_file != NULL)
1222 fprintf (lra_dump_file,
1223 " Elimination %d to %d is not possible anymore\n",
1224 ep->from, ep->to);
1225 /* If after processing RTL we decides that SP can be used as
1226 a result of elimination, it can not be changed. */
1227 gcc_assert ((ep->to_rtx != stack_pointer_rtx)
1228 || (ep->from < FIRST_PSEUDO_REGISTER
1229 && fixed_regs [ep->from]));
1230 /* Mark that is not eliminable anymore. */
1231 elimination_map[ep->from] = NULL;
1232 for (ep1 = ep + 1; ep1 < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep1++)
1233 if (ep1->can_eliminate && ep1->from == ep->from)
1234 break;
1235 if (ep1 < &reg_eliminate[NUM_ELIMINABLE_REGS])
1237 if (lra_dump_file != NULL)
1238 fprintf (lra_dump_file, " Using elimination %d to %d now\n",
1239 ep1->from, ep1->to);
1240 lra_assert (ep1->previous_offset == 0);
1241 ep1->previous_offset = ep->offset;
1243 else
1245 /* There is no elimination anymore just use the hard
1246 register `from' itself. Setup self elimination
1247 offset to restore the original offset values. */
1248 if (lra_dump_file != NULL)
1249 fprintf (lra_dump_file, " %d is not eliminable at all\n",
1250 ep->from);
1251 self_elim_offsets[ep->from] = -ep->offset;
1252 if (ep->offset != 0)
1253 bitmap_ior_into (insns_with_changed_offsets,
1254 &lra_reg_info[ep->from].insn_bitmap);
1258 #ifdef ELIMINABLE_REGS
1259 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->offset);
1260 #else
1261 INITIAL_FRAME_POINTER_OFFSET (ep->offset);
1262 #endif
1264 setup_elimination_map ();
1265 result = false;
1266 CLEAR_HARD_REG_SET (temp_hard_reg_set);
1267 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1268 if (elimination_map[ep->from] == NULL)
1269 SET_HARD_REG_BIT (temp_hard_reg_set, ep->from);
1270 else if (elimination_map[ep->from] == ep)
1272 /* Prevent the hard register into which we eliminate from
1273 the usage for pseudos. */
1274 if (ep->from != ep->to)
1275 SET_HARD_REG_BIT (temp_hard_reg_set, ep->to);
1276 if (ep->previous_offset != ep->offset)
1278 bitmap_ior_into (insns_with_changed_offsets,
1279 &lra_reg_info[ep->from].insn_bitmap);
1281 /* Update offset when the eliminate offset have been
1282 changed. */
1283 lra_update_reg_val_offset (lra_reg_info[ep->from].val,
1284 ep->offset - ep->previous_offset);
1285 result = true;
1288 IOR_HARD_REG_SET (lra_no_alloc_regs, temp_hard_reg_set);
1289 AND_COMPL_HARD_REG_SET (eliminable_regset, temp_hard_reg_set);
1290 spill_pseudos (temp_hard_reg_set);
1291 return result;
1294 /* Initialize the table of hard registers to eliminate.
1295 Pre-condition: global flag frame_pointer_needed has been set before
1296 calling this function. */
1297 static void
1298 init_elim_table (void)
1300 struct lra_elim_table *ep;
1301 #ifdef ELIMINABLE_REGS
1302 bool value_p;
1303 const struct elim_table_1 *ep1;
1304 #endif
1306 if (!reg_eliminate)
1307 reg_eliminate = XCNEWVEC (struct lra_elim_table, NUM_ELIMINABLE_REGS);
1309 memset (self_elim_offsets, 0, sizeof (self_elim_offsets));
1310 /* Initiate member values which will be never changed. */
1311 self_elim_table.can_eliminate = self_elim_table.prev_can_eliminate = true;
1312 self_elim_table.previous_offset = 0;
1313 #ifdef ELIMINABLE_REGS
1314 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
1315 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
1317 ep->offset = ep->previous_offset = 0;
1318 ep->from = ep1->from;
1319 ep->to = ep1->to;
1320 value_p = (targetm.can_eliminate (ep->from, ep->to)
1321 && ! (ep->to == STACK_POINTER_REGNUM
1322 && frame_pointer_needed
1323 && (! SUPPORTS_STACK_ALIGNMENT
1324 || ! stack_realign_fp)));
1325 setup_can_eliminate (ep, value_p);
1327 #else
1328 reg_eliminate[0].offset = reg_eliminate[0].previous_offset = 0;
1329 reg_eliminate[0].from = reg_eliminate_1[0].from;
1330 reg_eliminate[0].to = reg_eliminate_1[0].to;
1331 setup_can_eliminate (&reg_eliminate[0], ! frame_pointer_needed);
1332 #endif
1334 /* Build the FROM and TO REG rtx's. Note that code in gen_rtx_REG
1335 will cause, e.g., gen_rtx_REG (Pmode, STACK_POINTER_REGNUM) to
1336 equal stack_pointer_rtx. We depend on this. Threfore we switch
1337 off that we are in LRA temporarily. */
1338 lra_in_progress = 0;
1339 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1341 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
1342 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
1343 eliminable_reg_rtx[ep->from] = ep->from_rtx;
1345 lra_in_progress = 1;
1348 /* Function for initialization of elimination once per function. It
1349 sets up sp offset for each insn. */
1350 static void
1351 init_elimination (void)
1353 bool stop_to_sp_elimination_p;
1354 basic_block bb;
1355 rtx_insn *insn;
1356 struct lra_elim_table *ep;
1358 init_elim_table ();
1359 FOR_EACH_BB_FN (bb, cfun)
1361 curr_sp_change = 0;
1362 stop_to_sp_elimination_p = false;
1363 FOR_BB_INSNS (bb, insn)
1364 if (INSN_P (insn))
1366 lra_get_insn_recog_data (insn)->sp_offset = curr_sp_change;
1367 if (NONDEBUG_INSN_P (insn))
1369 mark_not_eliminable (PATTERN (insn), VOIDmode);
1370 if (curr_sp_change != 0
1371 && find_reg_note (insn, REG_LABEL_OPERAND, NULL_RTX))
1372 stop_to_sp_elimination_p = true;
1375 if (! frame_pointer_needed
1376 && (curr_sp_change != 0 || stop_to_sp_elimination_p)
1377 && bb->succs && bb->succs->length () != 0)
1378 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1379 if (ep->to == STACK_POINTER_REGNUM)
1380 setup_can_eliminate (ep, false);
1382 setup_elimination_map ();
1385 /* Eliminate hard reg given by its location LOC. */
1386 void
1387 lra_eliminate_reg_if_possible (rtx *loc)
1389 int regno;
1390 struct lra_elim_table *ep;
1392 lra_assert (REG_P (*loc));
1393 if ((regno = REGNO (*loc)) >= FIRST_PSEUDO_REGISTER
1394 || ! TEST_HARD_REG_BIT (lra_no_alloc_regs, regno))
1395 return;
1396 if ((ep = get_elimination (*loc)) != NULL)
1397 *loc = ep->to_rtx;
1400 /* Do (final if FINAL_P or first if FIRST_P) elimination in INSN. Add
1401 the insn for subsequent processing in the constraint pass, update
1402 the insn info. */
1403 static void
1404 process_insn_for_elimination (rtx_insn *insn, bool final_p, bool first_p)
1406 eliminate_regs_in_insn (insn, final_p, first_p, 0);
1407 if (! final_p)
1409 /* Check that insn changed its code. This is a case when a move
1410 insn becomes an add insn and we do not want to process the
1411 insn as a move anymore. */
1412 int icode = recog (PATTERN (insn), insn, 0);
1414 if (icode >= 0 && icode != INSN_CODE (insn))
1416 INSN_CODE (insn) = icode;
1417 lra_update_insn_recog_data (insn);
1419 lra_update_insn_regno_info (insn);
1420 lra_push_insn (insn);
1421 lra_set_used_insn_alternative (insn, -1);
1425 /* Entry function to do final elimination if FINAL_P or to update
1426 elimination register offsets (FIRST_P if we are doing it the first
1427 time). */
1428 void
1429 lra_eliminate (bool final_p, bool first_p)
1431 unsigned int uid;
1432 bitmap_head insns_with_changed_offsets;
1433 bitmap_iterator bi;
1434 struct lra_elim_table *ep;
1436 gcc_assert (! final_p || ! first_p);
1438 timevar_push (TV_LRA_ELIMINATE);
1440 if (first_p)
1441 init_elimination ();
1443 bitmap_initialize (&insns_with_changed_offsets, &reg_obstack);
1444 if (final_p)
1446 #ifdef ENABLE_CHECKING
1447 update_reg_eliminate (&insns_with_changed_offsets);
1448 if (! bitmap_empty_p (&insns_with_changed_offsets))
1449 gcc_unreachable ();
1450 #endif
1451 /* We change eliminable hard registers in insns so we should do
1452 this for all insns containing any eliminable hard
1453 register. */
1454 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1455 if (elimination_map[ep->from] != NULL)
1456 bitmap_ior_into (&insns_with_changed_offsets,
1457 &lra_reg_info[ep->from].insn_bitmap);
1459 else if (! update_reg_eliminate (&insns_with_changed_offsets))
1460 goto lra_eliminate_done;
1461 if (lra_dump_file != NULL)
1463 fprintf (lra_dump_file, "New elimination table:\n");
1464 print_elim_table (lra_dump_file);
1466 EXECUTE_IF_SET_IN_BITMAP (&insns_with_changed_offsets, 0, uid, bi)
1467 /* A dead insn can be deleted in process_insn_for_elimination. */
1468 if (lra_insn_recog_data[uid] != NULL)
1469 process_insn_for_elimination (lra_insn_recog_data[uid]->insn,
1470 final_p, first_p);
1471 bitmap_clear (&insns_with_changed_offsets);
1473 lra_eliminate_done:
1474 timevar_pop (TV_LRA_ELIMINATE);