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