libgomp: Use pthread mutexes in the nvptx plugin.
[official-gcc.git] / gcc / lra-eliminations.c
bloba591de634949b73b54f78e453125926d2c07fa5d
1 /* Code for RTL register eliminations.
2 Copyright (C) 2010-2015 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* Eliminable registers (like a soft argument or frame pointer) are
22 widely used in RTL. These eliminable registers should be replaced
23 by real hard registers (like the stack pointer or hard frame
24 pointer) plus some offset. The offsets usually change whenever the
25 stack is expanded. We know the final offsets only at the very end
26 of LRA.
28 Within LRA, we usually keep the RTL in such a state that the
29 eliminable registers can be replaced by just the corresponding hard
30 register (without any offset). To achieve this we should add the
31 initial elimination offset at the beginning of LRA and update the
32 offsets whenever the stack is expanded. We need to do this before
33 every constraint pass because the choice of offset often affects
34 whether a particular address or memory constraint is satisfied.
36 We keep RTL code at most time in such state that the virtual
37 registers can be changed by just the corresponding hard registers
38 (with zero offsets) and we have the right RTL code. To achieve this
39 we should add initial offset at the beginning of LRA work and update
40 offsets after each stack expanding. But actually we update virtual
41 registers to the same virtual registers + corresponding offsets
42 before every constraint pass because it affects constraint
43 satisfaction (e.g. an address displacement became too big for some
44 target).
46 The final change of eliminable registers to the corresponding hard
47 registers are done at the very end of LRA when there were no change
48 in offsets anymore:
50 fp + 42 => sp + 42
54 #include "config.h"
55 #include "system.h"
56 #include "coretypes.h"
57 #include "tm.h"
58 #include "hard-reg-set.h"
59 #include "rtl.h"
60 #include "tm_p.h"
61 #include "regs.h"
62 #include "insn-config.h"
63 #include "insn-codes.h"
64 #include "recog.h"
65 #include "output.h"
66 #include "addresses.h"
67 #include "target.h"
68 #include "hashtab.h"
69 #include "hash-set.h"
70 #include "vec.h"
71 #include "machmode.h"
72 #include "input.h"
73 #include "function.h"
74 #include "symtab.h"
75 #include "expr.h"
76 #include "predict.h"
77 #include "dominance.h"
78 #include "cfg.h"
79 #include "basic-block.h"
80 #include "except.h"
81 #include "optabs.h"
82 #include "df.h"
83 #include "ira.h"
84 #include "rtl-error.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;
171 /* Map: eliminable "from" register -> its current elimination,
172 or NULL if none. The elimination table may contain more than
173 one elimination for the same hard register, but this map specifies
174 the one that we are currently using. */
175 static struct lra_elim_table *elimination_map[FIRST_PSEUDO_REGISTER];
177 /* When an eliminable hard register becomes not eliminable, we use the
178 following special structure to restore original offsets for the
179 register. */
180 static struct lra_elim_table self_elim_table;
182 /* Offsets should be used to restore original offsets for eliminable
183 hard register which just became not eliminable. Zero,
184 otherwise. */
185 static HOST_WIDE_INT self_elim_offsets[FIRST_PSEUDO_REGISTER];
187 /* Map: hard regno -> RTL presentation. RTL presentations of all
188 potentially eliminable hard registers are stored in the map. */
189 static rtx eliminable_reg_rtx[FIRST_PSEUDO_REGISTER];
191 /* Set up ELIMINATION_MAP of the currently used eliminations. */
192 static void
193 setup_elimination_map (void)
195 int i;
196 struct lra_elim_table *ep;
198 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
199 elimination_map[i] = NULL;
200 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
201 if (ep->can_eliminate && elimination_map[ep->from] == NULL)
202 elimination_map[ep->from] = ep;
207 /* Compute the sum of X and Y, making canonicalizations assumed in an
208 address, namely: sum constant integers, surround the sum of two
209 constants with a CONST, put the constant as the second operand, and
210 group the constant on the outermost sum.
212 This routine assumes both inputs are already in canonical form. */
213 static rtx
214 form_sum (rtx x, rtx y)
216 rtx tem;
217 machine_mode mode = GET_MODE (x);
219 if (mode == VOIDmode)
220 mode = GET_MODE (y);
222 if (mode == VOIDmode)
223 mode = Pmode;
225 if (CONST_INT_P (x))
226 return plus_constant (mode, y, INTVAL (x));
227 else if (CONST_INT_P (y))
228 return plus_constant (mode, x, INTVAL (y));
229 else if (CONSTANT_P (x))
230 tem = x, x = y, y = tem;
232 if (GET_CODE (x) == PLUS && CONSTANT_P (XEXP (x, 1)))
233 return form_sum (XEXP (x, 0), form_sum (XEXP (x, 1), y));
235 /* Note that if the operands of Y are specified in the opposite
236 order in the recursive calls below, infinite recursion will
237 occur. */
238 if (GET_CODE (y) == PLUS && CONSTANT_P (XEXP (y, 1)))
239 return form_sum (form_sum (x, XEXP (y, 0)), XEXP (y, 1));
241 /* If both constant, encapsulate sum. Otherwise, just form sum. A
242 constant will have been placed second. */
243 if (CONSTANT_P (x) && CONSTANT_P (y))
245 if (GET_CODE (x) == CONST)
246 x = XEXP (x, 0);
247 if (GET_CODE (y) == CONST)
248 y = XEXP (y, 0);
250 return gen_rtx_CONST (VOIDmode, gen_rtx_PLUS (mode, x, y));
253 return gen_rtx_PLUS (mode, x, y);
256 /* Return the current substitution hard register of the elimination of
257 HARD_REGNO. If HARD_REGNO is not eliminable, return itself. */
259 lra_get_elimination_hard_regno (int hard_regno)
261 struct lra_elim_table *ep;
263 if (hard_regno < 0 || hard_regno >= FIRST_PSEUDO_REGISTER)
264 return hard_regno;
265 if ((ep = elimination_map[hard_regno]) == NULL)
266 return hard_regno;
267 return ep->to;
270 /* Return elimination which will be used for hard reg REG, NULL
271 otherwise. */
272 static struct lra_elim_table *
273 get_elimination (rtx reg)
275 int hard_regno;
276 struct lra_elim_table *ep;
277 HOST_WIDE_INT offset;
279 lra_assert (REG_P (reg));
280 if ((hard_regno = REGNO (reg)) < 0 || hard_regno >= FIRST_PSEUDO_REGISTER)
281 return NULL;
282 if ((ep = elimination_map[hard_regno]) != NULL)
283 return ep->from_rtx != reg ? NULL : ep;
284 if ((offset = self_elim_offsets[hard_regno]) == 0)
285 return NULL;
286 /* This is an iteration to restore offsets just after HARD_REGNO
287 stopped to be eliminable. */
288 self_elim_table.from = self_elim_table.to = hard_regno;
289 self_elim_table.from_rtx
290 = self_elim_table.to_rtx
291 = eliminable_reg_rtx[hard_regno];
292 lra_assert (self_elim_table.from_rtx != NULL);
293 self_elim_table.offset = offset;
294 return &self_elim_table;
297 /* Scan X and replace any eliminable registers (such as fp) with a
298 replacement (such as sp) if SUBST_P, plus an offset. The offset is
299 a change in the offset between the eliminable register and its
300 substitution if UPDATE_P, or the full offset if FULL_P, or
301 otherwise zero. If FULL_P, we also use the SP offsets for
302 elimination to SP. If UPDATE_P, use UPDATE_SP_OFFSET for updating
303 offsets of register elimnable to SP.
305 MEM_MODE is the mode of an enclosing MEM. We need this to know how
306 much to adjust a register for, e.g., PRE_DEC. Also, if we are
307 inside a MEM, we are allowed to replace a sum of a hard register
308 and the constant zero with the hard register, which we cannot do
309 outside a MEM. In addition, we need to record the fact that a
310 hard register is referenced outside a MEM.
312 If we make full substitution to SP for non-null INSN, add the insn
313 sp offset. */
315 lra_eliminate_regs_1 (rtx_insn *insn, rtx x, machine_mode mem_mode,
316 bool subst_p, bool update_p,
317 HOST_WIDE_INT update_sp_offset, bool full_p)
319 enum rtx_code code = GET_CODE (x);
320 struct lra_elim_table *ep;
321 rtx new_rtx;
322 int i, j;
323 const char *fmt;
324 int copied = 0;
326 gcc_assert (!update_p || !full_p);
327 if (! current_function_decl)
328 return x;
330 switch (code)
332 CASE_CONST_ANY:
333 case CONST:
334 case SYMBOL_REF:
335 case CODE_LABEL:
336 case PC:
337 case CC0:
338 case ASM_INPUT:
339 case ADDR_VEC:
340 case ADDR_DIFF_VEC:
341 case RETURN:
342 return x;
344 case REG:
345 /* First handle the case where we encounter a bare hard register
346 that is eliminable. Replace it with a PLUS. */
347 if ((ep = get_elimination (x)) != NULL)
349 rtx to = subst_p ? ep->to_rtx : ep->from_rtx;
351 if (update_p)
352 return plus_constant (Pmode, to,
353 ep->offset - ep->previous_offset
354 + (ep->to_rtx == stack_pointer_rtx
355 ? update_sp_offset : 0));
356 else if (full_p)
357 return plus_constant (Pmode, to,
358 ep->offset
359 - (insn != NULL_RTX
360 && ep->to_rtx == stack_pointer_rtx
361 ? lra_get_insn_recog_data (insn)->sp_offset
362 : 0));
363 else
364 return to;
366 return x;
368 case PLUS:
369 /* If this is the sum of an eliminable register and a constant, rework
370 the sum. */
371 if (REG_P (XEXP (x, 0)) && CONSTANT_P (XEXP (x, 1)))
373 if ((ep = get_elimination (XEXP (x, 0))) != NULL)
375 HOST_WIDE_INT offset;
376 rtx to = subst_p ? ep->to_rtx : ep->from_rtx;
378 if (! update_p && ! full_p)
379 return gen_rtx_PLUS (Pmode, to, XEXP (x, 1));
381 offset = (update_p
382 ? ep->offset - ep->previous_offset
383 + (ep->to_rtx == stack_pointer_rtx
384 ? update_sp_offset : 0)
385 : ep->offset);
386 if (full_p && insn != NULL_RTX && ep->to_rtx == stack_pointer_rtx)
387 offset -= lra_get_insn_recog_data (insn)->sp_offset;
388 if (CONST_INT_P (XEXP (x, 1))
389 && INTVAL (XEXP (x, 1)) == -offset)
390 return to;
391 else
392 return gen_rtx_PLUS (Pmode, to,
393 plus_constant (Pmode,
394 XEXP (x, 1), offset));
397 /* If the hard register is not eliminable, we are done since
398 the other operand is a constant. */
399 return x;
402 /* If this is part of an address, we want to bring any constant
403 to the outermost PLUS. We will do this by doing hard
404 register replacement in our operands and seeing if a constant
405 shows up in one of them.
407 Note that there is no risk of modifying the structure of the
408 insn, since we only get called for its operands, thus we are
409 either modifying the address inside a MEM, or something like
410 an address operand of a load-address insn. */
413 rtx new0 = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
414 subst_p, update_p,
415 update_sp_offset, full_p);
416 rtx new1 = lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
417 subst_p, update_p,
418 update_sp_offset, full_p);
420 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
421 return form_sum (new0, new1);
423 return x;
425 case MULT:
426 /* If this is the product of an eliminable hard register and a
427 constant, apply the distribute law and move the constant out
428 so that we have (plus (mult ..) ..). This is needed in order
429 to keep load-address insns valid. This case is pathological.
430 We ignore the possibility of overflow here. */
431 if (REG_P (XEXP (x, 0)) && CONST_INT_P (XEXP (x, 1))
432 && (ep = get_elimination (XEXP (x, 0))) != NULL)
434 rtx to = subst_p ? ep->to_rtx : ep->from_rtx;
436 if (update_p)
437 return plus_constant (Pmode,
438 gen_rtx_MULT (Pmode, to, XEXP (x, 1)),
439 (ep->offset - ep->previous_offset
440 + (ep->to_rtx == stack_pointer_rtx
441 ? update_sp_offset : 0))
442 * INTVAL (XEXP (x, 1)));
443 else if (full_p)
445 HOST_WIDE_INT offset = ep->offset;
447 if (insn != NULL_RTX && ep->to_rtx == stack_pointer_rtx)
448 offset -= lra_get_insn_recog_data (insn)->sp_offset;
449 return
450 plus_constant (Pmode,
451 gen_rtx_MULT (Pmode, to, XEXP (x, 1)),
452 offset * INTVAL (XEXP (x, 1)));
454 else
455 return gen_rtx_MULT (Pmode, to, XEXP (x, 1));
458 /* ... fall through ... */
460 case CALL:
461 case COMPARE:
462 /* See comments before PLUS about handling MINUS. */
463 case MINUS:
464 case DIV: case UDIV:
465 case MOD: case UMOD:
466 case AND: case IOR: case XOR:
467 case ROTATERT: case ROTATE:
468 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
469 case NE: case EQ:
470 case GE: case GT: case GEU: case GTU:
471 case LE: case LT: case LEU: case LTU:
473 rtx new0 = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
474 subst_p, update_p,
475 update_sp_offset, full_p);
476 rtx new1 = XEXP (x, 1)
477 ? lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
478 subst_p, update_p,
479 update_sp_offset, full_p) : 0;
481 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
482 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
484 return x;
486 case EXPR_LIST:
487 /* If we have something in XEXP (x, 0), the usual case,
488 eliminate it. */
489 if (XEXP (x, 0))
491 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
492 subst_p, update_p,
493 update_sp_offset, full_p);
494 if (new_rtx != XEXP (x, 0))
496 /* If this is a REG_DEAD note, it is not valid anymore.
497 Using the eliminated version could result in creating a
498 REG_DEAD note for the stack or frame pointer. */
499 if (REG_NOTE_KIND (x) == REG_DEAD)
500 return (XEXP (x, 1)
501 ? lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
502 subst_p, update_p,
503 update_sp_offset, full_p)
504 : NULL_RTX);
506 x = alloc_reg_note (REG_NOTE_KIND (x), new_rtx, XEXP (x, 1));
510 /* ... fall through ... */
512 case INSN_LIST:
513 case INT_LIST:
514 /* Now do eliminations in the rest of the chain. If this was
515 an EXPR_LIST, this might result in allocating more memory than is
516 strictly needed, but it simplifies the code. */
517 if (XEXP (x, 1))
519 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 1), mem_mode,
520 subst_p, update_p,
521 update_sp_offset, full_p);
522 if (new_rtx != XEXP (x, 1))
523 return
524 gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x),
525 XEXP (x, 0), new_rtx);
527 return x;
529 case PRE_INC:
530 case POST_INC:
531 case PRE_DEC:
532 case POST_DEC:
533 /* We do not support elimination of a register that is modified.
534 elimination_effects has already make sure that this does not
535 happen. */
536 return x;
538 case PRE_MODIFY:
539 case POST_MODIFY:
540 /* We do not support elimination of a hard register that is
541 modified. LRA has already make sure that this does not
542 happen. The only remaining case we need to consider here is
543 that the increment value may be an eliminable register. */
544 if (GET_CODE (XEXP (x, 1)) == PLUS
545 && XEXP (XEXP (x, 1), 0) == XEXP (x, 0))
547 rtx new_rtx = lra_eliminate_regs_1 (insn, XEXP (XEXP (x, 1), 1),
548 mem_mode, subst_p, update_p,
549 update_sp_offset, full_p);
551 if (new_rtx != XEXP (XEXP (x, 1), 1))
552 return gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (x, 0),
553 gen_rtx_PLUS (GET_MODE (x),
554 XEXP (x, 0), new_rtx));
556 return x;
558 case STRICT_LOW_PART:
559 case NEG: case NOT:
560 case SIGN_EXTEND: case ZERO_EXTEND:
561 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
562 case FLOAT: case FIX:
563 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
564 case ABS:
565 case SQRT:
566 case FFS:
567 case CLZ:
568 case CTZ:
569 case POPCOUNT:
570 case PARITY:
571 case BSWAP:
572 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 0), mem_mode,
573 subst_p, update_p,
574 update_sp_offset, full_p);
575 if (new_rtx != XEXP (x, 0))
576 return gen_rtx_fmt_e (code, GET_MODE (x), new_rtx);
577 return x;
579 case SUBREG:
580 new_rtx = lra_eliminate_regs_1 (insn, SUBREG_REG (x), mem_mode,
581 subst_p, update_p,
582 update_sp_offset, full_p);
584 if (new_rtx != SUBREG_REG (x))
586 int x_size = GET_MODE_SIZE (GET_MODE (x));
587 int new_size = GET_MODE_SIZE (GET_MODE (new_rtx));
589 if (MEM_P (new_rtx) && x_size <= new_size)
591 SUBREG_REG (x) = new_rtx;
592 alter_subreg (&x, false);
593 return x;
595 else if (! subst_p)
597 /* LRA can transform subregs itself. So don't call
598 simplify_gen_subreg until LRA transformations are
599 finished. Function simplify_gen_subreg can do
600 non-trivial transformations (like truncation) which
601 might make LRA work to fail. */
602 SUBREG_REG (x) = new_rtx;
603 return x;
605 else
606 return simplify_gen_subreg (GET_MODE (x), new_rtx,
607 GET_MODE (new_rtx), SUBREG_BYTE (x));
610 return x;
612 case MEM:
613 /* Our only special processing is to pass the mode of the MEM to our
614 recursive call and copy the flags. While we are here, handle this
615 case more efficiently. */
616 return
617 replace_equiv_address_nv
619 lra_eliminate_regs_1 (insn, XEXP (x, 0), GET_MODE (x),
620 subst_p, update_p, update_sp_offset, full_p));
622 case USE:
623 /* Handle insn_list USE that a call to a pure function may generate. */
624 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, 0), VOIDmode,
625 subst_p, update_p, update_sp_offset, full_p);
626 if (new_rtx != XEXP (x, 0))
627 return gen_rtx_USE (GET_MODE (x), new_rtx);
628 return x;
630 case CLOBBER:
631 case SET:
632 gcc_unreachable ();
634 default:
635 break;
638 /* Process each of our operands recursively. If any have changed, make a
639 copy of the rtx. */
640 fmt = GET_RTX_FORMAT (code);
641 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
643 if (*fmt == 'e')
645 new_rtx = lra_eliminate_regs_1 (insn, XEXP (x, i), mem_mode,
646 subst_p, update_p,
647 update_sp_offset, full_p);
648 if (new_rtx != XEXP (x, i) && ! copied)
650 x = shallow_copy_rtx (x);
651 copied = 1;
653 XEXP (x, i) = new_rtx;
655 else if (*fmt == 'E')
657 int copied_vec = 0;
658 for (j = 0; j < XVECLEN (x, i); j++)
660 new_rtx = lra_eliminate_regs_1 (insn, XVECEXP (x, i, j), mem_mode,
661 subst_p, update_p,
662 update_sp_offset, full_p);
663 if (new_rtx != XVECEXP (x, i, j) && ! copied_vec)
665 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
666 XVEC (x, i)->elem);
667 if (! copied)
669 x = shallow_copy_rtx (x);
670 copied = 1;
672 XVEC (x, i) = new_v;
673 copied_vec = 1;
675 XVECEXP (x, i, j) = new_rtx;
680 return x;
683 /* This function is used externally in subsequent passes of GCC. It
684 always does a full elimination of X. */
686 lra_eliminate_regs (rtx x, machine_mode mem_mode,
687 rtx insn ATTRIBUTE_UNUSED)
689 return lra_eliminate_regs_1 (NULL, x, mem_mode, true, false, 0, true);
692 /* Stack pointer offset before the current insn relative to one at the
693 func start. RTL insns can change SP explicitly. We keep the
694 changes from one insn to another through this variable. */
695 static HOST_WIDE_INT curr_sp_change;
697 /* Scan rtx X for references to elimination source or target registers
698 in contexts that would prevent the elimination from happening.
699 Update the table of eliminables to reflect the changed state.
700 MEM_MODE is the mode of an enclosing MEM rtx, or VOIDmode if not
701 within a MEM. */
702 static void
703 mark_not_eliminable (rtx x, machine_mode mem_mode)
705 enum rtx_code code = GET_CODE (x);
706 struct lra_elim_table *ep;
707 int i, j;
708 const char *fmt;
710 switch (code)
712 case PRE_INC:
713 case POST_INC:
714 case PRE_DEC:
715 case POST_DEC:
716 case POST_MODIFY:
717 case PRE_MODIFY:
718 if (XEXP (x, 0) == stack_pointer_rtx
719 && ((code != PRE_MODIFY && code != POST_MODIFY)
720 || (GET_CODE (XEXP (x, 1)) == PLUS
721 && XEXP (x, 0) == XEXP (XEXP (x, 1), 0)
722 && CONST_INT_P (XEXP (XEXP (x, 1), 1)))))
724 int size = GET_MODE_SIZE (mem_mode);
726 #ifdef PUSH_ROUNDING
727 /* If more bytes than MEM_MODE are pushed, account for
728 them. */
729 size = PUSH_ROUNDING (size);
730 #endif
731 if (code == PRE_DEC || code == POST_DEC)
732 curr_sp_change -= size;
733 else if (code == PRE_INC || code == POST_INC)
734 curr_sp_change += size;
735 else if (code == PRE_MODIFY || code == POST_MODIFY)
736 curr_sp_change += INTVAL (XEXP (XEXP (x, 1), 1));
738 else if (REG_P (XEXP (x, 0))
739 && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER)
741 /* If we modify the source of an elimination rule, disable
742 it. Do the same if it is the destination and not the
743 hard frame register. */
744 for (ep = reg_eliminate;
745 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
746 ep++)
747 if (ep->from_rtx == XEXP (x, 0)
748 || (ep->to_rtx == XEXP (x, 0)
749 && ep->to_rtx != hard_frame_pointer_rtx))
750 setup_can_eliminate (ep, false);
752 return;
754 case USE:
755 if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
756 /* If using a hard register that is the source of an eliminate
757 we still think can be performed, note it cannot be
758 performed since we don't know how this hard register is
759 used. */
760 for (ep = reg_eliminate;
761 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
762 ep++)
763 if (ep->from_rtx == XEXP (x, 0)
764 && ep->to_rtx != hard_frame_pointer_rtx)
765 setup_can_eliminate (ep, false);
766 return;
768 case CLOBBER:
769 if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
770 /* If clobbering a hard register that is the replacement
771 register for an elimination we still think can be
772 performed, note that it cannot be performed. Otherwise, we
773 need not be concerned about it. */
774 for (ep = reg_eliminate;
775 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
776 ep++)
777 if (ep->to_rtx == XEXP (x, 0)
778 && ep->to_rtx != hard_frame_pointer_rtx)
779 setup_can_eliminate (ep, false);
780 return;
782 case SET:
783 if (SET_DEST (x) == stack_pointer_rtx
784 && GET_CODE (SET_SRC (x)) == PLUS
785 && XEXP (SET_SRC (x), 0) == SET_DEST (x)
786 && CONST_INT_P (XEXP (SET_SRC (x), 1)))
788 curr_sp_change += INTVAL (XEXP (SET_SRC (x), 1));
789 return;
791 if (! REG_P (SET_DEST (x))
792 || REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER)
793 mark_not_eliminable (SET_DEST (x), mem_mode);
794 else
796 /* See if this is setting the replacement hard register for
797 an elimination.
799 If DEST is the hard frame pointer, we do nothing because
800 we assume that all assignments to the frame pointer are
801 for non-local gotos and are being done at a time when
802 they are valid and do not disturb anything else. Some
803 machines want to eliminate a fake argument pointer (or
804 even a fake frame pointer) with either the real frame
805 pointer or the stack pointer. Assignments to the hard
806 frame pointer must not prevent this elimination. */
807 for (ep = reg_eliminate;
808 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
809 ep++)
810 if (ep->to_rtx == SET_DEST (x)
811 && SET_DEST (x) != hard_frame_pointer_rtx)
812 setup_can_eliminate (ep, false);
815 mark_not_eliminable (SET_SRC (x), mem_mode);
816 return;
818 case MEM:
819 /* Our only special processing is to pass the mode of the MEM to
820 our recursive call. */
821 mark_not_eliminable (XEXP (x, 0), GET_MODE (x));
822 return;
824 default:
825 break;
828 fmt = GET_RTX_FORMAT (code);
829 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
831 if (*fmt == 'e')
832 mark_not_eliminable (XEXP (x, i), mem_mode);
833 else if (*fmt == 'E')
834 for (j = 0; j < XVECLEN (x, i); j++)
835 mark_not_eliminable (XVECEXP (x, i, j), mem_mode);
841 #ifdef HARD_FRAME_POINTER_REGNUM
843 /* Find offset equivalence note for reg WHAT in INSN and return the
844 found elmination offset. If the note is not found, return NULL.
845 Remove the found note. */
846 static rtx
847 remove_reg_equal_offset_note (rtx insn, rtx what)
849 rtx link, *link_loc;
851 for (link_loc = &REG_NOTES (insn);
852 (link = *link_loc) != NULL_RTX;
853 link_loc = &XEXP (link, 1))
854 if (REG_NOTE_KIND (link) == REG_EQUAL
855 && GET_CODE (XEXP (link, 0)) == PLUS
856 && XEXP (XEXP (link, 0), 0) == what
857 && CONST_INT_P (XEXP (XEXP (link, 0), 1)))
859 *link_loc = XEXP (link, 1);
860 return XEXP (XEXP (link, 0), 1);
862 return NULL_RTX;
865 #endif
867 /* Scan INSN and eliminate all eliminable hard registers in it.
869 If REPLACE_P is true, do the replacement destructively. Also
870 delete the insn as dead it if it is setting an eliminable register.
872 If REPLACE_P is false, just update the offsets while keeping the
873 base register the same. If FIRST_P, use the sp offset for
874 elimination to sp. Otherwise, use UPDATE_SP_OFFSET for this.
875 Attach the note about used elimination for insns setting frame
876 pointer to update elimination easy (without parsing already
877 generated elimination insns to find offset previously used) in
878 future. */
880 void
881 eliminate_regs_in_insn (rtx_insn *insn, bool replace_p, bool first_p,
882 HOST_WIDE_INT update_sp_offset)
884 int icode = recog_memoized (insn);
885 rtx old_set = single_set (insn);
886 bool validate_p;
887 int i;
888 rtx substed_operand[MAX_RECOG_OPERANDS];
889 rtx orig_operand[MAX_RECOG_OPERANDS];
890 struct lra_elim_table *ep;
891 rtx plus_src, plus_cst_src;
892 lra_insn_recog_data_t id;
893 struct lra_static_insn_data *static_id;
895 if (icode < 0 && asm_noperands (PATTERN (insn)) < 0 && ! DEBUG_INSN_P (insn))
897 lra_assert (GET_CODE (PATTERN (insn)) == USE
898 || GET_CODE (PATTERN (insn)) == CLOBBER
899 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
900 return;
903 /* Check for setting an eliminable register. */
904 if (old_set != 0 && REG_P (SET_DEST (old_set))
905 && (ep = get_elimination (SET_DEST (old_set))) != NULL)
907 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
908 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
910 bool delete_p = replace_p;
912 #ifdef HARD_FRAME_POINTER_REGNUM
913 if (ep->from == FRAME_POINTER_REGNUM
914 && ep->to == HARD_FRAME_POINTER_REGNUM)
915 /* If this is setting the frame pointer register to the
916 hardware frame pointer register and this is an
917 elimination that will be done (tested above), this
918 insn is really adjusting the frame pointer downward
919 to compensate for the adjustment done before a
920 nonlocal goto. */
922 rtx src = SET_SRC (old_set);
923 rtx off = remove_reg_equal_offset_note (insn, ep->to_rtx);
925 if (off != NULL_RTX
926 || src == ep->to_rtx
927 || (GET_CODE (src) == PLUS
928 && XEXP (src, 0) == ep->to_rtx
929 && CONST_INT_P (XEXP (src, 1))))
931 HOST_WIDE_INT offset;
933 if (replace_p)
935 SET_DEST (old_set) = ep->to_rtx;
936 lra_update_insn_recog_data (insn);
937 return;
939 offset = (off != NULL_RTX ? INTVAL (off)
940 : src == ep->to_rtx ? 0 : INTVAL (XEXP (src, 1)));
941 offset -= (ep->offset - ep->previous_offset);
942 src = plus_constant (Pmode, ep->to_rtx, offset);
944 /* First see if this insn remains valid when we
945 make the change. If not, keep the INSN_CODE
946 the same and let the constraint pass fit it
947 up. */
948 validate_change (insn, &SET_SRC (old_set), src, 1);
949 validate_change (insn, &SET_DEST (old_set),
950 ep->from_rtx, 1);
951 if (! apply_change_group ())
953 SET_SRC (old_set) = src;
954 SET_DEST (old_set) = ep->from_rtx;
956 lra_update_insn_recog_data (insn);
957 /* Add offset note for future updates. */
958 add_reg_note (insn, REG_EQUAL, src);
959 return;
962 #endif
964 /* This insn isn't serving a useful purpose. We delete it
965 when REPLACE is set. */
966 if (delete_p)
967 lra_delete_dead_insn (insn);
968 return;
972 /* We allow one special case which happens to work on all machines we
973 currently support: a single set with the source or a REG_EQUAL
974 note being a PLUS of an eliminable register and a constant. */
975 plus_src = plus_cst_src = 0;
976 if (old_set && REG_P (SET_DEST (old_set)))
978 if (GET_CODE (SET_SRC (old_set)) == PLUS)
979 plus_src = SET_SRC (old_set);
980 /* First see if the source is of the form (plus (...) CST). */
981 if (plus_src
982 && CONST_INT_P (XEXP (plus_src, 1)))
983 plus_cst_src = plus_src;
984 /* Check that the first operand of the PLUS is a hard reg or
985 the lowpart subreg of one. */
986 if (plus_cst_src)
988 rtx reg = XEXP (plus_cst_src, 0);
990 if (GET_CODE (reg) == SUBREG && subreg_lowpart_p (reg))
991 reg = SUBREG_REG (reg);
993 if (!REG_P (reg) || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
994 plus_cst_src = 0;
997 if (plus_cst_src)
999 rtx reg = XEXP (plus_cst_src, 0);
1000 HOST_WIDE_INT offset = INTVAL (XEXP (plus_cst_src, 1));
1002 if (GET_CODE (reg) == SUBREG)
1003 reg = SUBREG_REG (reg);
1005 if (REG_P (reg) && (ep = get_elimination (reg)) != NULL)
1007 rtx to_rtx = replace_p ? ep->to_rtx : ep->from_rtx;
1009 if (! replace_p)
1011 offset += (ep->offset - ep->previous_offset);
1012 if (ep->to_rtx == stack_pointer_rtx)
1014 if (first_p)
1015 offset -= lra_get_insn_recog_data (insn)->sp_offset;
1016 else
1017 offset += update_sp_offset;
1019 offset = trunc_int_for_mode (offset, GET_MODE (plus_cst_src));
1022 if (GET_CODE (XEXP (plus_cst_src, 0)) == SUBREG)
1023 to_rtx = gen_lowpart (GET_MODE (XEXP (plus_cst_src, 0)), to_rtx);
1024 /* If we have a nonzero offset, and the source is already a
1025 simple REG, the following transformation would increase
1026 the cost of the insn by replacing a simple REG with (plus
1027 (reg sp) CST). So try only when we already had a PLUS
1028 before. */
1029 if (offset == 0 || plus_src)
1031 rtx new_src = plus_constant (GET_MODE (to_rtx), to_rtx, offset);
1033 old_set = single_set (insn);
1035 /* First see if this insn remains valid when we make the
1036 change. If not, try to replace the whole pattern
1037 with a simple set (this may help if the original insn
1038 was a PARALLEL that was only recognized as single_set
1039 due to REG_UNUSED notes). If this isn't valid
1040 either, keep the INSN_CODE the same and let the
1041 constraint pass fix it up. */
1042 if (! validate_change (insn, &SET_SRC (old_set), new_src, 0))
1044 rtx new_pat = gen_rtx_SET (VOIDmode,
1045 SET_DEST (old_set), new_src);
1047 if (! validate_change (insn, &PATTERN (insn), new_pat, 0))
1048 SET_SRC (old_set) = new_src;
1050 lra_update_insn_recog_data (insn);
1051 /* This can't have an effect on elimination offsets, so skip
1052 right to the end. */
1053 return;
1058 /* Eliminate all eliminable registers occurring in operands that
1059 can be handled by the constraint pass. */
1060 id = lra_get_insn_recog_data (insn);
1061 static_id = id->insn_static_data;
1062 validate_p = false;
1063 for (i = 0; i < static_id->n_operands; i++)
1065 orig_operand[i] = *id->operand_loc[i];
1066 substed_operand[i] = *id->operand_loc[i];
1068 /* For an asm statement, every operand is eliminable. */
1069 if (icode < 0 || insn_data[icode].operand[i].eliminable)
1071 /* Check for setting a hard register that we know about. */
1072 if (static_id->operand[i].type != OP_IN
1073 && REG_P (orig_operand[i]))
1075 /* If we are assigning to a hard register that can be
1076 eliminated, it must be as part of a PARALLEL, since
1077 the code above handles single SETs. This reg can not
1078 be longer eliminated -- it is forced by
1079 mark_not_eliminable. */
1080 for (ep = reg_eliminate;
1081 ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
1082 ep++)
1083 lra_assert (ep->from_rtx != orig_operand[i]
1084 || ! ep->can_eliminate);
1087 /* Companion to the above plus substitution, we can allow
1088 invariants as the source of a plain move. */
1089 substed_operand[i]
1090 = lra_eliminate_regs_1 (insn, *id->operand_loc[i], VOIDmode,
1091 replace_p, ! replace_p && ! first_p,
1092 update_sp_offset, first_p);
1093 if (substed_operand[i] != orig_operand[i])
1094 validate_p = true;
1098 if (! validate_p)
1099 return;
1101 /* Substitute the operands; the new values are in the substed_operand
1102 array. */
1103 for (i = 0; i < static_id->n_operands; i++)
1104 *id->operand_loc[i] = substed_operand[i];
1105 for (i = 0; i < static_id->n_dups; i++)
1106 *id->dup_loc[i] = substed_operand[(int) static_id->dup_num[i]];
1108 /* If we had a move insn but now we don't, re-recognize it.
1109 This will cause spurious re-recognition if the old move had a
1110 PARALLEL since the new one still will, but we can't call
1111 single_set without having put new body into the insn and the
1112 re-recognition won't hurt in this rare case. */
1113 id = lra_update_insn_recog_data (insn);
1114 static_id = id->insn_static_data;
1117 /* Spill pseudos which are assigned to hard registers in SET. Add
1118 affected insns for processing in the subsequent constraint
1119 pass. */
1120 static void
1121 spill_pseudos (HARD_REG_SET set)
1123 int i;
1124 bitmap_head to_process;
1125 rtx_insn *insn;
1127 if (hard_reg_set_empty_p (set))
1128 return;
1129 if (lra_dump_file != NULL)
1131 fprintf (lra_dump_file, " Spilling non-eliminable hard regs:");
1132 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1133 if (TEST_HARD_REG_BIT (set, i))
1134 fprintf (lra_dump_file, " %d", i);
1135 fprintf (lra_dump_file, "\n");
1137 bitmap_initialize (&to_process, &reg_obstack);
1138 for (i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
1139 if (lra_reg_info[i].nrefs != 0 && reg_renumber[i] >= 0
1140 && overlaps_hard_reg_set_p (set,
1141 PSEUDO_REGNO_MODE (i), reg_renumber[i]))
1143 if (lra_dump_file != NULL)
1144 fprintf (lra_dump_file, " Spilling r%d(%d)\n",
1145 i, reg_renumber[i]);
1146 reg_renumber[i] = -1;
1147 bitmap_ior_into (&to_process, &lra_reg_info[i].insn_bitmap);
1149 IOR_HARD_REG_SET (lra_no_alloc_regs, set);
1150 for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
1151 if (bitmap_bit_p (&to_process, INSN_UID (insn)))
1153 lra_push_insn (insn);
1154 lra_set_used_insn_alternative (insn, -1);
1156 bitmap_clear (&to_process);
1159 /* Update all offsets and possibility for elimination on eliminable
1160 registers. Spill pseudos assigned to registers which are
1161 uneliminable, update LRA_NO_ALLOC_REGS and ELIMINABLE_REG_SET. Add
1162 insns to INSNS_WITH_CHANGED_OFFSETS containing eliminable hard
1163 registers whose offsets should be changed. Return true if any
1164 elimination offset changed. */
1165 static bool
1166 update_reg_eliminate (bitmap insns_with_changed_offsets)
1168 bool prev, result;
1169 struct lra_elim_table *ep, *ep1;
1170 HARD_REG_SET temp_hard_reg_set;
1172 /* Clear self elimination offsets. */
1173 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1174 self_elim_offsets[ep->from] = 0;
1175 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1177 /* If it is a currently used elimination: update the previous
1178 offset. */
1179 if (elimination_map[ep->from] == ep)
1180 ep->previous_offset = ep->offset;
1182 prev = ep->prev_can_eliminate;
1183 setup_can_eliminate (ep, targetm.can_eliminate (ep->from, ep->to));
1184 if (ep->can_eliminate && ! prev)
1186 /* It is possible that not eliminable register becomes
1187 eliminable because we took other reasons into account to
1188 set up eliminable regs in the initial set up. Just
1189 ignore new eliminable registers. */
1190 setup_can_eliminate (ep, false);
1191 continue;
1193 if (ep->can_eliminate != prev && elimination_map[ep->from] == ep)
1195 /* We cannot use this elimination anymore -- find another
1196 one. */
1197 if (lra_dump_file != NULL)
1198 fprintf (lra_dump_file,
1199 " Elimination %d to %d is not possible anymore\n",
1200 ep->from, ep->to);
1201 /* If after processing RTL we decides that SP can be used as
1202 a result of elimination, it can not be changed. */
1203 gcc_assert ((ep->to_rtx != stack_pointer_rtx)
1204 || (ep->from < FIRST_PSEUDO_REGISTER
1205 && fixed_regs [ep->from]));
1206 /* Mark that is not eliminable anymore. */
1207 elimination_map[ep->from] = NULL;
1208 for (ep1 = ep + 1; ep1 < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep1++)
1209 if (ep1->can_eliminate && ep1->from == ep->from)
1210 break;
1211 if (ep1 < &reg_eliminate[NUM_ELIMINABLE_REGS])
1213 if (lra_dump_file != NULL)
1214 fprintf (lra_dump_file, " Using elimination %d to %d now\n",
1215 ep1->from, ep1->to);
1216 lra_assert (ep1->previous_offset == 0);
1217 ep1->previous_offset = ep->offset;
1219 else
1221 /* There is no elimination anymore just use the hard
1222 register `from' itself. Setup self elimination
1223 offset to restore the original offset values. */
1224 if (lra_dump_file != NULL)
1225 fprintf (lra_dump_file, " %d is not eliminable at all\n",
1226 ep->from);
1227 self_elim_offsets[ep->from] = -ep->offset;
1228 if (ep->offset != 0)
1229 bitmap_ior_into (insns_with_changed_offsets,
1230 &lra_reg_info[ep->from].insn_bitmap);
1234 #ifdef ELIMINABLE_REGS
1235 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->offset);
1236 #else
1237 INITIAL_FRAME_POINTER_OFFSET (ep->offset);
1238 #endif
1240 setup_elimination_map ();
1241 result = false;
1242 CLEAR_HARD_REG_SET (temp_hard_reg_set);
1243 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1244 if (elimination_map[ep->from] == NULL)
1245 SET_HARD_REG_BIT (temp_hard_reg_set, ep->from);
1246 else if (elimination_map[ep->from] == ep)
1248 /* Prevent the hard register into which we eliminate from
1249 the usage for pseudos. */
1250 if (ep->from != ep->to)
1251 SET_HARD_REG_BIT (temp_hard_reg_set, ep->to);
1252 if (ep->previous_offset != ep->offset)
1254 bitmap_ior_into (insns_with_changed_offsets,
1255 &lra_reg_info[ep->from].insn_bitmap);
1257 /* Update offset when the eliminate offset have been
1258 changed. */
1259 lra_update_reg_val_offset (lra_reg_info[ep->from].val,
1260 ep->offset - ep->previous_offset);
1261 result = true;
1264 IOR_HARD_REG_SET (lra_no_alloc_regs, temp_hard_reg_set);
1265 AND_COMPL_HARD_REG_SET (eliminable_regset, temp_hard_reg_set);
1266 spill_pseudos (temp_hard_reg_set);
1267 return result;
1270 /* Initialize the table of hard registers to eliminate.
1271 Pre-condition: global flag frame_pointer_needed has been set before
1272 calling this function. */
1273 static void
1274 init_elim_table (void)
1276 struct lra_elim_table *ep;
1277 #ifdef ELIMINABLE_REGS
1278 bool value_p;
1279 const struct elim_table_1 *ep1;
1280 #endif
1282 if (!reg_eliminate)
1283 reg_eliminate = XCNEWVEC (struct lra_elim_table, NUM_ELIMINABLE_REGS);
1285 memset (self_elim_offsets, 0, sizeof (self_elim_offsets));
1286 /* Initiate member values which will be never changed. */
1287 self_elim_table.can_eliminate = self_elim_table.prev_can_eliminate = true;
1288 self_elim_table.previous_offset = 0;
1289 #ifdef ELIMINABLE_REGS
1290 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
1291 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
1293 ep->offset = ep->previous_offset = 0;
1294 ep->from = ep1->from;
1295 ep->to = ep1->to;
1296 value_p = (targetm.can_eliminate (ep->from, ep->to)
1297 && ! (ep->to == STACK_POINTER_REGNUM
1298 && frame_pointer_needed
1299 && (! SUPPORTS_STACK_ALIGNMENT
1300 || ! stack_realign_fp)));
1301 setup_can_eliminate (ep, value_p);
1303 #else
1304 reg_eliminate[0].offset = reg_eliminate[0].previous_offset = 0;
1305 reg_eliminate[0].from = reg_eliminate_1[0].from;
1306 reg_eliminate[0].to = reg_eliminate_1[0].to;
1307 setup_can_eliminate (&reg_eliminate[0], ! frame_pointer_needed);
1308 #endif
1310 /* Build the FROM and TO REG rtx's. Note that code in gen_rtx_REG
1311 will cause, e.g., gen_rtx_REG (Pmode, STACK_POINTER_REGNUM) to
1312 equal stack_pointer_rtx. We depend on this. Threfore we switch
1313 off that we are in LRA temporarily. */
1314 lra_in_progress = 0;
1315 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1317 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
1318 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
1319 eliminable_reg_rtx[ep->from] = ep->from_rtx;
1321 lra_in_progress = 1;
1324 /* Function for initialization of elimination once per function. It
1325 sets up sp offset for each insn. */
1326 static void
1327 init_elimination (void)
1329 bool stop_to_sp_elimination_p;
1330 basic_block bb;
1331 rtx_insn *insn;
1332 struct lra_elim_table *ep;
1334 init_elim_table ();
1335 FOR_EACH_BB_FN (bb, cfun)
1337 curr_sp_change = 0;
1338 stop_to_sp_elimination_p = false;
1339 FOR_BB_INSNS (bb, insn)
1340 if (INSN_P (insn))
1342 lra_get_insn_recog_data (insn)->sp_offset = curr_sp_change;
1343 if (NONDEBUG_INSN_P (insn))
1345 mark_not_eliminable (PATTERN (insn), VOIDmode);
1346 if (curr_sp_change != 0
1347 && find_reg_note (insn, REG_LABEL_OPERAND, NULL_RTX))
1348 stop_to_sp_elimination_p = true;
1351 if (! frame_pointer_needed
1352 && (curr_sp_change != 0 || stop_to_sp_elimination_p)
1353 && bb->succs && bb->succs->length () != 0)
1354 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1355 if (ep->to == STACK_POINTER_REGNUM)
1356 setup_can_eliminate (ep, false);
1358 setup_elimination_map ();
1361 /* Eliminate hard reg given by its location LOC. */
1362 void
1363 lra_eliminate_reg_if_possible (rtx *loc)
1365 int regno;
1366 struct lra_elim_table *ep;
1368 lra_assert (REG_P (*loc));
1369 if ((regno = REGNO (*loc)) >= FIRST_PSEUDO_REGISTER
1370 || ! TEST_HARD_REG_BIT (lra_no_alloc_regs, regno))
1371 return;
1372 if ((ep = get_elimination (*loc)) != NULL)
1373 *loc = ep->to_rtx;
1376 /* Do (final if FINAL_P or first if FIRST_P) elimination in INSN. Add
1377 the insn for subsequent processing in the constraint pass, update
1378 the insn info. */
1379 static void
1380 process_insn_for_elimination (rtx_insn *insn, bool final_p, bool first_p)
1382 eliminate_regs_in_insn (insn, final_p, first_p, 0);
1383 if (! final_p)
1385 /* Check that insn changed its code. This is a case when a move
1386 insn becomes an add insn and we do not want to process the
1387 insn as a move anymore. */
1388 int icode = recog (PATTERN (insn), insn, 0);
1390 if (icode >= 0 && icode != INSN_CODE (insn))
1392 INSN_CODE (insn) = icode;
1393 lra_update_insn_recog_data (insn);
1395 lra_update_insn_regno_info (insn);
1396 lra_push_insn (insn);
1397 lra_set_used_insn_alternative (insn, -1);
1401 /* Entry function to do final elimination if FINAL_P or to update
1402 elimination register offsets (FIRST_P if we are doing it the first
1403 time). */
1404 void
1405 lra_eliminate (bool final_p, bool first_p)
1407 unsigned int uid;
1408 bitmap_head insns_with_changed_offsets;
1409 bitmap_iterator bi;
1410 struct lra_elim_table *ep;
1412 gcc_assert (! final_p || ! first_p);
1414 timevar_push (TV_LRA_ELIMINATE);
1416 if (first_p)
1417 init_elimination ();
1419 bitmap_initialize (&insns_with_changed_offsets, &reg_obstack);
1420 if (final_p)
1422 #ifdef ENABLE_CHECKING
1423 update_reg_eliminate (&insns_with_changed_offsets);
1424 if (! bitmap_empty_p (&insns_with_changed_offsets))
1425 gcc_unreachable ();
1426 #endif
1427 /* We change eliminable hard registers in insns so we should do
1428 this for all insns containing any eliminable hard
1429 register. */
1430 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1431 if (elimination_map[ep->from] != NULL)
1432 bitmap_ior_into (&insns_with_changed_offsets,
1433 &lra_reg_info[ep->from].insn_bitmap);
1435 else if (! update_reg_eliminate (&insns_with_changed_offsets))
1436 goto lra_eliminate_done;
1437 if (lra_dump_file != NULL)
1439 fprintf (lra_dump_file, "New elimination table:\n");
1440 print_elim_table (lra_dump_file);
1442 EXECUTE_IF_SET_IN_BITMAP (&insns_with_changed_offsets, 0, uid, bi)
1443 /* A dead insn can be deleted in process_insn_for_elimination. */
1444 if (lra_insn_recog_data[uid] != NULL)
1445 process_insn_for_elimination (lra_insn_recog_data[uid]->insn,
1446 final_p, first_p);
1447 bitmap_clear (&insns_with_changed_offsets);
1449 lra_eliminate_done:
1450 timevar_pop (TV_LRA_ELIMINATE);