* lto-partition.c (add_symbol_to_partition_1,
[official-gcc.git] / gcc / lra-constraints.c
blobd3831380feed94c93c3dc7532208a8835b6a551e
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). The
443 result pseudo is returned through RESULT_REG. Return TRUE if we
444 created a new pseudo, FALSE if we reused the already created input
445 reload pseudo. Use TITLE to describe new registers for debug
446 purposes. */
447 static bool
448 get_reload_reg (enum op_type type, enum machine_mode mode, rtx original,
449 enum reg_class rclass, const char *title, rtx *result_reg)
451 int i, regno;
452 enum reg_class new_class;
454 if (type == OP_OUT)
456 *result_reg
457 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
458 return true;
460 /* Prevent reuse value of expression with side effects,
461 e.g. volatile memory. */
462 if (! side_effects_p (original))
463 for (i = 0; i < curr_insn_input_reloads_num; i++)
464 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
465 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
467 rtx reg = curr_insn_input_reloads[i].reg;
468 regno = REGNO (reg);
469 /* If input is equal to original and both are VOIDmode,
470 GET_MODE (reg) might be still different from mode.
471 Ensure we don't return *result_reg with wrong mode. */
472 if (GET_MODE (reg) != mode)
474 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
475 continue;
476 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
477 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
478 continue;
480 *result_reg = reg;
481 if (lra_dump_file != NULL)
483 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
484 dump_value_slim (lra_dump_file, original, 1);
486 if (new_class != lra_get_allocno_class (regno))
487 lra_change_class (regno, new_class, ", change to", false);
488 if (lra_dump_file != NULL)
489 fprintf (lra_dump_file, "\n");
490 return false;
492 *result_reg = lra_create_new_reg (mode, original, rclass, title);
493 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
494 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
495 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
496 return true;
501 /* The page contains code to extract memory address parts. */
503 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
504 static inline bool
505 ok_for_index_p_nonstrict (rtx reg)
507 unsigned regno = REGNO (reg);
509 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
512 /* A version of regno_ok_for_base_p for use here, when all pseudos
513 should count as OK. Arguments as for regno_ok_for_base_p. */
514 static inline bool
515 ok_for_base_p_nonstrict (rtx reg, enum machine_mode mode, addr_space_t as,
516 enum rtx_code outer_code, enum rtx_code index_code)
518 unsigned regno = REGNO (reg);
520 if (regno >= FIRST_PSEUDO_REGISTER)
521 return true;
522 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
527 /* The page contains major code to choose the current insn alternative
528 and generate reloads for it. */
530 /* Return the offset from REGNO of the least significant register
531 in (reg:MODE REGNO).
533 This function is used to tell whether two registers satisfy
534 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
536 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
537 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
539 lra_constraint_offset (int regno, enum machine_mode mode)
541 lra_assert (regno < FIRST_PSEUDO_REGISTER);
542 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
543 && SCALAR_INT_MODE_P (mode))
544 return hard_regno_nregs[regno][mode] - 1;
545 return 0;
548 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
549 if they are the same hard reg, and has special hacks for
550 auto-increment and auto-decrement. This is specifically intended for
551 process_alt_operands to use in determining whether two operands
552 match. X is the operand whose number is the lower of the two.
554 It is supposed that X is the output operand and Y is the input
555 operand. Y_HARD_REGNO is the final hard regno of register Y or
556 register in subreg Y as we know it now. Otherwise, it is a
557 negative value. */
558 static bool
559 operands_match_p (rtx x, rtx y, int y_hard_regno)
561 int i;
562 RTX_CODE code = GET_CODE (x);
563 const char *fmt;
565 if (x == y)
566 return true;
567 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
568 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
570 int j;
572 i = get_hard_regno (x);
573 if (i < 0)
574 goto slow;
576 if ((j = y_hard_regno) < 0)
577 goto slow;
579 i += lra_constraint_offset (i, GET_MODE (x));
580 j += lra_constraint_offset (j, GET_MODE (y));
582 return i == j;
585 /* If two operands must match, because they are really a single
586 operand of an assembler insn, then two post-increments are invalid
587 because the assembler insn would increment only once. On the
588 other hand, a post-increment matches ordinary indexing if the
589 post-increment is the output operand. */
590 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
591 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
593 /* Two pre-increments are invalid because the assembler insn would
594 increment only once. On the other hand, a pre-increment matches
595 ordinary indexing if the pre-increment is the input operand. */
596 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
597 || GET_CODE (y) == PRE_MODIFY)
598 return operands_match_p (x, XEXP (y, 0), -1);
600 slow:
602 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
603 && x == SUBREG_REG (y))
604 return true;
605 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
606 && SUBREG_REG (x) == y)
607 return true;
609 /* Now we have disposed of all the cases in which different rtx
610 codes can match. */
611 if (code != GET_CODE (y))
612 return false;
614 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
615 if (GET_MODE (x) != GET_MODE (y))
616 return false;
618 switch (code)
620 CASE_CONST_UNIQUE:
621 return false;
623 case LABEL_REF:
624 return XEXP (x, 0) == XEXP (y, 0);
625 case SYMBOL_REF:
626 return XSTR (x, 0) == XSTR (y, 0);
628 default:
629 break;
632 /* Compare the elements. If any pair of corresponding elements fail
633 to match, return false for the whole things. */
635 fmt = GET_RTX_FORMAT (code);
636 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
638 int val, j;
639 switch (fmt[i])
641 case 'w':
642 if (XWINT (x, i) != XWINT (y, i))
643 return false;
644 break;
646 case 'i':
647 if (XINT (x, i) != XINT (y, i))
648 return false;
649 break;
651 case 'e':
652 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
653 if (val == 0)
654 return false;
655 break;
657 case '0':
658 break;
660 case 'E':
661 if (XVECLEN (x, i) != XVECLEN (y, i))
662 return false;
663 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
665 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
666 if (val == 0)
667 return false;
669 break;
671 /* It is believed that rtx's at this level will never
672 contain anything but integers and other rtx's, except for
673 within LABEL_REFs and SYMBOL_REFs. */
674 default:
675 gcc_unreachable ();
678 return true;
681 /* True if X is a constant that can be forced into the constant pool.
682 MODE is the mode of the operand, or VOIDmode if not known. */
683 #define CONST_POOL_OK_P(MODE, X) \
684 ((MODE) != VOIDmode \
685 && CONSTANT_P (X) \
686 && GET_CODE (X) != HIGH \
687 && !targetm.cannot_force_const_mem (MODE, X))
689 /* True if C is a non-empty register class that has too few registers
690 to be safely used as a reload target class. */
691 #define SMALL_REGISTER_CLASS_P(C) \
692 (ira_class_hard_regs_num [(C)] == 1 \
693 || (ira_class_hard_regs_num [(C)] >= 1 \
694 && targetm.class_likely_spilled_p (C)))
696 /* If REG is a reload pseudo, try to make its class satisfying CL. */
697 static void
698 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
700 enum reg_class rclass;
702 /* Do not make more accurate class from reloads generated. They are
703 mostly moves with a lot of constraints. Making more accurate
704 class may results in very narrow class and impossibility of find
705 registers for several reloads of one insn. */
706 if (INSN_UID (curr_insn) >= new_insn_uid_start)
707 return;
708 if (GET_CODE (reg) == SUBREG)
709 reg = SUBREG_REG (reg);
710 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
711 return;
712 if (in_class_p (reg, cl, &rclass) && rclass != cl)
713 lra_change_class (REGNO (reg), rclass, " Change to", true);
716 /* Generate reloads for matching OUT and INS (array of input operand
717 numbers with end marker -1) with reg class GOAL_CLASS. Add input
718 and output reloads correspondingly to the lists *BEFORE and *AFTER.
719 OUT might be negative. In this case we generate input reloads for
720 matched input operands INS. */
721 static void
722 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
723 rtx *before, rtx *after)
725 int i, in;
726 rtx new_in_reg, new_out_reg, reg, clobber;
727 enum machine_mode inmode, outmode;
728 rtx in_rtx = *curr_id->operand_loc[ins[0]];
729 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
731 inmode = curr_operand_mode[ins[0]];
732 outmode = out < 0 ? inmode : curr_operand_mode[out];
733 push_to_sequence (*before);
734 if (inmode != outmode)
736 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
738 reg = new_in_reg
739 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
740 goal_class, "");
741 if (SCALAR_INT_MODE_P (inmode))
742 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
743 else
744 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
745 LRA_SUBREG_P (new_out_reg) = 1;
746 /* If the input reg is dying here, we can use the same hard
747 register for REG and IN_RTX. We do it only for original
748 pseudos as reload pseudos can die although original
749 pseudos still live where reload pseudos dies. */
750 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
751 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
752 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
754 else
756 reg = new_out_reg
757 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
758 goal_class, "");
759 if (SCALAR_INT_MODE_P (outmode))
760 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
761 else
762 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
763 /* NEW_IN_REG is non-paradoxical subreg. We don't want
764 NEW_OUT_REG living above. We add clobber clause for
765 this. This is just a temporary clobber. We can remove
766 it at the end of LRA work. */
767 clobber = emit_clobber (new_out_reg);
768 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
769 LRA_SUBREG_P (new_in_reg) = 1;
770 if (GET_CODE (in_rtx) == SUBREG)
772 rtx subreg_reg = SUBREG_REG (in_rtx);
774 /* If SUBREG_REG is dying here and sub-registers IN_RTX
775 and NEW_IN_REG are similar, we can use the same hard
776 register for REG and SUBREG_REG. */
777 if (REG_P (subreg_reg)
778 && (int) REGNO (subreg_reg) < lra_new_regno_start
779 && GET_MODE (subreg_reg) == outmode
780 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
781 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
782 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
786 else
788 /* Pseudos have values -- see comments for lra_reg_info.
789 Different pseudos with the same value do not conflict even if
790 they live in the same place. When we create a pseudo we
791 assign value of original pseudo (if any) from which we
792 created the new pseudo. If we create the pseudo from the
793 input pseudo, the new pseudo will no conflict with the input
794 pseudo which is wrong when the input pseudo lives after the
795 insn and as the new pseudo value is changed by the insn
796 output. Therefore we create the new pseudo from the output.
798 We cannot reuse the current output register because we might
799 have a situation like "a <- a op b", where the constraints
800 force the second input operand ("b") to match the output
801 operand ("a"). "b" must then be copied into a new register
802 so that it doesn't clobber the current value of "a". */
804 new_in_reg = new_out_reg
805 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
806 goal_class, "");
808 /* In operand can be got from transformations before processing insn
809 constraints. One example of such transformations is subreg
810 reloading (see function simplify_operand_subreg). The new
811 pseudos created by the transformations might have inaccurate
812 class (ALL_REGS) and we should make their classes more
813 accurate. */
814 narrow_reload_pseudo_class (in_rtx, goal_class);
815 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
816 *before = get_insns ();
817 end_sequence ();
818 for (i = 0; (in = ins[i]) >= 0; i++)
820 lra_assert
821 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
822 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
823 *curr_id->operand_loc[in] = new_in_reg;
825 lra_update_dups (curr_id, ins);
826 if (out < 0)
827 return;
828 /* See a comment for the input operand above. */
829 narrow_reload_pseudo_class (out_rtx, goal_class);
830 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
832 start_sequence ();
833 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
834 emit_insn (*after);
835 *after = get_insns ();
836 end_sequence ();
838 *curr_id->operand_loc[out] = new_out_reg;
839 lra_update_dup (curr_id, out);
842 /* Return register class which is union of all reg classes in insn
843 constraint alternative string starting with P. */
844 static enum reg_class
845 reg_class_from_constraints (const char *p)
847 int c, len;
848 enum reg_class op_class = NO_REGS;
851 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
853 case '#':
854 case ',':
855 return op_class;
857 case 'p':
858 op_class = (reg_class_subunion
859 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
860 ADDRESS, SCRATCH)]);
861 break;
863 case 'g':
864 case 'r':
865 op_class = reg_class_subunion[op_class][GENERAL_REGS];
866 break;
868 default:
869 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
871 #ifdef EXTRA_CONSTRAINT_STR
872 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
873 op_class
874 = (reg_class_subunion
875 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
876 ADDRESS, SCRATCH)]);
877 #endif
878 break;
881 op_class
882 = reg_class_subunion[op_class][REG_CLASS_FROM_CONSTRAINT (c, p)];
883 break;
885 while ((p += len), c);
886 return op_class;
889 /* If OP is a register, return the class of the register as per
890 get_reg_class, otherwise return NO_REGS. */
891 static inline enum reg_class
892 get_op_class (rtx op)
894 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
897 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
898 otherwise. If modes of MEM_PSEUDO and VAL are different, use
899 SUBREG for VAL to make them equal. */
900 static rtx
901 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
903 if (GET_MODE (mem_pseudo) != GET_MODE (val))
905 /* Usually size of mem_pseudo is greater than val size but in
906 rare cases it can be less as it can be defined by target
907 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
908 if (! MEM_P (val))
910 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
911 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
913 LRA_SUBREG_P (val) = 1;
915 else
917 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
918 LRA_SUBREG_P (mem_pseudo) = 1;
921 return (to_p
922 ? gen_move_insn (mem_pseudo, val)
923 : gen_move_insn (val, mem_pseudo));
926 /* Process a special case insn (register move), return true if we
927 don't need to process it anymore. INSN should be a single set
928 insn. Set up that RTL was changed through CHANGE_P and macro
929 SECONDARY_MEMORY_NEEDED says to use secondary memory through
930 SEC_MEM_P. */
931 static bool
932 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
934 int sregno, dregno;
935 rtx dest, src, dreg, sreg, old_sreg, new_reg, before, scratch_reg;
936 enum reg_class dclass, sclass, secondary_class;
937 enum machine_mode sreg_mode;
938 secondary_reload_info sri;
940 lra_assert (curr_insn_set != NULL_RTX);
941 dreg = dest = SET_DEST (curr_insn_set);
942 sreg = src = SET_SRC (curr_insn_set);
943 if (GET_CODE (dest) == SUBREG)
944 dreg = SUBREG_REG (dest);
945 if (GET_CODE (src) == SUBREG)
946 sreg = SUBREG_REG (src);
947 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
948 return false;
949 sclass = dclass = NO_REGS;
950 if (REG_P (dreg))
951 dclass = get_reg_class (REGNO (dreg));
952 if (dclass == ALL_REGS)
953 /* ALL_REGS is used for new pseudos created by transformations
954 like reload of SUBREG_REG (see function
955 simplify_operand_subreg). We don't know their class yet. We
956 should figure out the class from processing the insn
957 constraints not in this fast path function. Even if ALL_REGS
958 were a right class for the pseudo, secondary_... hooks usually
959 are not define for ALL_REGS. */
960 return false;
961 sreg_mode = GET_MODE (sreg);
962 old_sreg = sreg;
963 if (REG_P (sreg))
964 sclass = get_reg_class (REGNO (sreg));
965 if (sclass == ALL_REGS)
966 /* See comments above. */
967 return false;
968 if (sclass == NO_REGS && dclass == NO_REGS)
969 return false;
970 #ifdef SECONDARY_MEMORY_NEEDED
971 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
972 #ifdef SECONDARY_MEMORY_NEEDED_MODE
973 && ((sclass != NO_REGS && dclass != NO_REGS)
974 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
975 #endif
978 *sec_mem_p = true;
979 return false;
981 #endif
982 if (! REG_P (dreg) || ! REG_P (sreg))
983 return false;
984 sri.prev_sri = NULL;
985 sri.icode = CODE_FOR_nothing;
986 sri.extra_cost = 0;
987 secondary_class = NO_REGS;
988 /* Set up hard register for a reload pseudo for hook
989 secondary_reload because some targets just ignore unassigned
990 pseudos in the hook. */
991 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
993 dregno = REGNO (dreg);
994 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
996 else
997 dregno = -1;
998 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1000 sregno = REGNO (sreg);
1001 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1003 else
1004 sregno = -1;
1005 if (sclass != NO_REGS)
1006 secondary_class
1007 = (enum reg_class) targetm.secondary_reload (false, dest,
1008 (reg_class_t) sclass,
1009 GET_MODE (src), &sri);
1010 if (sclass == NO_REGS
1011 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1012 && dclass != NO_REGS))
1014 enum reg_class old_sclass = secondary_class;
1015 secondary_reload_info old_sri = sri;
1017 sri.prev_sri = NULL;
1018 sri.icode = CODE_FOR_nothing;
1019 sri.extra_cost = 0;
1020 secondary_class
1021 = (enum reg_class) targetm.secondary_reload (true, sreg,
1022 (reg_class_t) dclass,
1023 sreg_mode, &sri);
1024 /* Check the target hook consistency. */
1025 lra_assert
1026 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1027 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1028 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1030 if (sregno >= 0)
1031 reg_renumber [sregno] = -1;
1032 if (dregno >= 0)
1033 reg_renumber [dregno] = -1;
1034 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1035 return false;
1036 *change_p = true;
1037 new_reg = NULL_RTX;
1038 if (secondary_class != NO_REGS)
1039 new_reg = lra_create_new_reg_with_unique_value (sreg_mode, NULL_RTX,
1040 secondary_class,
1041 "secondary");
1042 start_sequence ();
1043 if (old_sreg != sreg)
1044 sreg = copy_rtx (sreg);
1045 if (sri.icode == CODE_FOR_nothing)
1046 lra_emit_move (new_reg, sreg);
1047 else
1049 enum reg_class scratch_class;
1051 scratch_class = (reg_class_from_constraints
1052 (insn_data[sri.icode].operand[2].constraint));
1053 scratch_reg = (lra_create_new_reg_with_unique_value
1054 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1055 scratch_class, "scratch"));
1056 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1057 sreg, scratch_reg));
1059 before = get_insns ();
1060 end_sequence ();
1061 lra_process_new_insns (curr_insn, before, NULL_RTX, "Inserting the move");
1062 if (new_reg != NULL_RTX)
1064 if (GET_CODE (src) == SUBREG)
1065 SUBREG_REG (src) = new_reg;
1066 else
1067 SET_SRC (curr_insn_set) = new_reg;
1069 else
1071 if (lra_dump_file != NULL)
1073 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1074 dump_insn_slim (lra_dump_file, curr_insn);
1076 lra_set_insn_deleted (curr_insn);
1077 return true;
1079 return false;
1082 /* The following data describe the result of process_alt_operands.
1083 The data are used in curr_insn_transform to generate reloads. */
1085 /* The chosen reg classes which should be used for the corresponding
1086 operands. */
1087 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1088 /* True if the operand should be the same as another operand and that
1089 other operand does not need a reload. */
1090 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1091 /* True if the operand does not need a reload. */
1092 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1093 /* True if the operand can be offsetable memory. */
1094 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1095 /* The number of an operand to which given operand can be matched to. */
1096 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1097 /* The number of elements in the following array. */
1098 static int goal_alt_dont_inherit_ops_num;
1099 /* Numbers of operands whose reload pseudos should not be inherited. */
1100 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1101 /* True if the insn commutative operands should be swapped. */
1102 static bool goal_alt_swapped;
1103 /* The chosen insn alternative. */
1104 static int goal_alt_number;
1106 /* The following five variables are used to choose the best insn
1107 alternative. They reflect final characteristics of the best
1108 alternative. */
1110 /* Number of necessary reloads and overall cost reflecting the
1111 previous value and other unpleasantness of the best alternative. */
1112 static int best_losers, best_overall;
1113 /* Overall number hard registers used for reloads. For example, on
1114 some targets we need 2 general registers to reload DFmode and only
1115 one floating point register. */
1116 static int best_reload_nregs;
1117 /* Overall number reflecting distances of previous reloading the same
1118 value. The distances are counted from the current BB start. It is
1119 used to improve inheritance chances. */
1120 static int best_reload_sum;
1122 /* True if the current insn should have no correspondingly input or
1123 output reloads. */
1124 static bool no_input_reloads_p, no_output_reloads_p;
1126 /* True if we swapped the commutative operands in the current
1127 insn. */
1128 static int curr_swapped;
1130 /* Arrange for address element *LOC to be a register of class CL.
1131 Add any input reloads to list BEFORE. AFTER is nonnull if *LOC is an
1132 automodified value; handle that case by adding the required output
1133 reloads to list AFTER. Return true if the RTL was changed. */
1134 static bool
1135 process_addr_reg (rtx *loc, rtx *before, rtx *after, enum reg_class cl)
1137 int regno;
1138 enum reg_class rclass, new_class;
1139 rtx reg;
1140 rtx new_reg;
1141 enum machine_mode mode;
1142 bool before_p = false;
1144 loc = strip_subreg (loc);
1145 reg = *loc;
1146 mode = GET_MODE (reg);
1147 if (! REG_P (reg))
1149 /* Always reload memory in an address even if the target supports
1150 such addresses. */
1151 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1152 before_p = true;
1154 else
1156 regno = REGNO (reg);
1157 rclass = get_reg_class (regno);
1158 if ((*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1160 if (lra_dump_file != NULL)
1162 fprintf (lra_dump_file,
1163 "Changing pseudo %d in address of insn %u on equiv ",
1164 REGNO (reg), INSN_UID (curr_insn));
1165 dump_value_slim (lra_dump_file, *loc, 1);
1166 fprintf (lra_dump_file, "\n");
1168 *loc = copy_rtx (*loc);
1170 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1172 reg = *loc;
1173 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1174 mode, reg, cl, "address", &new_reg))
1175 before_p = true;
1177 else if (new_class != NO_REGS && rclass != new_class)
1179 lra_change_class (regno, new_class, " Change to", true);
1180 return false;
1182 else
1183 return false;
1185 if (before_p)
1187 push_to_sequence (*before);
1188 lra_emit_move (new_reg, reg);
1189 *before = get_insns ();
1190 end_sequence ();
1192 *loc = new_reg;
1193 if (after != NULL)
1195 start_sequence ();
1196 lra_emit_move (reg, new_reg);
1197 emit_insn (*after);
1198 *after = get_insns ();
1199 end_sequence ();
1201 return true;
1204 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1205 the insn to be inserted before curr insn. AFTER returns the
1206 the insn to be inserted after curr insn. ORIGREG and NEWREG
1207 are the original reg and new reg for reload. */
1208 static void
1209 insert_move_for_subreg (rtx *before, rtx *after, rtx origreg, rtx newreg)
1211 if (before)
1213 push_to_sequence (*before);
1214 lra_emit_move (newreg, origreg);
1215 *before = get_insns ();
1216 end_sequence ();
1218 if (after)
1220 start_sequence ();
1221 lra_emit_move (origreg, newreg);
1222 emit_insn (*after);
1223 *after = get_insns ();
1224 end_sequence ();
1228 /* Make reloads for subreg in operand NOP with internal subreg mode
1229 REG_MODE, add new reloads for further processing. Return true if
1230 any reload was generated. */
1231 static bool
1232 simplify_operand_subreg (int nop, enum machine_mode reg_mode)
1234 int hard_regno;
1235 rtx before, after;
1236 enum machine_mode mode;
1237 rtx reg, new_reg;
1238 rtx operand = *curr_id->operand_loc[nop];
1239 enum reg_class regclass;
1240 enum op_type type;
1242 before = after = NULL_RTX;
1244 if (GET_CODE (operand) != SUBREG)
1245 return false;
1247 mode = GET_MODE (operand);
1248 reg = SUBREG_REG (operand);
1249 type = curr_static_id->operand[nop].type;
1250 /* If we change address for paradoxical subreg of memory, the
1251 address might violate the necessary alignment or the access might
1252 be slow. So take this into consideration. We should not worry
1253 about access beyond allocated memory for paradoxical memory
1254 subregs as we don't substitute such equiv memory (see processing
1255 equivalences in function lra_constraints) and because for spilled
1256 pseudos we allocate stack memory enough for the biggest
1257 corresponding paradoxical subreg. */
1258 if ((MEM_P (reg)
1259 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1260 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1261 || (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER))
1263 alter_subreg (curr_id->operand_loc[nop], false);
1264 return true;
1266 /* Put constant into memory when we have mixed modes. It generates
1267 a better code in most cases as it does not need a secondary
1268 reload memory. It also prevents LRA looping when LRA is using
1269 secondary reload memory again and again. */
1270 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1271 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1273 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1274 alter_subreg (curr_id->operand_loc[nop], false);
1275 return true;
1277 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1278 if there may be a problem accessing OPERAND in the outer
1279 mode. */
1280 if ((REG_P (reg)
1281 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1282 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1283 /* Don't reload paradoxical subregs because we could be looping
1284 having repeatedly final regno out of hard regs range. */
1285 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1286 >= hard_regno_nregs[hard_regno][mode])
1287 && simplify_subreg_regno (hard_regno, GET_MODE (reg),
1288 SUBREG_BYTE (operand), mode) < 0
1289 /* Don't reload subreg for matching reload. It is actually
1290 valid subreg in LRA. */
1291 && ! LRA_SUBREG_P (operand))
1292 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1294 enum reg_class rclass;
1296 if (REG_P (reg))
1297 /* There is a big probability that we will get the same class
1298 for the new pseudo and we will get the same insn which
1299 means infinite looping. So spill the new pseudo. */
1300 rclass = NO_REGS;
1301 else
1302 /* The class will be defined later in curr_insn_transform. */
1303 rclass
1304 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1306 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1307 rclass, "subreg reg", &new_reg))
1309 bool insert_before, insert_after;
1310 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1312 insert_before = (type != OP_OUT
1313 || GET_MODE_SIZE (GET_MODE (reg)) > GET_MODE_SIZE (mode));
1314 insert_after = (type != OP_IN);
1315 insert_move_for_subreg (insert_before ? &before : NULL,
1316 insert_after ? &after : NULL,
1317 reg, new_reg);
1319 SUBREG_REG (operand) = new_reg;
1320 lra_process_new_insns (curr_insn, before, after,
1321 "Inserting subreg reload");
1322 return true;
1324 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1325 IRA allocates hardreg to the inner pseudo reg according to its mode
1326 instead of the outermode, so the size of the hardreg may not be enough
1327 to contain the outermode operand, in that case we may need to insert
1328 reload for the reg. For the following two types of paradoxical subreg,
1329 we need to insert reload:
1330 1. If the op_type is OP_IN, and the hardreg could not be paired with
1331 other hardreg to contain the outermode operand
1332 (checked by in_hard_reg_set_p), we need to insert the reload.
1333 2. If the op_type is OP_OUT or OP_INOUT.
1335 Here is a paradoxical subreg example showing how the reload is generated:
1337 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1338 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1340 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1341 here, if reg107 is assigned to hardreg R15, because R15 is the last
1342 hardreg, compiler cannot find another hardreg to pair with R15 to
1343 contain TImode data. So we insert a TImode reload reg180 for it.
1344 After reload is inserted:
1346 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1347 (reg:DI 107 [ __comp ])) -1
1348 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1349 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1351 Two reload hard registers will be allocated to reg180 to save TImode data
1352 in LRA_assign. */
1353 else if (REG_P (reg)
1354 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1355 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1356 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1357 < hard_regno_nregs[hard_regno][mode])
1358 && (regclass = lra_get_allocno_class (REGNO (reg)))
1359 && (type != OP_IN
1360 || !in_hard_reg_set_p (reg_class_contents[regclass],
1361 mode, hard_regno)))
1363 /* The class will be defined later in curr_insn_transform. */
1364 enum reg_class rclass
1365 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1367 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1368 rclass, "paradoxical subreg", &new_reg))
1370 rtx subreg;
1371 bool insert_before, insert_after;
1373 PUT_MODE (new_reg, mode);
1374 subreg = simplify_gen_subreg (GET_MODE (reg), new_reg, mode, 0);
1375 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1377 insert_before = (type != OP_OUT);
1378 insert_after = (type != OP_IN);
1379 insert_move_for_subreg (insert_before ? &before : NULL,
1380 insert_after ? &after : NULL,
1381 reg, subreg);
1383 SUBREG_REG (operand) = new_reg;
1384 lra_process_new_insns (curr_insn, before, after,
1385 "Inserting paradoxical subreg reload");
1386 return true;
1388 return false;
1391 /* Return TRUE if X refers for a hard register from SET. */
1392 static bool
1393 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1395 int i, j, x_hard_regno;
1396 enum machine_mode mode;
1397 const char *fmt;
1398 enum rtx_code code;
1400 if (x == NULL_RTX)
1401 return false;
1402 code = GET_CODE (x);
1403 mode = GET_MODE (x);
1404 if (code == SUBREG)
1406 x = SUBREG_REG (x);
1407 code = GET_CODE (x);
1408 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1409 mode = GET_MODE (x);
1412 if (REG_P (x))
1414 x_hard_regno = get_hard_regno (x);
1415 return (x_hard_regno >= 0
1416 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1418 if (MEM_P (x))
1420 struct address_info ad;
1422 decompose_mem_address (&ad, x);
1423 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1424 return true;
1425 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1426 return true;
1428 fmt = GET_RTX_FORMAT (code);
1429 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1431 if (fmt[i] == 'e')
1433 if (uses_hard_regs_p (XEXP (x, i), set))
1434 return true;
1436 else if (fmt[i] == 'E')
1438 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1439 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1440 return true;
1443 return false;
1446 /* Return true if OP is a spilled pseudo. */
1447 static inline bool
1448 spilled_pseudo_p (rtx op)
1450 return (REG_P (op)
1451 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1454 /* Return true if X is a general constant. */
1455 static inline bool
1456 general_constant_p (rtx x)
1458 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1461 static bool
1462 reg_in_class_p (rtx reg, enum reg_class cl)
1464 if (cl == NO_REGS)
1465 return get_reg_class (REGNO (reg)) == NO_REGS;
1466 return in_class_p (reg, cl, NULL);
1469 /* Major function to choose the current insn alternative and what
1470 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1471 negative we should consider only this alternative. Return false if
1472 we can not choose the alternative or find how to reload the
1473 operands. */
1474 static bool
1475 process_alt_operands (int only_alternative)
1477 bool ok_p = false;
1478 int nop, overall, nalt;
1479 int n_alternatives = curr_static_id->n_alternatives;
1480 int n_operands = curr_static_id->n_operands;
1481 /* LOSERS counts the operands that don't fit this alternative and
1482 would require loading. */
1483 int losers;
1484 /* REJECT is a count of how undesirable this alternative says it is
1485 if any reloading is required. If the alternative matches exactly
1486 then REJECT is ignored, but otherwise it gets this much counted
1487 against it in addition to the reloading needed. */
1488 int reject;
1489 /* The number of elements in the following array. */
1490 int early_clobbered_regs_num;
1491 /* Numbers of operands which are early clobber registers. */
1492 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1493 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1494 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1495 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1496 bool curr_alt_win[MAX_RECOG_OPERANDS];
1497 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1498 int curr_alt_matches[MAX_RECOG_OPERANDS];
1499 /* The number of elements in the following array. */
1500 int curr_alt_dont_inherit_ops_num;
1501 /* Numbers of operands whose reload pseudos should not be inherited. */
1502 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1503 rtx op;
1504 /* The register when the operand is a subreg of register, otherwise the
1505 operand itself. */
1506 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1507 /* The register if the operand is a register or subreg of register,
1508 otherwise NULL. */
1509 rtx operand_reg[MAX_RECOG_OPERANDS];
1510 int hard_regno[MAX_RECOG_OPERANDS];
1511 enum machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1512 int reload_nregs, reload_sum;
1513 bool costly_p;
1514 enum reg_class cl;
1516 /* Calculate some data common for all alternatives to speed up the
1517 function. */
1518 for (nop = 0; nop < n_operands; nop++)
1520 rtx reg;
1522 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1523 /* The real hard regno of the operand after the allocation. */
1524 hard_regno[nop] = get_hard_regno (op);
1526 operand_reg[nop] = reg = op;
1527 biggest_mode[nop] = GET_MODE (op);
1528 if (GET_CODE (op) == SUBREG)
1530 operand_reg[nop] = reg = SUBREG_REG (op);
1531 if (GET_MODE_SIZE (biggest_mode[nop])
1532 < GET_MODE_SIZE (GET_MODE (reg)))
1533 biggest_mode[nop] = GET_MODE (reg);
1535 if (! REG_P (reg))
1536 operand_reg[nop] = NULL_RTX;
1537 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1538 || ((int) REGNO (reg)
1539 == lra_get_elimination_hard_regno (REGNO (reg))))
1540 no_subreg_reg_operand[nop] = reg;
1541 else
1542 operand_reg[nop] = no_subreg_reg_operand[nop]
1543 /* Just use natural mode for elimination result. It should
1544 be enough for extra constraints hooks. */
1545 = regno_reg_rtx[hard_regno[nop]];
1548 /* The constraints are made of several alternatives. Each operand's
1549 constraint looks like foo,bar,... with commas separating the
1550 alternatives. The first alternatives for all operands go
1551 together, the second alternatives go together, etc.
1553 First loop over alternatives. */
1554 for (nalt = 0; nalt < n_alternatives; nalt++)
1556 /* Loop over operands for one constraint alternative. */
1557 #if HAVE_ATTR_enabled
1558 if (curr_id->alternative_enabled_p != NULL
1559 && ! curr_id->alternative_enabled_p[nalt])
1560 continue;
1561 #endif
1563 if (only_alternative >= 0 && nalt != only_alternative)
1564 continue;
1567 overall = losers = reject = reload_nregs = reload_sum = 0;
1568 for (nop = 0; nop < n_operands; nop++)
1570 int inc = (curr_static_id
1571 ->operand_alternative[nalt * n_operands + nop].reject);
1572 if (lra_dump_file != NULL && inc != 0)
1573 fprintf (lra_dump_file,
1574 " Staticly defined alt reject+=%d\n", inc);
1575 reject += inc;
1577 early_clobbered_regs_num = 0;
1579 for (nop = 0; nop < n_operands; nop++)
1581 const char *p;
1582 char *end;
1583 int len, c, m, i, opalt_num, this_alternative_matches;
1584 bool win, did_match, offmemok, early_clobber_p;
1585 /* false => this operand can be reloaded somehow for this
1586 alternative. */
1587 bool badop;
1588 /* true => this operand can be reloaded if the alternative
1589 allows regs. */
1590 bool winreg;
1591 /* True if a constant forced into memory would be OK for
1592 this operand. */
1593 bool constmemok;
1594 enum reg_class this_alternative, this_costly_alternative;
1595 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1596 bool this_alternative_match_win, this_alternative_win;
1597 bool this_alternative_offmemok;
1598 bool scratch_p;
1599 enum machine_mode mode;
1601 opalt_num = nalt * n_operands + nop;
1602 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1604 /* Fast track for no constraints at all. */
1605 curr_alt[nop] = NO_REGS;
1606 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1607 curr_alt_win[nop] = true;
1608 curr_alt_match_win[nop] = false;
1609 curr_alt_offmemok[nop] = false;
1610 curr_alt_matches[nop] = -1;
1611 continue;
1614 op = no_subreg_reg_operand[nop];
1615 mode = curr_operand_mode[nop];
1617 win = did_match = winreg = offmemok = constmemok = false;
1618 badop = true;
1620 early_clobber_p = false;
1621 p = curr_static_id->operand_alternative[opalt_num].constraint;
1623 this_costly_alternative = this_alternative = NO_REGS;
1624 /* We update set of possible hard regs besides its class
1625 because reg class might be inaccurate. For example,
1626 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1627 is translated in HI_REGS because classes are merged by
1628 pairs and there is no accurate intermediate class. */
1629 CLEAR_HARD_REG_SET (this_alternative_set);
1630 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1631 this_alternative_win = false;
1632 this_alternative_match_win = false;
1633 this_alternative_offmemok = false;
1634 this_alternative_matches = -1;
1636 /* An empty constraint should be excluded by the fast
1637 track. */
1638 lra_assert (*p != 0 && *p != ',');
1640 /* Scan this alternative's specs for this operand; set WIN
1641 if the operand fits any letter in this alternative.
1642 Otherwise, clear BADOP if this operand could fit some
1643 letter after reloads, or set WINREG if this operand could
1644 fit after reloads provided the constraint allows some
1645 registers. */
1646 costly_p = false;
1649 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1651 case '\0':
1652 len = 0;
1653 break;
1654 case ',':
1655 c = '\0';
1656 break;
1658 case '=': case '+': case '?': case '*': case '!':
1659 case ' ': case '\t':
1660 break;
1662 case '%':
1663 /* We only support one commutative marker, the first
1664 one. We already set commutative above. */
1665 break;
1667 case '&':
1668 early_clobber_p = true;
1669 break;
1671 case '#':
1672 /* Ignore rest of this alternative. */
1673 c = '\0';
1674 break;
1676 case '0': case '1': case '2': case '3': case '4':
1677 case '5': case '6': case '7': case '8': case '9':
1679 int m_hregno;
1680 bool match_p;
1682 m = strtoul (p, &end, 10);
1683 p = end;
1684 len = 0;
1685 lra_assert (nop > m);
1687 this_alternative_matches = m;
1688 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1689 /* We are supposed to match a previous operand.
1690 If we do, we win if that one did. If we do
1691 not, count both of the operands as losers.
1692 (This is too conservative, since most of the
1693 time only a single reload insn will be needed
1694 to make the two operands win. As a result,
1695 this alternative may be rejected when it is
1696 actually desirable.) */
1697 match_p = false;
1698 if (operands_match_p (*curr_id->operand_loc[nop],
1699 *curr_id->operand_loc[m], m_hregno))
1701 /* We should reject matching of an early
1702 clobber operand if the matching operand is
1703 not dying in the insn. */
1704 if (! curr_static_id->operand[m].early_clobber
1705 || operand_reg[nop] == NULL_RTX
1706 || (find_regno_note (curr_insn, REG_DEAD,
1707 REGNO (op))
1708 || REGNO (op) == REGNO (operand_reg[m])))
1709 match_p = true;
1711 if (match_p)
1713 /* If we are matching a non-offsettable
1714 address where an offsettable address was
1715 expected, then we must reject this
1716 combination, because we can't reload
1717 it. */
1718 if (curr_alt_offmemok[m]
1719 && MEM_P (*curr_id->operand_loc[m])
1720 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1721 continue;
1723 else
1725 /* Operands don't match. Both operands must
1726 allow a reload register, otherwise we
1727 cannot make them match. */
1728 if (curr_alt[m] == NO_REGS)
1729 break;
1730 /* Retroactively mark the operand we had to
1731 match as a loser, if it wasn't already and
1732 it wasn't matched to a register constraint
1733 (e.g it might be matched by memory). */
1734 if (curr_alt_win[m]
1735 && (operand_reg[m] == NULL_RTX
1736 || hard_regno[m] < 0))
1738 losers++;
1739 reload_nregs
1740 += (ira_reg_class_max_nregs[curr_alt[m]]
1741 [GET_MODE (*curr_id->operand_loc[m])]);
1744 /* We prefer no matching alternatives because
1745 it gives more freedom in RA. */
1746 if (operand_reg[nop] == NULL_RTX
1747 || (find_regno_note (curr_insn, REG_DEAD,
1748 REGNO (operand_reg[nop]))
1749 == NULL_RTX))
1751 if (lra_dump_file != NULL)
1752 fprintf
1753 (lra_dump_file,
1754 " %d Matching alt: reject+=2\n",
1755 nop);
1756 reject += 2;
1759 /* If we have to reload this operand and some
1760 previous operand also had to match the same
1761 thing as this operand, we don't know how to do
1762 that. */
1763 if (!match_p || !curr_alt_win[m])
1765 for (i = 0; i < nop; i++)
1766 if (curr_alt_matches[i] == m)
1767 break;
1768 if (i < nop)
1769 break;
1771 else
1772 did_match = true;
1774 /* This can be fixed with reloads if the operand
1775 we are supposed to match can be fixed with
1776 reloads. */
1777 badop = false;
1778 this_alternative = curr_alt[m];
1779 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1780 winreg = this_alternative != NO_REGS;
1781 break;
1784 case 'p':
1785 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1786 ADDRESS, SCRATCH);
1787 this_alternative = reg_class_subunion[this_alternative][cl];
1788 IOR_HARD_REG_SET (this_alternative_set,
1789 reg_class_contents[cl]);
1790 if (costly_p)
1792 this_costly_alternative
1793 = reg_class_subunion[this_costly_alternative][cl];
1794 IOR_HARD_REG_SET (this_costly_alternative_set,
1795 reg_class_contents[cl]);
1797 win = true;
1798 badop = false;
1799 break;
1801 case TARGET_MEM_CONSTRAINT:
1802 if (MEM_P (op) || spilled_pseudo_p (op))
1803 win = true;
1804 /* We can put constant or pseudo value into memory
1805 to satisfy the constraint. */
1806 if (CONST_POOL_OK_P (mode, op) || REG_P (op))
1807 badop = false;
1808 constmemok = true;
1809 break;
1811 case '<':
1812 if (MEM_P (op)
1813 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
1814 || GET_CODE (XEXP (op, 0)) == POST_DEC))
1815 win = true;
1816 break;
1818 case '>':
1819 if (MEM_P (op)
1820 && (GET_CODE (XEXP (op, 0)) == PRE_INC
1821 || GET_CODE (XEXP (op, 0)) == POST_INC))
1822 win = true;
1823 break;
1825 /* Memory op whose address is not offsettable. */
1826 case 'V':
1827 if (MEM_P (op)
1828 && ! offsettable_nonstrict_memref_p (op))
1829 win = true;
1830 break;
1832 /* Memory operand whose address is offsettable. */
1833 case 'o':
1834 if ((MEM_P (op)
1835 && offsettable_nonstrict_memref_p (op))
1836 || spilled_pseudo_p (op))
1837 win = true;
1838 /* We can put constant or pseudo value into memory
1839 or make memory address offsetable to satisfy the
1840 constraint. */
1841 if (CONST_POOL_OK_P (mode, op) || MEM_P (op) || REG_P (op))
1842 badop = false;
1843 constmemok = true;
1844 offmemok = true;
1845 break;
1847 case 'E':
1848 case 'F':
1849 if (GET_CODE (op) == CONST_DOUBLE
1850 || (GET_CODE (op) == CONST_VECTOR
1851 && (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)))
1852 win = true;
1853 break;
1855 case 'G':
1856 case 'H':
1857 if (CONST_DOUBLE_AS_FLOAT_P (op)
1858 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
1859 win = true;
1860 break;
1862 case 's':
1863 if (CONST_SCALAR_INT_P (op))
1864 break;
1866 case 'i':
1867 if (general_constant_p (op))
1868 win = true;
1869 break;
1871 case 'n':
1872 if (CONST_SCALAR_INT_P (op))
1873 win = true;
1874 break;
1876 case 'I':
1877 case 'J':
1878 case 'K':
1879 case 'L':
1880 case 'M':
1881 case 'N':
1882 case 'O':
1883 case 'P':
1884 if (CONST_INT_P (op)
1885 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
1886 win = true;
1887 break;
1889 case 'X':
1890 /* This constraint should be excluded by the fast
1891 track. */
1892 gcc_unreachable ();
1893 break;
1895 case 'g':
1896 if (MEM_P (op)
1897 || general_constant_p (op)
1898 || spilled_pseudo_p (op))
1899 win = true;
1900 /* Drop through into 'r' case. */
1902 case 'r':
1903 this_alternative
1904 = reg_class_subunion[this_alternative][GENERAL_REGS];
1905 IOR_HARD_REG_SET (this_alternative_set,
1906 reg_class_contents[GENERAL_REGS]);
1907 if (costly_p)
1909 this_costly_alternative
1910 = (reg_class_subunion
1911 [this_costly_alternative][GENERAL_REGS]);
1912 IOR_HARD_REG_SET (this_costly_alternative_set,
1913 reg_class_contents[GENERAL_REGS]);
1915 goto reg;
1917 default:
1918 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
1920 #ifdef EXTRA_CONSTRAINT_STR
1921 if (EXTRA_MEMORY_CONSTRAINT (c, p))
1923 if (EXTRA_CONSTRAINT_STR (op, c, p))
1924 win = true;
1925 else if (spilled_pseudo_p (op))
1926 win = true;
1928 /* If we didn't already win, we can reload
1929 constants via force_const_mem or put the
1930 pseudo value into memory, or make other
1931 memory by reloading the address like for
1932 'o'. */
1933 if (CONST_POOL_OK_P (mode, op)
1934 || MEM_P (op) || REG_P (op))
1935 badop = false;
1936 constmemok = true;
1937 offmemok = true;
1938 break;
1940 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1942 if (EXTRA_CONSTRAINT_STR (op, c, p))
1943 win = true;
1945 /* If we didn't already win, we can reload
1946 the address into a base register. */
1947 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1948 ADDRESS, SCRATCH);
1949 this_alternative
1950 = reg_class_subunion[this_alternative][cl];
1951 IOR_HARD_REG_SET (this_alternative_set,
1952 reg_class_contents[cl]);
1953 if (costly_p)
1955 this_costly_alternative
1956 = (reg_class_subunion
1957 [this_costly_alternative][cl]);
1958 IOR_HARD_REG_SET (this_costly_alternative_set,
1959 reg_class_contents[cl]);
1961 badop = false;
1962 break;
1965 if (EXTRA_CONSTRAINT_STR (op, c, p))
1966 win = true;
1967 #endif
1968 break;
1971 cl = REG_CLASS_FROM_CONSTRAINT (c, p);
1972 this_alternative = reg_class_subunion[this_alternative][cl];
1973 IOR_HARD_REG_SET (this_alternative_set,
1974 reg_class_contents[cl]);
1975 if (costly_p)
1977 this_costly_alternative
1978 = reg_class_subunion[this_costly_alternative][cl];
1979 IOR_HARD_REG_SET (this_costly_alternative_set,
1980 reg_class_contents[cl]);
1982 reg:
1983 if (mode == BLKmode)
1984 break;
1985 winreg = true;
1986 if (REG_P (op))
1988 if (hard_regno[nop] >= 0
1989 && in_hard_reg_set_p (this_alternative_set,
1990 mode, hard_regno[nop]))
1991 win = true;
1992 else if (hard_regno[nop] < 0
1993 && in_class_p (op, this_alternative, NULL))
1994 win = true;
1996 break;
1998 if (c != ' ' && c != '\t')
1999 costly_p = c == '*';
2001 while ((p += len), c);
2003 scratch_p = (operand_reg[nop] != NULL_RTX
2004 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2005 /* Record which operands fit this alternative. */
2006 if (win)
2008 this_alternative_win = true;
2009 if (operand_reg[nop] != NULL_RTX)
2011 if (hard_regno[nop] >= 0)
2013 if (in_hard_reg_set_p (this_costly_alternative_set,
2014 mode, hard_regno[nop]))
2016 if (lra_dump_file != NULL)
2017 fprintf (lra_dump_file,
2018 " %d Costly set: reject++\n",
2019 nop);
2020 reject++;
2023 else
2025 /* Prefer won reg to spilled pseudo under other
2026 equal conditions for possibe inheritance. */
2027 if (! scratch_p)
2029 if (lra_dump_file != NULL)
2030 fprintf
2031 (lra_dump_file,
2032 " %d Non pseudo reload: reject++\n",
2033 nop);
2034 reject++;
2036 if (in_class_p (operand_reg[nop],
2037 this_costly_alternative, NULL))
2039 if (lra_dump_file != NULL)
2040 fprintf
2041 (lra_dump_file,
2042 " %d Non pseudo costly reload:"
2043 " reject++\n",
2044 nop);
2045 reject++;
2048 /* We simulate the behaviour of old reload here.
2049 Although scratches need hard registers and it
2050 might result in spilling other pseudos, no reload
2051 insns are generated for the scratches. So it
2052 might cost something but probably less than old
2053 reload pass believes. */
2054 if (scratch_p)
2056 if (lra_dump_file != NULL)
2057 fprintf (lra_dump_file,
2058 " %d Scratch win: reject+=2\n",
2059 nop);
2060 reject += 2;
2064 else if (did_match)
2065 this_alternative_match_win = true;
2066 else
2068 int const_to_mem = 0;
2069 bool no_regs_p;
2071 /* Never do output reload of stack pointer. It makes
2072 impossible to do elimination when SP is changed in
2073 RTL. */
2074 if (op == stack_pointer_rtx && ! frame_pointer_needed
2075 && curr_static_id->operand[nop].type != OP_IN)
2076 goto fail;
2078 /* If this alternative asks for a specific reg class, see if there
2079 is at least one allocatable register in that class. */
2080 no_regs_p
2081 = (this_alternative == NO_REGS
2082 || (hard_reg_set_subset_p
2083 (reg_class_contents[this_alternative],
2084 lra_no_alloc_regs)));
2086 /* For asms, verify that the class for this alternative is possible
2087 for the mode that is specified. */
2088 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2090 int i;
2091 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2092 if (HARD_REGNO_MODE_OK (i, mode)
2093 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2094 mode, i))
2095 break;
2096 if (i == FIRST_PSEUDO_REGISTER)
2097 winreg = false;
2100 /* If this operand accepts a register, and if the
2101 register class has at least one allocatable register,
2102 then this operand can be reloaded. */
2103 if (winreg && !no_regs_p)
2104 badop = false;
2106 if (badop)
2108 if (lra_dump_file != NULL)
2109 fprintf (lra_dump_file,
2110 " alt=%d: Bad operand -- refuse\n",
2111 nalt);
2112 goto fail;
2115 /* If not assigned pseudo has a class which a subset of
2116 required reg class, it is a less costly alternative
2117 as the pseudo still can get a hard reg of necessary
2118 class. */
2119 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2120 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2121 && ira_class_subset_p[this_alternative][cl])
2123 if (lra_dump_file != NULL)
2124 fprintf
2125 (lra_dump_file,
2126 " %d Super set class reg: reject-=3\n", nop);
2127 reject -= 3;
2130 this_alternative_offmemok = offmemok;
2131 if (this_costly_alternative != NO_REGS)
2133 if (lra_dump_file != NULL)
2134 fprintf (lra_dump_file,
2135 " %d Costly loser: reject++\n", nop);
2136 reject++;
2138 /* If the operand is dying, has a matching constraint,
2139 and satisfies constraints of the matched operand
2140 which failed to satisfy the own constraints, probably
2141 the reload for this operand will be gone. */
2142 if (this_alternative_matches >= 0
2143 && !curr_alt_win[this_alternative_matches]
2144 && REG_P (op)
2145 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2146 && (hard_regno[nop] >= 0
2147 ? in_hard_reg_set_p (this_alternative_set,
2148 mode, hard_regno[nop])
2149 : in_class_p (op, this_alternative, NULL)))
2151 if (lra_dump_file != NULL)
2152 fprintf
2153 (lra_dump_file,
2154 " %d Dying matched operand reload: reject++\n",
2155 nop);
2156 reject++;
2158 else
2160 /* Strict_low_part requires to reload the register
2161 not the sub-register. In this case we should
2162 check that a final reload hard reg can hold the
2163 value mode. */
2164 if (curr_static_id->operand[nop].strict_low
2165 && REG_P (op)
2166 && hard_regno[nop] < 0
2167 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2168 && ira_class_hard_regs_num[this_alternative] > 0
2169 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2170 [this_alternative][0],
2171 GET_MODE
2172 (*curr_id->operand_loc[nop])))
2174 if (lra_dump_file != NULL)
2175 fprintf
2176 (lra_dump_file,
2177 " alt=%d: Strict low subreg reload -- refuse\n",
2178 nalt);
2179 goto fail;
2181 losers++;
2183 if (operand_reg[nop] != NULL_RTX
2184 /* Output operands and matched input operands are
2185 not inherited. The following conditions do not
2186 exactly describe the previous statement but they
2187 are pretty close. */
2188 && curr_static_id->operand[nop].type != OP_OUT
2189 && (this_alternative_matches < 0
2190 || curr_static_id->operand[nop].type != OP_IN))
2192 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2193 (operand_reg[nop])]
2194 .last_reload);
2196 /* The value of reload_sum has sense only if we
2197 process insns in their order. It happens only on
2198 the first constraints sub-pass when we do most of
2199 reload work. */
2200 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2201 reload_sum += last_reload - bb_reload_num;
2203 /* If this is a constant that is reloaded into the
2204 desired class by copying it to memory first, count
2205 that as another reload. This is consistent with
2206 other code and is required to avoid choosing another
2207 alternative when the constant is moved into memory.
2208 Note that the test here is precisely the same as in
2209 the code below that calls force_const_mem. */
2210 if (CONST_POOL_OK_P (mode, op)
2211 && ((targetm.preferred_reload_class
2212 (op, this_alternative) == NO_REGS)
2213 || no_input_reloads_p))
2215 const_to_mem = 1;
2216 if (! no_regs_p)
2217 losers++;
2220 /* Alternative loses if it requires a type of reload not
2221 permitted for this insn. We can always reload
2222 objects with a REG_UNUSED note. */
2223 if ((curr_static_id->operand[nop].type != OP_IN
2224 && no_output_reloads_p
2225 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2226 || (curr_static_id->operand[nop].type != OP_OUT
2227 && no_input_reloads_p && ! const_to_mem)
2228 || (this_alternative_matches >= 0
2229 && (no_input_reloads_p
2230 || (no_output_reloads_p
2231 && (curr_static_id->operand
2232 [this_alternative_matches].type != OP_IN)
2233 && ! find_reg_note (curr_insn, REG_UNUSED,
2234 no_subreg_reg_operand
2235 [this_alternative_matches])))))
2237 if (lra_dump_file != NULL)
2238 fprintf
2239 (lra_dump_file,
2240 " alt=%d: No input/otput reload -- refuse\n",
2241 nalt);
2242 goto fail;
2245 /* Check strong discouragement of reload of non-constant
2246 into class THIS_ALTERNATIVE. */
2247 if (! CONSTANT_P (op) && ! no_regs_p
2248 && (targetm.preferred_reload_class
2249 (op, this_alternative) == NO_REGS
2250 || (curr_static_id->operand[nop].type == OP_OUT
2251 && (targetm.preferred_output_reload_class
2252 (op, this_alternative) == NO_REGS))))
2254 if (lra_dump_file != NULL)
2255 fprintf (lra_dump_file,
2256 " %d Non-prefered reload: reject+=%d\n",
2257 nop, LRA_MAX_REJECT);
2258 reject += LRA_MAX_REJECT;
2261 if (! (MEM_P (op) && offmemok)
2262 && ! (const_to_mem && constmemok))
2264 /* We prefer to reload pseudos over reloading other
2265 things, since such reloads may be able to be
2266 eliminated later. So bump REJECT in other cases.
2267 Don't do this in the case where we are forcing a
2268 constant into memory and it will then win since
2269 we don't want to have a different alternative
2270 match then. */
2271 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2273 if (lra_dump_file != NULL)
2274 fprintf
2275 (lra_dump_file,
2276 " %d Non-pseudo reload: reject+=2\n",
2277 nop);
2278 reject += 2;
2281 if (! no_regs_p)
2282 reload_nregs
2283 += ira_reg_class_max_nregs[this_alternative][mode];
2285 if (SMALL_REGISTER_CLASS_P (this_alternative))
2287 if (lra_dump_file != NULL)
2288 fprintf
2289 (lra_dump_file,
2290 " %d Small class reload: reject+=%d\n",
2291 nop, LRA_LOSER_COST_FACTOR / 2);
2292 reject += LRA_LOSER_COST_FACTOR / 2;
2296 /* We are trying to spill pseudo into memory. It is
2297 usually more costly than moving to a hard register
2298 although it might takes the same number of
2299 reloads. */
2300 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2302 if (lra_dump_file != NULL)
2303 fprintf
2304 (lra_dump_file,
2305 " %d Spill pseudo in memory: reject+=3\n",
2306 nop);
2307 reject += 3;
2310 #ifdef SECONDARY_MEMORY_NEEDED
2311 /* If reload requires moving value through secondary
2312 memory, it will need one more insn at least. */
2313 if (this_alternative != NO_REGS
2314 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2315 && ((curr_static_id->operand[nop].type != OP_OUT
2316 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2317 GET_MODE (op)))
2318 || (curr_static_id->operand[nop].type != OP_IN
2319 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2320 GET_MODE (op)))))
2321 losers++;
2322 #endif
2323 /* Input reloads can be inherited more often than output
2324 reloads can be removed, so penalize output
2325 reloads. */
2326 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2328 if (lra_dump_file != NULL)
2329 fprintf
2330 (lra_dump_file,
2331 " %d Non input pseudo reload: reject++\n",
2332 nop);
2333 reject++;
2337 if (early_clobber_p && ! scratch_p)
2339 if (lra_dump_file != NULL)
2340 fprintf (lra_dump_file,
2341 " %d Early clobber: reject++\n", nop);
2342 reject++;
2344 /* ??? We check early clobbers after processing all operands
2345 (see loop below) and there we update the costs more.
2346 Should we update the cost (may be approximately) here
2347 because of early clobber register reloads or it is a rare
2348 or non-important thing to be worth to do it. */
2349 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2350 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2352 if (lra_dump_file != NULL)
2353 fprintf (lra_dump_file,
2354 " alt=%d,overall=%d,losers=%d -- refuse\n",
2355 nalt, overall, losers);
2356 goto fail;
2359 curr_alt[nop] = this_alternative;
2360 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2361 curr_alt_win[nop] = this_alternative_win;
2362 curr_alt_match_win[nop] = this_alternative_match_win;
2363 curr_alt_offmemok[nop] = this_alternative_offmemok;
2364 curr_alt_matches[nop] = this_alternative_matches;
2366 if (this_alternative_matches >= 0
2367 && !did_match && !this_alternative_win)
2368 curr_alt_win[this_alternative_matches] = false;
2370 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2371 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2373 if (curr_insn_set != NULL_RTX && n_operands == 2
2374 /* Prevent processing non-move insns. */
2375 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2376 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2377 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2378 && REG_P (no_subreg_reg_operand[0])
2379 && REG_P (no_subreg_reg_operand[1])
2380 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2381 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2382 || (! curr_alt_win[0] && curr_alt_win[1]
2383 && REG_P (no_subreg_reg_operand[1])
2384 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2385 || (curr_alt_win[0] && ! curr_alt_win[1]
2386 && REG_P (no_subreg_reg_operand[0])
2387 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2388 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2389 no_subreg_reg_operand[1])
2390 || (targetm.preferred_reload_class
2391 (no_subreg_reg_operand[1],
2392 (enum reg_class) curr_alt[1]) != NO_REGS))
2393 /* If it is a result of recent elimination in move
2394 insn we can transform it into an add still by
2395 using this alternative. */
2396 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2398 /* We have a move insn and a new reload insn will be similar
2399 to the current insn. We should avoid such situation as it
2400 results in LRA cycling. */
2401 overall += LRA_MAX_REJECT;
2403 ok_p = true;
2404 curr_alt_dont_inherit_ops_num = 0;
2405 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2407 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2408 HARD_REG_SET temp_set;
2410 i = early_clobbered_nops[nop];
2411 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2412 || hard_regno[i] < 0)
2413 continue;
2414 lra_assert (operand_reg[i] != NULL_RTX);
2415 clobbered_hard_regno = hard_regno[i];
2416 CLEAR_HARD_REG_SET (temp_set);
2417 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2418 first_conflict_j = last_conflict_j = -1;
2419 for (j = 0; j < n_operands; j++)
2420 if (j == i
2421 /* We don't want process insides of match_operator and
2422 match_parallel because otherwise we would process
2423 their operands once again generating a wrong
2424 code. */
2425 || curr_static_id->operand[j].is_operator)
2426 continue;
2427 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2428 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2429 continue;
2430 /* If we don't reload j-th operand, check conflicts. */
2431 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2432 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2434 if (first_conflict_j < 0)
2435 first_conflict_j = j;
2436 last_conflict_j = j;
2438 if (last_conflict_j < 0)
2439 continue;
2440 /* If earlyclobber operand conflicts with another
2441 non-matching operand which is actually the same register
2442 as the earlyclobber operand, it is better to reload the
2443 another operand as an operand matching the earlyclobber
2444 operand can be also the same. */
2445 if (first_conflict_j == last_conflict_j
2446 && operand_reg[last_conflict_j]
2447 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2448 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2450 curr_alt_win[last_conflict_j] = false;
2451 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2452 = last_conflict_j;
2453 losers++;
2454 /* Early clobber was already reflected in REJECT. */
2455 lra_assert (reject > 0);
2456 if (lra_dump_file != NULL)
2457 fprintf
2458 (lra_dump_file,
2459 " %d Conflict early clobber reload: reject--\n",
2461 reject--;
2462 overall += LRA_LOSER_COST_FACTOR - 1;
2464 else
2466 /* We need to reload early clobbered register and the
2467 matched registers. */
2468 for (j = 0; j < n_operands; j++)
2469 if (curr_alt_matches[j] == i)
2471 curr_alt_match_win[j] = false;
2472 losers++;
2473 overall += LRA_LOSER_COST_FACTOR;
2475 if (! curr_alt_match_win[i])
2476 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2477 else
2479 /* Remember pseudos used for match reloads are never
2480 inherited. */
2481 lra_assert (curr_alt_matches[i] >= 0);
2482 curr_alt_win[curr_alt_matches[i]] = false;
2484 curr_alt_win[i] = curr_alt_match_win[i] = false;
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 Matched conflict early clobber reloads:"
2492 "reject--\n",
2494 reject--;
2495 overall += LRA_LOSER_COST_FACTOR - 1;
2498 if (lra_dump_file != NULL)
2499 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2500 nalt, overall, losers, reload_nregs);
2502 /* If this alternative can be made to work by reloading, and it
2503 needs less reloading than the others checked so far, record
2504 it as the chosen goal for reloading. */
2505 if ((best_losers != 0 && losers == 0)
2506 || (((best_losers == 0 && losers == 0)
2507 || (best_losers != 0 && losers != 0))
2508 && (best_overall > overall
2509 || (best_overall == overall
2510 /* If the cost of the reloads is the same,
2511 prefer alternative which requires minimal
2512 number of reload regs. */
2513 && (reload_nregs < best_reload_nregs
2514 || (reload_nregs == best_reload_nregs
2515 && (best_reload_sum < reload_sum
2516 || (best_reload_sum == reload_sum
2517 && nalt < goal_alt_number))))))))
2519 for (nop = 0; nop < n_operands; nop++)
2521 goal_alt_win[nop] = curr_alt_win[nop];
2522 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2523 goal_alt_matches[nop] = curr_alt_matches[nop];
2524 goal_alt[nop] = curr_alt[nop];
2525 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2527 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2528 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2529 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2530 goal_alt_swapped = curr_swapped;
2531 best_overall = overall;
2532 best_losers = losers;
2533 best_reload_nregs = reload_nregs;
2534 best_reload_sum = reload_sum;
2535 goal_alt_number = nalt;
2537 if (losers == 0)
2538 /* Everything is satisfied. Do not process alternatives
2539 anymore. */
2540 break;
2541 fail:
2544 return ok_p;
2547 /* Return 1 if ADDR is a valid memory address for mode MODE in address
2548 space AS, and check that each pseudo has the proper kind of hard
2549 reg. */
2550 static int
2551 valid_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2552 rtx addr, addr_space_t as)
2554 #ifdef GO_IF_LEGITIMATE_ADDRESS
2555 lra_assert (ADDR_SPACE_GENERIC_P (as));
2556 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
2557 return 0;
2559 win:
2560 return 1;
2561 #else
2562 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
2563 #endif
2566 /* Return whether address AD is valid. */
2568 static bool
2569 valid_address_p (struct address_info *ad)
2571 /* Some ports do not check displacements for eliminable registers,
2572 so we replace them temporarily with the elimination target. */
2573 rtx saved_base_reg = NULL_RTX;
2574 rtx saved_index_reg = NULL_RTX;
2575 rtx *base_term = strip_subreg (ad->base_term);
2576 rtx *index_term = strip_subreg (ad->index_term);
2577 if (base_term != NULL)
2579 saved_base_reg = *base_term;
2580 lra_eliminate_reg_if_possible (base_term);
2581 if (ad->base_term2 != NULL)
2582 *ad->base_term2 = *ad->base_term;
2584 if (index_term != NULL)
2586 saved_index_reg = *index_term;
2587 lra_eliminate_reg_if_possible (index_term);
2589 bool ok_p = valid_address_p (ad->mode, *ad->outer, ad->as);
2590 if (saved_base_reg != NULL_RTX)
2592 *base_term = saved_base_reg;
2593 if (ad->base_term2 != NULL)
2594 *ad->base_term2 = *ad->base_term;
2596 if (saved_index_reg != NULL_RTX)
2597 *index_term = saved_index_reg;
2598 return ok_p;
2601 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2602 static rtx
2603 base_plus_disp_to_reg (struct address_info *ad)
2605 enum reg_class cl;
2606 rtx new_reg;
2608 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2609 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2610 get_index_code (ad));
2611 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2612 cl, "base + disp");
2613 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2614 return new_reg;
2617 /* Return true if we can add a displacement to address AD, even if that
2618 makes the address invalid. The fix-up code requires any new address
2619 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2620 static bool
2621 can_add_disp_p (struct address_info *ad)
2623 return (!ad->autoinc_p
2624 && ad->segment == NULL
2625 && ad->base == ad->base_term
2626 && ad->disp == ad->disp_term);
2629 /* Make equiv substitution in address AD. Return true if a substitution
2630 was made. */
2631 static bool
2632 equiv_address_substitution (struct address_info *ad)
2634 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2635 HOST_WIDE_INT disp, scale;
2636 bool change_p;
2638 base_term = strip_subreg (ad->base_term);
2639 if (base_term == NULL)
2640 base_reg = new_base_reg = NULL_RTX;
2641 else
2643 base_reg = *base_term;
2644 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2646 index_term = strip_subreg (ad->index_term);
2647 if (index_term == NULL)
2648 index_reg = new_index_reg = NULL_RTX;
2649 else
2651 index_reg = *index_term;
2652 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2654 if (base_reg == new_base_reg && index_reg == new_index_reg)
2655 return false;
2656 disp = 0;
2657 change_p = false;
2658 if (lra_dump_file != NULL)
2660 fprintf (lra_dump_file, "Changing address in insn %d ",
2661 INSN_UID (curr_insn));
2662 dump_value_slim (lra_dump_file, *ad->outer, 1);
2664 if (base_reg != new_base_reg)
2666 if (REG_P (new_base_reg))
2668 *base_term = new_base_reg;
2669 change_p = true;
2671 else if (GET_CODE (new_base_reg) == PLUS
2672 && REG_P (XEXP (new_base_reg, 0))
2673 && CONST_INT_P (XEXP (new_base_reg, 1))
2674 && can_add_disp_p (ad))
2676 disp += INTVAL (XEXP (new_base_reg, 1));
2677 *base_term = XEXP (new_base_reg, 0);
2678 change_p = true;
2680 if (ad->base_term2 != NULL)
2681 *ad->base_term2 = *ad->base_term;
2683 if (index_reg != new_index_reg)
2685 if (REG_P (new_index_reg))
2687 *index_term = new_index_reg;
2688 change_p = true;
2690 else if (GET_CODE (new_index_reg) == PLUS
2691 && REG_P (XEXP (new_index_reg, 0))
2692 && CONST_INT_P (XEXP (new_index_reg, 1))
2693 && can_add_disp_p (ad)
2694 && (scale = get_index_scale (ad)))
2696 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2697 *index_term = XEXP (new_index_reg, 0);
2698 change_p = true;
2701 if (disp != 0)
2703 if (ad->disp != NULL)
2704 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2705 else
2707 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2708 update_address (ad);
2710 change_p = true;
2712 if (lra_dump_file != NULL)
2714 if (! change_p)
2715 fprintf (lra_dump_file, " -- no change\n");
2716 else
2718 fprintf (lra_dump_file, " on equiv ");
2719 dump_value_slim (lra_dump_file, *ad->outer, 1);
2720 fprintf (lra_dump_file, "\n");
2723 return change_p;
2726 /* Major function to make reloads for an address in operand NOP.
2727 The supported cases are:
2729 1) an address that existed before LRA started, at which point it
2730 must have been valid. These addresses are subject to elimination
2731 and may have become invalid due to the elimination offset being out
2732 of range.
2734 2) an address created by forcing a constant to memory
2735 (force_const_to_mem). The initial form of these addresses might
2736 not be valid, and it is this function's job to make them valid.
2738 3) a frame address formed from a register and a (possibly zero)
2739 constant offset. As above, these addresses might not be valid and
2740 this function must make them so.
2742 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2743 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2744 address. Return true for any RTL change. */
2745 static bool
2746 process_address (int nop, rtx *before, rtx *after)
2748 struct address_info ad;
2749 rtx new_reg;
2750 rtx op = *curr_id->operand_loc[nop];
2751 const char *constraint = curr_static_id->operand[nop].constraint;
2752 bool change_p;
2754 if (constraint[0] == 'p'
2755 || EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint))
2756 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2757 else if (MEM_P (op))
2758 decompose_mem_address (&ad, op);
2759 else if (GET_CODE (op) == SUBREG
2760 && MEM_P (SUBREG_REG (op)))
2761 decompose_mem_address (&ad, SUBREG_REG (op));
2762 else
2763 return false;
2764 change_p = equiv_address_substitution (&ad);
2765 if (ad.base_term != NULL
2766 && (process_addr_reg
2767 (ad.base_term, before,
2768 (ad.autoinc_p
2769 && !(REG_P (*ad.base_term)
2770 && find_regno_note (curr_insn, REG_DEAD,
2771 REGNO (*ad.base_term)) != NULL_RTX)
2772 ? after : NULL),
2773 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2774 get_index_code (&ad)))))
2776 change_p = true;
2777 if (ad.base_term2 != NULL)
2778 *ad.base_term2 = *ad.base_term;
2780 if (ad.index_term != NULL
2781 && process_addr_reg (ad.index_term, before, NULL, INDEX_REG_CLASS))
2782 change_p = true;
2784 #ifdef EXTRA_CONSTRAINT_STR
2785 /* Target hooks sometimes reject extra constraint addresses -- use
2786 EXTRA_CONSTRAINT_STR for the validation. */
2787 if (constraint[0] != 'p'
2788 && EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint)
2789 && EXTRA_CONSTRAINT_STR (op, constraint[0], constraint))
2790 return change_p;
2791 #endif
2793 /* There are three cases where the shape of *AD.INNER may now be invalid:
2795 1) the original address was valid, but either elimination or
2796 equiv_address_substitution was applied and that made
2797 the address invalid.
2799 2) the address is an invalid symbolic address created by
2800 force_const_to_mem.
2802 3) the address is a frame address with an invalid offset.
2804 All these cases involve a non-autoinc address, so there is no
2805 point revalidating other types. */
2806 if (ad.autoinc_p || valid_address_p (&ad))
2807 return change_p;
2809 /* Any index existed before LRA started, so we can assume that the
2810 presence and shape of the index is valid. */
2811 push_to_sequence (*before);
2812 lra_assert (ad.disp == ad.disp_term);
2813 if (ad.base == NULL)
2815 if (ad.index == NULL)
2817 int code = -1;
2818 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2819 SCRATCH, SCRATCH);
2820 rtx addr = *ad.inner;
2822 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2823 #ifdef HAVE_lo_sum
2825 rtx insn;
2826 rtx last = get_last_insn ();
2828 /* addr => lo_sum (new_base, addr), case (2) above. */
2829 insn = emit_insn (gen_rtx_SET
2830 (VOIDmode, new_reg,
2831 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2832 code = recog_memoized (insn);
2833 if (code >= 0)
2835 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2836 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2838 /* Try to put lo_sum into register. */
2839 insn = emit_insn (gen_rtx_SET
2840 (VOIDmode, new_reg,
2841 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2842 code = recog_memoized (insn);
2843 if (code >= 0)
2845 *ad.inner = new_reg;
2846 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2848 *ad.inner = addr;
2849 code = -1;
2855 if (code < 0)
2856 delete_insns_since (last);
2858 #endif
2859 if (code < 0)
2861 /* addr => new_base, case (2) above. */
2862 lra_emit_move (new_reg, addr);
2863 *ad.inner = new_reg;
2866 else
2868 /* index * scale + disp => new base + index * scale,
2869 case (1) above. */
2870 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
2871 GET_CODE (*ad.index));
2873 lra_assert (INDEX_REG_CLASS != NO_REGS);
2874 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
2875 lra_emit_move (new_reg, *ad.disp);
2876 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2877 new_reg, *ad.index);
2880 else if (ad.index == NULL)
2882 int regno;
2883 enum reg_class cl;
2884 rtx set, insns, last_insn;
2885 /* base + disp => new base, cases (1) and (3) above. */
2886 /* Another option would be to reload the displacement into an
2887 index register. However, postreload has code to optimize
2888 address reloads that have the same base and different
2889 displacements, so reloading into an index register would
2890 not necessarily be a win. */
2891 start_sequence ();
2892 new_reg = base_plus_disp_to_reg (&ad);
2893 insns = get_insns ();
2894 last_insn = get_last_insn ();
2895 /* If we generated at least two insns, try last insn source as
2896 an address. If we succeed, we generate one less insn. */
2897 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
2898 && GET_CODE (SET_SRC (set)) == PLUS
2899 && REG_P (XEXP (SET_SRC (set), 0))
2900 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
2902 *ad.inner = SET_SRC (set);
2903 if (valid_address_p (ad.mode, *ad.outer, ad.as))
2905 *ad.base_term = XEXP (SET_SRC (set), 0);
2906 *ad.disp_term = XEXP (SET_SRC (set), 1);
2907 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2908 get_index_code (&ad));
2909 regno = REGNO (*ad.base_term);
2910 if (regno >= FIRST_PSEUDO_REGISTER
2911 && cl != lra_get_allocno_class (regno))
2912 lra_change_class (regno, cl, " Change to", true);
2913 new_reg = SET_SRC (set);
2914 delete_insns_since (PREV_INSN (last_insn));
2917 end_sequence ();
2918 emit_insn (insns);
2919 *ad.inner = new_reg;
2921 else
2923 /* base + scale * index + disp => new base + scale * index,
2924 case (1) above. */
2925 new_reg = base_plus_disp_to_reg (&ad);
2926 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2927 new_reg, *ad.index);
2929 *before = get_insns ();
2930 end_sequence ();
2931 return true;
2934 /* Emit insns to reload VALUE into a new register. VALUE is an
2935 auto-increment or auto-decrement RTX whose operand is a register or
2936 memory location; so reloading involves incrementing that location.
2937 IN is either identical to VALUE, or some cheaper place to reload
2938 value being incremented/decremented from.
2940 INC_AMOUNT is the number to increment or decrement by (always
2941 positive and ignored for POST_MODIFY/PRE_MODIFY).
2943 Return pseudo containing the result. */
2944 static rtx
2945 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
2947 /* REG or MEM to be copied and incremented. */
2948 rtx incloc = XEXP (value, 0);
2949 /* Nonzero if increment after copying. */
2950 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
2951 || GET_CODE (value) == POST_MODIFY);
2952 rtx last;
2953 rtx inc;
2954 rtx add_insn;
2955 int code;
2956 rtx real_in = in == value ? incloc : in;
2957 rtx result;
2958 bool plus_p = true;
2960 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
2962 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
2963 || GET_CODE (XEXP (value, 1)) == MINUS);
2964 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
2965 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
2966 inc = XEXP (XEXP (value, 1), 1);
2968 else
2970 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
2971 inc_amount = -inc_amount;
2973 inc = GEN_INT (inc_amount);
2976 if (! post && REG_P (incloc))
2977 result = incloc;
2978 else
2979 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
2980 "INC/DEC result");
2982 if (real_in != result)
2984 /* First copy the location to the result register. */
2985 lra_assert (REG_P (result));
2986 emit_insn (gen_move_insn (result, real_in));
2989 /* We suppose that there are insns to add/sub with the constant
2990 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
2991 old reload worked with this assumption. If the assumption
2992 becomes wrong, we should use approach in function
2993 base_plus_disp_to_reg. */
2994 if (in == value)
2996 /* See if we can directly increment INCLOC. */
2997 last = get_last_insn ();
2998 add_insn = emit_insn (plus_p
2999 ? gen_add2_insn (incloc, inc)
3000 : gen_sub2_insn (incloc, inc));
3002 code = recog_memoized (add_insn);
3003 if (code >= 0)
3005 if (! post && result != incloc)
3006 emit_insn (gen_move_insn (result, incloc));
3007 return result;
3009 delete_insns_since (last);
3012 /* If couldn't do the increment directly, must increment in RESULT.
3013 The way we do this depends on whether this is pre- or
3014 post-increment. For pre-increment, copy INCLOC to the reload
3015 register, increment it there, then save back. */
3016 if (! post)
3018 if (real_in != result)
3019 emit_insn (gen_move_insn (result, real_in));
3020 if (plus_p)
3021 emit_insn (gen_add2_insn (result, inc));
3022 else
3023 emit_insn (gen_sub2_insn (result, inc));
3024 if (result != incloc)
3025 emit_insn (gen_move_insn (incloc, result));
3027 else
3029 /* Post-increment.
3031 Because this might be a jump insn or a compare, and because
3032 RESULT may not be available after the insn in an input
3033 reload, we must do the incrementing before the insn being
3034 reloaded for.
3036 We have already copied IN to RESULT. Increment the copy in
3037 RESULT, save that back, then decrement RESULT so it has
3038 the original value. */
3039 if (plus_p)
3040 emit_insn (gen_add2_insn (result, inc));
3041 else
3042 emit_insn (gen_sub2_insn (result, inc));
3043 emit_insn (gen_move_insn (incloc, result));
3044 /* Restore non-modified value for the result. We prefer this
3045 way because it does not require an additional hard
3046 register. */
3047 if (plus_p)
3049 if (CONST_INT_P (inc))
3050 emit_insn (gen_add2_insn (result,
3051 gen_int_mode (-INTVAL (inc),
3052 GET_MODE (result))));
3053 else
3054 emit_insn (gen_sub2_insn (result, inc));
3056 else
3057 emit_insn (gen_add2_insn (result, inc));
3059 return result;
3062 /* Return true if the current move insn does not need processing as we
3063 already know that it satisfies its constraints. */
3064 static bool
3065 simple_move_p (void)
3067 rtx dest, src;
3068 enum reg_class dclass, sclass;
3070 lra_assert (curr_insn_set != NULL_RTX);
3071 dest = SET_DEST (curr_insn_set);
3072 src = SET_SRC (curr_insn_set);
3073 return ((dclass = get_op_class (dest)) != NO_REGS
3074 && (sclass = get_op_class (src)) != NO_REGS
3075 /* The backend guarantees that register moves of cost 2
3076 never need reloads. */
3077 && targetm.register_move_cost (GET_MODE (src), dclass, sclass) == 2);
3080 /* Swap operands NOP and NOP + 1. */
3081 static inline void
3082 swap_operands (int nop)
3084 enum machine_mode mode = curr_operand_mode[nop];
3085 curr_operand_mode[nop] = curr_operand_mode[nop + 1];
3086 curr_operand_mode[nop + 1] = mode;
3087 rtx x = *curr_id->operand_loc[nop];
3088 *curr_id->operand_loc[nop] = *curr_id->operand_loc[nop + 1];
3089 *curr_id->operand_loc[nop + 1] = x;
3090 /* Swap the duplicates too. */
3091 lra_update_dup (curr_id, nop);
3092 lra_update_dup (curr_id, nop + 1);
3095 /* Main entry point of the constraint code: search the body of the
3096 current insn to choose the best alternative. It is mimicking insn
3097 alternative cost calculation model of former reload pass. That is
3098 because machine descriptions were written to use this model. This
3099 model can be changed in future. Make commutative operand exchange
3100 if it is chosen.
3102 Return true if some RTL changes happened during function call. */
3103 static bool
3104 curr_insn_transform (void)
3106 int i, j, k;
3107 int n_operands;
3108 int n_alternatives;
3109 int commutative;
3110 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3111 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3112 rtx before, after;
3113 bool alt_p = false;
3114 /* Flag that the insn has been changed through a transformation. */
3115 bool change_p;
3116 bool sec_mem_p;
3117 #ifdef SECONDARY_MEMORY_NEEDED
3118 bool use_sec_mem_p;
3119 #endif
3120 int max_regno_before;
3121 int reused_alternative_num;
3123 curr_insn_set = single_set (curr_insn);
3124 if (curr_insn_set != NULL_RTX && simple_move_p ())
3125 return false;
3127 no_input_reloads_p = no_output_reloads_p = false;
3128 goal_alt_number = -1;
3129 change_p = sec_mem_p = false;
3130 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3131 reloads; neither are insns that SET cc0. Insns that use CC0 are
3132 not allowed to have any input reloads. */
3133 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3134 no_output_reloads_p = true;
3136 #ifdef HAVE_cc0
3137 if (reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3138 no_input_reloads_p = true;
3139 if (reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3140 no_output_reloads_p = true;
3141 #endif
3143 n_operands = curr_static_id->n_operands;
3144 n_alternatives = curr_static_id->n_alternatives;
3146 /* Just return "no reloads" if insn has no operands with
3147 constraints. */
3148 if (n_operands == 0 || n_alternatives == 0)
3149 return false;
3151 max_regno_before = max_reg_num ();
3153 for (i = 0; i < n_operands; i++)
3155 goal_alt_matched[i][0] = -1;
3156 goal_alt_matches[i] = -1;
3159 commutative = curr_static_id->commutative;
3161 /* Now see what we need for pseudos that didn't get hard regs or got
3162 the wrong kind of hard reg. For this, we must consider all the
3163 operands together against the register constraints. */
3165 best_losers = best_overall = INT_MAX;
3166 best_reload_sum = 0;
3168 curr_swapped = false;
3169 goal_alt_swapped = false;
3171 /* Make equivalence substitution and memory subreg elimination
3172 before address processing because an address legitimacy can
3173 depend on memory mode. */
3174 for (i = 0; i < n_operands; i++)
3176 rtx op = *curr_id->operand_loc[i];
3177 rtx subst, old = op;
3178 bool op_change_p = false;
3180 if (GET_CODE (old) == SUBREG)
3181 old = SUBREG_REG (old);
3182 subst = get_equiv_with_elimination (old, curr_insn);
3183 if (subst != old)
3185 subst = copy_rtx (subst);
3186 lra_assert (REG_P (old));
3187 if (GET_CODE (op) == SUBREG)
3188 SUBREG_REG (op) = subst;
3189 else
3190 *curr_id->operand_loc[i] = subst;
3191 if (lra_dump_file != NULL)
3193 fprintf (lra_dump_file,
3194 "Changing pseudo %d in operand %i of insn %u on equiv ",
3195 REGNO (old), i, INSN_UID (curr_insn));
3196 dump_value_slim (lra_dump_file, subst, 1);
3197 fprintf (lra_dump_file, "\n");
3199 op_change_p = change_p = true;
3201 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3203 change_p = true;
3204 lra_update_dup (curr_id, i);
3208 /* Reload address registers and displacements. We do it before
3209 finding an alternative because of memory constraints. */
3210 before = after = NULL_RTX;
3211 for (i = 0; i < n_operands; i++)
3212 if (! curr_static_id->operand[i].is_operator
3213 && process_address (i, &before, &after))
3215 change_p = true;
3216 lra_update_dup (curr_id, i);
3219 if (change_p)
3220 /* If we've changed the instruction then any alternative that
3221 we chose previously may no longer be valid. */
3222 lra_set_used_insn_alternative (curr_insn, -1);
3224 if (curr_insn_set != NULL_RTX
3225 && check_and_process_move (&change_p, &sec_mem_p))
3226 return change_p;
3228 try_swapped:
3230 reused_alternative_num = curr_id->used_insn_alternative;
3231 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3232 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3233 reused_alternative_num, INSN_UID (curr_insn));
3235 if (process_alt_operands (reused_alternative_num))
3236 alt_p = true;
3238 /* If insn is commutative (it's safe to exchange a certain pair of
3239 operands) then we need to try each alternative twice, the second
3240 time matching those two operands as if we had exchanged them. To
3241 do this, really exchange them in operands.
3243 If we have just tried the alternatives the second time, return
3244 operands to normal and drop through. */
3246 if (reused_alternative_num < 0 && commutative >= 0)
3248 curr_swapped = !curr_swapped;
3249 if (curr_swapped)
3251 swap_operands (commutative);
3252 goto try_swapped;
3254 else
3255 swap_operands (commutative);
3258 if (! alt_p && ! sec_mem_p)
3260 /* No alternative works with reloads?? */
3261 if (INSN_CODE (curr_insn) >= 0)
3262 fatal_insn ("unable to generate reloads for:", curr_insn);
3263 error_for_asm (curr_insn,
3264 "inconsistent operand constraints in an %<asm%>");
3265 /* Avoid further trouble with this insn. */
3266 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3267 lra_invalidate_insn_data (curr_insn);
3268 return true;
3271 /* If the best alternative is with operands 1 and 2 swapped, swap
3272 them. Update the operand numbers of any reloads already
3273 pushed. */
3275 if (goal_alt_swapped)
3277 if (lra_dump_file != NULL)
3278 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3279 INSN_UID (curr_insn));
3281 /* Swap the duplicates too. */
3282 swap_operands (commutative);
3283 change_p = true;
3286 #ifdef SECONDARY_MEMORY_NEEDED
3287 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3288 too conservatively. So we use the secondary memory only if there
3289 is no any alternative without reloads. */
3290 use_sec_mem_p = false;
3291 if (! alt_p)
3292 use_sec_mem_p = true;
3293 else if (sec_mem_p)
3295 for (i = 0; i < n_operands; i++)
3296 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3297 break;
3298 use_sec_mem_p = i < n_operands;
3301 if (use_sec_mem_p)
3303 rtx new_reg, src, dest, rld;
3304 enum machine_mode sec_mode, rld_mode;
3306 lra_assert (sec_mem_p);
3307 lra_assert (curr_static_id->operand[0].type == OP_OUT
3308 && curr_static_id->operand[1].type == OP_IN);
3309 dest = *curr_id->operand_loc[0];
3310 src = *curr_id->operand_loc[1];
3311 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3312 ? dest : src);
3313 rld_mode = GET_MODE (rld);
3314 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3315 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3316 #else
3317 sec_mode = rld_mode;
3318 #endif
3319 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3320 NO_REGS, "secondary");
3321 /* If the mode is changed, it should be wider. */
3322 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3323 if (sec_mode != rld_mode)
3325 /* If the target says specifically to use another mode for
3326 secondary memory moves we can not reuse the original
3327 insn. */
3328 after = emit_spill_move (false, new_reg, dest);
3329 lra_process_new_insns (curr_insn, NULL_RTX, after,
3330 "Inserting the sec. move");
3331 /* We may have non null BEFORE here (e.g. after address
3332 processing. */
3333 push_to_sequence (before);
3334 before = emit_spill_move (true, new_reg, src);
3335 emit_insn (before);
3336 before = get_insns ();
3337 end_sequence ();
3338 lra_process_new_insns (curr_insn, before, NULL_RTX, "Changing on");
3339 lra_set_insn_deleted (curr_insn);
3341 else if (dest == rld)
3343 *curr_id->operand_loc[0] = new_reg;
3344 after = emit_spill_move (false, new_reg, dest);
3345 lra_process_new_insns (curr_insn, NULL_RTX, after,
3346 "Inserting the sec. move");
3348 else
3350 *curr_id->operand_loc[1] = new_reg;
3351 /* See comments above. */
3352 push_to_sequence (before);
3353 before = emit_spill_move (true, new_reg, src);
3354 emit_insn (before);
3355 before = get_insns ();
3356 end_sequence ();
3357 lra_process_new_insns (curr_insn, before, NULL_RTX,
3358 "Inserting the sec. move");
3360 lra_update_insn_regno_info (curr_insn);
3361 return true;
3363 #endif
3365 lra_assert (goal_alt_number >= 0);
3366 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3368 if (lra_dump_file != NULL)
3370 const char *p;
3372 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3373 goal_alt_number, INSN_UID (curr_insn));
3374 for (i = 0; i < n_operands; i++)
3376 p = (curr_static_id->operand_alternative
3377 [goal_alt_number * n_operands + i].constraint);
3378 if (*p == '\0')
3379 continue;
3380 fprintf (lra_dump_file, " (%d) ", i);
3381 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3382 fputc (*p, lra_dump_file);
3384 if (INSN_CODE (curr_insn) >= 0
3385 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3386 fprintf (lra_dump_file, " {%s}", p);
3387 if (curr_id->sp_offset != 0)
3388 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3389 curr_id->sp_offset);
3390 fprintf (lra_dump_file, "\n");
3393 /* Right now, for any pair of operands I and J that are required to
3394 match, with J < I, goal_alt_matches[I] is J. Add I to
3395 goal_alt_matched[J]. */
3397 for (i = 0; i < n_operands; i++)
3398 if ((j = goal_alt_matches[i]) >= 0)
3400 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3402 /* We allow matching one output operand and several input
3403 operands. */
3404 lra_assert (k == 0
3405 || (curr_static_id->operand[j].type == OP_OUT
3406 && curr_static_id->operand[i].type == OP_IN
3407 && (curr_static_id->operand
3408 [goal_alt_matched[j][0]].type == OP_IN)));
3409 goal_alt_matched[j][k] = i;
3410 goal_alt_matched[j][k + 1] = -1;
3413 for (i = 0; i < n_operands; i++)
3414 goal_alt_win[i] |= goal_alt_match_win[i];
3416 /* Any constants that aren't allowed and can't be reloaded into
3417 registers are here changed into memory references. */
3418 for (i = 0; i < n_operands; i++)
3419 if (goal_alt_win[i])
3421 int regno;
3422 enum reg_class new_class;
3423 rtx reg = *curr_id->operand_loc[i];
3425 if (GET_CODE (reg) == SUBREG)
3426 reg = SUBREG_REG (reg);
3428 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3430 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3432 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3434 lra_assert (ok_p);
3435 lra_change_class (regno, new_class, " Change to", true);
3439 else
3441 const char *constraint;
3442 char c;
3443 rtx op = *curr_id->operand_loc[i];
3444 rtx subreg = NULL_RTX;
3445 enum machine_mode mode = curr_operand_mode[i];
3447 if (GET_CODE (op) == SUBREG)
3449 subreg = op;
3450 op = SUBREG_REG (op);
3451 mode = GET_MODE (op);
3454 if (CONST_POOL_OK_P (mode, op)
3455 && ((targetm.preferred_reload_class
3456 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3457 || no_input_reloads_p))
3459 rtx tem = force_const_mem (mode, op);
3461 change_p = true;
3462 if (subreg != NULL_RTX)
3463 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3465 *curr_id->operand_loc[i] = tem;
3466 lra_update_dup (curr_id, i);
3467 process_address (i, &before, &after);
3469 /* If the alternative accepts constant pool refs directly
3470 there will be no reload needed at all. */
3471 if (subreg != NULL_RTX)
3472 continue;
3473 /* Skip alternatives before the one requested. */
3474 constraint = (curr_static_id->operand_alternative
3475 [goal_alt_number * n_operands + i].constraint);
3476 for (;
3477 (c = *constraint) && c != ',' && c != '#';
3478 constraint += CONSTRAINT_LEN (c, constraint))
3480 if (c == TARGET_MEM_CONSTRAINT || c == 'o')
3481 break;
3482 #ifdef EXTRA_CONSTRAINT_STR
3483 if (EXTRA_MEMORY_CONSTRAINT (c, constraint)
3484 && EXTRA_CONSTRAINT_STR (tem, c, constraint))
3485 break;
3486 #endif
3488 if (c == '\0' || c == ',' || c == '#')
3489 continue;
3491 goal_alt_win[i] = true;
3495 for (i = 0; i < n_operands; i++)
3497 int regno;
3498 bool optional_p = false;
3499 rtx old, new_reg;
3500 rtx op = *curr_id->operand_loc[i];
3502 if (goal_alt_win[i])
3504 if (goal_alt[i] == NO_REGS
3505 && REG_P (op)
3506 /* When we assign NO_REGS it means that we will not
3507 assign a hard register to the scratch pseudo by
3508 assigment pass and the scratch pseudo will be
3509 spilled. Spilled scratch pseudos are transformed
3510 back to scratches at the LRA end. */
3511 && lra_former_scratch_operand_p (curr_insn, i))
3513 int regno = REGNO (op);
3514 lra_change_class (regno, NO_REGS, " Change to", true);
3515 if (lra_get_regno_hard_regno (regno) >= 0)
3516 /* We don't have to mark all insn affected by the
3517 spilled pseudo as there is only one such insn, the
3518 current one. */
3519 reg_renumber[regno] = -1;
3521 /* We can do an optional reload. If the pseudo got a hard
3522 reg, we might improve the code through inheritance. If
3523 it does not get a hard register we coalesce memory/memory
3524 moves later. Ignore move insns to avoid cycling. */
3525 if (! lra_simple_p
3526 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3527 && goal_alt[i] != NO_REGS && REG_P (op)
3528 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3529 && regno < new_regno_start
3530 && ! lra_former_scratch_p (regno)
3531 && reg_renumber[regno] < 0
3532 && (curr_insn_set == NULL_RTX
3533 || !((REG_P (SET_SRC (curr_insn_set))
3534 || MEM_P (SET_SRC (curr_insn_set))
3535 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3536 && (REG_P (SET_DEST (curr_insn_set))
3537 || MEM_P (SET_DEST (curr_insn_set))
3538 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3539 optional_p = true;
3540 else
3541 continue;
3544 /* Operands that match previous ones have already been handled. */
3545 if (goal_alt_matches[i] >= 0)
3546 continue;
3548 /* We should not have an operand with a non-offsettable address
3549 appearing where an offsettable address will do. It also may
3550 be a case when the address should be special in other words
3551 not a general one (e.g. it needs no index reg). */
3552 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3554 enum reg_class rclass;
3555 rtx *loc = &XEXP (op, 0);
3556 enum rtx_code code = GET_CODE (*loc);
3558 push_to_sequence (before);
3559 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3560 MEM, SCRATCH);
3561 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3562 new_reg = emit_inc (rclass, *loc, *loc,
3563 /* This value does not matter for MODIFY. */
3564 GET_MODE_SIZE (GET_MODE (op)));
3565 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass,
3566 "offsetable address", &new_reg))
3567 lra_emit_move (new_reg, *loc);
3568 before = get_insns ();
3569 end_sequence ();
3570 *loc = new_reg;
3571 lra_update_dup (curr_id, i);
3573 else if (goal_alt_matched[i][0] == -1)
3575 enum machine_mode mode;
3576 rtx reg, *loc;
3577 int hard_regno, byte;
3578 enum op_type type = curr_static_id->operand[i].type;
3580 loc = curr_id->operand_loc[i];
3581 mode = curr_operand_mode[i];
3582 if (GET_CODE (*loc) == SUBREG)
3584 reg = SUBREG_REG (*loc);
3585 byte = SUBREG_BYTE (*loc);
3586 if (REG_P (reg)
3587 /* Strict_low_part requires reload the register not
3588 the sub-register. */
3589 && (curr_static_id->operand[i].strict_low
3590 || (GET_MODE_SIZE (mode)
3591 <= GET_MODE_SIZE (GET_MODE (reg))
3592 && (hard_regno
3593 = get_try_hard_regno (REGNO (reg))) >= 0
3594 && (simplify_subreg_regno
3595 (hard_regno,
3596 GET_MODE (reg), byte, mode) < 0)
3597 && (goal_alt[i] == NO_REGS
3598 || (simplify_subreg_regno
3599 (ira_class_hard_regs[goal_alt[i]][0],
3600 GET_MODE (reg), byte, mode) >= 0)))))
3602 loc = &SUBREG_REG (*loc);
3603 mode = GET_MODE (*loc);
3606 old = *loc;
3607 if (get_reload_reg (type, mode, old, goal_alt[i], "", &new_reg)
3608 && type != OP_OUT)
3610 push_to_sequence (before);
3611 lra_emit_move (new_reg, old);
3612 before = get_insns ();
3613 end_sequence ();
3615 *loc = new_reg;
3616 if (type != OP_IN
3617 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3619 start_sequence ();
3620 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3621 emit_insn (after);
3622 after = get_insns ();
3623 end_sequence ();
3624 *loc = new_reg;
3626 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3627 if (goal_alt_dont_inherit_ops[j] == i)
3629 lra_set_regno_unique_value (REGNO (new_reg));
3630 break;
3632 lra_update_dup (curr_id, i);
3634 else if (curr_static_id->operand[i].type == OP_IN
3635 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3636 == OP_OUT))
3638 /* generate reloads for input and matched outputs. */
3639 match_inputs[0] = i;
3640 match_inputs[1] = -1;
3641 match_reload (goal_alt_matched[i][0], match_inputs,
3642 goal_alt[i], &before, &after);
3644 else if (curr_static_id->operand[i].type == OP_OUT
3645 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3646 == OP_IN))
3647 /* Generate reloads for output and matched inputs. */
3648 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3649 else if (curr_static_id->operand[i].type == OP_IN
3650 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3651 == OP_IN))
3653 /* Generate reloads for matched inputs. */
3654 match_inputs[0] = i;
3655 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3656 match_inputs[j + 1] = k;
3657 match_inputs[j + 1] = -1;
3658 match_reload (-1, match_inputs, goal_alt[i], &before, &after);
3660 else
3661 /* We must generate code in any case when function
3662 process_alt_operands decides that it is possible. */
3663 gcc_unreachable ();
3664 if (optional_p)
3666 lra_assert (REG_P (op));
3667 regno = REGNO (op);
3668 op = *curr_id->operand_loc[i]; /* Substitution. */
3669 if (GET_CODE (op) == SUBREG)
3670 op = SUBREG_REG (op);
3671 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3672 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3673 lra_reg_info[REGNO (op)].restore_regno = regno;
3674 if (lra_dump_file != NULL)
3675 fprintf (lra_dump_file,
3676 " Making reload reg %d for reg %d optional\n",
3677 REGNO (op), regno);
3680 if (before != NULL_RTX || after != NULL_RTX
3681 || max_regno_before != max_reg_num ())
3682 change_p = true;
3683 if (change_p)
3685 lra_update_operator_dups (curr_id);
3686 /* Something changes -- process the insn. */
3687 lra_update_insn_regno_info (curr_insn);
3689 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3690 return change_p;
3693 /* Return true if X is in LIST. */
3694 static bool
3695 in_list_p (rtx x, rtx list)
3697 for (; list != NULL_RTX; list = XEXP (list, 1))
3698 if (XEXP (list, 0) == x)
3699 return true;
3700 return false;
3703 /* Return true if X contains an allocatable hard register (if
3704 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3705 static bool
3706 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3708 int i, j;
3709 const char *fmt;
3710 enum rtx_code code;
3712 code = GET_CODE (x);
3713 if (REG_P (x))
3715 int regno = REGNO (x);
3716 HARD_REG_SET alloc_regs;
3718 if (hard_reg_p)
3720 if (regno >= FIRST_PSEUDO_REGISTER)
3721 regno = lra_get_regno_hard_regno (regno);
3722 if (regno < 0)
3723 return false;
3724 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3725 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3727 else
3729 if (regno < FIRST_PSEUDO_REGISTER)
3730 return false;
3731 if (! spilled_p)
3732 return true;
3733 return lra_get_regno_hard_regno (regno) < 0;
3736 fmt = GET_RTX_FORMAT (code);
3737 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3739 if (fmt[i] == 'e')
3741 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3742 return true;
3744 else if (fmt[i] == 'E')
3746 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3747 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3748 return true;
3751 return false;
3754 /* Process all regs in location *LOC and change them on equivalent
3755 substitution. Return true if any change was done. */
3756 static bool
3757 loc_equivalence_change_p (rtx *loc)
3759 rtx subst, reg, x = *loc;
3760 bool result = false;
3761 enum rtx_code code = GET_CODE (x);
3762 const char *fmt;
3763 int i, j;
3765 if (code == SUBREG)
3767 reg = SUBREG_REG (x);
3768 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
3769 && GET_MODE (subst) == VOIDmode)
3771 /* We cannot reload debug location. Simplify subreg here
3772 while we know the inner mode. */
3773 *loc = simplify_gen_subreg (GET_MODE (x), subst,
3774 GET_MODE (reg), SUBREG_BYTE (x));
3775 return true;
3778 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
3780 *loc = subst;
3781 return true;
3784 /* Scan all the operand sub-expressions. */
3785 fmt = GET_RTX_FORMAT (code);
3786 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3788 if (fmt[i] == 'e')
3789 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
3790 else if (fmt[i] == 'E')
3791 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3792 result
3793 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
3795 return result;
3798 /* Similar to loc_equivalence_change_p, but for use as
3799 simplify_replace_fn_rtx callback. DATA is insn for which the
3800 elimination is done. If it null we don't do the elimination. */
3801 static rtx
3802 loc_equivalence_callback (rtx loc, const_rtx, void *data)
3804 if (!REG_P (loc))
3805 return NULL_RTX;
3807 rtx subst = (data == NULL
3808 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx) data));
3809 if (subst != loc)
3810 return subst;
3812 return NULL_RTX;
3815 /* Maximum number of generated reload insns per an insn. It is for
3816 preventing this pass cycling in a bug case. */
3817 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
3819 /* The current iteration number of this LRA pass. */
3820 int lra_constraint_iter;
3822 /* The current iteration number of this LRA pass after the last spill
3823 pass. */
3824 int lra_constraint_iter_after_spill;
3826 /* True if we substituted equiv which needs checking register
3827 allocation correctness because the equivalent value contains
3828 allocatable hard registers or when we restore multi-register
3829 pseudo. */
3830 bool lra_risky_transformations_p;
3832 /* Return true if REGNO is referenced in more than one block. */
3833 static bool
3834 multi_block_pseudo_p (int regno)
3836 basic_block bb = NULL;
3837 unsigned int uid;
3838 bitmap_iterator bi;
3840 if (regno < FIRST_PSEUDO_REGISTER)
3841 return false;
3843 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
3844 if (bb == NULL)
3845 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
3846 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
3847 return true;
3848 return false;
3851 /* Return true if LIST contains a deleted insn. */
3852 static bool
3853 contains_deleted_insn_p (rtx list)
3855 for (; list != NULL_RTX; list = XEXP (list, 1))
3856 if (NOTE_P (XEXP (list, 0))
3857 && NOTE_KIND (XEXP (list, 0)) == NOTE_INSN_DELETED)
3858 return true;
3859 return false;
3862 /* Return true if X contains a pseudo dying in INSN. */
3863 static bool
3864 dead_pseudo_p (rtx x, rtx insn)
3866 int i, j;
3867 const char *fmt;
3868 enum rtx_code code;
3870 if (REG_P (x))
3871 return (insn != NULL_RTX
3872 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
3873 code = GET_CODE (x);
3874 fmt = GET_RTX_FORMAT (code);
3875 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3877 if (fmt[i] == 'e')
3879 if (dead_pseudo_p (XEXP (x, i), insn))
3880 return true;
3882 else if (fmt[i] == 'E')
3884 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3885 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
3886 return true;
3889 return false;
3892 /* Return true if INSN contains a dying pseudo in INSN right hand
3893 side. */
3894 static bool
3895 insn_rhs_dead_pseudo_p (rtx insn)
3897 rtx set = single_set (insn);
3899 gcc_assert (set != NULL);
3900 return dead_pseudo_p (SET_SRC (set), insn);
3903 /* Return true if any init insn of REGNO contains a dying pseudo in
3904 insn right hand side. */
3905 static bool
3906 init_insn_rhs_dead_pseudo_p (int regno)
3908 rtx insns = ira_reg_equiv[regno].init_insns;
3910 if (insns == NULL)
3911 return false;
3912 if (INSN_P (insns))
3913 return insn_rhs_dead_pseudo_p (insns);
3914 for (; insns != NULL_RTX; insns = XEXP (insns, 1))
3915 if (insn_rhs_dead_pseudo_p (XEXP (insns, 0)))
3916 return true;
3917 return false;
3920 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
3921 reverse only if we have one init insn with given REGNO as a
3922 source. */
3923 static bool
3924 reverse_equiv_p (int regno)
3926 rtx insns, set;
3928 if ((insns = ira_reg_equiv[regno].init_insns) == NULL_RTX)
3929 return false;
3930 if (! INSN_P (XEXP (insns, 0))
3931 || XEXP (insns, 1) != NULL_RTX)
3932 return false;
3933 if ((set = single_set (XEXP (insns, 0))) == NULL_RTX)
3934 return false;
3935 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
3938 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
3939 call this function only for non-reverse equivalence. */
3940 static bool
3941 contains_reloaded_insn_p (int regno)
3943 rtx set;
3944 rtx list = ira_reg_equiv[regno].init_insns;
3946 for (; list != NULL_RTX; list = XEXP (list, 1))
3947 if ((set = single_set (XEXP (list, 0))) == NULL_RTX
3948 || ! REG_P (SET_DEST (set))
3949 || (int) REGNO (SET_DEST (set)) != regno)
3950 return true;
3951 return false;
3954 /* Entry function of LRA constraint pass. Return true if the
3955 constraint pass did change the code. */
3956 bool
3957 lra_constraints (bool first_p)
3959 bool changed_p;
3960 int i, hard_regno, new_insns_num;
3961 unsigned int min_len, new_min_len, uid;
3962 rtx set, x, reg, dest_reg;
3963 basic_block last_bb;
3964 bitmap_head equiv_insn_bitmap;
3965 bitmap_iterator bi;
3967 lra_constraint_iter++;
3968 if (lra_dump_file != NULL)
3969 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
3970 lra_constraint_iter);
3971 lra_constraint_iter_after_spill++;
3972 if (lra_constraint_iter_after_spill > LRA_MAX_CONSTRAINT_ITERATION_NUMBER)
3973 internal_error
3974 ("Maximum number of LRA constraint passes is achieved (%d)\n",
3975 LRA_MAX_CONSTRAINT_ITERATION_NUMBER);
3976 changed_p = false;
3977 lra_risky_transformations_p = false;
3978 new_insn_uid_start = get_max_uid ();
3979 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
3980 /* Mark used hard regs for target stack size calulations. */
3981 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
3982 if (lra_reg_info[i].nrefs != 0
3983 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
3985 int j, nregs;
3987 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
3988 for (j = 0; j < nregs; j++)
3989 df_set_regs_ever_live (hard_regno + j, true);
3991 /* Do elimination before the equivalence processing as we can spill
3992 some pseudos during elimination. */
3993 lra_eliminate (false, first_p);
3994 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
3995 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
3996 if (lra_reg_info[i].nrefs != 0)
3998 ira_reg_equiv[i].profitable_p = true;
3999 reg = regno_reg_rtx[i];
4000 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4002 bool pseudo_p = contains_reg_p (x, false, false);
4004 /* After RTL transformation, we can not guarantee that
4005 pseudo in the substitution was not reloaded which might
4006 make equivalence invalid. For example, in reverse
4007 equiv of p0
4009 p0 <- ...
4011 equiv_mem <- p0
4013 the memory address register was reloaded before the 2nd
4014 insn. */
4015 if ((! first_p && pseudo_p)
4016 /* We don't use DF for compilation speed sake. So it
4017 is problematic to update live info when we use an
4018 equivalence containing pseudos in more than one
4019 BB. */
4020 || (pseudo_p && multi_block_pseudo_p (i))
4021 /* If an init insn was deleted for some reason, cancel
4022 the equiv. We could update the equiv insns after
4023 transformations including an equiv insn deletion
4024 but it is not worthy as such cases are extremely
4025 rare. */
4026 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4027 /* If it is not a reverse equivalence, we check that a
4028 pseudo in rhs of the init insn is not dying in the
4029 insn. Otherwise, the live info at the beginning of
4030 the corresponding BB might be wrong after we
4031 removed the insn. When the equiv can be a
4032 constant, the right hand side of the init insn can
4033 be a pseudo. */
4034 || (! reverse_equiv_p (i)
4035 && (init_insn_rhs_dead_pseudo_p (i)
4036 /* If we reloaded the pseudo in an equivalence
4037 init insn, we can not remove the equiv init
4038 insns and the init insns might write into
4039 const memory in this case. */
4040 || contains_reloaded_insn_p (i)))
4041 /* Prevent access beyond equivalent memory for
4042 paradoxical subregs. */
4043 || (MEM_P (x)
4044 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4045 > GET_MODE_SIZE (GET_MODE (x)))))
4046 ira_reg_equiv[i].defined_p = false;
4047 if (contains_reg_p (x, false, true))
4048 ira_reg_equiv[i].profitable_p = false;
4049 if (get_equiv (reg) != reg)
4050 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4053 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4054 update_equiv (i);
4055 /* We should add all insns containing pseudos which should be
4056 substituted by their equivalences. */
4057 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4058 lra_push_insn_by_uid (uid);
4059 min_len = lra_insn_stack_length ();
4060 new_insns_num = 0;
4061 last_bb = NULL;
4062 changed_p = false;
4063 while ((new_min_len = lra_insn_stack_length ()) != 0)
4065 curr_insn = lra_pop_insn ();
4066 --new_min_len;
4067 curr_bb = BLOCK_FOR_INSN (curr_insn);
4068 if (curr_bb != last_bb)
4070 last_bb = curr_bb;
4071 bb_reload_num = lra_curr_reload_num;
4073 if (min_len > new_min_len)
4075 min_len = new_min_len;
4076 new_insns_num = 0;
4078 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4079 internal_error
4080 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4081 MAX_RELOAD_INSNS_NUMBER);
4082 new_insns_num++;
4083 if (DEBUG_INSN_P (curr_insn))
4085 /* We need to check equivalence in debug insn and change
4086 pseudo to the equivalent value if necessary. */
4087 curr_id = lra_get_insn_recog_data (curr_insn);
4088 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4090 rtx old = *curr_id->operand_loc[0];
4091 *curr_id->operand_loc[0]
4092 = simplify_replace_fn_rtx (old, NULL_RTX,
4093 loc_equivalence_callback, curr_insn);
4094 if (old != *curr_id->operand_loc[0])
4096 lra_update_insn_regno_info (curr_insn);
4097 changed_p = true;
4101 else if (INSN_P (curr_insn))
4103 if ((set = single_set (curr_insn)) != NULL_RTX)
4105 dest_reg = SET_DEST (set);
4106 /* The equivalence pseudo could be set up as SUBREG in a
4107 case when it is a call restore insn in a mode
4108 different from the pseudo mode. */
4109 if (GET_CODE (dest_reg) == SUBREG)
4110 dest_reg = SUBREG_REG (dest_reg);
4111 if ((REG_P (dest_reg)
4112 && (x = get_equiv (dest_reg)) != dest_reg
4113 /* Remove insns which set up a pseudo whose value
4114 can not be changed. Such insns might be not in
4115 init_insns because we don't update equiv data
4116 during insn transformations.
4118 As an example, let suppose that a pseudo got
4119 hard register and on the 1st pass was not
4120 changed to equivalent constant. We generate an
4121 additional insn setting up the pseudo because of
4122 secondary memory movement. Then the pseudo is
4123 spilled and we use the equiv constant. In this
4124 case we should remove the additional insn and
4125 this insn is not init_insns list. */
4126 && (! MEM_P (x) || MEM_READONLY_P (x)
4127 /* Check that this is actually an insn setting
4128 up the equivalence. */
4129 || in_list_p (curr_insn,
4130 ira_reg_equiv
4131 [REGNO (dest_reg)].init_insns)))
4132 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4133 && in_list_p (curr_insn,
4134 ira_reg_equiv
4135 [REGNO (SET_SRC (set))].init_insns)))
4137 /* This is equiv init insn of pseudo which did not get a
4138 hard register -- remove the insn. */
4139 if (lra_dump_file != NULL)
4141 fprintf (lra_dump_file,
4142 " Removing equiv init insn %i (freq=%d)\n",
4143 INSN_UID (curr_insn),
4144 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4145 dump_insn_slim (lra_dump_file, curr_insn);
4147 if (contains_reg_p (x, true, false))
4148 lra_risky_transformations_p = true;
4149 lra_set_insn_deleted (curr_insn);
4150 continue;
4153 curr_id = lra_get_insn_recog_data (curr_insn);
4154 curr_static_id = curr_id->insn_static_data;
4155 init_curr_insn_input_reloads ();
4156 init_curr_operand_mode ();
4157 if (curr_insn_transform ())
4158 changed_p = true;
4159 /* Check non-transformed insns too for equiv change as USE
4160 or CLOBBER don't need reloads but can contain pseudos
4161 being changed on their equivalences. */
4162 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4163 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4165 lra_update_insn_regno_info (curr_insn);
4166 changed_p = true;
4170 bitmap_clear (&equiv_insn_bitmap);
4171 /* If we used a new hard regno, changed_p should be true because the
4172 hard reg is assigned to a new pseudo. */
4173 #ifdef ENABLE_CHECKING
4174 if (! changed_p)
4176 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4177 if (lra_reg_info[i].nrefs != 0
4178 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4180 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4182 for (j = 0; j < nregs; j++)
4183 lra_assert (df_regs_ever_live_p (hard_regno + j));
4186 #endif
4187 return changed_p;
4190 /* Initiate the LRA constraint pass. It is done once per
4191 function. */
4192 void
4193 lra_constraints_init (void)
4197 /* Finalize the LRA constraint pass. It is done once per
4198 function. */
4199 void
4200 lra_constraints_finish (void)
4206 /* This page contains code to do inheritance/split
4207 transformations. */
4209 /* Number of reloads passed so far in current EBB. */
4210 static int reloads_num;
4212 /* Number of calls passed so far in current EBB. */
4213 static int calls_num;
4215 /* Current reload pseudo check for validity of elements in
4216 USAGE_INSNS. */
4217 static int curr_usage_insns_check;
4219 /* Info about last usage of registers in EBB to do inheritance/split
4220 transformation. Inheritance transformation is done from a spilled
4221 pseudo and split transformations from a hard register or a pseudo
4222 assigned to a hard register. */
4223 struct usage_insns
4225 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4226 value INSNS is valid. The insns is chain of optional debug insns
4227 and a finishing non-debug insn using the corresponding reg. The
4228 value is also used to mark the registers which are set up in the
4229 current insn. The negated insn uid is used for this. */
4230 int check;
4231 /* Value of global reloads_num at the last insn in INSNS. */
4232 int reloads_num;
4233 /* Value of global reloads_nums at the last insn in INSNS. */
4234 int calls_num;
4235 /* It can be true only for splitting. And it means that the restore
4236 insn should be put after insn given by the following member. */
4237 bool after_p;
4238 /* Next insns in the current EBB which use the original reg and the
4239 original reg value is not changed between the current insn and
4240 the next insns. In order words, e.g. for inheritance, if we need
4241 to use the original reg value again in the next insns we can try
4242 to use the value in a hard register from a reload insn of the
4243 current insn. */
4244 rtx insns;
4247 /* Map: regno -> corresponding pseudo usage insns. */
4248 static struct usage_insns *usage_insns;
4250 static void
4251 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4253 usage_insns[regno].check = curr_usage_insns_check;
4254 usage_insns[regno].insns = insn;
4255 usage_insns[regno].reloads_num = reloads_num;
4256 usage_insns[regno].calls_num = calls_num;
4257 usage_insns[regno].after_p = after_p;
4260 /* The function is used to form list REGNO usages which consists of
4261 optional debug insns finished by a non-debug insn using REGNO.
4262 RELOADS_NUM is current number of reload insns processed so far. */
4263 static void
4264 add_next_usage_insn (int regno, rtx insn, int reloads_num)
4266 rtx next_usage_insns;
4268 if (usage_insns[regno].check == curr_usage_insns_check
4269 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4270 && DEBUG_INSN_P (insn))
4272 /* Check that we did not add the debug insn yet. */
4273 if (next_usage_insns != insn
4274 && (GET_CODE (next_usage_insns) != INSN_LIST
4275 || XEXP (next_usage_insns, 0) != insn))
4276 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4277 next_usage_insns);
4279 else if (NONDEBUG_INSN_P (insn))
4280 setup_next_usage_insn (regno, insn, reloads_num, false);
4281 else
4282 usage_insns[regno].check = 0;
4285 /* Replace all references to register OLD_REGNO in *LOC with pseudo
4286 register NEW_REG. Return true if any change was made. */
4287 static bool
4288 substitute_pseudo (rtx *loc, int old_regno, rtx new_reg)
4290 rtx x = *loc;
4291 bool result = false;
4292 enum rtx_code code;
4293 const char *fmt;
4294 int i, j;
4296 if (x == NULL_RTX)
4297 return false;
4299 code = GET_CODE (x);
4300 if (code == REG && (int) REGNO (x) == old_regno)
4302 enum machine_mode mode = GET_MODE (*loc);
4303 enum machine_mode inner_mode = GET_MODE (new_reg);
4305 if (mode != inner_mode)
4307 if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
4308 || ! SCALAR_INT_MODE_P (inner_mode))
4309 new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
4310 else
4311 new_reg = gen_lowpart_SUBREG (mode, new_reg);
4313 *loc = new_reg;
4314 return true;
4317 /* Scan all the operand sub-expressions. */
4318 fmt = GET_RTX_FORMAT (code);
4319 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4321 if (fmt[i] == 'e')
4323 if (substitute_pseudo (&XEXP (x, i), old_regno, new_reg))
4324 result = true;
4326 else if (fmt[i] == 'E')
4328 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4329 if (substitute_pseudo (&XVECEXP (x, i, j), old_regno, new_reg))
4330 result = true;
4333 return result;
4336 /* Return first non-debug insn in list USAGE_INSNS. */
4337 static rtx
4338 skip_usage_debug_insns (rtx usage_insns)
4340 rtx insn;
4342 /* Skip debug insns. */
4343 for (insn = usage_insns;
4344 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4345 insn = XEXP (insn, 1))
4347 return insn;
4350 /* Return true if we need secondary memory moves for insn in
4351 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4352 into the insn. */
4353 static bool
4354 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4355 rtx usage_insns ATTRIBUTE_UNUSED)
4357 #ifndef SECONDARY_MEMORY_NEEDED
4358 return false;
4359 #else
4360 rtx insn, set, dest;
4361 enum reg_class cl;
4363 if (inher_cl == ALL_REGS
4364 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4365 return false;
4366 lra_assert (INSN_P (insn));
4367 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4368 return false;
4369 dest = SET_DEST (set);
4370 if (! REG_P (dest))
4371 return false;
4372 lra_assert (inher_cl != NO_REGS);
4373 cl = get_reg_class (REGNO (dest));
4374 return (cl != NO_REGS && cl != ALL_REGS
4375 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4376 #endif
4379 /* Registers involved in inheritance/split in the current EBB
4380 (inheritance/split pseudos and original registers). */
4381 static bitmap_head check_only_regs;
4383 /* Do inheritance transformations for insn INSN, which defines (if
4384 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4385 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4386 form as the "insns" field of usage_insns. Return true if we
4387 succeed in such transformation.
4389 The transformations look like:
4391 p <- ... i <- ...
4392 ... p <- i (new insn)
4393 ... =>
4394 <- ... p ... <- ... i ...
4396 ... i <- p (new insn)
4397 <- ... p ... <- ... i ...
4398 ... =>
4399 <- ... p ... <- ... i ...
4400 where p is a spilled original pseudo and i is a new inheritance pseudo.
4403 The inheritance pseudo has the smallest class of two classes CL and
4404 class of ORIGINAL REGNO. */
4405 static bool
4406 inherit_reload_reg (bool def_p, int original_regno,
4407 enum reg_class cl, rtx insn, rtx next_usage_insns)
4409 if (optimize_function_for_size_p (cfun))
4410 return false;
4412 enum reg_class rclass = lra_get_allocno_class (original_regno);
4413 rtx original_reg = regno_reg_rtx[original_regno];
4414 rtx new_reg, new_insns, usage_insn;
4416 lra_assert (! usage_insns[original_regno].after_p);
4417 if (lra_dump_file != NULL)
4418 fprintf (lra_dump_file,
4419 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4420 if (! ira_reg_classes_intersect_p[cl][rclass])
4422 if (lra_dump_file != NULL)
4424 fprintf (lra_dump_file,
4425 " Rejecting inheritance for %d "
4426 "because of disjoint classes %s and %s\n",
4427 original_regno, reg_class_names[cl],
4428 reg_class_names[rclass]);
4429 fprintf (lra_dump_file,
4430 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4432 return false;
4434 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4435 /* We don't use a subset of two classes because it can be
4436 NO_REGS. This transformation is still profitable in most
4437 cases even if the classes are not intersected as register
4438 move is probably cheaper than a memory load. */
4439 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4441 if (lra_dump_file != NULL)
4442 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4443 reg_class_names[cl], reg_class_names[rclass]);
4445 rclass = cl;
4447 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4449 /* Reject inheritance resulting in secondary memory moves.
4450 Otherwise, there is a danger in LRA cycling. Also such
4451 transformation will be unprofitable. */
4452 if (lra_dump_file != NULL)
4454 rtx insn = skip_usage_debug_insns (next_usage_insns);
4455 rtx set = single_set (insn);
4457 lra_assert (set != NULL_RTX);
4459 rtx dest = SET_DEST (set);
4461 lra_assert (REG_P (dest));
4462 fprintf (lra_dump_file,
4463 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4464 "as secondary mem is needed\n",
4465 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4466 original_regno, reg_class_names[rclass]);
4467 fprintf (lra_dump_file,
4468 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4470 return false;
4472 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4473 rclass, "inheritance");
4474 start_sequence ();
4475 if (def_p)
4476 emit_move_insn (original_reg, new_reg);
4477 else
4478 emit_move_insn (new_reg, original_reg);
4479 new_insns = get_insns ();
4480 end_sequence ();
4481 if (NEXT_INSN (new_insns) != NULL_RTX)
4483 if (lra_dump_file != NULL)
4485 fprintf (lra_dump_file,
4486 " Rejecting inheritance %d->%d "
4487 "as it results in 2 or more insns:\n",
4488 original_regno, REGNO (new_reg));
4489 dump_rtl_slim (lra_dump_file, new_insns, NULL_RTX, -1, 0);
4490 fprintf (lra_dump_file,
4491 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4493 return false;
4495 substitute_pseudo (&insn, original_regno, new_reg);
4496 lra_update_insn_regno_info (insn);
4497 if (! def_p)
4498 /* We now have a new usage insn for original regno. */
4499 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4500 if (lra_dump_file != NULL)
4501 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4502 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4503 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4504 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4505 bitmap_set_bit (&check_only_regs, original_regno);
4506 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4507 if (def_p)
4508 lra_process_new_insns (insn, NULL_RTX, new_insns,
4509 "Add original<-inheritance");
4510 else
4511 lra_process_new_insns (insn, new_insns, NULL_RTX,
4512 "Add inheritance<-original");
4513 while (next_usage_insns != NULL_RTX)
4515 if (GET_CODE (next_usage_insns) != INSN_LIST)
4517 usage_insn = next_usage_insns;
4518 lra_assert (NONDEBUG_INSN_P (usage_insn));
4519 next_usage_insns = NULL;
4521 else
4523 usage_insn = XEXP (next_usage_insns, 0);
4524 lra_assert (DEBUG_INSN_P (usage_insn));
4525 next_usage_insns = XEXP (next_usage_insns, 1);
4527 substitute_pseudo (&usage_insn, original_regno, new_reg);
4528 lra_update_insn_regno_info (usage_insn);
4529 if (lra_dump_file != NULL)
4531 fprintf (lra_dump_file,
4532 " Inheritance reuse change %d->%d (bb%d):\n",
4533 original_regno, REGNO (new_reg),
4534 BLOCK_FOR_INSN (usage_insn)->index);
4535 dump_insn_slim (lra_dump_file, usage_insn);
4538 if (lra_dump_file != NULL)
4539 fprintf (lra_dump_file,
4540 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4541 return true;
4544 /* Return true if we need a caller save/restore for pseudo REGNO which
4545 was assigned to a hard register. */
4546 static inline bool
4547 need_for_call_save_p (int regno)
4549 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4550 return (usage_insns[regno].calls_num < calls_num
4551 && (overlaps_hard_reg_set_p
4552 (call_used_reg_set,
4553 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4554 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4555 PSEUDO_REGNO_MODE (regno))));
4558 /* Global registers occurring in the current EBB. */
4559 static bitmap_head ebb_global_regs;
4561 /* Return true if we need a split for hard register REGNO or pseudo
4562 REGNO which was assigned to a hard register.
4563 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4564 used for reloads since the EBB end. It is an approximation of the
4565 used hard registers in the split range. The exact value would
4566 require expensive calculations. If we were aggressive with
4567 splitting because of the approximation, the split pseudo will save
4568 the same hard register assignment and will be removed in the undo
4569 pass. We still need the approximation because too aggressive
4570 splitting would result in too inaccurate cost calculation in the
4571 assignment pass because of too many generated moves which will be
4572 probably removed in the undo pass. */
4573 static inline bool
4574 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4576 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4578 lra_assert (hard_regno >= 0);
4579 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4580 /* Don't split eliminable hard registers, otherwise we can
4581 split hard registers like hard frame pointer, which
4582 lives on BB start/end according to DF-infrastructure,
4583 when there is a pseudo assigned to the register and
4584 living in the same BB. */
4585 && (regno >= FIRST_PSEUDO_REGISTER
4586 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4587 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4588 /* Don't split call clobbered hard regs living through
4589 calls, otherwise we might have a check problem in the
4590 assign sub-pass as in the most cases (exception is a
4591 situation when lra_risky_transformations_p value is
4592 true) the assign pass assumes that all pseudos living
4593 through calls are assigned to call saved hard regs. */
4594 && (regno >= FIRST_PSEUDO_REGISTER
4595 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4596 || usage_insns[regno].calls_num == calls_num)
4597 /* We need at least 2 reloads to make pseudo splitting
4598 profitable. We should provide hard regno splitting in
4599 any case to solve 1st insn scheduling problem when
4600 moving hard register definition up might result in
4601 impossibility to find hard register for reload pseudo of
4602 small register class. */
4603 && (usage_insns[regno].reloads_num
4604 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4605 && (regno < FIRST_PSEUDO_REGISTER
4606 /* For short living pseudos, spilling + inheritance can
4607 be considered a substitution for splitting.
4608 Therefore we do not splitting for local pseudos. It
4609 decreases also aggressiveness of splitting. The
4610 minimal number of references is chosen taking into
4611 account that for 2 references splitting has no sense
4612 as we can just spill the pseudo. */
4613 || (regno >= FIRST_PSEUDO_REGISTER
4614 && lra_reg_info[regno].nrefs > 3
4615 && bitmap_bit_p (&ebb_global_regs, regno))))
4616 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4619 /* Return class for the split pseudo created from original pseudo with
4620 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4621 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4622 results in no secondary memory movements. */
4623 static enum reg_class
4624 choose_split_class (enum reg_class allocno_class,
4625 int hard_regno ATTRIBUTE_UNUSED,
4626 enum machine_mode mode ATTRIBUTE_UNUSED)
4628 #ifndef SECONDARY_MEMORY_NEEDED
4629 return allocno_class;
4630 #else
4631 int i;
4632 enum reg_class cl, best_cl = NO_REGS;
4633 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4634 = REGNO_REG_CLASS (hard_regno);
4636 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4637 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4638 return allocno_class;
4639 for (i = 0;
4640 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4641 i++)
4642 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4643 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4644 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4645 && (best_cl == NO_REGS
4646 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4647 best_cl = cl;
4648 return best_cl;
4649 #endif
4652 /* Do split transformations for insn INSN, which defines or uses
4653 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4654 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4655 "insns" field of usage_insns.
4657 The transformations look like:
4659 p <- ... p <- ...
4660 ... s <- p (new insn -- save)
4661 ... =>
4662 ... p <- s (new insn -- restore)
4663 <- ... p ... <- ... p ...
4665 <- ... p ... <- ... p ...
4666 ... s <- p (new insn -- save)
4667 ... =>
4668 ... p <- s (new insn -- restore)
4669 <- ... p ... <- ... p ...
4671 where p is an original pseudo got a hard register or a hard
4672 register and s is a new split pseudo. The save is put before INSN
4673 if BEFORE_P is true. Return true if we succeed in such
4674 transformation. */
4675 static bool
4676 split_reg (bool before_p, int original_regno, rtx insn, rtx next_usage_insns)
4678 enum reg_class rclass;
4679 rtx original_reg;
4680 int hard_regno, nregs;
4681 rtx new_reg, save, restore, usage_insn;
4682 bool after_p;
4683 bool call_save_p;
4685 if (original_regno < FIRST_PSEUDO_REGISTER)
4687 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4688 hard_regno = original_regno;
4689 call_save_p = false;
4690 nregs = 1;
4692 else
4694 hard_regno = reg_renumber[original_regno];
4695 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4696 rclass = lra_get_allocno_class (original_regno);
4697 original_reg = regno_reg_rtx[original_regno];
4698 call_save_p = need_for_call_save_p (original_regno);
4700 original_reg = regno_reg_rtx[original_regno];
4701 lra_assert (hard_regno >= 0);
4702 if (lra_dump_file != NULL)
4703 fprintf (lra_dump_file,
4704 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4705 if (call_save_p)
4707 enum machine_mode mode = GET_MODE (original_reg);
4709 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4710 hard_regno_nregs[hard_regno][mode],
4711 mode);
4712 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4714 else
4716 rclass = choose_split_class (rclass, hard_regno,
4717 GET_MODE (original_reg));
4718 if (rclass == NO_REGS)
4720 if (lra_dump_file != NULL)
4722 fprintf (lra_dump_file,
4723 " Rejecting split of %d(%s): "
4724 "no good reg class for %d(%s)\n",
4725 original_regno,
4726 reg_class_names[lra_get_allocno_class (original_regno)],
4727 hard_regno,
4728 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4729 fprintf
4730 (lra_dump_file,
4731 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4733 return false;
4735 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4736 rclass, "split");
4737 reg_renumber[REGNO (new_reg)] = hard_regno;
4739 save = emit_spill_move (true, new_reg, original_reg);
4740 if (NEXT_INSN (save) != NULL_RTX)
4742 lra_assert (! call_save_p);
4743 if (lra_dump_file != NULL)
4745 fprintf
4746 (lra_dump_file,
4747 " Rejecting split %d->%d resulting in > 2 %s save insns:\n",
4748 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4749 dump_rtl_slim (lra_dump_file, save, NULL_RTX, -1, 0);
4750 fprintf (lra_dump_file,
4751 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4753 return false;
4755 restore = emit_spill_move (false, new_reg, original_reg);
4756 if (NEXT_INSN (restore) != NULL_RTX)
4758 lra_assert (! call_save_p);
4759 if (lra_dump_file != NULL)
4761 fprintf (lra_dump_file,
4762 " Rejecting split %d->%d "
4763 "resulting in > 2 %s restore insns:\n",
4764 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4765 dump_rtl_slim (lra_dump_file, restore, NULL_RTX, -1, 0);
4766 fprintf (lra_dump_file,
4767 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4769 return false;
4771 after_p = usage_insns[original_regno].after_p;
4772 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4773 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4774 bitmap_set_bit (&check_only_regs, original_regno);
4775 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
4776 for (;;)
4778 if (GET_CODE (next_usage_insns) != INSN_LIST)
4780 usage_insn = next_usage_insns;
4781 break;
4783 usage_insn = XEXP (next_usage_insns, 0);
4784 lra_assert (DEBUG_INSN_P (usage_insn));
4785 next_usage_insns = XEXP (next_usage_insns, 1);
4786 substitute_pseudo (&usage_insn, original_regno, new_reg);
4787 lra_update_insn_regno_info (usage_insn);
4788 if (lra_dump_file != NULL)
4790 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
4791 original_regno, REGNO (new_reg));
4792 dump_insn_slim (lra_dump_file, usage_insn);
4795 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
4796 lra_assert (usage_insn != insn || (after_p && before_p));
4797 lra_process_new_insns (usage_insn, after_p ? NULL_RTX : restore,
4798 after_p ? restore : NULL_RTX,
4799 call_save_p
4800 ? "Add reg<-save" : "Add reg<-split");
4801 lra_process_new_insns (insn, before_p ? save : NULL_RTX,
4802 before_p ? NULL_RTX : save,
4803 call_save_p
4804 ? "Add save<-reg" : "Add split<-reg");
4805 if (nregs > 1)
4806 /* If we are trying to split multi-register. We should check
4807 conflicts on the next assignment sub-pass. IRA can allocate on
4808 sub-register levels, LRA do this on pseudos level right now and
4809 this discrepancy may create allocation conflicts after
4810 splitting. */
4811 lra_risky_transformations_p = true;
4812 if (lra_dump_file != NULL)
4813 fprintf (lra_dump_file,
4814 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4815 return true;
4818 /* Recognize that we need a split transformation for insn INSN, which
4819 defines or uses REGNO in its insn biggest MODE (we use it only if
4820 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
4821 hard registers which might be used for reloads since the EBB end.
4822 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
4823 uid before starting INSN processing. Return true if we succeed in
4824 such transformation. */
4825 static bool
4826 split_if_necessary (int regno, enum machine_mode mode,
4827 HARD_REG_SET potential_reload_hard_regs,
4828 bool before_p, rtx insn, int max_uid)
4830 bool res = false;
4831 int i, nregs = 1;
4832 rtx next_usage_insns;
4834 if (regno < FIRST_PSEUDO_REGISTER)
4835 nregs = hard_regno_nregs[regno][mode];
4836 for (i = 0; i < nregs; i++)
4837 if (usage_insns[regno + i].check == curr_usage_insns_check
4838 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
4839 /* To avoid processing the register twice or more. */
4840 && ((GET_CODE (next_usage_insns) != INSN_LIST
4841 && INSN_UID (next_usage_insns) < max_uid)
4842 || (GET_CODE (next_usage_insns) == INSN_LIST
4843 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
4844 && need_for_split_p (potential_reload_hard_regs, regno + i)
4845 && split_reg (before_p, regno + i, insn, next_usage_insns))
4846 res = true;
4847 return res;
4850 /* Check only registers living at the current program point in the
4851 current EBB. */
4852 static bitmap_head live_regs;
4854 /* Update live info in EBB given by its HEAD and TAIL insns after
4855 inheritance/split transformation. The function removes dead moves
4856 too. */
4857 static void
4858 update_ebb_live_info (rtx head, rtx tail)
4860 unsigned int j;
4861 int i, regno;
4862 bool live_p;
4863 rtx prev_insn, set;
4864 bool remove_p;
4865 basic_block last_bb, prev_bb, curr_bb;
4866 bitmap_iterator bi;
4867 struct lra_insn_reg *reg;
4868 edge e;
4869 edge_iterator ei;
4871 last_bb = BLOCK_FOR_INSN (tail);
4872 prev_bb = NULL;
4873 for (curr_insn = tail;
4874 curr_insn != PREV_INSN (head);
4875 curr_insn = prev_insn)
4877 prev_insn = PREV_INSN (curr_insn);
4878 /* We need to process empty blocks too. They contain
4879 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
4880 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
4881 continue;
4882 curr_bb = BLOCK_FOR_INSN (curr_insn);
4883 if (curr_bb != prev_bb)
4885 if (prev_bb != NULL)
4887 /* Update df_get_live_in (prev_bb): */
4888 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4889 if (bitmap_bit_p (&live_regs, j))
4890 bitmap_set_bit (df_get_live_in (prev_bb), j);
4891 else
4892 bitmap_clear_bit (df_get_live_in (prev_bb), j);
4894 if (curr_bb != last_bb)
4896 /* Update df_get_live_out (curr_bb): */
4897 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4899 live_p = bitmap_bit_p (&live_regs, j);
4900 if (! live_p)
4901 FOR_EACH_EDGE (e, ei, curr_bb->succs)
4902 if (bitmap_bit_p (df_get_live_in (e->dest), j))
4904 live_p = true;
4905 break;
4907 if (live_p)
4908 bitmap_set_bit (df_get_live_out (curr_bb), j);
4909 else
4910 bitmap_clear_bit (df_get_live_out (curr_bb), j);
4913 prev_bb = curr_bb;
4914 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
4916 if (! NONDEBUG_INSN_P (curr_insn))
4917 continue;
4918 curr_id = lra_get_insn_recog_data (curr_insn);
4919 curr_static_id = curr_id->insn_static_data;
4920 remove_p = false;
4921 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
4922 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
4923 && bitmap_bit_p (&check_only_regs, regno)
4924 && ! bitmap_bit_p (&live_regs, regno))
4925 remove_p = true;
4926 /* See which defined values die here. */
4927 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4928 if (reg->type == OP_OUT && ! reg->subreg_p)
4929 bitmap_clear_bit (&live_regs, reg->regno);
4930 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
4931 if (reg->type == OP_OUT && ! reg->subreg_p)
4932 bitmap_clear_bit (&live_regs, reg->regno);
4933 /* Mark each used value as live. */
4934 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4935 if (reg->type != OP_OUT
4936 && bitmap_bit_p (&check_only_regs, reg->regno))
4937 bitmap_set_bit (&live_regs, reg->regno);
4938 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
4939 if (reg->type != OP_OUT
4940 && bitmap_bit_p (&check_only_regs, reg->regno))
4941 bitmap_set_bit (&live_regs, reg->regno);
4942 if (curr_id->arg_hard_regs != NULL)
4943 /* Make argument hard registers live. */
4944 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
4945 if (bitmap_bit_p (&check_only_regs, regno))
4946 bitmap_set_bit (&live_regs, regno);
4947 /* It is quite important to remove dead move insns because it
4948 means removing dead store. We don't need to process them for
4949 constraints. */
4950 if (remove_p)
4952 if (lra_dump_file != NULL)
4954 fprintf (lra_dump_file, " Removing dead insn:\n ");
4955 dump_insn_slim (lra_dump_file, curr_insn);
4957 lra_set_insn_deleted (curr_insn);
4962 /* The structure describes info to do an inheritance for the current
4963 insn. We need to collect such info first before doing the
4964 transformations because the transformations change the insn
4965 internal representation. */
4966 struct to_inherit
4968 /* Original regno. */
4969 int regno;
4970 /* Subsequent insns which can inherit original reg value. */
4971 rtx insns;
4974 /* Array containing all info for doing inheritance from the current
4975 insn. */
4976 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
4978 /* Number elements in the previous array. */
4979 static int to_inherit_num;
4981 /* Add inheritance info REGNO and INSNS. Their meaning is described in
4982 structure to_inherit. */
4983 static void
4984 add_to_inherit (int regno, rtx insns)
4986 int i;
4988 for (i = 0; i < to_inherit_num; i++)
4989 if (to_inherit[i].regno == regno)
4990 return;
4991 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
4992 to_inherit[to_inherit_num].regno = regno;
4993 to_inherit[to_inherit_num++].insns = insns;
4996 /* Return the last non-debug insn in basic block BB, or the block begin
4997 note if none. */
4998 static rtx
4999 get_last_insertion_point (basic_block bb)
5001 rtx insn;
5003 FOR_BB_INSNS_REVERSE (bb, insn)
5004 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5005 return insn;
5006 gcc_unreachable ();
5009 /* Set up RES by registers living on edges FROM except the edge (FROM,
5010 TO) or by registers set up in a jump insn in BB FROM. */
5011 static void
5012 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5014 rtx last;
5015 struct lra_insn_reg *reg;
5016 edge e;
5017 edge_iterator ei;
5019 lra_assert (to != NULL);
5020 bitmap_clear (res);
5021 FOR_EACH_EDGE (e, ei, from->succs)
5022 if (e->dest != to)
5023 bitmap_ior_into (res, df_get_live_in (e->dest));
5024 last = get_last_insertion_point (from);
5025 if (! JUMP_P (last))
5026 return;
5027 curr_id = lra_get_insn_recog_data (last);
5028 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5029 if (reg->type != OP_IN)
5030 bitmap_set_bit (res, reg->regno);
5033 /* Used as a temporary results of some bitmap calculations. */
5034 static bitmap_head temp_bitmap;
5036 /* We split for reloads of small class of hard regs. The following
5037 defines how many hard regs the class should have to be qualified as
5038 small. The code is mostly oriented to x86/x86-64 architecture
5039 where some insns need to use only specific register or pair of
5040 registers and these register can live in RTL explicitly, e.g. for
5041 parameter passing. */
5042 static const int max_small_class_regs_num = 2;
5044 /* Do inheritance/split transformations in EBB starting with HEAD and
5045 finishing on TAIL. We process EBB insns in the reverse order.
5046 Return true if we did any inheritance/split transformation in the
5047 EBB.
5049 We should avoid excessive splitting which results in worse code
5050 because of inaccurate cost calculations for spilling new split
5051 pseudos in such case. To achieve this we do splitting only if
5052 register pressure is high in given basic block and there are reload
5053 pseudos requiring hard registers. We could do more register
5054 pressure calculations at any given program point to avoid necessary
5055 splitting even more but it is to expensive and the current approach
5056 works well enough. */
5057 static bool
5058 inherit_in_ebb (rtx head, rtx tail)
5060 int i, src_regno, dst_regno, nregs;
5061 bool change_p, succ_p, update_reloads_num_p;
5062 rtx prev_insn, next_usage_insns, set, last_insn;
5063 enum reg_class cl;
5064 struct lra_insn_reg *reg;
5065 basic_block last_processed_bb, curr_bb = NULL;
5066 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5067 bitmap to_process;
5068 unsigned int j;
5069 bitmap_iterator bi;
5070 bool head_p, after_p;
5072 change_p = false;
5073 curr_usage_insns_check++;
5074 reloads_num = calls_num = 0;
5075 bitmap_clear (&check_only_regs);
5076 last_processed_bb = NULL;
5077 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5078 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5079 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5080 /* We don't process new insns generated in the loop. */
5081 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5083 prev_insn = PREV_INSN (curr_insn);
5084 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5085 curr_bb = BLOCK_FOR_INSN (curr_insn);
5086 if (last_processed_bb != curr_bb)
5088 /* We are at the end of BB. Add qualified living
5089 pseudos for potential splitting. */
5090 to_process = df_get_live_out (curr_bb);
5091 if (last_processed_bb != NULL)
5093 /* We are somewhere in the middle of EBB. */
5094 get_live_on_other_edges (curr_bb, last_processed_bb,
5095 &temp_bitmap);
5096 to_process = &temp_bitmap;
5098 last_processed_bb = curr_bb;
5099 last_insn = get_last_insertion_point (curr_bb);
5100 after_p = (! JUMP_P (last_insn)
5101 && (! CALL_P (last_insn)
5102 || (find_reg_note (last_insn,
5103 REG_NORETURN, NULL_RTX) == NULL_RTX
5104 && ! SIBLING_CALL_P (last_insn))));
5105 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5106 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5108 if ((int) j >= lra_constraint_new_regno_start)
5109 break;
5110 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5112 if (j < FIRST_PSEUDO_REGISTER)
5113 SET_HARD_REG_BIT (live_hard_regs, j);
5114 else
5115 add_to_hard_reg_set (&live_hard_regs,
5116 PSEUDO_REGNO_MODE (j),
5117 reg_renumber[j]);
5118 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5122 src_regno = dst_regno = -1;
5123 if (NONDEBUG_INSN_P (curr_insn)
5124 && (set = single_set (curr_insn)) != NULL_RTX
5125 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5127 src_regno = REGNO (SET_SRC (set));
5128 dst_regno = REGNO (SET_DEST (set));
5130 update_reloads_num_p = true;
5131 if (src_regno < lra_constraint_new_regno_start
5132 && src_regno >= FIRST_PSEUDO_REGISTER
5133 && reg_renumber[src_regno] < 0
5134 && dst_regno >= lra_constraint_new_regno_start
5135 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5137 /* 'reload_pseudo <- original_pseudo'. */
5138 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5139 reloads_num++;
5140 update_reloads_num_p = false;
5141 succ_p = false;
5142 if (usage_insns[src_regno].check == curr_usage_insns_check
5143 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5144 succ_p = inherit_reload_reg (false, src_regno, cl,
5145 curr_insn, next_usage_insns);
5146 if (succ_p)
5147 change_p = true;
5148 else
5149 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5150 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5151 IOR_HARD_REG_SET (potential_reload_hard_regs,
5152 reg_class_contents[cl]);
5154 else if (src_regno >= lra_constraint_new_regno_start
5155 && dst_regno < lra_constraint_new_regno_start
5156 && dst_regno >= FIRST_PSEUDO_REGISTER
5157 && reg_renumber[dst_regno] < 0
5158 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5159 && usage_insns[dst_regno].check == curr_usage_insns_check
5160 && (next_usage_insns
5161 = usage_insns[dst_regno].insns) != NULL_RTX)
5163 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5164 reloads_num++;
5165 update_reloads_num_p = false;
5166 /* 'original_pseudo <- reload_pseudo'. */
5167 if (! JUMP_P (curr_insn)
5168 && inherit_reload_reg (true, dst_regno, cl,
5169 curr_insn, next_usage_insns))
5170 change_p = true;
5171 /* Invalidate. */
5172 usage_insns[dst_regno].check = 0;
5173 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5174 IOR_HARD_REG_SET (potential_reload_hard_regs,
5175 reg_class_contents[cl]);
5177 else if (INSN_P (curr_insn))
5179 int iter;
5180 int max_uid = get_max_uid ();
5182 curr_id = lra_get_insn_recog_data (curr_insn);
5183 curr_static_id = curr_id->insn_static_data;
5184 to_inherit_num = 0;
5185 /* Process insn definitions. */
5186 for (iter = 0; iter < 2; iter++)
5187 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5188 reg != NULL;
5189 reg = reg->next)
5190 if (reg->type != OP_IN
5191 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5193 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5194 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5195 && usage_insns[dst_regno].check == curr_usage_insns_check
5196 && (next_usage_insns
5197 = usage_insns[dst_regno].insns) != NULL_RTX)
5199 struct lra_insn_reg *r;
5201 for (r = curr_id->regs; r != NULL; r = r->next)
5202 if (r->type != OP_OUT && r->regno == dst_regno)
5203 break;
5204 /* Don't do inheritance if the pseudo is also
5205 used in the insn. */
5206 if (r == NULL)
5207 /* We can not do inheritance right now
5208 because the current insn reg info (chain
5209 regs) can change after that. */
5210 add_to_inherit (dst_regno, next_usage_insns);
5212 /* We can not process one reg twice here because of
5213 usage_insns invalidation. */
5214 if ((dst_regno < FIRST_PSEUDO_REGISTER
5215 || reg_renumber[dst_regno] >= 0)
5216 && ! reg->subreg_p && reg->type != OP_IN)
5218 HARD_REG_SET s;
5220 if (split_if_necessary (dst_regno, reg->biggest_mode,
5221 potential_reload_hard_regs,
5222 false, curr_insn, max_uid))
5223 change_p = true;
5224 CLEAR_HARD_REG_SET (s);
5225 if (dst_regno < FIRST_PSEUDO_REGISTER)
5226 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5227 else
5228 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5229 reg_renumber[dst_regno]);
5230 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5232 /* We should invalidate potential inheritance or
5233 splitting for the current insn usages to the next
5234 usage insns (see code below) as the output pseudo
5235 prevents this. */
5236 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5237 && reg_renumber[dst_regno] < 0)
5238 || (reg->type == OP_OUT && ! reg->subreg_p
5239 && (dst_regno < FIRST_PSEUDO_REGISTER
5240 || reg_renumber[dst_regno] >= 0)))
5242 /* Invalidate and mark definitions. */
5243 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5244 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5245 else
5247 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5248 for (i = 0; i < nregs; i++)
5249 usage_insns[dst_regno + i].check
5250 = -(int) INSN_UID (curr_insn);
5254 if (! JUMP_P (curr_insn))
5255 for (i = 0; i < to_inherit_num; i++)
5256 if (inherit_reload_reg (true, to_inherit[i].regno,
5257 ALL_REGS, curr_insn,
5258 to_inherit[i].insns))
5259 change_p = true;
5260 if (CALL_P (curr_insn))
5262 rtx cheap, pat, dest, restore;
5263 int regno, hard_regno;
5265 calls_num++;
5266 if ((cheap = find_reg_note (curr_insn,
5267 REG_RETURNED, NULL_RTX)) != NULL_RTX
5268 && ((cheap = XEXP (cheap, 0)), true)
5269 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5270 && (hard_regno = reg_renumber[regno]) >= 0
5271 /* If there are pending saves/restores, the
5272 optimization is not worth. */
5273 && usage_insns[regno].calls_num == calls_num - 1
5274 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5276 /* Restore the pseudo from the call result as
5277 REG_RETURNED note says that the pseudo value is
5278 in the call result and the pseudo is an argument
5279 of the call. */
5280 pat = PATTERN (curr_insn);
5281 if (GET_CODE (pat) == PARALLEL)
5282 pat = XVECEXP (pat, 0, 0);
5283 dest = SET_DEST (pat);
5284 start_sequence ();
5285 emit_move_insn (cheap, copy_rtx (dest));
5286 restore = get_insns ();
5287 end_sequence ();
5288 lra_process_new_insns (curr_insn, NULL, restore,
5289 "Inserting call parameter restore");
5290 /* We don't need to save/restore of the pseudo from
5291 this call. */
5292 usage_insns[regno].calls_num = calls_num;
5293 bitmap_set_bit (&check_only_regs, regno);
5296 to_inherit_num = 0;
5297 /* Process insn usages. */
5298 for (iter = 0; iter < 2; iter++)
5299 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5300 reg != NULL;
5301 reg = reg->next)
5302 if ((reg->type != OP_OUT
5303 || (reg->type == OP_OUT && reg->subreg_p))
5304 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5306 if (src_regno >= FIRST_PSEUDO_REGISTER
5307 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5309 if (usage_insns[src_regno].check == curr_usage_insns_check
5310 && (next_usage_insns
5311 = usage_insns[src_regno].insns) != NULL_RTX
5312 && NONDEBUG_INSN_P (curr_insn))
5313 add_to_inherit (src_regno, next_usage_insns);
5314 else if (usage_insns[src_regno].check
5315 != -(int) INSN_UID (curr_insn))
5316 /* Add usages but only if the reg is not set up
5317 in the same insn. */
5318 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5320 else if (src_regno < FIRST_PSEUDO_REGISTER
5321 || reg_renumber[src_regno] >= 0)
5323 bool before_p;
5324 rtx use_insn = curr_insn;
5326 before_p = (JUMP_P (curr_insn)
5327 || (CALL_P (curr_insn) && reg->type == OP_IN));
5328 if (NONDEBUG_INSN_P (curr_insn)
5329 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5330 && split_if_necessary (src_regno, reg->biggest_mode,
5331 potential_reload_hard_regs,
5332 before_p, curr_insn, max_uid))
5334 if (reg->subreg_p)
5335 lra_risky_transformations_p = true;
5336 change_p = true;
5337 /* Invalidate. */
5338 usage_insns[src_regno].check = 0;
5339 if (before_p)
5340 use_insn = PREV_INSN (curr_insn);
5342 if (NONDEBUG_INSN_P (curr_insn))
5344 if (src_regno < FIRST_PSEUDO_REGISTER)
5345 add_to_hard_reg_set (&live_hard_regs,
5346 reg->biggest_mode, src_regno);
5347 else
5348 add_to_hard_reg_set (&live_hard_regs,
5349 PSEUDO_REGNO_MODE (src_regno),
5350 reg_renumber[src_regno]);
5352 add_next_usage_insn (src_regno, use_insn, reloads_num);
5355 /* Process call args. */
5356 if (curr_id->arg_hard_regs != NULL)
5357 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5358 if (src_regno < FIRST_PSEUDO_REGISTER)
5360 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5361 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5363 for (i = 0; i < to_inherit_num; i++)
5365 src_regno = to_inherit[i].regno;
5366 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5367 curr_insn, to_inherit[i].insns))
5368 change_p = true;
5369 else
5370 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5373 if (update_reloads_num_p
5374 && NONDEBUG_INSN_P (curr_insn)
5375 && (set = single_set (curr_insn)) != NULL_RTX)
5377 int regno = -1;
5378 if ((REG_P (SET_DEST (set))
5379 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5380 && reg_renumber[regno] < 0
5381 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5382 || (REG_P (SET_SRC (set))
5383 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5384 && reg_renumber[regno] < 0
5385 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5387 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5388 reloads_num++;
5389 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5390 IOR_HARD_REG_SET (potential_reload_hard_regs,
5391 reg_class_contents[cl]);
5394 /* We reached the start of the current basic block. */
5395 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5396 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5398 /* We reached the beginning of the current block -- do
5399 rest of spliting in the current BB. */
5400 to_process = df_get_live_in (curr_bb);
5401 if (BLOCK_FOR_INSN (head) != curr_bb)
5403 /* We are somewhere in the middle of EBB. */
5404 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5405 curr_bb, &temp_bitmap);
5406 to_process = &temp_bitmap;
5408 head_p = true;
5409 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5411 if ((int) j >= lra_constraint_new_regno_start)
5412 break;
5413 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5414 && usage_insns[j].check == curr_usage_insns_check
5415 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5417 if (need_for_split_p (potential_reload_hard_regs, j))
5419 if (lra_dump_file != NULL && head_p)
5421 fprintf (lra_dump_file,
5422 " ----------------------------------\n");
5423 head_p = false;
5425 if (split_reg (false, j, bb_note (curr_bb),
5426 next_usage_insns))
5427 change_p = true;
5429 usage_insns[j].check = 0;
5434 return change_p;
5437 /* This value affects EBB forming. If probability of edge from EBB to
5438 a BB is not greater than the following value, we don't add the BB
5439 to EBB. */
5440 #define EBB_PROBABILITY_CUTOFF ((REG_BR_PROB_BASE * 50) / 100)
5442 /* Current number of inheritance/split iteration. */
5443 int lra_inheritance_iter;
5445 /* Entry function for inheritance/split pass. */
5446 void
5447 lra_inheritance (void)
5449 int i;
5450 basic_block bb, start_bb;
5451 edge e;
5453 lra_inheritance_iter++;
5454 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5455 return;
5456 timevar_push (TV_LRA_INHERITANCE);
5457 if (lra_dump_file != NULL)
5458 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5459 lra_inheritance_iter);
5460 curr_usage_insns_check = 0;
5461 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5462 for (i = 0; i < lra_constraint_new_regno_start; i++)
5463 usage_insns[i].check = 0;
5464 bitmap_initialize (&check_only_regs, &reg_obstack);
5465 bitmap_initialize (&live_regs, &reg_obstack);
5466 bitmap_initialize (&temp_bitmap, &reg_obstack);
5467 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5468 FOR_EACH_BB_FN (bb, cfun)
5470 start_bb = bb;
5471 if (lra_dump_file != NULL)
5472 fprintf (lra_dump_file, "EBB");
5473 /* Form a EBB starting with BB. */
5474 bitmap_clear (&ebb_global_regs);
5475 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5476 for (;;)
5478 if (lra_dump_file != NULL)
5479 fprintf (lra_dump_file, " %d", bb->index);
5480 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5481 || LABEL_P (BB_HEAD (bb->next_bb)))
5482 break;
5483 e = find_fallthru_edge (bb->succs);
5484 if (! e)
5485 break;
5486 if (e->probability <= EBB_PROBABILITY_CUTOFF)
5487 break;
5488 bb = bb->next_bb;
5490 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5491 if (lra_dump_file != NULL)
5492 fprintf (lra_dump_file, "\n");
5493 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5494 /* Remember that the EBB head and tail can change in
5495 inherit_in_ebb. */
5496 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5498 bitmap_clear (&ebb_global_regs);
5499 bitmap_clear (&temp_bitmap);
5500 bitmap_clear (&live_regs);
5501 bitmap_clear (&check_only_regs);
5502 free (usage_insns);
5504 timevar_pop (TV_LRA_INHERITANCE);
5509 /* This page contains code to undo failed inheritance/split
5510 transformations. */
5512 /* Current number of iteration undoing inheritance/split. */
5513 int lra_undo_inheritance_iter;
5515 /* Fix BB live info LIVE after removing pseudos created on pass doing
5516 inheritance/split which are REMOVED_PSEUDOS. */
5517 static void
5518 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5520 unsigned int regno;
5521 bitmap_iterator bi;
5523 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5524 if (bitmap_clear_bit (live, regno))
5525 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5528 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5529 number. */
5530 static int
5531 get_regno (rtx reg)
5533 if (GET_CODE (reg) == SUBREG)
5534 reg = SUBREG_REG (reg);
5535 if (REG_P (reg))
5536 return REGNO (reg);
5537 return -1;
5540 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5541 return true if we did any change. The undo transformations for
5542 inheritance looks like
5543 i <- i2
5544 p <- i => p <- i2
5545 or removing
5546 p <- i, i <- p, and i <- i3
5547 where p is original pseudo from which inheritance pseudo i was
5548 created, i and i3 are removed inheritance pseudos, i2 is another
5549 not removed inheritance pseudo. All split pseudos or other
5550 occurrences of removed inheritance pseudos are changed on the
5551 corresponding original pseudos.
5553 The function also schedules insns changed and created during
5554 inheritance/split pass for processing by the subsequent constraint
5555 pass. */
5556 static bool
5557 remove_inheritance_pseudos (bitmap remove_pseudos)
5559 basic_block bb;
5560 int regno, sregno, prev_sregno, dregno, restore_regno;
5561 rtx set, prev_set, prev_insn;
5562 bool change_p, done_p;
5564 change_p = ! bitmap_empty_p (remove_pseudos);
5565 /* We can not finish the function right away if CHANGE_P is true
5566 because we need to marks insns affected by previous
5567 inheritance/split pass for processing by the subsequent
5568 constraint pass. */
5569 FOR_EACH_BB_FN (bb, cfun)
5571 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5572 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5573 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5575 if (! INSN_P (curr_insn))
5576 continue;
5577 done_p = false;
5578 sregno = dregno = -1;
5579 if (change_p && NONDEBUG_INSN_P (curr_insn)
5580 && (set = single_set (curr_insn)) != NULL_RTX)
5582 dregno = get_regno (SET_DEST (set));
5583 sregno = get_regno (SET_SRC (set));
5586 if (sregno >= 0 && dregno >= 0)
5588 if ((bitmap_bit_p (remove_pseudos, sregno)
5589 && (lra_reg_info[sregno].restore_regno == dregno
5590 || (bitmap_bit_p (remove_pseudos, dregno)
5591 && (lra_reg_info[sregno].restore_regno
5592 == lra_reg_info[dregno].restore_regno))))
5593 || (bitmap_bit_p (remove_pseudos, dregno)
5594 && lra_reg_info[dregno].restore_regno == sregno))
5595 /* One of the following cases:
5596 original <- removed inheritance pseudo
5597 removed inherit pseudo <- another removed inherit pseudo
5598 removed inherit pseudo <- original pseudo
5600 removed_split_pseudo <- original_reg
5601 original_reg <- removed_split_pseudo */
5603 if (lra_dump_file != NULL)
5605 fprintf (lra_dump_file, " Removing %s:\n",
5606 bitmap_bit_p (&lra_split_regs, sregno)
5607 || bitmap_bit_p (&lra_split_regs, dregno)
5608 ? "split" : "inheritance");
5609 dump_insn_slim (lra_dump_file, curr_insn);
5611 lra_set_insn_deleted (curr_insn);
5612 done_p = true;
5614 else if (bitmap_bit_p (remove_pseudos, sregno)
5615 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5617 /* Search the following pattern:
5618 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5619 original_pseudo <- inherit_or_split_pseudo1
5620 where the 2nd insn is the current insn and
5621 inherit_or_split_pseudo2 is not removed. If it is found,
5622 change the current insn onto:
5623 original_pseudo <- inherit_or_split_pseudo2. */
5624 for (prev_insn = PREV_INSN (curr_insn);
5625 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5626 prev_insn = PREV_INSN (prev_insn))
5628 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5629 && (prev_set = single_set (prev_insn)) != NULL_RTX
5630 /* There should be no subregs in insn we are
5631 searching because only the original reg might
5632 be in subreg when we changed the mode of
5633 load/store for splitting. */
5634 && REG_P (SET_DEST (prev_set))
5635 && REG_P (SET_SRC (prev_set))
5636 && (int) REGNO (SET_DEST (prev_set)) == sregno
5637 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5638 >= FIRST_PSEUDO_REGISTER)
5639 /* As we consider chain of inheritance or
5640 splitting described in above comment we should
5641 check that sregno and prev_sregno were
5642 inheritance/split pseudos created from the
5643 same original regno. */
5644 && (lra_reg_info[sregno].restore_regno
5645 == lra_reg_info[prev_sregno].restore_regno)
5646 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5648 lra_assert (GET_MODE (SET_SRC (prev_set))
5649 == GET_MODE (regno_reg_rtx[sregno]));
5650 if (GET_CODE (SET_SRC (set)) == SUBREG)
5651 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5652 else
5653 SET_SRC (set) = SET_SRC (prev_set);
5654 lra_push_insn_and_update_insn_regno_info (curr_insn);
5655 lra_set_used_insn_alternative_by_uid
5656 (INSN_UID (curr_insn), -1);
5657 done_p = true;
5658 if (lra_dump_file != NULL)
5660 fprintf (lra_dump_file, " Change reload insn:\n");
5661 dump_insn_slim (lra_dump_file, curr_insn);
5666 if (! done_p)
5668 struct lra_insn_reg *reg;
5669 bool restored_regs_p = false;
5670 bool kept_regs_p = false;
5672 curr_id = lra_get_insn_recog_data (curr_insn);
5673 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5675 regno = reg->regno;
5676 restore_regno = lra_reg_info[regno].restore_regno;
5677 if (restore_regno >= 0)
5679 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5681 substitute_pseudo (&curr_insn, regno,
5682 regno_reg_rtx[restore_regno]);
5683 restored_regs_p = true;
5685 else
5686 kept_regs_p = true;
5689 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5691 /* The instruction has changed since the previous
5692 constraints pass. */
5693 lra_push_insn_and_update_insn_regno_info (curr_insn);
5694 lra_set_used_insn_alternative_by_uid
5695 (INSN_UID (curr_insn), -1);
5697 else if (restored_regs_p)
5698 /* The instruction has been restored to the form that
5699 it had during the previous constraints pass. */
5700 lra_update_insn_regno_info (curr_insn);
5701 if (restored_regs_p && lra_dump_file != NULL)
5703 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5704 dump_insn_slim (lra_dump_file, curr_insn);
5709 return change_p;
5712 /* If optional reload pseudos failed to get a hard register or was not
5713 inherited, it is better to remove optional reloads. We do this
5714 transformation after undoing inheritance to figure out necessity to
5715 remove optional reloads easier. Return true if we do any
5716 change. */
5717 static bool
5718 undo_optional_reloads (void)
5720 bool change_p, keep_p;
5721 unsigned int regno, uid;
5722 bitmap_iterator bi, bi2;
5723 rtx insn, set, src, dest;
5724 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
5726 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
5727 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
5728 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5730 keep_p = false;
5731 /* Keep optional reloads from previous subpasses. */
5732 if (lra_reg_info[regno].restore_regno < 0
5733 /* If the original pseudo changed its allocation, just
5734 removing the optional pseudo is dangerous as the original
5735 pseudo will have longer live range. */
5736 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
5737 keep_p = true;
5738 else if (reg_renumber[regno] >= 0)
5739 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
5741 insn = lra_insn_recog_data[uid]->insn;
5742 if ((set = single_set (insn)) == NULL_RTX)
5743 continue;
5744 src = SET_SRC (set);
5745 dest = SET_DEST (set);
5746 if (! REG_P (src) || ! REG_P (dest))
5747 continue;
5748 if (REGNO (dest) == regno
5749 /* Ignore insn for optional reloads itself. */
5750 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
5751 /* Check only inheritance on last inheritance pass. */
5752 && (int) REGNO (src) >= new_regno_start
5753 /* Check that the optional reload was inherited. */
5754 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
5756 keep_p = true;
5757 break;
5760 if (keep_p)
5762 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
5763 if (lra_dump_file != NULL)
5764 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
5767 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
5768 bitmap_initialize (&insn_bitmap, &reg_obstack);
5769 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
5771 if (lra_dump_file != NULL)
5772 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
5773 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
5774 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
5776 insn = lra_insn_recog_data[uid]->insn;
5777 if ((set = single_set (insn)) != NULL_RTX)
5779 src = SET_SRC (set);
5780 dest = SET_DEST (set);
5781 if (REG_P (src) && REG_P (dest)
5782 && ((REGNO (src) == regno
5783 && (lra_reg_info[regno].restore_regno
5784 == (int) REGNO (dest)))
5785 || (REGNO (dest) == regno
5786 && (lra_reg_info[regno].restore_regno
5787 == (int) REGNO (src)))))
5789 if (lra_dump_file != NULL)
5791 fprintf (lra_dump_file, " Deleting move %u\n",
5792 INSN_UID (insn));
5793 dump_insn_slim (lra_dump_file, insn);
5795 lra_set_insn_deleted (insn);
5796 continue;
5798 /* We should not worry about generation memory-memory
5799 moves here as if the corresponding inheritance did
5800 not work (inheritance pseudo did not get a hard reg),
5801 we remove the inheritance pseudo and the optional
5802 reload. */
5804 substitute_pseudo (&insn, regno,
5805 regno_reg_rtx[lra_reg_info[regno].restore_regno]);
5806 lra_update_insn_regno_info (insn);
5807 if (lra_dump_file != NULL)
5809 fprintf (lra_dump_file,
5810 " Restoring original insn:\n");
5811 dump_insn_slim (lra_dump_file, insn);
5815 /* Clear restore_regnos. */
5816 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5817 lra_reg_info[regno].restore_regno = -1;
5818 bitmap_clear (&insn_bitmap);
5819 bitmap_clear (&removed_optional_reload_pseudos);
5820 return change_p;
5823 /* Entry function for undoing inheritance/split transformation. Return true
5824 if we did any RTL change in this pass. */
5825 bool
5826 lra_undo_inheritance (void)
5828 unsigned int regno;
5829 int restore_regno, hard_regno;
5830 int n_all_inherit, n_inherit, n_all_split, n_split;
5831 bitmap_head remove_pseudos;
5832 bitmap_iterator bi;
5833 bool change_p;
5835 lra_undo_inheritance_iter++;
5836 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5837 return false;
5838 if (lra_dump_file != NULL)
5839 fprintf (lra_dump_file,
5840 "\n********** Undoing inheritance #%d: **********\n\n",
5841 lra_undo_inheritance_iter);
5842 bitmap_initialize (&remove_pseudos, &reg_obstack);
5843 n_inherit = n_all_inherit = 0;
5844 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5845 if (lra_reg_info[regno].restore_regno >= 0)
5847 n_all_inherit++;
5848 if (reg_renumber[regno] < 0
5849 /* If the original pseudo changed its allocation, just
5850 removing inheritance is dangerous as for changing
5851 allocation we used shorter live-ranges. */
5852 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
5853 bitmap_set_bit (&remove_pseudos, regno);
5854 else
5855 n_inherit++;
5857 if (lra_dump_file != NULL && n_all_inherit != 0)
5858 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
5859 n_inherit, n_all_inherit,
5860 (double) n_inherit / n_all_inherit * 100);
5861 n_split = n_all_split = 0;
5862 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5863 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
5865 n_all_split++;
5866 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
5867 ? reg_renumber[restore_regno] : restore_regno);
5868 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
5869 bitmap_set_bit (&remove_pseudos, regno);
5870 else
5872 n_split++;
5873 if (lra_dump_file != NULL)
5874 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
5875 regno, restore_regno);
5878 if (lra_dump_file != NULL && n_all_split != 0)
5879 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
5880 n_split, n_all_split,
5881 (double) n_split / n_all_split * 100);
5882 change_p = remove_inheritance_pseudos (&remove_pseudos);
5883 bitmap_clear (&remove_pseudos);
5884 /* Clear restore_regnos. */
5885 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5886 lra_reg_info[regno].restore_regno = -1;
5887 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5888 lra_reg_info[regno].restore_regno = -1;
5889 change_p = undo_optional_reloads () || change_p;
5890 return change_p;