2016-08-24 Michael Collison <michael.collison@linaro.org>
[official-gcc.git] / gcc / lra-constraints.c
blob053a65aa404d90770791acc11265746a10fb5291
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 /* Searches X for any reference to a reg with the same value as REGNO,
844 returning the rtx of the reference found if any. Otherwise,
845 returns NULL_RTX. */
846 static rtx
847 regno_val_use_in (unsigned int regno, rtx x)
849 const char *fmt;
850 int i, j;
851 rtx tem;
853 if (REG_P (x) && lra_reg_info[REGNO (x)].val == lra_reg_info[regno].val)
854 return x;
856 fmt = GET_RTX_FORMAT (GET_CODE (x));
857 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
859 if (fmt[i] == 'e')
861 if ((tem = regno_val_use_in (regno, XEXP (x, i))))
862 return tem;
864 else if (fmt[i] == 'E')
865 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
866 if ((tem = regno_val_use_in (regno , XVECEXP (x, i, j))))
867 return tem;
870 return NULL_RTX;
873 /* Generate reloads for matching OUT and INS (array of input operand
874 numbers with end marker -1) with reg class GOAL_CLASS, considering
875 output operands OUTS (similar array to INS) needing to be in different
876 registers. Add input and output reloads correspondingly to the lists
877 *BEFORE and *AFTER. OUT might be negative. In this case we generate
878 input reloads for matched input operands INS. EARLY_CLOBBER_P is a flag
879 that the output operand is early clobbered for chosen alternative. */
880 static void
881 match_reload (signed char out, signed char *ins, signed char *outs,
882 enum reg_class goal_class, rtx_insn **before,
883 rtx_insn **after, bool early_clobber_p)
885 bool out_conflict;
886 int i, in;
887 rtx new_in_reg, new_out_reg, reg;
888 machine_mode inmode, outmode;
889 rtx in_rtx = *curr_id->operand_loc[ins[0]];
890 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
892 inmode = curr_operand_mode[ins[0]];
893 outmode = out < 0 ? inmode : curr_operand_mode[out];
894 push_to_sequence (*before);
895 if (inmode != outmode)
897 if (GET_MODE_SIZE (inmode) > GET_MODE_SIZE (outmode))
899 reg = new_in_reg
900 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
901 goal_class, "");
902 if (SCALAR_INT_MODE_P (inmode))
903 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
904 else
905 new_out_reg = gen_rtx_SUBREG (outmode, reg, 0);
906 LRA_SUBREG_P (new_out_reg) = 1;
907 /* If the input reg is dying here, we can use the same hard
908 register for REG and IN_RTX. We do it only for original
909 pseudos as reload pseudos can die although original
910 pseudos still live where reload pseudos dies. */
911 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
912 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx)))
913 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
915 else
917 reg = new_out_reg
918 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
919 goal_class, "");
920 if (SCALAR_INT_MODE_P (outmode))
921 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
922 else
923 new_in_reg = gen_rtx_SUBREG (inmode, reg, 0);
924 /* NEW_IN_REG is non-paradoxical subreg. We don't want
925 NEW_OUT_REG living above. We add clobber clause for
926 this. This is just a temporary clobber. We can remove
927 it at the end of LRA work. */
928 rtx_insn *clobber = emit_clobber (new_out_reg);
929 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
930 LRA_SUBREG_P (new_in_reg) = 1;
931 if (GET_CODE (in_rtx) == SUBREG)
933 rtx subreg_reg = SUBREG_REG (in_rtx);
935 /* If SUBREG_REG is dying here and sub-registers IN_RTX
936 and NEW_IN_REG are similar, we can use the same hard
937 register for REG and SUBREG_REG. */
938 if (REG_P (subreg_reg)
939 && (int) REGNO (subreg_reg) < lra_new_regno_start
940 && GET_MODE (subreg_reg) == outmode
941 && SUBREG_BYTE (in_rtx) == SUBREG_BYTE (new_in_reg)
942 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg)))
943 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
947 else
949 /* Pseudos have values -- see comments for lra_reg_info.
950 Different pseudos with the same value do not conflict even if
951 they live in the same place. When we create a pseudo we
952 assign value of original pseudo (if any) from which we
953 created the new pseudo. If we create the pseudo from the
954 input pseudo, the new pseudo will have no conflict with the
955 input pseudo which is wrong when the input pseudo lives after
956 the insn and as the new pseudo value is changed by the insn
957 output. Therefore we create the new pseudo from the output
958 except the case when we have single matched dying input
959 pseudo.
961 We cannot reuse the current output register because we might
962 have a situation like "a <- a op b", where the constraints
963 force the second input operand ("b") to match the output
964 operand ("a"). "b" must then be copied into a new register
965 so that it doesn't clobber the current value of "a".
967 We can not use the same value if the output pseudo is
968 early clobbered or the input pseudo is mentioned in the
969 output, e.g. as an address part in memory, because
970 output reload will actually extend the pseudo liveness.
971 We don't care about eliminable hard regs here as we are
972 interesting only in pseudos. */
974 /* Matching input's register value is the same as one of the other
975 output operand. Output operands in a parallel insn must be in
976 different registers. */
977 out_conflict = false;
978 if (REG_P (in_rtx))
980 for (i = 0; outs[i] >= 0; i++)
982 rtx other_out_rtx = *curr_id->operand_loc[outs[i]];
983 if (REG_P (other_out_rtx)
984 && (regno_val_use_in (REGNO (in_rtx), other_out_rtx)
985 != NULL_RTX))
987 out_conflict = true;
988 break;
993 new_in_reg = new_out_reg
994 = (! early_clobber_p && ins[1] < 0 && REG_P (in_rtx)
995 && (int) REGNO (in_rtx) < lra_new_regno_start
996 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
997 && (out < 0
998 || regno_val_use_in (REGNO (in_rtx), out_rtx) == NULL_RTX)
999 && !out_conflict
1000 ? lra_create_new_reg (inmode, in_rtx, goal_class, "")
1001 : lra_create_new_reg_with_unique_value (outmode, out_rtx,
1002 goal_class, ""));
1004 /* In operand can be got from transformations before processing insn
1005 constraints. One example of such transformations is subreg
1006 reloading (see function simplify_operand_subreg). The new
1007 pseudos created by the transformations might have inaccurate
1008 class (ALL_REGS) and we should make their classes more
1009 accurate. */
1010 narrow_reload_pseudo_class (in_rtx, goal_class);
1011 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
1012 *before = get_insns ();
1013 end_sequence ();
1014 for (i = 0; (in = ins[i]) >= 0; i++)
1016 lra_assert
1017 (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
1018 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]));
1019 *curr_id->operand_loc[in] = new_in_reg;
1021 lra_update_dups (curr_id, ins);
1022 if (out < 0)
1023 return;
1024 /* See a comment for the input operand above. */
1025 narrow_reload_pseudo_class (out_rtx, goal_class);
1026 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
1028 start_sequence ();
1029 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
1030 emit_insn (*after);
1031 *after = get_insns ();
1032 end_sequence ();
1034 *curr_id->operand_loc[out] = new_out_reg;
1035 lra_update_dup (curr_id, out);
1038 /* Return register class which is union of all reg classes in insn
1039 constraint alternative string starting with P. */
1040 static enum reg_class
1041 reg_class_from_constraints (const char *p)
1043 int c, len;
1044 enum reg_class op_class = NO_REGS;
1047 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1049 case '#':
1050 case ',':
1051 return op_class;
1053 case 'g':
1054 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1055 break;
1057 default:
1058 enum constraint_num cn = lookup_constraint (p);
1059 enum reg_class cl = reg_class_for_constraint (cn);
1060 if (cl == NO_REGS)
1062 if (insn_extra_address_constraint (cn))
1063 op_class
1064 = (reg_class_subunion
1065 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1066 ADDRESS, SCRATCH)]);
1067 break;
1070 op_class = reg_class_subunion[op_class][cl];
1071 break;
1073 while ((p += len), c);
1074 return op_class;
1077 /* If OP is a register, return the class of the register as per
1078 get_reg_class, otherwise return NO_REGS. */
1079 static inline enum reg_class
1080 get_op_class (rtx op)
1082 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1085 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1086 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1087 SUBREG for VAL to make them equal. */
1088 static rtx_insn *
1089 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1091 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1093 /* Usually size of mem_pseudo is greater than val size but in
1094 rare cases it can be less as it can be defined by target
1095 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1096 if (! MEM_P (val))
1098 val = gen_rtx_SUBREG (GET_MODE (mem_pseudo),
1099 GET_CODE (val) == SUBREG ? SUBREG_REG (val) : val,
1101 LRA_SUBREG_P (val) = 1;
1103 else
1105 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1106 LRA_SUBREG_P (mem_pseudo) = 1;
1109 return to_p ? gen_move_insn (mem_pseudo, val)
1110 : gen_move_insn (val, mem_pseudo);
1113 /* Process a special case insn (register move), return true if we
1114 don't need to process it anymore. INSN should be a single set
1115 insn. Set up that RTL was changed through CHANGE_P and macro
1116 SECONDARY_MEMORY_NEEDED says to use secondary memory through
1117 SEC_MEM_P. */
1118 static bool
1119 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1121 int sregno, dregno;
1122 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1123 rtx_insn *before;
1124 enum reg_class dclass, sclass, secondary_class;
1125 secondary_reload_info sri;
1127 lra_assert (curr_insn_set != NULL_RTX);
1128 dreg = dest = SET_DEST (curr_insn_set);
1129 sreg = src = SET_SRC (curr_insn_set);
1130 if (GET_CODE (dest) == SUBREG)
1131 dreg = SUBREG_REG (dest);
1132 if (GET_CODE (src) == SUBREG)
1133 sreg = SUBREG_REG (src);
1134 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1135 return false;
1136 sclass = dclass = NO_REGS;
1137 if (REG_P (dreg))
1138 dclass = get_reg_class (REGNO (dreg));
1139 if (dclass == ALL_REGS)
1140 /* ALL_REGS is used for new pseudos created by transformations
1141 like reload of SUBREG_REG (see function
1142 simplify_operand_subreg). We don't know their class yet. We
1143 should figure out the class from processing the insn
1144 constraints not in this fast path function. Even if ALL_REGS
1145 were a right class for the pseudo, secondary_... hooks usually
1146 are not define for ALL_REGS. */
1147 return false;
1148 if (REG_P (sreg))
1149 sclass = get_reg_class (REGNO (sreg));
1150 if (sclass == ALL_REGS)
1151 /* See comments above. */
1152 return false;
1153 if (sclass == NO_REGS && dclass == NO_REGS)
1154 return false;
1155 #ifdef SECONDARY_MEMORY_NEEDED
1156 if (SECONDARY_MEMORY_NEEDED (sclass, dclass, GET_MODE (src))
1157 #ifdef SECONDARY_MEMORY_NEEDED_MODE
1158 && ((sclass != NO_REGS && dclass != NO_REGS)
1159 || GET_MODE (src) != SECONDARY_MEMORY_NEEDED_MODE (GET_MODE (src)))
1160 #endif
1163 *sec_mem_p = true;
1164 return false;
1166 #endif
1167 if (! REG_P (dreg) || ! REG_P (sreg))
1168 return false;
1169 sri.prev_sri = NULL;
1170 sri.icode = CODE_FOR_nothing;
1171 sri.extra_cost = 0;
1172 secondary_class = NO_REGS;
1173 /* Set up hard register for a reload pseudo for hook
1174 secondary_reload because some targets just ignore unassigned
1175 pseudos in the hook. */
1176 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1178 dregno = REGNO (dreg);
1179 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1181 else
1182 dregno = -1;
1183 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1185 sregno = REGNO (sreg);
1186 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1188 else
1189 sregno = -1;
1190 if (sclass != NO_REGS)
1191 secondary_class
1192 = (enum reg_class) targetm.secondary_reload (false, dest,
1193 (reg_class_t) sclass,
1194 GET_MODE (src), &sri);
1195 if (sclass == NO_REGS
1196 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1197 && dclass != NO_REGS))
1199 enum reg_class old_sclass = secondary_class;
1200 secondary_reload_info old_sri = sri;
1202 sri.prev_sri = NULL;
1203 sri.icode = CODE_FOR_nothing;
1204 sri.extra_cost = 0;
1205 secondary_class
1206 = (enum reg_class) targetm.secondary_reload (true, src,
1207 (reg_class_t) dclass,
1208 GET_MODE (src), &sri);
1209 /* Check the target hook consistency. */
1210 lra_assert
1211 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1212 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1213 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1215 if (sregno >= 0)
1216 reg_renumber [sregno] = -1;
1217 if (dregno >= 0)
1218 reg_renumber [dregno] = -1;
1219 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1220 return false;
1221 *change_p = true;
1222 new_reg = NULL_RTX;
1223 if (secondary_class != NO_REGS)
1224 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1225 secondary_class,
1226 "secondary");
1227 start_sequence ();
1228 if (sri.icode == CODE_FOR_nothing)
1229 lra_emit_move (new_reg, src);
1230 else
1232 enum reg_class scratch_class;
1234 scratch_class = (reg_class_from_constraints
1235 (insn_data[sri.icode].operand[2].constraint));
1236 scratch_reg = (lra_create_new_reg_with_unique_value
1237 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1238 scratch_class, "scratch"));
1239 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1240 src, scratch_reg));
1242 before = get_insns ();
1243 end_sequence ();
1244 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1245 if (new_reg != NULL_RTX)
1246 SET_SRC (curr_insn_set) = new_reg;
1247 else
1249 if (lra_dump_file != NULL)
1251 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1252 dump_insn_slim (lra_dump_file, curr_insn);
1254 lra_set_insn_deleted (curr_insn);
1255 return true;
1257 return false;
1260 /* The following data describe the result of process_alt_operands.
1261 The data are used in curr_insn_transform to generate reloads. */
1263 /* The chosen reg classes which should be used for the corresponding
1264 operands. */
1265 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1266 /* True if the operand should be the same as another operand and that
1267 other operand does not need a reload. */
1268 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1269 /* True if the operand does not need a reload. */
1270 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1271 /* True if the operand can be offsetable memory. */
1272 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1273 /* The number of an operand to which given operand can be matched to. */
1274 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1275 /* The number of elements in the following array. */
1276 static int goal_alt_dont_inherit_ops_num;
1277 /* Numbers of operands whose reload pseudos should not be inherited. */
1278 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1279 /* True if the insn commutative operands should be swapped. */
1280 static bool goal_alt_swapped;
1281 /* The chosen insn alternative. */
1282 static int goal_alt_number;
1284 /* True if the corresponding operand is the result of an equivalence
1285 substitution. */
1286 static bool equiv_substition_p[MAX_RECOG_OPERANDS];
1288 /* The following five variables are used to choose the best insn
1289 alternative. They reflect final characteristics of the best
1290 alternative. */
1292 /* Number of necessary reloads and overall cost reflecting the
1293 previous value and other unpleasantness of the best alternative. */
1294 static int best_losers, best_overall;
1295 /* Overall number hard registers used for reloads. For example, on
1296 some targets we need 2 general registers to reload DFmode and only
1297 one floating point register. */
1298 static int best_reload_nregs;
1299 /* Overall number reflecting distances of previous reloading the same
1300 value. The distances are counted from the current BB start. It is
1301 used to improve inheritance chances. */
1302 static int best_reload_sum;
1304 /* True if the current insn should have no correspondingly input or
1305 output reloads. */
1306 static bool no_input_reloads_p, no_output_reloads_p;
1308 /* True if we swapped the commutative operands in the current
1309 insn. */
1310 static int curr_swapped;
1312 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1313 register of class CL. Add any input reloads to list BEFORE. AFTER
1314 is nonnull if *LOC is an automodified value; handle that case by
1315 adding the required output reloads to list AFTER. Return true if
1316 the RTL was changed.
1318 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1319 register. Return false if the address register is correct. */
1320 static bool
1321 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1322 enum reg_class cl)
1324 int regno;
1325 enum reg_class rclass, new_class;
1326 rtx reg;
1327 rtx new_reg;
1328 machine_mode mode;
1329 bool subreg_p, before_p = false;
1331 subreg_p = GET_CODE (*loc) == SUBREG;
1332 if (subreg_p)
1334 reg = SUBREG_REG (*loc);
1335 mode = GET_MODE (reg);
1337 /* For mode with size bigger than ptr_mode, there unlikely to be "mov"
1338 between two registers with different classes, but there normally will
1339 be "mov" which transfers element of vector register into the general
1340 register, and this normally will be a subreg which should be reloaded
1341 as a whole. This is particularly likely to be triggered when
1342 -fno-split-wide-types specified. */
1343 if (!REG_P (reg)
1344 || in_class_p (reg, cl, &new_class)
1345 || GET_MODE_SIZE (mode) <= GET_MODE_SIZE (ptr_mode))
1346 loc = &SUBREG_REG (*loc);
1349 reg = *loc;
1350 mode = GET_MODE (reg);
1351 if (! REG_P (reg))
1353 if (check_only_p)
1354 return true;
1355 /* Always reload memory in an address even if the target supports
1356 such addresses. */
1357 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1358 before_p = true;
1360 else
1362 regno = REGNO (reg);
1363 rclass = get_reg_class (regno);
1364 if (! check_only_p
1365 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1367 if (lra_dump_file != NULL)
1369 fprintf (lra_dump_file,
1370 "Changing pseudo %d in address of insn %u on equiv ",
1371 REGNO (reg), INSN_UID (curr_insn));
1372 dump_value_slim (lra_dump_file, *loc, 1);
1373 fprintf (lra_dump_file, "\n");
1375 *loc = copy_rtx (*loc);
1377 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1379 if (check_only_p)
1380 return true;
1381 reg = *loc;
1382 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1383 mode, reg, cl, subreg_p, "address", &new_reg))
1384 before_p = true;
1386 else if (new_class != NO_REGS && rclass != new_class)
1388 if (check_only_p)
1389 return true;
1390 lra_change_class (regno, new_class, " Change to", true);
1391 return false;
1393 else
1394 return false;
1396 if (before_p)
1398 push_to_sequence (*before);
1399 lra_emit_move (new_reg, reg);
1400 *before = get_insns ();
1401 end_sequence ();
1403 *loc = new_reg;
1404 if (after != NULL)
1406 start_sequence ();
1407 lra_emit_move (before_p ? copy_rtx (reg) : reg, new_reg);
1408 emit_insn (*after);
1409 *after = get_insns ();
1410 end_sequence ();
1412 return true;
1415 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1416 the insn to be inserted before curr insn. AFTER returns the
1417 the insn to be inserted after curr insn. ORIGREG and NEWREG
1418 are the original reg and new reg for reload. */
1419 static void
1420 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1421 rtx newreg)
1423 if (before)
1425 push_to_sequence (*before);
1426 lra_emit_move (newreg, origreg);
1427 *before = get_insns ();
1428 end_sequence ();
1430 if (after)
1432 start_sequence ();
1433 lra_emit_move (origreg, newreg);
1434 emit_insn (*after);
1435 *after = get_insns ();
1436 end_sequence ();
1440 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1442 /* Make reloads for subreg in operand NOP with internal subreg mode
1443 REG_MODE, add new reloads for further processing. Return true if
1444 any change was done. */
1445 static bool
1446 simplify_operand_subreg (int nop, machine_mode reg_mode)
1448 int hard_regno;
1449 rtx_insn *before, *after;
1450 machine_mode mode, innermode;
1451 rtx reg, new_reg;
1452 rtx operand = *curr_id->operand_loc[nop];
1453 enum reg_class regclass;
1454 enum op_type type;
1456 before = after = NULL;
1458 if (GET_CODE (operand) != SUBREG)
1459 return false;
1461 mode = GET_MODE (operand);
1462 reg = SUBREG_REG (operand);
1463 innermode = GET_MODE (reg);
1464 type = curr_static_id->operand[nop].type;
1465 if (MEM_P (reg))
1467 rtx subst;
1469 alter_subreg (curr_id->operand_loc[nop], false);
1470 subst = *curr_id->operand_loc[nop];
1471 lra_assert (MEM_P (subst));
1472 if (! valid_address_p (innermode, XEXP (reg, 0),
1473 MEM_ADDR_SPACE (reg))
1474 || valid_address_p (GET_MODE (subst), XEXP (subst, 0),
1475 MEM_ADDR_SPACE (subst))
1476 || ((get_constraint_type (lookup_constraint
1477 (curr_static_id->operand[nop].constraint))
1478 != CT_SPECIAL_MEMORY)
1479 /* We still can reload address and if the address is
1480 valid, we can remove subreg without reloading its
1481 inner memory. */
1482 && valid_address_p (GET_MODE (subst),
1483 regno_reg_rtx
1484 [ira_class_hard_regs
1485 [base_reg_class (GET_MODE (subst),
1486 MEM_ADDR_SPACE (subst),
1487 ADDRESS, SCRATCH)][0]],
1488 MEM_ADDR_SPACE (subst))))
1490 /* If we change address for paradoxical subreg of memory, the
1491 address might violate the necessary alignment or the access might
1492 be slow. So take this into consideration. We should not worry
1493 about access beyond allocated memory for paradoxical memory
1494 subregs as we don't substitute such equiv memory (see processing
1495 equivalences in function lra_constraints) and because for spilled
1496 pseudos we allocate stack memory enough for the biggest
1497 corresponding paradoxical subreg. */
1498 if (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (reg))
1499 || SLOW_UNALIGNED_ACCESS (innermode, MEM_ALIGN (reg))
1500 || MEM_ALIGN (reg) >= GET_MODE_ALIGNMENT (mode))
1501 return true;
1503 /* INNERMODE is fast, MODE slow. Reload the mem in INNERMODE. */
1504 enum reg_class rclass
1505 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1506 if (get_reload_reg (curr_static_id->operand[nop].type, innermode, reg,
1507 rclass, TRUE, "slow mem", &new_reg))
1509 bool insert_before, insert_after;
1510 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1512 insert_before = (type != OP_OUT
1513 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1514 insert_after = type != OP_IN;
1515 insert_move_for_subreg (insert_before ? &before : NULL,
1516 insert_after ? &after : NULL,
1517 reg, new_reg);
1519 *curr_id->operand_loc[nop] = operand;
1520 SUBREG_REG (operand) = new_reg;
1522 /* Convert to MODE. */
1523 reg = operand;
1524 rclass = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1525 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1526 rclass, TRUE, "slow mem", &new_reg))
1528 bool insert_before, insert_after;
1529 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1531 insert_before = type != OP_OUT;
1532 insert_after = type != OP_IN;
1533 insert_move_for_subreg (insert_before ? &before : NULL,
1534 insert_after ? &after : NULL,
1535 reg, new_reg);
1537 *curr_id->operand_loc[nop] = new_reg;
1538 lra_process_new_insns (curr_insn, before, after,
1539 "Inserting slow mem reload");
1540 return true;
1543 /* If the address was valid and became invalid, prefer to reload
1544 the memory. Typical case is when the index scale should
1545 correspond the memory. */
1546 *curr_id->operand_loc[nop] = operand;
1548 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1550 alter_subreg (curr_id->operand_loc[nop], false);
1551 return true;
1553 else if (CONSTANT_P (reg))
1555 /* Try to simplify subreg of constant. It is usually result of
1556 equivalence substitution. */
1557 if (innermode == VOIDmode
1558 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1559 innermode = curr_static_id->operand[nop].mode;
1560 if ((new_reg = simplify_subreg (mode, reg, innermode,
1561 SUBREG_BYTE (operand))) != NULL_RTX)
1563 *curr_id->operand_loc[nop] = new_reg;
1564 return true;
1567 /* Put constant into memory when we have mixed modes. It generates
1568 a better code in most cases as it does not need a secondary
1569 reload memory. It also prevents LRA looping when LRA is using
1570 secondary reload memory again and again. */
1571 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1572 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1574 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1575 alter_subreg (curr_id->operand_loc[nop], false);
1576 return true;
1578 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1579 if there may be a problem accessing OPERAND in the outer
1580 mode. */
1581 if ((REG_P (reg)
1582 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1583 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1584 /* Don't reload paradoxical subregs because we could be looping
1585 having repeatedly final regno out of hard regs range. */
1586 && (hard_regno_nregs[hard_regno][innermode]
1587 >= hard_regno_nregs[hard_regno][mode])
1588 && simplify_subreg_regno (hard_regno, innermode,
1589 SUBREG_BYTE (operand), mode) < 0
1590 /* Don't reload subreg for matching reload. It is actually
1591 valid subreg in LRA. */
1592 && ! LRA_SUBREG_P (operand))
1593 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1595 enum reg_class rclass;
1597 if (REG_P (reg))
1598 /* There is a big probability that we will get the same class
1599 for the new pseudo and we will get the same insn which
1600 means infinite looping. So spill the new pseudo. */
1601 rclass = NO_REGS;
1602 else
1603 /* The class will be defined later in curr_insn_transform. */
1604 rclass
1605 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1607 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1608 rclass, TRUE, "subreg reg", &new_reg))
1610 bool insert_before, insert_after;
1611 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1613 insert_before = (type != OP_OUT
1614 || GET_MODE_SIZE (innermode) > GET_MODE_SIZE (mode));
1615 insert_after = (type != OP_IN);
1616 insert_move_for_subreg (insert_before ? &before : NULL,
1617 insert_after ? &after : NULL,
1618 reg, new_reg);
1620 SUBREG_REG (operand) = new_reg;
1621 lra_process_new_insns (curr_insn, before, after,
1622 "Inserting subreg reload");
1623 return true;
1625 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1626 IRA allocates hardreg to the inner pseudo reg according to its mode
1627 instead of the outermode, so the size of the hardreg may not be enough
1628 to contain the outermode operand, in that case we may need to insert
1629 reload for the reg. For the following two types of paradoxical subreg,
1630 we need to insert reload:
1631 1. If the op_type is OP_IN, and the hardreg could not be paired with
1632 other hardreg to contain the outermode operand
1633 (checked by in_hard_reg_set_p), we need to insert the reload.
1634 2. If the op_type is OP_OUT or OP_INOUT.
1636 Here is a paradoxical subreg example showing how the reload is generated:
1638 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1639 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1641 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1642 here, if reg107 is assigned to hardreg R15, because R15 is the last
1643 hardreg, compiler cannot find another hardreg to pair with R15 to
1644 contain TImode data. So we insert a TImode reload reg180 for it.
1645 After reload is inserted:
1647 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1648 (reg:DI 107 [ __comp ])) -1
1649 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1650 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1652 Two reload hard registers will be allocated to reg180 to save TImode data
1653 in LRA_assign. */
1654 else if (REG_P (reg)
1655 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1656 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1657 && (hard_regno_nregs[hard_regno][innermode]
1658 < hard_regno_nregs[hard_regno][mode])
1659 && (regclass = lra_get_allocno_class (REGNO (reg)))
1660 && (type != OP_IN
1661 || !in_hard_reg_set_p (reg_class_contents[regclass],
1662 mode, hard_regno)))
1664 /* The class will be defined later in curr_insn_transform. */
1665 enum reg_class rclass
1666 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1668 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1669 rclass, TRUE, "paradoxical subreg", &new_reg))
1671 rtx subreg;
1672 bool insert_before, insert_after;
1674 PUT_MODE (new_reg, mode);
1675 subreg = gen_lowpart_SUBREG (innermode, new_reg);
1676 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1678 insert_before = (type != OP_OUT);
1679 insert_after = (type != OP_IN);
1680 insert_move_for_subreg (insert_before ? &before : NULL,
1681 insert_after ? &after : NULL,
1682 reg, subreg);
1684 SUBREG_REG (operand) = new_reg;
1685 lra_process_new_insns (curr_insn, before, after,
1686 "Inserting paradoxical subreg reload");
1687 return true;
1689 return false;
1692 /* Return TRUE if X refers for a hard register from SET. */
1693 static bool
1694 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1696 int i, j, x_hard_regno;
1697 machine_mode mode;
1698 const char *fmt;
1699 enum rtx_code code;
1701 if (x == NULL_RTX)
1702 return false;
1703 code = GET_CODE (x);
1704 mode = GET_MODE (x);
1705 if (code == SUBREG)
1707 x = SUBREG_REG (x);
1708 code = GET_CODE (x);
1709 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
1710 mode = GET_MODE (x);
1713 if (REG_P (x))
1715 x_hard_regno = get_hard_regno (x);
1716 return (x_hard_regno >= 0
1717 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1719 if (MEM_P (x))
1721 struct address_info ad;
1723 decompose_mem_address (&ad, x);
1724 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1725 return true;
1726 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1727 return true;
1729 fmt = GET_RTX_FORMAT (code);
1730 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1732 if (fmt[i] == 'e')
1734 if (uses_hard_regs_p (XEXP (x, i), set))
1735 return true;
1737 else if (fmt[i] == 'E')
1739 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1740 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1741 return true;
1744 return false;
1747 /* Return true if OP is a spilled pseudo. */
1748 static inline bool
1749 spilled_pseudo_p (rtx op)
1751 return (REG_P (op)
1752 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1755 /* Return true if X is a general constant. */
1756 static inline bool
1757 general_constant_p (rtx x)
1759 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1762 static bool
1763 reg_in_class_p (rtx reg, enum reg_class cl)
1765 if (cl == NO_REGS)
1766 return get_reg_class (REGNO (reg)) == NO_REGS;
1767 return in_class_p (reg, cl, NULL);
1770 /* Return true if SET of RCLASS contains no hard regs which can be
1771 used in MODE. */
1772 static bool
1773 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1774 HARD_REG_SET &set,
1775 enum machine_mode mode)
1777 HARD_REG_SET temp;
1779 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
1780 COPY_HARD_REG_SET (temp, set);
1781 AND_COMPL_HARD_REG_SET (temp, lra_no_alloc_regs);
1782 return (hard_reg_set_subset_p
1783 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1786 /* Major function to choose the current insn alternative and what
1787 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1788 negative we should consider only this alternative. Return false if
1789 we can not choose the alternative or find how to reload the
1790 operands. */
1791 static bool
1792 process_alt_operands (int only_alternative)
1794 bool ok_p = false;
1795 int nop, overall, nalt;
1796 int n_alternatives = curr_static_id->n_alternatives;
1797 int n_operands = curr_static_id->n_operands;
1798 /* LOSERS counts the operands that don't fit this alternative and
1799 would require loading. */
1800 int losers;
1801 /* REJECT is a count of how undesirable this alternative says it is
1802 if any reloading is required. If the alternative matches exactly
1803 then REJECT is ignored, but otherwise it gets this much counted
1804 against it in addition to the reloading needed. */
1805 int reject;
1806 int op_reject;
1807 /* The number of elements in the following array. */
1808 int early_clobbered_regs_num;
1809 /* Numbers of operands which are early clobber registers. */
1810 int early_clobbered_nops[MAX_RECOG_OPERANDS];
1811 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
1812 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
1813 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
1814 bool curr_alt_win[MAX_RECOG_OPERANDS];
1815 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
1816 int curr_alt_matches[MAX_RECOG_OPERANDS];
1817 /* The number of elements in the following array. */
1818 int curr_alt_dont_inherit_ops_num;
1819 /* Numbers of operands whose reload pseudos should not be inherited. */
1820 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1821 rtx op;
1822 /* The register when the operand is a subreg of register, otherwise the
1823 operand itself. */
1824 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
1825 /* The register if the operand is a register or subreg of register,
1826 otherwise NULL. */
1827 rtx operand_reg[MAX_RECOG_OPERANDS];
1828 int hard_regno[MAX_RECOG_OPERANDS];
1829 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
1830 int reload_nregs, reload_sum;
1831 bool costly_p;
1832 enum reg_class cl;
1834 /* Calculate some data common for all alternatives to speed up the
1835 function. */
1836 for (nop = 0; nop < n_operands; nop++)
1838 rtx reg;
1840 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
1841 /* The real hard regno of the operand after the allocation. */
1842 hard_regno[nop] = get_hard_regno (op);
1844 operand_reg[nop] = reg = op;
1845 biggest_mode[nop] = GET_MODE (op);
1846 if (GET_CODE (op) == SUBREG)
1848 operand_reg[nop] = reg = SUBREG_REG (op);
1849 if (GET_MODE_SIZE (biggest_mode[nop])
1850 < GET_MODE_SIZE (GET_MODE (reg)))
1851 biggest_mode[nop] = GET_MODE (reg);
1853 if (! REG_P (reg))
1854 operand_reg[nop] = NULL_RTX;
1855 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
1856 || ((int) REGNO (reg)
1857 == lra_get_elimination_hard_regno (REGNO (reg))))
1858 no_subreg_reg_operand[nop] = reg;
1859 else
1860 operand_reg[nop] = no_subreg_reg_operand[nop]
1861 /* Just use natural mode for elimination result. It should
1862 be enough for extra constraints hooks. */
1863 = regno_reg_rtx[hard_regno[nop]];
1866 /* The constraints are made of several alternatives. Each operand's
1867 constraint looks like foo,bar,... with commas separating the
1868 alternatives. The first alternatives for all operands go
1869 together, the second alternatives go together, etc.
1871 First loop over alternatives. */
1872 alternative_mask preferred = curr_id->preferred_alternatives;
1873 if (only_alternative >= 0)
1874 preferred &= ALTERNATIVE_BIT (only_alternative);
1876 for (nalt = 0; nalt < n_alternatives; nalt++)
1878 /* Loop over operands for one constraint alternative. */
1879 if (!TEST_BIT (preferred, nalt))
1880 continue;
1882 overall = losers = reject = reload_nregs = reload_sum = 0;
1883 for (nop = 0; nop < n_operands; nop++)
1885 int inc = (curr_static_id
1886 ->operand_alternative[nalt * n_operands + nop].reject);
1887 if (lra_dump_file != NULL && inc != 0)
1888 fprintf (lra_dump_file,
1889 " Staticly defined alt reject+=%d\n", inc);
1890 reject += inc;
1892 early_clobbered_regs_num = 0;
1894 for (nop = 0; nop < n_operands; nop++)
1896 const char *p;
1897 char *end;
1898 int len, c, m, i, opalt_num, this_alternative_matches;
1899 bool win, did_match, offmemok, early_clobber_p;
1900 /* false => this operand can be reloaded somehow for this
1901 alternative. */
1902 bool badop;
1903 /* true => this operand can be reloaded if the alternative
1904 allows regs. */
1905 bool winreg;
1906 /* True if a constant forced into memory would be OK for
1907 this operand. */
1908 bool constmemok;
1909 enum reg_class this_alternative, this_costly_alternative;
1910 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
1911 bool this_alternative_match_win, this_alternative_win;
1912 bool this_alternative_offmemok;
1913 bool scratch_p;
1914 machine_mode mode;
1915 enum constraint_num cn;
1917 opalt_num = nalt * n_operands + nop;
1918 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
1920 /* Fast track for no constraints at all. */
1921 curr_alt[nop] = NO_REGS;
1922 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
1923 curr_alt_win[nop] = true;
1924 curr_alt_match_win[nop] = false;
1925 curr_alt_offmemok[nop] = false;
1926 curr_alt_matches[nop] = -1;
1927 continue;
1930 op = no_subreg_reg_operand[nop];
1931 mode = curr_operand_mode[nop];
1933 win = did_match = winreg = offmemok = constmemok = false;
1934 badop = true;
1936 early_clobber_p = false;
1937 p = curr_static_id->operand_alternative[opalt_num].constraint;
1939 this_costly_alternative = this_alternative = NO_REGS;
1940 /* We update set of possible hard regs besides its class
1941 because reg class might be inaccurate. For example,
1942 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
1943 is translated in HI_REGS because classes are merged by
1944 pairs and there is no accurate intermediate class. */
1945 CLEAR_HARD_REG_SET (this_alternative_set);
1946 CLEAR_HARD_REG_SET (this_costly_alternative_set);
1947 this_alternative_win = false;
1948 this_alternative_match_win = false;
1949 this_alternative_offmemok = false;
1950 this_alternative_matches = -1;
1952 /* An empty constraint should be excluded by the fast
1953 track. */
1954 lra_assert (*p != 0 && *p != ',');
1956 op_reject = 0;
1957 /* Scan this alternative's specs for this operand; set WIN
1958 if the operand fits any letter in this alternative.
1959 Otherwise, clear BADOP if this operand could fit some
1960 letter after reloads, or set WINREG if this operand could
1961 fit after reloads provided the constraint allows some
1962 registers. */
1963 costly_p = false;
1966 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1968 case '\0':
1969 len = 0;
1970 break;
1971 case ',':
1972 c = '\0';
1973 break;
1975 case '&':
1976 early_clobber_p = true;
1977 break;
1979 case '$':
1980 op_reject += LRA_MAX_REJECT;
1981 break;
1982 case '^':
1983 op_reject += LRA_LOSER_COST_FACTOR;
1984 break;
1986 case '#':
1987 /* Ignore rest of this alternative. */
1988 c = '\0';
1989 break;
1991 case '0': case '1': case '2': case '3': case '4':
1992 case '5': case '6': case '7': case '8': case '9':
1994 int m_hregno;
1995 bool match_p;
1997 m = strtoul (p, &end, 10);
1998 p = end;
1999 len = 0;
2000 lra_assert (nop > m);
2002 this_alternative_matches = m;
2003 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
2004 /* We are supposed to match a previous operand.
2005 If we do, we win if that one did. If we do
2006 not, count both of the operands as losers.
2007 (This is too conservative, since most of the
2008 time only a single reload insn will be needed
2009 to make the two operands win. As a result,
2010 this alternative may be rejected when it is
2011 actually desirable.) */
2012 match_p = false;
2013 if (operands_match_p (*curr_id->operand_loc[nop],
2014 *curr_id->operand_loc[m], m_hregno))
2016 /* We should reject matching of an early
2017 clobber operand if the matching operand is
2018 not dying in the insn. */
2019 if (! curr_static_id->operand[m].early_clobber
2020 || operand_reg[nop] == NULL_RTX
2021 || (find_regno_note (curr_insn, REG_DEAD,
2022 REGNO (op))
2023 || REGNO (op) == REGNO (operand_reg[m])))
2024 match_p = true;
2026 if (match_p)
2028 /* If we are matching a non-offsettable
2029 address where an offsettable address was
2030 expected, then we must reject this
2031 combination, because we can't reload
2032 it. */
2033 if (curr_alt_offmemok[m]
2034 && MEM_P (*curr_id->operand_loc[m])
2035 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
2036 continue;
2038 else
2040 /* Operands don't match. Both operands must
2041 allow a reload register, otherwise we
2042 cannot make them match. */
2043 if (curr_alt[m] == NO_REGS)
2044 break;
2045 /* Retroactively mark the operand we had to
2046 match as a loser, if it wasn't already and
2047 it wasn't matched to a register constraint
2048 (e.g it might be matched by memory). */
2049 if (curr_alt_win[m]
2050 && (operand_reg[m] == NULL_RTX
2051 || hard_regno[m] < 0))
2053 losers++;
2054 reload_nregs
2055 += (ira_reg_class_max_nregs[curr_alt[m]]
2056 [GET_MODE (*curr_id->operand_loc[m])]);
2059 /* Prefer matching earlyclobber alternative as
2060 it results in less hard regs required for
2061 the insn than a non-matching earlyclobber
2062 alternative. */
2063 if (curr_static_id->operand[m].early_clobber)
2065 if (lra_dump_file != NULL)
2066 fprintf
2067 (lra_dump_file,
2068 " %d Matching earlyclobber alt:"
2069 " reject--\n",
2070 nop);
2071 reject--;
2073 /* Otherwise we prefer no matching
2074 alternatives because it gives more freedom
2075 in RA. */
2076 else if (operand_reg[nop] == NULL_RTX
2077 || (find_regno_note (curr_insn, REG_DEAD,
2078 REGNO (operand_reg[nop]))
2079 == NULL_RTX))
2081 if (lra_dump_file != NULL)
2082 fprintf
2083 (lra_dump_file,
2084 " %d Matching alt: reject+=2\n",
2085 nop);
2086 reject += 2;
2089 /* If we have to reload this operand and some
2090 previous operand also had to match the same
2091 thing as this operand, we don't know how to do
2092 that. */
2093 if (!match_p || !curr_alt_win[m])
2095 for (i = 0; i < nop; i++)
2096 if (curr_alt_matches[i] == m)
2097 break;
2098 if (i < nop)
2099 break;
2101 else
2102 did_match = true;
2104 /* This can be fixed with reloads if the operand
2105 we are supposed to match can be fixed with
2106 reloads. */
2107 badop = false;
2108 this_alternative = curr_alt[m];
2109 COPY_HARD_REG_SET (this_alternative_set, curr_alt_set[m]);
2110 winreg = this_alternative != NO_REGS;
2111 break;
2114 case 'g':
2115 if (MEM_P (op)
2116 || general_constant_p (op)
2117 || spilled_pseudo_p (op))
2118 win = true;
2119 cl = GENERAL_REGS;
2120 goto reg;
2122 default:
2123 cn = lookup_constraint (p);
2124 switch (get_constraint_type (cn))
2126 case CT_REGISTER:
2127 cl = reg_class_for_constraint (cn);
2128 if (cl != NO_REGS)
2129 goto reg;
2130 break;
2132 case CT_CONST_INT:
2133 if (CONST_INT_P (op)
2134 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2135 win = true;
2136 break;
2138 case CT_MEMORY:
2139 if (MEM_P (op)
2140 && satisfies_memory_constraint_p (op, cn))
2141 win = true;
2142 else if (spilled_pseudo_p (op))
2143 win = true;
2145 /* If we didn't already win, we can reload constants
2146 via force_const_mem or put the pseudo value into
2147 memory, or make other memory by reloading the
2148 address like for 'o'. */
2149 if (CONST_POOL_OK_P (mode, op)
2150 || MEM_P (op) || REG_P (op)
2151 /* We can restore the equiv insn by a
2152 reload. */
2153 || equiv_substition_p[nop])
2154 badop = false;
2155 constmemok = true;
2156 offmemok = true;
2157 break;
2159 case CT_ADDRESS:
2160 /* If we didn't already win, we can reload the address
2161 into a base register. */
2162 if (satisfies_address_constraint_p (op, cn))
2163 win = true;
2164 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2165 ADDRESS, SCRATCH);
2166 badop = false;
2167 goto reg;
2169 case CT_FIXED_FORM:
2170 if (constraint_satisfied_p (op, cn))
2171 win = true;
2172 break;
2174 case CT_SPECIAL_MEMORY:
2175 if (MEM_P (op)
2176 && satisfies_memory_constraint_p (op, cn))
2177 win = true;
2178 else if (spilled_pseudo_p (op))
2179 win = true;
2180 break;
2182 break;
2184 reg:
2185 this_alternative = reg_class_subunion[this_alternative][cl];
2186 IOR_HARD_REG_SET (this_alternative_set,
2187 reg_class_contents[cl]);
2188 if (costly_p)
2190 this_costly_alternative
2191 = reg_class_subunion[this_costly_alternative][cl];
2192 IOR_HARD_REG_SET (this_costly_alternative_set,
2193 reg_class_contents[cl]);
2195 if (mode == BLKmode)
2196 break;
2197 winreg = true;
2198 if (REG_P (op))
2200 if (hard_regno[nop] >= 0
2201 && in_hard_reg_set_p (this_alternative_set,
2202 mode, hard_regno[nop]))
2203 win = true;
2204 else if (hard_regno[nop] < 0
2205 && in_class_p (op, this_alternative, NULL))
2206 win = true;
2208 break;
2210 if (c != ' ' && c != '\t')
2211 costly_p = c == '*';
2213 while ((p += len), c);
2215 scratch_p = (operand_reg[nop] != NULL_RTX
2216 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2217 /* Record which operands fit this alternative. */
2218 if (win)
2220 this_alternative_win = true;
2221 if (operand_reg[nop] != NULL_RTX)
2223 if (hard_regno[nop] >= 0)
2225 if (in_hard_reg_set_p (this_costly_alternative_set,
2226 mode, hard_regno[nop]))
2228 if (lra_dump_file != NULL)
2229 fprintf (lra_dump_file,
2230 " %d Costly set: reject++\n",
2231 nop);
2232 reject++;
2235 else
2237 /* Prefer won reg to spilled pseudo under other
2238 equal conditions for possibe inheritance. */
2239 if (! scratch_p)
2241 if (lra_dump_file != NULL)
2242 fprintf
2243 (lra_dump_file,
2244 " %d Non pseudo reload: reject++\n",
2245 nop);
2246 reject++;
2248 if (in_class_p (operand_reg[nop],
2249 this_costly_alternative, NULL))
2251 if (lra_dump_file != NULL)
2252 fprintf
2253 (lra_dump_file,
2254 " %d Non pseudo costly reload:"
2255 " reject++\n",
2256 nop);
2257 reject++;
2260 /* We simulate the behavior of old reload here.
2261 Although scratches need hard registers and it
2262 might result in spilling other pseudos, no reload
2263 insns are generated for the scratches. So it
2264 might cost something but probably less than old
2265 reload pass believes. */
2266 if (scratch_p)
2268 if (lra_dump_file != NULL)
2269 fprintf (lra_dump_file,
2270 " %d Scratch win: reject+=2\n",
2271 nop);
2272 reject += 2;
2276 else if (did_match)
2277 this_alternative_match_win = true;
2278 else
2280 int const_to_mem = 0;
2281 bool no_regs_p;
2283 reject += op_reject;
2284 /* Never do output reload of stack pointer. It makes
2285 impossible to do elimination when SP is changed in
2286 RTL. */
2287 if (op == stack_pointer_rtx && ! frame_pointer_needed
2288 && curr_static_id->operand[nop].type != OP_IN)
2289 goto fail;
2291 /* If this alternative asks for a specific reg class, see if there
2292 is at least one allocatable register in that class. */
2293 no_regs_p
2294 = (this_alternative == NO_REGS
2295 || (hard_reg_set_subset_p
2296 (reg_class_contents[this_alternative],
2297 lra_no_alloc_regs)));
2299 /* For asms, verify that the class for this alternative is possible
2300 for the mode that is specified. */
2301 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2303 int i;
2304 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2305 if (HARD_REGNO_MODE_OK (i, mode)
2306 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2307 mode, i))
2308 break;
2309 if (i == FIRST_PSEUDO_REGISTER)
2310 winreg = false;
2313 /* If this operand accepts a register, and if the
2314 register class has at least one allocatable register,
2315 then this operand can be reloaded. */
2316 if (winreg && !no_regs_p)
2317 badop = false;
2319 if (badop)
2321 if (lra_dump_file != NULL)
2322 fprintf (lra_dump_file,
2323 " alt=%d: Bad operand -- refuse\n",
2324 nalt);
2325 goto fail;
2328 if (this_alternative != NO_REGS)
2330 HARD_REG_SET available_regs;
2332 COPY_HARD_REG_SET (available_regs,
2333 reg_class_contents[this_alternative]);
2334 AND_COMPL_HARD_REG_SET
2335 (available_regs,
2336 ira_prohibited_class_mode_regs[this_alternative][mode]);
2337 AND_COMPL_HARD_REG_SET (available_regs, lra_no_alloc_regs);
2338 if (hard_reg_set_empty_p (available_regs))
2340 /* There are no hard regs holding a value of given
2341 mode. */
2342 if (offmemok)
2344 this_alternative = NO_REGS;
2345 if (lra_dump_file != NULL)
2346 fprintf (lra_dump_file,
2347 " %d Using memory because of"
2348 " a bad mode: reject+=2\n",
2349 nop);
2350 reject += 2;
2352 else
2354 if (lra_dump_file != NULL)
2355 fprintf (lra_dump_file,
2356 " alt=%d: Wrong mode -- refuse\n",
2357 nalt);
2358 goto fail;
2363 /* If not assigned pseudo has a class which a subset of
2364 required reg class, it is a less costly alternative
2365 as the pseudo still can get a hard reg of necessary
2366 class. */
2367 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2368 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2369 && ira_class_subset_p[this_alternative][cl])
2371 if (lra_dump_file != NULL)
2372 fprintf
2373 (lra_dump_file,
2374 " %d Super set class reg: reject-=3\n", nop);
2375 reject -= 3;
2378 this_alternative_offmemok = offmemok;
2379 if (this_costly_alternative != NO_REGS)
2381 if (lra_dump_file != NULL)
2382 fprintf (lra_dump_file,
2383 " %d Costly loser: reject++\n", nop);
2384 reject++;
2386 /* If the operand is dying, has a matching constraint,
2387 and satisfies constraints of the matched operand
2388 which failed to satisfy the own constraints, most probably
2389 the reload for this operand will be gone. */
2390 if (this_alternative_matches >= 0
2391 && !curr_alt_win[this_alternative_matches]
2392 && REG_P (op)
2393 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2394 && (hard_regno[nop] >= 0
2395 ? in_hard_reg_set_p (this_alternative_set,
2396 mode, hard_regno[nop])
2397 : in_class_p (op, this_alternative, NULL)))
2399 if (lra_dump_file != NULL)
2400 fprintf
2401 (lra_dump_file,
2402 " %d Dying matched operand reload: reject++\n",
2403 nop);
2404 reject++;
2406 else
2408 /* Strict_low_part requires to reload the register
2409 not the sub-register. In this case we should
2410 check that a final reload hard reg can hold the
2411 value mode. */
2412 if (curr_static_id->operand[nop].strict_low
2413 && REG_P (op)
2414 && hard_regno[nop] < 0
2415 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2416 && ira_class_hard_regs_num[this_alternative] > 0
2417 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2418 [this_alternative][0],
2419 GET_MODE
2420 (*curr_id->operand_loc[nop])))
2422 if (lra_dump_file != NULL)
2423 fprintf
2424 (lra_dump_file,
2425 " alt=%d: Strict low subreg reload -- refuse\n",
2426 nalt);
2427 goto fail;
2429 losers++;
2431 if (operand_reg[nop] != NULL_RTX
2432 /* Output operands and matched input operands are
2433 not inherited. The following conditions do not
2434 exactly describe the previous statement but they
2435 are pretty close. */
2436 && curr_static_id->operand[nop].type != OP_OUT
2437 && (this_alternative_matches < 0
2438 || curr_static_id->operand[nop].type != OP_IN))
2440 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2441 (operand_reg[nop])]
2442 .last_reload);
2444 /* The value of reload_sum has sense only if we
2445 process insns in their order. It happens only on
2446 the first constraints sub-pass when we do most of
2447 reload work. */
2448 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2449 reload_sum += last_reload - bb_reload_num;
2451 /* If this is a constant that is reloaded into the
2452 desired class by copying it to memory first, count
2453 that as another reload. This is consistent with
2454 other code and is required to avoid choosing another
2455 alternative when the constant is moved into memory.
2456 Note that the test here is precisely the same as in
2457 the code below that calls force_const_mem. */
2458 if (CONST_POOL_OK_P (mode, op)
2459 && ((targetm.preferred_reload_class
2460 (op, this_alternative) == NO_REGS)
2461 || no_input_reloads_p))
2463 const_to_mem = 1;
2464 if (! no_regs_p)
2465 losers++;
2468 /* Alternative loses if it requires a type of reload not
2469 permitted for this insn. We can always reload
2470 objects with a REG_UNUSED note. */
2471 if ((curr_static_id->operand[nop].type != OP_IN
2472 && no_output_reloads_p
2473 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2474 || (curr_static_id->operand[nop].type != OP_OUT
2475 && no_input_reloads_p && ! const_to_mem)
2476 || (this_alternative_matches >= 0
2477 && (no_input_reloads_p
2478 || (no_output_reloads_p
2479 && (curr_static_id->operand
2480 [this_alternative_matches].type != OP_IN)
2481 && ! find_reg_note (curr_insn, REG_UNUSED,
2482 no_subreg_reg_operand
2483 [this_alternative_matches])))))
2485 if (lra_dump_file != NULL)
2486 fprintf
2487 (lra_dump_file,
2488 " alt=%d: No input/otput reload -- refuse\n",
2489 nalt);
2490 goto fail;
2493 /* Alternative loses if it required class pseudo can not
2494 hold value of required mode. Such insns can be
2495 described by insn definitions with mode iterators. */
2496 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2497 && ! hard_reg_set_empty_p (this_alternative_set)
2498 /* It is common practice for constraints to use a
2499 class which does not have actually enough regs to
2500 hold the value (e.g. x86 AREG for mode requiring
2501 more one general reg). Therefore we have 2
2502 conditions to check that the reload pseudo can
2503 not hold the mode value. */
2504 && ! HARD_REGNO_MODE_OK (ira_class_hard_regs
2505 [this_alternative][0],
2506 GET_MODE (*curr_id->operand_loc[nop]))
2507 /* The above condition is not enough as the first
2508 reg in ira_class_hard_regs can be not aligned for
2509 multi-words mode values. */
2510 && (prohibited_class_reg_set_mode_p
2511 (this_alternative, this_alternative_set,
2512 GET_MODE (*curr_id->operand_loc[nop]))))
2514 if (lra_dump_file != NULL)
2515 fprintf (lra_dump_file,
2516 " alt=%d: reload pseudo for op %d "
2517 " can not hold the mode value -- refuse\n",
2518 nalt, nop);
2519 goto fail;
2522 /* Check strong discouragement of reload of non-constant
2523 into class THIS_ALTERNATIVE. */
2524 if (! CONSTANT_P (op) && ! no_regs_p
2525 && (targetm.preferred_reload_class
2526 (op, this_alternative) == NO_REGS
2527 || (curr_static_id->operand[nop].type == OP_OUT
2528 && (targetm.preferred_output_reload_class
2529 (op, this_alternative) == NO_REGS))))
2531 if (lra_dump_file != NULL)
2532 fprintf (lra_dump_file,
2533 " %d Non-prefered reload: reject+=%d\n",
2534 nop, LRA_MAX_REJECT);
2535 reject += LRA_MAX_REJECT;
2538 if (! (MEM_P (op) && offmemok)
2539 && ! (const_to_mem && constmemok))
2541 /* We prefer to reload pseudos over reloading other
2542 things, since such reloads may be able to be
2543 eliminated later. So bump REJECT in other cases.
2544 Don't do this in the case where we are forcing a
2545 constant into memory and it will then win since
2546 we don't want to have a different alternative
2547 match then. */
2548 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2550 if (lra_dump_file != NULL)
2551 fprintf
2552 (lra_dump_file,
2553 " %d Non-pseudo reload: reject+=2\n",
2554 nop);
2555 reject += 2;
2558 if (! no_regs_p)
2559 reload_nregs
2560 += ira_reg_class_max_nregs[this_alternative][mode];
2562 if (SMALL_REGISTER_CLASS_P (this_alternative))
2564 if (lra_dump_file != NULL)
2565 fprintf
2566 (lra_dump_file,
2567 " %d Small class reload: reject+=%d\n",
2568 nop, LRA_LOSER_COST_FACTOR / 2);
2569 reject += LRA_LOSER_COST_FACTOR / 2;
2573 /* We are trying to spill pseudo into memory. It is
2574 usually more costly than moving to a hard register
2575 although it might takes the same number of
2576 reloads.
2578 Non-pseudo spill may happen also. Suppose a target allows both
2579 register and memory in the operand constraint alternatives,
2580 then it's typical that an eliminable register has a substition
2581 of "base + offset" which can either be reloaded by a simple
2582 "new_reg <= base + offset" which will match the register
2583 constraint, or a similar reg addition followed by further spill
2584 to and reload from memory which will match the memory
2585 constraint, but this memory spill will be much more costly
2586 usually.
2588 Code below increases the reject for both pseudo and non-pseudo
2589 spill. */
2590 if (no_regs_p
2591 && !(MEM_P (op) && offmemok)
2592 && !(REG_P (op) && hard_regno[nop] < 0))
2594 if (lra_dump_file != NULL)
2595 fprintf
2596 (lra_dump_file,
2597 " %d Spill %spseudo into memory: reject+=3\n",
2598 nop, REG_P (op) ? "" : "Non-");
2599 reject += 3;
2600 if (VECTOR_MODE_P (mode))
2602 /* Spilling vectors into memory is usually more
2603 costly as they contain big values. */
2604 if (lra_dump_file != NULL)
2605 fprintf
2606 (lra_dump_file,
2607 " %d Spill vector pseudo: reject+=2\n",
2608 nop);
2609 reject += 2;
2613 #ifdef SECONDARY_MEMORY_NEEDED
2614 /* If reload requires moving value through secondary
2615 memory, it will need one more insn at least. */
2616 if (this_alternative != NO_REGS
2617 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2618 && ((curr_static_id->operand[nop].type != OP_OUT
2619 && SECONDARY_MEMORY_NEEDED (cl, this_alternative,
2620 GET_MODE (op)))
2621 || (curr_static_id->operand[nop].type != OP_IN
2622 && SECONDARY_MEMORY_NEEDED (this_alternative, cl,
2623 GET_MODE (op)))))
2624 losers++;
2625 #endif
2626 /* Input reloads can be inherited more often than output
2627 reloads can be removed, so penalize output
2628 reloads. */
2629 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2631 if (lra_dump_file != NULL)
2632 fprintf
2633 (lra_dump_file,
2634 " %d Non input pseudo reload: reject++\n",
2635 nop);
2636 reject++;
2640 if (early_clobber_p && ! scratch_p)
2642 if (lra_dump_file != NULL)
2643 fprintf (lra_dump_file,
2644 " %d Early clobber: reject++\n", nop);
2645 reject++;
2647 /* ??? We check early clobbers after processing all operands
2648 (see loop below) and there we update the costs more.
2649 Should we update the cost (may be approximately) here
2650 because of early clobber register reloads or it is a rare
2651 or non-important thing to be worth to do it. */
2652 overall = losers * LRA_LOSER_COST_FACTOR + reject;
2653 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2655 if (lra_dump_file != NULL)
2656 fprintf (lra_dump_file,
2657 " alt=%d,overall=%d,losers=%d -- refuse\n",
2658 nalt, overall, losers);
2659 goto fail;
2662 curr_alt[nop] = this_alternative;
2663 COPY_HARD_REG_SET (curr_alt_set[nop], this_alternative_set);
2664 curr_alt_win[nop] = this_alternative_win;
2665 curr_alt_match_win[nop] = this_alternative_match_win;
2666 curr_alt_offmemok[nop] = this_alternative_offmemok;
2667 curr_alt_matches[nop] = this_alternative_matches;
2669 if (this_alternative_matches >= 0
2670 && !did_match && !this_alternative_win)
2671 curr_alt_win[this_alternative_matches] = false;
2673 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2674 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2676 if (curr_insn_set != NULL_RTX && n_operands == 2
2677 /* Prevent processing non-move insns. */
2678 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2679 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
2680 && ((! curr_alt_win[0] && ! curr_alt_win[1]
2681 && REG_P (no_subreg_reg_operand[0])
2682 && REG_P (no_subreg_reg_operand[1])
2683 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2684 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
2685 || (! curr_alt_win[0] && curr_alt_win[1]
2686 && REG_P (no_subreg_reg_operand[1])
2687 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
2688 || (curr_alt_win[0] && ! curr_alt_win[1]
2689 && REG_P (no_subreg_reg_operand[0])
2690 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
2691 && (! CONST_POOL_OK_P (curr_operand_mode[1],
2692 no_subreg_reg_operand[1])
2693 || (targetm.preferred_reload_class
2694 (no_subreg_reg_operand[1],
2695 (enum reg_class) curr_alt[1]) != NO_REGS))
2696 /* If it is a result of recent elimination in move
2697 insn we can transform it into an add still by
2698 using this alternative. */
2699 && GET_CODE (no_subreg_reg_operand[1]) != PLUS)))
2701 /* We have a move insn and a new reload insn will be similar
2702 to the current insn. We should avoid such situation as it
2703 results in LRA cycling. */
2704 overall += LRA_MAX_REJECT;
2706 ok_p = true;
2707 curr_alt_dont_inherit_ops_num = 0;
2708 for (nop = 0; nop < early_clobbered_regs_num; nop++)
2710 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
2711 HARD_REG_SET temp_set;
2713 i = early_clobbered_nops[nop];
2714 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
2715 || hard_regno[i] < 0)
2716 continue;
2717 lra_assert (operand_reg[i] != NULL_RTX);
2718 clobbered_hard_regno = hard_regno[i];
2719 CLEAR_HARD_REG_SET (temp_set);
2720 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
2721 first_conflict_j = last_conflict_j = -1;
2722 for (j = 0; j < n_operands; j++)
2723 if (j == i
2724 /* We don't want process insides of match_operator and
2725 match_parallel because otherwise we would process
2726 their operands once again generating a wrong
2727 code. */
2728 || curr_static_id->operand[j].is_operator)
2729 continue;
2730 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
2731 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
2732 continue;
2733 /* If we don't reload j-th operand, check conflicts. */
2734 else if ((curr_alt_win[j] || curr_alt_match_win[j])
2735 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
2737 if (first_conflict_j < 0)
2738 first_conflict_j = j;
2739 last_conflict_j = j;
2741 if (last_conflict_j < 0)
2742 continue;
2743 /* If earlyclobber operand conflicts with another
2744 non-matching operand which is actually the same register
2745 as the earlyclobber operand, it is better to reload the
2746 another operand as an operand matching the earlyclobber
2747 operand can be also the same. */
2748 if (first_conflict_j == last_conflict_j
2749 && operand_reg[last_conflict_j] != NULL_RTX
2750 && ! curr_alt_match_win[last_conflict_j]
2751 && REGNO (operand_reg[i]) == REGNO (operand_reg[last_conflict_j]))
2753 curr_alt_win[last_conflict_j] = false;
2754 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
2755 = last_conflict_j;
2756 losers++;
2757 /* Early clobber was already reflected in REJECT. */
2758 lra_assert (reject > 0);
2759 if (lra_dump_file != NULL)
2760 fprintf
2761 (lra_dump_file,
2762 " %d Conflict early clobber reload: reject--\n",
2764 reject--;
2765 overall += LRA_LOSER_COST_FACTOR - 1;
2767 else
2769 /* We need to reload early clobbered register and the
2770 matched registers. */
2771 for (j = 0; j < n_operands; j++)
2772 if (curr_alt_matches[j] == i)
2774 curr_alt_match_win[j] = false;
2775 losers++;
2776 overall += LRA_LOSER_COST_FACTOR;
2778 if (! curr_alt_match_win[i])
2779 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
2780 else
2782 /* Remember pseudos used for match reloads are never
2783 inherited. */
2784 lra_assert (curr_alt_matches[i] >= 0);
2785 curr_alt_win[curr_alt_matches[i]] = false;
2787 curr_alt_win[i] = curr_alt_match_win[i] = false;
2788 losers++;
2789 /* Early clobber was already reflected in REJECT. */
2790 lra_assert (reject > 0);
2791 if (lra_dump_file != NULL)
2792 fprintf
2793 (lra_dump_file,
2794 " %d Matched conflict early clobber reloads:"
2795 "reject--\n",
2797 reject--;
2798 overall += LRA_LOSER_COST_FACTOR - 1;
2801 if (lra_dump_file != NULL)
2802 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
2803 nalt, overall, losers, reload_nregs);
2805 /* If this alternative can be made to work by reloading, and it
2806 needs less reloading than the others checked so far, record
2807 it as the chosen goal for reloading. */
2808 if ((best_losers != 0 && losers == 0)
2809 || (((best_losers == 0 && losers == 0)
2810 || (best_losers != 0 && losers != 0))
2811 && (best_overall > overall
2812 || (best_overall == overall
2813 /* If the cost of the reloads is the same,
2814 prefer alternative which requires minimal
2815 number of reload regs. */
2816 && (reload_nregs < best_reload_nregs
2817 || (reload_nregs == best_reload_nregs
2818 && (best_reload_sum < reload_sum
2819 || (best_reload_sum == reload_sum
2820 && nalt < goal_alt_number))))))))
2822 for (nop = 0; nop < n_operands; nop++)
2824 goal_alt_win[nop] = curr_alt_win[nop];
2825 goal_alt_match_win[nop] = curr_alt_match_win[nop];
2826 goal_alt_matches[nop] = curr_alt_matches[nop];
2827 goal_alt[nop] = curr_alt[nop];
2828 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
2830 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
2831 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
2832 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
2833 goal_alt_swapped = curr_swapped;
2834 best_overall = overall;
2835 best_losers = losers;
2836 best_reload_nregs = reload_nregs;
2837 best_reload_sum = reload_sum;
2838 goal_alt_number = nalt;
2840 if (losers == 0)
2841 /* Everything is satisfied. Do not process alternatives
2842 anymore. */
2843 break;
2844 fail:
2847 return ok_p;
2850 /* Make reload base reg from address AD. */
2851 static rtx
2852 base_to_reg (struct address_info *ad)
2854 enum reg_class cl;
2855 int code = -1;
2856 rtx new_inner = NULL_RTX;
2857 rtx new_reg = NULL_RTX;
2858 rtx_insn *insn;
2859 rtx_insn *last_insn = get_last_insn();
2861 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2862 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2863 get_index_code (ad));
2864 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2865 cl, "base");
2866 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
2867 ad->disp_term == NULL
2868 ? gen_int_mode (0, ad->mode)
2869 : *ad->disp_term);
2870 if (!valid_address_p (ad->mode, new_inner, ad->as))
2871 return NULL_RTX;
2872 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base_term));
2873 code = recog_memoized (insn);
2874 if (code < 0)
2876 delete_insns_since (last_insn);
2877 return NULL_RTX;
2880 return new_inner;
2883 /* Make reload base reg + disp from address AD. Return the new pseudo. */
2884 static rtx
2885 base_plus_disp_to_reg (struct address_info *ad)
2887 enum reg_class cl;
2888 rtx new_reg;
2890 lra_assert (ad->base == ad->base_term && ad->disp == ad->disp_term);
2891 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
2892 get_index_code (ad));
2893 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
2894 cl, "base + disp");
2895 lra_emit_add (new_reg, *ad->base_term, *ad->disp_term);
2896 return new_reg;
2899 /* Make reload of index part of address AD. Return the new
2900 pseudo. */
2901 static rtx
2902 index_part_to_reg (struct address_info *ad)
2904 rtx new_reg;
2906 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
2907 INDEX_REG_CLASS, "index term");
2908 expand_mult (GET_MODE (*ad->index), *ad->index_term,
2909 GEN_INT (get_index_scale (ad)), new_reg, 1);
2910 return new_reg;
2913 /* Return true if we can add a displacement to address AD, even if that
2914 makes the address invalid. The fix-up code requires any new address
2915 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
2916 static bool
2917 can_add_disp_p (struct address_info *ad)
2919 return (!ad->autoinc_p
2920 && ad->segment == NULL
2921 && ad->base == ad->base_term
2922 && ad->disp == ad->disp_term);
2925 /* Make equiv substitution in address AD. Return true if a substitution
2926 was made. */
2927 static bool
2928 equiv_address_substitution (struct address_info *ad)
2930 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
2931 HOST_WIDE_INT disp, scale;
2932 bool change_p;
2934 base_term = strip_subreg (ad->base_term);
2935 if (base_term == NULL)
2936 base_reg = new_base_reg = NULL_RTX;
2937 else
2939 base_reg = *base_term;
2940 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
2942 index_term = strip_subreg (ad->index_term);
2943 if (index_term == NULL)
2944 index_reg = new_index_reg = NULL_RTX;
2945 else
2947 index_reg = *index_term;
2948 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
2950 if (base_reg == new_base_reg && index_reg == new_index_reg)
2951 return false;
2952 disp = 0;
2953 change_p = false;
2954 if (lra_dump_file != NULL)
2956 fprintf (lra_dump_file, "Changing address in insn %d ",
2957 INSN_UID (curr_insn));
2958 dump_value_slim (lra_dump_file, *ad->outer, 1);
2960 if (base_reg != new_base_reg)
2962 if (REG_P (new_base_reg))
2964 *base_term = new_base_reg;
2965 change_p = true;
2967 else if (GET_CODE (new_base_reg) == PLUS
2968 && REG_P (XEXP (new_base_reg, 0))
2969 && CONST_INT_P (XEXP (new_base_reg, 1))
2970 && can_add_disp_p (ad))
2972 disp += INTVAL (XEXP (new_base_reg, 1));
2973 *base_term = XEXP (new_base_reg, 0);
2974 change_p = true;
2976 if (ad->base_term2 != NULL)
2977 *ad->base_term2 = *ad->base_term;
2979 if (index_reg != new_index_reg)
2981 if (REG_P (new_index_reg))
2983 *index_term = new_index_reg;
2984 change_p = true;
2986 else if (GET_CODE (new_index_reg) == PLUS
2987 && REG_P (XEXP (new_index_reg, 0))
2988 && CONST_INT_P (XEXP (new_index_reg, 1))
2989 && can_add_disp_p (ad)
2990 && (scale = get_index_scale (ad)))
2992 disp += INTVAL (XEXP (new_index_reg, 1)) * scale;
2993 *index_term = XEXP (new_index_reg, 0);
2994 change_p = true;
2997 if (disp != 0)
2999 if (ad->disp != NULL)
3000 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
3001 else
3003 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
3004 update_address (ad);
3006 change_p = true;
3008 if (lra_dump_file != NULL)
3010 if (! change_p)
3011 fprintf (lra_dump_file, " -- no change\n");
3012 else
3014 fprintf (lra_dump_file, " on equiv ");
3015 dump_value_slim (lra_dump_file, *ad->outer, 1);
3016 fprintf (lra_dump_file, "\n");
3019 return change_p;
3022 /* Major function to make reloads for an address in operand NOP or
3023 check its correctness (If CHECK_ONLY_P is true). The supported
3024 cases are:
3026 1) an address that existed before LRA started, at which point it
3027 must have been valid. These addresses are subject to elimination
3028 and may have become invalid due to the elimination offset being out
3029 of range.
3031 2) an address created by forcing a constant to memory
3032 (force_const_to_mem). The initial form of these addresses might
3033 not be valid, and it is this function's job to make them valid.
3035 3) a frame address formed from a register and a (possibly zero)
3036 constant offset. As above, these addresses might not be valid and
3037 this function must make them so.
3039 Add reloads to the lists *BEFORE and *AFTER. We might need to add
3040 reloads to *AFTER because of inc/dec, {pre, post} modify in the
3041 address. Return true for any RTL change.
3043 The function is a helper function which does not produce all
3044 transformations (when CHECK_ONLY_P is false) which can be
3045 necessary. It does just basic steps. To do all necessary
3046 transformations use function process_address. */
3047 static bool
3048 process_address_1 (int nop, bool check_only_p,
3049 rtx_insn **before, rtx_insn **after)
3051 struct address_info ad;
3052 rtx new_reg;
3053 HOST_WIDE_INT scale;
3054 rtx op = *curr_id->operand_loc[nop];
3055 const char *constraint = curr_static_id->operand[nop].constraint;
3056 enum constraint_num cn = lookup_constraint (constraint);
3057 bool change_p = false;
3059 if (MEM_P (op)
3060 && GET_MODE (op) == BLKmode
3061 && GET_CODE (XEXP (op, 0)) == SCRATCH)
3062 return false;
3064 if (insn_extra_address_constraint (cn))
3065 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
3066 else if (MEM_P (op))
3067 decompose_mem_address (&ad, op);
3068 else if (GET_CODE (op) == SUBREG
3069 && MEM_P (SUBREG_REG (op)))
3070 decompose_mem_address (&ad, SUBREG_REG (op));
3071 else
3072 return false;
3073 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
3074 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
3075 when INDEX_REG_CLASS is a single register class. */
3076 if (ad.base_term != NULL
3077 && ad.index_term != NULL
3078 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
3079 && REG_P (*ad.base_term)
3080 && REG_P (*ad.index_term)
3081 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
3082 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
3084 std::swap (ad.base, ad.index);
3085 std::swap (ad.base_term, ad.index_term);
3087 if (! check_only_p)
3088 change_p = equiv_address_substitution (&ad);
3089 if (ad.base_term != NULL
3090 && (process_addr_reg
3091 (ad.base_term, check_only_p, before,
3092 (ad.autoinc_p
3093 && !(REG_P (*ad.base_term)
3094 && find_regno_note (curr_insn, REG_DEAD,
3095 REGNO (*ad.base_term)) != NULL_RTX)
3096 ? after : NULL),
3097 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3098 get_index_code (&ad)))))
3100 change_p = true;
3101 if (ad.base_term2 != NULL)
3102 *ad.base_term2 = *ad.base_term;
3104 if (ad.index_term != NULL
3105 && process_addr_reg (ad.index_term, check_only_p,
3106 before, NULL, INDEX_REG_CLASS))
3107 change_p = true;
3109 /* Target hooks sometimes don't treat extra-constraint addresses as
3110 legitimate address_operands, so handle them specially. */
3111 if (insn_extra_address_constraint (cn)
3112 && satisfies_address_constraint_p (&ad, cn))
3113 return change_p;
3115 if (check_only_p)
3116 return change_p;
3118 /* There are three cases where the shape of *AD.INNER may now be invalid:
3120 1) the original address was valid, but either elimination or
3121 equiv_address_substitution was applied and that made
3122 the address invalid.
3124 2) the address is an invalid symbolic address created by
3125 force_const_to_mem.
3127 3) the address is a frame address with an invalid offset.
3129 4) the address is a frame address with an invalid base.
3131 All these cases involve a non-autoinc address, so there is no
3132 point revalidating other types. */
3133 if (ad.autoinc_p || valid_address_p (&ad))
3134 return change_p;
3136 /* Any index existed before LRA started, so we can assume that the
3137 presence and shape of the index is valid. */
3138 push_to_sequence (*before);
3139 lra_assert (ad.disp == ad.disp_term);
3140 if (ad.base == NULL)
3142 if (ad.index == NULL)
3144 rtx_insn *insn;
3145 rtx_insn *last = get_last_insn ();
3146 int code = -1;
3147 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3148 SCRATCH, SCRATCH);
3149 rtx addr = *ad.inner;
3151 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
3152 if (HAVE_lo_sum)
3154 /* addr => lo_sum (new_base, addr), case (2) above. */
3155 insn = emit_insn (gen_rtx_SET
3156 (new_reg,
3157 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
3158 code = recog_memoized (insn);
3159 if (code >= 0)
3161 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
3162 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
3164 /* Try to put lo_sum into register. */
3165 insn = emit_insn (gen_rtx_SET
3166 (new_reg,
3167 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
3168 code = recog_memoized (insn);
3169 if (code >= 0)
3171 *ad.inner = new_reg;
3172 if (! valid_address_p (ad.mode, *ad.outer, ad.as))
3174 *ad.inner = addr;
3175 code = -1;
3181 if (code < 0)
3182 delete_insns_since (last);
3185 if (code < 0)
3187 /* addr => new_base, case (2) above. */
3188 lra_emit_move (new_reg, addr);
3190 for (insn = last == NULL_RTX ? get_insns () : NEXT_INSN (last);
3191 insn != NULL_RTX;
3192 insn = NEXT_INSN (insn))
3193 if (recog_memoized (insn) < 0)
3194 break;
3195 if (insn != NULL_RTX)
3197 /* Do nothing if we cannot generate right insns.
3198 This is analogous to reload pass behavior. */
3199 delete_insns_since (last);
3200 end_sequence ();
3201 return false;
3203 *ad.inner = new_reg;
3206 else
3208 /* index * scale + disp => new base + index * scale,
3209 case (1) above. */
3210 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3211 GET_CODE (*ad.index));
3213 lra_assert (INDEX_REG_CLASS != NO_REGS);
3214 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3215 lra_emit_move (new_reg, *ad.disp);
3216 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3217 new_reg, *ad.index);
3220 else if (ad.index == NULL)
3222 int regno;
3223 enum reg_class cl;
3224 rtx set;
3225 rtx_insn *insns, *last_insn;
3226 /* Try to reload base into register only if the base is invalid
3227 for the address but with valid offset, case (4) above. */
3228 start_sequence ();
3229 new_reg = base_to_reg (&ad);
3231 /* base + disp => new base, cases (1) and (3) above. */
3232 /* Another option would be to reload the displacement into an
3233 index register. However, postreload has code to optimize
3234 address reloads that have the same base and different
3235 displacements, so reloading into an index register would
3236 not necessarily be a win. */
3237 if (new_reg == NULL_RTX)
3238 new_reg = base_plus_disp_to_reg (&ad);
3239 insns = get_insns ();
3240 last_insn = get_last_insn ();
3241 /* If we generated at least two insns, try last insn source as
3242 an address. If we succeed, we generate one less insn. */
3243 if (last_insn != insns && (set = single_set (last_insn)) != NULL_RTX
3244 && GET_CODE (SET_SRC (set)) == PLUS
3245 && REG_P (XEXP (SET_SRC (set), 0))
3246 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3248 *ad.inner = SET_SRC (set);
3249 if (valid_address_p (ad.mode, *ad.outer, ad.as))
3251 *ad.base_term = XEXP (SET_SRC (set), 0);
3252 *ad.disp_term = XEXP (SET_SRC (set), 1);
3253 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3254 get_index_code (&ad));
3255 regno = REGNO (*ad.base_term);
3256 if (regno >= FIRST_PSEUDO_REGISTER
3257 && cl != lra_get_allocno_class (regno))
3258 lra_change_class (regno, cl, " Change to", true);
3259 new_reg = SET_SRC (set);
3260 delete_insns_since (PREV_INSN (last_insn));
3263 /* Try if target can split displacement into legitimite new disp
3264 and offset. If it's the case, we replace the last insn with
3265 insns for base + offset => new_reg and set new_reg + new disp
3266 to *ad.inner. */
3267 last_insn = get_last_insn ();
3268 if ((set = single_set (last_insn)) != NULL_RTX
3269 && GET_CODE (SET_SRC (set)) == PLUS
3270 && REG_P (XEXP (SET_SRC (set), 0))
3271 && REGNO (XEXP (SET_SRC (set), 0)) < FIRST_PSEUDO_REGISTER
3272 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3274 rtx addend, disp = XEXP (SET_SRC (set), 1);
3275 if (targetm.legitimize_address_displacement (&disp, &addend,
3276 ad.mode))
3278 rtx_insn *new_insns;
3279 start_sequence ();
3280 lra_emit_add (new_reg, XEXP (SET_SRC (set), 0), addend);
3281 new_insns = get_insns ();
3282 end_sequence ();
3283 new_reg = gen_rtx_PLUS (Pmode, new_reg, disp);
3284 delete_insns_since (PREV_INSN (last_insn));
3285 add_insn (new_insns);
3286 insns = get_insns ();
3289 end_sequence ();
3290 emit_insn (insns);
3291 *ad.inner = new_reg;
3293 else if (ad.disp_term != NULL)
3295 /* base + scale * index + disp => new base + scale * index,
3296 case (1) above. */
3297 new_reg = base_plus_disp_to_reg (&ad);
3298 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3299 new_reg, *ad.index);
3301 else if ((scale = get_index_scale (&ad)) == 1)
3303 /* The last transformation to one reg will be made in
3304 curr_insn_transform function. */
3305 end_sequence ();
3306 return false;
3308 else if (scale != 0)
3310 /* base + scale * index => base + new_reg,
3311 case (1) above.
3312 Index part of address may become invalid. For example, we
3313 changed pseudo on the equivalent memory and a subreg of the
3314 pseudo onto the memory of different mode for which the scale is
3315 prohibitted. */
3316 new_reg = index_part_to_reg (&ad);
3317 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3318 *ad.base_term, new_reg);
3320 else
3322 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3323 SCRATCH, SCRATCH);
3324 rtx addr = *ad.inner;
3326 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
3327 /* addr => new_base. */
3328 lra_emit_move (new_reg, addr);
3329 *ad.inner = new_reg;
3331 *before = get_insns ();
3332 end_sequence ();
3333 return true;
3336 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3337 Use process_address_1 as a helper function. Return true for any
3338 RTL changes.
3340 If CHECK_ONLY_P is true, just check address correctness. Return
3341 false if the address correct. */
3342 static bool
3343 process_address (int nop, bool check_only_p,
3344 rtx_insn **before, rtx_insn **after)
3346 bool res = false;
3348 while (process_address_1 (nop, check_only_p, before, after))
3350 if (check_only_p)
3351 return true;
3352 res = true;
3354 return res;
3357 /* Emit insns to reload VALUE into a new register. VALUE is an
3358 auto-increment or auto-decrement RTX whose operand is a register or
3359 memory location; so reloading involves incrementing that location.
3360 IN is either identical to VALUE, or some cheaper place to reload
3361 value being incremented/decremented from.
3363 INC_AMOUNT is the number to increment or decrement by (always
3364 positive and ignored for POST_MODIFY/PRE_MODIFY).
3366 Return pseudo containing the result. */
3367 static rtx
3368 emit_inc (enum reg_class new_rclass, rtx in, rtx value, int inc_amount)
3370 /* REG or MEM to be copied and incremented. */
3371 rtx incloc = XEXP (value, 0);
3372 /* Nonzero if increment after copying. */
3373 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3374 || GET_CODE (value) == POST_MODIFY);
3375 rtx_insn *last;
3376 rtx inc;
3377 rtx_insn *add_insn;
3378 int code;
3379 rtx real_in = in == value ? incloc : in;
3380 rtx result;
3381 bool plus_p = true;
3383 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3385 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3386 || GET_CODE (XEXP (value, 1)) == MINUS);
3387 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3388 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3389 inc = XEXP (XEXP (value, 1), 1);
3391 else
3393 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3394 inc_amount = -inc_amount;
3396 inc = GEN_INT (inc_amount);
3399 if (! post && REG_P (incloc))
3400 result = incloc;
3401 else
3402 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3403 "INC/DEC result");
3405 if (real_in != result)
3407 /* First copy the location to the result register. */
3408 lra_assert (REG_P (result));
3409 emit_insn (gen_move_insn (result, real_in));
3412 /* We suppose that there are insns to add/sub with the constant
3413 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3414 old reload worked with this assumption. If the assumption
3415 becomes wrong, we should use approach in function
3416 base_plus_disp_to_reg. */
3417 if (in == value)
3419 /* See if we can directly increment INCLOC. */
3420 last = get_last_insn ();
3421 add_insn = emit_insn (plus_p
3422 ? gen_add2_insn (incloc, inc)
3423 : gen_sub2_insn (incloc, inc));
3425 code = recog_memoized (add_insn);
3426 if (code >= 0)
3428 if (! post && result != incloc)
3429 emit_insn (gen_move_insn (result, incloc));
3430 return result;
3432 delete_insns_since (last);
3435 /* If couldn't do the increment directly, must increment in RESULT.
3436 The way we do this depends on whether this is pre- or
3437 post-increment. For pre-increment, copy INCLOC to the reload
3438 register, increment it there, then save back. */
3439 if (! post)
3441 if (real_in != result)
3442 emit_insn (gen_move_insn (result, real_in));
3443 if (plus_p)
3444 emit_insn (gen_add2_insn (result, inc));
3445 else
3446 emit_insn (gen_sub2_insn (result, inc));
3447 if (result != incloc)
3448 emit_insn (gen_move_insn (incloc, result));
3450 else
3452 /* Post-increment.
3454 Because this might be a jump insn or a compare, and because
3455 RESULT may not be available after the insn in an input
3456 reload, we must do the incrementing before the insn being
3457 reloaded for.
3459 We have already copied IN to RESULT. Increment the copy in
3460 RESULT, save that back, then decrement RESULT so it has
3461 the original value. */
3462 if (plus_p)
3463 emit_insn (gen_add2_insn (result, inc));
3464 else
3465 emit_insn (gen_sub2_insn (result, inc));
3466 emit_insn (gen_move_insn (incloc, result));
3467 /* Restore non-modified value for the result. We prefer this
3468 way because it does not require an additional hard
3469 register. */
3470 if (plus_p)
3472 if (CONST_INT_P (inc))
3473 emit_insn (gen_add2_insn (result,
3474 gen_int_mode (-INTVAL (inc),
3475 GET_MODE (result))));
3476 else
3477 emit_insn (gen_sub2_insn (result, inc));
3479 else
3480 emit_insn (gen_add2_insn (result, inc));
3482 return result;
3485 /* Return true if the current move insn does not need processing as we
3486 already know that it satisfies its constraints. */
3487 static bool
3488 simple_move_p (void)
3490 rtx dest, src;
3491 enum reg_class dclass, sclass;
3493 lra_assert (curr_insn_set != NULL_RTX);
3494 dest = SET_DEST (curr_insn_set);
3495 src = SET_SRC (curr_insn_set);
3497 /* If the instruction has multiple sets we need to process it even if it
3498 is single_set. This can happen if one or more of the SETs are dead.
3499 See PR73650. */
3500 if (multiple_sets (curr_insn))
3501 return false;
3503 return ((dclass = get_op_class (dest)) != NO_REGS
3504 && (sclass = get_op_class (src)) != NO_REGS
3505 /* The backend guarantees that register moves of cost 2
3506 never need reloads. */
3507 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3510 /* Swap operands NOP and NOP + 1. */
3511 static inline void
3512 swap_operands (int nop)
3514 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3515 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3516 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3517 std::swap (equiv_substition_p[nop], equiv_substition_p[nop + 1]);
3518 /* Swap the duplicates too. */
3519 lra_update_dup (curr_id, nop);
3520 lra_update_dup (curr_id, nop + 1);
3523 /* Main entry point of the constraint code: search the body of the
3524 current insn to choose the best alternative. It is mimicking insn
3525 alternative cost calculation model of former reload pass. That is
3526 because machine descriptions were written to use this model. This
3527 model can be changed in future. Make commutative operand exchange
3528 if it is chosen.
3530 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3531 constraints. Return true if any change happened during function
3532 call.
3534 If CHECK_ONLY_P is true then don't do any transformation. Just
3535 check that the insn satisfies all constraints. If the insn does
3536 not satisfy any constraint, return true. */
3537 static bool
3538 curr_insn_transform (bool check_only_p)
3540 int i, j, k;
3541 int n_operands;
3542 int n_alternatives;
3543 int n_outputs;
3544 int commutative;
3545 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3546 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3547 signed char outputs[MAX_RECOG_OPERANDS + 1];
3548 rtx_insn *before, *after;
3549 bool alt_p = false;
3550 /* Flag that the insn has been changed through a transformation. */
3551 bool change_p;
3552 bool sec_mem_p;
3553 #ifdef SECONDARY_MEMORY_NEEDED
3554 bool use_sec_mem_p;
3555 #endif
3556 int max_regno_before;
3557 int reused_alternative_num;
3559 curr_insn_set = single_set (curr_insn);
3560 if (curr_insn_set != NULL_RTX && simple_move_p ())
3561 return false;
3563 no_input_reloads_p = no_output_reloads_p = false;
3564 goal_alt_number = -1;
3565 change_p = sec_mem_p = false;
3566 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3567 reloads; neither are insns that SET cc0. Insns that use CC0 are
3568 not allowed to have any input reloads. */
3569 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3570 no_output_reloads_p = true;
3572 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3573 no_input_reloads_p = true;
3574 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3575 no_output_reloads_p = true;
3577 n_operands = curr_static_id->n_operands;
3578 n_alternatives = curr_static_id->n_alternatives;
3580 /* Just return "no reloads" if insn has no operands with
3581 constraints. */
3582 if (n_operands == 0 || n_alternatives == 0)
3583 return false;
3585 max_regno_before = max_reg_num ();
3587 for (i = 0; i < n_operands; i++)
3589 goal_alt_matched[i][0] = -1;
3590 goal_alt_matches[i] = -1;
3593 commutative = curr_static_id->commutative;
3595 /* Now see what we need for pseudos that didn't get hard regs or got
3596 the wrong kind of hard reg. For this, we must consider all the
3597 operands together against the register constraints. */
3599 best_losers = best_overall = INT_MAX;
3600 best_reload_sum = 0;
3602 curr_swapped = false;
3603 goal_alt_swapped = false;
3605 if (! check_only_p)
3606 /* Make equivalence substitution and memory subreg elimination
3607 before address processing because an address legitimacy can
3608 depend on memory mode. */
3609 for (i = 0; i < n_operands; i++)
3611 rtx op, subst, old;
3612 bool op_change_p = false;
3614 if (curr_static_id->operand[i].is_operator)
3615 continue;
3617 old = op = *curr_id->operand_loc[i];
3618 if (GET_CODE (old) == SUBREG)
3619 old = SUBREG_REG (old);
3620 subst = get_equiv_with_elimination (old, curr_insn);
3621 original_subreg_reg_mode[i] = VOIDmode;
3622 equiv_substition_p[i] = false;
3623 if (subst != old)
3625 equiv_substition_p[i] = true;
3626 subst = copy_rtx (subst);
3627 lra_assert (REG_P (old));
3628 if (GET_CODE (op) != SUBREG)
3629 *curr_id->operand_loc[i] = subst;
3630 else
3632 SUBREG_REG (op) = subst;
3633 if (GET_MODE (subst) == VOIDmode)
3634 original_subreg_reg_mode[i] = GET_MODE (old);
3636 if (lra_dump_file != NULL)
3638 fprintf (lra_dump_file,
3639 "Changing pseudo %d in operand %i of insn %u on equiv ",
3640 REGNO (old), i, INSN_UID (curr_insn));
3641 dump_value_slim (lra_dump_file, subst, 1);
3642 fprintf (lra_dump_file, "\n");
3644 op_change_p = change_p = true;
3646 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
3648 change_p = true;
3649 lra_update_dup (curr_id, i);
3653 /* Reload address registers and displacements. We do it before
3654 finding an alternative because of memory constraints. */
3655 before = after = NULL;
3656 for (i = 0; i < n_operands; i++)
3657 if (! curr_static_id->operand[i].is_operator
3658 && process_address (i, check_only_p, &before, &after))
3660 if (check_only_p)
3661 return true;
3662 change_p = true;
3663 lra_update_dup (curr_id, i);
3666 if (change_p)
3667 /* If we've changed the instruction then any alternative that
3668 we chose previously may no longer be valid. */
3669 lra_set_used_insn_alternative (curr_insn, -1);
3671 if (! check_only_p && curr_insn_set != NULL_RTX
3672 && check_and_process_move (&change_p, &sec_mem_p))
3673 return change_p;
3675 try_swapped:
3677 reused_alternative_num = check_only_p ? -1 : curr_id->used_insn_alternative;
3678 if (lra_dump_file != NULL && reused_alternative_num >= 0)
3679 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
3680 reused_alternative_num, INSN_UID (curr_insn));
3682 if (process_alt_operands (reused_alternative_num))
3683 alt_p = true;
3685 if (check_only_p)
3686 return ! alt_p || best_losers != 0;
3688 /* If insn is commutative (it's safe to exchange a certain pair of
3689 operands) then we need to try each alternative twice, the second
3690 time matching those two operands as if we had exchanged them. To
3691 do this, really exchange them in operands.
3693 If we have just tried the alternatives the second time, return
3694 operands to normal and drop through. */
3696 if (reused_alternative_num < 0 && commutative >= 0)
3698 curr_swapped = !curr_swapped;
3699 if (curr_swapped)
3701 swap_operands (commutative);
3702 goto try_swapped;
3704 else
3705 swap_operands (commutative);
3708 if (! alt_p && ! sec_mem_p)
3710 /* No alternative works with reloads?? */
3711 if (INSN_CODE (curr_insn) >= 0)
3712 fatal_insn ("unable to generate reloads for:", curr_insn);
3713 error_for_asm (curr_insn,
3714 "inconsistent operand constraints in an %<asm%>");
3715 /* Avoid further trouble with this insn. */
3716 PATTERN (curr_insn) = gen_rtx_USE (VOIDmode, const0_rtx);
3717 lra_invalidate_insn_data (curr_insn);
3718 return true;
3721 /* If the best alternative is with operands 1 and 2 swapped, swap
3722 them. Update the operand numbers of any reloads already
3723 pushed. */
3725 if (goal_alt_swapped)
3727 if (lra_dump_file != NULL)
3728 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
3729 INSN_UID (curr_insn));
3731 /* Swap the duplicates too. */
3732 swap_operands (commutative);
3733 change_p = true;
3736 #ifdef SECONDARY_MEMORY_NEEDED
3737 /* Some target macros SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
3738 too conservatively. So we use the secondary memory only if there
3739 is no any alternative without reloads. */
3740 use_sec_mem_p = false;
3741 if (! alt_p)
3742 use_sec_mem_p = true;
3743 else if (sec_mem_p)
3745 for (i = 0; i < n_operands; i++)
3746 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
3747 break;
3748 use_sec_mem_p = i < n_operands;
3751 if (use_sec_mem_p)
3753 int in = -1, out = -1;
3754 rtx new_reg, src, dest, rld;
3755 machine_mode sec_mode, rld_mode;
3757 lra_assert (curr_insn_set != NULL_RTX && sec_mem_p);
3758 dest = SET_DEST (curr_insn_set);
3759 src = SET_SRC (curr_insn_set);
3760 for (i = 0; i < n_operands; i++)
3761 if (*curr_id->operand_loc[i] == dest)
3762 out = i;
3763 else if (*curr_id->operand_loc[i] == src)
3764 in = i;
3765 for (i = 0; i < curr_static_id->n_dups; i++)
3766 if (out < 0 && *curr_id->dup_loc[i] == dest)
3767 out = curr_static_id->dup_num[i];
3768 else if (in < 0 && *curr_id->dup_loc[i] == src)
3769 in = curr_static_id->dup_num[i];
3770 lra_assert (out >= 0 && in >= 0
3771 && curr_static_id->operand[out].type == OP_OUT
3772 && curr_static_id->operand[in].type == OP_IN);
3773 rld = (GET_MODE_SIZE (GET_MODE (dest)) <= GET_MODE_SIZE (GET_MODE (src))
3774 ? dest : src);
3775 rld_mode = GET_MODE (rld);
3776 #ifdef SECONDARY_MEMORY_NEEDED_MODE
3777 sec_mode = SECONDARY_MEMORY_NEEDED_MODE (rld_mode);
3778 #else
3779 sec_mode = rld_mode;
3780 #endif
3781 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
3782 NO_REGS, "secondary");
3783 /* If the mode is changed, it should be wider. */
3784 lra_assert (GET_MODE_SIZE (sec_mode) >= GET_MODE_SIZE (rld_mode));
3785 if (sec_mode != rld_mode)
3787 /* If the target says specifically to use another mode for
3788 secondary memory moves we can not reuse the original
3789 insn. */
3790 after = emit_spill_move (false, new_reg, dest);
3791 lra_process_new_insns (curr_insn, NULL, after,
3792 "Inserting the sec. move");
3793 /* We may have non null BEFORE here (e.g. after address
3794 processing. */
3795 push_to_sequence (before);
3796 before = emit_spill_move (true, new_reg, src);
3797 emit_insn (before);
3798 before = get_insns ();
3799 end_sequence ();
3800 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
3801 lra_set_insn_deleted (curr_insn);
3803 else if (dest == rld)
3805 *curr_id->operand_loc[out] = new_reg;
3806 lra_update_dup (curr_id, out);
3807 after = emit_spill_move (false, new_reg, dest);
3808 lra_process_new_insns (curr_insn, NULL, after,
3809 "Inserting the sec. move");
3811 else
3813 *curr_id->operand_loc[in] = new_reg;
3814 lra_update_dup (curr_id, in);
3815 /* See comments above. */
3816 push_to_sequence (before);
3817 before = emit_spill_move (true, new_reg, src);
3818 emit_insn (before);
3819 before = get_insns ();
3820 end_sequence ();
3821 lra_process_new_insns (curr_insn, before, NULL,
3822 "Inserting the sec. move");
3824 lra_update_insn_regno_info (curr_insn);
3825 return true;
3827 #endif
3829 lra_assert (goal_alt_number >= 0);
3830 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
3832 if (lra_dump_file != NULL)
3834 const char *p;
3836 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
3837 goal_alt_number, INSN_UID (curr_insn));
3838 for (i = 0; i < n_operands; i++)
3840 p = (curr_static_id->operand_alternative
3841 [goal_alt_number * n_operands + i].constraint);
3842 if (*p == '\0')
3843 continue;
3844 fprintf (lra_dump_file, " (%d) ", i);
3845 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
3846 fputc (*p, lra_dump_file);
3848 if (INSN_CODE (curr_insn) >= 0
3849 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
3850 fprintf (lra_dump_file, " {%s}", p);
3851 if (curr_id->sp_offset != 0)
3852 fprintf (lra_dump_file, " (sp_off=%" HOST_WIDE_INT_PRINT "d)",
3853 curr_id->sp_offset);
3854 fprintf (lra_dump_file, "\n");
3857 /* Right now, for any pair of operands I and J that are required to
3858 match, with J < I, goal_alt_matches[I] is J. Add I to
3859 goal_alt_matched[J]. */
3861 for (i = 0; i < n_operands; i++)
3862 if ((j = goal_alt_matches[i]) >= 0)
3864 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
3866 /* We allow matching one output operand and several input
3867 operands. */
3868 lra_assert (k == 0
3869 || (curr_static_id->operand[j].type == OP_OUT
3870 && curr_static_id->operand[i].type == OP_IN
3871 && (curr_static_id->operand
3872 [goal_alt_matched[j][0]].type == OP_IN)));
3873 goal_alt_matched[j][k] = i;
3874 goal_alt_matched[j][k + 1] = -1;
3877 for (i = 0; i < n_operands; i++)
3878 goal_alt_win[i] |= goal_alt_match_win[i];
3880 /* Any constants that aren't allowed and can't be reloaded into
3881 registers are here changed into memory references. */
3882 for (i = 0; i < n_operands; i++)
3883 if (goal_alt_win[i])
3885 int regno;
3886 enum reg_class new_class;
3887 rtx reg = *curr_id->operand_loc[i];
3889 if (GET_CODE (reg) == SUBREG)
3890 reg = SUBREG_REG (reg);
3892 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
3894 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
3896 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
3898 lra_assert (ok_p);
3899 lra_change_class (regno, new_class, " Change to", true);
3903 else
3905 const char *constraint;
3906 char c;
3907 rtx op = *curr_id->operand_loc[i];
3908 rtx subreg = NULL_RTX;
3909 machine_mode mode = curr_operand_mode[i];
3911 if (GET_CODE (op) == SUBREG)
3913 subreg = op;
3914 op = SUBREG_REG (op);
3915 mode = GET_MODE (op);
3918 if (CONST_POOL_OK_P (mode, op)
3919 && ((targetm.preferred_reload_class
3920 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
3921 || no_input_reloads_p))
3923 rtx tem = force_const_mem (mode, op);
3925 change_p = true;
3926 if (subreg != NULL_RTX)
3927 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
3929 *curr_id->operand_loc[i] = tem;
3930 lra_update_dup (curr_id, i);
3931 process_address (i, false, &before, &after);
3933 /* If the alternative accepts constant pool refs directly
3934 there will be no reload needed at all. */
3935 if (subreg != NULL_RTX)
3936 continue;
3937 /* Skip alternatives before the one requested. */
3938 constraint = (curr_static_id->operand_alternative
3939 [goal_alt_number * n_operands + i].constraint);
3940 for (;
3941 (c = *constraint) && c != ',' && c != '#';
3942 constraint += CONSTRAINT_LEN (c, constraint))
3944 enum constraint_num cn = lookup_constraint (constraint);
3945 if ((insn_extra_memory_constraint (cn)
3946 || insn_extra_special_memory_constraint (cn))
3947 && satisfies_memory_constraint_p (tem, cn))
3948 break;
3950 if (c == '\0' || c == ',' || c == '#')
3951 continue;
3953 goal_alt_win[i] = true;
3957 n_outputs = 0;
3958 outputs[0] = -1;
3959 for (i = 0; i < n_operands; i++)
3961 int regno;
3962 bool optional_p = false;
3963 rtx old, new_reg;
3964 rtx op = *curr_id->operand_loc[i];
3966 if (goal_alt_win[i])
3968 if (goal_alt[i] == NO_REGS
3969 && REG_P (op)
3970 /* When we assign NO_REGS it means that we will not
3971 assign a hard register to the scratch pseudo by
3972 assigment pass and the scratch pseudo will be
3973 spilled. Spilled scratch pseudos are transformed
3974 back to scratches at the LRA end. */
3975 && lra_former_scratch_operand_p (curr_insn, i)
3976 && lra_former_scratch_p (REGNO (op)))
3978 int regno = REGNO (op);
3979 lra_change_class (regno, NO_REGS, " Change to", true);
3980 if (lra_get_regno_hard_regno (regno) >= 0)
3981 /* We don't have to mark all insn affected by the
3982 spilled pseudo as there is only one such insn, the
3983 current one. */
3984 reg_renumber[regno] = -1;
3985 lra_assert (bitmap_single_bit_set_p
3986 (&lra_reg_info[REGNO (op)].insn_bitmap));
3988 /* We can do an optional reload. If the pseudo got a hard
3989 reg, we might improve the code through inheritance. If
3990 it does not get a hard register we coalesce memory/memory
3991 moves later. Ignore move insns to avoid cycling. */
3992 if (! lra_simple_p
3993 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
3994 && goal_alt[i] != NO_REGS && REG_P (op)
3995 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
3996 && regno < new_regno_start
3997 && ! lra_former_scratch_p (regno)
3998 && reg_renumber[regno] < 0
3999 /* Check that the optional reload pseudo will be able to
4000 hold given mode value. */
4001 && ! (prohibited_class_reg_set_mode_p
4002 (goal_alt[i], reg_class_contents[goal_alt[i]],
4003 PSEUDO_REGNO_MODE (regno)))
4004 && (curr_insn_set == NULL_RTX
4005 || !((REG_P (SET_SRC (curr_insn_set))
4006 || MEM_P (SET_SRC (curr_insn_set))
4007 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
4008 && (REG_P (SET_DEST (curr_insn_set))
4009 || MEM_P (SET_DEST (curr_insn_set))
4010 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
4011 optional_p = true;
4012 else
4013 continue;
4016 /* Operands that match previous ones have already been handled. */
4017 if (goal_alt_matches[i] >= 0)
4018 continue;
4020 /* We should not have an operand with a non-offsettable address
4021 appearing where an offsettable address will do. It also may
4022 be a case when the address should be special in other words
4023 not a general one (e.g. it needs no index reg). */
4024 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
4026 enum reg_class rclass;
4027 rtx *loc = &XEXP (op, 0);
4028 enum rtx_code code = GET_CODE (*loc);
4030 push_to_sequence (before);
4031 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
4032 MEM, SCRATCH);
4033 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
4034 new_reg = emit_inc (rclass, *loc, *loc,
4035 /* This value does not matter for MODIFY. */
4036 GET_MODE_SIZE (GET_MODE (op)));
4037 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
4038 "offsetable address", &new_reg))
4039 lra_emit_move (new_reg, *loc);
4040 before = get_insns ();
4041 end_sequence ();
4042 *loc = new_reg;
4043 lra_update_dup (curr_id, i);
4045 else if (goal_alt_matched[i][0] == -1)
4047 machine_mode mode;
4048 rtx reg, *loc;
4049 int hard_regno, byte;
4050 enum op_type type = curr_static_id->operand[i].type;
4052 loc = curr_id->operand_loc[i];
4053 mode = curr_operand_mode[i];
4054 if (GET_CODE (*loc) == SUBREG)
4056 reg = SUBREG_REG (*loc);
4057 byte = SUBREG_BYTE (*loc);
4058 if (REG_P (reg)
4059 /* Strict_low_part requires reload the register not
4060 the sub-register. */
4061 && (curr_static_id->operand[i].strict_low
4062 || (GET_MODE_SIZE (mode)
4063 <= GET_MODE_SIZE (GET_MODE (reg))
4064 && (hard_regno
4065 = get_try_hard_regno (REGNO (reg))) >= 0
4066 && (simplify_subreg_regno
4067 (hard_regno,
4068 GET_MODE (reg), byte, mode) < 0)
4069 && (goal_alt[i] == NO_REGS
4070 || (simplify_subreg_regno
4071 (ira_class_hard_regs[goal_alt[i]][0],
4072 GET_MODE (reg), byte, mode) >= 0)))))
4074 if (type == OP_OUT)
4075 type = OP_INOUT;
4076 loc = &SUBREG_REG (*loc);
4077 mode = GET_MODE (*loc);
4080 old = *loc;
4081 if (get_reload_reg (type, mode, old, goal_alt[i],
4082 loc != curr_id->operand_loc[i], "", &new_reg)
4083 && type != OP_OUT)
4085 push_to_sequence (before);
4086 lra_emit_move (new_reg, old);
4087 before = get_insns ();
4088 end_sequence ();
4090 *loc = new_reg;
4091 if (type != OP_IN
4092 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
4094 start_sequence ();
4095 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
4096 emit_insn (after);
4097 after = get_insns ();
4098 end_sequence ();
4099 *loc = new_reg;
4101 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
4102 if (goal_alt_dont_inherit_ops[j] == i)
4104 lra_set_regno_unique_value (REGNO (new_reg));
4105 break;
4107 lra_update_dup (curr_id, i);
4109 else if (curr_static_id->operand[i].type == OP_IN
4110 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4111 == OP_OUT))
4113 /* generate reloads for input and matched outputs. */
4114 match_inputs[0] = i;
4115 match_inputs[1] = -1;
4116 match_reload (goal_alt_matched[i][0], match_inputs, outputs,
4117 goal_alt[i], &before, &after,
4118 curr_static_id->operand_alternative
4119 [goal_alt_number * n_operands + goal_alt_matched[i][0]]
4120 .earlyclobber);
4122 else if (curr_static_id->operand[i].type == OP_OUT
4123 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4124 == OP_IN))
4125 /* Generate reloads for output and matched inputs. */
4126 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i], &before,
4127 &after, curr_static_id->operand_alternative
4128 [goal_alt_number * n_operands + i].earlyclobber);
4129 else if (curr_static_id->operand[i].type == OP_IN
4130 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4131 == OP_IN))
4133 /* Generate reloads for matched inputs. */
4134 match_inputs[0] = i;
4135 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
4136 match_inputs[j + 1] = k;
4137 match_inputs[j + 1] = -1;
4138 match_reload (-1, match_inputs, outputs, goal_alt[i], &before,
4139 &after, false);
4141 else
4142 /* We must generate code in any case when function
4143 process_alt_operands decides that it is possible. */
4144 gcc_unreachable ();
4146 /* Memorise processed outputs so that output remaining to be processed
4147 can avoid using the same register value (see match_reload). */
4148 if (curr_static_id->operand[i].type == OP_OUT)
4150 outputs[n_outputs++] = i;
4151 outputs[n_outputs] = -1;
4154 if (optional_p)
4156 rtx reg = op;
4158 lra_assert (REG_P (reg));
4159 regno = REGNO (reg);
4160 op = *curr_id->operand_loc[i]; /* Substitution. */
4161 if (GET_CODE (op) == SUBREG)
4162 op = SUBREG_REG (op);
4163 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
4164 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
4165 lra_reg_info[REGNO (op)].restore_rtx = reg;
4166 if (lra_dump_file != NULL)
4167 fprintf (lra_dump_file,
4168 " Making reload reg %d for reg %d optional\n",
4169 REGNO (op), regno);
4172 if (before != NULL_RTX || after != NULL_RTX
4173 || max_regno_before != max_reg_num ())
4174 change_p = true;
4175 if (change_p)
4177 lra_update_operator_dups (curr_id);
4178 /* Something changes -- process the insn. */
4179 lra_update_insn_regno_info (curr_insn);
4181 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
4182 return change_p;
4185 /* Return true if INSN satisfies all constraints. In other words, no
4186 reload insns are needed. */
4187 bool
4188 lra_constrain_insn (rtx_insn *insn)
4190 int saved_new_regno_start = new_regno_start;
4191 int saved_new_insn_uid_start = new_insn_uid_start;
4192 bool change_p;
4194 curr_insn = insn;
4195 curr_id = lra_get_insn_recog_data (curr_insn);
4196 curr_static_id = curr_id->insn_static_data;
4197 new_insn_uid_start = get_max_uid ();
4198 new_regno_start = max_reg_num ();
4199 change_p = curr_insn_transform (true);
4200 new_regno_start = saved_new_regno_start;
4201 new_insn_uid_start = saved_new_insn_uid_start;
4202 return ! change_p;
4205 /* Return true if X is in LIST. */
4206 static bool
4207 in_list_p (rtx x, rtx list)
4209 for (; list != NULL_RTX; list = XEXP (list, 1))
4210 if (XEXP (list, 0) == x)
4211 return true;
4212 return false;
4215 /* Return true if X contains an allocatable hard register (if
4216 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
4217 static bool
4218 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
4220 int i, j;
4221 const char *fmt;
4222 enum rtx_code code;
4224 code = GET_CODE (x);
4225 if (REG_P (x))
4227 int regno = REGNO (x);
4228 HARD_REG_SET alloc_regs;
4230 if (hard_reg_p)
4232 if (regno >= FIRST_PSEUDO_REGISTER)
4233 regno = lra_get_regno_hard_regno (regno);
4234 if (regno < 0)
4235 return false;
4236 COMPL_HARD_REG_SET (alloc_regs, lra_no_alloc_regs);
4237 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
4239 else
4241 if (regno < FIRST_PSEUDO_REGISTER)
4242 return false;
4243 if (! spilled_p)
4244 return true;
4245 return lra_get_regno_hard_regno (regno) < 0;
4248 fmt = GET_RTX_FORMAT (code);
4249 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4251 if (fmt[i] == 'e')
4253 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
4254 return true;
4256 else if (fmt[i] == 'E')
4258 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4259 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4260 return true;
4263 return false;
4266 /* Process all regs in location *LOC and change them on equivalent
4267 substitution. Return true if any change was done. */
4268 static bool
4269 loc_equivalence_change_p (rtx *loc)
4271 rtx subst, reg, x = *loc;
4272 bool result = false;
4273 enum rtx_code code = GET_CODE (x);
4274 const char *fmt;
4275 int i, j;
4277 if (code == SUBREG)
4279 reg = SUBREG_REG (x);
4280 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4281 && GET_MODE (subst) == VOIDmode)
4283 /* We cannot reload debug location. Simplify subreg here
4284 while we know the inner mode. */
4285 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4286 GET_MODE (reg), SUBREG_BYTE (x));
4287 return true;
4290 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4292 *loc = subst;
4293 return true;
4296 /* Scan all the operand sub-expressions. */
4297 fmt = GET_RTX_FORMAT (code);
4298 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4300 if (fmt[i] == 'e')
4301 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4302 else if (fmt[i] == 'E')
4303 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4304 result
4305 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4307 return result;
4310 /* Similar to loc_equivalence_change_p, but for use as
4311 simplify_replace_fn_rtx callback. DATA is insn for which the
4312 elimination is done. If it null we don't do the elimination. */
4313 static rtx
4314 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4316 if (!REG_P (loc))
4317 return NULL_RTX;
4319 rtx subst = (data == NULL
4320 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4321 if (subst != loc)
4322 return subst;
4324 return NULL_RTX;
4327 /* Maximum number of generated reload insns per an insn. It is for
4328 preventing this pass cycling in a bug case. */
4329 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4331 /* The current iteration number of this LRA pass. */
4332 int lra_constraint_iter;
4334 /* True if we substituted equiv which needs checking register
4335 allocation correctness because the equivalent value contains
4336 allocatable hard registers or when we restore multi-register
4337 pseudo. */
4338 bool lra_risky_transformations_p;
4340 /* Return true if REGNO is referenced in more than one block. */
4341 static bool
4342 multi_block_pseudo_p (int regno)
4344 basic_block bb = NULL;
4345 unsigned int uid;
4346 bitmap_iterator bi;
4348 if (regno < FIRST_PSEUDO_REGISTER)
4349 return false;
4351 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4352 if (bb == NULL)
4353 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4354 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4355 return true;
4356 return false;
4359 /* Return true if LIST contains a deleted insn. */
4360 static bool
4361 contains_deleted_insn_p (rtx_insn_list *list)
4363 for (; list != NULL_RTX; list = list->next ())
4364 if (NOTE_P (list->insn ())
4365 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4366 return true;
4367 return false;
4370 /* Return true if X contains a pseudo dying in INSN. */
4371 static bool
4372 dead_pseudo_p (rtx x, rtx_insn *insn)
4374 int i, j;
4375 const char *fmt;
4376 enum rtx_code code;
4378 if (REG_P (x))
4379 return (insn != NULL_RTX
4380 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4381 code = GET_CODE (x);
4382 fmt = GET_RTX_FORMAT (code);
4383 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4385 if (fmt[i] == 'e')
4387 if (dead_pseudo_p (XEXP (x, i), insn))
4388 return true;
4390 else if (fmt[i] == 'E')
4392 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4393 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4394 return true;
4397 return false;
4400 /* Return true if INSN contains a dying pseudo in INSN right hand
4401 side. */
4402 static bool
4403 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4405 rtx set = single_set (insn);
4407 gcc_assert (set != NULL);
4408 return dead_pseudo_p (SET_SRC (set), insn);
4411 /* Return true if any init insn of REGNO contains a dying pseudo in
4412 insn right hand side. */
4413 static bool
4414 init_insn_rhs_dead_pseudo_p (int regno)
4416 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4418 if (insns == NULL)
4419 return false;
4420 for (; insns != NULL_RTX; insns = insns->next ())
4421 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4422 return true;
4423 return false;
4426 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4427 reverse only if we have one init insn with given REGNO as a
4428 source. */
4429 static bool
4430 reverse_equiv_p (int regno)
4432 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4433 rtx set;
4435 if (insns == NULL)
4436 return false;
4437 if (! INSN_P (insns->insn ())
4438 || insns->next () != NULL)
4439 return false;
4440 if ((set = single_set (insns->insn ())) == NULL_RTX)
4441 return false;
4442 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4445 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4446 call this function only for non-reverse equivalence. */
4447 static bool
4448 contains_reloaded_insn_p (int regno)
4450 rtx set;
4451 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4453 for (; list != NULL; list = list->next ())
4454 if ((set = single_set (list->insn ())) == NULL_RTX
4455 || ! REG_P (SET_DEST (set))
4456 || (int) REGNO (SET_DEST (set)) != regno)
4457 return true;
4458 return false;
4461 /* Entry function of LRA constraint pass. Return true if the
4462 constraint pass did change the code. */
4463 bool
4464 lra_constraints (bool first_p)
4466 bool changed_p;
4467 int i, hard_regno, new_insns_num;
4468 unsigned int min_len, new_min_len, uid;
4469 rtx set, x, reg, dest_reg;
4470 basic_block last_bb;
4471 bitmap_head equiv_insn_bitmap;
4472 bitmap_iterator bi;
4474 lra_constraint_iter++;
4475 if (lra_dump_file != NULL)
4476 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4477 lra_constraint_iter);
4478 changed_p = false;
4479 if (pic_offset_table_rtx
4480 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4481 lra_risky_transformations_p = true;
4482 else
4483 lra_risky_transformations_p = false;
4484 new_insn_uid_start = get_max_uid ();
4485 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4486 /* Mark used hard regs for target stack size calulations. */
4487 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4488 if (lra_reg_info[i].nrefs != 0
4489 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4491 int j, nregs;
4493 nregs = hard_regno_nregs[hard_regno][lra_reg_info[i].biggest_mode];
4494 for (j = 0; j < nregs; j++)
4495 df_set_regs_ever_live (hard_regno + j, true);
4497 /* Do elimination before the equivalence processing as we can spill
4498 some pseudos during elimination. */
4499 lra_eliminate (false, first_p);
4500 bitmap_initialize (&equiv_insn_bitmap, &reg_obstack);
4501 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4502 if (lra_reg_info[i].nrefs != 0)
4504 ira_reg_equiv[i].profitable_p = true;
4505 reg = regno_reg_rtx[i];
4506 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4508 bool pseudo_p = contains_reg_p (x, false, false);
4510 /* After RTL transformation, we can not guarantee that
4511 pseudo in the substitution was not reloaded which might
4512 make equivalence invalid. For example, in reverse
4513 equiv of p0
4515 p0 <- ...
4517 equiv_mem <- p0
4519 the memory address register was reloaded before the 2nd
4520 insn. */
4521 if ((! first_p && pseudo_p)
4522 /* We don't use DF for compilation speed sake. So it
4523 is problematic to update live info when we use an
4524 equivalence containing pseudos in more than one
4525 BB. */
4526 || (pseudo_p && multi_block_pseudo_p (i))
4527 /* If an init insn was deleted for some reason, cancel
4528 the equiv. We could update the equiv insns after
4529 transformations including an equiv insn deletion
4530 but it is not worthy as such cases are extremely
4531 rare. */
4532 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4533 /* If it is not a reverse equivalence, we check that a
4534 pseudo in rhs of the init insn is not dying in the
4535 insn. Otherwise, the live info at the beginning of
4536 the corresponding BB might be wrong after we
4537 removed the insn. When the equiv can be a
4538 constant, the right hand side of the init insn can
4539 be a pseudo. */
4540 || (! reverse_equiv_p (i)
4541 && (init_insn_rhs_dead_pseudo_p (i)
4542 /* If we reloaded the pseudo in an equivalence
4543 init insn, we can not remove the equiv init
4544 insns and the init insns might write into
4545 const memory in this case. */
4546 || contains_reloaded_insn_p (i)))
4547 /* Prevent access beyond equivalent memory for
4548 paradoxical subregs. */
4549 || (MEM_P (x)
4550 && (GET_MODE_SIZE (lra_reg_info[i].biggest_mode)
4551 > GET_MODE_SIZE (GET_MODE (x))))
4552 || (pic_offset_table_rtx
4553 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4554 && (targetm.preferred_reload_class
4555 (x, lra_get_allocno_class (i)) == NO_REGS))
4556 || contains_symbol_ref_p (x))))
4557 ira_reg_equiv[i].defined_p = false;
4558 if (contains_reg_p (x, false, true))
4559 ira_reg_equiv[i].profitable_p = false;
4560 if (get_equiv (reg) != reg)
4561 bitmap_ior_into (&equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4564 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4565 update_equiv (i);
4566 /* We should add all insns containing pseudos which should be
4567 substituted by their equivalences. */
4568 EXECUTE_IF_SET_IN_BITMAP (&equiv_insn_bitmap, 0, uid, bi)
4569 lra_push_insn_by_uid (uid);
4570 min_len = lra_insn_stack_length ();
4571 new_insns_num = 0;
4572 last_bb = NULL;
4573 changed_p = false;
4574 while ((new_min_len = lra_insn_stack_length ()) != 0)
4576 curr_insn = lra_pop_insn ();
4577 --new_min_len;
4578 curr_bb = BLOCK_FOR_INSN (curr_insn);
4579 if (curr_bb != last_bb)
4581 last_bb = curr_bb;
4582 bb_reload_num = lra_curr_reload_num;
4584 if (min_len > new_min_len)
4586 min_len = new_min_len;
4587 new_insns_num = 0;
4589 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
4590 internal_error
4591 ("Max. number of generated reload insns per insn is achieved (%d)\n",
4592 MAX_RELOAD_INSNS_NUMBER);
4593 new_insns_num++;
4594 if (DEBUG_INSN_P (curr_insn))
4596 /* We need to check equivalence in debug insn and change
4597 pseudo to the equivalent value if necessary. */
4598 curr_id = lra_get_insn_recog_data (curr_insn);
4599 if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn)))
4601 rtx old = *curr_id->operand_loc[0];
4602 *curr_id->operand_loc[0]
4603 = simplify_replace_fn_rtx (old, NULL_RTX,
4604 loc_equivalence_callback, curr_insn);
4605 if (old != *curr_id->operand_loc[0])
4607 lra_update_insn_regno_info (curr_insn);
4608 changed_p = true;
4612 else if (INSN_P (curr_insn))
4614 if ((set = single_set (curr_insn)) != NULL_RTX)
4616 dest_reg = SET_DEST (set);
4617 /* The equivalence pseudo could be set up as SUBREG in a
4618 case when it is a call restore insn in a mode
4619 different from the pseudo mode. */
4620 if (GET_CODE (dest_reg) == SUBREG)
4621 dest_reg = SUBREG_REG (dest_reg);
4622 if ((REG_P (dest_reg)
4623 && (x = get_equiv (dest_reg)) != dest_reg
4624 /* Remove insns which set up a pseudo whose value
4625 can not be changed. Such insns might be not in
4626 init_insns because we don't update equiv data
4627 during insn transformations.
4629 As an example, let suppose that a pseudo got
4630 hard register and on the 1st pass was not
4631 changed to equivalent constant. We generate an
4632 additional insn setting up the pseudo because of
4633 secondary memory movement. Then the pseudo is
4634 spilled and we use the equiv constant. In this
4635 case we should remove the additional insn and
4636 this insn is not init_insns list. */
4637 && (! MEM_P (x) || MEM_READONLY_P (x)
4638 /* Check that this is actually an insn setting
4639 up the equivalence. */
4640 || in_list_p (curr_insn,
4641 ira_reg_equiv
4642 [REGNO (dest_reg)].init_insns)))
4643 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
4644 && in_list_p (curr_insn,
4645 ira_reg_equiv
4646 [REGNO (SET_SRC (set))].init_insns)))
4648 /* This is equiv init insn of pseudo which did not get a
4649 hard register -- remove the insn. */
4650 if (lra_dump_file != NULL)
4652 fprintf (lra_dump_file,
4653 " Removing equiv init insn %i (freq=%d)\n",
4654 INSN_UID (curr_insn),
4655 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
4656 dump_insn_slim (lra_dump_file, curr_insn);
4658 if (contains_reg_p (x, true, false))
4659 lra_risky_transformations_p = true;
4660 lra_set_insn_deleted (curr_insn);
4661 continue;
4664 curr_id = lra_get_insn_recog_data (curr_insn);
4665 curr_static_id = curr_id->insn_static_data;
4666 init_curr_insn_input_reloads ();
4667 init_curr_operand_mode ();
4668 if (curr_insn_transform (false))
4669 changed_p = true;
4670 /* Check non-transformed insns too for equiv change as USE
4671 or CLOBBER don't need reloads but can contain pseudos
4672 being changed on their equivalences. */
4673 else if (bitmap_bit_p (&equiv_insn_bitmap, INSN_UID (curr_insn))
4674 && loc_equivalence_change_p (&PATTERN (curr_insn)))
4676 lra_update_insn_regno_info (curr_insn);
4677 changed_p = true;
4681 bitmap_clear (&equiv_insn_bitmap);
4682 /* If we used a new hard regno, changed_p should be true because the
4683 hard reg is assigned to a new pseudo. */
4684 if (flag_checking && !changed_p)
4686 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4687 if (lra_reg_info[i].nrefs != 0
4688 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4690 int j, nregs = hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (i)];
4692 for (j = 0; j < nregs; j++)
4693 lra_assert (df_regs_ever_live_p (hard_regno + j));
4696 return changed_p;
4699 static void initiate_invariants (void);
4700 static void finish_invariants (void);
4702 /* Initiate the LRA constraint pass. It is done once per
4703 function. */
4704 void
4705 lra_constraints_init (void)
4707 initiate_invariants ();
4710 /* Finalize the LRA constraint pass. It is done once per
4711 function. */
4712 void
4713 lra_constraints_finish (void)
4715 finish_invariants ();
4720 /* Structure describes invariants for ineheritance. */
4721 struct invariant
4723 /* The order number of the invariant. */
4724 int num;
4725 /* The invariant RTX. */
4726 rtx invariant_rtx;
4727 /* The origin insn of the invariant. */
4728 rtx_insn *insn;
4731 typedef struct invariant invariant_t;
4732 typedef invariant_t *invariant_ptr_t;
4733 typedef const invariant_t *const_invariant_ptr_t;
4735 /* Pointer to the inheritance invariants. */
4736 static vec<invariant_ptr_t> invariants;
4738 /* Allocation pool for the invariants. */
4739 static object_allocator<struct invariant> *invariants_pool;
4741 /* Hash table for the invariants. */
4742 static htab_t invariant_table;
4744 /* Hash function for INVARIANT. */
4745 static hashval_t
4746 invariant_hash (const void *invariant)
4748 rtx inv = ((const_invariant_ptr_t) invariant)->invariant_rtx;
4749 return lra_rtx_hash (inv);
4752 /* Equal function for invariants INVARIANT1 and INVARIANT2. */
4753 static int
4754 invariant_eq_p (const void *invariant1, const void *invariant2)
4756 rtx inv1 = ((const_invariant_ptr_t) invariant1)->invariant_rtx;
4757 rtx inv2 = ((const_invariant_ptr_t) invariant2)->invariant_rtx;
4759 return rtx_equal_p (inv1, inv2);
4762 /* Insert INVARIANT_RTX into the table if it is not there yet. Return
4763 invariant which is in the table. */
4764 static invariant_ptr_t
4765 insert_invariant (rtx invariant_rtx)
4767 void **entry_ptr;
4768 invariant_t invariant;
4769 invariant_ptr_t invariant_ptr;
4771 invariant.invariant_rtx = invariant_rtx;
4772 entry_ptr = htab_find_slot (invariant_table, &invariant, INSERT);
4773 if (*entry_ptr == NULL)
4775 invariant_ptr = invariants_pool->allocate ();
4776 invariant_ptr->invariant_rtx = invariant_rtx;
4777 invariant_ptr->insn = NULL;
4778 invariants.safe_push (invariant_ptr);
4779 *entry_ptr = (void *) invariant_ptr;
4781 return (invariant_ptr_t) *entry_ptr;
4784 /* Initiate the invariant table. */
4785 static void
4786 initiate_invariants (void)
4788 invariants.create (100);
4789 invariants_pool = new object_allocator<struct invariant> ("Inheritance invariants");
4790 invariant_table = htab_create (100, invariant_hash, invariant_eq_p, NULL);
4793 /* Finish the invariant table. */
4794 static void
4795 finish_invariants (void)
4797 htab_delete (invariant_table);
4798 delete invariants_pool;
4799 invariants.release ();
4802 /* Make the invariant table empty. */
4803 static void
4804 clear_invariants (void)
4806 htab_empty (invariant_table);
4807 invariants_pool->release ();
4808 invariants.truncate (0);
4813 /* This page contains code to do inheritance/split
4814 transformations. */
4816 /* Number of reloads passed so far in current EBB. */
4817 static int reloads_num;
4819 /* Number of calls passed so far in current EBB. */
4820 static int calls_num;
4822 /* Current reload pseudo check for validity of elements in
4823 USAGE_INSNS. */
4824 static int curr_usage_insns_check;
4826 /* Info about last usage of registers in EBB to do inheritance/split
4827 transformation. Inheritance transformation is done from a spilled
4828 pseudo and split transformations from a hard register or a pseudo
4829 assigned to a hard register. */
4830 struct usage_insns
4832 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
4833 value INSNS is valid. The insns is chain of optional debug insns
4834 and a finishing non-debug insn using the corresponding reg. The
4835 value is also used to mark the registers which are set up in the
4836 current insn. The negated insn uid is used for this. */
4837 int check;
4838 /* Value of global reloads_num at the last insn in INSNS. */
4839 int reloads_num;
4840 /* Value of global reloads_nums at the last insn in INSNS. */
4841 int calls_num;
4842 /* It can be true only for splitting. And it means that the restore
4843 insn should be put after insn given by the following member. */
4844 bool after_p;
4845 /* Next insns in the current EBB which use the original reg and the
4846 original reg value is not changed between the current insn and
4847 the next insns. In order words, e.g. for inheritance, if we need
4848 to use the original reg value again in the next insns we can try
4849 to use the value in a hard register from a reload insn of the
4850 current insn. */
4851 rtx insns;
4854 /* Map: regno -> corresponding pseudo usage insns. */
4855 static struct usage_insns *usage_insns;
4857 static void
4858 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
4860 usage_insns[regno].check = curr_usage_insns_check;
4861 usage_insns[regno].insns = insn;
4862 usage_insns[regno].reloads_num = reloads_num;
4863 usage_insns[regno].calls_num = calls_num;
4864 usage_insns[regno].after_p = after_p;
4867 /* The function is used to form list REGNO usages which consists of
4868 optional debug insns finished by a non-debug insn using REGNO.
4869 RELOADS_NUM is current number of reload insns processed so far. */
4870 static void
4871 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
4873 rtx next_usage_insns;
4875 if (usage_insns[regno].check == curr_usage_insns_check
4876 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
4877 && DEBUG_INSN_P (insn))
4879 /* Check that we did not add the debug insn yet. */
4880 if (next_usage_insns != insn
4881 && (GET_CODE (next_usage_insns) != INSN_LIST
4882 || XEXP (next_usage_insns, 0) != insn))
4883 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
4884 next_usage_insns);
4886 else if (NONDEBUG_INSN_P (insn))
4887 setup_next_usage_insn (regno, insn, reloads_num, false);
4888 else
4889 usage_insns[regno].check = 0;
4892 /* Return first non-debug insn in list USAGE_INSNS. */
4893 static rtx_insn *
4894 skip_usage_debug_insns (rtx usage_insns)
4896 rtx insn;
4898 /* Skip debug insns. */
4899 for (insn = usage_insns;
4900 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
4901 insn = XEXP (insn, 1))
4903 return safe_as_a <rtx_insn *> (insn);
4906 /* Return true if we need secondary memory moves for insn in
4907 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
4908 into the insn. */
4909 static bool
4910 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
4911 rtx usage_insns ATTRIBUTE_UNUSED)
4913 #ifndef SECONDARY_MEMORY_NEEDED
4914 return false;
4915 #else
4916 rtx_insn *insn;
4917 rtx set, dest;
4918 enum reg_class cl;
4920 if (inher_cl == ALL_REGS
4921 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
4922 return false;
4923 lra_assert (INSN_P (insn));
4924 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
4925 return false;
4926 dest = SET_DEST (set);
4927 if (! REG_P (dest))
4928 return false;
4929 lra_assert (inher_cl != NO_REGS);
4930 cl = get_reg_class (REGNO (dest));
4931 return (cl != NO_REGS && cl != ALL_REGS
4932 && SECONDARY_MEMORY_NEEDED (inher_cl, cl, GET_MODE (dest)));
4933 #endif
4936 /* Registers involved in inheritance/split in the current EBB
4937 (inheritance/split pseudos and original registers). */
4938 static bitmap_head check_only_regs;
4940 /* Reload pseudos can not be involded in invariant inheritance in the
4941 current EBB. */
4942 static bitmap_head invalid_invariant_regs;
4944 /* Do inheritance transformations for insn INSN, which defines (if
4945 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
4946 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
4947 form as the "insns" field of usage_insns. Return true if we
4948 succeed in such transformation.
4950 The transformations look like:
4952 p <- ... i <- ...
4953 ... p <- i (new insn)
4954 ... =>
4955 <- ... p ... <- ... i ...
4957 ... i <- p (new insn)
4958 <- ... p ... <- ... i ...
4959 ... =>
4960 <- ... p ... <- ... i ...
4961 where p is a spilled original pseudo and i is a new inheritance pseudo.
4964 The inheritance pseudo has the smallest class of two classes CL and
4965 class of ORIGINAL REGNO. */
4966 static bool
4967 inherit_reload_reg (bool def_p, int original_regno,
4968 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
4970 if (optimize_function_for_size_p (cfun))
4971 return false;
4973 enum reg_class rclass = lra_get_allocno_class (original_regno);
4974 rtx original_reg = regno_reg_rtx[original_regno];
4975 rtx new_reg, usage_insn;
4976 rtx_insn *new_insns;
4978 lra_assert (! usage_insns[original_regno].after_p);
4979 if (lra_dump_file != NULL)
4980 fprintf (lra_dump_file,
4981 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
4982 if (! ira_reg_classes_intersect_p[cl][rclass])
4984 if (lra_dump_file != NULL)
4986 fprintf (lra_dump_file,
4987 " Rejecting inheritance for %d "
4988 "because of disjoint classes %s and %s\n",
4989 original_regno, reg_class_names[cl],
4990 reg_class_names[rclass]);
4991 fprintf (lra_dump_file,
4992 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
4994 return false;
4996 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
4997 /* We don't use a subset of two classes because it can be
4998 NO_REGS. This transformation is still profitable in most
4999 cases even if the classes are not intersected as register
5000 move is probably cheaper than a memory load. */
5001 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
5003 if (lra_dump_file != NULL)
5004 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
5005 reg_class_names[cl], reg_class_names[rclass]);
5007 rclass = cl;
5009 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
5011 /* Reject inheritance resulting in secondary memory moves.
5012 Otherwise, there is a danger in LRA cycling. Also such
5013 transformation will be unprofitable. */
5014 if (lra_dump_file != NULL)
5016 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
5017 rtx set = single_set (insn);
5019 lra_assert (set != NULL_RTX);
5021 rtx dest = SET_DEST (set);
5023 lra_assert (REG_P (dest));
5024 fprintf (lra_dump_file,
5025 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
5026 "as secondary mem is needed\n",
5027 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
5028 original_regno, reg_class_names[rclass]);
5029 fprintf (lra_dump_file,
5030 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5032 return false;
5034 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
5035 rclass, "inheritance");
5036 start_sequence ();
5037 if (def_p)
5038 lra_emit_move (original_reg, new_reg);
5039 else
5040 lra_emit_move (new_reg, original_reg);
5041 new_insns = get_insns ();
5042 end_sequence ();
5043 if (NEXT_INSN (new_insns) != NULL_RTX)
5045 if (lra_dump_file != NULL)
5047 fprintf (lra_dump_file,
5048 " Rejecting inheritance %d->%d "
5049 "as it results in 2 or more insns:\n",
5050 original_regno, REGNO (new_reg));
5051 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
5052 fprintf (lra_dump_file,
5053 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5055 return false;
5057 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
5058 lra_update_insn_regno_info (insn);
5059 if (! def_p)
5060 /* We now have a new usage insn for original regno. */
5061 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
5062 if (lra_dump_file != NULL)
5063 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
5064 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
5065 lra_reg_info[REGNO (new_reg)].restore_rtx = regno_reg_rtx[original_regno];
5066 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5067 bitmap_set_bit (&check_only_regs, original_regno);
5068 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
5069 if (def_p)
5070 lra_process_new_insns (insn, NULL, new_insns,
5071 "Add original<-inheritance");
5072 else
5073 lra_process_new_insns (insn, new_insns, NULL,
5074 "Add inheritance<-original");
5075 while (next_usage_insns != NULL_RTX)
5077 if (GET_CODE (next_usage_insns) != INSN_LIST)
5079 usage_insn = next_usage_insns;
5080 lra_assert (NONDEBUG_INSN_P (usage_insn));
5081 next_usage_insns = NULL;
5083 else
5085 usage_insn = XEXP (next_usage_insns, 0);
5086 lra_assert (DEBUG_INSN_P (usage_insn));
5087 next_usage_insns = XEXP (next_usage_insns, 1);
5089 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
5090 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5091 if (lra_dump_file != NULL)
5093 fprintf (lra_dump_file,
5094 " Inheritance reuse change %d->%d (bb%d):\n",
5095 original_regno, REGNO (new_reg),
5096 BLOCK_FOR_INSN (usage_insn)->index);
5097 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5100 if (lra_dump_file != NULL)
5101 fprintf (lra_dump_file,
5102 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5103 return true;
5106 /* Return true if we need a caller save/restore for pseudo REGNO which
5107 was assigned to a hard register. */
5108 static inline bool
5109 need_for_call_save_p (int regno)
5111 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
5112 return (usage_insns[regno].calls_num < calls_num
5113 && (overlaps_hard_reg_set_p
5114 ((flag_ipa_ra &&
5115 ! hard_reg_set_empty_p (lra_reg_info[regno].actual_call_used_reg_set))
5116 ? lra_reg_info[regno].actual_call_used_reg_set
5117 : call_used_reg_set,
5118 PSEUDO_REGNO_MODE (regno), reg_renumber[regno])
5119 || HARD_REGNO_CALL_PART_CLOBBERED (reg_renumber[regno],
5120 PSEUDO_REGNO_MODE (regno))));
5123 /* Global registers occurring in the current EBB. */
5124 static bitmap_head ebb_global_regs;
5126 /* Return true if we need a split for hard register REGNO or pseudo
5127 REGNO which was assigned to a hard register.
5128 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
5129 used for reloads since the EBB end. It is an approximation of the
5130 used hard registers in the split range. The exact value would
5131 require expensive calculations. If we were aggressive with
5132 splitting because of the approximation, the split pseudo will save
5133 the same hard register assignment and will be removed in the undo
5134 pass. We still need the approximation because too aggressive
5135 splitting would result in too inaccurate cost calculation in the
5136 assignment pass because of too many generated moves which will be
5137 probably removed in the undo pass. */
5138 static inline bool
5139 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
5141 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
5143 lra_assert (hard_regno >= 0);
5144 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
5145 /* Don't split eliminable hard registers, otherwise we can
5146 split hard registers like hard frame pointer, which
5147 lives on BB start/end according to DF-infrastructure,
5148 when there is a pseudo assigned to the register and
5149 living in the same BB. */
5150 && (regno >= FIRST_PSEUDO_REGISTER
5151 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
5152 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
5153 /* Don't split call clobbered hard regs living through
5154 calls, otherwise we might have a check problem in the
5155 assign sub-pass as in the most cases (exception is a
5156 situation when lra_risky_transformations_p value is
5157 true) the assign pass assumes that all pseudos living
5158 through calls are assigned to call saved hard regs. */
5159 && (regno >= FIRST_PSEUDO_REGISTER
5160 || ! TEST_HARD_REG_BIT (call_used_reg_set, regno)
5161 || usage_insns[regno].calls_num == calls_num)
5162 /* We need at least 2 reloads to make pseudo splitting
5163 profitable. We should provide hard regno splitting in
5164 any case to solve 1st insn scheduling problem when
5165 moving hard register definition up might result in
5166 impossibility to find hard register for reload pseudo of
5167 small register class. */
5168 && (usage_insns[regno].reloads_num
5169 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
5170 && (regno < FIRST_PSEUDO_REGISTER
5171 /* For short living pseudos, spilling + inheritance can
5172 be considered a substitution for splitting.
5173 Therefore we do not splitting for local pseudos. It
5174 decreases also aggressiveness of splitting. The
5175 minimal number of references is chosen taking into
5176 account that for 2 references splitting has no sense
5177 as we can just spill the pseudo. */
5178 || (regno >= FIRST_PSEUDO_REGISTER
5179 && lra_reg_info[regno].nrefs > 3
5180 && bitmap_bit_p (&ebb_global_regs, regno))))
5181 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
5184 /* Return class for the split pseudo created from original pseudo with
5185 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
5186 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
5187 results in no secondary memory movements. */
5188 static enum reg_class
5189 choose_split_class (enum reg_class allocno_class,
5190 int hard_regno ATTRIBUTE_UNUSED,
5191 machine_mode mode ATTRIBUTE_UNUSED)
5193 #ifndef SECONDARY_MEMORY_NEEDED
5194 return allocno_class;
5195 #else
5196 int i;
5197 enum reg_class cl, best_cl = NO_REGS;
5198 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
5199 = REGNO_REG_CLASS (hard_regno);
5201 if (! SECONDARY_MEMORY_NEEDED (allocno_class, allocno_class, mode)
5202 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
5203 return allocno_class;
5204 for (i = 0;
5205 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
5206 i++)
5207 if (! SECONDARY_MEMORY_NEEDED (cl, hard_reg_class, mode)
5208 && ! SECONDARY_MEMORY_NEEDED (hard_reg_class, cl, mode)
5209 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
5210 && (best_cl == NO_REGS
5211 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
5212 best_cl = cl;
5213 return best_cl;
5214 #endif
5217 /* Do split transformations for insn INSN, which defines or uses
5218 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
5219 the EBB next uses ORIGINAL_REGNO; it has the same form as the
5220 "insns" field of usage_insns.
5222 The transformations look like:
5224 p <- ... p <- ...
5225 ... s <- p (new insn -- save)
5226 ... =>
5227 ... p <- s (new insn -- restore)
5228 <- ... p ... <- ... p ...
5230 <- ... p ... <- ... p ...
5231 ... s <- p (new insn -- save)
5232 ... =>
5233 ... p <- s (new insn -- restore)
5234 <- ... p ... <- ... p ...
5236 where p is an original pseudo got a hard register or a hard
5237 register and s is a new split pseudo. The save is put before INSN
5238 if BEFORE_P is true. Return true if we succeed in such
5239 transformation. */
5240 static bool
5241 split_reg (bool before_p, int original_regno, rtx_insn *insn,
5242 rtx next_usage_insns)
5244 enum reg_class rclass;
5245 rtx original_reg;
5246 int hard_regno, nregs;
5247 rtx new_reg, usage_insn;
5248 rtx_insn *restore, *save;
5249 bool after_p;
5250 bool call_save_p;
5251 machine_mode mode;
5253 if (original_regno < FIRST_PSEUDO_REGISTER)
5255 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
5256 hard_regno = original_regno;
5257 call_save_p = false;
5258 nregs = 1;
5259 mode = lra_reg_info[hard_regno].biggest_mode;
5260 machine_mode reg_rtx_mode = GET_MODE (regno_reg_rtx[hard_regno]);
5261 /* A reg can have a biggest_mode of VOIDmode if it was only ever seen
5262 as part of a multi-word register. In that case, or if the biggest
5263 mode was larger than a register, just use the reg_rtx. Otherwise,
5264 limit the size to that of the biggest access in the function. */
5265 if (mode == VOIDmode
5266 || GET_MODE_SIZE (mode) > GET_MODE_SIZE (reg_rtx_mode))
5268 original_reg = regno_reg_rtx[hard_regno];
5269 mode = reg_rtx_mode;
5271 else
5272 original_reg = gen_rtx_REG (mode, hard_regno);
5274 else
5276 mode = PSEUDO_REGNO_MODE (original_regno);
5277 hard_regno = reg_renumber[original_regno];
5278 nregs = hard_regno_nregs[hard_regno][mode];
5279 rclass = lra_get_allocno_class (original_regno);
5280 original_reg = regno_reg_rtx[original_regno];
5281 call_save_p = need_for_call_save_p (original_regno);
5283 lra_assert (hard_regno >= 0);
5284 if (lra_dump_file != NULL)
5285 fprintf (lra_dump_file,
5286 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
5288 if (call_save_p)
5290 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
5291 hard_regno_nregs[hard_regno][mode],
5292 mode);
5293 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
5295 else
5297 rclass = choose_split_class (rclass, hard_regno, mode);
5298 if (rclass == NO_REGS)
5300 if (lra_dump_file != NULL)
5302 fprintf (lra_dump_file,
5303 " Rejecting split of %d(%s): "
5304 "no good reg class for %d(%s)\n",
5305 original_regno,
5306 reg_class_names[lra_get_allocno_class (original_regno)],
5307 hard_regno,
5308 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
5309 fprintf
5310 (lra_dump_file,
5311 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5313 return false;
5315 new_reg = lra_create_new_reg (mode, original_reg, rclass, "split");
5316 reg_renumber[REGNO (new_reg)] = hard_regno;
5318 save = emit_spill_move (true, new_reg, original_reg);
5319 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
5321 if (lra_dump_file != NULL)
5323 fprintf
5324 (lra_dump_file,
5325 " Rejecting split %d->%d resulting in > 2 save insns:\n",
5326 original_regno, REGNO (new_reg));
5327 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
5328 fprintf (lra_dump_file,
5329 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5331 return false;
5333 restore = emit_spill_move (false, new_reg, original_reg);
5334 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
5336 if (lra_dump_file != NULL)
5338 fprintf (lra_dump_file,
5339 " Rejecting split %d->%d "
5340 "resulting in > 2 restore insns:\n",
5341 original_regno, REGNO (new_reg));
5342 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
5343 fprintf (lra_dump_file,
5344 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5346 return false;
5348 after_p = usage_insns[original_regno].after_p;
5349 lra_reg_info[REGNO (new_reg)].restore_rtx = regno_reg_rtx[original_regno];
5350 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5351 bitmap_set_bit (&check_only_regs, original_regno);
5352 bitmap_set_bit (&lra_split_regs, REGNO (new_reg));
5353 for (;;)
5355 if (GET_CODE (next_usage_insns) != INSN_LIST)
5357 usage_insn = next_usage_insns;
5358 break;
5360 usage_insn = XEXP (next_usage_insns, 0);
5361 lra_assert (DEBUG_INSN_P (usage_insn));
5362 next_usage_insns = XEXP (next_usage_insns, 1);
5363 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false);
5364 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5365 if (lra_dump_file != NULL)
5367 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5368 original_regno, REGNO (new_reg));
5369 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5372 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5373 lra_assert (usage_insn != insn || (after_p && before_p));
5374 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5375 after_p ? NULL : restore,
5376 after_p ? restore : NULL,
5377 call_save_p
5378 ? "Add reg<-save" : "Add reg<-split");
5379 lra_process_new_insns (insn, before_p ? save : NULL,
5380 before_p ? NULL : save,
5381 call_save_p
5382 ? "Add save<-reg" : "Add split<-reg");
5383 if (nregs > 1)
5384 /* If we are trying to split multi-register. We should check
5385 conflicts on the next assignment sub-pass. IRA can allocate on
5386 sub-register levels, LRA do this on pseudos level right now and
5387 this discrepancy may create allocation conflicts after
5388 splitting. */
5389 lra_risky_transformations_p = true;
5390 if (lra_dump_file != NULL)
5391 fprintf (lra_dump_file,
5392 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5393 return true;
5396 /* Recognize that we need a split transformation for insn INSN, which
5397 defines or uses REGNO in its insn biggest MODE (we use it only if
5398 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5399 hard registers which might be used for reloads since the EBB end.
5400 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5401 uid before starting INSN processing. Return true if we succeed in
5402 such transformation. */
5403 static bool
5404 split_if_necessary (int regno, machine_mode mode,
5405 HARD_REG_SET potential_reload_hard_regs,
5406 bool before_p, rtx_insn *insn, int max_uid)
5408 bool res = false;
5409 int i, nregs = 1;
5410 rtx next_usage_insns;
5412 if (regno < FIRST_PSEUDO_REGISTER)
5413 nregs = hard_regno_nregs[regno][mode];
5414 for (i = 0; i < nregs; i++)
5415 if (usage_insns[regno + i].check == curr_usage_insns_check
5416 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5417 /* To avoid processing the register twice or more. */
5418 && ((GET_CODE (next_usage_insns) != INSN_LIST
5419 && INSN_UID (next_usage_insns) < max_uid)
5420 || (GET_CODE (next_usage_insns) == INSN_LIST
5421 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5422 && need_for_split_p (potential_reload_hard_regs, regno + i)
5423 && split_reg (before_p, regno + i, insn, next_usage_insns))
5424 res = true;
5425 return res;
5428 /* Return TRUE if rtx X is considered as an invariant for
5429 inheritance. */
5430 static bool
5431 invariant_p (const_rtx x)
5433 machine_mode mode;
5434 const char *fmt;
5435 enum rtx_code code;
5436 int i, j;
5438 code = GET_CODE (x);
5439 mode = GET_MODE (x);
5440 if (code == SUBREG)
5442 x = SUBREG_REG (x);
5443 code = GET_CODE (x);
5444 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (mode))
5445 mode = GET_MODE (x);
5448 if (MEM_P (x))
5449 return false;
5451 if (REG_P (x))
5453 int i, nregs, regno = REGNO (x);
5455 if (regno >= FIRST_PSEUDO_REGISTER || regno == STACK_POINTER_REGNUM
5456 || TEST_HARD_REG_BIT (eliminable_regset, regno)
5457 || GET_MODE_CLASS (GET_MODE (x)) == MODE_CC)
5458 return false;
5459 nregs = hard_regno_nregs[regno][mode];
5460 for (i = 0; i < nregs; i++)
5461 if (! fixed_regs[regno + i]
5462 /* A hard register may be clobbered in the current insn
5463 but we can ignore this case because if the hard
5464 register is used it should be set somewhere after the
5465 clobber. */
5466 || bitmap_bit_p (&invalid_invariant_regs, regno + i))
5467 return false;
5469 fmt = GET_RTX_FORMAT (code);
5470 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5472 if (fmt[i] == 'e')
5474 if (! invariant_p (XEXP (x, i)))
5475 return false;
5477 else if (fmt[i] == 'E')
5479 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5480 if (! invariant_p (XVECEXP (x, i, j)))
5481 return false;
5484 return true;
5487 /* We have 'dest_reg <- invariant'. Let us try to make an invariant
5488 inheritance transformation (using dest_reg instead invariant in a
5489 subsequent insn). */
5490 static bool
5491 process_invariant_for_inheritance (rtx dst_reg, rtx invariant_rtx)
5493 invariant_ptr_t invariant_ptr;
5494 rtx_insn *insn, *new_insns;
5495 rtx insn_set, insn_reg, new_reg;
5496 int insn_regno;
5497 bool succ_p = false;
5498 int dst_regno = REGNO (dst_reg);
5499 enum machine_mode dst_mode = GET_MODE (dst_reg);
5500 enum reg_class cl = lra_get_allocno_class (dst_regno), insn_reg_cl;
5502 invariant_ptr = insert_invariant (invariant_rtx);
5503 if ((insn = invariant_ptr->insn) != NULL_RTX)
5505 /* We have a subsequent insn using the invariant. */
5506 insn_set = single_set (insn);
5507 lra_assert (insn_set != NULL);
5508 insn_reg = SET_DEST (insn_set);
5509 lra_assert (REG_P (insn_reg));
5510 insn_regno = REGNO (insn_reg);
5511 insn_reg_cl = lra_get_allocno_class (insn_regno);
5513 if (dst_mode == GET_MODE (insn_reg)
5514 /* We should consider only result move reg insns which are
5515 cheap. */
5516 && targetm.register_move_cost (dst_mode, cl, insn_reg_cl) == 2
5517 && targetm.register_move_cost (dst_mode, cl, cl) == 2)
5519 if (lra_dump_file != NULL)
5520 fprintf (lra_dump_file,
5521 " [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[\n");
5522 new_reg = lra_create_new_reg (dst_mode, dst_reg,
5523 cl, "invariant inheritance");
5524 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
5525 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5526 lra_reg_info[REGNO (new_reg)].restore_rtx = PATTERN (insn);
5527 start_sequence ();
5528 lra_emit_move (new_reg, dst_reg);
5529 new_insns = get_insns ();
5530 end_sequence ();
5531 lra_process_new_insns (curr_insn, NULL, new_insns,
5532 "Add invariant inheritance<-original");
5533 start_sequence ();
5534 lra_emit_move (SET_DEST (insn_set), new_reg);
5535 new_insns = get_insns ();
5536 end_sequence ();
5537 lra_process_new_insns (insn, NULL, new_insns,
5538 "Changing reload<-inheritance");
5539 lra_set_insn_deleted (insn);
5540 succ_p = true;
5541 if (lra_dump_file != NULL)
5543 fprintf (lra_dump_file,
5544 " Invariant inheritance reuse change %d (bb%d):\n",
5545 REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
5546 dump_insn_slim (lra_dump_file, insn);
5547 fprintf (lra_dump_file,
5548 " ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]\n");
5552 invariant_ptr->insn = curr_insn;
5553 return succ_p;
5556 /* Check only registers living at the current program point in the
5557 current EBB. */
5558 static bitmap_head live_regs;
5560 /* Update live info in EBB given by its HEAD and TAIL insns after
5561 inheritance/split transformation. The function removes dead moves
5562 too. */
5563 static void
5564 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
5566 unsigned int j;
5567 int i, regno;
5568 bool live_p;
5569 rtx_insn *prev_insn;
5570 rtx set;
5571 bool remove_p;
5572 basic_block last_bb, prev_bb, curr_bb;
5573 bitmap_iterator bi;
5574 struct lra_insn_reg *reg;
5575 edge e;
5576 edge_iterator ei;
5578 last_bb = BLOCK_FOR_INSN (tail);
5579 prev_bb = NULL;
5580 for (curr_insn = tail;
5581 curr_insn != PREV_INSN (head);
5582 curr_insn = prev_insn)
5584 prev_insn = PREV_INSN (curr_insn);
5585 /* We need to process empty blocks too. They contain
5586 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
5587 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
5588 continue;
5589 curr_bb = BLOCK_FOR_INSN (curr_insn);
5590 if (curr_bb != prev_bb)
5592 if (prev_bb != NULL)
5594 /* Update df_get_live_in (prev_bb): */
5595 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5596 if (bitmap_bit_p (&live_regs, j))
5597 bitmap_set_bit (df_get_live_in (prev_bb), j);
5598 else
5599 bitmap_clear_bit (df_get_live_in (prev_bb), j);
5601 if (curr_bb != last_bb)
5603 /* Update df_get_live_out (curr_bb): */
5604 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
5606 live_p = bitmap_bit_p (&live_regs, j);
5607 if (! live_p)
5608 FOR_EACH_EDGE (e, ei, curr_bb->succs)
5609 if (bitmap_bit_p (df_get_live_in (e->dest), j))
5611 live_p = true;
5612 break;
5614 if (live_p)
5615 bitmap_set_bit (df_get_live_out (curr_bb), j);
5616 else
5617 bitmap_clear_bit (df_get_live_out (curr_bb), j);
5620 prev_bb = curr_bb;
5621 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
5623 if (! NONDEBUG_INSN_P (curr_insn))
5624 continue;
5625 curr_id = lra_get_insn_recog_data (curr_insn);
5626 curr_static_id = curr_id->insn_static_data;
5627 remove_p = false;
5628 if ((set = single_set (curr_insn)) != NULL_RTX
5629 && REG_P (SET_DEST (set))
5630 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
5631 && SET_DEST (set) != pic_offset_table_rtx
5632 && bitmap_bit_p (&check_only_regs, regno)
5633 && ! bitmap_bit_p (&live_regs, regno))
5634 remove_p = true;
5635 /* See which defined values die here. */
5636 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5637 if (reg->type == OP_OUT && ! reg->subreg_p)
5638 bitmap_clear_bit (&live_regs, reg->regno);
5639 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5640 if (reg->type == OP_OUT && ! reg->subreg_p)
5641 bitmap_clear_bit (&live_regs, reg->regno);
5642 if (curr_id->arg_hard_regs != NULL)
5643 /* Make clobbered argument hard registers die. */
5644 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5645 if (regno >= FIRST_PSEUDO_REGISTER)
5646 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
5647 /* Mark each used value as live. */
5648 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5649 if (reg->type != OP_OUT
5650 && bitmap_bit_p (&check_only_regs, reg->regno))
5651 bitmap_set_bit (&live_regs, reg->regno);
5652 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
5653 if (reg->type != OP_OUT
5654 && bitmap_bit_p (&check_only_regs, reg->regno))
5655 bitmap_set_bit (&live_regs, reg->regno);
5656 if (curr_id->arg_hard_regs != NULL)
5657 /* Make used argument hard registers live. */
5658 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5659 if (regno < FIRST_PSEUDO_REGISTER
5660 && bitmap_bit_p (&check_only_regs, regno))
5661 bitmap_set_bit (&live_regs, regno);
5662 /* It is quite important to remove dead move insns because it
5663 means removing dead store. We don't need to process them for
5664 constraints. */
5665 if (remove_p)
5667 if (lra_dump_file != NULL)
5669 fprintf (lra_dump_file, " Removing dead insn:\n ");
5670 dump_insn_slim (lra_dump_file, curr_insn);
5672 lra_set_insn_deleted (curr_insn);
5677 /* The structure describes info to do an inheritance for the current
5678 insn. We need to collect such info first before doing the
5679 transformations because the transformations change the insn
5680 internal representation. */
5681 struct to_inherit
5683 /* Original regno. */
5684 int regno;
5685 /* Subsequent insns which can inherit original reg value. */
5686 rtx insns;
5689 /* Array containing all info for doing inheritance from the current
5690 insn. */
5691 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
5693 /* Number elements in the previous array. */
5694 static int to_inherit_num;
5696 /* Add inheritance info REGNO and INSNS. Their meaning is described in
5697 structure to_inherit. */
5698 static void
5699 add_to_inherit (int regno, rtx insns)
5701 int i;
5703 for (i = 0; i < to_inherit_num; i++)
5704 if (to_inherit[i].regno == regno)
5705 return;
5706 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
5707 to_inherit[to_inherit_num].regno = regno;
5708 to_inherit[to_inherit_num++].insns = insns;
5711 /* Return the last non-debug insn in basic block BB, or the block begin
5712 note if none. */
5713 static rtx_insn *
5714 get_last_insertion_point (basic_block bb)
5716 rtx_insn *insn;
5718 FOR_BB_INSNS_REVERSE (bb, insn)
5719 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
5720 return insn;
5721 gcc_unreachable ();
5724 /* Set up RES by registers living on edges FROM except the edge (FROM,
5725 TO) or by registers set up in a jump insn in BB FROM. */
5726 static void
5727 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
5729 rtx_insn *last;
5730 struct lra_insn_reg *reg;
5731 edge e;
5732 edge_iterator ei;
5734 lra_assert (to != NULL);
5735 bitmap_clear (res);
5736 FOR_EACH_EDGE (e, ei, from->succs)
5737 if (e->dest != to)
5738 bitmap_ior_into (res, df_get_live_in (e->dest));
5739 last = get_last_insertion_point (from);
5740 if (! JUMP_P (last))
5741 return;
5742 curr_id = lra_get_insn_recog_data (last);
5743 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
5744 if (reg->type != OP_IN)
5745 bitmap_set_bit (res, reg->regno);
5748 /* Used as a temporary results of some bitmap calculations. */
5749 static bitmap_head temp_bitmap;
5751 /* We split for reloads of small class of hard regs. The following
5752 defines how many hard regs the class should have to be qualified as
5753 small. The code is mostly oriented to x86/x86-64 architecture
5754 where some insns need to use only specific register or pair of
5755 registers and these register can live in RTL explicitly, e.g. for
5756 parameter passing. */
5757 static const int max_small_class_regs_num = 2;
5759 /* Do inheritance/split transformations in EBB starting with HEAD and
5760 finishing on TAIL. We process EBB insns in the reverse order.
5761 Return true if we did any inheritance/split transformation in the
5762 EBB.
5764 We should avoid excessive splitting which results in worse code
5765 because of inaccurate cost calculations for spilling new split
5766 pseudos in such case. To achieve this we do splitting only if
5767 register pressure is high in given basic block and there are reload
5768 pseudos requiring hard registers. We could do more register
5769 pressure calculations at any given program point to avoid necessary
5770 splitting even more but it is to expensive and the current approach
5771 works well enough. */
5772 static bool
5773 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
5775 int i, src_regno, dst_regno, nregs;
5776 bool change_p, succ_p, update_reloads_num_p;
5777 rtx_insn *prev_insn, *last_insn;
5778 rtx next_usage_insns, curr_set;
5779 enum reg_class cl;
5780 struct lra_insn_reg *reg;
5781 basic_block last_processed_bb, curr_bb = NULL;
5782 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
5783 bitmap to_process;
5784 unsigned int j;
5785 bitmap_iterator bi;
5786 bool head_p, after_p;
5788 change_p = false;
5789 curr_usage_insns_check++;
5790 clear_invariants ();
5791 reloads_num = calls_num = 0;
5792 bitmap_clear (&check_only_regs);
5793 bitmap_clear (&invalid_invariant_regs);
5794 last_processed_bb = NULL;
5795 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5796 COPY_HARD_REG_SET (live_hard_regs, eliminable_regset);
5797 IOR_HARD_REG_SET (live_hard_regs, lra_no_alloc_regs);
5798 /* We don't process new insns generated in the loop. */
5799 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
5801 prev_insn = PREV_INSN (curr_insn);
5802 if (BLOCK_FOR_INSN (curr_insn) != NULL)
5803 curr_bb = BLOCK_FOR_INSN (curr_insn);
5804 if (last_processed_bb != curr_bb)
5806 /* We are at the end of BB. Add qualified living
5807 pseudos for potential splitting. */
5808 to_process = df_get_live_out (curr_bb);
5809 if (last_processed_bb != NULL)
5811 /* We are somewhere in the middle of EBB. */
5812 get_live_on_other_edges (curr_bb, last_processed_bb,
5813 &temp_bitmap);
5814 to_process = &temp_bitmap;
5816 last_processed_bb = curr_bb;
5817 last_insn = get_last_insertion_point (curr_bb);
5818 after_p = (! JUMP_P (last_insn)
5819 && (! CALL_P (last_insn)
5820 || (find_reg_note (last_insn,
5821 REG_NORETURN, NULL_RTX) == NULL_RTX
5822 && ! SIBLING_CALL_P (last_insn))));
5823 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
5824 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
5826 if ((int) j >= lra_constraint_new_regno_start)
5827 break;
5828 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
5830 if (j < FIRST_PSEUDO_REGISTER)
5831 SET_HARD_REG_BIT (live_hard_regs, j);
5832 else
5833 add_to_hard_reg_set (&live_hard_regs,
5834 PSEUDO_REGNO_MODE (j),
5835 reg_renumber[j]);
5836 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
5840 src_regno = dst_regno = -1;
5841 curr_set = single_set (curr_insn);
5842 if (curr_set != NULL_RTX && REG_P (SET_DEST (curr_set)))
5843 dst_regno = REGNO (SET_DEST (curr_set));
5844 if (curr_set != NULL_RTX && REG_P (SET_SRC (curr_set)))
5845 src_regno = REGNO (SET_SRC (curr_set));
5846 update_reloads_num_p = true;
5847 if (src_regno < lra_constraint_new_regno_start
5848 && src_regno >= FIRST_PSEUDO_REGISTER
5849 && reg_renumber[src_regno] < 0
5850 && dst_regno >= lra_constraint_new_regno_start
5851 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
5853 /* 'reload_pseudo <- original_pseudo'. */
5854 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5855 reloads_num++;
5856 update_reloads_num_p = false;
5857 succ_p = false;
5858 if (usage_insns[src_regno].check == curr_usage_insns_check
5859 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
5860 succ_p = inherit_reload_reg (false, src_regno, cl,
5861 curr_insn, next_usage_insns);
5862 if (succ_p)
5863 change_p = true;
5864 else
5865 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
5866 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5867 IOR_HARD_REG_SET (potential_reload_hard_regs,
5868 reg_class_contents[cl]);
5870 else if (src_regno < 0
5871 && dst_regno >= lra_constraint_new_regno_start
5872 && invariant_p (SET_SRC (curr_set))
5873 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS
5874 && ! bitmap_bit_p (&invalid_invariant_regs, dst_regno))
5876 /* 'reload_pseudo <- invariant'. */
5877 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5878 reloads_num++;
5879 update_reloads_num_p = false;
5880 if (process_invariant_for_inheritance (SET_DEST (curr_set), SET_SRC (curr_set)))
5881 change_p = true;
5882 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5883 IOR_HARD_REG_SET (potential_reload_hard_regs,
5884 reg_class_contents[cl]);
5886 else if (src_regno >= lra_constraint_new_regno_start
5887 && dst_regno < lra_constraint_new_regno_start
5888 && dst_regno >= FIRST_PSEUDO_REGISTER
5889 && reg_renumber[dst_regno] < 0
5890 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
5891 && usage_insns[dst_regno].check == curr_usage_insns_check
5892 && (next_usage_insns
5893 = usage_insns[dst_regno].insns) != NULL_RTX)
5895 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
5896 reloads_num++;
5897 update_reloads_num_p = false;
5898 /* 'original_pseudo <- reload_pseudo'. */
5899 if (! JUMP_P (curr_insn)
5900 && inherit_reload_reg (true, dst_regno, cl,
5901 curr_insn, next_usage_insns))
5902 change_p = true;
5903 /* Invalidate. */
5904 usage_insns[dst_regno].check = 0;
5905 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
5906 IOR_HARD_REG_SET (potential_reload_hard_regs,
5907 reg_class_contents[cl]);
5909 else if (INSN_P (curr_insn))
5911 int iter;
5912 int max_uid = get_max_uid ();
5914 curr_id = lra_get_insn_recog_data (curr_insn);
5915 curr_static_id = curr_id->insn_static_data;
5916 to_inherit_num = 0;
5917 /* Process insn definitions. */
5918 for (iter = 0; iter < 2; iter++)
5919 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
5920 reg != NULL;
5921 reg = reg->next)
5922 if (reg->type != OP_IN
5923 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
5925 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
5926 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
5927 && usage_insns[dst_regno].check == curr_usage_insns_check
5928 && (next_usage_insns
5929 = usage_insns[dst_regno].insns) != NULL_RTX)
5931 struct lra_insn_reg *r;
5933 for (r = curr_id->regs; r != NULL; r = r->next)
5934 if (r->type != OP_OUT && r->regno == dst_regno)
5935 break;
5936 /* Don't do inheritance if the pseudo is also
5937 used in the insn. */
5938 if (r == NULL)
5939 /* We can not do inheritance right now
5940 because the current insn reg info (chain
5941 regs) can change after that. */
5942 add_to_inherit (dst_regno, next_usage_insns);
5944 /* We can not process one reg twice here because of
5945 usage_insns invalidation. */
5946 if ((dst_regno < FIRST_PSEUDO_REGISTER
5947 || reg_renumber[dst_regno] >= 0)
5948 && ! reg->subreg_p && reg->type != OP_IN)
5950 HARD_REG_SET s;
5952 if (split_if_necessary (dst_regno, reg->biggest_mode,
5953 potential_reload_hard_regs,
5954 false, curr_insn, max_uid))
5955 change_p = true;
5956 CLEAR_HARD_REG_SET (s);
5957 if (dst_regno < FIRST_PSEUDO_REGISTER)
5958 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
5959 else
5960 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
5961 reg_renumber[dst_regno]);
5962 AND_COMPL_HARD_REG_SET (live_hard_regs, s);
5964 /* We should invalidate potential inheritance or
5965 splitting for the current insn usages to the next
5966 usage insns (see code below) as the output pseudo
5967 prevents this. */
5968 if ((dst_regno >= FIRST_PSEUDO_REGISTER
5969 && reg_renumber[dst_regno] < 0)
5970 || (reg->type == OP_OUT && ! reg->subreg_p
5971 && (dst_regno < FIRST_PSEUDO_REGISTER
5972 || reg_renumber[dst_regno] >= 0)))
5974 /* Invalidate and mark definitions. */
5975 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5976 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
5977 else
5979 nregs = hard_regno_nregs[dst_regno][reg->biggest_mode];
5980 for (i = 0; i < nregs; i++)
5981 usage_insns[dst_regno + i].check
5982 = -(int) INSN_UID (curr_insn);
5986 /* Process clobbered call regs. */
5987 if (curr_id->arg_hard_regs != NULL)
5988 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
5989 if (dst_regno >= FIRST_PSEUDO_REGISTER)
5990 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
5991 = -(int) INSN_UID (curr_insn);
5992 if (! JUMP_P (curr_insn))
5993 for (i = 0; i < to_inherit_num; i++)
5994 if (inherit_reload_reg (true, to_inherit[i].regno,
5995 ALL_REGS, curr_insn,
5996 to_inherit[i].insns))
5997 change_p = true;
5998 if (CALL_P (curr_insn))
6000 rtx cheap, pat, dest;
6001 rtx_insn *restore;
6002 int regno, hard_regno;
6004 calls_num++;
6005 if ((cheap = find_reg_note (curr_insn,
6006 REG_RETURNED, NULL_RTX)) != NULL_RTX
6007 && ((cheap = XEXP (cheap, 0)), true)
6008 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
6009 && (hard_regno = reg_renumber[regno]) >= 0
6010 /* If there are pending saves/restores, the
6011 optimization is not worth. */
6012 && usage_insns[regno].calls_num == calls_num - 1
6013 && TEST_HARD_REG_BIT (call_used_reg_set, hard_regno))
6015 /* Restore the pseudo from the call result as
6016 REG_RETURNED note says that the pseudo value is
6017 in the call result and the pseudo is an argument
6018 of the call. */
6019 pat = PATTERN (curr_insn);
6020 if (GET_CODE (pat) == PARALLEL)
6021 pat = XVECEXP (pat, 0, 0);
6022 dest = SET_DEST (pat);
6023 /* For multiple return values dest is PARALLEL.
6024 Currently we handle only single return value case. */
6025 if (REG_P (dest))
6027 start_sequence ();
6028 emit_move_insn (cheap, copy_rtx (dest));
6029 restore = get_insns ();
6030 end_sequence ();
6031 lra_process_new_insns (curr_insn, NULL, restore,
6032 "Inserting call parameter restore");
6033 /* We don't need to save/restore of the pseudo from
6034 this call. */
6035 usage_insns[regno].calls_num = calls_num;
6036 bitmap_set_bit (&check_only_regs, regno);
6040 to_inherit_num = 0;
6041 /* Process insn usages. */
6042 for (iter = 0; iter < 2; iter++)
6043 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
6044 reg != NULL;
6045 reg = reg->next)
6046 if ((reg->type != OP_OUT
6047 || (reg->type == OP_OUT && reg->subreg_p))
6048 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
6050 if (src_regno >= FIRST_PSEUDO_REGISTER
6051 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
6053 if (usage_insns[src_regno].check == curr_usage_insns_check
6054 && (next_usage_insns
6055 = usage_insns[src_regno].insns) != NULL_RTX
6056 && NONDEBUG_INSN_P (curr_insn))
6057 add_to_inherit (src_regno, next_usage_insns);
6058 else if (usage_insns[src_regno].check
6059 != -(int) INSN_UID (curr_insn))
6060 /* Add usages but only if the reg is not set up
6061 in the same insn. */
6062 add_next_usage_insn (src_regno, curr_insn, reloads_num);
6064 else if (src_regno < FIRST_PSEUDO_REGISTER
6065 || reg_renumber[src_regno] >= 0)
6067 bool before_p;
6068 rtx_insn *use_insn = curr_insn;
6070 before_p = (JUMP_P (curr_insn)
6071 || (CALL_P (curr_insn) && reg->type == OP_IN));
6072 if (NONDEBUG_INSN_P (curr_insn)
6073 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
6074 && split_if_necessary (src_regno, reg->biggest_mode,
6075 potential_reload_hard_regs,
6076 before_p, curr_insn, max_uid))
6078 if (reg->subreg_p)
6079 lra_risky_transformations_p = true;
6080 change_p = true;
6081 /* Invalidate. */
6082 usage_insns[src_regno].check = 0;
6083 if (before_p)
6084 use_insn = PREV_INSN (curr_insn);
6086 if (NONDEBUG_INSN_P (curr_insn))
6088 if (src_regno < FIRST_PSEUDO_REGISTER)
6089 add_to_hard_reg_set (&live_hard_regs,
6090 reg->biggest_mode, src_regno);
6091 else
6092 add_to_hard_reg_set (&live_hard_regs,
6093 PSEUDO_REGNO_MODE (src_regno),
6094 reg_renumber[src_regno]);
6096 add_next_usage_insn (src_regno, use_insn, reloads_num);
6099 /* Process used call regs. */
6100 if (curr_id->arg_hard_regs != NULL)
6101 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6102 if (src_regno < FIRST_PSEUDO_REGISTER)
6104 SET_HARD_REG_BIT (live_hard_regs, src_regno);
6105 add_next_usage_insn (src_regno, curr_insn, reloads_num);
6107 for (i = 0; i < to_inherit_num; i++)
6109 src_regno = to_inherit[i].regno;
6110 if (inherit_reload_reg (false, src_regno, ALL_REGS,
6111 curr_insn, to_inherit[i].insns))
6112 change_p = true;
6113 else
6114 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
6117 if (update_reloads_num_p
6118 && NONDEBUG_INSN_P (curr_insn) && curr_set != NULL_RTX)
6120 int regno = -1;
6121 if ((REG_P (SET_DEST (curr_set))
6122 && (regno = REGNO (SET_DEST (curr_set))) >= lra_constraint_new_regno_start
6123 && reg_renumber[regno] < 0
6124 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
6125 || (REG_P (SET_SRC (curr_set))
6126 && (regno = REGNO (SET_SRC (curr_set))) >= lra_constraint_new_regno_start
6127 && reg_renumber[regno] < 0
6128 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
6130 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6131 reloads_num++;
6132 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6133 IOR_HARD_REG_SET (potential_reload_hard_regs,
6134 reg_class_contents[cl]);
6137 if (NONDEBUG_INSN_P (curr_insn))
6139 int regno;
6141 /* Invalidate invariants with changed regs. */
6142 curr_id = lra_get_insn_recog_data (curr_insn);
6143 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6144 if (reg->type != OP_IN)
6145 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
6146 curr_static_id = curr_id->insn_static_data;
6147 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6148 if (reg->type != OP_IN)
6149 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
6150 if (curr_id->arg_hard_regs != NULL)
6151 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6152 bitmap_set_bit (&invalid_invariant_regs,
6153 regno >= FIRST_PSEUDO_REGISTER
6154 ? regno : regno - FIRST_PSEUDO_REGISTER);
6156 /* We reached the start of the current basic block. */
6157 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
6158 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
6160 /* We reached the beginning of the current block -- do
6161 rest of spliting in the current BB. */
6162 to_process = df_get_live_in (curr_bb);
6163 if (BLOCK_FOR_INSN (head) != curr_bb)
6165 /* We are somewhere in the middle of EBB. */
6166 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
6167 curr_bb, &temp_bitmap);
6168 to_process = &temp_bitmap;
6170 head_p = true;
6171 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
6173 if ((int) j >= lra_constraint_new_regno_start)
6174 break;
6175 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
6176 && usage_insns[j].check == curr_usage_insns_check
6177 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
6179 if (need_for_split_p (potential_reload_hard_regs, j))
6181 if (lra_dump_file != NULL && head_p)
6183 fprintf (lra_dump_file,
6184 " ----------------------------------\n");
6185 head_p = false;
6187 if (split_reg (false, j, bb_note (curr_bb),
6188 next_usage_insns))
6189 change_p = true;
6191 usage_insns[j].check = 0;
6196 return change_p;
6199 /* This value affects EBB forming. If probability of edge from EBB to
6200 a BB is not greater than the following value, we don't add the BB
6201 to EBB. */
6202 #define EBB_PROBABILITY_CUTOFF \
6203 ((REG_BR_PROB_BASE * LRA_INHERITANCE_EBB_PROBABILITY_CUTOFF) / 100)
6205 /* Current number of inheritance/split iteration. */
6206 int lra_inheritance_iter;
6208 /* Entry function for inheritance/split pass. */
6209 void
6210 lra_inheritance (void)
6212 int i;
6213 basic_block bb, start_bb;
6214 edge e;
6216 lra_inheritance_iter++;
6217 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6218 return;
6219 timevar_push (TV_LRA_INHERITANCE);
6220 if (lra_dump_file != NULL)
6221 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
6222 lra_inheritance_iter);
6223 curr_usage_insns_check = 0;
6224 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
6225 for (i = 0; i < lra_constraint_new_regno_start; i++)
6226 usage_insns[i].check = 0;
6227 bitmap_initialize (&check_only_regs, &reg_obstack);
6228 bitmap_initialize (&invalid_invariant_regs, &reg_obstack);
6229 bitmap_initialize (&live_regs, &reg_obstack);
6230 bitmap_initialize (&temp_bitmap, &reg_obstack);
6231 bitmap_initialize (&ebb_global_regs, &reg_obstack);
6232 FOR_EACH_BB_FN (bb, cfun)
6234 start_bb = bb;
6235 if (lra_dump_file != NULL)
6236 fprintf (lra_dump_file, "EBB");
6237 /* Form a EBB starting with BB. */
6238 bitmap_clear (&ebb_global_regs);
6239 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
6240 for (;;)
6242 if (lra_dump_file != NULL)
6243 fprintf (lra_dump_file, " %d", bb->index);
6244 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
6245 || LABEL_P (BB_HEAD (bb->next_bb)))
6246 break;
6247 e = find_fallthru_edge (bb->succs);
6248 if (! e)
6249 break;
6250 if (e->probability < EBB_PROBABILITY_CUTOFF)
6251 break;
6252 bb = bb->next_bb;
6254 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
6255 if (lra_dump_file != NULL)
6256 fprintf (lra_dump_file, "\n");
6257 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
6258 /* Remember that the EBB head and tail can change in
6259 inherit_in_ebb. */
6260 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
6262 bitmap_clear (&ebb_global_regs);
6263 bitmap_clear (&temp_bitmap);
6264 bitmap_clear (&live_regs);
6265 bitmap_clear (&invalid_invariant_regs);
6266 bitmap_clear (&check_only_regs);
6267 free (usage_insns);
6269 timevar_pop (TV_LRA_INHERITANCE);
6274 /* This page contains code to undo failed inheritance/split
6275 transformations. */
6277 /* Current number of iteration undoing inheritance/split. */
6278 int lra_undo_inheritance_iter;
6280 /* Fix BB live info LIVE after removing pseudos created on pass doing
6281 inheritance/split which are REMOVED_PSEUDOS. */
6282 static void
6283 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
6285 unsigned int regno;
6286 bitmap_iterator bi;
6288 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
6289 if (bitmap_clear_bit (live, regno)
6290 && REG_P (lra_reg_info[regno].restore_rtx))
6291 bitmap_set_bit (live, REGNO (lra_reg_info[regno].restore_rtx));
6294 /* Return regno of the (subreg of) REG. Otherwise, return a negative
6295 number. */
6296 static int
6297 get_regno (rtx reg)
6299 if (GET_CODE (reg) == SUBREG)
6300 reg = SUBREG_REG (reg);
6301 if (REG_P (reg))
6302 return REGNO (reg);
6303 return -1;
6306 /* Delete a move INSN with destination reg DREGNO and a previous
6307 clobber insn with the same regno. The inheritance/split code can
6308 generate moves with preceding clobber and when we delete such moves
6309 we should delete the clobber insn too to keep the correct life
6310 info. */
6311 static void
6312 delete_move_and_clobber (rtx_insn *insn, int dregno)
6314 rtx_insn *prev_insn = PREV_INSN (insn);
6316 lra_set_insn_deleted (insn);
6317 lra_assert (dregno >= 0);
6318 if (prev_insn != NULL && NONDEBUG_INSN_P (prev_insn)
6319 && GET_CODE (PATTERN (prev_insn)) == CLOBBER
6320 && dregno == get_regno (XEXP (PATTERN (prev_insn), 0)))
6321 lra_set_insn_deleted (prev_insn);
6324 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
6325 return true if we did any change. The undo transformations for
6326 inheritance looks like
6327 i <- i2
6328 p <- i => p <- i2
6329 or removing
6330 p <- i, i <- p, and i <- i3
6331 where p is original pseudo from which inheritance pseudo i was
6332 created, i and i3 are removed inheritance pseudos, i2 is another
6333 not removed inheritance pseudo. All split pseudos or other
6334 occurrences of removed inheritance pseudos are changed on the
6335 corresponding original pseudos.
6337 The function also schedules insns changed and created during
6338 inheritance/split pass for processing by the subsequent constraint
6339 pass. */
6340 static bool
6341 remove_inheritance_pseudos (bitmap remove_pseudos)
6343 basic_block bb;
6344 int regno, sregno, prev_sregno, dregno;
6345 rtx restore_rtx;
6346 rtx set, prev_set;
6347 rtx_insn *prev_insn;
6348 bool change_p, done_p;
6350 change_p = ! bitmap_empty_p (remove_pseudos);
6351 /* We can not finish the function right away if CHANGE_P is true
6352 because we need to marks insns affected by previous
6353 inheritance/split pass for processing by the subsequent
6354 constraint pass. */
6355 FOR_EACH_BB_FN (bb, cfun)
6357 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
6358 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
6359 FOR_BB_INSNS_REVERSE (bb, curr_insn)
6361 if (! INSN_P (curr_insn))
6362 continue;
6363 done_p = false;
6364 sregno = dregno = -1;
6365 if (change_p && NONDEBUG_INSN_P (curr_insn)
6366 && (set = single_set (curr_insn)) != NULL_RTX)
6368 dregno = get_regno (SET_DEST (set));
6369 sregno = get_regno (SET_SRC (set));
6372 if (sregno >= 0 && dregno >= 0)
6374 if (bitmap_bit_p (remove_pseudos, dregno)
6375 && ! REG_P (lra_reg_info[dregno].restore_rtx))
6377 /* invariant inheritance pseudo <- original pseudo */
6378 if (lra_dump_file != NULL)
6380 fprintf (lra_dump_file, " Removing invariant inheritance:\n");
6381 dump_insn_slim (lra_dump_file, curr_insn);
6382 fprintf (lra_dump_file, "\n");
6384 delete_move_and_clobber (curr_insn, dregno);
6385 done_p = true;
6387 else if (bitmap_bit_p (remove_pseudos, sregno)
6388 && ! REG_P (lra_reg_info[sregno].restore_rtx))
6390 /* reload pseudo <- invariant inheritance pseudo */
6391 start_sequence ();
6392 /* We can not just change the source. It might be
6393 an insn different from the move. */
6394 emit_insn (lra_reg_info[sregno].restore_rtx);
6395 rtx_insn *new_insns = get_insns ();
6396 end_sequence ();
6397 lra_assert (single_set (new_insns) != NULL
6398 && SET_DEST (set) == SET_DEST (single_set (new_insns)));
6399 lra_process_new_insns (curr_insn, NULL, new_insns,
6400 "Changing reload<-invariant inheritance");
6401 delete_move_and_clobber (curr_insn, dregno);
6402 done_p = true;
6404 else if ((bitmap_bit_p (remove_pseudos, sregno)
6405 && (get_regno (lra_reg_info[sregno].restore_rtx) == dregno
6406 || (bitmap_bit_p (remove_pseudos, dregno)
6407 && get_regno (lra_reg_info[sregno].restore_rtx) >= 0
6408 && (get_regno (lra_reg_info[sregno].restore_rtx)
6409 == get_regno (lra_reg_info[dregno].restore_rtx)))))
6410 || (bitmap_bit_p (remove_pseudos, dregno)
6411 && get_regno (lra_reg_info[dregno].restore_rtx) == sregno))
6412 /* One of the following cases:
6413 original <- removed inheritance pseudo
6414 removed inherit pseudo <- another removed inherit pseudo
6415 removed inherit pseudo <- original pseudo
6417 removed_split_pseudo <- original_reg
6418 original_reg <- removed_split_pseudo */
6420 if (lra_dump_file != NULL)
6422 fprintf (lra_dump_file, " Removing %s:\n",
6423 bitmap_bit_p (&lra_split_regs, sregno)
6424 || bitmap_bit_p (&lra_split_regs, dregno)
6425 ? "split" : "inheritance");
6426 dump_insn_slim (lra_dump_file, curr_insn);
6428 delete_move_and_clobber (curr_insn, dregno);
6429 done_p = true;
6431 else if (bitmap_bit_p (remove_pseudos, sregno)
6432 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
6434 /* Search the following pattern:
6435 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
6436 original_pseudo <- inherit_or_split_pseudo1
6437 where the 2nd insn is the current insn and
6438 inherit_or_split_pseudo2 is not removed. If it is found,
6439 change the current insn onto:
6440 original_pseudo <- inherit_or_split_pseudo2. */
6441 for (prev_insn = PREV_INSN (curr_insn);
6442 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
6443 prev_insn = PREV_INSN (prev_insn))
6445 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
6446 && (prev_set = single_set (prev_insn)) != NULL_RTX
6447 /* There should be no subregs in insn we are
6448 searching because only the original reg might
6449 be in subreg when we changed the mode of
6450 load/store for splitting. */
6451 && REG_P (SET_DEST (prev_set))
6452 && REG_P (SET_SRC (prev_set))
6453 && (int) REGNO (SET_DEST (prev_set)) == sregno
6454 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
6455 >= FIRST_PSEUDO_REGISTER)
6456 && (lra_reg_info[prev_sregno].restore_rtx == NULL_RTX
6458 /* As we consider chain of inheritance or
6459 splitting described in above comment we should
6460 check that sregno and prev_sregno were
6461 inheritance/split pseudos created from the
6462 same original regno. */
6463 (get_regno (lra_reg_info[sregno].restore_rtx) >= 0
6464 && (get_regno (lra_reg_info[sregno].restore_rtx)
6465 == get_regno (lra_reg_info[prev_sregno].restore_rtx))))
6466 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
6468 lra_assert (GET_MODE (SET_SRC (prev_set))
6469 == GET_MODE (regno_reg_rtx[sregno]));
6470 if (GET_CODE (SET_SRC (set)) == SUBREG)
6471 SUBREG_REG (SET_SRC (set)) = SET_SRC (prev_set);
6472 else
6473 SET_SRC (set) = SET_SRC (prev_set);
6474 /* As we are finishing with processing the insn
6475 here, check the destination too as it might
6476 inheritance pseudo for another pseudo. */
6477 if (bitmap_bit_p (remove_pseudos, dregno)
6478 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
6479 && (restore_rtx
6480 = lra_reg_info[dregno].restore_rtx) != NULL_RTX)
6482 if (GET_CODE (SET_DEST (set)) == SUBREG)
6483 SUBREG_REG (SET_DEST (set)) = restore_rtx;
6484 else
6485 SET_DEST (set) = restore_rtx;
6487 lra_push_insn_and_update_insn_regno_info (curr_insn);
6488 lra_set_used_insn_alternative_by_uid
6489 (INSN_UID (curr_insn), -1);
6490 done_p = true;
6491 if (lra_dump_file != NULL)
6493 fprintf (lra_dump_file, " Change reload insn:\n");
6494 dump_insn_slim (lra_dump_file, curr_insn);
6499 if (! done_p)
6501 struct lra_insn_reg *reg;
6502 bool restored_regs_p = false;
6503 bool kept_regs_p = false;
6505 curr_id = lra_get_insn_recog_data (curr_insn);
6506 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6508 regno = reg->regno;
6509 restore_rtx = lra_reg_info[regno].restore_rtx;
6510 if (restore_rtx != NULL_RTX)
6512 if (change_p && bitmap_bit_p (remove_pseudos, regno))
6514 lra_substitute_pseudo_within_insn
6515 (curr_insn, regno, restore_rtx, false);
6516 restored_regs_p = true;
6518 else
6519 kept_regs_p = true;
6522 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
6524 /* The instruction has changed since the previous
6525 constraints pass. */
6526 lra_push_insn_and_update_insn_regno_info (curr_insn);
6527 lra_set_used_insn_alternative_by_uid
6528 (INSN_UID (curr_insn), -1);
6530 else if (restored_regs_p)
6531 /* The instruction has been restored to the form that
6532 it had during the previous constraints pass. */
6533 lra_update_insn_regno_info (curr_insn);
6534 if (restored_regs_p && lra_dump_file != NULL)
6536 fprintf (lra_dump_file, " Insn after restoring regs:\n");
6537 dump_insn_slim (lra_dump_file, curr_insn);
6542 return change_p;
6545 /* If optional reload pseudos failed to get a hard register or was not
6546 inherited, it is better to remove optional reloads. We do this
6547 transformation after undoing inheritance to figure out necessity to
6548 remove optional reloads easier. Return true if we do any
6549 change. */
6550 static bool
6551 undo_optional_reloads (void)
6553 bool change_p, keep_p;
6554 unsigned int regno, uid;
6555 bitmap_iterator bi, bi2;
6556 rtx_insn *insn;
6557 rtx set, src, dest;
6558 bitmap_head removed_optional_reload_pseudos, insn_bitmap;
6560 bitmap_initialize (&removed_optional_reload_pseudos, &reg_obstack);
6561 bitmap_copy (&removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
6562 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6564 keep_p = false;
6565 /* Keep optional reloads from previous subpasses. */
6566 if (lra_reg_info[regno].restore_rtx == NULL_RTX
6567 /* If the original pseudo changed its allocation, just
6568 removing the optional pseudo is dangerous as the original
6569 pseudo will have longer live range. */
6570 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] >= 0)
6571 keep_p = true;
6572 else if (reg_renumber[regno] >= 0)
6573 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
6575 insn = lra_insn_recog_data[uid]->insn;
6576 if ((set = single_set (insn)) == NULL_RTX)
6577 continue;
6578 src = SET_SRC (set);
6579 dest = SET_DEST (set);
6580 if (! REG_P (src) || ! REG_P (dest))
6581 continue;
6582 if (REGNO (dest) == regno
6583 /* Ignore insn for optional reloads itself. */
6584 && REGNO (lra_reg_info[regno].restore_rtx) != REGNO (src)
6585 /* Check only inheritance on last inheritance pass. */
6586 && (int) REGNO (src) >= new_regno_start
6587 /* Check that the optional reload was inherited. */
6588 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
6590 keep_p = true;
6591 break;
6594 if (keep_p)
6596 bitmap_clear_bit (&removed_optional_reload_pseudos, regno);
6597 if (lra_dump_file != NULL)
6598 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
6601 change_p = ! bitmap_empty_p (&removed_optional_reload_pseudos);
6602 bitmap_initialize (&insn_bitmap, &reg_obstack);
6603 EXECUTE_IF_SET_IN_BITMAP (&removed_optional_reload_pseudos, 0, regno, bi)
6605 if (lra_dump_file != NULL)
6606 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
6607 bitmap_copy (&insn_bitmap, &lra_reg_info[regno].insn_bitmap);
6608 EXECUTE_IF_SET_IN_BITMAP (&insn_bitmap, 0, uid, bi2)
6610 insn = lra_insn_recog_data[uid]->insn;
6611 if ((set = single_set (insn)) != NULL_RTX)
6613 src = SET_SRC (set);
6614 dest = SET_DEST (set);
6615 if (REG_P (src) && REG_P (dest)
6616 && ((REGNO (src) == regno
6617 && (REGNO (lra_reg_info[regno].restore_rtx)
6618 == REGNO (dest)))
6619 || (REGNO (dest) == regno
6620 && (REGNO (lra_reg_info[regno].restore_rtx)
6621 == REGNO (src)))))
6623 if (lra_dump_file != NULL)
6625 fprintf (lra_dump_file, " Deleting move %u\n",
6626 INSN_UID (insn));
6627 dump_insn_slim (lra_dump_file, insn);
6629 delete_move_and_clobber (insn, REGNO (dest));
6630 continue;
6632 /* We should not worry about generation memory-memory
6633 moves here as if the corresponding inheritance did
6634 not work (inheritance pseudo did not get a hard reg),
6635 we remove the inheritance pseudo and the optional
6636 reload. */
6638 lra_substitute_pseudo_within_insn
6639 (insn, regno, lra_reg_info[regno].restore_rtx, false);
6640 lra_update_insn_regno_info (insn);
6641 if (lra_dump_file != NULL)
6643 fprintf (lra_dump_file,
6644 " Restoring original insn:\n");
6645 dump_insn_slim (lra_dump_file, insn);
6649 /* Clear restore_regnos. */
6650 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
6651 lra_reg_info[regno].restore_rtx = NULL_RTX;
6652 bitmap_clear (&insn_bitmap);
6653 bitmap_clear (&removed_optional_reload_pseudos);
6654 return change_p;
6657 /* Entry function for undoing inheritance/split transformation. Return true
6658 if we did any RTL change in this pass. */
6659 bool
6660 lra_undo_inheritance (void)
6662 unsigned int regno;
6663 int hard_regno;
6664 int n_all_inherit, n_inherit, n_all_split, n_split;
6665 rtx restore_rtx;
6666 bitmap_head remove_pseudos;
6667 bitmap_iterator bi;
6668 bool change_p;
6670 lra_undo_inheritance_iter++;
6671 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6672 return false;
6673 if (lra_dump_file != NULL)
6674 fprintf (lra_dump_file,
6675 "\n********** Undoing inheritance #%d: **********\n\n",
6676 lra_undo_inheritance_iter);
6677 bitmap_initialize (&remove_pseudos, &reg_obstack);
6678 n_inherit = n_all_inherit = 0;
6679 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6680 if (lra_reg_info[regno].restore_rtx != NULL_RTX)
6682 n_all_inherit++;
6683 if (reg_renumber[regno] < 0
6684 /* If the original pseudo changed its allocation, just
6685 removing inheritance is dangerous as for changing
6686 allocation we used shorter live-ranges. */
6687 && (! REG_P (lra_reg_info[regno].restore_rtx)
6688 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] < 0))
6689 bitmap_set_bit (&remove_pseudos, regno);
6690 else
6691 n_inherit++;
6693 if (lra_dump_file != NULL && n_all_inherit != 0)
6694 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
6695 n_inherit, n_all_inherit,
6696 (double) n_inherit / n_all_inherit * 100);
6697 n_split = n_all_split = 0;
6698 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6699 if ((restore_rtx = lra_reg_info[regno].restore_rtx) != NULL_RTX)
6701 int restore_regno = REGNO (restore_rtx);
6703 n_all_split++;
6704 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
6705 ? reg_renumber[restore_regno] : restore_regno);
6706 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
6707 bitmap_set_bit (&remove_pseudos, regno);
6708 else
6710 n_split++;
6711 if (lra_dump_file != NULL)
6712 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
6713 regno, restore_regno);
6716 if (lra_dump_file != NULL && n_all_split != 0)
6717 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
6718 n_split, n_all_split,
6719 (double) n_split / n_all_split * 100);
6720 change_p = remove_inheritance_pseudos (&remove_pseudos);
6721 bitmap_clear (&remove_pseudos);
6722 /* Clear restore_regnos. */
6723 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
6724 lra_reg_info[regno].restore_rtx = NULL_RTX;
6725 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
6726 lra_reg_info[regno].restore_rtx = NULL_RTX;
6727 change_p = undo_optional_reloads () || change_p;
6728 return change_p;