2016-01-29 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / lra-constraints.c
blob9a7121fb6afc9b3367ab005fa1e583b3bdfbd43a
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2016 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 (special) 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 case CT_SPECIAL_MEMORY:
2043 if (MEM_P (op)
2044 && satisfies_memory_constraint_p (op, cn))
2045 win = true;
2046 else if (spilled_pseudo_p (op))
2047 win = true;
2048 break;
2050 break;
2052 reg:
2053 this_alternative = reg_class_subunion[this_alternative][cl];
2054 IOR_HARD_REG_SET (this_alternative_set,
2055 reg_class_contents[cl]);
2056 if (costly_p)
2058 this_costly_alternative
2059 = reg_class_subunion[this_costly_alternative][cl];
2060 IOR_HARD_REG_SET (this_costly_alternative_set,
2061 reg_class_contents[cl]);
2063 if (mode == BLKmode)
2064 break;
2065 winreg = true;
2066 if (REG_P (op))
2068 if (hard_regno[nop] >= 0
2069 && in_hard_reg_set_p (this_alternative_set,
2070 mode, hard_regno[nop]))
2071 win = true;
2072 else if (hard_regno[nop] < 0
2073 && in_class_p (op, this_alternative, NULL))
2074 win = true;
2076 break;
2078 if (c != ' ' && c != '\t')
2079 costly_p = c == '*';
2081 while ((p += len), c);
2083 scratch_p = (operand_reg[nop] != NULL_RTX
2084 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2085 /* Record which operands fit this alternative. */
2086 if (win)
2088 this_alternative_win = true;
2089 if (operand_reg[nop] != NULL_RTX)
2091 if (hard_regno[nop] >= 0)
2093 if (in_hard_reg_set_p (this_costly_alternative_set,
2094 mode, hard_regno[nop]))
2096 if (lra_dump_file != NULL)
2097 fprintf (lra_dump_file,
2098 " %d Costly set: reject++\n",
2099 nop);
2100 reject++;
2103 else
2105 /* Prefer won reg to spilled pseudo under other
2106 equal conditions for possibe inheritance. */
2107 if (! scratch_p)
2109 if (lra_dump_file != NULL)
2110 fprintf
2111 (lra_dump_file,
2112 " %d Non pseudo reload: reject++\n",
2113 nop);
2114 reject++;
2116 if (in_class_p (operand_reg[nop],
2117 this_costly_alternative, NULL))
2119 if (lra_dump_file != NULL)
2120 fprintf
2121 (lra_dump_file,
2122 " %d Non pseudo costly reload:"
2123 " reject++\n",
2124 nop);
2125 reject++;
2128 /* We simulate the behaviour of old reload here.
2129 Although scratches need hard registers and it
2130 might result in spilling other pseudos, no reload
2131 insns are generated for the scratches. So it
2132 might cost something but probably less than old
2133 reload pass believes. */
2134 if (scratch_p)
2136 if (lra_dump_file != NULL)
2137 fprintf (lra_dump_file,
2138 " %d Scratch win: reject+=2\n",
2139 nop);
2140 reject += 2;
2144 else if (did_match)
2145 this_alternative_match_win = true;
2146 else
2148 int const_to_mem = 0;
2149 bool no_regs_p;
2151 reject += op_reject;
2152 /* Never do output reload of stack pointer. It makes
2153 impossible to do elimination when SP is changed in
2154 RTL. */
2155 if (op == stack_pointer_rtx && ! frame_pointer_needed
2156 && curr_static_id->operand[nop].type != OP_IN)
2157 goto fail;
2159 /* If this alternative asks for a specific reg class, see if there
2160 is at least one allocatable register in that class. */
2161 no_regs_p
2162 = (this_alternative == NO_REGS
2163 || (hard_reg_set_subset_p
2164 (reg_class_contents[this_alternative],
2165 lra_no_alloc_regs)));
2167 /* For asms, verify that the class for this alternative is possible
2168 for the mode that is specified. */
2169 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2171 int i;
2172 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2173 if (HARD_REGNO_MODE_OK (i, mode)
2174 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2175 mode, i))
2176 break;
2177 if (i == FIRST_PSEUDO_REGISTER)
2178 winreg = false;
2181 /* If this operand accepts a register, and if the
2182 register class has at least one allocatable register,
2183 then this operand can be reloaded. */
2184 if (winreg && !no_regs_p)
2185 badop = false;
2187 if (badop)
2189 if (lra_dump_file != NULL)
2190 fprintf (lra_dump_file,
2191 " alt=%d: Bad operand -- refuse\n",
2192 nalt);
2193 goto fail;
2196 /* If not assigned pseudo has a class which a subset of
2197 required reg class, it is a less costly alternative
2198 as the pseudo still can get a hard reg of necessary
2199 class. */
2200 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2201 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2202 && ira_class_subset_p[this_alternative][cl])
2204 if (lra_dump_file != NULL)
2205 fprintf
2206 (lra_dump_file,
2207 " %d Super set class reg: reject-=3\n", nop);
2208 reject -= 3;
2211 this_alternative_offmemok = offmemok;
2212 if (this_costly_alternative != NO_REGS)
2214 if (lra_dump_file != NULL)
2215 fprintf (lra_dump_file,
2216 " %d Costly loser: reject++\n", nop);
2217 reject++;
2219 /* If the operand is dying, has a matching constraint,
2220 and satisfies constraints of the matched operand
2221 which failed to satisfy the own constraints, most probably
2222 the reload for this operand will be gone. */
2223 if (this_alternative_matches >= 0
2224 && !curr_alt_win[this_alternative_matches]
2225 && REG_P (op)
2226 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2227 && (hard_regno[nop] >= 0
2228 ? in_hard_reg_set_p (this_alternative_set,
2229 mode, hard_regno[nop])
2230 : in_class_p (op, this_alternative, NULL)))
2232 if (lra_dump_file != NULL)
2233 fprintf
2234 (lra_dump_file,
2235 " %d Dying matched operand reload: reject++\n",
2236 nop);
2237 reject++;
2239 else
2241 /* Strict_low_part requires to reload the register
2242 not the sub-register. In this case we should
2243 check that a final reload hard reg can hold the
2244 value mode. */
2245 if (curr_static_id->operand[nop].strict_low
2246 && REG_P (op)
2247 && hard_regno[nop] < 0
2248 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2249 && ira_class_hard_regs_num[this_alternative] > 0
2250 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2251 [this_alternative][0],
2252 GET_MODE
2253 (*curr_id->operand_loc[nop])))
2255 if (lra_dump_file != NULL)
2256 fprintf
2257 (lra_dump_file,
2258 " alt=%d: Strict low subreg reload -- refuse\n",
2259 nalt);
2260 goto fail;
2262 losers++;
2264 if (operand_reg[nop] != NULL_RTX
2265 /* Output operands and matched input operands are
2266 not inherited. The following conditions do not
2267 exactly describe the previous statement but they
2268 are pretty close. */
2269 && curr_static_id->operand[nop].type != OP_OUT
2270 && (this_alternative_matches < 0
2271 || curr_static_id->operand[nop].type != OP_IN))
2273 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2274 (operand_reg[nop])]
2275 .last_reload);
2277 /* The value of reload_sum has sense only if we
2278 process insns in their order. It happens only on
2279 the first constraints sub-pass when we do most of
2280 reload work. */
2281 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2282 reload_sum += last_reload - bb_reload_num;
2284 /* If this is a constant that is reloaded into the
2285 desired class by copying it to memory first, count
2286 that as another reload. This is consistent with
2287 other code and is required to avoid choosing another
2288 alternative when the constant is moved into memory.
2289 Note that the test here is precisely the same as in
2290 the code below that calls force_const_mem. */
2291 if (CONST_POOL_OK_P (mode, op)
2292 && ((targetm.preferred_reload_class
2293 (op, this_alternative) == NO_REGS)
2294 || no_input_reloads_p))
2296 const_to_mem = 1;
2297 if (! no_regs_p)
2298 losers++;
2301 /* Alternative loses if it requires a type of reload not
2302 permitted for this insn. We can always reload
2303 objects with a REG_UNUSED note. */
2304 if ((curr_static_id->operand[nop].type != OP_IN
2305 && no_output_reloads_p
2306 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2307 || (curr_static_id->operand[nop].type != OP_OUT
2308 && no_input_reloads_p && ! const_to_mem)
2309 || (this_alternative_matches >= 0
2310 && (no_input_reloads_p
2311 || (no_output_reloads_p
2312 && (curr_static_id->operand
2313 [this_alternative_matches].type != OP_IN)
2314 && ! find_reg_note (curr_insn, REG_UNUSED,
2315 no_subreg_reg_operand
2316 [this_alternative_matches])))))
2318 if (lra_dump_file != NULL)
2319 fprintf
2320 (lra_dump_file,
2321 " alt=%d: No input/otput reload -- refuse\n",
2322 nalt);
2323 goto fail;
2326 /* Alternative loses if it required class pseudo can not
2327 hold value of required mode. Such insns can be
2328 described by insn definitions with mode iterators. */
2329 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2330 && ! hard_reg_set_empty_p (this_alternative_set)
2331 /* It is common practice for constraints to use a
2332 class which does not have actually enough regs to
2333 hold the value (e.g. x86 AREG for mode requiring
2334 more one general reg). Therefore we have 2
2335 conditions to check that the reload pseudo can
2336 not hold the mode value. */
2337 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2338 [this_alternative][0],
2339 GET_MODE (*curr_id->operand_loc[nop]))
2340 /* The above condition is not enough as the first
2341 reg in ira_class_hard_regs can be not aligned for
2342 multi-words mode values. */
2343 && (prohibited_class_reg_set_mode_p
2344 (this_alternative, this_alternative_set,
2345 GET_MODE (*curr_id->operand_loc[nop]))))
2347 if (lra_dump_file != NULL)
2348 fprintf (lra_dump_file,
2349 " alt=%d: reload pseudo for op %d "
2350 " can not hold the mode value -- refuse\n",
2351 nalt, nop);
2352 goto fail;
2355 /* Check strong discouragement of reload of non-constant
2356 into class THIS_ALTERNATIVE. */
2357 if (! CONSTANT_P (op) && ! no_regs_p
2358 && (targetm.preferred_reload_class
2359 (op, this_alternative) == NO_REGS
2360 || (curr_static_id->operand[nop].type == OP_OUT
2361 && (targetm.preferred_output_reload_class
2362 (op, this_alternative) == NO_REGS))))
2364 if (lra_dump_file != NULL)
2365 fprintf (lra_dump_file,
2366 " %d Non-prefered reload: reject+=%d\n",
2367 nop, LRA_MAX_REJECT);
2368 reject += LRA_MAX_REJECT;
2371 if (! (MEM_P (op) && offmemok)
2372 && ! (const_to_mem && constmemok))
2374 /* We prefer to reload pseudos over reloading other
2375 things, since such reloads may be able to be
2376 eliminated later. So bump REJECT in other cases.
2377 Don't do this in the case where we are forcing a
2378 constant into memory and it will then win since
2379 we don't want to have a different alternative
2380 match then. */
2381 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2383 if (lra_dump_file != NULL)
2384 fprintf
2385 (lra_dump_file,
2386 " %d Non-pseudo reload: reject+=2\n",
2387 nop);
2388 reject += 2;
2391 if (! no_regs_p)
2392 reload_nregs
2393 += ira_reg_class_max_nregs[this_alternative][mode];
2395 if (SMALL_REGISTER_CLASS_P (this_alternative))
2397 if (lra_dump_file != NULL)
2398 fprintf
2399 (lra_dump_file,
2400 " %d Small class reload: reject+=%d\n",
2401 nop, LRA_LOSER_COST_FACTOR / 2);
2402 reject += LRA_LOSER_COST_FACTOR / 2;
2406 /* We are trying to spill pseudo into memory. It is
2407 usually more costly than moving to a hard register
2408 although it might takes the same number of
2409 reloads. */
2410 if (no_regs_p && REG_P (op) && hard_regno[nop] >= 0)
2412 if (lra_dump_file != NULL)
2413 fprintf
2414 (lra_dump_file,
2415 " %d Spill pseudo into memory: reject+=3\n",
2416 nop);
2417 reject += 3;
2418 if (VECTOR_MODE_P (mode))
2420 /* Spilling vectors into memory is usually more
2421 costly as they contain big values. */
2422 if (lra_dump_file != NULL)
2423 fprintf
2424 (lra_dump_file,
2425 " %d Spill vector pseudo: reject+=2\n",
2426 nop);
2427 reject += 2;
2431 #ifdef SECONDARY_MEMORY_NEEDED
2432 /* If reload requires moving value through secondary
2433 memory, it will need one more insn at least. */
2434 if (this_alternative != NO_REGS
2435 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2436 && ((curr_static_id->operand[nop].type != OP_OUT
2437 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2438 GET_MODE (op)))
2439 || (curr_static_id->operand[nop].type != OP_IN
2440 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2441 GET_MODE (op)))))
2442 losers++;
2443 #endif
2444 /* Input reloads can be inherited more often than output
2445 reloads can be removed, so penalize output
2446 reloads. */
2447 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2449 if (lra_dump_file != NULL)
2450 fprintf
2451 (lra_dump_file,
2452 " %d Non input pseudo reload: reject++\n",
2453 nop);
2454 reject++;
2458 if (early_clobber_p && ! scratch_p)
2460 if (lra_dump_file != NULL)
2461 fprintf (lra_dump_file,
2462 " %d Early clobber: reject++\n", nop);
2463 reject++;
2465 /* ??? We check early clobbers after processing all operands
2466 (see loop below) and there we update the costs more.
2467 Should we update the cost (may be approximately) here
2468 because of early clobber register reloads or it is a rare
2469 or non-important thing to be worth to do it. */
2470 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2471 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2473 if (lra_dump_file != NULL)
2474 fprintf (lra_dump_file,
2475 " alt=%d,overall=%d,losers=%d -- refuse\n",
2476 nalt, overall, losers);
2477 goto fail;
2480 curr_alt[nop] = this_alternative;
2481 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2482 curr_alt_win[nop] = this_alternative_win;
2483 curr_alt_match_win[nop] = this_alternative_match_win;
2484 curr_alt_offmemok[nop] = this_alternative_offmemok;
2485 curr_alt_matches[nop] = this_alternative_matches;
2487 if (this_alternative_matches >= 0
2488 && !did_match && !this_alternative_win)
2489 curr_alt_win[this_alternative_matches] = false;
2491 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2492 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2494 if (curr_insn_set != NULL_RTX && n_operands == 2
2495 /* Prevent processing non-move insns. */
2496 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2497 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2498 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2499 && REG_P (no_subreg_reg_operand[0])
2500 && REG_P (no_subreg_reg_operand[1])
2501 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2502 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2503 || (! curr_alt_win[0] && curr_alt_win[1]
2504 && REG_P (no_subreg_reg_operand[1])
2505 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2506 || (curr_alt_win[0] && ! curr_alt_win[1]
2507 && REG_P (no_subreg_reg_operand[0])
2508 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2509 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2510 no_subreg_reg_operand[1])
2511 || (targetm.preferred_reload_class
2512 (no_subreg_reg_operand[1],
2513 (enum reg_class) curr_alt[1]) != NO_REGS))
2514 /* If it is a result of recent elimination in move
2515 insn we can transform it into an add still by
2516 using this alternative. */
2517 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2519 /* We have a move insn and a new reload insn will be similar
2520 to the current insn. We should avoid such situation as it
2521 results in LRA cycling. */
2522 overall += LRA_MAX_REJECT;
2524 ok_p = true;
2525 curr_alt_dont_inherit_ops_num = 0;
2526 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2528 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2529 HARD_REG_SET temp_set;
2531 i = early_clobbered_nops[nop];
2532 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2533 || hard_regno[i] < 0)
2534 continue;
2535 lra_assert (operand_reg[i] != NULL_RTX);
2536 clobbered_hard_regno = hard_regno[i];
2537 CLEAR_HARD_REG_SET (temp_set);
2538 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2539 first_conflict_j = last_conflict_j = -1;
2540 for (j = 0; j < n_operands; j++)
2541 if (j == i
2542 /* We don't want process insides of match_operator and
2543 match_parallel because otherwise we would process
2544 their operands once again generating a wrong
2545 code. */
2546 || curr_static_id->operand[j].is_operator)
2547 continue;
2548 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2549 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2550 continue;
2551 /* If we don't reload j-th operand, check conflicts. */
2552 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2553 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2555 if (first_conflict_j < 0)
2556 first_conflict_j = j;
2557 last_conflict_j = j;
2559 if (last_conflict_j < 0)
2560 continue;
2561 /* If earlyclobber operand conflicts with another
2562 non-matching operand which is actually the same register
2563 as the earlyclobber operand, it is better to reload the
2564 another operand as an operand matching the earlyclobber
2565 operand can be also the same. */
2566 if (first_conflict_j == last_conflict_j
2567 && operand_reg[last_conflict_j] != NULL_RTX
2568 && ! curr_alt_match_win[last_conflict_j]
2569 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2571 curr_alt_win[last_conflict_j] = false;
2572 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2573 = last_conflict_j;
2574 losers++;
2575 /* Early clobber was already reflected in REJECT. */
2576 lra_assert (reject > 0);
2577 if (lra_dump_file != NULL)
2578 fprintf
2579 (lra_dump_file,
2580 " %d Conflict early clobber reload: reject--\n",
2582 reject--;
2583 overall += LRA_LOSER_COST_FACTOR - 1;
2585 else
2587 /* We need to reload early clobbered register and the
2588 matched registers. */
2589 for (j = 0; j < n_operands; j++)
2590 if (curr_alt_matches[j] == i)
2592 curr_alt_match_win[j] = false;
2593 losers++;
2594 overall += LRA_LOSER_COST_FACTOR;
2596 if (! curr_alt_match_win[i])
2597 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2598 else
2600 /* Remember pseudos used for match reloads are never
2601 inherited. */
2602 lra_assert (curr_alt_matches[i] >= 0);
2603 curr_alt_win[curr_alt_matches[i]] = false;
2605 curr_alt_win[i] = curr_alt_match_win[i] = false;
2606 losers++;
2607 /* Early clobber was already reflected in REJECT. */
2608 lra_assert (reject > 0);
2609 if (lra_dump_file != NULL)
2610 fprintf
2611 (lra_dump_file,
2612 " %d Matched conflict early clobber reloads:"
2613 "reject--\n",
2615 reject--;
2616 overall += LRA_LOSER_COST_FACTOR - 1;
2619 if (lra_dump_file != NULL)
2620 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2621 nalt, overall, losers, reload_nregs);
2623 /* If this alternative can be made to work by reloading, and it
2624 needs less reloading than the others checked so far, record
2625 it as the chosen goal for reloading. */
2626 if ((best_losers != 0 && losers == 0)
2627 || (((best_losers == 0 && losers == 0)
2628 || (best_losers != 0 && losers != 0))
2629 && (best_overall > overall
2630 || (best_overall == overall
2631 /* If the cost of the reloads is the same,
2632 prefer alternative which requires minimal
2633 number of reload regs. */
2634 && (reload_nregs < best_reload_nregs
2635 || (reload_nregs == best_reload_nregs
2636 && (best_reload_sum < reload_sum
2637 || (best_reload_sum == reload_sum
2638 && nalt < goal_alt_number))))))))
2640 for (nop = 0; nop < n_operands; nop++)
2642 goal_alt_win[nop] = curr_alt_win[nop];
2643 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2644 goal_alt_matches[nop] = curr_alt_matches[nop];
2645 goal_alt[nop] = curr_alt[nop];
2646 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2648 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2649 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2650 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2651 goal_alt_swapped = curr_swapped;
2652 best_overall = overall;
2653 best_losers = losers;
2654 best_reload_nregs = reload_nregs;
2655 best_reload_sum = reload_sum;
2656 goal_alt_number = nalt;
2658 if (losers == 0)
2659 /* Everything is satisfied. Do not process alternatives
2660 anymore. */
2661 break;
2662 fail:
2665 return ok_p;
2668 /* Make reload base reg from address AD. */
2669 static rtx
2670 base_to_reg (struct address_info *ad)
2672 enum reg_class cl;
2673 int code = -1;
2674 rtx new_inner = NULL_RTX;
2675 rtx new_reg = NULL_RTX;
2676 rtx_insn *insn;
2677 rtx_insn *last_insn = get_last_insn();
2679 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2680 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2681 get_index_code (ad));
2682 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2683 cl, "base");
2684 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2685 ad->disp_term == NULL
2686 ? gen_int_mode (0, ad->mode)
2687 : *ad->disp_term);
2688 if (!valid_address_p (ad->mode, new_inner, ad->as))
2689 return NULL_RTX;
2690 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base_term));
2691 code = recog_memoized (insn);
2692 if (code < 0)
2694 delete_insns_since (last_insn);
2695 return NULL_RTX;
2698 return new_inner;
2701 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2702 static rtx
2703 base_plus_disp_to_reg (struct address_info *ad)
2705 enum reg_class cl;
2706 rtx new_reg;
2708 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2709 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2710 get_index_code (ad));
2711 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2712 cl, "base + disp");
2713 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2714 return new_reg;
2717 /* Make reload of index part of address AD. Return the new
2718 pseudo. */
2719 static rtx
2720 index_part_to_reg (struct address_info *ad)
2722 rtx new_reg;
2724 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2725 INDEX_REG_CLASS, "index term");
2726 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2727 GEN_INT (get_index_scale (ad)), new_reg, 1);
2728 return new_reg;
2731 /* Return true if we can add a displacement to address AD, even if that
2732 makes the address invalid. The fix-up code requires any new address
2733 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2734 static bool
2735 can_add_disp_p (struct address_info *ad)
2737 return (!ad->autoinc_p
2738 && ad->segment == NULL
2739 && ad->base == ad->base_term
2740 && ad->disp == ad->disp_term);
2743 /* Make equiv substitution in address AD. Return true if a substitution
2744 was made. */
2745 static bool
2746 equiv_address_substitution (struct address_info *ad)
2748 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2749 HOST_WIDE_INT disp, scale;
2750 bool change_p;
2752 base_term = strip_subreg (ad->base_term);
2753 if (base_term == NULL)
2754 base_reg = new_base_reg = NULL_RTX;
2755 else
2757 base_reg = *base_term;
2758 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2760 index_term = strip_subreg (ad->index_term);
2761 if (index_term == NULL)
2762 index_reg = new_index_reg = NULL_RTX;
2763 else
2765 index_reg = *index_term;
2766 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2768 if (base_reg == new_base_reg && index_reg == new_index_reg)
2769 return false;
2770 disp = 0;
2771 change_p = false;
2772 if (lra_dump_file != NULL)
2774 fprintf (lra_dump_file, "Changing address in insn %d ",
2775 INSN_UID (curr_insn));
2776 dump_value_slim (lra_dump_file, *ad->outer, 1);
2778 if (base_reg != new_base_reg)
2780 if (REG_P (new_base_reg))
2782 *base_term = new_base_reg;
2783 change_p = true;
2785 else if (GET_CODE (new_base_reg) == PLUS
2786 && REG_P (XEXP (new_base_reg, 0))
2787 && CONST_INT_P (XEXP (new_base_reg, 1))
2788 && can_add_disp_p (ad))
2790 disp += INTVAL (XEXP (new_base_reg, 1));
2791 *base_term = XEXP (new_base_reg, 0);
2792 change_p = true;
2794 if (ad->base_term2 != NULL)
2795 *ad->base_term2 = *ad->base_term;
2797 if (index_reg != new_index_reg)
2799 if (REG_P (new_index_reg))
2801 *index_term = new_index_reg;
2802 change_p = true;
2804 else if (GET_CODE (new_index_reg) == PLUS
2805 && REG_P (XEXP (new_index_reg, 0))
2806 && CONST_INT_P (XEXP (new_index_reg, 1))
2807 && can_add_disp_p (ad)
2808 && (scale = get_index_scale (ad)))
2810 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2811 *index_term = XEXP (new_index_reg, 0);
2812 change_p = true;
2815 if (disp != 0)
2817 if (ad->disp != NULL)
2818 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
2819 else
2821 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
2822 update_address (ad);
2824 change_p = true;
2826 if (lra_dump_file != NULL)
2828 if (! change_p)
2829 fprintf (lra_dump_file, " -- no change\n");
2830 else
2832 fprintf (lra_dump_file, " on equiv ");
2833 dump_value_slim (lra_dump_file, *ad->outer, 1);
2834 fprintf (lra_dump_file, "\n");
2837 return change_p;
2840 /* Major function to make reloads for an address in operand NOP or
2841 check its correctness (If CHECK_ONLY_P is true). The supported
2842 cases are:
2844 1) an address that existed before LRA started, at which point it
2845 must have been valid. These addresses are subject to elimination
2846 and may have become invalid due to the elimination offset being out
2847 of range.
2849 2) an address created by forcing a constant to memory
2850 (force_const_to_mem). The initial form of these addresses might
2851 not be valid, and it is this function's job to make them valid.
2853 3) a frame address formed from a register and a (possibly zero)
2854 constant offset. As above, these addresses might not be valid and
2855 this function must make them so.
2857 Add reloads to the lists *BEFORE and *AFTER. We might need to add
2858 reloads to *AFTER because of inc/dec, {pre, post} modify in the
2859 address. Return true for any RTL change.
2861 The function is a helper function which does not produce all
2862 transformations (when CHECK_ONLY_P is false) which can be
2863 necessary. It does just basic steps. To do all necessary
2864 transformations use function process_address. */
2865 static bool
2866 process_address_1 (int nop, bool check_only_p,
2867 rtx_insn **before, rtx_insn **after)
2869 struct address_info ad;
2870 rtx new_reg;
2871 rtx op = *curr_id->operand_loc[nop];
2872 const char *constraint = curr_static_id->operand[nop].constraint;
2873 enum constraint_num cn = lookup_constraint (constraint);
2874 bool change_p = false;
2876 if (MEM_P (op)
2877 && GET_MODE (op) == BLKmode
2878 && GET_CODE (XEXP (op, 0)) == SCRATCH)
2879 return false;
2881 if (insn_extra_address_constraint (cn))
2882 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
2883 else if (MEM_P (op))
2884 decompose_mem_address (&ad, op);
2885 else if (GET_CODE (op) == SUBREG
2886 && MEM_P (SUBREG_REG (op)))
2887 decompose_mem_address (&ad, SUBREG_REG (op));
2888 else
2889 return false;
2890 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
2891 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
2892 when INDEX_REG_CLASS is a single register class. */
2893 if (ad.base_term != NULL
2894 && ad.index_term != NULL
2895 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
2896 && REG_P (*ad.base_term)
2897 && REG_P (*ad.index_term)
2898 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
2899 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
2901 std::swap (ad.base, ad.index);
2902 std::swap (ad.base_term, ad.index_term);
2904 if (! check_only_p)
2905 change_p = equiv_address_substitution (&ad);
2906 if (ad.base_term != NULL
2907 && (process_addr_reg
2908 (ad.base_term, check_only_p, before,
2909 (ad.autoinc_p
2910 && !(REG_P (*ad.base_term)
2911 && find_regno_note (curr_insn, REG_DEAD,
2912 REGNO (*ad.base_term)) != NULL_RTX)
2913 ? after : NULL),
2914 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
2915 get_index_code (&ad)))))
2917 change_p = true;
2918 if (ad.base_term2 != NULL)
2919 *ad.base_term2 = *ad.base_term;
2921 if (ad.index_term != NULL
2922 && process_addr_reg (ad.index_term, check_only_p,
2923 before, NULL, INDEX_REG_CLASS))
2924 change_p = true;
2926 /* Target hooks sometimes don't treat extra-constraint addresses as
2927 legitimate address_operands, so handle them specially. */
2928 if (insn_extra_address_constraint (cn)
2929 && satisfies_address_constraint_p (&ad, cn))
2930 return change_p;
2932 if (check_only_p)
2933 return change_p;
2935 /* There are three cases where the shape of *AD.INNER may now be invalid:
2937 1) the original address was valid, but either elimination or
2938 equiv_address_substitution was applied and that made
2939 the address invalid.
2941 2) the address is an invalid symbolic address created by
2942 force_const_to_mem.
2944 3) the address is a frame address with an invalid offset.
2946 4) the address is a frame address with an invalid base.
2948 All these cases involve a non-autoinc address, so there is no
2949 point revalidating other types. */
2950 if (ad.autoinc_p || valid_address_p (&ad))
2951 return change_p;
2953 /* Any index existed before LRA started, so we can assume that the
2954 presence and shape of the index is valid. */
2955 push_to_sequence (*before);
2956 lra_assert (ad.disp == ad.disp_term);
2957 if (ad.base == NULL)
2959 if (ad.index == NULL)
2961 int code = -1;
2962 enum reg_class cl = base_reg_class (ad.mode, ad.as,
2963 SCRATCH, SCRATCH);
2964 rtx addr = *ad.inner;
2966 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
2967 if (HAVE_lo_sum)
2969 rtx_insn *insn;
2970 rtx_insn *last = get_last_insn ();
2972 /* addr => lo_sum (new_base, addr), case (2) above. */
2973 insn = emit_insn (gen_rtx_SET
2974 (new_reg,
2975 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
2976 code = recog_memoized (insn);
2977 if (code >= 0)
2979 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
2980 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2982 /* Try to put lo_sum into register. */
2983 insn = emit_insn (gen_rtx_SET
2984 (new_reg,
2985 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
2986 code = recog_memoized (insn);
2987 if (code >= 0)
2989 *ad.inner = new_reg;
2990 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
2992 *ad.inner = addr;
2993 code = -1;
2999 if (code < 0)
3000 delete_insns_since (last);
3003 if (code < 0)
3005 /* addr => new_base, case (2) above. */
3006 lra_emit_move (new_reg, addr);
3007 *ad.inner = new_reg;
3010 else
3012 /* index * scale + disp => new base + index * scale,
3013 case (1) above. */
3014 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3015 GET_CODE (*ad.index));
3017 lra_assert (INDEX_REG_CLASS != NO_REGS);
3018 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3019 lra_emit_move (new_reg, *ad.disp);
3020 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3021 new_reg, *ad.index);
3024 else if (ad.index == NULL)
3026 int regno;
3027 enum reg_class cl;
3028 rtx set;
3029 rtx_insn *insns, *last_insn;
3030 /* Try to reload base into register only if the base is invalid
3031 for the address but with valid offset, case (4) above. */
3032 start_sequence ();
3033 new_reg = base_to_reg (&ad);
3035 /* base + disp => new base, cases (1) and (3) above. */
3036 /* Another option would be to reload the displacement into an
3037 index register. However, postreload has code to optimize
3038 address reloads that have the same base and different
3039 displacements, so reloading into an index register would
3040 not necessarily be a win. */
3041 if (new_reg == NULL_RTX)
3042 new_reg = base_plus_disp_to_reg (&ad);
3043 insns = get_insns ();
3044 last_insn = get_last_insn ();
3045 /* If we generated at least two insns, try last insn source as
3046 an address. If we succeed, we generate one less insn. */
3047 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
3048 && GET_CODE (SET_SRC (set)) == PLUS
3049 && REG_P (XEXP (SET_SRC (set), 0))
3050 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3052 *ad.inner = SET_SRC (set);
3053 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3055 *ad.base_term = XEXP (SET_SRC (set), 0);
3056 *ad.disp_term = XEXP (SET_SRC (set), 1);
3057 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3058 get_index_code (&ad));
3059 regno = REGNO (*ad.base_term);
3060 if (regno >= FIRST_PSEUDO_REGISTER
3061 && cl != lra_get_allocno_class (regno))
3062 lra_change_class (regno, cl, " Change to", true);
3063 new_reg = SET_SRC (set);
3064 delete_insns_since (PREV_INSN (last_insn));
3067 /* Try if target can split displacement into legitimite new disp
3068 and offset. If it's the case, we replace the last insn with
3069 insns for base + offset => new_reg and set new_reg + new disp
3070 to *ad.inner. */
3071 last_insn = get_last_insn ();
3072 if ((set = single_set (last_insn)) != NULL_RTX
3073 && GET_CODE (SET_SRC (set)) == PLUS
3074 && REG_P (XEXP (SET_SRC (set), 0))
3075 && REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
3076 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3078 rtx addend, disp = XEXP (SET_SRC (set), 1);
3079 if (targetm.legitimize_address_displacement (&disp, &addend,
3080 ad.mode))
3082 rtx_insn *new_insns;
3083 start_sequence ();
3084 lra_emit_add (new_reg, XEXP (SET_SRC (set), 0), addend);
3085 new_insns = get_insns ();
3086 end_sequence ();
3087 new_reg = gen_rtx_PLUS (Pmode, new_reg, disp);
3088 delete_insns_since (PREV_INSN (last_insn));
3089 add_insn (new_insns);
3090 insns = get_insns ();
3093 end_sequence ();
3094 emit_insn (insns);
3095 *ad.inner = new_reg;
3097 else if (ad.disp_term != NULL)
3099 /* base + scale * index + disp => new base + scale * index,
3100 case (1) above. */
3101 new_reg = base_plus_disp_to_reg (&ad);
3102 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3103 new_reg, *ad.index);
3105 else if (get_index_scale (&ad) == 1)
3107 /* The last transformation to one reg will be made in
3108 curr_insn_transform function. */
3109 end_sequence ();
3110 return false;
3112 else
3114 /* base + scale * index => base + new_reg,
3115 case (1) above.
3116 Index part of address may become invalid. For example, we
3117 changed pseudo on the equivalent memory and a subreg of the
3118 pseudo onto the memory of different mode for which the scale is
3119 prohibitted. */
3120 new_reg = index_part_to_reg (&ad);
3121 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3122 *ad.base_term, new_reg);
3124 *before = get_insns ();
3125 end_sequence ();
3126 return true;
3129 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3130 Use process_address_1 as a helper function. Return true for any
3131 RTL changes.
3133 If CHECK_ONLY_P is true, just check address correctness. Return
3134 false if the address correct. */
3135 static bool
3136 process_address (int nop, bool check_only_p,
3137 rtx_insn **before, rtx_insn **after)
3139 bool res = false;
3141 while (process_address_1 (nop, check_only_p, before, after))
3143 if (check_only_p)
3144 return true;
3145 res = true;
3147 return res;
3150 /* Emit insns to reload VALUE into a new register. VALUE is an
3151 auto-increment or auto-decrement RTX whose operand is a register or
3152 memory location; so reloading involves incrementing that location.
3153 IN is either identical to VALUE, or some cheaper place to reload
3154 value being incremented/decremented from.
3156 INC_AMOUNT is the number to increment or decrement by (always
3157 positive and ignored for POST_MODIFY/PRE_MODIFY).
3159 Return pseudo containing the result. */
3160 static rtx
3161 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3163 /* REG or MEM to be copied and incremented. */
3164 rtx incloc = XEXP (value, 0);
3165 /* Nonzero if increment after copying. */
3166 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3167 || GET_CODE (value) == POST_MODIFY);
3168 rtx_insn *last;
3169 rtx inc;
3170 rtx_insn *add_insn;
3171 int code;
3172 rtx real_in = in == value ? incloc : in;
3173 rtx result;
3174 bool plus_p = true;
3176 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3178 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3179 || GET_CODE (XEXP (value, 1)) == MINUS);
3180 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3181 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3182 inc = XEXP (XEXP (value, 1), 1);
3184 else
3186 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3187 inc_amount = -inc_amount;
3189 inc = GEN_INT (inc_amount);
3192 if (! post && REG_P (incloc))
3193 result = incloc;
3194 else
3195 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3196 "INC/DEC result");
3198 if (real_in != result)
3200 /* First copy the location to the result register. */
3201 lra_assert (REG_P (result));
3202 emit_insn (gen_move_insn (result, real_in));
3205 /* We suppose that there are insns to add/sub with the constant
3206 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3207 old reload worked with this assumption. If the assumption
3208 becomes wrong, we should use approach in function
3209 base_plus_disp_to_reg. */
3210 if (in == value)
3212 /* See if we can directly increment INCLOC. */
3213 last = get_last_insn ();
3214 add_insn = emit_insn (plus_p
3215 ? gen_add2_insn (incloc, inc)
3216 : gen_sub2_insn (incloc, inc));
3218 code = recog_memoized (add_insn);
3219 if (code >= 0)
3221 if (! post && result != incloc)
3222 emit_insn (gen_move_insn (result, incloc));
3223 return result;
3225 delete_insns_since (last);
3228 /* If couldn't do the increment directly, must increment in RESULT.
3229 The way we do this depends on whether this is pre- or
3230 post-increment. For pre-increment, copy INCLOC to the reload
3231 register, increment it there, then save back. */
3232 if (! post)
3234 if (real_in != result)
3235 emit_insn (gen_move_insn (result, real_in));
3236 if (plus_p)
3237 emit_insn (gen_add2_insn (result, inc));
3238 else
3239 emit_insn (gen_sub2_insn (result, inc));
3240 if (result != incloc)
3241 emit_insn (gen_move_insn (incloc, result));
3243 else
3245 /* Post-increment.
3247 Because this might be a jump insn or a compare, and because
3248 RESULT may not be available after the insn in an input
3249 reload, we must do the incrementing before the insn being
3250 reloaded for.
3252 We have already copied IN to RESULT. Increment the copy in
3253 RESULT, save that back, then decrement RESULT so it has
3254 the original value. */
3255 if (plus_p)
3256 emit_insn (gen_add2_insn (result, inc));
3257 else
3258 emit_insn (gen_sub2_insn (result, inc));
3259 emit_insn (gen_move_insn (incloc, result));
3260 /* Restore non-modified value for the result. We prefer this
3261 way because it does not require an additional hard
3262 register. */
3263 if (plus_p)
3265 if (CONST_INT_P (inc))
3266 emit_insn (gen_add2_insn (result,
3267 gen_int_mode (-INTVAL (inc),
3268 GET_MODE (result))));
3269 else
3270 emit_insn (gen_sub2_insn (result, inc));
3272 else
3273 emit_insn (gen_add2_insn (result, inc));
3275 return result;
3278 /* Return true if the current move insn does not need processing as we
3279 already know that it satisfies its constraints. */
3280 static bool
3281 simple_move_p (void)
3283 rtx dest, src;
3284 enum reg_class dclass, sclass;
3286 lra_assert (curr_insn_set != NULL_RTX);
3287 dest = SET_DEST (curr_insn_set);
3288 src = SET_SRC (curr_insn_set);
3289 return ((dclass = get_op_class (dest)) != NO_REGS
3290 && (sclass = get_op_class (src)) != NO_REGS
3291 /* The backend guarantees that register moves of cost 2
3292 never need reloads. */
3293 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3296 /* Swap operands NOP and NOP + 1. */
3297 static inline void
3298 swap_operands (int nop)
3300 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3301 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3302 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3303 /* Swap the duplicates too. */
3304 lra_update_dup (curr_id, nop);
3305 lra_update_dup (curr_id, nop + 1);
3308 /* Main entry point of the constraint code: search the body of the
3309 current insn to choose the best alternative. It is mimicking insn
3310 alternative cost calculation model of former reload pass. That is
3311 because machine descriptions were written to use this model. This
3312 model can be changed in future. Make commutative operand exchange
3313 if it is chosen.
3315 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3316 constraints. Return true if any change happened during function
3317 call.
3319 If CHECK_ONLY_P is true then don't do any transformation. Just
3320 check that the insn satisfies all constraints. If the insn does
3321 not satisfy any constraint, return true. */
3322 static bool
3323 curr_insn_transform (bool check_only_p)
3325 int i, j, k;
3326 int n_operands;
3327 int n_alternatives;
3328 int commutative;
3329 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3330 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3331 rtx_insn *before, *after;
3332 bool alt_p = false;
3333 /* Flag that the insn has been changed through a transformation. */
3334 bool change_p;
3335 bool sec_mem_p;
3336 #ifdef SECONDARY_MEMORY_NEEDED
3337 bool use_sec_mem_p;
3338 #endif
3339 int max_regno_before;
3340 int reused_alternative_num;
3342 curr_insn_set = single_set (curr_insn);
3343 if (curr_insn_set != NULL_RTX && simple_move_p ())
3344 return false;
3346 no_input_reloads_p = no_output_reloads_p = false;
3347 goal_alt_number = -1;
3348 change_p = sec_mem_p = false;
3349 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3350 reloads; neither are insns that SET cc0. Insns that use CC0 are
3351 not allowed to have any input reloads. */
3352 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3353 no_output_reloads_p = true;
3355 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3356 no_input_reloads_p = true;
3357 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3358 no_output_reloads_p = true;
3360 n_operands = curr_static_id->n_operands;
3361 n_alternatives = curr_static_id->n_alternatives;
3363 /* Just return "no reloads" if insn has no operands with
3364 constraints. */
3365 if (n_operands == 0 || n_alternatives == 0)
3366 return false;
3368 max_regno_before = max_reg_num ();
3370 for (i = 0; i < n_operands; i++)
3372 goal_alt_matched[i][0] = -1;
3373 goal_alt_matches[i] = -1;
3376 commutative = curr_static_id->commutative;
3378 /* Now see what we need for pseudos that didn't get hard regs or got
3379 the wrong kind of hard reg. For this, we must consider all the
3380 operands together against the register constraints. */
3382 best_losers = best_overall = INT_MAX;
3383 best_reload_sum = 0;
3385 curr_swapped = false;
3386 goal_alt_swapped = false;
3388 if (! check_only_p)
3389 /* Make equivalence substitution and memory subreg elimination
3390 before address processing because an address legitimacy can
3391 depend on memory mode. */
3392 for (i = 0; i < n_operands; i++)
3394 rtx op, subst, old;
3395 bool op_change_p = false;
3397 if (curr_static_id->operand[i].is_operator)
3398 continue;
3400 old = op = *curr_id->operand_loc[i];
3401 if (GET_CODE (old) == SUBREG)
3402 old = SUBREG_REG (old);
3403 subst = get_equiv_with_elimination (old, curr_insn);
3404 original_subreg_reg_mode[i] = VOIDmode;
3405 if (subst != old)
3407 subst = copy_rtx (subst);
3408 lra_assert (REG_P (old));
3409 if (GET_CODE (op) != SUBREG)
3410 *curr_id->operand_loc[i] = subst;
3411 else
3413 SUBREG_REG (op) = subst;
3414 if (GET_MODE (subst) == VOIDmode)
3415 original_subreg_reg_mode[i] = GET_MODE (old);
3417 if (lra_dump_file != NULL)
3419 fprintf (lra_dump_file,
3420 "Changing pseudo %d in operand %i of insn %u on equiv ",
3421 REGNO (old), i, INSN_UID (curr_insn));
3422 dump_value_slim (lra_dump_file, subst, 1);
3423 fprintf (lra_dump_file, "\n");
3425 op_change_p = change_p = true;
3427 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3429 change_p = true;
3430 lra_update_dup (curr_id, i);
3434 /* Reload address registers and displacements. We do it before
3435 finding an alternative because of memory constraints. */
3436 before = after = NULL;
3437 for (i = 0; i < n_operands; i++)
3438 if (! curr_static_id->operand[i].is_operator
3439 && process_address (i, check_only_p, &before, &after))
3441 if (check_only_p)
3442 return true;
3443 change_p = true;
3444 lra_update_dup (curr_id, i);
3447 if (change_p)
3448 /* If we've changed the instruction then any alternative that
3449 we chose previously may no longer be valid. */
3450 lra_set_used_insn_alternative (curr_insn, -1);
3452 if (! check_only_p && curr_insn_set != NULL_RTX
3453 && check_and_process_move (&change_p, &sec_mem_p))
3454 return change_p;
3456 try_swapped:
3458 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3459 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3460 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3461 reused_alternative_num, INSN_UID (curr_insn));
3463 if (process_alt_operands (reused_alternative_num))
3464 alt_p = true;
3466 if (check_only_p)
3467 return ! alt_p || best_losers != 0;
3469 /* If insn is commutative (it's safe to exchange a certain pair of
3470 operands) then we need to try each alternative twice, the second
3471 time matching those two operands as if we had exchanged them. To
3472 do this, really exchange them in operands.
3474 If we have just tried the alternatives the second time, return
3475 operands to normal and drop through. */
3477 if (reused_alternative_num < 0 && commutative >= 0)
3479 curr_swapped = !curr_swapped;
3480 if (curr_swapped)
3482 swap_operands (commutative);
3483 goto try_swapped;
3485 else
3486 swap_operands (commutative);
3489 if (! alt_p && ! sec_mem_p)
3491 /* No alternative works with reloads?? */
3492 if (INSN_CODE (curr_insn) >= 0)
3493 fatal_insn ("unable to generate reloads for:", curr_insn);
3494 error_for_asm (curr_insn,
3495 "inconsistent operand constraints in an %<asm%>");
3496 /* Avoid further trouble with this insn. */
3497 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3498 lra_invalidate_insn_data (curr_insn);
3499 return true;
3502 /* If the best alternative is with operands 1 and 2 swapped, swap
3503 them. Update the operand numbers of any reloads already
3504 pushed. */
3506 if (goal_alt_swapped)
3508 if (lra_dump_file != NULL)
3509 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3510 INSN_UID (curr_insn));
3512 /* Swap the duplicates too. */
3513 swap_operands (commutative);
3514 change_p = true;
3517 #ifdef SECONDARY_MEMORY_NEEDED
3518 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3519 too conservatively. So we use the secondary memory only if there
3520 is no any alternative without reloads. */
3521 use_sec_mem_p = false;
3522 if (! alt_p)
3523 use_sec_mem_p = true;
3524 else if (sec_mem_p)
3526 for (i = 0; i < n_operands; i++)
3527 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3528 break;
3529 use_sec_mem_p = i < n_operands;
3532 if (use_sec_mem_p)
3534 rtx new_reg, src, dest, rld;
3535 machine_mode sec_mode, rld_mode;
3537 lra_assert (sec_mem_p);
3538 lra_assert (curr_static_id->operand[0].type == OP_OUT
3539 && curr_static_id->operand[1].type == OP_IN);
3540 dest = *curr_id->operand_loc[0];
3541 src = *curr_id->operand_loc[1];
3542 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3543 ? dest : src);
3544 rld_mode = GET_MODE (rld);
3545 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3546 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3547 #else
3548 sec_mode = rld_mode;
3549 #endif
3550 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3551 NO_REGS, "secondary");
3552 /* If the mode is changed, it should be wider. */
3553 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3554 if (sec_mode != rld_mode)
3556 /* If the target says specifically to use another mode for
3557 secondary memory moves we can not reuse the original
3558 insn. */
3559 after = emit_spill_move (false, new_reg, dest);
3560 lra_process_new_insns (curr_insn, NULL, after,
3561 "Inserting the sec. move");
3562 /* We may have non null BEFORE here (e.g. after address
3563 processing. */
3564 push_to_sequence (before);
3565 before = emit_spill_move (true, new_reg, src);
3566 emit_insn (before);
3567 before = get_insns ();
3568 end_sequence ();
3569 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3570 lra_set_insn_deleted (curr_insn);
3572 else if (dest == rld)
3574 *curr_id->operand_loc[0] = new_reg;
3575 after = emit_spill_move (false, new_reg, dest);
3576 lra_process_new_insns (curr_insn, NULL, after,
3577 "Inserting the sec. move");
3579 else
3581 *curr_id->operand_loc[1] = new_reg;
3582 /* See comments above. */
3583 push_to_sequence (before);
3584 before = emit_spill_move (true, new_reg, src);
3585 emit_insn (before);
3586 before = get_insns ();
3587 end_sequence ();
3588 lra_process_new_insns (curr_insn, before, NULL,
3589 "Inserting the sec. move");
3591 lra_update_insn_regno_info (curr_insn);
3592 return true;
3594 #endif
3596 lra_assert (goal_alt_number >= 0);
3597 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3599 if (lra_dump_file != NULL)
3601 const char *p;
3603 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3604 goal_alt_number, INSN_UID (curr_insn));
3605 for (i = 0; i < n_operands; i++)
3607 p = (curr_static_id->operand_alternative
3608 [goal_alt_number * n_operands + i].constraint);
3609 if (*p == '\0')
3610 continue;
3611 fprintf (lra_dump_file, " (%d) ", i);
3612 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3613 fputc (*p, lra_dump_file);
3615 if (INSN_CODE (curr_insn) >= 0
3616 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3617 fprintf (lra_dump_file, " {%s}", p);
3618 if (curr_id->sp_offset != 0)
3619 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3620 curr_id->sp_offset);
3621 fprintf (lra_dump_file, "\n");
3624 /* Right now, for any pair of operands I and J that are required to
3625 match, with J < I, goal_alt_matches[I] is J. Add I to
3626 goal_alt_matched[J]. */
3628 for (i = 0; i < n_operands; i++)
3629 if ((j = goal_alt_matches[i]) >= 0)
3631 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3633 /* We allow matching one output operand and several input
3634 operands. */
3635 lra_assert (k == 0
3636 || (curr_static_id->operand[j].type == OP_OUT
3637 && curr_static_id->operand[i].type == OP_IN
3638 && (curr_static_id->operand
3639 [goal_alt_matched[j][0]].type == OP_IN)));
3640 goal_alt_matched[j][k] = i;
3641 goal_alt_matched[j][k + 1] = -1;
3644 for (i = 0; i < n_operands; i++)
3645 goal_alt_win[i] |= goal_alt_match_win[i];
3647 /* Any constants that aren't allowed and can't be reloaded into
3648 registers are here changed into memory references. */
3649 for (i = 0; i < n_operands; i++)
3650 if (goal_alt_win[i])
3652 int regno;
3653 enum reg_class new_class;
3654 rtx reg = *curr_id->operand_loc[i];
3656 if (GET_CODE (reg) == SUBREG)
3657 reg = SUBREG_REG (reg);
3659 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3661 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3663 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3665 lra_assert (ok_p);
3666 lra_change_class (regno, new_class, " Change to", true);
3670 else
3672 const char *constraint;
3673 char c;
3674 rtx op = *curr_id->operand_loc[i];
3675 rtx subreg = NULL_RTX;
3676 machine_mode mode = curr_operand_mode[i];
3678 if (GET_CODE (op) == SUBREG)
3680 subreg = op;
3681 op = SUBREG_REG (op);
3682 mode = GET_MODE (op);
3685 if (CONST_POOL_OK_P (mode, op)
3686 && ((targetm.preferred_reload_class
3687 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3688 || no_input_reloads_p))
3690 rtx tem = force_const_mem (mode, op);
3692 change_p = true;
3693 if (subreg != NULL_RTX)
3694 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3696 *curr_id->operand_loc[i] = tem;
3697 lra_update_dup (curr_id, i);
3698 process_address (i, false, &before, &after);
3700 /* If the alternative accepts constant pool refs directly
3701 there will be no reload needed at all. */
3702 if (subreg != NULL_RTX)
3703 continue;
3704 /* Skip alternatives before the one requested. */
3705 constraint = (curr_static_id->operand_alternative
3706 [goal_alt_number * n_operands + i].constraint);
3707 for (;
3708 (c = *constraint) && c != ',' && c != '#';
3709 constraint += CONSTRAINT_LEN (c, constraint))
3711 enum constraint_num cn = lookup_constraint (constraint);
3712 if ((insn_extra_memory_constraint (cn)
3713 || insn_extra_special_memory_constraint (cn))
3714 && satisfies_memory_constraint_p (tem, cn))
3715 break;
3717 if (c == '\0' || c == ',' || c == '#')
3718 continue;
3720 goal_alt_win[i] = true;
3724 for (i = 0; i < n_operands; i++)
3726 int regno;
3727 bool optional_p = false;
3728 rtx old, new_reg;
3729 rtx op = *curr_id->operand_loc[i];
3731 if (goal_alt_win[i])
3733 if (goal_alt[i] == NO_REGS
3734 && REG_P (op)
3735 /* When we assign NO_REGS it means that we will not
3736 assign a hard register to the scratch pseudo by
3737 assigment pass and the scratch pseudo will be
3738 spilled. Spilled scratch pseudos are transformed
3739 back to scratches at the LRA end. */
3740 && lra_former_scratch_operand_p (curr_insn, i)
3741 && lra_former_scratch_p (REGNO (op)))
3743 int regno = REGNO (op);
3744 lra_change_class (regno, NO_REGS, " Change to", true);
3745 if (lra_get_regno_hard_regno (regno) >= 0)
3746 /* We don't have to mark all insn affected by the
3747 spilled pseudo as there is only one such insn, the
3748 current one. */
3749 reg_renumber[regno] = -1;
3750 lra_assert (bitmap_single_bit_set_p
3751 (&lra_reg_info[REGNO (op)].insn_bitmap));
3753 /* We can do an optional reload. If the pseudo got a hard
3754 reg, we might improve the code through inheritance. If
3755 it does not get a hard register we coalesce memory/memory
3756 moves later. Ignore move insns to avoid cycling. */
3757 if (! lra_simple_p
3758 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3759 && goal_alt[i] != NO_REGS && REG_P (op)
3760 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3761 && regno < new_regno_start
3762 && ! lra_former_scratch_p (regno)
3763 && reg_renumber[regno] < 0
3764 /* Check that the optional reload pseudo will be able to
3765 hold given mode value. */
3766 && ! (prohibited_class_reg_set_mode_p
3767 (goal_alt[i], reg_class_contents[goal_alt[i]],
3768 PSEUDO_REGNO_MODE (regno)))
3769 && (curr_insn_set == NULL_RTX
3770 || !((REG_P (SET_SRC (curr_insn_set))
3771 || MEM_P (SET_SRC (curr_insn_set))
3772 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
3773 && (REG_P (SET_DEST (curr_insn_set))
3774 || MEM_P (SET_DEST (curr_insn_set))
3775 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
3776 optional_p = true;
3777 else
3778 continue;
3781 /* Operands that match previous ones have already been handled. */
3782 if (goal_alt_matches[i] >= 0)
3783 continue;
3785 /* We should not have an operand with a non-offsettable address
3786 appearing where an offsettable address will do. It also may
3787 be a case when the address should be special in other words
3788 not a general one (e.g. it needs no index reg). */
3789 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
3791 enum reg_class rclass;
3792 rtx *loc = &XEXP (op, 0);
3793 enum rtx_code code = GET_CODE (*loc);
3795 push_to_sequence (before);
3796 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
3797 MEM, SCRATCH);
3798 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
3799 new_reg = emit_inc (rclass, *loc, *loc,
3800 /* This value does not matter for MODIFY. */
3801 GET_MODE_SIZE (GET_MODE (op)));
3802 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
3803 "offsetable address", &new_reg))
3804 lra_emit_move (new_reg, *loc);
3805 before = get_insns ();
3806 end_sequence ();
3807 *loc = new_reg;
3808 lra_update_dup (curr_id, i);
3810 else if (goal_alt_matched[i][0] == -1)
3812 machine_mode mode;
3813 rtx reg, *loc;
3814 int hard_regno, byte;
3815 enum op_type type = curr_static_id->operand[i].type;
3817 loc = curr_id->operand_loc[i];
3818 mode = curr_operand_mode[i];
3819 if (GET_CODE (*loc) == SUBREG)
3821 reg = SUBREG_REG (*loc);
3822 byte = SUBREG_BYTE (*loc);
3823 if (REG_P (reg)
3824 /* Strict_low_part requires reload the register not
3825 the sub-register. */
3826 && (curr_static_id->operand[i].strict_low
3827 || (GET_MODE_SIZE (mode)
3828 <= GET_MODE_SIZE (GET_MODE (reg))
3829 && (hard_regno
3830 = get_try_hard_regno (REGNO (reg))) >= 0
3831 && (simplify_subreg_regno
3832 (hard_regno,
3833 GET_MODE (reg), byte, mode) < 0)
3834 && (goal_alt[i] == NO_REGS
3835 || (simplify_subreg_regno
3836 (ira_class_hard_regs[goal_alt[i]][0],
3837 GET_MODE (reg), byte, mode) >= 0)))))
3839 if (type == OP_OUT)
3840 type = OP_INOUT;
3841 loc = &SUBREG_REG (*loc);
3842 mode = GET_MODE (*loc);
3845 old = *loc;
3846 if (get_reload_reg (type, mode, old, goal_alt[i],
3847 loc != curr_id->operand_loc[i], "", &new_reg)
3848 && type != OP_OUT)
3850 push_to_sequence (before);
3851 lra_emit_move (new_reg, old);
3852 before = get_insns ();
3853 end_sequence ();
3855 *loc = new_reg;
3856 if (type != OP_IN
3857 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
3859 start_sequence ();
3860 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
3861 emit_insn (after);
3862 after = get_insns ();
3863 end_sequence ();
3864 *loc = new_reg;
3866 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
3867 if (goal_alt_dont_inherit_ops[j] == i)
3869 lra_set_regno_unique_value (REGNO (new_reg));
3870 break;
3872 lra_update_dup (curr_id, i);
3874 else if (curr_static_id->operand[i].type == OP_IN
3875 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3876 == OP_OUT))
3878 /* generate reloads for input and matched outputs. */
3879 match_inputs[0] = i;
3880 match_inputs[1] = -1;
3881 match_reload (goal_alt_matched[i][0], match_inputs,
3882 goal_alt[i], &before, &after,
3883 curr_static_id->operand_alternative
3884 [goal_alt_number * n_operands + goal_alt_matched[i][0]]
3885 .earlyclobber);
3887 else if (curr_static_id->operand[i].type == OP_OUT
3888 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3889 == OP_IN))
3890 /* Generate reloads for output and matched inputs. */
3891 match_reload (i, goal_alt_matched[i], goal_alt[i], &before, &after,
3892 curr_static_id->operand_alternative
3893 [goal_alt_number * n_operands + i].earlyclobber);
3894 else if (curr_static_id->operand[i].type == OP_IN
3895 && (curr_static_id->operand[goal_alt_matched[i][0]].type
3896 == OP_IN))
3898 /* Generate reloads for matched inputs. */
3899 match_inputs[0] = i;
3900 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
3901 match_inputs[j + 1] = k;
3902 match_inputs[j + 1] = -1;
3903 match_reload (-1, match_inputs, goal_alt[i], &before, &after, false);
3905 else
3906 /* We must generate code in any case when function
3907 process_alt_operands decides that it is possible. */
3908 gcc_unreachable ();
3909 if (optional_p)
3911 lra_assert (REG_P (op));
3912 regno = REGNO (op);
3913 op = *curr_id->operand_loc[i]; /* Substitution. */
3914 if (GET_CODE (op) == SUBREG)
3915 op = SUBREG_REG (op);
3916 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
3917 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
3918 lra_reg_info[REGNO (op)].restore_regno = regno;
3919 if (lra_dump_file != NULL)
3920 fprintf (lra_dump_file,
3921 " Making reload reg %d for reg %d optional\n",
3922 REGNO (op), regno);
3925 if (before != NULL_RTX || after != NULL_RTX
3926 || max_regno_before != max_reg_num ())
3927 change_p = true;
3928 if (change_p)
3930 lra_update_operator_dups (curr_id);
3931 /* Something changes -- process the insn. */
3932 lra_update_insn_regno_info (curr_insn);
3934 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
3935 return change_p;
3938 /* Return true if INSN satisfies all constraints. In other words, no
3939 reload insns are needed. */
3940 bool
3941 lra_constrain_insn (rtx_insn *insn)
3943 int saved_new_regno_start = new_regno_start;
3944 int saved_new_insn_uid_start = new_insn_uid_start;
3945 bool change_p;
3947 curr_insn = insn;
3948 curr_id = lra_get_insn_recog_data (curr_insn);
3949 curr_static_id = curr_id->insn_static_data;
3950 new_insn_uid_start = get_max_uid ();
3951 new_regno_start = max_reg_num ();
3952 change_p = curr_insn_transform (true);
3953 new_regno_start = saved_new_regno_start;
3954 new_insn_uid_start = saved_new_insn_uid_start;
3955 return ! change_p;
3958 /* Return true if X is in LIST. */
3959 static bool
3960 in_list_p (rtx x, rtx list)
3962 for (; list != NULL_RTX; list = XEXP (list, 1))
3963 if (XEXP (list, 0) == x)
3964 return true;
3965 return false;
3968 /* Return true if X contains an allocatable hard register (if
3969 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
3970 static bool
3971 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
3973 int i, j;
3974 const char *fmt;
3975 enum rtx_code code;
3977 code = GET_CODE (x);
3978 if (REG_P (x))
3980 int regno = REGNO (x);
3981 HARD_REG_SET alloc_regs;
3983 if (hard_reg_p)
3985 if (regno >= FIRST_PSEUDO_REGISTER)
3986 regno = lra_get_regno_hard_regno (regno);
3987 if (regno < 0)
3988 return false;
3989 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
3990 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
3992 else
3994 if (regno < FIRST_PSEUDO_REGISTER)
3995 return false;
3996 if (! spilled_p)
3997 return true;
3998 return lra_get_regno_hard_regno (regno) < 0;
4001 fmt = GET_RTX_FORMAT (code);
4002 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4004 if (fmt[i] == 'e')
4006 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
4007 return true;
4009 else if (fmt[i] == 'E')
4011 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4012 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4013 return true;
4016 return false;
4019 /* Process all regs in location *LOC and change them on equivalent
4020 substitution. Return true if any change was done. */
4021 static bool
4022 loc_equivalence_change_p (rtx *loc)
4024 rtx subst, reg, x = *loc;
4025 bool result = false;
4026 enum rtx_code code = GET_CODE (x);
4027 const char *fmt;
4028 int i, j;
4030 if (code == SUBREG)
4032 reg = SUBREG_REG (x);
4033 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4034 && GET_MODE (subst) == VOIDmode)
4036 /* We cannot reload debug location. Simplify subreg here
4037 while we know the inner mode. */
4038 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4039 GET_MODE (reg), SUBREG_BYTE (x));
4040 return true;
4043 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4045 *loc = subst;
4046 return true;
4049 /* Scan all the operand sub-expressions. */
4050 fmt = GET_RTX_FORMAT (code);
4051 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4053 if (fmt[i] == 'e')
4054 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4055 else if (fmt[i] == 'E')
4056 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4057 result
4058 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4060 return result;
4063 /* Similar to loc_equivalence_change_p, but for use as
4064 simplify_replace_fn_rtx callback. DATA is insn for which the
4065 elimination is done. If it null we don't do the elimination. */
4066 static rtx
4067 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4069 if (!REG_P (loc))
4070 return NULL_RTX;
4072 rtx subst = (data == NULL
4073 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4074 if (subst != loc)
4075 return subst;
4077 return NULL_RTX;
4080 /* Maximum number of generated reload insns per an insn. It is for
4081 preventing this pass cycling in a bug case. */
4082 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4084 /* The current iteration number of this LRA pass. */
4085 int lra_constraint_iter;
4087 /* True if we substituted equiv which needs checking register
4088 allocation correctness because the equivalent value contains
4089 allocatable hard registers or when we restore multi-register
4090 pseudo. */
4091 bool lra_risky_transformations_p;
4093 /* Return true if REGNO is referenced in more than one block. */
4094 static bool
4095 multi_block_pseudo_p (int regno)
4097 basic_block bb = NULL;
4098 unsigned int uid;
4099 bitmap_iterator bi;
4101 if (regno < FIRST_PSEUDO_REGISTER)
4102 return false;
4104 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4105 if (bb == NULL)
4106 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4107 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4108 return true;
4109 return false;
4112 /* Return true if LIST contains a deleted insn. */
4113 static bool
4114 contains_deleted_insn_p (rtx_insn_list *list)
4116 for (; list != NULL_RTX; list = list->next ())
4117 if (NOTE_P (list->insn ())
4118 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4119 return true;
4120 return false;
4123 /* Return true if X contains a pseudo dying in INSN. */
4124 static bool
4125 dead_pseudo_p (rtx x, rtx_insn *insn)
4127 int i, j;
4128 const char *fmt;
4129 enum rtx_code code;
4131 if (REG_P (x))
4132 return (insn != NULL_RTX
4133 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4134 code = GET_CODE (x);
4135 fmt = GET_RTX_FORMAT (code);
4136 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4138 if (fmt[i] == 'e')
4140 if (dead_pseudo_p (XEXP (x, i), insn))
4141 return true;
4143 else if (fmt[i] == 'E')
4145 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4146 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4147 return true;
4150 return false;
4153 /* Return true if INSN contains a dying pseudo in INSN right hand
4154 side. */
4155 static bool
4156 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4158 rtx set = single_set (insn);
4160 gcc_assert (set != NULL);
4161 return dead_pseudo_p (SET_SRC (set), insn);
4164 /* Return true if any init insn of REGNO contains a dying pseudo in
4165 insn right hand side. */
4166 static bool
4167 init_insn_rhs_dead_pseudo_p (int regno)
4169 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4171 if (insns == NULL)
4172 return false;
4173 for (; insns != NULL_RTX; insns = insns->next ())
4174 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4175 return true;
4176 return false;
4179 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4180 reverse only if we have one init insn with given REGNO as a
4181 source. */
4182 static bool
4183 reverse_equiv_p (int regno)
4185 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4186 rtx set;
4188 if (insns == NULL)
4189 return false;
4190 if (! INSN_P (insns->insn ())
4191 || insns->next () != NULL)
4192 return false;
4193 if ((set = single_set (insns->insn ())) == NULL_RTX)
4194 return false;
4195 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4198 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4199 call this function only for non-reverse equivalence. */
4200 static bool
4201 contains_reloaded_insn_p (int regno)
4203 rtx set;
4204 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4206 for (; list != NULL; list = list->next ())
4207 if ((set = single_set (list->insn ())) == NULL_RTX
4208 || ! REG_P (SET_DEST (set))
4209 || (int) REGNO (SET_DEST (set)) != regno)
4210 return true;
4211 return false;
4214 /* Entry function of LRA constraint pass. Return true if the
4215 constraint pass did change the code. */
4216 bool
4217 lra_constraints (bool first_p)
4219 bool changed_p;
4220 int i, hard_regno, new_insns_num;
4221 unsigned int min_len, new_min_len, uid;
4222 rtx set, x, reg, dest_reg;
4223 basic_block last_bb;
4224 bitmap_head equiv_insn_bitmap;
4225 bitmap_iterator bi;
4227 lra_constraint_iter++;
4228 if (lra_dump_file != NULL)
4229 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4230 lra_constraint_iter);
4231 changed_p = false;
4232 if (pic_offset_table_rtx
4233 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4234 lra_risky_transformations_p = true;
4235 else
4236 lra_risky_transformations_p = false;
4237 new_insn_uid_start = get_max_uid ();
4238 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4239 /* Mark used hard regs for target stack size calulations. */
4240 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4241 if (lra_reg_info[i].nrefs != 0
4242 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4244 int j, nregs;
4246 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4247 for (j = 0; j < nregs; j++)
4248 df_set_regs_ever_live (hard_regno + j, true);
4250 /* Do elimination before the equivalence processing as we can spill
4251 some pseudos during elimination. */
4252 lra_eliminate (false, first_p);
4253 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4254 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4255 if (lra_reg_info[i].nrefs != 0)
4257 ira_reg_equiv[i].profitable_p = true;
4258 reg = regno_reg_rtx[i];
4259 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4261 bool pseudo_p = contains_reg_p (x, false, false);
4263 /* After RTL transformation, we can not guarantee that
4264 pseudo in the substitution was not reloaded which might
4265 make equivalence invalid. For example, in reverse
4266 equiv of p0
4268 p0 <- ...
4270 equiv_mem <- p0
4272 the memory address register was reloaded before the 2nd
4273 insn. */
4274 if ((! first_p && pseudo_p)
4275 /* We don't use DF for compilation speed sake. So it
4276 is problematic to update live info when we use an
4277 equivalence containing pseudos in more than one
4278 BB. */
4279 || (pseudo_p && multi_block_pseudo_p (i))
4280 /* If an init insn was deleted for some reason, cancel
4281 the equiv. We could update the equiv insns after
4282 transformations including an equiv insn deletion
4283 but it is not worthy as such cases are extremely
4284 rare. */
4285 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4286 /* If it is not a reverse equivalence, we check that a
4287 pseudo in rhs of the init insn is not dying in the
4288 insn. Otherwise, the live info at the beginning of
4289 the corresponding BB might be wrong after we
4290 removed the insn. When the equiv can be a
4291 constant, the right hand side of the init insn can
4292 be a pseudo. */
4293 || (! reverse_equiv_p (i)
4294 && (init_insn_rhs_dead_pseudo_p (i)
4295 /* If we reloaded the pseudo in an equivalence
4296 init insn, we can not remove the equiv init
4297 insns and the init insns might write into
4298 const memory in this case. */
4299 || contains_reloaded_insn_p (i)))
4300 /* Prevent access beyond equivalent memory for
4301 paradoxical subregs. */
4302 || (MEM_P (x)
4303 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4304 > GET_MODE_SIZE (GET_MODE (x))))
4305 || (pic_offset_table_rtx
4306 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4307 && (targetm.preferred_reload_class
4308 (x, lra_get_allocno_class (i)) == NO_REGS))
4309 || contains_symbol_ref_p (x))))
4310 ira_reg_equiv[i].defined_p = false;
4311 if (contains_reg_p (x, false, true))
4312 ira_reg_equiv[i].profitable_p = false;
4313 if (get_equiv (reg) != reg)
4314 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4317 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4318 update_equiv (i);
4319 /* We should add all insns containing pseudos which should be
4320 substituted by their equivalences. */
4321 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4322 lra_push_insn_by_uid (uid);
4323 min_len = lra_insn_stack_length ();
4324 new_insns_num = 0;
4325 last_bb = NULL;
4326 changed_p = false;
4327 while ((new_min_len = lra_insn_stack_length ()) != 0)
4329 curr_insn = lra_pop_insn ();
4330 --new_min_len;
4331 curr_bb = BLOCK_FOR_INSN (curr_insn);
4332 if (curr_bb != last_bb)
4334 last_bb = curr_bb;
4335 bb_reload_num = lra_curr_reload_num;
4337 if (min_len > new_min_len)
4339 min_len = new_min_len;
4340 new_insns_num = 0;
4342 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4343 internal_error
4344 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4345 MAX_RELOAD_INSNS_NUMBER);
4346 new_insns_num++;
4347 if (DEBUG_INSN_P (curr_insn))
4349 /* We need to check equivalence in debug insn and change
4350 pseudo to the equivalent value if necessary. */
4351 curr_id = lra_get_insn_recog_data (curr_insn);
4352 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4354 rtx old = *curr_id->operand_loc[0];
4355 *curr_id->operand_loc[0]
4356 = simplify_replace_fn_rtx (old, NULL_RTX,
4357 loc_equivalence_callback, curr_insn);
4358 if (old != *curr_id->operand_loc[0])
4360 lra_update_insn_regno_info (curr_insn);
4361 changed_p = true;
4365 else if (INSN_P (curr_insn))
4367 if ((set = single_set (curr_insn)) != NULL_RTX)
4369 dest_reg = SET_DEST (set);
4370 /* The equivalence pseudo could be set up as SUBREG in a
4371 case when it is a call restore insn in a mode
4372 different from the pseudo mode. */
4373 if (GET_CODE (dest_reg) == SUBREG)
4374 dest_reg = SUBREG_REG (dest_reg);
4375 if ((REG_P (dest_reg)
4376 && (x = get_equiv (dest_reg)) != dest_reg
4377 /* Remove insns which set up a pseudo whose value
4378 can not be changed. Such insns might be not in
4379 init_insns because we don't update equiv data
4380 during insn transformations.
4382 As an example, let suppose that a pseudo got
4383 hard register and on the 1st pass was not
4384 changed to equivalent constant. We generate an
4385 additional insn setting up the pseudo because of
4386 secondary memory movement. Then the pseudo is
4387 spilled and we use the equiv constant. In this
4388 case we should remove the additional insn and
4389 this insn is not init_insns list. */
4390 && (! MEM_P (x) || MEM_READONLY_P (x)
4391 /* Check that this is actually an insn setting
4392 up the equivalence. */
4393 || in_list_p (curr_insn,
4394 ira_reg_equiv
4395 [REGNO (dest_reg)].init_insns)))
4396 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4397 && in_list_p (curr_insn,
4398 ira_reg_equiv
4399 [REGNO (SET_SRC (set))].init_insns)))
4401 /* This is equiv init insn of pseudo which did not get a
4402 hard register -- remove the insn. */
4403 if (lra_dump_file != NULL)
4405 fprintf (lra_dump_file,
4406 " Removing equiv init insn %i (freq=%d)\n",
4407 INSN_UID (curr_insn),
4408 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4409 dump_insn_slim (lra_dump_file, curr_insn);
4411 if (contains_reg_p (x, true, false))
4412 lra_risky_transformations_p = true;
4413 lra_set_insn_deleted (curr_insn);
4414 continue;
4417 curr_id = lra_get_insn_recog_data (curr_insn);
4418 curr_static_id = curr_id->insn_static_data;
4419 init_curr_insn_input_reloads ();
4420 init_curr_operand_mode ();
4421 if (curr_insn_transform (false))
4422 changed_p = true;
4423 /* Check non-transformed insns too for equiv change as USE
4424 or CLOBBER don't need reloads but can contain pseudos
4425 being changed on their equivalences. */
4426 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4427 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4429 lra_update_insn_regno_info (curr_insn);
4430 changed_p = true;
4434 bitmap_clear (&equiv_insn_bitmap);
4435 /* If we used a new hard regno, changed_p should be true because the
4436 hard reg is assigned to a new pseudo. */
4437 if (flag_checking && !changed_p)
4439 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4440 if (lra_reg_info[i].nrefs != 0
4441 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4443 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4445 for (j = 0; j < nregs; j++)
4446 lra_assert (df_regs_ever_live_p (hard_regno + j));
4449 return changed_p;
4452 /* Initiate the LRA constraint pass. It is done once per
4453 function. */
4454 void
4455 lra_constraints_init (void)
4459 /* Finalize the LRA constraint pass. It is done once per
4460 function. */
4461 void
4462 lra_constraints_finish (void)
4468 /* This page contains code to do inheritance/split
4469 transformations. */
4471 /* Number of reloads passed so far in current EBB. */
4472 static int reloads_num;
4474 /* Number of calls passed so far in current EBB. */
4475 static int calls_num;
4477 /* Current reload pseudo check for validity of elements in
4478 USAGE_INSNS. */
4479 static int curr_usage_insns_check;
4481 /* Info about last usage of registers in EBB to do inheritance/split
4482 transformation. Inheritance transformation is done from a spilled
4483 pseudo and split transformations from a hard register or a pseudo
4484 assigned to a hard register. */
4485 struct usage_insns
4487 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4488 value INSNS is valid. The insns is chain of optional debug insns
4489 and a finishing non-debug insn using the corresponding reg. The
4490 value is also used to mark the registers which are set up in the
4491 current insn. The negated insn uid is used for this. */
4492 int check;
4493 /* Value of global reloads_num at the last insn in INSNS. */
4494 int reloads_num;
4495 /* Value of global reloads_nums at the last insn in INSNS. */
4496 int calls_num;
4497 /* It can be true only for splitting. And it means that the restore
4498 insn should be put after insn given by the following member. */
4499 bool after_p;
4500 /* Next insns in the current EBB which use the original reg and the
4501 original reg value is not changed between the current insn and
4502 the next insns. In order words, e.g. for inheritance, if we need
4503 to use the original reg value again in the next insns we can try
4504 to use the value in a hard register from a reload insn of the
4505 current insn. */
4506 rtx insns;
4509 /* Map: regno -> corresponding pseudo usage insns. */
4510 static struct usage_insns *usage_insns;
4512 static void
4513 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4515 usage_insns[regno].check = curr_usage_insns_check;
4516 usage_insns[regno].insns = insn;
4517 usage_insns[regno].reloads_num = reloads_num;
4518 usage_insns[regno].calls_num = calls_num;
4519 usage_insns[regno].after_p = after_p;
4522 /* The function is used to form list REGNO usages which consists of
4523 optional debug insns finished by a non-debug insn using REGNO.
4524 RELOADS_NUM is current number of reload insns processed so far. */
4525 static void
4526 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
4528 rtx next_usage_insns;
4530 if (usage_insns[regno].check == curr_usage_insns_check
4531 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4532 && DEBUG_INSN_P (insn))
4534 /* Check that we did not add the debug insn yet. */
4535 if (next_usage_insns != insn
4536 && (GET_CODE (next_usage_insns) != INSN_LIST
4537 || XEXP (next_usage_insns, 0) != insn))
4538 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4539 next_usage_insns);
4541 else if (NONDEBUG_INSN_P (insn))
4542 setup_next_usage_insn (regno, insn, reloads_num, false);
4543 else
4544 usage_insns[regno].check = 0;
4547 /* Return first non-debug insn in list USAGE_INSNS. */
4548 static rtx_insn *
4549 skip_usage_debug_insns (rtx usage_insns)
4551 rtx insn;
4553 /* Skip debug insns. */
4554 for (insn = usage_insns;
4555 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4556 insn = XEXP (insn, 1))
4558 return safe_as_a <rtx_insn *> (insn);
4561 /* Return true if we need secondary memory moves for insn in
4562 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4563 into the insn. */
4564 static bool
4565 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4566 rtx usage_insns ATTRIBUTE_UNUSED)
4568 #ifndef SECONDARY_MEMORY_NEEDED
4569 return false;
4570 #else
4571 rtx_insn *insn;
4572 rtx set, dest;
4573 enum reg_class cl;
4575 if (inher_cl == ALL_REGS
4576 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4577 return false;
4578 lra_assert (INSN_P (insn));
4579 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4580 return false;
4581 dest = SET_DEST (set);
4582 if (! REG_P (dest))
4583 return false;
4584 lra_assert (inher_cl != NO_REGS);
4585 cl = get_reg_class (REGNO (dest));
4586 return (cl != NO_REGS && cl != ALL_REGS
4587 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4588 #endif
4591 /* Registers involved in inheritance/split in the current EBB
4592 (inheritance/split pseudos and original registers). */
4593 static bitmap_head check_only_regs;
4595 /* Do inheritance transformations for insn INSN, which defines (if
4596 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4597 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4598 form as the "insns" field of usage_insns. Return true if we
4599 succeed in such transformation.
4601 The transformations look like:
4603 p <- ... i <- ...
4604 ... p <- i (new insn)
4605 ... =>
4606 <- ... p ... <- ... i ...
4608 ... i <- p (new insn)
4609 <- ... p ... <- ... i ...
4610 ... =>
4611 <- ... p ... <- ... i ...
4612 where p is a spilled original pseudo and i is a new inheritance pseudo.
4615 The inheritance pseudo has the smallest class of two classes CL and
4616 class of ORIGINAL REGNO. */
4617 static bool
4618 inherit_reload_reg (bool def_p, int original_regno,
4619 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4621 if (optimize_function_for_size_p (cfun))
4622 return false;
4624 enum reg_class rclass = lra_get_allocno_class (original_regno);
4625 rtx original_reg = regno_reg_rtx[original_regno];
4626 rtx new_reg, usage_insn;
4627 rtx_insn *new_insns;
4629 lra_assert (! usage_insns[original_regno].after_p);
4630 if (lra_dump_file != NULL)
4631 fprintf (lra_dump_file,
4632 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4633 if (! ira_reg_classes_intersect_p[cl][rclass])
4635 if (lra_dump_file != NULL)
4637 fprintf (lra_dump_file,
4638 " Rejecting inheritance for %d "
4639 "because of disjoint classes %s and %s\n",
4640 original_regno, reg_class_names[cl],
4641 reg_class_names[rclass]);
4642 fprintf (lra_dump_file,
4643 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4645 return false;
4647 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4648 /* We don't use a subset of two classes because it can be
4649 NO_REGS. This transformation is still profitable in most
4650 cases even if the classes are not intersected as register
4651 move is probably cheaper than a memory load. */
4652 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
4654 if (lra_dump_file != NULL)
4655 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
4656 reg_class_names[cl], reg_class_names[rclass]);
4658 rclass = cl;
4660 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
4662 /* Reject inheritance resulting in secondary memory moves.
4663 Otherwise, there is a danger in LRA cycling. Also such
4664 transformation will be unprofitable. */
4665 if (lra_dump_file != NULL)
4667 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
4668 rtx set = single_set (insn);
4670 lra_assert (set != NULL_RTX);
4672 rtx dest = SET_DEST (set);
4674 lra_assert (REG_P (dest));
4675 fprintf (lra_dump_file,
4676 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
4677 "as secondary mem is needed\n",
4678 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
4679 original_regno, reg_class_names[rclass]);
4680 fprintf (lra_dump_file,
4681 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4683 return false;
4685 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4686 rclass, "inheritance");
4687 start_sequence ();
4688 if (def_p)
4689 lra_emit_move (original_reg, new_reg);
4690 else
4691 lra_emit_move (new_reg, original_reg);
4692 new_insns = get_insns ();
4693 end_sequence ();
4694 if (NEXT_INSN (new_insns) != NULL_RTX)
4696 if (lra_dump_file != NULL)
4698 fprintf (lra_dump_file,
4699 " Rejecting inheritance %d->%d "
4700 "as it results in 2 or more insns:\n",
4701 original_regno, REGNO (new_reg));
4702 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
4703 fprintf (lra_dump_file,
4704 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4706 return false;
4708 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
4709 lra_update_insn_regno_info (insn);
4710 if (! def_p)
4711 /* We now have a new usage insn for original regno. */
4712 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
4713 if (lra_dump_file != NULL)
4714 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
4715 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
4716 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4717 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4718 bitmap_set_bit (&check_only_regs, original_regno);
4719 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
4720 if (def_p)
4721 lra_process_new_insns (insn, NULL, new_insns,
4722 "Add original<-inheritance");
4723 else
4724 lra_process_new_insns (insn, new_insns, NULL,
4725 "Add inheritance<-original");
4726 while (next_usage_insns != NULL_RTX)
4728 if (GET_CODE (next_usage_insns) != INSN_LIST)
4730 usage_insn = next_usage_insns;
4731 lra_assert (NONDEBUG_INSN_P (usage_insn));
4732 next_usage_insns = NULL;
4734 else
4736 usage_insn = XEXP (next_usage_insns, 0);
4737 lra_assert (DEBUG_INSN_P (usage_insn));
4738 next_usage_insns = XEXP (next_usage_insns, 1);
4740 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
4741 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
4742 if (lra_dump_file != NULL)
4744 fprintf (lra_dump_file,
4745 " Inheritance reuse change %d->%d (bb%d):\n",
4746 original_regno, REGNO (new_reg),
4747 BLOCK_FOR_INSN (usage_insn)->index);
4748 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
4751 if (lra_dump_file != NULL)
4752 fprintf (lra_dump_file,
4753 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4754 return true;
4757 /* Return true if we need a caller save/restore for pseudo REGNO which
4758 was assigned to a hard register. */
4759 static inline bool
4760 need_for_call_save_p (int regno)
4762 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
4763 return (usage_insns[regno].calls_num < calls_num
4764 && (overlaps_hard_reg_set_p
4765 ((flag_ipa_ra &&
4766 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
4767 ? lra_reg_info[regno].actual_call_used_reg_set
4768 : call_used_reg_set,
4769 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
4770 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
4771 PSEUDO_REGNO_MODE (regno))));
4774 /* Global registers occurring in the current EBB. */
4775 static bitmap_head ebb_global_regs;
4777 /* Return true if we need a split for hard register REGNO or pseudo
4778 REGNO which was assigned to a hard register.
4779 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
4780 used for reloads since the EBB end. It is an approximation of the
4781 used hard registers in the split range. The exact value would
4782 require expensive calculations. If we were aggressive with
4783 splitting because of the approximation, the split pseudo will save
4784 the same hard register assignment and will be removed in the undo
4785 pass. We still need the approximation because too aggressive
4786 splitting would result in too inaccurate cost calculation in the
4787 assignment pass because of too many generated moves which will be
4788 probably removed in the undo pass. */
4789 static inline bool
4790 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
4792 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
4794 lra_assert (hard_regno >= 0);
4795 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
4796 /* Don't split eliminable hard registers, otherwise we can
4797 split hard registers like hard frame pointer, which
4798 lives on BB start/end according to DF-infrastructure,
4799 when there is a pseudo assigned to the register and
4800 living in the same BB. */
4801 && (regno >= FIRST_PSEUDO_REGISTER
4802 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
4803 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
4804 /* Don't split call clobbered hard regs living through
4805 calls, otherwise we might have a check problem in the
4806 assign sub-pass as in the most cases (exception is a
4807 situation when lra_risky_transformations_p value is
4808 true) the assign pass assumes that all pseudos living
4809 through calls are assigned to call saved hard regs. */
4810 && (regno >= FIRST_PSEUDO_REGISTER
4811 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
4812 || usage_insns[regno].calls_num == calls_num)
4813 /* We need at least 2 reloads to make pseudo splitting
4814 profitable. We should provide hard regno splitting in
4815 any case to solve 1st insn scheduling problem when
4816 moving hard register definition up might result in
4817 impossibility to find hard register for reload pseudo of
4818 small register class. */
4819 && (usage_insns[regno].reloads_num
4820 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
4821 && (regno < FIRST_PSEUDO_REGISTER
4822 /* For short living pseudos, spilling + inheritance can
4823 be considered a substitution for splitting.
4824 Therefore we do not splitting for local pseudos. It
4825 decreases also aggressiveness of splitting. The
4826 minimal number of references is chosen taking into
4827 account that for 2 references splitting has no sense
4828 as we can just spill the pseudo. */
4829 || (regno >= FIRST_PSEUDO_REGISTER
4830 && lra_reg_info[regno].nrefs > 3
4831 && bitmap_bit_p (&ebb_global_regs, regno))))
4832 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
4835 /* Return class for the split pseudo created from original pseudo with
4836 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
4837 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
4838 results in no secondary memory movements. */
4839 static enum reg_class
4840 choose_split_class (enum reg_class allocno_class,
4841 int hard_regno ATTRIBUTE_UNUSED,
4842 machine_mode mode ATTRIBUTE_UNUSED)
4844 #ifndef SECONDARY_MEMORY_NEEDED
4845 return allocno_class;
4846 #else
4847 int i;
4848 enum reg_class cl, best_cl = NO_REGS;
4849 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
4850 = REGNO_REG_CLASS (hard_regno);
4852 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
4853 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
4854 return allocno_class;
4855 for (i = 0;
4856 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
4857 i++)
4858 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
4859 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
4860 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
4861 && (best_cl == NO_REGS
4862 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
4863 best_cl = cl;
4864 return best_cl;
4865 #endif
4868 /* Do split transformations for insn INSN, which defines or uses
4869 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
4870 the EBB next uses ORIGINAL_REGNO; it has the same form as the
4871 "insns" field of usage_insns.
4873 The transformations look like:
4875 p <- ... p <- ...
4876 ... s <- p (new insn -- save)
4877 ... =>
4878 ... p <- s (new insn -- restore)
4879 <- ... p ... <- ... p ...
4881 <- ... p ... <- ... p ...
4882 ... s <- p (new insn -- save)
4883 ... =>
4884 ... p <- s (new insn -- restore)
4885 <- ... p ... <- ... p ...
4887 where p is an original pseudo got a hard register or a hard
4888 register and s is a new split pseudo. The save is put before INSN
4889 if BEFORE_P is true. Return true if we succeed in such
4890 transformation. */
4891 static bool
4892 split_reg (bool before_p, int original_regno, rtx_insn *insn,
4893 rtx next_usage_insns)
4895 enum reg_class rclass;
4896 rtx original_reg;
4897 int hard_regno, nregs;
4898 rtx new_reg, usage_insn;
4899 rtx_insn *restore, *save;
4900 bool after_p;
4901 bool call_save_p;
4903 if (original_regno < FIRST_PSEUDO_REGISTER)
4905 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
4906 hard_regno = original_regno;
4907 call_save_p = false;
4908 nregs = 1;
4910 else
4912 hard_regno = reg_renumber[original_regno];
4913 nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (original_regno)];
4914 rclass = lra_get_allocno_class (original_regno);
4915 original_reg = regno_reg_rtx[original_regno];
4916 call_save_p = need_for_call_save_p (original_regno);
4918 original_reg = regno_reg_rtx[original_regno];
4919 lra_assert (hard_regno >= 0);
4920 if (lra_dump_file != NULL)
4921 fprintf (lra_dump_file,
4922 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
4923 if (call_save_p)
4925 machine_mode mode = GET_MODE (original_reg);
4927 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
4928 hard_regno_nregs[hard_regno][mode],
4929 mode);
4930 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
4932 else
4934 rclass = choose_split_class (rclass, hard_regno,
4935 GET_MODE (original_reg));
4936 if (rclass == NO_REGS)
4938 if (lra_dump_file != NULL)
4940 fprintf (lra_dump_file,
4941 " Rejecting split of %d(%s): "
4942 "no good reg class for %d(%s)\n",
4943 original_regno,
4944 reg_class_names[lra_get_allocno_class (original_regno)],
4945 hard_regno,
4946 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
4947 fprintf
4948 (lra_dump_file,
4949 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4951 return false;
4953 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
4954 rclass, "split");
4955 reg_renumber[REGNO (new_reg)] = hard_regno;
4957 save = emit_spill_move (true, new_reg, original_reg);
4958 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
4960 if (lra_dump_file != NULL)
4962 fprintf
4963 (lra_dump_file,
4964 " Rejecting split %d->%d resulting in > 2 save insns:\n",
4965 original_regno, REGNO (new_reg));
4966 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
4967 fprintf (lra_dump_file,
4968 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4970 return false;
4972 restore = emit_spill_move (false, new_reg, original_reg);
4973 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
4975 if (lra_dump_file != NULL)
4977 fprintf (lra_dump_file,
4978 " Rejecting split %d->%d "
4979 "resulting in > 2 restore insns:\n",
4980 original_regno, REGNO (new_reg));
4981 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
4982 fprintf (lra_dump_file,
4983 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
4985 return false;
4987 after_p = usage_insns[original_regno].after_p;
4988 lra_reg_info[REGNO (new_reg)].restore_regno = original_regno;
4989 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
4990 bitmap_set_bit (&check_only_regs, original_regno);
4991 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
4992 for (;;)
4994 if (GET_CODE (next_usage_insns) != INSN_LIST)
4996 usage_insn = next_usage_insns;
4997 break;
4999 usage_insn = XEXP (next_usage_insns, 0);
5000 lra_assert (DEBUG_INSN_P (usage_insn));
5001 next_usage_insns = XEXP (next_usage_insns, 1);
5002 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
5003 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5004 if (lra_dump_file != NULL)
5006 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5007 original_regno, REGNO (new_reg));
5008 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5011 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5012 lra_assert (usage_insn != insn || (after_p && before_p));
5013 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5014 after_p ? NULL : restore,
5015 after_p ? restore : NULL,
5016 call_save_p
5017 ? "Add reg<-save" : "Add reg<-split");
5018 lra_process_new_insns (insn, before_p ? save : NULL,
5019 before_p ? NULL : save,
5020 call_save_p
5021 ? "Add save<-reg" : "Add split<-reg");
5022 if (nregs > 1)
5023 /* If we are trying to split multi-register. We should check
5024 conflicts on the next assignment sub-pass. IRA can allocate on
5025 sub-register levels, LRA do this on pseudos level right now and
5026 this discrepancy may create allocation conflicts after
5027 splitting. */
5028 lra_risky_transformations_p = true;
5029 if (lra_dump_file != NULL)
5030 fprintf (lra_dump_file,
5031 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5032 return true;
5035 /* Recognize that we need a split transformation for insn INSN, which
5036 defines or uses REGNO in its insn biggest MODE (we use it only if
5037 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5038 hard registers which might be used for reloads since the EBB end.
5039 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5040 uid before starting INSN processing. Return true if we succeed in
5041 such transformation. */
5042 static bool
5043 split_if_necessary (int regno, machine_mode mode,
5044 HARD_REG_SET potential_reload_hard_regs,
5045 bool before_p, rtx_insn *insn, int max_uid)
5047 bool res = false;
5048 int i, nregs = 1;
5049 rtx next_usage_insns;
5051 if (regno < FIRST_PSEUDO_REGISTER)
5052 nregs = hard_regno_nregs[regno][mode];
5053 for (i = 0; i < nregs; i++)
5054 if (usage_insns[regno + i].check == curr_usage_insns_check
5055 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5056 /* To avoid processing the register twice or more. */
5057 && ((GET_CODE (next_usage_insns) != INSN_LIST
5058 && INSN_UID (next_usage_insns) < max_uid)
5059 || (GET_CODE (next_usage_insns) == INSN_LIST
5060 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5061 && need_for_split_p (potential_reload_hard_regs, regno + i)
5062 && split_reg (before_p, regno + i, insn, next_usage_insns))
5063 res = true;
5064 return res;
5067 /* Check only registers living at the current program point in the
5068 current EBB. */
5069 static bitmap_head live_regs;
5071 /* Update live info in EBB given by its HEAD and TAIL insns after
5072 inheritance/split transformation. The function removes dead moves
5073 too. */
5074 static void
5075 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5077 unsigned int j;
5078 int i, regno;
5079 bool live_p;
5080 rtx_insn *prev_insn;
5081 rtx set;
5082 bool remove_p;
5083 basic_block last_bb, prev_bb, curr_bb;
5084 bitmap_iterator bi;
5085 struct lra_insn_reg *reg;
5086 edge e;
5087 edge_iterator ei;
5089 last_bb = BLOCK_FOR_INSN (tail);
5090 prev_bb = NULL;
5091 for (curr_insn = tail;
5092 curr_insn != PREV_INSN (head);
5093 curr_insn = prev_insn)
5095 prev_insn = PREV_INSN (curr_insn);
5096 /* We need to process empty blocks too. They contain
5097 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5098 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5099 continue;
5100 curr_bb = BLOCK_FOR_INSN (curr_insn);
5101 if (curr_bb != prev_bb)
5103 if (prev_bb != NULL)
5105 /* Update df_get_live_in (prev_bb): */
5106 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5107 if (bitmap_bit_p (&live_regs, j))
5108 bitmap_set_bit (df_get_live_in (prev_bb), j);
5109 else
5110 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5112 if (curr_bb != last_bb)
5114 /* Update df_get_live_out (curr_bb): */
5115 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5117 live_p = bitmap_bit_p (&live_regs, j);
5118 if (! live_p)
5119 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5120 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5122 live_p = true;
5123 break;
5125 if (live_p)
5126 bitmap_set_bit (df_get_live_out (curr_bb), j);
5127 else
5128 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5131 prev_bb = curr_bb;
5132 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5134 if (! NONDEBUG_INSN_P (curr_insn))
5135 continue;
5136 curr_id = lra_get_insn_recog_data (curr_insn);
5137 curr_static_id = curr_id->insn_static_data;
5138 remove_p = false;
5139 if ((set = single_set (curr_insn)) != NULL_RTX && REG_P (SET_DEST (set))
5140 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5141 && bitmap_bit_p (&check_only_regs, regno)
5142 && ! bitmap_bit_p (&live_regs, regno))
5143 remove_p = true;
5144 /* See which defined values die here. */
5145 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5146 if (reg->type == OP_OUT && ! reg->subreg_p)
5147 bitmap_clear_bit (&live_regs, reg->regno);
5148 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5149 if (reg->type == OP_OUT && ! reg->subreg_p)
5150 bitmap_clear_bit (&live_regs, reg->regno);
5151 if (curr_id->arg_hard_regs != NULL)
5152 /* Make clobbered argument hard registers die. */
5153 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5154 if (regno >= FIRST_PSEUDO_REGISTER)
5155 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
5156 /* Mark each used value as live. */
5157 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5158 if (reg->type != OP_OUT
5159 && bitmap_bit_p (&check_only_regs, reg->regno))
5160 bitmap_set_bit (&live_regs, reg->regno);
5161 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5162 if (reg->type != OP_OUT
5163 && bitmap_bit_p (&check_only_regs, reg->regno))
5164 bitmap_set_bit (&live_regs, reg->regno);
5165 if (curr_id->arg_hard_regs != NULL)
5166 /* Make used argument hard registers live. */
5167 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5168 if (regno < FIRST_PSEUDO_REGISTER
5169 && bitmap_bit_p (&check_only_regs, regno))
5170 bitmap_set_bit (&live_regs, regno);
5171 /* It is quite important to remove dead move insns because it
5172 means removing dead store. We don't need to process them for
5173 constraints. */
5174 if (remove_p)
5176 if (lra_dump_file != NULL)
5178 fprintf (lra_dump_file, " Removing dead insn:\n ");
5179 dump_insn_slim (lra_dump_file, curr_insn);
5181 lra_set_insn_deleted (curr_insn);
5186 /* The structure describes info to do an inheritance for the current
5187 insn. We need to collect such info first before doing the
5188 transformations because the transformations change the insn
5189 internal representation. */
5190 struct to_inherit
5192 /* Original regno. */
5193 int regno;
5194 /* Subsequent insns which can inherit original reg value. */
5195 rtx insns;
5198 /* Array containing all info for doing inheritance from the current
5199 insn. */
5200 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5202 /* Number elements in the previous array. */
5203 static int to_inherit_num;
5205 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5206 structure to_inherit. */
5207 static void
5208 add_to_inherit (int regno, rtx insns)
5210 int i;
5212 for (i = 0; i < to_inherit_num; i++)
5213 if (to_inherit[i].regno == regno)
5214 return;
5215 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5216 to_inherit[to_inherit_num].regno = regno;
5217 to_inherit[to_inherit_num++].insns = insns;
5220 /* Return the last non-debug insn in basic block BB, or the block begin
5221 note if none. */
5222 static rtx_insn *
5223 get_last_insertion_point (basic_block bb)
5225 rtx_insn *insn;
5227 FOR_BB_INSNS_REVERSE (bb, insn)
5228 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5229 return insn;
5230 gcc_unreachable ();
5233 /* Set up RES by registers living on edges FROM except the edge (FROM,
5234 TO) or by registers set up in a jump insn in BB FROM. */
5235 static void
5236 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5238 rtx_insn *last;
5239 struct lra_insn_reg *reg;
5240 edge e;
5241 edge_iterator ei;
5243 lra_assert (to != NULL);
5244 bitmap_clear (res);
5245 FOR_EACH_EDGE (e, ei, from->succs)
5246 if (e->dest != to)
5247 bitmap_ior_into (res, df_get_live_in (e->dest));
5248 last = get_last_insertion_point (from);
5249 if (! JUMP_P (last))
5250 return;
5251 curr_id = lra_get_insn_recog_data (last);
5252 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5253 if (reg->type != OP_IN)
5254 bitmap_set_bit (res, reg->regno);
5257 /* Used as a temporary results of some bitmap calculations. */
5258 static bitmap_head temp_bitmap;
5260 /* We split for reloads of small class of hard regs. The following
5261 defines how many hard regs the class should have to be qualified as
5262 small. The code is mostly oriented to x86/x86-64 architecture
5263 where some insns need to use only specific register or pair of
5264 registers and these register can live in RTL explicitly, e.g. for
5265 parameter passing. */
5266 static const int max_small_class_regs_num = 2;
5268 /* Do inheritance/split transformations in EBB starting with HEAD and
5269 finishing on TAIL. We process EBB insns in the reverse order.
5270 Return true if we did any inheritance/split transformation in the
5271 EBB.
5273 We should avoid excessive splitting which results in worse code
5274 because of inaccurate cost calculations for spilling new split
5275 pseudos in such case. To achieve this we do splitting only if
5276 register pressure is high in given basic block and there are reload
5277 pseudos requiring hard registers. We could do more register
5278 pressure calculations at any given program point to avoid necessary
5279 splitting even more but it is to expensive and the current approach
5280 works well enough. */
5281 static bool
5282 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5284 int i, src_regno, dst_regno, nregs;
5285 bool change_p, succ_p, update_reloads_num_p;
5286 rtx_insn *prev_insn, *last_insn;
5287 rtx next_usage_insns, set;
5288 enum reg_class cl;
5289 struct lra_insn_reg *reg;
5290 basic_block last_processed_bb, curr_bb = NULL;
5291 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5292 bitmap to_process;
5293 unsigned int j;
5294 bitmap_iterator bi;
5295 bool head_p, after_p;
5297 change_p = false;
5298 curr_usage_insns_check++;
5299 reloads_num = calls_num = 0;
5300 bitmap_clear (&check_only_regs);
5301 last_processed_bb = NULL;
5302 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5303 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5304 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5305 /* We don't process new insns generated in the loop. */
5306 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5308 prev_insn = PREV_INSN (curr_insn);
5309 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5310 curr_bb = BLOCK_FOR_INSN (curr_insn);
5311 if (last_processed_bb != curr_bb)
5313 /* We are at the end of BB. Add qualified living
5314 pseudos for potential splitting. */
5315 to_process = df_get_live_out (curr_bb);
5316 if (last_processed_bb != NULL)
5318 /* We are somewhere in the middle of EBB. */
5319 get_live_on_other_edges (curr_bb, last_processed_bb,
5320 &temp_bitmap);
5321 to_process = &temp_bitmap;
5323 last_processed_bb = curr_bb;
5324 last_insn = get_last_insertion_point (curr_bb);
5325 after_p = (! JUMP_P (last_insn)
5326 && (! CALL_P (last_insn)
5327 || (find_reg_note (last_insn,
5328 REG_NORETURN, NULL_RTX) == NULL_RTX
5329 && ! SIBLING_CALL_P (last_insn))));
5330 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5331 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5333 if ((int) j >= lra_constraint_new_regno_start)
5334 break;
5335 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5337 if (j < FIRST_PSEUDO_REGISTER)
5338 SET_HARD_REG_BIT (live_hard_regs, j);
5339 else
5340 add_to_hard_reg_set (&live_hard_regs,
5341 PSEUDO_REGNO_MODE (j),
5342 reg_renumber[j]);
5343 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5347 src_regno = dst_regno = -1;
5348 if (NONDEBUG_INSN_P (curr_insn)
5349 && (set = single_set (curr_insn)) != NULL_RTX
5350 && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
5352 src_regno = REGNO (SET_SRC (set));
5353 dst_regno = REGNO (SET_DEST (set));
5355 update_reloads_num_p = true;
5356 if (src_regno < lra_constraint_new_regno_start
5357 && src_regno >= FIRST_PSEUDO_REGISTER
5358 && reg_renumber[src_regno] < 0
5359 && dst_regno >= lra_constraint_new_regno_start
5360 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5362 /* 'reload_pseudo <- original_pseudo'. */
5363 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5364 reloads_num++;
5365 update_reloads_num_p = false;
5366 succ_p = false;
5367 if (usage_insns[src_regno].check == curr_usage_insns_check
5368 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5369 succ_p = inherit_reload_reg (false, src_regno, cl,
5370 curr_insn, next_usage_insns);
5371 if (succ_p)
5372 change_p = true;
5373 else
5374 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5375 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5376 IOR_HARD_REG_SET (potential_reload_hard_regs,
5377 reg_class_contents[cl]);
5379 else if (src_regno >= lra_constraint_new_regno_start
5380 && dst_regno < lra_constraint_new_regno_start
5381 && dst_regno >= FIRST_PSEUDO_REGISTER
5382 && reg_renumber[dst_regno] < 0
5383 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5384 && usage_insns[dst_regno].check == curr_usage_insns_check
5385 && (next_usage_insns
5386 = usage_insns[dst_regno].insns) != NULL_RTX)
5388 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5389 reloads_num++;
5390 update_reloads_num_p = false;
5391 /* 'original_pseudo <- reload_pseudo'. */
5392 if (! JUMP_P (curr_insn)
5393 && inherit_reload_reg (true, dst_regno, cl,
5394 curr_insn, next_usage_insns))
5395 change_p = true;
5396 /* Invalidate. */
5397 usage_insns[dst_regno].check = 0;
5398 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5399 IOR_HARD_REG_SET (potential_reload_hard_regs,
5400 reg_class_contents[cl]);
5402 else if (INSN_P (curr_insn))
5404 int iter;
5405 int max_uid = get_max_uid ();
5407 curr_id = lra_get_insn_recog_data (curr_insn);
5408 curr_static_id = curr_id->insn_static_data;
5409 to_inherit_num = 0;
5410 /* Process insn definitions. */
5411 for (iter = 0; iter < 2; iter++)
5412 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5413 reg != NULL;
5414 reg = reg->next)
5415 if (reg->type != OP_IN
5416 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5418 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5419 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5420 && usage_insns[dst_regno].check == curr_usage_insns_check
5421 && (next_usage_insns
5422 = usage_insns[dst_regno].insns) != NULL_RTX)
5424 struct lra_insn_reg *r;
5426 for (r = curr_id->regs; r != NULL; r = r->next)
5427 if (r->type != OP_OUT && r->regno == dst_regno)
5428 break;
5429 /* Don't do inheritance if the pseudo is also
5430 used in the insn. */
5431 if (r == NULL)
5432 /* We can not do inheritance right now
5433 because the current insn reg info (chain
5434 regs) can change after that. */
5435 add_to_inherit (dst_regno, next_usage_insns);
5437 /* We can not process one reg twice here because of
5438 usage_insns invalidation. */
5439 if ((dst_regno < FIRST_PSEUDO_REGISTER
5440 || reg_renumber[dst_regno] >= 0)
5441 && ! reg->subreg_p && reg->type != OP_IN)
5443 HARD_REG_SET s;
5445 if (split_if_necessary (dst_regno, reg->biggest_mode,
5446 potential_reload_hard_regs,
5447 false, curr_insn, max_uid))
5448 change_p = true;
5449 CLEAR_HARD_REG_SET (s);
5450 if (dst_regno < FIRST_PSEUDO_REGISTER)
5451 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5452 else
5453 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5454 reg_renumber[dst_regno]);
5455 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5457 /* We should invalidate potential inheritance or
5458 splitting for the current insn usages to the next
5459 usage insns (see code below) as the output pseudo
5460 prevents this. */
5461 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5462 && reg_renumber[dst_regno] < 0)
5463 || (reg->type == OP_OUT && ! reg->subreg_p
5464 && (dst_regno < FIRST_PSEUDO_REGISTER
5465 || reg_renumber[dst_regno] >= 0)))
5467 /* Invalidate and mark definitions. */
5468 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5469 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5470 else
5472 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5473 for (i = 0; i < nregs; i++)
5474 usage_insns[dst_regno + i].check
5475 = -(int) INSN_UID (curr_insn);
5479 /* Process clobbered call regs. */
5480 if (curr_id->arg_hard_regs != NULL)
5481 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5482 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5483 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
5484 = -(int) INSN_UID (curr_insn);
5485 if (! JUMP_P (curr_insn))
5486 for (i = 0; i < to_inherit_num; i++)
5487 if (inherit_reload_reg (true, to_inherit[i].regno,
5488 ALL_REGS, curr_insn,
5489 to_inherit[i].insns))
5490 change_p = true;
5491 if (CALL_P (curr_insn))
5493 rtx cheap, pat, dest;
5494 rtx_insn *restore;
5495 int regno, hard_regno;
5497 calls_num++;
5498 if ((cheap = find_reg_note (curr_insn,
5499 REG_RETURNED, NULL_RTX)) != NULL_RTX
5500 && ((cheap = XEXP (cheap, 0)), true)
5501 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
5502 && (hard_regno = reg_renumber[regno]) >= 0
5503 /* If there are pending saves/restores, the
5504 optimization is not worth. */
5505 && usage_insns[regno].calls_num == calls_num - 1
5506 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
5508 /* Restore the pseudo from the call result as
5509 REG_RETURNED note says that the pseudo value is
5510 in the call result and the pseudo is an argument
5511 of the call. */
5512 pat = PATTERN (curr_insn);
5513 if (GET_CODE (pat) == PARALLEL)
5514 pat = XVECEXP (pat, 0, 0);
5515 dest = SET_DEST (pat);
5516 /* For multiple return values dest is PARALLEL.
5517 Currently we handle only single return value case. */
5518 if (REG_P (dest))
5520 start_sequence ();
5521 emit_move_insn (cheap, copy_rtx (dest));
5522 restore = get_insns ();
5523 end_sequence ();
5524 lra_process_new_insns (curr_insn, NULL, restore,
5525 "Inserting call parameter restore");
5526 /* We don't need to save/restore of the pseudo from
5527 this call. */
5528 usage_insns[regno].calls_num = calls_num;
5529 bitmap_set_bit (&check_only_regs, regno);
5533 to_inherit_num = 0;
5534 /* Process insn usages. */
5535 for (iter = 0; iter < 2; iter++)
5536 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5537 reg != NULL;
5538 reg = reg->next)
5539 if ((reg->type != OP_OUT
5540 || (reg->type == OP_OUT && reg->subreg_p))
5541 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
5543 if (src_regno >= FIRST_PSEUDO_REGISTER
5544 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
5546 if (usage_insns[src_regno].check == curr_usage_insns_check
5547 && (next_usage_insns
5548 = usage_insns[src_regno].insns) != NULL_RTX
5549 && NONDEBUG_INSN_P (curr_insn))
5550 add_to_inherit (src_regno, next_usage_insns);
5551 else if (usage_insns[src_regno].check
5552 != -(int) INSN_UID (curr_insn))
5553 /* Add usages but only if the reg is not set up
5554 in the same insn. */
5555 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5557 else if (src_regno < FIRST_PSEUDO_REGISTER
5558 || reg_renumber[src_regno] >= 0)
5560 bool before_p;
5561 rtx_insn *use_insn = curr_insn;
5563 before_p = (JUMP_P (curr_insn)
5564 || (CALL_P (curr_insn) && reg->type == OP_IN));
5565 if (NONDEBUG_INSN_P (curr_insn)
5566 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
5567 && split_if_necessary (src_regno, reg->biggest_mode,
5568 potential_reload_hard_regs,
5569 before_p, curr_insn, max_uid))
5571 if (reg->subreg_p)
5572 lra_risky_transformations_p = true;
5573 change_p = true;
5574 /* Invalidate. */
5575 usage_insns[src_regno].check = 0;
5576 if (before_p)
5577 use_insn = PREV_INSN (curr_insn);
5579 if (NONDEBUG_INSN_P (curr_insn))
5581 if (src_regno < FIRST_PSEUDO_REGISTER)
5582 add_to_hard_reg_set (&live_hard_regs,
5583 reg->biggest_mode, src_regno);
5584 else
5585 add_to_hard_reg_set (&live_hard_regs,
5586 PSEUDO_REGNO_MODE (src_regno),
5587 reg_renumber[src_regno]);
5589 add_next_usage_insn (src_regno, use_insn, reloads_num);
5592 /* Process used call regs. */
5593 if (curr_id->arg_hard_regs != NULL)
5594 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5595 if (src_regno < FIRST_PSEUDO_REGISTER)
5597 SET_HARD_REG_BIT (live_hard_regs, src_regno);
5598 add_next_usage_insn (src_regno, curr_insn, reloads_num);
5600 for (i = 0; i < to_inherit_num; i++)
5602 src_regno = to_inherit[i].regno;
5603 if (inherit_reload_reg (false, src_regno, ALL_REGS,
5604 curr_insn, to_inherit[i].insns))
5605 change_p = true;
5606 else
5607 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5610 if (update_reloads_num_p
5611 && NONDEBUG_INSN_P (curr_insn)
5612 && (set = single_set (curr_insn)) != NULL_RTX)
5614 int regno = -1;
5615 if ((REG_P (SET_DEST (set))
5616 && (regno = REGNO (SET_DEST (set))) >= lra_constraint_new_regno_start
5617 && reg_renumber[regno] < 0
5618 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
5619 || (REG_P (SET_SRC (set))
5620 && (regno = REGNO (SET_SRC (set))) >= lra_constraint_new_regno_start
5621 && reg_renumber[regno] < 0
5622 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
5624 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5625 reloads_num++;
5626 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5627 IOR_HARD_REG_SET (potential_reload_hard_regs,
5628 reg_class_contents[cl]);
5631 /* We reached the start of the current basic block. */
5632 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
5633 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
5635 /* We reached the beginning of the current block -- do
5636 rest of spliting in the current BB. */
5637 to_process = df_get_live_in (curr_bb);
5638 if (BLOCK_FOR_INSN (head) != curr_bb)
5640 /* We are somewhere in the middle of EBB. */
5641 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
5642 curr_bb, &temp_bitmap);
5643 to_process = &temp_bitmap;
5645 head_p = true;
5646 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5648 if ((int) j >= lra_constraint_new_regno_start)
5649 break;
5650 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5651 && usage_insns[j].check == curr_usage_insns_check
5652 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
5654 if (need_for_split_p (potential_reload_hard_regs, j))
5656 if (lra_dump_file != NULL && head_p)
5658 fprintf (lra_dump_file,
5659 " ----------------------------------\n");
5660 head_p = false;
5662 if (split_reg (false, j, bb_note (curr_bb),
5663 next_usage_insns))
5664 change_p = true;
5666 usage_insns[j].check = 0;
5671 return change_p;
5674 /* This value affects EBB forming. If probability of edge from EBB to
5675 a BB is not greater than the following value, we don't add the BB
5676 to EBB. */
5677 #define EBB_PROBABILITY_CUTOFF \
5678 ((REG_BR_PROB_BASE * LRA_INHERITANCE_EBB_PROBABILITY_CUTOFF) / 100)
5680 /* Current number of inheritance/split iteration. */
5681 int lra_inheritance_iter;
5683 /* Entry function for inheritance/split pass. */
5684 void
5685 lra_inheritance (void)
5687 int i;
5688 basic_block bb, start_bb;
5689 edge e;
5691 lra_inheritance_iter++;
5692 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
5693 return;
5694 timevar_push (TV_LRA_INHERITANCE);
5695 if (lra_dump_file != NULL)
5696 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
5697 lra_inheritance_iter);
5698 curr_usage_insns_check = 0;
5699 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
5700 for (i = 0; i < lra_constraint_new_regno_start; i++)
5701 usage_insns[i].check = 0;
5702 bitmap_initialize (&check_only_regs, &reg_obstack);
5703 bitmap_initialize (&live_regs, &reg_obstack);
5704 bitmap_initialize (&temp_bitmap, &reg_obstack);
5705 bitmap_initialize (&ebb_global_regs, &reg_obstack);
5706 FOR_EACH_BB_FN (bb, cfun)
5708 start_bb = bb;
5709 if (lra_dump_file != NULL)
5710 fprintf (lra_dump_file, "EBB");
5711 /* Form a EBB starting with BB. */
5712 bitmap_clear (&ebb_global_regs);
5713 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
5714 for (;;)
5716 if (lra_dump_file != NULL)
5717 fprintf (lra_dump_file, " %d", bb->index);
5718 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
5719 || LABEL_P (BB_HEAD (bb->next_bb)))
5720 break;
5721 e = find_fallthru_edge (bb->succs);
5722 if (! e)
5723 break;
5724 if (e->probability < EBB_PROBABILITY_CUTOFF)
5725 break;
5726 bb = bb->next_bb;
5728 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
5729 if (lra_dump_file != NULL)
5730 fprintf (lra_dump_file, "\n");
5731 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
5732 /* Remember that the EBB head and tail can change in
5733 inherit_in_ebb. */
5734 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
5736 bitmap_clear (&ebb_global_regs);
5737 bitmap_clear (&temp_bitmap);
5738 bitmap_clear (&live_regs);
5739 bitmap_clear (&check_only_regs);
5740 free (usage_insns);
5742 timevar_pop (TV_LRA_INHERITANCE);
5747 /* This page contains code to undo failed inheritance/split
5748 transformations. */
5750 /* Current number of iteration undoing inheritance/split. */
5751 int lra_undo_inheritance_iter;
5753 /* Fix BB live info LIVE after removing pseudos created on pass doing
5754 inheritance/split which are REMOVED_PSEUDOS. */
5755 static void
5756 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
5758 unsigned int regno;
5759 bitmap_iterator bi;
5761 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
5762 if (bitmap_clear_bit (live, regno))
5763 bitmap_set_bit (live, lra_reg_info[regno].restore_regno);
5766 /* Return regno of the (subreg of) REG. Otherwise, return a negative
5767 number. */
5768 static int
5769 get_regno (rtx reg)
5771 if (GET_CODE (reg) == SUBREG)
5772 reg = SUBREG_REG (reg);
5773 if (REG_P (reg))
5774 return REGNO (reg);
5775 return -1;
5778 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
5779 return true if we did any change. The undo transformations for
5780 inheritance looks like
5781 i <- i2
5782 p <- i => p <- i2
5783 or removing
5784 p <- i, i <- p, and i <- i3
5785 where p is original pseudo from which inheritance pseudo i was
5786 created, i and i3 are removed inheritance pseudos, i2 is another
5787 not removed inheritance pseudo. All split pseudos or other
5788 occurrences of removed inheritance pseudos are changed on the
5789 corresponding original pseudos.
5791 The function also schedules insns changed and created during
5792 inheritance/split pass for processing by the subsequent constraint
5793 pass. */
5794 static bool
5795 remove_inheritance_pseudos (bitmap remove_pseudos)
5797 basic_block bb;
5798 int regno, sregno, prev_sregno, dregno, restore_regno;
5799 rtx set, prev_set;
5800 rtx_insn *prev_insn;
5801 bool change_p, done_p;
5803 change_p = ! bitmap_empty_p (remove_pseudos);
5804 /* We can not finish the function right away if CHANGE_P is true
5805 because we need to marks insns affected by previous
5806 inheritance/split pass for processing by the subsequent
5807 constraint pass. */
5808 FOR_EACH_BB_FN (bb, cfun)
5810 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
5811 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
5812 FOR_BB_INSNS_REVERSE (bb, curr_insn)
5814 if (! INSN_P (curr_insn))
5815 continue;
5816 done_p = false;
5817 sregno = dregno = -1;
5818 if (change_p && NONDEBUG_INSN_P (curr_insn)
5819 && (set = single_set (curr_insn)) != NULL_RTX)
5821 dregno = get_regno (SET_DEST (set));
5822 sregno = get_regno (SET_SRC (set));
5825 if (sregno >= 0 && dregno >= 0)
5827 if ((bitmap_bit_p (remove_pseudos, sregno)
5828 && (lra_reg_info[sregno].restore_regno == dregno
5829 || (bitmap_bit_p (remove_pseudos, dregno)
5830 && (lra_reg_info[sregno].restore_regno
5831 == lra_reg_info[dregno].restore_regno))))
5832 || (bitmap_bit_p (remove_pseudos, dregno)
5833 && lra_reg_info[dregno].restore_regno == sregno))
5834 /* One of the following cases:
5835 original <- removed inheritance pseudo
5836 removed inherit pseudo <- another removed inherit pseudo
5837 removed inherit pseudo <- original pseudo
5839 removed_split_pseudo <- original_reg
5840 original_reg <- removed_split_pseudo */
5842 if (lra_dump_file != NULL)
5844 fprintf (lra_dump_file, " Removing %s:\n",
5845 bitmap_bit_p (&lra_split_regs, sregno)
5846 || bitmap_bit_p (&lra_split_regs, dregno)
5847 ? "split" : "inheritance");
5848 dump_insn_slim (lra_dump_file, curr_insn);
5850 lra_set_insn_deleted (curr_insn);
5851 done_p = true;
5853 else if (bitmap_bit_p (remove_pseudos, sregno)
5854 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
5856 /* Search the following pattern:
5857 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
5858 original_pseudo <- inherit_or_split_pseudo1
5859 where the 2nd insn is the current insn and
5860 inherit_or_split_pseudo2 is not removed. If it is found,
5861 change the current insn onto:
5862 original_pseudo <- inherit_or_split_pseudo2. */
5863 for (prev_insn = PREV_INSN (curr_insn);
5864 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
5865 prev_insn = PREV_INSN (prev_insn))
5867 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
5868 && (prev_set = single_set (prev_insn)) != NULL_RTX
5869 /* There should be no subregs in insn we are
5870 searching because only the original reg might
5871 be in subreg when we changed the mode of
5872 load/store for splitting. */
5873 && REG_P (SET_DEST (prev_set))
5874 && REG_P (SET_SRC (prev_set))
5875 && (int) REGNO (SET_DEST (prev_set)) == sregno
5876 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
5877 >= FIRST_PSEUDO_REGISTER)
5878 /* As we consider chain of inheritance or
5879 splitting described in above comment we should
5880 check that sregno and prev_sregno were
5881 inheritance/split pseudos created from the
5882 same original regno. */
5883 && (lra_reg_info[sregno].restore_regno
5884 == lra_reg_info[prev_sregno].restore_regno)
5885 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
5887 lra_assert (GET_MODE (SET_SRC (prev_set))
5888 == GET_MODE (regno_reg_rtx[sregno]));
5889 if (GET_CODE (SET_SRC (set)) == SUBREG)
5890 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
5891 else
5892 SET_SRC (set) = SET_SRC (prev_set);
5893 /* As we are finishing with processing the insn
5894 here, check the destination too as it might
5895 inheritance pseudo for another pseudo. */
5896 if (bitmap_bit_p (remove_pseudos, dregno)
5897 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
5898 && (restore_regno
5899 = lra_reg_info[dregno].restore_regno) >= 0)
5901 if (GET_CODE (SET_DEST (set)) == SUBREG)
5902 SUBREG_REG (SET_DEST (set))
5903 = regno_reg_rtx[restore_regno];
5904 else
5905 SET_DEST (set) = regno_reg_rtx[restore_regno];
5907 lra_push_insn_and_update_insn_regno_info (curr_insn);
5908 lra_set_used_insn_alternative_by_uid
5909 (INSN_UID (curr_insn), -1);
5910 done_p = true;
5911 if (lra_dump_file != NULL)
5913 fprintf (lra_dump_file, " Change reload insn:\n");
5914 dump_insn_slim (lra_dump_file, curr_insn);
5919 if (! done_p)
5921 struct lra_insn_reg *reg;
5922 bool restored_regs_p = false;
5923 bool kept_regs_p = false;
5925 curr_id = lra_get_insn_recog_data (curr_insn);
5926 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5928 regno = reg->regno;
5929 restore_regno = lra_reg_info[regno].restore_regno;
5930 if (restore_regno >= 0)
5932 if (change_p && bitmap_bit_p (remove_pseudos, regno))
5934 lra_substitute_pseudo_within_insn
5935 (curr_insn, regno, regno_reg_rtx[restore_regno],
5936 false);
5937 restored_regs_p = true;
5939 else
5940 kept_regs_p = true;
5943 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
5945 /* The instruction has changed since the previous
5946 constraints pass. */
5947 lra_push_insn_and_update_insn_regno_info (curr_insn);
5948 lra_set_used_insn_alternative_by_uid
5949 (INSN_UID (curr_insn), -1);
5951 else if (restored_regs_p)
5952 /* The instruction has been restored to the form that
5953 it had during the previous constraints pass. */
5954 lra_update_insn_regno_info (curr_insn);
5955 if (restored_regs_p && lra_dump_file != NULL)
5957 fprintf (lra_dump_file, " Insn after restoring regs:\n");
5958 dump_insn_slim (lra_dump_file, curr_insn);
5963 return change_p;
5966 /* If optional reload pseudos failed to get a hard register or was not
5967 inherited, it is better to remove optional reloads. We do this
5968 transformation after undoing inheritance to figure out necessity to
5969 remove optional reloads easier. Return true if we do any
5970 change. */
5971 static bool
5972 undo_optional_reloads (void)
5974 bool change_p, keep_p;
5975 unsigned int regno, uid;
5976 bitmap_iterator bi, bi2;
5977 rtx_insn *insn;
5978 rtx set, src, dest;
5979 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
5981 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
5982 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
5983 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
5985 keep_p = false;
5986 /* Keep optional reloads from previous subpasses. */
5987 if (lra_reg_info[regno].restore_regno < 0
5988 /* If the original pseudo changed its allocation, just
5989 removing the optional pseudo is dangerous as the original
5990 pseudo will have longer live range. */
5991 || reg_renumber[lra_reg_info[regno].restore_regno] >= 0)
5992 keep_p = true;
5993 else if (reg_renumber[regno] >= 0)
5994 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
5996 insn = lra_insn_recog_data[uid]->insn;
5997 if ((set = single_set (insn)) == NULL_RTX)
5998 continue;
5999 src = SET_SRC (set);
6000 dest = SET_DEST (set);
6001 if (! REG_P (src) || ! REG_P (dest))
6002 continue;
6003 if (REGNO (dest) == regno
6004 /* Ignore insn for optional reloads itself. */
6005 && lra_reg_info[regno].restore_regno != (int) REGNO (src)
6006 /* Check only inheritance on last inheritance pass. */
6007 && (int) REGNO (src) >= new_regno_start
6008 /* Check that the optional reload was inherited. */
6009 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
6011 keep_p = true;
6012 break;
6015 if (keep_p)
6017 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
6018 if (lra_dump_file != NULL)
6019 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
6022 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
6023 bitmap_initialize (&insn_bitmap, &reg_obstack);
6024 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
6026 if (lra_dump_file != NULL)
6027 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
6028 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
6029 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
6031 insn = lra_insn_recog_data[uid]->insn;
6032 if ((set = single_set (insn)) != NULL_RTX)
6034 src = SET_SRC (set);
6035 dest = SET_DEST (set);
6036 if (REG_P (src) && REG_P (dest)
6037 && ((REGNO (src) == regno
6038 && (lra_reg_info[regno].restore_regno
6039 == (int) REGNO (dest)))
6040 || (REGNO (dest) == regno
6041 && (lra_reg_info[regno].restore_regno
6042 == (int) REGNO (src)))))
6044 if (lra_dump_file != NULL)
6046 fprintf (lra_dump_file, " Deleting move %u\n",
6047 INSN_UID (insn));
6048 dump_insn_slim (lra_dump_file, insn);
6050 lra_set_insn_deleted (insn);
6051 continue;
6053 /* We should not worry about generation memory-memory
6054 moves here as if the corresponding inheritance did
6055 not work (inheritance pseudo did not get a hard reg),
6056 we remove the inheritance pseudo and the optional
6057 reload. */
6059 lra_substitute_pseudo_within_insn
6060 (insn, regno, regno_reg_rtx[lra_reg_info[regno].restore_regno],
6061 false);
6062 lra_update_insn_regno_info (insn);
6063 if (lra_dump_file != NULL)
6065 fprintf (lra_dump_file,
6066 " Restoring original insn:\n");
6067 dump_insn_slim (lra_dump_file, insn);
6071 /* Clear restore_regnos. */
6072 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6073 lra_reg_info[regno].restore_regno = -1;
6074 bitmap_clear (&insn_bitmap);
6075 bitmap_clear (&removed_optional_reload_pseudos);
6076 return change_p;
6079 /* Entry function for undoing inheritance/split transformation. Return true
6080 if we did any RTL change in this pass. */
6081 bool
6082 lra_undo_inheritance (void)
6084 unsigned int regno;
6085 int restore_regno, hard_regno;
6086 int n_all_inherit, n_inherit, n_all_split, n_split;
6087 bitmap_head remove_pseudos;
6088 bitmap_iterator bi;
6089 bool change_p;
6091 lra_undo_inheritance_iter++;
6092 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6093 return false;
6094 if (lra_dump_file != NULL)
6095 fprintf (lra_dump_file,
6096 "\n********** Undoing inheritance #%d: **********\n\n",
6097 lra_undo_inheritance_iter);
6098 bitmap_initialize (&remove_pseudos, &reg_obstack);
6099 n_inherit = n_all_inherit = 0;
6100 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6101 if (lra_reg_info[regno].restore_regno >= 0)
6103 n_all_inherit++;
6104 if (reg_renumber[regno] < 0
6105 /* If the original pseudo changed its allocation, just
6106 removing inheritance is dangerous as for changing
6107 allocation we used shorter live-ranges. */
6108 && reg_renumber[lra_reg_info[regno].restore_regno] < 0)
6109 bitmap_set_bit (&remove_pseudos, regno);
6110 else
6111 n_inherit++;
6113 if (lra_dump_file != NULL && n_all_inherit != 0)
6114 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6115 n_inherit, n_all_inherit,
6116 (double) n_inherit / n_all_inherit * 100);
6117 n_split = n_all_split = 0;
6118 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6119 if ((restore_regno = lra_reg_info[regno].restore_regno) >= 0)
6121 n_all_split++;
6122 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6123 ? reg_renumber[restore_regno] : restore_regno);
6124 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6125 bitmap_set_bit (&remove_pseudos, regno);
6126 else
6128 n_split++;
6129 if (lra_dump_file != NULL)
6130 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6131 regno, restore_regno);
6134 if (lra_dump_file != NULL && n_all_split != 0)
6135 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6136 n_split, n_all_split,
6137 (double) n_split / n_all_split * 100);
6138 change_p = remove_inheritance_pseudos (&remove_pseudos);
6139 bitmap_clear (&remove_pseudos);
6140 /* Clear restore_regnos. */
6141 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6142 lra_reg_info[regno].restore_regno = -1;
6143 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6144 lra_reg_info[regno].restore_regno = -1;
6145 change_p = undo_optional_reloads () || change_p;
6146 return change_p;