S/390: Testsuite: Add asm scan patterns for -m31.
[official-gcc.git] / gcc / lra-constraints.c
bloba78edd8d344cf2485f3e8bde16999e3f1dc737e4
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2015 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 "backend.h"
113 #include "target.h"
114 #include "rtl.h"
115 #include "tree.h"
116 #include "predict.h"
117 #include "df.h"
118 #include "tm_p.h"
119 #include "expmed.h"
120 #include "optabs.h"
121 #include "regs.h"
122 #include "ira.h"
123 #include "recog.h"
124 #include "output.h"
125 #include "addresses.h"
126 #include "expr.h"
127 #include "cfgrtl.h"
128 #include "rtl-error.h"
129 #include "params.h"
130 #include "lra.h"
131 #include "lra-int.h"
132 #include "print-rtl.h"
134 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
135 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
136 reload insns. */
137 static int bb_reload_num;
139 /* The current insn being processed and corresponding its single set
140 (NULL otherwise), its data (basic block, the insn data, the insn
141 static data, and the mode of each operand). */
142 static rtx_insn *curr_insn;
143 static rtx curr_insn_set;
144 static basic_block curr_bb;
145 static lra_insn_recog_data_t curr_id;
146 static struct lra_static_insn_data *curr_static_id;
147 static machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
148 /* Mode of the register substituted by its equivalence with VOIDmode
149 (e.g. constant) and whose subreg is given operand of the current
150 insn. VOIDmode in all other cases. */
151 static machine_mode original_subreg_reg_mode[MAX_RECOG_OPERANDS];
155 /* Start numbers for new registers and insns at the current constraints
156 pass start. */
157 static int new_regno_start;
158 static int new_insn_uid_start;
160 /* If LOC is nonnull, strip any outer subreg from it. */
161 static inline rtx *
162 strip_subreg (rtx *loc)
164 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
167 /* Return hard regno of REGNO or if it is was not assigned to a hard
168 register, use a hard register from its allocno class. */
169 static int
170 get_try_hard_regno (int regno)
172 int hard_regno;
173 enum reg_class rclass;
175 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
176 hard_regno = lra_get_regno_hard_regno (regno);
177 if (hard_regno >= 0)
178 return hard_regno;
179 rclass = lra_get_allocno_class (regno);
180 if (rclass == NO_REGS)
181 return -1;
182 return ira_class_hard_regs[rclass][0];
185 /* Return final hard regno (plus offset) which will be after
186 elimination. We do this for matching constraints because the final
187 hard regno could have a different class. */
188 static int
189 get_final_hard_regno (int hard_regno, int offset)
191 if (hard_regno < 0)
192 return hard_regno;
193 hard_regno = lra_get_elimination_hard_regno (hard_regno);
194 return hard_regno + offset;
197 /* Return hard regno of X after removing subreg and making
198 elimination. If X is not a register or subreg of register, return
199 -1. For pseudo use its assignment. */
200 static int
201 get_hard_regno (rtx x)
203 rtx reg;
204 int offset, hard_regno;
206 reg = x;
207 if (GET_CODE (x) == SUBREG)
208 reg = SUBREG_REG (x);
209 if (! REG_P (reg))
210 return -1;
211 if ((hard_regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
212 hard_regno = lra_get_regno_hard_regno (hard_regno);
213 if (hard_regno < 0)
214 return -1;
215 offset = 0;
216 if (GET_CODE (x) == SUBREG)
217 offset += subreg_regno_offset (hard_regno, GET_MODE (reg),
218 SUBREG_BYTE (x), GET_MODE (x));
219 return get_final_hard_regno (hard_regno, offset);
222 /* If REGNO is a hard register or has been allocated a hard register,
223 return the class of that register. If REGNO is a reload pseudo
224 created by the current constraints pass, return its allocno class.
225 Return NO_REGS otherwise. */
226 static enum reg_class
227 get_reg_class (int regno)
229 int hard_regno;
231 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
232 hard_regno = lra_get_regno_hard_regno (regno);
233 if (hard_regno >= 0)
235 hard_regno = get_final_hard_regno (hard_regno, 0);
236 return REGNO_REG_CLASS (hard_regno);
238 if (regno >= new_regno_start)
239 return lra_get_allocno_class (regno);
240 return NO_REGS;
243 /* Return true if REG satisfies (or will satisfy) reg class constraint
244 CL. Use elimination first if REG is a hard register. If REG is a
245 reload pseudo created by this constraints pass, assume that it will
246 be allocated a hard register from its allocno class, but allow that
247 class to be narrowed to CL if it is currently a superset of CL.
249 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
250 REGNO (reg), or NO_REGS if no change in its class was needed. */
251 static bool
252 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class)
254 enum reg_class rclass, common_class;
255 machine_mode reg_mode;
256 int class_size, hard_regno, nregs, i, j;
257 int regno = REGNO (reg);
259 if (new_class != NULL)
260 *new_class = NO_REGS;
261 if (regno < FIRST_PSEUDO_REGISTER)
263 rtx final_reg = reg;
264 rtx *final_loc = &final_reg;
266 lra_eliminate_reg_if_possible (final_loc);
267 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
269 reg_mode = GET_MODE (reg);
270 rclass = get_reg_class (regno);
271 if (regno < new_regno_start
272 /* Do not allow the constraints for reload instructions to
273 influence the classes of new pseudos. These reloads are
274 typically moves that have many alternatives, and restricting
275 reload pseudos for one alternative may lead to situations
276 where other reload pseudos are no longer allocatable. */
277 || (INSN_UID (curr_insn) >= new_insn_uid_start
278 && curr_insn_set != NULL
279 && ((OBJECT_P (SET_SRC (curr_insn_set))
280 && ! CONSTANT_P (SET_SRC (curr_insn_set)))
281 || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
282 && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))
283 && ! CONSTANT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
284 /* When we don't know what class will be used finally for reload
285 pseudos, we use ALL_REGS. */
286 return ((regno >= new_regno_start && rclass == ALL_REGS)
287 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
288 && ! hard_reg_set_subset_p (reg_class_contents[cl],
289 lra_no_alloc_regs)));
290 else
292 common_class = ira_reg_class_subset[rclass][cl];
293 if (new_class != NULL)
294 *new_class = common_class;
295 if (hard_reg_set_subset_p (reg_class_contents[common_class],
296 lra_no_alloc_regs))
297 return false;
298 /* Check that there are enough allocatable regs. */
299 class_size = ira_class_hard_regs_num[common_class];
300 for (i = 0; i < class_size; i++)
302 hard_regno = ira_class_hard_regs[common_class][i];
303 nregs = hard_regno_nregs[hard_regno][reg_mode];
304 if (nregs == 1)
305 return true;
306 for (j = 0; j < nregs; j++)
307 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
308 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
309 hard_regno + j))
310 break;
311 if (j >= nregs)
312 return true;
314 return false;
318 /* Return true if REGNO satisfies a memory constraint. */
319 static bool
320 in_mem_p (int regno)
322 return get_reg_class (regno) == NO_REGS;
325 /* Return 1 if ADDR is a valid memory address for mode MODE in address
326 space AS, and check that each pseudo has the proper kind of hard
327 reg. */
328 static int
329 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
330 rtx addr, addr_space_t as)
332 #ifdef GO_IF_LEGITIMATE_ADDRESS
333 lra_assert (ADDR_SPACE_GENERIC_P (as));
334 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
335 return 0;
337 win:
338 return 1;
339 #else
340 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
341 #endif
344 namespace {
345 /* Temporarily eliminates registers in an address (for the lifetime of
346 the object). */
347 class address_eliminator {
348 public:
349 address_eliminator (struct address_info *ad);
350 ~address_eliminator ();
352 private:
353 struct address_info *m_ad;
354 rtx *m_base_loc;
355 rtx m_base_reg;
356 rtx *m_index_loc;
357 rtx m_index_reg;
361 address_eliminator::address_eliminator (struct address_info *ad)
362 : m_ad (ad),
363 m_base_loc (strip_subreg (ad->base_term)),
364 m_base_reg (NULL_RTX),
365 m_index_loc (strip_subreg (ad->index_term)),
366 m_index_reg (NULL_RTX)
368 if (m_base_loc != NULL)
370 m_base_reg = *m_base_loc;
371 lra_eliminate_reg_if_possible (m_base_loc);
372 if (m_ad->base_term2 != NULL)
373 *m_ad->base_term2 = *m_ad->base_term;
375 if (m_index_loc != NULL)
377 m_index_reg = *m_index_loc;
378 lra_eliminate_reg_if_possible (m_index_loc);
382 address_eliminator::~address_eliminator ()
384 if (m_base_loc && *m_base_loc != m_base_reg)
386 *m_base_loc = m_base_reg;
387 if (m_ad->base_term2 != NULL)
388 *m_ad->base_term2 = *m_ad->base_term;
390 if (m_index_loc && *m_index_loc != m_index_reg)
391 *m_index_loc = m_index_reg;
394 /* Return true if the eliminated form of AD is a legitimate target address. */
395 static bool
396 valid_address_p (struct address_info *ad)
398 address_eliminator eliminator (ad);
399 return valid_address_p (ad->mode, *ad->outer, ad->as);
402 /* Return true if the eliminated form of memory reference OP satisfies
403 extra memory constraint CONSTRAINT. */
404 static bool
405 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
407 struct address_info ad;
409 decompose_mem_address (&ad, op);
410 address_eliminator eliminator (&ad);
411 return constraint_satisfied_p (op, constraint);
414 /* Return true if the eliminated form of address AD satisfies extra
415 address constraint CONSTRAINT. */
416 static bool
417 satisfies_address_constraint_p (struct address_info *ad,
418 enum constraint_num constraint)
420 address_eliminator eliminator (ad);
421 return constraint_satisfied_p (*ad->outer, constraint);
424 /* Return true if the eliminated form of address OP satisfies extra
425 address constraint CONSTRAINT. */
426 static bool
427 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
429 struct address_info ad;
431 decompose_lea_address (&ad, &op);
432 return satisfies_address_constraint_p (&ad, constraint);
435 /* Initiate equivalences for LRA. As we keep original equivalences
436 before any elimination, we need to make copies otherwise any change
437 in insns might change the equivalences. */
438 void
439 lra_init_equiv (void)
441 ira_expand_reg_equiv ();
442 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
444 rtx res;
446 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
447 ira_reg_equiv[i].memory = copy_rtx (res);
448 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
449 ira_reg_equiv[i].invariant = copy_rtx (res);
453 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
455 /* Update equivalence for REGNO. We need to this as the equivalence
456 might contain other pseudos which are changed by their
457 equivalences. */
458 static void
459 update_equiv (int regno)
461 rtx x;
463 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
464 ira_reg_equiv[regno].memory
465 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
466 NULL_RTX);
467 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
468 ira_reg_equiv[regno].invariant
469 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
470 NULL_RTX);
473 /* If we have decided to substitute X with another value, return that
474 value, otherwise return X. */
475 static rtx
476 get_equiv (rtx x)
478 int regno;
479 rtx res;
481 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
482 || ! ira_reg_equiv[regno].defined_p
483 || ! ira_reg_equiv[regno].profitable_p
484 || lra_get_regno_hard_regno (regno) >= 0)
485 return x;
486 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
488 if (targetm.cannot_substitute_mem_equiv_p (res))
489 return x;
490 return res;
492 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
493 return res;
494 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
495 return res;
496 gcc_unreachable ();
499 /* If we have decided to substitute X with the equivalent value,
500 return that value after elimination for INSN, otherwise return
501 X. */
502 static rtx
503 get_equiv_with_elimination (rtx x, rtx_insn *insn)
505 rtx res = get_equiv (x);
507 if (x == res || CONSTANT_P (res))
508 return res;
509 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
510 false, false, 0, true);
513 /* Set up curr_operand_mode. */
514 static void
515 init_curr_operand_mode (void)
517 int nop = curr_static_id->n_operands;
518 for (int i = 0; i < nop; i++)
520 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
521 if (mode == VOIDmode)
523 /* The .md mode for address operands is the mode of the
524 addressed value rather than the mode of the address itself. */
525 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
526 mode = Pmode;
527 else
528 mode = curr_static_id->operand[i].mode;
530 curr_operand_mode[i] = mode;
536 /* The page contains code to reuse input reloads. */
538 /* Structure describes input reload of the current insns. */
539 struct input_reload
541 /* Reloaded value. */
542 rtx input;
543 /* Reload pseudo used. */
544 rtx reg;
547 /* The number of elements in the following array. */
548 static int curr_insn_input_reloads_num;
549 /* Array containing info about input reloads. It is used to find the
550 same input reload and reuse the reload pseudo in this case. */
551 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
553 /* Initiate data concerning reuse of input reloads for the current
554 insn. */
555 static void
556 init_curr_insn_input_reloads (void)
558 curr_insn_input_reloads_num = 0;
561 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse already
562 created input reload pseudo (only if TYPE is not OP_OUT). Don't
563 reuse pseudo if IN_SUBREG_P is true and the reused pseudo should be
564 wrapped up in SUBREG. The result pseudo is returned through
565 RESULT_REG. Return TRUE if we created a new pseudo, FALSE if we
566 reused the already created input reload pseudo. Use TITLE to
567 describe new registers for debug purposes. */
568 static bool
569 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
570 enum reg_class rclass, bool in_subreg_p,
571 const char *title, rtx *result_reg)
573 int i, regno;
574 enum reg_class new_class;
576 if (type == OP_OUT)
578 *result_reg
579 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
580 return true;
582 /* Prevent reuse value of expression with side effects,
583 e.g. volatile memory. */
584 if (! side_effects_p (original))
585 for (i = 0; i < curr_insn_input_reloads_num; i++)
586 if (rtx_equal_p (curr_insn_input_reloads[i].input, original)
587 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
589 rtx reg = curr_insn_input_reloads[i].reg;
590 regno = REGNO (reg);
591 /* If input is equal to original and both are VOIDmode,
592 GET_MODE (reg) might be still different from mode.
593 Ensure we don't return *result_reg with wrong mode. */
594 if (GET_MODE (reg) != mode)
596 if (in_subreg_p)
597 continue;
598 if (GET_MODE_SIZE (GET_MODE (reg)) < GET_MODE_SIZE (mode))
599 continue;
600 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
601 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
602 continue;
604 *result_reg = reg;
605 if (lra_dump_file != NULL)
607 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
608 dump_value_slim (lra_dump_file, original, 1);
610 if (new_class != lra_get_allocno_class (regno))
611 lra_change_class (regno, new_class, ", change to", false);
612 if (lra_dump_file != NULL)
613 fprintf (lra_dump_file, "\n");
614 return false;
616 *result_reg = lra_create_new_reg (mode, original, rclass, title);
617 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
618 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
619 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
620 return true;
625 /* The page contains code to extract memory address parts. */
627 /* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudos. */
628 static inline bool
629 ok_for_index_p_nonstrict (rtx reg)
631 unsigned regno = REGNO (reg);
633 return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
636 /* A version of regno_ok_for_base_p for use here, when all pseudos
637 should count as OK. Arguments as for regno_ok_for_base_p. */
638 static inline bool
639 ok_for_base_p_nonstrict (rtx reg, machine_mode mode, addr_space_t as,
640 enum rtx_code outer_code, enum rtx_code index_code)
642 unsigned regno = REGNO (reg);
644 if (regno >= FIRST_PSEUDO_REGISTER)
645 return true;
646 return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
651 /* The page contains major code to choose the current insn alternative
652 and generate reloads for it. */
654 /* Return the offset from REGNO of the least significant register
655 in (reg:MODE REGNO).
657 This function is used to tell whether two registers satisfy
658 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
660 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
661 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
663 lra_constraint_offset (int regno, machine_mode mode)
665 lra_assert (regno < FIRST_PSEUDO_REGISTER);
666 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (mode) > UNITS_PER_WORD
667 && SCALAR_INT_MODE_P (mode))
668 return hard_regno_nregs[regno][mode] - 1;
669 return 0;
672 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
673 if they are the same hard reg, and has special hacks for
674 auto-increment and auto-decrement. This is specifically intended for
675 process_alt_operands to use in determining whether two operands
676 match. X is the operand whose number is the lower of the two.
678 It is supposed that X is the output operand and Y is the input
679 operand. Y_HARD_REGNO is the final hard regno of register Y or
680 register in subreg Y as we know it now. Otherwise, it is a
681 negative value. */
682 static bool
683 operands_match_p (rtx x, rtx y, int y_hard_regno)
685 int i;
686 RTX_CODE code = GET_CODE (x);
687 const char *fmt;
689 if (x == y)
690 return true;
691 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
692 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
694 int j;
696 i = get_hard_regno (x);
697 if (i < 0)
698 goto slow;
700 if ((j = y_hard_regno) < 0)
701 goto slow;
703 i += lra_constraint_offset (i, GET_MODE (x));
704 j += lra_constraint_offset (j, GET_MODE (y));
706 return i == j;
709 /* If two operands must match, because they are really a single
710 operand of an assembler insn, then two post-increments are invalid
711 because the assembler insn would increment only once. On the
712 other hand, a post-increment matches ordinary indexing if the
713 post-increment is the output operand. */
714 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
715 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
717 /* Two pre-increments are invalid because the assembler insn would
718 increment only once. On the other hand, a pre-increment matches
719 ordinary indexing if the pre-increment is the input operand. */
720 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
721 || GET_CODE (y) == PRE_MODIFY)
722 return operands_match_p (x, XEXP (y, 0), -1);
724 slow:
726 if (code == REG && REG_P (y))
727 return REGNO (x) == REGNO (y);
729 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
730 && x == SUBREG_REG (y))
731 return true;
732 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
733 && SUBREG_REG (x) == y)
734 return true;
736 /* Now we have disposed of all the cases in which different rtx
737 codes can match. */
738 if (code != GET_CODE (y))
739 return false;
741 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
742 if (GET_MODE (x) != GET_MODE (y))
743 return false;
745 switch (code)
747 CASE_CONST_UNIQUE:
748 return false;
750 case LABEL_REF:
751 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
752 case SYMBOL_REF:
753 return XSTR (x, 0) == XSTR (y, 0);
755 default:
756 break;
759 /* Compare the elements. If any pair of corresponding elements fail
760 to match, return false for the whole things. */
762 fmt = GET_RTX_FORMAT (code);
763 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
765 int val, j;
766 switch (fmt[i])
768 case 'w':
769 if (XWINT (x, i) != XWINT (y, i))
770 return false;
771 break;
773 case 'i':
774 if (XINT (x, i) != XINT (y, i))
775 return false;
776 break;
778 case 'e':
779 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
780 if (val == 0)
781 return false;
782 break;
784 case '0':
785 break;
787 case 'E':
788 if (XVECLEN (x, i) != XVECLEN (y, i))
789 return false;
790 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
792 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
793 if (val == 0)
794 return false;
796 break;
798 /* It is believed that rtx's at this level will never
799 contain anything but integers and other rtx's, except for
800 within LABEL_REFs and SYMBOL_REFs. */
801 default:
802 gcc_unreachable ();
805 return true;
808 /* True if X is a constant that can be forced into the constant pool.
809 MODE is the mode of the operand, or VOIDmode if not known. */
810 #define CONST_POOL_OK_P(MODE, X) \
811 ((MODE) != VOIDmode \
812 && CONSTANT_P (X) \
813 && GET_CODE (X) != HIGH \
814 && !targetm.cannot_force_const_mem (MODE, X))
816 /* True if C is a non-empty register class that has too few registers
817 to be safely used as a reload target class. */
818 #define SMALL_REGISTER_CLASS_P(C) \
819 (ira_class_hard_regs_num [(C)] == 1 \
820 || (ira_class_hard_regs_num [(C)] >= 1 \
821 && targetm.class_likely_spilled_p (C)))
823 /* If REG is a reload pseudo, try to make its class satisfying CL. */
824 static void
825 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
827 enum reg_class rclass;
829 /* Do not make more accurate class from reloads generated. They are
830 mostly moves with a lot of constraints. Making more accurate
831 class may results in very narrow class and impossibility of find
832 registers for several reloads of one insn. */
833 if (INSN_UID (curr_insn) >= new_insn_uid_start)
834 return;
835 if (GET_CODE (reg) == SUBREG)
836 reg = SUBREG_REG (reg);
837 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
838 return;
839 if (in_class_p (reg, cl, &rclass) && rclass != cl)
840 lra_change_class (REGNO (reg), rclass, " Change to", true);
843 /* Generate reloads for matching OUT and INS (array of input operand
844 numbers with end marker -1) with reg class GOAL_CLASS. Add input
845 and output reloads correspondingly to the lists *BEFORE and *AFTER.
846 OUT might be negative. In this case we generate input reloads for
847 matched input operands INS. EARLY_CLOBBER_P is a flag that the
848 output operand is early clobbered for chosen alternative. */
849 static void
850 match_reload (signed char out, signed char *ins, enum reg_class goal_class,
851 rtx_insn **before, rtx_insn **after, bool early_clobber_p)
853 int i, in;
854 rtx new_in_reg, new_out_reg, reg;
855 machine_mode inmode, outmode;
856 rtx in_rtx = *curr_id->operand_loc[ins[0]];
857 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
859 inmode = curr_operand_mode[ins[0]];
860 outmode = out < 0 ? inmode : curr_operand_mode[out];
861 push_to_sequence (*before);
862 if (inmode != outmode)
864 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
866 reg = new_in_reg
867 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
868 goal_class, "");
869 if (SCALAR_INT_MODE_P (inmode))
870 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
871 else
872 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
873 LRA_SUBREG_P (new_out_reg) = 1;
874 /* If the input reg is dying here, we can use the same hard
875 register for REG and IN_RTX. We do it only for original
876 pseudos as reload pseudos can die although original
877 pseudos still live where reload pseudos dies. */
878 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
879 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
880 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
882 else
884 reg = new_out_reg
885 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
886 goal_class, "");
887 if (SCALAR_INT_MODE_P (outmode))
888 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
889 else
890 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
891 /* NEW_IN_REG is non-paradoxical subreg. We don't want
892 NEW_OUT_REG living above. We add clobber clause for
893 this. This is just a temporary clobber. We can remove
894 it at the end of LRA work. */
895 rtx_insn *clobber = emit_clobber (new_out_reg);
896 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
897 LRA_SUBREG_P (new_in_reg) = 1;
898 if (GET_CODE (in_rtx) == SUBREG)
900 rtx subreg_reg = SUBREG_REG (in_rtx);
902 /* If SUBREG_REG is dying here and sub-registers IN_RTX
903 and NEW_IN_REG are similar, we can use the same hard
904 register for REG and SUBREG_REG. */
905 if (REG_P (subreg_reg)
906 && (int) REGNO (subreg_reg) < lra_new_regno_start
907 && GET_MODE (subreg_reg) == outmode
908 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
909 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
910 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
914 else
916 /* Pseudos have values -- see comments for lra_reg_info.
917 Different pseudos with the same value do not conflict even if
918 they live in the same place. When we create a pseudo we
919 assign value of original pseudo (if any) from which we
920 created the new pseudo. If we create the pseudo from the
921 input pseudo, the new pseudo will have no conflict with the
922 input pseudo which is wrong when the input pseudo lives after
923 the insn and as the new pseudo value is changed by the insn
924 output. Therefore we create the new pseudo from the output
925 except the case when we have single matched dying input
926 pseudo.
928 We cannot reuse the current output register because we might
929 have a situation like "a <- a op b", where the constraints
930 force the second input operand ("b") to match the output
931 operand ("a"). "b" must then be copied into a new register
932 so that it doesn't clobber the current value of "a".
934 We can not use the same value if the output pseudo is
935 early clobbered or the input pseudo is mentioned in the
936 output, e.g. as an address part in memory, because
937 output reload will actually extend the pseudo liveness.
938 We don't care about eliminable hard regs here as we are
939 interesting only in pseudos. */
941 new_in_reg = new_out_reg
942 = (! early_clobber_p && ins[1] < 0 && REG_P (in_rtx)
943 && (int) REGNO (in_rtx) < lra_new_regno_start
944 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
945 && (out < 0 || regno_use_in (REGNO (in_rtx), out_rtx) == NULL_RTX)
946 ? lra_create_new_reg (inmode, in_rtx, goal_class, "")
947 : lra_create_new_reg_with_unique_value (outmode, out_rtx,
948 goal_class, ""));
950 /* In operand can be got from transformations before processing insn
951 constraints. One example of such transformations is subreg
952 reloading (see function simplify_operand_subreg). The new
953 pseudos created by the transformations might have inaccurate
954 class (ALL_REGS) and we should make their classes more
955 accurate. */
956 narrow_reload_pseudo_class (in_rtx, goal_class);
957 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
958 *before = get_insns ();
959 end_sequence ();
960 for (i = 0; (in = ins[i]) >= 0; i++)
962 lra_assert
963 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
964 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
965 *curr_id->operand_loc[in] = new_in_reg;
967 lra_update_dups (curr_id, ins);
968 if (out < 0)
969 return;
970 /* See a comment for the input operand above. */
971 narrow_reload_pseudo_class (out_rtx, goal_class);
972 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
974 start_sequence ();
975 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
976 emit_insn (*after);
977 *after = get_insns ();
978 end_sequence ();
980 *curr_id->operand_loc[out] = new_out_reg;
981 lra_update_dup (curr_id, out);
984 /* Return register class which is union of all reg classes in insn
985 constraint alternative string starting with P. */
986 static enum reg_class
987 reg_class_from_constraints (const char *p)
989 int c, len;
990 enum reg_class op_class = NO_REGS;
993 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
995 case '#':
996 case ',':
997 return op_class;
999 case 'g':
1000 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1001 break;
1003 default:
1004 enum constraint_num cn = lookup_constraint (p);
1005 enum reg_class cl = reg_class_for_constraint (cn);
1006 if (cl == NO_REGS)
1008 if (insn_extra_address_constraint (cn))
1009 op_class
1010 = (reg_class_subunion
1011 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1012 ADDRESS, SCRATCH)]);
1013 break;
1016 op_class = reg_class_subunion[op_class][cl];
1017 break;
1019 while ((p += len), c);
1020 return op_class;
1023 /* If OP is a register, return the class of the register as per
1024 get_reg_class, otherwise return NO_REGS. */
1025 static inline enum reg_class
1026 get_op_class (rtx op)
1028 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1031 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1032 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1033 SUBREG for VAL to make them equal. */
1034 static rtx_insn *
1035 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1037 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1039 /* Usually size of mem_pseudo is greater than val size but in
1040 rare cases it can be less as it can be defined by target
1041 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1042 if (! MEM_P (val))
1044 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
1045 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
1047 LRA_SUBREG_P (val) = 1;
1049 else
1051 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1052 LRA_SUBREG_P (mem_pseudo) = 1;
1055 return to_p ? gen_move_insn (mem_pseudo, val)
1056 : gen_move_insn (val, mem_pseudo);
1059 /* Process a special case insn (register move), return true if we
1060 don't need to process it anymore. INSN should be a single set
1061 insn. Set up that RTL was changed through CHANGE_P and macro
1062 SECONDARY_MEMORY_NEEDED says to use secondary memory through
1063 SEC_MEM_P. */
1064 static bool
1065 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1067 int sregno, dregno;
1068 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1069 rtx_insn *before;
1070 enum reg_class dclass, sclass, secondary_class;
1071 secondary_reload_info sri;
1073 lra_assert (curr_insn_set != NULL_RTX);
1074 dreg = dest = SET_DEST (curr_insn_set);
1075 sreg = src = SET_SRC (curr_insn_set);
1076 if (GET_CODE (dest) == SUBREG)
1077 dreg = SUBREG_REG (dest);
1078 if (GET_CODE (src) == SUBREG)
1079 sreg = SUBREG_REG (src);
1080 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1081 return false;
1082 sclass = dclass = NO_REGS;
1083 if (REG_P (dreg))
1084 dclass = get_reg_class (REGNO (dreg));
1085 if (dclass == ALL_REGS)
1086 /* ALL_REGS is used for new pseudos created by transformations
1087 like reload of SUBREG_REG (see function
1088 simplify_operand_subreg). We don't know their class yet. We
1089 should figure out the class from processing the insn
1090 constraints not in this fast path function. Even if ALL_REGS
1091 were a right class for the pseudo, secondary_... hooks usually
1092 are not define for ALL_REGS. */
1093 return false;
1094 if (REG_P (sreg))
1095 sclass = get_reg_class (REGNO (sreg));
1096 if (sclass == ALL_REGS)
1097 /* See comments above. */
1098 return false;
1099 if (sclass == NO_REGS && dclass == NO_REGS)
1100 return false;
1101 #ifdef SECONDARY_MEMORY_NEEDED
1102 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
1103 #ifdef SECONDARY_MEMORY_NEEDED_MODE
1104 && ((sclass != NO_REGS && dclass != NO_REGS)
1105 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
1106 #endif
1109 *sec_mem_p = true;
1110 return false;
1112 #endif
1113 if (! REG_P (dreg) || ! REG_P (sreg))
1114 return false;
1115 sri.prev_sri = NULL;
1116 sri.icode = CODE_FOR_nothing;
1117 sri.extra_cost = 0;
1118 secondary_class = NO_REGS;
1119 /* Set up hard register for a reload pseudo for hook
1120 secondary_reload because some targets just ignore unassigned
1121 pseudos in the hook. */
1122 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1124 dregno = REGNO (dreg);
1125 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1127 else
1128 dregno = -1;
1129 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1131 sregno = REGNO (sreg);
1132 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1134 else
1135 sregno = -1;
1136 if (sclass != NO_REGS)
1137 secondary_class
1138 = (enum reg_class) targetm.secondary_reload (false, dest,
1139 (reg_class_t) sclass,
1140 GET_MODE (src), &sri);
1141 if (sclass == NO_REGS
1142 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1143 && dclass != NO_REGS))
1145 enum reg_class old_sclass = secondary_class;
1146 secondary_reload_info old_sri = sri;
1148 sri.prev_sri = NULL;
1149 sri.icode = CODE_FOR_nothing;
1150 sri.extra_cost = 0;
1151 secondary_class
1152 = (enum reg_class) targetm.secondary_reload (true, src,
1153 (reg_class_t) dclass,
1154 GET_MODE (src), &sri);
1155 /* Check the target hook consistency. */
1156 lra_assert
1157 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1158 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1159 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1161 if (sregno >= 0)
1162 reg_renumber [sregno] = -1;
1163 if (dregno >= 0)
1164 reg_renumber [dregno] = -1;
1165 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1166 return false;
1167 *change_p = true;
1168 new_reg = NULL_RTX;
1169 if (secondary_class != NO_REGS)
1170 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1171 secondary_class,
1172 "secondary");
1173 start_sequence ();
1174 if (sri.icode == CODE_FOR_nothing)
1175 lra_emit_move (new_reg, src);
1176 else
1178 enum reg_class scratch_class;
1180 scratch_class = (reg_class_from_constraints
1181 (insn_data[sri.icode].operand[2].constraint));
1182 scratch_reg = (lra_create_new_reg_with_unique_value
1183 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1184 scratch_class, "scratch"));
1185 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1186 src, scratch_reg));
1188 before = get_insns ();
1189 end_sequence ();
1190 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1191 if (new_reg != NULL_RTX)
1192 SET_SRC (curr_insn_set) = new_reg;
1193 else
1195 if (lra_dump_file != NULL)
1197 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1198 dump_insn_slim (lra_dump_file, curr_insn);
1200 lra_set_insn_deleted (curr_insn);
1201 return true;
1203 return false;
1206 /* The following data describe the result of process_alt_operands.
1207 The data are used in curr_insn_transform to generate reloads. */
1209 /* The chosen reg classes which should be used for the corresponding
1210 operands. */
1211 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1212 /* True if the operand should be the same as another operand and that
1213 other operand does not need a reload. */
1214 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1215 /* True if the operand does not need a reload. */
1216 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1217 /* True if the operand can be offsetable memory. */
1218 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1219 /* The number of an operand to which given operand can be matched to. */
1220 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1221 /* The number of elements in the following array. */
1222 static int goal_alt_dont_inherit_ops_num;
1223 /* Numbers of operands whose reload pseudos should not be inherited. */
1224 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1225 /* True if the insn commutative operands should be swapped. */
1226 static bool goal_alt_swapped;
1227 /* The chosen insn alternative. */
1228 static int goal_alt_number;
1230 /* The following five variables are used to choose the best insn
1231 alternative. They reflect final characteristics of the best
1232 alternative. */
1234 /* Number of necessary reloads and overall cost reflecting the
1235 previous value and other unpleasantness of the best alternative. */
1236 static int best_losers, best_overall;
1237 /* Overall number hard registers used for reloads. For example, on
1238 some targets we need 2 general registers to reload DFmode and only
1239 one floating point register. */
1240 static int best_reload_nregs;
1241 /* Overall number reflecting distances of previous reloading the same
1242 value. The distances are counted from the current BB start. It is
1243 used to improve inheritance chances. */
1244 static int best_reload_sum;
1246 /* True if the current insn should have no correspondingly input or
1247 output reloads. */
1248 static bool no_input_reloads_p, no_output_reloads_p;
1250 /* True if we swapped the commutative operands in the current
1251 insn. */
1252 static int curr_swapped;
1254 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1255 register of class CL. Add any input reloads to list BEFORE. AFTER
1256 is nonnull if *LOC is an automodified value; handle that case by
1257 adding the required output reloads to list AFTER. Return true if
1258 the RTL was changed.
1260 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1261 register. Return false if the address register is correct. */
1262 static bool
1263 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1264 enum reg_class cl)
1266 int regno;
1267 enum reg_class rclass, new_class;
1268 rtx reg;
1269 rtx new_reg;
1270 machine_mode mode;
1271 bool subreg_p, before_p = false;
1273 subreg_p = GET_CODE (*loc) == SUBREG;
1274 if (subreg_p)
1275 loc = &SUBREG_REG (*loc);
1276 reg = *loc;
1277 mode = GET_MODE (reg);
1278 if (! REG_P (reg))
1280 if (check_only_p)
1281 return true;
1282 /* Always reload memory in an address even if the target supports
1283 such addresses. */
1284 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1285 before_p = true;
1287 else
1289 regno = REGNO (reg);
1290 rclass = get_reg_class (regno);
1291 if (! check_only_p
1292 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1294 if (lra_dump_file != NULL)
1296 fprintf (lra_dump_file,
1297 "Changing pseudo %d in address of insn %u on equiv ",
1298 REGNO (reg), INSN_UID (curr_insn));
1299 dump_value_slim (lra_dump_file, *loc, 1);
1300 fprintf (lra_dump_file, "\n");
1302 *loc = copy_rtx (*loc);
1304 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1306 if (check_only_p)
1307 return true;
1308 reg = *loc;
1309 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1310 mode, reg, cl, subreg_p, "address", &new_reg))
1311 before_p = true;
1313 else if (new_class != NO_REGS && rclass != new_class)
1315 if (check_only_p)
1316 return true;
1317 lra_change_class (regno, new_class, " Change to", true);
1318 return false;
1320 else
1321 return false;
1323 if (before_p)
1325 push_to_sequence (*before);
1326 lra_emit_move (new_reg, reg);
1327 *before = get_insns ();
1328 end_sequence ();
1330 *loc = new_reg;
1331 if (after != NULL)
1333 start_sequence ();
1334 lra_emit_move (before_p ? copy_rtx (reg) : reg, new_reg);
1335 emit_insn (*after);
1336 *after = get_insns ();
1337 end_sequence ();
1339 return true;
1342 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1343 the insn to be inserted before curr insn. AFTER returns the
1344 the insn to be inserted after curr insn. ORIGREG and NEWREG
1345 are the original reg and new reg for reload. */
1346 static void
1347 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1348 rtx newreg)
1350 if (before)
1352 push_to_sequence (*before);
1353 lra_emit_move (newreg, origreg);
1354 *before = get_insns ();
1355 end_sequence ();
1357 if (after)
1359 start_sequence ();
1360 lra_emit_move (origreg, newreg);
1361 emit_insn (*after);
1362 *after = get_insns ();
1363 end_sequence ();
1367 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1369 /* Make reloads for subreg in operand NOP with internal subreg mode
1370 REG_MODE, add new reloads for further processing. Return true if
1371 any change was done. */
1372 static bool
1373 simplify_operand_subreg (int nop, machine_mode reg_mode)
1375 int hard_regno;
1376 rtx_insn *before, *after;
1377 machine_mode mode, innermode;
1378 rtx reg, new_reg;
1379 rtx operand = *curr_id->operand_loc[nop];
1380 enum reg_class regclass;
1381 enum op_type type;
1383 before = after = NULL;
1385 if (GET_CODE (operand) != SUBREG)
1386 return false;
1388 mode = GET_MODE (operand);
1389 reg = SUBREG_REG (operand);
1390 innermode = GET_MODE (reg);
1391 type = curr_static_id->operand[nop].type;
1392 /* If we change address for paradoxical subreg of memory, the
1393 address might violate the necessary alignment or the access might
1394 be slow. So take this into consideration. We should not worry
1395 about access beyond allocated memory for paradoxical memory
1396 subregs as we don't substitute such equiv memory (see processing
1397 equivalences in function lra_constraints) and because for spilled
1398 pseudos we allocate stack memory enough for the biggest
1399 corresponding paradoxical subreg. */
1400 if (MEM_P (reg)
1401 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1402 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode)))
1404 rtx subst, old = *curr_id->operand_loc[nop];
1406 alter_subreg (curr_id->operand_loc[nop], false);
1407 subst = *curr_id->operand_loc[nop];
1408 lra_assert (MEM_P (subst));
1409 if (! valid_address_p (innermode, XEXP (reg, 0),
1410 MEM_ADDR_SPACE (reg))
1411 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1412 MEM_ADDR_SPACE (subst)))
1413 return true;
1414 /* If the address was valid and became invalid, prefer to reload
1415 the memory. Typical case is when the index scale should
1416 correspond the memory. */
1417 *curr_id->operand_loc[nop] = old;
1419 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1421 alter_subreg (curr_id->operand_loc[nop], false);
1422 return true;
1424 else if (CONSTANT_P (reg))
1426 /* Try to simplify subreg of constant. It is usually result of
1427 equivalence substitution. */
1428 if (innermode == VOIDmode
1429 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1430 innermode = curr_static_id->operand[nop].mode;
1431 if ((new_reg = simplify_subreg (mode, reg, innermode,
1432 SUBREG_BYTE (operand))) != NULL_RTX)
1434 *curr_id->operand_loc[nop] = new_reg;
1435 return true;
1438 /* Put constant into memory when we have mixed modes. It generates
1439 a better code in most cases as it does not need a secondary
1440 reload memory. It also prevents LRA looping when LRA is using
1441 secondary reload memory again and again. */
1442 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1443 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1445 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1446 alter_subreg (curr_id->operand_loc[nop], false);
1447 return true;
1449 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1450 if there may be a problem accessing OPERAND in the outer
1451 mode. */
1452 if ((REG_P (reg)
1453 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1454 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1455 /* Don't reload paradoxical subregs because we could be looping
1456 having repeatedly final regno out of hard regs range. */
1457 && (hard_regno_nregs[hard_regno][innermode]
1458 >= hard_regno_nregs[hard_regno][mode])
1459 && simplify_subreg_regno (hard_regno, innermode,
1460 SUBREG_BYTE (operand), mode) < 0
1461 /* Don't reload subreg for matching reload. It is actually
1462 valid subreg in LRA. */
1463 && ! LRA_SUBREG_P (operand))
1464 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1466 enum reg_class rclass;
1468 if (REG_P (reg))
1469 /* There is a big probability that we will get the same class
1470 for the new pseudo and we will get the same insn which
1471 means infinite looping. So spill the new pseudo. */
1472 rclass = NO_REGS;
1473 else
1474 /* The class will be defined later in curr_insn_transform. */
1475 rclass
1476 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1478 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1479 rclass, TRUE, "subreg reg", &new_reg))
1481 bool insert_before, insert_after;
1482 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1484 insert_before = (type != OP_OUT
1485 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1486 insert_after = (type != OP_IN);
1487 insert_move_for_subreg (insert_before ? &before : NULL,
1488 insert_after ? &after : NULL,
1489 reg, new_reg);
1491 SUBREG_REG (operand) = new_reg;
1492 lra_process_new_insns (curr_insn, before, after,
1493 "Inserting subreg reload");
1494 return true;
1496 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1497 IRA allocates hardreg to the inner pseudo reg according to its mode
1498 instead of the outermode, so the size of the hardreg may not be enough
1499 to contain the outermode operand, in that case we may need to insert
1500 reload for the reg. For the following two types of paradoxical subreg,
1501 we need to insert reload:
1502 1. If the op_type is OP_IN, and the hardreg could not be paired with
1503 other hardreg to contain the outermode operand
1504 (checked by in_hard_reg_set_p), we need to insert the reload.
1505 2. If the op_type is OP_OUT or OP_INOUT.
1507 Here is a paradoxical subreg example showing how the reload is generated:
1509 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1510 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1512 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1513 here, if reg107 is assigned to hardreg R15, because R15 is the last
1514 hardreg, compiler cannot find another hardreg to pair with R15 to
1515 contain TImode data. So we insert a TImode reload reg180 for it.
1516 After reload is inserted:
1518 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1519 (reg:DI 107 [ __comp ])) -1
1520 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1521 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1523 Two reload hard registers will be allocated to reg180 to save TImode data
1524 in LRA_assign. */
1525 else if (REG_P (reg)
1526 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1527 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1528 && (hard_regno_nregs[hard_regno][innermode]
1529 < hard_regno_nregs[hard_regno][mode])
1530 && (regclass = lra_get_allocno_class (REGNO (reg)))
1531 && (type != OP_IN
1532 || !in_hard_reg_set_p (reg_class_contents[regclass],
1533 mode, hard_regno)))
1535 /* The class will be defined later in curr_insn_transform. */
1536 enum reg_class rclass
1537 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1539 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1540 rclass, TRUE, "paradoxical subreg", &new_reg))
1542 rtx subreg;
1543 bool insert_before, insert_after;
1545 PUT_MODE (new_reg, mode);
1546 subreg = gen_lowpart_SUBREG (innermode, new_reg);
1547 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1549 insert_before = (type != OP_OUT);
1550 insert_after = (type != OP_IN);
1551 insert_move_for_subreg (insert_before ? &before : NULL,
1552 insert_after ? &after : NULL,
1553 reg, subreg);
1555 SUBREG_REG (operand) = new_reg;
1556 lra_process_new_insns (curr_insn, before, after,
1557 "Inserting paradoxical subreg reload");
1558 return true;
1560 return false;
1563 /* Return TRUE if X refers for a hard register from SET. */
1564 static bool
1565 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1567 int i, j, x_hard_regno;
1568 machine_mode mode;
1569 const char *fmt;
1570 enum rtx_code code;
1572 if (x == NULL_RTX)
1573 return false;
1574 code = GET_CODE (x);
1575 mode = GET_MODE (x);
1576 if (code == SUBREG)
1578 x = SUBREG_REG (x);
1579 code = GET_CODE (x);
1580 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1581 mode = GET_MODE (x);
1584 if (REG_P (x))
1586 x_hard_regno = get_hard_regno (x);
1587 return (x_hard_regno >= 0
1588 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1590 if (MEM_P (x))
1592 struct address_info ad;
1594 decompose_mem_address (&ad, x);
1595 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1596 return true;
1597 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1598 return true;
1600 fmt = GET_RTX_FORMAT (code);
1601 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1603 if (fmt[i] == 'e')
1605 if (uses_hard_regs_p (XEXP (x, i), set))
1606 return true;
1608 else if (fmt[i] == 'E')
1610 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1611 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1612 return true;
1615 return false;
1618 /* Return true if OP is a spilled pseudo. */
1619 static inline bool
1620 spilled_pseudo_p (rtx op)
1622 return (REG_P (op)
1623 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1626 /* Return true if X is a general constant. */
1627 static inline bool
1628 general_constant_p (rtx x)
1630 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1633 static bool
1634 reg_in_class_p (rtx reg, enum reg_class cl)
1636 if (cl == NO_REGS)
1637 return get_reg_class (REGNO (reg)) == NO_REGS;
1638 return in_class_p (reg, cl, NULL);
1641 /* Return true if SET of RCLASS contains no hard regs which can be
1642 used in MODE. */
1643 static bool
1644 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1645 HARD_REG_SET &set,
1646 enum machine_mode mode)
1648 HARD_REG_SET temp;
1650 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
1651 COPY_HARD_REG_SET (temp, set);
1652 AND_COMPL_HARD_REG_SET (temp, lra_no_alloc_regs);
1653 return (hard_reg_set_subset_p
1654 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1657 /* Major function to choose the current insn alternative and what
1658 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1659 negative we should consider only this alternative. Return false if
1660 we can not choose the alternative or find how to reload the
1661 operands. */
1662 static bool
1663 process_alt_operands (int only_alternative)
1665 bool ok_p = false;
1666 int nop, overall, nalt;
1667 int n_alternatives = curr_static_id->n_alternatives;
1668 int n_operands = curr_static_id->n_operands;
1669 /* LOSERS counts the operands that don't fit this alternative and
1670 would require loading. */
1671 int losers;
1672 /* REJECT is a count of how undesirable this alternative says it is
1673 if any reloading is required. If the alternative matches exactly
1674 then REJECT is ignored, but otherwise it gets this much counted
1675 against it in addition to the reloading needed. */
1676 int reject;
1677 int op_reject;
1678 /* The number of elements in the following array. */
1679 int early_clobbered_regs_num;
1680 /* Numbers of operands which are early clobber registers. */
1681 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1682 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1683 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1684 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1685 bool curr_alt_win[MAX_RECOG_OPERANDS];
1686 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1687 int curr_alt_matches[MAX_RECOG_OPERANDS];
1688 /* The number of elements in the following array. */
1689 int curr_alt_dont_inherit_ops_num;
1690 /* Numbers of operands whose reload pseudos should not be inherited. */
1691 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1692 rtx op;
1693 /* The register when the operand is a subreg of register, otherwise the
1694 operand itself. */
1695 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1696 /* The register if the operand is a register or subreg of register,
1697 otherwise NULL. */
1698 rtx operand_reg[MAX_RECOG_OPERANDS];
1699 int hard_regno[MAX_RECOG_OPERANDS];
1700 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1701 int reload_nregs, reload_sum;
1702 bool costly_p;
1703 enum reg_class cl;
1705 /* Calculate some data common for all alternatives to speed up the
1706 function. */
1707 for (nop = 0; nop < n_operands; nop++)
1709 rtx reg;
1711 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1712 /* The real hard regno of the operand after the allocation. */
1713 hard_regno[nop] = get_hard_regno (op);
1715 operand_reg[nop] = reg = op;
1716 biggest_mode[nop] = GET_MODE (op);
1717 if (GET_CODE (op) == SUBREG)
1719 operand_reg[nop] = reg = SUBREG_REG (op);
1720 if (GET_MODE_SIZE (biggest_mode[nop])
1721 < GET_MODE_SIZE (GET_MODE (reg)))
1722 biggest_mode[nop] = GET_MODE (reg);
1724 if (! REG_P (reg))
1725 operand_reg[nop] = NULL_RTX;
1726 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1727 || ((int) REGNO (reg)
1728 == lra_get_elimination_hard_regno (REGNO (reg))))
1729 no_subreg_reg_operand[nop] = reg;
1730 else
1731 operand_reg[nop] = no_subreg_reg_operand[nop]
1732 /* Just use natural mode for elimination result. It should
1733 be enough for extra constraints hooks. */
1734 = regno_reg_rtx[hard_regno[nop]];
1737 /* The constraints are made of several alternatives. Each operand's
1738 constraint looks like foo,bar,... with commas separating the
1739 alternatives. The first alternatives for all operands go
1740 together, the second alternatives go together, etc.
1742 First loop over alternatives. */
1743 alternative_mask preferred = curr_id->preferred_alternatives;
1744 if (only_alternative >= 0)
1745 preferred &= ALTERNATIVE_BIT (only_alternative);
1747 for (nalt = 0; nalt < n_alternatives; nalt++)
1749 /* Loop over operands for one constraint alternative. */
1750 if (!TEST_BIT (preferred, nalt))
1751 continue;
1753 overall = losers = reject = reload_nregs = reload_sum = 0;
1754 for (nop = 0; nop < n_operands; nop++)
1756 int inc = (curr_static_id
1757 ->operand_alternative[nalt * n_operands + nop].reject);
1758 if (lra_dump_file != NULL && inc != 0)
1759 fprintf (lra_dump_file,
1760 " Staticly defined alt reject+=%d\n", inc);
1761 reject += inc;
1763 early_clobbered_regs_num = 0;
1765 for (nop = 0; nop < n_operands; nop++)
1767 const char *p;
1768 char *end;
1769 int len, c, m, i, opalt_num, this_alternative_matches;
1770 bool win, did_match, offmemok, early_clobber_p;
1771 /* false => this operand can be reloaded somehow for this
1772 alternative. */
1773 bool badop;
1774 /* true => this operand can be reloaded if the alternative
1775 allows regs. */
1776 bool winreg;
1777 /* True if a constant forced into memory would be OK for
1778 this operand. */
1779 bool constmemok;
1780 enum reg_class this_alternative, this_costly_alternative;
1781 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1782 bool this_alternative_match_win, this_alternative_win;
1783 bool this_alternative_offmemok;
1784 bool scratch_p;
1785 machine_mode mode;
1786 enum constraint_num cn;
1788 opalt_num = nalt * n_operands + nop;
1789 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1791 /* Fast track for no constraints at all. */
1792 curr_alt[nop] = NO_REGS;
1793 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1794 curr_alt_win[nop] = true;
1795 curr_alt_match_win[nop] = false;
1796 curr_alt_offmemok[nop] = false;
1797 curr_alt_matches[nop] = -1;
1798 continue;
1801 op = no_subreg_reg_operand[nop];
1802 mode = curr_operand_mode[nop];
1804 win = did_match = winreg = offmemok = constmemok = false;
1805 badop = true;
1807 early_clobber_p = false;
1808 p = curr_static_id->operand_alternative[opalt_num].constraint;
1810 this_costly_alternative = this_alternative = NO_REGS;
1811 /* We update set of possible hard regs besides its class
1812 because reg class might be inaccurate. For example,
1813 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1814 is translated in HI_REGS because classes are merged by
1815 pairs and there is no accurate intermediate class. */
1816 CLEAR_HARD_REG_SET (this_alternative_set);
1817 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1818 this_alternative_win = false;
1819 this_alternative_match_win = false;
1820 this_alternative_offmemok = false;
1821 this_alternative_matches = -1;
1823 /* An empty constraint should be excluded by the fast
1824 track. */
1825 lra_assert (*p != 0 && *p != ',');
1827 op_reject = 0;
1828 /* Scan this alternative's specs for this operand; set WIN
1829 if the operand fits any letter in this alternative.
1830 Otherwise, clear BADOP if this operand could fit some
1831 letter after reloads, or set WINREG if this operand could
1832 fit after reloads provided the constraint allows some
1833 registers. */
1834 costly_p = false;
1837 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1839 case '\0':
1840 len = 0;
1841 break;
1842 case ',':
1843 c = '\0';
1844 break;
1846 case '&':
1847 early_clobber_p = true;
1848 break;
1850 case '$':
1851 op_reject += LRA_MAX_REJECT;
1852 break;
1853 case '^':
1854 op_reject += LRA_LOSER_COST_FACTOR;
1855 break;
1857 case '#':
1858 /* Ignore rest of this alternative. */
1859 c = '\0';
1860 break;
1862 case '0': case '1': case '2': case '3': case '4':
1863 case '5': case '6': case '7': case '8': case '9':
1865 int m_hregno;
1866 bool match_p;
1868 m = strtoul (p, &end, 10);
1869 p = end;
1870 len = 0;
1871 lra_assert (nop > m);
1873 this_alternative_matches = m;
1874 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
1875 /* We are supposed to match a previous operand.
1876 If we do, we win if that one did. If we do
1877 not, count both of the operands as losers.
1878 (This is too conservative, since most of the
1879 time only a single reload insn will be needed
1880 to make the two operands win. As a result,
1881 this alternative may be rejected when it is
1882 actually desirable.) */
1883 match_p = false;
1884 if (operands_match_p (*curr_id->operand_loc[nop],
1885 *curr_id->operand_loc[m], m_hregno))
1887 /* We should reject matching of an early
1888 clobber operand if the matching operand is
1889 not dying in the insn. */
1890 if (! curr_static_id->operand[m].early_clobber
1891 || operand_reg[nop] == NULL_RTX
1892 || (find_regno_note (curr_insn, REG_DEAD,
1893 REGNO (op))
1894 || REGNO (op) == REGNO (operand_reg[m])))
1895 match_p = true;
1897 if (match_p)
1899 /* If we are matching a non-offsettable
1900 address where an offsettable address was
1901 expected, then we must reject this
1902 combination, because we can't reload
1903 it. */
1904 if (curr_alt_offmemok[m]
1905 && MEM_P (*curr_id->operand_loc[m])
1906 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
1907 continue;
1909 else
1911 /* Operands don't match. Both operands must
1912 allow a reload register, otherwise we
1913 cannot make them match. */
1914 if (curr_alt[m] == NO_REGS)
1915 break;
1916 /* Retroactively mark the operand we had to
1917 match as a loser, if it wasn't already and
1918 it wasn't matched to a register constraint
1919 (e.g it might be matched by memory). */
1920 if (curr_alt_win[m]
1921 && (operand_reg[m] == NULL_RTX
1922 || hard_regno[m] < 0))
1924 losers++;
1925 reload_nregs
1926 += (ira_reg_class_max_nregs[curr_alt[m]]
1927 [GET_MODE (*curr_id->operand_loc[m])]);
1930 /* Prefer matching earlyclobber alternative as
1931 it results in less hard regs required for
1932 the insn than a non-matching earlyclobber
1933 alternative. */
1934 if (curr_static_id->operand[m].early_clobber)
1936 if (lra_dump_file != NULL)
1937 fprintf
1938 (lra_dump_file,
1939 " %d Matching earlyclobber alt:"
1940 " reject--\n",
1941 nop);
1942 reject--;
1944 /* Otherwise we prefer no matching
1945 alternatives because it gives more freedom
1946 in RA. */
1947 else if (operand_reg[nop] == NULL_RTX
1948 || (find_regno_note (curr_insn, REG_DEAD,
1949 REGNO (operand_reg[nop]))
1950 == NULL_RTX))
1952 if (lra_dump_file != NULL)
1953 fprintf
1954 (lra_dump_file,
1955 " %d Matching alt: reject+=2\n",
1956 nop);
1957 reject += 2;
1960 /* If we have to reload this operand and some
1961 previous operand also had to match the same
1962 thing as this operand, we don't know how to do
1963 that. */
1964 if (!match_p || !curr_alt_win[m])
1966 for (i = 0; i < nop; i++)
1967 if (curr_alt_matches[i] == m)
1968 break;
1969 if (i < nop)
1970 break;
1972 else
1973 did_match = true;
1975 /* This can be fixed with reloads if the operand
1976 we are supposed to match can be fixed with
1977 reloads. */
1978 badop = false;
1979 this_alternative = curr_alt[m];
1980 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
1981 winreg = this_alternative != NO_REGS;
1982 break;
1985 case 'g':
1986 if (MEM_P (op)
1987 || general_constant_p (op)
1988 || spilled_pseudo_p (op))
1989 win = true;
1990 cl = GENERAL_REGS;
1991 goto reg;
1993 default:
1994 cn = lookup_constraint (p);
1995 switch (get_constraint_type (cn))
1997 case CT_REGISTER:
1998 cl = reg_class_for_constraint (cn);
1999 if (cl != NO_REGS)
2000 goto reg;
2001 break;
2003 case CT_CONST_INT:
2004 if (CONST_INT_P (op)
2005 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2006 win = true;
2007 break;
2009 case CT_MEMORY:
2010 if (MEM_P (op)
2011 && satisfies_memory_constraint_p (op, cn))
2012 win = true;
2013 else if (spilled_pseudo_p (op))
2014 win = true;
2016 /* If we didn't already win, we can reload constants
2017 via force_const_mem or put the pseudo value into
2018 memory, or make other memory by reloading the
2019 address like for 'o'. */
2020 if (CONST_POOL_OK_P (mode, op)
2021 || MEM_P (op) || REG_P (op))
2022 badop = false;
2023 constmemok = true;
2024 offmemok = true;
2025 break;
2027 case CT_ADDRESS:
2028 /* If we didn't already win, we can reload the address
2029 into a base register. */
2030 if (satisfies_address_constraint_p (op, cn))
2031 win = true;
2032 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2033 ADDRESS, SCRATCH);
2034 badop = false;
2035 goto reg;
2037 case CT_FIXED_FORM:
2038 if (constraint_satisfied_p (op, cn))
2039 win = true;
2040 break;
2042 break;
2044 reg:
2045 this_alternative = reg_class_subunion[this_alternative][cl];
2046 IOR_HARD_REG_SET (this_alternative_set,
2047 reg_class_contents[cl]);
2048 if (costly_p)
2050 this_costly_alternative
2051 = reg_class_subunion[this_costly_alternative][cl];
2052 IOR_HARD_REG_SET (this_costly_alternative_set,
2053 reg_class_contents[cl]);
2055 if (mode == BLKmode)
2056 break;
2057 winreg = true;
2058 if (REG_P (op))
2060 if (hard_regno[nop] >= 0
2061 && in_hard_reg_set_p (this_alternative_set,
2062 mode, hard_regno[nop]))
2063 win = true;
2064 else if (hard_regno[nop] < 0
2065 && in_class_p (op, this_alternative, NULL))
2066 win = true;
2068 break;
2070 if (c != ' ' && c != '\t')
2071 costly_p = c == '*';
2073 while ((p += len), c);
2075 scratch_p = (operand_reg[nop] != NULL_RTX
2076 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2077 /* Record which operands fit this alternative. */
2078 if (win)
2080 this_alternative_win = true;
2081 if (operand_reg[nop] != NULL_RTX)
2083 if (hard_regno[nop] >= 0)
2085 if (in_hard_reg_set_p (this_costly_alternative_set,
2086 mode, hard_regno[nop]))
2088 if (lra_dump_file != NULL)
2089 fprintf (lra_dump_file,
2090 " %d Costly set: reject++\n",
2091 nop);
2092 reject++;
2095 else
2097 /* Prefer won reg to spilled pseudo under other
2098 equal conditions for possibe inheritance. */
2099 if (! scratch_p)
2101 if (lra_dump_file != NULL)
2102 fprintf
2103 (lra_dump_file,
2104 " %d Non pseudo reload: reject++\n",
2105 nop);
2106 reject++;
2108 if (in_class_p (operand_reg[nop],
2109 this_costly_alternative, NULL))
2111 if (lra_dump_file != NULL)
2112 fprintf
2113 (lra_dump_file,
2114 " %d Non pseudo costly reload:"
2115 " reject++\n",
2116 nop);
2117 reject++;
2120 /* We simulate the behaviour of old reload here.
2121 Although scratches need hard registers and it
2122 might result in spilling other pseudos, no reload
2123 insns are generated for the scratches. So it
2124 might cost something but probably less than old
2125 reload pass believes. */
2126 if (scratch_p)
2128 if (lra_dump_file != NULL)
2129 fprintf (lra_dump_file,
2130 " %d Scratch win: reject+=2\n",
2131 nop);
2132 reject += 2;
2136 else if (did_match)
2137 this_alternative_match_win = true;
2138 else
2140 int const_to_mem = 0;
2141 bool no_regs_p;
2143 reject += op_reject;
2144 /* Never do output reload of stack pointer. It makes
2145 impossible to do elimination when SP is changed in
2146 RTL. */
2147 if (op == stack_pointer_rtx && ! frame_pointer_needed
2148 && curr_static_id->operand[nop].type != OP_IN)
2149 goto fail;
2151 /* If this alternative asks for a specific reg class, see if there
2152 is at least one allocatable register in that class. */
2153 no_regs_p
2154 = (this_alternative == NO_REGS
2155 || (hard_reg_set_subset_p
2156 (reg_class_contents[this_alternative],
2157 lra_no_alloc_regs)));
2159 /* For asms, verify that the class for this alternative is possible
2160 for the mode that is specified. */
2161 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2163 int i;
2164 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2165 if (HARD_REGNO_MODE_OK (i, mode)
2166 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2167 mode, i))
2168 break;
2169 if (i == FIRST_PSEUDO_REGISTER)
2170 winreg = false;
2173 /* If this operand accepts a register, and if the
2174 register class has at least one allocatable register,
2175 then this operand can be reloaded. */
2176 if (winreg && !no_regs_p)
2177 badop = false;
2179 if (badop)
2181 if (lra_dump_file != NULL)
2182 fprintf (lra_dump_file,
2183 " alt=%d: Bad operand -- refuse\n",
2184 nalt);
2185 goto fail;
2188 /* If not assigned pseudo has a class which a subset of
2189 required reg class, it is a less costly alternative
2190 as the pseudo still can get a hard reg of necessary
2191 class. */
2192 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2193 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2194 && ira_class_subset_p[this_alternative][cl])
2196 if (lra_dump_file != NULL)
2197 fprintf
2198 (lra_dump_file,
2199 " %d Super set class reg: reject-=3\n", nop);
2200 reject -= 3;
2203 this_alternative_offmemok = offmemok;
2204 if (this_costly_alternative != NO_REGS)
2206 if (lra_dump_file != NULL)
2207 fprintf (lra_dump_file,
2208 " %d Costly loser: reject++\n", nop);
2209 reject++;
2211 /* If the operand is dying, has a matching constraint,
2212 and satisfies constraints of the matched operand
2213 which failed to satisfy the own constraints, most probably
2214 the reload for this operand will be gone. */
2215 if (this_alternative_matches >= 0
2216 && !curr_alt_win[this_alternative_matches]
2217 && REG_P (op)
2218 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2219 && (hard_regno[nop] >= 0
2220 ? in_hard_reg_set_p (this_alternative_set,
2221 mode, hard_regno[nop])
2222 : in_class_p (op, this_alternative, NULL)))
2224 if (lra_dump_file != NULL)
2225 fprintf
2226 (lra_dump_file,
2227 " %d Dying matched operand reload: reject++\n",
2228 nop);
2229 reject++;
2231 else
2233 /* Strict_low_part requires to reload the register
2234 not the sub-register. In this case we should
2235 check that a final reload hard reg can hold the
2236 value mode. */
2237 if (curr_static_id->operand[nop].strict_low
2238 && REG_P (op)
2239 && hard_regno[nop] < 0
2240 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2241 && ira_class_hard_regs_num[this_alternative] > 0
2242 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2243 [this_alternative][0],
2244 GET_MODE
2245 (*curr_id->operand_loc[nop])))
2247 if (lra_dump_file != NULL)
2248 fprintf
2249 (lra_dump_file,
2250 " alt=%d: Strict low subreg reload -- refuse\n",
2251 nalt);
2252 goto fail;
2254 losers++;
2256 if (operand_reg[nop] != NULL_RTX
2257 /* Output operands and matched input operands are
2258 not inherited. The following conditions do not
2259 exactly describe the previous statement but they
2260 are pretty close. */
2261 && curr_static_id->operand[nop].type != OP_OUT
2262 && (this_alternative_matches < 0
2263 || curr_static_id->operand[nop].type != OP_IN))
2265 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2266 (operand_reg[nop])]
2267 .last_reload);
2269 /* The value of reload_sum has sense only if we
2270 process insns in their order. It happens only on
2271 the first constraints sub-pass when we do most of
2272 reload work. */
2273 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2274 reload_sum += last_reload - bb_reload_num;
2276 /* If this is a constant that is reloaded into the
2277 desired class by copying it to memory first, count
2278 that as another reload. This is consistent with
2279 other code and is required to avoid choosing another
2280 alternative when the constant is moved into memory.
2281 Note that the test here is precisely the same as in
2282 the code below that calls force_const_mem. */
2283 if (CONST_POOL_OK_P (mode, op)
2284 && ((targetm.preferred_reload_class
2285 (op, this_alternative) == NO_REGS)
2286 || no_input_reloads_p))
2288 const_to_mem = 1;
2289 if (! no_regs_p)
2290 losers++;
2293 /* Alternative loses if it requires a type of reload not
2294 permitted for this insn. We can always reload
2295 objects with a REG_UNUSED note. */
2296 if ((curr_static_id->operand[nop].type != OP_IN
2297 && no_output_reloads_p
2298 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2299 || (curr_static_id->operand[nop].type != OP_OUT
2300 && no_input_reloads_p && ! const_to_mem)
2301 || (this_alternative_matches >= 0
2302 && (no_input_reloads_p
2303 || (no_output_reloads_p
2304 && (curr_static_id->operand
2305 [this_alternative_matches].type != OP_IN)
2306 && ! find_reg_note (curr_insn, REG_UNUSED,
2307 no_subreg_reg_operand
2308 [this_alternative_matches])))))
2310 if (lra_dump_file != NULL)
2311 fprintf
2312 (lra_dump_file,
2313 " alt=%d: No input/otput reload -- refuse\n",
2314 nalt);
2315 goto fail;
2318 /* Alternative loses if it required class pseudo can not
2319 hold value of required mode. Such insns can be
2320 described by insn definitions with mode iterators. */
2321 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2322 && ! hard_reg_set_empty_p (this_alternative_set)
2323 /* It is common practice for constraints to use a
2324 class which does not have actually enough regs to
2325 hold the value (e.g. x86 AREG for mode requiring
2326 more one general reg). Therefore we have 2
2327 conditions to check that the reload pseudo can
2328 not hold the mode value. */
2329 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2330 [this_alternative][0],
2331 GET_MODE (*curr_id->operand_loc[nop]))
2332 /* The above condition is not enough as the first
2333 reg in ira_class_hard_regs can be not aligned for
2334 multi-words mode values. */
2335 && (prohibited_class_reg_set_mode_p
2336 (this_alternative, this_alternative_set,
2337 GET_MODE (*curr_id->operand_loc[nop]))))
2339 if (lra_dump_file != NULL)
2340 fprintf (lra_dump_file,
2341 " alt=%d: reload pseudo for op %d "
2342 " can not hold the mode value -- refuse\n",
2343 nalt, nop);
2344 goto fail;
2347 /* Check strong discouragement of reload of non-constant
2348 into class THIS_ALTERNATIVE. */
2349 if (! CONSTANT_P (op) && ! no_regs_p
2350 && (targetm.preferred_reload_class
2351 (op, this_alternative) == NO_REGS
2352 || (curr_static_id->operand[nop].type == OP_OUT
2353 && (targetm.preferred_output_reload_class
2354 (op, this_alternative) == NO_REGS))))
2356 if (lra_dump_file != NULL)
2357 fprintf (lra_dump_file,
2358 " %d Non-prefered reload: reject+=%d\n",
2359 nop, LRA_MAX_REJECT);
2360 reject += LRA_MAX_REJECT;
2363 if (! (MEM_P (op) && offmemok)
2364 && ! (const_to_mem && constmemok))
2366 /* We prefer to reload pseudos over reloading other
2367 things, since such reloads may be able to be
2368 eliminated later. So bump REJECT in other cases.
2369 Don't do this in the case where we are forcing a
2370 constant into memory and it will then win since
2371 we don't want to have a different alternative
2372 match then. */
2373 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2375 if (lra_dump_file != NULL)
2376 fprintf
2377 (lra_dump_file,
2378 " %d Non-pseudo reload: reject+=2\n",
2379 nop);
2380 reject += 2;
2383 if (! no_regs_p)
2384 reload_nregs
2385 += ira_reg_class_max_nregs[this_alternative][mode];
2387 if (SMALL_REGISTER_CLASS_P (this_alternative))
2389 if (lra_dump_file != NULL)
2390 fprintf
2391 (lra_dump_file,
2392 " %d Small class reload: reject+=%d\n",
2393 nop, LRA_LOSER_COST_FACTOR / 2);
2394 reject += LRA_LOSER_COST_FACTOR / 2;
2398 /* We are trying to spill pseudo into memory. It is
2399 usually more costly than moving to a hard register
2400 although it might takes the same number of
2401 reloads. */
2402 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2404 if (lra_dump_file != NULL)
2405 fprintf
2406 (lra_dump_file,
2407 " %d Spill pseudo into memory: reject+=3\n",
2408 nop);
2409 reject += 3;
2410 if (VECTOR_MODE_P (mode))
2412 /* Spilling vectors into memory is usually more
2413 costly as they contain big values. */
2414 if (lra_dump_file != NULL)
2415 fprintf
2416 (lra_dump_file,
2417 " %d Spill vector pseudo: reject+=2\n",
2418 nop);
2419 reject += 2;
2423 #ifdef SECONDARY_MEMORY_NEEDED
2424 /* If reload requires moving value through secondary
2425 memory, it will need one more insn at least. */
2426 if (this_alternative != NO_REGS
2427 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2428 && ((curr_static_id->operand[nop].type != OP_OUT
2429 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2430 GET_MODE (op)))
2431 || (curr_static_id->operand[nop].type != OP_IN
2432 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2433 GET_MODE (op)))))
2434 losers++;
2435 #endif
2436 /* Input reloads can be inherited more often than output
2437 reloads can be removed, so penalize output
2438 reloads. */
2439 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2441 if (lra_dump_file != NULL)
2442 fprintf
2443 (lra_dump_file,
2444 " %d Non input pseudo reload: reject++\n",
2445 nop);
2446 reject++;
2450 if (early_clobber_p && ! scratch_p)
2452 if (lra_dump_file != NULL)
2453 fprintf (lra_dump_file,
2454 " %d Early clobber: reject++\n", nop);
2455 reject++;
2457 /* ??? We check early clobbers after processing all operands
2458 (see loop below) and there we update the costs more.
2459 Should we update the cost (may be approximately) here
2460 because of early clobber register reloads or it is a rare
2461 or non-important thing to be worth to do it. */
2462 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2463 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2465 if (lra_dump_file != NULL)
2466 fprintf (lra_dump_file,
2467 " alt=%d,overall=%d,losers=%d -- refuse\n",
2468 nalt, overall, losers);
2469 goto fail;
2472 curr_alt[nop] = this_alternative;
2473 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2474 curr_alt_win[nop] = this_alternative_win;
2475 curr_alt_match_win[nop] = this_alternative_match_win;
2476 curr_alt_offmemok[nop] = this_alternative_offmemok;
2477 curr_alt_matches[nop] = this_alternative_matches;
2479 if (this_alternative_matches >= 0
2480 && !did_match && !this_alternative_win)
2481 curr_alt_win[this_alternative_matches] = false;
2483 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2484 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2486 if (curr_insn_set != NULL_RTX && n_operands == 2
2487 /* Prevent processing non-move insns. */
2488 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2489 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2490 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2491 && REG_P (no_subreg_reg_operand[0])
2492 && REG_P (no_subreg_reg_operand[1])
2493 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2494 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2495 || (! curr_alt_win[0] && curr_alt_win[1]
2496 && REG_P (no_subreg_reg_operand[1])
2497 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2498 || (curr_alt_win[0] && ! curr_alt_win[1]
2499 && REG_P (no_subreg_reg_operand[0])
2500 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2501 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2502 no_subreg_reg_operand[1])
2503 || (targetm.preferred_reload_class
2504 (no_subreg_reg_operand[1],
2505 (enum reg_class) curr_alt[1]) != NO_REGS))
2506 /* If it is a result of recent elimination in move
2507 insn we can transform it into an add still by
2508 using this alternative. */
2509 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2511 /* We have a move insn and a new reload insn will be similar
2512 to the current insn. We should avoid such situation as it
2513 results in LRA cycling. */
2514 overall += LRA_MAX_REJECT;
2516 ok_p = true;
2517 curr_alt_dont_inherit_ops_num = 0;
2518 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2520 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2521 HARD_REG_SET temp_set;
2523 i = early_clobbered_nops[nop];
2524 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2525 || hard_regno[i] < 0)
2526 continue;
2527 lra_assert (operand_reg[i] != NULL_RTX);
2528 clobbered_hard_regno = hard_regno[i];
2529 CLEAR_HARD_REG_SET (temp_set);
2530 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2531 first_conflict_j = last_conflict_j = -1;
2532 for (j = 0; j < n_operands; j++)
2533 if (j == i
2534 /* We don't want process insides of match_operator and
2535 match_parallel because otherwise we would process
2536 their operands once again generating a wrong
2537 code. */
2538 || curr_static_id->operand[j].is_operator)
2539 continue;
2540 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2541 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2542 continue;
2543 /* If we don't reload j-th operand, check conflicts. */
2544 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2545 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2547 if (first_conflict_j < 0)
2548 first_conflict_j = j;
2549 last_conflict_j = j;
2551 if (last_conflict_j < 0)
2552 continue;
2553 /* If earlyclobber operand conflicts with another
2554 non-matching operand which is actually the same register
2555 as the earlyclobber operand, it is better to reload the
2556 another operand as an operand matching the earlyclobber
2557 operand can be also the same. */
2558 if (first_conflict_j == last_conflict_j
2559 && operand_reg[last_conflict_j] != NULL_RTX
2560 && ! curr_alt_match_win[last_conflict_j]
2561 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2563 curr_alt_win[last_conflict_j] = false;
2564 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2565 = last_conflict_j;
2566 losers++;
2567 /* Early clobber was already reflected in REJECT. */
2568 lra_assert (reject > 0);
2569 if (lra_dump_file != NULL)
2570 fprintf
2571 (lra_dump_file,
2572 " %d Conflict early clobber reload: reject--\n",
2574 reject--;
2575 overall += LRA_LOSER_COST_FACTOR - 1;
2577 else
2579 /* We need to reload early clobbered register and the
2580 matched registers. */
2581 for (j = 0; j < n_operands; j++)
2582 if (curr_alt_matches[j] == i)
2584 curr_alt_match_win[j] = false;
2585 losers++;
2586 overall += LRA_LOSER_COST_FACTOR;
2588 if (! curr_alt_match_win[i])
2589 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2590 else
2592 /* Remember pseudos used for match reloads are never
2593 inherited. */
2594 lra_assert (curr_alt_matches[i] >= 0);
2595 curr_alt_win[curr_alt_matches[i]] = false;
2597 curr_alt_win[i] = curr_alt_match_win[i] = false;
2598 losers++;
2599 /* Early clobber was already reflected in REJECT. */
2600 lra_assert (reject > 0);
2601 if (lra_dump_file != NULL)
2602 fprintf
2603 (lra_dump_file,
2604 " %d Matched conflict early clobber reloads:"
2605 "reject--\n",
2607 reject--;
2608 overall += LRA_LOSER_COST_FACTOR - 1;
2611 if (lra_dump_file != NULL)
2612 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2613 nalt, overall, losers, reload_nregs);
2615 /* If this alternative can be made to work by reloading, and it
2616 needs less reloading than the others checked so far, record
2617 it as the chosen goal for reloading. */
2618 if ((best_losers != 0 && losers == 0)
2619 || (((best_losers == 0 && losers == 0)
2620 || (best_losers != 0 && losers != 0))
2621 && (best_overall > overall
2622 || (best_overall == overall
2623 /* If the cost of the reloads is the same,
2624 prefer alternative which requires minimal
2625 number of reload regs. */
2626 && (reload_nregs < best_reload_nregs
2627 || (reload_nregs == best_reload_nregs
2628 && (best_reload_sum < reload_sum
2629 || (best_reload_sum == reload_sum
2630 && nalt < goal_alt_number))))))))
2632 for (nop = 0; nop < n_operands; nop++)
2634 goal_alt_win[nop] = curr_alt_win[nop];
2635 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2636 goal_alt_matches[nop] = curr_alt_matches[nop];
2637 goal_alt[nop] = curr_alt[nop];
2638 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2640 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2641 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2642 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2643 goal_alt_swapped = curr_swapped;
2644 best_overall = overall;
2645 best_losers = losers;
2646 best_reload_nregs = reload_nregs;
2647 best_reload_sum = reload_sum;
2648 goal_alt_number = nalt;
2650 if (losers == 0)
2651 /* Everything is satisfied. Do not process alternatives
2652 anymore. */
2653 break;
2654 fail:
2657 return ok_p;
2660 /* Make reload base reg from address AD. */
2661 static rtx
2662 base_to_reg (struct address_info *ad)
2664 enum reg_class cl;
2665 int code = -1;
2666 rtx new_inner = NULL_RTX;
2667 rtx new_reg = NULL_RTX;
2668 rtx_insn *insn;
2669 rtx_insn *last_insn = get_last_insn();
2671 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2672 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2673 get_index_code (ad));
2674 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2675 cl, "base");
2676 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2677 ad->disp_term == NULL
2678 ? gen_int_mode (0, ad->mode)
2679 : *ad->disp_term);
2680 if (!valid_address_p (ad->mode, new_inner, ad->as))
2681 return NULL_RTX;
2682 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base_term));
2683 code = recog_memoized (insn);
2684 if (code < 0)
2686 delete_insns_since (last_insn);
2687 return NULL_RTX;
2690 return new_inner;
2693 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2694 static rtx
2695 base_plus_disp_to_reg (struct address_info *ad)
2697 enum reg_class cl;
2698 rtx new_reg;
2700 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2701 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2702 get_index_code (ad));
2703 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2704 cl, "base + disp");
2705 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2706 return new_reg;
2709 /* Make reload of index part of address AD. Return the new
2710 pseudo. */
2711 static rtx
2712 index_part_to_reg (struct address_info *ad)
2714 rtx new_reg;
2716 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2717 INDEX_REG_CLASS, "index term");
2718 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2719 GEN_INT (get_index_scale (ad)), new_reg, 1);
2720 return new_reg;
2723 /* Return true if we can add a displacement to address AD, even if that
2724 makes the address invalid. The fix-up code requires any new address
2725 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2726 static bool
2727 can_add_disp_p (struct address_info *ad)
2729 return (!ad->autoinc_p
2730 && ad->segment == NULL
2731 && ad->base == ad->base_term
2732 && ad->disp == ad->disp_term);
2735 /* Make equiv substitution in address AD. Return true if a substitution
2736 was made. */
2737 static bool
2738 equiv_address_substitution (struct address_info *ad)
2740 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2741 HOST_WIDE_INT disp, scale;
2742 bool change_p;
2744 base_term = strip_subreg (ad->base_term);
2745 if (base_term == NULL)
2746 base_reg = new_base_reg = NULL_RTX;
2747 else
2749 base_reg = *base_term;
2750 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2752 index_term = strip_subreg (ad->index_term);
2753 if (index_term == NULL)
2754 index_reg = new_index_reg = NULL_RTX;
2755 else
2757 index_reg = *index_term;
2758 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2760 if (base_reg == new_base_reg && index_reg == new_index_reg)
2761 return false;
2762 disp = 0;
2763 change_p = false;
2764 if (lra_dump_file != NULL)
2766 fprintf (lra_dump_file, "Changing address in insn %d ",
2767 INSN_UID (curr_insn));
2768 dump_value_slim (lra_dump_file, *ad->outer, 1);
2770 if (base_reg != new_base_reg)
2772 if (REG_P (new_base_reg))
2774 *base_term = new_base_reg;
2775 change_p = true;
2777 else if (GET_CODE (new_base_reg) == PLUS
2778 && REG_P (XEXP (new_base_reg, 0))
2779 && CONST_INT_P (XEXP (new_base_reg, 1))
2780 && can_add_disp_p (ad))
2782 disp += INTVAL (XEXP (new_base_reg, 1));
2783 *base_term = XEXP (new_base_reg, 0);
2784 change_p = true;
2786 if (ad->base_term2 != NULL)
2787 *ad->base_term2 = *ad->base_term;
2789 if (index_reg != new_index_reg)
2791 if (REG_P (new_index_reg))
2793 *index_term = new_index_reg;
2794 change_p = true;
2796 else if (GET_CODE (new_index_reg) == PLUS
2797 && REG_P (XEXP (new_index_reg, 0))
2798 && CONST_INT_P (XEXP (new_index_reg, 1))
2799 && can_add_disp_p (ad)
2800 && (scale = get_index_scale (ad)))
2802 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2803 *index_term = XEXP (new_index_reg, 0);
2804 change_p = true;
2807 if (disp != 0)
2809 if (ad->disp != NULL)
2810 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2811 else
2813 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2814 update_address (ad);
2816 change_p = true;
2818 if (lra_dump_file != NULL)
2820 if (! change_p)
2821 fprintf (lra_dump_file, " -- no change\n");
2822 else
2824 fprintf (lra_dump_file, " on equiv ");
2825 dump_value_slim (lra_dump_file, *ad->outer, 1);
2826 fprintf (lra_dump_file, "\n");
2829 return change_p;
2832 /* Major function to make reloads for an address in operand NOP or
2833 check its correctness (If CHECK_ONLY_P is true). The supported
2834 cases are:
2836 1) an address that existed before LRA started, at which point it
2837 must have been valid. These addresses are subject to elimination
2838 and may have become invalid due to the elimination offset being out
2839 of range.
2841 2) an address created by forcing a constant to memory
2842 (force_const_to_mem). The initial form of these addresses might
2843 not be valid, and it is this function's job to make them valid.
2845 3) a frame address formed from a register and a (possibly zero)
2846 constant offset. As above, these addresses might not be valid and
2847 this function must make them so.
2849 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2850 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2851 address. Return true for any RTL change.
2853 The function is a helper function which does not produce all
2854 transformations (when CHECK_ONLY_P is false) which can be
2855 necessary. It does just basic steps. To do all necessary
2856 transformations use function process_address. */
2857 static bool
2858 process_address_1 (int nop, bool check_only_p,
2859 rtx_insn **before, rtx_insn **after)
2861 struct address_info ad;
2862 rtx new_reg;
2863 rtx op = *curr_id->operand_loc[nop];
2864 const char *constraint = curr_static_id->operand[nop].constraint;
2865 enum constraint_num cn = lookup_constraint (constraint);
2866 bool change_p = false;
2868 if (MEM_P (op)
2869 && GET_MODE (op) == BLKmode
2870 && GET_CODE (XEXP (op, 0)) == SCRATCH)
2871 return false;
2873 if (insn_extra_address_constraint (cn))
2874 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2875 else if (MEM_P (op))
2876 decompose_mem_address (&ad, op);
2877 else if (GET_CODE (op) == SUBREG
2878 && MEM_P (SUBREG_REG (op)))
2879 decompose_mem_address (&ad, SUBREG_REG (op));
2880 else
2881 return false;
2882 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
2883 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
2884 when INDEX_REG_CLASS is a single register class. */
2885 if (ad.base_term != NULL
2886 && ad.index_term != NULL
2887 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
2888 && REG_P (*ad.base_term)
2889 && REG_P (*ad.index_term)
2890 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
2891 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
2893 std::swap (ad.base, ad.index);
2894 std::swap (ad.base_term, ad.index_term);
2896 if (! check_only_p)
2897 change_p = equiv_address_substitution (&ad);
2898 if (ad.base_term != NULL
2899 && (process_addr_reg
2900 (ad.base_term, check_only_p, before,
2901 (ad.autoinc_p
2902 && !(REG_P (*ad.base_term)
2903 && find_regno_note (curr_insn, REG_DEAD,
2904 REGNO (*ad.base_term)) != NULL_RTX)
2905 ? after : NULL),
2906 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2907 get_index_code (&ad)))))
2909 change_p = true;
2910 if (ad.base_term2 != NULL)
2911 *ad.base_term2 = *ad.base_term;
2913 if (ad.index_term != NULL
2914 && process_addr_reg (ad.index_term, check_only_p,
2915 before, NULL, INDEX_REG_CLASS))
2916 change_p = true;
2918 /* Target hooks sometimes don't treat extra-constraint addresses as
2919 legitimate address_operands, so handle them specially. */
2920 if (insn_extra_address_constraint (cn)
2921 && satisfies_address_constraint_p (&ad, cn))
2922 return change_p;
2924 if (check_only_p)
2925 return change_p;
2927 /* There are three cases where the shape of *AD.INNER may now be invalid:
2929 1) the original address was valid, but either elimination or
2930 equiv_address_substitution was applied and that made
2931 the address invalid.
2933 2) the address is an invalid symbolic address created by
2934 force_const_to_mem.
2936 3) the address is a frame address with an invalid offset.
2938 4) the address is a frame address with an invalid base.
2940 All these cases involve a non-autoinc address, so there is no
2941 point revalidating other types. */
2942 if (ad.autoinc_p || valid_address_p (&ad))
2943 return change_p;
2945 /* Any index existed before LRA started, so we can assume that the
2946 presence and shape of the index is valid. */
2947 push_to_sequence (*before);
2948 lra_assert (ad.disp == ad.disp_term);
2949 if (ad.base == NULL)
2951 if (ad.index == NULL)
2953 int code = -1;
2954 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2955 SCRATCH, SCRATCH);
2956 rtx addr = *ad.inner;
2958 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2959 if (HAVE_lo_sum)
2961 rtx_insn *insn;
2962 rtx_insn *last = get_last_insn ();
2964 /* addr => lo_sum (new_base, addr), case (2) above. */
2965 insn = emit_insn (gen_rtx_SET
2966 (new_reg,
2967 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2968 code = recog_memoized (insn);
2969 if (code >= 0)
2971 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2972 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2974 /* Try to put lo_sum into register. */
2975 insn = emit_insn (gen_rtx_SET
2976 (new_reg,
2977 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2978 code = recog_memoized (insn);
2979 if (code >= 0)
2981 *ad.inner = new_reg;
2982 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2984 *ad.inner = addr;
2985 code = -1;
2991 if (code < 0)
2992 delete_insns_since (last);
2995 if (code < 0)
2997 /* addr => new_base, case (2) above. */
2998 lra_emit_move (new_reg, addr);
2999 *ad.inner = new_reg;
3002 else
3004 /* index * scale + disp => new base + index * scale,
3005 case (1) above. */
3006 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3007 GET_CODE (*ad.index));
3009 lra_assert (INDEX_REG_CLASS != NO_REGS);
3010 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3011 lra_emit_move (new_reg, *ad.disp);
3012 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3013 new_reg, *ad.index);
3016 else if (ad.index == NULL)
3018 int regno;
3019 enum reg_class cl;
3020 rtx set;
3021 rtx_insn *insns, *last_insn;
3022 /* Try to reload base into register only if the base is invalid
3023 for the address but with valid offset, case (4) above. */
3024 start_sequence ();
3025 new_reg = base_to_reg (&ad);
3027 /* base + disp => new base, cases (1) and (3) above. */
3028 /* Another option would be to reload the displacement into an
3029 index register. However, postreload has code to optimize
3030 address reloads that have the same base and different
3031 displacements, so reloading into an index register would
3032 not necessarily be a win. */
3033 if (new_reg == NULL_RTX)
3034 new_reg = base_plus_disp_to_reg (&ad);
3035 insns = get_insns ();
3036 last_insn = get_last_insn ();
3037 /* If we generated at least two insns, try last insn source as
3038 an address. If we succeed, we generate one less insn. */
3039 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
3040 && GET_CODE (SET_SRC (set)) == PLUS
3041 && REG_P (XEXP (SET_SRC (set), 0))
3042 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3044 *ad.inner = SET_SRC (set);
3045 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3047 *ad.base_term = XEXP (SET_SRC (set), 0);
3048 *ad.disp_term = XEXP (SET_SRC (set), 1);
3049 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3050 get_index_code (&ad));
3051 regno = REGNO (*ad.base_term);
3052 if (regno >= FIRST_PSEUDO_REGISTER
3053 && cl != lra_get_allocno_class (regno))
3054 lra_change_class (regno, cl, " Change to", true);
3055 new_reg = SET_SRC (set);
3056 delete_insns_since (PREV_INSN (last_insn));
3059 /* Try if target can split displacement into legitimite new disp
3060 and offset. If it's the case, we replace the last insn with
3061 insns for base + offset => new_reg and set new_reg + new disp
3062 to *ad.inner. */
3063 last_insn = get_last_insn ();
3064 if ((set = single_set (last_insn)) != NULL_RTX
3065 && GET_CODE (SET_SRC (set)) == PLUS
3066 && REG_P (XEXP (SET_SRC (set), 0))
3067 && REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
3068 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3070 rtx addend, disp = XEXP (SET_SRC (set), 1);
3071 if (targetm.legitimize_address_displacement (&disp, &addend,
3072 ad.mode))
3074 rtx_insn *new_insns;
3075 start_sequence ();
3076 lra_emit_add (new_reg, XEXP (SET_SRC (set), 0), addend);
3077 new_insns = get_insns ();
3078 end_sequence ();
3079 new_reg = gen_rtx_PLUS (Pmode, new_reg, disp);
3080 delete_insns_since (PREV_INSN (last_insn));
3081 add_insn (new_insns);
3082 insns = get_insns ();
3085 end_sequence ();
3086 emit_insn (insns);
3087 *ad.inner = new_reg;
3089 else if (ad.disp_term != NULL)
3091 /* base + scale * index + disp => new base + scale * index,
3092 case (1) above. */
3093 new_reg = base_plus_disp_to_reg (&ad);
3094 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3095 new_reg, *ad.index);
3097 else if (get_index_scale (&ad) == 1)
3099 /* The last transformation to one reg will be made in
3100 curr_insn_transform function. */
3101 end_sequence ();
3102 return false;
3104 else
3106 /* base + scale * index => base + new_reg,
3107 case (1) above.
3108 Index part of address may become invalid. For example, we
3109 changed pseudo on the equivalent memory and a subreg of the
3110 pseudo onto the memory of different mode for which the scale is
3111 prohibitted. */
3112 new_reg = index_part_to_reg (&ad);
3113 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3114 *ad.base_term, new_reg);
3116 *before = get_insns ();
3117 end_sequence ();
3118 return true;
3121 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3122 Use process_address_1 as a helper function. Return true for any
3123 RTL changes.
3125 If CHECK_ONLY_P is true, just check address correctness. Return
3126 false if the address correct. */
3127 static bool
3128 process_address (int nop, bool check_only_p,
3129 rtx_insn **before, rtx_insn **after)
3131 bool res = false;
3133 while (process_address_1 (nop, check_only_p, before, after))
3135 if (check_only_p)
3136 return true;
3137 res = true;
3139 return res;
3142 /* Emit insns to reload VALUE into a new register. VALUE is an
3143 auto-increment or auto-decrement RTX whose operand is a register or
3144 memory location; so reloading involves incrementing that location.
3145 IN is either identical to VALUE, or some cheaper place to reload
3146 value being incremented/decremented from.
3148 INC_AMOUNT is the number to increment or decrement by (always
3149 positive and ignored for POST_MODIFY/PRE_MODIFY).
3151 Return pseudo containing the result. */
3152 static rtx
3153 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3155 /* REG or MEM to be copied and incremented. */
3156 rtx incloc = XEXP (value, 0);
3157 /* Nonzero if increment after copying. */
3158 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3159 || GET_CODE (value) == POST_MODIFY);
3160 rtx_insn *last;
3161 rtx inc;
3162 rtx_insn *add_insn;
3163 int code;
3164 rtx real_in = in == value ? incloc : in;
3165 rtx result;
3166 bool plus_p = true;
3168 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3170 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3171 || GET_CODE (XEXP (value, 1)) == MINUS);
3172 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3173 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3174 inc = XEXP (XEXP (value, 1), 1);
3176 else
3178 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3179 inc_amount = -inc_amount;
3181 inc = GEN_INT (inc_amount);
3184 if (! post && REG_P (incloc))
3185 result = incloc;
3186 else
3187 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3188 "INC/DEC result");
3190 if (real_in != result)
3192 /* First copy the location to the result register. */
3193 lra_assert (REG_P (result));
3194 emit_insn (gen_move_insn (result, real_in));
3197 /* We suppose that there are insns to add/sub with the constant
3198 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3199 old reload worked with this assumption. If the assumption
3200 becomes wrong, we should use approach in function
3201 base_plus_disp_to_reg. */
3202 if (in == value)
3204 /* See if we can directly increment INCLOC. */
3205 last = get_last_insn ();
3206 add_insn = emit_insn (plus_p
3207 ? gen_add2_insn (incloc, inc)
3208 : gen_sub2_insn (incloc, inc));
3210 code = recog_memoized (add_insn);
3211 if (code >= 0)
3213 if (! post && result != incloc)
3214 emit_insn (gen_move_insn (result, incloc));
3215 return result;
3217 delete_insns_since (last);
3220 /* If couldn't do the increment directly, must increment in RESULT.
3221 The way we do this depends on whether this is pre- or
3222 post-increment. For pre-increment, copy INCLOC to the reload
3223 register, increment it there, then save back. */
3224 if (! post)
3226 if (real_in != result)
3227 emit_insn (gen_move_insn (result, real_in));
3228 if (plus_p)
3229 emit_insn (gen_add2_insn (result, inc));
3230 else
3231 emit_insn (gen_sub2_insn (result, inc));
3232 if (result != incloc)
3233 emit_insn (gen_move_insn (incloc, result));
3235 else
3237 /* Post-increment.
3239 Because this might be a jump insn or a compare, and because
3240 RESULT may not be available after the insn in an input
3241 reload, we must do the incrementing before the insn being
3242 reloaded for.
3244 We have already copied IN to RESULT. Increment the copy in
3245 RESULT, save that back, then decrement RESULT so it has
3246 the original value. */
3247 if (plus_p)
3248 emit_insn (gen_add2_insn (result, inc));
3249 else
3250 emit_insn (gen_sub2_insn (result, inc));
3251 emit_insn (gen_move_insn (incloc, result));
3252 /* Restore non-modified value for the result. We prefer this
3253 way because it does not require an additional hard
3254 register. */
3255 if (plus_p)
3257 if (CONST_INT_P (inc))
3258 emit_insn (gen_add2_insn (result,
3259 gen_int_mode (-INTVAL (inc),
3260 GET_MODE (result))));
3261 else
3262 emit_insn (gen_sub2_insn (result, inc));
3264 else
3265 emit_insn (gen_add2_insn (result, inc));
3267 return result;
3270 /* Return true if the current move insn does not need processing as we
3271 already know that it satisfies its constraints. */
3272 static bool
3273 simple_move_p (void)
3275 rtx dest, src;
3276 enum reg_class dclass, sclass;
3278 lra_assert (curr_insn_set != NULL_RTX);
3279 dest = SET_DEST (curr_insn_set);
3280 src = SET_SRC (curr_insn_set);
3281 return ((dclass = get_op_class (dest)) != NO_REGS
3282 && (sclass = get_op_class (src)) != NO_REGS
3283 /* The backend guarantees that register moves of cost 2
3284 never need reloads. */
3285 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3288 /* Swap operands NOP and NOP + 1. */
3289 static inline void
3290 swap_operands (int nop)
3292 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3293 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3294 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3295 /* Swap the duplicates too. */
3296 lra_update_dup (curr_id, nop);
3297 lra_update_dup (curr_id, nop + 1);
3300 /* Main entry point of the constraint code: search the body of the
3301 current insn to choose the best alternative. It is mimicking insn
3302 alternative cost calculation model of former reload pass. That is
3303 because machine descriptions were written to use this model. This
3304 model can be changed in future. Make commutative operand exchange
3305 if it is chosen.
3307 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3308 constraints. Return true if any change happened during function
3309 call.
3311 If CHECK_ONLY_P is true then don't do any transformation. Just
3312 check that the insn satisfies all constraints. If the insn does
3313 not satisfy any constraint, return true. */
3314 static bool
3315 curr_insn_transform (bool check_only_p)
3317 int i, j, k;
3318 int n_operands;
3319 int n_alternatives;
3320 int commutative;
3321 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3322 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3323 rtx_insn *before, *after;
3324 bool alt_p = false;
3325 /* Flag that the insn has been changed through a transformation. */
3326 bool change_p;
3327 bool sec_mem_p;
3328 #ifdef SECONDARY_MEMORY_NEEDED
3329 bool use_sec_mem_p;
3330 #endif
3331 int max_regno_before;
3332 int reused_alternative_num;
3334 curr_insn_set = single_set (curr_insn);
3335 if (curr_insn_set != NULL_RTX && simple_move_p ())
3336 return false;
3338 no_input_reloads_p = no_output_reloads_p = false;
3339 goal_alt_number = -1;
3340 change_p = sec_mem_p = false;
3341 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3342 reloads; neither are insns that SET cc0. Insns that use CC0 are
3343 not allowed to have any input reloads. */
3344 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3345 no_output_reloads_p = true;
3347 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3348 no_input_reloads_p = true;
3349 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3350 no_output_reloads_p = true;
3352 n_operands = curr_static_id->n_operands;
3353 n_alternatives = curr_static_id->n_alternatives;
3355 /* Just return "no reloads" if insn has no operands with
3356 constraints. */
3357 if (n_operands == 0 || n_alternatives == 0)
3358 return false;
3360 max_regno_before = max_reg_num ();
3362 for (i = 0; i < n_operands; i++)
3364 goal_alt_matched[i][0] = -1;
3365 goal_alt_matches[i] = -1;
3368 commutative = curr_static_id->commutative;
3370 /* Now see what we need for pseudos that didn't get hard regs or got
3371 the wrong kind of hard reg. For this, we must consider all the
3372 operands together against the register constraints. */
3374 best_losers = best_overall = INT_MAX;
3375 best_reload_sum = 0;
3377 curr_swapped = false;
3378 goal_alt_swapped = false;
3380 if (! check_only_p)
3381 /* Make equivalence substitution and memory subreg elimination
3382 before address processing because an address legitimacy can
3383 depend on memory mode. */
3384 for (i = 0; i < n_operands; i++)
3386 rtx op, subst, old;
3387 bool op_change_p = false;
3389 if (curr_static_id->operand[i].is_operator)
3390 continue;
3392 old = op = *curr_id->operand_loc[i];
3393 if (GET_CODE (old) == SUBREG)
3394 old = SUBREG_REG (old);
3395 subst = get_equiv_with_elimination (old, curr_insn);
3396 original_subreg_reg_mode[i] = VOIDmode;
3397 if (subst != old)
3399 subst = copy_rtx (subst);
3400 lra_assert (REG_P (old));
3401 if (GET_CODE (op) != SUBREG)
3402 *curr_id->operand_loc[i] = subst;
3403 else
3405 SUBREG_REG (op) = subst;
3406 if (GET_MODE (subst) == VOIDmode)
3407 original_subreg_reg_mode[i] = GET_MODE (old);
3409 if (lra_dump_file != NULL)
3411 fprintf (lra_dump_file,
3412 "Changing pseudo %d in operand %i of insn %u on equiv ",
3413 REGNO (old), i, INSN_UID (curr_insn));
3414 dump_value_slim (lra_dump_file, subst, 1);
3415 fprintf (lra_dump_file, "\n");
3417 op_change_p = change_p = true;
3419 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3421 change_p = true;
3422 lra_update_dup (curr_id, i);
3426 /* Reload address registers and displacements. We do it before
3427 finding an alternative because of memory constraints. */
3428 before = after = NULL;
3429 for (i = 0; i < n_operands; i++)
3430 if (! curr_static_id->operand[i].is_operator
3431 && process_address (i, check_only_p, &before, &after))
3433 if (check_only_p)
3434 return true;
3435 change_p = true;
3436 lra_update_dup (curr_id, i);
3439 if (change_p)
3440 /* If we've changed the instruction then any alternative that
3441 we chose previously may no longer be valid. */
3442 lra_set_used_insn_alternative (curr_insn, -1);
3444 if (! check_only_p && curr_insn_set != NULL_RTX
3445 && check_and_process_move (&change_p, &sec_mem_p))
3446 return change_p;
3448 try_swapped:
3450 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3451 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3452 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3453 reused_alternative_num, INSN_UID (curr_insn));
3455 if (process_alt_operands (reused_alternative_num))
3456 alt_p = true;
3458 if (check_only_p)
3459 return ! alt_p || best_losers != 0;
3461 /* If insn is commutative (it's safe to exchange a certain pair of
3462 operands) then we need to try each alternative twice, the second
3463 time matching those two operands as if we had exchanged them. To
3464 do this, really exchange them in operands.
3466 If we have just tried the alternatives the second time, return
3467 operands to normal and drop through. */
3469 if (reused_alternative_num < 0 && commutative >= 0)
3471 curr_swapped = !curr_swapped;
3472 if (curr_swapped)
3474 swap_operands (commutative);
3475 goto try_swapped;
3477 else
3478 swap_operands (commutative);
3481 if (! alt_p && ! sec_mem_p)
3483 /* No alternative works with reloads?? */
3484 if (INSN_CODE (curr_insn) >= 0)
3485 fatal_insn ("unable to generate reloads for:", curr_insn);
3486 error_for_asm (curr_insn,
3487 "inconsistent operand constraints in an %<asm%>");
3488 /* Avoid further trouble with this insn. */
3489 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3490 lra_invalidate_insn_data (curr_insn);
3491 return true;
3494 /* If the best alternative is with operands 1 and 2 swapped, swap
3495 them. Update the operand numbers of any reloads already
3496 pushed. */
3498 if (goal_alt_swapped)
3500 if (lra_dump_file != NULL)
3501 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3502 INSN_UID (curr_insn));
3504 /* Swap the duplicates too. */
3505 swap_operands (commutative);
3506 change_p = true;
3509 #ifdef SECONDARY_MEMORY_NEEDED
3510 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3511 too conservatively. So we use the secondary memory only if there
3512 is no any alternative without reloads. */
3513 use_sec_mem_p = false;
3514 if (! alt_p)
3515 use_sec_mem_p = true;
3516 else if (sec_mem_p)
3518 for (i = 0; i < n_operands; i++)
3519 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3520 break;
3521 use_sec_mem_p = i < n_operands;
3524 if (use_sec_mem_p)
3526 rtx new_reg, src, dest, rld;
3527 machine_mode sec_mode, rld_mode;
3529 lra_assert (sec_mem_p);
3530 lra_assert (curr_static_id->operand[0].type == OP_OUT
3531 && curr_static_id->operand[1].type == OP_IN);
3532 dest = *curr_id->operand_loc[0];
3533 src = *curr_id->operand_loc[1];
3534 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3535 ? dest : src);
3536 rld_mode = GET_MODE (rld);
3537 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3538 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3539 #else
3540 sec_mode = rld_mode;
3541 #endif
3542 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3543 NO_REGS, "secondary");
3544 /* If the mode is changed, it should be wider. */
3545 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3546 if (sec_mode != rld_mode)
3548 /* If the target says specifically to use another mode for
3549 secondary memory moves we can not reuse the original
3550 insn. */
3551 after = emit_spill_move (false, new_reg, dest);
3552 lra_process_new_insns (curr_insn, NULL, after,
3553 "Inserting the sec. move");
3554 /* We may have non null BEFORE here (e.g. after address
3555 processing. */
3556 push_to_sequence (before);
3557 before = emit_spill_move (true, new_reg, src);
3558 emit_insn (before);
3559 before = get_insns ();
3560 end_sequence ();
3561 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3562 lra_set_insn_deleted (curr_insn);
3564 else if (dest == rld)
3566 *curr_id->operand_loc[0] = new_reg;
3567 after = emit_spill_move (false, new_reg, dest);
3568 lra_process_new_insns (curr_insn, NULL, after,
3569 "Inserting the sec. move");
3571 else
3573 *curr_id->operand_loc[1] = new_reg;
3574 /* See comments above. */
3575 push_to_sequence (before);
3576 before = emit_spill_move (true, new_reg, src);
3577 emit_insn (before);
3578 before = get_insns ();
3579 end_sequence ();
3580 lra_process_new_insns (curr_insn, before, NULL,
3581 "Inserting the sec. move");
3583 lra_update_insn_regno_info (curr_insn);
3584 return true;
3586 #endif
3588 lra_assert (goal_alt_number >= 0);
3589 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3591 if (lra_dump_file != NULL)
3593 const char *p;
3595 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3596 goal_alt_number, INSN_UID (curr_insn));
3597 for (i = 0; i < n_operands; i++)
3599 p = (curr_static_id->operand_alternative
3600 [goal_alt_number * n_operands + i].constraint);
3601 if (*p == '\0')
3602 continue;
3603 fprintf (lra_dump_file, " (%d) ", i);
3604 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3605 fputc (*p, lra_dump_file);
3607 if (INSN_CODE (curr_insn) >= 0
3608 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3609 fprintf (lra_dump_file, " {%s}", p);
3610 if (curr_id->sp_offset != 0)
3611 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3612 curr_id->sp_offset);
3613 fprintf (lra_dump_file, "\n");
3616 /* Right now, for any pair of operands I and J that are required to
3617 match, with J < I, goal_alt_matches[I] is J. Add I to
3618 goal_alt_matched[J]. */
3620 for (i = 0; i < n_operands; i++)
3621 if ((j = goal_alt_matches[i]) >= 0)
3623 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3625 /* We allow matching one output operand and several input
3626 operands. */
3627 lra_assert (k == 0
3628 || (curr_static_id->operand[j].type == OP_OUT
3629 && curr_static_id->operand[i].type == OP_IN
3630 && (curr_static_id->operand
3631 [goal_alt_matched[j][0]].type == OP_IN)));
3632 goal_alt_matched[j][k] = i;
3633 goal_alt_matched[j][k + 1] = -1;
3636 for (i = 0; i < n_operands; i++)
3637 goal_alt_win[i] |= goal_alt_match_win[i];
3639 /* Any constants that aren't allowed and can't be reloaded into
3640 registers are here changed into memory references. */
3641 for (i = 0; i < n_operands; i++)
3642 if (goal_alt_win[i])
3644 int regno;
3645 enum reg_class new_class;
3646 rtx reg = *curr_id->operand_loc[i];
3648 if (GET_CODE (reg) == SUBREG)
3649 reg = SUBREG_REG (reg);
3651 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3653 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3655 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3657 lra_assert (ok_p);
3658 lra_change_class (regno, new_class, " Change to", true);
3662 else
3664 const char *constraint;
3665 char c;
3666 rtx op = *curr_id->operand_loc[i];
3667 rtx subreg = NULL_RTX;
3668 machine_mode mode = curr_operand_mode[i];
3670 if (GET_CODE (op) == SUBREG)
3672 subreg = op;
3673 op = SUBREG_REG (op);
3674 mode = GET_MODE (op);
3677 if (CONST_POOL_OK_P (mode, op)
3678 && ((targetm.preferred_reload_class
3679 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3680 || no_input_reloads_p))
3682 rtx tem = force_const_mem (mode, op);
3684 change_p = true;
3685 if (subreg != NULL_RTX)
3686 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3688 *curr_id->operand_loc[i] = tem;
3689 lra_update_dup (curr_id, i);
3690 process_address (i, false, &before, &after);
3692 /* If the alternative accepts constant pool refs directly
3693 there will be no reload needed at all. */
3694 if (subreg != NULL_RTX)
3695 continue;
3696 /* Skip alternatives before the one requested. */
3697 constraint = (curr_static_id->operand_alternative
3698 [goal_alt_number * n_operands + i].constraint);
3699 for (;
3700 (c = *constraint) && c != ',' && c != '#';
3701 constraint += CONSTRAINT_LEN (c, constraint))
3703 enum constraint_num cn = lookup_constraint (constraint);
3704 if (insn_extra_memory_constraint (cn)
3705 && satisfies_memory_constraint_p (tem, cn))
3706 break;
3708 if (c == '\0' || c == ',' || c == '#')
3709 continue;
3711 goal_alt_win[i] = true;
3715 for (i = 0; i < n_operands; i++)
3717 int regno;
3718 bool optional_p = false;
3719 rtx old, new_reg;
3720 rtx op = *curr_id->operand_loc[i];
3722 if (goal_alt_win[i])
3724 if (goal_alt[i] == NO_REGS
3725 && REG_P (op)
3726 /* When we assign NO_REGS it means that we will not
3727 assign a hard register to the scratch pseudo by
3728 assigment pass and the scratch pseudo will be
3729 spilled. Spilled scratch pseudos are transformed
3730 back to scratches at the LRA end. */
3731 && lra_former_scratch_operand_p (curr_insn, i)
3732 && lra_former_scratch_p (REGNO (op)))
3734 int regno = REGNO (op);
3735 lra_change_class (regno, NO_REGS, " Change to", true);
3736 if (lra_get_regno_hard_regno (regno) >= 0)
3737 /* We don't have to mark all insn affected by the
3738 spilled pseudo as there is only one such insn, the
3739 current one. */
3740 reg_renumber[regno] = -1;
3741 lra_assert (bitmap_single_bit_set_p
3742 (&lra_reg_info[REGNO (op)].insn_bitmap));
3744 /* We can do an optional reload. If the pseudo got a hard
3745 reg, we might improve the code through inheritance. If
3746 it does not get a hard register we coalesce memory/memory
3747 moves later. Ignore move insns to avoid cycling. */
3748 if (! lra_simple_p
3749 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3750 && goal_alt[i] != NO_REGS && REG_P (op)
3751 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3752 && regno < new_regno_start
3753 && ! lra_former_scratch_p (regno)
3754 && reg_renumber[regno] < 0
3755 /* Check that the optional reload pseudo will be able to
3756 hold given mode value. */
3757 && ! (prohibited_class_reg_set_mode_p
3758 (goal_alt[i], reg_class_contents[goal_alt[i]],
3759 PSEUDO_REGNO_MODE (regno)))
3760 && (curr_insn_set == NULL_RTX
3761 || !((REG_P (SET_SRC (curr_insn_set))
3762 || MEM_P (SET_SRC (curr_insn_set))
3763 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3764 && (REG_P (SET_DEST (curr_insn_set))
3765 || MEM_P (SET_DEST (curr_insn_set))
3766 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3767 optional_p = true;
3768 else
3769 continue;
3772 /* Operands that match previous ones have already been handled. */
3773 if (goal_alt_matches[i] >= 0)
3774 continue;
3776 /* We should not have an operand with a non-offsettable address
3777 appearing where an offsettable address will do. It also may
3778 be a case when the address should be special in other words
3779 not a general one (e.g. it needs no index reg). */
3780 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3782 enum reg_class rclass;
3783 rtx *loc = &XEXP (op, 0);
3784 enum rtx_code code = GET_CODE (*loc);
3786 push_to_sequence (before);
3787 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3788 MEM, SCRATCH);
3789 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3790 new_reg = emit_inc (rclass, *loc, *loc,
3791 /* This value does not matter for MODIFY. */
3792 GET_MODE_SIZE (GET_MODE (op)));
3793 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3794 "offsetable address", &new_reg))
3795 lra_emit_move (new_reg, *loc);
3796 before = get_insns ();
3797 end_sequence ();
3798 *loc = new_reg;
3799 lra_update_dup (curr_id, i);
3801 else if (goal_alt_matched[i][0] == -1)
3803 machine_mode mode;
3804 rtx reg, *loc;
3805 int hard_regno, byte;
3806 enum op_type type = curr_static_id->operand[i].type;
3808 loc = curr_id->operand_loc[i];
3809 mode = curr_operand_mode[i];
3810 if (GET_CODE (*loc) == SUBREG)
3812 reg = SUBREG_REG (*loc);
3813 byte = SUBREG_BYTE (*loc);
3814 if (REG_P (reg)
3815 /* Strict_low_part requires reload the register not
3816 the sub-register. */
3817 && (curr_static_id->operand[i].strict_low
3818 || (GET_MODE_SIZE (mode)
3819 <= GET_MODE_SIZE (GET_MODE (reg))
3820 && (hard_regno
3821 = get_try_hard_regno (REGNO (reg))) >= 0
3822 && (simplify_subreg_regno
3823 (hard_regno,
3824 GET_MODE (reg), byte, mode) < 0)
3825 && (goal_alt[i] == NO_REGS
3826 || (simplify_subreg_regno
3827 (ira_class_hard_regs[goal_alt[i]][0],
3828 GET_MODE (reg), byte, mode) >= 0)))))
3830 if (type == OP_OUT)
3831 type = OP_INOUT;
3832 loc = &SUBREG_REG (*loc);
3833 mode = GET_MODE (*loc);
3836 old = *loc;
3837 if (get_reload_reg (type, mode, old, goal_alt[i],
3838 loc != curr_id->operand_loc[i], "", &new_reg)
3839 && type != OP_OUT)
3841 push_to_sequence (before);
3842 lra_emit_move (new_reg, old);
3843 before = get_insns ();
3844 end_sequence ();
3846 *loc = new_reg;
3847 if (type != OP_IN
3848 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3850 start_sequence ();
3851 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3852 emit_insn (after);
3853 after = get_insns ();
3854 end_sequence ();
3855 *loc = new_reg;
3857 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3858 if (goal_alt_dont_inherit_ops[j] == i)
3860 lra_set_regno_unique_value (REGNO (new_reg));
3861 break;
3863 lra_update_dup (curr_id, i);
3865 else if (curr_static_id->operand[i].type == OP_IN
3866 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3867 == OP_OUT))
3869 /* generate reloads for input and matched outputs. */
3870 match_inputs[0] = i;
3871 match_inputs[1] = -1;
3872 match_reload (goal_alt_matched[i][0], match_inputs,
3873 goal_alt[i], &before, &after,
3874 curr_static_id->operand_alternative
3875 [goal_alt_number * n_operands + goal_alt_matched[i][0]]
3876 .earlyclobber);
3878 else if (curr_static_id->operand[i].type == OP_OUT
3879 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3880 == OP_IN))
3881 /* Generate reloads for output and matched inputs. */
3882 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after,
3883 curr_static_id->operand_alternative
3884 [goal_alt_number * n_operands + i].earlyclobber);
3885 else if (curr_static_id->operand[i].type == OP_IN
3886 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3887 == OP_IN))
3889 /* Generate reloads for matched inputs. */
3890 match_inputs[0] = i;
3891 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3892 match_inputs[j + 1] = k;
3893 match_inputs[j + 1] = -1;
3894 match_reload (-1, match_inputs, goal_alt[i], &before, &after, false);
3896 else
3897 /* We must generate code in any case when function
3898 process_alt_operands decides that it is possible. */
3899 gcc_unreachable ();
3900 if (optional_p)
3902 lra_assert (REG_P (op));
3903 regno = REGNO (op);
3904 op = *curr_id->operand_loc[i]; /* Substitution. */
3905 if (GET_CODE (op) == SUBREG)
3906 op = SUBREG_REG (op);
3907 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3908 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3909 lra_reg_info[REGNO (op)].restore_regno = regno;
3910 if (lra_dump_file != NULL)
3911 fprintf (lra_dump_file,
3912 " Making reload reg %d for reg %d optional\n",
3913 REGNO (op), regno);
3916 if (before != NULL_RTX || after != NULL_RTX
3917 || max_regno_before != max_reg_num ())
3918 change_p = true;
3919 if (change_p)
3921 lra_update_operator_dups (curr_id);
3922 /* Something changes -- process the insn. */
3923 lra_update_insn_regno_info (curr_insn);
3925 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3926 return change_p;
3929 /* Return true if INSN satisfies all constraints. In other words, no
3930 reload insns are needed. */
3931 bool
3932 lra_constrain_insn (rtx_insn *insn)
3934 int saved_new_regno_start = new_regno_start;
3935 int saved_new_insn_uid_start = new_insn_uid_start;
3936 bool change_p;
3938 curr_insn = insn;
3939 curr_id = lra_get_insn_recog_data (curr_insn);
3940 curr_static_id = curr_id->insn_static_data;
3941 new_insn_uid_start = get_max_uid ();
3942 new_regno_start = max_reg_num ();
3943 change_p = curr_insn_transform (true);
3944 new_regno_start = saved_new_regno_start;
3945 new_insn_uid_start = saved_new_insn_uid_start;
3946 return ! change_p;
3949 /* Return true if X is in LIST. */
3950 static bool
3951 in_list_p (rtx x, rtx list)
3953 for (; list != NULL_RTX; list = XEXP (list, 1))
3954 if (XEXP (list, 0) == x)
3955 return true;
3956 return false;
3959 /* Return true if X contains an allocatable hard register (if
3960 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3961 static bool
3962 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3964 int i, j;
3965 const char *fmt;
3966 enum rtx_code code;
3968 code = GET_CODE (x);
3969 if (REG_P (x))
3971 int regno = REGNO (x);
3972 HARD_REG_SET alloc_regs;
3974 if (hard_reg_p)
3976 if (regno >= FIRST_PSEUDO_REGISTER)
3977 regno = lra_get_regno_hard_regno (regno);
3978 if (regno < 0)
3979 return false;
3980 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3981 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3983 else
3985 if (regno < FIRST_PSEUDO_REGISTER)
3986 return false;
3987 if (! spilled_p)
3988 return true;
3989 return lra_get_regno_hard_regno (regno) < 0;
3992 fmt = GET_RTX_FORMAT (code);
3993 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3995 if (fmt[i] == 'e')
3997 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
3998 return true;
4000 else if (fmt[i] == 'E')
4002 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4003 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4004 return true;
4007 return false;
4010 /* Process all regs in location *LOC and change them on equivalent
4011 substitution. Return true if any change was done. */
4012 static bool
4013 loc_equivalence_change_p (rtx *loc)
4015 rtx subst, reg, x = *loc;
4016 bool result = false;
4017 enum rtx_code code = GET_CODE (x);
4018 const char *fmt;
4019 int i, j;
4021 if (code == SUBREG)
4023 reg = SUBREG_REG (x);
4024 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4025 && GET_MODE (subst) == VOIDmode)
4027 /* We cannot reload debug location. Simplify subreg here
4028 while we know the inner mode. */
4029 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4030 GET_MODE (reg), SUBREG_BYTE (x));
4031 return true;
4034 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4036 *loc = subst;
4037 return true;
4040 /* Scan all the operand sub-expressions. */
4041 fmt = GET_RTX_FORMAT (code);
4042 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4044 if (fmt[i] == 'e')
4045 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4046 else if (fmt[i] == 'E')
4047 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4048 result
4049 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4051 return result;
4054 /* Similar to loc_equivalence_change_p, but for use as
4055 simplify_replace_fn_rtx callback. DATA is insn for which the
4056 elimination is done. If it null we don't do the elimination. */
4057 static rtx
4058 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4060 if (!REG_P (loc))
4061 return NULL_RTX;
4063 rtx subst = (data == NULL
4064 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4065 if (subst != loc)
4066 return subst;
4068 return NULL_RTX;
4071 /* Maximum number of generated reload insns per an insn. It is for
4072 preventing this pass cycling in a bug case. */
4073 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4075 /* The current iteration number of this LRA pass. */
4076 int lra_constraint_iter;
4078 /* True if we substituted equiv which needs checking register
4079 allocation correctness because the equivalent value contains
4080 allocatable hard registers or when we restore multi-register
4081 pseudo. */
4082 bool lra_risky_transformations_p;
4084 /* Return true if REGNO is referenced in more than one block. */
4085 static bool
4086 multi_block_pseudo_p (int regno)
4088 basic_block bb = NULL;
4089 unsigned int uid;
4090 bitmap_iterator bi;
4092 if (regno < FIRST_PSEUDO_REGISTER)
4093 return false;
4095 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4096 if (bb == NULL)
4097 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4098 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4099 return true;
4100 return false;
4103 /* Return true if LIST contains a deleted insn. */
4104 static bool
4105 contains_deleted_insn_p (rtx_insn_list *list)
4107 for (; list != NULL_RTX; list = list->next ())
4108 if (NOTE_P (list->insn ())
4109 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4110 return true;
4111 return false;
4114 /* Return true if X contains a pseudo dying in INSN. */
4115 static bool
4116 dead_pseudo_p (rtx x, rtx_insn *insn)
4118 int i, j;
4119 const char *fmt;
4120 enum rtx_code code;
4122 if (REG_P (x))
4123 return (insn != NULL_RTX
4124 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4125 code = GET_CODE (x);
4126 fmt = GET_RTX_FORMAT (code);
4127 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4129 if (fmt[i] == 'e')
4131 if (dead_pseudo_p (XEXP (x, i), insn))
4132 return true;
4134 else if (fmt[i] == 'E')
4136 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4137 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4138 return true;
4141 return false;
4144 /* Return true if INSN contains a dying pseudo in INSN right hand
4145 side. */
4146 static bool
4147 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4149 rtx set = single_set (insn);
4151 gcc_assert (set != NULL);
4152 return dead_pseudo_p (SET_SRC (set), insn);
4155 /* Return true if any init insn of REGNO contains a dying pseudo in
4156 insn right hand side. */
4157 static bool
4158 init_insn_rhs_dead_pseudo_p (int regno)
4160 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4162 if (insns == NULL)
4163 return false;
4164 for (; insns != NULL_RTX; insns = insns->next ())
4165 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4166 return true;
4167 return false;
4170 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4171 reverse only if we have one init insn with given REGNO as a
4172 source. */
4173 static bool
4174 reverse_equiv_p (int regno)
4176 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4177 rtx set;
4179 if (insns == NULL)
4180 return false;
4181 if (! INSN_P (insns->insn ())
4182 || insns->next () != NULL)
4183 return false;
4184 if ((set = single_set (insns->insn ())) == NULL_RTX)
4185 return false;
4186 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4189 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4190 call this function only for non-reverse equivalence. */
4191 static bool
4192 contains_reloaded_insn_p (int regno)
4194 rtx set;
4195 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4197 for (; list != NULL; list = list->next ())
4198 if ((set = single_set (list->insn ())) == NULL_RTX
4199 || ! REG_P (SET_DEST (set))
4200 || (int) REGNO (SET_DEST (set)) != regno)
4201 return true;
4202 return false;
4205 /* Entry function of LRA constraint pass. Return true if the
4206 constraint pass did change the code. */
4207 bool
4208 lra_constraints (bool first_p)
4210 bool changed_p;
4211 int i, hard_regno, new_insns_num;
4212 unsigned int min_len, new_min_len, uid;
4213 rtx set, x, reg, dest_reg;
4214 basic_block last_bb;
4215 bitmap_head equiv_insn_bitmap;
4216 bitmap_iterator bi;
4218 lra_constraint_iter++;
4219 if (lra_dump_file != NULL)
4220 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4221 lra_constraint_iter);
4222 changed_p = false;
4223 if (pic_offset_table_rtx
4224 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4225 lra_risky_transformations_p = true;
4226 else
4227 lra_risky_transformations_p = false;
4228 new_insn_uid_start = get_max_uid ();
4229 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4230 /* Mark used hard regs for target stack size calulations. */
4231 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4232 if (lra_reg_info[i].nrefs != 0
4233 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4235 int j, nregs;
4237 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4238 for (j = 0; j < nregs; j++)
4239 df_set_regs_ever_live (hard_regno + j, true);
4241 /* Do elimination before the equivalence processing as we can spill
4242 some pseudos during elimination. */
4243 lra_eliminate (false, first_p);
4244 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4245 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4246 if (lra_reg_info[i].nrefs != 0)
4248 ira_reg_equiv[i].profitable_p = true;
4249 reg = regno_reg_rtx[i];
4250 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4252 bool pseudo_p = contains_reg_p (x, false, false);
4254 /* After RTL transformation, we can not guarantee that
4255 pseudo in the substitution was not reloaded which might
4256 make equivalence invalid. For example, in reverse
4257 equiv of p0
4259 p0 <- ...
4261 equiv_mem <- p0
4263 the memory address register was reloaded before the 2nd
4264 insn. */
4265 if ((! first_p && pseudo_p)
4266 /* We don't use DF for compilation speed sake. So it
4267 is problematic to update live info when we use an
4268 equivalence containing pseudos in more than one
4269 BB. */
4270 || (pseudo_p && multi_block_pseudo_p (i))
4271 /* If an init insn was deleted for some reason, cancel
4272 the equiv. We could update the equiv insns after
4273 transformations including an equiv insn deletion
4274 but it is not worthy as such cases are extremely
4275 rare. */
4276 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4277 /* If it is not a reverse equivalence, we check that a
4278 pseudo in rhs of the init insn is not dying in the
4279 insn. Otherwise, the live info at the beginning of
4280 the corresponding BB might be wrong after we
4281 removed the insn. When the equiv can be a
4282 constant, the right hand side of the init insn can
4283 be a pseudo. */
4284 || (! reverse_equiv_p (i)
4285 && (init_insn_rhs_dead_pseudo_p (i)
4286 /* If we reloaded the pseudo in an equivalence
4287 init insn, we can not remove the equiv init
4288 insns and the init insns might write into
4289 const memory in this case. */
4290 || contains_reloaded_insn_p (i)))
4291 /* Prevent access beyond equivalent memory for
4292 paradoxical subregs. */
4293 || (MEM_P (x)
4294 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4295 > GET_MODE_SIZE (GET_MODE (x))))
4296 || (pic_offset_table_rtx
4297 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4298 && (targetm.preferred_reload_class
4299 (x, lra_get_allocno_class (i)) == NO_REGS))
4300 || contains_symbol_ref_p (x))))
4301 ira_reg_equiv[i].defined_p = false;
4302 if (contains_reg_p (x, false, true))
4303 ira_reg_equiv[i].profitable_p = false;
4304 if (get_equiv (reg) != reg)
4305 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4308 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4309 update_equiv (i);
4310 /* We should add all insns containing pseudos which should be
4311 substituted by their equivalences. */
4312 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4313 lra_push_insn_by_uid (uid);
4314 min_len = lra_insn_stack_length ();
4315 new_insns_num = 0;
4316 last_bb = NULL;
4317 changed_p = false;
4318 while ((new_min_len = lra_insn_stack_length ()) != 0)
4320 curr_insn = lra_pop_insn ();
4321 --new_min_len;
4322 curr_bb = BLOCK_FOR_INSN (curr_insn);
4323 if (curr_bb != last_bb)
4325 last_bb = curr_bb;
4326 bb_reload_num = lra_curr_reload_num;
4328 if (min_len > new_min_len)
4330 min_len = new_min_len;
4331 new_insns_num = 0;
4333 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4334 internal_error
4335 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4336 MAX_RELOAD_INSNS_NUMBER);
4337 new_insns_num++;
4338 if (DEBUG_INSN_P (curr_insn))
4340 /* We need to check equivalence in debug insn and change
4341 pseudo to the equivalent value if necessary. */
4342 curr_id = lra_get_insn_recog_data (curr_insn);
4343 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4345 rtx old = *curr_id->operand_loc[0];
4346 *curr_id->operand_loc[0]
4347 = simplify_replace_fn_rtx (old, NULL_RTX,
4348 loc_equivalence_callback, curr_insn);
4349 if (old != *curr_id->operand_loc[0])
4351 lra_update_insn_regno_info (curr_insn);
4352 changed_p = true;
4356 else if (INSN_P (curr_insn))
4358 if ((set = single_set (curr_insn)) != NULL_RTX)
4360 dest_reg = SET_DEST (set);
4361 /* The equivalence pseudo could be set up as SUBREG in a
4362 case when it is a call restore insn in a mode
4363 different from the pseudo mode. */
4364 if (GET_CODE (dest_reg) == SUBREG)
4365 dest_reg = SUBREG_REG (dest_reg);
4366 if ((REG_P (dest_reg)
4367 && (x = get_equiv (dest_reg)) != dest_reg
4368 /* Remove insns which set up a pseudo whose value
4369 can not be changed. Such insns might be not in
4370 init_insns because we don't update equiv data
4371 during insn transformations.
4373 As an example, let suppose that a pseudo got
4374 hard register and on the 1st pass was not
4375 changed to equivalent constant. We generate an
4376 additional insn setting up the pseudo because of
4377 secondary memory movement. Then the pseudo is
4378 spilled and we use the equiv constant. In this
4379 case we should remove the additional insn and
4380 this insn is not init_insns list. */
4381 && (! MEM_P (x) || MEM_READONLY_P (x)
4382 /* Check that this is actually an insn setting
4383 up the equivalence. */
4384 || in_list_p (curr_insn,
4385 ira_reg_equiv
4386 [REGNO (dest_reg)].init_insns)))
4387 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4388 && in_list_p (curr_insn,
4389 ira_reg_equiv
4390 [REGNO (SET_SRC (set))].init_insns)))
4392 /* This is equiv init insn of pseudo which did not get a
4393 hard register -- remove the insn. */
4394 if (lra_dump_file != NULL)
4396 fprintf (lra_dump_file,
4397 " Removing equiv init insn %i (freq=%d)\n",
4398 INSN_UID (curr_insn),
4399 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4400 dump_insn_slim (lra_dump_file, curr_insn);
4402 if (contains_reg_p (x, true, false))
4403 lra_risky_transformations_p = true;
4404 lra_set_insn_deleted (curr_insn);
4405 continue;
4408 curr_id = lra_get_insn_recog_data (curr_insn);
4409 curr_static_id = curr_id->insn_static_data;
4410 init_curr_insn_input_reloads ();
4411 init_curr_operand_mode ();
4412 if (curr_insn_transform (false))
4413 changed_p = true;
4414 /* Check non-transformed insns too for equiv change as USE
4415 or CLOBBER don't need reloads but can contain pseudos
4416 being changed on their equivalences. */
4417 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4418 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4420 lra_update_insn_regno_info (curr_insn);
4421 changed_p = true;
4425 bitmap_clear (&equiv_insn_bitmap);
4426 /* If we used a new hard regno, changed_p should be true because the
4427 hard reg is assigned to a new pseudo. */
4428 if (flag_checking && !changed_p)
4430 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4431 if (lra_reg_info[i].nrefs != 0
4432 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4434 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4436 for (j = 0; j < nregs; j++)
4437 lra_assert (df_regs_ever_live_p (hard_regno + j));
4440 return changed_p;
4443 /* Initiate the LRA constraint pass. It is done once per
4444 function. */
4445 void
4446 lra_constraints_init (void)
4450 /* Finalize the LRA constraint pass. It is done once per
4451 function. */
4452 void
4453 lra_constraints_finish (void)
4459 /* This page contains code to do inheritance/split
4460 transformations. */
4462 /* Number of reloads passed so far in current EBB. */
4463 static int reloads_num;
4465 /* Number of calls passed so far in current EBB. */
4466 static int calls_num;
4468 /* Current reload pseudo check for validity of elements in
4469 USAGE_INSNS. */
4470 static int curr_usage_insns_check;
4472 /* Info about last usage of registers in EBB to do inheritance/split
4473 transformation. Inheritance transformation is done from a spilled
4474 pseudo and split transformations from a hard register or a pseudo
4475 assigned to a hard register. */
4476 struct usage_insns
4478 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4479 value INSNS is valid. The insns is chain of optional debug insns
4480 and a finishing non-debug insn using the corresponding reg. The
4481 value is also used to mark the registers which are set up in the
4482 current insn. The negated insn uid is used for this. */
4483 int check;
4484 /* Value of global reloads_num at the last insn in INSNS. */
4485 int reloads_num;
4486 /* Value of global reloads_nums at the last insn in INSNS. */
4487 int calls_num;
4488 /* It can be true only for splitting. And it means that the restore
4489 insn should be put after insn given by the following member. */
4490 bool after_p;
4491 /* Next insns in the current EBB which use the original reg and the
4492 original reg value is not changed between the current insn and
4493 the next insns. In order words, e.g. for inheritance, if we need
4494 to use the original reg value again in the next insns we can try
4495 to use the value in a hard register from a reload insn of the
4496 current insn. */
4497 rtx insns;
4500 /* Map: regno -> corresponding pseudo usage insns. */
4501 static struct usage_insns *usage_insns;
4503 static void
4504 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4506 usage_insns[regno].check = curr_usage_insns_check;
4507 usage_insns[regno].insns = insn;
4508 usage_insns[regno].reloads_num = reloads_num;
4509 usage_insns[regno].calls_num = calls_num;
4510 usage_insns[regno].after_p = after_p;
4513 /* The function is used to form list REGNO usages which consists of
4514 optional debug insns finished by a non-debug insn using REGNO.
4515 RELOADS_NUM is current number of reload insns processed so far. */
4516 static void
4517 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
4519 rtx next_usage_insns;
4521 if (usage_insns[regno].check == curr_usage_insns_check
4522 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4523 && DEBUG_INSN_P (insn))
4525 /* Check that we did not add the debug insn yet. */
4526 if (next_usage_insns != insn
4527 && (GET_CODE (next_usage_insns) != INSN_LIST
4528 || XEXP (next_usage_insns, 0) != insn))
4529 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4530 next_usage_insns);
4532 else if (NONDEBUG_INSN_P (insn))
4533 setup_next_usage_insn (regno, insn, reloads_num, false);
4534 else
4535 usage_insns[regno].check = 0;
4538 /* Return first non-debug insn in list USAGE_INSNS. */
4539 static rtx_insn *
4540 skip_usage_debug_insns (rtx usage_insns)
4542 rtx insn;
4544 /* Skip debug insns. */
4545 for (insn = usage_insns;
4546 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4547 insn = XEXP (insn, 1))
4549 return safe_as_a <rtx_insn *> (insn);
4552 /* Return true if we need secondary memory moves for insn in
4553 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4554 into the insn. */
4555 static bool
4556 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4557 rtx usage_insns ATTRIBUTE_UNUSED)
4559 #ifndef SECONDARY_MEMORY_NEEDED
4560 return false;
4561 #else
4562 rtx_insn *insn;
4563 rtx set, dest;
4564 enum reg_class cl;
4566 if (inher_cl == ALL_REGS
4567 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4568 return false;
4569 lra_assert (INSN_P (insn));
4570 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4571 return false;
4572 dest = SET_DEST (set);
4573 if (! REG_P (dest))
4574 return false;
4575 lra_assert (inher_cl != NO_REGS);
4576 cl = get_reg_class (REGNO (dest));
4577 return (cl != NO_REGS && cl != ALL_REGS
4578 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4579 #endif
4582 /* Registers involved in inheritance/split in the current EBB
4583 (inheritance/split pseudos and original registers). */
4584 static bitmap_head check_only_regs;
4586 /* Do inheritance transformations for insn INSN, which defines (if
4587 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4588 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4589 form as the "insns" field of usage_insns. Return true if we
4590 succeed in such transformation.
4592 The transformations look like:
4594 p <- ... i <- ...
4595 ... p <- i (new insn)
4596 ... =>
4597 <- ... p ... <- ... i ...
4599 ... i <- p (new insn)
4600 <- ... p ... <- ... i ...
4601 ... =>
4602 <- ... p ... <- ... i ...
4603 where p is a spilled original pseudo and i is a new inheritance pseudo.
4606 The inheritance pseudo has the smallest class of two classes CL and
4607 class of ORIGINAL REGNO. */
4608 static bool
4609 inherit_reload_reg (bool def_p, int original_regno,
4610 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4612 if (optimize_function_for_size_p (cfun))
4613 return false;
4615 enum reg_class rclass = lra_get_allocno_class (original_regno);
4616 rtx original_reg = regno_reg_rtx[original_regno];
4617 rtx new_reg, usage_insn;
4618 rtx_insn *new_insns;
4620 lra_assert (! usage_insns[original_regno].after_p);
4621 if (lra_dump_file != NULL)
4622 fprintf (lra_dump_file,
4623 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4624 if (! ira_reg_classes_intersect_p[cl][rclass])
4626 if (lra_dump_file != NULL)
4628 fprintf (lra_dump_file,
4629 " Rejecting inheritance for %d "
4630 "because of disjoint classes %s and %s\n",
4631 original_regno, reg_class_names[cl],
4632 reg_class_names[rclass]);
4633 fprintf (lra_dump_file,
4634 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4636 return false;
4638 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4639 /* We don't use a subset of two classes because it can be
4640 NO_REGS. This transformation is still profitable in most
4641 cases even if the classes are not intersected as register
4642 move is probably cheaper than a memory load. */
4643 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4645 if (lra_dump_file != NULL)
4646 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4647 reg_class_names[cl], reg_class_names[rclass]);
4649 rclass = cl;
4651 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4653 /* Reject inheritance resulting in secondary memory moves.
4654 Otherwise, there is a danger in LRA cycling. Also such
4655 transformation will be unprofitable. */
4656 if (lra_dump_file != NULL)
4658 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
4659 rtx set = single_set (insn);
4661 lra_assert (set != NULL_RTX);
4663 rtx dest = SET_DEST (set);
4665 lra_assert (REG_P (dest));
4666 fprintf (lra_dump_file,
4667 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4668 "as secondary mem is needed\n",
4669 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4670 original_regno, reg_class_names[rclass]);
4671 fprintf (lra_dump_file,
4672 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4674 return false;
4676 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4677 rclass, "inheritance");
4678 start_sequence ();
4679 if (def_p)
4680 lra_emit_move (original_reg, new_reg);
4681 else
4682 lra_emit_move (new_reg, original_reg);
4683 new_insns = get_insns ();
4684 end_sequence ();
4685 if (NEXT_INSN (new_insns) != NULL_RTX)
4687 if (lra_dump_file != NULL)
4689 fprintf (lra_dump_file,
4690 " Rejecting inheritance %d->%d "
4691 "as it results in 2 or more insns:\n",
4692 original_regno, REGNO (new_reg));
4693 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
4694 fprintf (lra_dump_file,
4695 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4697 return false;
4699 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
4700 lra_update_insn_regno_info (insn);
4701 if (! def_p)
4702 /* We now have a new usage insn for original regno. */
4703 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4704 if (lra_dump_file != NULL)
4705 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4706 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4707 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4708 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4709 bitmap_set_bit (&check_only_regs, original_regno);
4710 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4711 if (def_p)
4712 lra_process_new_insns (insn, NULL, new_insns,
4713 "Add original<-inheritance");
4714 else
4715 lra_process_new_insns (insn, new_insns, NULL,
4716 "Add inheritance<-original");
4717 while (next_usage_insns != NULL_RTX)
4719 if (GET_CODE (next_usage_insns) != INSN_LIST)
4721 usage_insn = next_usage_insns;
4722 lra_assert (NONDEBUG_INSN_P (usage_insn));
4723 next_usage_insns = NULL;
4725 else
4727 usage_insn = XEXP (next_usage_insns, 0);
4728 lra_assert (DEBUG_INSN_P (usage_insn));
4729 next_usage_insns = XEXP (next_usage_insns, 1);
4731 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
4732 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4733 if (lra_dump_file != NULL)
4735 fprintf (lra_dump_file,
4736 " Inheritance reuse change %d->%d (bb%d):\n",
4737 original_regno, REGNO (new_reg),
4738 BLOCK_FOR_INSN (usage_insn)->index);
4739 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
4742 if (lra_dump_file != NULL)
4743 fprintf (lra_dump_file,
4744 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4745 return true;
4748 /* Return true if we need a caller save/restore for pseudo REGNO which
4749 was assigned to a hard register. */
4750 static inline bool
4751 need_for_call_save_p (int regno)
4753 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4754 return (usage_insns[regno].calls_num < calls_num
4755 && (overlaps_hard_reg_set_p
4756 ((flag_ipa_ra &&
4757 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
4758 ? lra_reg_info[regno].actual_call_used_reg_set
4759 : call_used_reg_set,
4760 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4761 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4762 PSEUDO_REGNO_MODE (regno))));
4765 /* Global registers occurring in the current EBB. */
4766 static bitmap_head ebb_global_regs;
4768 /* Return true if we need a split for hard register REGNO or pseudo
4769 REGNO which was assigned to a hard register.
4770 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4771 used for reloads since the EBB end. It is an approximation of the
4772 used hard registers in the split range. The exact value would
4773 require expensive calculations. If we were aggressive with
4774 splitting because of the approximation, the split pseudo will save
4775 the same hard register assignment and will be removed in the undo
4776 pass. We still need the approximation because too aggressive
4777 splitting would result in too inaccurate cost calculation in the
4778 assignment pass because of too many generated moves which will be
4779 probably removed in the undo pass. */
4780 static inline bool
4781 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4783 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4785 lra_assert (hard_regno >= 0);
4786 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4787 /* Don't split eliminable hard registers, otherwise we can
4788 split hard registers like hard frame pointer, which
4789 lives on BB start/end according to DF-infrastructure,
4790 when there is a pseudo assigned to the register and
4791 living in the same BB. */
4792 && (regno >= FIRST_PSEUDO_REGISTER
4793 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4794 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4795 /* Don't split call clobbered hard regs living through
4796 calls, otherwise we might have a check problem in the
4797 assign sub-pass as in the most cases (exception is a
4798 situation when lra_risky_transformations_p value is
4799 true) the assign pass assumes that all pseudos living
4800 through calls are assigned to call saved hard regs. */
4801 && (regno >= FIRST_PSEUDO_REGISTER
4802 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4803 || usage_insns[regno].calls_num == calls_num)
4804 /* We need at least 2 reloads to make pseudo splitting
4805 profitable. We should provide hard regno splitting in
4806 any case to solve 1st insn scheduling problem when
4807 moving hard register definition up might result in
4808 impossibility to find hard register for reload pseudo of
4809 small register class. */
4810 && (usage_insns[regno].reloads_num
4811 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4812 && (regno < FIRST_PSEUDO_REGISTER
4813 /* For short living pseudos, spilling + inheritance can
4814 be considered a substitution for splitting.
4815 Therefore we do not splitting for local pseudos. It
4816 decreases also aggressiveness of splitting. The
4817 minimal number of references is chosen taking into
4818 account that for 2 references splitting has no sense
4819 as we can just spill the pseudo. */
4820 || (regno >= FIRST_PSEUDO_REGISTER
4821 && lra_reg_info[regno].nrefs > 3
4822 && bitmap_bit_p (&ebb_global_regs, regno))))
4823 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4826 /* Return class for the split pseudo created from original pseudo with
4827 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4828 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4829 results in no secondary memory movements. */
4830 static enum reg_class
4831 choose_split_class (enum reg_class allocno_class,
4832 int hard_regno ATTRIBUTE_UNUSED,
4833 machine_mode mode ATTRIBUTE_UNUSED)
4835 #ifndef SECONDARY_MEMORY_NEEDED
4836 return allocno_class;
4837 #else
4838 int i;
4839 enum reg_class cl, best_cl = NO_REGS;
4840 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4841 = REGNO_REG_CLASS (hard_regno);
4843 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4844 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4845 return allocno_class;
4846 for (i = 0;
4847 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4848 i++)
4849 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4850 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4851 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4852 && (best_cl == NO_REGS
4853 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4854 best_cl = cl;
4855 return best_cl;
4856 #endif
4859 /* Do split transformations for insn INSN, which defines or uses
4860 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4861 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4862 "insns" field of usage_insns.
4864 The transformations look like:
4866 p <- ... p <- ...
4867 ... s <- p (new insn -- save)
4868 ... =>
4869 ... p <- s (new insn -- restore)
4870 <- ... p ... <- ... p ...
4872 <- ... p ... <- ... p ...
4873 ... s <- p (new insn -- save)
4874 ... =>
4875 ... p <- s (new insn -- restore)
4876 <- ... p ... <- ... p ...
4878 where p is an original pseudo got a hard register or a hard
4879 register and s is a new split pseudo. The save is put before INSN
4880 if BEFORE_P is true. Return true if we succeed in such
4881 transformation. */
4882 static bool
4883 split_reg (bool before_p, int original_regno, rtx_insn *insn,
4884 rtx next_usage_insns)
4886 enum reg_class rclass;
4887 rtx original_reg;
4888 int hard_regno, nregs;
4889 rtx new_reg, usage_insn;
4890 rtx_insn *restore, *save;
4891 bool after_p;
4892 bool call_save_p;
4894 if (original_regno < FIRST_PSEUDO_REGISTER)
4896 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4897 hard_regno = original_regno;
4898 call_save_p = false;
4899 nregs = 1;
4901 else
4903 hard_regno = reg_renumber[original_regno];
4904 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4905 rclass = lra_get_allocno_class (original_regno);
4906 original_reg = regno_reg_rtx[original_regno];
4907 call_save_p = need_for_call_save_p (original_regno);
4909 original_reg = regno_reg_rtx[original_regno];
4910 lra_assert (hard_regno >= 0);
4911 if (lra_dump_file != NULL)
4912 fprintf (lra_dump_file,
4913 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4914 if (call_save_p)
4916 machine_mode mode = GET_MODE (original_reg);
4918 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4919 hard_regno_nregs[hard_regno][mode],
4920 mode);
4921 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4923 else
4925 rclass = choose_split_class (rclass, hard_regno,
4926 GET_MODE (original_reg));
4927 if (rclass == NO_REGS)
4929 if (lra_dump_file != NULL)
4931 fprintf (lra_dump_file,
4932 " Rejecting split of %d(%s): "
4933 "no good reg class for %d(%s)\n",
4934 original_regno,
4935 reg_class_names[lra_get_allocno_class (original_regno)],
4936 hard_regno,
4937 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4938 fprintf
4939 (lra_dump_file,
4940 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4942 return false;
4944 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4945 rclass, "split");
4946 reg_renumber[REGNO (new_reg)] = hard_regno;
4948 save = emit_spill_move (true, new_reg, original_reg);
4949 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
4951 if (lra_dump_file != NULL)
4953 fprintf
4954 (lra_dump_file,
4955 " Rejecting split %d->%d resulting in > 2 save insns:\n",
4956 original_regno, REGNO (new_reg));
4957 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
4958 fprintf (lra_dump_file,
4959 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4961 return false;
4963 restore = emit_spill_move (false, new_reg, original_reg);
4964 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
4966 if (lra_dump_file != NULL)
4968 fprintf (lra_dump_file,
4969 " Rejecting split %d->%d "
4970 "resulting in > 2 restore insns:\n",
4971 original_regno, REGNO (new_reg));
4972 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
4973 fprintf (lra_dump_file,
4974 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4976 return false;
4978 after_p = usage_insns[original_regno].after_p;
4979 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4980 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4981 bitmap_set_bit (&check_only_regs, original_regno);
4982 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
4983 for (;;)
4985 if (GET_CODE (next_usage_insns) != INSN_LIST)
4987 usage_insn = next_usage_insns;
4988 break;
4990 usage_insn = XEXP (next_usage_insns, 0);
4991 lra_assert (DEBUG_INSN_P (usage_insn));
4992 next_usage_insns = XEXP (next_usage_insns, 1);
4993 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
4994 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4995 if (lra_dump_file != NULL)
4997 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
4998 original_regno, REGNO (new_reg));
4999 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5002 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5003 lra_assert (usage_insn != insn || (after_p && before_p));
5004 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5005 after_p ? NULL : restore,
5006 after_p ? restore : NULL,
5007 call_save_p
5008 ? "Add reg<-save" : "Add reg<-split");
5009 lra_process_new_insns (insn, before_p ? save : NULL,
5010 before_p ? NULL : save,
5011 call_save_p
5012 ? "Add save<-reg" : "Add split<-reg");
5013 if (nregs > 1)
5014 /* If we are trying to split multi-register. We should check
5015 conflicts on the next assignment sub-pass. IRA can allocate on
5016 sub-register levels, LRA do this on pseudos level right now and
5017 this discrepancy may create allocation conflicts after
5018 splitting. */
5019 lra_risky_transformations_p = true;
5020 if (lra_dump_file != NULL)
5021 fprintf (lra_dump_file,
5022 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5023 return true;
5026 /* Recognize that we need a split transformation for insn INSN, which
5027 defines or uses REGNO in its insn biggest MODE (we use it only if
5028 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5029 hard registers which might be used for reloads since the EBB end.
5030 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5031 uid before starting INSN processing. Return true if we succeed in
5032 such transformation. */
5033 static bool
5034 split_if_necessary (int regno, machine_mode mode,
5035 HARD_REG_SET potential_reload_hard_regs,
5036 bool before_p, rtx_insn *insn, int max_uid)
5038 bool res = false;
5039 int i, nregs = 1;
5040 rtx next_usage_insns;
5042 if (regno < FIRST_PSEUDO_REGISTER)
5043 nregs = hard_regno_nregs[regno][mode];
5044 for (i = 0; i < nregs; i++)
5045 if (usage_insns[regno + i].check == curr_usage_insns_check
5046 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5047 /* To avoid processing the register twice or more. */
5048 && ((GET_CODE (next_usage_insns) != INSN_LIST
5049 && INSN_UID (next_usage_insns) < max_uid)
5050 || (GET_CODE (next_usage_insns) == INSN_LIST
5051 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5052 && need_for_split_p (potential_reload_hard_regs, regno + i)
5053 && split_reg (before_p, regno + i, insn, next_usage_insns))
5054 res = true;
5055 return res;
5058 /* Check only registers living at the current program point in the
5059 current EBB. */
5060 static bitmap_head live_regs;
5062 /* Update live info in EBB given by its HEAD and TAIL insns after
5063 inheritance/split transformation. The function removes dead moves
5064 too. */
5065 static void
5066 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5068 unsigned int j;
5069 int i, regno;
5070 bool live_p;
5071 rtx_insn *prev_insn;
5072 rtx set;
5073 bool remove_p;
5074 basic_block last_bb, prev_bb, curr_bb;
5075 bitmap_iterator bi;
5076 struct lra_insn_reg *reg;
5077 edge e;
5078 edge_iterator ei;
5080 last_bb = BLOCK_FOR_INSN (tail);
5081 prev_bb = NULL;
5082 for (curr_insn = tail;
5083 curr_insn != PREV_INSN (head);
5084 curr_insn = prev_insn)
5086 prev_insn = PREV_INSN (curr_insn);
5087 /* We need to process empty blocks too. They contain
5088 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5089 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5090 continue;
5091 curr_bb = BLOCK_FOR_INSN (curr_insn);
5092 if (curr_bb != prev_bb)
5094 if (prev_bb != NULL)
5096 /* Update df_get_live_in (prev_bb): */
5097 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5098 if (bitmap_bit_p (&live_regs, j))
5099 bitmap_set_bit (df_get_live_in (prev_bb), j);
5100 else
5101 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5103 if (curr_bb != last_bb)
5105 /* Update df_get_live_out (curr_bb): */
5106 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5108 live_p = bitmap_bit_p (&live_regs, j);
5109 if (! live_p)
5110 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5111 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5113 live_p = true;
5114 break;
5116 if (live_p)
5117 bitmap_set_bit (df_get_live_out (curr_bb), j);
5118 else
5119 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5122 prev_bb = curr_bb;
5123 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5125 if (! NONDEBUG_INSN_P (curr_insn))
5126 continue;
5127 curr_id = lra_get_insn_recog_data (curr_insn);
5128 curr_static_id = curr_id->insn_static_data;
5129 remove_p = false;
5130 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
5131 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5132 && bitmap_bit_p (&check_only_regs, regno)
5133 && ! bitmap_bit_p (&live_regs, regno))
5134 remove_p = true;
5135 /* See which defined values die here. */
5136 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5137 if (reg->type == OP_OUT && ! reg->subreg_p)
5138 bitmap_clear_bit (&live_regs, reg->regno);
5139 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5140 if (reg->type == OP_OUT && ! reg->subreg_p)
5141 bitmap_clear_bit (&live_regs, reg->regno);
5142 if (curr_id->arg_hard_regs != NULL)
5143 /* Make clobbered argument hard registers die. */
5144 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5145 if (regno >= FIRST_PSEUDO_REGISTER)
5146 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
5147 /* Mark each used value as live. */
5148 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5149 if (reg->type != OP_OUT
5150 && bitmap_bit_p (&check_only_regs, reg->regno))
5151 bitmap_set_bit (&live_regs, reg->regno);
5152 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5153 if (reg->type != OP_OUT
5154 && bitmap_bit_p (&check_only_regs, reg->regno))
5155 bitmap_set_bit (&live_regs, reg->regno);
5156 if (curr_id->arg_hard_regs != NULL)
5157 /* Make used argument hard registers live. */
5158 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5159 if (regno < FIRST_PSEUDO_REGISTER
5160 && bitmap_bit_p (&check_only_regs, regno))
5161 bitmap_set_bit (&live_regs, regno);
5162 /* It is quite important to remove dead move insns because it
5163 means removing dead store. We don't need to process them for
5164 constraints. */
5165 if (remove_p)
5167 if (lra_dump_file != NULL)
5169 fprintf (lra_dump_file, " Removing dead insn:\n ");
5170 dump_insn_slim (lra_dump_file, curr_insn);
5172 lra_set_insn_deleted (curr_insn);
5177 /* The structure describes info to do an inheritance for the current
5178 insn. We need to collect such info first before doing the
5179 transformations because the transformations change the insn
5180 internal representation. */
5181 struct to_inherit
5183 /* Original regno. */
5184 int regno;
5185 /* Subsequent insns which can inherit original reg value. */
5186 rtx insns;
5189 /* Array containing all info for doing inheritance from the current
5190 insn. */
5191 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5193 /* Number elements in the previous array. */
5194 static int to_inherit_num;
5196 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5197 structure to_inherit. */
5198 static void
5199 add_to_inherit (int regno, rtx insns)
5201 int i;
5203 for (i = 0; i < to_inherit_num; i++)
5204 if (to_inherit[i].regno == regno)
5205 return;
5206 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5207 to_inherit[to_inherit_num].regno = regno;
5208 to_inherit[to_inherit_num++].insns = insns;
5211 /* Return the last non-debug insn in basic block BB, or the block begin
5212 note if none. */
5213 static rtx_insn *
5214 get_last_insertion_point (basic_block bb)
5216 rtx_insn *insn;
5218 FOR_BB_INSNS_REVERSE (bb, insn)
5219 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5220 return insn;
5221 gcc_unreachable ();
5224 /* Set up RES by registers living on edges FROM except the edge (FROM,
5225 TO) or by registers set up in a jump insn in BB FROM. */
5226 static void
5227 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5229 rtx_insn *last;
5230 struct lra_insn_reg *reg;
5231 edge e;
5232 edge_iterator ei;
5234 lra_assert (to != NULL);
5235 bitmap_clear (res);
5236 FOR_EACH_EDGE (e, ei, from->succs)
5237 if (e->dest != to)
5238 bitmap_ior_into (res, df_get_live_in (e->dest));
5239 last = get_last_insertion_point (from);
5240 if (! JUMP_P (last))
5241 return;
5242 curr_id = lra_get_insn_recog_data (last);
5243 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5244 if (reg->type != OP_IN)
5245 bitmap_set_bit (res, reg->regno);
5248 /* Used as a temporary results of some bitmap calculations. */
5249 static bitmap_head temp_bitmap;
5251 /* We split for reloads of small class of hard regs. The following
5252 defines how many hard regs the class should have to be qualified as
5253 small. The code is mostly oriented to x86/x86-64 architecture
5254 where some insns need to use only specific register or pair of
5255 registers and these register can live in RTL explicitly, e.g. for
5256 parameter passing. */
5257 static const int max_small_class_regs_num = 2;
5259 /* Do inheritance/split transformations in EBB starting with HEAD and
5260 finishing on TAIL. We process EBB insns in the reverse order.
5261 Return true if we did any inheritance/split transformation in the
5262 EBB.
5264 We should avoid excessive splitting which results in worse code
5265 because of inaccurate cost calculations for spilling new split
5266 pseudos in such case. To achieve this we do splitting only if
5267 register pressure is high in given basic block and there are reload
5268 pseudos requiring hard registers. We could do more register
5269 pressure calculations at any given program point to avoid necessary
5270 splitting even more but it is to expensive and the current approach
5271 works well enough. */
5272 static bool
5273 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5275 int i, src_regno, dst_regno, nregs;
5276 bool change_p, succ_p, update_reloads_num_p;
5277 rtx_insn *prev_insn, *last_insn;
5278 rtx next_usage_insns, set;
5279 enum reg_class cl;
5280 struct lra_insn_reg *reg;
5281 basic_block last_processed_bb, curr_bb = NULL;
5282 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5283 bitmap to_process;
5284 unsigned int j;
5285 bitmap_iterator bi;
5286 bool head_p, after_p;
5288 change_p = false;
5289 curr_usage_insns_check++;
5290 reloads_num = calls_num = 0;
5291 bitmap_clear (&check_only_regs);
5292 last_processed_bb = NULL;
5293 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5294 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5295 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5296 /* We don't process new insns generated in the loop. */
5297 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5299 prev_insn = PREV_INSN (curr_insn);
5300 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5301 curr_bb = BLOCK_FOR_INSN (curr_insn);
5302 if (last_processed_bb != curr_bb)
5304 /* We are at the end of BB. Add qualified living
5305 pseudos for potential splitting. */
5306 to_process = df_get_live_out (curr_bb);
5307 if (last_processed_bb != NULL)
5309 /* We are somewhere in the middle of EBB. */
5310 get_live_on_other_edges (curr_bb, last_processed_bb,
5311 &temp_bitmap);
5312 to_process = &temp_bitmap;
5314 last_processed_bb = curr_bb;
5315 last_insn = get_last_insertion_point (curr_bb);
5316 after_p = (! JUMP_P (last_insn)
5317 && (! CALL_P (last_insn)
5318 || (find_reg_note (last_insn,
5319 REG_NORETURN, NULL_RTX) == NULL_RTX
5320 && ! SIBLING_CALL_P (last_insn))));
5321 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5322 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5324 if ((int) j >= lra_constraint_new_regno_start)
5325 break;
5326 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5328 if (j < FIRST_PSEUDO_REGISTER)
5329 SET_HARD_REG_BIT (live_hard_regs, j);
5330 else
5331 add_to_hard_reg_set (&live_hard_regs,
5332 PSEUDO_REGNO_MODE (j),
5333 reg_renumber[j]);
5334 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5338 src_regno = dst_regno = -1;
5339 if (NONDEBUG_INSN_P (curr_insn)
5340 && (set = single_set (curr_insn)) != NULL_RTX
5341 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5343 src_regno = REGNO (SET_SRC (set));
5344 dst_regno = REGNO (SET_DEST (set));
5346 update_reloads_num_p = true;
5347 if (src_regno < lra_constraint_new_regno_start
5348 && src_regno >= FIRST_PSEUDO_REGISTER
5349 && reg_renumber[src_regno] < 0
5350 && dst_regno >= lra_constraint_new_regno_start
5351 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5353 /* 'reload_pseudo <- original_pseudo'. */
5354 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5355 reloads_num++;
5356 update_reloads_num_p = false;
5357 succ_p = false;
5358 if (usage_insns[src_regno].check == curr_usage_insns_check
5359 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5360 succ_p = inherit_reload_reg (false, src_regno, cl,
5361 curr_insn, next_usage_insns);
5362 if (succ_p)
5363 change_p = true;
5364 else
5365 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5366 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5367 IOR_HARD_REG_SET (potential_reload_hard_regs,
5368 reg_class_contents[cl]);
5370 else if (src_regno >= lra_constraint_new_regno_start
5371 && dst_regno < lra_constraint_new_regno_start
5372 && dst_regno >= FIRST_PSEUDO_REGISTER
5373 && reg_renumber[dst_regno] < 0
5374 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5375 && usage_insns[dst_regno].check == curr_usage_insns_check
5376 && (next_usage_insns
5377 = usage_insns[dst_regno].insns) != NULL_RTX)
5379 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5380 reloads_num++;
5381 update_reloads_num_p = false;
5382 /* 'original_pseudo <- reload_pseudo'. */
5383 if (! JUMP_P (curr_insn)
5384 && inherit_reload_reg (true, dst_regno, cl,
5385 curr_insn, next_usage_insns))
5386 change_p = true;
5387 /* Invalidate. */
5388 usage_insns[dst_regno].check = 0;
5389 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5390 IOR_HARD_REG_SET (potential_reload_hard_regs,
5391 reg_class_contents[cl]);
5393 else if (INSN_P (curr_insn))
5395 int iter;
5396 int max_uid = get_max_uid ();
5398 curr_id = lra_get_insn_recog_data (curr_insn);
5399 curr_static_id = curr_id->insn_static_data;
5400 to_inherit_num = 0;
5401 /* Process insn definitions. */
5402 for (iter = 0; iter < 2; iter++)
5403 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5404 reg != NULL;
5405 reg = reg->next)
5406 if (reg->type != OP_IN
5407 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5409 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5410 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5411 && usage_insns[dst_regno].check == curr_usage_insns_check
5412 && (next_usage_insns
5413 = usage_insns[dst_regno].insns) != NULL_RTX)
5415 struct lra_insn_reg *r;
5417 for (r = curr_id->regs; r != NULL; r = r->next)
5418 if (r->type != OP_OUT && r->regno == dst_regno)
5419 break;
5420 /* Don't do inheritance if the pseudo is also
5421 used in the insn. */
5422 if (r == NULL)
5423 /* We can not do inheritance right now
5424 because the current insn reg info (chain
5425 regs) can change after that. */
5426 add_to_inherit (dst_regno, next_usage_insns);
5428 /* We can not process one reg twice here because of
5429 usage_insns invalidation. */
5430 if ((dst_regno < FIRST_PSEUDO_REGISTER
5431 || reg_renumber[dst_regno] >= 0)
5432 && ! reg->subreg_p && reg->type != OP_IN)
5434 HARD_REG_SET s;
5436 if (split_if_necessary (dst_regno, reg->biggest_mode,
5437 potential_reload_hard_regs,
5438 false, curr_insn, max_uid))
5439 change_p = true;
5440 CLEAR_HARD_REG_SET (s);
5441 if (dst_regno < FIRST_PSEUDO_REGISTER)
5442 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5443 else
5444 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5445 reg_renumber[dst_regno]);
5446 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5448 /* We should invalidate potential inheritance or
5449 splitting for the current insn usages to the next
5450 usage insns (see code below) as the output pseudo
5451 prevents this. */
5452 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5453 && reg_renumber[dst_regno] < 0)
5454 || (reg->type == OP_OUT && ! reg->subreg_p
5455 && (dst_regno < FIRST_PSEUDO_REGISTER
5456 || reg_renumber[dst_regno] >= 0)))
5458 /* Invalidate and mark definitions. */
5459 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5460 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5461 else
5463 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5464 for (i = 0; i < nregs; i++)
5465 usage_insns[dst_regno + i].check
5466 = -(int) INSN_UID (curr_insn);
5470 /* Process clobbered call regs. */
5471 if (curr_id->arg_hard_regs != NULL)
5472 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5473 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5474 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
5475 = -(int) INSN_UID (curr_insn);
5476 if (! JUMP_P (curr_insn))
5477 for (i = 0; i < to_inherit_num; i++)
5478 if (inherit_reload_reg (true, to_inherit[i].regno,
5479 ALL_REGS, curr_insn,
5480 to_inherit[i].insns))
5481 change_p = true;
5482 if (CALL_P (curr_insn))
5484 rtx cheap, pat, dest;
5485 rtx_insn *restore;
5486 int regno, hard_regno;
5488 calls_num++;
5489 if ((cheap = find_reg_note (curr_insn,
5490 REG_RETURNED, NULL_RTX)) != NULL_RTX
5491 && ((cheap = XEXP (cheap, 0)), true)
5492 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5493 && (hard_regno = reg_renumber[regno]) >= 0
5494 /* If there are pending saves/restores, the
5495 optimization is not worth. */
5496 && usage_insns[regno].calls_num == calls_num - 1
5497 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5499 /* Restore the pseudo from the call result as
5500 REG_RETURNED note says that the pseudo value is
5501 in the call result and the pseudo is an argument
5502 of the call. */
5503 pat = PATTERN (curr_insn);
5504 if (GET_CODE (pat) == PARALLEL)
5505 pat = XVECEXP (pat, 0, 0);
5506 dest = SET_DEST (pat);
5507 /* For multiple return values dest is PARALLEL.
5508 Currently we handle only single return value case. */
5509 if (REG_P (dest))
5511 start_sequence ();
5512 emit_move_insn (cheap, copy_rtx (dest));
5513 restore = get_insns ();
5514 end_sequence ();
5515 lra_process_new_insns (curr_insn, NULL, restore,
5516 "Inserting call parameter restore");
5517 /* We don't need to save/restore of the pseudo from
5518 this call. */
5519 usage_insns[regno].calls_num = calls_num;
5520 bitmap_set_bit (&check_only_regs, regno);
5524 to_inherit_num = 0;
5525 /* Process insn usages. */
5526 for (iter = 0; iter < 2; iter++)
5527 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5528 reg != NULL;
5529 reg = reg->next)
5530 if ((reg->type != OP_OUT
5531 || (reg->type == OP_OUT && reg->subreg_p))
5532 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5534 if (src_regno >= FIRST_PSEUDO_REGISTER
5535 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5537 if (usage_insns[src_regno].check == curr_usage_insns_check
5538 && (next_usage_insns
5539 = usage_insns[src_regno].insns) != NULL_RTX
5540 && NONDEBUG_INSN_P (curr_insn))
5541 add_to_inherit (src_regno, next_usage_insns);
5542 else if (usage_insns[src_regno].check
5543 != -(int) INSN_UID (curr_insn))
5544 /* Add usages but only if the reg is not set up
5545 in the same insn. */
5546 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5548 else if (src_regno < FIRST_PSEUDO_REGISTER
5549 || reg_renumber[src_regno] >= 0)
5551 bool before_p;
5552 rtx_insn *use_insn = curr_insn;
5554 before_p = (JUMP_P (curr_insn)
5555 || (CALL_P (curr_insn) && reg->type == OP_IN));
5556 if (NONDEBUG_INSN_P (curr_insn)
5557 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5558 && split_if_necessary (src_regno, reg->biggest_mode,
5559 potential_reload_hard_regs,
5560 before_p, curr_insn, max_uid))
5562 if (reg->subreg_p)
5563 lra_risky_transformations_p = true;
5564 change_p = true;
5565 /* Invalidate. */
5566 usage_insns[src_regno].check = 0;
5567 if (before_p)
5568 use_insn = PREV_INSN (curr_insn);
5570 if (NONDEBUG_INSN_P (curr_insn))
5572 if (src_regno < FIRST_PSEUDO_REGISTER)
5573 add_to_hard_reg_set (&live_hard_regs,
5574 reg->biggest_mode, src_regno);
5575 else
5576 add_to_hard_reg_set (&live_hard_regs,
5577 PSEUDO_REGNO_MODE (src_regno),
5578 reg_renumber[src_regno]);
5580 add_next_usage_insn (src_regno, use_insn, reloads_num);
5583 /* Process used call regs. */
5584 if (curr_id->arg_hard_regs != NULL)
5585 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5586 if (src_regno < FIRST_PSEUDO_REGISTER)
5588 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5589 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5591 for (i = 0; i < to_inherit_num; i++)
5593 src_regno = to_inherit[i].regno;
5594 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5595 curr_insn, to_inherit[i].insns))
5596 change_p = true;
5597 else
5598 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5601 if (update_reloads_num_p
5602 && NONDEBUG_INSN_P (curr_insn)
5603 && (set = single_set (curr_insn)) != NULL_RTX)
5605 int regno = -1;
5606 if ((REG_P (SET_DEST (set))
5607 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5608 && reg_renumber[regno] < 0
5609 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5610 || (REG_P (SET_SRC (set))
5611 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5612 && reg_renumber[regno] < 0
5613 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5615 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5616 reloads_num++;
5617 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5618 IOR_HARD_REG_SET (potential_reload_hard_regs,
5619 reg_class_contents[cl]);
5622 /* We reached the start of the current basic block. */
5623 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5624 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5626 /* We reached the beginning of the current block -- do
5627 rest of spliting in the current BB. */
5628 to_process = df_get_live_in (curr_bb);
5629 if (BLOCK_FOR_INSN (head) != curr_bb)
5631 /* We are somewhere in the middle of EBB. */
5632 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5633 curr_bb, &temp_bitmap);
5634 to_process = &temp_bitmap;
5636 head_p = true;
5637 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5639 if ((int) j >= lra_constraint_new_regno_start)
5640 break;
5641 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5642 && usage_insns[j].check == curr_usage_insns_check
5643 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5645 if (need_for_split_p (potential_reload_hard_regs, j))
5647 if (lra_dump_file != NULL && head_p)
5649 fprintf (lra_dump_file,
5650 " ----------------------------------\n");
5651 head_p = false;
5653 if (split_reg (false, j, bb_note (curr_bb),
5654 next_usage_insns))
5655 change_p = true;
5657 usage_insns[j].check = 0;
5662 return change_p;
5665 /* This value affects EBB forming. If probability of edge from EBB to
5666 a BB is not greater than the following value, we don't add the BB
5667 to EBB. */
5668 #define EBB_PROBABILITY_CUTOFF \
5669 ((REG_BR_PROB_BASE * LRA_INHERITANCE_EBB_PROBABILITY_CUTOFF) / 100)
5671 /* Current number of inheritance/split iteration. */
5672 int lra_inheritance_iter;
5674 /* Entry function for inheritance/split pass. */
5675 void
5676 lra_inheritance (void)
5678 int i;
5679 basic_block bb, start_bb;
5680 edge e;
5682 lra_inheritance_iter++;
5683 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5684 return;
5685 timevar_push (TV_LRA_INHERITANCE);
5686 if (lra_dump_file != NULL)
5687 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5688 lra_inheritance_iter);
5689 curr_usage_insns_check = 0;
5690 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5691 for (i = 0; i < lra_constraint_new_regno_start; i++)
5692 usage_insns[i].check = 0;
5693 bitmap_initialize (&check_only_regs, &reg_obstack);
5694 bitmap_initialize (&live_regs, &reg_obstack);
5695 bitmap_initialize (&temp_bitmap, &reg_obstack);
5696 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5697 FOR_EACH_BB_FN (bb, cfun)
5699 start_bb = bb;
5700 if (lra_dump_file != NULL)
5701 fprintf (lra_dump_file, "EBB");
5702 /* Form a EBB starting with BB. */
5703 bitmap_clear (&ebb_global_regs);
5704 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5705 for (;;)
5707 if (lra_dump_file != NULL)
5708 fprintf (lra_dump_file, " %d", bb->index);
5709 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5710 || LABEL_P (BB_HEAD (bb->next_bb)))
5711 break;
5712 e = find_fallthru_edge (bb->succs);
5713 if (! e)
5714 break;
5715 if (e->probability < EBB_PROBABILITY_CUTOFF)
5716 break;
5717 bb = bb->next_bb;
5719 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5720 if (lra_dump_file != NULL)
5721 fprintf (lra_dump_file, "\n");
5722 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5723 /* Remember that the EBB head and tail can change in
5724 inherit_in_ebb. */
5725 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5727 bitmap_clear (&ebb_global_regs);
5728 bitmap_clear (&temp_bitmap);
5729 bitmap_clear (&live_regs);
5730 bitmap_clear (&check_only_regs);
5731 free (usage_insns);
5733 timevar_pop (TV_LRA_INHERITANCE);
5738 /* This page contains code to undo failed inheritance/split
5739 transformations. */
5741 /* Current number of iteration undoing inheritance/split. */
5742 int lra_undo_inheritance_iter;
5744 /* Fix BB live info LIVE after removing pseudos created on pass doing
5745 inheritance/split which are REMOVED_PSEUDOS. */
5746 static void
5747 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5749 unsigned int regno;
5750 bitmap_iterator bi;
5752 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5753 if (bitmap_clear_bit (live, regno))
5754 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5757 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5758 number. */
5759 static int
5760 get_regno (rtx reg)
5762 if (GET_CODE (reg) == SUBREG)
5763 reg = SUBREG_REG (reg);
5764 if (REG_P (reg))
5765 return REGNO (reg);
5766 return -1;
5769 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5770 return true if we did any change. The undo transformations for
5771 inheritance looks like
5772 i <- i2
5773 p <- i => p <- i2
5774 or removing
5775 p <- i, i <- p, and i <- i3
5776 where p is original pseudo from which inheritance pseudo i was
5777 created, i and i3 are removed inheritance pseudos, i2 is another
5778 not removed inheritance pseudo. All split pseudos or other
5779 occurrences of removed inheritance pseudos are changed on the
5780 corresponding original pseudos.
5782 The function also schedules insns changed and created during
5783 inheritance/split pass for processing by the subsequent constraint
5784 pass. */
5785 static bool
5786 remove_inheritance_pseudos (bitmap remove_pseudos)
5788 basic_block bb;
5789 int regno, sregno, prev_sregno, dregno, restore_regno;
5790 rtx set, prev_set;
5791 rtx_insn *prev_insn;
5792 bool change_p, done_p;
5794 change_p = ! bitmap_empty_p (remove_pseudos);
5795 /* We can not finish the function right away if CHANGE_P is true
5796 because we need to marks insns affected by previous
5797 inheritance/split pass for processing by the subsequent
5798 constraint pass. */
5799 FOR_EACH_BB_FN (bb, cfun)
5801 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5802 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5803 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5805 if (! INSN_P (curr_insn))
5806 continue;
5807 done_p = false;
5808 sregno = dregno = -1;
5809 if (change_p && NONDEBUG_INSN_P (curr_insn)
5810 && (set = single_set (curr_insn)) != NULL_RTX)
5812 dregno = get_regno (SET_DEST (set));
5813 sregno = get_regno (SET_SRC (set));
5816 if (sregno >= 0 && dregno >= 0)
5818 if ((bitmap_bit_p (remove_pseudos, sregno)
5819 && (lra_reg_info[sregno].restore_regno == dregno
5820 || (bitmap_bit_p (remove_pseudos, dregno)
5821 && (lra_reg_info[sregno].restore_regno
5822 == lra_reg_info[dregno].restore_regno))))
5823 || (bitmap_bit_p (remove_pseudos, dregno)
5824 && lra_reg_info[dregno].restore_regno == sregno))
5825 /* One of the following cases:
5826 original <- removed inheritance pseudo
5827 removed inherit pseudo <- another removed inherit pseudo
5828 removed inherit pseudo <- original pseudo
5830 removed_split_pseudo <- original_reg
5831 original_reg <- removed_split_pseudo */
5833 if (lra_dump_file != NULL)
5835 fprintf (lra_dump_file, " Removing %s:\n",
5836 bitmap_bit_p (&lra_split_regs, sregno)
5837 || bitmap_bit_p (&lra_split_regs, dregno)
5838 ? "split" : "inheritance");
5839 dump_insn_slim (lra_dump_file, curr_insn);
5841 lra_set_insn_deleted (curr_insn);
5842 done_p = true;
5844 else if (bitmap_bit_p (remove_pseudos, sregno)
5845 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5847 /* Search the following pattern:
5848 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5849 original_pseudo <- inherit_or_split_pseudo1
5850 where the 2nd insn is the current insn and
5851 inherit_or_split_pseudo2 is not removed. If it is found,
5852 change the current insn onto:
5853 original_pseudo <- inherit_or_split_pseudo2. */
5854 for (prev_insn = PREV_INSN (curr_insn);
5855 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5856 prev_insn = PREV_INSN (prev_insn))
5858 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5859 && (prev_set = single_set (prev_insn)) != NULL_RTX
5860 /* There should be no subregs in insn we are
5861 searching because only the original reg might
5862 be in subreg when we changed the mode of
5863 load/store for splitting. */
5864 && REG_P (SET_DEST (prev_set))
5865 && REG_P (SET_SRC (prev_set))
5866 && (int) REGNO (SET_DEST (prev_set)) == sregno
5867 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5868 >= FIRST_PSEUDO_REGISTER)
5869 /* As we consider chain of inheritance or
5870 splitting described in above comment we should
5871 check that sregno and prev_sregno were
5872 inheritance/split pseudos created from the
5873 same original regno. */
5874 && (lra_reg_info[sregno].restore_regno
5875 == lra_reg_info[prev_sregno].restore_regno)
5876 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5878 lra_assert (GET_MODE (SET_SRC (prev_set))
5879 == GET_MODE (regno_reg_rtx[sregno]));
5880 if (GET_CODE (SET_SRC (set)) == SUBREG)
5881 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5882 else
5883 SET_SRC (set) = SET_SRC (prev_set);
5884 /* As we are finishing with processing the insn
5885 here, check the destination too as it might
5886 inheritance pseudo for another pseudo. */
5887 if (bitmap_bit_p (remove_pseudos, dregno)
5888 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
5889 && (restore_regno
5890 = lra_reg_info[dregno].restore_regno) >= 0)
5892 if (GET_CODE (SET_DEST (set)) == SUBREG)
5893 SUBREG_REG (SET_DEST (set))
5894 = regno_reg_rtx[restore_regno];
5895 else
5896 SET_DEST (set) = regno_reg_rtx[restore_regno];
5898 lra_push_insn_and_update_insn_regno_info (curr_insn);
5899 lra_set_used_insn_alternative_by_uid
5900 (INSN_UID (curr_insn), -1);
5901 done_p = true;
5902 if (lra_dump_file != NULL)
5904 fprintf (lra_dump_file, " Change reload insn:\n");
5905 dump_insn_slim (lra_dump_file, curr_insn);
5910 if (! done_p)
5912 struct lra_insn_reg *reg;
5913 bool restored_regs_p = false;
5914 bool kept_regs_p = false;
5916 curr_id = lra_get_insn_recog_data (curr_insn);
5917 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5919 regno = reg->regno;
5920 restore_regno = lra_reg_info[regno].restore_regno;
5921 if (restore_regno >= 0)
5923 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5925 lra_substitute_pseudo_within_insn
5926 (curr_insn, regno, regno_reg_rtx[restore_regno],
5927 false);
5928 restored_regs_p = true;
5930 else
5931 kept_regs_p = true;
5934 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5936 /* The instruction has changed since the previous
5937 constraints pass. */
5938 lra_push_insn_and_update_insn_regno_info (curr_insn);
5939 lra_set_used_insn_alternative_by_uid
5940 (INSN_UID (curr_insn), -1);
5942 else if (restored_regs_p)
5943 /* The instruction has been restored to the form that
5944 it had during the previous constraints pass. */
5945 lra_update_insn_regno_info (curr_insn);
5946 if (restored_regs_p && lra_dump_file != NULL)
5948 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5949 dump_insn_slim (lra_dump_file, curr_insn);
5954 return change_p;
5957 /* If optional reload pseudos failed to get a hard register or was not
5958 inherited, it is better to remove optional reloads. We do this
5959 transformation after undoing inheritance to figure out necessity to
5960 remove optional reloads easier. Return true if we do any
5961 change. */
5962 static bool
5963 undo_optional_reloads (void)
5965 bool change_p, keep_p;
5966 unsigned int regno, uid;
5967 bitmap_iterator bi, bi2;
5968 rtx_insn *insn;
5969 rtx set, src, dest;
5970 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
5972 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
5973 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
5974 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5976 keep_p = false;
5977 /* Keep optional reloads from previous subpasses. */
5978 if (lra_reg_info[regno].restore_regno < 0
5979 /* If the original pseudo changed its allocation, just
5980 removing the optional pseudo is dangerous as the original
5981 pseudo will have longer live range. */
5982 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
5983 keep_p = true;
5984 else if (reg_renumber[regno] >= 0)
5985 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
5987 insn = lra_insn_recog_data[uid]->insn;
5988 if ((set = single_set (insn)) == NULL_RTX)
5989 continue;
5990 src = SET_SRC (set);
5991 dest = SET_DEST (set);
5992 if (! REG_P (src) || ! REG_P (dest))
5993 continue;
5994 if (REGNO (dest) == regno
5995 /* Ignore insn for optional reloads itself. */
5996 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
5997 /* Check only inheritance on last inheritance pass. */
5998 && (int) REGNO (src) >= new_regno_start
5999 /* Check that the optional reload was inherited. */
6000 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
6002 keep_p = true;
6003 break;
6006 if (keep_p)
6008 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
6009 if (lra_dump_file != NULL)
6010 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
6013 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
6014 bitmap_initialize (&insn_bitmap, &reg_obstack);
6015 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
6017 if (lra_dump_file != NULL)
6018 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
6019 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
6020 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
6022 insn = lra_insn_recog_data[uid]->insn;
6023 if ((set = single_set (insn)) != NULL_RTX)
6025 src = SET_SRC (set);
6026 dest = SET_DEST (set);
6027 if (REG_P (src) && REG_P (dest)
6028 && ((REGNO (src) == regno
6029 && (lra_reg_info[regno].restore_regno
6030 == (int) REGNO (dest)))
6031 || (REGNO (dest) == regno
6032 && (lra_reg_info[regno].restore_regno
6033 == (int) REGNO (src)))))
6035 if (lra_dump_file != NULL)
6037 fprintf (lra_dump_file, " Deleting move %u\n",
6038 INSN_UID (insn));
6039 dump_insn_slim (lra_dump_file, insn);
6041 lra_set_insn_deleted (insn);
6042 continue;
6044 /* We should not worry about generation memory-memory
6045 moves here as if the corresponding inheritance did
6046 not work (inheritance pseudo did not get a hard reg),
6047 we remove the inheritance pseudo and the optional
6048 reload. */
6050 lra_substitute_pseudo_within_insn
6051 (insn, regno, regno_reg_rtx[lra_reg_info[regno].restore_regno],
6052 false);
6053 lra_update_insn_regno_info (insn);
6054 if (lra_dump_file != NULL)
6056 fprintf (lra_dump_file,
6057 " Restoring original insn:\n");
6058 dump_insn_slim (lra_dump_file, insn);
6062 /* Clear restore_regnos. */
6063 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6064 lra_reg_info[regno].restore_regno = -1;
6065 bitmap_clear (&insn_bitmap);
6066 bitmap_clear (&removed_optional_reload_pseudos);
6067 return change_p;
6070 /* Entry function for undoing inheritance/split transformation. Return true
6071 if we did any RTL change in this pass. */
6072 bool
6073 lra_undo_inheritance (void)
6075 unsigned int regno;
6076 int restore_regno, hard_regno;
6077 int n_all_inherit, n_inherit, n_all_split, n_split;
6078 bitmap_head remove_pseudos;
6079 bitmap_iterator bi;
6080 bool change_p;
6082 lra_undo_inheritance_iter++;
6083 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6084 return false;
6085 if (lra_dump_file != NULL)
6086 fprintf (lra_dump_file,
6087 "\n********** Undoing inheritance #%d: **********\n\n",
6088 lra_undo_inheritance_iter);
6089 bitmap_initialize (&remove_pseudos, &reg_obstack);
6090 n_inherit = n_all_inherit = 0;
6091 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6092 if (lra_reg_info[regno].restore_regno >= 0)
6094 n_all_inherit++;
6095 if (reg_renumber[regno] < 0
6096 /* If the original pseudo changed its allocation, just
6097 removing inheritance is dangerous as for changing
6098 allocation we used shorter live-ranges. */
6099 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
6100 bitmap_set_bit (&remove_pseudos, regno);
6101 else
6102 n_inherit++;
6104 if (lra_dump_file != NULL && n_all_inherit != 0)
6105 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6106 n_inherit, n_all_inherit,
6107 (double) n_inherit / n_all_inherit * 100);
6108 n_split = n_all_split = 0;
6109 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6110 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
6112 n_all_split++;
6113 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6114 ? reg_renumber[restore_regno] : restore_regno);
6115 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6116 bitmap_set_bit (&remove_pseudos, regno);
6117 else
6119 n_split++;
6120 if (lra_dump_file != NULL)
6121 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6122 regno, restore_regno);
6125 if (lra_dump_file != NULL && n_all_split != 0)
6126 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6127 n_split, n_all_split,
6128 (double) n_split / n_all_split * 100);
6129 change_p = remove_inheritance_pseudos (&remove_pseudos);
6130 bitmap_clear (&remove_pseudos);
6131 /* Clear restore_regnos. */
6132 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6133 lra_reg_info[regno].restore_regno = -1;
6134 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6135 lra_reg_info[regno].restore_regno = -1;
6136 change_p = undo_optional_reloads () || change_p;
6137 return change_p;