* ipa-inline-analysis.c: Include gimplify.h
[official-gcc.git] / gcc / lra-eliminations.c
blob38b1fbb2aab09e924580b08698a8e98f075b3541
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 "target.h"
59 #include "rtl.h"
60 #include "tree.h"
61 #include "df.h"
62 #include "tm_p.h"
63 #include "optabs.h"
64 #include "regs.h"
65 #include "ira.h"
66 #include "recog.h"
67 #include "output.h"
68 #include "rtl-error.h"
69 #include "lra-int.h"
71 /* This structure is used to record information about hard register
72 eliminations. */
73 struct lra_elim_table
75 /* Hard register number to be eliminated. */
76 int from;
77 /* Hard register number used as replacement. */
78 int to;
79 /* Difference between values of the two hard registers above on
80 previous iteration. */
81 HOST_WIDE_INT previous_offset;
82 /* Difference between the values on the current iteration. */
83 HOST_WIDE_INT offset;
84 /* Nonzero if this elimination can be done. */
85 bool can_eliminate;
86 /* CAN_ELIMINATE since the last check. */
87 bool prev_can_eliminate;
88 /* REG rtx for the register to be eliminated. We cannot simply
89 compare the number since we might then spuriously replace a hard
90 register corresponding to a pseudo assigned to the reg to be
91 eliminated. */
92 rtx from_rtx;
93 /* REG rtx for the replacement. */
94 rtx to_rtx;
97 /* The elimination table. Each array entry describes one possible way
98 of eliminating a register in favor of another. If there is more
99 than one way of eliminating a particular register, the most
100 preferred should be specified first. */
101 static struct lra_elim_table *reg_eliminate = 0;
103 /* This is an intermediate structure to initialize the table. It has
104 exactly the members provided by ELIMINABLE_REGS. */
105 static const struct elim_table_1
107 const int from;
108 const int to;
109 } reg_eliminate_1[] =
111 /* If a set of eliminable hard registers was specified, define the
112 table from it. Otherwise, default to the normal case of the frame
113 pointer being replaced by the stack pointer. */
115 #ifdef ELIMINABLE_REGS
116 ELIMINABLE_REGS;
117 #else
118 {{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}};
119 #endif
121 #define NUM_ELIMINABLE_REGS ARRAY_SIZE (reg_eliminate_1)
123 /* Print info about elimination table to file F. */
124 static void
125 print_elim_table (FILE *f)
127 struct lra_elim_table *ep;
129 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
130 fprintf (f, "%s eliminate %d to %d (offset=" HOST_WIDE_INT_PRINT_DEC
131 ", prev_offset=" HOST_WIDE_INT_PRINT_DEC ")\n",
132 ep->can_eliminate ? "Can" : "Can't",
133 ep->from, ep->to, ep->offset, ep->previous_offset);
136 /* Print info about elimination table to stderr. */
137 void
138 lra_debug_elim_table (void)
140 print_elim_table (stderr);
143 /* Setup possibility of elimination in elimination table element EP to
144 VALUE. Setup FRAME_POINTER_NEEDED if elimination from frame
145 pointer to stack pointer is not possible anymore. */
146 static void
147 setup_can_eliminate (struct lra_elim_table *ep, bool value)
149 ep->can_eliminate = ep->prev_can_eliminate = value;
150 if (! value
151 && ep->from == FRAME_POINTER_REGNUM && ep->to == STACK_POINTER_REGNUM)
152 frame_pointer_needed = 1;
153 if (!frame_pointer_needed)
154 REGNO_POINTER_ALIGN (HARD_FRAME_POINTER_REGNUM) = 0;
157 /* Map: eliminable "from" register -> its current elimination,
158 or NULL if none. The elimination table may contain more than
159 one elimination for the same hard register, but this map specifies
160 the one that we are currently using. */
161 static struct lra_elim_table *elimination_map[FIRST_PSEUDO_REGISTER];
163 /* When an eliminable hard register becomes not eliminable, we use the
164 following special structure to restore original offsets for the
165 register. */
166 static struct lra_elim_table self_elim_table;
168 /* Offsets should be used to restore original offsets for eliminable
169 hard register which just became not eliminable. Zero,
170 otherwise. */
171 static HOST_WIDE_INT self_elim_offsets[FIRST_PSEUDO_REGISTER];
173 /* Map: hard regno -> RTL presentation. RTL presentations of all
174 potentially eliminable hard registers are stored in the map. */
175 static rtx eliminable_reg_rtx[FIRST_PSEUDO_REGISTER];
177 /* Set up ELIMINATION_MAP of the currently used eliminations. */
178 static void
179 setup_elimination_map (void)
181 int i;
182 struct lra_elim_table *ep;
184 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
185 elimination_map[i] = NULL;
186 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
187 if (ep->can_eliminate && elimination_map[ep->from] == NULL)
188 elimination_map[ep->from] = ep;
193 /* Compute the sum of X and Y, making canonicalizations assumed in an
194 address, namely: sum constant integers, surround the sum of two
195 constants with a CONST, put the constant as the second operand, and
196 group the constant on the outermost sum.
198 This routine assumes both inputs are already in canonical form. */
199 static rtx
200 form_sum (rtx x, rtx y)
202 machine_mode mode = GET_MODE (x);
204 if (mode == VOIDmode)
205 mode = GET_MODE (y);
207 if (mode == VOIDmode)
208 mode = Pmode;
210 if (CONST_INT_P (x))
211 return plus_constant (mode, y, INTVAL (x));
212 else if (CONST_INT_P (y))
213 return plus_constant (mode, x, INTVAL (y));
214 else if (CONSTANT_P (x))
215 std::swap (x, y);
217 if (GET_CODE (x) == PLUS && CONSTANT_P (XEXP (x, 1)))
218 return form_sum (XEXP (x, 0), form_sum (XEXP (x, 1), y));
220 /* Note that if the operands of Y are specified in the opposite
221 order in the recursive calls below, infinite recursion will
222 occur. */
223 if (GET_CODE (y) == PLUS && CONSTANT_P (XEXP (y, 1)))
224 return form_sum (form_sum (x, XEXP (y, 0)), XEXP (y, 1));
226 /* If both constant, encapsulate sum. Otherwise, just form sum. A
227 constant will have been placed second. */
228 if (CONSTANT_P (x) && CONSTANT_P (y))
230 if (GET_CODE (x) == CONST)
231 x = XEXP (x, 0);
232 if (GET_CODE (y) == CONST)
233 y = XEXP (y, 0);
235 return gen_rtx_CONST (VOIDmode, gen_rtx_PLUS (mode, x, y));
238 return gen_rtx_PLUS (mode, x, y);
241 /* Return the current substitution hard register of the elimination of
242 HARD_REGNO. If HARD_REGNO is not eliminable, return itself. */
244 lra_get_elimination_hard_regno (int hard_regno)
246 struct lra_elim_table *ep;
248 if (hard_regno < 0 || hard_regno >= FIRST_PSEUDO_REGISTER)
249 return hard_regno;
250 if ((ep = elimination_map[hard_regno]) == NULL)
251 return hard_regno;
252 return ep->to;
255 /* Return elimination which will be used for hard reg REG, NULL
256 otherwise. */
257 static struct lra_elim_table *
258 get_elimination (rtx reg)
260 int hard_regno;
261 struct lra_elim_table *ep;
262 HOST_WIDE_INT offset;
264 lra_assert (REG_P (reg));
265 if ((hard_regno = REGNO (reg)) < 0 || hard_regno >= FIRST_PSEUDO_REGISTER)
266 return NULL;
267 if ((ep = elimination_map[hard_regno]) != NULL)
268 return ep->from_rtx != reg ? NULL : ep;
269 if ((offset = self_elim_offsets[hard_regno]) == 0)
270 return NULL;
271 /* This is an iteration to restore offsets just after HARD_REGNO
272 stopped to be eliminable. */
273 self_elim_table.from = self_elim_table.to = hard_regno;
274 self_elim_table.from_rtx
275 = self_elim_table.to_rtx
276 = eliminable_reg_rtx[hard_regno];
277 lra_assert (self_elim_table.from_rtx != NULL);
278 self_elim_table.offset = offset;
279 return &self_elim_table;
282 /* Scan X and replace any eliminable registers (such as fp) with a
283 replacement (such as sp) if SUBST_P, plus an offset. The offset is
284 a change in the offset between the eliminable register and its
285 substitution if UPDATE_P, or the full offset if FULL_P, or
286 otherwise zero. If FULL_P, we also use the SP offsets for
287 elimination to SP. If UPDATE_P, use UPDATE_SP_OFFSET for updating
288 offsets of register elimnable to SP. If UPDATE_SP_OFFSET is
289 non-zero, don't use difference of the offset and the previous
290 offset.
292 MEM_MODE is the mode of an enclosing MEM. We need this to know how
293 much to adjust a register for, e.g., PRE_DEC. Also, if we are
294 inside a MEM, we are allowed to replace a sum of a hard register
295 and the constant zero with the hard register, which we cannot do
296 outside a MEM. In addition, we need to record the fact that a
297 hard register is referenced outside a MEM.
299 If we make full substitution to SP for non-null INSN, add the insn
300 sp offset. */
302 lra_eliminate_regs_1 (rtx_insn *insn, rtx x, machine_mode mem_mode,
303 bool subst_p, bool update_p,
304 HOST_WIDE_INT update_sp_offset, bool full_p)
306 enum rtx_code code = GET_CODE (x);
307 struct lra_elim_table *ep;
308 rtx new_rtx;
309 int i, j;
310 const char *fmt;
311 int copied = 0;
313 lra_assert (!update_p || !full_p);
314 lra_assert (update_sp_offset == 0 || (!subst_p && update_p && !full_p));
315 if (! current_function_decl)
316 return x;
318 switch (code)
320 CASE_CONST_ANY:
321 case CONST:
322 case SYMBOL_REF:
323 case CODE_LABEL:
324 case PC:
325 case CC0:
326 case ASM_INPUT:
327 case ADDR_VEC:
328 case ADDR_DIFF_VEC:
329 case RETURN:
330 return x;
332 case REG:
333 /* First handle the case where we encounter a bare hard register
334 that is eliminable. Replace it with a PLUS. */
335 if ((ep = get_elimination (x)) != NULL)
337 rtx to = subst_p ? ep->to_rtx : ep->from_rtx;
339 if (update_sp_offset != 0)
341 if (ep->to_rtx == stack_pointer_rtx)
342 return plus_constant (Pmode, to, update_sp_offset);
343 return to;
345 else if (update_p)
346 return plus_constant (Pmode, to, ep->offset - ep->previous_offset);
347 else if (full_p)
348 return plus_constant (Pmode, to,
349 ep->offset
350 - (insn != NULL_RTX
351 && ep->to_rtx == stack_pointer_rtx
352 ? lra_get_insn_recog_data (insn)->sp_offset
353 : 0));
354 else
355 return to;
357 return x;
359 case PLUS:
360 /* If this is the sum of an eliminable register and a constant, rework
361 the sum. */
362 if (REG_P (XEXP (x, 0)) && CONSTANT_P (XEXP (x, 1)))
364 if ((ep = get_elimination (XEXP (x, 0))) != NULL)
366 HOST_WIDE_INT offset;
367 rtx to = subst_p ? ep->to_rtx : ep->from_rtx;
369 if (! update_p && ! full_p)
370 return gen_rtx_PLUS (Pmode, to, XEXP (x, 1));
372 if (update_sp_offset != 0)
373 offset = ep->to_rtx == stack_pointer_rtx ? update_sp_offset : 0;
374 else
375 offset = (update_p
376 ? ep->offset - ep->previous_offset : ep->offset);
377 if (full_p && insn != NULL_RTX && ep->to_rtx == stack_pointer_rtx)
378 offset -= lra_get_insn_recog_data (insn)->sp_offset;
379 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) == -offset)
380 return to;
381 else
382 return gen_rtx_PLUS (Pmode, to,
383 plus_constant (Pmode,
384 XEXP (x, 1), offset));
387 /* If the hard register is not eliminable, we are done since
388 the other operand is a constant. */
389 return x;
392 /* If this is part of an address, we want to bring any constant
393 to the outermost PLUS. We will do this by doing hard
394 register replacement in our operands and seeing if a constant
395 shows up in one of them.
397 Note that there is no risk of modifying the structure of the
398 insn, since we only get called for its operands, thus we are
399 either modifying the address inside a MEM, or something like
400 an address operand of a load-address insn. */
403 rtx new0 = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
404 subst_p, update_p,
405 update_sp_offset, full_p);
406 rtx new1 = lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
407 subst_p, update_p,
408 update_sp_offset, full_p);
410 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
411 return form_sum (new0, new1);
413 return x;
415 case MULT:
416 /* If this is the product of an eliminable hard register and a
417 constant, apply the distribute law and move the constant out
418 so that we have (plus (mult ..) ..). This is needed in order
419 to keep load-address insns valid. This case is pathological.
420 We ignore the possibility of overflow here. */
421 if (REG_P (XEXP (x, 0)) && CONST_INT_P (XEXP (x, 1))
422 && (ep = get_elimination (XEXP (x, 0))) != NULL)
424 rtx to = subst_p ? ep->to_rtx : ep->from_rtx;
426 if (update_sp_offset != 0)
428 if (ep->to_rtx == stack_pointer_rtx)
429 return plus_constant (Pmode,
430 gen_rtx_MULT (Pmode, to, XEXP (x, 1)),
431 update_sp_offset * INTVAL (XEXP (x, 1)));
432 return gen_rtx_MULT (Pmode, to, XEXP (x, 1));
434 else if (update_p)
435 return plus_constant (Pmode,
436 gen_rtx_MULT (Pmode, to, XEXP (x, 1)),
437 (ep->offset - ep->previous_offset)
438 * INTVAL (XEXP (x, 1)));
439 else if (full_p)
441 HOST_WIDE_INT offset = ep->offset;
443 if (insn != NULL_RTX && ep->to_rtx == stack_pointer_rtx)
444 offset -= lra_get_insn_recog_data (insn)->sp_offset;
445 return
446 plus_constant (Pmode,
447 gen_rtx_MULT (Pmode, to, XEXP (x, 1)),
448 offset * INTVAL (XEXP (x, 1)));
450 else
451 return gen_rtx_MULT (Pmode, to, XEXP (x, 1));
454 /* ... fall through ... */
456 case CALL:
457 case COMPARE:
458 /* See comments before PLUS about handling MINUS. */
459 case MINUS:
460 case DIV: case UDIV:
461 case MOD: case UMOD:
462 case AND: case IOR: case XOR:
463 case ROTATERT: case ROTATE:
464 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
465 case NE: case EQ:
466 case GE: case GT: case GEU: case GTU:
467 case LE: case LT: case LEU: case LTU:
469 rtx new0 = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
470 subst_p, update_p,
471 update_sp_offset, full_p);
472 rtx new1 = XEXP (x, 1)
473 ? lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
474 subst_p, update_p,
475 update_sp_offset, full_p) : 0;
477 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
478 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
480 return x;
482 case EXPR_LIST:
483 /* If we have something in XEXP (x, 0), the usual case,
484 eliminate it. */
485 if (XEXP (x, 0))
487 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
488 subst_p, update_p,
489 update_sp_offset, full_p);
490 if (new_rtx != XEXP (x, 0))
492 /* If this is a REG_DEAD note, it is not valid anymore.
493 Using the eliminated version could result in creating a
494 REG_DEAD note for the stack or frame pointer. */
495 if (REG_NOTE_KIND (x) == REG_DEAD)
496 return (XEXP (x, 1)
497 ? lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
498 subst_p, update_p,
499 update_sp_offset, full_p)
500 : NULL_RTX);
502 x = alloc_reg_note (REG_NOTE_KIND (x), new_rtx, XEXP (x, 1));
506 /* ... fall through ... */
508 case INSN_LIST:
509 case INT_LIST:
510 /* Now do eliminations in the rest of the chain. If this was
511 an EXPR_LIST, this might result in allocating more memory than is
512 strictly needed, but it simplifies the code. */
513 if (XEXP (x, 1))
515 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
516 subst_p, update_p,
517 update_sp_offset, full_p);
518 if (new_rtx != XEXP (x, 1))
519 return
520 gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x),
521 XEXP (x, 0), new_rtx);
523 return x;
525 case PRE_INC:
526 case POST_INC:
527 case PRE_DEC:
528 case POST_DEC:
529 /* We do not support elimination of a register that is modified.
530 elimination_effects has already make sure that this does not
531 happen. */
532 return x;
534 case PRE_MODIFY:
535 case POST_MODIFY:
536 /* We do not support elimination of a hard register that is
537 modified. LRA has already make sure that this does not
538 happen. The only remaining case we need to consider here is
539 that the increment value may be an eliminable register. */
540 if (GET_CODE (XEXP (x, 1)) == PLUS
541 && XEXP (XEXP (x, 1), 0) == XEXP (x, 0))
543 rtx new_rtx = lra_eliminate_regs_1 (insn, XEXP (XEXP (x, 1), 1),
544 mem_mode, subst_p, update_p,
545 update_sp_offset, full_p);
547 if (new_rtx != XEXP (XEXP (x, 1), 1))
548 return gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (x, 0),
549 gen_rtx_PLUS (GET_MODE (x),
550 XEXP (x, 0), new_rtx));
552 return x;
554 case STRICT_LOW_PART:
555 case NEG: case NOT:
556 case SIGN_EXTEND: case ZERO_EXTEND:
557 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
558 case FLOAT: case FIX:
559 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
560 case ABS:
561 case SQRT:
562 case FFS:
563 case CLZ:
564 case CTZ:
565 case POPCOUNT:
566 case PARITY:
567 case BSWAP:
568 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
569 subst_p, update_p,
570 update_sp_offset, full_p);
571 if (new_rtx != XEXP (x, 0))
572 return gen_rtx_fmt_e (code, GET_MODE (x), new_rtx);
573 return x;
575 case SUBREG:
576 new_rtx = lra_eliminate_regs_1 (insn, SUBREG_REG (x), mem_mode,
577 subst_p, update_p,
578 update_sp_offset, full_p);
580 if (new_rtx != SUBREG_REG (x))
582 int x_size = GET_MODE_SIZE (GET_MODE (x));
583 int new_size = GET_MODE_SIZE (GET_MODE (new_rtx));
585 if (MEM_P (new_rtx) && x_size <= new_size)
587 SUBREG_REG (x) = new_rtx;
588 alter_subreg (&x, false);
589 return x;
591 else if (! subst_p)
593 /* LRA can transform subregs itself. So don't call
594 simplify_gen_subreg until LRA transformations are
595 finished. Function simplify_gen_subreg can do
596 non-trivial transformations (like truncation) which
597 might make LRA work to fail. */
598 SUBREG_REG (x) = new_rtx;
599 return x;
601 else
602 return simplify_gen_subreg (GET_MODE (x), new_rtx,
603 GET_MODE (new_rtx), SUBREG_BYTE (x));
606 return x;
608 case MEM:
609 /* Our only special processing is to pass the mode of the MEM to our
610 recursive call and copy the flags. While we are here, handle this
611 case more efficiently. */
612 return
613 replace_equiv_address_nv
615 lra_eliminate_regs_1 (insn, XEXP (x, 0), GET_MODE (x),
616 subst_p, update_p, update_sp_offset, full_p));
618 case USE:
619 /* Handle insn_list USE that a call to a pure function may generate. */
620 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 0), VOIDmode,
621 subst_p, update_p, update_sp_offset, full_p);
622 if (new_rtx != XEXP (x, 0))
623 return gen_rtx_USE (GET_MODE (x), new_rtx);
624 return x;
626 case CLOBBER:
627 case SET:
628 gcc_unreachable ();
630 default:
631 break;
634 /* Process each of our operands recursively. If any have changed, make a
635 copy of the rtx. */
636 fmt = GET_RTX_FORMAT (code);
637 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
639 if (*fmt == 'e')
641 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, i), mem_mode,
642 subst_p, update_p,
643 update_sp_offset, full_p);
644 if (new_rtx != XEXP (x, i) && ! copied)
646 x = shallow_copy_rtx (x);
647 copied = 1;
649 XEXP (x, i) = new_rtx;
651 else if (*fmt == 'E')
653 int copied_vec = 0;
654 for (j = 0; j < XVECLEN (x, i); j++)
656 new_rtx = lra_eliminate_regs_1 (insn, XVECEXP (x, i, j), mem_mode,
657 subst_p, update_p,
658 update_sp_offset, full_p);
659 if (new_rtx != XVECEXP (x, i, j) && ! copied_vec)
661 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
662 XVEC (x, i)->elem);
663 if (! copied)
665 x = shallow_copy_rtx (x);
666 copied = 1;
668 XVEC (x, i) = new_v;
669 copied_vec = 1;
671 XVECEXP (x, i, j) = new_rtx;
676 return x;
679 /* This function is used externally in subsequent passes of GCC. It
680 always does a full elimination of X. */
682 lra_eliminate_regs (rtx x, machine_mode mem_mode,
683 rtx insn ATTRIBUTE_UNUSED)
685 return lra_eliminate_regs_1 (NULL, x, mem_mode, true, false, 0, true);
688 /* Stack pointer offset before the current insn relative to one at the
689 func start. RTL insns can change SP explicitly. We keep the
690 changes from one insn to another through this variable. */
691 static HOST_WIDE_INT curr_sp_change;
693 /* Scan rtx X for references to elimination source or target registers
694 in contexts that would prevent the elimination from happening.
695 Update the table of eliminables to reflect the changed state.
696 MEM_MODE is the mode of an enclosing MEM rtx, or VOIDmode if not
697 within a MEM. */
698 static void
699 mark_not_eliminable (rtx x, machine_mode mem_mode)
701 enum rtx_code code = GET_CODE (x);
702 struct lra_elim_table *ep;
703 int i, j;
704 const char *fmt;
706 switch (code)
708 case PRE_INC:
709 case POST_INC:
710 case PRE_DEC:
711 case POST_DEC:
712 case POST_MODIFY:
713 case PRE_MODIFY:
714 if (XEXP (x, 0) == stack_pointer_rtx
715 && ((code != PRE_MODIFY && code != POST_MODIFY)
716 || (GET_CODE (XEXP (x, 1)) == PLUS
717 && XEXP (x, 0) == XEXP (XEXP (x, 1), 0)
718 && CONST_INT_P (XEXP (XEXP (x, 1), 1)))))
720 int size = GET_MODE_SIZE (mem_mode);
722 #ifdef PUSH_ROUNDING
723 /* If more bytes than MEM_MODE are pushed, account for
724 them. */
725 size = PUSH_ROUNDING (size);
726 #endif
727 if (code == PRE_DEC || code == POST_DEC)
728 curr_sp_change -= size;
729 else if (code == PRE_INC || code == POST_INC)
730 curr_sp_change += size;
731 else if (code == PRE_MODIFY || code == POST_MODIFY)
732 curr_sp_change += INTVAL (XEXP (XEXP (x, 1), 1));
734 else if (REG_P (XEXP (x, 0))
735 && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER)
737 /* If we modify the source of an elimination rule, disable
738 it. Do the same if it is the destination and not the
739 hard frame register. */
740 for (ep = reg_eliminate;
741 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
742 ep++)
743 if (ep->from_rtx == XEXP (x, 0)
744 || (ep->to_rtx == XEXP (x, 0)
745 && ep->to_rtx != hard_frame_pointer_rtx))
746 setup_can_eliminate (ep, false);
748 return;
750 case USE:
751 if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
752 /* If using a hard register that is the source of an eliminate
753 we still think can be performed, note it cannot be
754 performed since we don't know how this hard register is
755 used. */
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 != hard_frame_pointer_rtx)
761 setup_can_eliminate (ep, false);
762 return;
764 case CLOBBER:
765 if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
766 /* If clobbering a hard register that is the replacement
767 register for an elimination we still think can be
768 performed, note that it cannot be performed. Otherwise, we
769 need not be concerned about it. */
770 for (ep = reg_eliminate;
771 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
772 ep++)
773 if (ep->to_rtx == XEXP (x, 0)
774 && ep->to_rtx != hard_frame_pointer_rtx)
775 setup_can_eliminate (ep, false);
776 return;
778 case SET:
779 if (SET_DEST (x) == stack_pointer_rtx
780 && GET_CODE (SET_SRC (x)) == PLUS
781 && XEXP (SET_SRC (x), 0) == SET_DEST (x)
782 && CONST_INT_P (XEXP (SET_SRC (x), 1)))
784 curr_sp_change += INTVAL (XEXP (SET_SRC (x), 1));
785 return;
787 if (! REG_P (SET_DEST (x))
788 || REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER)
789 mark_not_eliminable (SET_DEST (x), mem_mode);
790 else
792 /* See if this is setting the replacement hard register for
793 an elimination.
795 If DEST is the hard frame pointer, we do nothing because
796 we assume that all assignments to the frame pointer are
797 for non-local gotos and are being done at a time when
798 they are valid and do not disturb anything else. Some
799 machines want to eliminate a fake argument pointer (or
800 even a fake frame pointer) with either the real frame
801 pointer or the stack pointer. Assignments to the hard
802 frame pointer must not prevent this elimination. */
803 for (ep = reg_eliminate;
804 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
805 ep++)
806 if (ep->to_rtx == SET_DEST (x)
807 && SET_DEST (x) != hard_frame_pointer_rtx)
808 setup_can_eliminate (ep, false);
811 mark_not_eliminable (SET_SRC (x), mem_mode);
812 return;
814 case MEM:
815 /* Our only special processing is to pass the mode of the MEM to
816 our recursive call. */
817 mark_not_eliminable (XEXP (x, 0), GET_MODE (x));
818 return;
820 default:
821 break;
824 fmt = GET_RTX_FORMAT (code);
825 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
827 if (*fmt == 'e')
828 mark_not_eliminable (XEXP (x, i), mem_mode);
829 else if (*fmt == 'E')
830 for (j = 0; j < XVECLEN (x, i); j++)
831 mark_not_eliminable (XVECEXP (x, i, j), mem_mode);
837 #ifdef HARD_FRAME_POINTER_REGNUM
839 /* Find offset equivalence note for reg WHAT in INSN and return the
840 found elmination offset. If the note is not found, return NULL.
841 Remove the found note. */
842 static rtx
843 remove_reg_equal_offset_note (rtx_insn *insn, rtx what)
845 rtx link, *link_loc;
847 for (link_loc = &REG_NOTES (insn);
848 (link = *link_loc) != NULL_RTX;
849 link_loc = &XEXP (link, 1))
850 if (REG_NOTE_KIND (link) == REG_EQUAL
851 && GET_CODE (XEXP (link, 0)) == PLUS
852 && XEXP (XEXP (link, 0), 0) == what
853 && CONST_INT_P (XEXP (XEXP (link, 0), 1)))
855 *link_loc = XEXP (link, 1);
856 return XEXP (XEXP (link, 0), 1);
858 return NULL_RTX;
861 #endif
863 /* Scan INSN and eliminate all eliminable hard registers in it.
865 If REPLACE_P is true, do the replacement destructively. Also
866 delete the insn as dead it if it is setting an eliminable register.
868 If REPLACE_P is false, just update the offsets while keeping the
869 base register the same. If FIRST_P, use the sp offset for
870 elimination to sp. Otherwise, use UPDATE_SP_OFFSET for this. If
871 UPDATE_SP_OFFSET is non-zero, don't use difference of the offset
872 and the previous offset. Attach the note about used elimination
873 for insns setting frame pointer to update elimination easy (without
874 parsing already generated elimination insns to find offset
875 previously used) in future. */
877 void
878 eliminate_regs_in_insn (rtx_insn *insn, bool replace_p, bool first_p,
879 HOST_WIDE_INT update_sp_offset)
881 int icode = recog_memoized (insn);
882 rtx old_set = single_set (insn);
883 bool validate_p;
884 int i;
885 rtx substed_operand[MAX_RECOG_OPERANDS];
886 rtx orig_operand[MAX_RECOG_OPERANDS];
887 struct lra_elim_table *ep;
888 rtx plus_src, plus_cst_src;
889 lra_insn_recog_data_t id;
890 struct lra_static_insn_data *static_id;
892 if (icode < 0 && asm_noperands (PATTERN (insn)) < 0 && ! DEBUG_INSN_P (insn))
894 lra_assert (GET_CODE (PATTERN (insn)) == USE
895 || GET_CODE (PATTERN (insn)) == CLOBBER
896 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
897 return;
900 /* Check for setting an eliminable register. */
901 if (old_set != 0 && REG_P (SET_DEST (old_set))
902 && (ep = get_elimination (SET_DEST (old_set))) != NULL)
904 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
905 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
907 bool delete_p = replace_p;
909 #ifdef HARD_FRAME_POINTER_REGNUM
910 if (ep->from == FRAME_POINTER_REGNUM
911 && ep->to == HARD_FRAME_POINTER_REGNUM)
912 /* If this is setting the frame pointer register to the
913 hardware frame pointer register and this is an
914 elimination that will be done (tested above), this
915 insn is really adjusting the frame pointer downward
916 to compensate for the adjustment done before a
917 nonlocal goto. */
919 rtx src = SET_SRC (old_set);
920 rtx off = remove_reg_equal_offset_note (insn, ep->to_rtx);
922 /* We should never process such insn with non-zero
923 UPDATE_SP_OFFSET. */
924 lra_assert (update_sp_offset == 0);
926 if (off != NULL_RTX
927 || src == ep->to_rtx
928 || (GET_CODE (src) == PLUS
929 && XEXP (src, 0) == ep->to_rtx
930 && CONST_INT_P (XEXP (src, 1))))
932 HOST_WIDE_INT offset;
934 if (replace_p)
936 SET_DEST (old_set) = ep->to_rtx;
937 lra_update_insn_recog_data (insn);
938 return;
940 offset = (off != NULL_RTX ? INTVAL (off)
941 : src == ep->to_rtx ? 0 : INTVAL (XEXP (src, 1)));
942 offset -= (ep->offset - ep->previous_offset);
943 src = plus_constant (Pmode, ep->to_rtx, offset);
945 /* First see if this insn remains valid when we
946 make the change. If not, keep the INSN_CODE
947 the same and let the constraint pass fit it
948 up. */
949 validate_change (insn, &SET_SRC (old_set), src, 1);
950 validate_change (insn, &SET_DEST (old_set),
951 ep->from_rtx, 1);
952 if (! apply_change_group ())
954 SET_SRC (old_set) = src;
955 SET_DEST (old_set) = ep->from_rtx;
957 lra_update_insn_recog_data (insn);
958 /* Add offset note for future updates. */
959 add_reg_note (insn, REG_EQUAL, src);
960 return;
963 #endif
965 /* This insn isn't serving a useful purpose. We delete it
966 when REPLACE is set. */
967 if (delete_p)
968 lra_delete_dead_insn (insn);
969 return;
973 /* We allow one special case which happens to work on all machines we
974 currently support: a single set with the source or a REG_EQUAL
975 note being a PLUS of an eliminable register and a constant. */
976 plus_src = plus_cst_src = 0;
977 if (old_set && REG_P (SET_DEST (old_set)))
979 if (GET_CODE (SET_SRC (old_set)) == PLUS)
980 plus_src = SET_SRC (old_set);
981 /* First see if the source is of the form (plus (...) CST). */
982 if (plus_src
983 && CONST_INT_P (XEXP (plus_src, 1)))
984 plus_cst_src = plus_src;
985 /* Check that the first operand of the PLUS is a hard reg or
986 the lowpart subreg of one. */
987 if (plus_cst_src)
989 rtx reg = XEXP (plus_cst_src, 0);
991 if (GET_CODE (reg) == SUBREG && subreg_lowpart_p (reg))
992 reg = SUBREG_REG (reg);
994 if (!REG_P (reg) || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
995 plus_cst_src = 0;
998 if (plus_cst_src)
1000 rtx reg = XEXP (plus_cst_src, 0);
1001 HOST_WIDE_INT offset = INTVAL (XEXP (plus_cst_src, 1));
1003 if (GET_CODE (reg) == SUBREG)
1004 reg = SUBREG_REG (reg);
1006 if (REG_P (reg) && (ep = get_elimination (reg)) != NULL)
1008 rtx to_rtx = replace_p ? ep->to_rtx : ep->from_rtx;
1010 if (! replace_p)
1012 if (update_sp_offset == 0)
1013 offset += (ep->offset - ep->previous_offset);
1014 if (ep->to_rtx == stack_pointer_rtx)
1016 if (first_p)
1017 offset -= lra_get_insn_recog_data (insn)->sp_offset;
1018 else
1019 offset += update_sp_offset;
1021 offset = trunc_int_for_mode (offset, GET_MODE (plus_cst_src));
1024 if (GET_CODE (XEXP (plus_cst_src, 0)) == SUBREG)
1025 to_rtx = gen_lowpart (GET_MODE (XEXP (plus_cst_src, 0)), to_rtx);
1026 /* If we have a nonzero offset, and the source is already a
1027 simple REG, the following transformation would increase
1028 the cost of the insn by replacing a simple REG with (plus
1029 (reg sp) CST). So try only when we already had a PLUS
1030 before. */
1031 if (offset == 0 || plus_src)
1033 rtx new_src = plus_constant (GET_MODE (to_rtx), to_rtx, offset);
1035 old_set = single_set (insn);
1037 /* First see if this insn remains valid when we make the
1038 change. If not, try to replace the whole pattern
1039 with a simple set (this may help if the original insn
1040 was a PARALLEL that was only recognized as single_set
1041 due to REG_UNUSED notes). If this isn't valid
1042 either, keep the INSN_CODE the same and let the
1043 constraint pass fix it up. */
1044 if (! validate_change (insn, &SET_SRC (old_set), new_src, 0))
1046 rtx new_pat = gen_rtx_SET (SET_DEST (old_set), new_src);
1048 if (! validate_change (insn, &PATTERN (insn), new_pat, 0))
1049 SET_SRC (old_set) = new_src;
1051 lra_update_insn_recog_data (insn);
1052 /* This can't have an effect on elimination offsets, so skip
1053 right to the end. */
1054 return;
1059 /* Eliminate all eliminable registers occurring in operands that
1060 can be handled by the constraint pass. */
1061 id = lra_get_insn_recog_data (insn);
1062 static_id = id->insn_static_data;
1063 validate_p = false;
1064 for (i = 0; i < static_id->n_operands; i++)
1066 orig_operand[i] = *id->operand_loc[i];
1067 substed_operand[i] = *id->operand_loc[i];
1069 /* For an asm statement, every operand is eliminable. */
1070 if (icode < 0 || insn_data[icode].operand[i].eliminable)
1072 /* Check for setting a hard register that we know about. */
1073 if (static_id->operand[i].type != OP_IN
1074 && REG_P (orig_operand[i]))
1076 /* If we are assigning to a hard register that can be
1077 eliminated, it must be as part of a PARALLEL, since
1078 the code above handles single SETs. This reg can not
1079 be longer eliminated -- it is forced by
1080 mark_not_eliminable. */
1081 for (ep = reg_eliminate;
1082 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
1083 ep++)
1084 lra_assert (ep->from_rtx != orig_operand[i]
1085 || ! ep->can_eliminate);
1088 /* Companion to the above plus substitution, we can allow
1089 invariants as the source of a plain move. */
1090 substed_operand[i]
1091 = lra_eliminate_regs_1 (insn, *id->operand_loc[i], VOIDmode,
1092 replace_p, ! replace_p && ! first_p,
1093 update_sp_offset, first_p);
1094 if (substed_operand[i] != orig_operand[i])
1095 validate_p = true;
1099 if (! validate_p)
1100 return;
1102 /* Substitute the operands; the new values are in the substed_operand
1103 array. */
1104 for (i = 0; i < static_id->n_operands; i++)
1105 *id->operand_loc[i] = substed_operand[i];
1106 for (i = 0; i < static_id->n_dups; i++)
1107 *id->dup_loc[i] = substed_operand[(int) static_id->dup_num[i]];
1109 /* If we had a move insn but now we don't, re-recognize it.
1110 This will cause spurious re-recognition if the old move had a
1111 PARALLEL since the new one still will, but we can't call
1112 single_set without having put new body into the insn and the
1113 re-recognition won't hurt in this rare case. */
1114 id = lra_update_insn_recog_data (insn);
1115 static_id = id->insn_static_data;
1118 /* Spill pseudos which are assigned to hard registers in SET. Add
1119 affected insns for processing in the subsequent constraint
1120 pass. */
1121 static void
1122 spill_pseudos (HARD_REG_SET set)
1124 int i;
1125 bitmap_head to_process;
1126 rtx_insn *insn;
1128 if (hard_reg_set_empty_p (set))
1129 return;
1130 if (lra_dump_file != NULL)
1132 fprintf (lra_dump_file, " Spilling non-eliminable hard regs:");
1133 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1134 if (TEST_HARD_REG_BIT (set, i))
1135 fprintf (lra_dump_file, " %d", i);
1136 fprintf (lra_dump_file, "\n");
1138 bitmap_initialize (&to_process, &reg_obstack);
1139 for (i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
1140 if (lra_reg_info[i].nrefs != 0 && reg_renumber[i] >= 0
1141 && overlaps_hard_reg_set_p (set,
1142 PSEUDO_REGNO_MODE (i), reg_renumber[i]))
1144 if (lra_dump_file != NULL)
1145 fprintf (lra_dump_file, " Spilling r%d(%d)\n",
1146 i, reg_renumber[i]);
1147 reg_renumber[i] = -1;
1148 bitmap_ior_into (&to_process, &lra_reg_info[i].insn_bitmap);
1150 IOR_HARD_REG_SET (lra_no_alloc_regs, set);
1151 for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
1152 if (bitmap_bit_p (&to_process, INSN_UID (insn)))
1154 lra_push_insn (insn);
1155 lra_set_used_insn_alternative (insn, -1);
1157 bitmap_clear (&to_process);
1160 /* Update all offsets and possibility for elimination on eliminable
1161 registers. Spill pseudos assigned to registers which are
1162 uneliminable, update LRA_NO_ALLOC_REGS and ELIMINABLE_REG_SET. Add
1163 insns to INSNS_WITH_CHANGED_OFFSETS containing eliminable hard
1164 registers whose offsets should be changed. Return true if any
1165 elimination offset changed. */
1166 static bool
1167 update_reg_eliminate (bitmap insns_with_changed_offsets)
1169 bool prev, result;
1170 struct lra_elim_table *ep, *ep1;
1171 HARD_REG_SET temp_hard_reg_set;
1173 /* Clear self elimination offsets. */
1174 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1175 self_elim_offsets[ep->from] = 0;
1176 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1178 /* If it is a currently used elimination: update the previous
1179 offset. */
1180 if (elimination_map[ep->from] == ep)
1181 ep->previous_offset = ep->offset;
1183 prev = ep->prev_can_eliminate;
1184 setup_can_eliminate (ep, targetm.can_eliminate (ep->from, ep->to));
1185 if (ep->can_eliminate && ! prev)
1187 /* It is possible that not eliminable register becomes
1188 eliminable because we took other reasons into account to
1189 set up eliminable regs in the initial set up. Just
1190 ignore new eliminable registers. */
1191 setup_can_eliminate (ep, false);
1192 continue;
1194 if (ep->can_eliminate != prev && elimination_map[ep->from] == ep)
1196 /* We cannot use this elimination anymore -- find another
1197 one. */
1198 if (lra_dump_file != NULL)
1199 fprintf (lra_dump_file,
1200 " Elimination %d to %d is not possible anymore\n",
1201 ep->from, ep->to);
1202 /* If after processing RTL we decides that SP can be used as
1203 a result of elimination, it can not be changed. */
1204 gcc_assert ((ep->to_rtx != stack_pointer_rtx)
1205 || (ep->from < FIRST_PSEUDO_REGISTER
1206 && fixed_regs [ep->from]));
1207 /* Mark that is not eliminable anymore. */
1208 elimination_map[ep->from] = NULL;
1209 for (ep1 = ep + 1; ep1 < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep1++)
1210 if (ep1->can_eliminate && ep1->from == ep->from)
1211 break;
1212 if (ep1 < &reg_eliminate[NUM_ELIMINABLE_REGS])
1214 if (lra_dump_file != NULL)
1215 fprintf (lra_dump_file, " Using elimination %d to %d now\n",
1216 ep1->from, ep1->to);
1217 lra_assert (ep1->previous_offset == 0);
1218 ep1->previous_offset = ep->offset;
1220 else
1222 /* There is no elimination anymore just use the hard
1223 register `from' itself. Setup self elimination
1224 offset to restore the original offset values. */
1225 if (lra_dump_file != NULL)
1226 fprintf (lra_dump_file, " %d is not eliminable at all\n",
1227 ep->from);
1228 self_elim_offsets[ep->from] = -ep->offset;
1229 if (ep->offset != 0)
1230 bitmap_ior_into (insns_with_changed_offsets,
1231 &lra_reg_info[ep->from].insn_bitmap);
1235 #ifdef ELIMINABLE_REGS
1236 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->offset);
1237 #else
1238 INITIAL_FRAME_POINTER_OFFSET (ep->offset);
1239 #endif
1241 setup_elimination_map ();
1242 result = false;
1243 CLEAR_HARD_REG_SET (temp_hard_reg_set);
1244 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1245 if (elimination_map[ep->from] == NULL)
1246 SET_HARD_REG_BIT (temp_hard_reg_set, ep->from);
1247 else if (elimination_map[ep->from] == ep)
1249 /* Prevent the hard register into which we eliminate from
1250 the usage for pseudos. */
1251 if (ep->from != ep->to)
1252 SET_HARD_REG_BIT (temp_hard_reg_set, ep->to);
1253 if (ep->previous_offset != ep->offset)
1255 bitmap_ior_into (insns_with_changed_offsets,
1256 &lra_reg_info[ep->from].insn_bitmap);
1258 /* Update offset when the eliminate offset have been
1259 changed. */
1260 lra_update_reg_val_offset (lra_reg_info[ep->from].val,
1261 ep->offset - ep->previous_offset);
1262 result = true;
1265 IOR_HARD_REG_SET (lra_no_alloc_regs, temp_hard_reg_set);
1266 AND_COMPL_HARD_REG_SET (eliminable_regset, temp_hard_reg_set);
1267 spill_pseudos (temp_hard_reg_set);
1268 return result;
1271 /* Initialize the table of hard registers to eliminate.
1272 Pre-condition: global flag frame_pointer_needed has been set before
1273 calling this function. */
1274 static void
1275 init_elim_table (void)
1277 struct lra_elim_table *ep;
1278 #ifdef ELIMINABLE_REGS
1279 bool value_p;
1280 const struct elim_table_1 *ep1;
1281 #endif
1283 if (!reg_eliminate)
1284 reg_eliminate = XCNEWVEC (struct lra_elim_table, NUM_ELIMINABLE_REGS);
1286 memset (self_elim_offsets, 0, sizeof (self_elim_offsets));
1287 /* Initiate member values which will be never changed. */
1288 self_elim_table.can_eliminate = self_elim_table.prev_can_eliminate = true;
1289 self_elim_table.previous_offset = 0;
1290 #ifdef ELIMINABLE_REGS
1291 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
1292 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
1294 ep->offset = ep->previous_offset = 0;
1295 ep->from = ep1->from;
1296 ep->to = ep1->to;
1297 value_p = (targetm.can_eliminate (ep->from, ep->to)
1298 && ! (ep->to == STACK_POINTER_REGNUM
1299 && frame_pointer_needed
1300 && (! SUPPORTS_STACK_ALIGNMENT
1301 || ! stack_realign_fp)));
1302 setup_can_eliminate (ep, value_p);
1304 #else
1305 reg_eliminate[0].offset = reg_eliminate[0].previous_offset = 0;
1306 reg_eliminate[0].from = reg_eliminate_1[0].from;
1307 reg_eliminate[0].to = reg_eliminate_1[0].to;
1308 setup_can_eliminate (&reg_eliminate[0], ! frame_pointer_needed);
1309 #endif
1311 /* Build the FROM and TO REG rtx's. Note that code in gen_rtx_REG
1312 will cause, e.g., gen_rtx_REG (Pmode, STACK_POINTER_REGNUM) to
1313 equal stack_pointer_rtx. We depend on this. Threfore we switch
1314 off that we are in LRA temporarily. */
1315 lra_in_progress = 0;
1316 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1318 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
1319 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
1320 eliminable_reg_rtx[ep->from] = ep->from_rtx;
1322 lra_in_progress = 1;
1325 /* Function for initialization of elimination once per function. It
1326 sets up sp offset for each insn. */
1327 static void
1328 init_elimination (void)
1330 bool stop_to_sp_elimination_p;
1331 basic_block bb;
1332 rtx_insn *insn;
1333 struct lra_elim_table *ep;
1335 init_elim_table ();
1336 FOR_EACH_BB_FN (bb, cfun)
1338 curr_sp_change = 0;
1339 stop_to_sp_elimination_p = false;
1340 FOR_BB_INSNS (bb, insn)
1341 if (INSN_P (insn))
1343 lra_get_insn_recog_data (insn)->sp_offset = curr_sp_change;
1344 if (NONDEBUG_INSN_P (insn))
1346 mark_not_eliminable (PATTERN (insn), VOIDmode);
1347 if (curr_sp_change != 0
1348 && find_reg_note (insn, REG_LABEL_OPERAND, NULL_RTX))
1349 stop_to_sp_elimination_p = true;
1352 if (! frame_pointer_needed
1353 && (curr_sp_change != 0 || stop_to_sp_elimination_p)
1354 && bb->succs && bb->succs->length () != 0)
1355 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1356 if (ep->to == STACK_POINTER_REGNUM)
1357 setup_can_eliminate (ep, false);
1359 setup_elimination_map ();
1362 /* Eliminate hard reg given by its location LOC. */
1363 void
1364 lra_eliminate_reg_if_possible (rtx *loc)
1366 int regno;
1367 struct lra_elim_table *ep;
1369 lra_assert (REG_P (*loc));
1370 if ((regno = REGNO (*loc)) >= FIRST_PSEUDO_REGISTER
1371 || ! TEST_HARD_REG_BIT (lra_no_alloc_regs, regno))
1372 return;
1373 if ((ep = get_elimination (*loc)) != NULL)
1374 *loc = ep->to_rtx;
1377 /* Do (final if FINAL_P or first if FIRST_P) elimination in INSN. Add
1378 the insn for subsequent processing in the constraint pass, update
1379 the insn info. */
1380 static void
1381 process_insn_for_elimination (rtx_insn *insn, bool final_p, bool first_p)
1383 eliminate_regs_in_insn (insn, final_p, first_p, 0);
1384 if (! final_p)
1386 /* Check that insn changed its code. This is a case when a move
1387 insn becomes an add insn and we do not want to process the
1388 insn as a move anymore. */
1389 int icode = recog (PATTERN (insn), insn, 0);
1391 if (icode >= 0 && icode != INSN_CODE (insn))
1393 INSN_CODE (insn) = icode;
1394 lra_update_insn_recog_data (insn);
1396 lra_update_insn_regno_info (insn);
1397 lra_push_insn (insn);
1398 lra_set_used_insn_alternative (insn, -1);
1402 /* Entry function to do final elimination if FINAL_P or to update
1403 elimination register offsets (FIRST_P if we are doing it the first
1404 time). */
1405 void
1406 lra_eliminate (bool final_p, bool first_p)
1408 unsigned int uid;
1409 bitmap_head insns_with_changed_offsets;
1410 bitmap_iterator bi;
1411 struct lra_elim_table *ep;
1413 gcc_assert (! final_p || ! first_p);
1415 timevar_push (TV_LRA_ELIMINATE);
1417 if (first_p)
1418 init_elimination ();
1420 bitmap_initialize (&insns_with_changed_offsets, &reg_obstack);
1421 if (final_p)
1423 if (flag_checking)
1425 update_reg_eliminate (&insns_with_changed_offsets);
1426 gcc_assert (bitmap_empty_p (&insns_with_changed_offsets));
1428 /* We change eliminable hard registers in insns so we should do
1429 this for all insns containing any eliminable hard
1430 register. */
1431 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1432 if (elimination_map[ep->from] != NULL)
1433 bitmap_ior_into (&insns_with_changed_offsets,
1434 &lra_reg_info[ep->from].insn_bitmap);
1436 else if (! update_reg_eliminate (&insns_with_changed_offsets))
1437 goto lra_eliminate_done;
1438 if (lra_dump_file != NULL)
1440 fprintf (lra_dump_file, "New elimination table:\n");
1441 print_elim_table (lra_dump_file);
1443 EXECUTE_IF_SET_IN_BITMAP (&insns_with_changed_offsets, 0, uid, bi)
1444 /* A dead insn can be deleted in process_insn_for_elimination. */
1445 if (lra_insn_recog_data[uid] != NULL)
1446 process_insn_for_elimination (lra_insn_recog_data[uid]->insn,
1447 final_p, first_p);
1448 bitmap_clear (&insns_with_changed_offsets);
1450 lra_eliminate_done:
1451 timevar_pop (TV_LRA_ELIMINATE);