2015-06-24 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / lra-eliminations.c
blob1fc37e1b55dca478349a5382e68629cca0951657
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 "function.h"
69 #include "symtab.h"
70 #include "flags.h"
71 #include "alias.h"
72 #include "tree.h"
73 #include "expmed.h"
74 #include "dojump.h"
75 #include "explow.h"
76 #include "calls.h"
77 #include "emit-rtl.h"
78 #include "varasm.h"
79 #include "stmt.h"
80 #include "expr.h"
81 #include "predict.h"
82 #include "dominance.h"
83 #include "cfg.h"
84 #include "basic-block.h"
85 #include "except.h"
86 #include "optabs.h"
87 #include "df.h"
88 #include "ira.h"
89 #include "rtl-error.h"
90 #include "lra-int.h"
92 /* This structure is used to record information about hard register
93 eliminations. */
94 struct lra_elim_table
96 /* Hard register number to be eliminated. */
97 int from;
98 /* Hard register number used as replacement. */
99 int to;
100 /* Difference between values of the two hard registers above on
101 previous iteration. */
102 HOST_WIDE_INT previous_offset;
103 /* Difference between the values on the current iteration. */
104 HOST_WIDE_INT offset;
105 /* Nonzero if this elimination can be done. */
106 bool can_eliminate;
107 /* CAN_ELIMINATE since the last check. */
108 bool prev_can_eliminate;
109 /* REG rtx for the register to be eliminated. We cannot simply
110 compare the number since we might then spuriously replace a hard
111 register corresponding to a pseudo assigned to the reg to be
112 eliminated. */
113 rtx from_rtx;
114 /* REG rtx for the replacement. */
115 rtx to_rtx;
118 /* The elimination table. Each array entry describes one possible way
119 of eliminating a register in favor of another. If there is more
120 than one way of eliminating a particular register, the most
121 preferred should be specified first. */
122 static struct lra_elim_table *reg_eliminate = 0;
124 /* This is an intermediate structure to initialize the table. It has
125 exactly the members provided by ELIMINABLE_REGS. */
126 static const struct elim_table_1
128 const int from;
129 const int to;
130 } reg_eliminate_1[] =
132 /* If a set of eliminable hard registers was specified, define the
133 table from it. Otherwise, default to the normal case of the frame
134 pointer being replaced by the stack pointer. */
136 #ifdef ELIMINABLE_REGS
137 ELIMINABLE_REGS;
138 #else
139 {{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}};
140 #endif
142 #define NUM_ELIMINABLE_REGS ARRAY_SIZE (reg_eliminate_1)
144 /* Print info about elimination table to file F. */
145 static void
146 print_elim_table (FILE *f)
148 struct lra_elim_table *ep;
150 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
151 fprintf (f, "%s eliminate %d to %d (offset=" HOST_WIDE_INT_PRINT_DEC
152 ", prev_offset=" HOST_WIDE_INT_PRINT_DEC ")\n",
153 ep->can_eliminate ? "Can" : "Can't",
154 ep->from, ep->to, ep->offset, ep->previous_offset);
157 /* Print info about elimination table to stderr. */
158 void
159 lra_debug_elim_table (void)
161 print_elim_table (stderr);
164 /* Setup possibility of elimination in elimination table element EP to
165 VALUE. Setup FRAME_POINTER_NEEDED if elimination from frame
166 pointer to stack pointer is not possible anymore. */
167 static void
168 setup_can_eliminate (struct lra_elim_table *ep, bool value)
170 ep->can_eliminate = ep->prev_can_eliminate = value;
171 if (! value
172 && ep->from == FRAME_POINTER_REGNUM && ep->to == STACK_POINTER_REGNUM)
173 frame_pointer_needed = 1;
174 if (!frame_pointer_needed)
175 REGNO_POINTER_ALIGN (HARD_FRAME_POINTER_REGNUM) = 0;
178 /* Map: eliminable "from" register -> its current elimination,
179 or NULL if none. The elimination table may contain more than
180 one elimination for the same hard register, but this map specifies
181 the one that we are currently using. */
182 static struct lra_elim_table *elimination_map[FIRST_PSEUDO_REGISTER];
184 /* When an eliminable hard register becomes not eliminable, we use the
185 following special structure to restore original offsets for the
186 register. */
187 static struct lra_elim_table self_elim_table;
189 /* Offsets should be used to restore original offsets for eliminable
190 hard register which just became not eliminable. Zero,
191 otherwise. */
192 static HOST_WIDE_INT self_elim_offsets[FIRST_PSEUDO_REGISTER];
194 /* Map: hard regno -> RTL presentation. RTL presentations of all
195 potentially eliminable hard registers are stored in the map. */
196 static rtx eliminable_reg_rtx[FIRST_PSEUDO_REGISTER];
198 /* Set up ELIMINATION_MAP of the currently used eliminations. */
199 static void
200 setup_elimination_map (void)
202 int i;
203 struct lra_elim_table *ep;
205 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
206 elimination_map[i] = NULL;
207 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
208 if (ep->can_eliminate && elimination_map[ep->from] == NULL)
209 elimination_map[ep->from] = ep;
214 /* Compute the sum of X and Y, making canonicalizations assumed in an
215 address, namely: sum constant integers, surround the sum of two
216 constants with a CONST, put the constant as the second operand, and
217 group the constant on the outermost sum.
219 This routine assumes both inputs are already in canonical form. */
220 static rtx
221 form_sum (rtx x, rtx y)
223 rtx tem;
224 machine_mode mode = GET_MODE (x);
226 if (mode == VOIDmode)
227 mode = GET_MODE (y);
229 if (mode == VOIDmode)
230 mode = Pmode;
232 if (CONST_INT_P (x))
233 return plus_constant (mode, y, INTVAL (x));
234 else if (CONST_INT_P (y))
235 return plus_constant (mode, x, INTVAL (y));
236 else if (CONSTANT_P (x))
237 tem = x, x = y, y = tem;
239 if (GET_CODE (x) == PLUS && CONSTANT_P (XEXP (x, 1)))
240 return form_sum (XEXP (x, 0), form_sum (XEXP (x, 1), y));
242 /* Note that if the operands of Y are specified in the opposite
243 order in the recursive calls below, infinite recursion will
244 occur. */
245 if (GET_CODE (y) == PLUS && CONSTANT_P (XEXP (y, 1)))
246 return form_sum (form_sum (x, XEXP (y, 0)), XEXP (y, 1));
248 /* If both constant, encapsulate sum. Otherwise, just form sum. A
249 constant will have been placed second. */
250 if (CONSTANT_P (x) && CONSTANT_P (y))
252 if (GET_CODE (x) == CONST)
253 x = XEXP (x, 0);
254 if (GET_CODE (y) == CONST)
255 y = XEXP (y, 0);
257 return gen_rtx_CONST (VOIDmode, gen_rtx_PLUS (mode, x, y));
260 return gen_rtx_PLUS (mode, x, y);
263 /* Return the current substitution hard register of the elimination of
264 HARD_REGNO. If HARD_REGNO is not eliminable, return itself. */
266 lra_get_elimination_hard_regno (int hard_regno)
268 struct lra_elim_table *ep;
270 if (hard_regno < 0 || hard_regno >= FIRST_PSEUDO_REGISTER)
271 return hard_regno;
272 if ((ep = elimination_map[hard_regno]) == NULL)
273 return hard_regno;
274 return ep->to;
277 /* Return elimination which will be used for hard reg REG, NULL
278 otherwise. */
279 static struct lra_elim_table *
280 get_elimination (rtx reg)
282 int hard_regno;
283 struct lra_elim_table *ep;
284 HOST_WIDE_INT offset;
286 lra_assert (REG_P (reg));
287 if ((hard_regno = REGNO (reg)) < 0 || hard_regno >= FIRST_PSEUDO_REGISTER)
288 return NULL;
289 if ((ep = elimination_map[hard_regno]) != NULL)
290 return ep->from_rtx != reg ? NULL : ep;
291 if ((offset = self_elim_offsets[hard_regno]) == 0)
292 return NULL;
293 /* This is an iteration to restore offsets just after HARD_REGNO
294 stopped to be eliminable. */
295 self_elim_table.from = self_elim_table.to = hard_regno;
296 self_elim_table.from_rtx
297 = self_elim_table.to_rtx
298 = eliminable_reg_rtx[hard_regno];
299 lra_assert (self_elim_table.from_rtx != NULL);
300 self_elim_table.offset = offset;
301 return &self_elim_table;
304 /* Scan X and replace any eliminable registers (such as fp) with a
305 replacement (such as sp) if SUBST_P, plus an offset. The offset is
306 a change in the offset between the eliminable register and its
307 substitution if UPDATE_P, or the full offset if FULL_P, or
308 otherwise zero. If FULL_P, we also use the SP offsets for
309 elimination to SP. If UPDATE_P, use UPDATE_SP_OFFSET for updating
310 offsets of register elimnable to SP. If UPDATE_SP_OFFSET is
311 non-zero, don't use difference of the offset and the previous
312 offset.
314 MEM_MODE is the mode of an enclosing MEM. We need this to know how
315 much to adjust a register for, e.g., PRE_DEC. Also, if we are
316 inside a MEM, we are allowed to replace a sum of a hard register
317 and the constant zero with the hard register, which we cannot do
318 outside a MEM. In addition, we need to record the fact that a
319 hard register is referenced outside a MEM.
321 If we make full substitution to SP for non-null INSN, add the insn
322 sp offset. */
324 lra_eliminate_regs_1 (rtx_insn *insn, rtx x, machine_mode mem_mode,
325 bool subst_p, bool update_p,
326 HOST_WIDE_INT update_sp_offset, bool full_p)
328 enum rtx_code code = GET_CODE (x);
329 struct lra_elim_table *ep;
330 rtx new_rtx;
331 int i, j;
332 const char *fmt;
333 int copied = 0;
335 lra_assert (!update_p || !full_p);
336 lra_assert (update_sp_offset == 0 || (!subst_p && update_p && !full_p));
337 if (! current_function_decl)
338 return x;
340 switch (code)
342 CASE_CONST_ANY:
343 case CONST:
344 case SYMBOL_REF:
345 case CODE_LABEL:
346 case PC:
347 case CC0:
348 case ASM_INPUT:
349 case ADDR_VEC:
350 case ADDR_DIFF_VEC:
351 case RETURN:
352 return x;
354 case REG:
355 /* First handle the case where we encounter a bare hard register
356 that is eliminable. Replace it with a PLUS. */
357 if ((ep = get_elimination (x)) != NULL)
359 rtx to = subst_p ? ep->to_rtx : ep->from_rtx;
361 if (update_sp_offset != 0)
363 if (ep->to_rtx == stack_pointer_rtx)
364 return plus_constant (Pmode, to, update_sp_offset);
365 return to;
367 else if (update_p)
368 return plus_constant (Pmode, to, ep->offset - ep->previous_offset);
369 else if (full_p)
370 return plus_constant (Pmode, to,
371 ep->offset
372 - (insn != NULL_RTX
373 && ep->to_rtx == stack_pointer_rtx
374 ? lra_get_insn_recog_data (insn)->sp_offset
375 : 0));
376 else
377 return to;
379 return x;
381 case PLUS:
382 /* If this is the sum of an eliminable register and a constant, rework
383 the sum. */
384 if (REG_P (XEXP (x, 0)) && CONSTANT_P (XEXP (x, 1)))
386 if ((ep = get_elimination (XEXP (x, 0))) != NULL)
388 HOST_WIDE_INT offset;
389 rtx to = subst_p ? ep->to_rtx : ep->from_rtx;
391 if (! update_p && ! full_p)
392 return gen_rtx_PLUS (Pmode, to, XEXP (x, 1));
394 if (update_sp_offset != 0)
395 offset = ep->to_rtx == stack_pointer_rtx ? update_sp_offset : 0;
396 else
397 offset = (update_p
398 ? ep->offset - ep->previous_offset : ep->offset);
399 if (full_p && insn != NULL_RTX && ep->to_rtx == stack_pointer_rtx)
400 offset -= lra_get_insn_recog_data (insn)->sp_offset;
401 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) == -offset)
402 return to;
403 else
404 return gen_rtx_PLUS (Pmode, to,
405 plus_constant (Pmode,
406 XEXP (x, 1), offset));
409 /* If the hard register is not eliminable, we are done since
410 the other operand is a constant. */
411 return x;
414 /* If this is part of an address, we want to bring any constant
415 to the outermost PLUS. We will do this by doing hard
416 register replacement in our operands and seeing if a constant
417 shows up in one of them.
419 Note that there is no risk of modifying the structure of the
420 insn, since we only get called for its operands, thus we are
421 either modifying the address inside a MEM, or something like
422 an address operand of a load-address insn. */
425 rtx new0 = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
426 subst_p, update_p,
427 update_sp_offset, full_p);
428 rtx new1 = lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
429 subst_p, update_p,
430 update_sp_offset, full_p);
432 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
433 return form_sum (new0, new1);
435 return x;
437 case MULT:
438 /* If this is the product of an eliminable hard register and a
439 constant, apply the distribute law and move the constant out
440 so that we have (plus (mult ..) ..). This is needed in order
441 to keep load-address insns valid. This case is pathological.
442 We ignore the possibility of overflow here. */
443 if (REG_P (XEXP (x, 0)) && CONST_INT_P (XEXP (x, 1))
444 && (ep = get_elimination (XEXP (x, 0))) != NULL)
446 rtx to = subst_p ? ep->to_rtx : ep->from_rtx;
448 if (update_sp_offset != 0)
450 if (ep->to_rtx == stack_pointer_rtx)
451 return plus_constant (Pmode,
452 gen_rtx_MULT (Pmode, to, XEXP (x, 1)),
453 update_sp_offset * INTVAL (XEXP (x, 1)));
454 return gen_rtx_MULT (Pmode, to, XEXP (x, 1));
456 else if (update_p)
457 return plus_constant (Pmode,
458 gen_rtx_MULT (Pmode, to, XEXP (x, 1)),
459 (ep->offset - ep->previous_offset)
460 * INTVAL (XEXP (x, 1)));
461 else if (full_p)
463 HOST_WIDE_INT offset = ep->offset;
465 if (insn != NULL_RTX && ep->to_rtx == stack_pointer_rtx)
466 offset -= lra_get_insn_recog_data (insn)->sp_offset;
467 return
468 plus_constant (Pmode,
469 gen_rtx_MULT (Pmode, to, XEXP (x, 1)),
470 offset * INTVAL (XEXP (x, 1)));
472 else
473 return gen_rtx_MULT (Pmode, to, XEXP (x, 1));
476 /* ... fall through ... */
478 case CALL:
479 case COMPARE:
480 /* See comments before PLUS about handling MINUS. */
481 case MINUS:
482 case DIV: case UDIV:
483 case MOD: case UMOD:
484 case AND: case IOR: case XOR:
485 case ROTATERT: case ROTATE:
486 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
487 case NE: case EQ:
488 case GE: case GT: case GEU: case GTU:
489 case LE: case LT: case LEU: case LTU:
491 rtx new0 = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
492 subst_p, update_p,
493 update_sp_offset, full_p);
494 rtx new1 = XEXP (x, 1)
495 ? lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
496 subst_p, update_p,
497 update_sp_offset, full_p) : 0;
499 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
500 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
502 return x;
504 case EXPR_LIST:
505 /* If we have something in XEXP (x, 0), the usual case,
506 eliminate it. */
507 if (XEXP (x, 0))
509 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
510 subst_p, update_p,
511 update_sp_offset, full_p);
512 if (new_rtx != XEXP (x, 0))
514 /* If this is a REG_DEAD note, it is not valid anymore.
515 Using the eliminated version could result in creating a
516 REG_DEAD note for the stack or frame pointer. */
517 if (REG_NOTE_KIND (x) == REG_DEAD)
518 return (XEXP (x, 1)
519 ? lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
520 subst_p, update_p,
521 update_sp_offset, full_p)
522 : NULL_RTX);
524 x = alloc_reg_note (REG_NOTE_KIND (x), new_rtx, XEXP (x, 1));
528 /* ... fall through ... */
530 case INSN_LIST:
531 case INT_LIST:
532 /* Now do eliminations in the rest of the chain. If this was
533 an EXPR_LIST, this might result in allocating more memory than is
534 strictly needed, but it simplifies the code. */
535 if (XEXP (x, 1))
537 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
538 subst_p, update_p,
539 update_sp_offset, full_p);
540 if (new_rtx != XEXP (x, 1))
541 return
542 gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x),
543 XEXP (x, 0), new_rtx);
545 return x;
547 case PRE_INC:
548 case POST_INC:
549 case PRE_DEC:
550 case POST_DEC:
551 /* We do not support elimination of a register that is modified.
552 elimination_effects has already make sure that this does not
553 happen. */
554 return x;
556 case PRE_MODIFY:
557 case POST_MODIFY:
558 /* We do not support elimination of a hard register that is
559 modified. LRA has already make sure that this does not
560 happen. The only remaining case we need to consider here is
561 that the increment value may be an eliminable register. */
562 if (GET_CODE (XEXP (x, 1)) == PLUS
563 && XEXP (XEXP (x, 1), 0) == XEXP (x, 0))
565 rtx new_rtx = lra_eliminate_regs_1 (insn, XEXP (XEXP (x, 1), 1),
566 mem_mode, subst_p, update_p,
567 update_sp_offset, full_p);
569 if (new_rtx != XEXP (XEXP (x, 1), 1))
570 return gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (x, 0),
571 gen_rtx_PLUS (GET_MODE (x),
572 XEXP (x, 0), new_rtx));
574 return x;
576 case STRICT_LOW_PART:
577 case NEG: case NOT:
578 case SIGN_EXTEND: case ZERO_EXTEND:
579 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
580 case FLOAT: case FIX:
581 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
582 case ABS:
583 case SQRT:
584 case FFS:
585 case CLZ:
586 case CTZ:
587 case POPCOUNT:
588 case PARITY:
589 case BSWAP:
590 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
591 subst_p, update_p,
592 update_sp_offset, full_p);
593 if (new_rtx != XEXP (x, 0))
594 return gen_rtx_fmt_e (code, GET_MODE (x), new_rtx);
595 return x;
597 case SUBREG:
598 new_rtx = lra_eliminate_regs_1 (insn, SUBREG_REG (x), mem_mode,
599 subst_p, update_p,
600 update_sp_offset, full_p);
602 if (new_rtx != SUBREG_REG (x))
604 int x_size = GET_MODE_SIZE (GET_MODE (x));
605 int new_size = GET_MODE_SIZE (GET_MODE (new_rtx));
607 if (MEM_P (new_rtx) && x_size <= new_size)
609 SUBREG_REG (x) = new_rtx;
610 alter_subreg (&x, false);
611 return x;
613 else if (! subst_p)
615 /* LRA can transform subregs itself. So don't call
616 simplify_gen_subreg until LRA transformations are
617 finished. Function simplify_gen_subreg can do
618 non-trivial transformations (like truncation) which
619 might make LRA work to fail. */
620 SUBREG_REG (x) = new_rtx;
621 return x;
623 else
624 return simplify_gen_subreg (GET_MODE (x), new_rtx,
625 GET_MODE (new_rtx), SUBREG_BYTE (x));
628 return x;
630 case MEM:
631 /* Our only special processing is to pass the mode of the MEM to our
632 recursive call and copy the flags. While we are here, handle this
633 case more efficiently. */
634 return
635 replace_equiv_address_nv
637 lra_eliminate_regs_1 (insn, XEXP (x, 0), GET_MODE (x),
638 subst_p, update_p, update_sp_offset, full_p));
640 case USE:
641 /* Handle insn_list USE that a call to a pure function may generate. */
642 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 0), VOIDmode,
643 subst_p, update_p, update_sp_offset, full_p);
644 if (new_rtx != XEXP (x, 0))
645 return gen_rtx_USE (GET_MODE (x), new_rtx);
646 return x;
648 case CLOBBER:
649 case SET:
650 gcc_unreachable ();
652 default:
653 break;
656 /* Process each of our operands recursively. If any have changed, make a
657 copy of the rtx. */
658 fmt = GET_RTX_FORMAT (code);
659 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
661 if (*fmt == 'e')
663 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, i), mem_mode,
664 subst_p, update_p,
665 update_sp_offset, full_p);
666 if (new_rtx != XEXP (x, i) && ! copied)
668 x = shallow_copy_rtx (x);
669 copied = 1;
671 XEXP (x, i) = new_rtx;
673 else if (*fmt == 'E')
675 int copied_vec = 0;
676 for (j = 0; j < XVECLEN (x, i); j++)
678 new_rtx = lra_eliminate_regs_1 (insn, XVECEXP (x, i, j), mem_mode,
679 subst_p, update_p,
680 update_sp_offset, full_p);
681 if (new_rtx != XVECEXP (x, i, j) && ! copied_vec)
683 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
684 XVEC (x, i)->elem);
685 if (! copied)
687 x = shallow_copy_rtx (x);
688 copied = 1;
690 XVEC (x, i) = new_v;
691 copied_vec = 1;
693 XVECEXP (x, i, j) = new_rtx;
698 return x;
701 /* This function is used externally in subsequent passes of GCC. It
702 always does a full elimination of X. */
704 lra_eliminate_regs (rtx x, machine_mode mem_mode,
705 rtx insn ATTRIBUTE_UNUSED)
707 return lra_eliminate_regs_1 (NULL, x, mem_mode, true, false, 0, true);
710 /* Stack pointer offset before the current insn relative to one at the
711 func start. RTL insns can change SP explicitly. We keep the
712 changes from one insn to another through this variable. */
713 static HOST_WIDE_INT curr_sp_change;
715 /* Scan rtx X for references to elimination source or target registers
716 in contexts that would prevent the elimination from happening.
717 Update the table of eliminables to reflect the changed state.
718 MEM_MODE is the mode of an enclosing MEM rtx, or VOIDmode if not
719 within a MEM. */
720 static void
721 mark_not_eliminable (rtx x, machine_mode mem_mode)
723 enum rtx_code code = GET_CODE (x);
724 struct lra_elim_table *ep;
725 int i, j;
726 const char *fmt;
728 switch (code)
730 case PRE_INC:
731 case POST_INC:
732 case PRE_DEC:
733 case POST_DEC:
734 case POST_MODIFY:
735 case PRE_MODIFY:
736 if (XEXP (x, 0) == stack_pointer_rtx
737 && ((code != PRE_MODIFY && code != POST_MODIFY)
738 || (GET_CODE (XEXP (x, 1)) == PLUS
739 && XEXP (x, 0) == XEXP (XEXP (x, 1), 0)
740 && CONST_INT_P (XEXP (XEXP (x, 1), 1)))))
742 int size = GET_MODE_SIZE (mem_mode);
744 #ifdef PUSH_ROUNDING
745 /* If more bytes than MEM_MODE are pushed, account for
746 them. */
747 size = PUSH_ROUNDING (size);
748 #endif
749 if (code == PRE_DEC || code == POST_DEC)
750 curr_sp_change -= size;
751 else if (code == PRE_INC || code == POST_INC)
752 curr_sp_change += size;
753 else if (code == PRE_MODIFY || code == POST_MODIFY)
754 curr_sp_change += INTVAL (XEXP (XEXP (x, 1), 1));
756 else if (REG_P (XEXP (x, 0))
757 && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER)
759 /* If we modify the source of an elimination rule, disable
760 it. Do the same if it is the destination and not the
761 hard frame register. */
762 for (ep = reg_eliminate;
763 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
764 ep++)
765 if (ep->from_rtx == XEXP (x, 0)
766 || (ep->to_rtx == XEXP (x, 0)
767 && ep->to_rtx != hard_frame_pointer_rtx))
768 setup_can_eliminate (ep, false);
770 return;
772 case USE:
773 if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
774 /* If using a hard register that is the source of an eliminate
775 we still think can be performed, note it cannot be
776 performed since we don't know how this hard register is
777 used. */
778 for (ep = reg_eliminate;
779 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
780 ep++)
781 if (ep->from_rtx == XEXP (x, 0)
782 && ep->to_rtx != hard_frame_pointer_rtx)
783 setup_can_eliminate (ep, false);
784 return;
786 case CLOBBER:
787 if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
788 /* If clobbering a hard register that is the replacement
789 register for an elimination we still think can be
790 performed, note that it cannot be performed. Otherwise, we
791 need not be concerned about it. */
792 for (ep = reg_eliminate;
793 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
794 ep++)
795 if (ep->to_rtx == XEXP (x, 0)
796 && ep->to_rtx != hard_frame_pointer_rtx)
797 setup_can_eliminate (ep, false);
798 return;
800 case SET:
801 if (SET_DEST (x) == stack_pointer_rtx
802 && GET_CODE (SET_SRC (x)) == PLUS
803 && XEXP (SET_SRC (x), 0) == SET_DEST (x)
804 && CONST_INT_P (XEXP (SET_SRC (x), 1)))
806 curr_sp_change += INTVAL (XEXP (SET_SRC (x), 1));
807 return;
809 if (! REG_P (SET_DEST (x))
810 || REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER)
811 mark_not_eliminable (SET_DEST (x), mem_mode);
812 else
814 /* See if this is setting the replacement hard register for
815 an elimination.
817 If DEST is the hard frame pointer, we do nothing because
818 we assume that all assignments to the frame pointer are
819 for non-local gotos and are being done at a time when
820 they are valid and do not disturb anything else. Some
821 machines want to eliminate a fake argument pointer (or
822 even a fake frame pointer) with either the real frame
823 pointer or the stack pointer. Assignments to the hard
824 frame pointer must not prevent this elimination. */
825 for (ep = reg_eliminate;
826 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
827 ep++)
828 if (ep->to_rtx == SET_DEST (x)
829 && SET_DEST (x) != hard_frame_pointer_rtx)
830 setup_can_eliminate (ep, false);
833 mark_not_eliminable (SET_SRC (x), mem_mode);
834 return;
836 case MEM:
837 /* Our only special processing is to pass the mode of the MEM to
838 our recursive call. */
839 mark_not_eliminable (XEXP (x, 0), GET_MODE (x));
840 return;
842 default:
843 break;
846 fmt = GET_RTX_FORMAT (code);
847 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
849 if (*fmt == 'e')
850 mark_not_eliminable (XEXP (x, i), mem_mode);
851 else if (*fmt == 'E')
852 for (j = 0; j < XVECLEN (x, i); j++)
853 mark_not_eliminable (XVECEXP (x, i, j), mem_mode);
859 #ifdef HARD_FRAME_POINTER_REGNUM
861 /* Find offset equivalence note for reg WHAT in INSN and return the
862 found elmination offset. If the note is not found, return NULL.
863 Remove the found note. */
864 static rtx
865 remove_reg_equal_offset_note (rtx_insn *insn, rtx what)
867 rtx link, *link_loc;
869 for (link_loc = &REG_NOTES (insn);
870 (link = *link_loc) != NULL_RTX;
871 link_loc = &XEXP (link, 1))
872 if (REG_NOTE_KIND (link) == REG_EQUAL
873 && GET_CODE (XEXP (link, 0)) == PLUS
874 && XEXP (XEXP (link, 0), 0) == what
875 && CONST_INT_P (XEXP (XEXP (link, 0), 1)))
877 *link_loc = XEXP (link, 1);
878 return XEXP (XEXP (link, 0), 1);
880 return NULL_RTX;
883 #endif
885 /* Scan INSN and eliminate all eliminable hard registers in it.
887 If REPLACE_P is true, do the replacement destructively. Also
888 delete the insn as dead it if it is setting an eliminable register.
890 If REPLACE_P is false, just update the offsets while keeping the
891 base register the same. If FIRST_P, use the sp offset for
892 elimination to sp. Otherwise, use UPDATE_SP_OFFSET for this. If
893 UPDATE_SP_OFFSET is non-zero, don't use difference of the offset
894 and the previous offset. Attach the note about used elimination
895 for insns setting frame pointer to update elimination easy (without
896 parsing already generated elimination insns to find offset
897 previously used) in future. */
899 void
900 eliminate_regs_in_insn (rtx_insn *insn, bool replace_p, bool first_p,
901 HOST_WIDE_INT update_sp_offset)
903 int icode = recog_memoized (insn);
904 rtx old_set = single_set (insn);
905 bool validate_p;
906 int i;
907 rtx substed_operand[MAX_RECOG_OPERANDS];
908 rtx orig_operand[MAX_RECOG_OPERANDS];
909 struct lra_elim_table *ep;
910 rtx plus_src, plus_cst_src;
911 lra_insn_recog_data_t id;
912 struct lra_static_insn_data *static_id;
914 if (icode < 0 && asm_noperands (PATTERN (insn)) < 0 && ! DEBUG_INSN_P (insn))
916 lra_assert (GET_CODE (PATTERN (insn)) == USE
917 || GET_CODE (PATTERN (insn)) == CLOBBER
918 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
919 return;
922 /* Check for setting an eliminable register. */
923 if (old_set != 0 && REG_P (SET_DEST (old_set))
924 && (ep = get_elimination (SET_DEST (old_set))) != NULL)
926 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
927 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
929 bool delete_p = replace_p;
931 #ifdef HARD_FRAME_POINTER_REGNUM
932 if (ep->from == FRAME_POINTER_REGNUM
933 && ep->to == HARD_FRAME_POINTER_REGNUM)
934 /* If this is setting the frame pointer register to the
935 hardware frame pointer register and this is an
936 elimination that will be done (tested above), this
937 insn is really adjusting the frame pointer downward
938 to compensate for the adjustment done before a
939 nonlocal goto. */
941 rtx src = SET_SRC (old_set);
942 rtx off = remove_reg_equal_offset_note (insn, ep->to_rtx);
944 /* We should never process such insn with non-zero
945 UPDATE_SP_OFFSET. */
946 lra_assert (update_sp_offset == 0);
948 if (off != NULL_RTX
949 || src == ep->to_rtx
950 || (GET_CODE (src) == PLUS
951 && XEXP (src, 0) == ep->to_rtx
952 && CONST_INT_P (XEXP (src, 1))))
954 HOST_WIDE_INT offset;
956 if (replace_p)
958 SET_DEST (old_set) = ep->to_rtx;
959 lra_update_insn_recog_data (insn);
960 return;
962 offset = (off != NULL_RTX ? INTVAL (off)
963 : src == ep->to_rtx ? 0 : INTVAL (XEXP (src, 1)));
964 offset -= (ep->offset - ep->previous_offset);
965 src = plus_constant (Pmode, ep->to_rtx, offset);
967 /* First see if this insn remains valid when we
968 make the change. If not, keep the INSN_CODE
969 the same and let the constraint pass fit it
970 up. */
971 validate_change (insn, &SET_SRC (old_set), src, 1);
972 validate_change (insn, &SET_DEST (old_set),
973 ep->from_rtx, 1);
974 if (! apply_change_group ())
976 SET_SRC (old_set) = src;
977 SET_DEST (old_set) = ep->from_rtx;
979 lra_update_insn_recog_data (insn);
980 /* Add offset note for future updates. */
981 add_reg_note (insn, REG_EQUAL, src);
982 return;
985 #endif
987 /* This insn isn't serving a useful purpose. We delete it
988 when REPLACE is set. */
989 if (delete_p)
990 lra_delete_dead_insn (insn);
991 return;
995 /* We allow one special case which happens to work on all machines we
996 currently support: a single set with the source or a REG_EQUAL
997 note being a PLUS of an eliminable register and a constant. */
998 plus_src = plus_cst_src = 0;
999 if (old_set && REG_P (SET_DEST (old_set)))
1001 if (GET_CODE (SET_SRC (old_set)) == PLUS)
1002 plus_src = SET_SRC (old_set);
1003 /* First see if the source is of the form (plus (...) CST). */
1004 if (plus_src
1005 && CONST_INT_P (XEXP (plus_src, 1)))
1006 plus_cst_src = plus_src;
1007 /* Check that the first operand of the PLUS is a hard reg or
1008 the lowpart subreg of one. */
1009 if (plus_cst_src)
1011 rtx reg = XEXP (plus_cst_src, 0);
1013 if (GET_CODE (reg) == SUBREG && subreg_lowpart_p (reg))
1014 reg = SUBREG_REG (reg);
1016 if (!REG_P (reg) || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
1017 plus_cst_src = 0;
1020 if (plus_cst_src)
1022 rtx reg = XEXP (plus_cst_src, 0);
1023 HOST_WIDE_INT offset = INTVAL (XEXP (plus_cst_src, 1));
1025 if (GET_CODE (reg) == SUBREG)
1026 reg = SUBREG_REG (reg);
1028 if (REG_P (reg) && (ep = get_elimination (reg)) != NULL)
1030 rtx to_rtx = replace_p ? ep->to_rtx : ep->from_rtx;
1032 if (! replace_p)
1034 if (update_sp_offset == 0)
1035 offset += (ep->offset - ep->previous_offset);
1036 if (ep->to_rtx == stack_pointer_rtx)
1038 if (first_p)
1039 offset -= lra_get_insn_recog_data (insn)->sp_offset;
1040 else
1041 offset += update_sp_offset;
1043 offset = trunc_int_for_mode (offset, GET_MODE (plus_cst_src));
1046 if (GET_CODE (XEXP (plus_cst_src, 0)) == SUBREG)
1047 to_rtx = gen_lowpart (GET_MODE (XEXP (plus_cst_src, 0)), to_rtx);
1048 /* If we have a nonzero offset, and the source is already a
1049 simple REG, the following transformation would increase
1050 the cost of the insn by replacing a simple REG with (plus
1051 (reg sp) CST). So try only when we already had a PLUS
1052 before. */
1053 if (offset == 0 || plus_src)
1055 rtx new_src = plus_constant (GET_MODE (to_rtx), to_rtx, offset);
1057 old_set = single_set (insn);
1059 /* First see if this insn remains valid when we make the
1060 change. If not, try to replace the whole pattern
1061 with a simple set (this may help if the original insn
1062 was a PARALLEL that was only recognized as single_set
1063 due to REG_UNUSED notes). If this isn't valid
1064 either, keep the INSN_CODE the same and let the
1065 constraint pass fix it up. */
1066 if (! validate_change (insn, &SET_SRC (old_set), new_src, 0))
1068 rtx new_pat = gen_rtx_SET (SET_DEST (old_set), new_src);
1070 if (! validate_change (insn, &PATTERN (insn), new_pat, 0))
1071 SET_SRC (old_set) = new_src;
1073 lra_update_insn_recog_data (insn);
1074 /* This can't have an effect on elimination offsets, so skip
1075 right to the end. */
1076 return;
1081 /* Eliminate all eliminable registers occurring in operands that
1082 can be handled by the constraint pass. */
1083 id = lra_get_insn_recog_data (insn);
1084 static_id = id->insn_static_data;
1085 validate_p = false;
1086 for (i = 0; i < static_id->n_operands; i++)
1088 orig_operand[i] = *id->operand_loc[i];
1089 substed_operand[i] = *id->operand_loc[i];
1091 /* For an asm statement, every operand is eliminable. */
1092 if (icode < 0 || insn_data[icode].operand[i].eliminable)
1094 /* Check for setting a hard register that we know about. */
1095 if (static_id->operand[i].type != OP_IN
1096 && REG_P (orig_operand[i]))
1098 /* If we are assigning to a hard register that can be
1099 eliminated, it must be as part of a PARALLEL, since
1100 the code above handles single SETs. This reg can not
1101 be longer eliminated -- it is forced by
1102 mark_not_eliminable. */
1103 for (ep = reg_eliminate;
1104 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
1105 ep++)
1106 lra_assert (ep->from_rtx != orig_operand[i]
1107 || ! ep->can_eliminate);
1110 /* Companion to the above plus substitution, we can allow
1111 invariants as the source of a plain move. */
1112 substed_operand[i]
1113 = lra_eliminate_regs_1 (insn, *id->operand_loc[i], VOIDmode,
1114 replace_p, ! replace_p && ! first_p,
1115 update_sp_offset, first_p);
1116 if (substed_operand[i] != orig_operand[i])
1117 validate_p = true;
1121 if (! validate_p)
1122 return;
1124 /* Substitute the operands; the new values are in the substed_operand
1125 array. */
1126 for (i = 0; i < static_id->n_operands; i++)
1127 *id->operand_loc[i] = substed_operand[i];
1128 for (i = 0; i < static_id->n_dups; i++)
1129 *id->dup_loc[i] = substed_operand[(int) static_id->dup_num[i]];
1131 /* If we had a move insn but now we don't, re-recognize it.
1132 This will cause spurious re-recognition if the old move had a
1133 PARALLEL since the new one still will, but we can't call
1134 single_set without having put new body into the insn and the
1135 re-recognition won't hurt in this rare case. */
1136 id = lra_update_insn_recog_data (insn);
1137 static_id = id->insn_static_data;
1140 /* Spill pseudos which are assigned to hard registers in SET. Add
1141 affected insns for processing in the subsequent constraint
1142 pass. */
1143 static void
1144 spill_pseudos (HARD_REG_SET set)
1146 int i;
1147 bitmap_head to_process;
1148 rtx_insn *insn;
1150 if (hard_reg_set_empty_p (set))
1151 return;
1152 if (lra_dump_file != NULL)
1154 fprintf (lra_dump_file, " Spilling non-eliminable hard regs:");
1155 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1156 if (TEST_HARD_REG_BIT (set, i))
1157 fprintf (lra_dump_file, " %d", i);
1158 fprintf (lra_dump_file, "\n");
1160 bitmap_initialize (&to_process, &reg_obstack);
1161 for (i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
1162 if (lra_reg_info[i].nrefs != 0 && reg_renumber[i] >= 0
1163 && overlaps_hard_reg_set_p (set,
1164 PSEUDO_REGNO_MODE (i), reg_renumber[i]))
1166 if (lra_dump_file != NULL)
1167 fprintf (lra_dump_file, " Spilling r%d(%d)\n",
1168 i, reg_renumber[i]);
1169 reg_renumber[i] = -1;
1170 bitmap_ior_into (&to_process, &lra_reg_info[i].insn_bitmap);
1172 IOR_HARD_REG_SET (lra_no_alloc_regs, set);
1173 for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
1174 if (bitmap_bit_p (&to_process, INSN_UID (insn)))
1176 lra_push_insn (insn);
1177 lra_set_used_insn_alternative (insn, -1);
1179 bitmap_clear (&to_process);
1182 /* Update all offsets and possibility for elimination on eliminable
1183 registers. Spill pseudos assigned to registers which are
1184 uneliminable, update LRA_NO_ALLOC_REGS and ELIMINABLE_REG_SET. Add
1185 insns to INSNS_WITH_CHANGED_OFFSETS containing eliminable hard
1186 registers whose offsets should be changed. Return true if any
1187 elimination offset changed. */
1188 static bool
1189 update_reg_eliminate (bitmap insns_with_changed_offsets)
1191 bool prev, result;
1192 struct lra_elim_table *ep, *ep1;
1193 HARD_REG_SET temp_hard_reg_set;
1195 /* Clear self elimination offsets. */
1196 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1197 self_elim_offsets[ep->from] = 0;
1198 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1200 /* If it is a currently used elimination: update the previous
1201 offset. */
1202 if (elimination_map[ep->from] == ep)
1203 ep->previous_offset = ep->offset;
1205 prev = ep->prev_can_eliminate;
1206 setup_can_eliminate (ep, targetm.can_eliminate (ep->from, ep->to));
1207 if (ep->can_eliminate && ! prev)
1209 /* It is possible that not eliminable register becomes
1210 eliminable because we took other reasons into account to
1211 set up eliminable regs in the initial set up. Just
1212 ignore new eliminable registers. */
1213 setup_can_eliminate (ep, false);
1214 continue;
1216 if (ep->can_eliminate != prev && elimination_map[ep->from] == ep)
1218 /* We cannot use this elimination anymore -- find another
1219 one. */
1220 if (lra_dump_file != NULL)
1221 fprintf (lra_dump_file,
1222 " Elimination %d to %d is not possible anymore\n",
1223 ep->from, ep->to);
1224 /* If after processing RTL we decides that SP can be used as
1225 a result of elimination, it can not be changed. */
1226 gcc_assert ((ep->to_rtx != stack_pointer_rtx)
1227 || (ep->from < FIRST_PSEUDO_REGISTER
1228 && fixed_regs [ep->from]));
1229 /* Mark that is not eliminable anymore. */
1230 elimination_map[ep->from] = NULL;
1231 for (ep1 = ep + 1; ep1 < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep1++)
1232 if (ep1->can_eliminate && ep1->from == ep->from)
1233 break;
1234 if (ep1 < &reg_eliminate[NUM_ELIMINABLE_REGS])
1236 if (lra_dump_file != NULL)
1237 fprintf (lra_dump_file, " Using elimination %d to %d now\n",
1238 ep1->from, ep1->to);
1239 lra_assert (ep1->previous_offset == 0);
1240 ep1->previous_offset = ep->offset;
1242 else
1244 /* There is no elimination anymore just use the hard
1245 register `from' itself. Setup self elimination
1246 offset to restore the original offset values. */
1247 if (lra_dump_file != NULL)
1248 fprintf (lra_dump_file, " %d is not eliminable at all\n",
1249 ep->from);
1250 self_elim_offsets[ep->from] = -ep->offset;
1251 if (ep->offset != 0)
1252 bitmap_ior_into (insns_with_changed_offsets,
1253 &lra_reg_info[ep->from].insn_bitmap);
1257 #ifdef ELIMINABLE_REGS
1258 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->offset);
1259 #else
1260 INITIAL_FRAME_POINTER_OFFSET (ep->offset);
1261 #endif
1263 setup_elimination_map ();
1264 result = false;
1265 CLEAR_HARD_REG_SET (temp_hard_reg_set);
1266 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1267 if (elimination_map[ep->from] == NULL)
1268 SET_HARD_REG_BIT (temp_hard_reg_set, ep->from);
1269 else if (elimination_map[ep->from] == ep)
1271 /* Prevent the hard register into which we eliminate from
1272 the usage for pseudos. */
1273 if (ep->from != ep->to)
1274 SET_HARD_REG_BIT (temp_hard_reg_set, ep->to);
1275 if (ep->previous_offset != ep->offset)
1277 bitmap_ior_into (insns_with_changed_offsets,
1278 &lra_reg_info[ep->from].insn_bitmap);
1280 /* Update offset when the eliminate offset have been
1281 changed. */
1282 lra_update_reg_val_offset (lra_reg_info[ep->from].val,
1283 ep->offset - ep->previous_offset);
1284 result = true;
1287 IOR_HARD_REG_SET (lra_no_alloc_regs, temp_hard_reg_set);
1288 AND_COMPL_HARD_REG_SET (eliminable_regset, temp_hard_reg_set);
1289 spill_pseudos (temp_hard_reg_set);
1290 return result;
1293 /* Initialize the table of hard registers to eliminate.
1294 Pre-condition: global flag frame_pointer_needed has been set before
1295 calling this function. */
1296 static void
1297 init_elim_table (void)
1299 struct lra_elim_table *ep;
1300 #ifdef ELIMINABLE_REGS
1301 bool value_p;
1302 const struct elim_table_1 *ep1;
1303 #endif
1305 if (!reg_eliminate)
1306 reg_eliminate = XCNEWVEC (struct lra_elim_table, NUM_ELIMINABLE_REGS);
1308 memset (self_elim_offsets, 0, sizeof (self_elim_offsets));
1309 /* Initiate member values which will be never changed. */
1310 self_elim_table.can_eliminate = self_elim_table.prev_can_eliminate = true;
1311 self_elim_table.previous_offset = 0;
1312 #ifdef ELIMINABLE_REGS
1313 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
1314 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
1316 ep->offset = ep->previous_offset = 0;
1317 ep->from = ep1->from;
1318 ep->to = ep1->to;
1319 value_p = (targetm.can_eliminate (ep->from, ep->to)
1320 && ! (ep->to == STACK_POINTER_REGNUM
1321 && frame_pointer_needed
1322 && (! SUPPORTS_STACK_ALIGNMENT
1323 || ! stack_realign_fp)));
1324 setup_can_eliminate (ep, value_p);
1326 #else
1327 reg_eliminate[0].offset = reg_eliminate[0].previous_offset = 0;
1328 reg_eliminate[0].from = reg_eliminate_1[0].from;
1329 reg_eliminate[0].to = reg_eliminate_1[0].to;
1330 setup_can_eliminate (&reg_eliminate[0], ! frame_pointer_needed);
1331 #endif
1333 /* Build the FROM and TO REG rtx's. Note that code in gen_rtx_REG
1334 will cause, e.g., gen_rtx_REG (Pmode, STACK_POINTER_REGNUM) to
1335 equal stack_pointer_rtx. We depend on this. Threfore we switch
1336 off that we are in LRA temporarily. */
1337 lra_in_progress = 0;
1338 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1340 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
1341 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
1342 eliminable_reg_rtx[ep->from] = ep->from_rtx;
1344 lra_in_progress = 1;
1347 /* Function for initialization of elimination once per function. It
1348 sets up sp offset for each insn. */
1349 static void
1350 init_elimination (void)
1352 bool stop_to_sp_elimination_p;
1353 basic_block bb;
1354 rtx_insn *insn;
1355 struct lra_elim_table *ep;
1357 init_elim_table ();
1358 FOR_EACH_BB_FN (bb, cfun)
1360 curr_sp_change = 0;
1361 stop_to_sp_elimination_p = false;
1362 FOR_BB_INSNS (bb, insn)
1363 if (INSN_P (insn))
1365 lra_get_insn_recog_data (insn)->sp_offset = curr_sp_change;
1366 if (NONDEBUG_INSN_P (insn))
1368 mark_not_eliminable (PATTERN (insn), VOIDmode);
1369 if (curr_sp_change != 0
1370 && find_reg_note (insn, REG_LABEL_OPERAND, NULL_RTX))
1371 stop_to_sp_elimination_p = true;
1374 if (! frame_pointer_needed
1375 && (curr_sp_change != 0 || stop_to_sp_elimination_p)
1376 && bb->succs && bb->succs->length () != 0)
1377 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1378 if (ep->to == STACK_POINTER_REGNUM)
1379 setup_can_eliminate (ep, false);
1381 setup_elimination_map ();
1384 /* Eliminate hard reg given by its location LOC. */
1385 void
1386 lra_eliminate_reg_if_possible (rtx *loc)
1388 int regno;
1389 struct lra_elim_table *ep;
1391 lra_assert (REG_P (*loc));
1392 if ((regno = REGNO (*loc)) >= FIRST_PSEUDO_REGISTER
1393 || ! TEST_HARD_REG_BIT (lra_no_alloc_regs, regno))
1394 return;
1395 if ((ep = get_elimination (*loc)) != NULL)
1396 *loc = ep->to_rtx;
1399 /* Do (final if FINAL_P or first if FIRST_P) elimination in INSN. Add
1400 the insn for subsequent processing in the constraint pass, update
1401 the insn info. */
1402 static void
1403 process_insn_for_elimination (rtx_insn *insn, bool final_p, bool first_p)
1405 eliminate_regs_in_insn (insn, final_p, first_p, 0);
1406 if (! final_p)
1408 /* Check that insn changed its code. This is a case when a move
1409 insn becomes an add insn and we do not want to process the
1410 insn as a move anymore. */
1411 int icode = recog (PATTERN (insn), insn, 0);
1413 if (icode >= 0 && icode != INSN_CODE (insn))
1415 INSN_CODE (insn) = icode;
1416 lra_update_insn_recog_data (insn);
1418 lra_update_insn_regno_info (insn);
1419 lra_push_insn (insn);
1420 lra_set_used_insn_alternative (insn, -1);
1424 /* Entry function to do final elimination if FINAL_P or to update
1425 elimination register offsets (FIRST_P if we are doing it the first
1426 time). */
1427 void
1428 lra_eliminate (bool final_p, bool first_p)
1430 unsigned int uid;
1431 bitmap_head insns_with_changed_offsets;
1432 bitmap_iterator bi;
1433 struct lra_elim_table *ep;
1435 gcc_assert (! final_p || ! first_p);
1437 timevar_push (TV_LRA_ELIMINATE);
1439 if (first_p)
1440 init_elimination ();
1442 bitmap_initialize (&insns_with_changed_offsets, &reg_obstack);
1443 if (final_p)
1445 #ifdef ENABLE_CHECKING
1446 update_reg_eliminate (&insns_with_changed_offsets);
1447 if (! bitmap_empty_p (&insns_with_changed_offsets))
1448 gcc_unreachable ();
1449 #endif
1450 /* We change eliminable hard registers in insns so we should do
1451 this for all insns containing any eliminable hard
1452 register. */
1453 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1454 if (elimination_map[ep->from] != NULL)
1455 bitmap_ior_into (&insns_with_changed_offsets,
1456 &lra_reg_info[ep->from].insn_bitmap);
1458 else if (! update_reg_eliminate (&insns_with_changed_offsets))
1459 goto lra_eliminate_done;
1460 if (lra_dump_file != NULL)
1462 fprintf (lra_dump_file, "New elimination table:\n");
1463 print_elim_table (lra_dump_file);
1465 EXECUTE_IF_SET_IN_BITMAP (&insns_with_changed_offsets, 0, uid, bi)
1466 /* A dead insn can be deleted in process_insn_for_elimination. */
1467 if (lra_insn_recog_data[uid] != NULL)
1468 process_insn_for_elimination (lra_insn_recog_data[uid]->insn,
1469 final_p, first_p);
1470 bitmap_clear (&insns_with_changed_offsets);
1472 lra_eliminate_done:
1473 timevar_pop (TV_LRA_ELIMINATE);