Bump version number, post release.
[official-gcc.git] / gcc-4_9-branch / gcc / lra-constraints.c
blobae8f3cd949da5f75eb7d9dccc8b3df9ba3c04879
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2014 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file contains code for 3 passes: constraint pass,
23 inheritance/split pass, and pass for undoing failed inheritance and
24 split.
26 The major goal of constraint pass is to transform RTL to satisfy
27 insn and address constraints by:
28 o choosing insn alternatives;
29 o generating *reload insns* (or reloads in brief) and *reload
30 pseudos* which will get necessary hard registers later;
31 o substituting pseudos with equivalent values and removing the
32 instructions that initialized those pseudos.
34 The constraint pass has biggest and most complicated code in LRA.
35 There are a lot of important details like:
36 o reuse of input reload pseudos to simplify reload pseudo
37 allocations;
38 o some heuristics to choose insn alternative to improve the
39 inheritance;
40 o early clobbers etc.
42 The pass is mimicking former reload pass in alternative choosing
43 because the reload pass is oriented to current machine description
44 model. It might be changed if the machine description model is
45 changed.
47 There is special code for preventing all LRA and this pass cycling
48 in case of bugs.
50 On the first iteration of the pass we process every instruction and
51 choose an alternative for each one. On subsequent iterations we try
52 to avoid reprocessing instructions if we can be sure that the old
53 choice is still valid.
55 The inheritance/spilt pass is to transform code to achieve
56 ineheritance and live range splitting. It is done on backward
57 traversal of EBBs.
59 The inheritance optimization goal is to reuse values in hard
60 registers. There is analogous optimization in old reload pass. The
61 inheritance is achieved by following transformation:
63 reload_p1 <- p reload_p1 <- p
64 ... new_p <- reload_p1
65 ... => ...
66 reload_p2 <- p reload_p2 <- new_p
68 where p is spilled and not changed between the insns. Reload_p1 is
69 also called *original pseudo* and new_p is called *inheritance
70 pseudo*.
72 The subsequent assignment pass will try to assign the same (or
73 another if it is not possible) hard register to new_p as to
74 reload_p1 or reload_p2.
76 If the assignment pass fails to assign a hard register to new_p,
77 this file will undo the inheritance and restore the original code.
78 This is because implementing the above sequence with a spilled
79 new_p would make the code much worse. The inheritance is done in
80 EBB scope. The above is just a simplified example to get an idea
81 of the inheritance as the inheritance is also done for non-reload
82 insns.
84 Splitting (transformation) is also done in EBB scope on the same
85 pass as the inheritance:
87 r <- ... or ... <- r r <- ... or ... <- r
88 ... s <- r (new insn -- save)
89 ... =>
90 ... r <- s (new insn -- restore)
91 ... <- r ... <- r
93 The *split pseudo* s is assigned to the hard register of the
94 original pseudo or hard register r.
96 Splitting is done:
97 o In EBBs with high register pressure for global pseudos (living
98 in at least 2 BBs) and assigned to hard registers when there
99 are more one reloads needing the hard registers;
100 o for pseudos needing save/restore code around calls.
102 If the split pseudo still has the same hard register as the
103 original pseudo after the subsequent assignment pass or the
104 original pseudo was split, the opposite transformation is done on
105 the same pass for undoing inheritance. */
107 #undef REG_OK_STRICT
109 #include "config.h"
110 #include "system.h"
111 #include "coretypes.h"
112 #include "tm.h"
113 #include "hard-reg-set.h"
114 #include "rtl.h"
115 #include "tm_p.h"
116 #include "regs.h"
117 #include "insn-config.h"
118 #include "insn-codes.h"
119 #include "recog.h"
120 #include "output.h"
121 #include "addresses.h"
122 #include "target.h"
123 #include "function.h"
124 #include "expr.h"
125 #include "basic-block.h"
126 #include "except.h"
127 #include "optabs.h"
128 #include "df.h"
129 #include "ira.h"
130 #include "rtl-error.h"
131 #include "lra-int.h"
133 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
134 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
135 reload insns. */
136 static int bb_reload_num;
138 /* The current insn being processed and corresponding its single set
139 (NULL otherwise), its data (basic block, the insn data, the insn
140 static data, and the mode of each operand). */
141 static rtx curr_insn;
142 static rtx curr_insn_set;
143 static basic_block curr_bb;
144 static lra_insn_recog_data_t curr_id;
145 static struct lra_static_insn_data *curr_static_id;
146 static enum machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
147 /* Mode of the register substituted by its equivalence with VOIDmode
148 (e.g. constant) and whose subreg is given operand of the current
149 insn. VOIDmode in all other cases. */
150 static machine_mode original_subreg_reg_mode[MAX_RECOG_OPERANDS];
154 /* Start numbers for new registers and insns at the current constraints
155 pass start. */
156 static int new_regno_start;
157 static int new_insn_uid_start;
159 /* If LOC is nonnull, strip any outer subreg from it. */
160 static inline rtx *
161 strip_subreg (rtx *loc)
163 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
166 /* Return hard regno of REGNO or if it is was not assigned to a hard
167 register, use a hard register from its allocno class. */
168 static int
169 get_try_hard_regno (int regno)
171 int hard_regno;
172 enum reg_class rclass;
174 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
175 hard_regno = lra_get_regno_hard_regno (regno);
176 if (hard_regno >= 0)
177 return hard_regno;
178 rclass = lra_get_allocno_class (regno);
179 if (rclass == NO_REGS)
180 return -1;
181 return ira_class_hard_regs[rclass][0];
184 /* Return final hard regno (plus offset) which will be after
185 elimination. We do this for matching constraints because the final
186 hard regno could have a different class. */
187 static int
188 get_final_hard_regno (int hard_regno, int offset)
190 if (hard_regno < 0)
191 return hard_regno;
192 hard_regno = lra_get_elimination_hard_regno (hard_regno);
193 return hard_regno + offset;
196 /* Return hard regno of X after removing subreg and making
197 elimination. If X is not a register or subreg of register, return
198 -1. For pseudo use its assignment. */
199 static int
200 get_hard_regno (rtx x)
202 rtx reg;
203 int offset, hard_regno;
205 reg = x;
206 if (GET_CODE (x) == SUBREG)
207 reg = SUBREG_REG (x);
208 if (! REG_P (reg))
209 return -1;
210 if ((hard_regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
211 hard_regno = lra_get_regno_hard_regno (hard_regno);
212 if (hard_regno < 0)
213 return -1;
214 offset = 0;
215 if (GET_CODE (x) == SUBREG)
216 offset += subreg_regno_offset (hard_regno, GET_MODE (reg),
217 SUBREG_BYTE (x), GET_MODE (x));
218 return get_final_hard_regno (hard_regno, offset);
221 /* If REGNO is a hard register or has been allocated a hard register,
222 return the class of that register. If REGNO is a reload pseudo
223 created by the current constraints pass, return its allocno class.
224 Return NO_REGS otherwise. */
225 static enum reg_class
226 get_reg_class (int regno)
228 int hard_regno;
230 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
231 hard_regno = lra_get_regno_hard_regno (regno);
232 if (hard_regno >= 0)
234 hard_regno = get_final_hard_regno (hard_regno, 0);
235 return REGNO_REG_CLASS (hard_regno);
237 if (regno >= new_regno_start)
238 return lra_get_allocno_class (regno);
239 return NO_REGS;
242 /* Return true if REG satisfies (or will satisfy) reg class constraint
243 CL. Use elimination first if REG is a hard register. If REG is a
244 reload pseudo created by this constraints pass, assume that it will
245 be allocated a hard register from its allocno class, but allow that
246 class to be narrowed to CL if it is currently a superset of CL.
248 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
249 REGNO (reg), or NO_REGS if no change in its class was needed. */
250 static bool
251 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
253 enum reg_class rclass, common_class;
254 enum machine_mode reg_mode;
255 int class_size, hard_regno, nregs, i, j;
256 int regno = REGNO (reg);
258 if (new_class != NULL)
259 *new_class = NO_REGS;
260 if (regno < FIRST_PSEUDO_REGISTER)
262 rtx final_reg = reg;
263 rtx *final_loc = &final_reg;
265 lra_eliminate_reg_if_possible (final_loc);
266 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
268 reg_mode = GET_MODE (reg);
269 rclass = get_reg_class (regno);
270 if (regno < new_regno_start
271 /* Do not allow the constraints for reload instructions to
272 influence the classes of new pseudos. These reloads are
273 typically moves that have many alternatives, and restricting
274 reload pseudos for one alternative may lead to situations
275 where other reload pseudos are no longer allocatable. */
276 || (INSN_UID (curr_insn) >= new_insn_uid_start
277 && curr_insn_set != NULL
278 && ((OBJECT_P (SET_SRC (curr_insn_set))
279 && ! CONSTANT_P (SET_SRC (curr_insn_set)))
280 || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
281 && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))
282 && ! CONSTANT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
283 /* When we don't know what class will be used finally for reload
284 pseudos, we use ALL_REGS. */
285 return ((regno >= new_regno_start && rclass == ALL_REGS)
286 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
287 && ! hard_reg_set_subset_p (reg_class_contents[cl],
288 lra_no_alloc_regs)));
289 else
291 common_class = ira_reg_class_subset[rclass][cl];
292 if (new_class != NULL)
293 *new_class = common_class;
294 if (hard_reg_set_subset_p (reg_class_contents[common_class],
295 lra_no_alloc_regs))
296 return false;
297 /* Check that there are enough allocatable regs. */
298 class_size = ira_class_hard_regs_num[common_class];
299 for (i = 0; i < class_size; i++)
301 hard_regno = ira_class_hard_regs[common_class][i];
302 nregs = hard_regno_nregs[hard_regno][reg_mode];
303 if (nregs == 1)
304 return true;
305 for (j = 0; j < nregs; j++)
306 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
307 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
308 hard_regno + j))
309 break;
310 if (j >= nregs)
311 return true;
313 return false;
317 /* Return true if REGNO satisfies a memory constraint. */
318 static bool
319 in_mem_p (int regno)
321 return get_reg_class (regno) == NO_REGS;
324 /* Initiate equivalences for LRA. As we keep original equivalences
325 before any elimination, we need to make copies otherwise any change
326 in insns might change the equivalences. */
327 void
328 lra_init_equiv (void)
330 ira_expand_reg_equiv ();
331 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
333 rtx res;
335 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
336 ira_reg_equiv[i].memory = copy_rtx (res);
337 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
338 ira_reg_equiv[i].invariant = copy_rtx (res);
342 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
344 /* Update equivalence for REGNO. We need to this as the equivalence
345 might contain other pseudos which are changed by their
346 equivalences. */
347 static void
348 update_equiv (int regno)
350 rtx x;
352 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
353 ira_reg_equiv[regno].memory
354 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
355 NULL_RTX);
356 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
357 ira_reg_equiv[regno].invariant
358 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
359 NULL_RTX);
362 /* If we have decided to substitute X with another value, return that
363 value, otherwise return X. */
364 static rtx
365 get_equiv (rtx x)
367 int regno;
368 rtx res;
370 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
371 || ! ira_reg_equiv[regno].defined_p
372 || ! ira_reg_equiv[regno].profitable_p
373 || lra_get_regno_hard_regno (regno) >= 0)
374 return x;
375 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
376 return res;
377 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
378 return res;
379 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
380 return res;
381 gcc_unreachable ();
384 /* If we have decided to substitute X with the equivalent value,
385 return that value after elimination for INSN, otherwise return
386 X. */
387 static rtx
388 get_equiv_with_elimination (rtx x, rtx insn)
390 rtx res = get_equiv (x);
392 if (x == res || CONSTANT_P (res))
393 return res;
394 return lra_eliminate_regs_1 (insn, res, GET_MODE (res), false, false, true);
397 /* Set up curr_operand_mode. */
398 static void
399 init_curr_operand_mode (void)
401 int nop = curr_static_id->n_operands;
402 for (int i = 0; i < nop; i++)
404 enum machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
405 if (mode == VOIDmode)
407 /* The .md mode for address operands is the mode of the
408 addressed value rather than the mode of the address itself. */
409 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
410 mode = Pmode;
411 else
412 mode = curr_static_id->operand[i].mode;
414 curr_operand_mode[i] = mode;
420 /* The page contains code to reuse input reloads. */
422 /* Structure describes input reload of the current insns. */
423 struct input_reload
425 /* Reloaded value. */
426 rtx input;
427 /* Reload pseudo used. */
428 rtx reg;
431 /* The number of elements in the following array. */
432 static int curr_insn_input_reloads_num;
433 /* Array containing info about input reloads. It is used to find the
434 same input reload and reuse the reload pseudo in this case. */
435 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
437 /* Initiate data concerning reuse of input reloads for the current
438 insn. */
439 static void
440 init_curr_insn_input_reloads (void)
442 curr_insn_input_reloads_num = 0;
445 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
446 created input reload pseudo (only if TYPE is not OP_OUT). Don't
447 reuse pseudo if IN_SUBREG_P is true and the reused pseudo should be
448 wrapped up in SUBREG. The result pseudo is returned through
449 RESULT_REG. Return TRUE if we created a new pseudo, FALSE if we
450 reused the already created input reload pseudo. Use TITLE to
451 describe new registers for debug purposes. */
452 static bool
453 get_reload_reg (enum op_type type, enum machine_mode mode, rtx original,
454 enum reg_class rclass, bool in_subreg_p,
455 const char *title, rtx *result_reg)
457 int i, regno;
458 enum reg_class new_class;
460 if (type == OP_OUT)
462 *result_reg
463 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
464 return true;
466 /* Prevent reuse value of expression with side effects,
467 e.g. volatile memory. */
468 if (! side_effects_p (original))
469 for (i = 0; i < curr_insn_input_reloads_num; i++)
470 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
471 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
473 rtx reg = curr_insn_input_reloads[i].reg;
474 regno = REGNO (reg);
475 /* If input is equal to original and both are VOIDmode,
476 GET_MODE (reg) might be still different from mode.
477 Ensure we don't return *result_reg with wrong mode. */
478 if (GET_MODE (reg) != mode)
480 if (in_subreg_p)
481 continue;
482 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
483 continue;
484 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
485 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
486 continue;
488 *result_reg = reg;
489 if (lra_dump_file != NULL)
491 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
492 dump_value_slim (lra_dump_file, original, 1);
494 if (new_class != lra_get_allocno_class (regno))
495 lra_change_class (regno, new_class, ", change to", false);
496 if (lra_dump_file != NULL)
497 fprintf (lra_dump_file, "\n");
498 return false;
500 *result_reg = lra_create_new_reg (mode, original, rclass, title);
501 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
502 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
503 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
504 return true;
509 /* The page contains code to extract memory address parts. */
511 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
512 static inline bool
513 ok_for_index_p_nonstrict (rtx reg)
515 unsigned regno = REGNO (reg);
517 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
520 /* A version of regno_ok_for_base_p for use here, when all pseudos
521 should count as OK. Arguments as for regno_ok_for_base_p. */
522 static inline bool
523 ok_for_base_p_nonstrict (rtx reg, enum machine_mode mode, addr_space_t as,
524 enum rtx_code outer_code, enum rtx_code index_code)
526 unsigned regno = REGNO (reg);
528 if (regno >= FIRST_PSEUDO_REGISTER)
529 return true;
530 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
535 /* The page contains major code to choose the current insn alternative
536 and generate reloads for it. */
538 /* Return the offset from REGNO of the least significant register
539 in (reg:MODE REGNO).
541 This function is used to tell whether two registers satisfy
542 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
544 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
545 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
547 lra_constraint_offset (int regno, enum machine_mode mode)
549 lra_assert (regno < FIRST_PSEUDO_REGISTER);
550 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
551 && SCALAR_INT_MODE_P (mode))
552 return hard_regno_nregs[regno][mode] - 1;
553 return 0;
556 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
557 if they are the same hard reg, and has special hacks for
558 auto-increment and auto-decrement. This is specifically intended for
559 process_alt_operands to use in determining whether two operands
560 match. X is the operand whose number is the lower of the two.
562 It is supposed that X is the output operand and Y is the input
563 operand. Y_HARD_REGNO is the final hard regno of register Y or
564 register in subreg Y as we know it now. Otherwise, it is a
565 negative value. */
566 static bool
567 operands_match_p (rtx x, rtx y, int y_hard_regno)
569 int i;
570 RTX_CODE code = GET_CODE (x);
571 const char *fmt;
573 if (x == y)
574 return true;
575 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
576 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
578 int j;
580 i = get_hard_regno (x);
581 if (i < 0)
582 goto slow;
584 if ((j = y_hard_regno) < 0)
585 goto slow;
587 i += lra_constraint_offset (i, GET_MODE (x));
588 j += lra_constraint_offset (j, GET_MODE (y));
590 return i == j;
593 /* If two operands must match, because they are really a single
594 operand of an assembler insn, then two post-increments are invalid
595 because the assembler insn would increment only once. On the
596 other hand, a post-increment matches ordinary indexing if the
597 post-increment is the output operand. */
598 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
599 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
601 /* Two pre-increments are invalid because the assembler insn would
602 increment only once. On the other hand, a pre-increment matches
603 ordinary indexing if the pre-increment is the input operand. */
604 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
605 || GET_CODE (y) == PRE_MODIFY)
606 return operands_match_p (x, XEXP (y, 0), -1);
608 slow:
610 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
611 && x == SUBREG_REG (y))
612 return true;
613 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
614 && SUBREG_REG (x) == y)
615 return true;
617 /* Now we have disposed of all the cases in which different rtx
618 codes can match. */
619 if (code != GET_CODE (y))
620 return false;
622 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
623 if (GET_MODE (x) != GET_MODE (y))
624 return false;
626 switch (code)
628 CASE_CONST_UNIQUE:
629 return false;
631 case LABEL_REF:
632 return XEXP (x, 0) == XEXP (y, 0);
633 case SYMBOL_REF:
634 return XSTR (x, 0) == XSTR (y, 0);
636 default:
637 break;
640 /* Compare the elements. If any pair of corresponding elements fail
641 to match, return false for the whole things. */
643 fmt = GET_RTX_FORMAT (code);
644 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
646 int val, j;
647 switch (fmt[i])
649 case 'w':
650 if (XWINT (x, i) != XWINT (y, i))
651 return false;
652 break;
654 case 'i':
655 if (XINT (x, i) != XINT (y, i))
656 return false;
657 break;
659 case 'e':
660 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
661 if (val == 0)
662 return false;
663 break;
665 case '0':
666 break;
668 case 'E':
669 if (XVECLEN (x, i) != XVECLEN (y, i))
670 return false;
671 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
673 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
674 if (val == 0)
675 return false;
677 break;
679 /* It is believed that rtx's at this level will never
680 contain anything but integers and other rtx's, except for
681 within LABEL_REFs and SYMBOL_REFs. */
682 default:
683 gcc_unreachable ();
686 return true;
689 /* True if X is a constant that can be forced into the constant pool.
690 MODE is the mode of the operand, or VOIDmode if not known. */
691 #define CONST_POOL_OK_P(MODE, X) \
692 ((MODE) != VOIDmode \
693 && CONSTANT_P (X) \
694 && GET_CODE (X) != HIGH \
695 && !targetm.cannot_force_const_mem (MODE, X))
697 /* True if C is a non-empty register class that has too few registers
698 to be safely used as a reload target class. */
699 #define SMALL_REGISTER_CLASS_P(C) \
700 (ira_class_hard_regs_num [(C)] == 1 \
701 || (ira_class_hard_regs_num [(C)] >= 1 \
702 && targetm.class_likely_spilled_p (C)))
704 /* If REG is a reload pseudo, try to make its class satisfying CL. */
705 static void
706 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
708 enum reg_class rclass;
710 /* Do not make more accurate class from reloads generated. They are
711 mostly moves with a lot of constraints. Making more accurate
712 class may results in very narrow class and impossibility of find
713 registers for several reloads of one insn. */
714 if (INSN_UID (curr_insn) >= new_insn_uid_start)
715 return;
716 if (GET_CODE (reg) == SUBREG)
717 reg = SUBREG_REG (reg);
718 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
719 return;
720 if (in_class_p (reg, cl, &rclass) && rclass != cl)
721 lra_change_class (REGNO (reg), rclass, " Change to", true);
724 /* Generate reloads for matching OUT and INS (array of input operand
725 numbers with end marker -1) with reg class GOAL_CLASS. Add input
726 and output reloads correspondingly to the lists *BEFORE and *AFTER.
727 OUT might be negative. In this case we generate input reloads for
728 matched input operands INS. */
729 static void
730 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
731 rtx *before, rtx *after)
733 int i, in;
734 rtx new_in_reg, new_out_reg, reg, clobber;
735 enum machine_mode inmode, outmode;
736 rtx in_rtx = *curr_id->operand_loc[ins[0]];
737 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
739 inmode = curr_operand_mode[ins[0]];
740 outmode = out < 0 ? inmode : curr_operand_mode[out];
741 push_to_sequence (*before);
742 if (inmode != outmode)
744 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
746 reg = new_in_reg
747 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
748 goal_class, "");
749 if (SCALAR_INT_MODE_P (inmode))
750 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
751 else
752 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
753 LRA_SUBREG_P (new_out_reg) = 1;
754 /* If the input reg is dying here, we can use the same hard
755 register for REG and IN_RTX. We do it only for original
756 pseudos as reload pseudos can die although original
757 pseudos still live where reload pseudos dies. */
758 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
759 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
760 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
762 else
764 reg = new_out_reg
765 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
766 goal_class, "");
767 if (SCALAR_INT_MODE_P (outmode))
768 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
769 else
770 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
771 /* NEW_IN_REG is non-paradoxical subreg. We don't want
772 NEW_OUT_REG living above. We add clobber clause for
773 this. This is just a temporary clobber. We can remove
774 it at the end of LRA work. */
775 clobber = emit_clobber (new_out_reg);
776 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
777 LRA_SUBREG_P (new_in_reg) = 1;
778 if (GET_CODE (in_rtx) == SUBREG)
780 rtx subreg_reg = SUBREG_REG (in_rtx);
782 /* If SUBREG_REG is dying here and sub-registers IN_RTX
783 and NEW_IN_REG are similar, we can use the same hard
784 register for REG and SUBREG_REG. */
785 if (REG_P (subreg_reg)
786 && (int) REGNO (subreg_reg) < lra_new_regno_start
787 && GET_MODE (subreg_reg) == outmode
788 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
789 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
790 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
794 else
796 /* Pseudos have values -- see comments for lra_reg_info.
797 Different pseudos with the same value do not conflict even if
798 they live in the same place. When we create a pseudo we
799 assign value of original pseudo (if any) from which we
800 created the new pseudo. If we create the pseudo from the
801 input pseudo, the new pseudo will no conflict with the input
802 pseudo which is wrong when the input pseudo lives after the
803 insn and as the new pseudo value is changed by the insn
804 output. Therefore we create the new pseudo from the output.
806 We cannot reuse the current output register because we might
807 have a situation like "a <- a op b", where the constraints
808 force the second input operand ("b") to match the output
809 operand ("a"). "b" must then be copied into a new register
810 so that it doesn't clobber the current value of "a". */
812 new_in_reg = new_out_reg
813 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
814 goal_class, "");
816 /* In operand can be got from transformations before processing insn
817 constraints. One example of such transformations is subreg
818 reloading (see function simplify_operand_subreg). The new
819 pseudos created by the transformations might have inaccurate
820 class (ALL_REGS) and we should make their classes more
821 accurate. */
822 narrow_reload_pseudo_class (in_rtx, goal_class);
823 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
824 *before = get_insns ();
825 end_sequence ();
826 for (i = 0; (in = ins[i]) >= 0; i++)
828 lra_assert
829 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
830 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
831 *curr_id->operand_loc[in] = new_in_reg;
833 lra_update_dups (curr_id, ins);
834 if (out < 0)
835 return;
836 /* See a comment for the input operand above. */
837 narrow_reload_pseudo_class (out_rtx, goal_class);
838 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
840 start_sequence ();
841 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
842 emit_insn (*after);
843 *after = get_insns ();
844 end_sequence ();
846 *curr_id->operand_loc[out] = new_out_reg;
847 lra_update_dup (curr_id, out);
850 /* Return register class which is union of all reg classes in insn
851 constraint alternative string starting with P. */
852 static enum reg_class
853 reg_class_from_constraints (const char *p)
855 int c, len;
856 enum reg_class op_class = NO_REGS;
859 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
861 case '#':
862 case ',':
863 return op_class;
865 case 'p':
866 op_class = (reg_class_subunion
867 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
868 ADDRESS, SCRATCH)]);
869 break;
871 case 'g':
872 case 'r':
873 op_class = reg_class_subunion[op_class][GENERAL_REGS];
874 break;
876 default:
877 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
879 #ifdef EXTRA_CONSTRAINT_STR
880 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
881 op_class
882 = (reg_class_subunion
883 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
884 ADDRESS, SCRATCH)]);
885 #endif
886 break;
889 op_class
890 = reg_class_subunion[op_class][REG_CLASS_FROM_CONSTRAINT (c, p)];
891 break;
893 while ((p += len), c);
894 return op_class;
897 /* If OP is a register, return the class of the register as per
898 get_reg_class, otherwise return NO_REGS. */
899 static inline enum reg_class
900 get_op_class (rtx op)
902 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
905 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
906 otherwise. If modes of MEM_PSEUDO and VAL are different, use
907 SUBREG for VAL to make them equal. */
908 static rtx
909 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
911 if (GET_MODE (mem_pseudo) != GET_MODE (val))
913 /* Usually size of mem_pseudo is greater than val size but in
914 rare cases it can be less as it can be defined by target
915 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
916 if (! MEM_P (val))
918 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
919 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
921 LRA_SUBREG_P (val) = 1;
923 else
925 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
926 LRA_SUBREG_P (mem_pseudo) = 1;
929 return (to_p
930 ? gen_move_insn (mem_pseudo, val)
931 : gen_move_insn (val, mem_pseudo));
934 /* Process a special case insn (register move), return true if we
935 don't need to process it anymore. INSN should be a single set
936 insn. Set up that RTL was changed through CHANGE_P and macro
937 SECONDARY_MEMORY_NEEDED says to use secondary memory through
938 SEC_MEM_P. */
939 static bool
940 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
942 int sregno, dregno;
943 rtx dest, src, dreg, sreg, old_sreg, new_reg, before, scratch_reg;
944 enum reg_class dclass, sclass, secondary_class;
945 enum machine_mode sreg_mode;
946 secondary_reload_info sri;
948 lra_assert (curr_insn_set != NULL_RTX);
949 dreg = dest = SET_DEST (curr_insn_set);
950 sreg = src = SET_SRC (curr_insn_set);
951 if (GET_CODE (dest) == SUBREG)
952 dreg = SUBREG_REG (dest);
953 if (GET_CODE (src) == SUBREG)
954 sreg = SUBREG_REG (src);
955 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
956 return false;
957 sclass = dclass = NO_REGS;
958 if (REG_P (dreg))
959 dclass = get_reg_class (REGNO (dreg));
960 if (dclass == ALL_REGS)
961 /* ALL_REGS is used for new pseudos created by transformations
962 like reload of SUBREG_REG (see function
963 simplify_operand_subreg). We don't know their class yet. We
964 should figure out the class from processing the insn
965 constraints not in this fast path function. Even if ALL_REGS
966 were a right class for the pseudo, secondary_... hooks usually
967 are not define for ALL_REGS. */
968 return false;
969 sreg_mode = GET_MODE (sreg);
970 old_sreg = sreg;
971 if (REG_P (sreg))
972 sclass = get_reg_class (REGNO (sreg));
973 if (sclass == ALL_REGS)
974 /* See comments above. */
975 return false;
976 if (sclass == NO_REGS && dclass == NO_REGS)
977 return false;
978 #ifdef SECONDARY_MEMORY_NEEDED
979 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
980 #ifdef SECONDARY_MEMORY_NEEDED_MODE
981 && ((sclass != NO_REGS && dclass != NO_REGS)
982 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
983 #endif
986 *sec_mem_p = true;
987 return false;
989 #endif
990 if (! REG_P (dreg) || ! REG_P (sreg))
991 return false;
992 sri.prev_sri = NULL;
993 sri.icode = CODE_FOR_nothing;
994 sri.extra_cost = 0;
995 secondary_class = NO_REGS;
996 /* Set up hard register for a reload pseudo for hook
997 secondary_reload because some targets just ignore unassigned
998 pseudos in the hook. */
999 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1001 dregno = REGNO (dreg);
1002 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1004 else
1005 dregno = -1;
1006 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1008 sregno = REGNO (sreg);
1009 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1011 else
1012 sregno = -1;
1013 if (sclass != NO_REGS)
1014 secondary_class
1015 = (enum reg_class) targetm.secondary_reload (false, dest,
1016 (reg_class_t) sclass,
1017 GET_MODE (src), &sri);
1018 if (sclass == NO_REGS
1019 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1020 && dclass != NO_REGS))
1022 enum reg_class old_sclass = secondary_class;
1023 secondary_reload_info old_sri = sri;
1025 sri.prev_sri = NULL;
1026 sri.icode = CODE_FOR_nothing;
1027 sri.extra_cost = 0;
1028 secondary_class
1029 = (enum reg_class) targetm.secondary_reload (true, sreg,
1030 (reg_class_t) dclass,
1031 sreg_mode, &sri);
1032 /* Check the target hook consistency. */
1033 lra_assert
1034 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1035 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1036 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1038 if (sregno >= 0)
1039 reg_renumber [sregno] = -1;
1040 if (dregno >= 0)
1041 reg_renumber [dregno] = -1;
1042 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1043 return false;
1044 *change_p = true;
1045 new_reg = NULL_RTX;
1046 if (secondary_class != NO_REGS)
1047 new_reg = lra_create_new_reg_with_unique_value (sreg_mode, NULL_RTX,
1048 secondary_class,
1049 "secondary");
1050 start_sequence ();
1051 if (old_sreg != sreg)
1052 sreg = copy_rtx (sreg);
1053 if (sri.icode == CODE_FOR_nothing)
1054 lra_emit_move (new_reg, sreg);
1055 else
1057 enum reg_class scratch_class;
1059 scratch_class = (reg_class_from_constraints
1060 (insn_data[sri.icode].operand[2].constraint));
1061 scratch_reg = (lra_create_new_reg_with_unique_value
1062 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1063 scratch_class, "scratch"));
1064 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1065 sreg, scratch_reg));
1067 before = get_insns ();
1068 end_sequence ();
1069 lra_process_new_insns (curr_insn, before, NULL_RTX, "Inserting the move");
1070 if (new_reg != NULL_RTX)
1072 if (GET_CODE (src) == SUBREG)
1073 SUBREG_REG (src) = new_reg;
1074 else
1075 SET_SRC (curr_insn_set) = new_reg;
1077 else
1079 if (lra_dump_file != NULL)
1081 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1082 dump_insn_slim (lra_dump_file, curr_insn);
1084 lra_set_insn_deleted (curr_insn);
1085 return true;
1087 return false;
1090 /* The following data describe the result of process_alt_operands.
1091 The data are used in curr_insn_transform to generate reloads. */
1093 /* The chosen reg classes which should be used for the corresponding
1094 operands. */
1095 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1096 /* True if the operand should be the same as another operand and that
1097 other operand does not need a reload. */
1098 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1099 /* True if the operand does not need a reload. */
1100 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1101 /* True if the operand can be offsetable memory. */
1102 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1103 /* The number of an operand to which given operand can be matched to. */
1104 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1105 /* The number of elements in the following array. */
1106 static int goal_alt_dont_inherit_ops_num;
1107 /* Numbers of operands whose reload pseudos should not be inherited. */
1108 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1109 /* True if the insn commutative operands should be swapped. */
1110 static bool goal_alt_swapped;
1111 /* The chosen insn alternative. */
1112 static int goal_alt_number;
1114 /* The following five variables are used to choose the best insn
1115 alternative. They reflect final characteristics of the best
1116 alternative. */
1118 /* Number of necessary reloads and overall cost reflecting the
1119 previous value and other unpleasantness of the best alternative. */
1120 static int best_losers, best_overall;
1121 /* Overall number hard registers used for reloads. For example, on
1122 some targets we need 2 general registers to reload DFmode and only
1123 one floating point register. */
1124 static int best_reload_nregs;
1125 /* Overall number reflecting distances of previous reloading the same
1126 value. The distances are counted from the current BB start. It is
1127 used to improve inheritance chances. */
1128 static int best_reload_sum;
1130 /* True if the current insn should have no correspondingly input or
1131 output reloads. */
1132 static bool no_input_reloads_p, no_output_reloads_p;
1134 /* True if we swapped the commutative operands in the current
1135 insn. */
1136 static int curr_swapped;
1138 /* Arrange for address element *LOC to be a register of class CL.
1139 Add any input reloads to list BEFORE. AFTER is nonnull if *LOC is an
1140 automodified value; handle that case by adding the required output
1141 reloads to list AFTER. Return true if the RTL was changed. */
1142 static bool
1143 process_addr_reg (rtx *loc, rtx *before, rtx *after, enum reg_class cl)
1145 int regno;
1146 enum reg_class rclass, new_class;
1147 rtx reg;
1148 rtx new_reg;
1149 enum machine_mode mode;
1150 bool subreg_p, before_p = false;
1152 subreg_p = GET_CODE (*loc) == SUBREG;
1153 if (subreg_p)
1154 loc = &SUBREG_REG (*loc);
1155 reg = *loc;
1156 mode = GET_MODE (reg);
1157 if (! REG_P (reg))
1159 /* Always reload memory in an address even if the target supports
1160 such addresses. */
1161 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1162 before_p = true;
1164 else
1166 regno = REGNO (reg);
1167 rclass = get_reg_class (regno);
1168 if ((*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1170 if (lra_dump_file != NULL)
1172 fprintf (lra_dump_file,
1173 "Changing pseudo %d in address of insn %u on equiv ",
1174 REGNO (reg), INSN_UID (curr_insn));
1175 dump_value_slim (lra_dump_file, *loc, 1);
1176 fprintf (lra_dump_file, "\n");
1178 *loc = copy_rtx (*loc);
1180 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1182 reg = *loc;
1183 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1184 mode, reg, cl, subreg_p, "address", &new_reg))
1185 before_p = true;
1187 else if (new_class != NO_REGS && rclass != new_class)
1189 lra_change_class (regno, new_class, " Change to", true);
1190 return false;
1192 else
1193 return false;
1195 if (before_p)
1197 push_to_sequence (*before);
1198 lra_emit_move (new_reg, reg);
1199 *before = get_insns ();
1200 end_sequence ();
1202 *loc = new_reg;
1203 if (after != NULL)
1205 start_sequence ();
1206 lra_emit_move (reg, new_reg);
1207 emit_insn (*after);
1208 *after = get_insns ();
1209 end_sequence ();
1211 return true;
1214 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1215 the insn to be inserted before curr insn. AFTER returns the
1216 the insn to be inserted after curr insn. ORIGREG and NEWREG
1217 are the original reg and new reg for reload. */
1218 static void
1219 insert_move_for_subreg (rtx *before, rtx *after, rtx origreg, rtx newreg)
1221 if (before)
1223 push_to_sequence (*before);
1224 lra_emit_move (newreg, origreg);
1225 *before = get_insns ();
1226 end_sequence ();
1228 if (after)
1230 start_sequence ();
1231 lra_emit_move (origreg, newreg);
1232 emit_insn (*after);
1233 *after = get_insns ();
1234 end_sequence ();
1238 static int valid_address_p (enum machine_mode mode, rtx addr, addr_space_t as);
1240 /* Make reloads for subreg in operand NOP with internal subreg mode
1241 REG_MODE, add new reloads for further processing. Return true if
1242 any change was done. */
1243 static bool
1244 simplify_operand_subreg (int nop, enum machine_mode reg_mode)
1246 int hard_regno;
1247 rtx before, after;
1248 enum machine_mode mode, innermode;
1249 rtx reg, new_reg;
1250 rtx operand = *curr_id->operand_loc[nop];
1251 enum reg_class regclass;
1252 enum op_type type;
1254 before = after = NULL_RTX;
1256 if (GET_CODE (operand) != SUBREG)
1257 return false;
1259 mode = GET_MODE (operand);
1260 reg = SUBREG_REG (operand);
1261 innermode = GET_MODE (reg);
1262 type = curr_static_id->operand[nop].type;
1263 /* If we change address for paradoxical subreg of memory, the
1264 address might violate the necessary alignment or the access might
1265 be slow. So take this into consideration. We should not worry
1266 about access beyond allocated memory for paradoxical memory
1267 subregs as we don't substitute such equiv memory (see processing
1268 equivalences in function lra_constraints) and because for spilled
1269 pseudos we allocate stack memory enough for the biggest
1270 corresponding paradoxical subreg. */
1271 if (MEM_P (reg)
1272 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1273 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1275 rtx subst, old = *curr_id->operand_loc[nop];
1277 alter_subreg (curr_id->operand_loc[nop], false);
1278 subst = *curr_id->operand_loc[nop];
1279 lra_assert (MEM_P (subst));
1280 if (! valid_address_p (innermode, XEXP (reg, 0),
1281 MEM_ADDR_SPACE (reg))
1282 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1283 MEM_ADDR_SPACE (subst)))
1284 return true;
1285 /* If the address was valid and became invalid, prefer to reload
1286 the memory. Typical case is when the index scale should
1287 correspond the memory. */
1288 *curr_id->operand_loc[nop] = old;
1290 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1292 alter_subreg (curr_id->operand_loc[nop], false);
1293 return true;
1295 else if (CONSTANT_P (reg))
1297 /* Try to simplify subreg of constant. It is usually result of
1298 equivalence substitution. */
1299 if (innermode == VOIDmode
1300 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1301 innermode = curr_static_id->operand[nop].mode;
1302 if ((new_reg = simplify_subreg (mode, reg, innermode,
1303 SUBREG_BYTE (operand))) != NULL_RTX)
1305 *curr_id->operand_loc[nop] = new_reg;
1306 return true;
1309 /* Put constant into memory when we have mixed modes. It generates
1310 a better code in most cases as it does not need a secondary
1311 reload memory. It also prevents LRA looping when LRA is using
1312 secondary reload memory again and again. */
1313 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1314 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1316 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1317 alter_subreg (curr_id->operand_loc[nop], false);
1318 return true;
1320 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1321 if there may be a problem accessing OPERAND in the outer
1322 mode. */
1323 if ((REG_P (reg)
1324 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1325 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1326 /* Don't reload paradoxical subregs because we could be looping
1327 having repeatedly final regno out of hard regs range. */
1328 && (hard_regno_nregs[hard_regno][innermode]
1329 >= hard_regno_nregs[hard_regno][mode])
1330 && simplify_subreg_regno (hard_regno, innermode,
1331 SUBREG_BYTE (operand), mode) < 0
1332 /* Don't reload subreg for matching reload. It is actually
1333 valid subreg in LRA. */
1334 && ! LRA_SUBREG_P (operand))
1335 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1337 enum reg_class rclass;
1339 if (REG_P (reg))
1340 /* There is a big probability that we will get the same class
1341 for the new pseudo and we will get the same insn which
1342 means infinite looping. So spill the new pseudo. */
1343 rclass = NO_REGS;
1344 else
1345 /* The class will be defined later in curr_insn_transform. */
1346 rclass
1347 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1349 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1350 rclass, TRUE, "subreg reg", &new_reg))
1352 bool insert_before, insert_after;
1353 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1355 insert_before = (type != OP_OUT
1356 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1357 insert_after = (type != OP_IN);
1358 insert_move_for_subreg (insert_before ? &before : NULL,
1359 insert_after ? &after : NULL,
1360 reg, new_reg);
1362 SUBREG_REG (operand) = new_reg;
1363 lra_process_new_insns (curr_insn, before, after,
1364 "Inserting subreg reload");
1365 return true;
1367 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1368 IRA allocates hardreg to the inner pseudo reg according to its mode
1369 instead of the outermode, so the size of the hardreg may not be enough
1370 to contain the outermode operand, in that case we may need to insert
1371 reload for the reg. For the following two types of paradoxical subreg,
1372 we need to insert reload:
1373 1. If the op_type is OP_IN, and the hardreg could not be paired with
1374 other hardreg to contain the outermode operand
1375 (checked by in_hard_reg_set_p), we need to insert the reload.
1376 2. If the op_type is OP_OUT or OP_INOUT.
1378 Here is a paradoxical subreg example showing how the reload is generated:
1380 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1381 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1383 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1384 here, if reg107 is assigned to hardreg R15, because R15 is the last
1385 hardreg, compiler cannot find another hardreg to pair with R15 to
1386 contain TImode data. So we insert a TImode reload reg180 for it.
1387 After reload is inserted:
1389 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1390 (reg:DI 107 [ __comp ])) -1
1391 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1392 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1394 Two reload hard registers will be allocated to reg180 to save TImode data
1395 in LRA_assign. */
1396 else if (REG_P (reg)
1397 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1398 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1399 && (hard_regno_nregs[hard_regno][innermode]
1400 < hard_regno_nregs[hard_regno][mode])
1401 && (regclass = lra_get_allocno_class (REGNO (reg)))
1402 && (type != OP_IN
1403 || !in_hard_reg_set_p (reg_class_contents[regclass],
1404 mode, hard_regno)))
1406 /* The class will be defined later in curr_insn_transform. */
1407 enum reg_class rclass
1408 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1410 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1411 rclass, TRUE, "paradoxical subreg", &new_reg))
1413 rtx subreg;
1414 bool insert_before, insert_after;
1416 PUT_MODE (new_reg, mode);
1417 subreg = simplify_gen_subreg (innermode, new_reg, mode, 0);
1418 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1420 insert_before = (type != OP_OUT);
1421 insert_after = (type != OP_IN);
1422 insert_move_for_subreg (insert_before ? &before : NULL,
1423 insert_after ? &after : NULL,
1424 reg, subreg);
1426 SUBREG_REG (operand) = new_reg;
1427 lra_process_new_insns (curr_insn, before, after,
1428 "Inserting paradoxical subreg reload");
1429 return true;
1431 return false;
1434 /* Return TRUE if X refers for a hard register from SET. */
1435 static bool
1436 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1438 int i, j, x_hard_regno;
1439 enum machine_mode mode;
1440 const char *fmt;
1441 enum rtx_code code;
1443 if (x == NULL_RTX)
1444 return false;
1445 code = GET_CODE (x);
1446 mode = GET_MODE (x);
1447 if (code == SUBREG)
1449 x = SUBREG_REG (x);
1450 code = GET_CODE (x);
1451 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1452 mode = GET_MODE (x);
1455 if (REG_P (x))
1457 x_hard_regno = get_hard_regno (x);
1458 return (x_hard_regno >= 0
1459 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1461 if (MEM_P (x))
1463 struct address_info ad;
1465 decompose_mem_address (&ad, x);
1466 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1467 return true;
1468 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1469 return true;
1471 fmt = GET_RTX_FORMAT (code);
1472 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1474 if (fmt[i] == 'e')
1476 if (uses_hard_regs_p (XEXP (x, i), set))
1477 return true;
1479 else if (fmt[i] == 'E')
1481 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1482 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1483 return true;
1486 return false;
1489 /* Return true if OP is a spilled pseudo. */
1490 static inline bool
1491 spilled_pseudo_p (rtx op)
1493 return (REG_P (op)
1494 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1497 /* Return true if X is a general constant. */
1498 static inline bool
1499 general_constant_p (rtx x)
1501 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1504 static bool
1505 reg_in_class_p (rtx reg, enum reg_class cl)
1507 if (cl == NO_REGS)
1508 return get_reg_class (REGNO (reg)) == NO_REGS;
1509 return in_class_p (reg, cl, NULL);
1512 /* Major function to choose the current insn alternative and what
1513 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1514 negative we should consider only this alternative. Return false if
1515 we can not choose the alternative or find how to reload the
1516 operands. */
1517 static bool
1518 process_alt_operands (int only_alternative)
1520 bool ok_p = false;
1521 int nop, overall, nalt;
1522 int n_alternatives = curr_static_id->n_alternatives;
1523 int n_operands = curr_static_id->n_operands;
1524 /* LOSERS counts the operands that don't fit this alternative and
1525 would require loading. */
1526 int losers;
1527 /* REJECT is a count of how undesirable this alternative says it is
1528 if any reloading is required. If the alternative matches exactly
1529 then REJECT is ignored, but otherwise it gets this much counted
1530 against it in addition to the reloading needed. */
1531 int reject;
1532 /* The number of elements in the following array. */
1533 int early_clobbered_regs_num;
1534 /* Numbers of operands which are early clobber registers. */
1535 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1536 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1537 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1538 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1539 bool curr_alt_win[MAX_RECOG_OPERANDS];
1540 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1541 int curr_alt_matches[MAX_RECOG_OPERANDS];
1542 /* The number of elements in the following array. */
1543 int curr_alt_dont_inherit_ops_num;
1544 /* Numbers of operands whose reload pseudos should not be inherited. */
1545 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1546 rtx op;
1547 /* The register when the operand is a subreg of register, otherwise the
1548 operand itself. */
1549 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1550 /* The register if the operand is a register or subreg of register,
1551 otherwise NULL. */
1552 rtx operand_reg[MAX_RECOG_OPERANDS];
1553 int hard_regno[MAX_RECOG_OPERANDS];
1554 enum machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1555 int reload_nregs, reload_sum;
1556 bool costly_p;
1557 enum reg_class cl;
1559 /* Calculate some data common for all alternatives to speed up the
1560 function. */
1561 for (nop = 0; nop < n_operands; nop++)
1563 rtx reg;
1565 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1566 /* The real hard regno of the operand after the allocation. */
1567 hard_regno[nop] = get_hard_regno (op);
1569 operand_reg[nop] = reg = op;
1570 biggest_mode[nop] = GET_MODE (op);
1571 if (GET_CODE (op) == SUBREG)
1573 operand_reg[nop] = reg = SUBREG_REG (op);
1574 if (GET_MODE_SIZE (biggest_mode[nop])
1575 < GET_MODE_SIZE (GET_MODE (reg)))
1576 biggest_mode[nop] = GET_MODE (reg);
1578 if (! REG_P (reg))
1579 operand_reg[nop] = NULL_RTX;
1580 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1581 || ((int) REGNO (reg)
1582 == lra_get_elimination_hard_regno (REGNO (reg))))
1583 no_subreg_reg_operand[nop] = reg;
1584 else
1585 operand_reg[nop] = no_subreg_reg_operand[nop]
1586 /* Just use natural mode for elimination result. It should
1587 be enough for extra constraints hooks. */
1588 = regno_reg_rtx[hard_regno[nop]];
1591 /* The constraints are made of several alternatives. Each operand's
1592 constraint looks like foo,bar,... with commas separating the
1593 alternatives. The first alternatives for all operands go
1594 together, the second alternatives go together, etc.
1596 First loop over alternatives. */
1597 for (nalt = 0; nalt < n_alternatives; nalt++)
1599 /* Loop over operands for one constraint alternative. */
1600 #if HAVE_ATTR_enabled
1601 if (curr_id->alternative_enabled_p != NULL
1602 && ! curr_id->alternative_enabled_p[nalt])
1603 continue;
1604 #endif
1606 if (only_alternative >= 0 && nalt != only_alternative)
1607 continue;
1610 overall = losers = reject = reload_nregs = reload_sum = 0;
1611 for (nop = 0; nop < n_operands; nop++)
1613 int inc = (curr_static_id
1614 ->operand_alternative[nalt * n_operands + nop].reject);
1615 if (lra_dump_file != NULL && inc != 0)
1616 fprintf (lra_dump_file,
1617 " Staticly defined alt reject+=%d\n", inc);
1618 reject += inc;
1620 early_clobbered_regs_num = 0;
1622 for (nop = 0; nop < n_operands; nop++)
1624 const char *p;
1625 char *end;
1626 int len, c, m, i, opalt_num, this_alternative_matches;
1627 bool win, did_match, offmemok, early_clobber_p;
1628 /* false => this operand can be reloaded somehow for this
1629 alternative. */
1630 bool badop;
1631 /* true => this operand can be reloaded if the alternative
1632 allows regs. */
1633 bool winreg;
1634 /* True if a constant forced into memory would be OK for
1635 this operand. */
1636 bool constmemok;
1637 enum reg_class this_alternative, this_costly_alternative;
1638 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1639 bool this_alternative_match_win, this_alternative_win;
1640 bool this_alternative_offmemok;
1641 bool scratch_p;
1642 enum machine_mode mode;
1644 opalt_num = nalt * n_operands + nop;
1645 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1647 /* Fast track for no constraints at all. */
1648 curr_alt[nop] = NO_REGS;
1649 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1650 curr_alt_win[nop] = true;
1651 curr_alt_match_win[nop] = false;
1652 curr_alt_offmemok[nop] = false;
1653 curr_alt_matches[nop] = -1;
1654 continue;
1657 op = no_subreg_reg_operand[nop];
1658 mode = curr_operand_mode[nop];
1660 win = did_match = winreg = offmemok = constmemok = false;
1661 badop = true;
1663 early_clobber_p = false;
1664 p = curr_static_id->operand_alternative[opalt_num].constraint;
1666 this_costly_alternative = this_alternative = NO_REGS;
1667 /* We update set of possible hard regs besides its class
1668 because reg class might be inaccurate. For example,
1669 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1670 is translated in HI_REGS because classes are merged by
1671 pairs and there is no accurate intermediate class. */
1672 CLEAR_HARD_REG_SET (this_alternative_set);
1673 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1674 this_alternative_win = false;
1675 this_alternative_match_win = false;
1676 this_alternative_offmemok = false;
1677 this_alternative_matches = -1;
1679 /* An empty constraint should be excluded by the fast
1680 track. */
1681 lra_assert (*p != 0 && *p != ',');
1683 /* Scan this alternative's specs for this operand; set WIN
1684 if the operand fits any letter in this alternative.
1685 Otherwise, clear BADOP if this operand could fit some
1686 letter after reloads, or set WINREG if this operand could
1687 fit after reloads provided the constraint allows some
1688 registers. */
1689 costly_p = false;
1692 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1694 case '\0':
1695 len = 0;
1696 break;
1697 case ',':
1698 c = '\0';
1699 break;
1701 case '=': case '+': case '?': case '*': case '!':
1702 case ' ': case '\t':
1703 break;
1705 case '%':
1706 /* We only support one commutative marker, the first
1707 one. We already set commutative above. */
1708 break;
1710 case '&':
1711 early_clobber_p = true;
1712 break;
1714 case '#':
1715 /* Ignore rest of this alternative. */
1716 c = '\0';
1717 break;
1719 case '0': case '1': case '2': case '3': case '4':
1720 case '5': case '6': case '7': case '8': case '9':
1722 int m_hregno;
1723 bool match_p;
1725 m = strtoul (p, &end, 10);
1726 p = end;
1727 len = 0;
1728 lra_assert (nop > m);
1730 this_alternative_matches = m;
1731 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1732 /* We are supposed to match a previous operand.
1733 If we do, we win if that one did. If we do
1734 not, count both of the operands as losers.
1735 (This is too conservative, since most of the
1736 time only a single reload insn will be needed
1737 to make the two operands win. As a result,
1738 this alternative may be rejected when it is
1739 actually desirable.) */
1740 match_p = false;
1741 if (operands_match_p (*curr_id->operand_loc[nop],
1742 *curr_id->operand_loc[m], m_hregno))
1744 /* We should reject matching of an early
1745 clobber operand if the matching operand is
1746 not dying in the insn. */
1747 if (! curr_static_id->operand[m].early_clobber
1748 || operand_reg[nop] == NULL_RTX
1749 || (find_regno_note (curr_insn, REG_DEAD,
1750 REGNO (op))
1751 || REGNO (op) == REGNO (operand_reg[m])))
1752 match_p = true;
1754 if (match_p)
1756 /* If we are matching a non-offsettable
1757 address where an offsettable address was
1758 expected, then we must reject this
1759 combination, because we can't reload
1760 it. */
1761 if (curr_alt_offmemok[m]
1762 && MEM_P (*curr_id->operand_loc[m])
1763 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1764 continue;
1766 else
1768 /* Operands don't match. Both operands must
1769 allow a reload register, otherwise we
1770 cannot make them match. */
1771 if (curr_alt[m] == NO_REGS)
1772 break;
1773 /* Retroactively mark the operand we had to
1774 match as a loser, if it wasn't already and
1775 it wasn't matched to a register constraint
1776 (e.g it might be matched by memory). */
1777 if (curr_alt_win[m]
1778 && (operand_reg[m] == NULL_RTX
1779 || hard_regno[m] < 0))
1781 losers++;
1782 reload_nregs
1783 += (ira_reg_class_max_nregs[curr_alt[m]]
1784 [GET_MODE (*curr_id->operand_loc[m])]);
1787 /* Prefer matching earlyclobber alternative as
1788 it results in less hard regs required for
1789 the insn than a non-matching earlyclobber
1790 alternative. */
1791 if (curr_static_id->operand[m].early_clobber)
1793 if (lra_dump_file != NULL)
1794 fprintf
1795 (lra_dump_file,
1796 " %d Matching earlyclobber alt:"
1797 " reject--\n",
1798 nop);
1799 reject--;
1801 /* Otherwise we prefer no matching
1802 alternatives because it gives more freedom
1803 in RA. */
1804 else if (operand_reg[nop] == NULL_RTX
1805 || (find_regno_note (curr_insn, REG_DEAD,
1806 REGNO (operand_reg[nop]))
1807 == NULL_RTX))
1809 if (lra_dump_file != NULL)
1810 fprintf
1811 (lra_dump_file,
1812 " %d Matching alt: reject+=2\n",
1813 nop);
1814 reject += 2;
1817 /* If we have to reload this operand and some
1818 previous operand also had to match the same
1819 thing as this operand, we don't know how to do
1820 that. */
1821 if (!match_p || !curr_alt_win[m])
1823 for (i = 0; i < nop; i++)
1824 if (curr_alt_matches[i] == m)
1825 break;
1826 if (i < nop)
1827 break;
1829 else
1830 did_match = true;
1832 /* This can be fixed with reloads if the operand
1833 we are supposed to match can be fixed with
1834 reloads. */
1835 badop = false;
1836 this_alternative = curr_alt[m];
1837 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1838 winreg = this_alternative != NO_REGS;
1839 break;
1842 case 'p':
1843 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1844 ADDRESS, SCRATCH);
1845 this_alternative = reg_class_subunion[this_alternative][cl];
1846 IOR_HARD_REG_SET (this_alternative_set,
1847 reg_class_contents[cl]);
1848 if (costly_p)
1850 this_costly_alternative
1851 = reg_class_subunion[this_costly_alternative][cl];
1852 IOR_HARD_REG_SET (this_costly_alternative_set,
1853 reg_class_contents[cl]);
1855 win = true;
1856 badop = false;
1857 break;
1859 case TARGET_MEM_CONSTRAINT:
1860 if (MEM_P (op) || spilled_pseudo_p (op))
1861 win = true;
1862 /* We can put constant or pseudo value into memory
1863 to satisfy the constraint. */
1864 if (CONST_POOL_OK_P (mode, op) || REG_P (op))
1865 badop = false;
1866 constmemok = true;
1867 break;
1869 case '<':
1870 if (MEM_P (op)
1871 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
1872 || GET_CODE (XEXP (op, 0)) == POST_DEC))
1873 win = true;
1874 break;
1876 case '>':
1877 if (MEM_P (op)
1878 && (GET_CODE (XEXP (op, 0)) == PRE_INC
1879 || GET_CODE (XEXP (op, 0)) == POST_INC))
1880 win = true;
1881 break;
1883 /* Memory op whose address is not offsettable. */
1884 case 'V':
1885 if (MEM_P (op)
1886 && ! offsettable_nonstrict_memref_p (op))
1887 win = true;
1888 break;
1890 /* Memory operand whose address is offsettable. */
1891 case 'o':
1892 if ((MEM_P (op)
1893 && offsettable_nonstrict_memref_p (op))
1894 || spilled_pseudo_p (op))
1895 win = true;
1896 /* We can put constant or pseudo value into memory
1897 or make memory address offsetable to satisfy the
1898 constraint. */
1899 if (CONST_POOL_OK_P (mode, op) || MEM_P (op) || REG_P (op))
1900 badop = false;
1901 constmemok = true;
1902 offmemok = true;
1903 break;
1905 case 'E':
1906 case 'F':
1907 if (GET_CODE (op) == CONST_DOUBLE
1908 || (GET_CODE (op) == CONST_VECTOR
1909 && (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)))
1910 win = true;
1911 break;
1913 case 'G':
1914 case 'H':
1915 if (CONST_DOUBLE_AS_FLOAT_P (op)
1916 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
1917 win = true;
1918 break;
1920 case 's':
1921 if (CONST_SCALAR_INT_P (op))
1922 break;
1924 case 'i':
1925 if (general_constant_p (op))
1926 win = true;
1927 break;
1929 case 'n':
1930 if (CONST_SCALAR_INT_P (op))
1931 win = true;
1932 break;
1934 case 'I':
1935 case 'J':
1936 case 'K':
1937 case 'L':
1938 case 'M':
1939 case 'N':
1940 case 'O':
1941 case 'P':
1942 if (CONST_INT_P (op)
1943 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
1944 win = true;
1945 break;
1947 case 'X':
1948 /* This constraint should be excluded by the fast
1949 track. */
1950 gcc_unreachable ();
1951 break;
1953 case 'g':
1954 if (MEM_P (op)
1955 || general_constant_p (op)
1956 || spilled_pseudo_p (op))
1957 win = true;
1958 /* Drop through into 'r' case. */
1960 case 'r':
1961 this_alternative
1962 = reg_class_subunion[this_alternative][GENERAL_REGS];
1963 IOR_HARD_REG_SET (this_alternative_set,
1964 reg_class_contents[GENERAL_REGS]);
1965 if (costly_p)
1967 this_costly_alternative
1968 = (reg_class_subunion
1969 [this_costly_alternative][GENERAL_REGS]);
1970 IOR_HARD_REG_SET (this_costly_alternative_set,
1971 reg_class_contents[GENERAL_REGS]);
1973 goto reg;
1975 default:
1976 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
1978 #ifdef EXTRA_CONSTRAINT_STR
1979 if (EXTRA_MEMORY_CONSTRAINT (c, p))
1981 if (EXTRA_CONSTRAINT_STR (op, c, p))
1982 win = true;
1983 else if (spilled_pseudo_p (op))
1984 win = true;
1986 /* If we didn't already win, we can reload
1987 constants via force_const_mem or put the
1988 pseudo value into memory, or make other
1989 memory by reloading the address like for
1990 'o'. */
1991 if (CONST_POOL_OK_P (mode, op)
1992 || MEM_P (op) || REG_P (op))
1993 badop = false;
1994 constmemok = true;
1995 offmemok = true;
1996 break;
1998 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
2000 if (EXTRA_CONSTRAINT_STR (op, c, p))
2001 win = true;
2003 /* If we didn't already win, we can reload
2004 the address into a base register. */
2005 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2006 ADDRESS, SCRATCH);
2007 this_alternative
2008 = reg_class_subunion[this_alternative][cl];
2009 IOR_HARD_REG_SET (this_alternative_set,
2010 reg_class_contents[cl]);
2011 if (costly_p)
2013 this_costly_alternative
2014 = (reg_class_subunion
2015 [this_costly_alternative][cl]);
2016 IOR_HARD_REG_SET (this_costly_alternative_set,
2017 reg_class_contents[cl]);
2019 badop = false;
2020 break;
2023 if (EXTRA_CONSTRAINT_STR (op, c, p))
2024 win = true;
2025 #endif
2026 break;
2029 cl = REG_CLASS_FROM_CONSTRAINT (c, p);
2030 this_alternative = reg_class_subunion[this_alternative][cl];
2031 IOR_HARD_REG_SET (this_alternative_set,
2032 reg_class_contents[cl]);
2033 if (costly_p)
2035 this_costly_alternative
2036 = reg_class_subunion[this_costly_alternative][cl];
2037 IOR_HARD_REG_SET (this_costly_alternative_set,
2038 reg_class_contents[cl]);
2040 reg:
2041 if (mode == BLKmode)
2042 break;
2043 winreg = true;
2044 if (REG_P (op))
2046 if (hard_regno[nop] >= 0
2047 && in_hard_reg_set_p (this_alternative_set,
2048 mode, hard_regno[nop]))
2049 win = true;
2050 else if (hard_regno[nop] < 0
2051 && in_class_p (op, this_alternative, NULL))
2052 win = true;
2054 break;
2056 if (c != ' ' && c != '\t')
2057 costly_p = c == '*';
2059 while ((p += len), c);
2061 scratch_p = (operand_reg[nop] != NULL_RTX
2062 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2063 /* Record which operands fit this alternative. */
2064 if (win)
2066 this_alternative_win = true;
2067 if (operand_reg[nop] != NULL_RTX)
2069 if (hard_regno[nop] >= 0)
2071 if (in_hard_reg_set_p (this_costly_alternative_set,
2072 mode, hard_regno[nop]))
2074 if (lra_dump_file != NULL)
2075 fprintf (lra_dump_file,
2076 " %d Costly set: reject++\n",
2077 nop);
2078 reject++;
2081 else
2083 /* Prefer won reg to spilled pseudo under other
2084 equal conditions for possibe inheritance. */
2085 if (! scratch_p)
2087 if (lra_dump_file != NULL)
2088 fprintf
2089 (lra_dump_file,
2090 " %d Non pseudo reload: reject++\n",
2091 nop);
2092 reject++;
2094 if (in_class_p (operand_reg[nop],
2095 this_costly_alternative, NULL))
2097 if (lra_dump_file != NULL)
2098 fprintf
2099 (lra_dump_file,
2100 " %d Non pseudo costly reload:"
2101 " reject++\n",
2102 nop);
2103 reject++;
2106 /* We simulate the behaviour of old reload here.
2107 Although scratches need hard registers and it
2108 might result in spilling other pseudos, no reload
2109 insns are generated for the scratches. So it
2110 might cost something but probably less than old
2111 reload pass believes. */
2112 if (scratch_p)
2114 if (lra_dump_file != NULL)
2115 fprintf (lra_dump_file,
2116 " %d Scratch win: reject+=2\n",
2117 nop);
2118 reject += 2;
2122 else if (did_match)
2123 this_alternative_match_win = true;
2124 else
2126 int const_to_mem = 0;
2127 bool no_regs_p;
2129 /* Never do output reload of stack pointer. It makes
2130 impossible to do elimination when SP is changed in
2131 RTL. */
2132 if (op == stack_pointer_rtx && ! frame_pointer_needed
2133 && curr_static_id->operand[nop].type != OP_IN)
2134 goto fail;
2136 /* If this alternative asks for a specific reg class, see if there
2137 is at least one allocatable register in that class. */
2138 no_regs_p
2139 = (this_alternative == NO_REGS
2140 || (hard_reg_set_subset_p
2141 (reg_class_contents[this_alternative],
2142 lra_no_alloc_regs)));
2144 /* For asms, verify that the class for this alternative is possible
2145 for the mode that is specified. */
2146 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2148 int i;
2149 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2150 if (HARD_REGNO_MODE_OK (i, mode)
2151 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2152 mode, i))
2153 break;
2154 if (i == FIRST_PSEUDO_REGISTER)
2155 winreg = false;
2158 /* If this operand accepts a register, and if the
2159 register class has at least one allocatable register,
2160 then this operand can be reloaded. */
2161 if (winreg && !no_regs_p)
2162 badop = false;
2164 if (badop)
2166 if (lra_dump_file != NULL)
2167 fprintf (lra_dump_file,
2168 " alt=%d: Bad operand -- refuse\n",
2169 nalt);
2170 goto fail;
2173 /* If not assigned pseudo has a class which a subset of
2174 required reg class, it is a less costly alternative
2175 as the pseudo still can get a hard reg of necessary
2176 class. */
2177 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2178 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2179 && ira_class_subset_p[this_alternative][cl])
2181 if (lra_dump_file != NULL)
2182 fprintf
2183 (lra_dump_file,
2184 " %d Super set class reg: reject-=3\n", nop);
2185 reject -= 3;
2188 this_alternative_offmemok = offmemok;
2189 if (this_costly_alternative != NO_REGS)
2191 if (lra_dump_file != NULL)
2192 fprintf (lra_dump_file,
2193 " %d Costly loser: reject++\n", nop);
2194 reject++;
2196 /* If the operand is dying, has a matching constraint,
2197 and satisfies constraints of the matched operand
2198 which failed to satisfy the own constraints, most probably
2199 the reload for this operand will be gone. */
2200 if (this_alternative_matches >= 0
2201 && !curr_alt_win[this_alternative_matches]
2202 && REG_P (op)
2203 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2204 && (hard_regno[nop] >= 0
2205 ? in_hard_reg_set_p (this_alternative_set,
2206 mode, hard_regno[nop])
2207 : in_class_p (op, this_alternative, NULL)))
2209 if (lra_dump_file != NULL)
2210 fprintf
2211 (lra_dump_file,
2212 " %d Dying matched operand reload: reject++\n",
2213 nop);
2214 reject++;
2216 else
2218 /* Strict_low_part requires to reload the register
2219 not the sub-register. In this case we should
2220 check that a final reload hard reg can hold the
2221 value mode. */
2222 if (curr_static_id->operand[nop].strict_low
2223 && REG_P (op)
2224 && hard_regno[nop] < 0
2225 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2226 && ira_class_hard_regs_num[this_alternative] > 0
2227 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2228 [this_alternative][0],
2229 GET_MODE
2230 (*curr_id->operand_loc[nop])))
2232 if (lra_dump_file != NULL)
2233 fprintf
2234 (lra_dump_file,
2235 " alt=%d: Strict low subreg reload -- refuse\n",
2236 nalt);
2237 goto fail;
2239 losers++;
2241 if (operand_reg[nop] != NULL_RTX
2242 /* Output operands and matched input operands are
2243 not inherited. The following conditions do not
2244 exactly describe the previous statement but they
2245 are pretty close. */
2246 && curr_static_id->operand[nop].type != OP_OUT
2247 && (this_alternative_matches < 0
2248 || curr_static_id->operand[nop].type != OP_IN))
2250 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2251 (operand_reg[nop])]
2252 .last_reload);
2254 /* The value of reload_sum has sense only if we
2255 process insns in their order. It happens only on
2256 the first constraints sub-pass when we do most of
2257 reload work. */
2258 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2259 reload_sum += last_reload - bb_reload_num;
2261 /* If this is a constant that is reloaded into the
2262 desired class by copying it to memory first, count
2263 that as another reload. This is consistent with
2264 other code and is required to avoid choosing another
2265 alternative when the constant is moved into memory.
2266 Note that the test here is precisely the same as in
2267 the code below that calls force_const_mem. */
2268 if (CONST_POOL_OK_P (mode, op)
2269 && ((targetm.preferred_reload_class
2270 (op, this_alternative) == NO_REGS)
2271 || no_input_reloads_p))
2273 const_to_mem = 1;
2274 if (! no_regs_p)
2275 losers++;
2278 /* Alternative loses if it requires a type of reload not
2279 permitted for this insn. We can always reload
2280 objects with a REG_UNUSED note. */
2281 if ((curr_static_id->operand[nop].type != OP_IN
2282 && no_output_reloads_p
2283 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2284 || (curr_static_id->operand[nop].type != OP_OUT
2285 && no_input_reloads_p && ! const_to_mem)
2286 || (this_alternative_matches >= 0
2287 && (no_input_reloads_p
2288 || (no_output_reloads_p
2289 && (curr_static_id->operand
2290 [this_alternative_matches].type != OP_IN)
2291 && ! find_reg_note (curr_insn, REG_UNUSED,
2292 no_subreg_reg_operand
2293 [this_alternative_matches])))))
2295 if (lra_dump_file != NULL)
2296 fprintf
2297 (lra_dump_file,
2298 " alt=%d: No input/otput reload -- refuse\n",
2299 nalt);
2300 goto fail;
2303 /* Check strong discouragement of reload of non-constant
2304 into class THIS_ALTERNATIVE. */
2305 if (! CONSTANT_P (op) && ! no_regs_p
2306 && (targetm.preferred_reload_class
2307 (op, this_alternative) == NO_REGS
2308 || (curr_static_id->operand[nop].type == OP_OUT
2309 && (targetm.preferred_output_reload_class
2310 (op, this_alternative) == NO_REGS))))
2312 if (lra_dump_file != NULL)
2313 fprintf (lra_dump_file,
2314 " %d Non-prefered reload: reject+=%d\n",
2315 nop, LRA_MAX_REJECT);
2316 reject += LRA_MAX_REJECT;
2319 if (! (MEM_P (op) && offmemok)
2320 && ! (const_to_mem && constmemok))
2322 /* We prefer to reload pseudos over reloading other
2323 things, since such reloads may be able to be
2324 eliminated later. So bump REJECT in other cases.
2325 Don't do this in the case where we are forcing a
2326 constant into memory and it will then win since
2327 we don't want to have a different alternative
2328 match then. */
2329 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2331 if (lra_dump_file != NULL)
2332 fprintf
2333 (lra_dump_file,
2334 " %d Non-pseudo reload: reject+=2\n",
2335 nop);
2336 reject += 2;
2339 if (! no_regs_p)
2340 reload_nregs
2341 += ira_reg_class_max_nregs[this_alternative][mode];
2343 if (SMALL_REGISTER_CLASS_P (this_alternative))
2345 if (lra_dump_file != NULL)
2346 fprintf
2347 (lra_dump_file,
2348 " %d Small class reload: reject+=%d\n",
2349 nop, LRA_LOSER_COST_FACTOR / 2);
2350 reject += LRA_LOSER_COST_FACTOR / 2;
2354 /* We are trying to spill pseudo into memory. It is
2355 usually more costly than moving to a hard register
2356 although it might takes the same number of
2357 reloads. */
2358 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2360 if (lra_dump_file != NULL)
2361 fprintf
2362 (lra_dump_file,
2363 " %d Spill pseudo into memory: reject+=3\n",
2364 nop);
2365 reject += 3;
2366 if (VECTOR_MODE_P (mode))
2368 /* Spilling vectors into memory is usually more
2369 costly as they contain big values. */
2370 if (lra_dump_file != NULL)
2371 fprintf
2372 (lra_dump_file,
2373 " %d Spill vector pseudo: reject+=2\n",
2374 nop);
2375 reject += 2;
2379 #ifdef SECONDARY_MEMORY_NEEDED
2380 /* If reload requires moving value through secondary
2381 memory, it will need one more insn at least. */
2382 if (this_alternative != NO_REGS
2383 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2384 && ((curr_static_id->operand[nop].type != OP_OUT
2385 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2386 GET_MODE (op)))
2387 || (curr_static_id->operand[nop].type != OP_IN
2388 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2389 GET_MODE (op)))))
2390 losers++;
2391 #endif
2392 /* Input reloads can be inherited more often than output
2393 reloads can be removed, so penalize output
2394 reloads. */
2395 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2397 if (lra_dump_file != NULL)
2398 fprintf
2399 (lra_dump_file,
2400 " %d Non input pseudo reload: reject++\n",
2401 nop);
2402 reject++;
2406 if (early_clobber_p && ! scratch_p)
2408 if (lra_dump_file != NULL)
2409 fprintf (lra_dump_file,
2410 " %d Early clobber: reject++\n", nop);
2411 reject++;
2413 /* ??? We check early clobbers after processing all operands
2414 (see loop below) and there we update the costs more.
2415 Should we update the cost (may be approximately) here
2416 because of early clobber register reloads or it is a rare
2417 or non-important thing to be worth to do it. */
2418 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2419 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2421 if (lra_dump_file != NULL)
2422 fprintf (lra_dump_file,
2423 " alt=%d,overall=%d,losers=%d -- refuse\n",
2424 nalt, overall, losers);
2425 goto fail;
2428 curr_alt[nop] = this_alternative;
2429 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2430 curr_alt_win[nop] = this_alternative_win;
2431 curr_alt_match_win[nop] = this_alternative_match_win;
2432 curr_alt_offmemok[nop] = this_alternative_offmemok;
2433 curr_alt_matches[nop] = this_alternative_matches;
2435 if (this_alternative_matches >= 0
2436 && !did_match && !this_alternative_win)
2437 curr_alt_win[this_alternative_matches] = false;
2439 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2440 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2442 if (curr_insn_set != NULL_RTX && n_operands == 2
2443 /* Prevent processing non-move insns. */
2444 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2445 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2446 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2447 && REG_P (no_subreg_reg_operand[0])
2448 && REG_P (no_subreg_reg_operand[1])
2449 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2450 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2451 || (! curr_alt_win[0] && curr_alt_win[1]
2452 && REG_P (no_subreg_reg_operand[1])
2453 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2454 || (curr_alt_win[0] && ! curr_alt_win[1]
2455 && REG_P (no_subreg_reg_operand[0])
2456 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2457 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2458 no_subreg_reg_operand[1])
2459 || (targetm.preferred_reload_class
2460 (no_subreg_reg_operand[1],
2461 (enum reg_class) curr_alt[1]) != NO_REGS))
2462 /* If it is a result of recent elimination in move
2463 insn we can transform it into an add still by
2464 using this alternative. */
2465 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2467 /* We have a move insn and a new reload insn will be similar
2468 to the current insn. We should avoid such situation as it
2469 results in LRA cycling. */
2470 overall += LRA_MAX_REJECT;
2472 ok_p = true;
2473 curr_alt_dont_inherit_ops_num = 0;
2474 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2476 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2477 HARD_REG_SET temp_set;
2479 i = early_clobbered_nops[nop];
2480 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2481 || hard_regno[i] < 0)
2482 continue;
2483 lra_assert (operand_reg[i] != NULL_RTX);
2484 clobbered_hard_regno = hard_regno[i];
2485 CLEAR_HARD_REG_SET (temp_set);
2486 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2487 first_conflict_j = last_conflict_j = -1;
2488 for (j = 0; j < n_operands; j++)
2489 if (j == i
2490 /* We don't want process insides of match_operator and
2491 match_parallel because otherwise we would process
2492 their operands once again generating a wrong
2493 code. */
2494 || curr_static_id->operand[j].is_operator)
2495 continue;
2496 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2497 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2498 continue;
2499 /* If we don't reload j-th operand, check conflicts. */
2500 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2501 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2503 if (first_conflict_j < 0)
2504 first_conflict_j = j;
2505 last_conflict_j = j;
2507 if (last_conflict_j < 0)
2508 continue;
2509 /* If earlyclobber operand conflicts with another
2510 non-matching operand which is actually the same register
2511 as the earlyclobber operand, it is better to reload the
2512 another operand as an operand matching the earlyclobber
2513 operand can be also the same. */
2514 if (first_conflict_j == last_conflict_j
2515 && operand_reg[last_conflict_j]
2516 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2517 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2519 curr_alt_win[last_conflict_j] = false;
2520 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2521 = last_conflict_j;
2522 losers++;
2523 /* Early clobber was already reflected in REJECT. */
2524 lra_assert (reject > 0);
2525 if (lra_dump_file != NULL)
2526 fprintf
2527 (lra_dump_file,
2528 " %d Conflict early clobber reload: reject--\n",
2530 reject--;
2531 overall += LRA_LOSER_COST_FACTOR - 1;
2533 else
2535 /* We need to reload early clobbered register and the
2536 matched registers. */
2537 for (j = 0; j < n_operands; j++)
2538 if (curr_alt_matches[j] == i)
2540 curr_alt_match_win[j] = false;
2541 losers++;
2542 overall += LRA_LOSER_COST_FACTOR;
2544 if (! curr_alt_match_win[i])
2545 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2546 else
2548 /* Remember pseudos used for match reloads are never
2549 inherited. */
2550 lra_assert (curr_alt_matches[i] >= 0);
2551 curr_alt_win[curr_alt_matches[i]] = false;
2553 curr_alt_win[i] = curr_alt_match_win[i] = false;
2554 losers++;
2555 /* Early clobber was already reflected in REJECT. */
2556 lra_assert (reject > 0);
2557 if (lra_dump_file != NULL)
2558 fprintf
2559 (lra_dump_file,
2560 " %d Matched conflict early clobber reloads:"
2561 "reject--\n",
2563 reject--;
2564 overall += LRA_LOSER_COST_FACTOR - 1;
2567 if (lra_dump_file != NULL)
2568 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2569 nalt, overall, losers, reload_nregs);
2571 /* If this alternative can be made to work by reloading, and it
2572 needs less reloading than the others checked so far, record
2573 it as the chosen goal for reloading. */
2574 if ((best_losers != 0 && losers == 0)
2575 || (((best_losers == 0 && losers == 0)
2576 || (best_losers != 0 && losers != 0))
2577 && (best_overall > overall
2578 || (best_overall == overall
2579 /* If the cost of the reloads is the same,
2580 prefer alternative which requires minimal
2581 number of reload regs. */
2582 && (reload_nregs < best_reload_nregs
2583 || (reload_nregs == best_reload_nregs
2584 && (best_reload_sum < reload_sum
2585 || (best_reload_sum == reload_sum
2586 && nalt < goal_alt_number))))))))
2588 for (nop = 0; nop < n_operands; nop++)
2590 goal_alt_win[nop] = curr_alt_win[nop];
2591 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2592 goal_alt_matches[nop] = curr_alt_matches[nop];
2593 goal_alt[nop] = curr_alt[nop];
2594 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2596 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2597 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2598 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2599 goal_alt_swapped = curr_swapped;
2600 best_overall = overall;
2601 best_losers = losers;
2602 best_reload_nregs = reload_nregs;
2603 best_reload_sum = reload_sum;
2604 goal_alt_number = nalt;
2606 if (losers == 0)
2607 /* Everything is satisfied. Do not process alternatives
2608 anymore. */
2609 break;
2610 fail:
2613 return ok_p;
2616 /* Return 1 if ADDR is a valid memory address for mode MODE in address
2617 space AS, and check that each pseudo has the proper kind of hard
2618 reg. */
2619 static int
2620 valid_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2621 rtx addr, addr_space_t as)
2623 #ifdef GO_IF_LEGITIMATE_ADDRESS
2624 lra_assert (ADDR_SPACE_GENERIC_P (as));
2625 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
2626 return 0;
2628 win:
2629 return 1;
2630 #else
2631 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
2632 #endif
2635 /* Return whether address AD is valid. */
2637 static bool
2638 valid_address_p (struct address_info *ad)
2640 /* Some ports do not check displacements for eliminable registers,
2641 so we replace them temporarily with the elimination target. */
2642 rtx saved_base_reg = NULL_RTX;
2643 rtx saved_index_reg = NULL_RTX;
2644 rtx *base_term = strip_subreg (ad->base_term);
2645 rtx *index_term = strip_subreg (ad->index_term);
2646 if (base_term != NULL)
2648 saved_base_reg = *base_term;
2649 lra_eliminate_reg_if_possible (base_term);
2650 if (ad->base_term2 != NULL)
2651 *ad->base_term2 = *ad->base_term;
2653 if (index_term != NULL)
2655 saved_index_reg = *index_term;
2656 lra_eliminate_reg_if_possible (index_term);
2658 bool ok_p = valid_address_p (ad->mode, *ad->outer, ad->as);
2659 if (saved_base_reg != NULL_RTX)
2661 *base_term = saved_base_reg;
2662 if (ad->base_term2 != NULL)
2663 *ad->base_term2 = *ad->base_term;
2665 if (saved_index_reg != NULL_RTX)
2666 *index_term = saved_index_reg;
2667 return ok_p;
2670 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2671 static rtx
2672 base_plus_disp_to_reg (struct address_info *ad)
2674 enum reg_class cl;
2675 rtx new_reg;
2677 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2678 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2679 get_index_code (ad));
2680 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2681 cl, "base + disp");
2682 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2683 return new_reg;
2686 /* Make reload of index part of address AD. Return the new
2687 pseudo. */
2688 static rtx
2689 index_part_to_reg (struct address_info *ad)
2691 rtx new_reg;
2693 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2694 INDEX_REG_CLASS, "index term");
2695 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2696 GEN_INT (get_index_scale (ad)), new_reg, 1);
2697 return new_reg;
2700 /* Return true if we can add a displacement to address AD, even if that
2701 makes the address invalid. The fix-up code requires any new address
2702 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2703 static bool
2704 can_add_disp_p (struct address_info *ad)
2706 return (!ad->autoinc_p
2707 && ad->segment == NULL
2708 && ad->base == ad->base_term
2709 && ad->disp == ad->disp_term);
2712 /* Make equiv substitution in address AD. Return true if a substitution
2713 was made. */
2714 static bool
2715 equiv_address_substitution (struct address_info *ad)
2717 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2718 HOST_WIDE_INT disp, scale;
2719 bool change_p;
2721 base_term = strip_subreg (ad->base_term);
2722 if (base_term == NULL)
2723 base_reg = new_base_reg = NULL_RTX;
2724 else
2726 base_reg = *base_term;
2727 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2729 index_term = strip_subreg (ad->index_term);
2730 if (index_term == NULL)
2731 index_reg = new_index_reg = NULL_RTX;
2732 else
2734 index_reg = *index_term;
2735 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2737 if (base_reg == new_base_reg && index_reg == new_index_reg)
2738 return false;
2739 disp = 0;
2740 change_p = false;
2741 if (lra_dump_file != NULL)
2743 fprintf (lra_dump_file, "Changing address in insn %d ",
2744 INSN_UID (curr_insn));
2745 dump_value_slim (lra_dump_file, *ad->outer, 1);
2747 if (base_reg != new_base_reg)
2749 if (REG_P (new_base_reg))
2751 *base_term = new_base_reg;
2752 change_p = true;
2754 else if (GET_CODE (new_base_reg) == PLUS
2755 && REG_P (XEXP (new_base_reg, 0))
2756 && CONST_INT_P (XEXP (new_base_reg, 1))
2757 && can_add_disp_p (ad))
2759 disp += INTVAL (XEXP (new_base_reg, 1));
2760 *base_term = XEXP (new_base_reg, 0);
2761 change_p = true;
2763 if (ad->base_term2 != NULL)
2764 *ad->base_term2 = *ad->base_term;
2766 if (index_reg != new_index_reg)
2768 if (REG_P (new_index_reg))
2770 *index_term = new_index_reg;
2771 change_p = true;
2773 else if (GET_CODE (new_index_reg) == PLUS
2774 && REG_P (XEXP (new_index_reg, 0))
2775 && CONST_INT_P (XEXP (new_index_reg, 1))
2776 && can_add_disp_p (ad)
2777 && (scale = get_index_scale (ad)))
2779 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2780 *index_term = XEXP (new_index_reg, 0);
2781 change_p = true;
2784 if (disp != 0)
2786 if (ad->disp != NULL)
2787 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2788 else
2790 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2791 update_address (ad);
2793 change_p = true;
2795 if (lra_dump_file != NULL)
2797 if (! change_p)
2798 fprintf (lra_dump_file, " -- no change\n");
2799 else
2801 fprintf (lra_dump_file, " on equiv ");
2802 dump_value_slim (lra_dump_file, *ad->outer, 1);
2803 fprintf (lra_dump_file, "\n");
2806 return change_p;
2809 /* Major function to make reloads for an address in operand NOP.
2810 The supported cases are:
2812 1) an address that existed before LRA started, at which point it
2813 must have been valid. These addresses are subject to elimination
2814 and may have become invalid due to the elimination offset being out
2815 of range.
2817 2) an address created by forcing a constant to memory
2818 (force_const_to_mem). The initial form of these addresses might
2819 not be valid, and it is this function's job to make them valid.
2821 3) a frame address formed from a register and a (possibly zero)
2822 constant offset. As above, these addresses might not be valid and
2823 this function must make them so.
2825 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2826 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2827 address. Return true for any RTL change.
2829 The function is a helper function which does not produce all
2830 transformations which can be necessary. It does just basic steps.
2831 To do all necessary transformations use function
2832 process_address. */
2833 static bool
2834 process_address_1 (int nop, rtx *before, rtx *after)
2836 struct address_info ad;
2837 rtx new_reg;
2838 rtx op = *curr_id->operand_loc[nop];
2839 const char *constraint = curr_static_id->operand[nop].constraint;
2840 bool change_p;
2842 if (constraint[0] == 'p'
2843 || EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint))
2844 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2845 else if (MEM_P (op))
2846 decompose_mem_address (&ad, op);
2847 else if (GET_CODE (op) == SUBREG
2848 && MEM_P (SUBREG_REG (op)))
2849 decompose_mem_address (&ad, SUBREG_REG (op));
2850 else
2851 return false;
2852 change_p = equiv_address_substitution (&ad);
2853 if (ad.base_term != NULL
2854 && (process_addr_reg
2855 (ad.base_term, before,
2856 (ad.autoinc_p
2857 && !(REG_P (*ad.base_term)
2858 && find_regno_note (curr_insn, REG_DEAD,
2859 REGNO (*ad.base_term)) != NULL_RTX)
2860 ? after : NULL),
2861 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2862 get_index_code (&ad)))))
2864 change_p = true;
2865 if (ad.base_term2 != NULL)
2866 *ad.base_term2 = *ad.base_term;
2868 if (ad.index_term != NULL
2869 && process_addr_reg (ad.index_term, before, NULL, INDEX_REG_CLASS))
2870 change_p = true;
2872 #ifdef EXTRA_CONSTRAINT_STR
2873 /* Target hooks sometimes reject extra constraint addresses -- use
2874 EXTRA_CONSTRAINT_STR for the validation. */
2875 if (constraint[0] != 'p'
2876 && EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint)
2877 && EXTRA_CONSTRAINT_STR (op, constraint[0], constraint))
2878 return change_p;
2879 #endif
2881 /* There are three cases where the shape of *AD.INNER may now be invalid:
2883 1) the original address was valid, but either elimination or
2884 equiv_address_substitution was applied and that made
2885 the address invalid.
2887 2) the address is an invalid symbolic address created by
2888 force_const_to_mem.
2890 3) the address is a frame address with an invalid offset.
2892 All these cases involve a non-autoinc address, so there is no
2893 point revalidating other types. */
2894 if (ad.autoinc_p || valid_address_p (&ad))
2895 return change_p;
2897 /* Any index existed before LRA started, so we can assume that the
2898 presence and shape of the index is valid. */
2899 push_to_sequence (*before);
2900 lra_assert (ad.disp == ad.disp_term);
2901 if (ad.base == NULL)
2903 if (ad.index == NULL)
2905 int code = -1;
2906 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2907 SCRATCH, SCRATCH);
2908 rtx addr = *ad.inner;
2910 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2911 #ifdef HAVE_lo_sum
2913 rtx insn;
2914 rtx last = get_last_insn ();
2916 /* addr => lo_sum (new_base, addr), case (2) above. */
2917 insn = emit_insn (gen_rtx_SET
2918 (VOIDmode, new_reg,
2919 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2920 code = recog_memoized (insn);
2921 if (code >= 0)
2923 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2924 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2926 /* Try to put lo_sum into register. */
2927 insn = emit_insn (gen_rtx_SET
2928 (VOIDmode, new_reg,
2929 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2930 code = recog_memoized (insn);
2931 if (code >= 0)
2933 *ad.inner = new_reg;
2934 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2936 *ad.inner = addr;
2937 code = -1;
2943 if (code < 0)
2944 delete_insns_since (last);
2946 #endif
2947 if (code < 0)
2949 /* addr => new_base, case (2) above. */
2950 lra_emit_move (new_reg, addr);
2951 *ad.inner = new_reg;
2954 else
2956 /* index * scale + disp => new base + index * scale,
2957 case (1) above. */
2958 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
2959 GET_CODE (*ad.index));
2961 lra_assert (INDEX_REG_CLASS != NO_REGS);
2962 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
2963 lra_emit_move (new_reg, *ad.disp);
2964 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2965 new_reg, *ad.index);
2968 else if (ad.index == NULL)
2970 int regno;
2971 enum reg_class cl;
2972 rtx set, insns, last_insn;
2973 /* base + disp => new base, cases (1) and (3) above. */
2974 /* Another option would be to reload the displacement into an
2975 index register. However, postreload has code to optimize
2976 address reloads that have the same base and different
2977 displacements, so reloading into an index register would
2978 not necessarily be a win. */
2979 start_sequence ();
2980 new_reg = base_plus_disp_to_reg (&ad);
2981 insns = get_insns ();
2982 last_insn = get_last_insn ();
2983 /* If we generated at least two insns, try last insn source as
2984 an address. If we succeed, we generate one less insn. */
2985 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
2986 && GET_CODE (SET_SRC (set)) == PLUS
2987 && REG_P (XEXP (SET_SRC (set), 0))
2988 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
2990 *ad.inner = SET_SRC (set);
2991 if (valid_address_p (ad.mode, *ad.outer, ad.as))
2993 *ad.base_term = XEXP (SET_SRC (set), 0);
2994 *ad.disp_term = XEXP (SET_SRC (set), 1);
2995 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2996 get_index_code (&ad));
2997 regno = REGNO (*ad.base_term);
2998 if (regno >= FIRST_PSEUDO_REGISTER
2999 && cl != lra_get_allocno_class (regno))
3000 lra_change_class (regno, cl, " Change to", true);
3001 new_reg = SET_SRC (set);
3002 delete_insns_since (PREV_INSN (last_insn));
3005 end_sequence ();
3006 emit_insn (insns);
3007 *ad.inner = new_reg;
3009 else if (ad.disp_term != NULL)
3011 /* base + scale * index + disp => new base + scale * index,
3012 case (1) above. */
3013 new_reg = base_plus_disp_to_reg (&ad);
3014 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3015 new_reg, *ad.index);
3017 else if (get_index_scale (&ad) == 1)
3019 /* The last transformation to one reg will be made in
3020 curr_insn_transform function. */
3021 end_sequence ();
3022 return false;
3024 else
3026 /* base + scale * index => base + new_reg,
3027 case (1) above.
3028 Index part of address may become invalid. For example, we
3029 changed pseudo on the equivalent memory and a subreg of the
3030 pseudo onto the memory of different mode for which the scale is
3031 prohibitted. */
3032 new_reg = index_part_to_reg (&ad);
3033 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3034 *ad.base_term, new_reg);
3036 *before = get_insns ();
3037 end_sequence ();
3038 return true;
3041 /* Do address reloads until it is necessary. Use process_address_1 as
3042 a helper function. Return true for any RTL changes. */
3043 static bool
3044 process_address (int nop, rtx *before, rtx *after)
3046 bool res = false;
3048 while (process_address_1 (nop, before, after))
3049 res = true;
3050 return res;
3053 /* Emit insns to reload VALUE into a new register. VALUE is an
3054 auto-increment or auto-decrement RTX whose operand is a register or
3055 memory location; so reloading involves incrementing that location.
3056 IN is either identical to VALUE, or some cheaper place to reload
3057 value being incremented/decremented from.
3059 INC_AMOUNT is the number to increment or decrement by (always
3060 positive and ignored for POST_MODIFY/PRE_MODIFY).
3062 Return pseudo containing the result. */
3063 static rtx
3064 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3066 /* REG or MEM to be copied and incremented. */
3067 rtx incloc = XEXP (value, 0);
3068 /* Nonzero if increment after copying. */
3069 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3070 || GET_CODE (value) == POST_MODIFY);
3071 rtx last;
3072 rtx inc;
3073 rtx add_insn;
3074 int code;
3075 rtx real_in = in == value ? incloc : in;
3076 rtx result;
3077 bool plus_p = true;
3079 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3081 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3082 || GET_CODE (XEXP (value, 1)) == MINUS);
3083 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3084 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3085 inc = XEXP (XEXP (value, 1), 1);
3087 else
3089 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3090 inc_amount = -inc_amount;
3092 inc = GEN_INT (inc_amount);
3095 if (! post && REG_P (incloc))
3096 result = incloc;
3097 else
3098 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3099 "INC/DEC result");
3101 if (real_in != result)
3103 /* First copy the location to the result register. */
3104 lra_assert (REG_P (result));
3105 emit_insn (gen_move_insn (result, real_in));
3108 /* We suppose that there are insns to add/sub with the constant
3109 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3110 old reload worked with this assumption. If the assumption
3111 becomes wrong, we should use approach in function
3112 base_plus_disp_to_reg. */
3113 if (in == value)
3115 /* See if we can directly increment INCLOC. */
3116 last = get_last_insn ();
3117 add_insn = emit_insn (plus_p
3118 ? gen_add2_insn (incloc, inc)
3119 : gen_sub2_insn (incloc, inc));
3121 code = recog_memoized (add_insn);
3122 if (code >= 0)
3124 if (! post && result != incloc)
3125 emit_insn (gen_move_insn (result, incloc));
3126 return result;
3128 delete_insns_since (last);
3131 /* If couldn't do the increment directly, must increment in RESULT.
3132 The way we do this depends on whether this is pre- or
3133 post-increment. For pre-increment, copy INCLOC to the reload
3134 register, increment it there, then save back. */
3135 if (! post)
3137 if (real_in != result)
3138 emit_insn (gen_move_insn (result, real_in));
3139 if (plus_p)
3140 emit_insn (gen_add2_insn (result, inc));
3141 else
3142 emit_insn (gen_sub2_insn (result, inc));
3143 if (result != incloc)
3144 emit_insn (gen_move_insn (incloc, result));
3146 else
3148 /* Post-increment.
3150 Because this might be a jump insn or a compare, and because
3151 RESULT may not be available after the insn in an input
3152 reload, we must do the incrementing before the insn being
3153 reloaded for.
3155 We have already copied IN to RESULT. Increment the copy in
3156 RESULT, save that back, then decrement RESULT so it has
3157 the original value. */
3158 if (plus_p)
3159 emit_insn (gen_add2_insn (result, inc));
3160 else
3161 emit_insn (gen_sub2_insn (result, inc));
3162 emit_insn (gen_move_insn (incloc, result));
3163 /* Restore non-modified value for the result. We prefer this
3164 way because it does not require an additional hard
3165 register. */
3166 if (plus_p)
3168 if (CONST_INT_P (inc))
3169 emit_insn (gen_add2_insn (result,
3170 gen_int_mode (-INTVAL (inc),
3171 GET_MODE (result))));
3172 else
3173 emit_insn (gen_sub2_insn (result, inc));
3175 else
3176 emit_insn (gen_add2_insn (result, inc));
3178 return result;
3181 /* Return true if the current move insn does not need processing as we
3182 already know that it satisfies its constraints. */
3183 static bool
3184 simple_move_p (void)
3186 rtx dest, src;
3187 enum reg_class dclass, sclass;
3189 lra_assert (curr_insn_set != NULL_RTX);
3190 dest = SET_DEST (curr_insn_set);
3191 src = SET_SRC (curr_insn_set);
3192 return ((dclass = get_op_class (dest)) != NO_REGS
3193 && (sclass = get_op_class (src)) != NO_REGS
3194 /* The backend guarantees that register moves of cost 2
3195 never need reloads. */
3196 && targetm.register_move_cost (GET_MODE (src), dclass, sclass) == 2);
3199 /* Swap operands NOP and NOP + 1. */
3200 static inline void
3201 swap_operands (int nop)
3203 enum machine_mode mode = curr_operand_mode[nop];
3204 curr_operand_mode[nop] = curr_operand_mode[nop + 1];
3205 curr_operand_mode[nop + 1] = mode;
3206 mode = original_subreg_reg_mode[nop];
3207 original_subreg_reg_mode[nop] = original_subreg_reg_mode[nop + 1];
3208 original_subreg_reg_mode[nop + 1] = mode;
3209 rtx x = *curr_id->operand_loc[nop];
3210 *curr_id->operand_loc[nop] = *curr_id->operand_loc[nop + 1];
3211 *curr_id->operand_loc[nop + 1] = x;
3212 /* Swap the duplicates too. */
3213 lra_update_dup (curr_id, nop);
3214 lra_update_dup (curr_id, nop + 1);
3217 /* Main entry point of the constraint code: search the body of the
3218 current insn to choose the best alternative. It is mimicking insn
3219 alternative cost calculation model of former reload pass. That is
3220 because machine descriptions were written to use this model. This
3221 model can be changed in future. Make commutative operand exchange
3222 if it is chosen.
3224 Return true if some RTL changes happened during function call. */
3225 static bool
3226 curr_insn_transform (void)
3228 int i, j, k;
3229 int n_operands;
3230 int n_alternatives;
3231 int commutative;
3232 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3233 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3234 rtx before, after;
3235 bool alt_p = false;
3236 /* Flag that the insn has been changed through a transformation. */
3237 bool change_p;
3238 bool sec_mem_p;
3239 #ifdef SECONDARY_MEMORY_NEEDED
3240 bool use_sec_mem_p;
3241 #endif
3242 int max_regno_before;
3243 int reused_alternative_num;
3245 curr_insn_set = single_set (curr_insn);
3246 if (curr_insn_set != NULL_RTX && simple_move_p ())
3247 return false;
3249 no_input_reloads_p = no_output_reloads_p = false;
3250 goal_alt_number = -1;
3251 change_p = sec_mem_p = false;
3252 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3253 reloads; neither are insns that SET cc0. Insns that use CC0 are
3254 not allowed to have any input reloads. */
3255 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3256 no_output_reloads_p = true;
3258 #ifdef HAVE_cc0
3259 if (reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3260 no_input_reloads_p = true;
3261 if (reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3262 no_output_reloads_p = true;
3263 #endif
3265 n_operands = curr_static_id->n_operands;
3266 n_alternatives = curr_static_id->n_alternatives;
3268 /* Just return "no reloads" if insn has no operands with
3269 constraints. */
3270 if (n_operands == 0 || n_alternatives == 0)
3271 return false;
3273 max_regno_before = max_reg_num ();
3275 for (i = 0; i < n_operands; i++)
3277 goal_alt_matched[i][0] = -1;
3278 goal_alt_matches[i] = -1;
3281 commutative = curr_static_id->commutative;
3283 /* Now see what we need for pseudos that didn't get hard regs or got
3284 the wrong kind of hard reg. For this, we must consider all the
3285 operands together against the register constraints. */
3287 best_losers = best_overall = INT_MAX;
3288 best_reload_sum = 0;
3290 curr_swapped = false;
3291 goal_alt_swapped = false;
3293 /* Make equivalence substitution and memory subreg elimination
3294 before address processing because an address legitimacy can
3295 depend on memory mode. */
3296 for (i = 0; i < n_operands; i++)
3298 rtx op = *curr_id->operand_loc[i];
3299 rtx subst, old = op;
3300 bool op_change_p = false;
3302 if (GET_CODE (old) == SUBREG)
3303 old = SUBREG_REG (old);
3304 subst = get_equiv_with_elimination (old, curr_insn);
3305 original_subreg_reg_mode[i] = VOIDmode;
3306 if (subst != old)
3308 subst = copy_rtx (subst);
3309 lra_assert (REG_P (old));
3310 if (GET_CODE (op) != SUBREG)
3311 *curr_id->operand_loc[i] = subst;
3312 else
3314 SUBREG_REG (op) = subst;
3315 if (GET_MODE (subst) == VOIDmode)
3316 original_subreg_reg_mode[i] = GET_MODE (old);
3318 if (lra_dump_file != NULL)
3320 fprintf (lra_dump_file,
3321 "Changing pseudo %d in operand %i of insn %u on equiv ",
3322 REGNO (old), i, INSN_UID (curr_insn));
3323 dump_value_slim (lra_dump_file, subst, 1);
3324 fprintf (lra_dump_file, "\n");
3326 op_change_p = change_p = true;
3328 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3330 change_p = true;
3331 lra_update_dup (curr_id, i);
3335 /* Reload address registers and displacements. We do it before
3336 finding an alternative because of memory constraints. */
3337 before = after = NULL_RTX;
3338 for (i = 0; i < n_operands; i++)
3339 if (! curr_static_id->operand[i].is_operator
3340 && process_address (i, &before, &after))
3342 change_p = true;
3343 lra_update_dup (curr_id, i);
3346 if (change_p)
3347 /* If we've changed the instruction then any alternative that
3348 we chose previously may no longer be valid. */
3349 lra_set_used_insn_alternative (curr_insn, -1);
3351 if (curr_insn_set != NULL_RTX
3352 && check_and_process_move (&change_p, &sec_mem_p))
3353 return change_p;
3355 try_swapped:
3357 reused_alternative_num = curr_id->used_insn_alternative;
3358 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3359 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3360 reused_alternative_num, INSN_UID (curr_insn));
3362 if (process_alt_operands (reused_alternative_num))
3363 alt_p = true;
3365 /* If insn is commutative (it's safe to exchange a certain pair of
3366 operands) then we need to try each alternative twice, the second
3367 time matching those two operands as if we had exchanged them. To
3368 do this, really exchange them in operands.
3370 If we have just tried the alternatives the second time, return
3371 operands to normal and drop through. */
3373 if (reused_alternative_num < 0 && commutative >= 0)
3375 curr_swapped = !curr_swapped;
3376 if (curr_swapped)
3378 swap_operands (commutative);
3379 goto try_swapped;
3381 else
3382 swap_operands (commutative);
3385 if (! alt_p && ! sec_mem_p)
3387 /* No alternative works with reloads?? */
3388 if (INSN_CODE (curr_insn) >= 0)
3389 fatal_insn ("unable to generate reloads for:", curr_insn);
3390 error_for_asm (curr_insn,
3391 "inconsistent operand constraints in an %<asm%>");
3392 /* Avoid further trouble with this insn. */
3393 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3394 lra_invalidate_insn_data (curr_insn);
3395 return true;
3398 /* If the best alternative is with operands 1 and 2 swapped, swap
3399 them. Update the operand numbers of any reloads already
3400 pushed. */
3402 if (goal_alt_swapped)
3404 if (lra_dump_file != NULL)
3405 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3406 INSN_UID (curr_insn));
3408 /* Swap the duplicates too. */
3409 swap_operands (commutative);
3410 change_p = true;
3413 #ifdef SECONDARY_MEMORY_NEEDED
3414 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3415 too conservatively. So we use the secondary memory only if there
3416 is no any alternative without reloads. */
3417 use_sec_mem_p = false;
3418 if (! alt_p)
3419 use_sec_mem_p = true;
3420 else if (sec_mem_p)
3422 for (i = 0; i < n_operands; i++)
3423 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3424 break;
3425 use_sec_mem_p = i < n_operands;
3428 if (use_sec_mem_p)
3430 rtx new_reg, src, dest, rld;
3431 enum machine_mode sec_mode, rld_mode;
3433 lra_assert (sec_mem_p);
3434 lra_assert (curr_static_id->operand[0].type == OP_OUT
3435 && curr_static_id->operand[1].type == OP_IN);
3436 dest = *curr_id->operand_loc[0];
3437 src = *curr_id->operand_loc[1];
3438 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3439 ? dest : src);
3440 rld_mode = GET_MODE (rld);
3441 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3442 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3443 #else
3444 sec_mode = rld_mode;
3445 #endif
3446 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3447 NO_REGS, "secondary");
3448 /* If the mode is changed, it should be wider. */
3449 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3450 if (sec_mode != rld_mode)
3452 /* If the target says specifically to use another mode for
3453 secondary memory moves we can not reuse the original
3454 insn. */
3455 after = emit_spill_move (false, new_reg, dest);
3456 lra_process_new_insns (curr_insn, NULL_RTX, after,
3457 "Inserting the sec. move");
3458 /* We may have non null BEFORE here (e.g. after address
3459 processing. */
3460 push_to_sequence (before);
3461 before = emit_spill_move (true, new_reg, src);
3462 emit_insn (before);
3463 before = get_insns ();
3464 end_sequence ();
3465 lra_process_new_insns (curr_insn, before, NULL_RTX, "Changing on");
3466 lra_set_insn_deleted (curr_insn);
3468 else if (dest == rld)
3470 *curr_id->operand_loc[0] = new_reg;
3471 after = emit_spill_move (false, new_reg, dest);
3472 lra_process_new_insns (curr_insn, NULL_RTX, after,
3473 "Inserting the sec. move");
3475 else
3477 *curr_id->operand_loc[1] = new_reg;
3478 /* See comments above. */
3479 push_to_sequence (before);
3480 before = emit_spill_move (true, new_reg, src);
3481 emit_insn (before);
3482 before = get_insns ();
3483 end_sequence ();
3484 lra_process_new_insns (curr_insn, before, NULL_RTX,
3485 "Inserting the sec. move");
3487 lra_update_insn_regno_info (curr_insn);
3488 return true;
3490 #endif
3492 lra_assert (goal_alt_number >= 0);
3493 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3495 if (lra_dump_file != NULL)
3497 const char *p;
3499 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3500 goal_alt_number, INSN_UID (curr_insn));
3501 for (i = 0; i < n_operands; i++)
3503 p = (curr_static_id->operand_alternative
3504 [goal_alt_number * n_operands + i].constraint);
3505 if (*p == '\0')
3506 continue;
3507 fprintf (lra_dump_file, " (%d) ", i);
3508 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3509 fputc (*p, lra_dump_file);
3511 if (INSN_CODE (curr_insn) >= 0
3512 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3513 fprintf (lra_dump_file, " {%s}", p);
3514 if (curr_id->sp_offset != 0)
3515 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3516 curr_id->sp_offset);
3517 fprintf (lra_dump_file, "\n");
3520 /* Right now, for any pair of operands I and J that are required to
3521 match, with J < I, goal_alt_matches[I] is J. Add I to
3522 goal_alt_matched[J]. */
3524 for (i = 0; i < n_operands; i++)
3525 if ((j = goal_alt_matches[i]) >= 0)
3527 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3529 /* We allow matching one output operand and several input
3530 operands. */
3531 lra_assert (k == 0
3532 || (curr_static_id->operand[j].type == OP_OUT
3533 && curr_static_id->operand[i].type == OP_IN
3534 && (curr_static_id->operand
3535 [goal_alt_matched[j][0]].type == OP_IN)));
3536 goal_alt_matched[j][k] = i;
3537 goal_alt_matched[j][k + 1] = -1;
3540 for (i = 0; i < n_operands; i++)
3541 goal_alt_win[i] |= goal_alt_match_win[i];
3543 /* Any constants that aren't allowed and can't be reloaded into
3544 registers are here changed into memory references. */
3545 for (i = 0; i < n_operands; i++)
3546 if (goal_alt_win[i])
3548 int regno;
3549 enum reg_class new_class;
3550 rtx reg = *curr_id->operand_loc[i];
3552 if (GET_CODE (reg) == SUBREG)
3553 reg = SUBREG_REG (reg);
3555 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3557 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3559 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3561 lra_assert (ok_p);
3562 lra_change_class (regno, new_class, " Change to", true);
3566 else
3568 const char *constraint;
3569 char c;
3570 rtx op = *curr_id->operand_loc[i];
3571 rtx subreg = NULL_RTX;
3572 enum machine_mode mode = curr_operand_mode[i];
3574 if (GET_CODE (op) == SUBREG)
3576 subreg = op;
3577 op = SUBREG_REG (op);
3578 mode = GET_MODE (op);
3581 if (CONST_POOL_OK_P (mode, op)
3582 && ((targetm.preferred_reload_class
3583 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3584 || no_input_reloads_p))
3586 rtx tem = force_const_mem (mode, op);
3588 change_p = true;
3589 if (subreg != NULL_RTX)
3590 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3592 *curr_id->operand_loc[i] = tem;
3593 lra_update_dup (curr_id, i);
3594 process_address (i, &before, &after);
3596 /* If the alternative accepts constant pool refs directly
3597 there will be no reload needed at all. */
3598 if (subreg != NULL_RTX)
3599 continue;
3600 /* Skip alternatives before the one requested. */
3601 constraint = (curr_static_id->operand_alternative
3602 [goal_alt_number * n_operands + i].constraint);
3603 for (;
3604 (c = *constraint) && c != ',' && c != '#';
3605 constraint += CONSTRAINT_LEN (c, constraint))
3607 if (c == TARGET_MEM_CONSTRAINT || c == 'o')
3608 break;
3609 #ifdef EXTRA_CONSTRAINT_STR
3610 if (EXTRA_MEMORY_CONSTRAINT (c, constraint)
3611 && EXTRA_CONSTRAINT_STR (tem, c, constraint))
3612 break;
3613 #endif
3615 if (c == '\0' || c == ',' || c == '#')
3616 continue;
3618 goal_alt_win[i] = true;
3622 for (i = 0; i < n_operands; i++)
3624 int regno;
3625 bool optional_p = false;
3626 rtx old, new_reg;
3627 rtx op = *curr_id->operand_loc[i];
3629 if (goal_alt_win[i])
3631 if (goal_alt[i] == NO_REGS
3632 && REG_P (op)
3633 /* When we assign NO_REGS it means that we will not
3634 assign a hard register to the scratch pseudo by
3635 assigment pass and the scratch pseudo will be
3636 spilled. Spilled scratch pseudos are transformed
3637 back to scratches at the LRA end. */
3638 && lra_former_scratch_operand_p (curr_insn, i))
3640 int regno = REGNO (op);
3641 lra_change_class (regno, NO_REGS, " Change to", true);
3642 if (lra_get_regno_hard_regno (regno) >= 0)
3643 /* We don't have to mark all insn affected by the
3644 spilled pseudo as there is only one such insn, the
3645 current one. */
3646 reg_renumber[regno] = -1;
3648 /* We can do an optional reload. If the pseudo got a hard
3649 reg, we might improve the code through inheritance. If
3650 it does not get a hard register we coalesce memory/memory
3651 moves later. Ignore move insns to avoid cycling. */
3652 if (! lra_simple_p
3653 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3654 && goal_alt[i] != NO_REGS && REG_P (op)
3655 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3656 && regno < new_regno_start
3657 && ! lra_former_scratch_p (regno)
3658 && reg_renumber[regno] < 0
3659 && (curr_insn_set == NULL_RTX
3660 || !((REG_P (SET_SRC (curr_insn_set))
3661 || MEM_P (SET_SRC (curr_insn_set))
3662 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3663 && (REG_P (SET_DEST (curr_insn_set))
3664 || MEM_P (SET_DEST (curr_insn_set))
3665 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3666 optional_p = true;
3667 else
3668 continue;
3671 /* Operands that match previous ones have already been handled. */
3672 if (goal_alt_matches[i] >= 0)
3673 continue;
3675 /* We should not have an operand with a non-offsettable address
3676 appearing where an offsettable address will do. It also may
3677 be a case when the address should be special in other words
3678 not a general one (e.g. it needs no index reg). */
3679 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3681 enum reg_class rclass;
3682 rtx *loc = &XEXP (op, 0);
3683 enum rtx_code code = GET_CODE (*loc);
3685 push_to_sequence (before);
3686 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3687 MEM, SCRATCH);
3688 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3689 new_reg = emit_inc (rclass, *loc, *loc,
3690 /* This value does not matter for MODIFY. */
3691 GET_MODE_SIZE (GET_MODE (op)));
3692 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3693 "offsetable address", &new_reg))
3694 lra_emit_move (new_reg, *loc);
3695 before = get_insns ();
3696 end_sequence ();
3697 *loc = new_reg;
3698 lra_update_dup (curr_id, i);
3700 else if (goal_alt_matched[i][0] == -1)
3702 enum machine_mode mode;
3703 rtx reg, *loc;
3704 int hard_regno, byte;
3705 enum op_type type = curr_static_id->operand[i].type;
3707 loc = curr_id->operand_loc[i];
3708 mode = curr_operand_mode[i];
3709 if (GET_CODE (*loc) == SUBREG)
3711 reg = SUBREG_REG (*loc);
3712 byte = SUBREG_BYTE (*loc);
3713 if (REG_P (reg)
3714 /* Strict_low_part requires reload the register not
3715 the sub-register. */
3716 && (curr_static_id->operand[i].strict_low
3717 || (GET_MODE_SIZE (mode)
3718 <= GET_MODE_SIZE (GET_MODE (reg))
3719 && (hard_regno
3720 = get_try_hard_regno (REGNO (reg))) >= 0
3721 && (simplify_subreg_regno
3722 (hard_regno,
3723 GET_MODE (reg), byte, mode) < 0)
3724 && (goal_alt[i] == NO_REGS
3725 || (simplify_subreg_regno
3726 (ira_class_hard_regs[goal_alt[i]][0],
3727 GET_MODE (reg), byte, mode) >= 0)))))
3729 loc = &SUBREG_REG (*loc);
3730 mode = GET_MODE (*loc);
3733 old = *loc;
3734 if (get_reload_reg (type, mode, old, goal_alt[i],
3735 loc != curr_id->operand_loc[i], "", &new_reg)
3736 && type != OP_OUT)
3738 push_to_sequence (before);
3739 lra_emit_move (new_reg, old);
3740 before = get_insns ();
3741 end_sequence ();
3743 *loc = new_reg;
3744 if (type != OP_IN
3745 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3747 start_sequence ();
3748 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3749 emit_insn (after);
3750 after = get_insns ();
3751 end_sequence ();
3752 *loc = new_reg;
3754 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3755 if (goal_alt_dont_inherit_ops[j] == i)
3757 lra_set_regno_unique_value (REGNO (new_reg));
3758 break;
3760 lra_update_dup (curr_id, i);
3762 else if (curr_static_id->operand[i].type == OP_IN
3763 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3764 == OP_OUT))
3766 /* generate reloads for input and matched outputs. */
3767 match_inputs[0] = i;
3768 match_inputs[1] = -1;
3769 match_reload (goal_alt_matched[i][0], match_inputs,
3770 goal_alt[i], &before, &after);
3772 else if (curr_static_id->operand[i].type == OP_OUT
3773 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3774 == OP_IN))
3775 /* Generate reloads for output and matched inputs. */
3776 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3777 else if (curr_static_id->operand[i].type == OP_IN
3778 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3779 == OP_IN))
3781 /* Generate reloads for matched inputs. */
3782 match_inputs[0] = i;
3783 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3784 match_inputs[j + 1] = k;
3785 match_inputs[j + 1] = -1;
3786 match_reload (-1, match_inputs, goal_alt[i], &before, &after);
3788 else
3789 /* We must generate code in any case when function
3790 process_alt_operands decides that it is possible. */
3791 gcc_unreachable ();
3792 if (optional_p)
3794 lra_assert (REG_P (op));
3795 regno = REGNO (op);
3796 op = *curr_id->operand_loc[i]; /* Substitution. */
3797 if (GET_CODE (op) == SUBREG)
3798 op = SUBREG_REG (op);
3799 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3800 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3801 lra_reg_info[REGNO (op)].restore_regno = regno;
3802 if (lra_dump_file != NULL)
3803 fprintf (lra_dump_file,
3804 " Making reload reg %d for reg %d optional\n",
3805 REGNO (op), regno);
3808 if (before != NULL_RTX || after != NULL_RTX
3809 || max_regno_before != max_reg_num ())
3810 change_p = true;
3811 if (change_p)
3813 lra_update_operator_dups (curr_id);
3814 /* Something changes -- process the insn. */
3815 lra_update_insn_regno_info (curr_insn);
3817 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3818 return change_p;
3821 /* Return true if X is in LIST. */
3822 static bool
3823 in_list_p (rtx x, rtx list)
3825 for (; list != NULL_RTX; list = XEXP (list, 1))
3826 if (XEXP (list, 0) == x)
3827 return true;
3828 return false;
3831 /* Return true if X contains an allocatable hard register (if
3832 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3833 static bool
3834 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3836 int i, j;
3837 const char *fmt;
3838 enum rtx_code code;
3840 code = GET_CODE (x);
3841 if (REG_P (x))
3843 int regno = REGNO (x);
3844 HARD_REG_SET alloc_regs;
3846 if (hard_reg_p)
3848 if (regno >= FIRST_PSEUDO_REGISTER)
3849 regno = lra_get_regno_hard_regno (regno);
3850 if (regno < 0)
3851 return false;
3852 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3853 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3855 else
3857 if (regno < FIRST_PSEUDO_REGISTER)
3858 return false;
3859 if (! spilled_p)
3860 return true;
3861 return lra_get_regno_hard_regno (regno) < 0;
3864 fmt = GET_RTX_FORMAT (code);
3865 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3867 if (fmt[i] == 'e')
3869 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3870 return true;
3872 else if (fmt[i] == 'E')
3874 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3875 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3876 return true;
3879 return false;
3882 /* Process all regs in location *LOC and change them on equivalent
3883 substitution. Return true if any change was done. */
3884 static bool
3885 loc_equivalence_change_p (rtx *loc)
3887 rtx subst, reg, x = *loc;
3888 bool result = false;
3889 enum rtx_code code = GET_CODE (x);
3890 const char *fmt;
3891 int i, j;
3893 if (code == SUBREG)
3895 reg = SUBREG_REG (x);
3896 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
3897 && GET_MODE (subst) == VOIDmode)
3899 /* We cannot reload debug location. Simplify subreg here
3900 while we know the inner mode. */
3901 *loc = simplify_gen_subreg (GET_MODE (x), subst,
3902 GET_MODE (reg), SUBREG_BYTE (x));
3903 return true;
3906 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
3908 *loc = subst;
3909 return true;
3912 /* Scan all the operand sub-expressions. */
3913 fmt = GET_RTX_FORMAT (code);
3914 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3916 if (fmt[i] == 'e')
3917 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
3918 else if (fmt[i] == 'E')
3919 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3920 result
3921 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
3923 return result;
3926 /* Similar to loc_equivalence_change_p, but for use as
3927 simplify_replace_fn_rtx callback. DATA is insn for which the
3928 elimination is done. If it null we don't do the elimination. */
3929 static rtx
3930 loc_equivalence_callback (rtx loc, const_rtx, void *data)
3932 if (!REG_P (loc))
3933 return NULL_RTX;
3935 rtx subst = (data == NULL
3936 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx) data));
3937 if (subst != loc)
3938 return subst;
3940 return NULL_RTX;
3943 /* Maximum number of generated reload insns per an insn. It is for
3944 preventing this pass cycling in a bug case. */
3945 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
3947 /* The current iteration number of this LRA pass. */
3948 int lra_constraint_iter;
3950 /* True if we substituted equiv which needs checking register
3951 allocation correctness because the equivalent value contains
3952 allocatable hard registers or when we restore multi-register
3953 pseudo. */
3954 bool lra_risky_transformations_p;
3956 /* Return true if REGNO is referenced in more than one block. */
3957 static bool
3958 multi_block_pseudo_p (int regno)
3960 basic_block bb = NULL;
3961 unsigned int uid;
3962 bitmap_iterator bi;
3964 if (regno < FIRST_PSEUDO_REGISTER)
3965 return false;
3967 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
3968 if (bb == NULL)
3969 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
3970 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
3971 return true;
3972 return false;
3975 /* Return true if LIST contains a deleted insn. */
3976 static bool
3977 contains_deleted_insn_p (rtx list)
3979 for (; list != NULL_RTX; list = XEXP (list, 1))
3980 if (NOTE_P (XEXP (list, 0))
3981 && NOTE_KIND (XEXP (list, 0)) == NOTE_INSN_DELETED)
3982 return true;
3983 return false;
3986 /* Return true if X contains a pseudo dying in INSN. */
3987 static bool
3988 dead_pseudo_p (rtx x, rtx insn)
3990 int i, j;
3991 const char *fmt;
3992 enum rtx_code code;
3994 if (REG_P (x))
3995 return (insn != NULL_RTX
3996 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
3997 code = GET_CODE (x);
3998 fmt = GET_RTX_FORMAT (code);
3999 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4001 if (fmt[i] == 'e')
4003 if (dead_pseudo_p (XEXP (x, i), insn))
4004 return true;
4006 else if (fmt[i] == 'E')
4008 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4009 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4010 return true;
4013 return false;
4016 /* Return true if INSN contains a dying pseudo in INSN right hand
4017 side. */
4018 static bool
4019 insn_rhs_dead_pseudo_p (rtx insn)
4021 rtx set = single_set (insn);
4023 gcc_assert (set != NULL);
4024 return dead_pseudo_p (SET_SRC (set), insn);
4027 /* Return true if any init insn of REGNO contains a dying pseudo in
4028 insn right hand side. */
4029 static bool
4030 init_insn_rhs_dead_pseudo_p (int regno)
4032 rtx insns = ira_reg_equiv[regno].init_insns;
4034 if (insns == NULL)
4035 return false;
4036 if (INSN_P (insns))
4037 return insn_rhs_dead_pseudo_p (insns);
4038 for (; insns != NULL_RTX; insns = XEXP (insns, 1))
4039 if (insn_rhs_dead_pseudo_p (XEXP (insns, 0)))
4040 return true;
4041 return false;
4044 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4045 reverse only if we have one init insn with given REGNO as a
4046 source. */
4047 static bool
4048 reverse_equiv_p (int regno)
4050 rtx insns, set;
4052 if ((insns = ira_reg_equiv[regno].init_insns) == NULL_RTX)
4053 return false;
4054 if (! INSN_P (XEXP (insns, 0))
4055 || XEXP (insns, 1) != NULL_RTX)
4056 return false;
4057 if ((set = single_set (XEXP (insns, 0))) == NULL_RTX)
4058 return false;
4059 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4062 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4063 call this function only for non-reverse equivalence. */
4064 static bool
4065 contains_reloaded_insn_p (int regno)
4067 rtx set;
4068 rtx list = ira_reg_equiv[regno].init_insns;
4070 for (; list != NULL_RTX; list = XEXP (list, 1))
4071 if ((set = single_set (XEXP (list, 0))) == NULL_RTX
4072 || ! REG_P (SET_DEST (set))
4073 || (int) REGNO (SET_DEST (set)) != regno)
4074 return true;
4075 return false;
4078 /* Entry function of LRA constraint pass. Return true if the
4079 constraint pass did change the code. */
4080 bool
4081 lra_constraints (bool first_p)
4083 bool changed_p;
4084 int i, hard_regno, new_insns_num;
4085 unsigned int min_len, new_min_len, uid;
4086 rtx set, x, reg, dest_reg;
4087 basic_block last_bb;
4088 bitmap_head equiv_insn_bitmap;
4089 bitmap_iterator bi;
4091 lra_constraint_iter++;
4092 if (lra_dump_file != NULL)
4093 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4094 lra_constraint_iter);
4095 changed_p = false;
4096 lra_risky_transformations_p = false;
4097 new_insn_uid_start = get_max_uid ();
4098 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4099 /* Mark used hard regs for target stack size calulations. */
4100 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4101 if (lra_reg_info[i].nrefs != 0
4102 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4104 int j, nregs;
4106 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4107 for (j = 0; j < nregs; j++)
4108 df_set_regs_ever_live (hard_regno + j, true);
4110 /* Do elimination before the equivalence processing as we can spill
4111 some pseudos during elimination. */
4112 lra_eliminate (false, first_p);
4113 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4114 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4115 if (lra_reg_info[i].nrefs != 0)
4117 ira_reg_equiv[i].profitable_p = true;
4118 reg = regno_reg_rtx[i];
4119 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4121 bool pseudo_p = contains_reg_p (x, false, false);
4123 /* After RTL transformation, we can not guarantee that
4124 pseudo in the substitution was not reloaded which might
4125 make equivalence invalid. For example, in reverse
4126 equiv of p0
4128 p0 <- ...
4130 equiv_mem <- p0
4132 the memory address register was reloaded before the 2nd
4133 insn. */
4134 if ((! first_p && pseudo_p)
4135 /* We don't use DF for compilation speed sake. So it
4136 is problematic to update live info when we use an
4137 equivalence containing pseudos in more than one
4138 BB. */
4139 || (pseudo_p && multi_block_pseudo_p (i))
4140 /* If an init insn was deleted for some reason, cancel
4141 the equiv. We could update the equiv insns after
4142 transformations including an equiv insn deletion
4143 but it is not worthy as such cases are extremely
4144 rare. */
4145 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4146 /* If it is not a reverse equivalence, we check that a
4147 pseudo in rhs of the init insn is not dying in the
4148 insn. Otherwise, the live info at the beginning of
4149 the corresponding BB might be wrong after we
4150 removed the insn. When the equiv can be a
4151 constant, the right hand side of the init insn can
4152 be a pseudo. */
4153 || (! reverse_equiv_p (i)
4154 && (init_insn_rhs_dead_pseudo_p (i)
4155 /* If we reloaded the pseudo in an equivalence
4156 init insn, we can not remove the equiv init
4157 insns and the init insns might write into
4158 const memory in this case. */
4159 || contains_reloaded_insn_p (i)))
4160 /* Prevent access beyond equivalent memory for
4161 paradoxical subregs. */
4162 || (MEM_P (x)
4163 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4164 > GET_MODE_SIZE (GET_MODE (x)))))
4165 ira_reg_equiv[i].defined_p = false;
4166 if (contains_reg_p (x, false, true))
4167 ira_reg_equiv[i].profitable_p = false;
4168 if (get_equiv (reg) != reg)
4169 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4172 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4173 update_equiv (i);
4174 /* We should add all insns containing pseudos which should be
4175 substituted by their equivalences. */
4176 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4177 lra_push_insn_by_uid (uid);
4178 min_len = lra_insn_stack_length ();
4179 new_insns_num = 0;
4180 last_bb = NULL;
4181 changed_p = false;
4182 while ((new_min_len = lra_insn_stack_length ()) != 0)
4184 curr_insn = lra_pop_insn ();
4185 --new_min_len;
4186 curr_bb = BLOCK_FOR_INSN (curr_insn);
4187 if (curr_bb != last_bb)
4189 last_bb = curr_bb;
4190 bb_reload_num = lra_curr_reload_num;
4192 if (min_len > new_min_len)
4194 min_len = new_min_len;
4195 new_insns_num = 0;
4197 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4198 internal_error
4199 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4200 MAX_RELOAD_INSNS_NUMBER);
4201 new_insns_num++;
4202 if (DEBUG_INSN_P (curr_insn))
4204 /* We need to check equivalence in debug insn and change
4205 pseudo to the equivalent value if necessary. */
4206 curr_id = lra_get_insn_recog_data (curr_insn);
4207 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4209 rtx old = *curr_id->operand_loc[0];
4210 *curr_id->operand_loc[0]
4211 = simplify_replace_fn_rtx (old, NULL_RTX,
4212 loc_equivalence_callback, curr_insn);
4213 if (old != *curr_id->operand_loc[0])
4215 lra_update_insn_regno_info (curr_insn);
4216 changed_p = true;
4220 else if (INSN_P (curr_insn))
4222 if ((set = single_set (curr_insn)) != NULL_RTX)
4224 dest_reg = SET_DEST (set);
4225 /* The equivalence pseudo could be set up as SUBREG in a
4226 case when it is a call restore insn in a mode
4227 different from the pseudo mode. */
4228 if (GET_CODE (dest_reg) == SUBREG)
4229 dest_reg = SUBREG_REG (dest_reg);
4230 if ((REG_P (dest_reg)
4231 && (x = get_equiv (dest_reg)) != dest_reg
4232 /* Remove insns which set up a pseudo whose value
4233 can not be changed. Such insns might be not in
4234 init_insns because we don't update equiv data
4235 during insn transformations.
4237 As an example, let suppose that a pseudo got
4238 hard register and on the 1st pass was not
4239 changed to equivalent constant. We generate an
4240 additional insn setting up the pseudo because of
4241 secondary memory movement. Then the pseudo is
4242 spilled and we use the equiv constant. In this
4243 case we should remove the additional insn and
4244 this insn is not init_insns list. */
4245 && (! MEM_P (x) || MEM_READONLY_P (x)
4246 /* Check that this is actually an insn setting
4247 up the equivalence. */
4248 || in_list_p (curr_insn,
4249 ira_reg_equiv
4250 [REGNO (dest_reg)].init_insns)))
4251 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4252 && in_list_p (curr_insn,
4253 ira_reg_equiv
4254 [REGNO (SET_SRC (set))].init_insns)))
4256 /* This is equiv init insn of pseudo which did not get a
4257 hard register -- remove the insn. */
4258 if (lra_dump_file != NULL)
4260 fprintf (lra_dump_file,
4261 " Removing equiv init insn %i (freq=%d)\n",
4262 INSN_UID (curr_insn),
4263 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4264 dump_insn_slim (lra_dump_file, curr_insn);
4266 if (contains_reg_p (x, true, false))
4267 lra_risky_transformations_p = true;
4268 lra_set_insn_deleted (curr_insn);
4269 continue;
4272 curr_id = lra_get_insn_recog_data (curr_insn);
4273 curr_static_id = curr_id->insn_static_data;
4274 init_curr_insn_input_reloads ();
4275 init_curr_operand_mode ();
4276 if (curr_insn_transform ())
4277 changed_p = true;
4278 /* Check non-transformed insns too for equiv change as USE
4279 or CLOBBER don't need reloads but can contain pseudos
4280 being changed on their equivalences. */
4281 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4282 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4284 lra_update_insn_regno_info (curr_insn);
4285 changed_p = true;
4289 bitmap_clear (&equiv_insn_bitmap);
4290 /* If we used a new hard regno, changed_p should be true because the
4291 hard reg is assigned to a new pseudo. */
4292 #ifdef ENABLE_CHECKING
4293 if (! changed_p)
4295 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4296 if (lra_reg_info[i].nrefs != 0
4297 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4299 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4301 for (j = 0; j < nregs; j++)
4302 lra_assert (df_regs_ever_live_p (hard_regno + j));
4305 #endif
4306 return changed_p;
4309 /* Initiate the LRA constraint pass. It is done once per
4310 function. */
4311 void
4312 lra_constraints_init (void)
4316 /* Finalize the LRA constraint pass. It is done once per
4317 function. */
4318 void
4319 lra_constraints_finish (void)
4325 /* This page contains code to do inheritance/split
4326 transformations. */
4328 /* Number of reloads passed so far in current EBB. */
4329 static int reloads_num;
4331 /* Number of calls passed so far in current EBB. */
4332 static int calls_num;
4334 /* Current reload pseudo check for validity of elements in
4335 USAGE_INSNS. */
4336 static int curr_usage_insns_check;
4338 /* Info about last usage of registers in EBB to do inheritance/split
4339 transformation. Inheritance transformation is done from a spilled
4340 pseudo and split transformations from a hard register or a pseudo
4341 assigned to a hard register. */
4342 struct usage_insns
4344 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4345 value INSNS is valid. The insns is chain of optional debug insns
4346 and a finishing non-debug insn using the corresponding reg. The
4347 value is also used to mark the registers which are set up in the
4348 current insn. The negated insn uid is used for this. */
4349 int check;
4350 /* Value of global reloads_num at the last insn in INSNS. */
4351 int reloads_num;
4352 /* Value of global reloads_nums at the last insn in INSNS. */
4353 int calls_num;
4354 /* It can be true only for splitting. And it means that the restore
4355 insn should be put after insn given by the following member. */
4356 bool after_p;
4357 /* Next insns in the current EBB which use the original reg and the
4358 original reg value is not changed between the current insn and
4359 the next insns. In order words, e.g. for inheritance, if we need
4360 to use the original reg value again in the next insns we can try
4361 to use the value in a hard register from a reload insn of the
4362 current insn. */
4363 rtx insns;
4366 /* Map: regno -> corresponding pseudo usage insns. */
4367 static struct usage_insns *usage_insns;
4369 static void
4370 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4372 usage_insns[regno].check = curr_usage_insns_check;
4373 usage_insns[regno].insns = insn;
4374 usage_insns[regno].reloads_num = reloads_num;
4375 usage_insns[regno].calls_num = calls_num;
4376 usage_insns[regno].after_p = after_p;
4379 /* The function is used to form list REGNO usages which consists of
4380 optional debug insns finished by a non-debug insn using REGNO.
4381 RELOADS_NUM is current number of reload insns processed so far. */
4382 static void
4383 add_next_usage_insn (int regno, rtx insn, int reloads_num)
4385 rtx next_usage_insns;
4387 if (usage_insns[regno].check == curr_usage_insns_check
4388 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4389 && DEBUG_INSN_P (insn))
4391 /* Check that we did not add the debug insn yet. */
4392 if (next_usage_insns != insn
4393 && (GET_CODE (next_usage_insns) != INSN_LIST
4394 || XEXP (next_usage_insns, 0) != insn))
4395 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4396 next_usage_insns);
4398 else if (NONDEBUG_INSN_P (insn))
4399 setup_next_usage_insn (regno, insn, reloads_num, false);
4400 else
4401 usage_insns[regno].check = 0;
4404 /* Replace all references to register OLD_REGNO in *LOC with pseudo
4405 register NEW_REG. Return true if any change was made. */
4406 static bool
4407 substitute_pseudo (rtx *loc, int old_regno, rtx new_reg)
4409 rtx x = *loc;
4410 bool result = false;
4411 enum rtx_code code;
4412 const char *fmt;
4413 int i, j;
4415 if (x == NULL_RTX)
4416 return false;
4418 code = GET_CODE (x);
4419 if (code == REG && (int) REGNO (x) == old_regno)
4421 enum machine_mode mode = GET_MODE (*loc);
4422 enum machine_mode inner_mode = GET_MODE (new_reg);
4424 if (mode != inner_mode)
4426 if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
4427 || ! SCALAR_INT_MODE_P (inner_mode))
4428 new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
4429 else
4430 new_reg = gen_lowpart_SUBREG (mode, new_reg);
4432 *loc = new_reg;
4433 return true;
4436 /* Scan all the operand sub-expressions. */
4437 fmt = GET_RTX_FORMAT (code);
4438 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4440 if (fmt[i] == 'e')
4442 if (substitute_pseudo (&XEXP (x, i), old_regno, new_reg))
4443 result = true;
4445 else if (fmt[i] == 'E')
4447 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4448 if (substitute_pseudo (&XVECEXP (x, i, j), old_regno, new_reg))
4449 result = true;
4452 return result;
4455 /* Return first non-debug insn in list USAGE_INSNS. */
4456 static rtx
4457 skip_usage_debug_insns (rtx usage_insns)
4459 rtx insn;
4461 /* Skip debug insns. */
4462 for (insn = usage_insns;
4463 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4464 insn = XEXP (insn, 1))
4466 return insn;
4469 /* Return true if we need secondary memory moves for insn in
4470 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4471 into the insn. */
4472 static bool
4473 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4474 rtx usage_insns ATTRIBUTE_UNUSED)
4476 #ifndef SECONDARY_MEMORY_NEEDED
4477 return false;
4478 #else
4479 rtx insn, set, dest;
4480 enum reg_class cl;
4482 if (inher_cl == ALL_REGS
4483 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4484 return false;
4485 lra_assert (INSN_P (insn));
4486 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4487 return false;
4488 dest = SET_DEST (set);
4489 if (! REG_P (dest))
4490 return false;
4491 lra_assert (inher_cl != NO_REGS);
4492 cl = get_reg_class (REGNO (dest));
4493 return (cl != NO_REGS && cl != ALL_REGS
4494 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4495 #endif
4498 /* Registers involved in inheritance/split in the current EBB
4499 (inheritance/split pseudos and original registers). */
4500 static bitmap_head check_only_regs;
4502 /* Do inheritance transformations for insn INSN, which defines (if
4503 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4504 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4505 form as the "insns" field of usage_insns. Return true if we
4506 succeed in such transformation.
4508 The transformations look like:
4510 p <- ... i <- ...
4511 ... p <- i (new insn)
4512 ... =>
4513 <- ... p ... <- ... i ...
4515 ... i <- p (new insn)
4516 <- ... p ... <- ... i ...
4517 ... =>
4518 <- ... p ... <- ... i ...
4519 where p is a spilled original pseudo and i is a new inheritance pseudo.
4522 The inheritance pseudo has the smallest class of two classes CL and
4523 class of ORIGINAL REGNO. */
4524 static bool
4525 inherit_reload_reg (bool def_p, int original_regno,
4526 enum reg_class cl, rtx insn, rtx next_usage_insns)
4528 if (optimize_function_for_size_p (cfun))
4529 return false;
4531 enum reg_class rclass = lra_get_allocno_class (original_regno);
4532 rtx original_reg = regno_reg_rtx[original_regno];
4533 rtx new_reg, new_insns, usage_insn;
4535 lra_assert (! usage_insns[original_regno].after_p);
4536 if (lra_dump_file != NULL)
4537 fprintf (lra_dump_file,
4538 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4539 if (! ira_reg_classes_intersect_p[cl][rclass])
4541 if (lra_dump_file != NULL)
4543 fprintf (lra_dump_file,
4544 " Rejecting inheritance for %d "
4545 "because of disjoint classes %s and %s\n",
4546 original_regno, reg_class_names[cl],
4547 reg_class_names[rclass]);
4548 fprintf (lra_dump_file,
4549 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4551 return false;
4553 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4554 /* We don't use a subset of two classes because it can be
4555 NO_REGS. This transformation is still profitable in most
4556 cases even if the classes are not intersected as register
4557 move is probably cheaper than a memory load. */
4558 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4560 if (lra_dump_file != NULL)
4561 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4562 reg_class_names[cl], reg_class_names[rclass]);
4564 rclass = cl;
4566 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4568 /* Reject inheritance resulting in secondary memory moves.
4569 Otherwise, there is a danger in LRA cycling. Also such
4570 transformation will be unprofitable. */
4571 if (lra_dump_file != NULL)
4573 rtx insn = skip_usage_debug_insns (next_usage_insns);
4574 rtx set = single_set (insn);
4576 lra_assert (set != NULL_RTX);
4578 rtx dest = SET_DEST (set);
4580 lra_assert (REG_P (dest));
4581 fprintf (lra_dump_file,
4582 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4583 "as secondary mem is needed\n",
4584 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4585 original_regno, reg_class_names[rclass]);
4586 fprintf (lra_dump_file,
4587 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4589 return false;
4591 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4592 rclass, "inheritance");
4593 start_sequence ();
4594 if (def_p)
4595 lra_emit_move (original_reg, new_reg);
4596 else
4597 lra_emit_move (new_reg, original_reg);
4598 new_insns = get_insns ();
4599 end_sequence ();
4600 if (NEXT_INSN (new_insns) != NULL_RTX)
4602 if (lra_dump_file != NULL)
4604 fprintf (lra_dump_file,
4605 " Rejecting inheritance %d->%d "
4606 "as it results in 2 or more insns:\n",
4607 original_regno, REGNO (new_reg));
4608 dump_rtl_slim (lra_dump_file, new_insns, NULL_RTX, -1, 0);
4609 fprintf (lra_dump_file,
4610 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4612 return false;
4614 substitute_pseudo (&insn, original_regno, new_reg);
4615 lra_update_insn_regno_info (insn);
4616 if (! def_p)
4617 /* We now have a new usage insn for original regno. */
4618 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4619 if (lra_dump_file != NULL)
4620 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4621 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4622 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4623 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4624 bitmap_set_bit (&check_only_regs, original_regno);
4625 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4626 if (def_p)
4627 lra_process_new_insns (insn, NULL_RTX, new_insns,
4628 "Add original<-inheritance");
4629 else
4630 lra_process_new_insns (insn, new_insns, NULL_RTX,
4631 "Add inheritance<-original");
4632 while (next_usage_insns != NULL_RTX)
4634 if (GET_CODE (next_usage_insns) != INSN_LIST)
4636 usage_insn = next_usage_insns;
4637 lra_assert (NONDEBUG_INSN_P (usage_insn));
4638 next_usage_insns = NULL;
4640 else
4642 usage_insn = XEXP (next_usage_insns, 0);
4643 lra_assert (DEBUG_INSN_P (usage_insn));
4644 next_usage_insns = XEXP (next_usage_insns, 1);
4646 substitute_pseudo (&usage_insn, original_regno, new_reg);
4647 lra_update_insn_regno_info (usage_insn);
4648 if (lra_dump_file != NULL)
4650 fprintf (lra_dump_file,
4651 " Inheritance reuse change %d->%d (bb%d):\n",
4652 original_regno, REGNO (new_reg),
4653 BLOCK_FOR_INSN (usage_insn)->index);
4654 dump_insn_slim (lra_dump_file, usage_insn);
4657 if (lra_dump_file != NULL)
4658 fprintf (lra_dump_file,
4659 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4660 return true;
4663 /* Return true if we need a caller save/restore for pseudo REGNO which
4664 was assigned to a hard register. */
4665 static inline bool
4666 need_for_call_save_p (int regno)
4668 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4669 return (usage_insns[regno].calls_num < calls_num
4670 && (overlaps_hard_reg_set_p
4671 (call_used_reg_set,
4672 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4673 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4674 PSEUDO_REGNO_MODE (regno))));
4677 /* Global registers occurring in the current EBB. */
4678 static bitmap_head ebb_global_regs;
4680 /* Return true if we need a split for hard register REGNO or pseudo
4681 REGNO which was assigned to a hard register.
4682 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4683 used for reloads since the EBB end. It is an approximation of the
4684 used hard registers in the split range. The exact value would
4685 require expensive calculations. If we were aggressive with
4686 splitting because of the approximation, the split pseudo will save
4687 the same hard register assignment and will be removed in the undo
4688 pass. We still need the approximation because too aggressive
4689 splitting would result in too inaccurate cost calculation in the
4690 assignment pass because of too many generated moves which will be
4691 probably removed in the undo pass. */
4692 static inline bool
4693 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4695 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4697 lra_assert (hard_regno >= 0);
4698 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4699 /* Don't split eliminable hard registers, otherwise we can
4700 split hard registers like hard frame pointer, which
4701 lives on BB start/end according to DF-infrastructure,
4702 when there is a pseudo assigned to the register and
4703 living in the same BB. */
4704 && (regno >= FIRST_PSEUDO_REGISTER
4705 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4706 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4707 /* Don't split call clobbered hard regs living through
4708 calls, otherwise we might have a check problem in the
4709 assign sub-pass as in the most cases (exception is a
4710 situation when lra_risky_transformations_p value is
4711 true) the assign pass assumes that all pseudos living
4712 through calls are assigned to call saved hard regs. */
4713 && (regno >= FIRST_PSEUDO_REGISTER
4714 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4715 || usage_insns[regno].calls_num == calls_num)
4716 /* We need at least 2 reloads to make pseudo splitting
4717 profitable. We should provide hard regno splitting in
4718 any case to solve 1st insn scheduling problem when
4719 moving hard register definition up might result in
4720 impossibility to find hard register for reload pseudo of
4721 small register class. */
4722 && (usage_insns[regno].reloads_num
4723 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4724 && (regno < FIRST_PSEUDO_REGISTER
4725 /* For short living pseudos, spilling + inheritance can
4726 be considered a substitution for splitting.
4727 Therefore we do not splitting for local pseudos. It
4728 decreases also aggressiveness of splitting. The
4729 minimal number of references is chosen taking into
4730 account that for 2 references splitting has no sense
4731 as we can just spill the pseudo. */
4732 || (regno >= FIRST_PSEUDO_REGISTER
4733 && lra_reg_info[regno].nrefs > 3
4734 && bitmap_bit_p (&ebb_global_regs, regno))))
4735 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4738 /* Return class for the split pseudo created from original pseudo with
4739 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4740 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4741 results in no secondary memory movements. */
4742 static enum reg_class
4743 choose_split_class (enum reg_class allocno_class,
4744 int hard_regno ATTRIBUTE_UNUSED,
4745 enum machine_mode mode ATTRIBUTE_UNUSED)
4747 #ifndef SECONDARY_MEMORY_NEEDED
4748 return allocno_class;
4749 #else
4750 int i;
4751 enum reg_class cl, best_cl = NO_REGS;
4752 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4753 = REGNO_REG_CLASS (hard_regno);
4755 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4756 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4757 return allocno_class;
4758 for (i = 0;
4759 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4760 i++)
4761 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4762 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4763 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4764 && (best_cl == NO_REGS
4765 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4766 best_cl = cl;
4767 return best_cl;
4768 #endif
4771 /* Do split transformations for insn INSN, which defines or uses
4772 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4773 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4774 "insns" field of usage_insns.
4776 The transformations look like:
4778 p <- ... p <- ...
4779 ... s <- p (new insn -- save)
4780 ... =>
4781 ... p <- s (new insn -- restore)
4782 <- ... p ... <- ... p ...
4784 <- ... p ... <- ... p ...
4785 ... s <- p (new insn -- save)
4786 ... =>
4787 ... p <- s (new insn -- restore)
4788 <- ... p ... <- ... p ...
4790 where p is an original pseudo got a hard register or a hard
4791 register and s is a new split pseudo. The save is put before INSN
4792 if BEFORE_P is true. Return true if we succeed in such
4793 transformation. */
4794 static bool
4795 split_reg (bool before_p, int original_regno, rtx insn, rtx next_usage_insns)
4797 enum reg_class rclass;
4798 rtx original_reg;
4799 int hard_regno, nregs;
4800 rtx new_reg, save, restore, usage_insn;
4801 bool after_p;
4802 bool call_save_p;
4804 if (original_regno < FIRST_PSEUDO_REGISTER)
4806 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4807 hard_regno = original_regno;
4808 call_save_p = false;
4809 nregs = 1;
4811 else
4813 hard_regno = reg_renumber[original_regno];
4814 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4815 rclass = lra_get_allocno_class (original_regno);
4816 original_reg = regno_reg_rtx[original_regno];
4817 call_save_p = need_for_call_save_p (original_regno);
4819 original_reg = regno_reg_rtx[original_regno];
4820 lra_assert (hard_regno >= 0);
4821 if (lra_dump_file != NULL)
4822 fprintf (lra_dump_file,
4823 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4824 if (call_save_p)
4826 enum machine_mode mode = GET_MODE (original_reg);
4828 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4829 hard_regno_nregs[hard_regno][mode],
4830 mode);
4831 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4833 else
4835 rclass = choose_split_class (rclass, hard_regno,
4836 GET_MODE (original_reg));
4837 if (rclass == NO_REGS)
4839 if (lra_dump_file != NULL)
4841 fprintf (lra_dump_file,
4842 " Rejecting split of %d(%s): "
4843 "no good reg class for %d(%s)\n",
4844 original_regno,
4845 reg_class_names[lra_get_allocno_class (original_regno)],
4846 hard_regno,
4847 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4848 fprintf
4849 (lra_dump_file,
4850 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4852 return false;
4854 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4855 rclass, "split");
4856 reg_renumber[REGNO (new_reg)] = hard_regno;
4858 save = emit_spill_move (true, new_reg, original_reg);
4859 if (NEXT_INSN (save) != NULL_RTX)
4861 lra_assert (! call_save_p);
4862 if (lra_dump_file != NULL)
4864 fprintf
4865 (lra_dump_file,
4866 " Rejecting split %d->%d resulting in > 2 %s save insns:\n",
4867 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4868 dump_rtl_slim (lra_dump_file, save, NULL_RTX, -1, 0);
4869 fprintf (lra_dump_file,
4870 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4872 return false;
4874 restore = emit_spill_move (false, new_reg, original_reg);
4875 if (NEXT_INSN (restore) != NULL_RTX)
4877 lra_assert (! call_save_p);
4878 if (lra_dump_file != NULL)
4880 fprintf (lra_dump_file,
4881 " Rejecting split %d->%d "
4882 "resulting in > 2 %s restore insns:\n",
4883 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4884 dump_rtl_slim (lra_dump_file, restore, NULL_RTX, -1, 0);
4885 fprintf (lra_dump_file,
4886 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4888 return false;
4890 after_p = usage_insns[original_regno].after_p;
4891 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4892 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4893 bitmap_set_bit (&check_only_regs, original_regno);
4894 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
4895 for (;;)
4897 if (GET_CODE (next_usage_insns) != INSN_LIST)
4899 usage_insn = next_usage_insns;
4900 break;
4902 usage_insn = XEXP (next_usage_insns, 0);
4903 lra_assert (DEBUG_INSN_P (usage_insn));
4904 next_usage_insns = XEXP (next_usage_insns, 1);
4905 substitute_pseudo (&usage_insn, original_regno, new_reg);
4906 lra_update_insn_regno_info (usage_insn);
4907 if (lra_dump_file != NULL)
4909 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
4910 original_regno, REGNO (new_reg));
4911 dump_insn_slim (lra_dump_file, usage_insn);
4914 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
4915 lra_assert (usage_insn != insn || (after_p && before_p));
4916 lra_process_new_insns (usage_insn, after_p ? NULL_RTX : restore,
4917 after_p ? restore : NULL_RTX,
4918 call_save_p
4919 ? "Add reg<-save" : "Add reg<-split");
4920 lra_process_new_insns (insn, before_p ? save : NULL_RTX,
4921 before_p ? NULL_RTX : save,
4922 call_save_p
4923 ? "Add save<-reg" : "Add split<-reg");
4924 if (nregs > 1)
4925 /* If we are trying to split multi-register. We should check
4926 conflicts on the next assignment sub-pass. IRA can allocate on
4927 sub-register levels, LRA do this on pseudos level right now and
4928 this discrepancy may create allocation conflicts after
4929 splitting. */
4930 lra_risky_transformations_p = true;
4931 if (lra_dump_file != NULL)
4932 fprintf (lra_dump_file,
4933 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4934 return true;
4937 /* Recognize that we need a split transformation for insn INSN, which
4938 defines or uses REGNO in its insn biggest MODE (we use it only if
4939 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
4940 hard registers which might be used for reloads since the EBB end.
4941 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
4942 uid before starting INSN processing. Return true if we succeed in
4943 such transformation. */
4944 static bool
4945 split_if_necessary (int regno, enum machine_mode mode,
4946 HARD_REG_SET potential_reload_hard_regs,
4947 bool before_p, rtx insn, int max_uid)
4949 bool res = false;
4950 int i, nregs = 1;
4951 rtx next_usage_insns;
4953 if (regno < FIRST_PSEUDO_REGISTER)
4954 nregs = hard_regno_nregs[regno][mode];
4955 for (i = 0; i < nregs; i++)
4956 if (usage_insns[regno + i].check == curr_usage_insns_check
4957 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
4958 /* To avoid processing the register twice or more. */
4959 && ((GET_CODE (next_usage_insns) != INSN_LIST
4960 && INSN_UID (next_usage_insns) < max_uid)
4961 || (GET_CODE (next_usage_insns) == INSN_LIST
4962 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
4963 && need_for_split_p (potential_reload_hard_regs, regno + i)
4964 && split_reg (before_p, regno + i, insn, next_usage_insns))
4965 res = true;
4966 return res;
4969 /* Check only registers living at the current program point in the
4970 current EBB. */
4971 static bitmap_head live_regs;
4973 /* Update live info in EBB given by its HEAD and TAIL insns after
4974 inheritance/split transformation. The function removes dead moves
4975 too. */
4976 static void
4977 update_ebb_live_info (rtx head, rtx tail)
4979 unsigned int j;
4980 int i, regno;
4981 bool live_p;
4982 rtx prev_insn, set;
4983 bool remove_p;
4984 basic_block last_bb, prev_bb, curr_bb;
4985 bitmap_iterator bi;
4986 struct lra_insn_reg *reg;
4987 edge e;
4988 edge_iterator ei;
4990 last_bb = BLOCK_FOR_INSN (tail);
4991 prev_bb = NULL;
4992 for (curr_insn = tail;
4993 curr_insn != PREV_INSN (head);
4994 curr_insn = prev_insn)
4996 prev_insn = PREV_INSN (curr_insn);
4997 /* We need to process empty blocks too. They contain
4998 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
4999 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5000 continue;
5001 curr_bb = BLOCK_FOR_INSN (curr_insn);
5002 if (curr_bb != prev_bb)
5004 if (prev_bb != NULL)
5006 /* Update df_get_live_in (prev_bb): */
5007 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5008 if (bitmap_bit_p (&live_regs, j))
5009 bitmap_set_bit (df_get_live_in (prev_bb), j);
5010 else
5011 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5013 if (curr_bb != last_bb)
5015 /* Update df_get_live_out (curr_bb): */
5016 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5018 live_p = bitmap_bit_p (&live_regs, j);
5019 if (! live_p)
5020 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5021 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5023 live_p = true;
5024 break;
5026 if (live_p)
5027 bitmap_set_bit (df_get_live_out (curr_bb), j);
5028 else
5029 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5032 prev_bb = curr_bb;
5033 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5035 if (! NONDEBUG_INSN_P (curr_insn))
5036 continue;
5037 curr_id = lra_get_insn_recog_data (curr_insn);
5038 curr_static_id = curr_id->insn_static_data;
5039 remove_p = false;
5040 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
5041 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5042 && bitmap_bit_p (&check_only_regs, regno)
5043 && ! bitmap_bit_p (&live_regs, regno))
5044 remove_p = true;
5045 /* See which defined values die here. */
5046 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5047 if (reg->type == OP_OUT && ! reg->subreg_p)
5048 bitmap_clear_bit (&live_regs, reg->regno);
5049 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5050 if (reg->type == OP_OUT && ! reg->subreg_p)
5051 bitmap_clear_bit (&live_regs, reg->regno);
5052 /* Mark each used value as live. */
5053 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5054 if (reg->type != OP_OUT
5055 && bitmap_bit_p (&check_only_regs, reg->regno))
5056 bitmap_set_bit (&live_regs, reg->regno);
5057 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5058 if (reg->type != OP_OUT
5059 && bitmap_bit_p (&check_only_regs, reg->regno))
5060 bitmap_set_bit (&live_regs, reg->regno);
5061 if (curr_id->arg_hard_regs != NULL)
5062 /* Make argument hard registers live. */
5063 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5064 if (bitmap_bit_p (&check_only_regs, regno))
5065 bitmap_set_bit (&live_regs, regno);
5066 /* It is quite important to remove dead move insns because it
5067 means removing dead store. We don't need to process them for
5068 constraints. */
5069 if (remove_p)
5071 if (lra_dump_file != NULL)
5073 fprintf (lra_dump_file, " Removing dead insn:\n ");
5074 dump_insn_slim (lra_dump_file, curr_insn);
5076 lra_set_insn_deleted (curr_insn);
5081 /* The structure describes info to do an inheritance for the current
5082 insn. We need to collect such info first before doing the
5083 transformations because the transformations change the insn
5084 internal representation. */
5085 struct to_inherit
5087 /* Original regno. */
5088 int regno;
5089 /* Subsequent insns which can inherit original reg value. */
5090 rtx insns;
5093 /* Array containing all info for doing inheritance from the current
5094 insn. */
5095 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5097 /* Number elements in the previous array. */
5098 static int to_inherit_num;
5100 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5101 structure to_inherit. */
5102 static void
5103 add_to_inherit (int regno, rtx insns)
5105 int i;
5107 for (i = 0; i < to_inherit_num; i++)
5108 if (to_inherit[i].regno == regno)
5109 return;
5110 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5111 to_inherit[to_inherit_num].regno = regno;
5112 to_inherit[to_inherit_num++].insns = insns;
5115 /* Return the last non-debug insn in basic block BB, or the block begin
5116 note if none. */
5117 static rtx
5118 get_last_insertion_point (basic_block bb)
5120 rtx insn;
5122 FOR_BB_INSNS_REVERSE (bb, insn)
5123 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5124 return insn;
5125 gcc_unreachable ();
5128 /* Set up RES by registers living on edges FROM except the edge (FROM,
5129 TO) or by registers set up in a jump insn in BB FROM. */
5130 static void
5131 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5133 rtx last;
5134 struct lra_insn_reg *reg;
5135 edge e;
5136 edge_iterator ei;
5138 lra_assert (to != NULL);
5139 bitmap_clear (res);
5140 FOR_EACH_EDGE (e, ei, from->succs)
5141 if (e->dest != to)
5142 bitmap_ior_into (res, df_get_live_in (e->dest));
5143 last = get_last_insertion_point (from);
5144 if (! JUMP_P (last))
5145 return;
5146 curr_id = lra_get_insn_recog_data (last);
5147 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5148 if (reg->type != OP_IN)
5149 bitmap_set_bit (res, reg->regno);
5152 /* Used as a temporary results of some bitmap calculations. */
5153 static bitmap_head temp_bitmap;
5155 /* We split for reloads of small class of hard regs. The following
5156 defines how many hard regs the class should have to be qualified as
5157 small. The code is mostly oriented to x86/x86-64 architecture
5158 where some insns need to use only specific register or pair of
5159 registers and these register can live in RTL explicitly, e.g. for
5160 parameter passing. */
5161 static const int max_small_class_regs_num = 2;
5163 /* Do inheritance/split transformations in EBB starting with HEAD and
5164 finishing on TAIL. We process EBB insns in the reverse order.
5165 Return true if we did any inheritance/split transformation in the
5166 EBB.
5168 We should avoid excessive splitting which results in worse code
5169 because of inaccurate cost calculations for spilling new split
5170 pseudos in such case. To achieve this we do splitting only if
5171 register pressure is high in given basic block and there are reload
5172 pseudos requiring hard registers. We could do more register
5173 pressure calculations at any given program point to avoid necessary
5174 splitting even more but it is to expensive and the current approach
5175 works well enough. */
5176 static bool
5177 inherit_in_ebb (rtx head, rtx tail)
5179 int i, src_regno, dst_regno, nregs;
5180 bool change_p, succ_p, update_reloads_num_p;
5181 rtx prev_insn, next_usage_insns, set, last_insn;
5182 enum reg_class cl;
5183 struct lra_insn_reg *reg;
5184 basic_block last_processed_bb, curr_bb = NULL;
5185 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5186 bitmap to_process;
5187 unsigned int j;
5188 bitmap_iterator bi;
5189 bool head_p, after_p;
5191 change_p = false;
5192 curr_usage_insns_check++;
5193 reloads_num = calls_num = 0;
5194 bitmap_clear (&check_only_regs);
5195 last_processed_bb = NULL;
5196 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5197 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5198 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5199 /* We don't process new insns generated in the loop. */
5200 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5202 prev_insn = PREV_INSN (curr_insn);
5203 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5204 curr_bb = BLOCK_FOR_INSN (curr_insn);
5205 if (last_processed_bb != curr_bb)
5207 /* We are at the end of BB. Add qualified living
5208 pseudos for potential splitting. */
5209 to_process = df_get_live_out (curr_bb);
5210 if (last_processed_bb != NULL)
5212 /* We are somewhere in the middle of EBB. */
5213 get_live_on_other_edges (curr_bb, last_processed_bb,
5214 &temp_bitmap);
5215 to_process = &temp_bitmap;
5217 last_processed_bb = curr_bb;
5218 last_insn = get_last_insertion_point (curr_bb);
5219 after_p = (! JUMP_P (last_insn)
5220 && (! CALL_P (last_insn)
5221 || (find_reg_note (last_insn,
5222 REG_NORETURN, NULL_RTX) == NULL_RTX
5223 && ! SIBLING_CALL_P (last_insn))));
5224 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5225 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5227 if ((int) j >= lra_constraint_new_regno_start)
5228 break;
5229 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5231 if (j < FIRST_PSEUDO_REGISTER)
5232 SET_HARD_REG_BIT (live_hard_regs, j);
5233 else
5234 add_to_hard_reg_set (&live_hard_regs,
5235 PSEUDO_REGNO_MODE (j),
5236 reg_renumber[j]);
5237 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5241 src_regno = dst_regno = -1;
5242 if (NONDEBUG_INSN_P (curr_insn)
5243 && (set = single_set (curr_insn)) != NULL_RTX
5244 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5246 src_regno = REGNO (SET_SRC (set));
5247 dst_regno = REGNO (SET_DEST (set));
5249 update_reloads_num_p = true;
5250 if (src_regno < lra_constraint_new_regno_start
5251 && src_regno >= FIRST_PSEUDO_REGISTER
5252 && reg_renumber[src_regno] < 0
5253 && dst_regno >= lra_constraint_new_regno_start
5254 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5256 /* 'reload_pseudo <- original_pseudo'. */
5257 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5258 reloads_num++;
5259 update_reloads_num_p = false;
5260 succ_p = false;
5261 if (usage_insns[src_regno].check == curr_usage_insns_check
5262 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5263 succ_p = inherit_reload_reg (false, src_regno, cl,
5264 curr_insn, next_usage_insns);
5265 if (succ_p)
5266 change_p = true;
5267 else
5268 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5269 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5270 IOR_HARD_REG_SET (potential_reload_hard_regs,
5271 reg_class_contents[cl]);
5273 else if (src_regno >= lra_constraint_new_regno_start
5274 && dst_regno < lra_constraint_new_regno_start
5275 && dst_regno >= FIRST_PSEUDO_REGISTER
5276 && reg_renumber[dst_regno] < 0
5277 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5278 && usage_insns[dst_regno].check == curr_usage_insns_check
5279 && (next_usage_insns
5280 = usage_insns[dst_regno].insns) != NULL_RTX)
5282 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5283 reloads_num++;
5284 update_reloads_num_p = false;
5285 /* 'original_pseudo <- reload_pseudo'. */
5286 if (! JUMP_P (curr_insn)
5287 && inherit_reload_reg (true, dst_regno, cl,
5288 curr_insn, next_usage_insns))
5289 change_p = true;
5290 /* Invalidate. */
5291 usage_insns[dst_regno].check = 0;
5292 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5293 IOR_HARD_REG_SET (potential_reload_hard_regs,
5294 reg_class_contents[cl]);
5296 else if (INSN_P (curr_insn))
5298 int iter;
5299 int max_uid = get_max_uid ();
5301 curr_id = lra_get_insn_recog_data (curr_insn);
5302 curr_static_id = curr_id->insn_static_data;
5303 to_inherit_num = 0;
5304 /* Process insn definitions. */
5305 for (iter = 0; iter < 2; iter++)
5306 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5307 reg != NULL;
5308 reg = reg->next)
5309 if (reg->type != OP_IN
5310 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5312 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5313 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5314 && usage_insns[dst_regno].check == curr_usage_insns_check
5315 && (next_usage_insns
5316 = usage_insns[dst_regno].insns) != NULL_RTX)
5318 struct lra_insn_reg *r;
5320 for (r = curr_id->regs; r != NULL; r = r->next)
5321 if (r->type != OP_OUT && r->regno == dst_regno)
5322 break;
5323 /* Don't do inheritance if the pseudo is also
5324 used in the insn. */
5325 if (r == NULL)
5326 /* We can not do inheritance right now
5327 because the current insn reg info (chain
5328 regs) can change after that. */
5329 add_to_inherit (dst_regno, next_usage_insns);
5331 /* We can not process one reg twice here because of
5332 usage_insns invalidation. */
5333 if ((dst_regno < FIRST_PSEUDO_REGISTER
5334 || reg_renumber[dst_regno] >= 0)
5335 && ! reg->subreg_p && reg->type != OP_IN)
5337 HARD_REG_SET s;
5339 if (split_if_necessary (dst_regno, reg->biggest_mode,
5340 potential_reload_hard_regs,
5341 false, curr_insn, max_uid))
5342 change_p = true;
5343 CLEAR_HARD_REG_SET (s);
5344 if (dst_regno < FIRST_PSEUDO_REGISTER)
5345 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5346 else
5347 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5348 reg_renumber[dst_regno]);
5349 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5351 /* We should invalidate potential inheritance or
5352 splitting for the current insn usages to the next
5353 usage insns (see code below) as the output pseudo
5354 prevents this. */
5355 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5356 && reg_renumber[dst_regno] < 0)
5357 || (reg->type == OP_OUT && ! reg->subreg_p
5358 && (dst_regno < FIRST_PSEUDO_REGISTER
5359 || reg_renumber[dst_regno] >= 0)))
5361 /* Invalidate and mark definitions. */
5362 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5363 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5364 else
5366 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5367 for (i = 0; i < nregs; i++)
5368 usage_insns[dst_regno + i].check
5369 = -(int) INSN_UID (curr_insn);
5373 if (! JUMP_P (curr_insn))
5374 for (i = 0; i < to_inherit_num; i++)
5375 if (inherit_reload_reg (true, to_inherit[i].regno,
5376 ALL_REGS, curr_insn,
5377 to_inherit[i].insns))
5378 change_p = true;
5379 if (CALL_P (curr_insn))
5381 rtx cheap, pat, dest, restore;
5382 int regno, hard_regno;
5384 calls_num++;
5385 if ((cheap = find_reg_note (curr_insn,
5386 REG_RETURNED, NULL_RTX)) != NULL_RTX
5387 && ((cheap = XEXP (cheap, 0)), true)
5388 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5389 && (hard_regno = reg_renumber[regno]) >= 0
5390 /* If there are pending saves/restores, the
5391 optimization is not worth. */
5392 && usage_insns[regno].calls_num == calls_num - 1
5393 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5395 /* Restore the pseudo from the call result as
5396 REG_RETURNED note says that the pseudo value is
5397 in the call result and the pseudo is an argument
5398 of the call. */
5399 pat = PATTERN (curr_insn);
5400 if (GET_CODE (pat) == PARALLEL)
5401 pat = XVECEXP (pat, 0, 0);
5402 dest = SET_DEST (pat);
5403 start_sequence ();
5404 emit_move_insn (cheap, copy_rtx (dest));
5405 restore = get_insns ();
5406 end_sequence ();
5407 lra_process_new_insns (curr_insn, NULL, restore,
5408 "Inserting call parameter restore");
5409 /* We don't need to save/restore of the pseudo from
5410 this call. */
5411 usage_insns[regno].calls_num = calls_num;
5412 bitmap_set_bit (&check_only_regs, regno);
5415 to_inherit_num = 0;
5416 /* Process insn usages. */
5417 for (iter = 0; iter < 2; iter++)
5418 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5419 reg != NULL;
5420 reg = reg->next)
5421 if ((reg->type != OP_OUT
5422 || (reg->type == OP_OUT && reg->subreg_p))
5423 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5425 if (src_regno >= FIRST_PSEUDO_REGISTER
5426 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5428 if (usage_insns[src_regno].check == curr_usage_insns_check
5429 && (next_usage_insns
5430 = usage_insns[src_regno].insns) != NULL_RTX
5431 && NONDEBUG_INSN_P (curr_insn))
5432 add_to_inherit (src_regno, next_usage_insns);
5433 else if (usage_insns[src_regno].check
5434 != -(int) INSN_UID (curr_insn))
5435 /* Add usages but only if the reg is not set up
5436 in the same insn. */
5437 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5439 else if (src_regno < FIRST_PSEUDO_REGISTER
5440 || reg_renumber[src_regno] >= 0)
5442 bool before_p;
5443 rtx use_insn = curr_insn;
5445 before_p = (JUMP_P (curr_insn)
5446 || (CALL_P (curr_insn) && reg->type == OP_IN));
5447 if (NONDEBUG_INSN_P (curr_insn)
5448 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5449 && split_if_necessary (src_regno, reg->biggest_mode,
5450 potential_reload_hard_regs,
5451 before_p, curr_insn, max_uid))
5453 if (reg->subreg_p)
5454 lra_risky_transformations_p = true;
5455 change_p = true;
5456 /* Invalidate. */
5457 usage_insns[src_regno].check = 0;
5458 if (before_p)
5459 use_insn = PREV_INSN (curr_insn);
5461 if (NONDEBUG_INSN_P (curr_insn))
5463 if (src_regno < FIRST_PSEUDO_REGISTER)
5464 add_to_hard_reg_set (&live_hard_regs,
5465 reg->biggest_mode, src_regno);
5466 else
5467 add_to_hard_reg_set (&live_hard_regs,
5468 PSEUDO_REGNO_MODE (src_regno),
5469 reg_renumber[src_regno]);
5471 add_next_usage_insn (src_regno, use_insn, reloads_num);
5474 /* Process call args. */
5475 if (curr_id->arg_hard_regs != NULL)
5476 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5477 if (src_regno < FIRST_PSEUDO_REGISTER)
5479 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5480 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5482 for (i = 0; i < to_inherit_num; i++)
5484 src_regno = to_inherit[i].regno;
5485 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5486 curr_insn, to_inherit[i].insns))
5487 change_p = true;
5488 else
5489 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5492 if (update_reloads_num_p
5493 && NONDEBUG_INSN_P (curr_insn)
5494 && (set = single_set (curr_insn)) != NULL_RTX)
5496 int regno = -1;
5497 if ((REG_P (SET_DEST (set))
5498 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5499 && reg_renumber[regno] < 0
5500 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5501 || (REG_P (SET_SRC (set))
5502 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5503 && reg_renumber[regno] < 0
5504 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5506 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5507 reloads_num++;
5508 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5509 IOR_HARD_REG_SET (potential_reload_hard_regs,
5510 reg_class_contents[cl]);
5513 /* We reached the start of the current basic block. */
5514 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5515 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5517 /* We reached the beginning of the current block -- do
5518 rest of spliting in the current BB. */
5519 to_process = df_get_live_in (curr_bb);
5520 if (BLOCK_FOR_INSN (head) != curr_bb)
5522 /* We are somewhere in the middle of EBB. */
5523 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5524 curr_bb, &temp_bitmap);
5525 to_process = &temp_bitmap;
5527 head_p = true;
5528 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5530 if ((int) j >= lra_constraint_new_regno_start)
5531 break;
5532 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5533 && usage_insns[j].check == curr_usage_insns_check
5534 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5536 if (need_for_split_p (potential_reload_hard_regs, j))
5538 if (lra_dump_file != NULL && head_p)
5540 fprintf (lra_dump_file,
5541 " ----------------------------------\n");
5542 head_p = false;
5544 if (split_reg (false, j, bb_note (curr_bb),
5545 next_usage_insns))
5546 change_p = true;
5548 usage_insns[j].check = 0;
5553 return change_p;
5556 /* This value affects EBB forming. If probability of edge from EBB to
5557 a BB is not greater than the following value, we don't add the BB
5558 to EBB. */
5559 #define EBB_PROBABILITY_CUTOFF ((REG_BR_PROB_BASE * 50) / 100)
5561 /* Current number of inheritance/split iteration. */
5562 int lra_inheritance_iter;
5564 /* Entry function for inheritance/split pass. */
5565 void
5566 lra_inheritance (void)
5568 int i;
5569 basic_block bb, start_bb;
5570 edge e;
5572 lra_inheritance_iter++;
5573 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5574 return;
5575 timevar_push (TV_LRA_INHERITANCE);
5576 if (lra_dump_file != NULL)
5577 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5578 lra_inheritance_iter);
5579 curr_usage_insns_check = 0;
5580 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5581 for (i = 0; i < lra_constraint_new_regno_start; i++)
5582 usage_insns[i].check = 0;
5583 bitmap_initialize (&check_only_regs, &reg_obstack);
5584 bitmap_initialize (&live_regs, &reg_obstack);
5585 bitmap_initialize (&temp_bitmap, &reg_obstack);
5586 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5587 FOR_EACH_BB_FN (bb, cfun)
5589 start_bb = bb;
5590 if (lra_dump_file != NULL)
5591 fprintf (lra_dump_file, "EBB");
5592 /* Form a EBB starting with BB. */
5593 bitmap_clear (&ebb_global_regs);
5594 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5595 for (;;)
5597 if (lra_dump_file != NULL)
5598 fprintf (lra_dump_file, " %d", bb->index);
5599 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5600 || LABEL_P (BB_HEAD (bb->next_bb)))
5601 break;
5602 e = find_fallthru_edge (bb->succs);
5603 if (! e)
5604 break;
5605 if (e->probability <= EBB_PROBABILITY_CUTOFF)
5606 break;
5607 bb = bb->next_bb;
5609 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5610 if (lra_dump_file != NULL)
5611 fprintf (lra_dump_file, "\n");
5612 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5613 /* Remember that the EBB head and tail can change in
5614 inherit_in_ebb. */
5615 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5617 bitmap_clear (&ebb_global_regs);
5618 bitmap_clear (&temp_bitmap);
5619 bitmap_clear (&live_regs);
5620 bitmap_clear (&check_only_regs);
5621 free (usage_insns);
5623 timevar_pop (TV_LRA_INHERITANCE);
5628 /* This page contains code to undo failed inheritance/split
5629 transformations. */
5631 /* Current number of iteration undoing inheritance/split. */
5632 int lra_undo_inheritance_iter;
5634 /* Fix BB live info LIVE after removing pseudos created on pass doing
5635 inheritance/split which are REMOVED_PSEUDOS. */
5636 static void
5637 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5639 unsigned int regno;
5640 bitmap_iterator bi;
5642 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5643 if (bitmap_clear_bit (live, regno))
5644 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5647 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5648 number. */
5649 static int
5650 get_regno (rtx reg)
5652 if (GET_CODE (reg) == SUBREG)
5653 reg = SUBREG_REG (reg);
5654 if (REG_P (reg))
5655 return REGNO (reg);
5656 return -1;
5659 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5660 return true if we did any change. The undo transformations for
5661 inheritance looks like
5662 i <- i2
5663 p <- i => p <- i2
5664 or removing
5665 p <- i, i <- p, and i <- i3
5666 where p is original pseudo from which inheritance pseudo i was
5667 created, i and i3 are removed inheritance pseudos, i2 is another
5668 not removed inheritance pseudo. All split pseudos or other
5669 occurrences of removed inheritance pseudos are changed on the
5670 corresponding original pseudos.
5672 The function also schedules insns changed and created during
5673 inheritance/split pass for processing by the subsequent constraint
5674 pass. */
5675 static bool
5676 remove_inheritance_pseudos (bitmap remove_pseudos)
5678 basic_block bb;
5679 int regno, sregno, prev_sregno, dregno, restore_regno;
5680 rtx set, prev_set, prev_insn;
5681 bool change_p, done_p;
5683 change_p = ! bitmap_empty_p (remove_pseudos);
5684 /* We can not finish the function right away if CHANGE_P is true
5685 because we need to marks insns affected by previous
5686 inheritance/split pass for processing by the subsequent
5687 constraint pass. */
5688 FOR_EACH_BB_FN (bb, cfun)
5690 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5691 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5692 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5694 if (! INSN_P (curr_insn))
5695 continue;
5696 done_p = false;
5697 sregno = dregno = -1;
5698 if (change_p && NONDEBUG_INSN_P (curr_insn)
5699 && (set = single_set (curr_insn)) != NULL_RTX)
5701 dregno = get_regno (SET_DEST (set));
5702 sregno = get_regno (SET_SRC (set));
5705 if (sregno >= 0 && dregno >= 0)
5707 if ((bitmap_bit_p (remove_pseudos, sregno)
5708 && (lra_reg_info[sregno].restore_regno == dregno
5709 || (bitmap_bit_p (remove_pseudos, dregno)
5710 && (lra_reg_info[sregno].restore_regno
5711 == lra_reg_info[dregno].restore_regno))))
5712 || (bitmap_bit_p (remove_pseudos, dregno)
5713 && lra_reg_info[dregno].restore_regno == sregno))
5714 /* One of the following cases:
5715 original <- removed inheritance pseudo
5716 removed inherit pseudo <- another removed inherit pseudo
5717 removed inherit pseudo <- original pseudo
5719 removed_split_pseudo <- original_reg
5720 original_reg <- removed_split_pseudo */
5722 if (lra_dump_file != NULL)
5724 fprintf (lra_dump_file, " Removing %s:\n",
5725 bitmap_bit_p (&lra_split_regs, sregno)
5726 || bitmap_bit_p (&lra_split_regs, dregno)
5727 ? "split" : "inheritance");
5728 dump_insn_slim (lra_dump_file, curr_insn);
5730 lra_set_insn_deleted (curr_insn);
5731 done_p = true;
5733 else if (bitmap_bit_p (remove_pseudos, sregno)
5734 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5736 /* Search the following pattern:
5737 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5738 original_pseudo <- inherit_or_split_pseudo1
5739 where the 2nd insn is the current insn and
5740 inherit_or_split_pseudo2 is not removed. If it is found,
5741 change the current insn onto:
5742 original_pseudo <- inherit_or_split_pseudo2. */
5743 for (prev_insn = PREV_INSN (curr_insn);
5744 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5745 prev_insn = PREV_INSN (prev_insn))
5747 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5748 && (prev_set = single_set (prev_insn)) != NULL_RTX
5749 /* There should be no subregs in insn we are
5750 searching because only the original reg might
5751 be in subreg when we changed the mode of
5752 load/store for splitting. */
5753 && REG_P (SET_DEST (prev_set))
5754 && REG_P (SET_SRC (prev_set))
5755 && (int) REGNO (SET_DEST (prev_set)) == sregno
5756 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5757 >= FIRST_PSEUDO_REGISTER)
5758 /* As we consider chain of inheritance or
5759 splitting described in above comment we should
5760 check that sregno and prev_sregno were
5761 inheritance/split pseudos created from the
5762 same original regno. */
5763 && (lra_reg_info[sregno].restore_regno
5764 == lra_reg_info[prev_sregno].restore_regno)
5765 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5767 lra_assert (GET_MODE (SET_SRC (prev_set))
5768 == GET_MODE (regno_reg_rtx[sregno]));
5769 if (GET_CODE (SET_SRC (set)) == SUBREG)
5770 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5771 else
5772 SET_SRC (set) = SET_SRC (prev_set);
5773 /* As we are finishing with processing the insn
5774 here, check the destination too as it might
5775 inheritance pseudo for another pseudo. */
5776 if (bitmap_bit_p (remove_pseudos, dregno)
5777 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
5778 && (restore_regno
5779 = lra_reg_info[dregno].restore_regno) >= 0)
5781 if (GET_CODE (SET_DEST (set)) == SUBREG)
5782 SUBREG_REG (SET_DEST (set))
5783 = regno_reg_rtx[restore_regno];
5784 else
5785 SET_DEST (set) = regno_reg_rtx[restore_regno];
5787 lra_push_insn_and_update_insn_regno_info (curr_insn);
5788 lra_set_used_insn_alternative_by_uid
5789 (INSN_UID (curr_insn), -1);
5790 done_p = true;
5791 if (lra_dump_file != NULL)
5793 fprintf (lra_dump_file, " Change reload insn:\n");
5794 dump_insn_slim (lra_dump_file, curr_insn);
5799 if (! done_p)
5801 struct lra_insn_reg *reg;
5802 bool restored_regs_p = false;
5803 bool kept_regs_p = false;
5805 curr_id = lra_get_insn_recog_data (curr_insn);
5806 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5808 regno = reg->regno;
5809 restore_regno = lra_reg_info[regno].restore_regno;
5810 if (restore_regno >= 0)
5812 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5814 substitute_pseudo (&curr_insn, regno,
5815 regno_reg_rtx[restore_regno]);
5816 restored_regs_p = true;
5818 else
5819 kept_regs_p = true;
5822 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5824 /* The instruction has changed since the previous
5825 constraints pass. */
5826 lra_push_insn_and_update_insn_regno_info (curr_insn);
5827 lra_set_used_insn_alternative_by_uid
5828 (INSN_UID (curr_insn), -1);
5830 else if (restored_regs_p)
5831 /* The instruction has been restored to the form that
5832 it had during the previous constraints pass. */
5833 lra_update_insn_regno_info (curr_insn);
5834 if (restored_regs_p && lra_dump_file != NULL)
5836 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5837 dump_insn_slim (lra_dump_file, curr_insn);
5842 return change_p;
5845 /* If optional reload pseudos failed to get a hard register or was not
5846 inherited, it is better to remove optional reloads. We do this
5847 transformation after undoing inheritance to figure out necessity to
5848 remove optional reloads easier. Return true if we do any
5849 change. */
5850 static bool
5851 undo_optional_reloads (void)
5853 bool change_p, keep_p;
5854 unsigned int regno, uid;
5855 bitmap_iterator bi, bi2;
5856 rtx insn, set, src, dest;
5857 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
5859 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
5860 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
5861 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5863 keep_p = false;
5864 /* Keep optional reloads from previous subpasses. */
5865 if (lra_reg_info[regno].restore_regno < 0
5866 /* If the original pseudo changed its allocation, just
5867 removing the optional pseudo is dangerous as the original
5868 pseudo will have longer live range. */
5869 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
5870 keep_p = true;
5871 else if (reg_renumber[regno] >= 0)
5872 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
5874 insn = lra_insn_recog_data[uid]->insn;
5875 if ((set = single_set (insn)) == NULL_RTX)
5876 continue;
5877 src = SET_SRC (set);
5878 dest = SET_DEST (set);
5879 if (! REG_P (src) || ! REG_P (dest))
5880 continue;
5881 if (REGNO (dest) == regno
5882 /* Ignore insn for optional reloads itself. */
5883 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
5884 /* Check only inheritance on last inheritance pass. */
5885 && (int) REGNO (src) >= new_regno_start
5886 /* Check that the optional reload was inherited. */
5887 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
5889 keep_p = true;
5890 break;
5893 if (keep_p)
5895 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
5896 if (lra_dump_file != NULL)
5897 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
5900 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
5901 bitmap_initialize (&insn_bitmap, &reg_obstack);
5902 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
5904 if (lra_dump_file != NULL)
5905 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
5906 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
5907 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
5909 insn = lra_insn_recog_data[uid]->insn;
5910 if ((set = single_set (insn)) != NULL_RTX)
5912 src = SET_SRC (set);
5913 dest = SET_DEST (set);
5914 if (REG_P (src) && REG_P (dest)
5915 && ((REGNO (src) == regno
5916 && (lra_reg_info[regno].restore_regno
5917 == (int) REGNO (dest)))
5918 || (REGNO (dest) == regno
5919 && (lra_reg_info[regno].restore_regno
5920 == (int) REGNO (src)))))
5922 if (lra_dump_file != NULL)
5924 fprintf (lra_dump_file, " Deleting move %u\n",
5925 INSN_UID (insn));
5926 dump_insn_slim (lra_dump_file, insn);
5928 lra_set_insn_deleted (insn);
5929 continue;
5931 /* We should not worry about generation memory-memory
5932 moves here as if the corresponding inheritance did
5933 not work (inheritance pseudo did not get a hard reg),
5934 we remove the inheritance pseudo and the optional
5935 reload. */
5937 substitute_pseudo (&insn, regno,
5938 regno_reg_rtx[lra_reg_info[regno].restore_regno]);
5939 lra_update_insn_regno_info (insn);
5940 if (lra_dump_file != NULL)
5942 fprintf (lra_dump_file,
5943 " Restoring original insn:\n");
5944 dump_insn_slim (lra_dump_file, insn);
5948 /* Clear restore_regnos. */
5949 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5950 lra_reg_info[regno].restore_regno = -1;
5951 bitmap_clear (&insn_bitmap);
5952 bitmap_clear (&removed_optional_reload_pseudos);
5953 return change_p;
5956 /* Entry function for undoing inheritance/split transformation. Return true
5957 if we did any RTL change in this pass. */
5958 bool
5959 lra_undo_inheritance (void)
5961 unsigned int regno;
5962 int restore_regno, hard_regno;
5963 int n_all_inherit, n_inherit, n_all_split, n_split;
5964 bitmap_head remove_pseudos;
5965 bitmap_iterator bi;
5966 bool change_p;
5968 lra_undo_inheritance_iter++;
5969 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5970 return false;
5971 if (lra_dump_file != NULL)
5972 fprintf (lra_dump_file,
5973 "\n********** Undoing inheritance #%d: **********\n\n",
5974 lra_undo_inheritance_iter);
5975 bitmap_initialize (&remove_pseudos, &reg_obstack);
5976 n_inherit = n_all_inherit = 0;
5977 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5978 if (lra_reg_info[regno].restore_regno >= 0)
5980 n_all_inherit++;
5981 if (reg_renumber[regno] < 0
5982 /* If the original pseudo changed its allocation, just
5983 removing inheritance is dangerous as for changing
5984 allocation we used shorter live-ranges. */
5985 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
5986 bitmap_set_bit (&remove_pseudos, regno);
5987 else
5988 n_inherit++;
5990 if (lra_dump_file != NULL && n_all_inherit != 0)
5991 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
5992 n_inherit, n_all_inherit,
5993 (double) n_inherit / n_all_inherit * 100);
5994 n_split = n_all_split = 0;
5995 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5996 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
5998 n_all_split++;
5999 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6000 ? reg_renumber[restore_regno] : restore_regno);
6001 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6002 bitmap_set_bit (&remove_pseudos, regno);
6003 else
6005 n_split++;
6006 if (lra_dump_file != NULL)
6007 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6008 regno, restore_regno);
6011 if (lra_dump_file != NULL && n_all_split != 0)
6012 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6013 n_split, n_all_split,
6014 (double) n_split / n_all_split * 100);
6015 change_p = remove_inheritance_pseudos (&remove_pseudos);
6016 bitmap_clear (&remove_pseudos);
6017 /* Clear restore_regnos. */
6018 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6019 lra_reg_info[regno].restore_regno = -1;
6020 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6021 lra_reg_info[regno].restore_regno = -1;
6022 change_p = undo_optional_reloads () || change_p;
6023 return change_p;