Daily bump.
[official-gcc.git] / gcc / lra-constraints.c
blob874696865b30e0e5681bbf847872ebe35496b176
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2014 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file contains code for 3 passes: constraint pass,
23 inheritance/split pass, and pass for undoing failed inheritance and
24 split.
26 The major goal of constraint pass is to transform RTL to satisfy
27 insn and address constraints by:
28 o choosing insn alternatives;
29 o generating *reload insns* (or reloads in brief) and *reload
30 pseudos* which will get necessary hard registers later;
31 o substituting pseudos with equivalent values and removing the
32 instructions that initialized those pseudos.
34 The constraint pass has biggest and most complicated code in LRA.
35 There are a lot of important details like:
36 o reuse of input reload pseudos to simplify reload pseudo
37 allocations;
38 o some heuristics to choose insn alternative to improve the
39 inheritance;
40 o early clobbers etc.
42 The pass is mimicking former reload pass in alternative choosing
43 because the reload pass is oriented to current machine description
44 model. It might be changed if the machine description model is
45 changed.
47 There is special code for preventing all LRA and this pass cycling
48 in case of bugs.
50 On the first iteration of the pass we process every instruction and
51 choose an alternative for each one. On subsequent iterations we try
52 to avoid reprocessing instructions if we can be sure that the old
53 choice is still valid.
55 The inheritance/spilt pass is to transform code to achieve
56 ineheritance and live range splitting. It is done on backward
57 traversal of EBBs.
59 The inheritance optimization goal is to reuse values in hard
60 registers. There is analogous optimization in old reload pass. The
61 inheritance is achieved by following transformation:
63 reload_p1 <- p reload_p1 <- p
64 ... new_p <- reload_p1
65 ... => ...
66 reload_p2 <- p reload_p2 <- new_p
68 where p is spilled and not changed between the insns. Reload_p1 is
69 also called *original pseudo* and new_p is called *inheritance
70 pseudo*.
72 The subsequent assignment pass will try to assign the same (or
73 another if it is not possible) hard register to new_p as to
74 reload_p1 or reload_p2.
76 If the assignment pass fails to assign a hard register to new_p,
77 this file will undo the inheritance and restore the original code.
78 This is because implementing the above sequence with a spilled
79 new_p would make the code much worse. The inheritance is done in
80 EBB scope. The above is just a simplified example to get an idea
81 of the inheritance as the inheritance is also done for non-reload
82 insns.
84 Splitting (transformation) is also done in EBB scope on the same
85 pass as the inheritance:
87 r <- ... or ... <- r r <- ... or ... <- r
88 ... s <- r (new insn -- save)
89 ... =>
90 ... r <- s (new insn -- restore)
91 ... <- r ... <- r
93 The *split pseudo* s is assigned to the hard register of the
94 original pseudo or hard register r.
96 Splitting is done:
97 o In EBBs with high register pressure for global pseudos (living
98 in at least 2 BBs) and assigned to hard registers when there
99 are more one reloads needing the hard registers;
100 o for pseudos needing save/restore code around calls.
102 If the split pseudo still has the same hard register as the
103 original pseudo after the subsequent assignment pass or the
104 original pseudo was split, the opposite transformation is done on
105 the same pass for undoing inheritance. */
107 #undef REG_OK_STRICT
109 #include "config.h"
110 #include "system.h"
111 #include "coretypes.h"
112 #include "tm.h"
113 #include "hard-reg-set.h"
114 #include "rtl.h"
115 #include "tm_p.h"
116 #include "regs.h"
117 #include "insn-config.h"
118 #include "insn-codes.h"
119 #include "recog.h"
120 #include "output.h"
121 #include "addresses.h"
122 #include "target.h"
123 #include "function.h"
124 #include "expr.h"
125 #include "basic-block.h"
126 #include "except.h"
127 #include "optabs.h"
128 #include "df.h"
129 #include "ira.h"
130 #include "rtl-error.h"
131 #include "lra-int.h"
133 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
134 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
135 reload insns. */
136 static int bb_reload_num;
138 /* The current insn being processed and corresponding its single set
139 (NULL otherwise), its data (basic block, the insn data, the insn
140 static data, and the mode of each operand). */
141 static rtx curr_insn;
142 static rtx curr_insn_set;
143 static basic_block curr_bb;
144 static lra_insn_recog_data_t curr_id;
145 static struct lra_static_insn_data *curr_static_id;
146 static enum machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
150 /* Start numbers for new registers and insns at the current constraints
151 pass start. */
152 static int new_regno_start;
153 static int new_insn_uid_start;
155 /* If LOC is nonnull, strip any outer subreg from it. */
156 static inline rtx *
157 strip_subreg (rtx *loc)
159 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
162 /* Return hard regno of REGNO or if it is was not assigned to a hard
163 register, use a hard register from its allocno class. */
164 static int
165 get_try_hard_regno (int regno)
167 int hard_regno;
168 enum reg_class rclass;
170 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
171 hard_regno = lra_get_regno_hard_regno (regno);
172 if (hard_regno >= 0)
173 return hard_regno;
174 rclass = lra_get_allocno_class (regno);
175 if (rclass == NO_REGS)
176 return -1;
177 return ira_class_hard_regs[rclass][0];
180 /* Return final hard regno (plus offset) which will be after
181 elimination. We do this for matching constraints because the final
182 hard regno could have a different class. */
183 static int
184 get_final_hard_regno (int hard_regno, int offset)
186 if (hard_regno < 0)
187 return hard_regno;
188 hard_regno = lra_get_elimination_hard_regno (hard_regno);
189 return hard_regno + offset;
192 /* Return hard regno of X after removing subreg and making
193 elimination. If X is not a register or subreg of register, return
194 -1. For pseudo use its assignment. */
195 static int
196 get_hard_regno (rtx x)
198 rtx reg;
199 int offset, hard_regno;
201 reg = x;
202 if (GET_CODE (x) == SUBREG)
203 reg = SUBREG_REG (x);
204 if (! REG_P (reg))
205 return -1;
206 if ((hard_regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
207 hard_regno = lra_get_regno_hard_regno (hard_regno);
208 if (hard_regno < 0)
209 return -1;
210 offset = 0;
211 if (GET_CODE (x) == SUBREG)
212 offset += subreg_regno_offset (hard_regno, GET_MODE (reg),
213 SUBREG_BYTE (x), GET_MODE (x));
214 return get_final_hard_regno (hard_regno, offset);
217 /* If REGNO is a hard register or has been allocated a hard register,
218 return the class of that register. If REGNO is a reload pseudo
219 created by the current constraints pass, return its allocno class.
220 Return NO_REGS otherwise. */
221 static enum reg_class
222 get_reg_class (int regno)
224 int hard_regno;
226 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
227 hard_regno = lra_get_regno_hard_regno (regno);
228 if (hard_regno >= 0)
230 hard_regno = get_final_hard_regno (hard_regno, 0);
231 return REGNO_REG_CLASS (hard_regno);
233 if (regno >= new_regno_start)
234 return lra_get_allocno_class (regno);
235 return NO_REGS;
238 /* Return true if REG satisfies (or will satisfy) reg class constraint
239 CL. Use elimination first if REG is a hard register. If REG is a
240 reload pseudo created by this constraints pass, assume that it will
241 be allocated a hard register from its allocno class, but allow that
242 class to be narrowed to CL if it is currently a superset of CL.
244 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
245 REGNO (reg), or NO_REGS if no change in its class was needed. */
246 static bool
247 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
249 enum reg_class rclass, common_class;
250 enum machine_mode reg_mode;
251 int class_size, hard_regno, nregs, i, j;
252 int regno = REGNO (reg);
254 if (new_class != NULL)
255 *new_class = NO_REGS;
256 if (regno < FIRST_PSEUDO_REGISTER)
258 rtx final_reg = reg;
259 rtx *final_loc = &final_reg;
261 lra_eliminate_reg_if_possible (final_loc);
262 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
264 reg_mode = GET_MODE (reg);
265 rclass = get_reg_class (regno);
266 if (regno < new_regno_start
267 /* Do not allow the constraints for reload instructions to
268 influence the classes of new pseudos. These reloads are
269 typically moves that have many alternatives, and restricting
270 reload pseudos for one alternative may lead to situations
271 where other reload pseudos are no longer allocatable. */
272 || (INSN_UID (curr_insn) >= new_insn_uid_start
273 && curr_insn_set != NULL
274 && ((OBJECT_P (SET_SRC (curr_insn_set))
275 && ! CONSTANT_P (SET_SRC (curr_insn_set)))
276 || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
277 && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))
278 && ! CONSTANT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
279 /* When we don't know what class will be used finally for reload
280 pseudos, we use ALL_REGS. */
281 return ((regno >= new_regno_start && rclass == ALL_REGS)
282 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
283 && ! hard_reg_set_subset_p (reg_class_contents[cl],
284 lra_no_alloc_regs)));
285 else
287 common_class = ira_reg_class_subset[rclass][cl];
288 if (new_class != NULL)
289 *new_class = common_class;
290 if (hard_reg_set_subset_p (reg_class_contents[common_class],
291 lra_no_alloc_regs))
292 return false;
293 /* Check that there are enough allocatable regs. */
294 class_size = ira_class_hard_regs_num[common_class];
295 for (i = 0; i < class_size; i++)
297 hard_regno = ira_class_hard_regs[common_class][i];
298 nregs = hard_regno_nregs[hard_regno][reg_mode];
299 if (nregs == 1)
300 return true;
301 for (j = 0; j < nregs; j++)
302 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
303 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
304 hard_regno + j))
305 break;
306 if (j >= nregs)
307 return true;
309 return false;
313 /* Return true if REGNO satisfies a memory constraint. */
314 static bool
315 in_mem_p (int regno)
317 return get_reg_class (regno) == NO_REGS;
320 /* Initiate equivalences for LRA. As we keep original equivalences
321 before any elimination, we need to make copies otherwise any change
322 in insns might change the equivalences. */
323 void
324 lra_init_equiv (void)
326 ira_expand_reg_equiv ();
327 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
329 rtx res;
331 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
332 ira_reg_equiv[i].memory = copy_rtx (res);
333 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
334 ira_reg_equiv[i].invariant = copy_rtx (res);
338 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
340 /* Update equivalence for REGNO. We need to this as the equivalence
341 might contain other pseudos which are changed by their
342 equivalences. */
343 static void
344 update_equiv (int regno)
346 rtx x;
348 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
349 ira_reg_equiv[regno].memory
350 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
351 NULL_RTX);
352 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
353 ira_reg_equiv[regno].invariant
354 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
355 NULL_RTX);
358 /* If we have decided to substitute X with another value, return that
359 value, otherwise return X. */
360 static rtx
361 get_equiv (rtx x)
363 int regno;
364 rtx res;
366 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
367 || ! ira_reg_equiv[regno].defined_p
368 || ! ira_reg_equiv[regno].profitable_p
369 || lra_get_regno_hard_regno (regno) >= 0)
370 return x;
371 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
372 return res;
373 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
374 return res;
375 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
376 return res;
377 gcc_unreachable ();
380 /* If we have decided to substitute X with the equivalent value,
381 return that value after elimination for INSN, otherwise return
382 X. */
383 static rtx
384 get_equiv_with_elimination (rtx x, rtx insn)
386 rtx res = get_equiv (x);
388 if (x == res || CONSTANT_P (res))
389 return res;
390 return lra_eliminate_regs_1 (insn, res, GET_MODE (res), false, false, true);
393 /* Set up curr_operand_mode. */
394 static void
395 init_curr_operand_mode (void)
397 int nop = curr_static_id->n_operands;
398 for (int i = 0; i < nop; i++)
400 enum machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
401 if (mode == VOIDmode)
403 /* The .md mode for address operands is the mode of the
404 addressed value rather than the mode of the address itself. */
405 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
406 mode = Pmode;
407 else
408 mode = curr_static_id->operand[i].mode;
410 curr_operand_mode[i] = mode;
416 /* The page contains code to reuse input reloads. */
418 /* Structure describes input reload of the current insns. */
419 struct input_reload
421 /* Reloaded value. */
422 rtx input;
423 /* Reload pseudo used. */
424 rtx reg;
427 /* The number of elements in the following array. */
428 static int curr_insn_input_reloads_num;
429 /* Array containing info about input reloads. It is used to find the
430 same input reload and reuse the reload pseudo in this case. */
431 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
433 /* Initiate data concerning reuse of input reloads for the current
434 insn. */
435 static void
436 init_curr_insn_input_reloads (void)
438 curr_insn_input_reloads_num = 0;
441 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
442 created input reload pseudo (only if TYPE is not OP_OUT). Don't
443 reuse pseudo if IN_SUBREG_P is true and the reused pseudo should be
444 wrapped up in SUBREG. The result pseudo is returned through
445 RESULT_REG. Return TRUE if we created a new pseudo, FALSE if we
446 reused the already created input reload pseudo. Use TITLE to
447 describe new registers for debug purposes. */
448 static bool
449 get_reload_reg (enum op_type type, enum machine_mode mode, rtx original,
450 enum reg_class rclass, bool in_subreg_p,
451 const char *title, rtx *result_reg)
453 int i, regno;
454 enum reg_class new_class;
456 if (type == OP_OUT)
458 *result_reg
459 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
460 return true;
462 /* Prevent reuse value of expression with side effects,
463 e.g. volatile memory. */
464 if (! side_effects_p (original))
465 for (i = 0; i < curr_insn_input_reloads_num; i++)
466 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
467 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
469 rtx reg = curr_insn_input_reloads[i].reg;
470 regno = REGNO (reg);
471 /* If input is equal to original and both are VOIDmode,
472 GET_MODE (reg) might be still different from mode.
473 Ensure we don't return *result_reg with wrong mode. */
474 if (GET_MODE (reg) != mode)
476 if (in_subreg_p)
477 continue;
478 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
479 continue;
480 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
481 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
482 continue;
484 *result_reg = reg;
485 if (lra_dump_file != NULL)
487 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
488 dump_value_slim (lra_dump_file, original, 1);
490 if (new_class != lra_get_allocno_class (regno))
491 lra_change_class (regno, new_class, ", change to", false);
492 if (lra_dump_file != NULL)
493 fprintf (lra_dump_file, "\n");
494 return false;
496 *result_reg = lra_create_new_reg (mode, original, rclass, title);
497 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
498 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
499 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
500 return true;
505 /* The page contains code to extract memory address parts. */
507 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
508 static inline bool
509 ok_for_index_p_nonstrict (rtx reg)
511 unsigned regno = REGNO (reg);
513 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
516 /* A version of regno_ok_for_base_p for use here, when all pseudos
517 should count as OK. Arguments as for regno_ok_for_base_p. */
518 static inline bool
519 ok_for_base_p_nonstrict (rtx reg, enum machine_mode mode, addr_space_t as,
520 enum rtx_code outer_code, enum rtx_code index_code)
522 unsigned regno = REGNO (reg);
524 if (regno >= FIRST_PSEUDO_REGISTER)
525 return true;
526 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
531 /* The page contains major code to choose the current insn alternative
532 and generate reloads for it. */
534 /* Return the offset from REGNO of the least significant register
535 in (reg:MODE REGNO).
537 This function is used to tell whether two registers satisfy
538 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
540 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
541 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
543 lra_constraint_offset (int regno, enum machine_mode mode)
545 lra_assert (regno < FIRST_PSEUDO_REGISTER);
546 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
547 && SCALAR_INT_MODE_P (mode))
548 return hard_regno_nregs[regno][mode] - 1;
549 return 0;
552 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
553 if they are the same hard reg, and has special hacks for
554 auto-increment and auto-decrement. This is specifically intended for
555 process_alt_operands to use in determining whether two operands
556 match. X is the operand whose number is the lower of the two.
558 It is supposed that X is the output operand and Y is the input
559 operand. Y_HARD_REGNO is the final hard regno of register Y or
560 register in subreg Y as we know it now. Otherwise, it is a
561 negative value. */
562 static bool
563 operands_match_p (rtx x, rtx y, int y_hard_regno)
565 int i;
566 RTX_CODE code = GET_CODE (x);
567 const char *fmt;
569 if (x == y)
570 return true;
571 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
572 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
574 int j;
576 i = get_hard_regno (x);
577 if (i < 0)
578 goto slow;
580 if ((j = y_hard_regno) < 0)
581 goto slow;
583 i += lra_constraint_offset (i, GET_MODE (x));
584 j += lra_constraint_offset (j, GET_MODE (y));
586 return i == j;
589 /* If two operands must match, because they are really a single
590 operand of an assembler insn, then two post-increments are invalid
591 because the assembler insn would increment only once. On the
592 other hand, a post-increment matches ordinary indexing if the
593 post-increment is the output operand. */
594 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
595 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
597 /* Two pre-increments are invalid because the assembler insn would
598 increment only once. On the other hand, a pre-increment matches
599 ordinary indexing if the pre-increment is the input operand. */
600 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
601 || GET_CODE (y) == PRE_MODIFY)
602 return operands_match_p (x, XEXP (y, 0), -1);
604 slow:
606 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
607 && x == SUBREG_REG (y))
608 return true;
609 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
610 && SUBREG_REG (x) == y)
611 return true;
613 /* Now we have disposed of all the cases in which different rtx
614 codes can match. */
615 if (code != GET_CODE (y))
616 return false;
618 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
619 if (GET_MODE (x) != GET_MODE (y))
620 return false;
622 switch (code)
624 CASE_CONST_UNIQUE:
625 return false;
627 case LABEL_REF:
628 return XEXP (x, 0) == XEXP (y, 0);
629 case SYMBOL_REF:
630 return XSTR (x, 0) == XSTR (y, 0);
632 default:
633 break;
636 /* Compare the elements. If any pair of corresponding elements fail
637 to match, return false for the whole things. */
639 fmt = GET_RTX_FORMAT (code);
640 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
642 int val, j;
643 switch (fmt[i])
645 case 'w':
646 if (XWINT (x, i) != XWINT (y, i))
647 return false;
648 break;
650 case 'i':
651 if (XINT (x, i) != XINT (y, i))
652 return false;
653 break;
655 case 'e':
656 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
657 if (val == 0)
658 return false;
659 break;
661 case '0':
662 break;
664 case 'E':
665 if (XVECLEN (x, i) != XVECLEN (y, i))
666 return false;
667 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
669 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
670 if (val == 0)
671 return false;
673 break;
675 /* It is believed that rtx's at this level will never
676 contain anything but integers and other rtx's, except for
677 within LABEL_REFs and SYMBOL_REFs. */
678 default:
679 gcc_unreachable ();
682 return true;
685 /* True if X is a constant that can be forced into the constant pool.
686 MODE is the mode of the operand, or VOIDmode if not known. */
687 #define CONST_POOL_OK_P(MODE, X) \
688 ((MODE) != VOIDmode \
689 && CONSTANT_P (X) \
690 && GET_CODE (X) != HIGH \
691 && !targetm.cannot_force_const_mem (MODE, X))
693 /* True if C is a non-empty register class that has too few registers
694 to be safely used as a reload target class. */
695 #define SMALL_REGISTER_CLASS_P(C) \
696 (ira_class_hard_regs_num [(C)] == 1 \
697 || (ira_class_hard_regs_num [(C)] >= 1 \
698 && targetm.class_likely_spilled_p (C)))
700 /* If REG is a reload pseudo, try to make its class satisfying CL. */
701 static void
702 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
704 enum reg_class rclass;
706 /* Do not make more accurate class from reloads generated. They are
707 mostly moves with a lot of constraints. Making more accurate
708 class may results in very narrow class and impossibility of find
709 registers for several reloads of one insn. */
710 if (INSN_UID (curr_insn) >= new_insn_uid_start)
711 return;
712 if (GET_CODE (reg) == SUBREG)
713 reg = SUBREG_REG (reg);
714 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
715 return;
716 if (in_class_p (reg, cl, &rclass) && rclass != cl)
717 lra_change_class (REGNO (reg), rclass, " Change to", true);
720 /* Generate reloads for matching OUT and INS (array of input operand
721 numbers with end marker -1) with reg class GOAL_CLASS. Add input
722 and output reloads correspondingly to the lists *BEFORE and *AFTER.
723 OUT might be negative. In this case we generate input reloads for
724 matched input operands INS. */
725 static void
726 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
727 rtx *before, rtx *after)
729 int i, in;
730 rtx new_in_reg, new_out_reg, reg, clobber;
731 enum machine_mode inmode, outmode;
732 rtx in_rtx = *curr_id->operand_loc[ins[0]];
733 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
735 inmode = curr_operand_mode[ins[0]];
736 outmode = out < 0 ? inmode : curr_operand_mode[out];
737 push_to_sequence (*before);
738 if (inmode != outmode)
740 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
742 reg = new_in_reg
743 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
744 goal_class, "");
745 if (SCALAR_INT_MODE_P (inmode))
746 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
747 else
748 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
749 LRA_SUBREG_P (new_out_reg) = 1;
750 /* If the input reg is dying here, we can use the same hard
751 register for REG and IN_RTX. We do it only for original
752 pseudos as reload pseudos can die although original
753 pseudos still live where reload pseudos dies. */
754 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
755 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
756 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
758 else
760 reg = new_out_reg
761 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
762 goal_class, "");
763 if (SCALAR_INT_MODE_P (outmode))
764 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
765 else
766 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
767 /* NEW_IN_REG is non-paradoxical subreg. We don't want
768 NEW_OUT_REG living above. We add clobber clause for
769 this. This is just a temporary clobber. We can remove
770 it at the end of LRA work. */
771 clobber = emit_clobber (new_out_reg);
772 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
773 LRA_SUBREG_P (new_in_reg) = 1;
774 if (GET_CODE (in_rtx) == SUBREG)
776 rtx subreg_reg = SUBREG_REG (in_rtx);
778 /* If SUBREG_REG is dying here and sub-registers IN_RTX
779 and NEW_IN_REG are similar, we can use the same hard
780 register for REG and SUBREG_REG. */
781 if (REG_P (subreg_reg)
782 && (int) REGNO (subreg_reg) < lra_new_regno_start
783 && GET_MODE (subreg_reg) == outmode
784 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
785 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
786 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
790 else
792 /* Pseudos have values -- see comments for lra_reg_info.
793 Different pseudos with the same value do not conflict even if
794 they live in the same place. When we create a pseudo we
795 assign value of original pseudo (if any) from which we
796 created the new pseudo. If we create the pseudo from the
797 input pseudo, the new pseudo will no conflict with the input
798 pseudo which is wrong when the input pseudo lives after the
799 insn and as the new pseudo value is changed by the insn
800 output. Therefore we create the new pseudo from the output.
802 We cannot reuse the current output register because we might
803 have a situation like "a <- a op b", where the constraints
804 force the second input operand ("b") to match the output
805 operand ("a"). "b" must then be copied into a new register
806 so that it doesn't clobber the current value of "a". */
808 new_in_reg = new_out_reg
809 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
810 goal_class, "");
812 /* In operand can be got from transformations before processing insn
813 constraints. One example of such transformations is subreg
814 reloading (see function simplify_operand_subreg). The new
815 pseudos created by the transformations might have inaccurate
816 class (ALL_REGS) and we should make their classes more
817 accurate. */
818 narrow_reload_pseudo_class (in_rtx, goal_class);
819 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
820 *before = get_insns ();
821 end_sequence ();
822 for (i = 0; (in = ins[i]) >= 0; i++)
824 lra_assert
825 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
826 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
827 *curr_id->operand_loc[in] = new_in_reg;
829 lra_update_dups (curr_id, ins);
830 if (out < 0)
831 return;
832 /* See a comment for the input operand above. */
833 narrow_reload_pseudo_class (out_rtx, goal_class);
834 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
836 start_sequence ();
837 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
838 emit_insn (*after);
839 *after = get_insns ();
840 end_sequence ();
842 *curr_id->operand_loc[out] = new_out_reg;
843 lra_update_dup (curr_id, out);
846 /* Return register class which is union of all reg classes in insn
847 constraint alternative string starting with P. */
848 static enum reg_class
849 reg_class_from_constraints (const char *p)
851 int c, len;
852 enum reg_class op_class = NO_REGS;
855 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
857 case '#':
858 case ',':
859 return op_class;
861 case 'p':
862 op_class = (reg_class_subunion
863 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
864 ADDRESS, SCRATCH)]);
865 break;
867 case 'g':
868 case 'r':
869 op_class = reg_class_subunion[op_class][GENERAL_REGS];
870 break;
872 default:
873 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
875 #ifdef EXTRA_CONSTRAINT_STR
876 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
877 op_class
878 = (reg_class_subunion
879 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
880 ADDRESS, SCRATCH)]);
881 #endif
882 break;
885 op_class
886 = reg_class_subunion[op_class][REG_CLASS_FROM_CONSTRAINT (c, p)];
887 break;
889 while ((p += len), c);
890 return op_class;
893 /* If OP is a register, return the class of the register as per
894 get_reg_class, otherwise return NO_REGS. */
895 static inline enum reg_class
896 get_op_class (rtx op)
898 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
901 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
902 otherwise. If modes of MEM_PSEUDO and VAL are different, use
903 SUBREG for VAL to make them equal. */
904 static rtx
905 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
907 if (GET_MODE (mem_pseudo) != GET_MODE (val))
909 /* Usually size of mem_pseudo is greater than val size but in
910 rare cases it can be less as it can be defined by target
911 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
912 if (! MEM_P (val))
914 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
915 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
917 LRA_SUBREG_P (val) = 1;
919 else
921 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
922 LRA_SUBREG_P (mem_pseudo) = 1;
925 return (to_p
926 ? gen_move_insn (mem_pseudo, val)
927 : gen_move_insn (val, mem_pseudo));
930 /* Process a special case insn (register move), return true if we
931 don't need to process it anymore. INSN should be a single set
932 insn. Set up that RTL was changed through CHANGE_P and macro
933 SECONDARY_MEMORY_NEEDED says to use secondary memory through
934 SEC_MEM_P. */
935 static bool
936 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
938 int sregno, dregno;
939 rtx dest, src, dreg, sreg, old_sreg, new_reg, before, scratch_reg;
940 enum reg_class dclass, sclass, secondary_class;
941 enum machine_mode sreg_mode;
942 secondary_reload_info sri;
944 lra_assert (curr_insn_set != NULL_RTX);
945 dreg = dest = SET_DEST (curr_insn_set);
946 sreg = src = SET_SRC (curr_insn_set);
947 if (GET_CODE (dest) == SUBREG)
948 dreg = SUBREG_REG (dest);
949 if (GET_CODE (src) == SUBREG)
950 sreg = SUBREG_REG (src);
951 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
952 return false;
953 sclass = dclass = NO_REGS;
954 if (REG_P (dreg))
955 dclass = get_reg_class (REGNO (dreg));
956 if (dclass == ALL_REGS)
957 /* ALL_REGS is used for new pseudos created by transformations
958 like reload of SUBREG_REG (see function
959 simplify_operand_subreg). We don't know their class yet. We
960 should figure out the class from processing the insn
961 constraints not in this fast path function. Even if ALL_REGS
962 were a right class for the pseudo, secondary_... hooks usually
963 are not define for ALL_REGS. */
964 return false;
965 sreg_mode = GET_MODE (sreg);
966 old_sreg = sreg;
967 if (REG_P (sreg))
968 sclass = get_reg_class (REGNO (sreg));
969 if (sclass == ALL_REGS)
970 /* See comments above. */
971 return false;
972 if (sclass == NO_REGS && dclass == NO_REGS)
973 return false;
974 #ifdef SECONDARY_MEMORY_NEEDED
975 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
976 #ifdef SECONDARY_MEMORY_NEEDED_MODE
977 && ((sclass != NO_REGS && dclass != NO_REGS)
978 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
979 #endif
982 *sec_mem_p = true;
983 return false;
985 #endif
986 if (! REG_P (dreg) || ! REG_P (sreg))
987 return false;
988 sri.prev_sri = NULL;
989 sri.icode = CODE_FOR_nothing;
990 sri.extra_cost = 0;
991 secondary_class = NO_REGS;
992 /* Set up hard register for a reload pseudo for hook
993 secondary_reload because some targets just ignore unassigned
994 pseudos in the hook. */
995 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
997 dregno = REGNO (dreg);
998 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1000 else
1001 dregno = -1;
1002 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1004 sregno = REGNO (sreg);
1005 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1007 else
1008 sregno = -1;
1009 if (sclass != NO_REGS)
1010 secondary_class
1011 = (enum reg_class) targetm.secondary_reload (false, dest,
1012 (reg_class_t) sclass,
1013 GET_MODE (src), &sri);
1014 if (sclass == NO_REGS
1015 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1016 && dclass != NO_REGS))
1018 enum reg_class old_sclass = secondary_class;
1019 secondary_reload_info old_sri = sri;
1021 sri.prev_sri = NULL;
1022 sri.icode = CODE_FOR_nothing;
1023 sri.extra_cost = 0;
1024 secondary_class
1025 = (enum reg_class) targetm.secondary_reload (true, sreg,
1026 (reg_class_t) dclass,
1027 sreg_mode, &sri);
1028 /* Check the target hook consistency. */
1029 lra_assert
1030 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1031 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1032 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1034 if (sregno >= 0)
1035 reg_renumber [sregno] = -1;
1036 if (dregno >= 0)
1037 reg_renumber [dregno] = -1;
1038 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1039 return false;
1040 *change_p = true;
1041 new_reg = NULL_RTX;
1042 if (secondary_class != NO_REGS)
1043 new_reg = lra_create_new_reg_with_unique_value (sreg_mode, NULL_RTX,
1044 secondary_class,
1045 "secondary");
1046 start_sequence ();
1047 if (old_sreg != sreg)
1048 sreg = copy_rtx (sreg);
1049 if (sri.icode == CODE_FOR_nothing)
1050 lra_emit_move (new_reg, sreg);
1051 else
1053 enum reg_class scratch_class;
1055 scratch_class = (reg_class_from_constraints
1056 (insn_data[sri.icode].operand[2].constraint));
1057 scratch_reg = (lra_create_new_reg_with_unique_value
1058 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1059 scratch_class, "scratch"));
1060 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1061 sreg, scratch_reg));
1063 before = get_insns ();
1064 end_sequence ();
1065 lra_process_new_insns (curr_insn, before, NULL_RTX, "Inserting the move");
1066 if (new_reg != NULL_RTX)
1068 if (GET_CODE (src) == SUBREG)
1069 SUBREG_REG (src) = new_reg;
1070 else
1071 SET_SRC (curr_insn_set) = new_reg;
1073 else
1075 if (lra_dump_file != NULL)
1077 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1078 dump_insn_slim (lra_dump_file, curr_insn);
1080 lra_set_insn_deleted (curr_insn);
1081 return true;
1083 return false;
1086 /* The following data describe the result of process_alt_operands.
1087 The data are used in curr_insn_transform to generate reloads. */
1089 /* The chosen reg classes which should be used for the corresponding
1090 operands. */
1091 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1092 /* True if the operand should be the same as another operand and that
1093 other operand does not need a reload. */
1094 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1095 /* True if the operand does not need a reload. */
1096 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1097 /* True if the operand can be offsetable memory. */
1098 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1099 /* The number of an operand to which given operand can be matched to. */
1100 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1101 /* The number of elements in the following array. */
1102 static int goal_alt_dont_inherit_ops_num;
1103 /* Numbers of operands whose reload pseudos should not be inherited. */
1104 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1105 /* True if the insn commutative operands should be swapped. */
1106 static bool goal_alt_swapped;
1107 /* The chosen insn alternative. */
1108 static int goal_alt_number;
1110 /* The following five variables are used to choose the best insn
1111 alternative. They reflect final characteristics of the best
1112 alternative. */
1114 /* Number of necessary reloads and overall cost reflecting the
1115 previous value and other unpleasantness of the best alternative. */
1116 static int best_losers, best_overall;
1117 /* Overall number hard registers used for reloads. For example, on
1118 some targets we need 2 general registers to reload DFmode and only
1119 one floating point register. */
1120 static int best_reload_nregs;
1121 /* Overall number reflecting distances of previous reloading the same
1122 value. The distances are counted from the current BB start. It is
1123 used to improve inheritance chances. */
1124 static int best_reload_sum;
1126 /* True if the current insn should have no correspondingly input or
1127 output reloads. */
1128 static bool no_input_reloads_p, no_output_reloads_p;
1130 /* True if we swapped the commutative operands in the current
1131 insn. */
1132 static int curr_swapped;
1134 /* Arrange for address element *LOC to be a register of class CL.
1135 Add any input reloads to list BEFORE. AFTER is nonnull if *LOC is an
1136 automodified value; handle that case by adding the required output
1137 reloads to list AFTER. Return true if the RTL was changed. */
1138 static bool
1139 process_addr_reg (rtx *loc, rtx *before, rtx *after, enum reg_class cl)
1141 int regno;
1142 enum reg_class rclass, new_class;
1143 rtx reg;
1144 rtx new_reg;
1145 enum machine_mode mode;
1146 bool subreg_p, before_p = false;
1148 subreg_p = GET_CODE (*loc) == SUBREG;
1149 if (subreg_p)
1150 loc = &SUBREG_REG (*loc);
1151 reg = *loc;
1152 mode = GET_MODE (reg);
1153 if (! REG_P (reg))
1155 /* Always reload memory in an address even if the target supports
1156 such addresses. */
1157 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1158 before_p = true;
1160 else
1162 regno = REGNO (reg);
1163 rclass = get_reg_class (regno);
1164 if ((*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1166 if (lra_dump_file != NULL)
1168 fprintf (lra_dump_file,
1169 "Changing pseudo %d in address of insn %u on equiv ",
1170 REGNO (reg), INSN_UID (curr_insn));
1171 dump_value_slim (lra_dump_file, *loc, 1);
1172 fprintf (lra_dump_file, "\n");
1174 *loc = copy_rtx (*loc);
1176 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1178 reg = *loc;
1179 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1180 mode, reg, cl, subreg_p, "address", &new_reg))
1181 before_p = true;
1183 else if (new_class != NO_REGS && rclass != new_class)
1185 lra_change_class (regno, new_class, " Change to", true);
1186 return false;
1188 else
1189 return false;
1191 if (before_p)
1193 push_to_sequence (*before);
1194 lra_emit_move (new_reg, reg);
1195 *before = get_insns ();
1196 end_sequence ();
1198 *loc = new_reg;
1199 if (after != NULL)
1201 start_sequence ();
1202 lra_emit_move (reg, new_reg);
1203 emit_insn (*after);
1204 *after = get_insns ();
1205 end_sequence ();
1207 return true;
1210 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1211 the insn to be inserted before curr insn. AFTER returns the
1212 the insn to be inserted after curr insn. ORIGREG and NEWREG
1213 are the original reg and new reg for reload. */
1214 static void
1215 insert_move_for_subreg (rtx *before, rtx *after, rtx origreg, rtx newreg)
1217 if (before)
1219 push_to_sequence (*before);
1220 lra_emit_move (newreg, origreg);
1221 *before = get_insns ();
1222 end_sequence ();
1224 if (after)
1226 start_sequence ();
1227 lra_emit_move (origreg, newreg);
1228 emit_insn (*after);
1229 *after = get_insns ();
1230 end_sequence ();
1234 static int valid_address_p (enum machine_mode mode, rtx addr, addr_space_t as);
1236 /* Make reloads for subreg in operand NOP with internal subreg mode
1237 REG_MODE, add new reloads for further processing. Return true if
1238 any reload was generated. */
1239 static bool
1240 simplify_operand_subreg (int nop, enum machine_mode reg_mode)
1242 int hard_regno;
1243 rtx before, after;
1244 enum machine_mode mode;
1245 rtx reg, new_reg;
1246 rtx operand = *curr_id->operand_loc[nop];
1247 enum reg_class regclass;
1248 enum op_type type;
1250 before = after = NULL_RTX;
1252 if (GET_CODE (operand) != SUBREG)
1253 return false;
1255 mode = GET_MODE (operand);
1256 reg = SUBREG_REG (operand);
1257 type = curr_static_id->operand[nop].type;
1258 /* If we change address for paradoxical subreg of memory, the
1259 address might violate the necessary alignment or the access might
1260 be slow. So take this into consideration. We should not worry
1261 about access beyond allocated memory for paradoxical memory
1262 subregs as we don't substitute such equiv memory (see processing
1263 equivalences in function lra_constraints) and because for spilled
1264 pseudos we allocate stack memory enough for the biggest
1265 corresponding paradoxical subreg. */
1266 if (MEM_P (reg)
1267 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1268 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1270 rtx subst, old = *curr_id->operand_loc[nop];
1272 alter_subreg (curr_id->operand_loc[nop], false);
1273 subst = *curr_id->operand_loc[nop];
1274 lra_assert (MEM_P (subst));
1275 if (! valid_address_p (GET_MODE (reg), XEXP (reg, 0),
1276 MEM_ADDR_SPACE (reg))
1277 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1278 MEM_ADDR_SPACE (subst)))
1279 return true;
1280 /* If the address was valid and became invalid, prefer to reload
1281 the memory. Typical case is when the index scale should
1282 correspond the memory. */
1283 *curr_id->operand_loc[nop] = old;
1285 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1287 alter_subreg (curr_id->operand_loc[nop], false);
1288 return true;
1290 /* Put constant into memory when we have mixed modes. It generates
1291 a better code in most cases as it does not need a secondary
1292 reload memory. It also prevents LRA looping when LRA is using
1293 secondary reload memory again and again. */
1294 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1295 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1297 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1298 alter_subreg (curr_id->operand_loc[nop], false);
1299 return true;
1301 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1302 if there may be a problem accessing OPERAND in the outer
1303 mode. */
1304 if ((REG_P (reg)
1305 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1306 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1307 /* Don't reload paradoxical subregs because we could be looping
1308 having repeatedly final regno out of hard regs range. */
1309 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1310 >= hard_regno_nregs[hard_regno][mode])
1311 && simplify_subreg_regno (hard_regno, GET_MODE (reg),
1312 SUBREG_BYTE (operand), mode) < 0
1313 /* Don't reload subreg for matching reload. It is actually
1314 valid subreg in LRA. */
1315 && ! LRA_SUBREG_P (operand))
1316 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1318 enum reg_class rclass;
1320 if (REG_P (reg))
1321 /* There is a big probability that we will get the same class
1322 for the new pseudo and we will get the same insn which
1323 means infinite looping. So spill the new pseudo. */
1324 rclass = NO_REGS;
1325 else
1326 /* The class will be defined later in curr_insn_transform. */
1327 rclass
1328 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1330 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1331 rclass, TRUE, "subreg reg", &new_reg))
1333 bool insert_before, insert_after;
1334 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1336 insert_before = (type != OP_OUT
1337 || GET_MODE_SIZE (GET_MODE (reg)) > GET_MODE_SIZE (mode));
1338 insert_after = (type != OP_IN);
1339 insert_move_for_subreg (insert_before ? &before : NULL,
1340 insert_after ? &after : NULL,
1341 reg, new_reg);
1343 SUBREG_REG (operand) = new_reg;
1344 lra_process_new_insns (curr_insn, before, after,
1345 "Inserting subreg reload");
1346 return true;
1348 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1349 IRA allocates hardreg to the inner pseudo reg according to its mode
1350 instead of the outermode, so the size of the hardreg may not be enough
1351 to contain the outermode operand, in that case we may need to insert
1352 reload for the reg. For the following two types of paradoxical subreg,
1353 we need to insert reload:
1354 1. If the op_type is OP_IN, and the hardreg could not be paired with
1355 other hardreg to contain the outermode operand
1356 (checked by in_hard_reg_set_p), we need to insert the reload.
1357 2. If the op_type is OP_OUT or OP_INOUT.
1359 Here is a paradoxical subreg example showing how the reload is generated:
1361 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1362 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1364 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1365 here, if reg107 is assigned to hardreg R15, because R15 is the last
1366 hardreg, compiler cannot find another hardreg to pair with R15 to
1367 contain TImode data. So we insert a TImode reload reg180 for it.
1368 After reload is inserted:
1370 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1371 (reg:DI 107 [ __comp ])) -1
1372 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1373 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1375 Two reload hard registers will be allocated to reg180 to save TImode data
1376 in LRA_assign. */
1377 else if (REG_P (reg)
1378 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1379 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1380 && (hard_regno_nregs[hard_regno][GET_MODE (reg)]
1381 < hard_regno_nregs[hard_regno][mode])
1382 && (regclass = lra_get_allocno_class (REGNO (reg)))
1383 && (type != OP_IN
1384 || !in_hard_reg_set_p (reg_class_contents[regclass],
1385 mode, hard_regno)))
1387 /* The class will be defined later in curr_insn_transform. */
1388 enum reg_class rclass
1389 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1391 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1392 rclass, TRUE, "paradoxical subreg", &new_reg))
1394 rtx subreg;
1395 bool insert_before, insert_after;
1397 PUT_MODE (new_reg, mode);
1398 subreg = simplify_gen_subreg (GET_MODE (reg), new_reg, mode, 0);
1399 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1401 insert_before = (type != OP_OUT);
1402 insert_after = (type != OP_IN);
1403 insert_move_for_subreg (insert_before ? &before : NULL,
1404 insert_after ? &after : NULL,
1405 reg, subreg);
1407 SUBREG_REG (operand) = new_reg;
1408 lra_process_new_insns (curr_insn, before, after,
1409 "Inserting paradoxical subreg reload");
1410 return true;
1412 return false;
1415 /* Return TRUE if X refers for a hard register from SET. */
1416 static bool
1417 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1419 int i, j, x_hard_regno;
1420 enum machine_mode mode;
1421 const char *fmt;
1422 enum rtx_code code;
1424 if (x == NULL_RTX)
1425 return false;
1426 code = GET_CODE (x);
1427 mode = GET_MODE (x);
1428 if (code == SUBREG)
1430 x = SUBREG_REG (x);
1431 code = GET_CODE (x);
1432 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1433 mode = GET_MODE (x);
1436 if (REG_P (x))
1438 x_hard_regno = get_hard_regno (x);
1439 return (x_hard_regno >= 0
1440 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1442 if (MEM_P (x))
1444 struct address_info ad;
1446 decompose_mem_address (&ad, x);
1447 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1448 return true;
1449 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1450 return true;
1452 fmt = GET_RTX_FORMAT (code);
1453 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1455 if (fmt[i] == 'e')
1457 if (uses_hard_regs_p (XEXP (x, i), set))
1458 return true;
1460 else if (fmt[i] == 'E')
1462 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1463 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1464 return true;
1467 return false;
1470 /* Return true if OP is a spilled pseudo. */
1471 static inline bool
1472 spilled_pseudo_p (rtx op)
1474 return (REG_P (op)
1475 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1478 /* Return true if X is a general constant. */
1479 static inline bool
1480 general_constant_p (rtx x)
1482 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1485 static bool
1486 reg_in_class_p (rtx reg, enum reg_class cl)
1488 if (cl == NO_REGS)
1489 return get_reg_class (REGNO (reg)) == NO_REGS;
1490 return in_class_p (reg, cl, NULL);
1493 /* Major function to choose the current insn alternative and what
1494 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1495 negative we should consider only this alternative. Return false if
1496 we can not choose the alternative or find how to reload the
1497 operands. */
1498 static bool
1499 process_alt_operands (int only_alternative)
1501 bool ok_p = false;
1502 int nop, overall, nalt;
1503 int n_alternatives = curr_static_id->n_alternatives;
1504 int n_operands = curr_static_id->n_operands;
1505 /* LOSERS counts the operands that don't fit this alternative and
1506 would require loading. */
1507 int losers;
1508 /* REJECT is a count of how undesirable this alternative says it is
1509 if any reloading is required. If the alternative matches exactly
1510 then REJECT is ignored, but otherwise it gets this much counted
1511 against it in addition to the reloading needed. */
1512 int reject;
1513 /* The number of elements in the following array. */
1514 int early_clobbered_regs_num;
1515 /* Numbers of operands which are early clobber registers. */
1516 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1517 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1518 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1519 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1520 bool curr_alt_win[MAX_RECOG_OPERANDS];
1521 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1522 int curr_alt_matches[MAX_RECOG_OPERANDS];
1523 /* The number of elements in the following array. */
1524 int curr_alt_dont_inherit_ops_num;
1525 /* Numbers of operands whose reload pseudos should not be inherited. */
1526 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1527 rtx op;
1528 /* The register when the operand is a subreg of register, otherwise the
1529 operand itself. */
1530 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1531 /* The register if the operand is a register or subreg of register,
1532 otherwise NULL. */
1533 rtx operand_reg[MAX_RECOG_OPERANDS];
1534 int hard_regno[MAX_RECOG_OPERANDS];
1535 enum machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1536 int reload_nregs, reload_sum;
1537 bool costly_p;
1538 enum reg_class cl;
1540 /* Calculate some data common for all alternatives to speed up the
1541 function. */
1542 for (nop = 0; nop < n_operands; nop++)
1544 rtx reg;
1546 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1547 /* The real hard regno of the operand after the allocation. */
1548 hard_regno[nop] = get_hard_regno (op);
1550 operand_reg[nop] = reg = op;
1551 biggest_mode[nop] = GET_MODE (op);
1552 if (GET_CODE (op) == SUBREG)
1554 operand_reg[nop] = reg = SUBREG_REG (op);
1555 if (GET_MODE_SIZE (biggest_mode[nop])
1556 < GET_MODE_SIZE (GET_MODE (reg)))
1557 biggest_mode[nop] = GET_MODE (reg);
1559 if (! REG_P (reg))
1560 operand_reg[nop] = NULL_RTX;
1561 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1562 || ((int) REGNO (reg)
1563 == lra_get_elimination_hard_regno (REGNO (reg))))
1564 no_subreg_reg_operand[nop] = reg;
1565 else
1566 operand_reg[nop] = no_subreg_reg_operand[nop]
1567 /* Just use natural mode for elimination result. It should
1568 be enough for extra constraints hooks. */
1569 = regno_reg_rtx[hard_regno[nop]];
1572 /* The constraints are made of several alternatives. Each operand's
1573 constraint looks like foo,bar,... with commas separating the
1574 alternatives. The first alternatives for all operands go
1575 together, the second alternatives go together, etc.
1577 First loop over alternatives. */
1578 for (nalt = 0; nalt < n_alternatives; nalt++)
1580 /* Loop over operands for one constraint alternative. */
1581 #if HAVE_ATTR_enabled
1582 if (curr_id->alternative_enabled_p != NULL
1583 && ! curr_id->alternative_enabled_p[nalt])
1584 continue;
1585 #endif
1587 if (only_alternative >= 0 && nalt != only_alternative)
1588 continue;
1591 overall = losers = reject = reload_nregs = reload_sum = 0;
1592 for (nop = 0; nop < n_operands; nop++)
1594 int inc = (curr_static_id
1595 ->operand_alternative[nalt * n_operands + nop].reject);
1596 if (lra_dump_file != NULL && inc != 0)
1597 fprintf (lra_dump_file,
1598 " Staticly defined alt reject+=%d\n", inc);
1599 reject += inc;
1601 early_clobbered_regs_num = 0;
1603 for (nop = 0; nop < n_operands; nop++)
1605 const char *p;
1606 char *end;
1607 int len, c, m, i, opalt_num, this_alternative_matches;
1608 bool win, did_match, offmemok, early_clobber_p;
1609 /* false => this operand can be reloaded somehow for this
1610 alternative. */
1611 bool badop;
1612 /* true => this operand can be reloaded if the alternative
1613 allows regs. */
1614 bool winreg;
1615 /* True if a constant forced into memory would be OK for
1616 this operand. */
1617 bool constmemok;
1618 enum reg_class this_alternative, this_costly_alternative;
1619 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1620 bool this_alternative_match_win, this_alternative_win;
1621 bool this_alternative_offmemok;
1622 bool scratch_p;
1623 enum machine_mode mode;
1625 opalt_num = nalt * n_operands + nop;
1626 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1628 /* Fast track for no constraints at all. */
1629 curr_alt[nop] = NO_REGS;
1630 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1631 curr_alt_win[nop] = true;
1632 curr_alt_match_win[nop] = false;
1633 curr_alt_offmemok[nop] = false;
1634 curr_alt_matches[nop] = -1;
1635 continue;
1638 op = no_subreg_reg_operand[nop];
1639 mode = curr_operand_mode[nop];
1641 win = did_match = winreg = offmemok = constmemok = false;
1642 badop = true;
1644 early_clobber_p = false;
1645 p = curr_static_id->operand_alternative[opalt_num].constraint;
1647 this_costly_alternative = this_alternative = NO_REGS;
1648 /* We update set of possible hard regs besides its class
1649 because reg class might be inaccurate. For example,
1650 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1651 is translated in HI_REGS because classes are merged by
1652 pairs and there is no accurate intermediate class. */
1653 CLEAR_HARD_REG_SET (this_alternative_set);
1654 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1655 this_alternative_win = false;
1656 this_alternative_match_win = false;
1657 this_alternative_offmemok = false;
1658 this_alternative_matches = -1;
1660 /* An empty constraint should be excluded by the fast
1661 track. */
1662 lra_assert (*p != 0 && *p != ',');
1664 /* Scan this alternative's specs for this operand; set WIN
1665 if the operand fits any letter in this alternative.
1666 Otherwise, clear BADOP if this operand could fit some
1667 letter after reloads, or set WINREG if this operand could
1668 fit after reloads provided the constraint allows some
1669 registers. */
1670 costly_p = false;
1673 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1675 case '\0':
1676 len = 0;
1677 break;
1678 case ',':
1679 c = '\0';
1680 break;
1682 case '=': case '+': case '?': case '*': case '!':
1683 case ' ': case '\t':
1684 break;
1686 case '%':
1687 /* We only support one commutative marker, the first
1688 one. We already set commutative above. */
1689 break;
1691 case '&':
1692 early_clobber_p = true;
1693 break;
1695 case '#':
1696 /* Ignore rest of this alternative. */
1697 c = '\0';
1698 break;
1700 case '0': case '1': case '2': case '3': case '4':
1701 case '5': case '6': case '7': case '8': case '9':
1703 int m_hregno;
1704 bool match_p;
1706 m = strtoul (p, &end, 10);
1707 p = end;
1708 len = 0;
1709 lra_assert (nop > m);
1711 this_alternative_matches = m;
1712 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1713 /* We are supposed to match a previous operand.
1714 If we do, we win if that one did. If we do
1715 not, count both of the operands as losers.
1716 (This is too conservative, since most of the
1717 time only a single reload insn will be needed
1718 to make the two operands win. As a result,
1719 this alternative may be rejected when it is
1720 actually desirable.) */
1721 match_p = false;
1722 if (operands_match_p (*curr_id->operand_loc[nop],
1723 *curr_id->operand_loc[m], m_hregno))
1725 /* We should reject matching of an early
1726 clobber operand if the matching operand is
1727 not dying in the insn. */
1728 if (! curr_static_id->operand[m].early_clobber
1729 || operand_reg[nop] == NULL_RTX
1730 || (find_regno_note (curr_insn, REG_DEAD,
1731 REGNO (op))
1732 || REGNO (op) == REGNO (operand_reg[m])))
1733 match_p = true;
1735 if (match_p)
1737 /* If we are matching a non-offsettable
1738 address where an offsettable address was
1739 expected, then we must reject this
1740 combination, because we can't reload
1741 it. */
1742 if (curr_alt_offmemok[m]
1743 && MEM_P (*curr_id->operand_loc[m])
1744 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1745 continue;
1747 else
1749 /* Operands don't match. Both operands must
1750 allow a reload register, otherwise we
1751 cannot make them match. */
1752 if (curr_alt[m] == NO_REGS)
1753 break;
1754 /* Retroactively mark the operand we had to
1755 match as a loser, if it wasn't already and
1756 it wasn't matched to a register constraint
1757 (e.g it might be matched by memory). */
1758 if (curr_alt_win[m]
1759 && (operand_reg[m] == NULL_RTX
1760 || hard_regno[m] < 0))
1762 losers++;
1763 reload_nregs
1764 += (ira_reg_class_max_nregs[curr_alt[m]]
1765 [GET_MODE (*curr_id->operand_loc[m])]);
1768 /* Prefer matching earlyclobber alternative as
1769 it results in less hard regs required for
1770 the insn than a non-matching earlyclobber
1771 alternative. */
1772 if (curr_static_id->operand[m].early_clobber)
1774 if (lra_dump_file != NULL)
1775 fprintf
1776 (lra_dump_file,
1777 " %d Matching earlyclobber alt:"
1778 " reject--\n",
1779 nop);
1780 reject--;
1782 /* Otherwise we prefer no matching
1783 alternatives because it gives more freedom
1784 in RA. */
1785 else if (operand_reg[nop] == NULL_RTX
1786 || (find_regno_note (curr_insn, REG_DEAD,
1787 REGNO (operand_reg[nop]))
1788 == NULL_RTX))
1790 if (lra_dump_file != NULL)
1791 fprintf
1792 (lra_dump_file,
1793 " %d Matching alt: reject+=2\n",
1794 nop);
1795 reject += 2;
1798 /* If we have to reload this operand and some
1799 previous operand also had to match the same
1800 thing as this operand, we don't know how to do
1801 that. */
1802 if (!match_p || !curr_alt_win[m])
1804 for (i = 0; i < nop; i++)
1805 if (curr_alt_matches[i] == m)
1806 break;
1807 if (i < nop)
1808 break;
1810 else
1811 did_match = true;
1813 /* This can be fixed with reloads if the operand
1814 we are supposed to match can be fixed with
1815 reloads. */
1816 badop = false;
1817 this_alternative = curr_alt[m];
1818 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1819 winreg = this_alternative != NO_REGS;
1820 break;
1823 case 'p':
1824 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1825 ADDRESS, SCRATCH);
1826 this_alternative = reg_class_subunion[this_alternative][cl];
1827 IOR_HARD_REG_SET (this_alternative_set,
1828 reg_class_contents[cl]);
1829 if (costly_p)
1831 this_costly_alternative
1832 = reg_class_subunion[this_costly_alternative][cl];
1833 IOR_HARD_REG_SET (this_costly_alternative_set,
1834 reg_class_contents[cl]);
1836 win = true;
1837 badop = false;
1838 break;
1840 case TARGET_MEM_CONSTRAINT:
1841 if (MEM_P (op) || spilled_pseudo_p (op))
1842 win = true;
1843 /* We can put constant or pseudo value into memory
1844 to satisfy the constraint. */
1845 if (CONST_POOL_OK_P (mode, op) || REG_P (op))
1846 badop = false;
1847 constmemok = true;
1848 break;
1850 case '<':
1851 if (MEM_P (op)
1852 && (GET_CODE (XEXP (op, 0)) == PRE_DEC
1853 || GET_CODE (XEXP (op, 0)) == POST_DEC))
1854 win = true;
1855 break;
1857 case '>':
1858 if (MEM_P (op)
1859 && (GET_CODE (XEXP (op, 0)) == PRE_INC
1860 || GET_CODE (XEXP (op, 0)) == POST_INC))
1861 win = true;
1862 break;
1864 /* Memory op whose address is not offsettable. */
1865 case 'V':
1866 if (MEM_P (op)
1867 && ! offsettable_nonstrict_memref_p (op))
1868 win = true;
1869 break;
1871 /* Memory operand whose address is offsettable. */
1872 case 'o':
1873 if ((MEM_P (op)
1874 && offsettable_nonstrict_memref_p (op))
1875 || spilled_pseudo_p (op))
1876 win = true;
1877 /* We can put constant or pseudo value into memory
1878 or make memory address offsetable to satisfy the
1879 constraint. */
1880 if (CONST_POOL_OK_P (mode, op) || MEM_P (op) || REG_P (op))
1881 badop = false;
1882 constmemok = true;
1883 offmemok = true;
1884 break;
1886 case 'E':
1887 case 'F':
1888 if (GET_CODE (op) == CONST_DOUBLE
1889 || (GET_CODE (op) == CONST_VECTOR
1890 && (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)))
1891 win = true;
1892 break;
1894 case 'G':
1895 case 'H':
1896 if (CONST_DOUBLE_AS_FLOAT_P (op)
1897 && CONST_DOUBLE_OK_FOR_CONSTRAINT_P (op, c, p))
1898 win = true;
1899 break;
1901 case 's':
1902 if (CONST_SCALAR_INT_P (op))
1903 break;
1905 case 'i':
1906 if (general_constant_p (op))
1907 win = true;
1908 break;
1910 case 'n':
1911 if (CONST_SCALAR_INT_P (op))
1912 win = true;
1913 break;
1915 case 'I':
1916 case 'J':
1917 case 'K':
1918 case 'L':
1919 case 'M':
1920 case 'N':
1921 case 'O':
1922 case 'P':
1923 if (CONST_INT_P (op)
1924 && CONST_OK_FOR_CONSTRAINT_P (INTVAL (op), c, p))
1925 win = true;
1926 break;
1928 case 'X':
1929 /* This constraint should be excluded by the fast
1930 track. */
1931 gcc_unreachable ();
1932 break;
1934 case 'g':
1935 if (MEM_P (op)
1936 || general_constant_p (op)
1937 || spilled_pseudo_p (op))
1938 win = true;
1939 /* Drop through into 'r' case. */
1941 case 'r':
1942 this_alternative
1943 = reg_class_subunion[this_alternative][GENERAL_REGS];
1944 IOR_HARD_REG_SET (this_alternative_set,
1945 reg_class_contents[GENERAL_REGS]);
1946 if (costly_p)
1948 this_costly_alternative
1949 = (reg_class_subunion
1950 [this_costly_alternative][GENERAL_REGS]);
1951 IOR_HARD_REG_SET (this_costly_alternative_set,
1952 reg_class_contents[GENERAL_REGS]);
1954 goto reg;
1956 default:
1957 if (REG_CLASS_FROM_CONSTRAINT (c, p) == NO_REGS)
1959 #ifdef EXTRA_CONSTRAINT_STR
1960 if (EXTRA_MEMORY_CONSTRAINT (c, p))
1962 if (EXTRA_CONSTRAINT_STR (op, c, p))
1963 win = true;
1964 else if (spilled_pseudo_p (op))
1965 win = true;
1967 /* If we didn't already win, we can reload
1968 constants via force_const_mem or put the
1969 pseudo value into memory, or make other
1970 memory by reloading the address like for
1971 'o'. */
1972 if (CONST_POOL_OK_P (mode, op)
1973 || MEM_P (op) || REG_P (op))
1974 badop = false;
1975 constmemok = true;
1976 offmemok = true;
1977 break;
1979 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1981 if (EXTRA_CONSTRAINT_STR (op, c, p))
1982 win = true;
1984 /* If we didn't already win, we can reload
1985 the address into a base register. */
1986 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1987 ADDRESS, SCRATCH);
1988 this_alternative
1989 = reg_class_subunion[this_alternative][cl];
1990 IOR_HARD_REG_SET (this_alternative_set,
1991 reg_class_contents[cl]);
1992 if (costly_p)
1994 this_costly_alternative
1995 = (reg_class_subunion
1996 [this_costly_alternative][cl]);
1997 IOR_HARD_REG_SET (this_costly_alternative_set,
1998 reg_class_contents[cl]);
2000 badop = false;
2001 break;
2004 if (EXTRA_CONSTRAINT_STR (op, c, p))
2005 win = true;
2006 #endif
2007 break;
2010 cl = REG_CLASS_FROM_CONSTRAINT (c, p);
2011 this_alternative = reg_class_subunion[this_alternative][cl];
2012 IOR_HARD_REG_SET (this_alternative_set,
2013 reg_class_contents[cl]);
2014 if (costly_p)
2016 this_costly_alternative
2017 = reg_class_subunion[this_costly_alternative][cl];
2018 IOR_HARD_REG_SET (this_costly_alternative_set,
2019 reg_class_contents[cl]);
2021 reg:
2022 if (mode == BLKmode)
2023 break;
2024 winreg = true;
2025 if (REG_P (op))
2027 if (hard_regno[nop] >= 0
2028 && in_hard_reg_set_p (this_alternative_set,
2029 mode, hard_regno[nop]))
2030 win = true;
2031 else if (hard_regno[nop] < 0
2032 && in_class_p (op, this_alternative, NULL))
2033 win = true;
2035 break;
2037 if (c != ' ' && c != '\t')
2038 costly_p = c == '*';
2040 while ((p += len), c);
2042 scratch_p = (operand_reg[nop] != NULL_RTX
2043 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2044 /* Record which operands fit this alternative. */
2045 if (win)
2047 this_alternative_win = true;
2048 if (operand_reg[nop] != NULL_RTX)
2050 if (hard_regno[nop] >= 0)
2052 if (in_hard_reg_set_p (this_costly_alternative_set,
2053 mode, hard_regno[nop]))
2055 if (lra_dump_file != NULL)
2056 fprintf (lra_dump_file,
2057 " %d Costly set: reject++\n",
2058 nop);
2059 reject++;
2062 else
2064 /* Prefer won reg to spilled pseudo under other
2065 equal conditions for possibe inheritance. */
2066 if (! scratch_p)
2068 if (lra_dump_file != NULL)
2069 fprintf
2070 (lra_dump_file,
2071 " %d Non pseudo reload: reject++\n",
2072 nop);
2073 reject++;
2075 if (in_class_p (operand_reg[nop],
2076 this_costly_alternative, NULL))
2078 if (lra_dump_file != NULL)
2079 fprintf
2080 (lra_dump_file,
2081 " %d Non pseudo costly reload:"
2082 " reject++\n",
2083 nop);
2084 reject++;
2087 /* We simulate the behaviour of old reload here.
2088 Although scratches need hard registers and it
2089 might result in spilling other pseudos, no reload
2090 insns are generated for the scratches. So it
2091 might cost something but probably less than old
2092 reload pass believes. */
2093 if (scratch_p)
2095 if (lra_dump_file != NULL)
2096 fprintf (lra_dump_file,
2097 " %d Scratch win: reject+=2\n",
2098 nop);
2099 reject += 2;
2103 else if (did_match)
2104 this_alternative_match_win = true;
2105 else
2107 int const_to_mem = 0;
2108 bool no_regs_p;
2110 /* Never do output reload of stack pointer. It makes
2111 impossible to do elimination when SP is changed in
2112 RTL. */
2113 if (op == stack_pointer_rtx && ! frame_pointer_needed
2114 && curr_static_id->operand[nop].type != OP_IN)
2115 goto fail;
2117 /* If this alternative asks for a specific reg class, see if there
2118 is at least one allocatable register in that class. */
2119 no_regs_p
2120 = (this_alternative == NO_REGS
2121 || (hard_reg_set_subset_p
2122 (reg_class_contents[this_alternative],
2123 lra_no_alloc_regs)));
2125 /* For asms, verify that the class for this alternative is possible
2126 for the mode that is specified. */
2127 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2129 int i;
2130 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2131 if (HARD_REGNO_MODE_OK (i, mode)
2132 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2133 mode, i))
2134 break;
2135 if (i == FIRST_PSEUDO_REGISTER)
2136 winreg = false;
2139 /* If this operand accepts a register, and if the
2140 register class has at least one allocatable register,
2141 then this operand can be reloaded. */
2142 if (winreg && !no_regs_p)
2143 badop = false;
2145 if (badop)
2147 if (lra_dump_file != NULL)
2148 fprintf (lra_dump_file,
2149 " alt=%d: Bad operand -- refuse\n",
2150 nalt);
2151 goto fail;
2154 /* If not assigned pseudo has a class which a subset of
2155 required reg class, it is a less costly alternative
2156 as the pseudo still can get a hard reg of necessary
2157 class. */
2158 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2159 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2160 && ira_class_subset_p[this_alternative][cl])
2162 if (lra_dump_file != NULL)
2163 fprintf
2164 (lra_dump_file,
2165 " %d Super set class reg: reject-=3\n", nop);
2166 reject -= 3;
2169 this_alternative_offmemok = offmemok;
2170 if (this_costly_alternative != NO_REGS)
2172 if (lra_dump_file != NULL)
2173 fprintf (lra_dump_file,
2174 " %d Costly loser: reject++\n", nop);
2175 reject++;
2177 /* If the operand is dying, has a matching constraint,
2178 and satisfies constraints of the matched operand
2179 which failed to satisfy the own constraints, most probably
2180 the reload for this operand will be gone. */
2181 if (this_alternative_matches >= 0
2182 && !curr_alt_win[this_alternative_matches]
2183 && REG_P (op)
2184 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2185 && (hard_regno[nop] >= 0
2186 ? in_hard_reg_set_p (this_alternative_set,
2187 mode, hard_regno[nop])
2188 : in_class_p (op, this_alternative, NULL)))
2190 if (lra_dump_file != NULL)
2191 fprintf
2192 (lra_dump_file,
2193 " %d Dying matched operand reload: reject++\n",
2194 nop);
2195 reject++;
2197 else
2199 /* Strict_low_part requires to reload the register
2200 not the sub-register. In this case we should
2201 check that a final reload hard reg can hold the
2202 value mode. */
2203 if (curr_static_id->operand[nop].strict_low
2204 && REG_P (op)
2205 && hard_regno[nop] < 0
2206 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2207 && ira_class_hard_regs_num[this_alternative] > 0
2208 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2209 [this_alternative][0],
2210 GET_MODE
2211 (*curr_id->operand_loc[nop])))
2213 if (lra_dump_file != NULL)
2214 fprintf
2215 (lra_dump_file,
2216 " alt=%d: Strict low subreg reload -- refuse\n",
2217 nalt);
2218 goto fail;
2220 losers++;
2222 if (operand_reg[nop] != NULL_RTX
2223 /* Output operands and matched input operands are
2224 not inherited. The following conditions do not
2225 exactly describe the previous statement but they
2226 are pretty close. */
2227 && curr_static_id->operand[nop].type != OP_OUT
2228 && (this_alternative_matches < 0
2229 || curr_static_id->operand[nop].type != OP_IN))
2231 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2232 (operand_reg[nop])]
2233 .last_reload);
2235 /* The value of reload_sum has sense only if we
2236 process insns in their order. It happens only on
2237 the first constraints sub-pass when we do most of
2238 reload work. */
2239 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2240 reload_sum += last_reload - bb_reload_num;
2242 /* If this is a constant that is reloaded into the
2243 desired class by copying it to memory first, count
2244 that as another reload. This is consistent with
2245 other code and is required to avoid choosing another
2246 alternative when the constant is moved into memory.
2247 Note that the test here is precisely the same as in
2248 the code below that calls force_const_mem. */
2249 if (CONST_POOL_OK_P (mode, op)
2250 && ((targetm.preferred_reload_class
2251 (op, this_alternative) == NO_REGS)
2252 || no_input_reloads_p))
2254 const_to_mem = 1;
2255 if (! no_regs_p)
2256 losers++;
2259 /* Alternative loses if it requires a type of reload not
2260 permitted for this insn. We can always reload
2261 objects with a REG_UNUSED note. */
2262 if ((curr_static_id->operand[nop].type != OP_IN
2263 && no_output_reloads_p
2264 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2265 || (curr_static_id->operand[nop].type != OP_OUT
2266 && no_input_reloads_p && ! const_to_mem)
2267 || (this_alternative_matches >= 0
2268 && (no_input_reloads_p
2269 || (no_output_reloads_p
2270 && (curr_static_id->operand
2271 [this_alternative_matches].type != OP_IN)
2272 && ! find_reg_note (curr_insn, REG_UNUSED,
2273 no_subreg_reg_operand
2274 [this_alternative_matches])))))
2276 if (lra_dump_file != NULL)
2277 fprintf
2278 (lra_dump_file,
2279 " alt=%d: No input/otput reload -- refuse\n",
2280 nalt);
2281 goto fail;
2284 /* Check strong discouragement of reload of non-constant
2285 into class THIS_ALTERNATIVE. */
2286 if (! CONSTANT_P (op) && ! no_regs_p
2287 && (targetm.preferred_reload_class
2288 (op, this_alternative) == NO_REGS
2289 || (curr_static_id->operand[nop].type == OP_OUT
2290 && (targetm.preferred_output_reload_class
2291 (op, this_alternative) == NO_REGS))))
2293 if (lra_dump_file != NULL)
2294 fprintf (lra_dump_file,
2295 " %d Non-prefered reload: reject+=%d\n",
2296 nop, LRA_MAX_REJECT);
2297 reject += LRA_MAX_REJECT;
2300 if (! (MEM_P (op) && offmemok)
2301 && ! (const_to_mem && constmemok))
2303 /* We prefer to reload pseudos over reloading other
2304 things, since such reloads may be able to be
2305 eliminated later. So bump REJECT in other cases.
2306 Don't do this in the case where we are forcing a
2307 constant into memory and it will then win since
2308 we don't want to have a different alternative
2309 match then. */
2310 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2312 if (lra_dump_file != NULL)
2313 fprintf
2314 (lra_dump_file,
2315 " %d Non-pseudo reload: reject+=2\n",
2316 nop);
2317 reject += 2;
2320 if (! no_regs_p)
2321 reload_nregs
2322 += ira_reg_class_max_nregs[this_alternative][mode];
2324 if (SMALL_REGISTER_CLASS_P (this_alternative))
2326 if (lra_dump_file != NULL)
2327 fprintf
2328 (lra_dump_file,
2329 " %d Small class reload: reject+=%d\n",
2330 nop, LRA_LOSER_COST_FACTOR / 2);
2331 reject += LRA_LOSER_COST_FACTOR / 2;
2335 /* We are trying to spill pseudo into memory. It is
2336 usually more costly than moving to a hard register
2337 although it might takes the same number of
2338 reloads. */
2339 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2341 if (lra_dump_file != NULL)
2342 fprintf
2343 (lra_dump_file,
2344 " %d Spill pseudo into memory: reject+=3\n",
2345 nop);
2346 reject += 3;
2347 if (VECTOR_MODE_P (mode))
2349 /* Spilling vectors into memory is usually more
2350 costly as they contain big values. */
2351 if (lra_dump_file != NULL)
2352 fprintf
2353 (lra_dump_file,
2354 " %d Spill vector pseudo: reject+=2\n",
2355 nop);
2356 reject += 2;
2360 #ifdef SECONDARY_MEMORY_NEEDED
2361 /* If reload requires moving value through secondary
2362 memory, it will need one more insn at least. */
2363 if (this_alternative != NO_REGS
2364 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2365 && ((curr_static_id->operand[nop].type != OP_OUT
2366 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2367 GET_MODE (op)))
2368 || (curr_static_id->operand[nop].type != OP_IN
2369 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2370 GET_MODE (op)))))
2371 losers++;
2372 #endif
2373 /* Input reloads can be inherited more often than output
2374 reloads can be removed, so penalize output
2375 reloads. */
2376 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2378 if (lra_dump_file != NULL)
2379 fprintf
2380 (lra_dump_file,
2381 " %d Non input pseudo reload: reject++\n",
2382 nop);
2383 reject++;
2387 if (early_clobber_p && ! scratch_p)
2389 if (lra_dump_file != NULL)
2390 fprintf (lra_dump_file,
2391 " %d Early clobber: reject++\n", nop);
2392 reject++;
2394 /* ??? We check early clobbers after processing all operands
2395 (see loop below) and there we update the costs more.
2396 Should we update the cost (may be approximately) here
2397 because of early clobber register reloads or it is a rare
2398 or non-important thing to be worth to do it. */
2399 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2400 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2402 if (lra_dump_file != NULL)
2403 fprintf (lra_dump_file,
2404 " alt=%d,overall=%d,losers=%d -- refuse\n",
2405 nalt, overall, losers);
2406 goto fail;
2409 curr_alt[nop] = this_alternative;
2410 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2411 curr_alt_win[nop] = this_alternative_win;
2412 curr_alt_match_win[nop] = this_alternative_match_win;
2413 curr_alt_offmemok[nop] = this_alternative_offmemok;
2414 curr_alt_matches[nop] = this_alternative_matches;
2416 if (this_alternative_matches >= 0
2417 && !did_match && !this_alternative_win)
2418 curr_alt_win[this_alternative_matches] = false;
2420 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2421 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2423 if (curr_insn_set != NULL_RTX && n_operands == 2
2424 /* Prevent processing non-move insns. */
2425 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2426 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2427 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2428 && REG_P (no_subreg_reg_operand[0])
2429 && REG_P (no_subreg_reg_operand[1])
2430 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2431 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2432 || (! curr_alt_win[0] && curr_alt_win[1]
2433 && REG_P (no_subreg_reg_operand[1])
2434 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2435 || (curr_alt_win[0] && ! curr_alt_win[1]
2436 && REG_P (no_subreg_reg_operand[0])
2437 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2438 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2439 no_subreg_reg_operand[1])
2440 || (targetm.preferred_reload_class
2441 (no_subreg_reg_operand[1],
2442 (enum reg_class) curr_alt[1]) != NO_REGS))
2443 /* If it is a result of recent elimination in move
2444 insn we can transform it into an add still by
2445 using this alternative. */
2446 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2448 /* We have a move insn and a new reload insn will be similar
2449 to the current insn. We should avoid such situation as it
2450 results in LRA cycling. */
2451 overall += LRA_MAX_REJECT;
2453 ok_p = true;
2454 curr_alt_dont_inherit_ops_num = 0;
2455 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2457 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2458 HARD_REG_SET temp_set;
2460 i = early_clobbered_nops[nop];
2461 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2462 || hard_regno[i] < 0)
2463 continue;
2464 lra_assert (operand_reg[i] != NULL_RTX);
2465 clobbered_hard_regno = hard_regno[i];
2466 CLEAR_HARD_REG_SET (temp_set);
2467 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2468 first_conflict_j = last_conflict_j = -1;
2469 for (j = 0; j < n_operands; j++)
2470 if (j == i
2471 /* We don't want process insides of match_operator and
2472 match_parallel because otherwise we would process
2473 their operands once again generating a wrong
2474 code. */
2475 || curr_static_id->operand[j].is_operator)
2476 continue;
2477 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2478 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2479 continue;
2480 /* If we don't reload j-th operand, check conflicts. */
2481 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2482 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2484 if (first_conflict_j < 0)
2485 first_conflict_j = j;
2486 last_conflict_j = j;
2488 if (last_conflict_j < 0)
2489 continue;
2490 /* If earlyclobber operand conflicts with another
2491 non-matching operand which is actually the same register
2492 as the earlyclobber operand, it is better to reload the
2493 another operand as an operand matching the earlyclobber
2494 operand can be also the same. */
2495 if (first_conflict_j == last_conflict_j
2496 && operand_reg[last_conflict_j]
2497 != NULL_RTX && ! curr_alt_match_win[last_conflict_j]
2498 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2500 curr_alt_win[last_conflict_j] = false;
2501 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2502 = last_conflict_j;
2503 losers++;
2504 /* Early clobber was already reflected in REJECT. */
2505 lra_assert (reject > 0);
2506 if (lra_dump_file != NULL)
2507 fprintf
2508 (lra_dump_file,
2509 " %d Conflict early clobber reload: reject--\n",
2511 reject--;
2512 overall += LRA_LOSER_COST_FACTOR - 1;
2514 else
2516 /* We need to reload early clobbered register and the
2517 matched registers. */
2518 for (j = 0; j < n_operands; j++)
2519 if (curr_alt_matches[j] == i)
2521 curr_alt_match_win[j] = false;
2522 losers++;
2523 overall += LRA_LOSER_COST_FACTOR;
2525 if (! curr_alt_match_win[i])
2526 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2527 else
2529 /* Remember pseudos used for match reloads are never
2530 inherited. */
2531 lra_assert (curr_alt_matches[i] >= 0);
2532 curr_alt_win[curr_alt_matches[i]] = false;
2534 curr_alt_win[i] = curr_alt_match_win[i] = false;
2535 losers++;
2536 /* Early clobber was already reflected in REJECT. */
2537 lra_assert (reject > 0);
2538 if (lra_dump_file != NULL)
2539 fprintf
2540 (lra_dump_file,
2541 " %d Matched conflict early clobber reloads:"
2542 "reject--\n",
2544 reject--;
2545 overall += LRA_LOSER_COST_FACTOR - 1;
2548 if (lra_dump_file != NULL)
2549 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2550 nalt, overall, losers, reload_nregs);
2552 /* If this alternative can be made to work by reloading, and it
2553 needs less reloading than the others checked so far, record
2554 it as the chosen goal for reloading. */
2555 if ((best_losers != 0 && losers == 0)
2556 || (((best_losers == 0 && losers == 0)
2557 || (best_losers != 0 && losers != 0))
2558 && (best_overall > overall
2559 || (best_overall == overall
2560 /* If the cost of the reloads is the same,
2561 prefer alternative which requires minimal
2562 number of reload regs. */
2563 && (reload_nregs < best_reload_nregs
2564 || (reload_nregs == best_reload_nregs
2565 && (best_reload_sum < reload_sum
2566 || (best_reload_sum == reload_sum
2567 && nalt < goal_alt_number))))))))
2569 for (nop = 0; nop < n_operands; nop++)
2571 goal_alt_win[nop] = curr_alt_win[nop];
2572 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2573 goal_alt_matches[nop] = curr_alt_matches[nop];
2574 goal_alt[nop] = curr_alt[nop];
2575 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2577 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2578 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2579 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2580 goal_alt_swapped = curr_swapped;
2581 best_overall = overall;
2582 best_losers = losers;
2583 best_reload_nregs = reload_nregs;
2584 best_reload_sum = reload_sum;
2585 goal_alt_number = nalt;
2587 if (losers == 0)
2588 /* Everything is satisfied. Do not process alternatives
2589 anymore. */
2590 break;
2591 fail:
2594 return ok_p;
2597 /* Return 1 if ADDR is a valid memory address for mode MODE in address
2598 space AS, and check that each pseudo has the proper kind of hard
2599 reg. */
2600 static int
2601 valid_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2602 rtx addr, addr_space_t as)
2604 #ifdef GO_IF_LEGITIMATE_ADDRESS
2605 lra_assert (ADDR_SPACE_GENERIC_P (as));
2606 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
2607 return 0;
2609 win:
2610 return 1;
2611 #else
2612 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
2613 #endif
2616 /* Return whether address AD is valid. */
2618 static bool
2619 valid_address_p (struct address_info *ad)
2621 /* Some ports do not check displacements for eliminable registers,
2622 so we replace them temporarily with the elimination target. */
2623 rtx saved_base_reg = NULL_RTX;
2624 rtx saved_index_reg = NULL_RTX;
2625 rtx *base_term = strip_subreg (ad->base_term);
2626 rtx *index_term = strip_subreg (ad->index_term);
2627 if (base_term != NULL)
2629 saved_base_reg = *base_term;
2630 lra_eliminate_reg_if_possible (base_term);
2631 if (ad->base_term2 != NULL)
2632 *ad->base_term2 = *ad->base_term;
2634 if (index_term != NULL)
2636 saved_index_reg = *index_term;
2637 lra_eliminate_reg_if_possible (index_term);
2639 bool ok_p = valid_address_p (ad->mode, *ad->outer, ad->as);
2640 if (saved_base_reg != NULL_RTX)
2642 *base_term = saved_base_reg;
2643 if (ad->base_term2 != NULL)
2644 *ad->base_term2 = *ad->base_term;
2646 if (saved_index_reg != NULL_RTX)
2647 *index_term = saved_index_reg;
2648 return ok_p;
2651 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2652 static rtx
2653 base_plus_disp_to_reg (struct address_info *ad)
2655 enum reg_class cl;
2656 rtx new_reg;
2658 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2659 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2660 get_index_code (ad));
2661 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2662 cl, "base + disp");
2663 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2664 return new_reg;
2667 /* Make reload of index part of address AD. Return the new
2668 pseudo. */
2669 static rtx
2670 index_part_to_reg (struct address_info *ad)
2672 rtx new_reg;
2674 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2675 INDEX_REG_CLASS, "index term");
2676 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2677 GEN_INT (get_index_scale (ad)), new_reg, 1);
2678 return new_reg;
2681 /* Return true if we can add a displacement to address AD, even if that
2682 makes the address invalid. The fix-up code requires any new address
2683 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2684 static bool
2685 can_add_disp_p (struct address_info *ad)
2687 return (!ad->autoinc_p
2688 && ad->segment == NULL
2689 && ad->base == ad->base_term
2690 && ad->disp == ad->disp_term);
2693 /* Make equiv substitution in address AD. Return true if a substitution
2694 was made. */
2695 static bool
2696 equiv_address_substitution (struct address_info *ad)
2698 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2699 HOST_WIDE_INT disp, scale;
2700 bool change_p;
2702 base_term = strip_subreg (ad->base_term);
2703 if (base_term == NULL)
2704 base_reg = new_base_reg = NULL_RTX;
2705 else
2707 base_reg = *base_term;
2708 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2710 index_term = strip_subreg (ad->index_term);
2711 if (index_term == NULL)
2712 index_reg = new_index_reg = NULL_RTX;
2713 else
2715 index_reg = *index_term;
2716 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2718 if (base_reg == new_base_reg && index_reg == new_index_reg)
2719 return false;
2720 disp = 0;
2721 change_p = false;
2722 if (lra_dump_file != NULL)
2724 fprintf (lra_dump_file, "Changing address in insn %d ",
2725 INSN_UID (curr_insn));
2726 dump_value_slim (lra_dump_file, *ad->outer, 1);
2728 if (base_reg != new_base_reg)
2730 if (REG_P (new_base_reg))
2732 *base_term = new_base_reg;
2733 change_p = true;
2735 else if (GET_CODE (new_base_reg) == PLUS
2736 && REG_P (XEXP (new_base_reg, 0))
2737 && CONST_INT_P (XEXP (new_base_reg, 1))
2738 && can_add_disp_p (ad))
2740 disp += INTVAL (XEXP (new_base_reg, 1));
2741 *base_term = XEXP (new_base_reg, 0);
2742 change_p = true;
2744 if (ad->base_term2 != NULL)
2745 *ad->base_term2 = *ad->base_term;
2747 if (index_reg != new_index_reg)
2749 if (REG_P (new_index_reg))
2751 *index_term = new_index_reg;
2752 change_p = true;
2754 else if (GET_CODE (new_index_reg) == PLUS
2755 && REG_P (XEXP (new_index_reg, 0))
2756 && CONST_INT_P (XEXP (new_index_reg, 1))
2757 && can_add_disp_p (ad)
2758 && (scale = get_index_scale (ad)))
2760 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2761 *index_term = XEXP (new_index_reg, 0);
2762 change_p = true;
2765 if (disp != 0)
2767 if (ad->disp != NULL)
2768 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2769 else
2771 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2772 update_address (ad);
2774 change_p = true;
2776 if (lra_dump_file != NULL)
2778 if (! change_p)
2779 fprintf (lra_dump_file, " -- no change\n");
2780 else
2782 fprintf (lra_dump_file, " on equiv ");
2783 dump_value_slim (lra_dump_file, *ad->outer, 1);
2784 fprintf (lra_dump_file, "\n");
2787 return change_p;
2790 /* Major function to make reloads for an address in operand NOP.
2791 The supported cases are:
2793 1) an address that existed before LRA started, at which point it
2794 must have been valid. These addresses are subject to elimination
2795 and may have become invalid due to the elimination offset being out
2796 of range.
2798 2) an address created by forcing a constant to memory
2799 (force_const_to_mem). The initial form of these addresses might
2800 not be valid, and it is this function's job to make them valid.
2802 3) a frame address formed from a register and a (possibly zero)
2803 constant offset. As above, these addresses might not be valid and
2804 this function must make them so.
2806 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2807 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2808 address. Return true for any RTL change.
2810 The function is a helper function which does not produce all
2811 transformations which can be necessary. It does just basic steps.
2812 To do all necessary transformations use function
2813 process_address. */
2814 static bool
2815 process_address_1 (int nop, rtx *before, rtx *after)
2817 struct address_info ad;
2818 rtx new_reg;
2819 rtx op = *curr_id->operand_loc[nop];
2820 const char *constraint = curr_static_id->operand[nop].constraint;
2821 bool change_p;
2823 if (constraint[0] == 'p'
2824 || EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint))
2825 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2826 else if (MEM_P (op))
2827 decompose_mem_address (&ad, op);
2828 else if (GET_CODE (op) == SUBREG
2829 && MEM_P (SUBREG_REG (op)))
2830 decompose_mem_address (&ad, SUBREG_REG (op));
2831 else
2832 return false;
2833 change_p = equiv_address_substitution (&ad);
2834 if (ad.base_term != NULL
2835 && (process_addr_reg
2836 (ad.base_term, before,
2837 (ad.autoinc_p
2838 && !(REG_P (*ad.base_term)
2839 && find_regno_note (curr_insn, REG_DEAD,
2840 REGNO (*ad.base_term)) != NULL_RTX)
2841 ? after : NULL),
2842 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2843 get_index_code (&ad)))))
2845 change_p = true;
2846 if (ad.base_term2 != NULL)
2847 *ad.base_term2 = *ad.base_term;
2849 if (ad.index_term != NULL
2850 && process_addr_reg (ad.index_term, before, NULL, INDEX_REG_CLASS))
2851 change_p = true;
2853 #ifdef EXTRA_CONSTRAINT_STR
2854 /* Target hooks sometimes reject extra constraint addresses -- use
2855 EXTRA_CONSTRAINT_STR for the validation. */
2856 if (constraint[0] != 'p'
2857 && EXTRA_ADDRESS_CONSTRAINT (constraint[0], constraint)
2858 && EXTRA_CONSTRAINT_STR (op, constraint[0], constraint))
2859 return change_p;
2860 #endif
2862 /* There are three cases where the shape of *AD.INNER may now be invalid:
2864 1) the original address was valid, but either elimination or
2865 equiv_address_substitution was applied and that made
2866 the address invalid.
2868 2) the address is an invalid symbolic address created by
2869 force_const_to_mem.
2871 3) the address is a frame address with an invalid offset.
2873 All these cases involve a non-autoinc address, so there is no
2874 point revalidating other types. */
2875 if (ad.autoinc_p || valid_address_p (&ad))
2876 return change_p;
2878 /* Any index existed before LRA started, so we can assume that the
2879 presence and shape of the index is valid. */
2880 push_to_sequence (*before);
2881 lra_assert (ad.disp == ad.disp_term);
2882 if (ad.base == NULL)
2884 if (ad.index == NULL)
2886 int code = -1;
2887 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2888 SCRATCH, SCRATCH);
2889 rtx addr = *ad.inner;
2891 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2892 #ifdef HAVE_lo_sum
2894 rtx insn;
2895 rtx last = get_last_insn ();
2897 /* addr => lo_sum (new_base, addr), case (2) above. */
2898 insn = emit_insn (gen_rtx_SET
2899 (VOIDmode, new_reg,
2900 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2901 code = recog_memoized (insn);
2902 if (code >= 0)
2904 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2905 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2907 /* Try to put lo_sum into register. */
2908 insn = emit_insn (gen_rtx_SET
2909 (VOIDmode, new_reg,
2910 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2911 code = recog_memoized (insn);
2912 if (code >= 0)
2914 *ad.inner = new_reg;
2915 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2917 *ad.inner = addr;
2918 code = -1;
2924 if (code < 0)
2925 delete_insns_since (last);
2927 #endif
2928 if (code < 0)
2930 /* addr => new_base, case (2) above. */
2931 lra_emit_move (new_reg, addr);
2932 *ad.inner = new_reg;
2935 else
2937 /* index * scale + disp => new base + index * scale,
2938 case (1) above. */
2939 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
2940 GET_CODE (*ad.index));
2942 lra_assert (INDEX_REG_CLASS != NO_REGS);
2943 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
2944 lra_emit_move (new_reg, *ad.disp);
2945 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2946 new_reg, *ad.index);
2949 else if (ad.index == NULL)
2951 int regno;
2952 enum reg_class cl;
2953 rtx set, insns, last_insn;
2954 /* base + disp => new base, cases (1) and (3) above. */
2955 /* Another option would be to reload the displacement into an
2956 index register. However, postreload has code to optimize
2957 address reloads that have the same base and different
2958 displacements, so reloading into an index register would
2959 not necessarily be a win. */
2960 start_sequence ();
2961 new_reg = base_plus_disp_to_reg (&ad);
2962 insns = get_insns ();
2963 last_insn = get_last_insn ();
2964 /* If we generated at least two insns, try last insn source as
2965 an address. If we succeed, we generate one less insn. */
2966 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
2967 && GET_CODE (SET_SRC (set)) == PLUS
2968 && REG_P (XEXP (SET_SRC (set), 0))
2969 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
2971 *ad.inner = SET_SRC (set);
2972 if (valid_address_p (ad.mode, *ad.outer, ad.as))
2974 *ad.base_term = XEXP (SET_SRC (set), 0);
2975 *ad.disp_term = XEXP (SET_SRC (set), 1);
2976 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2977 get_index_code (&ad));
2978 regno = REGNO (*ad.base_term);
2979 if (regno >= FIRST_PSEUDO_REGISTER
2980 && cl != lra_get_allocno_class (regno))
2981 lra_change_class (regno, cl, " Change to", true);
2982 new_reg = SET_SRC (set);
2983 delete_insns_since (PREV_INSN (last_insn));
2986 end_sequence ();
2987 emit_insn (insns);
2988 *ad.inner = new_reg;
2990 else if (ad.disp_term != NULL)
2992 /* base + scale * index + disp => new base + scale * index,
2993 case (1) above. */
2994 new_reg = base_plus_disp_to_reg (&ad);
2995 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
2996 new_reg, *ad.index);
2998 else if (get_index_scale (&ad) == 1)
3000 /* The last transformation to one reg will be made in
3001 curr_insn_transform function. */
3002 end_sequence ();
3003 return false;
3005 else
3007 /* base + scale * index => base + new_reg,
3008 case (1) above.
3009 Index part of address may become invalid. For example, we
3010 changed pseudo on the equivalent memory and a subreg of the
3011 pseudo onto the memory of different mode for which the scale is
3012 prohibitted. */
3013 new_reg = index_part_to_reg (&ad);
3014 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3015 *ad.base_term, new_reg);
3017 *before = get_insns ();
3018 end_sequence ();
3019 return true;
3022 /* Do address reloads until it is necessary. Use process_address_1 as
3023 a helper function. Return true for any RTL changes. */
3024 static bool
3025 process_address (int nop, rtx *before, rtx *after)
3027 bool res = false;
3029 while (process_address_1 (nop, before, after))
3030 res = true;
3031 return res;
3034 /* Emit insns to reload VALUE into a new register. VALUE is an
3035 auto-increment or auto-decrement RTX whose operand is a register or
3036 memory location; so reloading involves incrementing that location.
3037 IN is either identical to VALUE, or some cheaper place to reload
3038 value being incremented/decremented from.
3040 INC_AMOUNT is the number to increment or decrement by (always
3041 positive and ignored for POST_MODIFY/PRE_MODIFY).
3043 Return pseudo containing the result. */
3044 static rtx
3045 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3047 /* REG or MEM to be copied and incremented. */
3048 rtx incloc = XEXP (value, 0);
3049 /* Nonzero if increment after copying. */
3050 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3051 || GET_CODE (value) == POST_MODIFY);
3052 rtx last;
3053 rtx inc;
3054 rtx add_insn;
3055 int code;
3056 rtx real_in = in == value ? incloc : in;
3057 rtx result;
3058 bool plus_p = true;
3060 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3062 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3063 || GET_CODE (XEXP (value, 1)) == MINUS);
3064 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3065 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3066 inc = XEXP (XEXP (value, 1), 1);
3068 else
3070 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3071 inc_amount = -inc_amount;
3073 inc = GEN_INT (inc_amount);
3076 if (! post && REG_P (incloc))
3077 result = incloc;
3078 else
3079 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3080 "INC/DEC result");
3082 if (real_in != result)
3084 /* First copy the location to the result register. */
3085 lra_assert (REG_P (result));
3086 emit_insn (gen_move_insn (result, real_in));
3089 /* We suppose that there are insns to add/sub with the constant
3090 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3091 old reload worked with this assumption. If the assumption
3092 becomes wrong, we should use approach in function
3093 base_plus_disp_to_reg. */
3094 if (in == value)
3096 /* See if we can directly increment INCLOC. */
3097 last = get_last_insn ();
3098 add_insn = emit_insn (plus_p
3099 ? gen_add2_insn (incloc, inc)
3100 : gen_sub2_insn (incloc, inc));
3102 code = recog_memoized (add_insn);
3103 if (code >= 0)
3105 if (! post && result != incloc)
3106 emit_insn (gen_move_insn (result, incloc));
3107 return result;
3109 delete_insns_since (last);
3112 /* If couldn't do the increment directly, must increment in RESULT.
3113 The way we do this depends on whether this is pre- or
3114 post-increment. For pre-increment, copy INCLOC to the reload
3115 register, increment it there, then save back. */
3116 if (! post)
3118 if (real_in != result)
3119 emit_insn (gen_move_insn (result, real_in));
3120 if (plus_p)
3121 emit_insn (gen_add2_insn (result, inc));
3122 else
3123 emit_insn (gen_sub2_insn (result, inc));
3124 if (result != incloc)
3125 emit_insn (gen_move_insn (incloc, result));
3127 else
3129 /* Post-increment.
3131 Because this might be a jump insn or a compare, and because
3132 RESULT may not be available after the insn in an input
3133 reload, we must do the incrementing before the insn being
3134 reloaded for.
3136 We have already copied IN to RESULT. Increment the copy in
3137 RESULT, save that back, then decrement RESULT so it has
3138 the original value. */
3139 if (plus_p)
3140 emit_insn (gen_add2_insn (result, inc));
3141 else
3142 emit_insn (gen_sub2_insn (result, inc));
3143 emit_insn (gen_move_insn (incloc, result));
3144 /* Restore non-modified value for the result. We prefer this
3145 way because it does not require an additional hard
3146 register. */
3147 if (plus_p)
3149 if (CONST_INT_P (inc))
3150 emit_insn (gen_add2_insn (result,
3151 gen_int_mode (-INTVAL (inc),
3152 GET_MODE (result))));
3153 else
3154 emit_insn (gen_sub2_insn (result, inc));
3156 else
3157 emit_insn (gen_add2_insn (result, inc));
3159 return result;
3162 /* Return true if the current move insn does not need processing as we
3163 already know that it satisfies its constraints. */
3164 static bool
3165 simple_move_p (void)
3167 rtx dest, src;
3168 enum reg_class dclass, sclass;
3170 lra_assert (curr_insn_set != NULL_RTX);
3171 dest = SET_DEST (curr_insn_set);
3172 src = SET_SRC (curr_insn_set);
3173 return ((dclass = get_op_class (dest)) != NO_REGS
3174 && (sclass = get_op_class (src)) != NO_REGS
3175 /* The backend guarantees that register moves of cost 2
3176 never need reloads. */
3177 && targetm.register_move_cost (GET_MODE (src), dclass, sclass) == 2);
3180 /* Swap operands NOP and NOP + 1. */
3181 static inline void
3182 swap_operands (int nop)
3184 enum machine_mode mode = curr_operand_mode[nop];
3185 curr_operand_mode[nop] = curr_operand_mode[nop + 1];
3186 curr_operand_mode[nop + 1] = mode;
3187 rtx x = *curr_id->operand_loc[nop];
3188 *curr_id->operand_loc[nop] = *curr_id->operand_loc[nop + 1];
3189 *curr_id->operand_loc[nop + 1] = x;
3190 /* Swap the duplicates too. */
3191 lra_update_dup (curr_id, nop);
3192 lra_update_dup (curr_id, nop + 1);
3195 /* Main entry point of the constraint code: search the body of the
3196 current insn to choose the best alternative. It is mimicking insn
3197 alternative cost calculation model of former reload pass. That is
3198 because machine descriptions were written to use this model. This
3199 model can be changed in future. Make commutative operand exchange
3200 if it is chosen.
3202 Return true if some RTL changes happened during function call. */
3203 static bool
3204 curr_insn_transform (void)
3206 int i, j, k;
3207 int n_operands;
3208 int n_alternatives;
3209 int commutative;
3210 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3211 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3212 rtx before, after;
3213 bool alt_p = false;
3214 /* Flag that the insn has been changed through a transformation. */
3215 bool change_p;
3216 bool sec_mem_p;
3217 #ifdef SECONDARY_MEMORY_NEEDED
3218 bool use_sec_mem_p;
3219 #endif
3220 int max_regno_before;
3221 int reused_alternative_num;
3223 curr_insn_set = single_set (curr_insn);
3224 if (curr_insn_set != NULL_RTX && simple_move_p ())
3225 return false;
3227 no_input_reloads_p = no_output_reloads_p = false;
3228 goal_alt_number = -1;
3229 change_p = sec_mem_p = false;
3230 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3231 reloads; neither are insns that SET cc0. Insns that use CC0 are
3232 not allowed to have any input reloads. */
3233 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3234 no_output_reloads_p = true;
3236 #ifdef HAVE_cc0
3237 if (reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3238 no_input_reloads_p = true;
3239 if (reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3240 no_output_reloads_p = true;
3241 #endif
3243 n_operands = curr_static_id->n_operands;
3244 n_alternatives = curr_static_id->n_alternatives;
3246 /* Just return "no reloads" if insn has no operands with
3247 constraints. */
3248 if (n_operands == 0 || n_alternatives == 0)
3249 return false;
3251 max_regno_before = max_reg_num ();
3253 for (i = 0; i < n_operands; i++)
3255 goal_alt_matched[i][0] = -1;
3256 goal_alt_matches[i] = -1;
3259 commutative = curr_static_id->commutative;
3261 /* Now see what we need for pseudos that didn't get hard regs or got
3262 the wrong kind of hard reg. For this, we must consider all the
3263 operands together against the register constraints. */
3265 best_losers = best_overall = INT_MAX;
3266 best_reload_sum = 0;
3268 curr_swapped = false;
3269 goal_alt_swapped = false;
3271 /* Make equivalence substitution and memory subreg elimination
3272 before address processing because an address legitimacy can
3273 depend on memory mode. */
3274 for (i = 0; i < n_operands; i++)
3276 rtx op = *curr_id->operand_loc[i];
3277 rtx subst, old = op;
3278 bool op_change_p = false;
3280 if (GET_CODE (old) == SUBREG)
3281 old = SUBREG_REG (old);
3282 subst = get_equiv_with_elimination (old, curr_insn);
3283 if (subst != old)
3285 subst = copy_rtx (subst);
3286 lra_assert (REG_P (old));
3287 if (GET_CODE (op) == SUBREG)
3288 SUBREG_REG (op) = subst;
3289 else
3290 *curr_id->operand_loc[i] = subst;
3291 if (lra_dump_file != NULL)
3293 fprintf (lra_dump_file,
3294 "Changing pseudo %d in operand %i of insn %u on equiv ",
3295 REGNO (old), i, INSN_UID (curr_insn));
3296 dump_value_slim (lra_dump_file, subst, 1);
3297 fprintf (lra_dump_file, "\n");
3299 op_change_p = change_p = true;
3301 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3303 change_p = true;
3304 lra_update_dup (curr_id, i);
3308 /* Reload address registers and displacements. We do it before
3309 finding an alternative because of memory constraints. */
3310 before = after = NULL_RTX;
3311 for (i = 0; i < n_operands; i++)
3312 if (! curr_static_id->operand[i].is_operator
3313 && process_address (i, &before, &after))
3315 change_p = true;
3316 lra_update_dup (curr_id, i);
3319 if (change_p)
3320 /* If we've changed the instruction then any alternative that
3321 we chose previously may no longer be valid. */
3322 lra_set_used_insn_alternative (curr_insn, -1);
3324 if (curr_insn_set != NULL_RTX
3325 && check_and_process_move (&change_p, &sec_mem_p))
3326 return change_p;
3328 try_swapped:
3330 reused_alternative_num = curr_id->used_insn_alternative;
3331 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3332 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3333 reused_alternative_num, INSN_UID (curr_insn));
3335 if (process_alt_operands (reused_alternative_num))
3336 alt_p = true;
3338 /* If insn is commutative (it's safe to exchange a certain pair of
3339 operands) then we need to try each alternative twice, the second
3340 time matching those two operands as if we had exchanged them. To
3341 do this, really exchange them in operands.
3343 If we have just tried the alternatives the second time, return
3344 operands to normal and drop through. */
3346 if (reused_alternative_num < 0 && commutative >= 0)
3348 curr_swapped = !curr_swapped;
3349 if (curr_swapped)
3351 swap_operands (commutative);
3352 goto try_swapped;
3354 else
3355 swap_operands (commutative);
3358 if (! alt_p && ! sec_mem_p)
3360 /* No alternative works with reloads?? */
3361 if (INSN_CODE (curr_insn) >= 0)
3362 fatal_insn ("unable to generate reloads for:", curr_insn);
3363 error_for_asm (curr_insn,
3364 "inconsistent operand constraints in an %<asm%>");
3365 /* Avoid further trouble with this insn. */
3366 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3367 lra_invalidate_insn_data (curr_insn);
3368 return true;
3371 /* If the best alternative is with operands 1 and 2 swapped, swap
3372 them. Update the operand numbers of any reloads already
3373 pushed. */
3375 if (goal_alt_swapped)
3377 if (lra_dump_file != NULL)
3378 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3379 INSN_UID (curr_insn));
3381 /* Swap the duplicates too. */
3382 swap_operands (commutative);
3383 change_p = true;
3386 #ifdef SECONDARY_MEMORY_NEEDED
3387 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3388 too conservatively. So we use the secondary memory only if there
3389 is no any alternative without reloads. */
3390 use_sec_mem_p = false;
3391 if (! alt_p)
3392 use_sec_mem_p = true;
3393 else if (sec_mem_p)
3395 for (i = 0; i < n_operands; i++)
3396 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3397 break;
3398 use_sec_mem_p = i < n_operands;
3401 if (use_sec_mem_p)
3403 rtx new_reg, src, dest, rld;
3404 enum machine_mode sec_mode, rld_mode;
3406 lra_assert (sec_mem_p);
3407 lra_assert (curr_static_id->operand[0].type == OP_OUT
3408 && curr_static_id->operand[1].type == OP_IN);
3409 dest = *curr_id->operand_loc[0];
3410 src = *curr_id->operand_loc[1];
3411 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3412 ? dest : src);
3413 rld_mode = GET_MODE (rld);
3414 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3415 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3416 #else
3417 sec_mode = rld_mode;
3418 #endif
3419 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3420 NO_REGS, "secondary");
3421 /* If the mode is changed, it should be wider. */
3422 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3423 if (sec_mode != rld_mode)
3425 /* If the target says specifically to use another mode for
3426 secondary memory moves we can not reuse the original
3427 insn. */
3428 after = emit_spill_move (false, new_reg, dest);
3429 lra_process_new_insns (curr_insn, NULL_RTX, after,
3430 "Inserting the sec. move");
3431 /* We may have non null BEFORE here (e.g. after address
3432 processing. */
3433 push_to_sequence (before);
3434 before = emit_spill_move (true, new_reg, src);
3435 emit_insn (before);
3436 before = get_insns ();
3437 end_sequence ();
3438 lra_process_new_insns (curr_insn, before, NULL_RTX, "Changing on");
3439 lra_set_insn_deleted (curr_insn);
3441 else if (dest == rld)
3443 *curr_id->operand_loc[0] = new_reg;
3444 after = emit_spill_move (false, new_reg, dest);
3445 lra_process_new_insns (curr_insn, NULL_RTX, after,
3446 "Inserting the sec. move");
3448 else
3450 *curr_id->operand_loc[1] = new_reg;
3451 /* See comments above. */
3452 push_to_sequence (before);
3453 before = emit_spill_move (true, new_reg, src);
3454 emit_insn (before);
3455 before = get_insns ();
3456 end_sequence ();
3457 lra_process_new_insns (curr_insn, before, NULL_RTX,
3458 "Inserting the sec. move");
3460 lra_update_insn_regno_info (curr_insn);
3461 return true;
3463 #endif
3465 lra_assert (goal_alt_number >= 0);
3466 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3468 if (lra_dump_file != NULL)
3470 const char *p;
3472 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3473 goal_alt_number, INSN_UID (curr_insn));
3474 for (i = 0; i < n_operands; i++)
3476 p = (curr_static_id->operand_alternative
3477 [goal_alt_number * n_operands + i].constraint);
3478 if (*p == '\0')
3479 continue;
3480 fprintf (lra_dump_file, " (%d) ", i);
3481 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3482 fputc (*p, lra_dump_file);
3484 if (INSN_CODE (curr_insn) >= 0
3485 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3486 fprintf (lra_dump_file, " {%s}", p);
3487 if (curr_id->sp_offset != 0)
3488 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3489 curr_id->sp_offset);
3490 fprintf (lra_dump_file, "\n");
3493 /* Right now, for any pair of operands I and J that are required to
3494 match, with J < I, goal_alt_matches[I] is J. Add I to
3495 goal_alt_matched[J]. */
3497 for (i = 0; i < n_operands; i++)
3498 if ((j = goal_alt_matches[i]) >= 0)
3500 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3502 /* We allow matching one output operand and several input
3503 operands. */
3504 lra_assert (k == 0
3505 || (curr_static_id->operand[j].type == OP_OUT
3506 && curr_static_id->operand[i].type == OP_IN
3507 && (curr_static_id->operand
3508 [goal_alt_matched[j][0]].type == OP_IN)));
3509 goal_alt_matched[j][k] = i;
3510 goal_alt_matched[j][k + 1] = -1;
3513 for (i = 0; i < n_operands; i++)
3514 goal_alt_win[i] |= goal_alt_match_win[i];
3516 /* Any constants that aren't allowed and can't be reloaded into
3517 registers are here changed into memory references. */
3518 for (i = 0; i < n_operands; i++)
3519 if (goal_alt_win[i])
3521 int regno;
3522 enum reg_class new_class;
3523 rtx reg = *curr_id->operand_loc[i];
3525 if (GET_CODE (reg) == SUBREG)
3526 reg = SUBREG_REG (reg);
3528 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3530 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3532 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3534 lra_assert (ok_p);
3535 lra_change_class (regno, new_class, " Change to", true);
3539 else
3541 const char *constraint;
3542 char c;
3543 rtx op = *curr_id->operand_loc[i];
3544 rtx subreg = NULL_RTX;
3545 enum machine_mode mode = curr_operand_mode[i];
3547 if (GET_CODE (op) == SUBREG)
3549 subreg = op;
3550 op = SUBREG_REG (op);
3551 mode = GET_MODE (op);
3554 if (CONST_POOL_OK_P (mode, op)
3555 && ((targetm.preferred_reload_class
3556 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3557 || no_input_reloads_p))
3559 rtx tem = force_const_mem (mode, op);
3561 change_p = true;
3562 if (subreg != NULL_RTX)
3563 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3565 *curr_id->operand_loc[i] = tem;
3566 lra_update_dup (curr_id, i);
3567 process_address (i, &before, &after);
3569 /* If the alternative accepts constant pool refs directly
3570 there will be no reload needed at all. */
3571 if (subreg != NULL_RTX)
3572 continue;
3573 /* Skip alternatives before the one requested. */
3574 constraint = (curr_static_id->operand_alternative
3575 [goal_alt_number * n_operands + i].constraint);
3576 for (;
3577 (c = *constraint) && c != ',' && c != '#';
3578 constraint += CONSTRAINT_LEN (c, constraint))
3580 if (c == TARGET_MEM_CONSTRAINT || c == 'o')
3581 break;
3582 #ifdef EXTRA_CONSTRAINT_STR
3583 if (EXTRA_MEMORY_CONSTRAINT (c, constraint)
3584 && EXTRA_CONSTRAINT_STR (tem, c, constraint))
3585 break;
3586 #endif
3588 if (c == '\0' || c == ',' || c == '#')
3589 continue;
3591 goal_alt_win[i] = true;
3595 for (i = 0; i < n_operands; i++)
3597 int regno;
3598 bool optional_p = false;
3599 rtx old, new_reg;
3600 rtx op = *curr_id->operand_loc[i];
3602 if (goal_alt_win[i])
3604 if (goal_alt[i] == NO_REGS
3605 && REG_P (op)
3606 /* When we assign NO_REGS it means that we will not
3607 assign a hard register to the scratch pseudo by
3608 assigment pass and the scratch pseudo will be
3609 spilled. Spilled scratch pseudos are transformed
3610 back to scratches at the LRA end. */
3611 && lra_former_scratch_operand_p (curr_insn, i))
3613 int regno = REGNO (op);
3614 lra_change_class (regno, NO_REGS, " Change to", true);
3615 if (lra_get_regno_hard_regno (regno) >= 0)
3616 /* We don't have to mark all insn affected by the
3617 spilled pseudo as there is only one such insn, the
3618 current one. */
3619 reg_renumber[regno] = -1;
3621 /* We can do an optional reload. If the pseudo got a hard
3622 reg, we might improve the code through inheritance. If
3623 it does not get a hard register we coalesce memory/memory
3624 moves later. Ignore move insns to avoid cycling. */
3625 if (! lra_simple_p
3626 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3627 && goal_alt[i] != NO_REGS && REG_P (op)
3628 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3629 && regno < new_regno_start
3630 && ! lra_former_scratch_p (regno)
3631 && reg_renumber[regno] < 0
3632 && (curr_insn_set == NULL_RTX
3633 || !((REG_P (SET_SRC (curr_insn_set))
3634 || MEM_P (SET_SRC (curr_insn_set))
3635 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3636 && (REG_P (SET_DEST (curr_insn_set))
3637 || MEM_P (SET_DEST (curr_insn_set))
3638 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3639 optional_p = true;
3640 else
3641 continue;
3644 /* Operands that match previous ones have already been handled. */
3645 if (goal_alt_matches[i] >= 0)
3646 continue;
3648 /* We should not have an operand with a non-offsettable address
3649 appearing where an offsettable address will do. It also may
3650 be a case when the address should be special in other words
3651 not a general one (e.g. it needs no index reg). */
3652 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3654 enum reg_class rclass;
3655 rtx *loc = &XEXP (op, 0);
3656 enum rtx_code code = GET_CODE (*loc);
3658 push_to_sequence (before);
3659 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3660 MEM, SCRATCH);
3661 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3662 new_reg = emit_inc (rclass, *loc, *loc,
3663 /* This value does not matter for MODIFY. */
3664 GET_MODE_SIZE (GET_MODE (op)));
3665 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3666 "offsetable address", &new_reg))
3667 lra_emit_move (new_reg, *loc);
3668 before = get_insns ();
3669 end_sequence ();
3670 *loc = new_reg;
3671 lra_update_dup (curr_id, i);
3673 else if (goal_alt_matched[i][0] == -1)
3675 enum machine_mode mode;
3676 rtx reg, *loc;
3677 int hard_regno, byte;
3678 enum op_type type = curr_static_id->operand[i].type;
3680 loc = curr_id->operand_loc[i];
3681 mode = curr_operand_mode[i];
3682 if (GET_CODE (*loc) == SUBREG)
3684 reg = SUBREG_REG (*loc);
3685 byte = SUBREG_BYTE (*loc);
3686 if (REG_P (reg)
3687 /* Strict_low_part requires reload the register not
3688 the sub-register. */
3689 && (curr_static_id->operand[i].strict_low
3690 || (GET_MODE_SIZE (mode)
3691 <= GET_MODE_SIZE (GET_MODE (reg))
3692 && (hard_regno
3693 = get_try_hard_regno (REGNO (reg))) >= 0
3694 && (simplify_subreg_regno
3695 (hard_regno,
3696 GET_MODE (reg), byte, mode) < 0)
3697 && (goal_alt[i] == NO_REGS
3698 || (simplify_subreg_regno
3699 (ira_class_hard_regs[goal_alt[i]][0],
3700 GET_MODE (reg), byte, mode) >= 0)))))
3702 loc = &SUBREG_REG (*loc);
3703 mode = GET_MODE (*loc);
3706 old = *loc;
3707 if (get_reload_reg (type, mode, old, goal_alt[i],
3708 loc != curr_id->operand_loc[i], "", &new_reg)
3709 && type != OP_OUT)
3711 push_to_sequence (before);
3712 lra_emit_move (new_reg, old);
3713 before = get_insns ();
3714 end_sequence ();
3716 *loc = new_reg;
3717 if (type != OP_IN
3718 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3720 start_sequence ();
3721 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3722 emit_insn (after);
3723 after = get_insns ();
3724 end_sequence ();
3725 *loc = new_reg;
3727 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3728 if (goal_alt_dont_inherit_ops[j] == i)
3730 lra_set_regno_unique_value (REGNO (new_reg));
3731 break;
3733 lra_update_dup (curr_id, i);
3735 else if (curr_static_id->operand[i].type == OP_IN
3736 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3737 == OP_OUT))
3739 /* generate reloads for input and matched outputs. */
3740 match_inputs[0] = i;
3741 match_inputs[1] = -1;
3742 match_reload (goal_alt_matched[i][0], match_inputs,
3743 goal_alt[i], &before, &after);
3745 else if (curr_static_id->operand[i].type == OP_OUT
3746 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3747 == OP_IN))
3748 /* Generate reloads for output and matched inputs. */
3749 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after);
3750 else if (curr_static_id->operand[i].type == OP_IN
3751 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3752 == OP_IN))
3754 /* Generate reloads for matched inputs. */
3755 match_inputs[0] = i;
3756 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3757 match_inputs[j + 1] = k;
3758 match_inputs[j + 1] = -1;
3759 match_reload (-1, match_inputs, goal_alt[i], &before, &after);
3761 else
3762 /* We must generate code in any case when function
3763 process_alt_operands decides that it is possible. */
3764 gcc_unreachable ();
3765 if (optional_p)
3767 lra_assert (REG_P (op));
3768 regno = REGNO (op);
3769 op = *curr_id->operand_loc[i]; /* Substitution. */
3770 if (GET_CODE (op) == SUBREG)
3771 op = SUBREG_REG (op);
3772 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3773 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3774 lra_reg_info[REGNO (op)].restore_regno = regno;
3775 if (lra_dump_file != NULL)
3776 fprintf (lra_dump_file,
3777 " Making reload reg %d for reg %d optional\n",
3778 REGNO (op), regno);
3781 if (before != NULL_RTX || after != NULL_RTX
3782 || max_regno_before != max_reg_num ())
3783 change_p = true;
3784 if (change_p)
3786 lra_update_operator_dups (curr_id);
3787 /* Something changes -- process the insn. */
3788 lra_update_insn_regno_info (curr_insn);
3790 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3791 return change_p;
3794 /* Return true if X is in LIST. */
3795 static bool
3796 in_list_p (rtx x, rtx list)
3798 for (; list != NULL_RTX; list = XEXP (list, 1))
3799 if (XEXP (list, 0) == x)
3800 return true;
3801 return false;
3804 /* Return true if X contains an allocatable hard register (if
3805 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3806 static bool
3807 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3809 int i, j;
3810 const char *fmt;
3811 enum rtx_code code;
3813 code = GET_CODE (x);
3814 if (REG_P (x))
3816 int regno = REGNO (x);
3817 HARD_REG_SET alloc_regs;
3819 if (hard_reg_p)
3821 if (regno >= FIRST_PSEUDO_REGISTER)
3822 regno = lra_get_regno_hard_regno (regno);
3823 if (regno < 0)
3824 return false;
3825 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3826 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3828 else
3830 if (regno < FIRST_PSEUDO_REGISTER)
3831 return false;
3832 if (! spilled_p)
3833 return true;
3834 return lra_get_regno_hard_regno (regno) < 0;
3837 fmt = GET_RTX_FORMAT (code);
3838 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3840 if (fmt[i] == 'e')
3842 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3843 return true;
3845 else if (fmt[i] == 'E')
3847 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3848 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
3849 return true;
3852 return false;
3855 /* Process all regs in location *LOC and change them on equivalent
3856 substitution. Return true if any change was done. */
3857 static bool
3858 loc_equivalence_change_p (rtx *loc)
3860 rtx subst, reg, x = *loc;
3861 bool result = false;
3862 enum rtx_code code = GET_CODE (x);
3863 const char *fmt;
3864 int i, j;
3866 if (code == SUBREG)
3868 reg = SUBREG_REG (x);
3869 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
3870 && GET_MODE (subst) == VOIDmode)
3872 /* We cannot reload debug location. Simplify subreg here
3873 while we know the inner mode. */
3874 *loc = simplify_gen_subreg (GET_MODE (x), subst,
3875 GET_MODE (reg), SUBREG_BYTE (x));
3876 return true;
3879 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
3881 *loc = subst;
3882 return true;
3885 /* Scan all the operand sub-expressions. */
3886 fmt = GET_RTX_FORMAT (code);
3887 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3889 if (fmt[i] == 'e')
3890 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
3891 else if (fmt[i] == 'E')
3892 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3893 result
3894 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
3896 return result;
3899 /* Similar to loc_equivalence_change_p, but for use as
3900 simplify_replace_fn_rtx callback. DATA is insn for which the
3901 elimination is done. If it null we don't do the elimination. */
3902 static rtx
3903 loc_equivalence_callback (rtx loc, const_rtx, void *data)
3905 if (!REG_P (loc))
3906 return NULL_RTX;
3908 rtx subst = (data == NULL
3909 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx) data));
3910 if (subst != loc)
3911 return subst;
3913 return NULL_RTX;
3916 /* Maximum number of generated reload insns per an insn. It is for
3917 preventing this pass cycling in a bug case. */
3918 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
3920 /* The current iteration number of this LRA pass. */
3921 int lra_constraint_iter;
3923 /* True if we substituted equiv which needs checking register
3924 allocation correctness because the equivalent value contains
3925 allocatable hard registers or when we restore multi-register
3926 pseudo. */
3927 bool lra_risky_transformations_p;
3929 /* Return true if REGNO is referenced in more than one block. */
3930 static bool
3931 multi_block_pseudo_p (int regno)
3933 basic_block bb = NULL;
3934 unsigned int uid;
3935 bitmap_iterator bi;
3937 if (regno < FIRST_PSEUDO_REGISTER)
3938 return false;
3940 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
3941 if (bb == NULL)
3942 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
3943 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
3944 return true;
3945 return false;
3948 /* Return true if LIST contains a deleted insn. */
3949 static bool
3950 contains_deleted_insn_p (rtx list)
3952 for (; list != NULL_RTX; list = XEXP (list, 1))
3953 if (NOTE_P (XEXP (list, 0))
3954 && NOTE_KIND (XEXP (list, 0)) == NOTE_INSN_DELETED)
3955 return true;
3956 return false;
3959 /* Return true if X contains a pseudo dying in INSN. */
3960 static bool
3961 dead_pseudo_p (rtx x, rtx insn)
3963 int i, j;
3964 const char *fmt;
3965 enum rtx_code code;
3967 if (REG_P (x))
3968 return (insn != NULL_RTX
3969 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
3970 code = GET_CODE (x);
3971 fmt = GET_RTX_FORMAT (code);
3972 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3974 if (fmt[i] == 'e')
3976 if (dead_pseudo_p (XEXP (x, i), insn))
3977 return true;
3979 else if (fmt[i] == 'E')
3981 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3982 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
3983 return true;
3986 return false;
3989 /* Return true if INSN contains a dying pseudo in INSN right hand
3990 side. */
3991 static bool
3992 insn_rhs_dead_pseudo_p (rtx insn)
3994 rtx set = single_set (insn);
3996 gcc_assert (set != NULL);
3997 return dead_pseudo_p (SET_SRC (set), insn);
4000 /* Return true if any init insn of REGNO contains a dying pseudo in
4001 insn right hand side. */
4002 static bool
4003 init_insn_rhs_dead_pseudo_p (int regno)
4005 rtx insns = ira_reg_equiv[regno].init_insns;
4007 if (insns == NULL)
4008 return false;
4009 if (INSN_P (insns))
4010 return insn_rhs_dead_pseudo_p (insns);
4011 for (; insns != NULL_RTX; insns = XEXP (insns, 1))
4012 if (insn_rhs_dead_pseudo_p (XEXP (insns, 0)))
4013 return true;
4014 return false;
4017 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4018 reverse only if we have one init insn with given REGNO as a
4019 source. */
4020 static bool
4021 reverse_equiv_p (int regno)
4023 rtx insns, set;
4025 if ((insns = ira_reg_equiv[regno].init_insns) == NULL_RTX)
4026 return false;
4027 if (! INSN_P (XEXP (insns, 0))
4028 || XEXP (insns, 1) != NULL_RTX)
4029 return false;
4030 if ((set = single_set (XEXP (insns, 0))) == NULL_RTX)
4031 return false;
4032 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4035 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4036 call this function only for non-reverse equivalence. */
4037 static bool
4038 contains_reloaded_insn_p (int regno)
4040 rtx set;
4041 rtx list = ira_reg_equiv[regno].init_insns;
4043 for (; list != NULL_RTX; list = XEXP (list, 1))
4044 if ((set = single_set (XEXP (list, 0))) == NULL_RTX
4045 || ! REG_P (SET_DEST (set))
4046 || (int) REGNO (SET_DEST (set)) != regno)
4047 return true;
4048 return false;
4051 /* Entry function of LRA constraint pass. Return true if the
4052 constraint pass did change the code. */
4053 bool
4054 lra_constraints (bool first_p)
4056 bool changed_p;
4057 int i, hard_regno, new_insns_num;
4058 unsigned int min_len, new_min_len, uid;
4059 rtx set, x, reg, dest_reg;
4060 basic_block last_bb;
4061 bitmap_head equiv_insn_bitmap;
4062 bitmap_iterator bi;
4064 lra_constraint_iter++;
4065 if (lra_dump_file != NULL)
4066 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4067 lra_constraint_iter);
4068 changed_p = false;
4069 lra_risky_transformations_p = false;
4070 new_insn_uid_start = get_max_uid ();
4071 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4072 /* Mark used hard regs for target stack size calulations. */
4073 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4074 if (lra_reg_info[i].nrefs != 0
4075 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4077 int j, nregs;
4079 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4080 for (j = 0; j < nregs; j++)
4081 df_set_regs_ever_live (hard_regno + j, true);
4083 /* Do elimination before the equivalence processing as we can spill
4084 some pseudos during elimination. */
4085 lra_eliminate (false, first_p);
4086 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4087 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4088 if (lra_reg_info[i].nrefs != 0)
4090 ira_reg_equiv[i].profitable_p = true;
4091 reg = regno_reg_rtx[i];
4092 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4094 bool pseudo_p = contains_reg_p (x, false, false);
4096 /* After RTL transformation, we can not guarantee that
4097 pseudo in the substitution was not reloaded which might
4098 make equivalence invalid. For example, in reverse
4099 equiv of p0
4101 p0 <- ...
4103 equiv_mem <- p0
4105 the memory address register was reloaded before the 2nd
4106 insn. */
4107 if ((! first_p && pseudo_p)
4108 /* We don't use DF for compilation speed sake. So it
4109 is problematic to update live info when we use an
4110 equivalence containing pseudos in more than one
4111 BB. */
4112 || (pseudo_p && multi_block_pseudo_p (i))
4113 /* If an init insn was deleted for some reason, cancel
4114 the equiv. We could update the equiv insns after
4115 transformations including an equiv insn deletion
4116 but it is not worthy as such cases are extremely
4117 rare. */
4118 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4119 /* If it is not a reverse equivalence, we check that a
4120 pseudo in rhs of the init insn is not dying in the
4121 insn. Otherwise, the live info at the beginning of
4122 the corresponding BB might be wrong after we
4123 removed the insn. When the equiv can be a
4124 constant, the right hand side of the init insn can
4125 be a pseudo. */
4126 || (! reverse_equiv_p (i)
4127 && (init_insn_rhs_dead_pseudo_p (i)
4128 /* If we reloaded the pseudo in an equivalence
4129 init insn, we can not remove the equiv init
4130 insns and the init insns might write into
4131 const memory in this case. */
4132 || contains_reloaded_insn_p (i)))
4133 /* Prevent access beyond equivalent memory for
4134 paradoxical subregs. */
4135 || (MEM_P (x)
4136 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4137 > GET_MODE_SIZE (GET_MODE (x)))))
4138 ira_reg_equiv[i].defined_p = false;
4139 if (contains_reg_p (x, false, true))
4140 ira_reg_equiv[i].profitable_p = false;
4141 if (get_equiv (reg) != reg)
4142 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4145 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4146 update_equiv (i);
4147 /* We should add all insns containing pseudos which should be
4148 substituted by their equivalences. */
4149 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4150 lra_push_insn_by_uid (uid);
4151 min_len = lra_insn_stack_length ();
4152 new_insns_num = 0;
4153 last_bb = NULL;
4154 changed_p = false;
4155 while ((new_min_len = lra_insn_stack_length ()) != 0)
4157 curr_insn = lra_pop_insn ();
4158 --new_min_len;
4159 curr_bb = BLOCK_FOR_INSN (curr_insn);
4160 if (curr_bb != last_bb)
4162 last_bb = curr_bb;
4163 bb_reload_num = lra_curr_reload_num;
4165 if (min_len > new_min_len)
4167 min_len = new_min_len;
4168 new_insns_num = 0;
4170 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4171 internal_error
4172 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4173 MAX_RELOAD_INSNS_NUMBER);
4174 new_insns_num++;
4175 if (DEBUG_INSN_P (curr_insn))
4177 /* We need to check equivalence in debug insn and change
4178 pseudo to the equivalent value if necessary. */
4179 curr_id = lra_get_insn_recog_data (curr_insn);
4180 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4182 rtx old = *curr_id->operand_loc[0];
4183 *curr_id->operand_loc[0]
4184 = simplify_replace_fn_rtx (old, NULL_RTX,
4185 loc_equivalence_callback, curr_insn);
4186 if (old != *curr_id->operand_loc[0])
4188 lra_update_insn_regno_info (curr_insn);
4189 changed_p = true;
4193 else if (INSN_P (curr_insn))
4195 if ((set = single_set (curr_insn)) != NULL_RTX)
4197 dest_reg = SET_DEST (set);
4198 /* The equivalence pseudo could be set up as SUBREG in a
4199 case when it is a call restore insn in a mode
4200 different from the pseudo mode. */
4201 if (GET_CODE (dest_reg) == SUBREG)
4202 dest_reg = SUBREG_REG (dest_reg);
4203 if ((REG_P (dest_reg)
4204 && (x = get_equiv (dest_reg)) != dest_reg
4205 /* Remove insns which set up a pseudo whose value
4206 can not be changed. Such insns might be not in
4207 init_insns because we don't update equiv data
4208 during insn transformations.
4210 As an example, let suppose that a pseudo got
4211 hard register and on the 1st pass was not
4212 changed to equivalent constant. We generate an
4213 additional insn setting up the pseudo because of
4214 secondary memory movement. Then the pseudo is
4215 spilled and we use the equiv constant. In this
4216 case we should remove the additional insn and
4217 this insn is not init_insns list. */
4218 && (! MEM_P (x) || MEM_READONLY_P (x)
4219 /* Check that this is actually an insn setting
4220 up the equivalence. */
4221 || in_list_p (curr_insn,
4222 ira_reg_equiv
4223 [REGNO (dest_reg)].init_insns)))
4224 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4225 && in_list_p (curr_insn,
4226 ira_reg_equiv
4227 [REGNO (SET_SRC (set))].init_insns)))
4229 /* This is equiv init insn of pseudo which did not get a
4230 hard register -- remove the insn. */
4231 if (lra_dump_file != NULL)
4233 fprintf (lra_dump_file,
4234 " Removing equiv init insn %i (freq=%d)\n",
4235 INSN_UID (curr_insn),
4236 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4237 dump_insn_slim (lra_dump_file, curr_insn);
4239 if (contains_reg_p (x, true, false))
4240 lra_risky_transformations_p = true;
4241 lra_set_insn_deleted (curr_insn);
4242 continue;
4245 curr_id = lra_get_insn_recog_data (curr_insn);
4246 curr_static_id = curr_id->insn_static_data;
4247 init_curr_insn_input_reloads ();
4248 init_curr_operand_mode ();
4249 if (curr_insn_transform ())
4250 changed_p = true;
4251 /* Check non-transformed insns too for equiv change as USE
4252 or CLOBBER don't need reloads but can contain pseudos
4253 being changed on their equivalences. */
4254 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4255 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4257 lra_update_insn_regno_info (curr_insn);
4258 changed_p = true;
4262 bitmap_clear (&equiv_insn_bitmap);
4263 /* If we used a new hard regno, changed_p should be true because the
4264 hard reg is assigned to a new pseudo. */
4265 #ifdef ENABLE_CHECKING
4266 if (! changed_p)
4268 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4269 if (lra_reg_info[i].nrefs != 0
4270 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4272 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4274 for (j = 0; j < nregs; j++)
4275 lra_assert (df_regs_ever_live_p (hard_regno + j));
4278 #endif
4279 return changed_p;
4282 /* Initiate the LRA constraint pass. It is done once per
4283 function. */
4284 void
4285 lra_constraints_init (void)
4289 /* Finalize the LRA constraint pass. It is done once per
4290 function. */
4291 void
4292 lra_constraints_finish (void)
4298 /* This page contains code to do inheritance/split
4299 transformations. */
4301 /* Number of reloads passed so far in current EBB. */
4302 static int reloads_num;
4304 /* Number of calls passed so far in current EBB. */
4305 static int calls_num;
4307 /* Current reload pseudo check for validity of elements in
4308 USAGE_INSNS. */
4309 static int curr_usage_insns_check;
4311 /* Info about last usage of registers in EBB to do inheritance/split
4312 transformation. Inheritance transformation is done from a spilled
4313 pseudo and split transformations from a hard register or a pseudo
4314 assigned to a hard register. */
4315 struct usage_insns
4317 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4318 value INSNS is valid. The insns is chain of optional debug insns
4319 and a finishing non-debug insn using the corresponding reg. The
4320 value is also used to mark the registers which are set up in the
4321 current insn. The negated insn uid is used for this. */
4322 int check;
4323 /* Value of global reloads_num at the last insn in INSNS. */
4324 int reloads_num;
4325 /* Value of global reloads_nums at the last insn in INSNS. */
4326 int calls_num;
4327 /* It can be true only for splitting. And it means that the restore
4328 insn should be put after insn given by the following member. */
4329 bool after_p;
4330 /* Next insns in the current EBB which use the original reg and the
4331 original reg value is not changed between the current insn and
4332 the next insns. In order words, e.g. for inheritance, if we need
4333 to use the original reg value again in the next insns we can try
4334 to use the value in a hard register from a reload insn of the
4335 current insn. */
4336 rtx insns;
4339 /* Map: regno -> corresponding pseudo usage insns. */
4340 static struct usage_insns *usage_insns;
4342 static void
4343 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4345 usage_insns[regno].check = curr_usage_insns_check;
4346 usage_insns[regno].insns = insn;
4347 usage_insns[regno].reloads_num = reloads_num;
4348 usage_insns[regno].calls_num = calls_num;
4349 usage_insns[regno].after_p = after_p;
4352 /* The function is used to form list REGNO usages which consists of
4353 optional debug insns finished by a non-debug insn using REGNO.
4354 RELOADS_NUM is current number of reload insns processed so far. */
4355 static void
4356 add_next_usage_insn (int regno, rtx insn, int reloads_num)
4358 rtx next_usage_insns;
4360 if (usage_insns[regno].check == curr_usage_insns_check
4361 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4362 && DEBUG_INSN_P (insn))
4364 /* Check that we did not add the debug insn yet. */
4365 if (next_usage_insns != insn
4366 && (GET_CODE (next_usage_insns) != INSN_LIST
4367 || XEXP (next_usage_insns, 0) != insn))
4368 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4369 next_usage_insns);
4371 else if (NONDEBUG_INSN_P (insn))
4372 setup_next_usage_insn (regno, insn, reloads_num, false);
4373 else
4374 usage_insns[regno].check = 0;
4377 /* Replace all references to register OLD_REGNO in *LOC with pseudo
4378 register NEW_REG. Return true if any change was made. */
4379 static bool
4380 substitute_pseudo (rtx *loc, int old_regno, rtx new_reg)
4382 rtx x = *loc;
4383 bool result = false;
4384 enum rtx_code code;
4385 const char *fmt;
4386 int i, j;
4388 if (x == NULL_RTX)
4389 return false;
4391 code = GET_CODE (x);
4392 if (code == REG && (int) REGNO (x) == old_regno)
4394 enum machine_mode mode = GET_MODE (*loc);
4395 enum machine_mode inner_mode = GET_MODE (new_reg);
4397 if (mode != inner_mode)
4399 if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
4400 || ! SCALAR_INT_MODE_P (inner_mode))
4401 new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
4402 else
4403 new_reg = gen_lowpart_SUBREG (mode, new_reg);
4405 *loc = new_reg;
4406 return true;
4409 /* Scan all the operand sub-expressions. */
4410 fmt = GET_RTX_FORMAT (code);
4411 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4413 if (fmt[i] == 'e')
4415 if (substitute_pseudo (&XEXP (x, i), old_regno, new_reg))
4416 result = true;
4418 else if (fmt[i] == 'E')
4420 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4421 if (substitute_pseudo (&XVECEXP (x, i, j), old_regno, new_reg))
4422 result = true;
4425 return result;
4428 /* Return first non-debug insn in list USAGE_INSNS. */
4429 static rtx
4430 skip_usage_debug_insns (rtx usage_insns)
4432 rtx insn;
4434 /* Skip debug insns. */
4435 for (insn = usage_insns;
4436 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4437 insn = XEXP (insn, 1))
4439 return insn;
4442 /* Return true if we need secondary memory moves for insn in
4443 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4444 into the insn. */
4445 static bool
4446 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4447 rtx usage_insns ATTRIBUTE_UNUSED)
4449 #ifndef SECONDARY_MEMORY_NEEDED
4450 return false;
4451 #else
4452 rtx insn, set, dest;
4453 enum reg_class cl;
4455 if (inher_cl == ALL_REGS
4456 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4457 return false;
4458 lra_assert (INSN_P (insn));
4459 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4460 return false;
4461 dest = SET_DEST (set);
4462 if (! REG_P (dest))
4463 return false;
4464 lra_assert (inher_cl != NO_REGS);
4465 cl = get_reg_class (REGNO (dest));
4466 return (cl != NO_REGS && cl != ALL_REGS
4467 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4468 #endif
4471 /* Registers involved in inheritance/split in the current EBB
4472 (inheritance/split pseudos and original registers). */
4473 static bitmap_head check_only_regs;
4475 /* Do inheritance transformations for insn INSN, which defines (if
4476 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4477 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4478 form as the "insns" field of usage_insns. Return true if we
4479 succeed in such transformation.
4481 The transformations look like:
4483 p <- ... i <- ...
4484 ... p <- i (new insn)
4485 ... =>
4486 <- ... p ... <- ... i ...
4488 ... i <- p (new insn)
4489 <- ... p ... <- ... i ...
4490 ... =>
4491 <- ... p ... <- ... i ...
4492 where p is a spilled original pseudo and i is a new inheritance pseudo.
4495 The inheritance pseudo has the smallest class of two classes CL and
4496 class of ORIGINAL REGNO. */
4497 static bool
4498 inherit_reload_reg (bool def_p, int original_regno,
4499 enum reg_class cl, rtx insn, rtx next_usage_insns)
4501 if (optimize_function_for_size_p (cfun))
4502 return false;
4504 enum reg_class rclass = lra_get_allocno_class (original_regno);
4505 rtx original_reg = regno_reg_rtx[original_regno];
4506 rtx new_reg, new_insns, usage_insn;
4508 lra_assert (! usage_insns[original_regno].after_p);
4509 if (lra_dump_file != NULL)
4510 fprintf (lra_dump_file,
4511 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4512 if (! ira_reg_classes_intersect_p[cl][rclass])
4514 if (lra_dump_file != NULL)
4516 fprintf (lra_dump_file,
4517 " Rejecting inheritance for %d "
4518 "because of disjoint classes %s and %s\n",
4519 original_regno, reg_class_names[cl],
4520 reg_class_names[rclass]);
4521 fprintf (lra_dump_file,
4522 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4524 return false;
4526 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4527 /* We don't use a subset of two classes because it can be
4528 NO_REGS. This transformation is still profitable in most
4529 cases even if the classes are not intersected as register
4530 move is probably cheaper than a memory load. */
4531 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4533 if (lra_dump_file != NULL)
4534 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4535 reg_class_names[cl], reg_class_names[rclass]);
4537 rclass = cl;
4539 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4541 /* Reject inheritance resulting in secondary memory moves.
4542 Otherwise, there is a danger in LRA cycling. Also such
4543 transformation will be unprofitable. */
4544 if (lra_dump_file != NULL)
4546 rtx insn = skip_usage_debug_insns (next_usage_insns);
4547 rtx set = single_set (insn);
4549 lra_assert (set != NULL_RTX);
4551 rtx dest = SET_DEST (set);
4553 lra_assert (REG_P (dest));
4554 fprintf (lra_dump_file,
4555 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4556 "as secondary mem is needed\n",
4557 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4558 original_regno, reg_class_names[rclass]);
4559 fprintf (lra_dump_file,
4560 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4562 return false;
4564 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4565 rclass, "inheritance");
4566 start_sequence ();
4567 if (def_p)
4568 lra_emit_move (original_reg, new_reg);
4569 else
4570 lra_emit_move (new_reg, original_reg);
4571 new_insns = get_insns ();
4572 end_sequence ();
4573 if (NEXT_INSN (new_insns) != NULL_RTX)
4575 if (lra_dump_file != NULL)
4577 fprintf (lra_dump_file,
4578 " Rejecting inheritance %d->%d "
4579 "as it results in 2 or more insns:\n",
4580 original_regno, REGNO (new_reg));
4581 dump_rtl_slim (lra_dump_file, new_insns, NULL_RTX, -1, 0);
4582 fprintf (lra_dump_file,
4583 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4585 return false;
4587 substitute_pseudo (&insn, original_regno, new_reg);
4588 lra_update_insn_regno_info (insn);
4589 if (! def_p)
4590 /* We now have a new usage insn for original regno. */
4591 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4592 if (lra_dump_file != NULL)
4593 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4594 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4595 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4596 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4597 bitmap_set_bit (&check_only_regs, original_regno);
4598 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4599 if (def_p)
4600 lra_process_new_insns (insn, NULL_RTX, new_insns,
4601 "Add original<-inheritance");
4602 else
4603 lra_process_new_insns (insn, new_insns, NULL_RTX,
4604 "Add inheritance<-original");
4605 while (next_usage_insns != NULL_RTX)
4607 if (GET_CODE (next_usage_insns) != INSN_LIST)
4609 usage_insn = next_usage_insns;
4610 lra_assert (NONDEBUG_INSN_P (usage_insn));
4611 next_usage_insns = NULL;
4613 else
4615 usage_insn = XEXP (next_usage_insns, 0);
4616 lra_assert (DEBUG_INSN_P (usage_insn));
4617 next_usage_insns = XEXP (next_usage_insns, 1);
4619 substitute_pseudo (&usage_insn, original_regno, new_reg);
4620 lra_update_insn_regno_info (usage_insn);
4621 if (lra_dump_file != NULL)
4623 fprintf (lra_dump_file,
4624 " Inheritance reuse change %d->%d (bb%d):\n",
4625 original_regno, REGNO (new_reg),
4626 BLOCK_FOR_INSN (usage_insn)->index);
4627 dump_insn_slim (lra_dump_file, usage_insn);
4630 if (lra_dump_file != NULL)
4631 fprintf (lra_dump_file,
4632 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4633 return true;
4636 /* Return true if we need a caller save/restore for pseudo REGNO which
4637 was assigned to a hard register. */
4638 static inline bool
4639 need_for_call_save_p (int regno)
4641 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4642 return (usage_insns[regno].calls_num < calls_num
4643 && (overlaps_hard_reg_set_p
4644 (call_used_reg_set,
4645 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4646 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4647 PSEUDO_REGNO_MODE (regno))));
4650 /* Global registers occurring in the current EBB. */
4651 static bitmap_head ebb_global_regs;
4653 /* Return true if we need a split for hard register REGNO or pseudo
4654 REGNO which was assigned to a hard register.
4655 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4656 used for reloads since the EBB end. It is an approximation of the
4657 used hard registers in the split range. The exact value would
4658 require expensive calculations. If we were aggressive with
4659 splitting because of the approximation, the split pseudo will save
4660 the same hard register assignment and will be removed in the undo
4661 pass. We still need the approximation because too aggressive
4662 splitting would result in too inaccurate cost calculation in the
4663 assignment pass because of too many generated moves which will be
4664 probably removed in the undo pass. */
4665 static inline bool
4666 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4668 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4670 lra_assert (hard_regno >= 0);
4671 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4672 /* Don't split eliminable hard registers, otherwise we can
4673 split hard registers like hard frame pointer, which
4674 lives on BB start/end according to DF-infrastructure,
4675 when there is a pseudo assigned to the register and
4676 living in the same BB. */
4677 && (regno >= FIRST_PSEUDO_REGISTER
4678 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4679 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4680 /* Don't split call clobbered hard regs living through
4681 calls, otherwise we might have a check problem in the
4682 assign sub-pass as in the most cases (exception is a
4683 situation when lra_risky_transformations_p value is
4684 true) the assign pass assumes that all pseudos living
4685 through calls are assigned to call saved hard regs. */
4686 && (regno >= FIRST_PSEUDO_REGISTER
4687 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4688 || usage_insns[regno].calls_num == calls_num)
4689 /* We need at least 2 reloads to make pseudo splitting
4690 profitable. We should provide hard regno splitting in
4691 any case to solve 1st insn scheduling problem when
4692 moving hard register definition up might result in
4693 impossibility to find hard register for reload pseudo of
4694 small register class. */
4695 && (usage_insns[regno].reloads_num
4696 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4697 && (regno < FIRST_PSEUDO_REGISTER
4698 /* For short living pseudos, spilling + inheritance can
4699 be considered a substitution for splitting.
4700 Therefore we do not splitting for local pseudos. It
4701 decreases also aggressiveness of splitting. The
4702 minimal number of references is chosen taking into
4703 account that for 2 references splitting has no sense
4704 as we can just spill the pseudo. */
4705 || (regno >= FIRST_PSEUDO_REGISTER
4706 && lra_reg_info[regno].nrefs > 3
4707 && bitmap_bit_p (&ebb_global_regs, regno))))
4708 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4711 /* Return class for the split pseudo created from original pseudo with
4712 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4713 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4714 results in no secondary memory movements. */
4715 static enum reg_class
4716 choose_split_class (enum reg_class allocno_class,
4717 int hard_regno ATTRIBUTE_UNUSED,
4718 enum machine_mode mode ATTRIBUTE_UNUSED)
4720 #ifndef SECONDARY_MEMORY_NEEDED
4721 return allocno_class;
4722 #else
4723 int i;
4724 enum reg_class cl, best_cl = NO_REGS;
4725 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4726 = REGNO_REG_CLASS (hard_regno);
4728 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4729 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4730 return allocno_class;
4731 for (i = 0;
4732 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4733 i++)
4734 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4735 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4736 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4737 && (best_cl == NO_REGS
4738 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4739 best_cl = cl;
4740 return best_cl;
4741 #endif
4744 /* Do split transformations for insn INSN, which defines or uses
4745 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4746 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4747 "insns" field of usage_insns.
4749 The transformations look like:
4751 p <- ... p <- ...
4752 ... s <- p (new insn -- save)
4753 ... =>
4754 ... p <- s (new insn -- restore)
4755 <- ... p ... <- ... p ...
4757 <- ... p ... <- ... p ...
4758 ... s <- p (new insn -- save)
4759 ... =>
4760 ... p <- s (new insn -- restore)
4761 <- ... p ... <- ... p ...
4763 where p is an original pseudo got a hard register or a hard
4764 register and s is a new split pseudo. The save is put before INSN
4765 if BEFORE_P is true. Return true if we succeed in such
4766 transformation. */
4767 static bool
4768 split_reg (bool before_p, int original_regno, rtx insn, rtx next_usage_insns)
4770 enum reg_class rclass;
4771 rtx original_reg;
4772 int hard_regno, nregs;
4773 rtx new_reg, save, restore, usage_insn;
4774 bool after_p;
4775 bool call_save_p;
4777 if (original_regno < FIRST_PSEUDO_REGISTER)
4779 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4780 hard_regno = original_regno;
4781 call_save_p = false;
4782 nregs = 1;
4784 else
4786 hard_regno = reg_renumber[original_regno];
4787 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4788 rclass = lra_get_allocno_class (original_regno);
4789 original_reg = regno_reg_rtx[original_regno];
4790 call_save_p = need_for_call_save_p (original_regno);
4792 original_reg = regno_reg_rtx[original_regno];
4793 lra_assert (hard_regno >= 0);
4794 if (lra_dump_file != NULL)
4795 fprintf (lra_dump_file,
4796 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4797 if (call_save_p)
4799 enum machine_mode mode = GET_MODE (original_reg);
4801 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4802 hard_regno_nregs[hard_regno][mode],
4803 mode);
4804 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4806 else
4808 rclass = choose_split_class (rclass, hard_regno,
4809 GET_MODE (original_reg));
4810 if (rclass == NO_REGS)
4812 if (lra_dump_file != NULL)
4814 fprintf (lra_dump_file,
4815 " Rejecting split of %d(%s): "
4816 "no good reg class for %d(%s)\n",
4817 original_regno,
4818 reg_class_names[lra_get_allocno_class (original_regno)],
4819 hard_regno,
4820 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4821 fprintf
4822 (lra_dump_file,
4823 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4825 return false;
4827 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4828 rclass, "split");
4829 reg_renumber[REGNO (new_reg)] = hard_regno;
4831 save = emit_spill_move (true, new_reg, original_reg);
4832 if (NEXT_INSN (save) != NULL_RTX)
4834 lra_assert (! call_save_p);
4835 if (lra_dump_file != NULL)
4837 fprintf
4838 (lra_dump_file,
4839 " Rejecting split %d->%d resulting in > 2 %s save insns:\n",
4840 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4841 dump_rtl_slim (lra_dump_file, save, NULL_RTX, -1, 0);
4842 fprintf (lra_dump_file,
4843 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4845 return false;
4847 restore = emit_spill_move (false, new_reg, original_reg);
4848 if (NEXT_INSN (restore) != NULL_RTX)
4850 lra_assert (! call_save_p);
4851 if (lra_dump_file != NULL)
4853 fprintf (lra_dump_file,
4854 " Rejecting split %d->%d "
4855 "resulting in > 2 %s restore insns:\n",
4856 original_regno, REGNO (new_reg), call_save_p ? "call" : "");
4857 dump_rtl_slim (lra_dump_file, restore, NULL_RTX, -1, 0);
4858 fprintf (lra_dump_file,
4859 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4861 return false;
4863 after_p = usage_insns[original_regno].after_p;
4864 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4865 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4866 bitmap_set_bit (&check_only_regs, original_regno);
4867 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
4868 for (;;)
4870 if (GET_CODE (next_usage_insns) != INSN_LIST)
4872 usage_insn = next_usage_insns;
4873 break;
4875 usage_insn = XEXP (next_usage_insns, 0);
4876 lra_assert (DEBUG_INSN_P (usage_insn));
4877 next_usage_insns = XEXP (next_usage_insns, 1);
4878 substitute_pseudo (&usage_insn, original_regno, new_reg);
4879 lra_update_insn_regno_info (usage_insn);
4880 if (lra_dump_file != NULL)
4882 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
4883 original_regno, REGNO (new_reg));
4884 dump_insn_slim (lra_dump_file, usage_insn);
4887 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
4888 lra_assert (usage_insn != insn || (after_p && before_p));
4889 lra_process_new_insns (usage_insn, after_p ? NULL_RTX : restore,
4890 after_p ? restore : NULL_RTX,
4891 call_save_p
4892 ? "Add reg<-save" : "Add reg<-split");
4893 lra_process_new_insns (insn, before_p ? save : NULL_RTX,
4894 before_p ? NULL_RTX : save,
4895 call_save_p
4896 ? "Add save<-reg" : "Add split<-reg");
4897 if (nregs > 1)
4898 /* If we are trying to split multi-register. We should check
4899 conflicts on the next assignment sub-pass. IRA can allocate on
4900 sub-register levels, LRA do this on pseudos level right now and
4901 this discrepancy may create allocation conflicts after
4902 splitting. */
4903 lra_risky_transformations_p = true;
4904 if (lra_dump_file != NULL)
4905 fprintf (lra_dump_file,
4906 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4907 return true;
4910 /* Recognize that we need a split transformation for insn INSN, which
4911 defines or uses REGNO in its insn biggest MODE (we use it only if
4912 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
4913 hard registers which might be used for reloads since the EBB end.
4914 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
4915 uid before starting INSN processing. Return true if we succeed in
4916 such transformation. */
4917 static bool
4918 split_if_necessary (int regno, enum machine_mode mode,
4919 HARD_REG_SET potential_reload_hard_regs,
4920 bool before_p, rtx insn, int max_uid)
4922 bool res = false;
4923 int i, nregs = 1;
4924 rtx next_usage_insns;
4926 if (regno < FIRST_PSEUDO_REGISTER)
4927 nregs = hard_regno_nregs[regno][mode];
4928 for (i = 0; i < nregs; i++)
4929 if (usage_insns[regno + i].check == curr_usage_insns_check
4930 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
4931 /* To avoid processing the register twice or more. */
4932 && ((GET_CODE (next_usage_insns) != INSN_LIST
4933 && INSN_UID (next_usage_insns) < max_uid)
4934 || (GET_CODE (next_usage_insns) == INSN_LIST
4935 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
4936 && need_for_split_p (potential_reload_hard_regs, regno + i)
4937 && split_reg (before_p, regno + i, insn, next_usage_insns))
4938 res = true;
4939 return res;
4942 /* Check only registers living at the current program point in the
4943 current EBB. */
4944 static bitmap_head live_regs;
4946 /* Update live info in EBB given by its HEAD and TAIL insns after
4947 inheritance/split transformation. The function removes dead moves
4948 too. */
4949 static void
4950 update_ebb_live_info (rtx head, rtx tail)
4952 unsigned int j;
4953 int i, regno;
4954 bool live_p;
4955 rtx prev_insn, set;
4956 bool remove_p;
4957 basic_block last_bb, prev_bb, curr_bb;
4958 bitmap_iterator bi;
4959 struct lra_insn_reg *reg;
4960 edge e;
4961 edge_iterator ei;
4963 last_bb = BLOCK_FOR_INSN (tail);
4964 prev_bb = NULL;
4965 for (curr_insn = tail;
4966 curr_insn != PREV_INSN (head);
4967 curr_insn = prev_insn)
4969 prev_insn = PREV_INSN (curr_insn);
4970 /* We need to process empty blocks too. They contain
4971 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
4972 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
4973 continue;
4974 curr_bb = BLOCK_FOR_INSN (curr_insn);
4975 if (curr_bb != prev_bb)
4977 if (prev_bb != NULL)
4979 /* Update df_get_live_in (prev_bb): */
4980 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4981 if (bitmap_bit_p (&live_regs, j))
4982 bitmap_set_bit (df_get_live_in (prev_bb), j);
4983 else
4984 bitmap_clear_bit (df_get_live_in (prev_bb), j);
4986 if (curr_bb != last_bb)
4988 /* Update df_get_live_out (curr_bb): */
4989 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
4991 live_p = bitmap_bit_p (&live_regs, j);
4992 if (! live_p)
4993 FOR_EACH_EDGE (e, ei, curr_bb->succs)
4994 if (bitmap_bit_p (df_get_live_in (e->dest), j))
4996 live_p = true;
4997 break;
4999 if (live_p)
5000 bitmap_set_bit (df_get_live_out (curr_bb), j);
5001 else
5002 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5005 prev_bb = curr_bb;
5006 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5008 if (! NONDEBUG_INSN_P (curr_insn))
5009 continue;
5010 curr_id = lra_get_insn_recog_data (curr_insn);
5011 curr_static_id = curr_id->insn_static_data;
5012 remove_p = false;
5013 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
5014 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5015 && bitmap_bit_p (&check_only_regs, regno)
5016 && ! bitmap_bit_p (&live_regs, regno))
5017 remove_p = true;
5018 /* See which defined values die here. */
5019 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5020 if (reg->type == OP_OUT && ! reg->subreg_p)
5021 bitmap_clear_bit (&live_regs, reg->regno);
5022 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5023 if (reg->type == OP_OUT && ! reg->subreg_p)
5024 bitmap_clear_bit (&live_regs, reg->regno);
5025 /* Mark each used value as live. */
5026 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5027 if (reg->type != OP_OUT
5028 && bitmap_bit_p (&check_only_regs, reg->regno))
5029 bitmap_set_bit (&live_regs, reg->regno);
5030 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5031 if (reg->type != OP_OUT
5032 && bitmap_bit_p (&check_only_regs, reg->regno))
5033 bitmap_set_bit (&live_regs, reg->regno);
5034 if (curr_id->arg_hard_regs != NULL)
5035 /* Make argument hard registers live. */
5036 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5037 if (bitmap_bit_p (&check_only_regs, regno))
5038 bitmap_set_bit (&live_regs, regno);
5039 /* It is quite important to remove dead move insns because it
5040 means removing dead store. We don't need to process them for
5041 constraints. */
5042 if (remove_p)
5044 if (lra_dump_file != NULL)
5046 fprintf (lra_dump_file, " Removing dead insn:\n ");
5047 dump_insn_slim (lra_dump_file, curr_insn);
5049 lra_set_insn_deleted (curr_insn);
5054 /* The structure describes info to do an inheritance for the current
5055 insn. We need to collect such info first before doing the
5056 transformations because the transformations change the insn
5057 internal representation. */
5058 struct to_inherit
5060 /* Original regno. */
5061 int regno;
5062 /* Subsequent insns which can inherit original reg value. */
5063 rtx insns;
5066 /* Array containing all info for doing inheritance from the current
5067 insn. */
5068 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5070 /* Number elements in the previous array. */
5071 static int to_inherit_num;
5073 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5074 structure to_inherit. */
5075 static void
5076 add_to_inherit (int regno, rtx insns)
5078 int i;
5080 for (i = 0; i < to_inherit_num; i++)
5081 if (to_inherit[i].regno == regno)
5082 return;
5083 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5084 to_inherit[to_inherit_num].regno = regno;
5085 to_inherit[to_inherit_num++].insns = insns;
5088 /* Return the last non-debug insn in basic block BB, or the block begin
5089 note if none. */
5090 static rtx
5091 get_last_insertion_point (basic_block bb)
5093 rtx insn;
5095 FOR_BB_INSNS_REVERSE (bb, insn)
5096 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5097 return insn;
5098 gcc_unreachable ();
5101 /* Set up RES by registers living on edges FROM except the edge (FROM,
5102 TO) or by registers set up in a jump insn in BB FROM. */
5103 static void
5104 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5106 rtx last;
5107 struct lra_insn_reg *reg;
5108 edge e;
5109 edge_iterator ei;
5111 lra_assert (to != NULL);
5112 bitmap_clear (res);
5113 FOR_EACH_EDGE (e, ei, from->succs)
5114 if (e->dest != to)
5115 bitmap_ior_into (res, df_get_live_in (e->dest));
5116 last = get_last_insertion_point (from);
5117 if (! JUMP_P (last))
5118 return;
5119 curr_id = lra_get_insn_recog_data (last);
5120 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5121 if (reg->type != OP_IN)
5122 bitmap_set_bit (res, reg->regno);
5125 /* Used as a temporary results of some bitmap calculations. */
5126 static bitmap_head temp_bitmap;
5128 /* We split for reloads of small class of hard regs. The following
5129 defines how many hard regs the class should have to be qualified as
5130 small. The code is mostly oriented to x86/x86-64 architecture
5131 where some insns need to use only specific register or pair of
5132 registers and these register can live in RTL explicitly, e.g. for
5133 parameter passing. */
5134 static const int max_small_class_regs_num = 2;
5136 /* Do inheritance/split transformations in EBB starting with HEAD and
5137 finishing on TAIL. We process EBB insns in the reverse order.
5138 Return true if we did any inheritance/split transformation in the
5139 EBB.
5141 We should avoid excessive splitting which results in worse code
5142 because of inaccurate cost calculations for spilling new split
5143 pseudos in such case. To achieve this we do splitting only if
5144 register pressure is high in given basic block and there are reload
5145 pseudos requiring hard registers. We could do more register
5146 pressure calculations at any given program point to avoid necessary
5147 splitting even more but it is to expensive and the current approach
5148 works well enough. */
5149 static bool
5150 inherit_in_ebb (rtx head, rtx tail)
5152 int i, src_regno, dst_regno, nregs;
5153 bool change_p, succ_p, update_reloads_num_p;
5154 rtx prev_insn, next_usage_insns, set, last_insn;
5155 enum reg_class cl;
5156 struct lra_insn_reg *reg;
5157 basic_block last_processed_bb, curr_bb = NULL;
5158 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5159 bitmap to_process;
5160 unsigned int j;
5161 bitmap_iterator bi;
5162 bool head_p, after_p;
5164 change_p = false;
5165 curr_usage_insns_check++;
5166 reloads_num = calls_num = 0;
5167 bitmap_clear (&check_only_regs);
5168 last_processed_bb = NULL;
5169 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5170 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5171 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5172 /* We don't process new insns generated in the loop. */
5173 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5175 prev_insn = PREV_INSN (curr_insn);
5176 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5177 curr_bb = BLOCK_FOR_INSN (curr_insn);
5178 if (last_processed_bb != curr_bb)
5180 /* We are at the end of BB. Add qualified living
5181 pseudos for potential splitting. */
5182 to_process = df_get_live_out (curr_bb);
5183 if (last_processed_bb != NULL)
5185 /* We are somewhere in the middle of EBB. */
5186 get_live_on_other_edges (curr_bb, last_processed_bb,
5187 &temp_bitmap);
5188 to_process = &temp_bitmap;
5190 last_processed_bb = curr_bb;
5191 last_insn = get_last_insertion_point (curr_bb);
5192 after_p = (! JUMP_P (last_insn)
5193 && (! CALL_P (last_insn)
5194 || (find_reg_note (last_insn,
5195 REG_NORETURN, NULL_RTX) == NULL_RTX
5196 && ! SIBLING_CALL_P (last_insn))));
5197 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5198 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5200 if ((int) j >= lra_constraint_new_regno_start)
5201 break;
5202 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5204 if (j < FIRST_PSEUDO_REGISTER)
5205 SET_HARD_REG_BIT (live_hard_regs, j);
5206 else
5207 add_to_hard_reg_set (&live_hard_regs,
5208 PSEUDO_REGNO_MODE (j),
5209 reg_renumber[j]);
5210 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5214 src_regno = dst_regno = -1;
5215 if (NONDEBUG_INSN_P (curr_insn)
5216 && (set = single_set (curr_insn)) != NULL_RTX
5217 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5219 src_regno = REGNO (SET_SRC (set));
5220 dst_regno = REGNO (SET_DEST (set));
5222 update_reloads_num_p = true;
5223 if (src_regno < lra_constraint_new_regno_start
5224 && src_regno >= FIRST_PSEUDO_REGISTER
5225 && reg_renumber[src_regno] < 0
5226 && dst_regno >= lra_constraint_new_regno_start
5227 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5229 /* 'reload_pseudo <- original_pseudo'. */
5230 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5231 reloads_num++;
5232 update_reloads_num_p = false;
5233 succ_p = false;
5234 if (usage_insns[src_regno].check == curr_usage_insns_check
5235 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5236 succ_p = inherit_reload_reg (false, src_regno, cl,
5237 curr_insn, next_usage_insns);
5238 if (succ_p)
5239 change_p = true;
5240 else
5241 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5242 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5243 IOR_HARD_REG_SET (potential_reload_hard_regs,
5244 reg_class_contents[cl]);
5246 else if (src_regno >= lra_constraint_new_regno_start
5247 && dst_regno < lra_constraint_new_regno_start
5248 && dst_regno >= FIRST_PSEUDO_REGISTER
5249 && reg_renumber[dst_regno] < 0
5250 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5251 && usage_insns[dst_regno].check == curr_usage_insns_check
5252 && (next_usage_insns
5253 = usage_insns[dst_regno].insns) != NULL_RTX)
5255 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5256 reloads_num++;
5257 update_reloads_num_p = false;
5258 /* 'original_pseudo <- reload_pseudo'. */
5259 if (! JUMP_P (curr_insn)
5260 && inherit_reload_reg (true, dst_regno, cl,
5261 curr_insn, next_usage_insns))
5262 change_p = true;
5263 /* Invalidate. */
5264 usage_insns[dst_regno].check = 0;
5265 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5266 IOR_HARD_REG_SET (potential_reload_hard_regs,
5267 reg_class_contents[cl]);
5269 else if (INSN_P (curr_insn))
5271 int iter;
5272 int max_uid = get_max_uid ();
5274 curr_id = lra_get_insn_recog_data (curr_insn);
5275 curr_static_id = curr_id->insn_static_data;
5276 to_inherit_num = 0;
5277 /* Process insn definitions. */
5278 for (iter = 0; iter < 2; iter++)
5279 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5280 reg != NULL;
5281 reg = reg->next)
5282 if (reg->type != OP_IN
5283 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5285 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5286 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5287 && usage_insns[dst_regno].check == curr_usage_insns_check
5288 && (next_usage_insns
5289 = usage_insns[dst_regno].insns) != NULL_RTX)
5291 struct lra_insn_reg *r;
5293 for (r = curr_id->regs; r != NULL; r = r->next)
5294 if (r->type != OP_OUT && r->regno == dst_regno)
5295 break;
5296 /* Don't do inheritance if the pseudo is also
5297 used in the insn. */
5298 if (r == NULL)
5299 /* We can not do inheritance right now
5300 because the current insn reg info (chain
5301 regs) can change after that. */
5302 add_to_inherit (dst_regno, next_usage_insns);
5304 /* We can not process one reg twice here because of
5305 usage_insns invalidation. */
5306 if ((dst_regno < FIRST_PSEUDO_REGISTER
5307 || reg_renumber[dst_regno] >= 0)
5308 && ! reg->subreg_p && reg->type != OP_IN)
5310 HARD_REG_SET s;
5312 if (split_if_necessary (dst_regno, reg->biggest_mode,
5313 potential_reload_hard_regs,
5314 false, curr_insn, max_uid))
5315 change_p = true;
5316 CLEAR_HARD_REG_SET (s);
5317 if (dst_regno < FIRST_PSEUDO_REGISTER)
5318 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5319 else
5320 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5321 reg_renumber[dst_regno]);
5322 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5324 /* We should invalidate potential inheritance or
5325 splitting for the current insn usages to the next
5326 usage insns (see code below) as the output pseudo
5327 prevents this. */
5328 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5329 && reg_renumber[dst_regno] < 0)
5330 || (reg->type == OP_OUT && ! reg->subreg_p
5331 && (dst_regno < FIRST_PSEUDO_REGISTER
5332 || reg_renumber[dst_regno] >= 0)))
5334 /* Invalidate and mark definitions. */
5335 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5336 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5337 else
5339 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5340 for (i = 0; i < nregs; i++)
5341 usage_insns[dst_regno + i].check
5342 = -(int) INSN_UID (curr_insn);
5346 if (! JUMP_P (curr_insn))
5347 for (i = 0; i < to_inherit_num; i++)
5348 if (inherit_reload_reg (true, to_inherit[i].regno,
5349 ALL_REGS, curr_insn,
5350 to_inherit[i].insns))
5351 change_p = true;
5352 if (CALL_P (curr_insn))
5354 rtx cheap, pat, dest, restore;
5355 int regno, hard_regno;
5357 calls_num++;
5358 if ((cheap = find_reg_note (curr_insn,
5359 REG_RETURNED, NULL_RTX)) != NULL_RTX
5360 && ((cheap = XEXP (cheap, 0)), true)
5361 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5362 && (hard_regno = reg_renumber[regno]) >= 0
5363 /* If there are pending saves/restores, the
5364 optimization is not worth. */
5365 && usage_insns[regno].calls_num == calls_num - 1
5366 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5368 /* Restore the pseudo from the call result as
5369 REG_RETURNED note says that the pseudo value is
5370 in the call result and the pseudo is an argument
5371 of the call. */
5372 pat = PATTERN (curr_insn);
5373 if (GET_CODE (pat) == PARALLEL)
5374 pat = XVECEXP (pat, 0, 0);
5375 dest = SET_DEST (pat);
5376 start_sequence ();
5377 emit_move_insn (cheap, copy_rtx (dest));
5378 restore = get_insns ();
5379 end_sequence ();
5380 lra_process_new_insns (curr_insn, NULL, restore,
5381 "Inserting call parameter restore");
5382 /* We don't need to save/restore of the pseudo from
5383 this call. */
5384 usage_insns[regno].calls_num = calls_num;
5385 bitmap_set_bit (&check_only_regs, regno);
5388 to_inherit_num = 0;
5389 /* Process insn usages. */
5390 for (iter = 0; iter < 2; iter++)
5391 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5392 reg != NULL;
5393 reg = reg->next)
5394 if ((reg->type != OP_OUT
5395 || (reg->type == OP_OUT && reg->subreg_p))
5396 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5398 if (src_regno >= FIRST_PSEUDO_REGISTER
5399 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5401 if (usage_insns[src_regno].check == curr_usage_insns_check
5402 && (next_usage_insns
5403 = usage_insns[src_regno].insns) != NULL_RTX
5404 && NONDEBUG_INSN_P (curr_insn))
5405 add_to_inherit (src_regno, next_usage_insns);
5406 else if (usage_insns[src_regno].check
5407 != -(int) INSN_UID (curr_insn))
5408 /* Add usages but only if the reg is not set up
5409 in the same insn. */
5410 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5412 else if (src_regno < FIRST_PSEUDO_REGISTER
5413 || reg_renumber[src_regno] >= 0)
5415 bool before_p;
5416 rtx use_insn = curr_insn;
5418 before_p = (JUMP_P (curr_insn)
5419 || (CALL_P (curr_insn) && reg->type == OP_IN));
5420 if (NONDEBUG_INSN_P (curr_insn)
5421 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5422 && split_if_necessary (src_regno, reg->biggest_mode,
5423 potential_reload_hard_regs,
5424 before_p, curr_insn, max_uid))
5426 if (reg->subreg_p)
5427 lra_risky_transformations_p = true;
5428 change_p = true;
5429 /* Invalidate. */
5430 usage_insns[src_regno].check = 0;
5431 if (before_p)
5432 use_insn = PREV_INSN (curr_insn);
5434 if (NONDEBUG_INSN_P (curr_insn))
5436 if (src_regno < FIRST_PSEUDO_REGISTER)
5437 add_to_hard_reg_set (&live_hard_regs,
5438 reg->biggest_mode, src_regno);
5439 else
5440 add_to_hard_reg_set (&live_hard_regs,
5441 PSEUDO_REGNO_MODE (src_regno),
5442 reg_renumber[src_regno]);
5444 add_next_usage_insn (src_regno, use_insn, reloads_num);
5447 /* Process call args. */
5448 if (curr_id->arg_hard_regs != NULL)
5449 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5450 if (src_regno < FIRST_PSEUDO_REGISTER)
5452 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5453 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5455 for (i = 0; i < to_inherit_num; i++)
5457 src_regno = to_inherit[i].regno;
5458 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5459 curr_insn, to_inherit[i].insns))
5460 change_p = true;
5461 else
5462 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5465 if (update_reloads_num_p
5466 && NONDEBUG_INSN_P (curr_insn)
5467 && (set = single_set (curr_insn)) != NULL_RTX)
5469 int regno = -1;
5470 if ((REG_P (SET_DEST (set))
5471 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5472 && reg_renumber[regno] < 0
5473 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5474 || (REG_P (SET_SRC (set))
5475 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5476 && reg_renumber[regno] < 0
5477 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5479 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5480 reloads_num++;
5481 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5482 IOR_HARD_REG_SET (potential_reload_hard_regs,
5483 reg_class_contents[cl]);
5486 /* We reached the start of the current basic block. */
5487 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5488 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5490 /* We reached the beginning of the current block -- do
5491 rest of spliting in the current BB. */
5492 to_process = df_get_live_in (curr_bb);
5493 if (BLOCK_FOR_INSN (head) != curr_bb)
5495 /* We are somewhere in the middle of EBB. */
5496 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5497 curr_bb, &temp_bitmap);
5498 to_process = &temp_bitmap;
5500 head_p = true;
5501 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5503 if ((int) j >= lra_constraint_new_regno_start)
5504 break;
5505 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5506 && usage_insns[j].check == curr_usage_insns_check
5507 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5509 if (need_for_split_p (potential_reload_hard_regs, j))
5511 if (lra_dump_file != NULL && head_p)
5513 fprintf (lra_dump_file,
5514 " ----------------------------------\n");
5515 head_p = false;
5517 if (split_reg (false, j, bb_note (curr_bb),
5518 next_usage_insns))
5519 change_p = true;
5521 usage_insns[j].check = 0;
5526 return change_p;
5529 /* This value affects EBB forming. If probability of edge from EBB to
5530 a BB is not greater than the following value, we don't add the BB
5531 to EBB. */
5532 #define EBB_PROBABILITY_CUTOFF ((REG_BR_PROB_BASE * 50) / 100)
5534 /* Current number of inheritance/split iteration. */
5535 int lra_inheritance_iter;
5537 /* Entry function for inheritance/split pass. */
5538 void
5539 lra_inheritance (void)
5541 int i;
5542 basic_block bb, start_bb;
5543 edge e;
5545 lra_inheritance_iter++;
5546 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5547 return;
5548 timevar_push (TV_LRA_INHERITANCE);
5549 if (lra_dump_file != NULL)
5550 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5551 lra_inheritance_iter);
5552 curr_usage_insns_check = 0;
5553 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5554 for (i = 0; i < lra_constraint_new_regno_start; i++)
5555 usage_insns[i].check = 0;
5556 bitmap_initialize (&check_only_regs, &reg_obstack);
5557 bitmap_initialize (&live_regs, &reg_obstack);
5558 bitmap_initialize (&temp_bitmap, &reg_obstack);
5559 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5560 FOR_EACH_BB_FN (bb, cfun)
5562 start_bb = bb;
5563 if (lra_dump_file != NULL)
5564 fprintf (lra_dump_file, "EBB");
5565 /* Form a EBB starting with BB. */
5566 bitmap_clear (&ebb_global_regs);
5567 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5568 for (;;)
5570 if (lra_dump_file != NULL)
5571 fprintf (lra_dump_file, " %d", bb->index);
5572 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5573 || LABEL_P (BB_HEAD (bb->next_bb)))
5574 break;
5575 e = find_fallthru_edge (bb->succs);
5576 if (! e)
5577 break;
5578 if (e->probability <= EBB_PROBABILITY_CUTOFF)
5579 break;
5580 bb = bb->next_bb;
5582 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5583 if (lra_dump_file != NULL)
5584 fprintf (lra_dump_file, "\n");
5585 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5586 /* Remember that the EBB head and tail can change in
5587 inherit_in_ebb. */
5588 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5590 bitmap_clear (&ebb_global_regs);
5591 bitmap_clear (&temp_bitmap);
5592 bitmap_clear (&live_regs);
5593 bitmap_clear (&check_only_regs);
5594 free (usage_insns);
5596 timevar_pop (TV_LRA_INHERITANCE);
5601 /* This page contains code to undo failed inheritance/split
5602 transformations. */
5604 /* Current number of iteration undoing inheritance/split. */
5605 int lra_undo_inheritance_iter;
5607 /* Fix BB live info LIVE after removing pseudos created on pass doing
5608 inheritance/split which are REMOVED_PSEUDOS. */
5609 static void
5610 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5612 unsigned int regno;
5613 bitmap_iterator bi;
5615 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5616 if (bitmap_clear_bit (live, regno))
5617 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5620 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5621 number. */
5622 static int
5623 get_regno (rtx reg)
5625 if (GET_CODE (reg) == SUBREG)
5626 reg = SUBREG_REG (reg);
5627 if (REG_P (reg))
5628 return REGNO (reg);
5629 return -1;
5632 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5633 return true if we did any change. The undo transformations for
5634 inheritance looks like
5635 i <- i2
5636 p <- i => p <- i2
5637 or removing
5638 p <- i, i <- p, and i <- i3
5639 where p is original pseudo from which inheritance pseudo i was
5640 created, i and i3 are removed inheritance pseudos, i2 is another
5641 not removed inheritance pseudo. All split pseudos or other
5642 occurrences of removed inheritance pseudos are changed on the
5643 corresponding original pseudos.
5645 The function also schedules insns changed and created during
5646 inheritance/split pass for processing by the subsequent constraint
5647 pass. */
5648 static bool
5649 remove_inheritance_pseudos (bitmap remove_pseudos)
5651 basic_block bb;
5652 int regno, sregno, prev_sregno, dregno, restore_regno;
5653 rtx set, prev_set, prev_insn;
5654 bool change_p, done_p;
5656 change_p = ! bitmap_empty_p (remove_pseudos);
5657 /* We can not finish the function right away if CHANGE_P is true
5658 because we need to marks insns affected by previous
5659 inheritance/split pass for processing by the subsequent
5660 constraint pass. */
5661 FOR_EACH_BB_FN (bb, cfun)
5663 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5664 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5665 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5667 if (! INSN_P (curr_insn))
5668 continue;
5669 done_p = false;
5670 sregno = dregno = -1;
5671 if (change_p && NONDEBUG_INSN_P (curr_insn)
5672 && (set = single_set (curr_insn)) != NULL_RTX)
5674 dregno = get_regno (SET_DEST (set));
5675 sregno = get_regno (SET_SRC (set));
5678 if (sregno >= 0 && dregno >= 0)
5680 if ((bitmap_bit_p (remove_pseudos, sregno)
5681 && (lra_reg_info[sregno].restore_regno == dregno
5682 || (bitmap_bit_p (remove_pseudos, dregno)
5683 && (lra_reg_info[sregno].restore_regno
5684 == lra_reg_info[dregno].restore_regno))))
5685 || (bitmap_bit_p (remove_pseudos, dregno)
5686 && lra_reg_info[dregno].restore_regno == sregno))
5687 /* One of the following cases:
5688 original <- removed inheritance pseudo
5689 removed inherit pseudo <- another removed inherit pseudo
5690 removed inherit pseudo <- original pseudo
5692 removed_split_pseudo <- original_reg
5693 original_reg <- removed_split_pseudo */
5695 if (lra_dump_file != NULL)
5697 fprintf (lra_dump_file, " Removing %s:\n",
5698 bitmap_bit_p (&lra_split_regs, sregno)
5699 || bitmap_bit_p (&lra_split_regs, dregno)
5700 ? "split" : "inheritance");
5701 dump_insn_slim (lra_dump_file, curr_insn);
5703 lra_set_insn_deleted (curr_insn);
5704 done_p = true;
5706 else if (bitmap_bit_p (remove_pseudos, sregno)
5707 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5709 /* Search the following pattern:
5710 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5711 original_pseudo <- inherit_or_split_pseudo1
5712 where the 2nd insn is the current insn and
5713 inherit_or_split_pseudo2 is not removed. If it is found,
5714 change the current insn onto:
5715 original_pseudo <- inherit_or_split_pseudo2. */
5716 for (prev_insn = PREV_INSN (curr_insn);
5717 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5718 prev_insn = PREV_INSN (prev_insn))
5720 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5721 && (prev_set = single_set (prev_insn)) != NULL_RTX
5722 /* There should be no subregs in insn we are
5723 searching because only the original reg might
5724 be in subreg when we changed the mode of
5725 load/store for splitting. */
5726 && REG_P (SET_DEST (prev_set))
5727 && REG_P (SET_SRC (prev_set))
5728 && (int) REGNO (SET_DEST (prev_set)) == sregno
5729 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5730 >= FIRST_PSEUDO_REGISTER)
5731 /* As we consider chain of inheritance or
5732 splitting described in above comment we should
5733 check that sregno and prev_sregno were
5734 inheritance/split pseudos created from the
5735 same original regno. */
5736 && (lra_reg_info[sregno].restore_regno
5737 == lra_reg_info[prev_sregno].restore_regno)
5738 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5740 lra_assert (GET_MODE (SET_SRC (prev_set))
5741 == GET_MODE (regno_reg_rtx[sregno]));
5742 if (GET_CODE (SET_SRC (set)) == SUBREG)
5743 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5744 else
5745 SET_SRC (set) = SET_SRC (prev_set);
5746 /* As we are finishing with processing the insn
5747 here, check the destination too as it might
5748 inheritance pseudo for another pseudo. */
5749 if (bitmap_bit_p (remove_pseudos, dregno)
5750 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
5751 && (restore_regno
5752 = lra_reg_info[dregno].restore_regno) >= 0)
5754 if (GET_CODE (SET_DEST (set)) == SUBREG)
5755 SUBREG_REG (SET_DEST (set))
5756 = regno_reg_rtx[restore_regno];
5757 else
5758 SET_DEST (set) = regno_reg_rtx[restore_regno];
5760 lra_push_insn_and_update_insn_regno_info (curr_insn);
5761 lra_set_used_insn_alternative_by_uid
5762 (INSN_UID (curr_insn), -1);
5763 done_p = true;
5764 if (lra_dump_file != NULL)
5766 fprintf (lra_dump_file, " Change reload insn:\n");
5767 dump_insn_slim (lra_dump_file, curr_insn);
5772 if (! done_p)
5774 struct lra_insn_reg *reg;
5775 bool restored_regs_p = false;
5776 bool kept_regs_p = false;
5778 curr_id = lra_get_insn_recog_data (curr_insn);
5779 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5781 regno = reg->regno;
5782 restore_regno = lra_reg_info[regno].restore_regno;
5783 if (restore_regno >= 0)
5785 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5787 substitute_pseudo (&curr_insn, regno,
5788 regno_reg_rtx[restore_regno]);
5789 restored_regs_p = true;
5791 else
5792 kept_regs_p = true;
5795 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5797 /* The instruction has changed since the previous
5798 constraints pass. */
5799 lra_push_insn_and_update_insn_regno_info (curr_insn);
5800 lra_set_used_insn_alternative_by_uid
5801 (INSN_UID (curr_insn), -1);
5803 else if (restored_regs_p)
5804 /* The instruction has been restored to the form that
5805 it had during the previous constraints pass. */
5806 lra_update_insn_regno_info (curr_insn);
5807 if (restored_regs_p && lra_dump_file != NULL)
5809 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5810 dump_insn_slim (lra_dump_file, curr_insn);
5815 return change_p;
5818 /* If optional reload pseudos failed to get a hard register or was not
5819 inherited, it is better to remove optional reloads. We do this
5820 transformation after undoing inheritance to figure out necessity to
5821 remove optional reloads easier. Return true if we do any
5822 change. */
5823 static bool
5824 undo_optional_reloads (void)
5826 bool change_p, keep_p;
5827 unsigned int regno, uid;
5828 bitmap_iterator bi, bi2;
5829 rtx insn, set, src, dest;
5830 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
5832 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
5833 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
5834 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5836 keep_p = false;
5837 /* Keep optional reloads from previous subpasses. */
5838 if (lra_reg_info[regno].restore_regno < 0
5839 /* If the original pseudo changed its allocation, just
5840 removing the optional pseudo is dangerous as the original
5841 pseudo will have longer live range. */
5842 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
5843 keep_p = true;
5844 else if (reg_renumber[regno] >= 0)
5845 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
5847 insn = lra_insn_recog_data[uid]->insn;
5848 if ((set = single_set (insn)) == NULL_RTX)
5849 continue;
5850 src = SET_SRC (set);
5851 dest = SET_DEST (set);
5852 if (! REG_P (src) || ! REG_P (dest))
5853 continue;
5854 if (REGNO (dest) == regno
5855 /* Ignore insn for optional reloads itself. */
5856 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
5857 /* Check only inheritance on last inheritance pass. */
5858 && (int) REGNO (src) >= new_regno_start
5859 /* Check that the optional reload was inherited. */
5860 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
5862 keep_p = true;
5863 break;
5866 if (keep_p)
5868 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
5869 if (lra_dump_file != NULL)
5870 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
5873 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
5874 bitmap_initialize (&insn_bitmap, &reg_obstack);
5875 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
5877 if (lra_dump_file != NULL)
5878 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
5879 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
5880 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
5882 insn = lra_insn_recog_data[uid]->insn;
5883 if ((set = single_set (insn)) != NULL_RTX)
5885 src = SET_SRC (set);
5886 dest = SET_DEST (set);
5887 if (REG_P (src) && REG_P (dest)
5888 && ((REGNO (src) == regno
5889 && (lra_reg_info[regno].restore_regno
5890 == (int) REGNO (dest)))
5891 || (REGNO (dest) == regno
5892 && (lra_reg_info[regno].restore_regno
5893 == (int) REGNO (src)))))
5895 if (lra_dump_file != NULL)
5897 fprintf (lra_dump_file, " Deleting move %u\n",
5898 INSN_UID (insn));
5899 dump_insn_slim (lra_dump_file, insn);
5901 lra_set_insn_deleted (insn);
5902 continue;
5904 /* We should not worry about generation memory-memory
5905 moves here as if the corresponding inheritance did
5906 not work (inheritance pseudo did not get a hard reg),
5907 we remove the inheritance pseudo and the optional
5908 reload. */
5910 substitute_pseudo (&insn, regno,
5911 regno_reg_rtx[lra_reg_info[regno].restore_regno]);
5912 lra_update_insn_regno_info (insn);
5913 if (lra_dump_file != NULL)
5915 fprintf (lra_dump_file,
5916 " Restoring original insn:\n");
5917 dump_insn_slim (lra_dump_file, insn);
5921 /* Clear restore_regnos. */
5922 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5923 lra_reg_info[regno].restore_regno = -1;
5924 bitmap_clear (&insn_bitmap);
5925 bitmap_clear (&removed_optional_reload_pseudos);
5926 return change_p;
5929 /* Entry function for undoing inheritance/split transformation. Return true
5930 if we did any RTL change in this pass. */
5931 bool
5932 lra_undo_inheritance (void)
5934 unsigned int regno;
5935 int restore_regno, hard_regno;
5936 int n_all_inherit, n_inherit, n_all_split, n_split;
5937 bitmap_head remove_pseudos;
5938 bitmap_iterator bi;
5939 bool change_p;
5941 lra_undo_inheritance_iter++;
5942 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5943 return false;
5944 if (lra_dump_file != NULL)
5945 fprintf (lra_dump_file,
5946 "\n********** Undoing inheritance #%d: **********\n\n",
5947 lra_undo_inheritance_iter);
5948 bitmap_initialize (&remove_pseudos, &reg_obstack);
5949 n_inherit = n_all_inherit = 0;
5950 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5951 if (lra_reg_info[regno].restore_regno >= 0)
5953 n_all_inherit++;
5954 if (reg_renumber[regno] < 0
5955 /* If the original pseudo changed its allocation, just
5956 removing inheritance is dangerous as for changing
5957 allocation we used shorter live-ranges. */
5958 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
5959 bitmap_set_bit (&remove_pseudos, regno);
5960 else
5961 n_inherit++;
5963 if (lra_dump_file != NULL && n_all_inherit != 0)
5964 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
5965 n_inherit, n_all_inherit,
5966 (double) n_inherit / n_all_inherit * 100);
5967 n_split = n_all_split = 0;
5968 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5969 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
5971 n_all_split++;
5972 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
5973 ? reg_renumber[restore_regno] : restore_regno);
5974 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
5975 bitmap_set_bit (&remove_pseudos, regno);
5976 else
5978 n_split++;
5979 if (lra_dump_file != NULL)
5980 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
5981 regno, restore_regno);
5984 if (lra_dump_file != NULL && n_all_split != 0)
5985 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
5986 n_split, n_all_split,
5987 (double) n_split / n_all_split * 100);
5988 change_p = remove_inheritance_pseudos (&remove_pseudos);
5989 bitmap_clear (&remove_pseudos);
5990 /* Clear restore_regnos. */
5991 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
5992 lra_reg_info[regno].restore_regno = -1;
5993 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
5994 lra_reg_info[regno].restore_regno = -1;
5995 change_p = undo_optional_reloads () || change_p;
5996 return change_p;