2015-09-03 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / lra-eliminations.c
blobfdf4179927ef4ded29a974c612d57b45c10ac0b8
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 "backend.h"
58 #include "tree.h"
59 #include "rtl.h"
60 #include "df.h"
61 #include "tm_p.h"
62 #include "regs.h"
63 #include "insn-config.h"
64 #include "insn-codes.h"
65 #include "recog.h"
66 #include "output.h"
67 #include "addresses.h"
68 #include "target.h"
69 #include "flags.h"
70 #include "alias.h"
71 #include "expmed.h"
72 #include "dojump.h"
73 #include "explow.h"
74 #include "calls.h"
75 #include "emit-rtl.h"
76 #include "varasm.h"
77 #include "stmt.h"
78 #include "expr.h"
79 #include "except.h"
80 #include "optabs.h"
81 #include "ira.h"
82 #include "rtl-error.h"
83 #include "lra.h"
84 #include "insn-attr.h"
85 #include "lra-int.h"
87 /* This structure is used to record information about hard register
88 eliminations. */
89 struct lra_elim_table
91 /* Hard register number to be eliminated. */
92 int from;
93 /* Hard register number used as replacement. */
94 int to;
95 /* Difference between values of the two hard registers above on
96 previous iteration. */
97 HOST_WIDE_INT previous_offset;
98 /* Difference between the values on the current iteration. */
99 HOST_WIDE_INT offset;
100 /* Nonzero if this elimination can be done. */
101 bool can_eliminate;
102 /* CAN_ELIMINATE since the last check. */
103 bool prev_can_eliminate;
104 /* REG rtx for the register to be eliminated. We cannot simply
105 compare the number since we might then spuriously replace a hard
106 register corresponding to a pseudo assigned to the reg to be
107 eliminated. */
108 rtx from_rtx;
109 /* REG rtx for the replacement. */
110 rtx to_rtx;
113 /* The elimination table. Each array entry describes one possible way
114 of eliminating a register in favor of another. If there is more
115 than one way of eliminating a particular register, the most
116 preferred should be specified first. */
117 static struct lra_elim_table *reg_eliminate = 0;
119 /* This is an intermediate structure to initialize the table. It has
120 exactly the members provided by ELIMINABLE_REGS. */
121 static const struct elim_table_1
123 const int from;
124 const int to;
125 } reg_eliminate_1[] =
127 /* If a set of eliminable hard registers was specified, define the
128 table from it. Otherwise, default to the normal case of the frame
129 pointer being replaced by the stack pointer. */
131 #ifdef ELIMINABLE_REGS
132 ELIMINABLE_REGS;
133 #else
134 {{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}};
135 #endif
137 #define NUM_ELIMINABLE_REGS ARRAY_SIZE (reg_eliminate_1)
139 /* Print info about elimination table to file F. */
140 static void
141 print_elim_table (FILE *f)
143 struct lra_elim_table *ep;
145 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
146 fprintf (f, "%s eliminate %d to %d (offset=" HOST_WIDE_INT_PRINT_DEC
147 ", prev_offset=" HOST_WIDE_INT_PRINT_DEC ")\n",
148 ep->can_eliminate ? "Can" : "Can't",
149 ep->from, ep->to, ep->offset, ep->previous_offset);
152 /* Print info about elimination table to stderr. */
153 void
154 lra_debug_elim_table (void)
156 print_elim_table (stderr);
159 /* Setup possibility of elimination in elimination table element EP to
160 VALUE. Setup FRAME_POINTER_NEEDED if elimination from frame
161 pointer to stack pointer is not possible anymore. */
162 static void
163 setup_can_eliminate (struct lra_elim_table *ep, bool value)
165 ep->can_eliminate = ep->prev_can_eliminate = value;
166 if (! value
167 && ep->from == FRAME_POINTER_REGNUM && ep->to == STACK_POINTER_REGNUM)
168 frame_pointer_needed = 1;
169 if (!frame_pointer_needed)
170 REGNO_POINTER_ALIGN (HARD_FRAME_POINTER_REGNUM) = 0;
173 /* Map: eliminable "from" register -> its current elimination,
174 or NULL if none. The elimination table may contain more than
175 one elimination for the same hard register, but this map specifies
176 the one that we are currently using. */
177 static struct lra_elim_table *elimination_map[FIRST_PSEUDO_REGISTER];
179 /* When an eliminable hard register becomes not eliminable, we use the
180 following special structure to restore original offsets for the
181 register. */
182 static struct lra_elim_table self_elim_table;
184 /* Offsets should be used to restore original offsets for eliminable
185 hard register which just became not eliminable. Zero,
186 otherwise. */
187 static HOST_WIDE_INT self_elim_offsets[FIRST_PSEUDO_REGISTER];
189 /* Map: hard regno -> RTL presentation. RTL presentations of all
190 potentially eliminable hard registers are stored in the map. */
191 static rtx eliminable_reg_rtx[FIRST_PSEUDO_REGISTER];
193 /* Set up ELIMINATION_MAP of the currently used eliminations. */
194 static void
195 setup_elimination_map (void)
197 int i;
198 struct lra_elim_table *ep;
200 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
201 elimination_map[i] = NULL;
202 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
203 if (ep->can_eliminate && elimination_map[ep->from] == NULL)
204 elimination_map[ep->from] = ep;
209 /* Compute the sum of X and Y, making canonicalizations assumed in an
210 address, namely: sum constant integers, surround the sum of two
211 constants with a CONST, put the constant as the second operand, and
212 group the constant on the outermost sum.
214 This routine assumes both inputs are already in canonical form. */
215 static rtx
216 form_sum (rtx x, rtx y)
218 machine_mode mode = GET_MODE (x);
220 if (mode == VOIDmode)
221 mode = GET_MODE (y);
223 if (mode == VOIDmode)
224 mode = Pmode;
226 if (CONST_INT_P (x))
227 return plus_constant (mode, y, INTVAL (x));
228 else if (CONST_INT_P (y))
229 return plus_constant (mode, x, INTVAL (y));
230 else if (CONSTANT_P (x))
231 std::swap (x, y);
233 if (GET_CODE (x) == PLUS && CONSTANT_P (XEXP (x, 1)))
234 return form_sum (XEXP (x, 0), form_sum (XEXP (x, 1), y));
236 /* Note that if the operands of Y are specified in the opposite
237 order in the recursive calls below, infinite recursion will
238 occur. */
239 if (GET_CODE (y) == PLUS && CONSTANT_P (XEXP (y, 1)))
240 return form_sum (form_sum (x, XEXP (y, 0)), XEXP (y, 1));
242 /* If both constant, encapsulate sum. Otherwise, just form sum. A
243 constant will have been placed second. */
244 if (CONSTANT_P (x) && CONSTANT_P (y))
246 if (GET_CODE (x) == CONST)
247 x = XEXP (x, 0);
248 if (GET_CODE (y) == CONST)
249 y = XEXP (y, 0);
251 return gen_rtx_CONST (VOIDmode, gen_rtx_PLUS (mode, x, y));
254 return gen_rtx_PLUS (mode, x, y);
257 /* Return the current substitution hard register of the elimination of
258 HARD_REGNO. If HARD_REGNO is not eliminable, return itself. */
260 lra_get_elimination_hard_regno (int hard_regno)
262 struct lra_elim_table *ep;
264 if (hard_regno < 0 || hard_regno >= FIRST_PSEUDO_REGISTER)
265 return hard_regno;
266 if ((ep = elimination_map[hard_regno]) == NULL)
267 return hard_regno;
268 return ep->to;
271 /* Return elimination which will be used for hard reg REG, NULL
272 otherwise. */
273 static struct lra_elim_table *
274 get_elimination (rtx reg)
276 int hard_regno;
277 struct lra_elim_table *ep;
278 HOST_WIDE_INT offset;
280 lra_assert (REG_P (reg));
281 if ((hard_regno = REGNO (reg)) < 0 || hard_regno >= FIRST_PSEUDO_REGISTER)
282 return NULL;
283 if ((ep = elimination_map[hard_regno]) != NULL)
284 return ep->from_rtx != reg ? NULL : ep;
285 if ((offset = self_elim_offsets[hard_regno]) == 0)
286 return NULL;
287 /* This is an iteration to restore offsets just after HARD_REGNO
288 stopped to be eliminable. */
289 self_elim_table.from = self_elim_table.to = hard_regno;
290 self_elim_table.from_rtx
291 = self_elim_table.to_rtx
292 = eliminable_reg_rtx[hard_regno];
293 lra_assert (self_elim_table.from_rtx != NULL);
294 self_elim_table.offset = offset;
295 return &self_elim_table;
298 /* Scan X and replace any eliminable registers (such as fp) with a
299 replacement (such as sp) if SUBST_P, plus an offset. The offset is
300 a change in the offset between the eliminable register and its
301 substitution if UPDATE_P, or the full offset if FULL_P, or
302 otherwise zero. If FULL_P, we also use the SP offsets for
303 elimination to SP. If UPDATE_P, use UPDATE_SP_OFFSET for updating
304 offsets of register elimnable to SP. If UPDATE_SP_OFFSET is
305 non-zero, don't use difference of the offset and the previous
306 offset.
308 MEM_MODE is the mode of an enclosing MEM. We need this to know how
309 much to adjust a register for, e.g., PRE_DEC. Also, if we are
310 inside a MEM, we are allowed to replace a sum of a hard register
311 and the constant zero with the hard register, which we cannot do
312 outside a MEM. In addition, we need to record the fact that a
313 hard register is referenced outside a MEM.
315 If we make full substitution to SP for non-null INSN, add the insn
316 sp offset. */
318 lra_eliminate_regs_1 (rtx_insn *insn, rtx x, machine_mode mem_mode,
319 bool subst_p, bool update_p,
320 HOST_WIDE_INT update_sp_offset, bool full_p)
322 enum rtx_code code = GET_CODE (x);
323 struct lra_elim_table *ep;
324 rtx new_rtx;
325 int i, j;
326 const char *fmt;
327 int copied = 0;
329 lra_assert (!update_p || !full_p);
330 lra_assert (update_sp_offset == 0 || (!subst_p && update_p && !full_p));
331 if (! current_function_decl)
332 return x;
334 switch (code)
336 CASE_CONST_ANY:
337 case CONST:
338 case SYMBOL_REF:
339 case CODE_LABEL:
340 case PC:
341 case CC0:
342 case ASM_INPUT:
343 case ADDR_VEC:
344 case ADDR_DIFF_VEC:
345 case RETURN:
346 return x;
348 case REG:
349 /* First handle the case where we encounter a bare hard register
350 that is eliminable. Replace it with a PLUS. */
351 if ((ep = get_elimination (x)) != NULL)
353 rtx to = subst_p ? ep->to_rtx : ep->from_rtx;
355 if (update_sp_offset != 0)
357 if (ep->to_rtx == stack_pointer_rtx)
358 return plus_constant (Pmode, to, update_sp_offset);
359 return to;
361 else if (update_p)
362 return plus_constant (Pmode, to, ep->offset - ep->previous_offset);
363 else if (full_p)
364 return plus_constant (Pmode, to,
365 ep->offset
366 - (insn != NULL_RTX
367 && ep->to_rtx == stack_pointer_rtx
368 ? lra_get_insn_recog_data (insn)->sp_offset
369 : 0));
370 else
371 return to;
373 return x;
375 case PLUS:
376 /* If this is the sum of an eliminable register and a constant, rework
377 the sum. */
378 if (REG_P (XEXP (x, 0)) && CONSTANT_P (XEXP (x, 1)))
380 if ((ep = get_elimination (XEXP (x, 0))) != NULL)
382 HOST_WIDE_INT offset;
383 rtx to = subst_p ? ep->to_rtx : ep->from_rtx;
385 if (! update_p && ! full_p)
386 return gen_rtx_PLUS (Pmode, to, XEXP (x, 1));
388 if (update_sp_offset != 0)
389 offset = ep->to_rtx == stack_pointer_rtx ? update_sp_offset : 0;
390 else
391 offset = (update_p
392 ? ep->offset - ep->previous_offset : ep->offset);
393 if (full_p && insn != NULL_RTX && ep->to_rtx == stack_pointer_rtx)
394 offset -= lra_get_insn_recog_data (insn)->sp_offset;
395 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) == -offset)
396 return to;
397 else
398 return gen_rtx_PLUS (Pmode, to,
399 plus_constant (Pmode,
400 XEXP (x, 1), offset));
403 /* If the hard register is not eliminable, we are done since
404 the other operand is a constant. */
405 return x;
408 /* If this is part of an address, we want to bring any constant
409 to the outermost PLUS. We will do this by doing hard
410 register replacement in our operands and seeing if a constant
411 shows up in one of them.
413 Note that there is no risk of modifying the structure of the
414 insn, since we only get called for its operands, thus we are
415 either modifying the address inside a MEM, or something like
416 an address operand of a load-address insn. */
419 rtx new0 = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
420 subst_p, update_p,
421 update_sp_offset, full_p);
422 rtx new1 = lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
423 subst_p, update_p,
424 update_sp_offset, full_p);
426 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
427 return form_sum (new0, new1);
429 return x;
431 case MULT:
432 /* If this is the product of an eliminable hard register and a
433 constant, apply the distribute law and move the constant out
434 so that we have (plus (mult ..) ..). This is needed in order
435 to keep load-address insns valid. This case is pathological.
436 We ignore the possibility of overflow here. */
437 if (REG_P (XEXP (x, 0)) && CONST_INT_P (XEXP (x, 1))
438 && (ep = get_elimination (XEXP (x, 0))) != NULL)
440 rtx to = subst_p ? ep->to_rtx : ep->from_rtx;
442 if (update_sp_offset != 0)
444 if (ep->to_rtx == stack_pointer_rtx)
445 return plus_constant (Pmode,
446 gen_rtx_MULT (Pmode, to, XEXP (x, 1)),
447 update_sp_offset * INTVAL (XEXP (x, 1)));
448 return gen_rtx_MULT (Pmode, to, XEXP (x, 1));
450 else if (update_p)
451 return plus_constant (Pmode,
452 gen_rtx_MULT (Pmode, to, XEXP (x, 1)),
453 (ep->offset - ep->previous_offset)
454 * INTVAL (XEXP (x, 1)));
455 else if (full_p)
457 HOST_WIDE_INT offset = ep->offset;
459 if (insn != NULL_RTX && ep->to_rtx == stack_pointer_rtx)
460 offset -= lra_get_insn_recog_data (insn)->sp_offset;
461 return
462 plus_constant (Pmode,
463 gen_rtx_MULT (Pmode, to, XEXP (x, 1)),
464 offset * INTVAL (XEXP (x, 1)));
466 else
467 return gen_rtx_MULT (Pmode, to, XEXP (x, 1));
470 /* ... fall through ... */
472 case CALL:
473 case COMPARE:
474 /* See comments before PLUS about handling MINUS. */
475 case MINUS:
476 case DIV: case UDIV:
477 case MOD: case UMOD:
478 case AND: case IOR: case XOR:
479 case ROTATERT: case ROTATE:
480 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
481 case NE: case EQ:
482 case GE: case GT: case GEU: case GTU:
483 case LE: case LT: case LEU: case LTU:
485 rtx new0 = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
486 subst_p, update_p,
487 update_sp_offset, full_p);
488 rtx new1 = XEXP (x, 1)
489 ? lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
490 subst_p, update_p,
491 update_sp_offset, full_p) : 0;
493 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
494 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
496 return x;
498 case EXPR_LIST:
499 /* If we have something in XEXP (x, 0), the usual case,
500 eliminate it. */
501 if (XEXP (x, 0))
503 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
504 subst_p, update_p,
505 update_sp_offset, full_p);
506 if (new_rtx != XEXP (x, 0))
508 /* If this is a REG_DEAD note, it is not valid anymore.
509 Using the eliminated version could result in creating a
510 REG_DEAD note for the stack or frame pointer. */
511 if (REG_NOTE_KIND (x) == REG_DEAD)
512 return (XEXP (x, 1)
513 ? lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
514 subst_p, update_p,
515 update_sp_offset, full_p)
516 : NULL_RTX);
518 x = alloc_reg_note (REG_NOTE_KIND (x), new_rtx, XEXP (x, 1));
522 /* ... fall through ... */
524 case INSN_LIST:
525 case INT_LIST:
526 /* Now do eliminations in the rest of the chain. If this was
527 an EXPR_LIST, this might result in allocating more memory than is
528 strictly needed, but it simplifies the code. */
529 if (XEXP (x, 1))
531 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
532 subst_p, update_p,
533 update_sp_offset, full_p);
534 if (new_rtx != XEXP (x, 1))
535 return
536 gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x),
537 XEXP (x, 0), new_rtx);
539 return x;
541 case PRE_INC:
542 case POST_INC:
543 case PRE_DEC:
544 case POST_DEC:
545 /* We do not support elimination of a register that is modified.
546 elimination_effects has already make sure that this does not
547 happen. */
548 return x;
550 case PRE_MODIFY:
551 case POST_MODIFY:
552 /* We do not support elimination of a hard register that is
553 modified. LRA has already make sure that this does not
554 happen. The only remaining case we need to consider here is
555 that the increment value may be an eliminable register. */
556 if (GET_CODE (XEXP (x, 1)) == PLUS
557 && XEXP (XEXP (x, 1), 0) == XEXP (x, 0))
559 rtx new_rtx = lra_eliminate_regs_1 (insn, XEXP (XEXP (x, 1), 1),
560 mem_mode, subst_p, update_p,
561 update_sp_offset, full_p);
563 if (new_rtx != XEXP (XEXP (x, 1), 1))
564 return gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (x, 0),
565 gen_rtx_PLUS (GET_MODE (x),
566 XEXP (x, 0), new_rtx));
568 return x;
570 case STRICT_LOW_PART:
571 case NEG: case NOT:
572 case SIGN_EXTEND: case ZERO_EXTEND:
573 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
574 case FLOAT: case FIX:
575 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
576 case ABS:
577 case SQRT:
578 case FFS:
579 case CLZ:
580 case CTZ:
581 case POPCOUNT:
582 case PARITY:
583 case BSWAP:
584 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
585 subst_p, update_p,
586 update_sp_offset, full_p);
587 if (new_rtx != XEXP (x, 0))
588 return gen_rtx_fmt_e (code, GET_MODE (x), new_rtx);
589 return x;
591 case SUBREG:
592 new_rtx = lra_eliminate_regs_1 (insn, SUBREG_REG (x), mem_mode,
593 subst_p, update_p,
594 update_sp_offset, full_p);
596 if (new_rtx != SUBREG_REG (x))
598 int x_size = GET_MODE_SIZE (GET_MODE (x));
599 int new_size = GET_MODE_SIZE (GET_MODE (new_rtx));
601 if (MEM_P (new_rtx) && x_size <= new_size)
603 SUBREG_REG (x) = new_rtx;
604 alter_subreg (&x, false);
605 return x;
607 else if (! subst_p)
609 /* LRA can transform subregs itself. So don't call
610 simplify_gen_subreg until LRA transformations are
611 finished. Function simplify_gen_subreg can do
612 non-trivial transformations (like truncation) which
613 might make LRA work to fail. */
614 SUBREG_REG (x) = new_rtx;
615 return x;
617 else
618 return simplify_gen_subreg (GET_MODE (x), new_rtx,
619 GET_MODE (new_rtx), SUBREG_BYTE (x));
622 return x;
624 case MEM:
625 /* Our only special processing is to pass the mode of the MEM to our
626 recursive call and copy the flags. While we are here, handle this
627 case more efficiently. */
628 return
629 replace_equiv_address_nv
631 lra_eliminate_regs_1 (insn, XEXP (x, 0), GET_MODE (x),
632 subst_p, update_p, update_sp_offset, full_p));
634 case USE:
635 /* Handle insn_list USE that a call to a pure function may generate. */
636 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 0), VOIDmode,
637 subst_p, update_p, update_sp_offset, full_p);
638 if (new_rtx != XEXP (x, 0))
639 return gen_rtx_USE (GET_MODE (x), new_rtx);
640 return x;
642 case CLOBBER:
643 case SET:
644 gcc_unreachable ();
646 default:
647 break;
650 /* Process each of our operands recursively. If any have changed, make a
651 copy of the rtx. */
652 fmt = GET_RTX_FORMAT (code);
653 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
655 if (*fmt == 'e')
657 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, i), mem_mode,
658 subst_p, update_p,
659 update_sp_offset, full_p);
660 if (new_rtx != XEXP (x, i) && ! copied)
662 x = shallow_copy_rtx (x);
663 copied = 1;
665 XEXP (x, i) = new_rtx;
667 else if (*fmt == 'E')
669 int copied_vec = 0;
670 for (j = 0; j < XVECLEN (x, i); j++)
672 new_rtx = lra_eliminate_regs_1 (insn, XVECEXP (x, i, j), mem_mode,
673 subst_p, update_p,
674 update_sp_offset, full_p);
675 if (new_rtx != XVECEXP (x, i, j) && ! copied_vec)
677 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
678 XVEC (x, i)->elem);
679 if (! copied)
681 x = shallow_copy_rtx (x);
682 copied = 1;
684 XVEC (x, i) = new_v;
685 copied_vec = 1;
687 XVECEXP (x, i, j) = new_rtx;
692 return x;
695 /* This function is used externally in subsequent passes of GCC. It
696 always does a full elimination of X. */
698 lra_eliminate_regs (rtx x, machine_mode mem_mode,
699 rtx insn ATTRIBUTE_UNUSED)
701 return lra_eliminate_regs_1 (NULL, x, mem_mode, true, false, 0, true);
704 /* Stack pointer offset before the current insn relative to one at the
705 func start. RTL insns can change SP explicitly. We keep the
706 changes from one insn to another through this variable. */
707 static HOST_WIDE_INT curr_sp_change;
709 /* Scan rtx X for references to elimination source or target registers
710 in contexts that would prevent the elimination from happening.
711 Update the table of eliminables to reflect the changed state.
712 MEM_MODE is the mode of an enclosing MEM rtx, or VOIDmode if not
713 within a MEM. */
714 static void
715 mark_not_eliminable (rtx x, machine_mode mem_mode)
717 enum rtx_code code = GET_CODE (x);
718 struct lra_elim_table *ep;
719 int i, j;
720 const char *fmt;
722 switch (code)
724 case PRE_INC:
725 case POST_INC:
726 case PRE_DEC:
727 case POST_DEC:
728 case POST_MODIFY:
729 case PRE_MODIFY:
730 if (XEXP (x, 0) == stack_pointer_rtx
731 && ((code != PRE_MODIFY && code != POST_MODIFY)
732 || (GET_CODE (XEXP (x, 1)) == PLUS
733 && XEXP (x, 0) == XEXP (XEXP (x, 1), 0)
734 && CONST_INT_P (XEXP (XEXP (x, 1), 1)))))
736 int size = GET_MODE_SIZE (mem_mode);
738 #ifdef PUSH_ROUNDING
739 /* If more bytes than MEM_MODE are pushed, account for
740 them. */
741 size = PUSH_ROUNDING (size);
742 #endif
743 if (code == PRE_DEC || code == POST_DEC)
744 curr_sp_change -= size;
745 else if (code == PRE_INC || code == POST_INC)
746 curr_sp_change += size;
747 else if (code == PRE_MODIFY || code == POST_MODIFY)
748 curr_sp_change += INTVAL (XEXP (XEXP (x, 1), 1));
750 else if (REG_P (XEXP (x, 0))
751 && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER)
753 /* If we modify the source of an elimination rule, disable
754 it. Do the same if it is the destination and not the
755 hard frame register. */
756 for (ep = reg_eliminate;
757 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
758 ep++)
759 if (ep->from_rtx == XEXP (x, 0)
760 || (ep->to_rtx == XEXP (x, 0)
761 && ep->to_rtx != hard_frame_pointer_rtx))
762 setup_can_eliminate (ep, false);
764 return;
766 case USE:
767 if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
768 /* If using a hard register that is the source of an eliminate
769 we still think can be performed, note it cannot be
770 performed since we don't know how this hard register is
771 used. */
772 for (ep = reg_eliminate;
773 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
774 ep++)
775 if (ep->from_rtx == XEXP (x, 0)
776 && ep->to_rtx != hard_frame_pointer_rtx)
777 setup_can_eliminate (ep, false);
778 return;
780 case CLOBBER:
781 if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
782 /* If clobbering a hard register that is the replacement
783 register for an elimination we still think can be
784 performed, note that it cannot be performed. Otherwise, we
785 need not be concerned about it. */
786 for (ep = reg_eliminate;
787 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
788 ep++)
789 if (ep->to_rtx == XEXP (x, 0)
790 && ep->to_rtx != hard_frame_pointer_rtx)
791 setup_can_eliminate (ep, false);
792 return;
794 case SET:
795 if (SET_DEST (x) == stack_pointer_rtx
796 && GET_CODE (SET_SRC (x)) == PLUS
797 && XEXP (SET_SRC (x), 0) == SET_DEST (x)
798 && CONST_INT_P (XEXP (SET_SRC (x), 1)))
800 curr_sp_change += INTVAL (XEXP (SET_SRC (x), 1));
801 return;
803 if (! REG_P (SET_DEST (x))
804 || REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER)
805 mark_not_eliminable (SET_DEST (x), mem_mode);
806 else
808 /* See if this is setting the replacement hard register for
809 an elimination.
811 If DEST is the hard frame pointer, we do nothing because
812 we assume that all assignments to the frame pointer are
813 for non-local gotos and are being done at a time when
814 they are valid and do not disturb anything else. Some
815 machines want to eliminate a fake argument pointer (or
816 even a fake frame pointer) with either the real frame
817 pointer or the stack pointer. Assignments to the hard
818 frame pointer must not prevent this elimination. */
819 for (ep = reg_eliminate;
820 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
821 ep++)
822 if (ep->to_rtx == SET_DEST (x)
823 && SET_DEST (x) != hard_frame_pointer_rtx)
824 setup_can_eliminate (ep, false);
827 mark_not_eliminable (SET_SRC (x), mem_mode);
828 return;
830 case MEM:
831 /* Our only special processing is to pass the mode of the MEM to
832 our recursive call. */
833 mark_not_eliminable (XEXP (x, 0), GET_MODE (x));
834 return;
836 default:
837 break;
840 fmt = GET_RTX_FORMAT (code);
841 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
843 if (*fmt == 'e')
844 mark_not_eliminable (XEXP (x, i), mem_mode);
845 else if (*fmt == 'E')
846 for (j = 0; j < XVECLEN (x, i); j++)
847 mark_not_eliminable (XVECEXP (x, i, j), mem_mode);
853 #ifdef HARD_FRAME_POINTER_REGNUM
855 /* Find offset equivalence note for reg WHAT in INSN and return the
856 found elmination offset. If the note is not found, return NULL.
857 Remove the found note. */
858 static rtx
859 remove_reg_equal_offset_note (rtx_insn *insn, rtx what)
861 rtx link, *link_loc;
863 for (link_loc = &REG_NOTES (insn);
864 (link = *link_loc) != NULL_RTX;
865 link_loc = &XEXP (link, 1))
866 if (REG_NOTE_KIND (link) == REG_EQUAL
867 && GET_CODE (XEXP (link, 0)) == PLUS
868 && XEXP (XEXP (link, 0), 0) == what
869 && CONST_INT_P (XEXP (XEXP (link, 0), 1)))
871 *link_loc = XEXP (link, 1);
872 return XEXP (XEXP (link, 0), 1);
874 return NULL_RTX;
877 #endif
879 /* Scan INSN and eliminate all eliminable hard registers in it.
881 If REPLACE_P is true, do the replacement destructively. Also
882 delete the insn as dead it if it is setting an eliminable register.
884 If REPLACE_P is false, just update the offsets while keeping the
885 base register the same. If FIRST_P, use the sp offset for
886 elimination to sp. Otherwise, use UPDATE_SP_OFFSET for this. If
887 UPDATE_SP_OFFSET is non-zero, don't use difference of the offset
888 and the previous offset. Attach the note about used elimination
889 for insns setting frame pointer to update elimination easy (without
890 parsing already generated elimination insns to find offset
891 previously used) in future. */
893 void
894 eliminate_regs_in_insn (rtx_insn *insn, bool replace_p, bool first_p,
895 HOST_WIDE_INT update_sp_offset)
897 int icode = recog_memoized (insn);
898 rtx old_set = single_set (insn);
899 bool validate_p;
900 int i;
901 rtx substed_operand[MAX_RECOG_OPERANDS];
902 rtx orig_operand[MAX_RECOG_OPERANDS];
903 struct lra_elim_table *ep;
904 rtx plus_src, plus_cst_src;
905 lra_insn_recog_data_t id;
906 struct lra_static_insn_data *static_id;
908 if (icode < 0 && asm_noperands (PATTERN (insn)) < 0 && ! DEBUG_INSN_P (insn))
910 lra_assert (GET_CODE (PATTERN (insn)) == USE
911 || GET_CODE (PATTERN (insn)) == CLOBBER
912 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
913 return;
916 /* Check for setting an eliminable register. */
917 if (old_set != 0 && REG_P (SET_DEST (old_set))
918 && (ep = get_elimination (SET_DEST (old_set))) != NULL)
920 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
921 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
923 bool delete_p = replace_p;
925 #ifdef HARD_FRAME_POINTER_REGNUM
926 if (ep->from == FRAME_POINTER_REGNUM
927 && ep->to == HARD_FRAME_POINTER_REGNUM)
928 /* If this is setting the frame pointer register to the
929 hardware frame pointer register and this is an
930 elimination that will be done (tested above), this
931 insn is really adjusting the frame pointer downward
932 to compensate for the adjustment done before a
933 nonlocal goto. */
935 rtx src = SET_SRC (old_set);
936 rtx off = remove_reg_equal_offset_note (insn, ep->to_rtx);
938 /* We should never process such insn with non-zero
939 UPDATE_SP_OFFSET. */
940 lra_assert (update_sp_offset == 0);
942 if (off != NULL_RTX
943 || src == ep->to_rtx
944 || (GET_CODE (src) == PLUS
945 && XEXP (src, 0) == ep->to_rtx
946 && CONST_INT_P (XEXP (src, 1))))
948 HOST_WIDE_INT offset;
950 if (replace_p)
952 SET_DEST (old_set) = ep->to_rtx;
953 lra_update_insn_recog_data (insn);
954 return;
956 offset = (off != NULL_RTX ? INTVAL (off)
957 : src == ep->to_rtx ? 0 : INTVAL (XEXP (src, 1)));
958 offset -= (ep->offset - ep->previous_offset);
959 src = plus_constant (Pmode, ep->to_rtx, offset);
961 /* First see if this insn remains valid when we
962 make the change. If not, keep the INSN_CODE
963 the same and let the constraint pass fit it
964 up. */
965 validate_change (insn, &SET_SRC (old_set), src, 1);
966 validate_change (insn, &SET_DEST (old_set),
967 ep->from_rtx, 1);
968 if (! apply_change_group ())
970 SET_SRC (old_set) = src;
971 SET_DEST (old_set) = ep->from_rtx;
973 lra_update_insn_recog_data (insn);
974 /* Add offset note for future updates. */
975 add_reg_note (insn, REG_EQUAL, src);
976 return;
979 #endif
981 /* This insn isn't serving a useful purpose. We delete it
982 when REPLACE is set. */
983 if (delete_p)
984 lra_delete_dead_insn (insn);
985 return;
989 /* We allow one special case which happens to work on all machines we
990 currently support: a single set with the source or a REG_EQUAL
991 note being a PLUS of an eliminable register and a constant. */
992 plus_src = plus_cst_src = 0;
993 if (old_set && REG_P (SET_DEST (old_set)))
995 if (GET_CODE (SET_SRC (old_set)) == PLUS)
996 plus_src = SET_SRC (old_set);
997 /* First see if the source is of the form (plus (...) CST). */
998 if (plus_src
999 && CONST_INT_P (XEXP (plus_src, 1)))
1000 plus_cst_src = plus_src;
1001 /* Check that the first operand of the PLUS is a hard reg or
1002 the lowpart subreg of one. */
1003 if (plus_cst_src)
1005 rtx reg = XEXP (plus_cst_src, 0);
1007 if (GET_CODE (reg) == SUBREG && subreg_lowpart_p (reg))
1008 reg = SUBREG_REG (reg);
1010 if (!REG_P (reg) || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
1011 plus_cst_src = 0;
1014 if (plus_cst_src)
1016 rtx reg = XEXP (plus_cst_src, 0);
1017 HOST_WIDE_INT offset = INTVAL (XEXP (plus_cst_src, 1));
1019 if (GET_CODE (reg) == SUBREG)
1020 reg = SUBREG_REG (reg);
1022 if (REG_P (reg) && (ep = get_elimination (reg)) != NULL)
1024 rtx to_rtx = replace_p ? ep->to_rtx : ep->from_rtx;
1026 if (! replace_p)
1028 if (update_sp_offset == 0)
1029 offset += (ep->offset - ep->previous_offset);
1030 if (ep->to_rtx == stack_pointer_rtx)
1032 if (first_p)
1033 offset -= lra_get_insn_recog_data (insn)->sp_offset;
1034 else
1035 offset += update_sp_offset;
1037 offset = trunc_int_for_mode (offset, GET_MODE (plus_cst_src));
1040 if (GET_CODE (XEXP (plus_cst_src, 0)) == SUBREG)
1041 to_rtx = gen_lowpart (GET_MODE (XEXP (plus_cst_src, 0)), to_rtx);
1042 /* If we have a nonzero offset, and the source is already a
1043 simple REG, the following transformation would increase
1044 the cost of the insn by replacing a simple REG with (plus
1045 (reg sp) CST). So try only when we already had a PLUS
1046 before. */
1047 if (offset == 0 || plus_src)
1049 rtx new_src = plus_constant (GET_MODE (to_rtx), to_rtx, offset);
1051 old_set = single_set (insn);
1053 /* First see if this insn remains valid when we make the
1054 change. If not, try to replace the whole pattern
1055 with a simple set (this may help if the original insn
1056 was a PARALLEL that was only recognized as single_set
1057 due to REG_UNUSED notes). If this isn't valid
1058 either, keep the INSN_CODE the same and let the
1059 constraint pass fix it up. */
1060 if (! validate_change (insn, &SET_SRC (old_set), new_src, 0))
1062 rtx new_pat = gen_rtx_SET (SET_DEST (old_set), new_src);
1064 if (! validate_change (insn, &PATTERN (insn), new_pat, 0))
1065 SET_SRC (old_set) = new_src;
1067 lra_update_insn_recog_data (insn);
1068 /* This can't have an effect on elimination offsets, so skip
1069 right to the end. */
1070 return;
1075 /* Eliminate all eliminable registers occurring in operands that
1076 can be handled by the constraint pass. */
1077 id = lra_get_insn_recog_data (insn);
1078 static_id = id->insn_static_data;
1079 validate_p = false;
1080 for (i = 0; i < static_id->n_operands; i++)
1082 orig_operand[i] = *id->operand_loc[i];
1083 substed_operand[i] = *id->operand_loc[i];
1085 /* For an asm statement, every operand is eliminable. */
1086 if (icode < 0 || insn_data[icode].operand[i].eliminable)
1088 /* Check for setting a hard register that we know about. */
1089 if (static_id->operand[i].type != OP_IN
1090 && REG_P (orig_operand[i]))
1092 /* If we are assigning to a hard register that can be
1093 eliminated, it must be as part of a PARALLEL, since
1094 the code above handles single SETs. This reg can not
1095 be longer eliminated -- it is forced by
1096 mark_not_eliminable. */
1097 for (ep = reg_eliminate;
1098 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
1099 ep++)
1100 lra_assert (ep->from_rtx != orig_operand[i]
1101 || ! ep->can_eliminate);
1104 /* Companion to the above plus substitution, we can allow
1105 invariants as the source of a plain move. */
1106 substed_operand[i]
1107 = lra_eliminate_regs_1 (insn, *id->operand_loc[i], VOIDmode,
1108 replace_p, ! replace_p && ! first_p,
1109 update_sp_offset, first_p);
1110 if (substed_operand[i] != orig_operand[i])
1111 validate_p = true;
1115 if (! validate_p)
1116 return;
1118 /* Substitute the operands; the new values are in the substed_operand
1119 array. */
1120 for (i = 0; i < static_id->n_operands; i++)
1121 *id->operand_loc[i] = substed_operand[i];
1122 for (i = 0; i < static_id->n_dups; i++)
1123 *id->dup_loc[i] = substed_operand[(int) static_id->dup_num[i]];
1125 /* If we had a move insn but now we don't, re-recognize it.
1126 This will cause spurious re-recognition if the old move had a
1127 PARALLEL since the new one still will, but we can't call
1128 single_set without having put new body into the insn and the
1129 re-recognition won't hurt in this rare case. */
1130 id = lra_update_insn_recog_data (insn);
1131 static_id = id->insn_static_data;
1134 /* Spill pseudos which are assigned to hard registers in SET. Add
1135 affected insns for processing in the subsequent constraint
1136 pass. */
1137 static void
1138 spill_pseudos (HARD_REG_SET set)
1140 int i;
1141 bitmap_head to_process;
1142 rtx_insn *insn;
1144 if (hard_reg_set_empty_p (set))
1145 return;
1146 if (lra_dump_file != NULL)
1148 fprintf (lra_dump_file, " Spilling non-eliminable hard regs:");
1149 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1150 if (TEST_HARD_REG_BIT (set, i))
1151 fprintf (lra_dump_file, " %d", i);
1152 fprintf (lra_dump_file, "\n");
1154 bitmap_initialize (&to_process, &reg_obstack);
1155 for (i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
1156 if (lra_reg_info[i].nrefs != 0 && reg_renumber[i] >= 0
1157 && overlaps_hard_reg_set_p (set,
1158 PSEUDO_REGNO_MODE (i), reg_renumber[i]))
1160 if (lra_dump_file != NULL)
1161 fprintf (lra_dump_file, " Spilling r%d(%d)\n",
1162 i, reg_renumber[i]);
1163 reg_renumber[i] = -1;
1164 bitmap_ior_into (&to_process, &lra_reg_info[i].insn_bitmap);
1166 IOR_HARD_REG_SET (lra_no_alloc_regs, set);
1167 for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
1168 if (bitmap_bit_p (&to_process, INSN_UID (insn)))
1170 lra_push_insn (insn);
1171 lra_set_used_insn_alternative (insn, -1);
1173 bitmap_clear (&to_process);
1176 /* Update all offsets and possibility for elimination on eliminable
1177 registers. Spill pseudos assigned to registers which are
1178 uneliminable, update LRA_NO_ALLOC_REGS and ELIMINABLE_REG_SET. Add
1179 insns to INSNS_WITH_CHANGED_OFFSETS containing eliminable hard
1180 registers whose offsets should be changed. Return true if any
1181 elimination offset changed. */
1182 static bool
1183 update_reg_eliminate (bitmap insns_with_changed_offsets)
1185 bool prev, result;
1186 struct lra_elim_table *ep, *ep1;
1187 HARD_REG_SET temp_hard_reg_set;
1189 /* Clear self elimination offsets. */
1190 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1191 self_elim_offsets[ep->from] = 0;
1192 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1194 /* If it is a currently used elimination: update the previous
1195 offset. */
1196 if (elimination_map[ep->from] == ep)
1197 ep->previous_offset = ep->offset;
1199 prev = ep->prev_can_eliminate;
1200 setup_can_eliminate (ep, targetm.can_eliminate (ep->from, ep->to));
1201 if (ep->can_eliminate && ! prev)
1203 /* It is possible that not eliminable register becomes
1204 eliminable because we took other reasons into account to
1205 set up eliminable regs in the initial set up. Just
1206 ignore new eliminable registers. */
1207 setup_can_eliminate (ep, false);
1208 continue;
1210 if (ep->can_eliminate != prev && elimination_map[ep->from] == ep)
1212 /* We cannot use this elimination anymore -- find another
1213 one. */
1214 if (lra_dump_file != NULL)
1215 fprintf (lra_dump_file,
1216 " Elimination %d to %d is not possible anymore\n",
1217 ep->from, ep->to);
1218 /* If after processing RTL we decides that SP can be used as
1219 a result of elimination, it can not be changed. */
1220 gcc_assert ((ep->to_rtx != stack_pointer_rtx)
1221 || (ep->from < FIRST_PSEUDO_REGISTER
1222 && fixed_regs [ep->from]));
1223 /* Mark that is not eliminable anymore. */
1224 elimination_map[ep->from] = NULL;
1225 for (ep1 = ep + 1; ep1 < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep1++)
1226 if (ep1->can_eliminate && ep1->from == ep->from)
1227 break;
1228 if (ep1 < &reg_eliminate[NUM_ELIMINABLE_REGS])
1230 if (lra_dump_file != NULL)
1231 fprintf (lra_dump_file, " Using elimination %d to %d now\n",
1232 ep1->from, ep1->to);
1233 lra_assert (ep1->previous_offset == 0);
1234 ep1->previous_offset = ep->offset;
1236 else
1238 /* There is no elimination anymore just use the hard
1239 register `from' itself. Setup self elimination
1240 offset to restore the original offset values. */
1241 if (lra_dump_file != NULL)
1242 fprintf (lra_dump_file, " %d is not eliminable at all\n",
1243 ep->from);
1244 self_elim_offsets[ep->from] = -ep->offset;
1245 if (ep->offset != 0)
1246 bitmap_ior_into (insns_with_changed_offsets,
1247 &lra_reg_info[ep->from].insn_bitmap);
1251 #ifdef ELIMINABLE_REGS
1252 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->offset);
1253 #else
1254 INITIAL_FRAME_POINTER_OFFSET (ep->offset);
1255 #endif
1257 setup_elimination_map ();
1258 result = false;
1259 CLEAR_HARD_REG_SET (temp_hard_reg_set);
1260 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1261 if (elimination_map[ep->from] == NULL)
1262 SET_HARD_REG_BIT (temp_hard_reg_set, ep->from);
1263 else if (elimination_map[ep->from] == ep)
1265 /* Prevent the hard register into which we eliminate from
1266 the usage for pseudos. */
1267 if (ep->from != ep->to)
1268 SET_HARD_REG_BIT (temp_hard_reg_set, ep->to);
1269 if (ep->previous_offset != ep->offset)
1271 bitmap_ior_into (insns_with_changed_offsets,
1272 &lra_reg_info[ep->from].insn_bitmap);
1274 /* Update offset when the eliminate offset have been
1275 changed. */
1276 lra_update_reg_val_offset (lra_reg_info[ep->from].val,
1277 ep->offset - ep->previous_offset);
1278 result = true;
1281 IOR_HARD_REG_SET (lra_no_alloc_regs, temp_hard_reg_set);
1282 AND_COMPL_HARD_REG_SET (eliminable_regset, temp_hard_reg_set);
1283 spill_pseudos (temp_hard_reg_set);
1284 return result;
1287 /* Initialize the table of hard registers to eliminate.
1288 Pre-condition: global flag frame_pointer_needed has been set before
1289 calling this function. */
1290 static void
1291 init_elim_table (void)
1293 struct lra_elim_table *ep;
1294 #ifdef ELIMINABLE_REGS
1295 bool value_p;
1296 const struct elim_table_1 *ep1;
1297 #endif
1299 if (!reg_eliminate)
1300 reg_eliminate = XCNEWVEC (struct lra_elim_table, NUM_ELIMINABLE_REGS);
1302 memset (self_elim_offsets, 0, sizeof (self_elim_offsets));
1303 /* Initiate member values which will be never changed. */
1304 self_elim_table.can_eliminate = self_elim_table.prev_can_eliminate = true;
1305 self_elim_table.previous_offset = 0;
1306 #ifdef ELIMINABLE_REGS
1307 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
1308 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
1310 ep->offset = ep->previous_offset = 0;
1311 ep->from = ep1->from;
1312 ep->to = ep1->to;
1313 value_p = (targetm.can_eliminate (ep->from, ep->to)
1314 && ! (ep->to == STACK_POINTER_REGNUM
1315 && frame_pointer_needed
1316 && (! SUPPORTS_STACK_ALIGNMENT
1317 || ! stack_realign_fp)));
1318 setup_can_eliminate (ep, value_p);
1320 #else
1321 reg_eliminate[0].offset = reg_eliminate[0].previous_offset = 0;
1322 reg_eliminate[0].from = reg_eliminate_1[0].from;
1323 reg_eliminate[0].to = reg_eliminate_1[0].to;
1324 setup_can_eliminate (&reg_eliminate[0], ! frame_pointer_needed);
1325 #endif
1327 /* Build the FROM and TO REG rtx's. Note that code in gen_rtx_REG
1328 will cause, e.g., gen_rtx_REG (Pmode, STACK_POINTER_REGNUM) to
1329 equal stack_pointer_rtx. We depend on this. Threfore we switch
1330 off that we are in LRA temporarily. */
1331 lra_in_progress = 0;
1332 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1334 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
1335 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
1336 eliminable_reg_rtx[ep->from] = ep->from_rtx;
1338 lra_in_progress = 1;
1341 /* Function for initialization of elimination once per function. It
1342 sets up sp offset for each insn. */
1343 static void
1344 init_elimination (void)
1346 bool stop_to_sp_elimination_p;
1347 basic_block bb;
1348 rtx_insn *insn;
1349 struct lra_elim_table *ep;
1351 init_elim_table ();
1352 FOR_EACH_BB_FN (bb, cfun)
1354 curr_sp_change = 0;
1355 stop_to_sp_elimination_p = false;
1356 FOR_BB_INSNS (bb, insn)
1357 if (INSN_P (insn))
1359 lra_get_insn_recog_data (insn)->sp_offset = curr_sp_change;
1360 if (NONDEBUG_INSN_P (insn))
1362 mark_not_eliminable (PATTERN (insn), VOIDmode);
1363 if (curr_sp_change != 0
1364 && find_reg_note (insn, REG_LABEL_OPERAND, NULL_RTX))
1365 stop_to_sp_elimination_p = true;
1368 if (! frame_pointer_needed
1369 && (curr_sp_change != 0 || stop_to_sp_elimination_p)
1370 && bb->succs && bb->succs->length () != 0)
1371 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1372 if (ep->to == STACK_POINTER_REGNUM)
1373 setup_can_eliminate (ep, false);
1375 setup_elimination_map ();
1378 /* Eliminate hard reg given by its location LOC. */
1379 void
1380 lra_eliminate_reg_if_possible (rtx *loc)
1382 int regno;
1383 struct lra_elim_table *ep;
1385 lra_assert (REG_P (*loc));
1386 if ((regno = REGNO (*loc)) >= FIRST_PSEUDO_REGISTER
1387 || ! TEST_HARD_REG_BIT (lra_no_alloc_regs, regno))
1388 return;
1389 if ((ep = get_elimination (*loc)) != NULL)
1390 *loc = ep->to_rtx;
1393 /* Do (final if FINAL_P or first if FIRST_P) elimination in INSN. Add
1394 the insn for subsequent processing in the constraint pass, update
1395 the insn info. */
1396 static void
1397 process_insn_for_elimination (rtx_insn *insn, bool final_p, bool first_p)
1399 eliminate_regs_in_insn (insn, final_p, first_p, 0);
1400 if (! final_p)
1402 /* Check that insn changed its code. This is a case when a move
1403 insn becomes an add insn and we do not want to process the
1404 insn as a move anymore. */
1405 int icode = recog (PATTERN (insn), insn, 0);
1407 if (icode >= 0 && icode != INSN_CODE (insn))
1409 INSN_CODE (insn) = icode;
1410 lra_update_insn_recog_data (insn);
1412 lra_update_insn_regno_info (insn);
1413 lra_push_insn (insn);
1414 lra_set_used_insn_alternative (insn, -1);
1418 /* Entry function to do final elimination if FINAL_P or to update
1419 elimination register offsets (FIRST_P if we are doing it the first
1420 time). */
1421 void
1422 lra_eliminate (bool final_p, bool first_p)
1424 unsigned int uid;
1425 bitmap_head insns_with_changed_offsets;
1426 bitmap_iterator bi;
1427 struct lra_elim_table *ep;
1429 gcc_assert (! final_p || ! first_p);
1431 timevar_push (TV_LRA_ELIMINATE);
1433 if (first_p)
1434 init_elimination ();
1436 bitmap_initialize (&insns_with_changed_offsets, &reg_obstack);
1437 if (final_p)
1439 #ifdef ENABLE_CHECKING
1440 update_reg_eliminate (&insns_with_changed_offsets);
1441 if (! bitmap_empty_p (&insns_with_changed_offsets))
1442 gcc_unreachable ();
1443 #endif
1444 /* We change eliminable hard registers in insns so we should do
1445 this for all insns containing any eliminable hard
1446 register. */
1447 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1448 if (elimination_map[ep->from] != NULL)
1449 bitmap_ior_into (&insns_with_changed_offsets,
1450 &lra_reg_info[ep->from].insn_bitmap);
1452 else if (! update_reg_eliminate (&insns_with_changed_offsets))
1453 goto lra_eliminate_done;
1454 if (lra_dump_file != NULL)
1456 fprintf (lra_dump_file, "New elimination table:\n");
1457 print_elim_table (lra_dump_file);
1459 EXECUTE_IF_SET_IN_BITMAP (&insns_with_changed_offsets, 0, uid, bi)
1460 /* A dead insn can be deleted in process_insn_for_elimination. */
1461 if (lra_insn_recog_data[uid] != NULL)
1462 process_insn_for_elimination (lra_insn_recog_data[uid]->insn,
1463 final_p, first_p);
1464 bitmap_clear (&insns_with_changed_offsets);
1466 lra_eliminate_done:
1467 timevar_pop (TV_LRA_ELIMINATE);