/cp
[official-gcc.git] / gcc / lra-constraints.c
blob34159f75a10f1ef87e42f0d5f21361dd92e9ba6a
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 && curr_insn_set != NULL_RTX
1298 && (REG_P (SET_SRC (curr_insn_set))
1299 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG))
1300 /* There is big probability that we will get the same class
1301 for the new pseudo and we will get the same insn which
1302 means infinite looping. So spill the new pseudo. */
1303 rclass = NO_REGS;
1304 else
1305 /* The class will be defined later in curr_insn_transform. */
1306 rclass
1307 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1309 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1310 rclass, "subreg reg", &new_reg))
1312 bool insert_before, insert_after;
1313 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1315 insert_before = (type != OP_OUT
1316 || GET_MODE_SIZE (GET_MODE (reg)) > GET_MODE_SIZE (mode));
1317 insert_after = (type != OP_IN);
1318 insert_move_for_subreg (insert_before ? &before : NULL,
1319 insert_after ? &after : NULL,
1320 reg, new_reg);
1322 SUBREG_REG (operand) = new_reg;
1323 lra_process_new_insns (curr_insn, before, after,
1324 "Inserting subreg reload");
1325 return true;
1327 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1328 IRA allocates hardreg to the inner pseudo reg according to its mode
1329 instead of the outermode, so the size of the hardreg may not be enough
1330 to contain the outermode operand, in that case we may need to insert
1331 reload for the reg. For the following two types of paradoxical subreg,
1332 we need to insert reload:
1333 1. If the op_type is OP_IN, and the hardreg could not be paired with
1334 other hardreg to contain the outermode operand
1335 (checked by in_hard_reg_set_p), we need to insert the reload.
1336 2. If the op_type is OP_OUT or OP_INOUT.
1338 Here is a paradoxical subreg example showing how the reload is generated:
1340 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1341 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1343 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1344 here, if reg107 is assigned to hardreg R15, because R15 is the last
1345 hardreg, compiler cannot find another hardreg to pair with R15 to
1346 contain TImode data. So we insert a TImode reload reg180 for it.
1347 After reload is inserted:
1349 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1350 (reg:DI 107 [ __comp ])) -1
1351 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1352 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1354 Two reload hard registers will be allocated to reg180 to save TImode data
1355 in LRA_assign. */
1356 else if (REG_P (reg)
1357 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1358 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1359 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1360 < hard_regno_nregs[hard_regno][mode])
1361 && (regclass = lra_get_allocno_class (REGNO (reg)))
1362 && (type != OP_IN
1363 || !in_hard_reg_set_p (reg_class_contents[regclass],
1364 mode, hard_regno)))
1366 /* The class will be defined later in curr_insn_transform. */
1367 enum reg_class rclass
1368 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1370 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1371 rclass, "paradoxical subreg", &new_reg))
1373 rtx subreg;
1374 bool insert_before, insert_after;
1376 PUT_MODE (new_reg, mode);
1377 subreg = simplify_gen_subreg (GET_MODE (reg), new_reg, mode, 0);
1378 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1380 insert_before = (type != OP_OUT);
1381 insert_after = (type != OP_IN);
1382 insert_move_for_subreg (insert_before ? &before : NULL,
1383 insert_after ? &after : NULL,
1384 reg, subreg);
1386 SUBREG_REG (operand) = new_reg;
1387 lra_process_new_insns (curr_insn, before, after,
1388 "Inserting paradoxical subreg reload");
1389 return true;
1391 return false;
1394 /* Return TRUE if X refers for a hard register from SET. */
1395 static bool
1396 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1398 int i, j, x_hard_regno;
1399 enum machine_mode mode;
1400 const char *fmt;
1401 enum rtx_code code;
1403 if (x == NULL_RTX)
1404 return false;
1405 code = GET_CODE (x);
1406 mode = GET_MODE (x);
1407 if (code == SUBREG)
1409 x = SUBREG_REG (x);
1410 code = GET_CODE (x);
1411 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1412 mode = GET_MODE (x);
1415 if (REG_P (x))
1417 x_hard_regno = get_hard_regno (x);
1418 return (x_hard_regno >= 0
1419 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1421 if (MEM_P (x))
1423 struct address_info ad;
1425 decompose_mem_address (&ad, x);
1426 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1427 return true;
1428 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1429 return true;
1431 fmt = GET_RTX_FORMAT (code);
1432 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1434 if (fmt[i] == 'e')
1436 if (uses_hard_regs_p (XEXP (x, i), set))
1437 return true;
1439 else if (fmt[i] == 'E')
1441 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1442 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1443 return true;
1446 return false;
1449 /* Return true if OP is a spilled pseudo. */
1450 static inline bool
1451 spilled_pseudo_p (rtx op)
1453 return (REG_P (op)
1454 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1457 /* Return true if X is a general constant. */
1458 static inline bool
1459 general_constant_p (rtx x)
1461 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1464 static bool
1465 reg_in_class_p (rtx reg, enum reg_class cl)
1467 if (cl == NO_REGS)
1468 return get_reg_class (REGNO (reg)) == NO_REGS;
1469 return in_class_p (reg, cl, NULL);
1472 /* Major function to choose the current insn alternative and what
1473 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1474 negative we should consider only this alternative. Return false if
1475 we can not choose the alternative or find how to reload the
1476 operands. */
1477 static bool
1478 process_alt_operands (int only_alternative)
1480 bool ok_p = false;
1481 int nop, overall, nalt;
1482 int n_alternatives = curr_static_id->n_alternatives;
1483 int n_operands = curr_static_id->n_operands;
1484 /* LOSERS counts the operands that don't fit this alternative and
1485 would require loading. */
1486 int losers;
1487 /* REJECT is a count of how undesirable this alternative says it is
1488 if any reloading is required. If the alternative matches exactly
1489 then REJECT is ignored, but otherwise it gets this much counted
1490 against it in addition to the reloading needed. */
1491 int reject;
1492 /* The number of elements in the following array. */
1493 int early_clobbered_regs_num;
1494 /* Numbers of operands which are early clobber registers. */
1495 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1496 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1497 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1498 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1499 bool curr_alt_win[MAX_RECOG_OPERANDS];
1500 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1501 int curr_alt_matches[MAX_RECOG_OPERANDS];
1502 /* The number of elements in the following array. */
1503 int curr_alt_dont_inherit_ops_num;
1504 /* Numbers of operands whose reload pseudos should not be inherited. */
1505 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1506 rtx op;
1507 /* The register when the operand is a subreg of register, otherwise the
1508 operand itself. */
1509 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1510 /* The register if the operand is a register or subreg of register,
1511 otherwise NULL. */
1512 rtx operand_reg[MAX_RECOG_OPERANDS];
1513 int hard_regno[MAX_RECOG_OPERANDS];
1514 enum machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1515 int reload_nregs, reload_sum;
1516 bool costly_p;
1517 enum reg_class cl;
1519 /* Calculate some data common for all alternatives to speed up the
1520 function. */
1521 for (nop = 0; nop < n_operands; nop++)
1523 rtx reg;
1525 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1526 /* The real hard regno of the operand after the allocation. */
1527 hard_regno[nop] = get_hard_regno (op);
1529 operand_reg[nop] = reg = op;
1530 biggest_mode[nop] = GET_MODE (op);
1531 if (GET_CODE (op) == SUBREG)
1533 operand_reg[nop] = reg = SUBREG_REG (op);
1534 if (GET_MODE_SIZE (biggest_mode[nop])
1535 < GET_MODE_SIZE (GET_MODE (reg)))
1536 biggest_mode[nop] = GET_MODE (reg);
1538 if (! REG_P (reg))
1539 operand_reg[nop] = NULL_RTX;
1540 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1541 || ((int) REGNO (reg)
1542 == lra_get_elimination_hard_regno (REGNO (reg))))
1543 no_subreg_reg_operand[nop] = reg;
1544 else
1545 operand_reg[nop] = no_subreg_reg_operand[nop]
1546 /* Just use natural mode for elimination result. It should
1547 be enough for extra constraints hooks. */
1548 = regno_reg_rtx[hard_regno[nop]];
1551 /* The constraints are made of several alternatives. Each operand's
1552 constraint looks like foo,bar,... with commas separating the
1553 alternatives. The first alternatives for all operands go
1554 together, the second alternatives go together, etc.
1556 First loop over alternatives. */
1557 for (nalt = 0; nalt < n_alternatives; nalt++)
1559 /* Loop over operands for one constraint alternative. */
1560 #if HAVE_ATTR_enabled
1561 if (curr_id->alternative_enabled_p != NULL
1562 && ! curr_id->alternative_enabled_p[nalt])
1563 continue;
1564 #endif
1566 if (only_alternative >= 0 && nalt != only_alternative)
1567 continue;
1570 overall = losers = reject = reload_nregs = reload_sum = 0;
1571 for (nop = 0; nop < n_operands; nop++)
1573 int inc = (curr_static_id
1574 ->operand_alternative[nalt * n_operands + nop].reject);
1575 if (lra_dump_file != NULL && inc != 0)
1576 fprintf (lra_dump_file,
1577 " Staticly defined alt reject+=%d\n", inc);
1578 reject += inc;
1580 early_clobbered_regs_num = 0;
1582 for (nop = 0; nop < n_operands; nop++)
1584 const char *p;
1585 char *end;
1586 int len, c, m, i, opalt_num, this_alternative_matches;
1587 bool win, did_match, offmemok, early_clobber_p;
1588 /* false => this operand can be reloaded somehow for this
1589 alternative. */
1590 bool badop;
1591 /* true => this operand can be reloaded if the alternative
1592 allows regs. */
1593 bool winreg;
1594 /* True if a constant forced into memory would be OK for
1595 this operand. */
1596 bool constmemok;
1597 enum reg_class this_alternative, this_costly_alternative;
1598 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1599 bool this_alternative_match_win, this_alternative_win;
1600 bool this_alternative_offmemok;
1601 bool scratch_p;
1602 enum machine_mode mode;
1604 opalt_num = nalt * n_operands + nop;
1605 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1607 /* Fast track for no constraints at all. */
1608 curr_alt[nop] = NO_REGS;
1609 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1610 curr_alt_win[nop] = true;
1611 curr_alt_match_win[nop] = false;
1612 curr_alt_offmemok[nop] = false;
1613 curr_alt_matches[nop] = -1;
1614 continue;
1617 op = no_subreg_reg_operand[nop];
1618 mode = curr_operand_mode[nop];
1620 win = did_match = winreg = offmemok = constmemok = false;
1621 badop = true;
1623 early_clobber_p = false;
1624 p = curr_static_id->operand_alternative[opalt_num].constraint;
1626 this_costly_alternative = this_alternative = NO_REGS;
1627 /* We update set of possible hard regs besides its class
1628 because reg class might be inaccurate. For example,
1629 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1630 is translated in HI_REGS because classes are merged by
1631 pairs and there is no accurate intermediate class. */
1632 CLEAR_HARD_REG_SET (this_alternative_set);
1633 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1634 this_alternative_win = false;
1635 this_alternative_match_win = false;
1636 this_alternative_offmemok = false;
1637 this_alternative_matches = -1;
1639 /* An empty constraint should be excluded by the fast
1640 track. */
1641 lra_assert (*p != 0 && *p != ',');
1643 /* Scan this alternative's specs for this operand; set WIN
1644 if the operand fits any letter in this alternative.
1645 Otherwise, clear BADOP if this operand could fit some
1646 letter after reloads, or set WINREG if this operand could
1647 fit after reloads provided the constraint allows some
1648 registers. */
1649 costly_p = false;
1652 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1654 case '\0':
1655 len = 0;
1656 break;
1657 case ',':
1658 c = '\0';
1659 break;
1661 case '=': case '+': case '?': case '*': case '!':
1662 case ' ': case '\t':
1663 break;
1665 case '%':
1666 /* We only support one commutative marker, the first
1667 one. We already set commutative above. */
1668 break;
1670 case '&':
1671 early_clobber_p = true;
1672 break;
1674 case '#':
1675 /* Ignore rest of this alternative. */
1676 c = '\0';
1677 break;
1679 case '0': case '1': case '2': case '3': case '4':
1680 case '5': case '6': case '7': case '8': case '9':
1682 int m_hregno;
1683 bool match_p;
1685 m = strtoul (p, &end, 10);
1686 p = end;
1687 len = 0;
1688 lra_assert (nop > m);
1690 this_alternative_matches = m;
1691 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1692 /* We are supposed to match a previous operand.
1693 If we do, we win if that one did. If we do
1694 not, count both of the operands as losers.
1695 (This is too conservative, since most of the
1696 time only a single reload insn will be needed
1697 to make the two operands win. As a result,
1698 this alternative may be rejected when it is
1699 actually desirable.) */
1700 match_p = false;
1701 if (operands_match_p (*curr_id->operand_loc[nop],
1702 *curr_id->operand_loc[m], m_hregno))
1704 /* We should reject matching of an early
1705 clobber operand if the matching operand is
1706 not dying in the insn. */
1707 if (! curr_static_id->operand[m].early_clobber
1708 || operand_reg[nop] == NULL_RTX
1709 || (find_regno_note (curr_insn, REG_DEAD,
1710 REGNO (op))
1711 || REGNO (op) == REGNO (operand_reg[m])))
1712 match_p = true;
1714 if (match_p)
1716 /* If we are matching a non-offsettable
1717 address where an offsettable address was
1718 expected, then we must reject this
1719 combination, because we can't reload
1720 it. */
1721 if (curr_alt_offmemok[m]
1722 && MEM_P (*curr_id->operand_loc[m])
1723 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1724 continue;
1726 else
1728 /* Operands don't match. Both operands must
1729 allow a reload register, otherwise we
1730 cannot make them match. */
1731 if (curr_alt[m] == NO_REGS)
1732 break;
1733 /* Retroactively mark the operand we had to
1734 match as a loser, if it wasn't already and
1735 it wasn't matched to a register constraint
1736 (e.g it might be matched by memory). */
1737 if (curr_alt_win[m]
1738 && (operand_reg[m] == NULL_RTX
1739 || hard_regno[m] < 0))
1741 losers++;
1742 reload_nregs
1743 += (ira_reg_class_max_nregs[curr_alt[m]]
1744 [GET_MODE (*curr_id->operand_loc[m])]);
1747 /* We prefer no matching alternatives because
1748 it gives more freedom in RA. */
1749 if (operand_reg[nop] == NULL_RTX
1750 || (find_regno_note (curr_insn, REG_DEAD,
1751 REGNO (operand_reg[nop]))
1752 == NULL_RTX))
1754 if (lra_dump_file != NULL)
1755 fprintf
1756 (lra_dump_file,
1757 " %d Matching alt: reject+=2\n",
1758 nop);
1759 reject += 2;
1762 /* If we have to reload this operand and some
1763 previous operand also had to match the same
1764 thing as this operand, we don't know how to do
1765 that. */
1766 if (!match_p || !curr_alt_win[m])
1768 for (i = 0; i < nop; i++)
1769 if (curr_alt_matches[i] == m)
1770 break;
1771 if (i < nop)
1772 break;
1774 else
1775 did_match = true;
1777 /* This can be fixed with reloads if the operand
1778 we are supposed to match can be fixed with
1779 reloads. */
1780 badop = false;
1781 this_alternative = curr_alt[m];
1782 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1783 winreg = this_alternative != NO_REGS;
1784 break;
1787 case 'p':
1788 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1789 ADDRESS, SCRATCH);
1790 this_alternative = reg_class_subunion[this_alternative][cl];
1791 IOR_HARD_REG_SET (this_alternative_set,
1792 reg_class_contents[cl]);
1793 if (costly_p)
1795 this_costly_alternative
1796 = reg_class_subunion[this_costly_alternative][cl];
1797 IOR_HARD_REG_SET (this_costly_alternative_set,
1798 reg_class_contents[cl]);
1800 win = true;
1801 badop = false;
1802 break;
1804 case TARGET_MEM_CONSTRAINT:
1805 if (MEM_P (op) || spilled_pseudo_p (op))
1806 win = true;
1807 /* We can put constant or pseudo value into memory
1808 to satisfy the constraint. */
1809 if (CONST_POOL_OK_P (mode, op) || REG_P (op))
1810 badop = false;
1811 constmemok = true;
1812 break;
1814 case '<':
1815 if (MEM_P (op)
1816 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
1817 || GET_CODE (XEXP (op, 0)) == POST_DEC))
1818 win = true;
1819 break;
1821 case '>':
1822 if (MEM_P (op)
1823 && (GET_CODE (XEXP (op, 0)) == PRE_INC
1824 || GET_CODE (XEXP (op, 0)) == POST_INC))
1825 win = true;
1826 break;
1828 /* Memory op whose address is not offsettable. */
1829 case 'V':
1830 if (MEM_P (op)
1831 && ! offsettable_nonstrict_memref_p (op))
1832 win = true;
1833 break;
1835 /* Memory operand whose address is offsettable. */
1836 case 'o':
1837 if ((MEM_P (op)
1838 && offsettable_nonstrict_memref_p (op))
1839 || spilled_pseudo_p (op))
1840 win = true;
1841 /* We can put constant or pseudo value into memory
1842 or make memory address offsetable to satisfy the
1843 constraint. */
1844 if (CONST_POOL_OK_P (mode, op) || MEM_P (op) || REG_P (op))
1845 badop = false;
1846 constmemok = true;
1847 offmemok = true;
1848 break;
1850 case 'E':
1851 case 'F':
1852 if (GET_CODE (op) == CONST_DOUBLE
1853 || (GET_CODE (op) == CONST_VECTOR
1854 && (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)))
1855 win = true;
1856 break;
1858 case 'G':
1859 case 'H':
1860 if (CONST_DOUBLE_AS_FLOAT_P (op)
1861 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
1862 win = true;
1863 break;
1865 case 's':
1866 if (CONST_SCALAR_INT_P (op))
1867 break;
1869 case 'i':
1870 if (general_constant_p (op))
1871 win = true;
1872 break;
1874 case 'n':
1875 if (CONST_SCALAR_INT_P (op))
1876 win = true;
1877 break;
1879 case 'I':
1880 case 'J':
1881 case 'K':
1882 case 'L':
1883 case 'M':
1884 case 'N':
1885 case 'O':
1886 case 'P':
1887 if (CONST_INT_P (op)
1888 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
1889 win = true;
1890 break;
1892 case 'X':
1893 /* This constraint should be excluded by the fast
1894 track. */
1895 gcc_unreachable ();
1896 break;
1898 case 'g':
1899 if (MEM_P (op)
1900 || general_constant_p (op)
1901 || spilled_pseudo_p (op))
1902 win = true;
1903 /* Drop through into 'r' case. */
1905 case 'r':
1906 this_alternative
1907 = reg_class_subunion[this_alternative][GENERAL_REGS];
1908 IOR_HARD_REG_SET (this_alternative_set,
1909 reg_class_contents[GENERAL_REGS]);
1910 if (costly_p)
1912 this_costly_alternative
1913 = (reg_class_subunion
1914 [this_costly_alternative][GENERAL_REGS]);
1915 IOR_HARD_REG_SET (this_costly_alternative_set,
1916 reg_class_contents[GENERAL_REGS]);
1918 goto reg;
1920 default:
1921 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
1923 #ifdef EXTRA_CONSTRAINT_STR
1924 if (EXTRA_MEMORY_CONSTRAINT (c, p))
1926 if (EXTRA_CONSTRAINT_STR (op, c, p))
1927 win = true;
1928 else if (spilled_pseudo_p (op))
1929 win = true;
1931 /* If we didn't already win, we can reload
1932 constants via force_const_mem or put the
1933 pseudo value into memory, or make other
1934 memory by reloading the address like for
1935 'o'. */
1936 if (CONST_POOL_OK_P (mode, op)
1937 || MEM_P (op) || REG_P (op))
1938 badop = false;
1939 constmemok = true;
1940 offmemok = true;
1941 break;
1943 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1945 if (EXTRA_CONSTRAINT_STR (op, c, p))
1946 win = true;
1948 /* If we didn't already win, we can reload
1949 the address into a base register. */
1950 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1951 ADDRESS, SCRATCH);
1952 this_alternative
1953 = reg_class_subunion[this_alternative][cl];
1954 IOR_HARD_REG_SET (this_alternative_set,
1955 reg_class_contents[cl]);
1956 if (costly_p)
1958 this_costly_alternative
1959 = (reg_class_subunion
1960 [this_costly_alternative][cl]);
1961 IOR_HARD_REG_SET (this_costly_alternative_set,
1962 reg_class_contents[cl]);
1964 badop = false;
1965 break;
1968 if (EXTRA_CONSTRAINT_STR (op, c, p))
1969 win = true;
1970 #endif
1971 break;
1974 cl = REG_CLASS_FROM_CONSTRAINT (c, p);
1975 this_alternative = reg_class_subunion[this_alternative][cl];
1976 IOR_HARD_REG_SET (this_alternative_set,
1977 reg_class_contents[cl]);
1978 if (costly_p)
1980 this_costly_alternative
1981 = reg_class_subunion[this_costly_alternative][cl];
1982 IOR_HARD_REG_SET (this_costly_alternative_set,
1983 reg_class_contents[cl]);
1985 reg:
1986 if (mode == BLKmode)
1987 break;
1988 winreg = true;
1989 if (REG_P (op))
1991 if (hard_regno[nop] >= 0
1992 && in_hard_reg_set_p (this_alternative_set,
1993 mode, hard_regno[nop]))
1994 win = true;
1995 else if (hard_regno[nop] < 0
1996 && in_class_p (op, this_alternative, NULL))
1997 win = true;
1999 break;
2001 if (c != ' ' && c != '\t')
2002 costly_p = c == '*';
2004 while ((p += len), c);
2006 scratch_p = (operand_reg[nop] != NULL_RTX
2007 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2008 /* Record which operands fit this alternative. */
2009 if (win)
2011 this_alternative_win = true;
2012 if (operand_reg[nop] != NULL_RTX)
2014 if (hard_regno[nop] >= 0)
2016 if (in_hard_reg_set_p (this_costly_alternative_set,
2017 mode, hard_regno[nop]))
2019 if (lra_dump_file != NULL)
2020 fprintf (lra_dump_file,
2021 " %d Costly set: reject++\n",
2022 nop);
2023 reject++;
2026 else
2028 /* Prefer won reg to spilled pseudo under other
2029 equal conditions for possibe inheritance. */
2030 if (! scratch_p)
2032 if (lra_dump_file != NULL)
2033 fprintf
2034 (lra_dump_file,
2035 " %d Non pseudo reload: reject++\n",
2036 nop);
2037 reject++;
2039 if (in_class_p (operand_reg[nop],
2040 this_costly_alternative, NULL))
2042 if (lra_dump_file != NULL)
2043 fprintf
2044 (lra_dump_file,
2045 " %d Non pseudo costly reload:"
2046 " reject++\n",
2047 nop);
2048 reject++;
2051 /* We simulate the behaviour of old reload here.
2052 Although scratches need hard registers and it
2053 might result in spilling other pseudos, no reload
2054 insns are generated for the scratches. So it
2055 might cost something but probably less than old
2056 reload pass believes. */
2057 if (scratch_p)
2059 if (lra_dump_file != NULL)
2060 fprintf (lra_dump_file,
2061 " %d Scratch win: reject+=2\n",
2062 nop);
2063 reject += 2;
2067 else if (did_match)
2068 this_alternative_match_win = true;
2069 else
2071 int const_to_mem = 0;
2072 bool no_regs_p;
2074 /* Never do output reload of stack pointer. It makes
2075 impossible to do elimination when SP is changed in
2076 RTL. */
2077 if (op == stack_pointer_rtx && ! frame_pointer_needed
2078 && curr_static_id->operand[nop].type != OP_IN)
2079 goto fail;
2081 /* If this alternative asks for a specific reg class, see if there
2082 is at least one allocatable register in that class. */
2083 no_regs_p
2084 = (this_alternative == NO_REGS
2085 || (hard_reg_set_subset_p
2086 (reg_class_contents[this_alternative],
2087 lra_no_alloc_regs)));
2089 /* For asms, verify that the class for this alternative is possible
2090 for the mode that is specified. */
2091 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2093 int i;
2094 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2095 if (HARD_REGNO_MODE_OK (i, mode)
2096 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2097 mode, i))
2098 break;
2099 if (i == FIRST_PSEUDO_REGISTER)
2100 winreg = false;
2103 /* If this operand accepts a register, and if the
2104 register class has at least one allocatable register,
2105 then this operand can be reloaded. */
2106 if (winreg && !no_regs_p)
2107 badop = false;
2109 if (badop)
2111 if (lra_dump_file != NULL)
2112 fprintf (lra_dump_file,
2113 " alt=%d: Bad operand -- refuse\n",
2114 nalt);
2115 goto fail;
2118 this_alternative_offmemok = offmemok;
2119 if (this_costly_alternative != NO_REGS)
2121 if (lra_dump_file != NULL)
2122 fprintf (lra_dump_file,
2123 " %d Costly loser: reject++\n", nop);
2124 reject++;
2126 /* If the operand is dying, has a matching constraint,
2127 and satisfies constraints of the matched operand
2128 which failed to satisfy the own constraints, probably
2129 the reload for this operand will be gone. */
2130 if (this_alternative_matches >= 0
2131 && !curr_alt_win[this_alternative_matches]
2132 && REG_P (op)
2133 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2134 && (hard_regno[nop] >= 0
2135 ? in_hard_reg_set_p (this_alternative_set,
2136 mode, hard_regno[nop])
2137 : in_class_p (op, this_alternative, NULL)))
2139 if (lra_dump_file != NULL)
2140 fprintf
2141 (lra_dump_file,
2142 " %d Dying matched operand reload: reject++\n",
2143 nop);
2144 reject++;
2146 else
2148 /* Strict_low_part requires to reload the register
2149 not the sub-register. In this case we should
2150 check that a final reload hard reg can hold the
2151 value mode. */
2152 if (curr_static_id->operand[nop].strict_low
2153 && REG_P (op)
2154 && hard_regno[nop] < 0
2155 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2156 && ira_class_hard_regs_num[this_alternative] > 0
2157 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2158 [this_alternative][0],
2159 GET_MODE
2160 (*curr_id->operand_loc[nop])))
2162 if (lra_dump_file != NULL)
2163 fprintf
2164 (lra_dump_file,
2165 " alt=%d: Strict low subreg reload -- refuse\n",
2166 nalt);
2167 goto fail;
2169 losers++;
2171 if (operand_reg[nop] != NULL_RTX
2172 /* Output operands and matched input operands are
2173 not inherited. The following conditions do not
2174 exactly describe the previous statement but they
2175 are pretty close. */
2176 && curr_static_id->operand[nop].type != OP_OUT
2177 && (this_alternative_matches < 0
2178 || curr_static_id->operand[nop].type != OP_IN))
2180 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2181 (operand_reg[nop])]
2182 .last_reload);
2184 if (last_reload > bb_reload_num)
2185 reload_sum += last_reload - bb_reload_num;
2187 /* If this is a constant that is reloaded into the
2188 desired class by copying it to memory first, count
2189 that as another reload. This is consistent with
2190 other code and is required to avoid choosing another
2191 alternative when the constant is moved into memory.
2192 Note that the test here is precisely the same as in
2193 the code below that calls force_const_mem. */
2194 if (CONST_POOL_OK_P (mode, op)
2195 && ((targetm.preferred_reload_class
2196 (op, this_alternative) == NO_REGS)
2197 || no_input_reloads_p))
2199 const_to_mem = 1;
2200 if (! no_regs_p)
2201 losers++;
2204 /* Alternative loses if it requires a type of reload not
2205 permitted for this insn. We can always reload
2206 objects with a REG_UNUSED note. */
2207 if ((curr_static_id->operand[nop].type != OP_IN
2208 && no_output_reloads_p
2209 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2210 || (curr_static_id->operand[nop].type != OP_OUT
2211 && no_input_reloads_p && ! const_to_mem)
2212 || (this_alternative_matches >= 0
2213 && (no_input_reloads_p
2214 || (no_output_reloads_p
2215 && (curr_static_id->operand
2216 [this_alternative_matches].type != OP_IN)
2217 && ! find_reg_note (curr_insn, REG_UNUSED,
2218 no_subreg_reg_operand
2219 [this_alternative_matches])))))
2221 if (lra_dump_file != NULL)
2222 fprintf
2223 (lra_dump_file,
2224 " alt=%d: No input/otput reload -- refuse\n",
2225 nalt);
2226 goto fail;
2229 /* Check strong discouragement of reload of non-constant
2230 into class THIS_ALTERNATIVE. */
2231 if (! CONSTANT_P (op) && ! no_regs_p
2232 && (targetm.preferred_reload_class
2233 (op, this_alternative) == NO_REGS
2234 || (curr_static_id->operand[nop].type == OP_OUT
2235 && (targetm.preferred_output_reload_class
2236 (op, this_alternative) == NO_REGS))))
2238 if (lra_dump_file != NULL)
2239 fprintf (lra_dump_file,
2240 " %d Non-prefered reload: reject+=%d\n",
2241 nop, LRA_MAX_REJECT);
2242 reject += LRA_MAX_REJECT;
2245 if (! (MEM_P (op) && offmemok)
2246 && ! (const_to_mem && constmemok))
2248 /* We prefer to reload pseudos over reloading other
2249 things, since such reloads may be able to be
2250 eliminated later. So bump REJECT in other cases.
2251 Don't do this in the case where we are forcing a
2252 constant into memory and it will then win since
2253 we don't want to have a different alternative
2254 match then. */
2255 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2257 if (lra_dump_file != NULL)
2258 fprintf
2259 (lra_dump_file,
2260 " %d Non-pseudo reload: reject+=2\n",
2261 nop);
2262 reject += 2;
2265 if (! no_regs_p)
2266 reload_nregs
2267 += ira_reg_class_max_nregs[this_alternative][mode];
2269 if (SMALL_REGISTER_CLASS_P (this_alternative))
2271 if (lra_dump_file != NULL)
2272 fprintf
2273 (lra_dump_file,
2274 " %d Small class reload: reject+=%d\n",
2275 nop, LRA_LOSER_COST_FACTOR / 2);
2276 reject += LRA_LOSER_COST_FACTOR / 2;
2280 /* We are trying to spill pseudo into memory. It is
2281 usually more costly than moving to a hard register
2282 although it might takes the same number of
2283 reloads. */
2284 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2286 if (lra_dump_file != NULL)
2287 fprintf
2288 (lra_dump_file,
2289 " %d Spill pseudo in memory: reject+=3\n",
2290 nop);
2291 reject += 3;
2294 #ifdef SECONDARY_MEMORY_NEEDED
2295 /* If reload requires moving value through secondary
2296 memory, it will need one more insn at least. */
2297 if (this_alternative != NO_REGS
2298 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2299 && ((curr_static_id->operand[nop].type != OP_OUT
2300 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2301 GET_MODE (op)))
2302 || (curr_static_id->operand[nop].type != OP_IN
2303 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2304 GET_MODE (op)))))
2305 losers++;
2306 #endif
2307 /* Input reloads can be inherited more often than output
2308 reloads can be removed, so penalize output
2309 reloads. */
2310 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2312 if (lra_dump_file != NULL)
2313 fprintf
2314 (lra_dump_file,
2315 " %d Non input pseudo reload: reject++\n",
2316 nop);
2317 reject++;
2321 if (early_clobber_p && ! scratch_p)
2323 if (lra_dump_file != NULL)
2324 fprintf (lra_dump_file,
2325 " %d Early clobber: reject++\n", nop);
2326 reject++;
2328 /* ??? We check early clobbers after processing all operands
2329 (see loop below) and there we update the costs more.
2330 Should we update the cost (may be approximately) here
2331 because of early clobber register reloads or it is a rare
2332 or non-important thing to be worth to do it. */
2333 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2334 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2336 if (lra_dump_file != NULL)
2337 fprintf (lra_dump_file,
2338 " alt=%d,overall=%d,losers=%d -- refuse\n",
2339 nalt, overall, losers);
2340 goto fail;
2343 curr_alt[nop] = this_alternative;
2344 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2345 curr_alt_win[nop] = this_alternative_win;
2346 curr_alt_match_win[nop] = this_alternative_match_win;
2347 curr_alt_offmemok[nop] = this_alternative_offmemok;
2348 curr_alt_matches[nop] = this_alternative_matches;
2350 if (this_alternative_matches >= 0
2351 && !did_match && !this_alternative_win)
2352 curr_alt_win[this_alternative_matches] = false;
2354 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2355 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2357 if (curr_insn_set != NULL_RTX && n_operands == 2
2358 /* Prevent processing non-move insns. */
2359 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2360 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2361 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2362 && REG_P (no_subreg_reg_operand[0])
2363 && REG_P (no_subreg_reg_operand[1])
2364 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2365 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2366 || (! curr_alt_win[0] && curr_alt_win[1]
2367 && REG_P (no_subreg_reg_operand[1])
2368 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2369 || (curr_alt_win[0] && ! curr_alt_win[1]
2370 && REG_P (no_subreg_reg_operand[0])
2371 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2372 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2373 no_subreg_reg_operand[1])
2374 || (targetm.preferred_reload_class
2375 (no_subreg_reg_operand[1],
2376 (enum reg_class) curr_alt[1]) != NO_REGS))
2377 /* If it is a result of recent elimination in move
2378 insn we can transform it into an add still by
2379 using this alternative. */
2380 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2382 /* We have a move insn and a new reload insn will be similar
2383 to the current insn. We should avoid such situation as it
2384 results in LRA cycling. */
2385 overall += LRA_MAX_REJECT;
2387 ok_p = true;
2388 curr_alt_dont_inherit_ops_num = 0;
2389 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2391 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2392 HARD_REG_SET temp_set;
2394 i = early_clobbered_nops[nop];
2395 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2396 || hard_regno[i] < 0)
2397 continue;
2398 lra_assert (operand_reg[i] != NULL_RTX);
2399 clobbered_hard_regno = hard_regno[i];
2400 CLEAR_HARD_REG_SET (temp_set);
2401 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2402 first_conflict_j = last_conflict_j = -1;
2403 for (j = 0; j < n_operands; j++)
2404 if (j == i
2405 /* We don't want process insides of match_operator and
2406 match_parallel because otherwise we would process
2407 their operands once again generating a wrong
2408 code. */
2409 || curr_static_id->operand[j].is_operator)
2410 continue;
2411 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2412 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2413 continue;
2414 /* If we don't reload j-th operand, check conflicts. */
2415 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2416 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2418 if (first_conflict_j < 0)
2419 first_conflict_j = j;
2420 last_conflict_j = j;
2422 if (last_conflict_j < 0)
2423 continue;
2424 /* If earlyclobber operand conflicts with another
2425 non-matching operand which is actually the same register
2426 as the earlyclobber operand, it is better to reload the
2427 another operand as an operand matching the earlyclobber
2428 operand can be also the same. */
2429 if (first_conflict_j == last_conflict_j
2430 && operand_reg[last_conflict_j]
2431 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2432 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2434 curr_alt_win[last_conflict_j] = false;
2435 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2436 = last_conflict_j;
2437 losers++;
2438 /* Early clobber was already reflected in REJECT. */
2439 lra_assert (reject > 0);
2440 if (lra_dump_file != NULL)
2441 fprintf
2442 (lra_dump_file,
2443 " %d Conflict early clobber reload: reject--\n",
2445 reject--;
2446 overall += LRA_LOSER_COST_FACTOR - 1;
2448 else
2450 /* We need to reload early clobbered register and the
2451 matched registers. */
2452 for (j = 0; j < n_operands; j++)
2453 if (curr_alt_matches[j] == i)
2455 curr_alt_match_win[j] = false;
2456 losers++;
2457 overall += LRA_LOSER_COST_FACTOR;
2459 if (! curr_alt_match_win[i])
2460 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2461 else
2463 /* Remember pseudos used for match reloads are never
2464 inherited. */
2465 lra_assert (curr_alt_matches[i] >= 0);
2466 curr_alt_win[curr_alt_matches[i]] = false;
2468 curr_alt_win[i] = curr_alt_match_win[i] = false;
2469 losers++;
2470 /* Early clobber was already reflected in REJECT. */
2471 lra_assert (reject > 0);
2472 if (lra_dump_file != NULL)
2473 fprintf
2474 (lra_dump_file,
2475 " %d Matched conflict early clobber reloads:"
2476 "reject--\n",
2478 reject--;
2479 overall += LRA_LOSER_COST_FACTOR - 1;
2482 if (lra_dump_file != NULL)
2483 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2484 nalt, overall, losers, reload_nregs);
2486 /* If this alternative can be made to work by reloading, and it
2487 needs less reloading than the others checked so far, record
2488 it as the chosen goal for reloading. */
2489 if ((best_losers != 0 && losers == 0)
2490 || (((best_losers == 0 && losers == 0)
2491 || (best_losers != 0 && losers != 0))
2492 && (best_overall > overall
2493 || (best_overall == overall
2494 /* If the cost of the reloads is the same,
2495 prefer alternative which requires minimal
2496 number of reload regs. */
2497 && (reload_nregs < best_reload_nregs
2498 || (reload_nregs == best_reload_nregs
2499 && (best_reload_sum < reload_sum
2500 || (best_reload_sum == reload_sum
2501 && nalt < goal_alt_number))))))))
2503 for (nop = 0; nop < n_operands; nop++)
2505 goal_alt_win[nop] = curr_alt_win[nop];
2506 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2507 goal_alt_matches[nop] = curr_alt_matches[nop];
2508 goal_alt[nop] = curr_alt[nop];
2509 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2511 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2512 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2513 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2514 goal_alt_swapped = curr_swapped;
2515 best_overall = overall;
2516 best_losers = losers;
2517 best_reload_nregs = reload_nregs;
2518 best_reload_sum = reload_sum;
2519 goal_alt_number = nalt;
2521 if (losers == 0)
2522 /* Everything is satisfied. Do not process alternatives
2523 anymore. */
2524 break;
2525 fail:
2528 return ok_p;
2531 /* Return 1 if ADDR is a valid memory address for mode MODE in address
2532 space AS, and check that each pseudo has the proper kind of hard
2533 reg. */
2534 static int
2535 valid_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2536 rtx addr, addr_space_t as)
2538 #ifdef GO_IF_LEGITIMATE_ADDRESS
2539 lra_assert (ADDR_SPACE_GENERIC_P (as));
2540 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
2541 return 0;
2543 win:
2544 return 1;
2545 #else
2546 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
2547 #endif
2550 /* Return whether address AD is valid. */
2552 static bool
2553 valid_address_p (struct address_info *ad)
2555 /* Some ports do not check displacements for eliminable registers,
2556 so we replace them temporarily with the elimination target. */
2557 rtx saved_base_reg = NULL_RTX;
2558 rtx saved_index_reg = NULL_RTX;
2559 rtx *base_term = strip_subreg (ad->base_term);
2560 rtx *index_term = strip_subreg (ad->index_term);
2561 if (base_term != NULL)
2563 saved_base_reg = *base_term;
2564 lra_eliminate_reg_if_possible (base_term);
2565 if (ad->base_term2 != NULL)
2566 *ad->base_term2 = *ad->base_term;
2568 if (index_term != NULL)
2570 saved_index_reg = *index_term;
2571 lra_eliminate_reg_if_possible (index_term);
2573 bool ok_p = valid_address_p (ad->mode, *ad->outer, ad->as);
2574 if (saved_base_reg != NULL_RTX)
2576 *base_term = saved_base_reg;
2577 if (ad->base_term2 != NULL)
2578 *ad->base_term2 = *ad->base_term;
2580 if (saved_index_reg != NULL_RTX)
2581 *index_term = saved_index_reg;
2582 return ok_p;
2585 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2586 static rtx
2587 base_plus_disp_to_reg (struct address_info *ad)
2589 enum reg_class cl;
2590 rtx new_reg;
2592 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2593 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2594 get_index_code (ad));
2595 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2596 cl, "base + disp");
2597 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2598 return new_reg;
2601 /* Return true if we can add a displacement to address AD, even if that
2602 makes the address invalid. The fix-up code requires any new address
2603 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2604 static bool
2605 can_add_disp_p (struct address_info *ad)
2607 return (!ad->autoinc_p
2608 && ad->segment == NULL
2609 && ad->base == ad->base_term
2610 && ad->disp == ad->disp_term);
2613 /* Make equiv substitution in address AD. Return true if a substitution
2614 was made. */
2615 static bool
2616 equiv_address_substitution (struct address_info *ad)
2618 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2619 HOST_WIDE_INT disp, scale;
2620 bool change_p;
2622 base_term = strip_subreg (ad->base_term);
2623 if (base_term == NULL)
2624 base_reg = new_base_reg = NULL_RTX;
2625 else
2627 base_reg = *base_term;
2628 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2630 index_term = strip_subreg (ad->index_term);
2631 if (index_term == NULL)
2632 index_reg = new_index_reg = NULL_RTX;
2633 else
2635 index_reg = *index_term;
2636 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2638 if (base_reg == new_base_reg && index_reg == new_index_reg)
2639 return false;
2640 disp = 0;
2641 change_p = false;
2642 if (lra_dump_file != NULL)
2644 fprintf (lra_dump_file, "Changing address in insn %d ",
2645 INSN_UID (curr_insn));
2646 dump_value_slim (lra_dump_file, *ad->outer, 1);
2648 if (base_reg != new_base_reg)
2650 if (REG_P (new_base_reg))
2652 *base_term = new_base_reg;
2653 change_p = true;
2655 else if (GET_CODE (new_base_reg) == PLUS
2656 && REG_P (XEXP (new_base_reg, 0))
2657 && CONST_INT_P (XEXP (new_base_reg, 1))
2658 && can_add_disp_p (ad))
2660 disp += INTVAL (XEXP (new_base_reg, 1));
2661 *base_term = XEXP (new_base_reg, 0);
2662 change_p = true;
2664 if (ad->base_term2 != NULL)
2665 *ad->base_term2 = *ad->base_term;
2667 if (index_reg != new_index_reg)
2669 if (REG_P (new_index_reg))
2671 *index_term = new_index_reg;
2672 change_p = true;
2674 else if (GET_CODE (new_index_reg) == PLUS
2675 && REG_P (XEXP (new_index_reg, 0))
2676 && CONST_INT_P (XEXP (new_index_reg, 1))
2677 && can_add_disp_p (ad)
2678 && (scale = get_index_scale (ad)))
2680 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2681 *index_term = XEXP (new_index_reg, 0);
2682 change_p = true;
2685 if (disp != 0)
2687 if (ad->disp != NULL)
2688 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2689 else
2691 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2692 update_address (ad);
2694 change_p = true;
2696 if (lra_dump_file != NULL)
2698 if (! change_p)
2699 fprintf (lra_dump_file, " -- no change\n");
2700 else
2702 fprintf (lra_dump_file, " on equiv ");
2703 dump_value_slim (lra_dump_file, *ad->outer, 1);
2704 fprintf (lra_dump_file, "\n");
2707 return change_p;
2710 /* Major function to make reloads for an address in operand NOP.
2711 The supported cases are:
2713 1) an address that existed before LRA started, at which point it
2714 must have been valid. These addresses are subject to elimination
2715 and may have become invalid due to the elimination offset being out
2716 of range.
2718 2) an address created by forcing a constant to memory
2719 (force_const_to_mem). The initial form of these addresses might
2720 not be valid, and it is this function's job to make them valid.
2722 3) a frame address formed from a register and a (possibly zero)
2723 constant offset. As above, these addresses might not be valid and
2724 this function must make them so.
2726 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2727 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2728 address. Return true for any RTL change. */
2729 static bool
2730 process_address (int nop, rtx *before, rtx *after)
2732 struct address_info ad;
2733 rtx new_reg;
2734 rtx op = *curr_id->operand_loc[nop];
2735 const char *constraint = curr_static_id->operand[nop].constraint;
2736 bool change_p;
2738 if (constraint[0] == 'p'
2739 || EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint))
2740 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2741 else if (MEM_P (op))
2742 decompose_mem_address (&ad, op);
2743 else if (GET_CODE (op) == SUBREG
2744 && MEM_P (SUBREG_REG (op)))
2745 decompose_mem_address (&ad, SUBREG_REG (op));
2746 else
2747 return false;
2748 change_p = equiv_address_substitution (&ad);
2749 if (ad.base_term != NULL
2750 && (process_addr_reg
2751 (ad.base_term, before,
2752 (ad.autoinc_p
2753 && !(REG_P (*ad.base_term)
2754 && find_regno_note (curr_insn, REG_DEAD,
2755 REGNO (*ad.base_term)) != NULL_RTX)
2756 ? after : NULL),
2757 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2758 get_index_code (&ad)))))
2760 change_p = true;
2761 if (ad.base_term2 != NULL)
2762 *ad.base_term2 = *ad.base_term;
2764 if (ad.index_term != NULL
2765 && process_addr_reg (ad.index_term, before, NULL, INDEX_REG_CLASS))
2766 change_p = true;
2768 #ifdef EXTRA_CONSTRAINT_STR
2769 /* Target hooks sometimes reject extra constraint addresses -- use
2770 EXTRA_CONSTRAINT_STR for the validation. */
2771 if (constraint[0] != 'p'
2772 && EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint)
2773 && EXTRA_CONSTRAINT_STR (op, constraint[0], constraint))
2774 return change_p;
2775 #endif
2777 /* There are three cases where the shape of *AD.INNER may now be invalid:
2779 1) the original address was valid, but either elimination or
2780 equiv_address_substitution was applied and that made
2781 the address invalid.
2783 2) the address is an invalid symbolic address created by
2784 force_const_to_mem.
2786 3) the address is a frame address with an invalid offset.
2788 All these cases involve a non-autoinc address, so there is no
2789 point revalidating other types. */
2790 if (ad.autoinc_p || valid_address_p (&ad))
2791 return change_p;
2793 /* Any index existed before LRA started, so we can assume that the
2794 presence and shape of the index is valid. */
2795 push_to_sequence (*before);
2796 lra_assert (ad.disp == ad.disp_term);
2797 if (ad.base == NULL)
2799 if (ad.index == NULL)
2801 int code = -1;
2802 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2803 SCRATCH, SCRATCH);
2804 rtx addr = *ad.inner;
2806 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2807 #ifdef HAVE_lo_sum
2809 rtx insn;
2810 rtx last = get_last_insn ();
2812 /* addr => lo_sum (new_base, addr), case (2) above. */
2813 insn = emit_insn (gen_rtx_SET
2814 (VOIDmode, new_reg,
2815 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2816 code = recog_memoized (insn);
2817 if (code >= 0)
2819 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2820 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2822 /* Try to put lo_sum into register. */
2823 insn = emit_insn (gen_rtx_SET
2824 (VOIDmode, new_reg,
2825 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2826 code = recog_memoized (insn);
2827 if (code >= 0)
2829 *ad.inner = new_reg;
2830 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2832 *ad.inner = addr;
2833 code = -1;
2839 if (code < 0)
2840 delete_insns_since (last);
2842 #endif
2843 if (code < 0)
2845 /* addr => new_base, case (2) above. */
2846 lra_emit_move (new_reg, addr);
2847 *ad.inner = new_reg;
2850 else
2852 /* index * scale + disp => new base + index * scale,
2853 case (1) above. */
2854 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
2855 GET_CODE (*ad.index));
2857 lra_assert (INDEX_REG_CLASS != NO_REGS);
2858 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
2859 lra_emit_move (new_reg, *ad.disp);
2860 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2861 new_reg, *ad.index);
2864 else if (ad.index == NULL)
2866 int regno;
2867 enum reg_class cl;
2868 rtx set, insns, last_insn;
2869 /* base + disp => new base, cases (1) and (3) above. */
2870 /* Another option would be to reload the displacement into an
2871 index register. However, postreload has code to optimize
2872 address reloads that have the same base and different
2873 displacements, so reloading into an index register would
2874 not necessarily be a win. */
2875 start_sequence ();
2876 new_reg = base_plus_disp_to_reg (&ad);
2877 insns = get_insns ();
2878 last_insn = get_last_insn ();
2879 /* If we generated at least two insns, try last insn source as
2880 an address. If we succeed, we generate one less insn. */
2881 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
2882 && GET_CODE (SET_SRC (set)) == PLUS
2883 && REG_P (XEXP (SET_SRC (set), 0))
2884 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
2886 *ad.inner = SET_SRC (set);
2887 if (valid_address_p (ad.mode, *ad.outer, ad.as))
2889 *ad.base_term = XEXP (SET_SRC (set), 0);
2890 *ad.disp_term = XEXP (SET_SRC (set), 1);
2891 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2892 get_index_code (&ad));
2893 regno = REGNO (*ad.base_term);
2894 if (regno >= FIRST_PSEUDO_REGISTER
2895 && cl != lra_get_allocno_class (regno))
2896 lra_change_class (regno, cl, " Change to", true);
2897 new_reg = SET_SRC (set);
2898 delete_insns_since (PREV_INSN (last_insn));
2901 end_sequence ();
2902 emit_insn (insns);
2903 *ad.inner = new_reg;
2905 else
2907 /* base + scale * index + disp => new base + scale * index,
2908 case (1) above. */
2909 new_reg = base_plus_disp_to_reg (&ad);
2910 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2911 new_reg, *ad.index);
2913 *before = get_insns ();
2914 end_sequence ();
2915 return true;
2918 /* Emit insns to reload VALUE into a new register. VALUE is an
2919 auto-increment or auto-decrement RTX whose operand is a register or
2920 memory location; so reloading involves incrementing that location.
2921 IN is either identical to VALUE, or some cheaper place to reload
2922 value being incremented/decremented from.
2924 INC_AMOUNT is the number to increment or decrement by (always
2925 positive and ignored for POST_MODIFY/PRE_MODIFY).
2927 Return pseudo containing the result. */
2928 static rtx
2929 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
2931 /* REG or MEM to be copied and incremented. */
2932 rtx incloc = XEXP (value, 0);
2933 /* Nonzero if increment after copying. */
2934 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
2935 || GET_CODE (value) == POST_MODIFY);
2936 rtx last;
2937 rtx inc;
2938 rtx add_insn;
2939 int code;
2940 rtx real_in = in == value ? incloc : in;
2941 rtx result;
2942 bool plus_p = true;
2944 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
2946 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
2947 || GET_CODE (XEXP (value, 1)) == MINUS);
2948 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
2949 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
2950 inc = XEXP (XEXP (value, 1), 1);
2952 else
2954 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
2955 inc_amount = -inc_amount;
2957 inc = GEN_INT (inc_amount);
2960 if (! post && REG_P (incloc))
2961 result = incloc;
2962 else
2963 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
2964 "INC/DEC result");
2966 if (real_in != result)
2968 /* First copy the location to the result register. */
2969 lra_assert (REG_P (result));
2970 emit_insn (gen_move_insn (result, real_in));
2973 /* We suppose that there are insns to add/sub with the constant
2974 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
2975 old reload worked with this assumption. If the assumption
2976 becomes wrong, we should use approach in function
2977 base_plus_disp_to_reg. */
2978 if (in == value)
2980 /* See if we can directly increment INCLOC. */
2981 last = get_last_insn ();
2982 add_insn = emit_insn (plus_p
2983 ? gen_add2_insn (incloc, inc)
2984 : gen_sub2_insn (incloc, inc));
2986 code = recog_memoized (add_insn);
2987 if (code >= 0)
2989 if (! post && result != incloc)
2990 emit_insn (gen_move_insn (result, incloc));
2991 return result;
2993 delete_insns_since (last);
2996 /* If couldn't do the increment directly, must increment in RESULT.
2997 The way we do this depends on whether this is pre- or
2998 post-increment. For pre-increment, copy INCLOC to the reload
2999 register, increment it there, then save back. */
3000 if (! post)
3002 if (real_in != result)
3003 emit_insn (gen_move_insn (result, real_in));
3004 if (plus_p)
3005 emit_insn (gen_add2_insn (result, inc));
3006 else
3007 emit_insn (gen_sub2_insn (result, inc));
3008 if (result != incloc)
3009 emit_insn (gen_move_insn (incloc, result));
3011 else
3013 /* Post-increment.
3015 Because this might be a jump insn or a compare, and because
3016 RESULT may not be available after the insn in an input
3017 reload, we must do the incrementing before the insn being
3018 reloaded for.
3020 We have already copied IN to RESULT. Increment the copy in
3021 RESULT, save that back, then decrement RESULT so it has
3022 the original value. */
3023 if (plus_p)
3024 emit_insn (gen_add2_insn (result, inc));
3025 else
3026 emit_insn (gen_sub2_insn (result, inc));
3027 emit_insn (gen_move_insn (incloc, result));
3028 /* Restore non-modified value for the result. We prefer this
3029 way because it does not require an additional hard
3030 register. */
3031 if (plus_p)
3033 if (CONST_INT_P (inc))
3034 emit_insn (gen_add2_insn (result,
3035 gen_int_mode (-INTVAL (inc),
3036 GET_MODE (result))));
3037 else
3038 emit_insn (gen_sub2_insn (result, inc));
3040 else
3041 emit_insn (gen_add2_insn (result, inc));
3043 return result;
3046 /* Return true if the current move insn does not need processing as we
3047 already know that it satisfies its constraints. */
3048 static bool
3049 simple_move_p (void)
3051 rtx dest, src;
3052 enum reg_class dclass, sclass;
3054 lra_assert (curr_insn_set != NULL_RTX);
3055 dest = SET_DEST (curr_insn_set);
3056 src = SET_SRC (curr_insn_set);
3057 return ((dclass = get_op_class (dest)) != NO_REGS
3058 && (sclass = get_op_class (src)) != NO_REGS
3059 /* The backend guarantees that register moves of cost 2
3060 never need reloads. */
3061 && targetm.register_move_cost (GET_MODE (src), dclass, sclass) == 2);
3064 /* Swap operands NOP and NOP + 1. */
3065 static inline void
3066 swap_operands (int nop)
3068 enum machine_mode mode = curr_operand_mode[nop];
3069 curr_operand_mode[nop] = curr_operand_mode[nop + 1];
3070 curr_operand_mode[nop + 1] = mode;
3071 rtx x = *curr_id->operand_loc[nop];
3072 *curr_id->operand_loc[nop] = *curr_id->operand_loc[nop + 1];
3073 *curr_id->operand_loc[nop + 1] = x;
3074 /* Swap the duplicates too. */
3075 lra_update_dup (curr_id, nop);
3076 lra_update_dup (curr_id, nop + 1);
3079 /* Main entry point of the constraint code: search the body of the
3080 current insn to choose the best alternative. It is mimicking insn
3081 alternative cost calculation model of former reload pass. That is
3082 because machine descriptions were written to use this model. This
3083 model can be changed in future. Make commutative operand exchange
3084 if it is chosen.
3086 Return true if some RTL changes happened during function call. */
3087 static bool
3088 curr_insn_transform (void)
3090 int i, j, k;
3091 int n_operands;
3092 int n_alternatives;
3093 int commutative;
3094 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3095 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3096 rtx before, after;
3097 bool alt_p = false;
3098 /* Flag that the insn has been changed through a transformation. */
3099 bool change_p;
3100 bool sec_mem_p;
3101 #ifdef SECONDARY_MEMORY_NEEDED
3102 bool use_sec_mem_p;
3103 #endif
3104 int max_regno_before;
3105 int reused_alternative_num;
3107 curr_insn_set = single_set (curr_insn);
3108 if (curr_insn_set != NULL_RTX && simple_move_p ())
3109 return false;
3111 no_input_reloads_p = no_output_reloads_p = false;
3112 goal_alt_number = -1;
3113 change_p = sec_mem_p = false;
3114 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3115 reloads; neither are insns that SET cc0. Insns that use CC0 are
3116 not allowed to have any input reloads. */
3117 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3118 no_output_reloads_p = true;
3120 #ifdef HAVE_cc0
3121 if (reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3122 no_input_reloads_p = true;
3123 if (reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3124 no_output_reloads_p = true;
3125 #endif
3127 n_operands = curr_static_id->n_operands;
3128 n_alternatives = curr_static_id->n_alternatives;
3130 /* Just return "no reloads" if insn has no operands with
3131 constraints. */
3132 if (n_operands == 0 || n_alternatives == 0)
3133 return false;
3135 max_regno_before = max_reg_num ();
3137 for (i = 0; i < n_operands; i++)
3139 goal_alt_matched[i][0] = -1;
3140 goal_alt_matches[i] = -1;
3143 commutative = curr_static_id->commutative;
3145 /* Now see what we need for pseudos that didn't get hard regs or got
3146 the wrong kind of hard reg. For this, we must consider all the
3147 operands together against the register constraints. */
3149 best_losers = best_overall = INT_MAX;
3150 best_reload_sum = 0;
3152 curr_swapped = false;
3153 goal_alt_swapped = false;
3155 /* Make equivalence substitution and memory subreg elimination
3156 before address processing because an address legitimacy can
3157 depend on memory mode. */
3158 for (i = 0; i < n_operands; i++)
3160 rtx op = *curr_id->operand_loc[i];
3161 rtx subst, old = op;
3162 bool op_change_p = false;
3164 if (GET_CODE (old) == SUBREG)
3165 old = SUBREG_REG (old);
3166 subst = get_equiv_with_elimination (old, curr_insn);
3167 if (subst != old)
3169 subst = copy_rtx (subst);
3170 lra_assert (REG_P (old));
3171 if (GET_CODE (op) == SUBREG)
3172 SUBREG_REG (op) = subst;
3173 else
3174 *curr_id->operand_loc[i] = subst;
3175 if (lra_dump_file != NULL)
3177 fprintf (lra_dump_file,
3178 "Changing pseudo %d in operand %i of insn %u on equiv ",
3179 REGNO (old), i, INSN_UID (curr_insn));
3180 dump_value_slim (lra_dump_file, subst, 1);
3181 fprintf (lra_dump_file, "\n");
3183 op_change_p = change_p = true;
3185 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3187 change_p = true;
3188 lra_update_dup (curr_id, i);
3192 /* Reload address registers and displacements. We do it before
3193 finding an alternative because of memory constraints. */
3194 before = after = NULL_RTX;
3195 for (i = 0; i < n_operands; i++)
3196 if (! curr_static_id->operand[i].is_operator
3197 && process_address (i, &before, &after))
3199 change_p = true;
3200 lra_update_dup (curr_id, i);
3203 if (change_p)
3204 /* If we've changed the instruction then any alternative that
3205 we chose previously may no longer be valid. */
3206 lra_set_used_insn_alternative (curr_insn, -1);
3208 if (curr_insn_set != NULL_RTX
3209 && check_and_process_move (&change_p, &sec_mem_p))
3210 return change_p;
3212 try_swapped:
3214 reused_alternative_num = curr_id->used_insn_alternative;
3215 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3216 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3217 reused_alternative_num, INSN_UID (curr_insn));
3219 if (process_alt_operands (reused_alternative_num))
3220 alt_p = true;
3222 /* If insn is commutative (it's safe to exchange a certain pair of
3223 operands) then we need to try each alternative twice, the second
3224 time matching those two operands as if we had exchanged them. To
3225 do this, really exchange them in operands.
3227 If we have just tried the alternatives the second time, return
3228 operands to normal and drop through. */
3230 if (reused_alternative_num < 0 && commutative >= 0)
3232 curr_swapped = !curr_swapped;
3233 if (curr_swapped)
3235 swap_operands (commutative);
3236 goto try_swapped;
3238 else
3239 swap_operands (commutative);
3242 if (! alt_p && ! sec_mem_p)
3244 /* No alternative works with reloads?? */
3245 if (INSN_CODE (curr_insn) >= 0)
3246 fatal_insn ("unable to generate reloads for:", curr_insn);
3247 error_for_asm (curr_insn,
3248 "inconsistent operand constraints in an %<asm%>");
3249 /* Avoid further trouble with this insn. */
3250 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3251 lra_invalidate_insn_data (curr_insn);
3252 return true;
3255 /* If the best alternative is with operands 1 and 2 swapped, swap
3256 them. Update the operand numbers of any reloads already
3257 pushed. */
3259 if (goal_alt_swapped)
3261 if (lra_dump_file != NULL)
3262 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3263 INSN_UID (curr_insn));
3265 /* Swap the duplicates too. */
3266 swap_operands (commutative);
3267 change_p = true;
3270 #ifdef SECONDARY_MEMORY_NEEDED
3271 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3272 too conservatively. So we use the secondary memory only if there
3273 is no any alternative without reloads. */
3274 use_sec_mem_p = false;
3275 if (! alt_p)
3276 use_sec_mem_p = true;
3277 else if (sec_mem_p)
3279 for (i = 0; i < n_operands; i++)
3280 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3281 break;
3282 use_sec_mem_p = i < n_operands;
3285 if (use_sec_mem_p)
3287 rtx new_reg, src, dest, rld;
3288 enum machine_mode sec_mode, rld_mode;
3290 lra_assert (sec_mem_p);
3291 lra_assert (curr_static_id->operand[0].type == OP_OUT
3292 && curr_static_id->operand[1].type == OP_IN);
3293 dest = *curr_id->operand_loc[0];
3294 src = *curr_id->operand_loc[1];
3295 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3296 ? dest : src);
3297 rld_mode = GET_MODE (rld);
3298 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3299 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3300 #else
3301 sec_mode = rld_mode;
3302 #endif
3303 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3304 NO_REGS, "secondary");
3305 /* If the mode is changed, it should be wider. */
3306 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3307 if (sec_mode != rld_mode)
3309 /* If the target says specifically to use another mode for
3310 secondary memory moves we can not reuse the original
3311 insn. */
3312 after = emit_spill_move (false, new_reg, dest);
3313 lra_process_new_insns (curr_insn, NULL_RTX, after,
3314 "Inserting the sec. move");
3315 /* We may have non null BEFORE here (e.g. after address
3316 processing. */
3317 push_to_sequence (before);
3318 before = emit_spill_move (true, new_reg, src);
3319 emit_insn (before);
3320 before = get_insns ();
3321 end_sequence ();
3322 lra_process_new_insns (curr_insn, before, NULL_RTX, "Changing on");
3323 lra_set_insn_deleted (curr_insn);
3325 else if (dest == rld)
3327 *curr_id->operand_loc[0] = new_reg;
3328 after = emit_spill_move (false, new_reg, dest);
3329 lra_process_new_insns (curr_insn, NULL_RTX, after,
3330 "Inserting the sec. move");
3332 else
3334 *curr_id->operand_loc[1] = new_reg;
3335 /* See comments above. */
3336 push_to_sequence (before);
3337 before = emit_spill_move (true, new_reg, src);
3338 emit_insn (before);
3339 before = get_insns ();
3340 end_sequence ();
3341 lra_process_new_insns (curr_insn, before, NULL_RTX,
3342 "Inserting the sec. move");
3344 lra_update_insn_regno_info (curr_insn);
3345 return true;
3347 #endif
3349 lra_assert (goal_alt_number >= 0);
3350 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3352 if (lra_dump_file != NULL)
3354 const char *p;
3356 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3357 goal_alt_number, INSN_UID (curr_insn));
3358 for (i = 0; i < n_operands; i++)
3360 p = (curr_static_id->operand_alternative
3361 [goal_alt_number * n_operands + i].constraint);
3362 if (*p == '\0')
3363 continue;
3364 fprintf (lra_dump_file, " (%d) ", i);
3365 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3366 fputc (*p, lra_dump_file);
3368 if (INSN_CODE (curr_insn) >= 0
3369 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3370 fprintf (lra_dump_file, " {%s}", p);
3371 if (curr_id->sp_offset != 0)
3372 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3373 curr_id->sp_offset);
3374 fprintf (lra_dump_file, "\n");
3377 /* Right now, for any pair of operands I and J that are required to
3378 match, with J < I, goal_alt_matches[I] is J. Add I to
3379 goal_alt_matched[J]. */
3381 for (i = 0; i < n_operands; i++)
3382 if ((j = goal_alt_matches[i]) >= 0)
3384 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3386 /* We allow matching one output operand and several input
3387 operands. */
3388 lra_assert (k == 0
3389 || (curr_static_id->operand[j].type == OP_OUT
3390 && curr_static_id->operand[i].type == OP_IN
3391 && (curr_static_id->operand
3392 [goal_alt_matched[j][0]].type == OP_IN)));
3393 goal_alt_matched[j][k] = i;
3394 goal_alt_matched[j][k + 1] = -1;
3397 for (i = 0; i < n_operands; i++)
3398 goal_alt_win[i] |= goal_alt_match_win[i];
3400 /* Any constants that aren't allowed and can't be reloaded into
3401 registers are here changed into memory references. */
3402 for (i = 0; i < n_operands; i++)
3403 if (goal_alt_win[i])
3405 int regno;
3406 enum reg_class new_class;
3407 rtx reg = *curr_id->operand_loc[i];
3409 if (GET_CODE (reg) == SUBREG)
3410 reg = SUBREG_REG (reg);
3412 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3414 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3416 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3418 lra_assert (ok_p);
3419 lra_change_class (regno, new_class, " Change to", true);
3423 else
3425 const char *constraint;
3426 char c;
3427 rtx op = *curr_id->operand_loc[i];
3428 rtx subreg = NULL_RTX;
3429 enum machine_mode mode = curr_operand_mode[i];
3431 if (GET_CODE (op) == SUBREG)
3433 subreg = op;
3434 op = SUBREG_REG (op);
3435 mode = GET_MODE (op);
3438 if (CONST_POOL_OK_P (mode, op)
3439 && ((targetm.preferred_reload_class
3440 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3441 || no_input_reloads_p))
3443 rtx tem = force_const_mem (mode, op);
3445 change_p = true;
3446 if (subreg != NULL_RTX)
3447 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3449 *curr_id->operand_loc[i] = tem;
3450 lra_update_dup (curr_id, i);
3451 process_address (i, &before, &after);
3453 /* If the alternative accepts constant pool refs directly
3454 there will be no reload needed at all. */
3455 if (subreg != NULL_RTX)
3456 continue;
3457 /* Skip alternatives before the one requested. */
3458 constraint = (curr_static_id->operand_alternative
3459 [goal_alt_number * n_operands + i].constraint);
3460 for (;
3461 (c = *constraint) && c != ',' && c != '#';
3462 constraint += CONSTRAINT_LEN (c, constraint))
3464 if (c == TARGET_MEM_CONSTRAINT || c == 'o')
3465 break;
3466 #ifdef EXTRA_CONSTRAINT_STR
3467 if (EXTRA_MEMORY_CONSTRAINT (c, constraint)
3468 && EXTRA_CONSTRAINT_STR (tem, c, constraint))
3469 break;
3470 #endif
3472 if (c == '\0' || c == ',' || c == '#')
3473 continue;
3475 goal_alt_win[i] = true;
3479 for (i = 0; i < n_operands; i++)
3481 int regno;
3482 bool optional_p = false;
3483 rtx old, new_reg;
3484 rtx op = *curr_id->operand_loc[i];
3486 if (goal_alt_win[i])
3488 if (goal_alt[i] == NO_REGS
3489 && REG_P (op)
3490 /* When we assign NO_REGS it means that we will not
3491 assign a hard register to the scratch pseudo by
3492 assigment pass and the scratch pseudo will be
3493 spilled. Spilled scratch pseudos are transformed
3494 back to scratches at the LRA end. */
3495 && lra_former_scratch_operand_p (curr_insn, i))
3497 int regno = REGNO (op);
3498 lra_change_class (regno, NO_REGS, " Change to", true);
3499 if (lra_get_regno_hard_regno (regno) >= 0)
3500 /* We don't have to mark all insn affected by the
3501 spilled pseudo as there is only one such insn, the
3502 current one. */
3503 reg_renumber[regno] = -1;
3505 /* We can do an optional reload. If the pseudo got a hard
3506 reg, we might improve the code through inheritance. If
3507 it does not get a hard register we coalesce memory/memory
3508 moves later. Ignore move insns to avoid cycling. */
3509 if (! lra_simple_p
3510 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3511 && goal_alt[i] != NO_REGS && REG_P (op)
3512 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3513 && regno < new_regno_start
3514 && ! lra_former_scratch_p (regno)
3515 && reg_renumber[regno] < 0
3516 && (curr_insn_set == NULL_RTX
3517 || !((REG_P (SET_SRC (curr_insn_set))
3518 || MEM_P (SET_SRC (curr_insn_set))
3519 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3520 && (REG_P (SET_DEST (curr_insn_set))
3521 || MEM_P (SET_DEST (curr_insn_set))
3522 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3523 optional_p = true;
3524 else
3525 continue;
3528 /* Operands that match previous ones have already been handled. */
3529 if (goal_alt_matches[i] >= 0)
3530 continue;
3532 /* We should not have an operand with a non-offsettable address
3533 appearing where an offsettable address will do. It also may
3534 be a case when the address should be special in other words
3535 not a general one (e.g. it needs no index reg). */
3536 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3538 enum reg_class rclass;
3539 rtx *loc = &XEXP (op, 0);
3540 enum rtx_code code = GET_CODE (*loc);
3542 push_to_sequence (before);
3543 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3544 MEM, SCRATCH);
3545 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3546 new_reg = emit_inc (rclass, *loc, *loc,
3547 /* This value does not matter for MODIFY. */
3548 GET_MODE_SIZE (GET_MODE (op)));
3549 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass,
3550 "offsetable address", &new_reg))
3551 lra_emit_move (new_reg, *loc);
3552 before = get_insns ();
3553 end_sequence ();
3554 *loc = new_reg;
3555 lra_update_dup (curr_id, i);
3557 else if (goal_alt_matched[i][0] == -1)
3559 enum machine_mode mode;
3560 rtx reg, *loc;
3561 int hard_regno, byte;
3562 enum op_type type = curr_static_id->operand[i].type;
3564 loc = curr_id->operand_loc[i];
3565 mode = curr_operand_mode[i];
3566 if (GET_CODE (*loc) == SUBREG)
3568 reg = SUBREG_REG (*loc);
3569 byte = SUBREG_BYTE (*loc);
3570 if (REG_P (reg)
3571 /* Strict_low_part requires reload the register not
3572 the sub-register. */
3573 && (curr_static_id->operand[i].strict_low
3574 || (GET_MODE_SIZE (mode)
3575 <= GET_MODE_SIZE (GET_MODE (reg))
3576 && (hard_regno
3577 = get_try_hard_regno (REGNO (reg))) >= 0
3578 && (simplify_subreg_regno
3579 (hard_regno,
3580 GET_MODE (reg), byte, mode) < 0)
3581 && (goal_alt[i] == NO_REGS
3582 || (simplify_subreg_regno
3583 (ira_class_hard_regs[goal_alt[i]][0],
3584 GET_MODE (reg), byte, mode) >= 0)))))
3586 loc = &SUBREG_REG (*loc);
3587 mode = GET_MODE (*loc);
3590 old = *loc;
3591 if (get_reload_reg (type, mode, old, goal_alt[i], "", &new_reg)
3592 && type != OP_OUT)
3594 push_to_sequence (before);
3595 lra_emit_move (new_reg, old);
3596 before = get_insns ();
3597 end_sequence ();
3599 *loc = new_reg;
3600 if (type != OP_IN
3601 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3603 start_sequence ();
3604 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3605 emit_insn (after);
3606 after = get_insns ();
3607 end_sequence ();
3608 *loc = new_reg;
3610 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3611 if (goal_alt_dont_inherit_ops[j] == i)
3613 lra_set_regno_unique_value (REGNO (new_reg));
3614 break;
3616 lra_update_dup (curr_id, i);
3618 else if (curr_static_id->operand[i].type == OP_IN
3619 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3620 == OP_OUT))
3622 /* generate reloads for input and matched outputs. */
3623 match_inputs[0] = i;
3624 match_inputs[1] = -1;
3625 match_reload (goal_alt_matched[i][0], match_inputs,
3626 goal_alt[i], &before, &after);
3628 else if (curr_static_id->operand[i].type == OP_OUT
3629 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3630 == OP_IN))
3631 /* Generate reloads for output and matched inputs. */
3632 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3633 else if (curr_static_id->operand[i].type == OP_IN
3634 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3635 == OP_IN))
3637 /* Generate reloads for matched inputs. */
3638 match_inputs[0] = i;
3639 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3640 match_inputs[j + 1] = k;
3641 match_inputs[j + 1] = -1;
3642 match_reload (-1, match_inputs, goal_alt[i], &before, &after);
3644 else
3645 /* We must generate code in any case when function
3646 process_alt_operands decides that it is possible. */
3647 gcc_unreachable ();
3648 if (optional_p)
3650 lra_assert (REG_P (op));
3651 regno = REGNO (op);
3652 op = *curr_id->operand_loc[i]; /* Substitution. */
3653 if (GET_CODE (op) == SUBREG)
3654 op = SUBREG_REG (op);
3655 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3656 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3657 lra_reg_info[REGNO (op)].restore_regno = regno;
3658 if (lra_dump_file != NULL)
3659 fprintf (lra_dump_file,
3660 " Making reload reg %d for reg %d optional\n",
3661 REGNO (op), regno);
3664 if (before != NULL_RTX || after != NULL_RTX
3665 || max_regno_before != max_reg_num ())
3666 change_p = true;
3667 if (change_p)
3669 lra_update_operator_dups (curr_id);
3670 /* Something changes -- process the insn. */
3671 lra_update_insn_regno_info (curr_insn);
3673 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3674 return change_p;
3677 /* Return true if X is in LIST. */
3678 static bool
3679 in_list_p (rtx x, rtx list)
3681 for (; list != NULL_RTX; list = XEXP (list, 1))
3682 if (XEXP (list, 0) == x)
3683 return true;
3684 return false;
3687 /* Return true if X contains an allocatable hard register (if
3688 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3689 static bool
3690 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3692 int i, j;
3693 const char *fmt;
3694 enum rtx_code code;
3696 code = GET_CODE (x);
3697 if (REG_P (x))
3699 int regno = REGNO (x);
3700 HARD_REG_SET alloc_regs;
3702 if (hard_reg_p)
3704 if (regno >= FIRST_PSEUDO_REGISTER)
3705 regno = lra_get_regno_hard_regno (regno);
3706 if (regno < 0)
3707 return false;
3708 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3709 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3711 else
3713 if (regno < FIRST_PSEUDO_REGISTER)
3714 return false;
3715 if (! spilled_p)
3716 return true;
3717 return lra_get_regno_hard_regno (regno) < 0;
3720 fmt = GET_RTX_FORMAT (code);
3721 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3723 if (fmt[i] == 'e')
3725 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3726 return true;
3728 else if (fmt[i] == 'E')
3730 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3731 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3732 return true;
3735 return false;
3738 /* Process all regs in location *LOC and change them on equivalent
3739 substitution. Return true if any change was done. */
3740 static bool
3741 loc_equivalence_change_p (rtx *loc)
3743 rtx subst, reg, x = *loc;
3744 bool result = false;
3745 enum rtx_code code = GET_CODE (x);
3746 const char *fmt;
3747 int i, j;
3749 if (code == SUBREG)
3751 reg = SUBREG_REG (x);
3752 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
3753 && GET_MODE (subst) == VOIDmode)
3755 /* We cannot reload debug location. Simplify subreg here
3756 while we know the inner mode. */
3757 *loc = simplify_gen_subreg (GET_MODE (x), subst,
3758 GET_MODE (reg), SUBREG_BYTE (x));
3759 return true;
3762 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
3764 *loc = subst;
3765 return true;
3768 /* Scan all the operand sub-expressions. */
3769 fmt = GET_RTX_FORMAT (code);
3770 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3772 if (fmt[i] == 'e')
3773 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
3774 else if (fmt[i] == 'E')
3775 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3776 result
3777 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
3779 return result;
3782 /* Similar to loc_equivalence_change_p, but for use as
3783 simplify_replace_fn_rtx callback. DATA is insn for which the
3784 elimination is done. If it null we don't do the elimination. */
3785 static rtx
3786 loc_equivalence_callback (rtx loc, const_rtx, void *data)
3788 if (!REG_P (loc))
3789 return NULL_RTX;
3791 rtx subst = (data == NULL
3792 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx) data));
3793 if (subst != loc)
3794 return subst;
3796 return NULL_RTX;
3799 /* Maximum number of generated reload insns per an insn. It is for
3800 preventing this pass cycling in a bug case. */
3801 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
3803 /* The current iteration number of this LRA pass. */
3804 int lra_constraint_iter;
3806 /* The current iteration number of this LRA pass after the last spill
3807 pass. */
3808 int lra_constraint_iter_after_spill;
3810 /* True if we substituted equiv which needs checking register
3811 allocation correctness because the equivalent value contains
3812 allocatable hard registers or when we restore multi-register
3813 pseudo. */
3814 bool lra_risky_transformations_p;
3816 /* Return true if REGNO is referenced in more than one block. */
3817 static bool
3818 multi_block_pseudo_p (int regno)
3820 basic_block bb = NULL;
3821 unsigned int uid;
3822 bitmap_iterator bi;
3824 if (regno < FIRST_PSEUDO_REGISTER)
3825 return false;
3827 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
3828 if (bb == NULL)
3829 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
3830 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
3831 return true;
3832 return false;
3835 /* Return true if LIST contains a deleted insn. */
3836 static bool
3837 contains_deleted_insn_p (rtx list)
3839 for (; list != NULL_RTX; list = XEXP (list, 1))
3840 if (NOTE_P (XEXP (list, 0))
3841 && NOTE_KIND (XEXP (list, 0)) == NOTE_INSN_DELETED)
3842 return true;
3843 return false;
3846 /* Return true if X contains a pseudo dying in INSN. */
3847 static bool
3848 dead_pseudo_p (rtx x, rtx insn)
3850 int i, j;
3851 const char *fmt;
3852 enum rtx_code code;
3854 if (REG_P (x))
3855 return (insn != NULL_RTX
3856 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
3857 code = GET_CODE (x);
3858 fmt = GET_RTX_FORMAT (code);
3859 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3861 if (fmt[i] == 'e')
3863 if (dead_pseudo_p (XEXP (x, i), insn))
3864 return true;
3866 else if (fmt[i] == 'E')
3868 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3869 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
3870 return true;
3873 return false;
3876 /* Return true if INSN contains a dying pseudo in INSN right hand
3877 side. */
3878 static bool
3879 insn_rhs_dead_pseudo_p (rtx insn)
3881 rtx set = single_set (insn);
3883 gcc_assert (set != NULL);
3884 return dead_pseudo_p (SET_SRC (set), insn);
3887 /* Return true if any init insn of REGNO contains a dying pseudo in
3888 insn right hand side. */
3889 static bool
3890 init_insn_rhs_dead_pseudo_p (int regno)
3892 rtx insns = ira_reg_equiv[regno].init_insns;
3894 if (insns == NULL)
3895 return false;
3896 if (INSN_P (insns))
3897 return insn_rhs_dead_pseudo_p (insns);
3898 for (; insns != NULL_RTX; insns = XEXP (insns, 1))
3899 if (insn_rhs_dead_pseudo_p (XEXP (insns, 0)))
3900 return true;
3901 return false;
3904 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
3905 reverse only if we have one init insn with given REGNO as a
3906 source. */
3907 static bool
3908 reverse_equiv_p (int regno)
3910 rtx insns, set;
3912 if ((insns = ira_reg_equiv[regno].init_insns) == NULL_RTX)
3913 return false;
3914 if (! INSN_P (XEXP (insns, 0))
3915 || XEXP (insns, 1) != NULL_RTX)
3916 return false;
3917 if ((set = single_set (XEXP (insns, 0))) == NULL_RTX)
3918 return false;
3919 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
3922 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
3923 call this function only for non-reverse equivalence. */
3924 static bool
3925 contains_reloaded_insn_p (int regno)
3927 rtx set;
3928 rtx list = ira_reg_equiv[regno].init_insns;
3930 for (; list != NULL_RTX; list = XEXP (list, 1))
3931 if ((set = single_set (XEXP (list, 0))) == NULL_RTX
3932 || ! REG_P (SET_DEST (set))
3933 || (int) REGNO (SET_DEST (set)) != regno)
3934 return true;
3935 return false;
3938 /* Entry function of LRA constraint pass. Return true if the
3939 constraint pass did change the code. */
3940 bool
3941 lra_constraints (bool first_p)
3943 bool changed_p;
3944 int i, hard_regno, new_insns_num;
3945 unsigned int min_len, new_min_len, uid;
3946 rtx set, x, reg, dest_reg;
3947 basic_block last_bb;
3948 bitmap_head equiv_insn_bitmap;
3949 bitmap_iterator bi;
3951 lra_constraint_iter++;
3952 if (lra_dump_file != NULL)
3953 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
3954 lra_constraint_iter);
3955 lra_constraint_iter_after_spill++;
3956 if (lra_constraint_iter_after_spill > LRA_MAX_CONSTRAINT_ITERATION_NUMBER)
3957 internal_error
3958 ("Maximum number of LRA constraint passes is achieved (%d)\n",
3959 LRA_MAX_CONSTRAINT_ITERATION_NUMBER);
3960 changed_p = false;
3961 lra_risky_transformations_p = false;
3962 new_insn_uid_start = get_max_uid ();
3963 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
3964 /* Mark used hard regs for target stack size calulations. */
3965 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
3966 if (lra_reg_info[i].nrefs != 0
3967 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
3969 int j, nregs;
3971 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
3972 for (j = 0; j < nregs; j++)
3973 df_set_regs_ever_live (hard_regno + j, true);
3975 /* Do elimination before the equivalence processing as we can spill
3976 some pseudos during elimination. */
3977 lra_eliminate (false, first_p);
3978 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
3979 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
3980 if (lra_reg_info[i].nrefs != 0)
3982 ira_reg_equiv[i].profitable_p = true;
3983 reg = regno_reg_rtx[i];
3984 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
3986 bool pseudo_p = contains_reg_p (x, false, false);
3988 /* After RTL transformation, we can not guarantee that
3989 pseudo in the substitution was not reloaded which might
3990 make equivalence invalid. For example, in reverse
3991 equiv of p0
3993 p0 <- ...
3995 equiv_mem <- p0
3997 the memory address register was reloaded before the 2nd
3998 insn. */
3999 if ((! first_p && pseudo_p)
4000 /* We don't use DF for compilation speed sake. So it
4001 is problematic to update live info when we use an
4002 equivalence containing pseudos in more than one
4003 BB. */
4004 || (pseudo_p && multi_block_pseudo_p (i))
4005 /* If an init insn was deleted for some reason, cancel
4006 the equiv. We could update the equiv insns after
4007 transformations including an equiv insn deletion
4008 but it is not worthy as such cases are extremely
4009 rare. */
4010 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4011 /* If it is not a reverse equivalence, we check that a
4012 pseudo in rhs of the init insn is not dying in the
4013 insn. Otherwise, the live info at the beginning of
4014 the corresponding BB might be wrong after we
4015 removed the insn. When the equiv can be a
4016 constant, the right hand side of the init insn can
4017 be a pseudo. */
4018 || (! reverse_equiv_p (i)
4019 && (init_insn_rhs_dead_pseudo_p (i)
4020 /* If we reloaded the pseudo in an equivalence
4021 init insn, we can not remove the equiv init
4022 insns and the init insns might write into
4023 const memory in this case. */
4024 || contains_reloaded_insn_p (i)))
4025 /* Prevent access beyond equivalent memory for
4026 paradoxical subregs. */
4027 || (MEM_P (x)
4028 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4029 > GET_MODE_SIZE (GET_MODE (x)))))
4030 ira_reg_equiv[i].defined_p = false;
4031 if (contains_reg_p (x, false, true))
4032 ira_reg_equiv[i].profitable_p = false;
4033 if (get_equiv (reg) != reg)
4034 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4037 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4038 update_equiv (i);
4039 /* We should add all insns containing pseudos which should be
4040 substituted by their equivalences. */
4041 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4042 lra_push_insn_by_uid (uid);
4043 min_len = lra_insn_stack_length ();
4044 new_insns_num = 0;
4045 last_bb = NULL;
4046 changed_p = false;
4047 while ((new_min_len = lra_insn_stack_length ()) != 0)
4049 curr_insn = lra_pop_insn ();
4050 --new_min_len;
4051 curr_bb = BLOCK_FOR_INSN (curr_insn);
4052 if (curr_bb != last_bb)
4054 last_bb = curr_bb;
4055 bb_reload_num = lra_curr_reload_num;
4057 if (min_len > new_min_len)
4059 min_len = new_min_len;
4060 new_insns_num = 0;
4062 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4063 internal_error
4064 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4065 MAX_RELOAD_INSNS_NUMBER);
4066 new_insns_num++;
4067 if (DEBUG_INSN_P (curr_insn))
4069 /* We need to check equivalence in debug insn and change
4070 pseudo to the equivalent value if necessary. */
4071 curr_id = lra_get_insn_recog_data (curr_insn);
4072 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4074 rtx old = *curr_id->operand_loc[0];
4075 *curr_id->operand_loc[0]
4076 = simplify_replace_fn_rtx (old, NULL_RTX,
4077 loc_equivalence_callback, curr_insn);
4078 if (old != *curr_id->operand_loc[0])
4080 lra_update_insn_regno_info (curr_insn);
4081 changed_p = true;
4085 else if (INSN_P (curr_insn))
4087 if ((set = single_set (curr_insn)) != NULL_RTX)
4089 dest_reg = SET_DEST (set);
4090 /* The equivalence pseudo could be set up as SUBREG in a
4091 case when it is a call restore insn in a mode
4092 different from the pseudo mode. */
4093 if (GET_CODE (dest_reg) == SUBREG)
4094 dest_reg = SUBREG_REG (dest_reg);
4095 if ((REG_P (dest_reg)
4096 && (x = get_equiv (dest_reg)) != dest_reg
4097 /* Remove insns which set up a pseudo whose value
4098 can not be changed. Such insns might be not in
4099 init_insns because we don't update equiv data
4100 during insn transformations.
4102 As an example, let suppose that a pseudo got
4103 hard register and on the 1st pass was not
4104 changed to equivalent constant. We generate an
4105 additional insn setting up the pseudo because of
4106 secondary memory movement. Then the pseudo is
4107 spilled and we use the equiv constant. In this
4108 case we should remove the additional insn and
4109 this insn is not init_insns list. */
4110 && (! MEM_P (x) || MEM_READONLY_P (x)
4111 /* Check that this is actually an insn setting
4112 up the equivalence. */
4113 || in_list_p (curr_insn,
4114 ira_reg_equiv
4115 [REGNO (dest_reg)].init_insns)))
4116 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4117 && in_list_p (curr_insn,
4118 ira_reg_equiv
4119 [REGNO (SET_SRC (set))].init_insns)))
4121 /* This is equiv init insn of pseudo which did not get a
4122 hard register -- remove the insn. */
4123 if (lra_dump_file != NULL)
4125 fprintf (lra_dump_file,
4126 " Removing equiv init insn %i (freq=%d)\n",
4127 INSN_UID (curr_insn),
4128 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4129 dump_insn_slim (lra_dump_file, curr_insn);
4131 if (contains_reg_p (x, true, false))
4132 lra_risky_transformations_p = true;
4133 lra_set_insn_deleted (curr_insn);
4134 continue;
4137 curr_id = lra_get_insn_recog_data (curr_insn);
4138 curr_static_id = curr_id->insn_static_data;
4139 init_curr_insn_input_reloads ();
4140 init_curr_operand_mode ();
4141 if (curr_insn_transform ())
4142 changed_p = true;
4143 /* Check non-transformed insns too for equiv change as USE
4144 or CLOBBER don't need reloads but can contain pseudos
4145 being changed on their equivalences. */
4146 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4147 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4149 lra_update_insn_regno_info (curr_insn);
4150 changed_p = true;
4154 bitmap_clear (&equiv_insn_bitmap);
4155 /* If we used a new hard regno, changed_p should be true because the
4156 hard reg is assigned to a new pseudo. */
4157 #ifdef ENABLE_CHECKING
4158 if (! changed_p)
4160 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4161 if (lra_reg_info[i].nrefs != 0
4162 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4164 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4166 for (j = 0; j < nregs; j++)
4167 lra_assert (df_regs_ever_live_p (hard_regno + j));
4170 #endif
4171 return changed_p;
4174 /* Initiate the LRA constraint pass. It is done once per
4175 function. */
4176 void
4177 lra_constraints_init (void)
4181 /* Finalize the LRA constraint pass. It is done once per
4182 function. */
4183 void
4184 lra_constraints_finish (void)
4190 /* This page contains code to do inheritance/split
4191 transformations. */
4193 /* Number of reloads passed so far in current EBB. */
4194 static int reloads_num;
4196 /* Number of calls passed so far in current EBB. */
4197 static int calls_num;
4199 /* Current reload pseudo check for validity of elements in
4200 USAGE_INSNS. */
4201 static int curr_usage_insns_check;
4203 /* Info about last usage of registers in EBB to do inheritance/split
4204 transformation. Inheritance transformation is done from a spilled
4205 pseudo and split transformations from a hard register or a pseudo
4206 assigned to a hard register. */
4207 struct usage_insns
4209 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4210 value INSNS is valid. The insns is chain of optional debug insns
4211 and a finishing non-debug insn using the corresponding reg. The
4212 value is also used to mark the registers which are set up in the
4213 current insn. The negated insn uid is used for this. */
4214 int check;
4215 /* Value of global reloads_num at the last insn in INSNS. */
4216 int reloads_num;
4217 /* Value of global reloads_nums at the last insn in INSNS. */
4218 int calls_num;
4219 /* It can be true only for splitting. And it means that the restore
4220 insn should be put after insn given by the following member. */
4221 bool after_p;
4222 /* Next insns in the current EBB which use the original reg and the
4223 original reg value is not changed between the current insn and
4224 the next insns. In order words, e.g. for inheritance, if we need
4225 to use the original reg value again in the next insns we can try
4226 to use the value in a hard register from a reload insn of the
4227 current insn. */
4228 rtx insns;
4231 /* Map: regno -> corresponding pseudo usage insns. */
4232 static struct usage_insns *usage_insns;
4234 static void
4235 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4237 usage_insns[regno].check = curr_usage_insns_check;
4238 usage_insns[regno].insns = insn;
4239 usage_insns[regno].reloads_num = reloads_num;
4240 usage_insns[regno].calls_num = calls_num;
4241 usage_insns[regno].after_p = after_p;
4244 /* The function is used to form list REGNO usages which consists of
4245 optional debug insns finished by a non-debug insn using REGNO.
4246 RELOADS_NUM is current number of reload insns processed so far. */
4247 static void
4248 add_next_usage_insn (int regno, rtx insn, int reloads_num)
4250 rtx next_usage_insns;
4252 if (usage_insns[regno].check == curr_usage_insns_check
4253 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4254 && DEBUG_INSN_P (insn))
4256 /* Check that we did not add the debug insn yet. */
4257 if (next_usage_insns != insn
4258 && (GET_CODE (next_usage_insns) != INSN_LIST
4259 || XEXP (next_usage_insns, 0) != insn))
4260 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4261 next_usage_insns);
4263 else if (NONDEBUG_INSN_P (insn))
4264 setup_next_usage_insn (regno, insn, reloads_num, false);
4265 else
4266 usage_insns[regno].check = 0;
4269 /* Replace all references to register OLD_REGNO in *LOC with pseudo
4270 register NEW_REG. Return true if any change was made. */
4271 static bool
4272 substitute_pseudo (rtx *loc, int old_regno, rtx new_reg)
4274 rtx x = *loc;
4275 bool result = false;
4276 enum rtx_code code;
4277 const char *fmt;
4278 int i, j;
4280 if (x == NULL_RTX)
4281 return false;
4283 code = GET_CODE (x);
4284 if (code == REG && (int) REGNO (x) == old_regno)
4286 enum machine_mode mode = GET_MODE (*loc);
4287 enum machine_mode inner_mode = GET_MODE (new_reg);
4289 if (mode != inner_mode)
4291 if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
4292 || ! SCALAR_INT_MODE_P (inner_mode))
4293 new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
4294 else
4295 new_reg = gen_lowpart_SUBREG (mode, new_reg);
4297 *loc = new_reg;
4298 return true;
4301 /* Scan all the operand sub-expressions. */
4302 fmt = GET_RTX_FORMAT (code);
4303 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4305 if (fmt[i] == 'e')
4307 if (substitute_pseudo (&XEXP (x, i), old_regno, new_reg))
4308 result = true;
4310 else if (fmt[i] == 'E')
4312 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4313 if (substitute_pseudo (&XVECEXP (x, i, j), old_regno, new_reg))
4314 result = true;
4317 return result;
4320 /* Return first non-debug insn in list USAGE_INSNS. */
4321 static rtx
4322 skip_usage_debug_insns (rtx usage_insns)
4324 rtx insn;
4326 /* Skip debug insns. */
4327 for (insn = usage_insns;
4328 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4329 insn = XEXP (insn, 1))
4331 return insn;
4334 /* Return true if we need secondary memory moves for insn in
4335 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4336 into the insn. */
4337 static bool
4338 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4339 rtx usage_insns ATTRIBUTE_UNUSED)
4341 #ifndef SECONDARY_MEMORY_NEEDED
4342 return false;
4343 #else
4344 rtx insn, set, dest;
4345 enum reg_class cl;
4347 if (inher_cl == ALL_REGS
4348 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4349 return false;
4350 lra_assert (INSN_P (insn));
4351 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4352 return false;
4353 dest = SET_DEST (set);
4354 if (! REG_P (dest))
4355 return false;
4356 lra_assert (inher_cl != NO_REGS);
4357 cl = get_reg_class (REGNO (dest));
4358 return (cl != NO_REGS && cl != ALL_REGS
4359 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4360 #endif
4363 /* Registers involved in inheritance/split in the current EBB
4364 (inheritance/split pseudos and original registers). */
4365 static bitmap_head check_only_regs;
4367 /* Do inheritance transformations for insn INSN, which defines (if
4368 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4369 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4370 form as the "insns" field of usage_insns. Return true if we
4371 succeed in such transformation.
4373 The transformations look like:
4375 p <- ... i <- ...
4376 ... p <- i (new insn)
4377 ... =>
4378 <- ... p ... <- ... i ...
4380 ... i <- p (new insn)
4381 <- ... p ... <- ... i ...
4382 ... =>
4383 <- ... p ... <- ... i ...
4384 where p is a spilled original pseudo and i is a new inheritance pseudo.
4387 The inheritance pseudo has the smallest class of two classes CL and
4388 class of ORIGINAL REGNO. */
4389 static bool
4390 inherit_reload_reg (bool def_p, int original_regno,
4391 enum reg_class cl, rtx insn, rtx next_usage_insns)
4393 enum reg_class rclass = lra_get_allocno_class (original_regno);
4394 rtx original_reg = regno_reg_rtx[original_regno];
4395 rtx new_reg, new_insns, usage_insn;
4397 lra_assert (! usage_insns[original_regno].after_p);
4398 if (lra_dump_file != NULL)
4399 fprintf (lra_dump_file,
4400 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4401 if (! ira_reg_classes_intersect_p[cl][rclass])
4403 if (lra_dump_file != NULL)
4405 fprintf (lra_dump_file,
4406 " Rejecting inheritance for %d "
4407 "because of disjoint classes %s and %s\n",
4408 original_regno, reg_class_names[cl],
4409 reg_class_names[rclass]);
4410 fprintf (lra_dump_file,
4411 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4413 return false;
4415 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4416 /* We don't use a subset of two classes because it can be
4417 NO_REGS. This transformation is still profitable in most
4418 cases even if the classes are not intersected as register
4419 move is probably cheaper than a memory load. */
4420 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4422 if (lra_dump_file != NULL)
4423 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4424 reg_class_names[cl], reg_class_names[rclass]);
4426 rclass = cl;
4428 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4430 /* Reject inheritance resulting in secondary memory moves.
4431 Otherwise, there is a danger in LRA cycling. Also such
4432 transformation will be unprofitable. */
4433 if (lra_dump_file != NULL)
4435 rtx insn = skip_usage_debug_insns (next_usage_insns);
4436 rtx set = single_set (insn);
4438 lra_assert (set != NULL_RTX);
4440 rtx dest = SET_DEST (set);
4442 lra_assert (REG_P (dest));
4443 fprintf (lra_dump_file,
4444 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4445 "as secondary mem is needed\n",
4446 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4447 original_regno, reg_class_names[rclass]);
4448 fprintf (lra_dump_file,
4449 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4451 return false;
4453 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4454 rclass, "inheritance");
4455 start_sequence ();
4456 if (def_p)
4457 emit_move_insn (original_reg, new_reg);
4458 else
4459 emit_move_insn (new_reg, original_reg);
4460 new_insns = get_insns ();
4461 end_sequence ();
4462 if (NEXT_INSN (new_insns) != NULL_RTX)
4464 if (lra_dump_file != NULL)
4466 fprintf (lra_dump_file,
4467 " Rejecting inheritance %d->%d "
4468 "as it results in 2 or more insns:\n",
4469 original_regno, REGNO (new_reg));
4470 dump_rtl_slim (lra_dump_file, new_insns, NULL_RTX, -1, 0);
4471 fprintf (lra_dump_file,
4472 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4474 return false;
4476 substitute_pseudo (&insn, original_regno, new_reg);
4477 lra_update_insn_regno_info (insn);
4478 if (! def_p)
4479 /* We now have a new usage insn for original regno. */
4480 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4481 if (lra_dump_file != NULL)
4482 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4483 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4484 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4485 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4486 bitmap_set_bit (&check_only_regs, original_regno);
4487 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4488 if (def_p)
4489 lra_process_new_insns (insn, NULL_RTX, new_insns,
4490 "Add original<-inheritance");
4491 else
4492 lra_process_new_insns (insn, new_insns, NULL_RTX,
4493 "Add inheritance<-original");
4494 while (next_usage_insns != NULL_RTX)
4496 if (GET_CODE (next_usage_insns) != INSN_LIST)
4498 usage_insn = next_usage_insns;
4499 lra_assert (NONDEBUG_INSN_P (usage_insn));
4500 next_usage_insns = NULL;
4502 else
4504 usage_insn = XEXP (next_usage_insns, 0);
4505 lra_assert (DEBUG_INSN_P (usage_insn));
4506 next_usage_insns = XEXP (next_usage_insns, 1);
4508 substitute_pseudo (&usage_insn, original_regno, new_reg);
4509 lra_update_insn_regno_info (usage_insn);
4510 if (lra_dump_file != NULL)
4512 fprintf (lra_dump_file,
4513 " Inheritance reuse change %d->%d (bb%d):\n",
4514 original_regno, REGNO (new_reg),
4515 BLOCK_FOR_INSN (usage_insn)->index);
4516 dump_insn_slim (lra_dump_file, usage_insn);
4519 if (lra_dump_file != NULL)
4520 fprintf (lra_dump_file,
4521 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4522 return true;
4525 /* Return true if we need a caller save/restore for pseudo REGNO which
4526 was assigned to a hard register. */
4527 static inline bool
4528 need_for_call_save_p (int regno)
4530 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4531 return (usage_insns[regno].calls_num < calls_num
4532 && (overlaps_hard_reg_set_p
4533 (call_used_reg_set,
4534 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4535 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4536 PSEUDO_REGNO_MODE (regno))));
4539 /* Global registers occurring in the current EBB. */
4540 static bitmap_head ebb_global_regs;
4542 /* Return true if we need a split for hard register REGNO or pseudo
4543 REGNO which was assigned to a hard register.
4544 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4545 used for reloads since the EBB end. It is an approximation of the
4546 used hard registers in the split range. The exact value would
4547 require expensive calculations. If we were aggressive with
4548 splitting because of the approximation, the split pseudo will save
4549 the same hard register assignment and will be removed in the undo
4550 pass. We still need the approximation because too aggressive
4551 splitting would result in too inaccurate cost calculation in the
4552 assignment pass because of too many generated moves which will be
4553 probably removed in the undo pass. */
4554 static inline bool
4555 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4557 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4559 lra_assert (hard_regno >= 0);
4560 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4561 /* Don't split eliminable hard registers, otherwise we can
4562 split hard registers like hard frame pointer, which
4563 lives on BB start/end according to DF-infrastructure,
4564 when there is a pseudo assigned to the register and
4565 living in the same BB. */
4566 && (regno >= FIRST_PSEUDO_REGISTER
4567 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4568 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4569 /* Don't split call clobbered hard regs living through
4570 calls, otherwise we might have a check problem in the
4571 assign sub-pass as in the most cases (exception is a
4572 situation when lra_risky_transformations_p value is
4573 true) the assign pass assumes that all pseudos living
4574 through calls are assigned to call saved hard regs. */
4575 && (regno >= FIRST_PSEUDO_REGISTER
4576 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4577 || usage_insns[regno].calls_num == calls_num)
4578 /* We need at least 2 reloads to make pseudo splitting
4579 profitable. We should provide hard regno splitting in
4580 any case to solve 1st insn scheduling problem when
4581 moving hard register definition up might result in
4582 impossibility to find hard register for reload pseudo of
4583 small register class. */
4584 && (usage_insns[regno].reloads_num
4585 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 2) < reloads_num)
4586 && (regno < FIRST_PSEUDO_REGISTER
4587 /* For short living pseudos, spilling + inheritance can
4588 be considered a substitution for splitting.
4589 Therefore we do not splitting for local pseudos. It
4590 decreases also aggressiveness of splitting. The
4591 minimal number of references is chosen taking into
4592 account that for 2 references splitting has no sense
4593 as we can just spill the pseudo. */
4594 || (regno >= FIRST_PSEUDO_REGISTER
4595 && lra_reg_info[regno].nrefs > 3
4596 && bitmap_bit_p (&ebb_global_regs, regno))))
4597 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4600 /* Return class for the split pseudo created from original pseudo with
4601 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4602 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4603 results in no secondary memory movements. */
4604 static enum reg_class
4605 choose_split_class (enum reg_class allocno_class,
4606 int hard_regno ATTRIBUTE_UNUSED,
4607 enum machine_mode mode ATTRIBUTE_UNUSED)
4609 #ifndef SECONDARY_MEMORY_NEEDED
4610 return allocno_class;
4611 #else
4612 int i;
4613 enum reg_class cl, best_cl = NO_REGS;
4614 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4615 = REGNO_REG_CLASS (hard_regno);
4617 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4618 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4619 return allocno_class;
4620 for (i = 0;
4621 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4622 i++)
4623 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4624 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4625 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4626 && (best_cl == NO_REGS
4627 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4628 best_cl = cl;
4629 return best_cl;
4630 #endif
4633 /* Do split transformations for insn INSN, which defines or uses
4634 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4635 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4636 "insns" field of usage_insns.
4638 The transformations look like:
4640 p <- ... p <- ...
4641 ... s <- p (new insn -- save)
4642 ... =>
4643 ... p <- s (new insn -- restore)
4644 <- ... p ... <- ... p ...
4646 <- ... p ... <- ... p ...
4647 ... s <- p (new insn -- save)
4648 ... =>
4649 ... p <- s (new insn -- restore)
4650 <- ... p ... <- ... p ...
4652 where p is an original pseudo got a hard register or a hard
4653 register and s is a new split pseudo. The save is put before INSN
4654 if BEFORE_P is true. Return true if we succeed in such
4655 transformation. */
4656 static bool
4657 split_reg (bool before_p, int original_regno, rtx insn, rtx next_usage_insns)
4659 enum reg_class rclass;
4660 rtx original_reg;
4661 int hard_regno, nregs;
4662 rtx new_reg, save, restore, usage_insn;
4663 bool after_p;
4664 bool call_save_p;
4666 if (original_regno < FIRST_PSEUDO_REGISTER)
4668 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4669 hard_regno = original_regno;
4670 call_save_p = false;
4671 nregs = 1;
4673 else
4675 hard_regno = reg_renumber[original_regno];
4676 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4677 rclass = lra_get_allocno_class (original_regno);
4678 original_reg = regno_reg_rtx[original_regno];
4679 call_save_p = need_for_call_save_p (original_regno);
4681 original_reg = regno_reg_rtx[original_regno];
4682 lra_assert (hard_regno >= 0);
4683 if (lra_dump_file != NULL)
4684 fprintf (lra_dump_file,
4685 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4686 if (call_save_p)
4688 enum machine_mode mode = GET_MODE (original_reg);
4690 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4691 hard_regno_nregs[hard_regno][mode],
4692 mode);
4693 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4695 else
4697 rclass = choose_split_class (rclass, hard_regno,
4698 GET_MODE (original_reg));
4699 if (rclass == NO_REGS)
4701 if (lra_dump_file != NULL)
4703 fprintf (lra_dump_file,
4704 " Rejecting split of %d(%s): "
4705 "no good reg class for %d(%s)\n",
4706 original_regno,
4707 reg_class_names[lra_get_allocno_class (original_regno)],
4708 hard_regno,
4709 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4710 fprintf
4711 (lra_dump_file,
4712 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4714 return false;
4716 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4717 rclass, "split");
4718 reg_renumber[REGNO (new_reg)] = hard_regno;
4720 save = emit_spill_move (true, new_reg, original_reg);
4721 if (NEXT_INSN (save) != NULL_RTX)
4723 lra_assert (! call_save_p);
4724 if (lra_dump_file != NULL)
4726 fprintf
4727 (lra_dump_file,
4728 " Rejecting split %d->%d resulting in > 2 %s save insns:\n",
4729 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4730 dump_rtl_slim (lra_dump_file, save, NULL_RTX, -1, 0);
4731 fprintf (lra_dump_file,
4732 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4734 return false;
4736 restore = emit_spill_move (false, new_reg, original_reg);
4737 if (NEXT_INSN (restore) != NULL_RTX)
4739 lra_assert (! call_save_p);
4740 if (lra_dump_file != NULL)
4742 fprintf (lra_dump_file,
4743 " Rejecting split %d->%d "
4744 "resulting in > 2 %s restore insns:\n",
4745 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4746 dump_rtl_slim (lra_dump_file, restore, NULL_RTX, -1, 0);
4747 fprintf (lra_dump_file,
4748 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4750 return false;
4752 after_p = usage_insns[original_regno].after_p;
4753 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4754 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4755 bitmap_set_bit (&check_only_regs, original_regno);
4756 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
4757 for (;;)
4759 if (GET_CODE (next_usage_insns) != INSN_LIST)
4761 usage_insn = next_usage_insns;
4762 break;
4764 usage_insn = XEXP (next_usage_insns, 0);
4765 lra_assert (DEBUG_INSN_P (usage_insn));
4766 next_usage_insns = XEXP (next_usage_insns, 1);
4767 substitute_pseudo (&usage_insn, original_regno, new_reg);
4768 lra_update_insn_regno_info (usage_insn);
4769 if (lra_dump_file != NULL)
4771 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
4772 original_regno, REGNO (new_reg));
4773 dump_insn_slim (lra_dump_file, usage_insn);
4776 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
4777 lra_assert (usage_insn != insn || (after_p && before_p));
4778 lra_process_new_insns (usage_insn, after_p ? NULL_RTX : restore,
4779 after_p ? restore : NULL_RTX,
4780 call_save_p
4781 ? "Add reg<-save" : "Add reg<-split");
4782 lra_process_new_insns (insn, before_p ? save : NULL_RTX,
4783 before_p ? NULL_RTX : save,
4784 call_save_p
4785 ? "Add save<-reg" : "Add split<-reg");
4786 if (nregs > 1)
4787 /* If we are trying to split multi-register. We should check
4788 conflicts on the next assignment sub-pass. IRA can allocate on
4789 sub-register levels, LRA do this on pseudos level right now and
4790 this discrepancy may create allocation conflicts after
4791 splitting. */
4792 lra_risky_transformations_p = true;
4793 if (lra_dump_file != NULL)
4794 fprintf (lra_dump_file,
4795 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4796 return true;
4799 /* Recognize that we need a split transformation for insn INSN, which
4800 defines or uses REGNO in its insn biggest MODE (we use it only if
4801 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
4802 hard registers which might be used for reloads since the EBB end.
4803 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
4804 uid before starting INSN processing. Return true if we succeed in
4805 such transformation. */
4806 static bool
4807 split_if_necessary (int regno, enum machine_mode mode,
4808 HARD_REG_SET potential_reload_hard_regs,
4809 bool before_p, rtx insn, int max_uid)
4811 bool res = false;
4812 int i, nregs = 1;
4813 rtx next_usage_insns;
4815 if (regno < FIRST_PSEUDO_REGISTER)
4816 nregs = hard_regno_nregs[regno][mode];
4817 for (i = 0; i < nregs; i++)
4818 if (usage_insns[regno + i].check == curr_usage_insns_check
4819 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
4820 /* To avoid processing the register twice or more. */
4821 && ((GET_CODE (next_usage_insns) != INSN_LIST
4822 && INSN_UID (next_usage_insns) < max_uid)
4823 || (GET_CODE (next_usage_insns) == INSN_LIST
4824 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
4825 && need_for_split_p (potential_reload_hard_regs, regno + i)
4826 && split_reg (before_p, regno + i, insn, next_usage_insns))
4827 res = true;
4828 return res;
4831 /* Check only registers living at the current program point in the
4832 current EBB. */
4833 static bitmap_head live_regs;
4835 /* Update live info in EBB given by its HEAD and TAIL insns after
4836 inheritance/split transformation. The function removes dead moves
4837 too. */
4838 static void
4839 update_ebb_live_info (rtx head, rtx tail)
4841 unsigned int j;
4842 int regno;
4843 bool live_p;
4844 rtx prev_insn, set;
4845 bool remove_p;
4846 basic_block last_bb, prev_bb, curr_bb;
4847 bitmap_iterator bi;
4848 struct lra_insn_reg *reg;
4849 edge e;
4850 edge_iterator ei;
4852 last_bb = BLOCK_FOR_INSN (tail);
4853 prev_bb = NULL;
4854 for (curr_insn = tail;
4855 curr_insn != PREV_INSN (head);
4856 curr_insn = prev_insn)
4858 prev_insn = PREV_INSN (curr_insn);
4859 /* We need to process empty blocks too. They contain
4860 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
4861 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
4862 continue;
4863 curr_bb = BLOCK_FOR_INSN (curr_insn);
4864 if (curr_bb != prev_bb)
4866 if (prev_bb != NULL)
4868 /* Update df_get_live_in (prev_bb): */
4869 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4870 if (bitmap_bit_p (&live_regs, j))
4871 bitmap_set_bit (df_get_live_in (prev_bb), j);
4872 else
4873 bitmap_clear_bit (df_get_live_in (prev_bb), j);
4875 if (curr_bb != last_bb)
4877 /* Update df_get_live_out (curr_bb): */
4878 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4880 live_p = bitmap_bit_p (&live_regs, j);
4881 if (! live_p)
4882 FOR_EACH_EDGE (e, ei, curr_bb->succs)
4883 if (bitmap_bit_p (df_get_live_in (e->dest), j))
4885 live_p = true;
4886 break;
4888 if (live_p)
4889 bitmap_set_bit (df_get_live_out (curr_bb), j);
4890 else
4891 bitmap_clear_bit (df_get_live_out (curr_bb), j);
4894 prev_bb = curr_bb;
4895 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
4897 if (! NONDEBUG_INSN_P (curr_insn))
4898 continue;
4899 curr_id = lra_get_insn_recog_data (curr_insn);
4900 remove_p = false;
4901 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
4902 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
4903 && bitmap_bit_p (&check_only_regs, regno)
4904 && ! bitmap_bit_p (&live_regs, regno))
4905 remove_p = true;
4906 /* See which defined values die here. */
4907 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4908 if (reg->type == OP_OUT && ! reg->subreg_p)
4909 bitmap_clear_bit (&live_regs, reg->regno);
4910 /* Mark each used value as live. */
4911 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4912 if (reg->type != OP_OUT
4913 && bitmap_bit_p (&check_only_regs, reg->regno))
4914 bitmap_set_bit (&live_regs, reg->regno);
4915 /* It is quite important to remove dead move insns because it
4916 means removing dead store. We don't need to process them for
4917 constraints. */
4918 if (remove_p)
4920 if (lra_dump_file != NULL)
4922 fprintf (lra_dump_file, " Removing dead insn:\n ");
4923 dump_insn_slim (lra_dump_file, curr_insn);
4925 lra_set_insn_deleted (curr_insn);
4930 /* The structure describes info to do an inheritance for the current
4931 insn. We need to collect such info first before doing the
4932 transformations because the transformations change the insn
4933 internal representation. */
4934 struct to_inherit
4936 /* Original regno. */
4937 int regno;
4938 /* Subsequent insns which can inherit original reg value. */
4939 rtx insns;
4942 /* Array containing all info for doing inheritance from the current
4943 insn. */
4944 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
4946 /* Number elements in the previous array. */
4947 static int to_inherit_num;
4949 /* Add inheritance info REGNO and INSNS. Their meaning is described in
4950 structure to_inherit. */
4951 static void
4952 add_to_inherit (int regno, rtx insns)
4954 int i;
4956 for (i = 0; i < to_inherit_num; i++)
4957 if (to_inherit[i].regno == regno)
4958 return;
4959 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
4960 to_inherit[to_inherit_num].regno = regno;
4961 to_inherit[to_inherit_num++].insns = insns;
4964 /* Return the last non-debug insn in basic block BB, or the block begin
4965 note if none. */
4966 static rtx
4967 get_last_insertion_point (basic_block bb)
4969 rtx insn;
4971 FOR_BB_INSNS_REVERSE (bb, insn)
4972 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
4973 return insn;
4974 gcc_unreachable ();
4977 /* Set up RES by registers living on edges FROM except the edge (FROM,
4978 TO) or by registers set up in a jump insn in BB FROM. */
4979 static void
4980 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
4982 rtx last;
4983 struct lra_insn_reg *reg;
4984 edge e;
4985 edge_iterator ei;
4987 lra_assert (to != NULL);
4988 bitmap_clear (res);
4989 FOR_EACH_EDGE (e, ei, from->succs)
4990 if (e->dest != to)
4991 bitmap_ior_into (res, df_get_live_in (e->dest));
4992 last = get_last_insertion_point (from);
4993 if (! JUMP_P (last))
4994 return;
4995 curr_id = lra_get_insn_recog_data (last);
4996 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4997 if (reg->type != OP_IN)
4998 bitmap_set_bit (res, reg->regno);
5001 /* Used as a temporary results of some bitmap calculations. */
5002 static bitmap_head temp_bitmap;
5004 /* Do inheritance/split transformations in EBB starting with HEAD and
5005 finishing on TAIL. We process EBB insns in the reverse order.
5006 Return true if we did any inheritance/split transformation in the
5007 EBB.
5009 We should avoid excessive splitting which results in worse code
5010 because of inaccurate cost calculations for spilling new split
5011 pseudos in such case. To achieve this we do splitting only if
5012 register pressure is high in given basic block and there are reload
5013 pseudos requiring hard registers. We could do more register
5014 pressure calculations at any given program point to avoid necessary
5015 splitting even more but it is to expensive and the current approach
5016 works well enough. */
5017 static bool
5018 inherit_in_ebb (rtx head, rtx tail)
5020 int i, src_regno, dst_regno, nregs;
5021 bool change_p, succ_p, update_reloads_num_p;
5022 rtx prev_insn, next_usage_insns, set, last_insn;
5023 enum reg_class cl;
5024 struct lra_insn_reg *reg;
5025 basic_block last_processed_bb, curr_bb = NULL;
5026 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5027 bitmap to_process;
5028 unsigned int j;
5029 bitmap_iterator bi;
5030 bool head_p, after_p;
5032 change_p = false;
5033 curr_usage_insns_check++;
5034 reloads_num = calls_num = 0;
5035 bitmap_clear (&check_only_regs);
5036 last_processed_bb = NULL;
5037 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5038 CLEAR_HARD_REG_SET (live_hard_regs);
5039 /* We don't process new insns generated in the loop. */
5040 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5042 prev_insn = PREV_INSN (curr_insn);
5043 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5044 curr_bb = BLOCK_FOR_INSN (curr_insn);
5045 if (last_processed_bb != curr_bb)
5047 /* We are at the end of BB. Add qualified living
5048 pseudos for potential splitting. */
5049 to_process = df_get_live_out (curr_bb);
5050 if (last_processed_bb != NULL)
5052 /* We are somewhere in the middle of EBB. */
5053 get_live_on_other_edges (curr_bb, last_processed_bb,
5054 &temp_bitmap);
5055 to_process = &temp_bitmap;
5057 last_processed_bb = curr_bb;
5058 last_insn = get_last_insertion_point (curr_bb);
5059 after_p = (! JUMP_P (last_insn)
5060 && (! CALL_P (last_insn)
5061 || (find_reg_note (last_insn,
5062 REG_NORETURN, NULL_RTX) == NULL_RTX
5063 && ! SIBLING_CALL_P (last_insn))));
5064 REG_SET_TO_HARD_REG_SET (live_hard_regs, df_get_live_out (curr_bb));
5065 IOR_HARD_REG_SET (live_hard_regs, eliminable_regset);
5066 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5067 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5068 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5070 if ((int) j >= lra_constraint_new_regno_start)
5071 break;
5072 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5074 if (j < FIRST_PSEUDO_REGISTER)
5075 SET_HARD_REG_BIT (live_hard_regs, j);
5076 else
5077 add_to_hard_reg_set (&live_hard_regs,
5078 PSEUDO_REGNO_MODE (j),
5079 reg_renumber[j]);
5080 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5084 src_regno = dst_regno = -1;
5085 if (NONDEBUG_INSN_P (curr_insn)
5086 && (set = single_set (curr_insn)) != NULL_RTX
5087 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5089 src_regno = REGNO (SET_SRC (set));
5090 dst_regno = REGNO (SET_DEST (set));
5092 update_reloads_num_p = true;
5093 if (src_regno < lra_constraint_new_regno_start
5094 && src_regno >= FIRST_PSEUDO_REGISTER
5095 && reg_renumber[src_regno] < 0
5096 && dst_regno >= lra_constraint_new_regno_start
5097 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5099 /* 'reload_pseudo <- original_pseudo'. */
5100 reloads_num++;
5101 update_reloads_num_p = false;
5102 succ_p = false;
5103 if (usage_insns[src_regno].check == curr_usage_insns_check
5104 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5105 succ_p = inherit_reload_reg (false, src_regno, cl,
5106 curr_insn, next_usage_insns);
5107 if (succ_p)
5108 change_p = true;
5109 else
5110 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5111 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5112 IOR_HARD_REG_SET (potential_reload_hard_regs,
5113 reg_class_contents[cl]);
5115 else if (src_regno >= lra_constraint_new_regno_start
5116 && dst_regno < lra_constraint_new_regno_start
5117 && dst_regno >= FIRST_PSEUDO_REGISTER
5118 && reg_renumber[dst_regno] < 0
5119 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5120 && usage_insns[dst_regno].check == curr_usage_insns_check
5121 && (next_usage_insns
5122 = usage_insns[dst_regno].insns) != NULL_RTX)
5124 reloads_num++;
5125 update_reloads_num_p = false;
5126 /* 'original_pseudo <- reload_pseudo'. */
5127 if (! JUMP_P (curr_insn)
5128 && inherit_reload_reg (true, dst_regno, cl,
5129 curr_insn, next_usage_insns))
5130 change_p = true;
5131 /* Invalidate. */
5132 usage_insns[dst_regno].check = 0;
5133 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5134 IOR_HARD_REG_SET (potential_reload_hard_regs,
5135 reg_class_contents[cl]);
5137 else if (INSN_P (curr_insn))
5139 int iter;
5140 int max_uid = get_max_uid ();
5142 curr_id = lra_get_insn_recog_data (curr_insn);
5143 curr_static_id = curr_id->insn_static_data;
5144 to_inherit_num = 0;
5145 /* Process insn definitions. */
5146 for (iter = 0; iter < 2; iter++)
5147 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5148 reg != NULL;
5149 reg = reg->next)
5150 if (reg->type != OP_IN
5151 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5153 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5154 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5155 && usage_insns[dst_regno].check == curr_usage_insns_check
5156 && (next_usage_insns
5157 = usage_insns[dst_regno].insns) != NULL_RTX)
5159 struct lra_insn_reg *r;
5161 for (r = curr_id->regs; r != NULL; r = r->next)
5162 if (r->type != OP_OUT && r->regno == dst_regno)
5163 break;
5164 /* Don't do inheritance if the pseudo is also
5165 used in the insn. */
5166 if (r == NULL)
5167 /* We can not do inheritance right now
5168 because the current insn reg info (chain
5169 regs) can change after that. */
5170 add_to_inherit (dst_regno, next_usage_insns);
5172 /* We can not process one reg twice here because of
5173 usage_insns invalidation. */
5174 if ((dst_regno < FIRST_PSEUDO_REGISTER
5175 || reg_renumber[dst_regno] >= 0)
5176 && ! reg->subreg_p && reg->type != OP_IN)
5178 HARD_REG_SET s;
5180 if (split_if_necessary (dst_regno, reg->biggest_mode,
5181 potential_reload_hard_regs,
5182 false, curr_insn, max_uid))
5183 change_p = true;
5184 CLEAR_HARD_REG_SET (s);
5185 if (dst_regno < FIRST_PSEUDO_REGISTER)
5186 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5187 else
5188 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5189 reg_renumber[dst_regno]);
5190 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5192 /* We should invalidate potential inheritance or
5193 splitting for the current insn usages to the next
5194 usage insns (see code below) as the output pseudo
5195 prevents this. */
5196 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5197 && reg_renumber[dst_regno] < 0)
5198 || (reg->type == OP_OUT && ! reg->subreg_p
5199 && (dst_regno < FIRST_PSEUDO_REGISTER
5200 || reg_renumber[dst_regno] >= 0)))
5202 /* Invalidate and mark definitions. */
5203 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5204 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5205 else
5207 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5208 for (i = 0; i < nregs; i++)
5209 usage_insns[dst_regno + i].check
5210 = -(int) INSN_UID (curr_insn);
5214 if (! JUMP_P (curr_insn))
5215 for (i = 0; i < to_inherit_num; i++)
5216 if (inherit_reload_reg (true, to_inherit[i].regno,
5217 ALL_REGS, curr_insn,
5218 to_inherit[i].insns))
5219 change_p = true;
5220 if (CALL_P (curr_insn))
5222 rtx cheap, pat, dest, restore;
5223 int regno, hard_regno;
5225 calls_num++;
5226 if ((cheap = find_reg_note (curr_insn,
5227 REG_RETURNED, NULL_RTX)) != NULL_RTX
5228 && ((cheap = XEXP (cheap, 0)), true)
5229 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5230 && (hard_regno = reg_renumber[regno]) >= 0
5231 /* If there are pending saves/restores, the
5232 optimization is not worth. */
5233 && usage_insns[regno].calls_num == calls_num - 1
5234 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5236 /* Restore the pseudo from the call result as
5237 REG_RETURNED note says that the pseudo value is
5238 in the call result and the pseudo is an argument
5239 of the call. */
5240 pat = PATTERN (curr_insn);
5241 if (GET_CODE (pat) == PARALLEL)
5242 pat = XVECEXP (pat, 0, 0);
5243 dest = SET_DEST (pat);
5244 start_sequence ();
5245 emit_move_insn (cheap, copy_rtx (dest));
5246 restore = get_insns ();
5247 end_sequence ();
5248 lra_process_new_insns (curr_insn, NULL, restore,
5249 "Inserting call parameter restore");
5250 /* We don't need to save/restore of the pseudo from
5251 this call. */
5252 usage_insns[regno].calls_num = calls_num;
5253 bitmap_set_bit (&check_only_regs, regno);
5256 to_inherit_num = 0;
5257 /* Process insn usages. */
5258 for (iter = 0; iter < 2; iter++)
5259 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5260 reg != NULL;
5261 reg = reg->next)
5262 if ((reg->type != OP_OUT
5263 || (reg->type == OP_OUT && reg->subreg_p))
5264 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5266 if (src_regno >= FIRST_PSEUDO_REGISTER
5267 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5269 if (usage_insns[src_regno].check == curr_usage_insns_check
5270 && (next_usage_insns
5271 = usage_insns[src_regno].insns) != NULL_RTX
5272 && NONDEBUG_INSN_P (curr_insn))
5273 add_to_inherit (src_regno, next_usage_insns);
5274 else if (usage_insns[src_regno].check
5275 != -(int) INSN_UID (curr_insn))
5276 /* Add usages but only if the reg is not set up
5277 in the same insn. */
5278 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5280 else if (src_regno < FIRST_PSEUDO_REGISTER
5281 || reg_renumber[src_regno] >= 0)
5283 bool before_p;
5284 rtx use_insn = curr_insn;
5286 before_p = (JUMP_P (curr_insn)
5287 || (CALL_P (curr_insn) && reg->type == OP_IN));
5288 if (NONDEBUG_INSN_P (curr_insn)
5289 && split_if_necessary (src_regno, reg->biggest_mode,
5290 potential_reload_hard_regs,
5291 before_p, curr_insn, max_uid))
5293 if (reg->subreg_p)
5294 lra_risky_transformations_p = true;
5295 change_p = true;
5296 /* Invalidate. */
5297 usage_insns[src_regno].check = 0;
5298 if (before_p)
5299 use_insn = PREV_INSN (curr_insn);
5301 if (NONDEBUG_INSN_P (curr_insn))
5303 if (src_regno < FIRST_PSEUDO_REGISTER)
5304 add_to_hard_reg_set (&live_hard_regs,
5305 reg->biggest_mode, src_regno);
5306 else
5307 add_to_hard_reg_set (&live_hard_regs,
5308 PSEUDO_REGNO_MODE (src_regno),
5309 reg_renumber[src_regno]);
5311 add_next_usage_insn (src_regno, use_insn, reloads_num);
5314 /* Process call args. */
5315 if (curr_id->arg_hard_regs != NULL)
5316 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5317 if (src_regno < FIRST_PSEUDO_REGISTER)
5319 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5320 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5322 for (i = 0; i < to_inherit_num; i++)
5324 src_regno = to_inherit[i].regno;
5325 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5326 curr_insn, to_inherit[i].insns))
5327 change_p = true;
5328 else
5329 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5332 if (update_reloads_num_p
5333 && NONDEBUG_INSN_P (curr_insn)
5334 && (set = single_set (curr_insn)) != NULL_RTX)
5336 int regno = -1;
5337 if ((REG_P (SET_DEST (set))
5338 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5339 && reg_renumber[regno] < 0
5340 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5341 || (REG_P (SET_SRC (set))
5342 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5343 && reg_renumber[regno] < 0
5344 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5346 reloads_num++;
5347 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5348 IOR_HARD_REG_SET (potential_reload_hard_regs,
5349 reg_class_contents[cl]);
5352 /* We reached the start of the current basic block. */
5353 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5354 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5356 /* We reached the beginning of the current block -- do
5357 rest of spliting in the current BB. */
5358 to_process = df_get_live_in (curr_bb);
5359 if (BLOCK_FOR_INSN (head) != curr_bb)
5361 /* We are somewhere in the middle of EBB. */
5362 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5363 curr_bb, &temp_bitmap);
5364 to_process = &temp_bitmap;
5366 head_p = true;
5367 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5369 if ((int) j >= lra_constraint_new_regno_start)
5370 break;
5371 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5372 && usage_insns[j].check == curr_usage_insns_check
5373 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5375 if (need_for_split_p (potential_reload_hard_regs, j))
5377 if (lra_dump_file != NULL && head_p)
5379 fprintf (lra_dump_file,
5380 " ----------------------------------\n");
5381 head_p = false;
5383 if (split_reg (false, j, bb_note (curr_bb),
5384 next_usage_insns))
5385 change_p = true;
5387 usage_insns[j].check = 0;
5392 return change_p;
5395 /* This value affects EBB forming. If probability of edge from EBB to
5396 a BB is not greater than the following value, we don't add the BB
5397 to EBB. */
5398 #define EBB_PROBABILITY_CUTOFF ((REG_BR_PROB_BASE * 50) / 100)
5400 /* Current number of inheritance/split iteration. */
5401 int lra_inheritance_iter;
5403 /* Entry function for inheritance/split pass. */
5404 void
5405 lra_inheritance (void)
5407 int i;
5408 basic_block bb, start_bb;
5409 edge e;
5411 lra_inheritance_iter++;
5412 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5413 return;
5414 timevar_push (TV_LRA_INHERITANCE);
5415 if (lra_dump_file != NULL)
5416 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5417 lra_inheritance_iter);
5418 curr_usage_insns_check = 0;
5419 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5420 for (i = 0; i < lra_constraint_new_regno_start; i++)
5421 usage_insns[i].check = 0;
5422 bitmap_initialize (&check_only_regs, &reg_obstack);
5423 bitmap_initialize (&live_regs, &reg_obstack);
5424 bitmap_initialize (&temp_bitmap, &reg_obstack);
5425 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5426 FOR_EACH_BB_FN (bb, cfun)
5428 start_bb = bb;
5429 if (lra_dump_file != NULL)
5430 fprintf (lra_dump_file, "EBB");
5431 /* Form a EBB starting with BB. */
5432 bitmap_clear (&ebb_global_regs);
5433 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5434 for (;;)
5436 if (lra_dump_file != NULL)
5437 fprintf (lra_dump_file, " %d", bb->index);
5438 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5439 || LABEL_P (BB_HEAD (bb->next_bb)))
5440 break;
5441 e = find_fallthru_edge (bb->succs);
5442 if (! e)
5443 break;
5444 if (e->probability <= EBB_PROBABILITY_CUTOFF)
5445 break;
5446 bb = bb->next_bb;
5448 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5449 if (lra_dump_file != NULL)
5450 fprintf (lra_dump_file, "\n");
5451 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5452 /* Remember that the EBB head and tail can change in
5453 inherit_in_ebb. */
5454 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5456 bitmap_clear (&ebb_global_regs);
5457 bitmap_clear (&temp_bitmap);
5458 bitmap_clear (&live_regs);
5459 bitmap_clear (&check_only_regs);
5460 free (usage_insns);
5462 timevar_pop (TV_LRA_INHERITANCE);
5467 /* This page contains code to undo failed inheritance/split
5468 transformations. */
5470 /* Current number of iteration undoing inheritance/split. */
5471 int lra_undo_inheritance_iter;
5473 /* Fix BB live info LIVE after removing pseudos created on pass doing
5474 inheritance/split which are REMOVED_PSEUDOS. */
5475 static void
5476 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5478 unsigned int regno;
5479 bitmap_iterator bi;
5481 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5482 if (bitmap_clear_bit (live, regno))
5483 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5486 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5487 number. */
5488 static int
5489 get_regno (rtx reg)
5491 if (GET_CODE (reg) == SUBREG)
5492 reg = SUBREG_REG (reg);
5493 if (REG_P (reg))
5494 return REGNO (reg);
5495 return -1;
5498 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5499 return true if we did any change. The undo transformations for
5500 inheritance looks like
5501 i <- i2
5502 p <- i => p <- i2
5503 or removing
5504 p <- i, i <- p, and i <- i3
5505 where p is original pseudo from which inheritance pseudo i was
5506 created, i and i3 are removed inheritance pseudos, i2 is another
5507 not removed inheritance pseudo. All split pseudos or other
5508 occurrences of removed inheritance pseudos are changed on the
5509 corresponding original pseudos.
5511 The function also schedules insns changed and created during
5512 inheritance/split pass for processing by the subsequent constraint
5513 pass. */
5514 static bool
5515 remove_inheritance_pseudos (bitmap remove_pseudos)
5517 basic_block bb;
5518 int regno, sregno, prev_sregno, dregno, restore_regno;
5519 rtx set, prev_set, prev_insn;
5520 bool change_p, done_p;
5522 change_p = ! bitmap_empty_p (remove_pseudos);
5523 /* We can not finish the function right away if CHANGE_P is true
5524 because we need to marks insns affected by previous
5525 inheritance/split pass for processing by the subsequent
5526 constraint pass. */
5527 FOR_EACH_BB_FN (bb, cfun)
5529 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5530 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5531 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5533 if (! INSN_P (curr_insn))
5534 continue;
5535 done_p = false;
5536 sregno = dregno = -1;
5537 if (change_p && NONDEBUG_INSN_P (curr_insn)
5538 && (set = single_set (curr_insn)) != NULL_RTX)
5540 dregno = get_regno (SET_DEST (set));
5541 sregno = get_regno (SET_SRC (set));
5544 if (sregno >= 0 && dregno >= 0)
5546 if ((bitmap_bit_p (remove_pseudos, sregno)
5547 && (lra_reg_info[sregno].restore_regno == dregno
5548 || (bitmap_bit_p (remove_pseudos, dregno)
5549 && (lra_reg_info[sregno].restore_regno
5550 == lra_reg_info[dregno].restore_regno))))
5551 || (bitmap_bit_p (remove_pseudos, dregno)
5552 && lra_reg_info[dregno].restore_regno == sregno))
5553 /* One of the following cases:
5554 original <- removed inheritance pseudo
5555 removed inherit pseudo <- another removed inherit pseudo
5556 removed inherit pseudo <- original pseudo
5558 removed_split_pseudo <- original_reg
5559 original_reg <- removed_split_pseudo */
5561 if (lra_dump_file != NULL)
5563 fprintf (lra_dump_file, " Removing %s:\n",
5564 bitmap_bit_p (&lra_split_regs, sregno)
5565 || bitmap_bit_p (&lra_split_regs, dregno)
5566 ? "split" : "inheritance");
5567 dump_insn_slim (lra_dump_file, curr_insn);
5569 lra_set_insn_deleted (curr_insn);
5570 done_p = true;
5572 else if (bitmap_bit_p (remove_pseudos, sregno)
5573 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5575 /* Search the following pattern:
5576 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5577 original_pseudo <- inherit_or_split_pseudo1
5578 where the 2nd insn is the current insn and
5579 inherit_or_split_pseudo2 is not removed. If it is found,
5580 change the current insn onto:
5581 original_pseudo <- inherit_or_split_pseudo2. */
5582 for (prev_insn = PREV_INSN (curr_insn);
5583 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5584 prev_insn = PREV_INSN (prev_insn))
5586 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5587 && (prev_set = single_set (prev_insn)) != NULL_RTX
5588 /* There should be no subregs in insn we are
5589 searching because only the original reg might
5590 be in subreg when we changed the mode of
5591 load/store for splitting. */
5592 && REG_P (SET_DEST (prev_set))
5593 && REG_P (SET_SRC (prev_set))
5594 && (int) REGNO (SET_DEST (prev_set)) == sregno
5595 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5596 >= FIRST_PSEUDO_REGISTER)
5597 /* As we consider chain of inheritance or
5598 splitting described in above comment we should
5599 check that sregno and prev_sregno were
5600 inheritance/split pseudos created from the
5601 same original regno. */
5602 && (lra_reg_info[sregno].restore_regno
5603 == lra_reg_info[prev_sregno].restore_regno)
5604 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5606 lra_assert (GET_MODE (SET_SRC (prev_set))
5607 == GET_MODE (regno_reg_rtx[sregno]));
5608 if (GET_CODE (SET_SRC (set)) == SUBREG)
5609 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5610 else
5611 SET_SRC (set) = SET_SRC (prev_set);
5612 lra_push_insn_and_update_insn_regno_info (curr_insn);
5613 lra_set_used_insn_alternative_by_uid
5614 (INSN_UID (curr_insn), -1);
5615 done_p = true;
5616 if (lra_dump_file != NULL)
5618 fprintf (lra_dump_file, " Change reload insn:\n");
5619 dump_insn_slim (lra_dump_file, curr_insn);
5624 if (! done_p)
5626 struct lra_insn_reg *reg;
5627 bool restored_regs_p = false;
5628 bool kept_regs_p = false;
5630 curr_id = lra_get_insn_recog_data (curr_insn);
5631 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5633 regno = reg->regno;
5634 restore_regno = lra_reg_info[regno].restore_regno;
5635 if (restore_regno >= 0)
5637 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5639 substitute_pseudo (&curr_insn, regno,
5640 regno_reg_rtx[restore_regno]);
5641 restored_regs_p = true;
5643 else
5644 kept_regs_p = true;
5647 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5649 /* The instruction has changed since the previous
5650 constraints pass. */
5651 lra_push_insn_and_update_insn_regno_info (curr_insn);
5652 lra_set_used_insn_alternative_by_uid
5653 (INSN_UID (curr_insn), -1);
5655 else if (restored_regs_p)
5656 /* The instruction has been restored to the form that
5657 it had during the previous constraints pass. */
5658 lra_update_insn_regno_info (curr_insn);
5659 if (restored_regs_p && lra_dump_file != NULL)
5661 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5662 dump_insn_slim (lra_dump_file, curr_insn);
5667 return change_p;
5670 /* If optional reload pseudos failed to get a hard register or was not
5671 inherited, it is better to remove optional reloads. We do this
5672 transformation after undoing inheritance to figure out necessity to
5673 remove optional reloads easier. Return true if we do any
5674 change. */
5675 static bool
5676 undo_optional_reloads (void)
5678 bool change_p, keep_p;
5679 unsigned int regno, uid;
5680 bitmap_iterator bi, bi2;
5681 rtx insn, set, src, dest;
5682 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
5684 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
5685 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
5686 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5688 keep_p = false;
5689 /* Keep optional reloads from previous subpasses. */
5690 if (lra_reg_info[regno].restore_regno < 0
5691 /* If the original pseudo changed its allocation, just
5692 removing the optional pseudo is dangerous as the original
5693 pseudo will have longer live range. */
5694 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
5695 keep_p = true;
5696 else if (reg_renumber[regno] >= 0)
5697 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
5699 insn = lra_insn_recog_data[uid]->insn;
5700 if ((set = single_set (insn)) == NULL_RTX)
5701 continue;
5702 src = SET_SRC (set);
5703 dest = SET_DEST (set);
5704 if (! REG_P (src) || ! REG_P (dest))
5705 continue;
5706 if (REGNO (dest) == regno
5707 /* Ignore insn for optional reloads itself. */
5708 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
5709 /* Check only inheritance on last inheritance pass. */
5710 && (int) REGNO (src) >= new_regno_start
5711 /* Check that the optional reload was inherited. */
5712 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
5714 keep_p = true;
5715 break;
5718 if (keep_p)
5720 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
5721 if (lra_dump_file != NULL)
5722 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
5725 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
5726 bitmap_initialize (&insn_bitmap, &reg_obstack);
5727 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
5729 if (lra_dump_file != NULL)
5730 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
5731 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
5732 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
5734 insn = lra_insn_recog_data[uid]->insn;
5735 if ((set = single_set (insn)) != NULL_RTX)
5737 src = SET_SRC (set);
5738 dest = SET_DEST (set);
5739 if (REG_P (src) && REG_P (dest)
5740 && ((REGNO (src) == regno
5741 && (lra_reg_info[regno].restore_regno
5742 == (int) REGNO (dest)))
5743 || (REGNO (dest) == regno
5744 && (lra_reg_info[regno].restore_regno
5745 == (int) REGNO (src)))))
5747 if (lra_dump_file != NULL)
5749 fprintf (lra_dump_file, " Deleting move %u\n",
5750 INSN_UID (insn));
5751 dump_insn_slim (lra_dump_file, insn);
5753 lra_set_insn_deleted (insn);
5754 continue;
5756 /* We should not worry about generation memory-memory
5757 moves here as if the corresponding inheritance did
5758 not work (inheritance pseudo did not get a hard reg),
5759 we remove the inheritance pseudo and the optional
5760 reload. */
5762 substitute_pseudo (&insn, regno,
5763 regno_reg_rtx[lra_reg_info[regno].restore_regno]);
5764 lra_update_insn_regno_info (insn);
5765 if (lra_dump_file != NULL)
5767 fprintf (lra_dump_file,
5768 " Restoring original insn:\n");
5769 dump_insn_slim (lra_dump_file, insn);
5773 /* Clear restore_regnos. */
5774 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5775 lra_reg_info[regno].restore_regno = -1;
5776 bitmap_clear (&insn_bitmap);
5777 bitmap_clear (&removed_optional_reload_pseudos);
5778 return change_p;
5781 /* Entry function for undoing inheritance/split transformation. Return true
5782 if we did any RTL change in this pass. */
5783 bool
5784 lra_undo_inheritance (void)
5786 unsigned int regno;
5787 int restore_regno, hard_regno;
5788 int n_all_inherit, n_inherit, n_all_split, n_split;
5789 bitmap_head remove_pseudos;
5790 bitmap_iterator bi;
5791 bool change_p;
5793 lra_undo_inheritance_iter++;
5794 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5795 return false;
5796 if (lra_dump_file != NULL)
5797 fprintf (lra_dump_file,
5798 "\n********** Undoing inheritance #%d: **********\n\n",
5799 lra_undo_inheritance_iter);
5800 bitmap_initialize (&remove_pseudos, &reg_obstack);
5801 n_inherit = n_all_inherit = 0;
5802 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5803 if (lra_reg_info[regno].restore_regno >= 0)
5805 n_all_inherit++;
5806 if (reg_renumber[regno] < 0
5807 /* If the original pseudo changed its allocation, just
5808 removing inheritance is dangerous as for changing
5809 allocation we used shorter live-ranges. */
5810 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
5811 bitmap_set_bit (&remove_pseudos, regno);
5812 else
5813 n_inherit++;
5815 if (lra_dump_file != NULL && n_all_inherit != 0)
5816 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
5817 n_inherit, n_all_inherit,
5818 (double) n_inherit / n_all_inherit * 100);
5819 n_split = n_all_split = 0;
5820 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5821 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
5823 n_all_split++;
5824 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
5825 ? reg_renumber[restore_regno] : restore_regno);
5826 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
5827 bitmap_set_bit (&remove_pseudos, regno);
5828 else
5830 n_split++;
5831 if (lra_dump_file != NULL)
5832 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
5833 regno, restore_regno);
5836 if (lra_dump_file != NULL && n_all_split != 0)
5837 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
5838 n_split, n_all_split,
5839 (double) n_split / n_all_split * 100);
5840 change_p = remove_inheritance_pseudos (&remove_pseudos);
5841 bitmap_clear (&remove_pseudos);
5842 /* Clear restore_regnos. */
5843 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5844 lra_reg_info[regno].restore_regno = -1;
5845 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5846 lra_reg_info[regno].restore_regno = -1;
5847 change_p = undo_optional_reloads () || change_p;
5848 return change_p;