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