gcc/testsuite/
[official-gcc.git] / gcc / lra-constraints.c
blobaac50876d21a5c9be55803d9022892861984bf47
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];
150 /* Start numbers for new registers and insns at the current constraints
151 pass start. */
152 static int new_regno_start;
153 static int new_insn_uid_start;
155 /* If LOC is nonnull, strip any outer subreg from it. */
156 static inline rtx *
157 strip_subreg (rtx *loc)
159 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
162 /* Return hard regno of REGNO or if it is was not assigned to a hard
163 register, use a hard register from its allocno class. */
164 static int
165 get_try_hard_regno (int regno)
167 int hard_regno;
168 enum reg_class rclass;
170 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
171 hard_regno = lra_get_regno_hard_regno (regno);
172 if (hard_regno >= 0)
173 return hard_regno;
174 rclass = lra_get_allocno_class (regno);
175 if (rclass == NO_REGS)
176 return -1;
177 return ira_class_hard_regs[rclass][0];
180 /* Return final hard regno (plus offset) which will be after
181 elimination. We do this for matching constraints because the final
182 hard regno could have a different class. */
183 static int
184 get_final_hard_regno (int hard_regno, int offset)
186 if (hard_regno < 0)
187 return hard_regno;
188 hard_regno = lra_get_elimination_hard_regno (hard_regno);
189 return hard_regno + offset;
192 /* Return hard regno of X after removing subreg and making
193 elimination. If X is not a register or subreg of register, return
194 -1. For pseudo use its assignment. */
195 static int
196 get_hard_regno (rtx x)
198 rtx reg;
199 int offset, hard_regno;
201 reg = x;
202 if (GET_CODE (x) == SUBREG)
203 reg = SUBREG_REG (x);
204 if (! REG_P (reg))
205 return -1;
206 if ((hard_regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
207 hard_regno = lra_get_regno_hard_regno (hard_regno);
208 if (hard_regno < 0)
209 return -1;
210 offset = 0;
211 if (GET_CODE (x) == SUBREG)
212 offset += subreg_regno_offset (hard_regno, GET_MODE (reg),
213 SUBREG_BYTE (x), GET_MODE (x));
214 return get_final_hard_regno (hard_regno, offset);
217 /* If REGNO is a hard register or has been allocated a hard register,
218 return the class of that register. If REGNO is a reload pseudo
219 created by the current constraints pass, return its allocno class.
220 Return NO_REGS otherwise. */
221 static enum reg_class
222 get_reg_class (int regno)
224 int hard_regno;
226 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
227 hard_regno = lra_get_regno_hard_regno (regno);
228 if (hard_regno >= 0)
230 hard_regno = get_final_hard_regno (hard_regno, 0);
231 return REGNO_REG_CLASS (hard_regno);
233 if (regno >= new_regno_start)
234 return lra_get_allocno_class (regno);
235 return NO_REGS;
238 /* Return true if REG satisfies (or will satisfy) reg class constraint
239 CL. Use elimination first if REG is a hard register. If REG is a
240 reload pseudo created by this constraints pass, assume that it will
241 be allocated a hard register from its allocno class, but allow that
242 class to be narrowed to CL if it is currently a superset of CL.
244 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
245 REGNO (reg), or NO_REGS if no change in its class was needed. */
246 static bool
247 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
249 enum reg_class rclass, common_class;
250 enum machine_mode reg_mode;
251 int class_size, hard_regno, nregs, i, j;
252 int regno = REGNO (reg);
254 if (new_class != NULL)
255 *new_class = NO_REGS;
256 if (regno < FIRST_PSEUDO_REGISTER)
258 rtx final_reg = reg;
259 rtx *final_loc = &final_reg;
261 lra_eliminate_reg_if_possible (final_loc);
262 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
264 reg_mode = GET_MODE (reg);
265 rclass = get_reg_class (regno);
266 if (regno < new_regno_start
267 /* Do not allow the constraints for reload instructions to
268 influence the classes of new pseudos. These reloads are
269 typically moves that have many alternatives, and restricting
270 reload pseudos for one alternative may lead to situations
271 where other reload pseudos are no longer allocatable. */
272 || (INSN_UID (curr_insn) >= new_insn_uid_start
273 && curr_insn_set != NULL
274 && ((OBJECT_P (SET_SRC (curr_insn_set))
275 && ! CONSTANT_P (SET_SRC (curr_insn_set)))
276 || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
277 && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))
278 && ! CONSTANT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
279 /* When we don't know what class will be used finally for reload
280 pseudos, we use ALL_REGS. */
281 return ((regno >= new_regno_start && rclass == ALL_REGS)
282 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
283 && ! hard_reg_set_subset_p (reg_class_contents[cl],
284 lra_no_alloc_regs)));
285 else
287 common_class = ira_reg_class_subset[rclass][cl];
288 if (new_class != NULL)
289 *new_class = common_class;
290 if (hard_reg_set_subset_p (reg_class_contents[common_class],
291 lra_no_alloc_regs))
292 return false;
293 /* Check that there are enough allocatable regs. */
294 class_size = ira_class_hard_regs_num[common_class];
295 for (i = 0; i < class_size; i++)
297 hard_regno = ira_class_hard_regs[common_class][i];
298 nregs = hard_regno_nregs[hard_regno][reg_mode];
299 if (nregs == 1)
300 return true;
301 for (j = 0; j < nregs; j++)
302 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
303 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
304 hard_regno + j))
305 break;
306 if (j >= nregs)
307 return true;
309 return false;
313 /* Return true if REGNO satisfies a memory constraint. */
314 static bool
315 in_mem_p (int regno)
317 return get_reg_class (regno) == NO_REGS;
320 /* Initiate equivalences for LRA. As we keep original equivalences
321 before any elimination, we need to make copies otherwise any change
322 in insns might change the equivalences. */
323 void
324 lra_init_equiv (void)
326 ira_expand_reg_equiv ();
327 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
329 rtx res;
331 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
332 ira_reg_equiv[i].memory = copy_rtx (res);
333 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
334 ira_reg_equiv[i].invariant = copy_rtx (res);
338 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
340 /* Update equivalence for REGNO. We need to this as the equivalence
341 might contain other pseudos which are changed by their
342 equivalences. */
343 static void
344 update_equiv (int regno)
346 rtx x;
348 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
349 ira_reg_equiv[regno].memory
350 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
351 NULL_RTX);
352 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
353 ira_reg_equiv[regno].invariant
354 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
355 NULL_RTX);
358 /* If we have decided to substitute X with another value, return that
359 value, otherwise return X. */
360 static rtx
361 get_equiv (rtx x)
363 int regno;
364 rtx res;
366 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
367 || ! ira_reg_equiv[regno].defined_p
368 || ! ira_reg_equiv[regno].profitable_p
369 || lra_get_regno_hard_regno (regno) >= 0)
370 return x;
371 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
372 return res;
373 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
374 return res;
375 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
376 return res;
377 gcc_unreachable ();
380 /* If we have decided to substitute X with the equivalent value,
381 return that value after elimination for INSN, otherwise return
382 X. */
383 static rtx
384 get_equiv_with_elimination (rtx x, rtx insn)
386 rtx res = get_equiv (x);
388 if (x == res || CONSTANT_P (res))
389 return res;
390 return lra_eliminate_regs_1 (insn, res, GET_MODE (res), false, false, true);
393 /* Set up curr_operand_mode. */
394 static void
395 init_curr_operand_mode (void)
397 int nop = curr_static_id->n_operands;
398 for (int i = 0; i < nop; i++)
400 enum machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
401 if (mode == VOIDmode)
403 /* The .md mode for address operands is the mode of the
404 addressed value rather than the mode of the address itself. */
405 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
406 mode = Pmode;
407 else
408 mode = curr_static_id->operand[i].mode;
410 curr_operand_mode[i] = mode;
416 /* The page contains code to reuse input reloads. */
418 /* Structure describes input reload of the current insns. */
419 struct input_reload
421 /* Reloaded value. */
422 rtx input;
423 /* Reload pseudo used. */
424 rtx reg;
427 /* The number of elements in the following array. */
428 static int curr_insn_input_reloads_num;
429 /* Array containing info about input reloads. It is used to find the
430 same input reload and reuse the reload pseudo in this case. */
431 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
433 /* Initiate data concerning reuse of input reloads for the current
434 insn. */
435 static void
436 init_curr_insn_input_reloads (void)
438 curr_insn_input_reloads_num = 0;
441 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
442 created input reload pseudo (only if TYPE is not OP_OUT). Don't
443 reuse pseudo if IN_SUBREG_P is true and the reused pseudo should be
444 wrapped up in SUBREG. The result pseudo is returned through
445 RESULT_REG. Return TRUE if we created a new pseudo, FALSE if we
446 reused the already created input reload pseudo. Use TITLE to
447 describe new registers for debug purposes. */
448 static bool
449 get_reload_reg (enum op_type type, enum machine_mode mode, rtx original,
450 enum reg_class rclass, bool in_subreg_p,
451 const char *title, rtx *result_reg)
453 int i, regno;
454 enum reg_class new_class;
456 if (type == OP_OUT)
458 *result_reg
459 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
460 return true;
462 /* Prevent reuse value of expression with side effects,
463 e.g. volatile memory. */
464 if (! side_effects_p (original))
465 for (i = 0; i < curr_insn_input_reloads_num; i++)
466 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
467 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
469 rtx reg = curr_insn_input_reloads[i].reg;
470 regno = REGNO (reg);
471 /* If input is equal to original and both are VOIDmode,
472 GET_MODE (reg) might be still different from mode.
473 Ensure we don't return *result_reg with wrong mode. */
474 if (GET_MODE (reg) != mode)
476 if (in_subreg_p)
477 continue;
478 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
479 continue;
480 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
481 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
482 continue;
484 *result_reg = reg;
485 if (lra_dump_file != NULL)
487 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
488 dump_value_slim (lra_dump_file, original, 1);
490 if (new_class != lra_get_allocno_class (regno))
491 lra_change_class (regno, new_class, ", change to", false);
492 if (lra_dump_file != NULL)
493 fprintf (lra_dump_file, "\n");
494 return false;
496 *result_reg = lra_create_new_reg (mode, original, rclass, title);
497 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
498 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
499 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
500 return true;
505 /* The page contains code to extract memory address parts. */
507 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
508 static inline bool
509 ok_for_index_p_nonstrict (rtx reg)
511 unsigned regno = REGNO (reg);
513 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
516 /* A version of regno_ok_for_base_p for use here, when all pseudos
517 should count as OK. Arguments as for regno_ok_for_base_p. */
518 static inline bool
519 ok_for_base_p_nonstrict (rtx reg, enum machine_mode mode, addr_space_t as,
520 enum rtx_code outer_code, enum rtx_code index_code)
522 unsigned regno = REGNO (reg);
524 if (regno >= FIRST_PSEUDO_REGISTER)
525 return true;
526 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
531 /* The page contains major code to choose the current insn alternative
532 and generate reloads for it. */
534 /* Return the offset from REGNO of the least significant register
535 in (reg:MODE REGNO).
537 This function is used to tell whether two registers satisfy
538 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
540 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
541 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
543 lra_constraint_offset (int regno, enum machine_mode mode)
545 lra_assert (regno < FIRST_PSEUDO_REGISTER);
546 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
547 && SCALAR_INT_MODE_P (mode))
548 return hard_regno_nregs[regno][mode] - 1;
549 return 0;
552 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
553 if they are the same hard reg, and has special hacks for
554 auto-increment and auto-decrement. This is specifically intended for
555 process_alt_operands to use in determining whether two operands
556 match. X is the operand whose number is the lower of the two.
558 It is supposed that X is the output operand and Y is the input
559 operand. Y_HARD_REGNO is the final hard regno of register Y or
560 register in subreg Y as we know it now. Otherwise, it is a
561 negative value. */
562 static bool
563 operands_match_p (rtx x, rtx y, int y_hard_regno)
565 int i;
566 RTX_CODE code = GET_CODE (x);
567 const char *fmt;
569 if (x == y)
570 return true;
571 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
572 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
574 int j;
576 i = get_hard_regno (x);
577 if (i < 0)
578 goto slow;
580 if ((j = y_hard_regno) < 0)
581 goto slow;
583 i += lra_constraint_offset (i, GET_MODE (x));
584 j += lra_constraint_offset (j, GET_MODE (y));
586 return i == j;
589 /* If two operands must match, because they are really a single
590 operand of an assembler insn, then two post-increments are invalid
591 because the assembler insn would increment only once. On the
592 other hand, a post-increment matches ordinary indexing if the
593 post-increment is the output operand. */
594 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
595 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
597 /* Two pre-increments are invalid because the assembler insn would
598 increment only once. On the other hand, a pre-increment matches
599 ordinary indexing if the pre-increment is the input operand. */
600 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
601 || GET_CODE (y) == PRE_MODIFY)
602 return operands_match_p (x, XEXP (y, 0), -1);
604 slow:
606 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
607 && x == SUBREG_REG (y))
608 return true;
609 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
610 && SUBREG_REG (x) == y)
611 return true;
613 /* Now we have disposed of all the cases in which different rtx
614 codes can match. */
615 if (code != GET_CODE (y))
616 return false;
618 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
619 if (GET_MODE (x) != GET_MODE (y))
620 return false;
622 switch (code)
624 CASE_CONST_UNIQUE:
625 return false;
627 case LABEL_REF:
628 return XEXP (x, 0) == XEXP (y, 0);
629 case SYMBOL_REF:
630 return XSTR (x, 0) == XSTR (y, 0);
632 default:
633 break;
636 /* Compare the elements. If any pair of corresponding elements fail
637 to match, return false for the whole things. */
639 fmt = GET_RTX_FORMAT (code);
640 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
642 int val, j;
643 switch (fmt[i])
645 case 'w':
646 if (XWINT (x, i) != XWINT (y, i))
647 return false;
648 break;
650 case 'i':
651 if (XINT (x, i) != XINT (y, i))
652 return false;
653 break;
655 case 'e':
656 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
657 if (val == 0)
658 return false;
659 break;
661 case '0':
662 break;
664 case 'E':
665 if (XVECLEN (x, i) != XVECLEN (y, i))
666 return false;
667 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
669 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
670 if (val == 0)
671 return false;
673 break;
675 /* It is believed that rtx's at this level will never
676 contain anything but integers and other rtx's, except for
677 within LABEL_REFs and SYMBOL_REFs. */
678 default:
679 gcc_unreachable ();
682 return true;
685 /* True if X is a constant that can be forced into the constant pool.
686 MODE is the mode of the operand, or VOIDmode if not known. */
687 #define CONST_POOL_OK_P(MODE, X) \
688 ((MODE) != VOIDmode \
689 && CONSTANT_P (X) \
690 && GET_CODE (X) != HIGH \
691 && !targetm.cannot_force_const_mem (MODE, X))
693 /* True if C is a non-empty register class that has too few registers
694 to be safely used as a reload target class. */
695 #define SMALL_REGISTER_CLASS_P(C) \
696 (ira_class_hard_regs_num [(C)] == 1 \
697 || (ira_class_hard_regs_num [(C)] >= 1 \
698 && targetm.class_likely_spilled_p (C)))
700 /* If REG is a reload pseudo, try to make its class satisfying CL. */
701 static void
702 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
704 enum reg_class rclass;
706 /* Do not make more accurate class from reloads generated. They are
707 mostly moves with a lot of constraints. Making more accurate
708 class may results in very narrow class and impossibility of find
709 registers for several reloads of one insn. */
710 if (INSN_UID (curr_insn) >= new_insn_uid_start)
711 return;
712 if (GET_CODE (reg) == SUBREG)
713 reg = SUBREG_REG (reg);
714 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
715 return;
716 if (in_class_p (reg, cl, &rclass) && rclass != cl)
717 lra_change_class (REGNO (reg), rclass, " Change to", true);
720 /* Generate reloads for matching OUT and INS (array of input operand
721 numbers with end marker -1) with reg class GOAL_CLASS. Add input
722 and output reloads correspondingly to the lists *BEFORE and *AFTER.
723 OUT might be negative. In this case we generate input reloads for
724 matched input operands INS. */
725 static void
726 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
727 rtx *before, rtx *after)
729 int i, in;
730 rtx new_in_reg, new_out_reg, reg, clobber;
731 enum machine_mode inmode, outmode;
732 rtx in_rtx = *curr_id->operand_loc[ins[0]];
733 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
735 inmode = curr_operand_mode[ins[0]];
736 outmode = out < 0 ? inmode : curr_operand_mode[out];
737 push_to_sequence (*before);
738 if (inmode != outmode)
740 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
742 reg = new_in_reg
743 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
744 goal_class, "");
745 if (SCALAR_INT_MODE_P (inmode))
746 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
747 else
748 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
749 LRA_SUBREG_P (new_out_reg) = 1;
750 /* If the input reg is dying here, we can use the same hard
751 register for REG and IN_RTX. We do it only for original
752 pseudos as reload pseudos can die although original
753 pseudos still live where reload pseudos dies. */
754 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
755 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
756 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
758 else
760 reg = new_out_reg
761 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
762 goal_class, "");
763 if (SCALAR_INT_MODE_P (outmode))
764 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
765 else
766 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
767 /* NEW_IN_REG is non-paradoxical subreg. We don't want
768 NEW_OUT_REG living above. We add clobber clause for
769 this. This is just a temporary clobber. We can remove
770 it at the end of LRA work. */
771 clobber = emit_clobber (new_out_reg);
772 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
773 LRA_SUBREG_P (new_in_reg) = 1;
774 if (GET_CODE (in_rtx) == SUBREG)
776 rtx subreg_reg = SUBREG_REG (in_rtx);
778 /* If SUBREG_REG is dying here and sub-registers IN_RTX
779 and NEW_IN_REG are similar, we can use the same hard
780 register for REG and SUBREG_REG. */
781 if (REG_P (subreg_reg)
782 && (int) REGNO (subreg_reg) < lra_new_regno_start
783 && GET_MODE (subreg_reg) == outmode
784 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
785 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
786 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
790 else
792 /* Pseudos have values -- see comments for lra_reg_info.
793 Different pseudos with the same value do not conflict even if
794 they live in the same place. When we create a pseudo we
795 assign value of original pseudo (if any) from which we
796 created the new pseudo. If we create the pseudo from the
797 input pseudo, the new pseudo will no conflict with the input
798 pseudo which is wrong when the input pseudo lives after the
799 insn and as the new pseudo value is changed by the insn
800 output. Therefore we create the new pseudo from the output.
802 We cannot reuse the current output register because we might
803 have a situation like "a <- a op b", where the constraints
804 force the second input operand ("b") to match the output
805 operand ("a"). "b" must then be copied into a new register
806 so that it doesn't clobber the current value of "a". */
808 new_in_reg = new_out_reg
809 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
810 goal_class, "");
812 /* In operand can be got from transformations before processing insn
813 constraints. One example of such transformations is subreg
814 reloading (see function simplify_operand_subreg). The new
815 pseudos created by the transformations might have inaccurate
816 class (ALL_REGS) and we should make their classes more
817 accurate. */
818 narrow_reload_pseudo_class (in_rtx, goal_class);
819 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
820 *before = get_insns ();
821 end_sequence ();
822 for (i = 0; (in = ins[i]) >= 0; i++)
824 lra_assert
825 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
826 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
827 *curr_id->operand_loc[in] = new_in_reg;
829 lra_update_dups (curr_id, ins);
830 if (out < 0)
831 return;
832 /* See a comment for the input operand above. */
833 narrow_reload_pseudo_class (out_rtx, goal_class);
834 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
836 start_sequence ();
837 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
838 emit_insn (*after);
839 *after = get_insns ();
840 end_sequence ();
842 *curr_id->operand_loc[out] = new_out_reg;
843 lra_update_dup (curr_id, out);
846 /* Return register class which is union of all reg classes in insn
847 constraint alternative string starting with P. */
848 static enum reg_class
849 reg_class_from_constraints (const char *p)
851 int c, len;
852 enum reg_class op_class = NO_REGS;
855 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
857 case '#':
858 case ',':
859 return op_class;
861 case 'p':
862 op_class = (reg_class_subunion
863 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
864 ADDRESS, SCRATCH)]);
865 break;
867 case 'g':
868 case 'r':
869 op_class = reg_class_subunion[op_class][GENERAL_REGS];
870 break;
872 default:
873 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
875 #ifdef EXTRA_CONSTRAINT_STR
876 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
877 op_class
878 = (reg_class_subunion
879 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
880 ADDRESS, SCRATCH)]);
881 #endif
882 break;
885 op_class
886 = reg_class_subunion[op_class][REG_CLASS_FROM_CONSTRAINT (c, p)];
887 break;
889 while ((p += len), c);
890 return op_class;
893 /* If OP is a register, return the class of the register as per
894 get_reg_class, otherwise return NO_REGS. */
895 static inline enum reg_class
896 get_op_class (rtx op)
898 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
901 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
902 otherwise. If modes of MEM_PSEUDO and VAL are different, use
903 SUBREG for VAL to make them equal. */
904 static rtx
905 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
907 if (GET_MODE (mem_pseudo) != GET_MODE (val))
909 /* Usually size of mem_pseudo is greater than val size but in
910 rare cases it can be less as it can be defined by target
911 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
912 if (! MEM_P (val))
914 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
915 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
917 LRA_SUBREG_P (val) = 1;
919 else
921 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
922 LRA_SUBREG_P (mem_pseudo) = 1;
925 return (to_p
926 ? gen_move_insn (mem_pseudo, val)
927 : gen_move_insn (val, mem_pseudo));
930 /* Process a special case insn (register move), return true if we
931 don't need to process it anymore. INSN should be a single set
932 insn. Set up that RTL was changed through CHANGE_P and macro
933 SECONDARY_MEMORY_NEEDED says to use secondary memory through
934 SEC_MEM_P. */
935 static bool
936 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
938 int sregno, dregno;
939 rtx dest, src, dreg, sreg, old_sreg, new_reg, before, scratch_reg;
940 enum reg_class dclass, sclass, secondary_class;
941 enum machine_mode sreg_mode;
942 secondary_reload_info sri;
944 lra_assert (curr_insn_set != NULL_RTX);
945 dreg = dest = SET_DEST (curr_insn_set);
946 sreg = src = SET_SRC (curr_insn_set);
947 if (GET_CODE (dest) == SUBREG)
948 dreg = SUBREG_REG (dest);
949 if (GET_CODE (src) == SUBREG)
950 sreg = SUBREG_REG (src);
951 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
952 return false;
953 sclass = dclass = NO_REGS;
954 if (REG_P (dreg))
955 dclass = get_reg_class (REGNO (dreg));
956 if (dclass == ALL_REGS)
957 /* ALL_REGS is used for new pseudos created by transformations
958 like reload of SUBREG_REG (see function
959 simplify_operand_subreg). We don't know their class yet. We
960 should figure out the class from processing the insn
961 constraints not in this fast path function. Even if ALL_REGS
962 were a right class for the pseudo, secondary_... hooks usually
963 are not define for ALL_REGS. */
964 return false;
965 sreg_mode = GET_MODE (sreg);
966 old_sreg = sreg;
967 if (REG_P (sreg))
968 sclass = get_reg_class (REGNO (sreg));
969 if (sclass == ALL_REGS)
970 /* See comments above. */
971 return false;
972 if (sclass == NO_REGS && dclass == NO_REGS)
973 return false;
974 #ifdef SECONDARY_MEMORY_NEEDED
975 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
976 #ifdef SECONDARY_MEMORY_NEEDED_MODE
977 && ((sclass != NO_REGS && dclass != NO_REGS)
978 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
979 #endif
982 *sec_mem_p = true;
983 return false;
985 #endif
986 if (! REG_P (dreg) || ! REG_P (sreg))
987 return false;
988 sri.prev_sri = NULL;
989 sri.icode = CODE_FOR_nothing;
990 sri.extra_cost = 0;
991 secondary_class = NO_REGS;
992 /* Set up hard register for a reload pseudo for hook
993 secondary_reload because some targets just ignore unassigned
994 pseudos in the hook. */
995 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
997 dregno = REGNO (dreg);
998 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1000 else
1001 dregno = -1;
1002 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1004 sregno = REGNO (sreg);
1005 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1007 else
1008 sregno = -1;
1009 if (sclass != NO_REGS)
1010 secondary_class
1011 = (enum reg_class) targetm.secondary_reload (false, dest,
1012 (reg_class_t) sclass,
1013 GET_MODE (src), &sri);
1014 if (sclass == NO_REGS
1015 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1016 && dclass != NO_REGS))
1018 enum reg_class old_sclass = secondary_class;
1019 secondary_reload_info old_sri = sri;
1021 sri.prev_sri = NULL;
1022 sri.icode = CODE_FOR_nothing;
1023 sri.extra_cost = 0;
1024 secondary_class
1025 = (enum reg_class) targetm.secondary_reload (true, sreg,
1026 (reg_class_t) dclass,
1027 sreg_mode, &sri);
1028 /* Check the target hook consistency. */
1029 lra_assert
1030 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1031 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1032 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1034 if (sregno >= 0)
1035 reg_renumber [sregno] = -1;
1036 if (dregno >= 0)
1037 reg_renumber [dregno] = -1;
1038 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1039 return false;
1040 *change_p = true;
1041 new_reg = NULL_RTX;
1042 if (secondary_class != NO_REGS)
1043 new_reg = lra_create_new_reg_with_unique_value (sreg_mode, NULL_RTX,
1044 secondary_class,
1045 "secondary");
1046 start_sequence ();
1047 if (old_sreg != sreg)
1048 sreg = copy_rtx (sreg);
1049 if (sri.icode == CODE_FOR_nothing)
1050 lra_emit_move (new_reg, sreg);
1051 else
1053 enum reg_class scratch_class;
1055 scratch_class = (reg_class_from_constraints
1056 (insn_data[sri.icode].operand[2].constraint));
1057 scratch_reg = (lra_create_new_reg_with_unique_value
1058 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1059 scratch_class, "scratch"));
1060 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1061 sreg, scratch_reg));
1063 before = get_insns ();
1064 end_sequence ();
1065 lra_process_new_insns (curr_insn, before, NULL_RTX, "Inserting the move");
1066 if (new_reg != NULL_RTX)
1068 if (GET_CODE (src) == SUBREG)
1069 SUBREG_REG (src) = new_reg;
1070 else
1071 SET_SRC (curr_insn_set) = new_reg;
1073 else
1075 if (lra_dump_file != NULL)
1077 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1078 dump_insn_slim (lra_dump_file, curr_insn);
1080 lra_set_insn_deleted (curr_insn);
1081 return true;
1083 return false;
1086 /* The following data describe the result of process_alt_operands.
1087 The data are used in curr_insn_transform to generate reloads. */
1089 /* The chosen reg classes which should be used for the corresponding
1090 operands. */
1091 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1092 /* True if the operand should be the same as another operand and that
1093 other operand does not need a reload. */
1094 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1095 /* True if the operand does not need a reload. */
1096 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1097 /* True if the operand can be offsetable memory. */
1098 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1099 /* The number of an operand to which given operand can be matched to. */
1100 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1101 /* The number of elements in the following array. */
1102 static int goal_alt_dont_inherit_ops_num;
1103 /* Numbers of operands whose reload pseudos should not be inherited. */
1104 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1105 /* True if the insn commutative operands should be swapped. */
1106 static bool goal_alt_swapped;
1107 /* The chosen insn alternative. */
1108 static int goal_alt_number;
1110 /* The following five variables are used to choose the best insn
1111 alternative. They reflect final characteristics of the best
1112 alternative. */
1114 /* Number of necessary reloads and overall cost reflecting the
1115 previous value and other unpleasantness of the best alternative. */
1116 static int best_losers, best_overall;
1117 /* Overall number hard registers used for reloads. For example, on
1118 some targets we need 2 general registers to reload DFmode and only
1119 one floating point register. */
1120 static int best_reload_nregs;
1121 /* Overall number reflecting distances of previous reloading the same
1122 value. The distances are counted from the current BB start. It is
1123 used to improve inheritance chances. */
1124 static int best_reload_sum;
1126 /* True if the current insn should have no correspondingly input or
1127 output reloads. */
1128 static bool no_input_reloads_p, no_output_reloads_p;
1130 /* True if we swapped the commutative operands in the current
1131 insn. */
1132 static int curr_swapped;
1134 /* Arrange for address element *LOC to be a register of class CL.
1135 Add any input reloads to list BEFORE. AFTER is nonnull if *LOC is an
1136 automodified value; handle that case by adding the required output
1137 reloads to list AFTER. Return true if the RTL was changed. */
1138 static bool
1139 process_addr_reg (rtx *loc, rtx *before, rtx *after, enum reg_class cl)
1141 int regno;
1142 enum reg_class rclass, new_class;
1143 rtx reg;
1144 rtx new_reg;
1145 enum machine_mode mode;
1146 bool subreg_p, before_p = false;
1148 subreg_p = GET_CODE (*loc) == SUBREG;
1149 if (subreg_p)
1150 loc = &SUBREG_REG (*loc);
1151 reg = *loc;
1152 mode = GET_MODE (reg);
1153 if (! REG_P (reg))
1155 /* Always reload memory in an address even if the target supports
1156 such addresses. */
1157 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1158 before_p = true;
1160 else
1162 regno = REGNO (reg);
1163 rclass = get_reg_class (regno);
1164 if ((*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1166 if (lra_dump_file != NULL)
1168 fprintf (lra_dump_file,
1169 "Changing pseudo %d in address of insn %u on equiv ",
1170 REGNO (reg), INSN_UID (curr_insn));
1171 dump_value_slim (lra_dump_file, *loc, 1);
1172 fprintf (lra_dump_file, "\n");
1174 *loc = copy_rtx (*loc);
1176 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1178 reg = *loc;
1179 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1180 mode, reg, cl, subreg_p, "address", &new_reg))
1181 before_p = true;
1183 else if (new_class != NO_REGS && rclass != new_class)
1185 lra_change_class (regno, new_class, " Change to", true);
1186 return false;
1188 else
1189 return false;
1191 if (before_p)
1193 push_to_sequence (*before);
1194 lra_emit_move (new_reg, reg);
1195 *before = get_insns ();
1196 end_sequence ();
1198 *loc = new_reg;
1199 if (after != NULL)
1201 start_sequence ();
1202 lra_emit_move (reg, new_reg);
1203 emit_insn (*after);
1204 *after = get_insns ();
1205 end_sequence ();
1207 return true;
1210 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1211 the insn to be inserted before curr insn. AFTER returns the
1212 the insn to be inserted after curr insn. ORIGREG and NEWREG
1213 are the original reg and new reg for reload. */
1214 static void
1215 insert_move_for_subreg (rtx *before, rtx *after, rtx origreg, rtx newreg)
1217 if (before)
1219 push_to_sequence (*before);
1220 lra_emit_move (newreg, origreg);
1221 *before = get_insns ();
1222 end_sequence ();
1224 if (after)
1226 start_sequence ();
1227 lra_emit_move (origreg, newreg);
1228 emit_insn (*after);
1229 *after = get_insns ();
1230 end_sequence ();
1234 /* Make reloads for subreg in operand NOP with internal subreg mode
1235 REG_MODE, add new reloads for further processing. Return true if
1236 any reload was generated. */
1237 static bool
1238 simplify_operand_subreg (int nop, enum machine_mode reg_mode)
1240 int hard_regno;
1241 rtx before, after;
1242 enum machine_mode mode;
1243 rtx reg, new_reg;
1244 rtx operand = *curr_id->operand_loc[nop];
1245 enum reg_class regclass;
1246 enum op_type type;
1248 before = after = NULL_RTX;
1250 if (GET_CODE (operand) != SUBREG)
1251 return false;
1253 mode = GET_MODE (operand);
1254 reg = SUBREG_REG (operand);
1255 type = curr_static_id->operand[nop].type;
1256 /* If we change address for paradoxical subreg of memory, the
1257 address might violate the necessary alignment or the access might
1258 be slow. So take this into consideration. We should not worry
1259 about access beyond allocated memory for paradoxical memory
1260 subregs as we don't substitute such equiv memory (see processing
1261 equivalences in function lra_constraints) and because for spilled
1262 pseudos we allocate stack memory enough for the biggest
1263 corresponding paradoxical subreg. */
1264 if ((MEM_P (reg)
1265 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1266 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1267 || (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER))
1269 alter_subreg (curr_id->operand_loc[nop], false);
1270 return true;
1272 /* Put constant into memory when we have mixed modes. It generates
1273 a better code in most cases as it does not need a secondary
1274 reload memory. It also prevents LRA looping when LRA is using
1275 secondary reload memory again and again. */
1276 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1277 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1279 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1280 alter_subreg (curr_id->operand_loc[nop], false);
1281 return true;
1283 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1284 if there may be a problem accessing OPERAND in the outer
1285 mode. */
1286 if ((REG_P (reg)
1287 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1288 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1289 /* Don't reload paradoxical subregs because we could be looping
1290 having repeatedly final regno out of hard regs range. */
1291 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1292 >= hard_regno_nregs[hard_regno][mode])
1293 && simplify_subreg_regno (hard_regno, GET_MODE (reg),
1294 SUBREG_BYTE (operand), mode) < 0
1295 /* Don't reload subreg for matching reload. It is actually
1296 valid subreg in LRA. */
1297 && ! LRA_SUBREG_P (operand))
1298 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1300 enum reg_class rclass;
1302 if (REG_P (reg))
1303 /* There is a big probability that we will get the same class
1304 for the new pseudo and we will get the same insn which
1305 means infinite looping. So spill the new pseudo. */
1306 rclass = NO_REGS;
1307 else
1308 /* The class will be defined later in curr_insn_transform. */
1309 rclass
1310 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1312 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1313 rclass, TRUE, "subreg reg", &new_reg))
1315 bool insert_before, insert_after;
1316 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1318 insert_before = (type != OP_OUT
1319 || GET_MODE_SIZE (GET_MODE (reg)) > GET_MODE_SIZE (mode));
1320 insert_after = (type != OP_IN);
1321 insert_move_for_subreg (insert_before ? &before : NULL,
1322 insert_after ? &after : NULL,
1323 reg, new_reg);
1325 SUBREG_REG (operand) = new_reg;
1326 lra_process_new_insns (curr_insn, before, after,
1327 "Inserting subreg reload");
1328 return true;
1330 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1331 IRA allocates hardreg to the inner pseudo reg according to its mode
1332 instead of the outermode, so the size of the hardreg may not be enough
1333 to contain the outermode operand, in that case we may need to insert
1334 reload for the reg. For the following two types of paradoxical subreg,
1335 we need to insert reload:
1336 1. If the op_type is OP_IN, and the hardreg could not be paired with
1337 other hardreg to contain the outermode operand
1338 (checked by in_hard_reg_set_p), we need to insert the reload.
1339 2. If the op_type is OP_OUT or OP_INOUT.
1341 Here is a paradoxical subreg example showing how the reload is generated:
1343 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1344 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1346 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1347 here, if reg107 is assigned to hardreg R15, because R15 is the last
1348 hardreg, compiler cannot find another hardreg to pair with R15 to
1349 contain TImode data. So we insert a TImode reload reg180 for it.
1350 After reload is inserted:
1352 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1353 (reg:DI 107 [ __comp ])) -1
1354 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1355 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1357 Two reload hard registers will be allocated to reg180 to save TImode data
1358 in LRA_assign. */
1359 else if (REG_P (reg)
1360 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1361 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1362 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1363 < hard_regno_nregs[hard_regno][mode])
1364 && (regclass = lra_get_allocno_class (REGNO (reg)))
1365 && (type != OP_IN
1366 || !in_hard_reg_set_p (reg_class_contents[regclass],
1367 mode, hard_regno)))
1369 /* The class will be defined later in curr_insn_transform. */
1370 enum reg_class rclass
1371 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1373 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1374 rclass, TRUE, "paradoxical subreg", &new_reg))
1376 rtx subreg;
1377 bool insert_before, insert_after;
1379 PUT_MODE (new_reg, mode);
1380 subreg = simplify_gen_subreg (GET_MODE (reg), new_reg, mode, 0);
1381 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1383 insert_before = (type != OP_OUT);
1384 insert_after = (type != OP_IN);
1385 insert_move_for_subreg (insert_before ? &before : NULL,
1386 insert_after ? &after : NULL,
1387 reg, subreg);
1389 SUBREG_REG (operand) = new_reg;
1390 lra_process_new_insns (curr_insn, before, after,
1391 "Inserting paradoxical subreg reload");
1392 return true;
1394 return false;
1397 /* Return TRUE if X refers for a hard register from SET. */
1398 static bool
1399 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1401 int i, j, x_hard_regno;
1402 enum machine_mode mode;
1403 const char *fmt;
1404 enum rtx_code code;
1406 if (x == NULL_RTX)
1407 return false;
1408 code = GET_CODE (x);
1409 mode = GET_MODE (x);
1410 if (code == SUBREG)
1412 x = SUBREG_REG (x);
1413 code = GET_CODE (x);
1414 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1415 mode = GET_MODE (x);
1418 if (REG_P (x))
1420 x_hard_regno = get_hard_regno (x);
1421 return (x_hard_regno >= 0
1422 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1424 if (MEM_P (x))
1426 struct address_info ad;
1428 decompose_mem_address (&ad, x);
1429 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1430 return true;
1431 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1432 return true;
1434 fmt = GET_RTX_FORMAT (code);
1435 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1437 if (fmt[i] == 'e')
1439 if (uses_hard_regs_p (XEXP (x, i), set))
1440 return true;
1442 else if (fmt[i] == 'E')
1444 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1445 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1446 return true;
1449 return false;
1452 /* Return true if OP is a spilled pseudo. */
1453 static inline bool
1454 spilled_pseudo_p (rtx op)
1456 return (REG_P (op)
1457 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1460 /* Return true if X is a general constant. */
1461 static inline bool
1462 general_constant_p (rtx x)
1464 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1467 static bool
1468 reg_in_class_p (rtx reg, enum reg_class cl)
1470 if (cl == NO_REGS)
1471 return get_reg_class (REGNO (reg)) == NO_REGS;
1472 return in_class_p (reg, cl, NULL);
1475 /* Major function to choose the current insn alternative and what
1476 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1477 negative we should consider only this alternative. Return false if
1478 we can not choose the alternative or find how to reload the
1479 operands. */
1480 static bool
1481 process_alt_operands (int only_alternative)
1483 bool ok_p = false;
1484 int nop, overall, nalt;
1485 int n_alternatives = curr_static_id->n_alternatives;
1486 int n_operands = curr_static_id->n_operands;
1487 /* LOSERS counts the operands that don't fit this alternative and
1488 would require loading. */
1489 int losers;
1490 /* REJECT is a count of how undesirable this alternative says it is
1491 if any reloading is required. If the alternative matches exactly
1492 then REJECT is ignored, but otherwise it gets this much counted
1493 against it in addition to the reloading needed. */
1494 int reject;
1495 /* The number of elements in the following array. */
1496 int early_clobbered_regs_num;
1497 /* Numbers of operands which are early clobber registers. */
1498 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1499 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1500 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1501 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1502 bool curr_alt_win[MAX_RECOG_OPERANDS];
1503 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1504 int curr_alt_matches[MAX_RECOG_OPERANDS];
1505 /* The number of elements in the following array. */
1506 int curr_alt_dont_inherit_ops_num;
1507 /* Numbers of operands whose reload pseudos should not be inherited. */
1508 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1509 rtx op;
1510 /* The register when the operand is a subreg of register, otherwise the
1511 operand itself. */
1512 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1513 /* The register if the operand is a register or subreg of register,
1514 otherwise NULL. */
1515 rtx operand_reg[MAX_RECOG_OPERANDS];
1516 int hard_regno[MAX_RECOG_OPERANDS];
1517 enum machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1518 int reload_nregs, reload_sum;
1519 bool costly_p;
1520 enum reg_class cl;
1522 /* Calculate some data common for all alternatives to speed up the
1523 function. */
1524 for (nop = 0; nop < n_operands; nop++)
1526 rtx reg;
1528 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1529 /* The real hard regno of the operand after the allocation. */
1530 hard_regno[nop] = get_hard_regno (op);
1532 operand_reg[nop] = reg = op;
1533 biggest_mode[nop] = GET_MODE (op);
1534 if (GET_CODE (op) == SUBREG)
1536 operand_reg[nop] = reg = SUBREG_REG (op);
1537 if (GET_MODE_SIZE (biggest_mode[nop])
1538 < GET_MODE_SIZE (GET_MODE (reg)))
1539 biggest_mode[nop] = GET_MODE (reg);
1541 if (! REG_P (reg))
1542 operand_reg[nop] = NULL_RTX;
1543 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1544 || ((int) REGNO (reg)
1545 == lra_get_elimination_hard_regno (REGNO (reg))))
1546 no_subreg_reg_operand[nop] = reg;
1547 else
1548 operand_reg[nop] = no_subreg_reg_operand[nop]
1549 /* Just use natural mode for elimination result. It should
1550 be enough for extra constraints hooks. */
1551 = regno_reg_rtx[hard_regno[nop]];
1554 /* The constraints are made of several alternatives. Each operand's
1555 constraint looks like foo,bar,... with commas separating the
1556 alternatives. The first alternatives for all operands go
1557 together, the second alternatives go together, etc.
1559 First loop over alternatives. */
1560 for (nalt = 0; nalt < n_alternatives; nalt++)
1562 /* Loop over operands for one constraint alternative. */
1563 #if HAVE_ATTR_enabled
1564 if (curr_id->alternative_enabled_p != NULL
1565 && ! curr_id->alternative_enabled_p[nalt])
1566 continue;
1567 #endif
1569 if (only_alternative >= 0 && nalt != only_alternative)
1570 continue;
1573 overall = losers = reject = reload_nregs = reload_sum = 0;
1574 for (nop = 0; nop < n_operands; nop++)
1576 int inc = (curr_static_id
1577 ->operand_alternative[nalt * n_operands + nop].reject);
1578 if (lra_dump_file != NULL && inc != 0)
1579 fprintf (lra_dump_file,
1580 " Staticly defined alt reject+=%d\n", inc);
1581 reject += inc;
1583 early_clobbered_regs_num = 0;
1585 for (nop = 0; nop < n_operands; nop++)
1587 const char *p;
1588 char *end;
1589 int len, c, m, i, opalt_num, this_alternative_matches;
1590 bool win, did_match, offmemok, early_clobber_p;
1591 /* false => this operand can be reloaded somehow for this
1592 alternative. */
1593 bool badop;
1594 /* true => this operand can be reloaded if the alternative
1595 allows regs. */
1596 bool winreg;
1597 /* True if a constant forced into memory would be OK for
1598 this operand. */
1599 bool constmemok;
1600 enum reg_class this_alternative, this_costly_alternative;
1601 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1602 bool this_alternative_match_win, this_alternative_win;
1603 bool this_alternative_offmemok;
1604 bool scratch_p;
1605 enum machine_mode mode;
1607 opalt_num = nalt * n_operands + nop;
1608 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1610 /* Fast track for no constraints at all. */
1611 curr_alt[nop] = NO_REGS;
1612 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1613 curr_alt_win[nop] = true;
1614 curr_alt_match_win[nop] = false;
1615 curr_alt_offmemok[nop] = false;
1616 curr_alt_matches[nop] = -1;
1617 continue;
1620 op = no_subreg_reg_operand[nop];
1621 mode = curr_operand_mode[nop];
1623 win = did_match = winreg = offmemok = constmemok = false;
1624 badop = true;
1626 early_clobber_p = false;
1627 p = curr_static_id->operand_alternative[opalt_num].constraint;
1629 this_costly_alternative = this_alternative = NO_REGS;
1630 /* We update set of possible hard regs besides its class
1631 because reg class might be inaccurate. For example,
1632 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1633 is translated in HI_REGS because classes are merged by
1634 pairs and there is no accurate intermediate class. */
1635 CLEAR_HARD_REG_SET (this_alternative_set);
1636 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1637 this_alternative_win = false;
1638 this_alternative_match_win = false;
1639 this_alternative_offmemok = false;
1640 this_alternative_matches = -1;
1642 /* An empty constraint should be excluded by the fast
1643 track. */
1644 lra_assert (*p != 0 && *p != ',');
1646 /* Scan this alternative's specs for this operand; set WIN
1647 if the operand fits any letter in this alternative.
1648 Otherwise, clear BADOP if this operand could fit some
1649 letter after reloads, or set WINREG if this operand could
1650 fit after reloads provided the constraint allows some
1651 registers. */
1652 costly_p = false;
1655 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1657 case '\0':
1658 len = 0;
1659 break;
1660 case ',':
1661 c = '\0';
1662 break;
1664 case '=': case '+': case '?': case '*': case '!':
1665 case ' ': case '\t':
1666 break;
1668 case '%':
1669 /* We only support one commutative marker, the first
1670 one. We already set commutative above. */
1671 break;
1673 case '&':
1674 early_clobber_p = true;
1675 break;
1677 case '#':
1678 /* Ignore rest of this alternative. */
1679 c = '\0';
1680 break;
1682 case '0': case '1': case '2': case '3': case '4':
1683 case '5': case '6': case '7': case '8': case '9':
1685 int m_hregno;
1686 bool match_p;
1688 m = strtoul (p, &end, 10);
1689 p = end;
1690 len = 0;
1691 lra_assert (nop > m);
1693 this_alternative_matches = m;
1694 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1695 /* We are supposed to match a previous operand.
1696 If we do, we win if that one did. If we do
1697 not, count both of the operands as losers.
1698 (This is too conservative, since most of the
1699 time only a single reload insn will be needed
1700 to make the two operands win. As a result,
1701 this alternative may be rejected when it is
1702 actually desirable.) */
1703 match_p = false;
1704 if (operands_match_p (*curr_id->operand_loc[nop],
1705 *curr_id->operand_loc[m], m_hregno))
1707 /* We should reject matching of an early
1708 clobber operand if the matching operand is
1709 not dying in the insn. */
1710 if (! curr_static_id->operand[m].early_clobber
1711 || operand_reg[nop] == NULL_RTX
1712 || (find_regno_note (curr_insn, REG_DEAD,
1713 REGNO (op))
1714 || REGNO (op) == REGNO (operand_reg[m])))
1715 match_p = true;
1717 if (match_p)
1719 /* If we are matching a non-offsettable
1720 address where an offsettable address was
1721 expected, then we must reject this
1722 combination, because we can't reload
1723 it. */
1724 if (curr_alt_offmemok[m]
1725 && MEM_P (*curr_id->operand_loc[m])
1726 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1727 continue;
1729 else
1731 /* Operands don't match. Both operands must
1732 allow a reload register, otherwise we
1733 cannot make them match. */
1734 if (curr_alt[m] == NO_REGS)
1735 break;
1736 /* Retroactively mark the operand we had to
1737 match as a loser, if it wasn't already and
1738 it wasn't matched to a register constraint
1739 (e.g it might be matched by memory). */
1740 if (curr_alt_win[m]
1741 && (operand_reg[m] == NULL_RTX
1742 || hard_regno[m] < 0))
1744 losers++;
1745 reload_nregs
1746 += (ira_reg_class_max_nregs[curr_alt[m]]
1747 [GET_MODE (*curr_id->operand_loc[m])]);
1750 /* Prefer matching earlyclobber alternative as
1751 it results in less hard regs required for
1752 the insn than a non-matching earlyclobber
1753 alternative. */
1754 if (curr_static_id->operand[m].early_clobber)
1756 if (lra_dump_file != NULL)
1757 fprintf
1758 (lra_dump_file,
1759 " %d Matching earlyclobber alt:"
1760 " reject--\n",
1761 nop);
1762 reject--;
1764 /* Otherwise we prefer no matching
1765 alternatives because it gives more freedom
1766 in RA. */
1767 else if (operand_reg[nop] == NULL_RTX
1768 || (find_regno_note (curr_insn, REG_DEAD,
1769 REGNO (operand_reg[nop]))
1770 == NULL_RTX))
1772 if (lra_dump_file != NULL)
1773 fprintf
1774 (lra_dump_file,
1775 " %d Matching alt: reject+=2\n",
1776 nop);
1777 reject += 2;
1780 /* If we have to reload this operand and some
1781 previous operand also had to match the same
1782 thing as this operand, we don't know how to do
1783 that. */
1784 if (!match_p || !curr_alt_win[m])
1786 for (i = 0; i < nop; i++)
1787 if (curr_alt_matches[i] == m)
1788 break;
1789 if (i < nop)
1790 break;
1792 else
1793 did_match = true;
1795 /* This can be fixed with reloads if the operand
1796 we are supposed to match can be fixed with
1797 reloads. */
1798 badop = false;
1799 this_alternative = curr_alt[m];
1800 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1801 winreg = this_alternative != NO_REGS;
1802 break;
1805 case 'p':
1806 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1807 ADDRESS, SCRATCH);
1808 this_alternative = reg_class_subunion[this_alternative][cl];
1809 IOR_HARD_REG_SET (this_alternative_set,
1810 reg_class_contents[cl]);
1811 if (costly_p)
1813 this_costly_alternative
1814 = reg_class_subunion[this_costly_alternative][cl];
1815 IOR_HARD_REG_SET (this_costly_alternative_set,
1816 reg_class_contents[cl]);
1818 win = true;
1819 badop = false;
1820 break;
1822 case TARGET_MEM_CONSTRAINT:
1823 if (MEM_P (op) || spilled_pseudo_p (op))
1824 win = true;
1825 /* We can put constant or pseudo value into memory
1826 to satisfy the constraint. */
1827 if (CONST_POOL_OK_P (mode, op) || REG_P (op))
1828 badop = false;
1829 constmemok = true;
1830 break;
1832 case '<':
1833 if (MEM_P (op)
1834 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
1835 || GET_CODE (XEXP (op, 0)) == POST_DEC))
1836 win = true;
1837 break;
1839 case '>':
1840 if (MEM_P (op)
1841 && (GET_CODE (XEXP (op, 0)) == PRE_INC
1842 || GET_CODE (XEXP (op, 0)) == POST_INC))
1843 win = true;
1844 break;
1846 /* Memory op whose address is not offsettable. */
1847 case 'V':
1848 if (MEM_P (op)
1849 && ! offsettable_nonstrict_memref_p (op))
1850 win = true;
1851 break;
1853 /* Memory operand whose address is offsettable. */
1854 case 'o':
1855 if ((MEM_P (op)
1856 && offsettable_nonstrict_memref_p (op))
1857 || spilled_pseudo_p (op))
1858 win = true;
1859 /* We can put constant or pseudo value into memory
1860 or make memory address offsetable to satisfy the
1861 constraint. */
1862 if (CONST_POOL_OK_P (mode, op) || MEM_P (op) || REG_P (op))
1863 badop = false;
1864 constmemok = true;
1865 offmemok = true;
1866 break;
1868 case 'E':
1869 case 'F':
1870 if (GET_CODE (op) == CONST_DOUBLE
1871 || (GET_CODE (op) == CONST_VECTOR
1872 && (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)))
1873 win = true;
1874 break;
1876 case 'G':
1877 case 'H':
1878 if (CONST_DOUBLE_AS_FLOAT_P (op)
1879 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
1880 win = true;
1881 break;
1883 case 's':
1884 if (CONST_SCALAR_INT_P (op))
1885 break;
1887 case 'i':
1888 if (general_constant_p (op))
1889 win = true;
1890 break;
1892 case 'n':
1893 if (CONST_SCALAR_INT_P (op))
1894 win = true;
1895 break;
1897 case 'I':
1898 case 'J':
1899 case 'K':
1900 case 'L':
1901 case 'M':
1902 case 'N':
1903 case 'O':
1904 case 'P':
1905 if (CONST_INT_P (op)
1906 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
1907 win = true;
1908 break;
1910 case 'X':
1911 /* This constraint should be excluded by the fast
1912 track. */
1913 gcc_unreachable ();
1914 break;
1916 case 'g':
1917 if (MEM_P (op)
1918 || general_constant_p (op)
1919 || spilled_pseudo_p (op))
1920 win = true;
1921 /* Drop through into 'r' case. */
1923 case 'r':
1924 this_alternative
1925 = reg_class_subunion[this_alternative][GENERAL_REGS];
1926 IOR_HARD_REG_SET (this_alternative_set,
1927 reg_class_contents[GENERAL_REGS]);
1928 if (costly_p)
1930 this_costly_alternative
1931 = (reg_class_subunion
1932 [this_costly_alternative][GENERAL_REGS]);
1933 IOR_HARD_REG_SET (this_costly_alternative_set,
1934 reg_class_contents[GENERAL_REGS]);
1936 goto reg;
1938 default:
1939 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
1941 #ifdef EXTRA_CONSTRAINT_STR
1942 if (EXTRA_MEMORY_CONSTRAINT (c, p))
1944 if (EXTRA_CONSTRAINT_STR (op, c, p))
1945 win = true;
1946 else if (spilled_pseudo_p (op))
1947 win = true;
1949 /* If we didn't already win, we can reload
1950 constants via force_const_mem or put the
1951 pseudo value into memory, or make other
1952 memory by reloading the address like for
1953 'o'. */
1954 if (CONST_POOL_OK_P (mode, op)
1955 || MEM_P (op) || REG_P (op))
1956 badop = false;
1957 constmemok = true;
1958 offmemok = true;
1959 break;
1961 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1963 if (EXTRA_CONSTRAINT_STR (op, c, p))
1964 win = true;
1966 /* If we didn't already win, we can reload
1967 the address into a base register. */
1968 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1969 ADDRESS, SCRATCH);
1970 this_alternative
1971 = reg_class_subunion[this_alternative][cl];
1972 IOR_HARD_REG_SET (this_alternative_set,
1973 reg_class_contents[cl]);
1974 if (costly_p)
1976 this_costly_alternative
1977 = (reg_class_subunion
1978 [this_costly_alternative][cl]);
1979 IOR_HARD_REG_SET (this_costly_alternative_set,
1980 reg_class_contents[cl]);
1982 badop = false;
1983 break;
1986 if (EXTRA_CONSTRAINT_STR (op, c, p))
1987 win = true;
1988 #endif
1989 break;
1992 cl = REG_CLASS_FROM_CONSTRAINT (c, p);
1993 this_alternative = reg_class_subunion[this_alternative][cl];
1994 IOR_HARD_REG_SET (this_alternative_set,
1995 reg_class_contents[cl]);
1996 if (costly_p)
1998 this_costly_alternative
1999 = reg_class_subunion[this_costly_alternative][cl];
2000 IOR_HARD_REG_SET (this_costly_alternative_set,
2001 reg_class_contents[cl]);
2003 reg:
2004 if (mode == BLKmode)
2005 break;
2006 winreg = true;
2007 if (REG_P (op))
2009 if (hard_regno[nop] >= 0
2010 && in_hard_reg_set_p (this_alternative_set,
2011 mode, hard_regno[nop]))
2012 win = true;
2013 else if (hard_regno[nop] < 0
2014 && in_class_p (op, this_alternative, NULL))
2015 win = true;
2017 break;
2019 if (c != ' ' && c != '\t')
2020 costly_p = c == '*';
2022 while ((p += len), c);
2024 scratch_p = (operand_reg[nop] != NULL_RTX
2025 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2026 /* Record which operands fit this alternative. */
2027 if (win)
2029 this_alternative_win = true;
2030 if (operand_reg[nop] != NULL_RTX)
2032 if (hard_regno[nop] >= 0)
2034 if (in_hard_reg_set_p (this_costly_alternative_set,
2035 mode, hard_regno[nop]))
2037 if (lra_dump_file != NULL)
2038 fprintf (lra_dump_file,
2039 " %d Costly set: reject++\n",
2040 nop);
2041 reject++;
2044 else
2046 /* Prefer won reg to spilled pseudo under other
2047 equal conditions for possibe inheritance. */
2048 if (! scratch_p)
2050 if (lra_dump_file != NULL)
2051 fprintf
2052 (lra_dump_file,
2053 " %d Non pseudo reload: reject++\n",
2054 nop);
2055 reject++;
2057 if (in_class_p (operand_reg[nop],
2058 this_costly_alternative, NULL))
2060 if (lra_dump_file != NULL)
2061 fprintf
2062 (lra_dump_file,
2063 " %d Non pseudo costly reload:"
2064 " reject++\n",
2065 nop);
2066 reject++;
2069 /* We simulate the behaviour of old reload here.
2070 Although scratches need hard registers and it
2071 might result in spilling other pseudos, no reload
2072 insns are generated for the scratches. So it
2073 might cost something but probably less than old
2074 reload pass believes. */
2075 if (scratch_p)
2077 if (lra_dump_file != NULL)
2078 fprintf (lra_dump_file,
2079 " %d Scratch win: reject+=2\n",
2080 nop);
2081 reject += 2;
2085 else if (did_match)
2086 this_alternative_match_win = true;
2087 else
2089 int const_to_mem = 0;
2090 bool no_regs_p;
2092 /* Never do output reload of stack pointer. It makes
2093 impossible to do elimination when SP is changed in
2094 RTL. */
2095 if (op == stack_pointer_rtx && ! frame_pointer_needed
2096 && curr_static_id->operand[nop].type != OP_IN)
2097 goto fail;
2099 /* If this alternative asks for a specific reg class, see if there
2100 is at least one allocatable register in that class. */
2101 no_regs_p
2102 = (this_alternative == NO_REGS
2103 || (hard_reg_set_subset_p
2104 (reg_class_contents[this_alternative],
2105 lra_no_alloc_regs)));
2107 /* For asms, verify that the class for this alternative is possible
2108 for the mode that is specified. */
2109 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2111 int i;
2112 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2113 if (HARD_REGNO_MODE_OK (i, mode)
2114 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2115 mode, i))
2116 break;
2117 if (i == FIRST_PSEUDO_REGISTER)
2118 winreg = false;
2121 /* If this operand accepts a register, and if the
2122 register class has at least one allocatable register,
2123 then this operand can be reloaded. */
2124 if (winreg && !no_regs_p)
2125 badop = false;
2127 if (badop)
2129 if (lra_dump_file != NULL)
2130 fprintf (lra_dump_file,
2131 " alt=%d: Bad operand -- refuse\n",
2132 nalt);
2133 goto fail;
2136 /* If not assigned pseudo has a class which a subset of
2137 required reg class, it is a less costly alternative
2138 as the pseudo still can get a hard reg of necessary
2139 class. */
2140 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2141 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2142 && ira_class_subset_p[this_alternative][cl])
2144 if (lra_dump_file != NULL)
2145 fprintf
2146 (lra_dump_file,
2147 " %d Super set class reg: reject-=3\n", nop);
2148 reject -= 3;
2151 this_alternative_offmemok = offmemok;
2152 if (this_costly_alternative != NO_REGS)
2154 if (lra_dump_file != NULL)
2155 fprintf (lra_dump_file,
2156 " %d Costly loser: reject++\n", nop);
2157 reject++;
2159 /* If the operand is dying, has a matching constraint,
2160 and satisfies constraints of the matched operand
2161 which failed to satisfy the own constraints, most probably
2162 the reload for this operand will be gone. */
2163 if (this_alternative_matches >= 0
2164 && !curr_alt_win[this_alternative_matches]
2165 && REG_P (op)
2166 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2167 && (hard_regno[nop] >= 0
2168 ? in_hard_reg_set_p (this_alternative_set,
2169 mode, hard_regno[nop])
2170 : in_class_p (op, this_alternative, NULL)))
2172 if (lra_dump_file != NULL)
2173 fprintf
2174 (lra_dump_file,
2175 " %d Dying matched operand reload: reject++\n",
2176 nop);
2177 reject++;
2179 else
2181 /* Strict_low_part requires to reload the register
2182 not the sub-register. In this case we should
2183 check that a final reload hard reg can hold the
2184 value mode. */
2185 if (curr_static_id->operand[nop].strict_low
2186 && REG_P (op)
2187 && hard_regno[nop] < 0
2188 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2189 && ira_class_hard_regs_num[this_alternative] > 0
2190 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2191 [this_alternative][0],
2192 GET_MODE
2193 (*curr_id->operand_loc[nop])))
2195 if (lra_dump_file != NULL)
2196 fprintf
2197 (lra_dump_file,
2198 " alt=%d: Strict low subreg reload -- refuse\n",
2199 nalt);
2200 goto fail;
2202 losers++;
2204 if (operand_reg[nop] != NULL_RTX
2205 /* Output operands and matched input operands are
2206 not inherited. The following conditions do not
2207 exactly describe the previous statement but they
2208 are pretty close. */
2209 && curr_static_id->operand[nop].type != OP_OUT
2210 && (this_alternative_matches < 0
2211 || curr_static_id->operand[nop].type != OP_IN))
2213 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2214 (operand_reg[nop])]
2215 .last_reload);
2217 /* The value of reload_sum has sense only if we
2218 process insns in their order. It happens only on
2219 the first constraints sub-pass when we do most of
2220 reload work. */
2221 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2222 reload_sum += last_reload - bb_reload_num;
2224 /* If this is a constant that is reloaded into the
2225 desired class by copying it to memory first, count
2226 that as another reload. This is consistent with
2227 other code and is required to avoid choosing another
2228 alternative when the constant is moved into memory.
2229 Note that the test here is precisely the same as in
2230 the code below that calls force_const_mem. */
2231 if (CONST_POOL_OK_P (mode, op)
2232 && ((targetm.preferred_reload_class
2233 (op, this_alternative) == NO_REGS)
2234 || no_input_reloads_p))
2236 const_to_mem = 1;
2237 if (! no_regs_p)
2238 losers++;
2241 /* Alternative loses if it requires a type of reload not
2242 permitted for this insn. We can always reload
2243 objects with a REG_UNUSED note. */
2244 if ((curr_static_id->operand[nop].type != OP_IN
2245 && no_output_reloads_p
2246 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2247 || (curr_static_id->operand[nop].type != OP_OUT
2248 && no_input_reloads_p && ! const_to_mem)
2249 || (this_alternative_matches >= 0
2250 && (no_input_reloads_p
2251 || (no_output_reloads_p
2252 && (curr_static_id->operand
2253 [this_alternative_matches].type != OP_IN)
2254 && ! find_reg_note (curr_insn, REG_UNUSED,
2255 no_subreg_reg_operand
2256 [this_alternative_matches])))))
2258 if (lra_dump_file != NULL)
2259 fprintf
2260 (lra_dump_file,
2261 " alt=%d: No input/otput reload -- refuse\n",
2262 nalt);
2263 goto fail;
2266 /* Check strong discouragement of reload of non-constant
2267 into class THIS_ALTERNATIVE. */
2268 if (! CONSTANT_P (op) && ! no_regs_p
2269 && (targetm.preferred_reload_class
2270 (op, this_alternative) == NO_REGS
2271 || (curr_static_id->operand[nop].type == OP_OUT
2272 && (targetm.preferred_output_reload_class
2273 (op, this_alternative) == NO_REGS))))
2275 if (lra_dump_file != NULL)
2276 fprintf (lra_dump_file,
2277 " %d Non-prefered reload: reject+=%d\n",
2278 nop, LRA_MAX_REJECT);
2279 reject += LRA_MAX_REJECT;
2282 if (! (MEM_P (op) && offmemok)
2283 && ! (const_to_mem && constmemok))
2285 /* We prefer to reload pseudos over reloading other
2286 things, since such reloads may be able to be
2287 eliminated later. So bump REJECT in other cases.
2288 Don't do this in the case where we are forcing a
2289 constant into memory and it will then win since
2290 we don't want to have a different alternative
2291 match then. */
2292 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2294 if (lra_dump_file != NULL)
2295 fprintf
2296 (lra_dump_file,
2297 " %d Non-pseudo reload: reject+=2\n",
2298 nop);
2299 reject += 2;
2302 if (! no_regs_p)
2303 reload_nregs
2304 += ira_reg_class_max_nregs[this_alternative][mode];
2306 if (SMALL_REGISTER_CLASS_P (this_alternative))
2308 if (lra_dump_file != NULL)
2309 fprintf
2310 (lra_dump_file,
2311 " %d Small class reload: reject+=%d\n",
2312 nop, LRA_LOSER_COST_FACTOR / 2);
2313 reject += LRA_LOSER_COST_FACTOR / 2;
2317 /* We are trying to spill pseudo into memory. It is
2318 usually more costly than moving to a hard register
2319 although it might takes the same number of
2320 reloads. */
2321 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2323 if (lra_dump_file != NULL)
2324 fprintf
2325 (lra_dump_file,
2326 " %d Spill pseudo into memory: reject+=3\n",
2327 nop);
2328 reject += 3;
2329 if (VECTOR_MODE_P (mode))
2331 /* Spilling vectors into memory is usually more
2332 costly as they contain big values. */
2333 if (lra_dump_file != NULL)
2334 fprintf
2335 (lra_dump_file,
2336 " %d Spill vector pseudo: reject+=2\n",
2337 nop);
2338 reject += 2;
2342 #ifdef SECONDARY_MEMORY_NEEDED
2343 /* If reload requires moving value through secondary
2344 memory, it will need one more insn at least. */
2345 if (this_alternative != NO_REGS
2346 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2347 && ((curr_static_id->operand[nop].type != OP_OUT
2348 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2349 GET_MODE (op)))
2350 || (curr_static_id->operand[nop].type != OP_IN
2351 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2352 GET_MODE (op)))))
2353 losers++;
2354 #endif
2355 /* Input reloads can be inherited more often than output
2356 reloads can be removed, so penalize output
2357 reloads. */
2358 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2360 if (lra_dump_file != NULL)
2361 fprintf
2362 (lra_dump_file,
2363 " %d Non input pseudo reload: reject++\n",
2364 nop);
2365 reject++;
2369 if (early_clobber_p && ! scratch_p)
2371 if (lra_dump_file != NULL)
2372 fprintf (lra_dump_file,
2373 " %d Early clobber: reject++\n", nop);
2374 reject++;
2376 /* ??? We check early clobbers after processing all operands
2377 (see loop below) and there we update the costs more.
2378 Should we update the cost (may be approximately) here
2379 because of early clobber register reloads or it is a rare
2380 or non-important thing to be worth to do it. */
2381 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2382 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2384 if (lra_dump_file != NULL)
2385 fprintf (lra_dump_file,
2386 " alt=%d,overall=%d,losers=%d -- refuse\n",
2387 nalt, overall, losers);
2388 goto fail;
2391 curr_alt[nop] = this_alternative;
2392 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2393 curr_alt_win[nop] = this_alternative_win;
2394 curr_alt_match_win[nop] = this_alternative_match_win;
2395 curr_alt_offmemok[nop] = this_alternative_offmemok;
2396 curr_alt_matches[nop] = this_alternative_matches;
2398 if (this_alternative_matches >= 0
2399 && !did_match && !this_alternative_win)
2400 curr_alt_win[this_alternative_matches] = false;
2402 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2403 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2405 if (curr_insn_set != NULL_RTX && n_operands == 2
2406 /* Prevent processing non-move insns. */
2407 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2408 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2409 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2410 && REG_P (no_subreg_reg_operand[0])
2411 && REG_P (no_subreg_reg_operand[1])
2412 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2413 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2414 || (! curr_alt_win[0] && curr_alt_win[1]
2415 && REG_P (no_subreg_reg_operand[1])
2416 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2417 || (curr_alt_win[0] && ! curr_alt_win[1]
2418 && REG_P (no_subreg_reg_operand[0])
2419 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2420 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2421 no_subreg_reg_operand[1])
2422 || (targetm.preferred_reload_class
2423 (no_subreg_reg_operand[1],
2424 (enum reg_class) curr_alt[1]) != NO_REGS))
2425 /* If it is a result of recent elimination in move
2426 insn we can transform it into an add still by
2427 using this alternative. */
2428 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2430 /* We have a move insn and a new reload insn will be similar
2431 to the current insn. We should avoid such situation as it
2432 results in LRA cycling. */
2433 overall += LRA_MAX_REJECT;
2435 ok_p = true;
2436 curr_alt_dont_inherit_ops_num = 0;
2437 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2439 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2440 HARD_REG_SET temp_set;
2442 i = early_clobbered_nops[nop];
2443 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2444 || hard_regno[i] < 0)
2445 continue;
2446 lra_assert (operand_reg[i] != NULL_RTX);
2447 clobbered_hard_regno = hard_regno[i];
2448 CLEAR_HARD_REG_SET (temp_set);
2449 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2450 first_conflict_j = last_conflict_j = -1;
2451 for (j = 0; j < n_operands; j++)
2452 if (j == i
2453 /* We don't want process insides of match_operator and
2454 match_parallel because otherwise we would process
2455 their operands once again generating a wrong
2456 code. */
2457 || curr_static_id->operand[j].is_operator)
2458 continue;
2459 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2460 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2461 continue;
2462 /* If we don't reload j-th operand, check conflicts. */
2463 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2464 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2466 if (first_conflict_j < 0)
2467 first_conflict_j = j;
2468 last_conflict_j = j;
2470 if (last_conflict_j < 0)
2471 continue;
2472 /* If earlyclobber operand conflicts with another
2473 non-matching operand which is actually the same register
2474 as the earlyclobber operand, it is better to reload the
2475 another operand as an operand matching the earlyclobber
2476 operand can be also the same. */
2477 if (first_conflict_j == last_conflict_j
2478 && operand_reg[last_conflict_j]
2479 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2480 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2482 curr_alt_win[last_conflict_j] = false;
2483 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2484 = last_conflict_j;
2485 losers++;
2486 /* Early clobber was already reflected in REJECT. */
2487 lra_assert (reject > 0);
2488 if (lra_dump_file != NULL)
2489 fprintf
2490 (lra_dump_file,
2491 " %d Conflict early clobber reload: reject--\n",
2493 reject--;
2494 overall += LRA_LOSER_COST_FACTOR - 1;
2496 else
2498 /* We need to reload early clobbered register and the
2499 matched registers. */
2500 for (j = 0; j < n_operands; j++)
2501 if (curr_alt_matches[j] == i)
2503 curr_alt_match_win[j] = false;
2504 losers++;
2505 overall += LRA_LOSER_COST_FACTOR;
2507 if (! curr_alt_match_win[i])
2508 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2509 else
2511 /* Remember pseudos used for match reloads are never
2512 inherited. */
2513 lra_assert (curr_alt_matches[i] >= 0);
2514 curr_alt_win[curr_alt_matches[i]] = false;
2516 curr_alt_win[i] = curr_alt_match_win[i] = false;
2517 losers++;
2518 /* Early clobber was already reflected in REJECT. */
2519 lra_assert (reject > 0);
2520 if (lra_dump_file != NULL)
2521 fprintf
2522 (lra_dump_file,
2523 " %d Matched conflict early clobber reloads:"
2524 "reject--\n",
2526 reject--;
2527 overall += LRA_LOSER_COST_FACTOR - 1;
2530 if (lra_dump_file != NULL)
2531 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2532 nalt, overall, losers, reload_nregs);
2534 /* If this alternative can be made to work by reloading, and it
2535 needs less reloading than the others checked so far, record
2536 it as the chosen goal for reloading. */
2537 if ((best_losers != 0 && losers == 0)
2538 || (((best_losers == 0 && losers == 0)
2539 || (best_losers != 0 && losers != 0))
2540 && (best_overall > overall
2541 || (best_overall == overall
2542 /* If the cost of the reloads is the same,
2543 prefer alternative which requires minimal
2544 number of reload regs. */
2545 && (reload_nregs < best_reload_nregs
2546 || (reload_nregs == best_reload_nregs
2547 && (best_reload_sum < reload_sum
2548 || (best_reload_sum == reload_sum
2549 && nalt < goal_alt_number))))))))
2551 for (nop = 0; nop < n_operands; nop++)
2553 goal_alt_win[nop] = curr_alt_win[nop];
2554 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2555 goal_alt_matches[nop] = curr_alt_matches[nop];
2556 goal_alt[nop] = curr_alt[nop];
2557 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2559 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2560 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2561 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2562 goal_alt_swapped = curr_swapped;
2563 best_overall = overall;
2564 best_losers = losers;
2565 best_reload_nregs = reload_nregs;
2566 best_reload_sum = reload_sum;
2567 goal_alt_number = nalt;
2569 if (losers == 0)
2570 /* Everything is satisfied. Do not process alternatives
2571 anymore. */
2572 break;
2573 fail:
2576 return ok_p;
2579 /* Return 1 if ADDR is a valid memory address for mode MODE in address
2580 space AS, and check that each pseudo has the proper kind of hard
2581 reg. */
2582 static int
2583 valid_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2584 rtx addr, addr_space_t as)
2586 #ifdef GO_IF_LEGITIMATE_ADDRESS
2587 lra_assert (ADDR_SPACE_GENERIC_P (as));
2588 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
2589 return 0;
2591 win:
2592 return 1;
2593 #else
2594 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
2595 #endif
2598 /* Return whether address AD is valid. */
2600 static bool
2601 valid_address_p (struct address_info *ad)
2603 /* Some ports do not check displacements for eliminable registers,
2604 so we replace them temporarily with the elimination target. */
2605 rtx saved_base_reg = NULL_RTX;
2606 rtx saved_index_reg = NULL_RTX;
2607 rtx *base_term = strip_subreg (ad->base_term);
2608 rtx *index_term = strip_subreg (ad->index_term);
2609 if (base_term != NULL)
2611 saved_base_reg = *base_term;
2612 lra_eliminate_reg_if_possible (base_term);
2613 if (ad->base_term2 != NULL)
2614 *ad->base_term2 = *ad->base_term;
2616 if (index_term != NULL)
2618 saved_index_reg = *index_term;
2619 lra_eliminate_reg_if_possible (index_term);
2621 bool ok_p = valid_address_p (ad->mode, *ad->outer, ad->as);
2622 if (saved_base_reg != NULL_RTX)
2624 *base_term = saved_base_reg;
2625 if (ad->base_term2 != NULL)
2626 *ad->base_term2 = *ad->base_term;
2628 if (saved_index_reg != NULL_RTX)
2629 *index_term = saved_index_reg;
2630 return ok_p;
2633 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2634 static rtx
2635 base_plus_disp_to_reg (struct address_info *ad)
2637 enum reg_class cl;
2638 rtx new_reg;
2640 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2641 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2642 get_index_code (ad));
2643 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2644 cl, "base + disp");
2645 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2646 return new_reg;
2649 /* Make reload of index part of address AD. Return the new
2650 pseudo. */
2651 static rtx
2652 index_part_to_reg (struct address_info *ad)
2654 rtx new_reg;
2656 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2657 INDEX_REG_CLASS, "index term");
2658 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2659 GEN_INT (get_index_scale (ad)), new_reg, 1);
2660 return new_reg;
2663 /* Return true if we can add a displacement to address AD, even if that
2664 makes the address invalid. The fix-up code requires any new address
2665 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2666 static bool
2667 can_add_disp_p (struct address_info *ad)
2669 return (!ad->autoinc_p
2670 && ad->segment == NULL
2671 && ad->base == ad->base_term
2672 && ad->disp == ad->disp_term);
2675 /* Make equiv substitution in address AD. Return true if a substitution
2676 was made. */
2677 static bool
2678 equiv_address_substitution (struct address_info *ad)
2680 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2681 HOST_WIDE_INT disp, scale;
2682 bool change_p;
2684 base_term = strip_subreg (ad->base_term);
2685 if (base_term == NULL)
2686 base_reg = new_base_reg = NULL_RTX;
2687 else
2689 base_reg = *base_term;
2690 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2692 index_term = strip_subreg (ad->index_term);
2693 if (index_term == NULL)
2694 index_reg = new_index_reg = NULL_RTX;
2695 else
2697 index_reg = *index_term;
2698 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2700 if (base_reg == new_base_reg && index_reg == new_index_reg)
2701 return false;
2702 disp = 0;
2703 change_p = false;
2704 if (lra_dump_file != NULL)
2706 fprintf (lra_dump_file, "Changing address in insn %d ",
2707 INSN_UID (curr_insn));
2708 dump_value_slim (lra_dump_file, *ad->outer, 1);
2710 if (base_reg != new_base_reg)
2712 if (REG_P (new_base_reg))
2714 *base_term = new_base_reg;
2715 change_p = true;
2717 else if (GET_CODE (new_base_reg) == PLUS
2718 && REG_P (XEXP (new_base_reg, 0))
2719 && CONST_INT_P (XEXP (new_base_reg, 1))
2720 && can_add_disp_p (ad))
2722 disp += INTVAL (XEXP (new_base_reg, 1));
2723 *base_term = XEXP (new_base_reg, 0);
2724 change_p = true;
2726 if (ad->base_term2 != NULL)
2727 *ad->base_term2 = *ad->base_term;
2729 if (index_reg != new_index_reg)
2731 if (REG_P (new_index_reg))
2733 *index_term = new_index_reg;
2734 change_p = true;
2736 else if (GET_CODE (new_index_reg) == PLUS
2737 && REG_P (XEXP (new_index_reg, 0))
2738 && CONST_INT_P (XEXP (new_index_reg, 1))
2739 && can_add_disp_p (ad)
2740 && (scale = get_index_scale (ad)))
2742 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2743 *index_term = XEXP (new_index_reg, 0);
2744 change_p = true;
2747 if (disp != 0)
2749 if (ad->disp != NULL)
2750 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2751 else
2753 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2754 update_address (ad);
2756 change_p = true;
2758 if (lra_dump_file != NULL)
2760 if (! change_p)
2761 fprintf (lra_dump_file, " -- no change\n");
2762 else
2764 fprintf (lra_dump_file, " on equiv ");
2765 dump_value_slim (lra_dump_file, *ad->outer, 1);
2766 fprintf (lra_dump_file, "\n");
2769 return change_p;
2772 /* Major function to make reloads for an address in operand NOP.
2773 The supported cases are:
2775 1) an address that existed before LRA started, at which point it
2776 must have been valid. These addresses are subject to elimination
2777 and may have become invalid due to the elimination offset being out
2778 of range.
2780 2) an address created by forcing a constant to memory
2781 (force_const_to_mem). The initial form of these addresses might
2782 not be valid, and it is this function's job to make them valid.
2784 3) a frame address formed from a register and a (possibly zero)
2785 constant offset. As above, these addresses might not be valid and
2786 this function must make them so.
2788 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2789 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2790 address. Return true for any RTL change. */
2791 static bool
2792 process_address (int nop, rtx *before, rtx *after)
2794 struct address_info ad;
2795 rtx new_reg;
2796 rtx op = *curr_id->operand_loc[nop];
2797 const char *constraint = curr_static_id->operand[nop].constraint;
2798 bool change_p;
2800 if (constraint[0] == 'p'
2801 || EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint))
2802 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2803 else if (MEM_P (op))
2804 decompose_mem_address (&ad, op);
2805 else if (GET_CODE (op) == SUBREG
2806 && MEM_P (SUBREG_REG (op)))
2807 decompose_mem_address (&ad, SUBREG_REG (op));
2808 else
2809 return false;
2810 change_p = equiv_address_substitution (&ad);
2811 if (ad.base_term != NULL
2812 && (process_addr_reg
2813 (ad.base_term, before,
2814 (ad.autoinc_p
2815 && !(REG_P (*ad.base_term)
2816 && find_regno_note (curr_insn, REG_DEAD,
2817 REGNO (*ad.base_term)) != NULL_RTX)
2818 ? after : NULL),
2819 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2820 get_index_code (&ad)))))
2822 change_p = true;
2823 if (ad.base_term2 != NULL)
2824 *ad.base_term2 = *ad.base_term;
2826 if (ad.index_term != NULL
2827 && process_addr_reg (ad.index_term, before, NULL, INDEX_REG_CLASS))
2828 change_p = true;
2830 #ifdef EXTRA_CONSTRAINT_STR
2831 /* Target hooks sometimes reject extra constraint addresses -- use
2832 EXTRA_CONSTRAINT_STR for the validation. */
2833 if (constraint[0] != 'p'
2834 && EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint)
2835 && EXTRA_CONSTRAINT_STR (op, constraint[0], constraint))
2836 return change_p;
2837 #endif
2839 /* There are three cases where the shape of *AD.INNER may now be invalid:
2841 1) the original address was valid, but either elimination or
2842 equiv_address_substitution was applied and that made
2843 the address invalid.
2845 2) the address is an invalid symbolic address created by
2846 force_const_to_mem.
2848 3) the address is a frame address with an invalid offset.
2850 All these cases involve a non-autoinc address, so there is no
2851 point revalidating other types. */
2852 if (ad.autoinc_p || valid_address_p (&ad))
2853 return change_p;
2855 /* Any index existed before LRA started, so we can assume that the
2856 presence and shape of the index is valid. */
2857 push_to_sequence (*before);
2858 lra_assert (ad.disp == ad.disp_term);
2859 if (ad.base == NULL)
2861 if (ad.index == NULL)
2863 int code = -1;
2864 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2865 SCRATCH, SCRATCH);
2866 rtx addr = *ad.inner;
2868 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2869 #ifdef HAVE_lo_sum
2871 rtx insn;
2872 rtx last = get_last_insn ();
2874 /* addr => lo_sum (new_base, addr), case (2) above. */
2875 insn = emit_insn (gen_rtx_SET
2876 (VOIDmode, new_reg,
2877 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2878 code = recog_memoized (insn);
2879 if (code >= 0)
2881 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2882 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2884 /* Try to put lo_sum into register. */
2885 insn = emit_insn (gen_rtx_SET
2886 (VOIDmode, new_reg,
2887 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2888 code = recog_memoized (insn);
2889 if (code >= 0)
2891 *ad.inner = new_reg;
2892 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2894 *ad.inner = addr;
2895 code = -1;
2901 if (code < 0)
2902 delete_insns_since (last);
2904 #endif
2905 if (code < 0)
2907 /* addr => new_base, case (2) above. */
2908 lra_emit_move (new_reg, addr);
2909 *ad.inner = new_reg;
2912 else
2914 /* index * scale + disp => new base + index * scale,
2915 case (1) above. */
2916 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
2917 GET_CODE (*ad.index));
2919 lra_assert (INDEX_REG_CLASS != NO_REGS);
2920 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
2921 lra_emit_move (new_reg, *ad.disp);
2922 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2923 new_reg, *ad.index);
2926 else if (ad.index == NULL)
2928 int regno;
2929 enum reg_class cl;
2930 rtx set, insns, last_insn;
2931 /* base + disp => new base, cases (1) and (3) above. */
2932 /* Another option would be to reload the displacement into an
2933 index register. However, postreload has code to optimize
2934 address reloads that have the same base and different
2935 displacements, so reloading into an index register would
2936 not necessarily be a win. */
2937 start_sequence ();
2938 new_reg = base_plus_disp_to_reg (&ad);
2939 insns = get_insns ();
2940 last_insn = get_last_insn ();
2941 /* If we generated at least two insns, try last insn source as
2942 an address. If we succeed, we generate one less insn. */
2943 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
2944 && GET_CODE (SET_SRC (set)) == PLUS
2945 && REG_P (XEXP (SET_SRC (set), 0))
2946 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
2948 *ad.inner = SET_SRC (set);
2949 if (valid_address_p (ad.mode, *ad.outer, ad.as))
2951 *ad.base_term = XEXP (SET_SRC (set), 0);
2952 *ad.disp_term = XEXP (SET_SRC (set), 1);
2953 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2954 get_index_code (&ad));
2955 regno = REGNO (*ad.base_term);
2956 if (regno >= FIRST_PSEUDO_REGISTER
2957 && cl != lra_get_allocno_class (regno))
2958 lra_change_class (regno, cl, " Change to", true);
2959 new_reg = SET_SRC (set);
2960 delete_insns_since (PREV_INSN (last_insn));
2963 end_sequence ();
2964 emit_insn (insns);
2965 *ad.inner = new_reg;
2967 else if (ad.disp_term != NULL)
2969 /* base + scale * index + disp => new base + scale * index,
2970 case (1) above. */
2971 new_reg = base_plus_disp_to_reg (&ad);
2972 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2973 new_reg, *ad.index);
2975 else
2977 /* base + scale * index => base + new_reg,
2978 case (1) above.
2979 Index part of address may become invalid. For example, we
2980 changed pseudo on the equivalent memory and a subreg of the
2981 pseudo onto the memory of different mode for which the scale is
2982 prohibitted. */
2983 new_reg = index_part_to_reg (&ad);
2984 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2985 *ad.base_term, new_reg);
2987 *before = get_insns ();
2988 end_sequence ();
2989 return true;
2992 /* Emit insns to reload VALUE into a new register. VALUE is an
2993 auto-increment or auto-decrement RTX whose operand is a register or
2994 memory location; so reloading involves incrementing that location.
2995 IN is either identical to VALUE, or some cheaper place to reload
2996 value being incremented/decremented from.
2998 INC_AMOUNT is the number to increment or decrement by (always
2999 positive and ignored for POST_MODIFY/PRE_MODIFY).
3001 Return pseudo containing the result. */
3002 static rtx
3003 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3005 /* REG or MEM to be copied and incremented. */
3006 rtx incloc = XEXP (value, 0);
3007 /* Nonzero if increment after copying. */
3008 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3009 || GET_CODE (value) == POST_MODIFY);
3010 rtx last;
3011 rtx inc;
3012 rtx add_insn;
3013 int code;
3014 rtx real_in = in == value ? incloc : in;
3015 rtx result;
3016 bool plus_p = true;
3018 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3020 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3021 || GET_CODE (XEXP (value, 1)) == MINUS);
3022 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3023 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3024 inc = XEXP (XEXP (value, 1), 1);
3026 else
3028 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3029 inc_amount = -inc_amount;
3031 inc = GEN_INT (inc_amount);
3034 if (! post && REG_P (incloc))
3035 result = incloc;
3036 else
3037 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3038 "INC/DEC result");
3040 if (real_in != result)
3042 /* First copy the location to the result register. */
3043 lra_assert (REG_P (result));
3044 emit_insn (gen_move_insn (result, real_in));
3047 /* We suppose that there are insns to add/sub with the constant
3048 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3049 old reload worked with this assumption. If the assumption
3050 becomes wrong, we should use approach in function
3051 base_plus_disp_to_reg. */
3052 if (in == value)
3054 /* See if we can directly increment INCLOC. */
3055 last = get_last_insn ();
3056 add_insn = emit_insn (plus_p
3057 ? gen_add2_insn (incloc, inc)
3058 : gen_sub2_insn (incloc, inc));
3060 code = recog_memoized (add_insn);
3061 if (code >= 0)
3063 if (! post && result != incloc)
3064 emit_insn (gen_move_insn (result, incloc));
3065 return result;
3067 delete_insns_since (last);
3070 /* If couldn't do the increment directly, must increment in RESULT.
3071 The way we do this depends on whether this is pre- or
3072 post-increment. For pre-increment, copy INCLOC to the reload
3073 register, increment it there, then save back. */
3074 if (! post)
3076 if (real_in != result)
3077 emit_insn (gen_move_insn (result, real_in));
3078 if (plus_p)
3079 emit_insn (gen_add2_insn (result, inc));
3080 else
3081 emit_insn (gen_sub2_insn (result, inc));
3082 if (result != incloc)
3083 emit_insn (gen_move_insn (incloc, result));
3085 else
3087 /* Post-increment.
3089 Because this might be a jump insn or a compare, and because
3090 RESULT may not be available after the insn in an input
3091 reload, we must do the incrementing before the insn being
3092 reloaded for.
3094 We have already copied IN to RESULT. Increment the copy in
3095 RESULT, save that back, then decrement RESULT so it has
3096 the original value. */
3097 if (plus_p)
3098 emit_insn (gen_add2_insn (result, inc));
3099 else
3100 emit_insn (gen_sub2_insn (result, inc));
3101 emit_insn (gen_move_insn (incloc, result));
3102 /* Restore non-modified value for the result. We prefer this
3103 way because it does not require an additional hard
3104 register. */
3105 if (plus_p)
3107 if (CONST_INT_P (inc))
3108 emit_insn (gen_add2_insn (result,
3109 gen_int_mode (-INTVAL (inc),
3110 GET_MODE (result))));
3111 else
3112 emit_insn (gen_sub2_insn (result, inc));
3114 else
3115 emit_insn (gen_add2_insn (result, inc));
3117 return result;
3120 /* Return true if the current move insn does not need processing as we
3121 already know that it satisfies its constraints. */
3122 static bool
3123 simple_move_p (void)
3125 rtx dest, src;
3126 enum reg_class dclass, sclass;
3128 lra_assert (curr_insn_set != NULL_RTX);
3129 dest = SET_DEST (curr_insn_set);
3130 src = SET_SRC (curr_insn_set);
3131 return ((dclass = get_op_class (dest)) != NO_REGS
3132 && (sclass = get_op_class (src)) != NO_REGS
3133 /* The backend guarantees that register moves of cost 2
3134 never need reloads. */
3135 && targetm.register_move_cost (GET_MODE (src), dclass, sclass) == 2);
3138 /* Swap operands NOP and NOP + 1. */
3139 static inline void
3140 swap_operands (int nop)
3142 enum machine_mode mode = curr_operand_mode[nop];
3143 curr_operand_mode[nop] = curr_operand_mode[nop + 1];
3144 curr_operand_mode[nop + 1] = mode;
3145 rtx x = *curr_id->operand_loc[nop];
3146 *curr_id->operand_loc[nop] = *curr_id->operand_loc[nop + 1];
3147 *curr_id->operand_loc[nop + 1] = x;
3148 /* Swap the duplicates too. */
3149 lra_update_dup (curr_id, nop);
3150 lra_update_dup (curr_id, nop + 1);
3153 /* Main entry point of the constraint code: search the body of the
3154 current insn to choose the best alternative. It is mimicking insn
3155 alternative cost calculation model of former reload pass. That is
3156 because machine descriptions were written to use this model. This
3157 model can be changed in future. Make commutative operand exchange
3158 if it is chosen.
3160 Return true if some RTL changes happened during function call. */
3161 static bool
3162 curr_insn_transform (void)
3164 int i, j, k;
3165 int n_operands;
3166 int n_alternatives;
3167 int commutative;
3168 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3169 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3170 rtx before, after;
3171 bool alt_p = false;
3172 /* Flag that the insn has been changed through a transformation. */
3173 bool change_p;
3174 bool sec_mem_p;
3175 #ifdef SECONDARY_MEMORY_NEEDED
3176 bool use_sec_mem_p;
3177 #endif
3178 int max_regno_before;
3179 int reused_alternative_num;
3181 curr_insn_set = single_set (curr_insn);
3182 if (curr_insn_set != NULL_RTX && simple_move_p ())
3183 return false;
3185 no_input_reloads_p = no_output_reloads_p = false;
3186 goal_alt_number = -1;
3187 change_p = sec_mem_p = false;
3188 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3189 reloads; neither are insns that SET cc0. Insns that use CC0 are
3190 not allowed to have any input reloads. */
3191 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3192 no_output_reloads_p = true;
3194 #ifdef HAVE_cc0
3195 if (reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3196 no_input_reloads_p = true;
3197 if (reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3198 no_output_reloads_p = true;
3199 #endif
3201 n_operands = curr_static_id->n_operands;
3202 n_alternatives = curr_static_id->n_alternatives;
3204 /* Just return "no reloads" if insn has no operands with
3205 constraints. */
3206 if (n_operands == 0 || n_alternatives == 0)
3207 return false;
3209 max_regno_before = max_reg_num ();
3211 for (i = 0; i < n_operands; i++)
3213 goal_alt_matched[i][0] = -1;
3214 goal_alt_matches[i] = -1;
3217 commutative = curr_static_id->commutative;
3219 /* Now see what we need for pseudos that didn't get hard regs or got
3220 the wrong kind of hard reg. For this, we must consider all the
3221 operands together against the register constraints. */
3223 best_losers = best_overall = INT_MAX;
3224 best_reload_sum = 0;
3226 curr_swapped = false;
3227 goal_alt_swapped = false;
3229 /* Make equivalence substitution and memory subreg elimination
3230 before address processing because an address legitimacy can
3231 depend on memory mode. */
3232 for (i = 0; i < n_operands; i++)
3234 rtx op = *curr_id->operand_loc[i];
3235 rtx subst, old = op;
3236 bool op_change_p = false;
3238 if (GET_CODE (old) == SUBREG)
3239 old = SUBREG_REG (old);
3240 subst = get_equiv_with_elimination (old, curr_insn);
3241 if (subst != old)
3243 subst = copy_rtx (subst);
3244 lra_assert (REG_P (old));
3245 if (GET_CODE (op) == SUBREG)
3246 SUBREG_REG (op) = subst;
3247 else
3248 *curr_id->operand_loc[i] = subst;
3249 if (lra_dump_file != NULL)
3251 fprintf (lra_dump_file,
3252 "Changing pseudo %d in operand %i of insn %u on equiv ",
3253 REGNO (old), i, INSN_UID (curr_insn));
3254 dump_value_slim (lra_dump_file, subst, 1);
3255 fprintf (lra_dump_file, "\n");
3257 op_change_p = change_p = true;
3259 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3261 change_p = true;
3262 lra_update_dup (curr_id, i);
3266 /* Reload address registers and displacements. We do it before
3267 finding an alternative because of memory constraints. */
3268 before = after = NULL_RTX;
3269 for (i = 0; i < n_operands; i++)
3270 if (! curr_static_id->operand[i].is_operator
3271 && process_address (i, &before, &after))
3273 change_p = true;
3274 lra_update_dup (curr_id, i);
3277 if (change_p)
3278 /* If we've changed the instruction then any alternative that
3279 we chose previously may no longer be valid. */
3280 lra_set_used_insn_alternative (curr_insn, -1);
3282 if (curr_insn_set != NULL_RTX
3283 && check_and_process_move (&change_p, &sec_mem_p))
3284 return change_p;
3286 try_swapped:
3288 reused_alternative_num = curr_id->used_insn_alternative;
3289 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3290 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3291 reused_alternative_num, INSN_UID (curr_insn));
3293 if (process_alt_operands (reused_alternative_num))
3294 alt_p = true;
3296 /* If insn is commutative (it's safe to exchange a certain pair of
3297 operands) then we need to try each alternative twice, the second
3298 time matching those two operands as if we had exchanged them. To
3299 do this, really exchange them in operands.
3301 If we have just tried the alternatives the second time, return
3302 operands to normal and drop through. */
3304 if (reused_alternative_num < 0 && commutative >= 0)
3306 curr_swapped = !curr_swapped;
3307 if (curr_swapped)
3309 swap_operands (commutative);
3310 goto try_swapped;
3312 else
3313 swap_operands (commutative);
3316 if (! alt_p && ! sec_mem_p)
3318 /* No alternative works with reloads?? */
3319 if (INSN_CODE (curr_insn) >= 0)
3320 fatal_insn ("unable to generate reloads for:", curr_insn);
3321 error_for_asm (curr_insn,
3322 "inconsistent operand constraints in an %<asm%>");
3323 /* Avoid further trouble with this insn. */
3324 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3325 lra_invalidate_insn_data (curr_insn);
3326 return true;
3329 /* If the best alternative is with operands 1 and 2 swapped, swap
3330 them. Update the operand numbers of any reloads already
3331 pushed. */
3333 if (goal_alt_swapped)
3335 if (lra_dump_file != NULL)
3336 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3337 INSN_UID (curr_insn));
3339 /* Swap the duplicates too. */
3340 swap_operands (commutative);
3341 change_p = true;
3344 #ifdef SECONDARY_MEMORY_NEEDED
3345 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3346 too conservatively. So we use the secondary memory only if there
3347 is no any alternative without reloads. */
3348 use_sec_mem_p = false;
3349 if (! alt_p)
3350 use_sec_mem_p = true;
3351 else if (sec_mem_p)
3353 for (i = 0; i < n_operands; i++)
3354 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3355 break;
3356 use_sec_mem_p = i < n_operands;
3359 if (use_sec_mem_p)
3361 rtx new_reg, src, dest, rld;
3362 enum machine_mode sec_mode, rld_mode;
3364 lra_assert (sec_mem_p);
3365 lra_assert (curr_static_id->operand[0].type == OP_OUT
3366 && curr_static_id->operand[1].type == OP_IN);
3367 dest = *curr_id->operand_loc[0];
3368 src = *curr_id->operand_loc[1];
3369 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3370 ? dest : src);
3371 rld_mode = GET_MODE (rld);
3372 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3373 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3374 #else
3375 sec_mode = rld_mode;
3376 #endif
3377 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3378 NO_REGS, "secondary");
3379 /* If the mode is changed, it should be wider. */
3380 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3381 if (sec_mode != rld_mode)
3383 /* If the target says specifically to use another mode for
3384 secondary memory moves we can not reuse the original
3385 insn. */
3386 after = emit_spill_move (false, new_reg, dest);
3387 lra_process_new_insns (curr_insn, NULL_RTX, after,
3388 "Inserting the sec. move");
3389 /* We may have non null BEFORE here (e.g. after address
3390 processing. */
3391 push_to_sequence (before);
3392 before = emit_spill_move (true, new_reg, src);
3393 emit_insn (before);
3394 before = get_insns ();
3395 end_sequence ();
3396 lra_process_new_insns (curr_insn, before, NULL_RTX, "Changing on");
3397 lra_set_insn_deleted (curr_insn);
3399 else if (dest == rld)
3401 *curr_id->operand_loc[0] = new_reg;
3402 after = emit_spill_move (false, new_reg, dest);
3403 lra_process_new_insns (curr_insn, NULL_RTX, after,
3404 "Inserting the sec. move");
3406 else
3408 *curr_id->operand_loc[1] = new_reg;
3409 /* See comments above. */
3410 push_to_sequence (before);
3411 before = emit_spill_move (true, new_reg, src);
3412 emit_insn (before);
3413 before = get_insns ();
3414 end_sequence ();
3415 lra_process_new_insns (curr_insn, before, NULL_RTX,
3416 "Inserting the sec. move");
3418 lra_update_insn_regno_info (curr_insn);
3419 return true;
3421 #endif
3423 lra_assert (goal_alt_number >= 0);
3424 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3426 if (lra_dump_file != NULL)
3428 const char *p;
3430 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3431 goal_alt_number, INSN_UID (curr_insn));
3432 for (i = 0; i < n_operands; i++)
3434 p = (curr_static_id->operand_alternative
3435 [goal_alt_number * n_operands + i].constraint);
3436 if (*p == '\0')
3437 continue;
3438 fprintf (lra_dump_file, " (%d) ", i);
3439 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3440 fputc (*p, lra_dump_file);
3442 if (INSN_CODE (curr_insn) >= 0
3443 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3444 fprintf (lra_dump_file, " {%s}", p);
3445 if (curr_id->sp_offset != 0)
3446 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3447 curr_id->sp_offset);
3448 fprintf (lra_dump_file, "\n");
3451 /* Right now, for any pair of operands I and J that are required to
3452 match, with J < I, goal_alt_matches[I] is J. Add I to
3453 goal_alt_matched[J]. */
3455 for (i = 0; i < n_operands; i++)
3456 if ((j = goal_alt_matches[i]) >= 0)
3458 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3460 /* We allow matching one output operand and several input
3461 operands. */
3462 lra_assert (k == 0
3463 || (curr_static_id->operand[j].type == OP_OUT
3464 && curr_static_id->operand[i].type == OP_IN
3465 && (curr_static_id->operand
3466 [goal_alt_matched[j][0]].type == OP_IN)));
3467 goal_alt_matched[j][k] = i;
3468 goal_alt_matched[j][k + 1] = -1;
3471 for (i = 0; i < n_operands; i++)
3472 goal_alt_win[i] |= goal_alt_match_win[i];
3474 /* Any constants that aren't allowed and can't be reloaded into
3475 registers are here changed into memory references. */
3476 for (i = 0; i < n_operands; i++)
3477 if (goal_alt_win[i])
3479 int regno;
3480 enum reg_class new_class;
3481 rtx reg = *curr_id->operand_loc[i];
3483 if (GET_CODE (reg) == SUBREG)
3484 reg = SUBREG_REG (reg);
3486 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3488 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3490 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3492 lra_assert (ok_p);
3493 lra_change_class (regno, new_class, " Change to", true);
3497 else
3499 const char *constraint;
3500 char c;
3501 rtx op = *curr_id->operand_loc[i];
3502 rtx subreg = NULL_RTX;
3503 enum machine_mode mode = curr_operand_mode[i];
3505 if (GET_CODE (op) == SUBREG)
3507 subreg = op;
3508 op = SUBREG_REG (op);
3509 mode = GET_MODE (op);
3512 if (CONST_POOL_OK_P (mode, op)
3513 && ((targetm.preferred_reload_class
3514 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3515 || no_input_reloads_p))
3517 rtx tem = force_const_mem (mode, op);
3519 change_p = true;
3520 if (subreg != NULL_RTX)
3521 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3523 *curr_id->operand_loc[i] = tem;
3524 lra_update_dup (curr_id, i);
3525 process_address (i, &before, &after);
3527 /* If the alternative accepts constant pool refs directly
3528 there will be no reload needed at all. */
3529 if (subreg != NULL_RTX)
3530 continue;
3531 /* Skip alternatives before the one requested. */
3532 constraint = (curr_static_id->operand_alternative
3533 [goal_alt_number * n_operands + i].constraint);
3534 for (;
3535 (c = *constraint) && c != ',' && c != '#';
3536 constraint += CONSTRAINT_LEN (c, constraint))
3538 if (c == TARGET_MEM_CONSTRAINT || c == 'o')
3539 break;
3540 #ifdef EXTRA_CONSTRAINT_STR
3541 if (EXTRA_MEMORY_CONSTRAINT (c, constraint)
3542 && EXTRA_CONSTRAINT_STR (tem, c, constraint))
3543 break;
3544 #endif
3546 if (c == '\0' || c == ',' || c == '#')
3547 continue;
3549 goal_alt_win[i] = true;
3553 for (i = 0; i < n_operands; i++)
3555 int regno;
3556 bool optional_p = false;
3557 rtx old, new_reg;
3558 rtx op = *curr_id->operand_loc[i];
3560 if (goal_alt_win[i])
3562 if (goal_alt[i] == NO_REGS
3563 && REG_P (op)
3564 /* When we assign NO_REGS it means that we will not
3565 assign a hard register to the scratch pseudo by
3566 assigment pass and the scratch pseudo will be
3567 spilled. Spilled scratch pseudos are transformed
3568 back to scratches at the LRA end. */
3569 && lra_former_scratch_operand_p (curr_insn, i))
3571 int regno = REGNO (op);
3572 lra_change_class (regno, NO_REGS, " Change to", true);
3573 if (lra_get_regno_hard_regno (regno) >= 0)
3574 /* We don't have to mark all insn affected by the
3575 spilled pseudo as there is only one such insn, the
3576 current one. */
3577 reg_renumber[regno] = -1;
3579 /* We can do an optional reload. If the pseudo got a hard
3580 reg, we might improve the code through inheritance. If
3581 it does not get a hard register we coalesce memory/memory
3582 moves later. Ignore move insns to avoid cycling. */
3583 if (! lra_simple_p
3584 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3585 && goal_alt[i] != NO_REGS && REG_P (op)
3586 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3587 && regno < new_regno_start
3588 && ! lra_former_scratch_p (regno)
3589 && reg_renumber[regno] < 0
3590 && (curr_insn_set == NULL_RTX
3591 || !((REG_P (SET_SRC (curr_insn_set))
3592 || MEM_P (SET_SRC (curr_insn_set))
3593 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3594 && (REG_P (SET_DEST (curr_insn_set))
3595 || MEM_P (SET_DEST (curr_insn_set))
3596 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3597 optional_p = true;
3598 else
3599 continue;
3602 /* Operands that match previous ones have already been handled. */
3603 if (goal_alt_matches[i] >= 0)
3604 continue;
3606 /* We should not have an operand with a non-offsettable address
3607 appearing where an offsettable address will do. It also may
3608 be a case when the address should be special in other words
3609 not a general one (e.g. it needs no index reg). */
3610 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3612 enum reg_class rclass;
3613 rtx *loc = &XEXP (op, 0);
3614 enum rtx_code code = GET_CODE (*loc);
3616 push_to_sequence (before);
3617 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3618 MEM, SCRATCH);
3619 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3620 new_reg = emit_inc (rclass, *loc, *loc,
3621 /* This value does not matter for MODIFY. */
3622 GET_MODE_SIZE (GET_MODE (op)));
3623 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3624 "offsetable address", &new_reg))
3625 lra_emit_move (new_reg, *loc);
3626 before = get_insns ();
3627 end_sequence ();
3628 *loc = new_reg;
3629 lra_update_dup (curr_id, i);
3631 else if (goal_alt_matched[i][0] == -1)
3633 enum machine_mode mode;
3634 rtx reg, *loc;
3635 int hard_regno, byte;
3636 enum op_type type = curr_static_id->operand[i].type;
3638 loc = curr_id->operand_loc[i];
3639 mode = curr_operand_mode[i];
3640 if (GET_CODE (*loc) == SUBREG)
3642 reg = SUBREG_REG (*loc);
3643 byte = SUBREG_BYTE (*loc);
3644 if (REG_P (reg)
3645 /* Strict_low_part requires reload the register not
3646 the sub-register. */
3647 && (curr_static_id->operand[i].strict_low
3648 || (GET_MODE_SIZE (mode)
3649 <= GET_MODE_SIZE (GET_MODE (reg))
3650 && (hard_regno
3651 = get_try_hard_regno (REGNO (reg))) >= 0
3652 && (simplify_subreg_regno
3653 (hard_regno,
3654 GET_MODE (reg), byte, mode) < 0)
3655 && (goal_alt[i] == NO_REGS
3656 || (simplify_subreg_regno
3657 (ira_class_hard_regs[goal_alt[i]][0],
3658 GET_MODE (reg), byte, mode) >= 0)))))
3660 loc = &SUBREG_REG (*loc);
3661 mode = GET_MODE (*loc);
3664 old = *loc;
3665 if (get_reload_reg (type, mode, old, goal_alt[i],
3666 loc != curr_id->operand_loc[i], "", &new_reg)
3667 && type != OP_OUT)
3669 push_to_sequence (before);
3670 lra_emit_move (new_reg, old);
3671 before = get_insns ();
3672 end_sequence ();
3674 *loc = new_reg;
3675 if (type != OP_IN
3676 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3678 start_sequence ();
3679 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3680 emit_insn (after);
3681 after = get_insns ();
3682 end_sequence ();
3683 *loc = new_reg;
3685 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3686 if (goal_alt_dont_inherit_ops[j] == i)
3688 lra_set_regno_unique_value (REGNO (new_reg));
3689 break;
3691 lra_update_dup (curr_id, i);
3693 else if (curr_static_id->operand[i].type == OP_IN
3694 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3695 == OP_OUT))
3697 /* generate reloads for input and matched outputs. */
3698 match_inputs[0] = i;
3699 match_inputs[1] = -1;
3700 match_reload (goal_alt_matched[i][0], match_inputs,
3701 goal_alt[i], &before, &after);
3703 else if (curr_static_id->operand[i].type == OP_OUT
3704 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3705 == OP_IN))
3706 /* Generate reloads for output and matched inputs. */
3707 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3708 else if (curr_static_id->operand[i].type == OP_IN
3709 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3710 == OP_IN))
3712 /* Generate reloads for matched inputs. */
3713 match_inputs[0] = i;
3714 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3715 match_inputs[j + 1] = k;
3716 match_inputs[j + 1] = -1;
3717 match_reload (-1, match_inputs, goal_alt[i], &before, &after);
3719 else
3720 /* We must generate code in any case when function
3721 process_alt_operands decides that it is possible. */
3722 gcc_unreachable ();
3723 if (optional_p)
3725 lra_assert (REG_P (op));
3726 regno = REGNO (op);
3727 op = *curr_id->operand_loc[i]; /* Substitution. */
3728 if (GET_CODE (op) == SUBREG)
3729 op = SUBREG_REG (op);
3730 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3731 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3732 lra_reg_info[REGNO (op)].restore_regno = regno;
3733 if (lra_dump_file != NULL)
3734 fprintf (lra_dump_file,
3735 " Making reload reg %d for reg %d optional\n",
3736 REGNO (op), regno);
3739 if (before != NULL_RTX || after != NULL_RTX
3740 || max_regno_before != max_reg_num ())
3741 change_p = true;
3742 if (change_p)
3744 lra_update_operator_dups (curr_id);
3745 /* Something changes -- process the insn. */
3746 lra_update_insn_regno_info (curr_insn);
3748 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3749 return change_p;
3752 /* Return true if X is in LIST. */
3753 static bool
3754 in_list_p (rtx x, rtx list)
3756 for (; list != NULL_RTX; list = XEXP (list, 1))
3757 if (XEXP (list, 0) == x)
3758 return true;
3759 return false;
3762 /* Return true if X contains an allocatable hard register (if
3763 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3764 static bool
3765 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3767 int i, j;
3768 const char *fmt;
3769 enum rtx_code code;
3771 code = GET_CODE (x);
3772 if (REG_P (x))
3774 int regno = REGNO (x);
3775 HARD_REG_SET alloc_regs;
3777 if (hard_reg_p)
3779 if (regno >= FIRST_PSEUDO_REGISTER)
3780 regno = lra_get_regno_hard_regno (regno);
3781 if (regno < 0)
3782 return false;
3783 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3784 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3786 else
3788 if (regno < FIRST_PSEUDO_REGISTER)
3789 return false;
3790 if (! spilled_p)
3791 return true;
3792 return lra_get_regno_hard_regno (regno) < 0;
3795 fmt = GET_RTX_FORMAT (code);
3796 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3798 if (fmt[i] == 'e')
3800 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3801 return true;
3803 else if (fmt[i] == 'E')
3805 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3806 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3807 return true;
3810 return false;
3813 /* Process all regs in location *LOC and change them on equivalent
3814 substitution. Return true if any change was done. */
3815 static bool
3816 loc_equivalence_change_p (rtx *loc)
3818 rtx subst, reg, x = *loc;
3819 bool result = false;
3820 enum rtx_code code = GET_CODE (x);
3821 const char *fmt;
3822 int i, j;
3824 if (code == SUBREG)
3826 reg = SUBREG_REG (x);
3827 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
3828 && GET_MODE (subst) == VOIDmode)
3830 /* We cannot reload debug location. Simplify subreg here
3831 while we know the inner mode. */
3832 *loc = simplify_gen_subreg (GET_MODE (x), subst,
3833 GET_MODE (reg), SUBREG_BYTE (x));
3834 return true;
3837 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
3839 *loc = subst;
3840 return true;
3843 /* Scan all the operand sub-expressions. */
3844 fmt = GET_RTX_FORMAT (code);
3845 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3847 if (fmt[i] == 'e')
3848 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
3849 else if (fmt[i] == 'E')
3850 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3851 result
3852 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
3854 return result;
3857 /* Similar to loc_equivalence_change_p, but for use as
3858 simplify_replace_fn_rtx callback. DATA is insn for which the
3859 elimination is done. If it null we don't do the elimination. */
3860 static rtx
3861 loc_equivalence_callback (rtx loc, const_rtx, void *data)
3863 if (!REG_P (loc))
3864 return NULL_RTX;
3866 rtx subst = (data == NULL
3867 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx) data));
3868 if (subst != loc)
3869 return subst;
3871 return NULL_RTX;
3874 /* Maximum number of generated reload insns per an insn. It is for
3875 preventing this pass cycling in a bug case. */
3876 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
3878 /* The current iteration number of this LRA pass. */
3879 int lra_constraint_iter;
3881 /* The current iteration number of this LRA pass after the last spill
3882 pass. */
3883 int lra_constraint_iter_after_spill;
3885 /* True if we substituted equiv which needs checking register
3886 allocation correctness because the equivalent value contains
3887 allocatable hard registers or when we restore multi-register
3888 pseudo. */
3889 bool lra_risky_transformations_p;
3891 /* Return true if REGNO is referenced in more than one block. */
3892 static bool
3893 multi_block_pseudo_p (int regno)
3895 basic_block bb = NULL;
3896 unsigned int uid;
3897 bitmap_iterator bi;
3899 if (regno < FIRST_PSEUDO_REGISTER)
3900 return false;
3902 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
3903 if (bb == NULL)
3904 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
3905 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
3906 return true;
3907 return false;
3910 /* Return true if LIST contains a deleted insn. */
3911 static bool
3912 contains_deleted_insn_p (rtx list)
3914 for (; list != NULL_RTX; list = XEXP (list, 1))
3915 if (NOTE_P (XEXP (list, 0))
3916 && NOTE_KIND (XEXP (list, 0)) == NOTE_INSN_DELETED)
3917 return true;
3918 return false;
3921 /* Return true if X contains a pseudo dying in INSN. */
3922 static bool
3923 dead_pseudo_p (rtx x, rtx insn)
3925 int i, j;
3926 const char *fmt;
3927 enum rtx_code code;
3929 if (REG_P (x))
3930 return (insn != NULL_RTX
3931 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
3932 code = GET_CODE (x);
3933 fmt = GET_RTX_FORMAT (code);
3934 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3936 if (fmt[i] == 'e')
3938 if (dead_pseudo_p (XEXP (x, i), insn))
3939 return true;
3941 else if (fmt[i] == 'E')
3943 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3944 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
3945 return true;
3948 return false;
3951 /* Return true if INSN contains a dying pseudo in INSN right hand
3952 side. */
3953 static bool
3954 insn_rhs_dead_pseudo_p (rtx insn)
3956 rtx set = single_set (insn);
3958 gcc_assert (set != NULL);
3959 return dead_pseudo_p (SET_SRC (set), insn);
3962 /* Return true if any init insn of REGNO contains a dying pseudo in
3963 insn right hand side. */
3964 static bool
3965 init_insn_rhs_dead_pseudo_p (int regno)
3967 rtx insns = ira_reg_equiv[regno].init_insns;
3969 if (insns == NULL)
3970 return false;
3971 if (INSN_P (insns))
3972 return insn_rhs_dead_pseudo_p (insns);
3973 for (; insns != NULL_RTX; insns = XEXP (insns, 1))
3974 if (insn_rhs_dead_pseudo_p (XEXP (insns, 0)))
3975 return true;
3976 return false;
3979 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
3980 reverse only if we have one init insn with given REGNO as a
3981 source. */
3982 static bool
3983 reverse_equiv_p (int regno)
3985 rtx insns, set;
3987 if ((insns = ira_reg_equiv[regno].init_insns) == NULL_RTX)
3988 return false;
3989 if (! INSN_P (XEXP (insns, 0))
3990 || XEXP (insns, 1) != NULL_RTX)
3991 return false;
3992 if ((set = single_set (XEXP (insns, 0))) == NULL_RTX)
3993 return false;
3994 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
3997 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
3998 call this function only for non-reverse equivalence. */
3999 static bool
4000 contains_reloaded_insn_p (int regno)
4002 rtx set;
4003 rtx list = ira_reg_equiv[regno].init_insns;
4005 for (; list != NULL_RTX; list = XEXP (list, 1))
4006 if ((set = single_set (XEXP (list, 0))) == NULL_RTX
4007 || ! REG_P (SET_DEST (set))
4008 || (int) REGNO (SET_DEST (set)) != regno)
4009 return true;
4010 return false;
4013 /* Entry function of LRA constraint pass. Return true if the
4014 constraint pass did change the code. */
4015 bool
4016 lra_constraints (bool first_p)
4018 bool changed_p;
4019 int i, hard_regno, new_insns_num;
4020 unsigned int min_len, new_min_len, uid;
4021 rtx set, x, reg, dest_reg;
4022 basic_block last_bb;
4023 bitmap_head equiv_insn_bitmap;
4024 bitmap_iterator bi;
4026 lra_constraint_iter++;
4027 if (lra_dump_file != NULL)
4028 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4029 lra_constraint_iter);
4030 lra_constraint_iter_after_spill++;
4031 if (lra_constraint_iter_after_spill > LRA_MAX_CONSTRAINT_ITERATION_NUMBER)
4032 internal_error
4033 ("Maximum number of LRA constraint passes is achieved (%d)\n",
4034 LRA_MAX_CONSTRAINT_ITERATION_NUMBER);
4035 changed_p = false;
4036 lra_risky_transformations_p = false;
4037 new_insn_uid_start = get_max_uid ();
4038 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4039 /* Mark used hard regs for target stack size calulations. */
4040 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4041 if (lra_reg_info[i].nrefs != 0
4042 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4044 int j, nregs;
4046 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4047 for (j = 0; j < nregs; j++)
4048 df_set_regs_ever_live (hard_regno + j, true);
4050 /* Do elimination before the equivalence processing as we can spill
4051 some pseudos during elimination. */
4052 lra_eliminate (false, first_p);
4053 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4054 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4055 if (lra_reg_info[i].nrefs != 0)
4057 ira_reg_equiv[i].profitable_p = true;
4058 reg = regno_reg_rtx[i];
4059 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4061 bool pseudo_p = contains_reg_p (x, false, false);
4063 /* After RTL transformation, we can not guarantee that
4064 pseudo in the substitution was not reloaded which might
4065 make equivalence invalid. For example, in reverse
4066 equiv of p0
4068 p0 <- ...
4070 equiv_mem <- p0
4072 the memory address register was reloaded before the 2nd
4073 insn. */
4074 if ((! first_p && pseudo_p)
4075 /* We don't use DF for compilation speed sake. So it
4076 is problematic to update live info when we use an
4077 equivalence containing pseudos in more than one
4078 BB. */
4079 || (pseudo_p && multi_block_pseudo_p (i))
4080 /* If an init insn was deleted for some reason, cancel
4081 the equiv. We could update the equiv insns after
4082 transformations including an equiv insn deletion
4083 but it is not worthy as such cases are extremely
4084 rare. */
4085 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4086 /* If it is not a reverse equivalence, we check that a
4087 pseudo in rhs of the init insn is not dying in the
4088 insn. Otherwise, the live info at the beginning of
4089 the corresponding BB might be wrong after we
4090 removed the insn. When the equiv can be a
4091 constant, the right hand side of the init insn can
4092 be a pseudo. */
4093 || (! reverse_equiv_p (i)
4094 && (init_insn_rhs_dead_pseudo_p (i)
4095 /* If we reloaded the pseudo in an equivalence
4096 init insn, we can not remove the equiv init
4097 insns and the init insns might write into
4098 const memory in this case. */
4099 || contains_reloaded_insn_p (i)))
4100 /* Prevent access beyond equivalent memory for
4101 paradoxical subregs. */
4102 || (MEM_P (x)
4103 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4104 > GET_MODE_SIZE (GET_MODE (x)))))
4105 ira_reg_equiv[i].defined_p = false;
4106 if (contains_reg_p (x, false, true))
4107 ira_reg_equiv[i].profitable_p = false;
4108 if (get_equiv (reg) != reg)
4109 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4112 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4113 update_equiv (i);
4114 /* We should add all insns containing pseudos which should be
4115 substituted by their equivalences. */
4116 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4117 lra_push_insn_by_uid (uid);
4118 min_len = lra_insn_stack_length ();
4119 new_insns_num = 0;
4120 last_bb = NULL;
4121 changed_p = false;
4122 while ((new_min_len = lra_insn_stack_length ()) != 0)
4124 curr_insn = lra_pop_insn ();
4125 --new_min_len;
4126 curr_bb = BLOCK_FOR_INSN (curr_insn);
4127 if (curr_bb != last_bb)
4129 last_bb = curr_bb;
4130 bb_reload_num = lra_curr_reload_num;
4132 if (min_len > new_min_len)
4134 min_len = new_min_len;
4135 new_insns_num = 0;
4137 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4138 internal_error
4139 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4140 MAX_RELOAD_INSNS_NUMBER);
4141 new_insns_num++;
4142 if (DEBUG_INSN_P (curr_insn))
4144 /* We need to check equivalence in debug insn and change
4145 pseudo to the equivalent value if necessary. */
4146 curr_id = lra_get_insn_recog_data (curr_insn);
4147 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4149 rtx old = *curr_id->operand_loc[0];
4150 *curr_id->operand_loc[0]
4151 = simplify_replace_fn_rtx (old, NULL_RTX,
4152 loc_equivalence_callback, curr_insn);
4153 if (old != *curr_id->operand_loc[0])
4155 lra_update_insn_regno_info (curr_insn);
4156 changed_p = true;
4160 else if (INSN_P (curr_insn))
4162 if ((set = single_set (curr_insn)) != NULL_RTX)
4164 dest_reg = SET_DEST (set);
4165 /* The equivalence pseudo could be set up as SUBREG in a
4166 case when it is a call restore insn in a mode
4167 different from the pseudo mode. */
4168 if (GET_CODE (dest_reg) == SUBREG)
4169 dest_reg = SUBREG_REG (dest_reg);
4170 if ((REG_P (dest_reg)
4171 && (x = get_equiv (dest_reg)) != dest_reg
4172 /* Remove insns which set up a pseudo whose value
4173 can not be changed. Such insns might be not in
4174 init_insns because we don't update equiv data
4175 during insn transformations.
4177 As an example, let suppose that a pseudo got
4178 hard register and on the 1st pass was not
4179 changed to equivalent constant. We generate an
4180 additional insn setting up the pseudo because of
4181 secondary memory movement. Then the pseudo is
4182 spilled and we use the equiv constant. In this
4183 case we should remove the additional insn and
4184 this insn is not init_insns list. */
4185 && (! MEM_P (x) || MEM_READONLY_P (x)
4186 /* Check that this is actually an insn setting
4187 up the equivalence. */
4188 || in_list_p (curr_insn,
4189 ira_reg_equiv
4190 [REGNO (dest_reg)].init_insns)))
4191 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4192 && in_list_p (curr_insn,
4193 ira_reg_equiv
4194 [REGNO (SET_SRC (set))].init_insns)))
4196 /* This is equiv init insn of pseudo which did not get a
4197 hard register -- remove the insn. */
4198 if (lra_dump_file != NULL)
4200 fprintf (lra_dump_file,
4201 " Removing equiv init insn %i (freq=%d)\n",
4202 INSN_UID (curr_insn),
4203 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4204 dump_insn_slim (lra_dump_file, curr_insn);
4206 if (contains_reg_p (x, true, false))
4207 lra_risky_transformations_p = true;
4208 lra_set_insn_deleted (curr_insn);
4209 continue;
4212 curr_id = lra_get_insn_recog_data (curr_insn);
4213 curr_static_id = curr_id->insn_static_data;
4214 init_curr_insn_input_reloads ();
4215 init_curr_operand_mode ();
4216 if (curr_insn_transform ())
4217 changed_p = true;
4218 /* Check non-transformed insns too for equiv change as USE
4219 or CLOBBER don't need reloads but can contain pseudos
4220 being changed on their equivalences. */
4221 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4222 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4224 lra_update_insn_regno_info (curr_insn);
4225 changed_p = true;
4229 bitmap_clear (&equiv_insn_bitmap);
4230 /* If we used a new hard regno, changed_p should be true because the
4231 hard reg is assigned to a new pseudo. */
4232 #ifdef ENABLE_CHECKING
4233 if (! changed_p)
4235 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4236 if (lra_reg_info[i].nrefs != 0
4237 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4239 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4241 for (j = 0; j < nregs; j++)
4242 lra_assert (df_regs_ever_live_p (hard_regno + j));
4245 #endif
4246 return changed_p;
4249 /* Initiate the LRA constraint pass. It is done once per
4250 function. */
4251 void
4252 lra_constraints_init (void)
4256 /* Finalize the LRA constraint pass. It is done once per
4257 function. */
4258 void
4259 lra_constraints_finish (void)
4265 /* This page contains code to do inheritance/split
4266 transformations. */
4268 /* Number of reloads passed so far in current EBB. */
4269 static int reloads_num;
4271 /* Number of calls passed so far in current EBB. */
4272 static int calls_num;
4274 /* Current reload pseudo check for validity of elements in
4275 USAGE_INSNS. */
4276 static int curr_usage_insns_check;
4278 /* Info about last usage of registers in EBB to do inheritance/split
4279 transformation. Inheritance transformation is done from a spilled
4280 pseudo and split transformations from a hard register or a pseudo
4281 assigned to a hard register. */
4282 struct usage_insns
4284 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4285 value INSNS is valid. The insns is chain of optional debug insns
4286 and a finishing non-debug insn using the corresponding reg. The
4287 value is also used to mark the registers which are set up in the
4288 current insn. The negated insn uid is used for this. */
4289 int check;
4290 /* Value of global reloads_num at the last insn in INSNS. */
4291 int reloads_num;
4292 /* Value of global reloads_nums at the last insn in INSNS. */
4293 int calls_num;
4294 /* It can be true only for splitting. And it means that the restore
4295 insn should be put after insn given by the following member. */
4296 bool after_p;
4297 /* Next insns in the current EBB which use the original reg and the
4298 original reg value is not changed between the current insn and
4299 the next insns. In order words, e.g. for inheritance, if we need
4300 to use the original reg value again in the next insns we can try
4301 to use the value in a hard register from a reload insn of the
4302 current insn. */
4303 rtx insns;
4306 /* Map: regno -> corresponding pseudo usage insns. */
4307 static struct usage_insns *usage_insns;
4309 static void
4310 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4312 usage_insns[regno].check = curr_usage_insns_check;
4313 usage_insns[regno].insns = insn;
4314 usage_insns[regno].reloads_num = reloads_num;
4315 usage_insns[regno].calls_num = calls_num;
4316 usage_insns[regno].after_p = after_p;
4319 /* The function is used to form list REGNO usages which consists of
4320 optional debug insns finished by a non-debug insn using REGNO.
4321 RELOADS_NUM is current number of reload insns processed so far. */
4322 static void
4323 add_next_usage_insn (int regno, rtx insn, int reloads_num)
4325 rtx next_usage_insns;
4327 if (usage_insns[regno].check == curr_usage_insns_check
4328 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4329 && DEBUG_INSN_P (insn))
4331 /* Check that we did not add the debug insn yet. */
4332 if (next_usage_insns != insn
4333 && (GET_CODE (next_usage_insns) != INSN_LIST
4334 || XEXP (next_usage_insns, 0) != insn))
4335 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4336 next_usage_insns);
4338 else if (NONDEBUG_INSN_P (insn))
4339 setup_next_usage_insn (regno, insn, reloads_num, false);
4340 else
4341 usage_insns[regno].check = 0;
4344 /* Replace all references to register OLD_REGNO in *LOC with pseudo
4345 register NEW_REG. Return true if any change was made. */
4346 static bool
4347 substitute_pseudo (rtx *loc, int old_regno, rtx new_reg)
4349 rtx x = *loc;
4350 bool result = false;
4351 enum rtx_code code;
4352 const char *fmt;
4353 int i, j;
4355 if (x == NULL_RTX)
4356 return false;
4358 code = GET_CODE (x);
4359 if (code == REG && (int) REGNO (x) == old_regno)
4361 enum machine_mode mode = GET_MODE (*loc);
4362 enum machine_mode inner_mode = GET_MODE (new_reg);
4364 if (mode != inner_mode)
4366 if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
4367 || ! SCALAR_INT_MODE_P (inner_mode))
4368 new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
4369 else
4370 new_reg = gen_lowpart_SUBREG (mode, new_reg);
4372 *loc = new_reg;
4373 return true;
4376 /* Scan all the operand sub-expressions. */
4377 fmt = GET_RTX_FORMAT (code);
4378 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4380 if (fmt[i] == 'e')
4382 if (substitute_pseudo (&XEXP (x, i), old_regno, new_reg))
4383 result = true;
4385 else if (fmt[i] == 'E')
4387 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4388 if (substitute_pseudo (&XVECEXP (x, i, j), old_regno, new_reg))
4389 result = true;
4392 return result;
4395 /* Return first non-debug insn in list USAGE_INSNS. */
4396 static rtx
4397 skip_usage_debug_insns (rtx usage_insns)
4399 rtx insn;
4401 /* Skip debug insns. */
4402 for (insn = usage_insns;
4403 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4404 insn = XEXP (insn, 1))
4406 return insn;
4409 /* Return true if we need secondary memory moves for insn in
4410 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4411 into the insn. */
4412 static bool
4413 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4414 rtx usage_insns ATTRIBUTE_UNUSED)
4416 #ifndef SECONDARY_MEMORY_NEEDED
4417 return false;
4418 #else
4419 rtx insn, set, dest;
4420 enum reg_class cl;
4422 if (inher_cl == ALL_REGS
4423 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4424 return false;
4425 lra_assert (INSN_P (insn));
4426 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4427 return false;
4428 dest = SET_DEST (set);
4429 if (! REG_P (dest))
4430 return false;
4431 lra_assert (inher_cl != NO_REGS);
4432 cl = get_reg_class (REGNO (dest));
4433 return (cl != NO_REGS && cl != ALL_REGS
4434 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4435 #endif
4438 /* Registers involved in inheritance/split in the current EBB
4439 (inheritance/split pseudos and original registers). */
4440 static bitmap_head check_only_regs;
4442 /* Do inheritance transformations for insn INSN, which defines (if
4443 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4444 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4445 form as the "insns" field of usage_insns. Return true if we
4446 succeed in such transformation.
4448 The transformations look like:
4450 p <- ... i <- ...
4451 ... p <- i (new insn)
4452 ... =>
4453 <- ... p ... <- ... i ...
4455 ... i <- p (new insn)
4456 <- ... p ... <- ... i ...
4457 ... =>
4458 <- ... p ... <- ... i ...
4459 where p is a spilled original pseudo and i is a new inheritance pseudo.
4462 The inheritance pseudo has the smallest class of two classes CL and
4463 class of ORIGINAL REGNO. */
4464 static bool
4465 inherit_reload_reg (bool def_p, int original_regno,
4466 enum reg_class cl, rtx insn, rtx next_usage_insns)
4468 if (optimize_function_for_size_p (cfun))
4469 return false;
4471 enum reg_class rclass = lra_get_allocno_class (original_regno);
4472 rtx original_reg = regno_reg_rtx[original_regno];
4473 rtx new_reg, new_insns, usage_insn;
4475 lra_assert (! usage_insns[original_regno].after_p);
4476 if (lra_dump_file != NULL)
4477 fprintf (lra_dump_file,
4478 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4479 if (! ira_reg_classes_intersect_p[cl][rclass])
4481 if (lra_dump_file != NULL)
4483 fprintf (lra_dump_file,
4484 " Rejecting inheritance for %d "
4485 "because of disjoint classes %s and %s\n",
4486 original_regno, reg_class_names[cl],
4487 reg_class_names[rclass]);
4488 fprintf (lra_dump_file,
4489 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4491 return false;
4493 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4494 /* We don't use a subset of two classes because it can be
4495 NO_REGS. This transformation is still profitable in most
4496 cases even if the classes are not intersected as register
4497 move is probably cheaper than a memory load. */
4498 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4500 if (lra_dump_file != NULL)
4501 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4502 reg_class_names[cl], reg_class_names[rclass]);
4504 rclass = cl;
4506 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4508 /* Reject inheritance resulting in secondary memory moves.
4509 Otherwise, there is a danger in LRA cycling. Also such
4510 transformation will be unprofitable. */
4511 if (lra_dump_file != NULL)
4513 rtx insn = skip_usage_debug_insns (next_usage_insns);
4514 rtx set = single_set (insn);
4516 lra_assert (set != NULL_RTX);
4518 rtx dest = SET_DEST (set);
4520 lra_assert (REG_P (dest));
4521 fprintf (lra_dump_file,
4522 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4523 "as secondary mem is needed\n",
4524 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4525 original_regno, reg_class_names[rclass]);
4526 fprintf (lra_dump_file,
4527 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4529 return false;
4531 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4532 rclass, "inheritance");
4533 start_sequence ();
4534 if (def_p)
4535 lra_emit_move (original_reg, new_reg);
4536 else
4537 lra_emit_move (new_reg, original_reg);
4538 new_insns = get_insns ();
4539 end_sequence ();
4540 if (NEXT_INSN (new_insns) != NULL_RTX)
4542 if (lra_dump_file != NULL)
4544 fprintf (lra_dump_file,
4545 " Rejecting inheritance %d->%d "
4546 "as it results in 2 or more insns:\n",
4547 original_regno, REGNO (new_reg));
4548 dump_rtl_slim (lra_dump_file, new_insns, NULL_RTX, -1, 0);
4549 fprintf (lra_dump_file,
4550 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4552 return false;
4554 substitute_pseudo (&insn, original_regno, new_reg);
4555 lra_update_insn_regno_info (insn);
4556 if (! def_p)
4557 /* We now have a new usage insn for original regno. */
4558 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4559 if (lra_dump_file != NULL)
4560 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4561 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4562 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4563 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4564 bitmap_set_bit (&check_only_regs, original_regno);
4565 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4566 if (def_p)
4567 lra_process_new_insns (insn, NULL_RTX, new_insns,
4568 "Add original<-inheritance");
4569 else
4570 lra_process_new_insns (insn, new_insns, NULL_RTX,
4571 "Add inheritance<-original");
4572 while (next_usage_insns != NULL_RTX)
4574 if (GET_CODE (next_usage_insns) != INSN_LIST)
4576 usage_insn = next_usage_insns;
4577 lra_assert (NONDEBUG_INSN_P (usage_insn));
4578 next_usage_insns = NULL;
4580 else
4582 usage_insn = XEXP (next_usage_insns, 0);
4583 lra_assert (DEBUG_INSN_P (usage_insn));
4584 next_usage_insns = XEXP (next_usage_insns, 1);
4586 substitute_pseudo (&usage_insn, original_regno, new_reg);
4587 lra_update_insn_regno_info (usage_insn);
4588 if (lra_dump_file != NULL)
4590 fprintf (lra_dump_file,
4591 " Inheritance reuse change %d->%d (bb%d):\n",
4592 original_regno, REGNO (new_reg),
4593 BLOCK_FOR_INSN (usage_insn)->index);
4594 dump_insn_slim (lra_dump_file, usage_insn);
4597 if (lra_dump_file != NULL)
4598 fprintf (lra_dump_file,
4599 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4600 return true;
4603 /* Return true if we need a caller save/restore for pseudo REGNO which
4604 was assigned to a hard register. */
4605 static inline bool
4606 need_for_call_save_p (int regno)
4608 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4609 return (usage_insns[regno].calls_num < calls_num
4610 && (overlaps_hard_reg_set_p
4611 (call_used_reg_set,
4612 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4613 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4614 PSEUDO_REGNO_MODE (regno))));
4617 /* Global registers occurring in the current EBB. */
4618 static bitmap_head ebb_global_regs;
4620 /* Return true if we need a split for hard register REGNO or pseudo
4621 REGNO which was assigned to a hard register.
4622 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4623 used for reloads since the EBB end. It is an approximation of the
4624 used hard registers in the split range. The exact value would
4625 require expensive calculations. If we were aggressive with
4626 splitting because of the approximation, the split pseudo will save
4627 the same hard register assignment and will be removed in the undo
4628 pass. We still need the approximation because too aggressive
4629 splitting would result in too inaccurate cost calculation in the
4630 assignment pass because of too many generated moves which will be
4631 probably removed in the undo pass. */
4632 static inline bool
4633 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4635 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4637 lra_assert (hard_regno >= 0);
4638 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4639 /* Don't split eliminable hard registers, otherwise we can
4640 split hard registers like hard frame pointer, which
4641 lives on BB start/end according to DF-infrastructure,
4642 when there is a pseudo assigned to the register and
4643 living in the same BB. */
4644 && (regno >= FIRST_PSEUDO_REGISTER
4645 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4646 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4647 /* Don't split call clobbered hard regs living through
4648 calls, otherwise we might have a check problem in the
4649 assign sub-pass as in the most cases (exception is a
4650 situation when lra_risky_transformations_p value is
4651 true) the assign pass assumes that all pseudos living
4652 through calls are assigned to call saved hard regs. */
4653 && (regno >= FIRST_PSEUDO_REGISTER
4654 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4655 || usage_insns[regno].calls_num == calls_num)
4656 /* We need at least 2 reloads to make pseudo splitting
4657 profitable. We should provide hard regno splitting in
4658 any case to solve 1st insn scheduling problem when
4659 moving hard register definition up might result in
4660 impossibility to find hard register for reload pseudo of
4661 small register class. */
4662 && (usage_insns[regno].reloads_num
4663 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4664 && (regno < FIRST_PSEUDO_REGISTER
4665 /* For short living pseudos, spilling + inheritance can
4666 be considered a substitution for splitting.
4667 Therefore we do not splitting for local pseudos. It
4668 decreases also aggressiveness of splitting. The
4669 minimal number of references is chosen taking into
4670 account that for 2 references splitting has no sense
4671 as we can just spill the pseudo. */
4672 || (regno >= FIRST_PSEUDO_REGISTER
4673 && lra_reg_info[regno].nrefs > 3
4674 && bitmap_bit_p (&ebb_global_regs, regno))))
4675 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4678 /* Return class for the split pseudo created from original pseudo with
4679 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4680 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4681 results in no secondary memory movements. */
4682 static enum reg_class
4683 choose_split_class (enum reg_class allocno_class,
4684 int hard_regno ATTRIBUTE_UNUSED,
4685 enum machine_mode mode ATTRIBUTE_UNUSED)
4687 #ifndef SECONDARY_MEMORY_NEEDED
4688 return allocno_class;
4689 #else
4690 int i;
4691 enum reg_class cl, best_cl = NO_REGS;
4692 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4693 = REGNO_REG_CLASS (hard_regno);
4695 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4696 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4697 return allocno_class;
4698 for (i = 0;
4699 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4700 i++)
4701 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4702 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4703 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4704 && (best_cl == NO_REGS
4705 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4706 best_cl = cl;
4707 return best_cl;
4708 #endif
4711 /* Do split transformations for insn INSN, which defines or uses
4712 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4713 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4714 "insns" field of usage_insns.
4716 The transformations look like:
4718 p <- ... p <- ...
4719 ... s <- p (new insn -- save)
4720 ... =>
4721 ... p <- s (new insn -- restore)
4722 <- ... p ... <- ... p ...
4724 <- ... p ... <- ... p ...
4725 ... s <- p (new insn -- save)
4726 ... =>
4727 ... p <- s (new insn -- restore)
4728 <- ... p ... <- ... p ...
4730 where p is an original pseudo got a hard register or a hard
4731 register and s is a new split pseudo. The save is put before INSN
4732 if BEFORE_P is true. Return true if we succeed in such
4733 transformation. */
4734 static bool
4735 split_reg (bool before_p, int original_regno, rtx insn, rtx next_usage_insns)
4737 enum reg_class rclass;
4738 rtx original_reg;
4739 int hard_regno, nregs;
4740 rtx new_reg, save, restore, usage_insn;
4741 bool after_p;
4742 bool call_save_p;
4744 if (original_regno < FIRST_PSEUDO_REGISTER)
4746 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4747 hard_regno = original_regno;
4748 call_save_p = false;
4749 nregs = 1;
4751 else
4753 hard_regno = reg_renumber[original_regno];
4754 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4755 rclass = lra_get_allocno_class (original_regno);
4756 original_reg = regno_reg_rtx[original_regno];
4757 call_save_p = need_for_call_save_p (original_regno);
4759 original_reg = regno_reg_rtx[original_regno];
4760 lra_assert (hard_regno >= 0);
4761 if (lra_dump_file != NULL)
4762 fprintf (lra_dump_file,
4763 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4764 if (call_save_p)
4766 enum machine_mode mode = GET_MODE (original_reg);
4768 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4769 hard_regno_nregs[hard_regno][mode],
4770 mode);
4771 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4773 else
4775 rclass = choose_split_class (rclass, hard_regno,
4776 GET_MODE (original_reg));
4777 if (rclass == NO_REGS)
4779 if (lra_dump_file != NULL)
4781 fprintf (lra_dump_file,
4782 " Rejecting split of %d(%s): "
4783 "no good reg class for %d(%s)\n",
4784 original_regno,
4785 reg_class_names[lra_get_allocno_class (original_regno)],
4786 hard_regno,
4787 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4788 fprintf
4789 (lra_dump_file,
4790 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4792 return false;
4794 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4795 rclass, "split");
4796 reg_renumber[REGNO (new_reg)] = hard_regno;
4798 save = emit_spill_move (true, new_reg, original_reg);
4799 if (NEXT_INSN (save) != NULL_RTX)
4801 lra_assert (! call_save_p);
4802 if (lra_dump_file != NULL)
4804 fprintf
4805 (lra_dump_file,
4806 " Rejecting split %d->%d resulting in > 2 %s save insns:\n",
4807 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4808 dump_rtl_slim (lra_dump_file, save, NULL_RTX, -1, 0);
4809 fprintf (lra_dump_file,
4810 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4812 return false;
4814 restore = emit_spill_move (false, new_reg, original_reg);
4815 if (NEXT_INSN (restore) != NULL_RTX)
4817 lra_assert (! call_save_p);
4818 if (lra_dump_file != NULL)
4820 fprintf (lra_dump_file,
4821 " Rejecting split %d->%d "
4822 "resulting in > 2 %s restore insns:\n",
4823 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4824 dump_rtl_slim (lra_dump_file, restore, NULL_RTX, -1, 0);
4825 fprintf (lra_dump_file,
4826 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4828 return false;
4830 after_p = usage_insns[original_regno].after_p;
4831 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4832 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4833 bitmap_set_bit (&check_only_regs, original_regno);
4834 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
4835 for (;;)
4837 if (GET_CODE (next_usage_insns) != INSN_LIST)
4839 usage_insn = next_usage_insns;
4840 break;
4842 usage_insn = XEXP (next_usage_insns, 0);
4843 lra_assert (DEBUG_INSN_P (usage_insn));
4844 next_usage_insns = XEXP (next_usage_insns, 1);
4845 substitute_pseudo (&usage_insn, original_regno, new_reg);
4846 lra_update_insn_regno_info (usage_insn);
4847 if (lra_dump_file != NULL)
4849 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
4850 original_regno, REGNO (new_reg));
4851 dump_insn_slim (lra_dump_file, usage_insn);
4854 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
4855 lra_assert (usage_insn != insn || (after_p && before_p));
4856 lra_process_new_insns (usage_insn, after_p ? NULL_RTX : restore,
4857 after_p ? restore : NULL_RTX,
4858 call_save_p
4859 ? "Add reg<-save" : "Add reg<-split");
4860 lra_process_new_insns (insn, before_p ? save : NULL_RTX,
4861 before_p ? NULL_RTX : save,
4862 call_save_p
4863 ? "Add save<-reg" : "Add split<-reg");
4864 if (nregs > 1)
4865 /* If we are trying to split multi-register. We should check
4866 conflicts on the next assignment sub-pass. IRA can allocate on
4867 sub-register levels, LRA do this on pseudos level right now and
4868 this discrepancy may create allocation conflicts after
4869 splitting. */
4870 lra_risky_transformations_p = true;
4871 if (lra_dump_file != NULL)
4872 fprintf (lra_dump_file,
4873 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4874 return true;
4877 /* Recognize that we need a split transformation for insn INSN, which
4878 defines or uses REGNO in its insn biggest MODE (we use it only if
4879 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
4880 hard registers which might be used for reloads since the EBB end.
4881 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
4882 uid before starting INSN processing. Return true if we succeed in
4883 such transformation. */
4884 static bool
4885 split_if_necessary (int regno, enum machine_mode mode,
4886 HARD_REG_SET potential_reload_hard_regs,
4887 bool before_p, rtx insn, int max_uid)
4889 bool res = false;
4890 int i, nregs = 1;
4891 rtx next_usage_insns;
4893 if (regno < FIRST_PSEUDO_REGISTER)
4894 nregs = hard_regno_nregs[regno][mode];
4895 for (i = 0; i < nregs; i++)
4896 if (usage_insns[regno + i].check == curr_usage_insns_check
4897 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
4898 /* To avoid processing the register twice or more. */
4899 && ((GET_CODE (next_usage_insns) != INSN_LIST
4900 && INSN_UID (next_usage_insns) < max_uid)
4901 || (GET_CODE (next_usage_insns) == INSN_LIST
4902 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
4903 && need_for_split_p (potential_reload_hard_regs, regno + i)
4904 && split_reg (before_p, regno + i, insn, next_usage_insns))
4905 res = true;
4906 return res;
4909 /* Check only registers living at the current program point in the
4910 current EBB. */
4911 static bitmap_head live_regs;
4913 /* Update live info in EBB given by its HEAD and TAIL insns after
4914 inheritance/split transformation. The function removes dead moves
4915 too. */
4916 static void
4917 update_ebb_live_info (rtx head, rtx tail)
4919 unsigned int j;
4920 int i, regno;
4921 bool live_p;
4922 rtx prev_insn, set;
4923 bool remove_p;
4924 basic_block last_bb, prev_bb, curr_bb;
4925 bitmap_iterator bi;
4926 struct lra_insn_reg *reg;
4927 edge e;
4928 edge_iterator ei;
4930 last_bb = BLOCK_FOR_INSN (tail);
4931 prev_bb = NULL;
4932 for (curr_insn = tail;
4933 curr_insn != PREV_INSN (head);
4934 curr_insn = prev_insn)
4936 prev_insn = PREV_INSN (curr_insn);
4937 /* We need to process empty blocks too. They contain
4938 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
4939 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
4940 continue;
4941 curr_bb = BLOCK_FOR_INSN (curr_insn);
4942 if (curr_bb != prev_bb)
4944 if (prev_bb != NULL)
4946 /* Update df_get_live_in (prev_bb): */
4947 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4948 if (bitmap_bit_p (&live_regs, j))
4949 bitmap_set_bit (df_get_live_in (prev_bb), j);
4950 else
4951 bitmap_clear_bit (df_get_live_in (prev_bb), j);
4953 if (curr_bb != last_bb)
4955 /* Update df_get_live_out (curr_bb): */
4956 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4958 live_p = bitmap_bit_p (&live_regs, j);
4959 if (! live_p)
4960 FOR_EACH_EDGE (e, ei, curr_bb->succs)
4961 if (bitmap_bit_p (df_get_live_in (e->dest), j))
4963 live_p = true;
4964 break;
4966 if (live_p)
4967 bitmap_set_bit (df_get_live_out (curr_bb), j);
4968 else
4969 bitmap_clear_bit (df_get_live_out (curr_bb), j);
4972 prev_bb = curr_bb;
4973 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
4975 if (! NONDEBUG_INSN_P (curr_insn))
4976 continue;
4977 curr_id = lra_get_insn_recog_data (curr_insn);
4978 curr_static_id = curr_id->insn_static_data;
4979 remove_p = false;
4980 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
4981 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
4982 && bitmap_bit_p (&check_only_regs, regno)
4983 && ! bitmap_bit_p (&live_regs, regno))
4984 remove_p = true;
4985 /* See which defined values die here. */
4986 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4987 if (reg->type == OP_OUT && ! reg->subreg_p)
4988 bitmap_clear_bit (&live_regs, reg->regno);
4989 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
4990 if (reg->type == OP_OUT && ! reg->subreg_p)
4991 bitmap_clear_bit (&live_regs, reg->regno);
4992 /* Mark each used value as live. */
4993 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4994 if (reg->type != OP_OUT
4995 && bitmap_bit_p (&check_only_regs, reg->regno))
4996 bitmap_set_bit (&live_regs, reg->regno);
4997 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
4998 if (reg->type != OP_OUT
4999 && bitmap_bit_p (&check_only_regs, reg->regno))
5000 bitmap_set_bit (&live_regs, reg->regno);
5001 if (curr_id->arg_hard_regs != NULL)
5002 /* Make argument hard registers live. */
5003 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5004 if (bitmap_bit_p (&check_only_regs, regno))
5005 bitmap_set_bit (&live_regs, regno);
5006 /* It is quite important to remove dead move insns because it
5007 means removing dead store. We don't need to process them for
5008 constraints. */
5009 if (remove_p)
5011 if (lra_dump_file != NULL)
5013 fprintf (lra_dump_file, " Removing dead insn:\n ");
5014 dump_insn_slim (lra_dump_file, curr_insn);
5016 lra_set_insn_deleted (curr_insn);
5021 /* The structure describes info to do an inheritance for the current
5022 insn. We need to collect such info first before doing the
5023 transformations because the transformations change the insn
5024 internal representation. */
5025 struct to_inherit
5027 /* Original regno. */
5028 int regno;
5029 /* Subsequent insns which can inherit original reg value. */
5030 rtx insns;
5033 /* Array containing all info for doing inheritance from the current
5034 insn. */
5035 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5037 /* Number elements in the previous array. */
5038 static int to_inherit_num;
5040 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5041 structure to_inherit. */
5042 static void
5043 add_to_inherit (int regno, rtx insns)
5045 int i;
5047 for (i = 0; i < to_inherit_num; i++)
5048 if (to_inherit[i].regno == regno)
5049 return;
5050 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5051 to_inherit[to_inherit_num].regno = regno;
5052 to_inherit[to_inherit_num++].insns = insns;
5055 /* Return the last non-debug insn in basic block BB, or the block begin
5056 note if none. */
5057 static rtx
5058 get_last_insertion_point (basic_block bb)
5060 rtx insn;
5062 FOR_BB_INSNS_REVERSE (bb, insn)
5063 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5064 return insn;
5065 gcc_unreachable ();
5068 /* Set up RES by registers living on edges FROM except the edge (FROM,
5069 TO) or by registers set up in a jump insn in BB FROM. */
5070 static void
5071 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5073 rtx last;
5074 struct lra_insn_reg *reg;
5075 edge e;
5076 edge_iterator ei;
5078 lra_assert (to != NULL);
5079 bitmap_clear (res);
5080 FOR_EACH_EDGE (e, ei, from->succs)
5081 if (e->dest != to)
5082 bitmap_ior_into (res, df_get_live_in (e->dest));
5083 last = get_last_insertion_point (from);
5084 if (! JUMP_P (last))
5085 return;
5086 curr_id = lra_get_insn_recog_data (last);
5087 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5088 if (reg->type != OP_IN)
5089 bitmap_set_bit (res, reg->regno);
5092 /* Used as a temporary results of some bitmap calculations. */
5093 static bitmap_head temp_bitmap;
5095 /* We split for reloads of small class of hard regs. The following
5096 defines how many hard regs the class should have to be qualified as
5097 small. The code is mostly oriented to x86/x86-64 architecture
5098 where some insns need to use only specific register or pair of
5099 registers and these register can live in RTL explicitly, e.g. for
5100 parameter passing. */
5101 static const int max_small_class_regs_num = 2;
5103 /* Do inheritance/split transformations in EBB starting with HEAD and
5104 finishing on TAIL. We process EBB insns in the reverse order.
5105 Return true if we did any inheritance/split transformation in the
5106 EBB.
5108 We should avoid excessive splitting which results in worse code
5109 because of inaccurate cost calculations for spilling new split
5110 pseudos in such case. To achieve this we do splitting only if
5111 register pressure is high in given basic block and there are reload
5112 pseudos requiring hard registers. We could do more register
5113 pressure calculations at any given program point to avoid necessary
5114 splitting even more but it is to expensive and the current approach
5115 works well enough. */
5116 static bool
5117 inherit_in_ebb (rtx head, rtx tail)
5119 int i, src_regno, dst_regno, nregs;
5120 bool change_p, succ_p, update_reloads_num_p;
5121 rtx prev_insn, next_usage_insns, set, last_insn;
5122 enum reg_class cl;
5123 struct lra_insn_reg *reg;
5124 basic_block last_processed_bb, curr_bb = NULL;
5125 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5126 bitmap to_process;
5127 unsigned int j;
5128 bitmap_iterator bi;
5129 bool head_p, after_p;
5131 change_p = false;
5132 curr_usage_insns_check++;
5133 reloads_num = calls_num = 0;
5134 bitmap_clear (&check_only_regs);
5135 last_processed_bb = NULL;
5136 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5137 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5138 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5139 /* We don't process new insns generated in the loop. */
5140 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5142 prev_insn = PREV_INSN (curr_insn);
5143 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5144 curr_bb = BLOCK_FOR_INSN (curr_insn);
5145 if (last_processed_bb != curr_bb)
5147 /* We are at the end of BB. Add qualified living
5148 pseudos for potential splitting. */
5149 to_process = df_get_live_out (curr_bb);
5150 if (last_processed_bb != NULL)
5152 /* We are somewhere in the middle of EBB. */
5153 get_live_on_other_edges (curr_bb, last_processed_bb,
5154 &temp_bitmap);
5155 to_process = &temp_bitmap;
5157 last_processed_bb = curr_bb;
5158 last_insn = get_last_insertion_point (curr_bb);
5159 after_p = (! JUMP_P (last_insn)
5160 && (! CALL_P (last_insn)
5161 || (find_reg_note (last_insn,
5162 REG_NORETURN, NULL_RTX) == NULL_RTX
5163 && ! SIBLING_CALL_P (last_insn))));
5164 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5165 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5167 if ((int) j >= lra_constraint_new_regno_start)
5168 break;
5169 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5171 if (j < FIRST_PSEUDO_REGISTER)
5172 SET_HARD_REG_BIT (live_hard_regs, j);
5173 else
5174 add_to_hard_reg_set (&live_hard_regs,
5175 PSEUDO_REGNO_MODE (j),
5176 reg_renumber[j]);
5177 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5181 src_regno = dst_regno = -1;
5182 if (NONDEBUG_INSN_P (curr_insn)
5183 && (set = single_set (curr_insn)) != NULL_RTX
5184 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5186 src_regno = REGNO (SET_SRC (set));
5187 dst_regno = REGNO (SET_DEST (set));
5189 update_reloads_num_p = true;
5190 if (src_regno < lra_constraint_new_regno_start
5191 && src_regno >= FIRST_PSEUDO_REGISTER
5192 && reg_renumber[src_regno] < 0
5193 && dst_regno >= lra_constraint_new_regno_start
5194 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5196 /* 'reload_pseudo <- original_pseudo'. */
5197 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5198 reloads_num++;
5199 update_reloads_num_p = false;
5200 succ_p = false;
5201 if (usage_insns[src_regno].check == curr_usage_insns_check
5202 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5203 succ_p = inherit_reload_reg (false, src_regno, cl,
5204 curr_insn, next_usage_insns);
5205 if (succ_p)
5206 change_p = true;
5207 else
5208 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5209 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5210 IOR_HARD_REG_SET (potential_reload_hard_regs,
5211 reg_class_contents[cl]);
5213 else if (src_regno >= lra_constraint_new_regno_start
5214 && dst_regno < lra_constraint_new_regno_start
5215 && dst_regno >= FIRST_PSEUDO_REGISTER
5216 && reg_renumber[dst_regno] < 0
5217 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5218 && usage_insns[dst_regno].check == curr_usage_insns_check
5219 && (next_usage_insns
5220 = usage_insns[dst_regno].insns) != NULL_RTX)
5222 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5223 reloads_num++;
5224 update_reloads_num_p = false;
5225 /* 'original_pseudo <- reload_pseudo'. */
5226 if (! JUMP_P (curr_insn)
5227 && inherit_reload_reg (true, dst_regno, cl,
5228 curr_insn, next_usage_insns))
5229 change_p = true;
5230 /* Invalidate. */
5231 usage_insns[dst_regno].check = 0;
5232 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5233 IOR_HARD_REG_SET (potential_reload_hard_regs,
5234 reg_class_contents[cl]);
5236 else if (INSN_P (curr_insn))
5238 int iter;
5239 int max_uid = get_max_uid ();
5241 curr_id = lra_get_insn_recog_data (curr_insn);
5242 curr_static_id = curr_id->insn_static_data;
5243 to_inherit_num = 0;
5244 /* Process insn definitions. */
5245 for (iter = 0; iter < 2; iter++)
5246 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5247 reg != NULL;
5248 reg = reg->next)
5249 if (reg->type != OP_IN
5250 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5252 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5253 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5254 && usage_insns[dst_regno].check == curr_usage_insns_check
5255 && (next_usage_insns
5256 = usage_insns[dst_regno].insns) != NULL_RTX)
5258 struct lra_insn_reg *r;
5260 for (r = curr_id->regs; r != NULL; r = r->next)
5261 if (r->type != OP_OUT && r->regno == dst_regno)
5262 break;
5263 /* Don't do inheritance if the pseudo is also
5264 used in the insn. */
5265 if (r == NULL)
5266 /* We can not do inheritance right now
5267 because the current insn reg info (chain
5268 regs) can change after that. */
5269 add_to_inherit (dst_regno, next_usage_insns);
5271 /* We can not process one reg twice here because of
5272 usage_insns invalidation. */
5273 if ((dst_regno < FIRST_PSEUDO_REGISTER
5274 || reg_renumber[dst_regno] >= 0)
5275 && ! reg->subreg_p && reg->type != OP_IN)
5277 HARD_REG_SET s;
5279 if (split_if_necessary (dst_regno, reg->biggest_mode,
5280 potential_reload_hard_regs,
5281 false, curr_insn, max_uid))
5282 change_p = true;
5283 CLEAR_HARD_REG_SET (s);
5284 if (dst_regno < FIRST_PSEUDO_REGISTER)
5285 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5286 else
5287 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5288 reg_renumber[dst_regno]);
5289 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5291 /* We should invalidate potential inheritance or
5292 splitting for the current insn usages to the next
5293 usage insns (see code below) as the output pseudo
5294 prevents this. */
5295 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5296 && reg_renumber[dst_regno] < 0)
5297 || (reg->type == OP_OUT && ! reg->subreg_p
5298 && (dst_regno < FIRST_PSEUDO_REGISTER
5299 || reg_renumber[dst_regno] >= 0)))
5301 /* Invalidate and mark definitions. */
5302 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5303 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5304 else
5306 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5307 for (i = 0; i < nregs; i++)
5308 usage_insns[dst_regno + i].check
5309 = -(int) INSN_UID (curr_insn);
5313 if (! JUMP_P (curr_insn))
5314 for (i = 0; i < to_inherit_num; i++)
5315 if (inherit_reload_reg (true, to_inherit[i].regno,
5316 ALL_REGS, curr_insn,
5317 to_inherit[i].insns))
5318 change_p = true;
5319 if (CALL_P (curr_insn))
5321 rtx cheap, pat, dest, restore;
5322 int regno, hard_regno;
5324 calls_num++;
5325 if ((cheap = find_reg_note (curr_insn,
5326 REG_RETURNED, NULL_RTX)) != NULL_RTX
5327 && ((cheap = XEXP (cheap, 0)), true)
5328 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5329 && (hard_regno = reg_renumber[regno]) >= 0
5330 /* If there are pending saves/restores, the
5331 optimization is not worth. */
5332 && usage_insns[regno].calls_num == calls_num - 1
5333 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5335 /* Restore the pseudo from the call result as
5336 REG_RETURNED note says that the pseudo value is
5337 in the call result and the pseudo is an argument
5338 of the call. */
5339 pat = PATTERN (curr_insn);
5340 if (GET_CODE (pat) == PARALLEL)
5341 pat = XVECEXP (pat, 0, 0);
5342 dest = SET_DEST (pat);
5343 start_sequence ();
5344 emit_move_insn (cheap, copy_rtx (dest));
5345 restore = get_insns ();
5346 end_sequence ();
5347 lra_process_new_insns (curr_insn, NULL, restore,
5348 "Inserting call parameter restore");
5349 /* We don't need to save/restore of the pseudo from
5350 this call. */
5351 usage_insns[regno].calls_num = calls_num;
5352 bitmap_set_bit (&check_only_regs, regno);
5355 to_inherit_num = 0;
5356 /* Process insn usages. */
5357 for (iter = 0; iter < 2; iter++)
5358 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5359 reg != NULL;
5360 reg = reg->next)
5361 if ((reg->type != OP_OUT
5362 || (reg->type == OP_OUT && reg->subreg_p))
5363 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5365 if (src_regno >= FIRST_PSEUDO_REGISTER
5366 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5368 if (usage_insns[src_regno].check == curr_usage_insns_check
5369 && (next_usage_insns
5370 = usage_insns[src_regno].insns) != NULL_RTX
5371 && NONDEBUG_INSN_P (curr_insn))
5372 add_to_inherit (src_regno, next_usage_insns);
5373 else if (usage_insns[src_regno].check
5374 != -(int) INSN_UID (curr_insn))
5375 /* Add usages but only if the reg is not set up
5376 in the same insn. */
5377 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5379 else if (src_regno < FIRST_PSEUDO_REGISTER
5380 || reg_renumber[src_regno] >= 0)
5382 bool before_p;
5383 rtx use_insn = curr_insn;
5385 before_p = (JUMP_P (curr_insn)
5386 || (CALL_P (curr_insn) && reg->type == OP_IN));
5387 if (NONDEBUG_INSN_P (curr_insn)
5388 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5389 && split_if_necessary (src_regno, reg->biggest_mode,
5390 potential_reload_hard_regs,
5391 before_p, curr_insn, max_uid))
5393 if (reg->subreg_p)
5394 lra_risky_transformations_p = true;
5395 change_p = true;
5396 /* Invalidate. */
5397 usage_insns[src_regno].check = 0;
5398 if (before_p)
5399 use_insn = PREV_INSN (curr_insn);
5401 if (NONDEBUG_INSN_P (curr_insn))
5403 if (src_regno < FIRST_PSEUDO_REGISTER)
5404 add_to_hard_reg_set (&live_hard_regs,
5405 reg->biggest_mode, src_regno);
5406 else
5407 add_to_hard_reg_set (&live_hard_regs,
5408 PSEUDO_REGNO_MODE (src_regno),
5409 reg_renumber[src_regno]);
5411 add_next_usage_insn (src_regno, use_insn, reloads_num);
5414 /* Process call args. */
5415 if (curr_id->arg_hard_regs != NULL)
5416 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5417 if (src_regno < FIRST_PSEUDO_REGISTER)
5419 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5420 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5422 for (i = 0; i < to_inherit_num; i++)
5424 src_regno = to_inherit[i].regno;
5425 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5426 curr_insn, to_inherit[i].insns))
5427 change_p = true;
5428 else
5429 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5432 if (update_reloads_num_p
5433 && NONDEBUG_INSN_P (curr_insn)
5434 && (set = single_set (curr_insn)) != NULL_RTX)
5436 int regno = -1;
5437 if ((REG_P (SET_DEST (set))
5438 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5439 && reg_renumber[regno] < 0
5440 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5441 || (REG_P (SET_SRC (set))
5442 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5443 && reg_renumber[regno] < 0
5444 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5446 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5447 reloads_num++;
5448 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5449 IOR_HARD_REG_SET (potential_reload_hard_regs,
5450 reg_class_contents[cl]);
5453 /* We reached the start of the current basic block. */
5454 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5455 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5457 /* We reached the beginning of the current block -- do
5458 rest of spliting in the current BB. */
5459 to_process = df_get_live_in (curr_bb);
5460 if (BLOCK_FOR_INSN (head) != curr_bb)
5462 /* We are somewhere in the middle of EBB. */
5463 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5464 curr_bb, &temp_bitmap);
5465 to_process = &temp_bitmap;
5467 head_p = true;
5468 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5470 if ((int) j >= lra_constraint_new_regno_start)
5471 break;
5472 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5473 && usage_insns[j].check == curr_usage_insns_check
5474 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5476 if (need_for_split_p (potential_reload_hard_regs, j))
5478 if (lra_dump_file != NULL && head_p)
5480 fprintf (lra_dump_file,
5481 " ----------------------------------\n");
5482 head_p = false;
5484 if (split_reg (false, j, bb_note (curr_bb),
5485 next_usage_insns))
5486 change_p = true;
5488 usage_insns[j].check = 0;
5493 return change_p;
5496 /* This value affects EBB forming. If probability of edge from EBB to
5497 a BB is not greater than the following value, we don't add the BB
5498 to EBB. */
5499 #define EBB_PROBABILITY_CUTOFF ((REG_BR_PROB_BASE * 50) / 100)
5501 /* Current number of inheritance/split iteration. */
5502 int lra_inheritance_iter;
5504 /* Entry function for inheritance/split pass. */
5505 void
5506 lra_inheritance (void)
5508 int i;
5509 basic_block bb, start_bb;
5510 edge e;
5512 lra_inheritance_iter++;
5513 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5514 return;
5515 timevar_push (TV_LRA_INHERITANCE);
5516 if (lra_dump_file != NULL)
5517 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5518 lra_inheritance_iter);
5519 curr_usage_insns_check = 0;
5520 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5521 for (i = 0; i < lra_constraint_new_regno_start; i++)
5522 usage_insns[i].check = 0;
5523 bitmap_initialize (&check_only_regs, &reg_obstack);
5524 bitmap_initialize (&live_regs, &reg_obstack);
5525 bitmap_initialize (&temp_bitmap, &reg_obstack);
5526 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5527 FOR_EACH_BB_FN (bb, cfun)
5529 start_bb = bb;
5530 if (lra_dump_file != NULL)
5531 fprintf (lra_dump_file, "EBB");
5532 /* Form a EBB starting with BB. */
5533 bitmap_clear (&ebb_global_regs);
5534 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5535 for (;;)
5537 if (lra_dump_file != NULL)
5538 fprintf (lra_dump_file, " %d", bb->index);
5539 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5540 || LABEL_P (BB_HEAD (bb->next_bb)))
5541 break;
5542 e = find_fallthru_edge (bb->succs);
5543 if (! e)
5544 break;
5545 if (e->probability <= EBB_PROBABILITY_CUTOFF)
5546 break;
5547 bb = bb->next_bb;
5549 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5550 if (lra_dump_file != NULL)
5551 fprintf (lra_dump_file, "\n");
5552 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5553 /* Remember that the EBB head and tail can change in
5554 inherit_in_ebb. */
5555 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5557 bitmap_clear (&ebb_global_regs);
5558 bitmap_clear (&temp_bitmap);
5559 bitmap_clear (&live_regs);
5560 bitmap_clear (&check_only_regs);
5561 free (usage_insns);
5563 timevar_pop (TV_LRA_INHERITANCE);
5568 /* This page contains code to undo failed inheritance/split
5569 transformations. */
5571 /* Current number of iteration undoing inheritance/split. */
5572 int lra_undo_inheritance_iter;
5574 /* Fix BB live info LIVE after removing pseudos created on pass doing
5575 inheritance/split which are REMOVED_PSEUDOS. */
5576 static void
5577 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5579 unsigned int regno;
5580 bitmap_iterator bi;
5582 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5583 if (bitmap_clear_bit (live, regno))
5584 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5587 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5588 number. */
5589 static int
5590 get_regno (rtx reg)
5592 if (GET_CODE (reg) == SUBREG)
5593 reg = SUBREG_REG (reg);
5594 if (REG_P (reg))
5595 return REGNO (reg);
5596 return -1;
5599 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5600 return true if we did any change. The undo transformations for
5601 inheritance looks like
5602 i <- i2
5603 p <- i => p <- i2
5604 or removing
5605 p <- i, i <- p, and i <- i3
5606 where p is original pseudo from which inheritance pseudo i was
5607 created, i and i3 are removed inheritance pseudos, i2 is another
5608 not removed inheritance pseudo. All split pseudos or other
5609 occurrences of removed inheritance pseudos are changed on the
5610 corresponding original pseudos.
5612 The function also schedules insns changed and created during
5613 inheritance/split pass for processing by the subsequent constraint
5614 pass. */
5615 static bool
5616 remove_inheritance_pseudos (bitmap remove_pseudos)
5618 basic_block bb;
5619 int regno, sregno, prev_sregno, dregno, restore_regno;
5620 rtx set, prev_set, prev_insn;
5621 bool change_p, done_p;
5623 change_p = ! bitmap_empty_p (remove_pseudos);
5624 /* We can not finish the function right away if CHANGE_P is true
5625 because we need to marks insns affected by previous
5626 inheritance/split pass for processing by the subsequent
5627 constraint pass. */
5628 FOR_EACH_BB_FN (bb, cfun)
5630 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5631 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5632 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5634 if (! INSN_P (curr_insn))
5635 continue;
5636 done_p = false;
5637 sregno = dregno = -1;
5638 if (change_p && NONDEBUG_INSN_P (curr_insn)
5639 && (set = single_set (curr_insn)) != NULL_RTX)
5641 dregno = get_regno (SET_DEST (set));
5642 sregno = get_regno (SET_SRC (set));
5645 if (sregno >= 0 && dregno >= 0)
5647 if ((bitmap_bit_p (remove_pseudos, sregno)
5648 && (lra_reg_info[sregno].restore_regno == dregno
5649 || (bitmap_bit_p (remove_pseudos, dregno)
5650 && (lra_reg_info[sregno].restore_regno
5651 == lra_reg_info[dregno].restore_regno))))
5652 || (bitmap_bit_p (remove_pseudos, dregno)
5653 && lra_reg_info[dregno].restore_regno == sregno))
5654 /* One of the following cases:
5655 original <- removed inheritance pseudo
5656 removed inherit pseudo <- another removed inherit pseudo
5657 removed inherit pseudo <- original pseudo
5659 removed_split_pseudo <- original_reg
5660 original_reg <- removed_split_pseudo */
5662 if (lra_dump_file != NULL)
5664 fprintf (lra_dump_file, " Removing %s:\n",
5665 bitmap_bit_p (&lra_split_regs, sregno)
5666 || bitmap_bit_p (&lra_split_regs, dregno)
5667 ? "split" : "inheritance");
5668 dump_insn_slim (lra_dump_file, curr_insn);
5670 lra_set_insn_deleted (curr_insn);
5671 done_p = true;
5673 else if (bitmap_bit_p (remove_pseudos, sregno)
5674 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5676 /* Search the following pattern:
5677 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5678 original_pseudo <- inherit_or_split_pseudo1
5679 where the 2nd insn is the current insn and
5680 inherit_or_split_pseudo2 is not removed. If it is found,
5681 change the current insn onto:
5682 original_pseudo <- inherit_or_split_pseudo2. */
5683 for (prev_insn = PREV_INSN (curr_insn);
5684 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5685 prev_insn = PREV_INSN (prev_insn))
5687 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5688 && (prev_set = single_set (prev_insn)) != NULL_RTX
5689 /* There should be no subregs in insn we are
5690 searching because only the original reg might
5691 be in subreg when we changed the mode of
5692 load/store for splitting. */
5693 && REG_P (SET_DEST (prev_set))
5694 && REG_P (SET_SRC (prev_set))
5695 && (int) REGNO (SET_DEST (prev_set)) == sregno
5696 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5697 >= FIRST_PSEUDO_REGISTER)
5698 /* As we consider chain of inheritance or
5699 splitting described in above comment we should
5700 check that sregno and prev_sregno were
5701 inheritance/split pseudos created from the
5702 same original regno. */
5703 && (lra_reg_info[sregno].restore_regno
5704 == lra_reg_info[prev_sregno].restore_regno)
5705 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5707 lra_assert (GET_MODE (SET_SRC (prev_set))
5708 == GET_MODE (regno_reg_rtx[sregno]));
5709 if (GET_CODE (SET_SRC (set)) == SUBREG)
5710 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5711 else
5712 SET_SRC (set) = SET_SRC (prev_set);
5713 lra_push_insn_and_update_insn_regno_info (curr_insn);
5714 lra_set_used_insn_alternative_by_uid
5715 (INSN_UID (curr_insn), -1);
5716 done_p = true;
5717 if (lra_dump_file != NULL)
5719 fprintf (lra_dump_file, " Change reload insn:\n");
5720 dump_insn_slim (lra_dump_file, curr_insn);
5725 if (! done_p)
5727 struct lra_insn_reg *reg;
5728 bool restored_regs_p = false;
5729 bool kept_regs_p = false;
5731 curr_id = lra_get_insn_recog_data (curr_insn);
5732 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5734 regno = reg->regno;
5735 restore_regno = lra_reg_info[regno].restore_regno;
5736 if (restore_regno >= 0)
5738 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5740 substitute_pseudo (&curr_insn, regno,
5741 regno_reg_rtx[restore_regno]);
5742 restored_regs_p = true;
5744 else
5745 kept_regs_p = true;
5748 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5750 /* The instruction has changed since the previous
5751 constraints pass. */
5752 lra_push_insn_and_update_insn_regno_info (curr_insn);
5753 lra_set_used_insn_alternative_by_uid
5754 (INSN_UID (curr_insn), -1);
5756 else if (restored_regs_p)
5757 /* The instruction has been restored to the form that
5758 it had during the previous constraints pass. */
5759 lra_update_insn_regno_info (curr_insn);
5760 if (restored_regs_p && lra_dump_file != NULL)
5762 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5763 dump_insn_slim (lra_dump_file, curr_insn);
5768 return change_p;
5771 /* If optional reload pseudos failed to get a hard register or was not
5772 inherited, it is better to remove optional reloads. We do this
5773 transformation after undoing inheritance to figure out necessity to
5774 remove optional reloads easier. Return true if we do any
5775 change. */
5776 static bool
5777 undo_optional_reloads (void)
5779 bool change_p, keep_p;
5780 unsigned int regno, uid;
5781 bitmap_iterator bi, bi2;
5782 rtx insn, set, src, dest;
5783 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
5785 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
5786 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
5787 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5789 keep_p = false;
5790 /* Keep optional reloads from previous subpasses. */
5791 if (lra_reg_info[regno].restore_regno < 0
5792 /* If the original pseudo changed its allocation, just
5793 removing the optional pseudo is dangerous as the original
5794 pseudo will have longer live range. */
5795 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
5796 keep_p = true;
5797 else if (reg_renumber[regno] >= 0)
5798 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
5800 insn = lra_insn_recog_data[uid]->insn;
5801 if ((set = single_set (insn)) == NULL_RTX)
5802 continue;
5803 src = SET_SRC (set);
5804 dest = SET_DEST (set);
5805 if (! REG_P (src) || ! REG_P (dest))
5806 continue;
5807 if (REGNO (dest) == regno
5808 /* Ignore insn for optional reloads itself. */
5809 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
5810 /* Check only inheritance on last inheritance pass. */
5811 && (int) REGNO (src) >= new_regno_start
5812 /* Check that the optional reload was inherited. */
5813 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
5815 keep_p = true;
5816 break;
5819 if (keep_p)
5821 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
5822 if (lra_dump_file != NULL)
5823 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
5826 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
5827 bitmap_initialize (&insn_bitmap, &reg_obstack);
5828 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
5830 if (lra_dump_file != NULL)
5831 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
5832 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
5833 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
5835 insn = lra_insn_recog_data[uid]->insn;
5836 if ((set = single_set (insn)) != NULL_RTX)
5838 src = SET_SRC (set);
5839 dest = SET_DEST (set);
5840 if (REG_P (src) && REG_P (dest)
5841 && ((REGNO (src) == regno
5842 && (lra_reg_info[regno].restore_regno
5843 == (int) REGNO (dest)))
5844 || (REGNO (dest) == regno
5845 && (lra_reg_info[regno].restore_regno
5846 == (int) REGNO (src)))))
5848 if (lra_dump_file != NULL)
5850 fprintf (lra_dump_file, " Deleting move %u\n",
5851 INSN_UID (insn));
5852 dump_insn_slim (lra_dump_file, insn);
5854 lra_set_insn_deleted (insn);
5855 continue;
5857 /* We should not worry about generation memory-memory
5858 moves here as if the corresponding inheritance did
5859 not work (inheritance pseudo did not get a hard reg),
5860 we remove the inheritance pseudo and the optional
5861 reload. */
5863 substitute_pseudo (&insn, regno,
5864 regno_reg_rtx[lra_reg_info[regno].restore_regno]);
5865 lra_update_insn_regno_info (insn);
5866 if (lra_dump_file != NULL)
5868 fprintf (lra_dump_file,
5869 " Restoring original insn:\n");
5870 dump_insn_slim (lra_dump_file, insn);
5874 /* Clear restore_regnos. */
5875 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5876 lra_reg_info[regno].restore_regno = -1;
5877 bitmap_clear (&insn_bitmap);
5878 bitmap_clear (&removed_optional_reload_pseudos);
5879 return change_p;
5882 /* Entry function for undoing inheritance/split transformation. Return true
5883 if we did any RTL change in this pass. */
5884 bool
5885 lra_undo_inheritance (void)
5887 unsigned int regno;
5888 int restore_regno, hard_regno;
5889 int n_all_inherit, n_inherit, n_all_split, n_split;
5890 bitmap_head remove_pseudos;
5891 bitmap_iterator bi;
5892 bool change_p;
5894 lra_undo_inheritance_iter++;
5895 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5896 return false;
5897 if (lra_dump_file != NULL)
5898 fprintf (lra_dump_file,
5899 "\n********** Undoing inheritance #%d: **********\n\n",
5900 lra_undo_inheritance_iter);
5901 bitmap_initialize (&remove_pseudos, &reg_obstack);
5902 n_inherit = n_all_inherit = 0;
5903 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5904 if (lra_reg_info[regno].restore_regno >= 0)
5906 n_all_inherit++;
5907 if (reg_renumber[regno] < 0
5908 /* If the original pseudo changed its allocation, just
5909 removing inheritance is dangerous as for changing
5910 allocation we used shorter live-ranges. */
5911 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
5912 bitmap_set_bit (&remove_pseudos, regno);
5913 else
5914 n_inherit++;
5916 if (lra_dump_file != NULL && n_all_inherit != 0)
5917 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
5918 n_inherit, n_all_inherit,
5919 (double) n_inherit / n_all_inherit * 100);
5920 n_split = n_all_split = 0;
5921 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5922 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
5924 n_all_split++;
5925 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
5926 ? reg_renumber[restore_regno] : restore_regno);
5927 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
5928 bitmap_set_bit (&remove_pseudos, regno);
5929 else
5931 n_split++;
5932 if (lra_dump_file != NULL)
5933 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
5934 regno, restore_regno);
5937 if (lra_dump_file != NULL && n_all_split != 0)
5938 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
5939 n_split, n_all_split,
5940 (double) n_split / n_all_split * 100);
5941 change_p = remove_inheritance_pseudos (&remove_pseudos);
5942 bitmap_clear (&remove_pseudos);
5943 /* Clear restore_regnos. */
5944 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5945 lra_reg_info[regno].restore_regno = -1;
5946 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5947 lra_reg_info[regno].restore_regno = -1;
5948 change_p = undo_optional_reloads () || change_p;
5949 return change_p;