2014-03-14 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / lra-constraints.c
blob288e24b4fe15df3b23923ccd76dccda8ca51a2a7
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2014 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file contains code for 3 passes: constraint pass,
23 inheritance/split pass, and pass for undoing failed inheritance and
24 split.
26 The major goal of constraint pass is to transform RTL to satisfy
27 insn and address constraints by:
28 o choosing insn alternatives;
29 o generating *reload insns* (or reloads in brief) and *reload
30 pseudos* which will get necessary hard registers later;
31 o substituting pseudos with equivalent values and removing the
32 instructions that initialized those pseudos.
34 The constraint pass has biggest and most complicated code in LRA.
35 There are a lot of important details like:
36 o reuse of input reload pseudos to simplify reload pseudo
37 allocations;
38 o some heuristics to choose insn alternative to improve the
39 inheritance;
40 o early clobbers etc.
42 The pass is mimicking former reload pass in alternative choosing
43 because the reload pass is oriented to current machine description
44 model. It might be changed if the machine description model is
45 changed.
47 There is special code for preventing all LRA and this pass cycling
48 in case of bugs.
50 On the first iteration of the pass we process every instruction and
51 choose an alternative for each one. On subsequent iterations we try
52 to avoid reprocessing instructions if we can be sure that the old
53 choice is still valid.
55 The inheritance/spilt pass is to transform code to achieve
56 ineheritance and live range splitting. It is done on backward
57 traversal of EBBs.
59 The inheritance optimization goal is to reuse values in hard
60 registers. There is analogous optimization in old reload pass. The
61 inheritance is achieved by following transformation:
63 reload_p1 <- p reload_p1 <- p
64 ... new_p <- reload_p1
65 ... => ...
66 reload_p2 <- p reload_p2 <- new_p
68 where p is spilled and not changed between the insns. Reload_p1 is
69 also called *original pseudo* and new_p is called *inheritance
70 pseudo*.
72 The subsequent assignment pass will try to assign the same (or
73 another if it is not possible) hard register to new_p as to
74 reload_p1 or reload_p2.
76 If the assignment pass fails to assign a hard register to new_p,
77 this file will undo the inheritance and restore the original code.
78 This is because implementing the above sequence with a spilled
79 new_p would make the code much worse. The inheritance is done in
80 EBB scope. The above is just a simplified example to get an idea
81 of the inheritance as the inheritance is also done for non-reload
82 insns.
84 Splitting (transformation) is also done in EBB scope on the same
85 pass as the inheritance:
87 r <- ... or ... <- r r <- ... or ... <- r
88 ... s <- r (new insn -- save)
89 ... =>
90 ... r <- s (new insn -- restore)
91 ... <- r ... <- r
93 The *split pseudo* s is assigned to the hard register of the
94 original pseudo or hard register r.
96 Splitting is done:
97 o In EBBs with high register pressure for global pseudos (living
98 in at least 2 BBs) and assigned to hard registers when there
99 are more one reloads needing the hard registers;
100 o for pseudos needing save/restore code around calls.
102 If the split pseudo still has the same hard register as the
103 original pseudo after the subsequent assignment pass or the
104 original pseudo was split, the opposite transformation is done on
105 the same pass for undoing inheritance. */
107 #undef REG_OK_STRICT
109 #include "config.h"
110 #include "system.h"
111 #include "coretypes.h"
112 #include "tm.h"
113 #include "hard-reg-set.h"
114 #include "rtl.h"
115 #include "tm_p.h"
116 #include "regs.h"
117 #include "insn-config.h"
118 #include "insn-codes.h"
119 #include "recog.h"
120 #include "output.h"
121 #include "addresses.h"
122 #include "target.h"
123 #include "function.h"
124 #include "expr.h"
125 #include "basic-block.h"
126 #include "except.h"
127 #include "optabs.h"
128 #include "df.h"
129 #include "ira.h"
130 #include "rtl-error.h"
131 #include "lra-int.h"
133 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
134 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
135 reload insns. */
136 static int bb_reload_num;
138 /* The current insn being processed and corresponding its single set
139 (NULL otherwise), its data (basic block, the insn data, the insn
140 static data, and the mode of each operand). */
141 static rtx curr_insn;
142 static rtx curr_insn_set;
143 static basic_block curr_bb;
144 static lra_insn_recog_data_t curr_id;
145 static struct lra_static_insn_data *curr_static_id;
146 static enum machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
150 /* Start numbers for new registers and insns at the current constraints
151 pass start. */
152 static int new_regno_start;
153 static int new_insn_uid_start;
155 /* If LOC is nonnull, strip any outer subreg from it. */
156 static inline rtx *
157 strip_subreg (rtx *loc)
159 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
162 /* Return hard regno of REGNO or if it is was not assigned to a hard
163 register, use a hard register from its allocno class. */
164 static int
165 get_try_hard_regno (int regno)
167 int hard_regno;
168 enum reg_class rclass;
170 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
171 hard_regno = lra_get_regno_hard_regno (regno);
172 if (hard_regno >= 0)
173 return hard_regno;
174 rclass = lra_get_allocno_class (regno);
175 if (rclass == NO_REGS)
176 return -1;
177 return ira_class_hard_regs[rclass][0];
180 /* Return final hard regno (plus offset) which will be after
181 elimination. We do this for matching constraints because the final
182 hard regno could have a different class. */
183 static int
184 get_final_hard_regno (int hard_regno, int offset)
186 if (hard_regno < 0)
187 return hard_regno;
188 hard_regno = lra_get_elimination_hard_regno (hard_regno);
189 return hard_regno + offset;
192 /* Return hard regno of X after removing subreg and making
193 elimination. If X is not a register or subreg of register, return
194 -1. For pseudo use its assignment. */
195 static int
196 get_hard_regno (rtx x)
198 rtx reg;
199 int offset, hard_regno;
201 reg = x;
202 if (GET_CODE (x) == SUBREG)
203 reg = SUBREG_REG (x);
204 if (! REG_P (reg))
205 return -1;
206 if ((hard_regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
207 hard_regno = lra_get_regno_hard_regno (hard_regno);
208 if (hard_regno < 0)
209 return -1;
210 offset = 0;
211 if (GET_CODE (x) == SUBREG)
212 offset += subreg_regno_offset (hard_regno, GET_MODE (reg),
213 SUBREG_BYTE (x), GET_MODE (x));
214 return get_final_hard_regno (hard_regno, offset);
217 /* If REGNO is a hard register or has been allocated a hard register,
218 return the class of that register. If REGNO is a reload pseudo
219 created by the current constraints pass, return its allocno class.
220 Return NO_REGS otherwise. */
221 static enum reg_class
222 get_reg_class (int regno)
224 int hard_regno;
226 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
227 hard_regno = lra_get_regno_hard_regno (regno);
228 if (hard_regno >= 0)
230 hard_regno = get_final_hard_regno (hard_regno, 0);
231 return REGNO_REG_CLASS (hard_regno);
233 if (regno >= new_regno_start)
234 return lra_get_allocno_class (regno);
235 return NO_REGS;
238 /* Return true if REG satisfies (or will satisfy) reg class constraint
239 CL. Use elimination first if REG is a hard register. If REG is a
240 reload pseudo created by this constraints pass, assume that it will
241 be allocated a hard register from its allocno class, but allow that
242 class to be narrowed to CL if it is currently a superset of CL.
244 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
245 REGNO (reg), or NO_REGS if no change in its class was needed. */
246 static bool
247 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
249 enum reg_class rclass, common_class;
250 enum machine_mode reg_mode;
251 int class_size, hard_regno, nregs, i, j;
252 int regno = REGNO (reg);
254 if (new_class != NULL)
255 *new_class = NO_REGS;
256 if (regno < FIRST_PSEUDO_REGISTER)
258 rtx final_reg = reg;
259 rtx *final_loc = &final_reg;
261 lra_eliminate_reg_if_possible (final_loc);
262 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
264 reg_mode = GET_MODE (reg);
265 rclass = get_reg_class (regno);
266 if (regno < new_regno_start
267 /* Do not allow the constraints for reload instructions to
268 influence the classes of new pseudos. These reloads are
269 typically moves that have many alternatives, and restricting
270 reload pseudos for one alternative may lead to situations
271 where other reload pseudos are no longer allocatable. */
272 || (INSN_UID (curr_insn) >= new_insn_uid_start
273 && curr_insn_set != NULL
274 && ((OBJECT_P (SET_SRC (curr_insn_set))
275 && ! CONSTANT_P (SET_SRC (curr_insn_set)))
276 || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
277 && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))
278 && ! CONSTANT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
279 /* When we don't know what class will be used finally for reload
280 pseudos, we use ALL_REGS. */
281 return ((regno >= new_regno_start && rclass == ALL_REGS)
282 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
283 && ! hard_reg_set_subset_p (reg_class_contents[cl],
284 lra_no_alloc_regs)));
285 else
287 common_class = ira_reg_class_subset[rclass][cl];
288 if (new_class != NULL)
289 *new_class = common_class;
290 if (hard_reg_set_subset_p (reg_class_contents[common_class],
291 lra_no_alloc_regs))
292 return false;
293 /* Check that there are enough allocatable regs. */
294 class_size = ira_class_hard_regs_num[common_class];
295 for (i = 0; i < class_size; i++)
297 hard_regno = ira_class_hard_regs[common_class][i];
298 nregs = hard_regno_nregs[hard_regno][reg_mode];
299 if (nregs == 1)
300 return true;
301 for (j = 0; j < nregs; j++)
302 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
303 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
304 hard_regno + j))
305 break;
306 if (j >= nregs)
307 return true;
309 return false;
313 /* Return true if REGNO satisfies a memory constraint. */
314 static bool
315 in_mem_p (int regno)
317 return get_reg_class (regno) == NO_REGS;
320 /* Initiate equivalences for LRA. As we keep original equivalences
321 before any elimination, we need to make copies otherwise any change
322 in insns might change the equivalences. */
323 void
324 lra_init_equiv (void)
326 ira_expand_reg_equiv ();
327 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
329 rtx res;
331 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
332 ira_reg_equiv[i].memory = copy_rtx (res);
333 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
334 ira_reg_equiv[i].invariant = copy_rtx (res);
338 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
340 /* Update equivalence for REGNO. We need to this as the equivalence
341 might contain other pseudos which are changed by their
342 equivalences. */
343 static void
344 update_equiv (int regno)
346 rtx x;
348 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
349 ira_reg_equiv[regno].memory
350 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
351 NULL_RTX);
352 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
353 ira_reg_equiv[regno].invariant
354 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
355 NULL_RTX);
358 /* If we have decided to substitute X with another value, return that
359 value, otherwise return X. */
360 static rtx
361 get_equiv (rtx x)
363 int regno;
364 rtx res;
366 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
367 || ! ira_reg_equiv[regno].defined_p
368 || ! ira_reg_equiv[regno].profitable_p
369 || lra_get_regno_hard_regno (regno) >= 0)
370 return x;
371 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
372 return res;
373 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
374 return res;
375 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
376 return res;
377 gcc_unreachable ();
380 /* If we have decided to substitute X with the equivalent value,
381 return that value after elimination for INSN, otherwise return
382 X. */
383 static rtx
384 get_equiv_with_elimination (rtx x, rtx insn)
386 rtx res = get_equiv (x);
388 if (x == res || CONSTANT_P (res))
389 return res;
390 return lra_eliminate_regs_1 (insn, res, GET_MODE (res), false, false, true);
393 /* Set up curr_operand_mode. */
394 static void
395 init_curr_operand_mode (void)
397 int nop = curr_static_id->n_operands;
398 for (int i = 0; i < nop; i++)
400 enum machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
401 if (mode == VOIDmode)
403 /* The .md mode for address operands is the mode of the
404 addressed value rather than the mode of the address itself. */
405 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
406 mode = Pmode;
407 else
408 mode = curr_static_id->operand[i].mode;
410 curr_operand_mode[i] = mode;
416 /* The page contains code to reuse input reloads. */
418 /* Structure describes input reload of the current insns. */
419 struct input_reload
421 /* Reloaded value. */
422 rtx input;
423 /* Reload pseudo used. */
424 rtx reg;
427 /* The number of elements in the following array. */
428 static int curr_insn_input_reloads_num;
429 /* Array containing info about input reloads. It is used to find the
430 same input reload and reuse the reload pseudo in this case. */
431 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
433 /* Initiate data concerning reuse of input reloads for the current
434 insn. */
435 static void
436 init_curr_insn_input_reloads (void)
438 curr_insn_input_reloads_num = 0;
441 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
442 created input reload pseudo (only if TYPE is not OP_OUT). The
443 result pseudo is returned through RESULT_REG. Return TRUE if we
444 created a new pseudo, FALSE if we reused the already created input
445 reload pseudo. Use TITLE to describe new registers for debug
446 purposes. */
447 static bool
448 get_reload_reg (enum op_type type, enum machine_mode mode, rtx original,
449 enum reg_class rclass, const char *title, rtx *result_reg)
451 int i, regno;
452 enum reg_class new_class;
454 if (type == OP_OUT)
456 *result_reg
457 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
458 return true;
460 /* Prevent reuse value of expression with side effects,
461 e.g. volatile memory. */
462 if (! side_effects_p (original))
463 for (i = 0; i < curr_insn_input_reloads_num; i++)
464 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
465 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
467 rtx reg = curr_insn_input_reloads[i].reg;
468 regno = REGNO (reg);
469 /* If input is equal to original and both are VOIDmode,
470 GET_MODE (reg) might be still different from mode.
471 Ensure we don't return *result_reg with wrong mode. */
472 if (GET_MODE (reg) != mode)
474 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
475 continue;
476 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
477 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
478 continue;
480 *result_reg = reg;
481 if (lra_dump_file != NULL)
483 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
484 dump_value_slim (lra_dump_file, original, 1);
486 if (new_class != lra_get_allocno_class (regno))
487 lra_change_class (regno, new_class, ", change to", false);
488 if (lra_dump_file != NULL)
489 fprintf (lra_dump_file, "\n");
490 return false;
492 *result_reg = lra_create_new_reg (mode, original, rclass, title);
493 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
494 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
495 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
496 return true;
501 /* The page contains code to extract memory address parts. */
503 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
504 static inline bool
505 ok_for_index_p_nonstrict (rtx reg)
507 unsigned regno = REGNO (reg);
509 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
512 /* A version of regno_ok_for_base_p for use here, when all pseudos
513 should count as OK. Arguments as for regno_ok_for_base_p. */
514 static inline bool
515 ok_for_base_p_nonstrict (rtx reg, enum machine_mode mode, addr_space_t as,
516 enum rtx_code outer_code, enum rtx_code index_code)
518 unsigned regno = REGNO (reg);
520 if (regno >= FIRST_PSEUDO_REGISTER)
521 return true;
522 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
527 /* The page contains major code to choose the current insn alternative
528 and generate reloads for it. */
530 /* Return the offset from REGNO of the least significant register
531 in (reg:MODE REGNO).
533 This function is used to tell whether two registers satisfy
534 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
536 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
537 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
539 lra_constraint_offset (int regno, enum machine_mode mode)
541 lra_assert (regno < FIRST_PSEUDO_REGISTER);
542 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
543 && SCALAR_INT_MODE_P (mode))
544 return hard_regno_nregs[regno][mode] - 1;
545 return 0;
548 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
549 if they are the same hard reg, and has special hacks for
550 auto-increment and auto-decrement. This is specifically intended for
551 process_alt_operands to use in determining whether two operands
552 match. X is the operand whose number is the lower of the two.
554 It is supposed that X is the output operand and Y is the input
555 operand. Y_HARD_REGNO is the final hard regno of register Y or
556 register in subreg Y as we know it now. Otherwise, it is a
557 negative value. */
558 static bool
559 operands_match_p (rtx x, rtx y, int y_hard_regno)
561 int i;
562 RTX_CODE code = GET_CODE (x);
563 const char *fmt;
565 if (x == y)
566 return true;
567 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
568 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
570 int j;
572 i = get_hard_regno (x);
573 if (i < 0)
574 goto slow;
576 if ((j = y_hard_regno) < 0)
577 goto slow;
579 i += lra_constraint_offset (i, GET_MODE (x));
580 j += lra_constraint_offset (j, GET_MODE (y));
582 return i == j;
585 /* If two operands must match, because they are really a single
586 operand of an assembler insn, then two post-increments are invalid
587 because the assembler insn would increment only once. On the
588 other hand, a post-increment matches ordinary indexing if the
589 post-increment is the output operand. */
590 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
591 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
593 /* Two pre-increments are invalid because the assembler insn would
594 increment only once. On the other hand, a pre-increment matches
595 ordinary indexing if the pre-increment is the input operand. */
596 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
597 || GET_CODE (y) == PRE_MODIFY)
598 return operands_match_p (x, XEXP (y, 0), -1);
600 slow:
602 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
603 && x == SUBREG_REG (y))
604 return true;
605 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
606 && SUBREG_REG (x) == y)
607 return true;
609 /* Now we have disposed of all the cases in which different rtx
610 codes can match. */
611 if (code != GET_CODE (y))
612 return false;
614 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
615 if (GET_MODE (x) != GET_MODE (y))
616 return false;
618 switch (code)
620 CASE_CONST_UNIQUE:
621 return false;
623 case LABEL_REF:
624 return XEXP (x, 0) == XEXP (y, 0);
625 case SYMBOL_REF:
626 return XSTR (x, 0) == XSTR (y, 0);
628 default:
629 break;
632 /* Compare the elements. If any pair of corresponding elements fail
633 to match, return false for the whole things. */
635 fmt = GET_RTX_FORMAT (code);
636 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
638 int val, j;
639 switch (fmt[i])
641 case 'w':
642 if (XWINT (x, i) != XWINT (y, i))
643 return false;
644 break;
646 case 'i':
647 if (XINT (x, i) != XINT (y, i))
648 return false;
649 break;
651 case 'e':
652 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
653 if (val == 0)
654 return false;
655 break;
657 case '0':
658 break;
660 case 'E':
661 if (XVECLEN (x, i) != XVECLEN (y, i))
662 return false;
663 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
665 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
666 if (val == 0)
667 return false;
669 break;
671 /* It is believed that rtx's at this level will never
672 contain anything but integers and other rtx's, except for
673 within LABEL_REFs and SYMBOL_REFs. */
674 default:
675 gcc_unreachable ();
678 return true;
681 /* True if X is a constant that can be forced into the constant pool.
682 MODE is the mode of the operand, or VOIDmode if not known. */
683 #define CONST_POOL_OK_P(MODE, X) \
684 ((MODE) != VOIDmode \
685 && CONSTANT_P (X) \
686 && GET_CODE (X) != HIGH \
687 && !targetm.cannot_force_const_mem (MODE, X))
689 /* True if C is a non-empty register class that has too few registers
690 to be safely used as a reload target class. */
691 #define SMALL_REGISTER_CLASS_P(C) \
692 (ira_class_hard_regs_num [(C)] == 1 \
693 || (ira_class_hard_regs_num [(C)] >= 1 \
694 && targetm.class_likely_spilled_p (C)))
696 /* If REG is a reload pseudo, try to make its class satisfying CL. */
697 static void
698 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
700 enum reg_class rclass;
702 /* Do not make more accurate class from reloads generated. They are
703 mostly moves with a lot of constraints. Making more accurate
704 class may results in very narrow class and impossibility of find
705 registers for several reloads of one insn. */
706 if (INSN_UID (curr_insn) >= new_insn_uid_start)
707 return;
708 if (GET_CODE (reg) == SUBREG)
709 reg = SUBREG_REG (reg);
710 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
711 return;
712 if (in_class_p (reg, cl, &rclass) && rclass != cl)
713 lra_change_class (REGNO (reg), rclass, " Change to", true);
716 /* Generate reloads for matching OUT and INS (array of input operand
717 numbers with end marker -1) with reg class GOAL_CLASS. Add input
718 and output reloads correspondingly to the lists *BEFORE and *AFTER.
719 OUT might be negative. In this case we generate input reloads for
720 matched input operands INS. */
721 static void
722 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
723 rtx *before, rtx *after)
725 int i, in;
726 rtx new_in_reg, new_out_reg, reg, clobber;
727 enum machine_mode inmode, outmode;
728 rtx in_rtx = *curr_id->operand_loc[ins[0]];
729 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
731 inmode = curr_operand_mode[ins[0]];
732 outmode = out < 0 ? inmode : curr_operand_mode[out];
733 push_to_sequence (*before);
734 if (inmode != outmode)
736 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
738 reg = new_in_reg
739 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
740 goal_class, "");
741 if (SCALAR_INT_MODE_P (inmode))
742 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
743 else
744 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
745 LRA_SUBREG_P (new_out_reg) = 1;
746 /* If the input reg is dying here, we can use the same hard
747 register for REG and IN_RTX. We do it only for original
748 pseudos as reload pseudos can die although original
749 pseudos still live where reload pseudos dies. */
750 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
751 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
752 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
754 else
756 reg = new_out_reg
757 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
758 goal_class, "");
759 if (SCALAR_INT_MODE_P (outmode))
760 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
761 else
762 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
763 /* NEW_IN_REG is non-paradoxical subreg. We don't want
764 NEW_OUT_REG living above. We add clobber clause for
765 this. This is just a temporary clobber. We can remove
766 it at the end of LRA work. */
767 clobber = emit_clobber (new_out_reg);
768 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
769 LRA_SUBREG_P (new_in_reg) = 1;
770 if (GET_CODE (in_rtx) == SUBREG)
772 rtx subreg_reg = SUBREG_REG (in_rtx);
774 /* If SUBREG_REG is dying here and sub-registers IN_RTX
775 and NEW_IN_REG are similar, we can use the same hard
776 register for REG and SUBREG_REG. */
777 if (REG_P (subreg_reg)
778 && (int) REGNO (subreg_reg) < lra_new_regno_start
779 && GET_MODE (subreg_reg) == outmode
780 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
781 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
782 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
786 else
788 /* Pseudos have values -- see comments for lra_reg_info.
789 Different pseudos with the same value do not conflict even if
790 they live in the same place. When we create a pseudo we
791 assign value of original pseudo (if any) from which we
792 created the new pseudo. If we create the pseudo from the
793 input pseudo, the new pseudo will no conflict with the input
794 pseudo which is wrong when the input pseudo lives after the
795 insn and as the new pseudo value is changed by the insn
796 output. Therefore we create the new pseudo from the output.
798 We cannot reuse the current output register because we might
799 have a situation like "a <- a op b", where the constraints
800 force the second input operand ("b") to match the output
801 operand ("a"). "b" must then be copied into a new register
802 so that it doesn't clobber the current value of "a". */
804 new_in_reg = new_out_reg
805 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
806 goal_class, "");
808 /* In operand can be got from transformations before processing insn
809 constraints. One example of such transformations is subreg
810 reloading (see function simplify_operand_subreg). The new
811 pseudos created by the transformations might have inaccurate
812 class (ALL_REGS) and we should make their classes more
813 accurate. */
814 narrow_reload_pseudo_class (in_rtx, goal_class);
815 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
816 *before = get_insns ();
817 end_sequence ();
818 for (i = 0; (in = ins[i]) >= 0; i++)
820 lra_assert
821 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
822 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
823 *curr_id->operand_loc[in] = new_in_reg;
825 lra_update_dups (curr_id, ins);
826 if (out < 0)
827 return;
828 /* See a comment for the input operand above. */
829 narrow_reload_pseudo_class (out_rtx, goal_class);
830 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
832 start_sequence ();
833 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
834 emit_insn (*after);
835 *after = get_insns ();
836 end_sequence ();
838 *curr_id->operand_loc[out] = new_out_reg;
839 lra_update_dup (curr_id, out);
842 /* Return register class which is union of all reg classes in insn
843 constraint alternative string starting with P. */
844 static enum reg_class
845 reg_class_from_constraints (const char *p)
847 int c, len;
848 enum reg_class op_class = NO_REGS;
851 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
853 case '#':
854 case ',':
855 return op_class;
857 case 'p':
858 op_class = (reg_class_subunion
859 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
860 ADDRESS, SCRATCH)]);
861 break;
863 case 'g':
864 case 'r':
865 op_class = reg_class_subunion[op_class][GENERAL_REGS];
866 break;
868 default:
869 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
871 #ifdef EXTRA_CONSTRAINT_STR
872 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
873 op_class
874 = (reg_class_subunion
875 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
876 ADDRESS, SCRATCH)]);
877 #endif
878 break;
881 op_class
882 = reg_class_subunion[op_class][REG_CLASS_FROM_CONSTRAINT (c, p)];
883 break;
885 while ((p += len), c);
886 return op_class;
889 /* If OP is a register, return the class of the register as per
890 get_reg_class, otherwise return NO_REGS. */
891 static inline enum reg_class
892 get_op_class (rtx op)
894 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
897 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
898 otherwise. If modes of MEM_PSEUDO and VAL are different, use
899 SUBREG for VAL to make them equal. */
900 static rtx
901 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
903 if (GET_MODE (mem_pseudo) != GET_MODE (val))
905 /* Usually size of mem_pseudo is greater than val size but in
906 rare cases it can be less as it can be defined by target
907 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
908 if (! MEM_P (val))
910 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
911 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
913 LRA_SUBREG_P (val) = 1;
915 else
917 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
918 LRA_SUBREG_P (mem_pseudo) = 1;
921 return (to_p
922 ? gen_move_insn (mem_pseudo, val)
923 : gen_move_insn (val, mem_pseudo));
926 /* Process a special case insn (register move), return true if we
927 don't need to process it anymore. INSN should be a single set
928 insn. Set up that RTL was changed through CHANGE_P and macro
929 SECONDARY_MEMORY_NEEDED says to use secondary memory through
930 SEC_MEM_P. */
931 static bool
932 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
934 int sregno, dregno;
935 rtx dest, src, dreg, sreg, old_sreg, new_reg, before, scratch_reg;
936 enum reg_class dclass, sclass, secondary_class;
937 enum machine_mode sreg_mode;
938 secondary_reload_info sri;
940 lra_assert (curr_insn_set != NULL_RTX);
941 dreg = dest = SET_DEST (curr_insn_set);
942 sreg = src = SET_SRC (curr_insn_set);
943 if (GET_CODE (dest) == SUBREG)
944 dreg = SUBREG_REG (dest);
945 if (GET_CODE (src) == SUBREG)
946 sreg = SUBREG_REG (src);
947 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
948 return false;
949 sclass = dclass = NO_REGS;
950 if (REG_P (dreg))
951 dclass = get_reg_class (REGNO (dreg));
952 if (dclass == ALL_REGS)
953 /* ALL_REGS is used for new pseudos created by transformations
954 like reload of SUBREG_REG (see function
955 simplify_operand_subreg). We don't know their class yet. We
956 should figure out the class from processing the insn
957 constraints not in this fast path function. Even if ALL_REGS
958 were a right class for the pseudo, secondary_... hooks usually
959 are not define for ALL_REGS. */
960 return false;
961 sreg_mode = GET_MODE (sreg);
962 old_sreg = sreg;
963 if (REG_P (sreg))
964 sclass = get_reg_class (REGNO (sreg));
965 if (sclass == ALL_REGS)
966 /* See comments above. */
967 return false;
968 if (sclass == NO_REGS && dclass == NO_REGS)
969 return false;
970 #ifdef SECONDARY_MEMORY_NEEDED
971 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
972 #ifdef SECONDARY_MEMORY_NEEDED_MODE
973 && ((sclass != NO_REGS && dclass != NO_REGS)
974 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
975 #endif
978 *sec_mem_p = true;
979 return false;
981 #endif
982 if (! REG_P (dreg) || ! REG_P (sreg))
983 return false;
984 sri.prev_sri = NULL;
985 sri.icode = CODE_FOR_nothing;
986 sri.extra_cost = 0;
987 secondary_class = NO_REGS;
988 /* Set up hard register for a reload pseudo for hook
989 secondary_reload because some targets just ignore unassigned
990 pseudos in the hook. */
991 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
993 dregno = REGNO (dreg);
994 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
996 else
997 dregno = -1;
998 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1000 sregno = REGNO (sreg);
1001 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1003 else
1004 sregno = -1;
1005 if (sclass != NO_REGS)
1006 secondary_class
1007 = (enum reg_class) targetm.secondary_reload (false, dest,
1008 (reg_class_t) sclass,
1009 GET_MODE (src), &sri);
1010 if (sclass == NO_REGS
1011 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1012 && dclass != NO_REGS))
1014 enum reg_class old_sclass = secondary_class;
1015 secondary_reload_info old_sri = sri;
1017 sri.prev_sri = NULL;
1018 sri.icode = CODE_FOR_nothing;
1019 sri.extra_cost = 0;
1020 secondary_class
1021 = (enum reg_class) targetm.secondary_reload (true, sreg,
1022 (reg_class_t) dclass,
1023 sreg_mode, &sri);
1024 /* Check the target hook consistency. */
1025 lra_assert
1026 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1027 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1028 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1030 if (sregno >= 0)
1031 reg_renumber [sregno] = -1;
1032 if (dregno >= 0)
1033 reg_renumber [dregno] = -1;
1034 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1035 return false;
1036 *change_p = true;
1037 new_reg = NULL_RTX;
1038 if (secondary_class != NO_REGS)
1039 new_reg = lra_create_new_reg_with_unique_value (sreg_mode, NULL_RTX,
1040 secondary_class,
1041 "secondary");
1042 start_sequence ();
1043 if (old_sreg != sreg)
1044 sreg = copy_rtx (sreg);
1045 if (sri.icode == CODE_FOR_nothing)
1046 lra_emit_move (new_reg, sreg);
1047 else
1049 enum reg_class scratch_class;
1051 scratch_class = (reg_class_from_constraints
1052 (insn_data[sri.icode].operand[2].constraint));
1053 scratch_reg = (lra_create_new_reg_with_unique_value
1054 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1055 scratch_class, "scratch"));
1056 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1057 sreg, scratch_reg));
1059 before = get_insns ();
1060 end_sequence ();
1061 lra_process_new_insns (curr_insn, before, NULL_RTX, "Inserting the move");
1062 if (new_reg != NULL_RTX)
1064 if (GET_CODE (src) == SUBREG)
1065 SUBREG_REG (src) = new_reg;
1066 else
1067 SET_SRC (curr_insn_set) = new_reg;
1069 else
1071 if (lra_dump_file != NULL)
1073 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1074 dump_insn_slim (lra_dump_file, curr_insn);
1076 lra_set_insn_deleted (curr_insn);
1077 return true;
1079 return false;
1082 /* The following data describe the result of process_alt_operands.
1083 The data are used in curr_insn_transform to generate reloads. */
1085 /* The chosen reg classes which should be used for the corresponding
1086 operands. */
1087 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1088 /* True if the operand should be the same as another operand and that
1089 other operand does not need a reload. */
1090 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1091 /* True if the operand does not need a reload. */
1092 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1093 /* True if the operand can be offsetable memory. */
1094 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1095 /* The number of an operand to which given operand can be matched to. */
1096 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1097 /* The number of elements in the following array. */
1098 static int goal_alt_dont_inherit_ops_num;
1099 /* Numbers of operands whose reload pseudos should not be inherited. */
1100 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1101 /* True if the insn commutative operands should be swapped. */
1102 static bool goal_alt_swapped;
1103 /* The chosen insn alternative. */
1104 static int goal_alt_number;
1106 /* The following five variables are used to choose the best insn
1107 alternative. They reflect final characteristics of the best
1108 alternative. */
1110 /* Number of necessary reloads and overall cost reflecting the
1111 previous value and other unpleasantness of the best alternative. */
1112 static int best_losers, best_overall;
1113 /* Overall number hard registers used for reloads. For example, on
1114 some targets we need 2 general registers to reload DFmode and only
1115 one floating point register. */
1116 static int best_reload_nregs;
1117 /* Overall number reflecting distances of previous reloading the same
1118 value. The distances are counted from the current BB start. It is
1119 used to improve inheritance chances. */
1120 static int best_reload_sum;
1122 /* True if the current insn should have no correspondingly input or
1123 output reloads. */
1124 static bool no_input_reloads_p, no_output_reloads_p;
1126 /* True if we swapped the commutative operands in the current
1127 insn. */
1128 static int curr_swapped;
1130 /* Arrange for address element *LOC to be a register of class CL.
1131 Add any input reloads to list BEFORE. AFTER is nonnull if *LOC is an
1132 automodified value; handle that case by adding the required output
1133 reloads to list AFTER. Return true if the RTL was changed. */
1134 static bool
1135 process_addr_reg (rtx *loc, rtx *before, rtx *after, enum reg_class cl)
1137 int regno;
1138 enum reg_class rclass, new_class;
1139 rtx reg;
1140 rtx new_reg;
1141 enum machine_mode mode;
1142 bool before_p = false;
1144 loc = strip_subreg (loc);
1145 reg = *loc;
1146 mode = GET_MODE (reg);
1147 if (! REG_P (reg))
1149 /* Always reload memory in an address even if the target supports
1150 such addresses. */
1151 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1152 before_p = true;
1154 else
1156 regno = REGNO (reg);
1157 rclass = get_reg_class (regno);
1158 if ((*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1160 if (lra_dump_file != NULL)
1162 fprintf (lra_dump_file,
1163 "Changing pseudo %d in address of insn %u on equiv ",
1164 REGNO (reg), INSN_UID (curr_insn));
1165 dump_value_slim (lra_dump_file, *loc, 1);
1166 fprintf (lra_dump_file, "\n");
1168 *loc = copy_rtx (*loc);
1170 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1172 reg = *loc;
1173 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1174 mode, reg, cl, "address", &new_reg))
1175 before_p = true;
1177 else if (new_class != NO_REGS && rclass != new_class)
1179 lra_change_class (regno, new_class, " Change to", true);
1180 return false;
1182 else
1183 return false;
1185 if (before_p)
1187 push_to_sequence (*before);
1188 lra_emit_move (new_reg, reg);
1189 *before = get_insns ();
1190 end_sequence ();
1192 *loc = new_reg;
1193 if (after != NULL)
1195 start_sequence ();
1196 lra_emit_move (reg, new_reg);
1197 emit_insn (*after);
1198 *after = get_insns ();
1199 end_sequence ();
1201 return true;
1204 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1205 the insn to be inserted before curr insn. AFTER returns the
1206 the insn to be inserted after curr insn. ORIGREG and NEWREG
1207 are the original reg and new reg for reload. */
1208 static void
1209 insert_move_for_subreg (rtx *before, rtx *after, rtx origreg, rtx newreg)
1211 if (before)
1213 push_to_sequence (*before);
1214 lra_emit_move (newreg, origreg);
1215 *before = get_insns ();
1216 end_sequence ();
1218 if (after)
1220 start_sequence ();
1221 lra_emit_move (origreg, newreg);
1222 emit_insn (*after);
1223 *after = get_insns ();
1224 end_sequence ();
1228 /* Make reloads for subreg in operand NOP with internal subreg mode
1229 REG_MODE, add new reloads for further processing. Return true if
1230 any reload was generated. */
1231 static bool
1232 simplify_operand_subreg (int nop, enum machine_mode reg_mode)
1234 int hard_regno;
1235 rtx before, after;
1236 enum machine_mode mode;
1237 rtx reg, new_reg;
1238 rtx operand = *curr_id->operand_loc[nop];
1239 enum reg_class regclass;
1240 enum op_type type;
1242 before = after = NULL_RTX;
1244 if (GET_CODE (operand) != SUBREG)
1245 return false;
1247 mode = GET_MODE (operand);
1248 reg = SUBREG_REG (operand);
1249 type = curr_static_id->operand[nop].type;
1250 /* If we change address for paradoxical subreg of memory, the
1251 address might violate the necessary alignment or the access might
1252 be slow. So take this into consideration. We should not worry
1253 about access beyond allocated memory for paradoxical memory
1254 subregs as we don't substitute such equiv memory (see processing
1255 equivalences in function lra_constraints) and because for spilled
1256 pseudos we allocate stack memory enough for the biggest
1257 corresponding paradoxical subreg. */
1258 if ((MEM_P (reg)
1259 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1260 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1261 || (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER))
1263 alter_subreg (curr_id->operand_loc[nop], false);
1264 return true;
1266 /* Put constant into memory when we have mixed modes. It generates
1267 a better code in most cases as it does not need a secondary
1268 reload memory. It also prevents LRA looping when LRA is using
1269 secondary reload memory again and again. */
1270 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1271 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1273 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1274 alter_subreg (curr_id->operand_loc[nop], false);
1275 return true;
1277 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1278 if there may be a problem accessing OPERAND in the outer
1279 mode. */
1280 if ((REG_P (reg)
1281 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1282 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1283 /* Don't reload paradoxical subregs because we could be looping
1284 having repeatedly final regno out of hard regs range. */
1285 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1286 >= hard_regno_nregs[hard_regno][mode])
1287 && simplify_subreg_regno (hard_regno, GET_MODE (reg),
1288 SUBREG_BYTE (operand), mode) < 0
1289 /* Don't reload subreg for matching reload. It is actually
1290 valid subreg in LRA. */
1291 && ! LRA_SUBREG_P (operand))
1292 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1294 enum reg_class rclass;
1296 if (REG_P (reg))
1297 /* There is a big probability that we will get the same class
1298 for the new pseudo and we will get the same insn which
1299 means infinite looping. So spill the new pseudo. */
1300 rclass = NO_REGS;
1301 else
1302 /* The class will be defined later in curr_insn_transform. */
1303 rclass
1304 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1306 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1307 rclass, "subreg reg", &new_reg))
1309 bool insert_before, insert_after;
1310 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1312 insert_before = (type != OP_OUT
1313 || GET_MODE_SIZE (GET_MODE (reg)) > GET_MODE_SIZE (mode));
1314 insert_after = (type != OP_IN);
1315 insert_move_for_subreg (insert_before ? &before : NULL,
1316 insert_after ? &after : NULL,
1317 reg, new_reg);
1319 SUBREG_REG (operand) = new_reg;
1320 lra_process_new_insns (curr_insn, before, after,
1321 "Inserting subreg reload");
1322 return true;
1324 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1325 IRA allocates hardreg to the inner pseudo reg according to its mode
1326 instead of the outermode, so the size of the hardreg may not be enough
1327 to contain the outermode operand, in that case we may need to insert
1328 reload for the reg. For the following two types of paradoxical subreg,
1329 we need to insert reload:
1330 1. If the op_type is OP_IN, and the hardreg could not be paired with
1331 other hardreg to contain the outermode operand
1332 (checked by in_hard_reg_set_p), we need to insert the reload.
1333 2. If the op_type is OP_OUT or OP_INOUT.
1335 Here is a paradoxical subreg example showing how the reload is generated:
1337 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1338 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1340 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1341 here, if reg107 is assigned to hardreg R15, because R15 is the last
1342 hardreg, compiler cannot find another hardreg to pair with R15 to
1343 contain TImode data. So we insert a TImode reload reg180 for it.
1344 After reload is inserted:
1346 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1347 (reg:DI 107 [ __comp ])) -1
1348 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1349 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1351 Two reload hard registers will be allocated to reg180 to save TImode data
1352 in LRA_assign. */
1353 else if (REG_P (reg)
1354 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1355 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1356 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1357 < hard_regno_nregs[hard_regno][mode])
1358 && (regclass = lra_get_allocno_class (REGNO (reg)))
1359 && (type != OP_IN
1360 || !in_hard_reg_set_p (reg_class_contents[regclass],
1361 mode, hard_regno)))
1363 /* The class will be defined later in curr_insn_transform. */
1364 enum reg_class rclass
1365 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1367 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1368 rclass, "paradoxical subreg", &new_reg))
1370 rtx subreg;
1371 bool insert_before, insert_after;
1373 PUT_MODE (new_reg, mode);
1374 subreg = simplify_gen_subreg (GET_MODE (reg), new_reg, mode, 0);
1375 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1377 insert_before = (type != OP_OUT);
1378 insert_after = (type != OP_IN);
1379 insert_move_for_subreg (insert_before ? &before : NULL,
1380 insert_after ? &after : NULL,
1381 reg, subreg);
1383 SUBREG_REG (operand) = new_reg;
1384 lra_process_new_insns (curr_insn, before, after,
1385 "Inserting paradoxical subreg reload");
1386 return true;
1388 return false;
1391 /* Return TRUE if X refers for a hard register from SET. */
1392 static bool
1393 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1395 int i, j, x_hard_regno;
1396 enum machine_mode mode;
1397 const char *fmt;
1398 enum rtx_code code;
1400 if (x == NULL_RTX)
1401 return false;
1402 code = GET_CODE (x);
1403 mode = GET_MODE (x);
1404 if (code == SUBREG)
1406 x = SUBREG_REG (x);
1407 code = GET_CODE (x);
1408 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1409 mode = GET_MODE (x);
1412 if (REG_P (x))
1414 x_hard_regno = get_hard_regno (x);
1415 return (x_hard_regno >= 0
1416 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1418 if (MEM_P (x))
1420 struct address_info ad;
1422 decompose_mem_address (&ad, x);
1423 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1424 return true;
1425 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1426 return true;
1428 fmt = GET_RTX_FORMAT (code);
1429 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1431 if (fmt[i] == 'e')
1433 if (uses_hard_regs_p (XEXP (x, i), set))
1434 return true;
1436 else if (fmt[i] == 'E')
1438 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1439 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1440 return true;
1443 return false;
1446 /* Return true if OP is a spilled pseudo. */
1447 static inline bool
1448 spilled_pseudo_p (rtx op)
1450 return (REG_P (op)
1451 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1454 /* Return true if X is a general constant. */
1455 static inline bool
1456 general_constant_p (rtx x)
1458 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1461 static bool
1462 reg_in_class_p (rtx reg, enum reg_class cl)
1464 if (cl == NO_REGS)
1465 return get_reg_class (REGNO (reg)) == NO_REGS;
1466 return in_class_p (reg, cl, NULL);
1469 /* Major function to choose the current insn alternative and what
1470 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1471 negative we should consider only this alternative. Return false if
1472 we can not choose the alternative or find how to reload the
1473 operands. */
1474 static bool
1475 process_alt_operands (int only_alternative)
1477 bool ok_p = false;
1478 int nop, overall, nalt;
1479 int n_alternatives = curr_static_id->n_alternatives;
1480 int n_operands = curr_static_id->n_operands;
1481 /* LOSERS counts the operands that don't fit this alternative and
1482 would require loading. */
1483 int losers;
1484 /* REJECT is a count of how undesirable this alternative says it is
1485 if any reloading is required. If the alternative matches exactly
1486 then REJECT is ignored, but otherwise it gets this much counted
1487 against it in addition to the reloading needed. */
1488 int reject;
1489 /* The number of elements in the following array. */
1490 int early_clobbered_regs_num;
1491 /* Numbers of operands which are early clobber registers. */
1492 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1493 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1494 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1495 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1496 bool curr_alt_win[MAX_RECOG_OPERANDS];
1497 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1498 int curr_alt_matches[MAX_RECOG_OPERANDS];
1499 /* The number of elements in the following array. */
1500 int curr_alt_dont_inherit_ops_num;
1501 /* Numbers of operands whose reload pseudos should not be inherited. */
1502 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1503 rtx op;
1504 /* The register when the operand is a subreg of register, otherwise the
1505 operand itself. */
1506 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1507 /* The register if the operand is a register or subreg of register,
1508 otherwise NULL. */
1509 rtx operand_reg[MAX_RECOG_OPERANDS];
1510 int hard_regno[MAX_RECOG_OPERANDS];
1511 enum machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1512 int reload_nregs, reload_sum;
1513 bool costly_p;
1514 enum reg_class cl;
1516 /* Calculate some data common for all alternatives to speed up the
1517 function. */
1518 for (nop = 0; nop < n_operands; nop++)
1520 rtx reg;
1522 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1523 /* The real hard regno of the operand after the allocation. */
1524 hard_regno[nop] = get_hard_regno (op);
1526 operand_reg[nop] = reg = op;
1527 biggest_mode[nop] = GET_MODE (op);
1528 if (GET_CODE (op) == SUBREG)
1530 operand_reg[nop] = reg = SUBREG_REG (op);
1531 if (GET_MODE_SIZE (biggest_mode[nop])
1532 < GET_MODE_SIZE (GET_MODE (reg)))
1533 biggest_mode[nop] = GET_MODE (reg);
1535 if (! REG_P (reg))
1536 operand_reg[nop] = NULL_RTX;
1537 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1538 || ((int) REGNO (reg)
1539 == lra_get_elimination_hard_regno (REGNO (reg))))
1540 no_subreg_reg_operand[nop] = reg;
1541 else
1542 operand_reg[nop] = no_subreg_reg_operand[nop]
1543 /* Just use natural mode for elimination result. It should
1544 be enough for extra constraints hooks. */
1545 = regno_reg_rtx[hard_regno[nop]];
1548 /* The constraints are made of several alternatives. Each operand's
1549 constraint looks like foo,bar,... with commas separating the
1550 alternatives. The first alternatives for all operands go
1551 together, the second alternatives go together, etc.
1553 First loop over alternatives. */
1554 for (nalt = 0; nalt < n_alternatives; nalt++)
1556 /* Loop over operands for one constraint alternative. */
1557 #if HAVE_ATTR_enabled
1558 if (curr_id->alternative_enabled_p != NULL
1559 && ! curr_id->alternative_enabled_p[nalt])
1560 continue;
1561 #endif
1563 if (only_alternative >= 0 && nalt != only_alternative)
1564 continue;
1567 overall = losers = reject = reload_nregs = reload_sum = 0;
1568 for (nop = 0; nop < n_operands; nop++)
1570 int inc = (curr_static_id
1571 ->operand_alternative[nalt * n_operands + nop].reject);
1572 if (lra_dump_file != NULL && inc != 0)
1573 fprintf (lra_dump_file,
1574 " Staticly defined alt reject+=%d\n", inc);
1575 reject += inc;
1577 early_clobbered_regs_num = 0;
1579 for (nop = 0; nop < n_operands; nop++)
1581 const char *p;
1582 char *end;
1583 int len, c, m, i, opalt_num, this_alternative_matches;
1584 bool win, did_match, offmemok, early_clobber_p;
1585 /* false => this operand can be reloaded somehow for this
1586 alternative. */
1587 bool badop;
1588 /* true => this operand can be reloaded if the alternative
1589 allows regs. */
1590 bool winreg;
1591 /* True if a constant forced into memory would be OK for
1592 this operand. */
1593 bool constmemok;
1594 enum reg_class this_alternative, this_costly_alternative;
1595 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1596 bool this_alternative_match_win, this_alternative_win;
1597 bool this_alternative_offmemok;
1598 bool scratch_p;
1599 enum machine_mode mode;
1601 opalt_num = nalt * n_operands + nop;
1602 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1604 /* Fast track for no constraints at all. */
1605 curr_alt[nop] = NO_REGS;
1606 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1607 curr_alt_win[nop] = true;
1608 curr_alt_match_win[nop] = false;
1609 curr_alt_offmemok[nop] = false;
1610 curr_alt_matches[nop] = -1;
1611 continue;
1614 op = no_subreg_reg_operand[nop];
1615 mode = curr_operand_mode[nop];
1617 win = did_match = winreg = offmemok = constmemok = false;
1618 badop = true;
1620 early_clobber_p = false;
1621 p = curr_static_id->operand_alternative[opalt_num].constraint;
1623 this_costly_alternative = this_alternative = NO_REGS;
1624 /* We update set of possible hard regs besides its class
1625 because reg class might be inaccurate. For example,
1626 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1627 is translated in HI_REGS because classes are merged by
1628 pairs and there is no accurate intermediate class. */
1629 CLEAR_HARD_REG_SET (this_alternative_set);
1630 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1631 this_alternative_win = false;
1632 this_alternative_match_win = false;
1633 this_alternative_offmemok = false;
1634 this_alternative_matches = -1;
1636 /* An empty constraint should be excluded by the fast
1637 track. */
1638 lra_assert (*p != 0 && *p != ',');
1640 /* Scan this alternative's specs for this operand; set WIN
1641 if the operand fits any letter in this alternative.
1642 Otherwise, clear BADOP if this operand could fit some
1643 letter after reloads, or set WINREG if this operand could
1644 fit after reloads provided the constraint allows some
1645 registers. */
1646 costly_p = false;
1649 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1651 case '\0':
1652 len = 0;
1653 break;
1654 case ',':
1655 c = '\0';
1656 break;
1658 case '=': case '+': case '?': case '*': case '!':
1659 case ' ': case '\t':
1660 break;
1662 case '%':
1663 /* We only support one commutative marker, the first
1664 one. We already set commutative above. */
1665 break;
1667 case '&':
1668 early_clobber_p = true;
1669 break;
1671 case '#':
1672 /* Ignore rest of this alternative. */
1673 c = '\0';
1674 break;
1676 case '0': case '1': case '2': case '3': case '4':
1677 case '5': case '6': case '7': case '8': case '9':
1679 int m_hregno;
1680 bool match_p;
1682 m = strtoul (p, &end, 10);
1683 p = end;
1684 len = 0;
1685 lra_assert (nop > m);
1687 this_alternative_matches = m;
1688 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1689 /* We are supposed to match a previous operand.
1690 If we do, we win if that one did. If we do
1691 not, count both of the operands as losers.
1692 (This is too conservative, since most of the
1693 time only a single reload insn will be needed
1694 to make the two operands win. As a result,
1695 this alternative may be rejected when it is
1696 actually desirable.) */
1697 match_p = false;
1698 if (operands_match_p (*curr_id->operand_loc[nop],
1699 *curr_id->operand_loc[m], m_hregno))
1701 /* We should reject matching of an early
1702 clobber operand if the matching operand is
1703 not dying in the insn. */
1704 if (! curr_static_id->operand[m].early_clobber
1705 || operand_reg[nop] == NULL_RTX
1706 || (find_regno_note (curr_insn, REG_DEAD,
1707 REGNO (op))
1708 || REGNO (op) == REGNO (operand_reg[m])))
1709 match_p = true;
1711 if (match_p)
1713 /* If we are matching a non-offsettable
1714 address where an offsettable address was
1715 expected, then we must reject this
1716 combination, because we can't reload
1717 it. */
1718 if (curr_alt_offmemok[m]
1719 && MEM_P (*curr_id->operand_loc[m])
1720 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1721 continue;
1723 else
1725 /* Operands don't match. Both operands must
1726 allow a reload register, otherwise we
1727 cannot make them match. */
1728 if (curr_alt[m] == NO_REGS)
1729 break;
1730 /* Retroactively mark the operand we had to
1731 match as a loser, if it wasn't already and
1732 it wasn't matched to a register constraint
1733 (e.g it might be matched by memory). */
1734 if (curr_alt_win[m]
1735 && (operand_reg[m] == NULL_RTX
1736 || hard_regno[m] < 0))
1738 losers++;
1739 reload_nregs
1740 += (ira_reg_class_max_nregs[curr_alt[m]]
1741 [GET_MODE (*curr_id->operand_loc[m])]);
1744 /* We prefer no matching alternatives because
1745 it gives more freedom in RA. */
1746 if (operand_reg[nop] == NULL_RTX
1747 || (find_regno_note (curr_insn, REG_DEAD,
1748 REGNO (operand_reg[nop]))
1749 == NULL_RTX))
1751 if (lra_dump_file != NULL)
1752 fprintf
1753 (lra_dump_file,
1754 " %d Matching alt: reject+=2\n",
1755 nop);
1756 reject += 2;
1759 /* If we have to reload this operand and some
1760 previous operand also had to match the same
1761 thing as this operand, we don't know how to do
1762 that. */
1763 if (!match_p || !curr_alt_win[m])
1765 for (i = 0; i < nop; i++)
1766 if (curr_alt_matches[i] == m)
1767 break;
1768 if (i < nop)
1769 break;
1771 else
1772 did_match = true;
1774 /* This can be fixed with reloads if the operand
1775 we are supposed to match can be fixed with
1776 reloads. */
1777 badop = false;
1778 this_alternative = curr_alt[m];
1779 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1780 winreg = this_alternative != NO_REGS;
1781 break;
1784 case 'p':
1785 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1786 ADDRESS, SCRATCH);
1787 this_alternative = reg_class_subunion[this_alternative][cl];
1788 IOR_HARD_REG_SET (this_alternative_set,
1789 reg_class_contents[cl]);
1790 if (costly_p)
1792 this_costly_alternative
1793 = reg_class_subunion[this_costly_alternative][cl];
1794 IOR_HARD_REG_SET (this_costly_alternative_set,
1795 reg_class_contents[cl]);
1797 win = true;
1798 badop = false;
1799 break;
1801 case TARGET_MEM_CONSTRAINT:
1802 if (MEM_P (op) || spilled_pseudo_p (op))
1803 win = true;
1804 /* We can put constant or pseudo value into memory
1805 to satisfy the constraint. */
1806 if (CONST_POOL_OK_P (mode, op) || REG_P (op))
1807 badop = false;
1808 constmemok = true;
1809 break;
1811 case '<':
1812 if (MEM_P (op)
1813 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
1814 || GET_CODE (XEXP (op, 0)) == POST_DEC))
1815 win = true;
1816 break;
1818 case '>':
1819 if (MEM_P (op)
1820 && (GET_CODE (XEXP (op, 0)) == PRE_INC
1821 || GET_CODE (XEXP (op, 0)) == POST_INC))
1822 win = true;
1823 break;
1825 /* Memory op whose address is not offsettable. */
1826 case 'V':
1827 if (MEM_P (op)
1828 && ! offsettable_nonstrict_memref_p (op))
1829 win = true;
1830 break;
1832 /* Memory operand whose address is offsettable. */
1833 case 'o':
1834 if ((MEM_P (op)
1835 && offsettable_nonstrict_memref_p (op))
1836 || spilled_pseudo_p (op))
1837 win = true;
1838 /* We can put constant or pseudo value into memory
1839 or make memory address offsetable to satisfy the
1840 constraint. */
1841 if (CONST_POOL_OK_P (mode, op) || MEM_P (op) || REG_P (op))
1842 badop = false;
1843 constmemok = true;
1844 offmemok = true;
1845 break;
1847 case 'E':
1848 case 'F':
1849 if (GET_CODE (op) == CONST_DOUBLE
1850 || (GET_CODE (op) == CONST_VECTOR
1851 && (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)))
1852 win = true;
1853 break;
1855 case 'G':
1856 case 'H':
1857 if (CONST_DOUBLE_AS_FLOAT_P (op)
1858 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
1859 win = true;
1860 break;
1862 case 's':
1863 if (CONST_SCALAR_INT_P (op))
1864 break;
1866 case 'i':
1867 if (general_constant_p (op))
1868 win = true;
1869 break;
1871 case 'n':
1872 if (CONST_SCALAR_INT_P (op))
1873 win = true;
1874 break;
1876 case 'I':
1877 case 'J':
1878 case 'K':
1879 case 'L':
1880 case 'M':
1881 case 'N':
1882 case 'O':
1883 case 'P':
1884 if (CONST_INT_P (op)
1885 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
1886 win = true;
1887 break;
1889 case 'X':
1890 /* This constraint should be excluded by the fast
1891 track. */
1892 gcc_unreachable ();
1893 break;
1895 case 'g':
1896 if (MEM_P (op)
1897 || general_constant_p (op)
1898 || spilled_pseudo_p (op))
1899 win = true;
1900 /* Drop through into 'r' case. */
1902 case 'r':
1903 this_alternative
1904 = reg_class_subunion[this_alternative][GENERAL_REGS];
1905 IOR_HARD_REG_SET (this_alternative_set,
1906 reg_class_contents[GENERAL_REGS]);
1907 if (costly_p)
1909 this_costly_alternative
1910 = (reg_class_subunion
1911 [this_costly_alternative][GENERAL_REGS]);
1912 IOR_HARD_REG_SET (this_costly_alternative_set,
1913 reg_class_contents[GENERAL_REGS]);
1915 goto reg;
1917 default:
1918 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
1920 #ifdef EXTRA_CONSTRAINT_STR
1921 if (EXTRA_MEMORY_CONSTRAINT (c, p))
1923 if (EXTRA_CONSTRAINT_STR (op, c, p))
1924 win = true;
1925 else if (spilled_pseudo_p (op))
1926 win = true;
1928 /* If we didn't already win, we can reload
1929 constants via force_const_mem or put the
1930 pseudo value into memory, or make other
1931 memory by reloading the address like for
1932 'o'. */
1933 if (CONST_POOL_OK_P (mode, op)
1934 || MEM_P (op) || REG_P (op))
1935 badop = false;
1936 constmemok = true;
1937 offmemok = true;
1938 break;
1940 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1942 if (EXTRA_CONSTRAINT_STR (op, c, p))
1943 win = true;
1945 /* If we didn't already win, we can reload
1946 the address into a base register. */
1947 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1948 ADDRESS, SCRATCH);
1949 this_alternative
1950 = reg_class_subunion[this_alternative][cl];
1951 IOR_HARD_REG_SET (this_alternative_set,
1952 reg_class_contents[cl]);
1953 if (costly_p)
1955 this_costly_alternative
1956 = (reg_class_subunion
1957 [this_costly_alternative][cl]);
1958 IOR_HARD_REG_SET (this_costly_alternative_set,
1959 reg_class_contents[cl]);
1961 badop = false;
1962 break;
1965 if (EXTRA_CONSTRAINT_STR (op, c, p))
1966 win = true;
1967 #endif
1968 break;
1971 cl = REG_CLASS_FROM_CONSTRAINT (c, p);
1972 this_alternative = reg_class_subunion[this_alternative][cl];
1973 IOR_HARD_REG_SET (this_alternative_set,
1974 reg_class_contents[cl]);
1975 if (costly_p)
1977 this_costly_alternative
1978 = reg_class_subunion[this_costly_alternative][cl];
1979 IOR_HARD_REG_SET (this_costly_alternative_set,
1980 reg_class_contents[cl]);
1982 reg:
1983 if (mode == BLKmode)
1984 break;
1985 winreg = true;
1986 if (REG_P (op))
1988 if (hard_regno[nop] >= 0
1989 && in_hard_reg_set_p (this_alternative_set,
1990 mode, hard_regno[nop]))
1991 win = true;
1992 else if (hard_regno[nop] < 0
1993 && in_class_p (op, this_alternative, NULL))
1994 win = true;
1996 break;
1998 if (c != ' ' && c != '\t')
1999 costly_p = c == '*';
2001 while ((p += len), c);
2003 scratch_p = (operand_reg[nop] != NULL_RTX
2004 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2005 /* Record which operands fit this alternative. */
2006 if (win)
2008 this_alternative_win = true;
2009 if (operand_reg[nop] != NULL_RTX)
2011 if (hard_regno[nop] >= 0)
2013 if (in_hard_reg_set_p (this_costly_alternative_set,
2014 mode, hard_regno[nop]))
2016 if (lra_dump_file != NULL)
2017 fprintf (lra_dump_file,
2018 " %d Costly set: reject++\n",
2019 nop);
2020 reject++;
2023 else
2025 /* Prefer won reg to spilled pseudo under other
2026 equal conditions for possibe inheritance. */
2027 if (! scratch_p)
2029 if (lra_dump_file != NULL)
2030 fprintf
2031 (lra_dump_file,
2032 " %d Non pseudo reload: reject++\n",
2033 nop);
2034 reject++;
2036 if (in_class_p (operand_reg[nop],
2037 this_costly_alternative, NULL))
2039 if (lra_dump_file != NULL)
2040 fprintf
2041 (lra_dump_file,
2042 " %d Non pseudo costly reload:"
2043 " reject++\n",
2044 nop);
2045 reject++;
2048 /* We simulate the behaviour of old reload here.
2049 Although scratches need hard registers and it
2050 might result in spilling other pseudos, no reload
2051 insns are generated for the scratches. So it
2052 might cost something but probably less than old
2053 reload pass believes. */
2054 if (scratch_p)
2056 if (lra_dump_file != NULL)
2057 fprintf (lra_dump_file,
2058 " %d Scratch win: reject+=2\n",
2059 nop);
2060 reject += 2;
2064 else if (did_match)
2065 this_alternative_match_win = true;
2066 else
2068 int const_to_mem = 0;
2069 bool no_regs_p;
2071 /* Never do output reload of stack pointer. It makes
2072 impossible to do elimination when SP is changed in
2073 RTL. */
2074 if (op == stack_pointer_rtx && ! frame_pointer_needed
2075 && curr_static_id->operand[nop].type != OP_IN)
2076 goto fail;
2078 /* If this alternative asks for a specific reg class, see if there
2079 is at least one allocatable register in that class. */
2080 no_regs_p
2081 = (this_alternative == NO_REGS
2082 || (hard_reg_set_subset_p
2083 (reg_class_contents[this_alternative],
2084 lra_no_alloc_regs)));
2086 /* For asms, verify that the class for this alternative is possible
2087 for the mode that is specified. */
2088 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2090 int i;
2091 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2092 if (HARD_REGNO_MODE_OK (i, mode)
2093 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2094 mode, i))
2095 break;
2096 if (i == FIRST_PSEUDO_REGISTER)
2097 winreg = false;
2100 /* If this operand accepts a register, and if the
2101 register class has at least one allocatable register,
2102 then this operand can be reloaded. */
2103 if (winreg && !no_regs_p)
2104 badop = false;
2106 if (badop)
2108 if (lra_dump_file != NULL)
2109 fprintf (lra_dump_file,
2110 " alt=%d: Bad operand -- refuse\n",
2111 nalt);
2112 goto fail;
2115 /* If not assigned pseudo has a class which a subset of
2116 required reg class, it is a less costly alternative
2117 as the pseudo still can get a hard reg of necessary
2118 class. */
2119 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2120 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2121 && ira_class_subset_p[this_alternative][cl])
2123 if (lra_dump_file != NULL)
2124 fprintf
2125 (lra_dump_file,
2126 " %d Super set class reg: reject-=3\n", nop);
2127 reject -= 3;
2130 this_alternative_offmemok = offmemok;
2131 if (this_costly_alternative != NO_REGS)
2133 if (lra_dump_file != NULL)
2134 fprintf (lra_dump_file,
2135 " %d Costly loser: reject++\n", nop);
2136 reject++;
2138 /* If the operand is dying, has a matching constraint,
2139 and satisfies constraints of the matched operand
2140 which failed to satisfy the own constraints, probably
2141 the reload for this operand will be gone. */
2142 if (this_alternative_matches >= 0
2143 && !curr_alt_win[this_alternative_matches]
2144 && REG_P (op)
2145 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2146 && (hard_regno[nop] >= 0
2147 ? in_hard_reg_set_p (this_alternative_set,
2148 mode, hard_regno[nop])
2149 : in_class_p (op, this_alternative, NULL)))
2151 if (lra_dump_file != NULL)
2152 fprintf
2153 (lra_dump_file,
2154 " %d Dying matched operand reload: reject++\n",
2155 nop);
2156 reject++;
2158 else
2160 /* Strict_low_part requires to reload the register
2161 not the sub-register. In this case we should
2162 check that a final reload hard reg can hold the
2163 value mode. */
2164 if (curr_static_id->operand[nop].strict_low
2165 && REG_P (op)
2166 && hard_regno[nop] < 0
2167 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2168 && ira_class_hard_regs_num[this_alternative] > 0
2169 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2170 [this_alternative][0],
2171 GET_MODE
2172 (*curr_id->operand_loc[nop])))
2174 if (lra_dump_file != NULL)
2175 fprintf
2176 (lra_dump_file,
2177 " alt=%d: Strict low subreg reload -- refuse\n",
2178 nalt);
2179 goto fail;
2181 losers++;
2183 if (operand_reg[nop] != NULL_RTX
2184 /* Output operands and matched input operands are
2185 not inherited. The following conditions do not
2186 exactly describe the previous statement but they
2187 are pretty close. */
2188 && curr_static_id->operand[nop].type != OP_OUT
2189 && (this_alternative_matches < 0
2190 || curr_static_id->operand[nop].type != OP_IN))
2192 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2193 (operand_reg[nop])]
2194 .last_reload);
2196 /* The value of reload_sum has sense only if we
2197 process insns in their order. It happens only on
2198 the first constraints sub-pass when we do most of
2199 reload work. */
2200 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2201 reload_sum += last_reload - bb_reload_num;
2203 /* If this is a constant that is reloaded into the
2204 desired class by copying it to memory first, count
2205 that as another reload. This is consistent with
2206 other code and is required to avoid choosing another
2207 alternative when the constant is moved into memory.
2208 Note that the test here is precisely the same as in
2209 the code below that calls force_const_mem. */
2210 if (CONST_POOL_OK_P (mode, op)
2211 && ((targetm.preferred_reload_class
2212 (op, this_alternative) == NO_REGS)
2213 || no_input_reloads_p))
2215 const_to_mem = 1;
2216 if (! no_regs_p)
2217 losers++;
2220 /* Alternative loses if it requires a type of reload not
2221 permitted for this insn. We can always reload
2222 objects with a REG_UNUSED note. */
2223 if ((curr_static_id->operand[nop].type != OP_IN
2224 && no_output_reloads_p
2225 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2226 || (curr_static_id->operand[nop].type != OP_OUT
2227 && no_input_reloads_p && ! const_to_mem)
2228 || (this_alternative_matches >= 0
2229 && (no_input_reloads_p
2230 || (no_output_reloads_p
2231 && (curr_static_id->operand
2232 [this_alternative_matches].type != OP_IN)
2233 && ! find_reg_note (curr_insn, REG_UNUSED,
2234 no_subreg_reg_operand
2235 [this_alternative_matches])))))
2237 if (lra_dump_file != NULL)
2238 fprintf
2239 (lra_dump_file,
2240 " alt=%d: No input/otput reload -- refuse\n",
2241 nalt);
2242 goto fail;
2245 /* Check strong discouragement of reload of non-constant
2246 into class THIS_ALTERNATIVE. */
2247 if (! CONSTANT_P (op) && ! no_regs_p
2248 && (targetm.preferred_reload_class
2249 (op, this_alternative) == NO_REGS
2250 || (curr_static_id->operand[nop].type == OP_OUT
2251 && (targetm.preferred_output_reload_class
2252 (op, this_alternative) == NO_REGS))))
2254 if (lra_dump_file != NULL)
2255 fprintf (lra_dump_file,
2256 " %d Non-prefered reload: reject+=%d\n",
2257 nop, LRA_MAX_REJECT);
2258 reject += LRA_MAX_REJECT;
2261 if (! (MEM_P (op) && offmemok)
2262 && ! (const_to_mem && constmemok))
2264 /* We prefer to reload pseudos over reloading other
2265 things, since such reloads may be able to be
2266 eliminated later. So bump REJECT in other cases.
2267 Don't do this in the case where we are forcing a
2268 constant into memory and it will then win since
2269 we don't want to have a different alternative
2270 match then. */
2271 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2273 if (lra_dump_file != NULL)
2274 fprintf
2275 (lra_dump_file,
2276 " %d Non-pseudo reload: reject+=2\n",
2277 nop);
2278 reject += 2;
2281 if (! no_regs_p)
2282 reload_nregs
2283 += ira_reg_class_max_nregs[this_alternative][mode];
2285 if (SMALL_REGISTER_CLASS_P (this_alternative))
2287 if (lra_dump_file != NULL)
2288 fprintf
2289 (lra_dump_file,
2290 " %d Small class reload: reject+=%d\n",
2291 nop, LRA_LOSER_COST_FACTOR / 2);
2292 reject += LRA_LOSER_COST_FACTOR / 2;
2296 /* We are trying to spill pseudo into memory. It is
2297 usually more costly than moving to a hard register
2298 although it might takes the same number of
2299 reloads. */
2300 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2302 if (lra_dump_file != NULL)
2303 fprintf
2304 (lra_dump_file,
2305 " %d Spill pseudo into memory: reject+=3\n",
2306 nop);
2307 reject += 3;
2308 if (VECTOR_MODE_P (mode))
2310 /* Spilling vectors into memory is usually more
2311 costly as they contain big values. */
2312 if (lra_dump_file != NULL)
2313 fprintf
2314 (lra_dump_file,
2315 " %d Spill vector pseudo: reject+=2\n",
2316 nop);
2317 reject += 2;
2321 #ifdef SECONDARY_MEMORY_NEEDED
2322 /* If reload requires moving value through secondary
2323 memory, it will need one more insn at least. */
2324 if (this_alternative != NO_REGS
2325 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2326 && ((curr_static_id->operand[nop].type != OP_OUT
2327 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2328 GET_MODE (op)))
2329 || (curr_static_id->operand[nop].type != OP_IN
2330 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2331 GET_MODE (op)))))
2332 losers++;
2333 #endif
2334 /* Input reloads can be inherited more often than output
2335 reloads can be removed, so penalize output
2336 reloads. */
2337 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2339 if (lra_dump_file != NULL)
2340 fprintf
2341 (lra_dump_file,
2342 " %d Non input pseudo reload: reject++\n",
2343 nop);
2344 reject++;
2348 if (early_clobber_p && ! scratch_p)
2350 if (lra_dump_file != NULL)
2351 fprintf (lra_dump_file,
2352 " %d Early clobber: reject++\n", nop);
2353 reject++;
2355 /* ??? We check early clobbers after processing all operands
2356 (see loop below) and there we update the costs more.
2357 Should we update the cost (may be approximately) here
2358 because of early clobber register reloads or it is a rare
2359 or non-important thing to be worth to do it. */
2360 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2361 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2363 if (lra_dump_file != NULL)
2364 fprintf (lra_dump_file,
2365 " alt=%d,overall=%d,losers=%d -- refuse\n",
2366 nalt, overall, losers);
2367 goto fail;
2370 curr_alt[nop] = this_alternative;
2371 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2372 curr_alt_win[nop] = this_alternative_win;
2373 curr_alt_match_win[nop] = this_alternative_match_win;
2374 curr_alt_offmemok[nop] = this_alternative_offmemok;
2375 curr_alt_matches[nop] = this_alternative_matches;
2377 if (this_alternative_matches >= 0
2378 && !did_match && !this_alternative_win)
2379 curr_alt_win[this_alternative_matches] = false;
2381 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2382 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2384 if (curr_insn_set != NULL_RTX && n_operands == 2
2385 /* Prevent processing non-move insns. */
2386 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2387 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2388 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2389 && REG_P (no_subreg_reg_operand[0])
2390 && REG_P (no_subreg_reg_operand[1])
2391 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2392 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2393 || (! curr_alt_win[0] && curr_alt_win[1]
2394 && REG_P (no_subreg_reg_operand[1])
2395 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2396 || (curr_alt_win[0] && ! curr_alt_win[1]
2397 && REG_P (no_subreg_reg_operand[0])
2398 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2399 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2400 no_subreg_reg_operand[1])
2401 || (targetm.preferred_reload_class
2402 (no_subreg_reg_operand[1],
2403 (enum reg_class) curr_alt[1]) != NO_REGS))
2404 /* If it is a result of recent elimination in move
2405 insn we can transform it into an add still by
2406 using this alternative. */
2407 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2409 /* We have a move insn and a new reload insn will be similar
2410 to the current insn. We should avoid such situation as it
2411 results in LRA cycling. */
2412 overall += LRA_MAX_REJECT;
2414 ok_p = true;
2415 curr_alt_dont_inherit_ops_num = 0;
2416 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2418 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2419 HARD_REG_SET temp_set;
2421 i = early_clobbered_nops[nop];
2422 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2423 || hard_regno[i] < 0)
2424 continue;
2425 lra_assert (operand_reg[i] != NULL_RTX);
2426 clobbered_hard_regno = hard_regno[i];
2427 CLEAR_HARD_REG_SET (temp_set);
2428 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2429 first_conflict_j = last_conflict_j = -1;
2430 for (j = 0; j < n_operands; j++)
2431 if (j == i
2432 /* We don't want process insides of match_operator and
2433 match_parallel because otherwise we would process
2434 their operands once again generating a wrong
2435 code. */
2436 || curr_static_id->operand[j].is_operator)
2437 continue;
2438 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2439 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2440 continue;
2441 /* If we don't reload j-th operand, check conflicts. */
2442 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2443 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2445 if (first_conflict_j < 0)
2446 first_conflict_j = j;
2447 last_conflict_j = j;
2449 if (last_conflict_j < 0)
2450 continue;
2451 /* If earlyclobber operand conflicts with another
2452 non-matching operand which is actually the same register
2453 as the earlyclobber operand, it is better to reload the
2454 another operand as an operand matching the earlyclobber
2455 operand can be also the same. */
2456 if (first_conflict_j == last_conflict_j
2457 && operand_reg[last_conflict_j]
2458 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2459 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2461 curr_alt_win[last_conflict_j] = false;
2462 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2463 = last_conflict_j;
2464 losers++;
2465 /* Early clobber was already reflected in REJECT. */
2466 lra_assert (reject > 0);
2467 if (lra_dump_file != NULL)
2468 fprintf
2469 (lra_dump_file,
2470 " %d Conflict early clobber reload: reject--\n",
2472 reject--;
2473 overall += LRA_LOSER_COST_FACTOR - 1;
2475 else
2477 /* We need to reload early clobbered register and the
2478 matched registers. */
2479 for (j = 0; j < n_operands; j++)
2480 if (curr_alt_matches[j] == i)
2482 curr_alt_match_win[j] = false;
2483 losers++;
2484 overall += LRA_LOSER_COST_FACTOR;
2486 if (! curr_alt_match_win[i])
2487 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2488 else
2490 /* Remember pseudos used for match reloads are never
2491 inherited. */
2492 lra_assert (curr_alt_matches[i] >= 0);
2493 curr_alt_win[curr_alt_matches[i]] = false;
2495 curr_alt_win[i] = curr_alt_match_win[i] = false;
2496 losers++;
2497 /* Early clobber was already reflected in REJECT. */
2498 lra_assert (reject > 0);
2499 if (lra_dump_file != NULL)
2500 fprintf
2501 (lra_dump_file,
2502 " %d Matched conflict early clobber reloads:"
2503 "reject--\n",
2505 reject--;
2506 overall += LRA_LOSER_COST_FACTOR - 1;
2509 if (lra_dump_file != NULL)
2510 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2511 nalt, overall, losers, reload_nregs);
2513 /* If this alternative can be made to work by reloading, and it
2514 needs less reloading than the others checked so far, record
2515 it as the chosen goal for reloading. */
2516 if ((best_losers != 0 && losers == 0)
2517 || (((best_losers == 0 && losers == 0)
2518 || (best_losers != 0 && losers != 0))
2519 && (best_overall > overall
2520 || (best_overall == overall
2521 /* If the cost of the reloads is the same,
2522 prefer alternative which requires minimal
2523 number of reload regs. */
2524 && (reload_nregs < best_reload_nregs
2525 || (reload_nregs == best_reload_nregs
2526 && (best_reload_sum < reload_sum
2527 || (best_reload_sum == reload_sum
2528 && nalt < goal_alt_number))))))))
2530 for (nop = 0; nop < n_operands; nop++)
2532 goal_alt_win[nop] = curr_alt_win[nop];
2533 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2534 goal_alt_matches[nop] = curr_alt_matches[nop];
2535 goal_alt[nop] = curr_alt[nop];
2536 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2538 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2539 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2540 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2541 goal_alt_swapped = curr_swapped;
2542 best_overall = overall;
2543 best_losers = losers;
2544 best_reload_nregs = reload_nregs;
2545 best_reload_sum = reload_sum;
2546 goal_alt_number = nalt;
2548 if (losers == 0)
2549 /* Everything is satisfied. Do not process alternatives
2550 anymore. */
2551 break;
2552 fail:
2555 return ok_p;
2558 /* Return 1 if ADDR is a valid memory address for mode MODE in address
2559 space AS, and check that each pseudo has the proper kind of hard
2560 reg. */
2561 static int
2562 valid_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2563 rtx addr, addr_space_t as)
2565 #ifdef GO_IF_LEGITIMATE_ADDRESS
2566 lra_assert (ADDR_SPACE_GENERIC_P (as));
2567 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
2568 return 0;
2570 win:
2571 return 1;
2572 #else
2573 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
2574 #endif
2577 /* Return whether address AD is valid. */
2579 static bool
2580 valid_address_p (struct address_info *ad)
2582 /* Some ports do not check displacements for eliminable registers,
2583 so we replace them temporarily with the elimination target. */
2584 rtx saved_base_reg = NULL_RTX;
2585 rtx saved_index_reg = NULL_RTX;
2586 rtx *base_term = strip_subreg (ad->base_term);
2587 rtx *index_term = strip_subreg (ad->index_term);
2588 if (base_term != NULL)
2590 saved_base_reg = *base_term;
2591 lra_eliminate_reg_if_possible (base_term);
2592 if (ad->base_term2 != NULL)
2593 *ad->base_term2 = *ad->base_term;
2595 if (index_term != NULL)
2597 saved_index_reg = *index_term;
2598 lra_eliminate_reg_if_possible (index_term);
2600 bool ok_p = valid_address_p (ad->mode, *ad->outer, ad->as);
2601 if (saved_base_reg != NULL_RTX)
2603 *base_term = saved_base_reg;
2604 if (ad->base_term2 != NULL)
2605 *ad->base_term2 = *ad->base_term;
2607 if (saved_index_reg != NULL_RTX)
2608 *index_term = saved_index_reg;
2609 return ok_p;
2612 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2613 static rtx
2614 base_plus_disp_to_reg (struct address_info *ad)
2616 enum reg_class cl;
2617 rtx new_reg;
2619 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2620 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2621 get_index_code (ad));
2622 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2623 cl, "base + disp");
2624 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2625 return new_reg;
2628 /* Return true if we can add a displacement to address AD, even if that
2629 makes the address invalid. The fix-up code requires any new address
2630 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2631 static bool
2632 can_add_disp_p (struct address_info *ad)
2634 return (!ad->autoinc_p
2635 && ad->segment == NULL
2636 && ad->base == ad->base_term
2637 && ad->disp == ad->disp_term);
2640 /* Make equiv substitution in address AD. Return true if a substitution
2641 was made. */
2642 static bool
2643 equiv_address_substitution (struct address_info *ad)
2645 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2646 HOST_WIDE_INT disp, scale;
2647 bool change_p;
2649 base_term = strip_subreg (ad->base_term);
2650 if (base_term == NULL)
2651 base_reg = new_base_reg = NULL_RTX;
2652 else
2654 base_reg = *base_term;
2655 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2657 index_term = strip_subreg (ad->index_term);
2658 if (index_term == NULL)
2659 index_reg = new_index_reg = NULL_RTX;
2660 else
2662 index_reg = *index_term;
2663 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2665 if (base_reg == new_base_reg && index_reg == new_index_reg)
2666 return false;
2667 disp = 0;
2668 change_p = false;
2669 if (lra_dump_file != NULL)
2671 fprintf (lra_dump_file, "Changing address in insn %d ",
2672 INSN_UID (curr_insn));
2673 dump_value_slim (lra_dump_file, *ad->outer, 1);
2675 if (base_reg != new_base_reg)
2677 if (REG_P (new_base_reg))
2679 *base_term = new_base_reg;
2680 change_p = true;
2682 else if (GET_CODE (new_base_reg) == PLUS
2683 && REG_P (XEXP (new_base_reg, 0))
2684 && CONST_INT_P (XEXP (new_base_reg, 1))
2685 && can_add_disp_p (ad))
2687 disp += INTVAL (XEXP (new_base_reg, 1));
2688 *base_term = XEXP (new_base_reg, 0);
2689 change_p = true;
2691 if (ad->base_term2 != NULL)
2692 *ad->base_term2 = *ad->base_term;
2694 if (index_reg != new_index_reg)
2696 if (REG_P (new_index_reg))
2698 *index_term = new_index_reg;
2699 change_p = true;
2701 else if (GET_CODE (new_index_reg) == PLUS
2702 && REG_P (XEXP (new_index_reg, 0))
2703 && CONST_INT_P (XEXP (new_index_reg, 1))
2704 && can_add_disp_p (ad)
2705 && (scale = get_index_scale (ad)))
2707 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2708 *index_term = XEXP (new_index_reg, 0);
2709 change_p = true;
2712 if (disp != 0)
2714 if (ad->disp != NULL)
2715 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2716 else
2718 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2719 update_address (ad);
2721 change_p = true;
2723 if (lra_dump_file != NULL)
2725 if (! change_p)
2726 fprintf (lra_dump_file, " -- no change\n");
2727 else
2729 fprintf (lra_dump_file, " on equiv ");
2730 dump_value_slim (lra_dump_file, *ad->outer, 1);
2731 fprintf (lra_dump_file, "\n");
2734 return change_p;
2737 /* Major function to make reloads for an address in operand NOP.
2738 The supported cases are:
2740 1) an address that existed before LRA started, at which point it
2741 must have been valid. These addresses are subject to elimination
2742 and may have become invalid due to the elimination offset being out
2743 of range.
2745 2) an address created by forcing a constant to memory
2746 (force_const_to_mem). The initial form of these addresses might
2747 not be valid, and it is this function's job to make them valid.
2749 3) a frame address formed from a register and a (possibly zero)
2750 constant offset. As above, these addresses might not be valid and
2751 this function must make them so.
2753 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2754 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2755 address. Return true for any RTL change. */
2756 static bool
2757 process_address (int nop, rtx *before, rtx *after)
2759 struct address_info ad;
2760 rtx new_reg;
2761 rtx op = *curr_id->operand_loc[nop];
2762 const char *constraint = curr_static_id->operand[nop].constraint;
2763 bool change_p;
2765 if (constraint[0] == 'p'
2766 || EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint))
2767 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2768 else if (MEM_P (op))
2769 decompose_mem_address (&ad, op);
2770 else if (GET_CODE (op) == SUBREG
2771 && MEM_P (SUBREG_REG (op)))
2772 decompose_mem_address (&ad, SUBREG_REG (op));
2773 else
2774 return false;
2775 change_p = equiv_address_substitution (&ad);
2776 if (ad.base_term != NULL
2777 && (process_addr_reg
2778 (ad.base_term, before,
2779 (ad.autoinc_p
2780 && !(REG_P (*ad.base_term)
2781 && find_regno_note (curr_insn, REG_DEAD,
2782 REGNO (*ad.base_term)) != NULL_RTX)
2783 ? after : NULL),
2784 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2785 get_index_code (&ad)))))
2787 change_p = true;
2788 if (ad.base_term2 != NULL)
2789 *ad.base_term2 = *ad.base_term;
2791 if (ad.index_term != NULL
2792 && process_addr_reg (ad.index_term, before, NULL, INDEX_REG_CLASS))
2793 change_p = true;
2795 #ifdef EXTRA_CONSTRAINT_STR
2796 /* Target hooks sometimes reject extra constraint addresses -- use
2797 EXTRA_CONSTRAINT_STR for the validation. */
2798 if (constraint[0] != 'p'
2799 && EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint)
2800 && EXTRA_CONSTRAINT_STR (op, constraint[0], constraint))
2801 return change_p;
2802 #endif
2804 /* There are three cases where the shape of *AD.INNER may now be invalid:
2806 1) the original address was valid, but either elimination or
2807 equiv_address_substitution was applied and that made
2808 the address invalid.
2810 2) the address is an invalid symbolic address created by
2811 force_const_to_mem.
2813 3) the address is a frame address with an invalid offset.
2815 All these cases involve a non-autoinc address, so there is no
2816 point revalidating other types. */
2817 if (ad.autoinc_p || valid_address_p (&ad))
2818 return change_p;
2820 /* Any index existed before LRA started, so we can assume that the
2821 presence and shape of the index is valid. */
2822 push_to_sequence (*before);
2823 lra_assert (ad.disp == ad.disp_term);
2824 if (ad.base == NULL)
2826 if (ad.index == NULL)
2828 int code = -1;
2829 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2830 SCRATCH, SCRATCH);
2831 rtx addr = *ad.inner;
2833 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2834 #ifdef HAVE_lo_sum
2836 rtx insn;
2837 rtx last = get_last_insn ();
2839 /* addr => lo_sum (new_base, addr), case (2) above. */
2840 insn = emit_insn (gen_rtx_SET
2841 (VOIDmode, new_reg,
2842 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2843 code = recog_memoized (insn);
2844 if (code >= 0)
2846 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2847 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2849 /* Try to put lo_sum into register. */
2850 insn = emit_insn (gen_rtx_SET
2851 (VOIDmode, new_reg,
2852 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2853 code = recog_memoized (insn);
2854 if (code >= 0)
2856 *ad.inner = new_reg;
2857 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2859 *ad.inner = addr;
2860 code = -1;
2866 if (code < 0)
2867 delete_insns_since (last);
2869 #endif
2870 if (code < 0)
2872 /* addr => new_base, case (2) above. */
2873 lra_emit_move (new_reg, addr);
2874 *ad.inner = new_reg;
2877 else
2879 /* index * scale + disp => new base + index * scale,
2880 case (1) above. */
2881 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
2882 GET_CODE (*ad.index));
2884 lra_assert (INDEX_REG_CLASS != NO_REGS);
2885 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
2886 lra_emit_move (new_reg, *ad.disp);
2887 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2888 new_reg, *ad.index);
2891 else if (ad.index == NULL)
2893 int regno;
2894 enum reg_class cl;
2895 rtx set, insns, last_insn;
2896 /* base + disp => new base, cases (1) and (3) above. */
2897 /* Another option would be to reload the displacement into an
2898 index register. However, postreload has code to optimize
2899 address reloads that have the same base and different
2900 displacements, so reloading into an index register would
2901 not necessarily be a win. */
2902 start_sequence ();
2903 new_reg = base_plus_disp_to_reg (&ad);
2904 insns = get_insns ();
2905 last_insn = get_last_insn ();
2906 /* If we generated at least two insns, try last insn source as
2907 an address. If we succeed, we generate one less insn. */
2908 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
2909 && GET_CODE (SET_SRC (set)) == PLUS
2910 && REG_P (XEXP (SET_SRC (set), 0))
2911 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
2913 *ad.inner = SET_SRC (set);
2914 if (valid_address_p (ad.mode, *ad.outer, ad.as))
2916 *ad.base_term = XEXP (SET_SRC (set), 0);
2917 *ad.disp_term = XEXP (SET_SRC (set), 1);
2918 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2919 get_index_code (&ad));
2920 regno = REGNO (*ad.base_term);
2921 if (regno >= FIRST_PSEUDO_REGISTER
2922 && cl != lra_get_allocno_class (regno))
2923 lra_change_class (regno, cl, " Change to", true);
2924 new_reg = SET_SRC (set);
2925 delete_insns_since (PREV_INSN (last_insn));
2928 end_sequence ();
2929 emit_insn (insns);
2930 *ad.inner = new_reg;
2932 else
2934 /* base + scale * index + disp => new base + scale * index,
2935 case (1) above. */
2936 new_reg = base_plus_disp_to_reg (&ad);
2937 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2938 new_reg, *ad.index);
2940 *before = get_insns ();
2941 end_sequence ();
2942 return true;
2945 /* Emit insns to reload VALUE into a new register. VALUE is an
2946 auto-increment or auto-decrement RTX whose operand is a register or
2947 memory location; so reloading involves incrementing that location.
2948 IN is either identical to VALUE, or some cheaper place to reload
2949 value being incremented/decremented from.
2951 INC_AMOUNT is the number to increment or decrement by (always
2952 positive and ignored for POST_MODIFY/PRE_MODIFY).
2954 Return pseudo containing the result. */
2955 static rtx
2956 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
2958 /* REG or MEM to be copied and incremented. */
2959 rtx incloc = XEXP (value, 0);
2960 /* Nonzero if increment after copying. */
2961 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
2962 || GET_CODE (value) == POST_MODIFY);
2963 rtx last;
2964 rtx inc;
2965 rtx add_insn;
2966 int code;
2967 rtx real_in = in == value ? incloc : in;
2968 rtx result;
2969 bool plus_p = true;
2971 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
2973 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
2974 || GET_CODE (XEXP (value, 1)) == MINUS);
2975 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
2976 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
2977 inc = XEXP (XEXP (value, 1), 1);
2979 else
2981 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
2982 inc_amount = -inc_amount;
2984 inc = GEN_INT (inc_amount);
2987 if (! post && REG_P (incloc))
2988 result = incloc;
2989 else
2990 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
2991 "INC/DEC result");
2993 if (real_in != result)
2995 /* First copy the location to the result register. */
2996 lra_assert (REG_P (result));
2997 emit_insn (gen_move_insn (result, real_in));
3000 /* We suppose that there are insns to add/sub with the constant
3001 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3002 old reload worked with this assumption. If the assumption
3003 becomes wrong, we should use approach in function
3004 base_plus_disp_to_reg. */
3005 if (in == value)
3007 /* See if we can directly increment INCLOC. */
3008 last = get_last_insn ();
3009 add_insn = emit_insn (plus_p
3010 ? gen_add2_insn (incloc, inc)
3011 : gen_sub2_insn (incloc, inc));
3013 code = recog_memoized (add_insn);
3014 if (code >= 0)
3016 if (! post && result != incloc)
3017 emit_insn (gen_move_insn (result, incloc));
3018 return result;
3020 delete_insns_since (last);
3023 /* If couldn't do the increment directly, must increment in RESULT.
3024 The way we do this depends on whether this is pre- or
3025 post-increment. For pre-increment, copy INCLOC to the reload
3026 register, increment it there, then save back. */
3027 if (! post)
3029 if (real_in != result)
3030 emit_insn (gen_move_insn (result, real_in));
3031 if (plus_p)
3032 emit_insn (gen_add2_insn (result, inc));
3033 else
3034 emit_insn (gen_sub2_insn (result, inc));
3035 if (result != incloc)
3036 emit_insn (gen_move_insn (incloc, result));
3038 else
3040 /* Post-increment.
3042 Because this might be a jump insn or a compare, and because
3043 RESULT may not be available after the insn in an input
3044 reload, we must do the incrementing before the insn being
3045 reloaded for.
3047 We have already copied IN to RESULT. Increment the copy in
3048 RESULT, save that back, then decrement RESULT so it has
3049 the original value. */
3050 if (plus_p)
3051 emit_insn (gen_add2_insn (result, inc));
3052 else
3053 emit_insn (gen_sub2_insn (result, inc));
3054 emit_insn (gen_move_insn (incloc, result));
3055 /* Restore non-modified value for the result. We prefer this
3056 way because it does not require an additional hard
3057 register. */
3058 if (plus_p)
3060 if (CONST_INT_P (inc))
3061 emit_insn (gen_add2_insn (result,
3062 gen_int_mode (-INTVAL (inc),
3063 GET_MODE (result))));
3064 else
3065 emit_insn (gen_sub2_insn (result, inc));
3067 else
3068 emit_insn (gen_add2_insn (result, inc));
3070 return result;
3073 /* Return true if the current move insn does not need processing as we
3074 already know that it satisfies its constraints. */
3075 static bool
3076 simple_move_p (void)
3078 rtx dest, src;
3079 enum reg_class dclass, sclass;
3081 lra_assert (curr_insn_set != NULL_RTX);
3082 dest = SET_DEST (curr_insn_set);
3083 src = SET_SRC (curr_insn_set);
3084 return ((dclass = get_op_class (dest)) != NO_REGS
3085 && (sclass = get_op_class (src)) != NO_REGS
3086 /* The backend guarantees that register moves of cost 2
3087 never need reloads. */
3088 && targetm.register_move_cost (GET_MODE (src), dclass, sclass) == 2);
3091 /* Swap operands NOP and NOP + 1. */
3092 static inline void
3093 swap_operands (int nop)
3095 enum machine_mode mode = curr_operand_mode[nop];
3096 curr_operand_mode[nop] = curr_operand_mode[nop + 1];
3097 curr_operand_mode[nop + 1] = mode;
3098 rtx x = *curr_id->operand_loc[nop];
3099 *curr_id->operand_loc[nop] = *curr_id->operand_loc[nop + 1];
3100 *curr_id->operand_loc[nop + 1] = x;
3101 /* Swap the duplicates too. */
3102 lra_update_dup (curr_id, nop);
3103 lra_update_dup (curr_id, nop + 1);
3106 /* Main entry point of the constraint code: search the body of the
3107 current insn to choose the best alternative. It is mimicking insn
3108 alternative cost calculation model of former reload pass. That is
3109 because machine descriptions were written to use this model. This
3110 model can be changed in future. Make commutative operand exchange
3111 if it is chosen.
3113 Return true if some RTL changes happened during function call. */
3114 static bool
3115 curr_insn_transform (void)
3117 int i, j, k;
3118 int n_operands;
3119 int n_alternatives;
3120 int commutative;
3121 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3122 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3123 rtx before, after;
3124 bool alt_p = false;
3125 /* Flag that the insn has been changed through a transformation. */
3126 bool change_p;
3127 bool sec_mem_p;
3128 #ifdef SECONDARY_MEMORY_NEEDED
3129 bool use_sec_mem_p;
3130 #endif
3131 int max_regno_before;
3132 int reused_alternative_num;
3134 curr_insn_set = single_set (curr_insn);
3135 if (curr_insn_set != NULL_RTX && simple_move_p ())
3136 return false;
3138 no_input_reloads_p = no_output_reloads_p = false;
3139 goal_alt_number = -1;
3140 change_p = sec_mem_p = false;
3141 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3142 reloads; neither are insns that SET cc0. Insns that use CC0 are
3143 not allowed to have any input reloads. */
3144 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3145 no_output_reloads_p = true;
3147 #ifdef HAVE_cc0
3148 if (reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3149 no_input_reloads_p = true;
3150 if (reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3151 no_output_reloads_p = true;
3152 #endif
3154 n_operands = curr_static_id->n_operands;
3155 n_alternatives = curr_static_id->n_alternatives;
3157 /* Just return "no reloads" if insn has no operands with
3158 constraints. */
3159 if (n_operands == 0 || n_alternatives == 0)
3160 return false;
3162 max_regno_before = max_reg_num ();
3164 for (i = 0; i < n_operands; i++)
3166 goal_alt_matched[i][0] = -1;
3167 goal_alt_matches[i] = -1;
3170 commutative = curr_static_id->commutative;
3172 /* Now see what we need for pseudos that didn't get hard regs or got
3173 the wrong kind of hard reg. For this, we must consider all the
3174 operands together against the register constraints. */
3176 best_losers = best_overall = INT_MAX;
3177 best_reload_sum = 0;
3179 curr_swapped = false;
3180 goal_alt_swapped = false;
3182 /* Make equivalence substitution and memory subreg elimination
3183 before address processing because an address legitimacy can
3184 depend on memory mode. */
3185 for (i = 0; i < n_operands; i++)
3187 rtx op = *curr_id->operand_loc[i];
3188 rtx subst, old = op;
3189 bool op_change_p = false;
3191 if (GET_CODE (old) == SUBREG)
3192 old = SUBREG_REG (old);
3193 subst = get_equiv_with_elimination (old, curr_insn);
3194 if (subst != old)
3196 subst = copy_rtx (subst);
3197 lra_assert (REG_P (old));
3198 if (GET_CODE (op) == SUBREG)
3199 SUBREG_REG (op) = subst;
3200 else
3201 *curr_id->operand_loc[i] = subst;
3202 if (lra_dump_file != NULL)
3204 fprintf (lra_dump_file,
3205 "Changing pseudo %d in operand %i of insn %u on equiv ",
3206 REGNO (old), i, INSN_UID (curr_insn));
3207 dump_value_slim (lra_dump_file, subst, 1);
3208 fprintf (lra_dump_file, "\n");
3210 op_change_p = change_p = true;
3212 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3214 change_p = true;
3215 lra_update_dup (curr_id, i);
3219 /* Reload address registers and displacements. We do it before
3220 finding an alternative because of memory constraints. */
3221 before = after = NULL_RTX;
3222 for (i = 0; i < n_operands; i++)
3223 if (! curr_static_id->operand[i].is_operator
3224 && process_address (i, &before, &after))
3226 change_p = true;
3227 lra_update_dup (curr_id, i);
3230 if (change_p)
3231 /* If we've changed the instruction then any alternative that
3232 we chose previously may no longer be valid. */
3233 lra_set_used_insn_alternative (curr_insn, -1);
3235 if (curr_insn_set != NULL_RTX
3236 && check_and_process_move (&change_p, &sec_mem_p))
3237 return change_p;
3239 try_swapped:
3241 reused_alternative_num = curr_id->used_insn_alternative;
3242 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3243 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3244 reused_alternative_num, INSN_UID (curr_insn));
3246 if (process_alt_operands (reused_alternative_num))
3247 alt_p = true;
3249 /* If insn is commutative (it's safe to exchange a certain pair of
3250 operands) then we need to try each alternative twice, the second
3251 time matching those two operands as if we had exchanged them. To
3252 do this, really exchange them in operands.
3254 If we have just tried the alternatives the second time, return
3255 operands to normal and drop through. */
3257 if (reused_alternative_num < 0 && commutative >= 0)
3259 curr_swapped = !curr_swapped;
3260 if (curr_swapped)
3262 swap_operands (commutative);
3263 goto try_swapped;
3265 else
3266 swap_operands (commutative);
3269 if (! alt_p && ! sec_mem_p)
3271 /* No alternative works with reloads?? */
3272 if (INSN_CODE (curr_insn) >= 0)
3273 fatal_insn ("unable to generate reloads for:", curr_insn);
3274 error_for_asm (curr_insn,
3275 "inconsistent operand constraints in an %<asm%>");
3276 /* Avoid further trouble with this insn. */
3277 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3278 lra_invalidate_insn_data (curr_insn);
3279 return true;
3282 /* If the best alternative is with operands 1 and 2 swapped, swap
3283 them. Update the operand numbers of any reloads already
3284 pushed. */
3286 if (goal_alt_swapped)
3288 if (lra_dump_file != NULL)
3289 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3290 INSN_UID (curr_insn));
3292 /* Swap the duplicates too. */
3293 swap_operands (commutative);
3294 change_p = true;
3297 #ifdef SECONDARY_MEMORY_NEEDED
3298 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3299 too conservatively. So we use the secondary memory only if there
3300 is no any alternative without reloads. */
3301 use_sec_mem_p = false;
3302 if (! alt_p)
3303 use_sec_mem_p = true;
3304 else if (sec_mem_p)
3306 for (i = 0; i < n_operands; i++)
3307 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3308 break;
3309 use_sec_mem_p = i < n_operands;
3312 if (use_sec_mem_p)
3314 rtx new_reg, src, dest, rld;
3315 enum machine_mode sec_mode, rld_mode;
3317 lra_assert (sec_mem_p);
3318 lra_assert (curr_static_id->operand[0].type == OP_OUT
3319 && curr_static_id->operand[1].type == OP_IN);
3320 dest = *curr_id->operand_loc[0];
3321 src = *curr_id->operand_loc[1];
3322 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3323 ? dest : src);
3324 rld_mode = GET_MODE (rld);
3325 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3326 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3327 #else
3328 sec_mode = rld_mode;
3329 #endif
3330 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3331 NO_REGS, "secondary");
3332 /* If the mode is changed, it should be wider. */
3333 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3334 if (sec_mode != rld_mode)
3336 /* If the target says specifically to use another mode for
3337 secondary memory moves we can not reuse the original
3338 insn. */
3339 after = emit_spill_move (false, new_reg, dest);
3340 lra_process_new_insns (curr_insn, NULL_RTX, after,
3341 "Inserting the sec. move");
3342 /* We may have non null BEFORE here (e.g. after address
3343 processing. */
3344 push_to_sequence (before);
3345 before = emit_spill_move (true, new_reg, src);
3346 emit_insn (before);
3347 before = get_insns ();
3348 end_sequence ();
3349 lra_process_new_insns (curr_insn, before, NULL_RTX, "Changing on");
3350 lra_set_insn_deleted (curr_insn);
3352 else if (dest == rld)
3354 *curr_id->operand_loc[0] = new_reg;
3355 after = emit_spill_move (false, new_reg, dest);
3356 lra_process_new_insns (curr_insn, NULL_RTX, after,
3357 "Inserting the sec. move");
3359 else
3361 *curr_id->operand_loc[1] = new_reg;
3362 /* See comments above. */
3363 push_to_sequence (before);
3364 before = emit_spill_move (true, new_reg, src);
3365 emit_insn (before);
3366 before = get_insns ();
3367 end_sequence ();
3368 lra_process_new_insns (curr_insn, before, NULL_RTX,
3369 "Inserting the sec. move");
3371 lra_update_insn_regno_info (curr_insn);
3372 return true;
3374 #endif
3376 lra_assert (goal_alt_number >= 0);
3377 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3379 if (lra_dump_file != NULL)
3381 const char *p;
3383 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3384 goal_alt_number, INSN_UID (curr_insn));
3385 for (i = 0; i < n_operands; i++)
3387 p = (curr_static_id->operand_alternative
3388 [goal_alt_number * n_operands + i].constraint);
3389 if (*p == '\0')
3390 continue;
3391 fprintf (lra_dump_file, " (%d) ", i);
3392 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3393 fputc (*p, lra_dump_file);
3395 if (INSN_CODE (curr_insn) >= 0
3396 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3397 fprintf (lra_dump_file, " {%s}", p);
3398 if (curr_id->sp_offset != 0)
3399 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3400 curr_id->sp_offset);
3401 fprintf (lra_dump_file, "\n");
3404 /* Right now, for any pair of operands I and J that are required to
3405 match, with J < I, goal_alt_matches[I] is J. Add I to
3406 goal_alt_matched[J]. */
3408 for (i = 0; i < n_operands; i++)
3409 if ((j = goal_alt_matches[i]) >= 0)
3411 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3413 /* We allow matching one output operand and several input
3414 operands. */
3415 lra_assert (k == 0
3416 || (curr_static_id->operand[j].type == OP_OUT
3417 && curr_static_id->operand[i].type == OP_IN
3418 && (curr_static_id->operand
3419 [goal_alt_matched[j][0]].type == OP_IN)));
3420 goal_alt_matched[j][k] = i;
3421 goal_alt_matched[j][k + 1] = -1;
3424 for (i = 0; i < n_operands; i++)
3425 goal_alt_win[i] |= goal_alt_match_win[i];
3427 /* Any constants that aren't allowed and can't be reloaded into
3428 registers are here changed into memory references. */
3429 for (i = 0; i < n_operands; i++)
3430 if (goal_alt_win[i])
3432 int regno;
3433 enum reg_class new_class;
3434 rtx reg = *curr_id->operand_loc[i];
3436 if (GET_CODE (reg) == SUBREG)
3437 reg = SUBREG_REG (reg);
3439 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3441 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3443 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3445 lra_assert (ok_p);
3446 lra_change_class (regno, new_class, " Change to", true);
3450 else
3452 const char *constraint;
3453 char c;
3454 rtx op = *curr_id->operand_loc[i];
3455 rtx subreg = NULL_RTX;
3456 enum machine_mode mode = curr_operand_mode[i];
3458 if (GET_CODE (op) == SUBREG)
3460 subreg = op;
3461 op = SUBREG_REG (op);
3462 mode = GET_MODE (op);
3465 if (CONST_POOL_OK_P (mode, op)
3466 && ((targetm.preferred_reload_class
3467 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3468 || no_input_reloads_p))
3470 rtx tem = force_const_mem (mode, op);
3472 change_p = true;
3473 if (subreg != NULL_RTX)
3474 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3476 *curr_id->operand_loc[i] = tem;
3477 lra_update_dup (curr_id, i);
3478 process_address (i, &before, &after);
3480 /* If the alternative accepts constant pool refs directly
3481 there will be no reload needed at all. */
3482 if (subreg != NULL_RTX)
3483 continue;
3484 /* Skip alternatives before the one requested. */
3485 constraint = (curr_static_id->operand_alternative
3486 [goal_alt_number * n_operands + i].constraint);
3487 for (;
3488 (c = *constraint) && c != ',' && c != '#';
3489 constraint += CONSTRAINT_LEN (c, constraint))
3491 if (c == TARGET_MEM_CONSTRAINT || c == 'o')
3492 break;
3493 #ifdef EXTRA_CONSTRAINT_STR
3494 if (EXTRA_MEMORY_CONSTRAINT (c, constraint)
3495 && EXTRA_CONSTRAINT_STR (tem, c, constraint))
3496 break;
3497 #endif
3499 if (c == '\0' || c == ',' || c == '#')
3500 continue;
3502 goal_alt_win[i] = true;
3506 for (i = 0; i < n_operands; i++)
3508 int regno;
3509 bool optional_p = false;
3510 rtx old, new_reg;
3511 rtx op = *curr_id->operand_loc[i];
3513 if (goal_alt_win[i])
3515 if (goal_alt[i] == NO_REGS
3516 && REG_P (op)
3517 /* When we assign NO_REGS it means that we will not
3518 assign a hard register to the scratch pseudo by
3519 assigment pass and the scratch pseudo will be
3520 spilled. Spilled scratch pseudos are transformed
3521 back to scratches at the LRA end. */
3522 && lra_former_scratch_operand_p (curr_insn, i))
3524 int regno = REGNO (op);
3525 lra_change_class (regno, NO_REGS, " Change to", true);
3526 if (lra_get_regno_hard_regno (regno) >= 0)
3527 /* We don't have to mark all insn affected by the
3528 spilled pseudo as there is only one such insn, the
3529 current one. */
3530 reg_renumber[regno] = -1;
3532 /* We can do an optional reload. If the pseudo got a hard
3533 reg, we might improve the code through inheritance. If
3534 it does not get a hard register we coalesce memory/memory
3535 moves later. Ignore move insns to avoid cycling. */
3536 if (! lra_simple_p
3537 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3538 && goal_alt[i] != NO_REGS && REG_P (op)
3539 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3540 && regno < new_regno_start
3541 && ! lra_former_scratch_p (regno)
3542 && reg_renumber[regno] < 0
3543 && (curr_insn_set == NULL_RTX
3544 || !((REG_P (SET_SRC (curr_insn_set))
3545 || MEM_P (SET_SRC (curr_insn_set))
3546 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3547 && (REG_P (SET_DEST (curr_insn_set))
3548 || MEM_P (SET_DEST (curr_insn_set))
3549 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3550 optional_p = true;
3551 else
3552 continue;
3555 /* Operands that match previous ones have already been handled. */
3556 if (goal_alt_matches[i] >= 0)
3557 continue;
3559 /* We should not have an operand with a non-offsettable address
3560 appearing where an offsettable address will do. It also may
3561 be a case when the address should be special in other words
3562 not a general one (e.g. it needs no index reg). */
3563 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3565 enum reg_class rclass;
3566 rtx *loc = &XEXP (op, 0);
3567 enum rtx_code code = GET_CODE (*loc);
3569 push_to_sequence (before);
3570 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3571 MEM, SCRATCH);
3572 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3573 new_reg = emit_inc (rclass, *loc, *loc,
3574 /* This value does not matter for MODIFY. */
3575 GET_MODE_SIZE (GET_MODE (op)));
3576 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass,
3577 "offsetable address", &new_reg))
3578 lra_emit_move (new_reg, *loc);
3579 before = get_insns ();
3580 end_sequence ();
3581 *loc = new_reg;
3582 lra_update_dup (curr_id, i);
3584 else if (goal_alt_matched[i][0] == -1)
3586 enum machine_mode mode;
3587 rtx reg, *loc;
3588 int hard_regno, byte;
3589 enum op_type type = curr_static_id->operand[i].type;
3591 loc = curr_id->operand_loc[i];
3592 mode = curr_operand_mode[i];
3593 if (GET_CODE (*loc) == SUBREG)
3595 reg = SUBREG_REG (*loc);
3596 byte = SUBREG_BYTE (*loc);
3597 if (REG_P (reg)
3598 /* Strict_low_part requires reload the register not
3599 the sub-register. */
3600 && (curr_static_id->operand[i].strict_low
3601 || (GET_MODE_SIZE (mode)
3602 <= GET_MODE_SIZE (GET_MODE (reg))
3603 && (hard_regno
3604 = get_try_hard_regno (REGNO (reg))) >= 0
3605 && (simplify_subreg_regno
3606 (hard_regno,
3607 GET_MODE (reg), byte, mode) < 0)
3608 && (goal_alt[i] == NO_REGS
3609 || (simplify_subreg_regno
3610 (ira_class_hard_regs[goal_alt[i]][0],
3611 GET_MODE (reg), byte, mode) >= 0)))))
3613 loc = &SUBREG_REG (*loc);
3614 mode = GET_MODE (*loc);
3617 old = *loc;
3618 if (get_reload_reg (type, mode, old, goal_alt[i], "", &new_reg)
3619 && type != OP_OUT)
3621 push_to_sequence (before);
3622 lra_emit_move (new_reg, old);
3623 before = get_insns ();
3624 end_sequence ();
3626 *loc = new_reg;
3627 if (type != OP_IN
3628 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3630 start_sequence ();
3631 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3632 emit_insn (after);
3633 after = get_insns ();
3634 end_sequence ();
3635 *loc = new_reg;
3637 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3638 if (goal_alt_dont_inherit_ops[j] == i)
3640 lra_set_regno_unique_value (REGNO (new_reg));
3641 break;
3643 lra_update_dup (curr_id, i);
3645 else if (curr_static_id->operand[i].type == OP_IN
3646 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3647 == OP_OUT))
3649 /* generate reloads for input and matched outputs. */
3650 match_inputs[0] = i;
3651 match_inputs[1] = -1;
3652 match_reload (goal_alt_matched[i][0], match_inputs,
3653 goal_alt[i], &before, &after);
3655 else if (curr_static_id->operand[i].type == OP_OUT
3656 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3657 == OP_IN))
3658 /* Generate reloads for output and matched inputs. */
3659 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3660 else if (curr_static_id->operand[i].type == OP_IN
3661 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3662 == OP_IN))
3664 /* Generate reloads for matched inputs. */
3665 match_inputs[0] = i;
3666 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3667 match_inputs[j + 1] = k;
3668 match_inputs[j + 1] = -1;
3669 match_reload (-1, match_inputs, goal_alt[i], &before, &after);
3671 else
3672 /* We must generate code in any case when function
3673 process_alt_operands decides that it is possible. */
3674 gcc_unreachable ();
3675 if (optional_p)
3677 lra_assert (REG_P (op));
3678 regno = REGNO (op);
3679 op = *curr_id->operand_loc[i]; /* Substitution. */
3680 if (GET_CODE (op) == SUBREG)
3681 op = SUBREG_REG (op);
3682 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3683 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3684 lra_reg_info[REGNO (op)].restore_regno = regno;
3685 if (lra_dump_file != NULL)
3686 fprintf (lra_dump_file,
3687 " Making reload reg %d for reg %d optional\n",
3688 REGNO (op), regno);
3691 if (before != NULL_RTX || after != NULL_RTX
3692 || max_regno_before != max_reg_num ())
3693 change_p = true;
3694 if (change_p)
3696 lra_update_operator_dups (curr_id);
3697 /* Something changes -- process the insn. */
3698 lra_update_insn_regno_info (curr_insn);
3700 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3701 return change_p;
3704 /* Return true if X is in LIST. */
3705 static bool
3706 in_list_p (rtx x, rtx list)
3708 for (; list != NULL_RTX; list = XEXP (list, 1))
3709 if (XEXP (list, 0) == x)
3710 return true;
3711 return false;
3714 /* Return true if X contains an allocatable hard register (if
3715 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3716 static bool
3717 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3719 int i, j;
3720 const char *fmt;
3721 enum rtx_code code;
3723 code = GET_CODE (x);
3724 if (REG_P (x))
3726 int regno = REGNO (x);
3727 HARD_REG_SET alloc_regs;
3729 if (hard_reg_p)
3731 if (regno >= FIRST_PSEUDO_REGISTER)
3732 regno = lra_get_regno_hard_regno (regno);
3733 if (regno < 0)
3734 return false;
3735 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3736 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3738 else
3740 if (regno < FIRST_PSEUDO_REGISTER)
3741 return false;
3742 if (! spilled_p)
3743 return true;
3744 return lra_get_regno_hard_regno (regno) < 0;
3747 fmt = GET_RTX_FORMAT (code);
3748 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3750 if (fmt[i] == 'e')
3752 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3753 return true;
3755 else if (fmt[i] == 'E')
3757 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3758 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3759 return true;
3762 return false;
3765 /* Process all regs in location *LOC and change them on equivalent
3766 substitution. Return true if any change was done. */
3767 static bool
3768 loc_equivalence_change_p (rtx *loc)
3770 rtx subst, reg, x = *loc;
3771 bool result = false;
3772 enum rtx_code code = GET_CODE (x);
3773 const char *fmt;
3774 int i, j;
3776 if (code == SUBREG)
3778 reg = SUBREG_REG (x);
3779 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
3780 && GET_MODE (subst) == VOIDmode)
3782 /* We cannot reload debug location. Simplify subreg here
3783 while we know the inner mode. */
3784 *loc = simplify_gen_subreg (GET_MODE (x), subst,
3785 GET_MODE (reg), SUBREG_BYTE (x));
3786 return true;
3789 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
3791 *loc = subst;
3792 return true;
3795 /* Scan all the operand sub-expressions. */
3796 fmt = GET_RTX_FORMAT (code);
3797 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3799 if (fmt[i] == 'e')
3800 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
3801 else if (fmt[i] == 'E')
3802 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3803 result
3804 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
3806 return result;
3809 /* Similar to loc_equivalence_change_p, but for use as
3810 simplify_replace_fn_rtx callback. DATA is insn for which the
3811 elimination is done. If it null we don't do the elimination. */
3812 static rtx
3813 loc_equivalence_callback (rtx loc, const_rtx, void *data)
3815 if (!REG_P (loc))
3816 return NULL_RTX;
3818 rtx subst = (data == NULL
3819 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx) data));
3820 if (subst != loc)
3821 return subst;
3823 return NULL_RTX;
3826 /* Maximum number of generated reload insns per an insn. It is for
3827 preventing this pass cycling in a bug case. */
3828 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
3830 /* The current iteration number of this LRA pass. */
3831 int lra_constraint_iter;
3833 /* The current iteration number of this LRA pass after the last spill
3834 pass. */
3835 int lra_constraint_iter_after_spill;
3837 /* True if we substituted equiv which needs checking register
3838 allocation correctness because the equivalent value contains
3839 allocatable hard registers or when we restore multi-register
3840 pseudo. */
3841 bool lra_risky_transformations_p;
3843 /* Return true if REGNO is referenced in more than one block. */
3844 static bool
3845 multi_block_pseudo_p (int regno)
3847 basic_block bb = NULL;
3848 unsigned int uid;
3849 bitmap_iterator bi;
3851 if (regno < FIRST_PSEUDO_REGISTER)
3852 return false;
3854 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
3855 if (bb == NULL)
3856 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
3857 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
3858 return true;
3859 return false;
3862 /* Return true if LIST contains a deleted insn. */
3863 static bool
3864 contains_deleted_insn_p (rtx list)
3866 for (; list != NULL_RTX; list = XEXP (list, 1))
3867 if (NOTE_P (XEXP (list, 0))
3868 && NOTE_KIND (XEXP (list, 0)) == NOTE_INSN_DELETED)
3869 return true;
3870 return false;
3873 /* Return true if X contains a pseudo dying in INSN. */
3874 static bool
3875 dead_pseudo_p (rtx x, rtx insn)
3877 int i, j;
3878 const char *fmt;
3879 enum rtx_code code;
3881 if (REG_P (x))
3882 return (insn != NULL_RTX
3883 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
3884 code = GET_CODE (x);
3885 fmt = GET_RTX_FORMAT (code);
3886 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3888 if (fmt[i] == 'e')
3890 if (dead_pseudo_p (XEXP (x, i), insn))
3891 return true;
3893 else if (fmt[i] == 'E')
3895 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3896 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
3897 return true;
3900 return false;
3903 /* Return true if INSN contains a dying pseudo in INSN right hand
3904 side. */
3905 static bool
3906 insn_rhs_dead_pseudo_p (rtx insn)
3908 rtx set = single_set (insn);
3910 gcc_assert (set != NULL);
3911 return dead_pseudo_p (SET_SRC (set), insn);
3914 /* Return true if any init insn of REGNO contains a dying pseudo in
3915 insn right hand side. */
3916 static bool
3917 init_insn_rhs_dead_pseudo_p (int regno)
3919 rtx insns = ira_reg_equiv[regno].init_insns;
3921 if (insns == NULL)
3922 return false;
3923 if (INSN_P (insns))
3924 return insn_rhs_dead_pseudo_p (insns);
3925 for (; insns != NULL_RTX; insns = XEXP (insns, 1))
3926 if (insn_rhs_dead_pseudo_p (XEXP (insns, 0)))
3927 return true;
3928 return false;
3931 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
3932 reverse only if we have one init insn with given REGNO as a
3933 source. */
3934 static bool
3935 reverse_equiv_p (int regno)
3937 rtx insns, set;
3939 if ((insns = ira_reg_equiv[regno].init_insns) == NULL_RTX)
3940 return false;
3941 if (! INSN_P (XEXP (insns, 0))
3942 || XEXP (insns, 1) != NULL_RTX)
3943 return false;
3944 if ((set = single_set (XEXP (insns, 0))) == NULL_RTX)
3945 return false;
3946 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
3949 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
3950 call this function only for non-reverse equivalence. */
3951 static bool
3952 contains_reloaded_insn_p (int regno)
3954 rtx set;
3955 rtx list = ira_reg_equiv[regno].init_insns;
3957 for (; list != NULL_RTX; list = XEXP (list, 1))
3958 if ((set = single_set (XEXP (list, 0))) == NULL_RTX
3959 || ! REG_P (SET_DEST (set))
3960 || (int) REGNO (SET_DEST (set)) != regno)
3961 return true;
3962 return false;
3965 /* Entry function of LRA constraint pass. Return true if the
3966 constraint pass did change the code. */
3967 bool
3968 lra_constraints (bool first_p)
3970 bool changed_p;
3971 int i, hard_regno, new_insns_num;
3972 unsigned int min_len, new_min_len, uid;
3973 rtx set, x, reg, dest_reg;
3974 basic_block last_bb;
3975 bitmap_head equiv_insn_bitmap;
3976 bitmap_iterator bi;
3978 lra_constraint_iter++;
3979 if (lra_dump_file != NULL)
3980 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
3981 lra_constraint_iter);
3982 lra_constraint_iter_after_spill++;
3983 if (lra_constraint_iter_after_spill > LRA_MAX_CONSTRAINT_ITERATION_NUMBER)
3984 internal_error
3985 ("Maximum number of LRA constraint passes is achieved (%d)\n",
3986 LRA_MAX_CONSTRAINT_ITERATION_NUMBER);
3987 changed_p = false;
3988 lra_risky_transformations_p = false;
3989 new_insn_uid_start = get_max_uid ();
3990 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
3991 /* Mark used hard regs for target stack size calulations. */
3992 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
3993 if (lra_reg_info[i].nrefs != 0
3994 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
3996 int j, nregs;
3998 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
3999 for (j = 0; j < nregs; j++)
4000 df_set_regs_ever_live (hard_regno + j, true);
4002 /* Do elimination before the equivalence processing as we can spill
4003 some pseudos during elimination. */
4004 lra_eliminate (false, first_p);
4005 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4006 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4007 if (lra_reg_info[i].nrefs != 0)
4009 ira_reg_equiv[i].profitable_p = true;
4010 reg = regno_reg_rtx[i];
4011 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4013 bool pseudo_p = contains_reg_p (x, false, false);
4015 /* After RTL transformation, we can not guarantee that
4016 pseudo in the substitution was not reloaded which might
4017 make equivalence invalid. For example, in reverse
4018 equiv of p0
4020 p0 <- ...
4022 equiv_mem <- p0
4024 the memory address register was reloaded before the 2nd
4025 insn. */
4026 if ((! first_p && pseudo_p)
4027 /* We don't use DF for compilation speed sake. So it
4028 is problematic to update live info when we use an
4029 equivalence containing pseudos in more than one
4030 BB. */
4031 || (pseudo_p && multi_block_pseudo_p (i))
4032 /* If an init insn was deleted for some reason, cancel
4033 the equiv. We could update the equiv insns after
4034 transformations including an equiv insn deletion
4035 but it is not worthy as such cases are extremely
4036 rare. */
4037 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4038 /* If it is not a reverse equivalence, we check that a
4039 pseudo in rhs of the init insn is not dying in the
4040 insn. Otherwise, the live info at the beginning of
4041 the corresponding BB might be wrong after we
4042 removed the insn. When the equiv can be a
4043 constant, the right hand side of the init insn can
4044 be a pseudo. */
4045 || (! reverse_equiv_p (i)
4046 && (init_insn_rhs_dead_pseudo_p (i)
4047 /* If we reloaded the pseudo in an equivalence
4048 init insn, we can not remove the equiv init
4049 insns and the init insns might write into
4050 const memory in this case. */
4051 || contains_reloaded_insn_p (i)))
4052 /* Prevent access beyond equivalent memory for
4053 paradoxical subregs. */
4054 || (MEM_P (x)
4055 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4056 > GET_MODE_SIZE (GET_MODE (x)))))
4057 ira_reg_equiv[i].defined_p = false;
4058 if (contains_reg_p (x, false, true))
4059 ira_reg_equiv[i].profitable_p = false;
4060 if (get_equiv (reg) != reg)
4061 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4064 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4065 update_equiv (i);
4066 /* We should add all insns containing pseudos which should be
4067 substituted by their equivalences. */
4068 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4069 lra_push_insn_by_uid (uid);
4070 min_len = lra_insn_stack_length ();
4071 new_insns_num = 0;
4072 last_bb = NULL;
4073 changed_p = false;
4074 while ((new_min_len = lra_insn_stack_length ()) != 0)
4076 curr_insn = lra_pop_insn ();
4077 --new_min_len;
4078 curr_bb = BLOCK_FOR_INSN (curr_insn);
4079 if (curr_bb != last_bb)
4081 last_bb = curr_bb;
4082 bb_reload_num = lra_curr_reload_num;
4084 if (min_len > new_min_len)
4086 min_len = new_min_len;
4087 new_insns_num = 0;
4089 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4090 internal_error
4091 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4092 MAX_RELOAD_INSNS_NUMBER);
4093 new_insns_num++;
4094 if (DEBUG_INSN_P (curr_insn))
4096 /* We need to check equivalence in debug insn and change
4097 pseudo to the equivalent value if necessary. */
4098 curr_id = lra_get_insn_recog_data (curr_insn);
4099 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4101 rtx old = *curr_id->operand_loc[0];
4102 *curr_id->operand_loc[0]
4103 = simplify_replace_fn_rtx (old, NULL_RTX,
4104 loc_equivalence_callback, curr_insn);
4105 if (old != *curr_id->operand_loc[0])
4107 lra_update_insn_regno_info (curr_insn);
4108 changed_p = true;
4112 else if (INSN_P (curr_insn))
4114 if ((set = single_set (curr_insn)) != NULL_RTX)
4116 dest_reg = SET_DEST (set);
4117 /* The equivalence pseudo could be set up as SUBREG in a
4118 case when it is a call restore insn in a mode
4119 different from the pseudo mode. */
4120 if (GET_CODE (dest_reg) == SUBREG)
4121 dest_reg = SUBREG_REG (dest_reg);
4122 if ((REG_P (dest_reg)
4123 && (x = get_equiv (dest_reg)) != dest_reg
4124 /* Remove insns which set up a pseudo whose value
4125 can not be changed. Such insns might be not in
4126 init_insns because we don't update equiv data
4127 during insn transformations.
4129 As an example, let suppose that a pseudo got
4130 hard register and on the 1st pass was not
4131 changed to equivalent constant. We generate an
4132 additional insn setting up the pseudo because of
4133 secondary memory movement. Then the pseudo is
4134 spilled and we use the equiv constant. In this
4135 case we should remove the additional insn and
4136 this insn is not init_insns list. */
4137 && (! MEM_P (x) || MEM_READONLY_P (x)
4138 /* Check that this is actually an insn setting
4139 up the equivalence. */
4140 || in_list_p (curr_insn,
4141 ira_reg_equiv
4142 [REGNO (dest_reg)].init_insns)))
4143 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4144 && in_list_p (curr_insn,
4145 ira_reg_equiv
4146 [REGNO (SET_SRC (set))].init_insns)))
4148 /* This is equiv init insn of pseudo which did not get a
4149 hard register -- remove the insn. */
4150 if (lra_dump_file != NULL)
4152 fprintf (lra_dump_file,
4153 " Removing equiv init insn %i (freq=%d)\n",
4154 INSN_UID (curr_insn),
4155 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4156 dump_insn_slim (lra_dump_file, curr_insn);
4158 if (contains_reg_p (x, true, false))
4159 lra_risky_transformations_p = true;
4160 lra_set_insn_deleted (curr_insn);
4161 continue;
4164 curr_id = lra_get_insn_recog_data (curr_insn);
4165 curr_static_id = curr_id->insn_static_data;
4166 init_curr_insn_input_reloads ();
4167 init_curr_operand_mode ();
4168 if (curr_insn_transform ())
4169 changed_p = true;
4170 /* Check non-transformed insns too for equiv change as USE
4171 or CLOBBER don't need reloads but can contain pseudos
4172 being changed on their equivalences. */
4173 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4174 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4176 lra_update_insn_regno_info (curr_insn);
4177 changed_p = true;
4181 bitmap_clear (&equiv_insn_bitmap);
4182 /* If we used a new hard regno, changed_p should be true because the
4183 hard reg is assigned to a new pseudo. */
4184 #ifdef ENABLE_CHECKING
4185 if (! changed_p)
4187 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4188 if (lra_reg_info[i].nrefs != 0
4189 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4191 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4193 for (j = 0; j < nregs; j++)
4194 lra_assert (df_regs_ever_live_p (hard_regno + j));
4197 #endif
4198 return changed_p;
4201 /* Initiate the LRA constraint pass. It is done once per
4202 function. */
4203 void
4204 lra_constraints_init (void)
4208 /* Finalize the LRA constraint pass. It is done once per
4209 function. */
4210 void
4211 lra_constraints_finish (void)
4217 /* This page contains code to do inheritance/split
4218 transformations. */
4220 /* Number of reloads passed so far in current EBB. */
4221 static int reloads_num;
4223 /* Number of calls passed so far in current EBB. */
4224 static int calls_num;
4226 /* Current reload pseudo check for validity of elements in
4227 USAGE_INSNS. */
4228 static int curr_usage_insns_check;
4230 /* Info about last usage of registers in EBB to do inheritance/split
4231 transformation. Inheritance transformation is done from a spilled
4232 pseudo and split transformations from a hard register or a pseudo
4233 assigned to a hard register. */
4234 struct usage_insns
4236 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4237 value INSNS is valid. The insns is chain of optional debug insns
4238 and a finishing non-debug insn using the corresponding reg. The
4239 value is also used to mark the registers which are set up in the
4240 current insn. The negated insn uid is used for this. */
4241 int check;
4242 /* Value of global reloads_num at the last insn in INSNS. */
4243 int reloads_num;
4244 /* Value of global reloads_nums at the last insn in INSNS. */
4245 int calls_num;
4246 /* It can be true only for splitting. And it means that the restore
4247 insn should be put after insn given by the following member. */
4248 bool after_p;
4249 /* Next insns in the current EBB which use the original reg and the
4250 original reg value is not changed between the current insn and
4251 the next insns. In order words, e.g. for inheritance, if we need
4252 to use the original reg value again in the next insns we can try
4253 to use the value in a hard register from a reload insn of the
4254 current insn. */
4255 rtx insns;
4258 /* Map: regno -> corresponding pseudo usage insns. */
4259 static struct usage_insns *usage_insns;
4261 static void
4262 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4264 usage_insns[regno].check = curr_usage_insns_check;
4265 usage_insns[regno].insns = insn;
4266 usage_insns[regno].reloads_num = reloads_num;
4267 usage_insns[regno].calls_num = calls_num;
4268 usage_insns[regno].after_p = after_p;
4271 /* The function is used to form list REGNO usages which consists of
4272 optional debug insns finished by a non-debug insn using REGNO.
4273 RELOADS_NUM is current number of reload insns processed so far. */
4274 static void
4275 add_next_usage_insn (int regno, rtx insn, int reloads_num)
4277 rtx next_usage_insns;
4279 if (usage_insns[regno].check == curr_usage_insns_check
4280 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4281 && DEBUG_INSN_P (insn))
4283 /* Check that we did not add the debug insn yet. */
4284 if (next_usage_insns != insn
4285 && (GET_CODE (next_usage_insns) != INSN_LIST
4286 || XEXP (next_usage_insns, 0) != insn))
4287 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4288 next_usage_insns);
4290 else if (NONDEBUG_INSN_P (insn))
4291 setup_next_usage_insn (regno, insn, reloads_num, false);
4292 else
4293 usage_insns[regno].check = 0;
4296 /* Replace all references to register OLD_REGNO in *LOC with pseudo
4297 register NEW_REG. Return true if any change was made. */
4298 static bool
4299 substitute_pseudo (rtx *loc, int old_regno, rtx new_reg)
4301 rtx x = *loc;
4302 bool result = false;
4303 enum rtx_code code;
4304 const char *fmt;
4305 int i, j;
4307 if (x == NULL_RTX)
4308 return false;
4310 code = GET_CODE (x);
4311 if (code == REG && (int) REGNO (x) == old_regno)
4313 enum machine_mode mode = GET_MODE (*loc);
4314 enum machine_mode inner_mode = GET_MODE (new_reg);
4316 if (mode != inner_mode)
4318 if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
4319 || ! SCALAR_INT_MODE_P (inner_mode))
4320 new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
4321 else
4322 new_reg = gen_lowpart_SUBREG (mode, new_reg);
4324 *loc = new_reg;
4325 return true;
4328 /* Scan all the operand sub-expressions. */
4329 fmt = GET_RTX_FORMAT (code);
4330 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4332 if (fmt[i] == 'e')
4334 if (substitute_pseudo (&XEXP (x, i), old_regno, new_reg))
4335 result = true;
4337 else if (fmt[i] == 'E')
4339 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4340 if (substitute_pseudo (&XVECEXP (x, i, j), old_regno, new_reg))
4341 result = true;
4344 return result;
4347 /* Return first non-debug insn in list USAGE_INSNS. */
4348 static rtx
4349 skip_usage_debug_insns (rtx usage_insns)
4351 rtx insn;
4353 /* Skip debug insns. */
4354 for (insn = usage_insns;
4355 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4356 insn = XEXP (insn, 1))
4358 return insn;
4361 /* Return true if we need secondary memory moves for insn in
4362 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4363 into the insn. */
4364 static bool
4365 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4366 rtx usage_insns ATTRIBUTE_UNUSED)
4368 #ifndef SECONDARY_MEMORY_NEEDED
4369 return false;
4370 #else
4371 rtx insn, set, dest;
4372 enum reg_class cl;
4374 if (inher_cl == ALL_REGS
4375 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4376 return false;
4377 lra_assert (INSN_P (insn));
4378 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4379 return false;
4380 dest = SET_DEST (set);
4381 if (! REG_P (dest))
4382 return false;
4383 lra_assert (inher_cl != NO_REGS);
4384 cl = get_reg_class (REGNO (dest));
4385 return (cl != NO_REGS && cl != ALL_REGS
4386 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4387 #endif
4390 /* Registers involved in inheritance/split in the current EBB
4391 (inheritance/split pseudos and original registers). */
4392 static bitmap_head check_only_regs;
4394 /* Do inheritance transformations for insn INSN, which defines (if
4395 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4396 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4397 form as the "insns" field of usage_insns. Return true if we
4398 succeed in such transformation.
4400 The transformations look like:
4402 p <- ... i <- ...
4403 ... p <- i (new insn)
4404 ... =>
4405 <- ... p ... <- ... i ...
4407 ... i <- p (new insn)
4408 <- ... p ... <- ... i ...
4409 ... =>
4410 <- ... p ... <- ... i ...
4411 where p is a spilled original pseudo and i is a new inheritance pseudo.
4414 The inheritance pseudo has the smallest class of two classes CL and
4415 class of ORIGINAL REGNO. */
4416 static bool
4417 inherit_reload_reg (bool def_p, int original_regno,
4418 enum reg_class cl, rtx insn, rtx next_usage_insns)
4420 if (optimize_function_for_size_p (cfun))
4421 return false;
4423 enum reg_class rclass = lra_get_allocno_class (original_regno);
4424 rtx original_reg = regno_reg_rtx[original_regno];
4425 rtx new_reg, new_insns, usage_insn;
4427 lra_assert (! usage_insns[original_regno].after_p);
4428 if (lra_dump_file != NULL)
4429 fprintf (lra_dump_file,
4430 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4431 if (! ira_reg_classes_intersect_p[cl][rclass])
4433 if (lra_dump_file != NULL)
4435 fprintf (lra_dump_file,
4436 " Rejecting inheritance for %d "
4437 "because of disjoint classes %s and %s\n",
4438 original_regno, reg_class_names[cl],
4439 reg_class_names[rclass]);
4440 fprintf (lra_dump_file,
4441 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4443 return false;
4445 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4446 /* We don't use a subset of two classes because it can be
4447 NO_REGS. This transformation is still profitable in most
4448 cases even if the classes are not intersected as register
4449 move is probably cheaper than a memory load. */
4450 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4452 if (lra_dump_file != NULL)
4453 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4454 reg_class_names[cl], reg_class_names[rclass]);
4456 rclass = cl;
4458 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4460 /* Reject inheritance resulting in secondary memory moves.
4461 Otherwise, there is a danger in LRA cycling. Also such
4462 transformation will be unprofitable. */
4463 if (lra_dump_file != NULL)
4465 rtx insn = skip_usage_debug_insns (next_usage_insns);
4466 rtx set = single_set (insn);
4468 lra_assert (set != NULL_RTX);
4470 rtx dest = SET_DEST (set);
4472 lra_assert (REG_P (dest));
4473 fprintf (lra_dump_file,
4474 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4475 "as secondary mem is needed\n",
4476 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4477 original_regno, reg_class_names[rclass]);
4478 fprintf (lra_dump_file,
4479 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4481 return false;
4483 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4484 rclass, "inheritance");
4485 start_sequence ();
4486 if (def_p)
4487 lra_emit_move (original_reg, new_reg);
4488 else
4489 lra_emit_move (new_reg, original_reg);
4490 new_insns = get_insns ();
4491 end_sequence ();
4492 if (NEXT_INSN (new_insns) != NULL_RTX)
4494 if (lra_dump_file != NULL)
4496 fprintf (lra_dump_file,
4497 " Rejecting inheritance %d->%d "
4498 "as it results in 2 or more insns:\n",
4499 original_regno, REGNO (new_reg));
4500 dump_rtl_slim (lra_dump_file, new_insns, NULL_RTX, -1, 0);
4501 fprintf (lra_dump_file,
4502 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4504 return false;
4506 substitute_pseudo (&insn, original_regno, new_reg);
4507 lra_update_insn_regno_info (insn);
4508 if (! def_p)
4509 /* We now have a new usage insn for original regno. */
4510 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4511 if (lra_dump_file != NULL)
4512 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4513 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4514 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4515 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4516 bitmap_set_bit (&check_only_regs, original_regno);
4517 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4518 if (def_p)
4519 lra_process_new_insns (insn, NULL_RTX, new_insns,
4520 "Add original<-inheritance");
4521 else
4522 lra_process_new_insns (insn, new_insns, NULL_RTX,
4523 "Add inheritance<-original");
4524 while (next_usage_insns != NULL_RTX)
4526 if (GET_CODE (next_usage_insns) != INSN_LIST)
4528 usage_insn = next_usage_insns;
4529 lra_assert (NONDEBUG_INSN_P (usage_insn));
4530 next_usage_insns = NULL;
4532 else
4534 usage_insn = XEXP (next_usage_insns, 0);
4535 lra_assert (DEBUG_INSN_P (usage_insn));
4536 next_usage_insns = XEXP (next_usage_insns, 1);
4538 substitute_pseudo (&usage_insn, original_regno, new_reg);
4539 lra_update_insn_regno_info (usage_insn);
4540 if (lra_dump_file != NULL)
4542 fprintf (lra_dump_file,
4543 " Inheritance reuse change %d->%d (bb%d):\n",
4544 original_regno, REGNO (new_reg),
4545 BLOCK_FOR_INSN (usage_insn)->index);
4546 dump_insn_slim (lra_dump_file, usage_insn);
4549 if (lra_dump_file != NULL)
4550 fprintf (lra_dump_file,
4551 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4552 return true;
4555 /* Return true if we need a caller save/restore for pseudo REGNO which
4556 was assigned to a hard register. */
4557 static inline bool
4558 need_for_call_save_p (int regno)
4560 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4561 return (usage_insns[regno].calls_num < calls_num
4562 && (overlaps_hard_reg_set_p
4563 (call_used_reg_set,
4564 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4565 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4566 PSEUDO_REGNO_MODE (regno))));
4569 /* Global registers occurring in the current EBB. */
4570 static bitmap_head ebb_global_regs;
4572 /* Return true if we need a split for hard register REGNO or pseudo
4573 REGNO which was assigned to a hard register.
4574 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4575 used for reloads since the EBB end. It is an approximation of the
4576 used hard registers in the split range. The exact value would
4577 require expensive calculations. If we were aggressive with
4578 splitting because of the approximation, the split pseudo will save
4579 the same hard register assignment and will be removed in the undo
4580 pass. We still need the approximation because too aggressive
4581 splitting would result in too inaccurate cost calculation in the
4582 assignment pass because of too many generated moves which will be
4583 probably removed in the undo pass. */
4584 static inline bool
4585 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4587 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4589 lra_assert (hard_regno >= 0);
4590 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4591 /* Don't split eliminable hard registers, otherwise we can
4592 split hard registers like hard frame pointer, which
4593 lives on BB start/end according to DF-infrastructure,
4594 when there is a pseudo assigned to the register and
4595 living in the same BB. */
4596 && (regno >= FIRST_PSEUDO_REGISTER
4597 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4598 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4599 /* Don't split call clobbered hard regs living through
4600 calls, otherwise we might have a check problem in the
4601 assign sub-pass as in the most cases (exception is a
4602 situation when lra_risky_transformations_p value is
4603 true) the assign pass assumes that all pseudos living
4604 through calls are assigned to call saved hard regs. */
4605 && (regno >= FIRST_PSEUDO_REGISTER
4606 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4607 || usage_insns[regno].calls_num == calls_num)
4608 /* We need at least 2 reloads to make pseudo splitting
4609 profitable. We should provide hard regno splitting in
4610 any case to solve 1st insn scheduling problem when
4611 moving hard register definition up might result in
4612 impossibility to find hard register for reload pseudo of
4613 small register class. */
4614 && (usage_insns[regno].reloads_num
4615 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4616 && (regno < FIRST_PSEUDO_REGISTER
4617 /* For short living pseudos, spilling + inheritance can
4618 be considered a substitution for splitting.
4619 Therefore we do not splitting for local pseudos. It
4620 decreases also aggressiveness of splitting. The
4621 minimal number of references is chosen taking into
4622 account that for 2 references splitting has no sense
4623 as we can just spill the pseudo. */
4624 || (regno >= FIRST_PSEUDO_REGISTER
4625 && lra_reg_info[regno].nrefs > 3
4626 && bitmap_bit_p (&ebb_global_regs, regno))))
4627 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4630 /* Return class for the split pseudo created from original pseudo with
4631 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4632 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4633 results in no secondary memory movements. */
4634 static enum reg_class
4635 choose_split_class (enum reg_class allocno_class,
4636 int hard_regno ATTRIBUTE_UNUSED,
4637 enum machine_mode mode ATTRIBUTE_UNUSED)
4639 #ifndef SECONDARY_MEMORY_NEEDED
4640 return allocno_class;
4641 #else
4642 int i;
4643 enum reg_class cl, best_cl = NO_REGS;
4644 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4645 = REGNO_REG_CLASS (hard_regno);
4647 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4648 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4649 return allocno_class;
4650 for (i = 0;
4651 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4652 i++)
4653 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4654 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4655 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4656 && (best_cl == NO_REGS
4657 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4658 best_cl = cl;
4659 return best_cl;
4660 #endif
4663 /* Do split transformations for insn INSN, which defines or uses
4664 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4665 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4666 "insns" field of usage_insns.
4668 The transformations look like:
4670 p <- ... p <- ...
4671 ... s <- p (new insn -- save)
4672 ... =>
4673 ... p <- s (new insn -- restore)
4674 <- ... p ... <- ... p ...
4676 <- ... p ... <- ... p ...
4677 ... s <- p (new insn -- save)
4678 ... =>
4679 ... p <- s (new insn -- restore)
4680 <- ... p ... <- ... p ...
4682 where p is an original pseudo got a hard register or a hard
4683 register and s is a new split pseudo. The save is put before INSN
4684 if BEFORE_P is true. Return true if we succeed in such
4685 transformation. */
4686 static bool
4687 split_reg (bool before_p, int original_regno, rtx insn, rtx next_usage_insns)
4689 enum reg_class rclass;
4690 rtx original_reg;
4691 int hard_regno, nregs;
4692 rtx new_reg, save, restore, usage_insn;
4693 bool after_p;
4694 bool call_save_p;
4696 if (original_regno < FIRST_PSEUDO_REGISTER)
4698 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4699 hard_regno = original_regno;
4700 call_save_p = false;
4701 nregs = 1;
4703 else
4705 hard_regno = reg_renumber[original_regno];
4706 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4707 rclass = lra_get_allocno_class (original_regno);
4708 original_reg = regno_reg_rtx[original_regno];
4709 call_save_p = need_for_call_save_p (original_regno);
4711 original_reg = regno_reg_rtx[original_regno];
4712 lra_assert (hard_regno >= 0);
4713 if (lra_dump_file != NULL)
4714 fprintf (lra_dump_file,
4715 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4716 if (call_save_p)
4718 enum machine_mode mode = GET_MODE (original_reg);
4720 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4721 hard_regno_nregs[hard_regno][mode],
4722 mode);
4723 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4725 else
4727 rclass = choose_split_class (rclass, hard_regno,
4728 GET_MODE (original_reg));
4729 if (rclass == NO_REGS)
4731 if (lra_dump_file != NULL)
4733 fprintf (lra_dump_file,
4734 " Rejecting split of %d(%s): "
4735 "no good reg class for %d(%s)\n",
4736 original_regno,
4737 reg_class_names[lra_get_allocno_class (original_regno)],
4738 hard_regno,
4739 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4740 fprintf
4741 (lra_dump_file,
4742 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4744 return false;
4746 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4747 rclass, "split");
4748 reg_renumber[REGNO (new_reg)] = hard_regno;
4750 save = emit_spill_move (true, new_reg, original_reg);
4751 if (NEXT_INSN (save) != NULL_RTX)
4753 lra_assert (! call_save_p);
4754 if (lra_dump_file != NULL)
4756 fprintf
4757 (lra_dump_file,
4758 " Rejecting split %d->%d resulting in > 2 %s save insns:\n",
4759 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4760 dump_rtl_slim (lra_dump_file, save, NULL_RTX, -1, 0);
4761 fprintf (lra_dump_file,
4762 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4764 return false;
4766 restore = emit_spill_move (false, new_reg, original_reg);
4767 if (NEXT_INSN (restore) != NULL_RTX)
4769 lra_assert (! call_save_p);
4770 if (lra_dump_file != NULL)
4772 fprintf (lra_dump_file,
4773 " Rejecting split %d->%d "
4774 "resulting in > 2 %s restore insns:\n",
4775 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4776 dump_rtl_slim (lra_dump_file, restore, NULL_RTX, -1, 0);
4777 fprintf (lra_dump_file,
4778 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4780 return false;
4782 after_p = usage_insns[original_regno].after_p;
4783 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4784 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4785 bitmap_set_bit (&check_only_regs, original_regno);
4786 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
4787 for (;;)
4789 if (GET_CODE (next_usage_insns) != INSN_LIST)
4791 usage_insn = next_usage_insns;
4792 break;
4794 usage_insn = XEXP (next_usage_insns, 0);
4795 lra_assert (DEBUG_INSN_P (usage_insn));
4796 next_usage_insns = XEXP (next_usage_insns, 1);
4797 substitute_pseudo (&usage_insn, original_regno, new_reg);
4798 lra_update_insn_regno_info (usage_insn);
4799 if (lra_dump_file != NULL)
4801 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
4802 original_regno, REGNO (new_reg));
4803 dump_insn_slim (lra_dump_file, usage_insn);
4806 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
4807 lra_assert (usage_insn != insn || (after_p && before_p));
4808 lra_process_new_insns (usage_insn, after_p ? NULL_RTX : restore,
4809 after_p ? restore : NULL_RTX,
4810 call_save_p
4811 ? "Add reg<-save" : "Add reg<-split");
4812 lra_process_new_insns (insn, before_p ? save : NULL_RTX,
4813 before_p ? NULL_RTX : save,
4814 call_save_p
4815 ? "Add save<-reg" : "Add split<-reg");
4816 if (nregs > 1)
4817 /* If we are trying to split multi-register. We should check
4818 conflicts on the next assignment sub-pass. IRA can allocate on
4819 sub-register levels, LRA do this on pseudos level right now and
4820 this discrepancy may create allocation conflicts after
4821 splitting. */
4822 lra_risky_transformations_p = true;
4823 if (lra_dump_file != NULL)
4824 fprintf (lra_dump_file,
4825 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4826 return true;
4829 /* Recognize that we need a split transformation for insn INSN, which
4830 defines or uses REGNO in its insn biggest MODE (we use it only if
4831 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
4832 hard registers which might be used for reloads since the EBB end.
4833 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
4834 uid before starting INSN processing. Return true if we succeed in
4835 such transformation. */
4836 static bool
4837 split_if_necessary (int regno, enum machine_mode mode,
4838 HARD_REG_SET potential_reload_hard_regs,
4839 bool before_p, rtx insn, int max_uid)
4841 bool res = false;
4842 int i, nregs = 1;
4843 rtx next_usage_insns;
4845 if (regno < FIRST_PSEUDO_REGISTER)
4846 nregs = hard_regno_nregs[regno][mode];
4847 for (i = 0; i < nregs; i++)
4848 if (usage_insns[regno + i].check == curr_usage_insns_check
4849 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
4850 /* To avoid processing the register twice or more. */
4851 && ((GET_CODE (next_usage_insns) != INSN_LIST
4852 && INSN_UID (next_usage_insns) < max_uid)
4853 || (GET_CODE (next_usage_insns) == INSN_LIST
4854 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
4855 && need_for_split_p (potential_reload_hard_regs, regno + i)
4856 && split_reg (before_p, regno + i, insn, next_usage_insns))
4857 res = true;
4858 return res;
4861 /* Check only registers living at the current program point in the
4862 current EBB. */
4863 static bitmap_head live_regs;
4865 /* Update live info in EBB given by its HEAD and TAIL insns after
4866 inheritance/split transformation. The function removes dead moves
4867 too. */
4868 static void
4869 update_ebb_live_info (rtx head, rtx tail)
4871 unsigned int j;
4872 int i, regno;
4873 bool live_p;
4874 rtx prev_insn, set;
4875 bool remove_p;
4876 basic_block last_bb, prev_bb, curr_bb;
4877 bitmap_iterator bi;
4878 struct lra_insn_reg *reg;
4879 edge e;
4880 edge_iterator ei;
4882 last_bb = BLOCK_FOR_INSN (tail);
4883 prev_bb = NULL;
4884 for (curr_insn = tail;
4885 curr_insn != PREV_INSN (head);
4886 curr_insn = prev_insn)
4888 prev_insn = PREV_INSN (curr_insn);
4889 /* We need to process empty blocks too. They contain
4890 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
4891 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
4892 continue;
4893 curr_bb = BLOCK_FOR_INSN (curr_insn);
4894 if (curr_bb != prev_bb)
4896 if (prev_bb != NULL)
4898 /* Update df_get_live_in (prev_bb): */
4899 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4900 if (bitmap_bit_p (&live_regs, j))
4901 bitmap_set_bit (df_get_live_in (prev_bb), j);
4902 else
4903 bitmap_clear_bit (df_get_live_in (prev_bb), j);
4905 if (curr_bb != last_bb)
4907 /* Update df_get_live_out (curr_bb): */
4908 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4910 live_p = bitmap_bit_p (&live_regs, j);
4911 if (! live_p)
4912 FOR_EACH_EDGE (e, ei, curr_bb->succs)
4913 if (bitmap_bit_p (df_get_live_in (e->dest), j))
4915 live_p = true;
4916 break;
4918 if (live_p)
4919 bitmap_set_bit (df_get_live_out (curr_bb), j);
4920 else
4921 bitmap_clear_bit (df_get_live_out (curr_bb), j);
4924 prev_bb = curr_bb;
4925 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
4927 if (! NONDEBUG_INSN_P (curr_insn))
4928 continue;
4929 curr_id = lra_get_insn_recog_data (curr_insn);
4930 curr_static_id = curr_id->insn_static_data;
4931 remove_p = false;
4932 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
4933 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
4934 && bitmap_bit_p (&check_only_regs, regno)
4935 && ! bitmap_bit_p (&live_regs, regno))
4936 remove_p = true;
4937 /* See which defined values die here. */
4938 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4939 if (reg->type == OP_OUT && ! reg->subreg_p)
4940 bitmap_clear_bit (&live_regs, reg->regno);
4941 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
4942 if (reg->type == OP_OUT && ! reg->subreg_p)
4943 bitmap_clear_bit (&live_regs, reg->regno);
4944 /* Mark each used value as live. */
4945 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
4946 if (reg->type != OP_OUT
4947 && bitmap_bit_p (&check_only_regs, reg->regno))
4948 bitmap_set_bit (&live_regs, reg->regno);
4949 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
4950 if (reg->type != OP_OUT
4951 && bitmap_bit_p (&check_only_regs, reg->regno))
4952 bitmap_set_bit (&live_regs, reg->regno);
4953 if (curr_id->arg_hard_regs != NULL)
4954 /* Make argument hard registers live. */
4955 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
4956 if (bitmap_bit_p (&check_only_regs, regno))
4957 bitmap_set_bit (&live_regs, regno);
4958 /* It is quite important to remove dead move insns because it
4959 means removing dead store. We don't need to process them for
4960 constraints. */
4961 if (remove_p)
4963 if (lra_dump_file != NULL)
4965 fprintf (lra_dump_file, " Removing dead insn:\n ");
4966 dump_insn_slim (lra_dump_file, curr_insn);
4968 lra_set_insn_deleted (curr_insn);
4973 /* The structure describes info to do an inheritance for the current
4974 insn. We need to collect such info first before doing the
4975 transformations because the transformations change the insn
4976 internal representation. */
4977 struct to_inherit
4979 /* Original regno. */
4980 int regno;
4981 /* Subsequent insns which can inherit original reg value. */
4982 rtx insns;
4985 /* Array containing all info for doing inheritance from the current
4986 insn. */
4987 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
4989 /* Number elements in the previous array. */
4990 static int to_inherit_num;
4992 /* Add inheritance info REGNO and INSNS. Their meaning is described in
4993 structure to_inherit. */
4994 static void
4995 add_to_inherit (int regno, rtx insns)
4997 int i;
4999 for (i = 0; i < to_inherit_num; i++)
5000 if (to_inherit[i].regno == regno)
5001 return;
5002 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5003 to_inherit[to_inherit_num].regno = regno;
5004 to_inherit[to_inherit_num++].insns = insns;
5007 /* Return the last non-debug insn in basic block BB, or the block begin
5008 note if none. */
5009 static rtx
5010 get_last_insertion_point (basic_block bb)
5012 rtx insn;
5014 FOR_BB_INSNS_REVERSE (bb, insn)
5015 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5016 return insn;
5017 gcc_unreachable ();
5020 /* Set up RES by registers living on edges FROM except the edge (FROM,
5021 TO) or by registers set up in a jump insn in BB FROM. */
5022 static void
5023 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5025 rtx last;
5026 struct lra_insn_reg *reg;
5027 edge e;
5028 edge_iterator ei;
5030 lra_assert (to != NULL);
5031 bitmap_clear (res);
5032 FOR_EACH_EDGE (e, ei, from->succs)
5033 if (e->dest != to)
5034 bitmap_ior_into (res, df_get_live_in (e->dest));
5035 last = get_last_insertion_point (from);
5036 if (! JUMP_P (last))
5037 return;
5038 curr_id = lra_get_insn_recog_data (last);
5039 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5040 if (reg->type != OP_IN)
5041 bitmap_set_bit (res, reg->regno);
5044 /* Used as a temporary results of some bitmap calculations. */
5045 static bitmap_head temp_bitmap;
5047 /* We split for reloads of small class of hard regs. The following
5048 defines how many hard regs the class should have to be qualified as
5049 small. The code is mostly oriented to x86/x86-64 architecture
5050 where some insns need to use only specific register or pair of
5051 registers and these register can live in RTL explicitly, e.g. for
5052 parameter passing. */
5053 static const int max_small_class_regs_num = 2;
5055 /* Do inheritance/split transformations in EBB starting with HEAD and
5056 finishing on TAIL. We process EBB insns in the reverse order.
5057 Return true if we did any inheritance/split transformation in the
5058 EBB.
5060 We should avoid excessive splitting which results in worse code
5061 because of inaccurate cost calculations for spilling new split
5062 pseudos in such case. To achieve this we do splitting only if
5063 register pressure is high in given basic block and there are reload
5064 pseudos requiring hard registers. We could do more register
5065 pressure calculations at any given program point to avoid necessary
5066 splitting even more but it is to expensive and the current approach
5067 works well enough. */
5068 static bool
5069 inherit_in_ebb (rtx head, rtx tail)
5071 int i, src_regno, dst_regno, nregs;
5072 bool change_p, succ_p, update_reloads_num_p;
5073 rtx prev_insn, next_usage_insns, set, last_insn;
5074 enum reg_class cl;
5075 struct lra_insn_reg *reg;
5076 basic_block last_processed_bb, curr_bb = NULL;
5077 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5078 bitmap to_process;
5079 unsigned int j;
5080 bitmap_iterator bi;
5081 bool head_p, after_p;
5083 change_p = false;
5084 curr_usage_insns_check++;
5085 reloads_num = calls_num = 0;
5086 bitmap_clear (&check_only_regs);
5087 last_processed_bb = NULL;
5088 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5089 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5090 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5091 /* We don't process new insns generated in the loop. */
5092 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5094 prev_insn = PREV_INSN (curr_insn);
5095 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5096 curr_bb = BLOCK_FOR_INSN (curr_insn);
5097 if (last_processed_bb != curr_bb)
5099 /* We are at the end of BB. Add qualified living
5100 pseudos for potential splitting. */
5101 to_process = df_get_live_out (curr_bb);
5102 if (last_processed_bb != NULL)
5104 /* We are somewhere in the middle of EBB. */
5105 get_live_on_other_edges (curr_bb, last_processed_bb,
5106 &temp_bitmap);
5107 to_process = &temp_bitmap;
5109 last_processed_bb = curr_bb;
5110 last_insn = get_last_insertion_point (curr_bb);
5111 after_p = (! JUMP_P (last_insn)
5112 && (! CALL_P (last_insn)
5113 || (find_reg_note (last_insn,
5114 REG_NORETURN, NULL_RTX) == NULL_RTX
5115 && ! SIBLING_CALL_P (last_insn))));
5116 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5117 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5119 if ((int) j >= lra_constraint_new_regno_start)
5120 break;
5121 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5123 if (j < FIRST_PSEUDO_REGISTER)
5124 SET_HARD_REG_BIT (live_hard_regs, j);
5125 else
5126 add_to_hard_reg_set (&live_hard_regs,
5127 PSEUDO_REGNO_MODE (j),
5128 reg_renumber[j]);
5129 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5133 src_regno = dst_regno = -1;
5134 if (NONDEBUG_INSN_P (curr_insn)
5135 && (set = single_set (curr_insn)) != NULL_RTX
5136 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5138 src_regno = REGNO (SET_SRC (set));
5139 dst_regno = REGNO (SET_DEST (set));
5141 update_reloads_num_p = true;
5142 if (src_regno < lra_constraint_new_regno_start
5143 && src_regno >= FIRST_PSEUDO_REGISTER
5144 && reg_renumber[src_regno] < 0
5145 && dst_regno >= lra_constraint_new_regno_start
5146 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5148 /* 'reload_pseudo <- original_pseudo'. */
5149 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5150 reloads_num++;
5151 update_reloads_num_p = false;
5152 succ_p = false;
5153 if (usage_insns[src_regno].check == curr_usage_insns_check
5154 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5155 succ_p = inherit_reload_reg (false, src_regno, cl,
5156 curr_insn, next_usage_insns);
5157 if (succ_p)
5158 change_p = true;
5159 else
5160 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5161 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5162 IOR_HARD_REG_SET (potential_reload_hard_regs,
5163 reg_class_contents[cl]);
5165 else if (src_regno >= lra_constraint_new_regno_start
5166 && dst_regno < lra_constraint_new_regno_start
5167 && dst_regno >= FIRST_PSEUDO_REGISTER
5168 && reg_renumber[dst_regno] < 0
5169 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5170 && usage_insns[dst_regno].check == curr_usage_insns_check
5171 && (next_usage_insns
5172 = usage_insns[dst_regno].insns) != NULL_RTX)
5174 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5175 reloads_num++;
5176 update_reloads_num_p = false;
5177 /* 'original_pseudo <- reload_pseudo'. */
5178 if (! JUMP_P (curr_insn)
5179 && inherit_reload_reg (true, dst_regno, cl,
5180 curr_insn, next_usage_insns))
5181 change_p = true;
5182 /* Invalidate. */
5183 usage_insns[dst_regno].check = 0;
5184 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5185 IOR_HARD_REG_SET (potential_reload_hard_regs,
5186 reg_class_contents[cl]);
5188 else if (INSN_P (curr_insn))
5190 int iter;
5191 int max_uid = get_max_uid ();
5193 curr_id = lra_get_insn_recog_data (curr_insn);
5194 curr_static_id = curr_id->insn_static_data;
5195 to_inherit_num = 0;
5196 /* Process insn definitions. */
5197 for (iter = 0; iter < 2; iter++)
5198 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5199 reg != NULL;
5200 reg = reg->next)
5201 if (reg->type != OP_IN
5202 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5204 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5205 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5206 && usage_insns[dst_regno].check == curr_usage_insns_check
5207 && (next_usage_insns
5208 = usage_insns[dst_regno].insns) != NULL_RTX)
5210 struct lra_insn_reg *r;
5212 for (r = curr_id->regs; r != NULL; r = r->next)
5213 if (r->type != OP_OUT && r->regno == dst_regno)
5214 break;
5215 /* Don't do inheritance if the pseudo is also
5216 used in the insn. */
5217 if (r == NULL)
5218 /* We can not do inheritance right now
5219 because the current insn reg info (chain
5220 regs) can change after that. */
5221 add_to_inherit (dst_regno, next_usage_insns);
5223 /* We can not process one reg twice here because of
5224 usage_insns invalidation. */
5225 if ((dst_regno < FIRST_PSEUDO_REGISTER
5226 || reg_renumber[dst_regno] >= 0)
5227 && ! reg->subreg_p && reg->type != OP_IN)
5229 HARD_REG_SET s;
5231 if (split_if_necessary (dst_regno, reg->biggest_mode,
5232 potential_reload_hard_regs,
5233 false, curr_insn, max_uid))
5234 change_p = true;
5235 CLEAR_HARD_REG_SET (s);
5236 if (dst_regno < FIRST_PSEUDO_REGISTER)
5237 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5238 else
5239 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5240 reg_renumber[dst_regno]);
5241 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5243 /* We should invalidate potential inheritance or
5244 splitting for the current insn usages to the next
5245 usage insns (see code below) as the output pseudo
5246 prevents this. */
5247 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5248 && reg_renumber[dst_regno] < 0)
5249 || (reg->type == OP_OUT && ! reg->subreg_p
5250 && (dst_regno < FIRST_PSEUDO_REGISTER
5251 || reg_renumber[dst_regno] >= 0)))
5253 /* Invalidate and mark definitions. */
5254 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5255 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5256 else
5258 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5259 for (i = 0; i < nregs; i++)
5260 usage_insns[dst_regno + i].check
5261 = -(int) INSN_UID (curr_insn);
5265 if (! JUMP_P (curr_insn))
5266 for (i = 0; i < to_inherit_num; i++)
5267 if (inherit_reload_reg (true, to_inherit[i].regno,
5268 ALL_REGS, curr_insn,
5269 to_inherit[i].insns))
5270 change_p = true;
5271 if (CALL_P (curr_insn))
5273 rtx cheap, pat, dest, restore;
5274 int regno, hard_regno;
5276 calls_num++;
5277 if ((cheap = find_reg_note (curr_insn,
5278 REG_RETURNED, NULL_RTX)) != NULL_RTX
5279 && ((cheap = XEXP (cheap, 0)), true)
5280 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5281 && (hard_regno = reg_renumber[regno]) >= 0
5282 /* If there are pending saves/restores, the
5283 optimization is not worth. */
5284 && usage_insns[regno].calls_num == calls_num - 1
5285 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5287 /* Restore the pseudo from the call result as
5288 REG_RETURNED note says that the pseudo value is
5289 in the call result and the pseudo is an argument
5290 of the call. */
5291 pat = PATTERN (curr_insn);
5292 if (GET_CODE (pat) == PARALLEL)
5293 pat = XVECEXP (pat, 0, 0);
5294 dest = SET_DEST (pat);
5295 start_sequence ();
5296 emit_move_insn (cheap, copy_rtx (dest));
5297 restore = get_insns ();
5298 end_sequence ();
5299 lra_process_new_insns (curr_insn, NULL, restore,
5300 "Inserting call parameter restore");
5301 /* We don't need to save/restore of the pseudo from
5302 this call. */
5303 usage_insns[regno].calls_num = calls_num;
5304 bitmap_set_bit (&check_only_regs, regno);
5307 to_inherit_num = 0;
5308 /* Process insn usages. */
5309 for (iter = 0; iter < 2; iter++)
5310 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5311 reg != NULL;
5312 reg = reg->next)
5313 if ((reg->type != OP_OUT
5314 || (reg->type == OP_OUT && reg->subreg_p))
5315 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5317 if (src_regno >= FIRST_PSEUDO_REGISTER
5318 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5320 if (usage_insns[src_regno].check == curr_usage_insns_check
5321 && (next_usage_insns
5322 = usage_insns[src_regno].insns) != NULL_RTX
5323 && NONDEBUG_INSN_P (curr_insn))
5324 add_to_inherit (src_regno, next_usage_insns);
5325 else if (usage_insns[src_regno].check
5326 != -(int) INSN_UID (curr_insn))
5327 /* Add usages but only if the reg is not set up
5328 in the same insn. */
5329 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5331 else if (src_regno < FIRST_PSEUDO_REGISTER
5332 || reg_renumber[src_regno] >= 0)
5334 bool before_p;
5335 rtx use_insn = curr_insn;
5337 before_p = (JUMP_P (curr_insn)
5338 || (CALL_P (curr_insn) && reg->type == OP_IN));
5339 if (NONDEBUG_INSN_P (curr_insn)
5340 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5341 && split_if_necessary (src_regno, reg->biggest_mode,
5342 potential_reload_hard_regs,
5343 before_p, curr_insn, max_uid))
5345 if (reg->subreg_p)
5346 lra_risky_transformations_p = true;
5347 change_p = true;
5348 /* Invalidate. */
5349 usage_insns[src_regno].check = 0;
5350 if (before_p)
5351 use_insn = PREV_INSN (curr_insn);
5353 if (NONDEBUG_INSN_P (curr_insn))
5355 if (src_regno < FIRST_PSEUDO_REGISTER)
5356 add_to_hard_reg_set (&live_hard_regs,
5357 reg->biggest_mode, src_regno);
5358 else
5359 add_to_hard_reg_set (&live_hard_regs,
5360 PSEUDO_REGNO_MODE (src_regno),
5361 reg_renumber[src_regno]);
5363 add_next_usage_insn (src_regno, use_insn, reloads_num);
5366 /* Process call args. */
5367 if (curr_id->arg_hard_regs != NULL)
5368 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5369 if (src_regno < FIRST_PSEUDO_REGISTER)
5371 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5372 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5374 for (i = 0; i < to_inherit_num; i++)
5376 src_regno = to_inherit[i].regno;
5377 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5378 curr_insn, to_inherit[i].insns))
5379 change_p = true;
5380 else
5381 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5384 if (update_reloads_num_p
5385 && NONDEBUG_INSN_P (curr_insn)
5386 && (set = single_set (curr_insn)) != NULL_RTX)
5388 int regno = -1;
5389 if ((REG_P (SET_DEST (set))
5390 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5391 && reg_renumber[regno] < 0
5392 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5393 || (REG_P (SET_SRC (set))
5394 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5395 && reg_renumber[regno] < 0
5396 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5398 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5399 reloads_num++;
5400 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5401 IOR_HARD_REG_SET (potential_reload_hard_regs,
5402 reg_class_contents[cl]);
5405 /* We reached the start of the current basic block. */
5406 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5407 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5409 /* We reached the beginning of the current block -- do
5410 rest of spliting in the current BB. */
5411 to_process = df_get_live_in (curr_bb);
5412 if (BLOCK_FOR_INSN (head) != curr_bb)
5414 /* We are somewhere in the middle of EBB. */
5415 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5416 curr_bb, &temp_bitmap);
5417 to_process = &temp_bitmap;
5419 head_p = true;
5420 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5422 if ((int) j >= lra_constraint_new_regno_start)
5423 break;
5424 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5425 && usage_insns[j].check == curr_usage_insns_check
5426 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5428 if (need_for_split_p (potential_reload_hard_regs, j))
5430 if (lra_dump_file != NULL && head_p)
5432 fprintf (lra_dump_file,
5433 " ----------------------------------\n");
5434 head_p = false;
5436 if (split_reg (false, j, bb_note (curr_bb),
5437 next_usage_insns))
5438 change_p = true;
5440 usage_insns[j].check = 0;
5445 return change_p;
5448 /* This value affects EBB forming. If probability of edge from EBB to
5449 a BB is not greater than the following value, we don't add the BB
5450 to EBB. */
5451 #define EBB_PROBABILITY_CUTOFF ((REG_BR_PROB_BASE * 50) / 100)
5453 /* Current number of inheritance/split iteration. */
5454 int lra_inheritance_iter;
5456 /* Entry function for inheritance/split pass. */
5457 void
5458 lra_inheritance (void)
5460 int i;
5461 basic_block bb, start_bb;
5462 edge e;
5464 lra_inheritance_iter++;
5465 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5466 return;
5467 timevar_push (TV_LRA_INHERITANCE);
5468 if (lra_dump_file != NULL)
5469 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5470 lra_inheritance_iter);
5471 curr_usage_insns_check = 0;
5472 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5473 for (i = 0; i < lra_constraint_new_regno_start; i++)
5474 usage_insns[i].check = 0;
5475 bitmap_initialize (&check_only_regs, &reg_obstack);
5476 bitmap_initialize (&live_regs, &reg_obstack);
5477 bitmap_initialize (&temp_bitmap, &reg_obstack);
5478 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5479 FOR_EACH_BB_FN (bb, cfun)
5481 start_bb = bb;
5482 if (lra_dump_file != NULL)
5483 fprintf (lra_dump_file, "EBB");
5484 /* Form a EBB starting with BB. */
5485 bitmap_clear (&ebb_global_regs);
5486 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5487 for (;;)
5489 if (lra_dump_file != NULL)
5490 fprintf (lra_dump_file, " %d", bb->index);
5491 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5492 || LABEL_P (BB_HEAD (bb->next_bb)))
5493 break;
5494 e = find_fallthru_edge (bb->succs);
5495 if (! e)
5496 break;
5497 if (e->probability <= EBB_PROBABILITY_CUTOFF)
5498 break;
5499 bb = bb->next_bb;
5501 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5502 if (lra_dump_file != NULL)
5503 fprintf (lra_dump_file, "\n");
5504 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5505 /* Remember that the EBB head and tail can change in
5506 inherit_in_ebb. */
5507 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5509 bitmap_clear (&ebb_global_regs);
5510 bitmap_clear (&temp_bitmap);
5511 bitmap_clear (&live_regs);
5512 bitmap_clear (&check_only_regs);
5513 free (usage_insns);
5515 timevar_pop (TV_LRA_INHERITANCE);
5520 /* This page contains code to undo failed inheritance/split
5521 transformations. */
5523 /* Current number of iteration undoing inheritance/split. */
5524 int lra_undo_inheritance_iter;
5526 /* Fix BB live info LIVE after removing pseudos created on pass doing
5527 inheritance/split which are REMOVED_PSEUDOS. */
5528 static void
5529 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5531 unsigned int regno;
5532 bitmap_iterator bi;
5534 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5535 if (bitmap_clear_bit (live, regno))
5536 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5539 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5540 number. */
5541 static int
5542 get_regno (rtx reg)
5544 if (GET_CODE (reg) == SUBREG)
5545 reg = SUBREG_REG (reg);
5546 if (REG_P (reg))
5547 return REGNO (reg);
5548 return -1;
5551 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5552 return true if we did any change. The undo transformations for
5553 inheritance looks like
5554 i <- i2
5555 p <- i => p <- i2
5556 or removing
5557 p <- i, i <- p, and i <- i3
5558 where p is original pseudo from which inheritance pseudo i was
5559 created, i and i3 are removed inheritance pseudos, i2 is another
5560 not removed inheritance pseudo. All split pseudos or other
5561 occurrences of removed inheritance pseudos are changed on the
5562 corresponding original pseudos.
5564 The function also schedules insns changed and created during
5565 inheritance/split pass for processing by the subsequent constraint
5566 pass. */
5567 static bool
5568 remove_inheritance_pseudos (bitmap remove_pseudos)
5570 basic_block bb;
5571 int regno, sregno, prev_sregno, dregno, restore_regno;
5572 rtx set, prev_set, prev_insn;
5573 bool change_p, done_p;
5575 change_p = ! bitmap_empty_p (remove_pseudos);
5576 /* We can not finish the function right away if CHANGE_P is true
5577 because we need to marks insns affected by previous
5578 inheritance/split pass for processing by the subsequent
5579 constraint pass. */
5580 FOR_EACH_BB_FN (bb, cfun)
5582 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5583 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5584 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5586 if (! INSN_P (curr_insn))
5587 continue;
5588 done_p = false;
5589 sregno = dregno = -1;
5590 if (change_p && NONDEBUG_INSN_P (curr_insn)
5591 && (set = single_set (curr_insn)) != NULL_RTX)
5593 dregno = get_regno (SET_DEST (set));
5594 sregno = get_regno (SET_SRC (set));
5597 if (sregno >= 0 && dregno >= 0)
5599 if ((bitmap_bit_p (remove_pseudos, sregno)
5600 && (lra_reg_info[sregno].restore_regno == dregno
5601 || (bitmap_bit_p (remove_pseudos, dregno)
5602 && (lra_reg_info[sregno].restore_regno
5603 == lra_reg_info[dregno].restore_regno))))
5604 || (bitmap_bit_p (remove_pseudos, dregno)
5605 && lra_reg_info[dregno].restore_regno == sregno))
5606 /* One of the following cases:
5607 original <- removed inheritance pseudo
5608 removed inherit pseudo <- another removed inherit pseudo
5609 removed inherit pseudo <- original pseudo
5611 removed_split_pseudo <- original_reg
5612 original_reg <- removed_split_pseudo */
5614 if (lra_dump_file != NULL)
5616 fprintf (lra_dump_file, " Removing %s:\n",
5617 bitmap_bit_p (&lra_split_regs, sregno)
5618 || bitmap_bit_p (&lra_split_regs, dregno)
5619 ? "split" : "inheritance");
5620 dump_insn_slim (lra_dump_file, curr_insn);
5622 lra_set_insn_deleted (curr_insn);
5623 done_p = true;
5625 else if (bitmap_bit_p (remove_pseudos, sregno)
5626 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5628 /* Search the following pattern:
5629 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5630 original_pseudo <- inherit_or_split_pseudo1
5631 where the 2nd insn is the current insn and
5632 inherit_or_split_pseudo2 is not removed. If it is found,
5633 change the current insn onto:
5634 original_pseudo <- inherit_or_split_pseudo2. */
5635 for (prev_insn = PREV_INSN (curr_insn);
5636 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5637 prev_insn = PREV_INSN (prev_insn))
5639 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5640 && (prev_set = single_set (prev_insn)) != NULL_RTX
5641 /* There should be no subregs in insn we are
5642 searching because only the original reg might
5643 be in subreg when we changed the mode of
5644 load/store for splitting. */
5645 && REG_P (SET_DEST (prev_set))
5646 && REG_P (SET_SRC (prev_set))
5647 && (int) REGNO (SET_DEST (prev_set)) == sregno
5648 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5649 >= FIRST_PSEUDO_REGISTER)
5650 /* As we consider chain of inheritance or
5651 splitting described in above comment we should
5652 check that sregno and prev_sregno were
5653 inheritance/split pseudos created from the
5654 same original regno. */
5655 && (lra_reg_info[sregno].restore_regno
5656 == lra_reg_info[prev_sregno].restore_regno)
5657 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5659 lra_assert (GET_MODE (SET_SRC (prev_set))
5660 == GET_MODE (regno_reg_rtx[sregno]));
5661 if (GET_CODE (SET_SRC (set)) == SUBREG)
5662 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5663 else
5664 SET_SRC (set) = SET_SRC (prev_set);
5665 lra_push_insn_and_update_insn_regno_info (curr_insn);
5666 lra_set_used_insn_alternative_by_uid
5667 (INSN_UID (curr_insn), -1);
5668 done_p = true;
5669 if (lra_dump_file != NULL)
5671 fprintf (lra_dump_file, " Change reload insn:\n");
5672 dump_insn_slim (lra_dump_file, curr_insn);
5677 if (! done_p)
5679 struct lra_insn_reg *reg;
5680 bool restored_regs_p = false;
5681 bool kept_regs_p = false;
5683 curr_id = lra_get_insn_recog_data (curr_insn);
5684 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5686 regno = reg->regno;
5687 restore_regno = lra_reg_info[regno].restore_regno;
5688 if (restore_regno >= 0)
5690 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5692 substitute_pseudo (&curr_insn, regno,
5693 regno_reg_rtx[restore_regno]);
5694 restored_regs_p = true;
5696 else
5697 kept_regs_p = true;
5700 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5702 /* The instruction has changed since the previous
5703 constraints pass. */
5704 lra_push_insn_and_update_insn_regno_info (curr_insn);
5705 lra_set_used_insn_alternative_by_uid
5706 (INSN_UID (curr_insn), -1);
5708 else if (restored_regs_p)
5709 /* The instruction has been restored to the form that
5710 it had during the previous constraints pass. */
5711 lra_update_insn_regno_info (curr_insn);
5712 if (restored_regs_p && lra_dump_file != NULL)
5714 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5715 dump_insn_slim (lra_dump_file, curr_insn);
5720 return change_p;
5723 /* If optional reload pseudos failed to get a hard register or was not
5724 inherited, it is better to remove optional reloads. We do this
5725 transformation after undoing inheritance to figure out necessity to
5726 remove optional reloads easier. Return true if we do any
5727 change. */
5728 static bool
5729 undo_optional_reloads (void)
5731 bool change_p, keep_p;
5732 unsigned int regno, uid;
5733 bitmap_iterator bi, bi2;
5734 rtx insn, set, src, dest;
5735 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
5737 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
5738 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
5739 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5741 keep_p = false;
5742 /* Keep optional reloads from previous subpasses. */
5743 if (lra_reg_info[regno].restore_regno < 0
5744 /* If the original pseudo changed its allocation, just
5745 removing the optional pseudo is dangerous as the original
5746 pseudo will have longer live range. */
5747 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
5748 keep_p = true;
5749 else if (reg_renumber[regno] >= 0)
5750 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
5752 insn = lra_insn_recog_data[uid]->insn;
5753 if ((set = single_set (insn)) == NULL_RTX)
5754 continue;
5755 src = SET_SRC (set);
5756 dest = SET_DEST (set);
5757 if (! REG_P (src) || ! REG_P (dest))
5758 continue;
5759 if (REGNO (dest) == regno
5760 /* Ignore insn for optional reloads itself. */
5761 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
5762 /* Check only inheritance on last inheritance pass. */
5763 && (int) REGNO (src) >= new_regno_start
5764 /* Check that the optional reload was inherited. */
5765 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
5767 keep_p = true;
5768 break;
5771 if (keep_p)
5773 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
5774 if (lra_dump_file != NULL)
5775 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
5778 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
5779 bitmap_initialize (&insn_bitmap, &reg_obstack);
5780 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
5782 if (lra_dump_file != NULL)
5783 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
5784 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
5785 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
5787 insn = lra_insn_recog_data[uid]->insn;
5788 if ((set = single_set (insn)) != NULL_RTX)
5790 src = SET_SRC (set);
5791 dest = SET_DEST (set);
5792 if (REG_P (src) && REG_P (dest)
5793 && ((REGNO (src) == regno
5794 && (lra_reg_info[regno].restore_regno
5795 == (int) REGNO (dest)))
5796 || (REGNO (dest) == regno
5797 && (lra_reg_info[regno].restore_regno
5798 == (int) REGNO (src)))))
5800 if (lra_dump_file != NULL)
5802 fprintf (lra_dump_file, " Deleting move %u\n",
5803 INSN_UID (insn));
5804 dump_insn_slim (lra_dump_file, insn);
5806 lra_set_insn_deleted (insn);
5807 continue;
5809 /* We should not worry about generation memory-memory
5810 moves here as if the corresponding inheritance did
5811 not work (inheritance pseudo did not get a hard reg),
5812 we remove the inheritance pseudo and the optional
5813 reload. */
5815 substitute_pseudo (&insn, regno,
5816 regno_reg_rtx[lra_reg_info[regno].restore_regno]);
5817 lra_update_insn_regno_info (insn);
5818 if (lra_dump_file != NULL)
5820 fprintf (lra_dump_file,
5821 " Restoring original insn:\n");
5822 dump_insn_slim (lra_dump_file, insn);
5826 /* Clear restore_regnos. */
5827 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5828 lra_reg_info[regno].restore_regno = -1;
5829 bitmap_clear (&insn_bitmap);
5830 bitmap_clear (&removed_optional_reload_pseudos);
5831 return change_p;
5834 /* Entry function for undoing inheritance/split transformation. Return true
5835 if we did any RTL change in this pass. */
5836 bool
5837 lra_undo_inheritance (void)
5839 unsigned int regno;
5840 int restore_regno, hard_regno;
5841 int n_all_inherit, n_inherit, n_all_split, n_split;
5842 bitmap_head remove_pseudos;
5843 bitmap_iterator bi;
5844 bool change_p;
5846 lra_undo_inheritance_iter++;
5847 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5848 return false;
5849 if (lra_dump_file != NULL)
5850 fprintf (lra_dump_file,
5851 "\n********** Undoing inheritance #%d: **********\n\n",
5852 lra_undo_inheritance_iter);
5853 bitmap_initialize (&remove_pseudos, &reg_obstack);
5854 n_inherit = n_all_inherit = 0;
5855 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5856 if (lra_reg_info[regno].restore_regno >= 0)
5858 n_all_inherit++;
5859 if (reg_renumber[regno] < 0
5860 /* If the original pseudo changed its allocation, just
5861 removing inheritance is dangerous as for changing
5862 allocation we used shorter live-ranges. */
5863 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
5864 bitmap_set_bit (&remove_pseudos, regno);
5865 else
5866 n_inherit++;
5868 if (lra_dump_file != NULL && n_all_inherit != 0)
5869 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
5870 n_inherit, n_all_inherit,
5871 (double) n_inherit / n_all_inherit * 100);
5872 n_split = n_all_split = 0;
5873 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5874 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
5876 n_all_split++;
5877 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
5878 ? reg_renumber[restore_regno] : restore_regno);
5879 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
5880 bitmap_set_bit (&remove_pseudos, regno);
5881 else
5883 n_split++;
5884 if (lra_dump_file != NULL)
5885 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
5886 regno, restore_regno);
5889 if (lra_dump_file != NULL && n_all_split != 0)
5890 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
5891 n_split, n_all_split,
5892 (double) n_split / n_all_split * 100);
5893 change_p = remove_inheritance_pseudos (&remove_pseudos);
5894 bitmap_clear (&remove_pseudos);
5895 /* Clear restore_regnos. */
5896 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5897 lra_reg_info[regno].restore_regno = -1;
5898 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5899 lra_reg_info[regno].restore_regno = -1;
5900 change_p = undo_optional_reloads () || change_p;
5901 return change_p;