ada: Fix internal error on call with parameter of predicated subtype
[official-gcc.git] / gcc / lra-constraints.cc
blob05479ab98dd5a803952786564834d767590e33a4
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2023 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 "hooks.h"
114 #include "target.h"
115 #include "rtl.h"
116 #include "tree.h"
117 #include "predict.h"
118 #include "df.h"
119 #include "memmodel.h"
120 #include "tm_p.h"
121 #include "expmed.h"
122 #include "optabs.h"
123 #include "regs.h"
124 #include "ira.h"
125 #include "recog.h"
126 #include "output.h"
127 #include "addresses.h"
128 #include "expr.h"
129 #include "cfgrtl.h"
130 #include "rtl-error.h"
131 #include "lra.h"
132 #include "lra-int.h"
133 #include "print-rtl.h"
134 #include "function-abi.h"
135 #include "rtl-iter.h"
137 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
138 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
139 reload insns. */
140 static int bb_reload_num;
142 /* The current insn being processed and corresponding its single set
143 (NULL otherwise), its data (basic block, the insn data, the insn
144 static data, and the mode of each operand). */
145 static rtx_insn *curr_insn;
146 static rtx curr_insn_set;
147 static basic_block curr_bb;
148 static lra_insn_recog_data_t curr_id;
149 static struct lra_static_insn_data *curr_static_id;
150 static machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
151 /* Mode of the register substituted by its equivalence with VOIDmode
152 (e.g. constant) and whose subreg is given operand of the current
153 insn. VOIDmode in all other cases. */
154 static machine_mode original_subreg_reg_mode[MAX_RECOG_OPERANDS];
158 /* Start numbers for new registers and insns at the current constraints
159 pass start. */
160 static int new_regno_start;
161 static int new_insn_uid_start;
163 /* If LOC is nonnull, strip any outer subreg from it. */
164 static inline rtx *
165 strip_subreg (rtx *loc)
167 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
170 /* Return hard regno of REGNO or if it is was not assigned to a hard
171 register, use a hard register from its allocno class. */
172 static int
173 get_try_hard_regno (int regno)
175 int hard_regno;
176 enum reg_class rclass;
178 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
179 hard_regno = lra_get_regno_hard_regno (regno);
180 if (hard_regno >= 0)
181 return hard_regno;
182 rclass = lra_get_allocno_class (regno);
183 if (rclass == NO_REGS)
184 return -1;
185 return ira_class_hard_regs[rclass][0];
188 /* Return the hard regno of X after removing its subreg. If X is not a
189 register or a subreg of a register, return -1. If X is a pseudo, use its
190 assignment. If X is a hard regno, return the final hard regno which will be
191 after elimination. */
192 static int
193 get_hard_regno (rtx x)
195 rtx reg;
196 int hard_regno;
198 reg = x;
199 if (SUBREG_P (x))
200 reg = SUBREG_REG (x);
201 if (! REG_P (reg))
202 return -1;
203 if (! HARD_REGISTER_NUM_P (hard_regno = REGNO (reg)))
204 hard_regno = lra_get_regno_hard_regno (hard_regno);
205 if (hard_regno < 0)
206 return -1;
207 if (HARD_REGISTER_NUM_P (REGNO (reg)))
208 hard_regno = lra_get_elimination_hard_regno (hard_regno);
209 if (SUBREG_P (x))
210 hard_regno += subreg_regno_offset (hard_regno, GET_MODE (reg),
211 SUBREG_BYTE (x), GET_MODE (x));
212 return hard_regno;
215 /* If REGNO is a hard register or has been allocated a hard register,
216 return the class of that register. If REGNO is a reload pseudo
217 created by the current constraints pass, return its allocno class.
218 Return NO_REGS otherwise. */
219 static enum reg_class
220 get_reg_class (int regno)
222 int hard_regno;
224 if (! HARD_REGISTER_NUM_P (hard_regno = regno))
225 hard_regno = lra_get_regno_hard_regno (regno);
226 if (hard_regno >= 0)
228 hard_regno = lra_get_elimination_hard_regno (hard_regno);
229 return REGNO_REG_CLASS (hard_regno);
231 if (regno >= new_regno_start)
232 return lra_get_allocno_class (regno);
233 return NO_REGS;
236 /* Return true if REG_CLASS has enough allocatable hard regs to keep value of
237 REG_MODE. */
238 static bool
239 enough_allocatable_hard_regs_p (enum reg_class reg_class,
240 enum machine_mode reg_mode)
242 int i, j, hard_regno, class_size, nregs;
244 if (hard_reg_set_subset_p (reg_class_contents[reg_class], lra_no_alloc_regs))
245 return false;
246 class_size = ira_class_hard_regs_num[reg_class];
247 for (i = 0; i < class_size; i++)
249 hard_regno = ira_class_hard_regs[reg_class][i];
250 nregs = hard_regno_nregs (hard_regno, reg_mode);
251 if (nregs == 1)
252 return true;
253 for (j = 0; j < nregs; j++)
254 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
255 || ! TEST_HARD_REG_BIT (reg_class_contents[reg_class],
256 hard_regno + j))
257 break;
258 if (j >= nregs)
259 return true;
261 return false;
264 /* True if C is a non-empty register class that has too few registers
265 to be safely used as a reload target class. */
266 #define SMALL_REGISTER_CLASS_P(C) \
267 (ira_class_hard_regs_num [(C)] == 1 \
268 || (ira_class_hard_regs_num [(C)] >= 1 \
269 && targetm.class_likely_spilled_p (C)))
271 /* Return true if REG satisfies (or will satisfy) reg class constraint
272 CL. Use elimination first if REG is a hard register. If REG is a
273 reload pseudo created by this constraints pass, assume that it will
274 be allocated a hard register from its allocno class, but allow that
275 class to be narrowed to CL if it is currently a superset of CL and
276 if either:
278 - ALLOW_ALL_RELOAD_CLASS_CHANGES_P is true or
279 - the instruction we're processing is not a reload move.
281 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
282 REGNO (reg), or NO_REGS if no change in its class was needed. */
283 static bool
284 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class,
285 bool allow_all_reload_class_changes_p = false)
287 enum reg_class rclass, common_class;
288 machine_mode reg_mode;
289 rtx src;
290 int regno = REGNO (reg);
292 if (new_class != NULL)
293 *new_class = NO_REGS;
294 if (regno < FIRST_PSEUDO_REGISTER)
296 rtx final_reg = reg;
297 rtx *final_loc = &final_reg;
299 lra_eliminate_reg_if_possible (final_loc);
300 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
302 reg_mode = GET_MODE (reg);
303 rclass = get_reg_class (regno);
304 src = curr_insn_set != NULL ? SET_SRC (curr_insn_set) : NULL;
305 if (regno < new_regno_start
306 /* Do not allow the constraints for reload instructions to
307 influence the classes of new pseudos. These reloads are
308 typically moves that have many alternatives, and restricting
309 reload pseudos for one alternative may lead to situations
310 where other reload pseudos are no longer allocatable. */
311 || (!allow_all_reload_class_changes_p
312 && INSN_UID (curr_insn) >= new_insn_uid_start
313 && src != NULL
314 && ((REG_P (src) || MEM_P (src))
315 || (GET_CODE (src) == SUBREG
316 && (REG_P (SUBREG_REG (src)) || MEM_P (SUBREG_REG (src)))))))
317 /* When we don't know what class will be used finally for reload
318 pseudos, we use ALL_REGS. */
319 return ((regno >= new_regno_start && rclass == ALL_REGS)
320 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
321 && ! hard_reg_set_subset_p (reg_class_contents[cl],
322 lra_no_alloc_regs)));
323 else
325 common_class = ira_reg_class_subset[rclass][cl];
326 if (new_class != NULL)
327 *new_class = common_class;
328 return (enough_allocatable_hard_regs_p (common_class, reg_mode)
329 /* Do not permit reload insn operand matching (new_class == NULL
330 case) if the new class is too small. */
331 && (new_class != NULL || common_class == rclass
332 || !SMALL_REGISTER_CLASS_P (common_class)));
336 /* Return true if REGNO satisfies a memory constraint. */
337 static bool
338 in_mem_p (int regno)
340 return get_reg_class (regno) == NO_REGS;
343 /* Return true if ADDR is a valid memory address for mode MODE in address
344 space AS, and check that each pseudo has the proper kind of hard
345 reg. */
346 static bool
347 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
348 rtx addr, addr_space_t as)
350 #ifdef GO_IF_LEGITIMATE_ADDRESS
351 lra_assert (ADDR_SPACE_GENERIC_P (as));
352 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
353 return false;
355 win:
356 return true;
357 #else
358 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as,
359 ERROR_MARK);
360 #endif
363 namespace {
364 /* Temporarily eliminates registers in an address (for the lifetime of
365 the object). */
366 class address_eliminator {
367 public:
368 address_eliminator (struct address_info *ad);
369 ~address_eliminator ();
371 private:
372 struct address_info *m_ad;
373 rtx *m_base_loc;
374 rtx m_base_reg;
375 rtx *m_index_loc;
376 rtx m_index_reg;
380 address_eliminator::address_eliminator (struct address_info *ad)
381 : m_ad (ad),
382 m_base_loc (strip_subreg (ad->base_term)),
383 m_base_reg (NULL_RTX),
384 m_index_loc (strip_subreg (ad->index_term)),
385 m_index_reg (NULL_RTX)
387 if (m_base_loc != NULL)
389 m_base_reg = *m_base_loc;
390 /* If we have non-legitimate address which is decomposed not in
391 the way we expected, don't do elimination here. In such case
392 the address will be reloaded and elimination will be done in
393 reload insn finally. */
394 if (REG_P (m_base_reg))
395 lra_eliminate_reg_if_possible (m_base_loc);
396 if (m_ad->base_term2 != NULL)
397 *m_ad->base_term2 = *m_ad->base_term;
399 if (m_index_loc != NULL)
401 m_index_reg = *m_index_loc;
402 if (REG_P (m_index_reg))
403 lra_eliminate_reg_if_possible (m_index_loc);
407 address_eliminator::~address_eliminator ()
409 if (m_base_loc && *m_base_loc != m_base_reg)
411 *m_base_loc = m_base_reg;
412 if (m_ad->base_term2 != NULL)
413 *m_ad->base_term2 = *m_ad->base_term;
415 if (m_index_loc && *m_index_loc != m_index_reg)
416 *m_index_loc = m_index_reg;
419 /* Return true if the eliminated form of AD is a legitimate target address.
420 If OP is a MEM, AD is the address within OP, otherwise OP should be
421 ignored. CONSTRAINT is one constraint that the operand may need
422 to meet. */
423 static bool
424 valid_address_p (rtx op, struct address_info *ad,
425 enum constraint_num constraint)
427 address_eliminator eliminator (ad);
429 /* Allow a memory OP if it matches CONSTRAINT, even if CONSTRAINT is more
430 forgiving than "m".
431 Need to extract memory from op for special memory constraint,
432 i.e. bcst_mem_operand in i386 backend. */
433 if (MEM_P (extract_mem_from_operand (op))
434 && insn_extra_relaxed_memory_constraint (constraint)
435 && constraint_satisfied_p (op, constraint))
436 return true;
438 return valid_address_p (ad->mode, *ad->outer, ad->as);
441 /* For special_memory_operand, it could be false for MEM_P (op),
442 i.e. bcst_mem_operand in i386 backend.
443 Extract and return real memory operand or op. */
445 extract_mem_from_operand (rtx op)
447 for (rtx x = op;; x = XEXP (x, 0))
449 if (MEM_P (x))
450 return x;
451 if (GET_RTX_LENGTH (GET_CODE (x)) != 1
452 || GET_RTX_FORMAT (GET_CODE (x))[0] != 'e')
453 break;
455 return op;
458 /* Return true if the eliminated form of memory reference OP satisfies
459 extra (special) memory constraint CONSTRAINT. */
460 static bool
461 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
463 struct address_info ad;
464 rtx mem = extract_mem_from_operand (op);
465 if (!MEM_P (mem))
466 return false;
468 decompose_mem_address (&ad, mem);
469 address_eliminator eliminator (&ad);
470 return constraint_satisfied_p (op, constraint);
473 /* Return true if the eliminated form of address AD satisfies extra
474 address constraint CONSTRAINT. */
475 static bool
476 satisfies_address_constraint_p (struct address_info *ad,
477 enum constraint_num constraint)
479 address_eliminator eliminator (ad);
480 return constraint_satisfied_p (*ad->outer, constraint);
483 /* Return true if the eliminated form of address OP satisfies extra
484 address constraint CONSTRAINT. */
485 static bool
486 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
488 struct address_info ad;
490 decompose_lea_address (&ad, &op);
491 return satisfies_address_constraint_p (&ad, constraint);
494 /* Initiate equivalences for LRA. As we keep original equivalences
495 before any elimination, we need to make copies otherwise any change
496 in insns might change the equivalences. */
497 void
498 lra_init_equiv (void)
500 ira_expand_reg_equiv ();
501 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
503 rtx res;
505 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
506 ira_reg_equiv[i].memory = copy_rtx (res);
507 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
508 ira_reg_equiv[i].invariant = copy_rtx (res);
512 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
514 /* Update equivalence for REGNO. We need to this as the equivalence
515 might contain other pseudos which are changed by their
516 equivalences. */
517 static void
518 update_equiv (int regno)
520 rtx x;
522 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
523 ira_reg_equiv[regno].memory
524 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
525 NULL_RTX);
526 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
527 ira_reg_equiv[regno].invariant
528 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
529 NULL_RTX);
532 /* If we have decided to substitute X with another value, return that
533 value, otherwise return X. */
534 static rtx
535 get_equiv (rtx x)
537 int regno;
538 rtx res;
540 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
541 || ! ira_reg_equiv[regno].defined_p
542 || ! ira_reg_equiv[regno].profitable_p
543 || lra_get_regno_hard_regno (regno) >= 0)
544 return x;
545 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
547 if (targetm.cannot_substitute_mem_equiv_p (res))
548 return x;
549 return res;
551 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
552 return res;
553 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
554 return res;
555 gcc_unreachable ();
558 /* If we have decided to substitute X with the equivalent value,
559 return that value after elimination for INSN, otherwise return
560 X. */
561 static rtx
562 get_equiv_with_elimination (rtx x, rtx_insn *insn)
564 rtx res = get_equiv (x);
566 if (x == res || CONSTANT_P (res))
567 return res;
568 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
569 false, false, 0, true);
572 /* Set up curr_operand_mode. */
573 static void
574 init_curr_operand_mode (void)
576 int nop = curr_static_id->n_operands;
577 for (int i = 0; i < nop; i++)
579 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
580 if (mode == VOIDmode)
582 /* The .md mode for address operands is the mode of the
583 addressed value rather than the mode of the address itself. */
584 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
585 mode = Pmode;
586 else
587 mode = curr_static_id->operand[i].mode;
589 curr_operand_mode[i] = mode;
595 /* The page contains code to reuse input reloads. */
597 /* Structure describes input reload of the current insns. */
598 struct input_reload
600 /* True for input reload of matched operands. */
601 bool match_p;
602 /* Reloaded value. */
603 rtx input;
604 /* Reload pseudo used. */
605 rtx reg;
608 /* The number of elements in the following array. */
609 static int curr_insn_input_reloads_num;
610 /* Array containing info about input reloads. It is used to find the
611 same input reload and reuse the reload pseudo in this case. */
612 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
614 /* Initiate data concerning reuse of input reloads for the current
615 insn. */
616 static void
617 init_curr_insn_input_reloads (void)
619 curr_insn_input_reloads_num = 0;
622 /* The canonical form of an rtx inside a MEM is not necessarily the same as the
623 canonical form of the rtx outside the MEM. Fix this up in the case that
624 we're reloading an address (and therefore pulling it outside a MEM). */
625 static rtx
626 canonicalize_reload_addr (rtx addr)
628 subrtx_var_iterator::array_type array;
629 FOR_EACH_SUBRTX_VAR (iter, array, addr, NONCONST)
631 rtx x = *iter;
632 if (GET_CODE (x) == MULT && CONST_INT_P (XEXP (x, 1)))
634 const HOST_WIDE_INT ci = INTVAL (XEXP (x, 1));
635 const int pwr2 = exact_log2 (ci);
636 if (pwr2 > 0)
638 /* Rewrite this to use a shift instead, which is canonical when
639 outside of a MEM. */
640 PUT_CODE (x, ASHIFT);
641 XEXP (x, 1) = GEN_INT (pwr2);
646 return addr;
649 /* Create a new pseudo using MODE, RCLASS, EXCLUDE_START_HARD_REGS, ORIGINAL or
650 reuse an existing reload pseudo. Don't reuse an existing reload pseudo if
651 IN_SUBREG_P is true and the reused pseudo should be wrapped up in a SUBREG.
652 The result pseudo is returned through RESULT_REG. Return TRUE if we created
653 a new pseudo, FALSE if we reused an existing reload pseudo. Use TITLE to
654 describe new registers for debug purposes. */
655 static bool
656 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
657 enum reg_class rclass, HARD_REG_SET *exclude_start_hard_regs,
658 bool in_subreg_p, const char *title, rtx *result_reg)
660 int i, regno;
661 enum reg_class new_class;
662 bool unique_p = false;
664 if (type == OP_OUT)
666 /* Output reload registers tend to start out with a conservative
667 choice of register class. Usually this is ALL_REGS, although
668 a target might narrow it (for performance reasons) through
669 targetm.preferred_reload_class. It's therefore quite common
670 for a reload instruction to require a more restrictive class
671 than the class that was originally assigned to the reload register.
673 In these situations, it's more efficient to refine the choice
674 of register class rather than create a second reload register.
675 This also helps to avoid cycling for registers that are only
676 used by reload instructions. */
677 if (REG_P (original)
678 && (int) REGNO (original) >= new_regno_start
679 && INSN_UID (curr_insn) >= new_insn_uid_start
680 && in_class_p (original, rclass, &new_class, true))
682 unsigned int regno = REGNO (original);
683 if (lra_dump_file != NULL)
685 fprintf (lra_dump_file, " Reuse r%d for output ", regno);
686 dump_value_slim (lra_dump_file, original, 1);
688 if (new_class != lra_get_allocno_class (regno))
689 lra_change_class (regno, new_class, ", change to", false);
690 if (lra_dump_file != NULL)
691 fprintf (lra_dump_file, "\n");
692 *result_reg = original;
693 return false;
695 *result_reg
696 = lra_create_new_reg_with_unique_value (mode, original, rclass,
697 exclude_start_hard_regs, title);
698 return true;
700 /* Prevent reuse value of expression with side effects,
701 e.g. volatile memory. */
702 if (! side_effects_p (original))
703 for (i = 0; i < curr_insn_input_reloads_num; i++)
705 if (! curr_insn_input_reloads[i].match_p
706 && rtx_equal_p (curr_insn_input_reloads[i].input, original)
707 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
709 rtx reg = curr_insn_input_reloads[i].reg;
710 regno = REGNO (reg);
711 /* If input is equal to original and both are VOIDmode,
712 GET_MODE (reg) might be still different from mode.
713 Ensure we don't return *result_reg with wrong mode. */
714 if (GET_MODE (reg) != mode)
716 if (in_subreg_p)
717 continue;
718 if (maybe_lt (GET_MODE_SIZE (GET_MODE (reg)),
719 GET_MODE_SIZE (mode)))
720 continue;
721 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
722 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
723 continue;
725 *result_reg = reg;
726 if (lra_dump_file != NULL)
728 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
729 dump_value_slim (lra_dump_file, original, 1);
731 if (new_class != lra_get_allocno_class (regno))
732 lra_change_class (regno, new_class, ", change to", false);
733 if (lra_dump_file != NULL)
734 fprintf (lra_dump_file, "\n");
735 return false;
737 /* If we have an input reload with a different mode, make sure it
738 will get a different hard reg. */
739 else if (REG_P (original)
740 && REG_P (curr_insn_input_reloads[i].input)
741 && REGNO (original) == REGNO (curr_insn_input_reloads[i].input)
742 && (GET_MODE (original)
743 != GET_MODE (curr_insn_input_reloads[i].input)))
744 unique_p = true;
746 *result_reg = (unique_p
747 ? lra_create_new_reg_with_unique_value
748 : lra_create_new_reg) (mode, original, rclass,
749 exclude_start_hard_regs, title);
750 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
751 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
752 curr_insn_input_reloads[curr_insn_input_reloads_num].match_p = false;
753 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
754 return true;
758 /* The page contains major code to choose the current insn alternative
759 and generate reloads for it. */
761 /* Return the offset from REGNO of the least significant register
762 in (reg:MODE REGNO).
764 This function is used to tell whether two registers satisfy
765 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
767 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
768 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
770 lra_constraint_offset (int regno, machine_mode mode)
772 lra_assert (regno < FIRST_PSEUDO_REGISTER);
774 scalar_int_mode int_mode;
775 if (WORDS_BIG_ENDIAN
776 && is_a <scalar_int_mode> (mode, &int_mode)
777 && GET_MODE_SIZE (int_mode) > UNITS_PER_WORD)
778 return hard_regno_nregs (regno, mode) - 1;
779 return 0;
782 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
783 if they are the same hard reg, and has special hacks for
784 auto-increment and auto-decrement. This is specifically intended for
785 process_alt_operands to use in determining whether two operands
786 match. X is the operand whose number is the lower of the two.
788 It is supposed that X is the output operand and Y is the input
789 operand. Y_HARD_REGNO is the final hard regno of register Y or
790 register in subreg Y as we know it now. Otherwise, it is a
791 negative value. */
792 static bool
793 operands_match_p (rtx x, rtx y, int y_hard_regno)
795 int i;
796 RTX_CODE code = GET_CODE (x);
797 const char *fmt;
799 if (x == y)
800 return true;
801 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
802 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
804 int j;
806 i = get_hard_regno (x);
807 if (i < 0)
808 goto slow;
810 if ((j = y_hard_regno) < 0)
811 goto slow;
813 i += lra_constraint_offset (i, GET_MODE (x));
814 j += lra_constraint_offset (j, GET_MODE (y));
816 return i == j;
819 /* If two operands must match, because they are really a single
820 operand of an assembler insn, then two post-increments are invalid
821 because the assembler insn would increment only once. On the
822 other hand, a post-increment matches ordinary indexing if the
823 post-increment is the output operand. */
824 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
825 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
827 /* Two pre-increments are invalid because the assembler insn would
828 increment only once. On the other hand, a pre-increment matches
829 ordinary indexing if the pre-increment is the input operand. */
830 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
831 || GET_CODE (y) == PRE_MODIFY)
832 return operands_match_p (x, XEXP (y, 0), -1);
834 slow:
836 if (code == REG && REG_P (y))
837 return REGNO (x) == REGNO (y);
839 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
840 && x == SUBREG_REG (y))
841 return true;
842 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
843 && SUBREG_REG (x) == y)
844 return true;
846 /* Now we have disposed of all the cases in which different rtx
847 codes can match. */
848 if (code != GET_CODE (y))
849 return false;
851 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
852 if (GET_MODE (x) != GET_MODE (y))
853 return false;
855 switch (code)
857 CASE_CONST_UNIQUE:
858 return false;
860 case CONST_VECTOR:
861 if (!same_vector_encodings_p (x, y))
862 return false;
863 break;
865 case LABEL_REF:
866 return label_ref_label (x) == label_ref_label (y);
867 case SYMBOL_REF:
868 return XSTR (x, 0) == XSTR (y, 0);
870 default:
871 break;
874 /* Compare the elements. If any pair of corresponding elements fail
875 to match, return false for the whole things. */
877 fmt = GET_RTX_FORMAT (code);
878 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
880 int val, j;
881 switch (fmt[i])
883 case 'w':
884 if (XWINT (x, i) != XWINT (y, i))
885 return false;
886 break;
888 case 'i':
889 if (XINT (x, i) != XINT (y, i))
890 return false;
891 break;
893 case 'p':
894 if (maybe_ne (SUBREG_BYTE (x), SUBREG_BYTE (y)))
895 return false;
896 break;
898 case 'e':
899 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
900 if (val == 0)
901 return false;
902 break;
904 case '0':
905 break;
907 case 'E':
908 if (XVECLEN (x, i) != XVECLEN (y, i))
909 return false;
910 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
912 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
913 if (val == 0)
914 return false;
916 break;
918 /* It is believed that rtx's at this level will never
919 contain anything but integers and other rtx's, except for
920 within LABEL_REFs and SYMBOL_REFs. */
921 default:
922 gcc_unreachable ();
925 return true;
928 /* True if X is a constant that can be forced into the constant pool.
929 MODE is the mode of the operand, or VOIDmode if not known. */
930 #define CONST_POOL_OK_P(MODE, X) \
931 ((MODE) != VOIDmode \
932 && CONSTANT_P (X) \
933 && GET_CODE (X) != HIGH \
934 && GET_MODE_SIZE (MODE).is_constant () \
935 && !targetm.cannot_force_const_mem (MODE, X))
937 /* If REG is a reload pseudo, try to make its class satisfying CL. */
938 static void
939 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
941 enum reg_class rclass;
943 /* Do not make more accurate class from reloads generated. They are
944 mostly moves with a lot of constraints. Making more accurate
945 class may results in very narrow class and impossibility of find
946 registers for several reloads of one insn. */
947 if (INSN_UID (curr_insn) >= new_insn_uid_start)
948 return;
949 if (GET_CODE (reg) == SUBREG)
950 reg = SUBREG_REG (reg);
951 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
952 return;
953 if (in_class_p (reg, cl, &rclass) && rclass != cl)
954 lra_change_class (REGNO (reg), rclass, " Change to", true);
957 /* Searches X for any reference to a reg with the same value as REGNO,
958 returning the rtx of the reference found if any. Otherwise,
959 returns NULL_RTX. */
960 static rtx
961 regno_val_use_in (unsigned int regno, rtx x)
963 const char *fmt;
964 int i, j;
965 rtx tem;
967 if (REG_P (x) && lra_reg_info[REGNO (x)].val == lra_reg_info[regno].val)
968 return x;
970 fmt = GET_RTX_FORMAT (GET_CODE (x));
971 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
973 if (fmt[i] == 'e')
975 if ((tem = regno_val_use_in (regno, XEXP (x, i))))
976 return tem;
978 else if (fmt[i] == 'E')
979 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
980 if ((tem = regno_val_use_in (regno , XVECEXP (x, i, j))))
981 return tem;
984 return NULL_RTX;
987 /* Return true if all current insn non-output operands except INS (it
988 has a negaitve end marker) do not use pseudos with the same value
989 as REGNO. */
990 static bool
991 check_conflict_input_operands (int regno, signed char *ins)
993 int in;
994 int n_operands = curr_static_id->n_operands;
996 for (int nop = 0; nop < n_operands; nop++)
997 if (! curr_static_id->operand[nop].is_operator
998 && curr_static_id->operand[nop].type != OP_OUT)
1000 for (int i = 0; (in = ins[i]) >= 0; i++)
1001 if (in == nop)
1002 break;
1003 if (in < 0
1004 && regno_val_use_in (regno, *curr_id->operand_loc[nop]) != NULL_RTX)
1005 return false;
1007 return true;
1010 /* Generate reloads for matching OUT and INS (array of input operand numbers
1011 with end marker -1) with reg class GOAL_CLASS and EXCLUDE_START_HARD_REGS,
1012 considering output operands OUTS (similar array to INS) needing to be in
1013 different registers. Add input and output reloads correspondingly to the
1014 lists *BEFORE and *AFTER. OUT might be negative. In this case we generate
1015 input reloads for matched input operands INS. EARLY_CLOBBER_P is a flag
1016 that the output operand is early clobbered for chosen alternative. */
1017 static void
1018 match_reload (signed char out, signed char *ins, signed char *outs,
1019 enum reg_class goal_class, HARD_REG_SET *exclude_start_hard_regs,
1020 rtx_insn **before, rtx_insn **after, bool early_clobber_p)
1022 bool out_conflict;
1023 int i, in;
1024 rtx new_in_reg, new_out_reg, reg;
1025 machine_mode inmode, outmode;
1026 rtx in_rtx = *curr_id->operand_loc[ins[0]];
1027 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
1029 inmode = curr_operand_mode[ins[0]];
1030 outmode = out < 0 ? inmode : curr_operand_mode[out];
1031 push_to_sequence (*before);
1032 if (inmode != outmode)
1034 /* process_alt_operands has already checked that the mode sizes
1035 are ordered. */
1036 if (partial_subreg_p (outmode, inmode))
1038 bool asm_p = asm_noperands (PATTERN (curr_insn)) >= 0;
1039 int hr;
1040 HARD_REG_SET temp_hard_reg_set;
1042 if (asm_p && (hr = get_hard_regno (out_rtx)) >= 0
1043 && hard_regno_nregs (hr, inmode) > 1)
1045 /* See gcc.c-torture/execute/20030222-1.c.
1046 Consider the code for 32-bit (e.g. BE) target:
1047 int i, v; long x; x = v; asm ("" : "=r" (i) : "0" (x));
1048 We generate the following RTL with reload insns:
1049 1. subreg:si(x:di, 0) = 0;
1050 2. subreg:si(x:di, 4) = v:si;
1051 3. t:di = x:di, dead x;
1052 4. asm ("" : "=r" (subreg:si(t:di,4)) : "0" (t:di))
1053 5. i:si = subreg:si(t:di,4);
1054 If we assign hard reg of x to t, dead code elimination
1055 will remove insn #2 and we will use unitialized hard reg.
1056 So exclude the hard reg of x for t. We could ignore this
1057 problem for non-empty asm using all x value but it is hard to
1058 check that the asm are expanded into insn realy using x
1059 and setting r. */
1060 CLEAR_HARD_REG_SET (temp_hard_reg_set);
1061 if (exclude_start_hard_regs != NULL)
1062 temp_hard_reg_set = *exclude_start_hard_regs;
1063 SET_HARD_REG_BIT (temp_hard_reg_set, hr);
1064 exclude_start_hard_regs = &temp_hard_reg_set;
1066 reg = new_in_reg
1067 = lra_create_new_reg_with_unique_value (inmode, in_rtx, goal_class,
1068 exclude_start_hard_regs,
1069 "");
1070 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
1071 LRA_SUBREG_P (new_out_reg) = 1;
1072 /* If the input reg is dying here, we can use the same hard
1073 register for REG and IN_RTX. We do it only for original
1074 pseudos as reload pseudos can die although original
1075 pseudos still live where reload pseudos dies. */
1076 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
1077 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
1078 && (!early_clobber_p
1079 || check_conflict_input_operands(REGNO (in_rtx), ins)))
1080 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
1082 else
1084 reg = new_out_reg
1085 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
1086 goal_class,
1087 exclude_start_hard_regs,
1088 "");
1089 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
1090 /* NEW_IN_REG is non-paradoxical subreg. We don't want
1091 NEW_OUT_REG living above. We add clobber clause for
1092 this. This is just a temporary clobber. We can remove
1093 it at the end of LRA work. */
1094 rtx_insn *clobber = emit_clobber (new_out_reg);
1095 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
1096 LRA_SUBREG_P (new_in_reg) = 1;
1097 if (GET_CODE (in_rtx) == SUBREG)
1099 rtx subreg_reg = SUBREG_REG (in_rtx);
1101 /* If SUBREG_REG is dying here and sub-registers IN_RTX
1102 and NEW_IN_REG are similar, we can use the same hard
1103 register for REG and SUBREG_REG. */
1104 if (REG_P (subreg_reg)
1105 && (int) REGNO (subreg_reg) < lra_new_regno_start
1106 && GET_MODE (subreg_reg) == outmode
1107 && known_eq (SUBREG_BYTE (in_rtx), SUBREG_BYTE (new_in_reg))
1108 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg))
1109 && (! early_clobber_p
1110 || check_conflict_input_operands (REGNO (subreg_reg),
1111 ins)))
1112 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
1116 else
1118 /* Pseudos have values -- see comments for lra_reg_info.
1119 Different pseudos with the same value do not conflict even if
1120 they live in the same place. When we create a pseudo we
1121 assign value of original pseudo (if any) from which we
1122 created the new pseudo. If we create the pseudo from the
1123 input pseudo, the new pseudo will have no conflict with the
1124 input pseudo which is wrong when the input pseudo lives after
1125 the insn and as the new pseudo value is changed by the insn
1126 output. Therefore we create the new pseudo from the output
1127 except the case when we have single matched dying input
1128 pseudo.
1130 We cannot reuse the current output register because we might
1131 have a situation like "a <- a op b", where the constraints
1132 force the second input operand ("b") to match the output
1133 operand ("a"). "b" must then be copied into a new register
1134 so that it doesn't clobber the current value of "a".
1136 We cannot use the same value if the output pseudo is
1137 early clobbered or the input pseudo is mentioned in the
1138 output, e.g. as an address part in memory, because
1139 output reload will actually extend the pseudo liveness.
1140 We don't care about eliminable hard regs here as we are
1141 interesting only in pseudos. */
1143 /* Matching input's register value is the same as one of the other
1144 output operand. Output operands in a parallel insn must be in
1145 different registers. */
1146 out_conflict = false;
1147 if (REG_P (in_rtx))
1149 for (i = 0; outs[i] >= 0; i++)
1151 rtx other_out_rtx = *curr_id->operand_loc[outs[i]];
1152 if (outs[i] != out && REG_P (other_out_rtx)
1153 && (regno_val_use_in (REGNO (in_rtx), other_out_rtx)
1154 != NULL_RTX))
1156 out_conflict = true;
1157 break;
1162 new_in_reg = new_out_reg
1163 = (! early_clobber_p && ins[1] < 0 && REG_P (in_rtx)
1164 && (int) REGNO (in_rtx) < lra_new_regno_start
1165 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
1166 && (! early_clobber_p
1167 || check_conflict_input_operands (REGNO (in_rtx), ins))
1168 && (out < 0
1169 || regno_val_use_in (REGNO (in_rtx), out_rtx) == NULL_RTX)
1170 && !out_conflict
1171 ? lra_create_new_reg (inmode, in_rtx, goal_class,
1172 exclude_start_hard_regs, "")
1173 : lra_create_new_reg_with_unique_value (outmode, out_rtx, goal_class,
1174 exclude_start_hard_regs,
1175 ""));
1177 /* In operand can be got from transformations before processing insn
1178 constraints. One example of such transformations is subreg
1179 reloading (see function simplify_operand_subreg). The new
1180 pseudos created by the transformations might have inaccurate
1181 class (ALL_REGS) and we should make their classes more
1182 accurate. */
1183 narrow_reload_pseudo_class (in_rtx, goal_class);
1184 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
1185 *before = get_insns ();
1186 end_sequence ();
1187 /* Add the new pseudo to consider values of subsequent input reload
1188 pseudos. */
1189 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
1190 curr_insn_input_reloads[curr_insn_input_reloads_num].input = in_rtx;
1191 curr_insn_input_reloads[curr_insn_input_reloads_num].match_p = true;
1192 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = new_in_reg;
1193 for (i = 0; (in = ins[i]) >= 0; i++)
1194 if (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
1195 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]))
1196 *curr_id->operand_loc[in] = new_in_reg;
1197 else
1199 lra_assert
1200 (GET_MODE (new_out_reg) == GET_MODE (*curr_id->operand_loc[in]));
1201 *curr_id->operand_loc[in] = new_out_reg;
1203 lra_update_dups (curr_id, ins);
1204 if (out < 0)
1205 return;
1206 /* See a comment for the input operand above. */
1207 narrow_reload_pseudo_class (out_rtx, goal_class);
1208 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
1210 reg = SUBREG_P (out_rtx) ? SUBREG_REG (out_rtx) : out_rtx;
1211 start_sequence ();
1212 /* If we had strict_low_part, use it also in reload to keep other
1213 parts unchanged but do it only for regs as strict_low_part
1214 has no sense for memory and probably there is no insn pattern
1215 to match the reload insn in memory case. */
1216 if (out >= 0 && curr_static_id->operand[out].strict_low && REG_P (reg))
1217 out_rtx = gen_rtx_STRICT_LOW_PART (VOIDmode, out_rtx);
1218 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
1219 emit_insn (*after);
1220 *after = get_insns ();
1221 end_sequence ();
1223 *curr_id->operand_loc[out] = new_out_reg;
1224 lra_update_dup (curr_id, out);
1227 /* Return register class which is union of all reg classes in insn
1228 constraint alternative string starting with P. */
1229 static enum reg_class
1230 reg_class_from_constraints (const char *p)
1232 int c, len;
1233 enum reg_class op_class = NO_REGS;
1236 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1238 case '#':
1239 case ',':
1240 return op_class;
1242 case 'g':
1243 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1244 break;
1246 default:
1247 enum constraint_num cn = lookup_constraint (p);
1248 enum reg_class cl = reg_class_for_constraint (cn);
1249 if (cl == NO_REGS)
1251 if (insn_extra_address_constraint (cn))
1252 op_class
1253 = (reg_class_subunion
1254 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1255 ADDRESS, SCRATCH)]);
1256 break;
1259 op_class = reg_class_subunion[op_class][cl];
1260 break;
1262 while ((p += len), c);
1263 return op_class;
1266 /* If OP is a register, return the class of the register as per
1267 get_reg_class, otherwise return NO_REGS. */
1268 static inline enum reg_class
1269 get_op_class (rtx op)
1271 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1274 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1275 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1276 SUBREG for VAL to make them equal. */
1277 static rtx_insn *
1278 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1280 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1282 /* Usually size of mem_pseudo is greater than val size but in
1283 rare cases it can be less as it can be defined by target
1284 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1285 if (! MEM_P (val))
1287 val = gen_lowpart_SUBREG (GET_MODE (mem_pseudo),
1288 GET_CODE (val) == SUBREG
1289 ? SUBREG_REG (val) : val);
1290 LRA_SUBREG_P (val) = 1;
1292 else
1294 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1295 LRA_SUBREG_P (mem_pseudo) = 1;
1298 return to_p ? gen_move_insn (mem_pseudo, val)
1299 : gen_move_insn (val, mem_pseudo);
1302 /* Process a special case insn (register move), return true if we
1303 don't need to process it anymore. INSN should be a single set
1304 insn. Set up that RTL was changed through CHANGE_P and that hook
1305 TARGET_SECONDARY_MEMORY_NEEDED says to use secondary memory through
1306 SEC_MEM_P. */
1307 static bool
1308 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1310 int sregno, dregno;
1311 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1312 rtx_insn *before;
1313 enum reg_class dclass, sclass, secondary_class;
1314 secondary_reload_info sri;
1316 lra_assert (curr_insn_set != NULL_RTX);
1317 dreg = dest = SET_DEST (curr_insn_set);
1318 sreg = src = SET_SRC (curr_insn_set);
1319 if (GET_CODE (dest) == SUBREG)
1320 dreg = SUBREG_REG (dest);
1321 if (GET_CODE (src) == SUBREG)
1322 sreg = SUBREG_REG (src);
1323 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1324 return false;
1325 sclass = dclass = NO_REGS;
1326 if (REG_P (dreg))
1327 dclass = get_reg_class (REGNO (dreg));
1328 gcc_assert (dclass < LIM_REG_CLASSES && dclass >= NO_REGS);
1329 if (dclass == ALL_REGS)
1330 /* ALL_REGS is used for new pseudos created by transformations
1331 like reload of SUBREG_REG (see function
1332 simplify_operand_subreg). We don't know their class yet. We
1333 should figure out the class from processing the insn
1334 constraints not in this fast path function. Even if ALL_REGS
1335 were a right class for the pseudo, secondary_... hooks usually
1336 are not define for ALL_REGS. */
1337 return false;
1338 if (REG_P (sreg))
1339 sclass = get_reg_class (REGNO (sreg));
1340 gcc_assert (sclass < LIM_REG_CLASSES && sclass >= NO_REGS);
1341 if (sclass == ALL_REGS)
1342 /* See comments above. */
1343 return false;
1344 if (sclass == NO_REGS && dclass == NO_REGS)
1345 return false;
1346 if (targetm.secondary_memory_needed (GET_MODE (src), sclass, dclass)
1347 && ((sclass != NO_REGS && dclass != NO_REGS)
1348 || (GET_MODE (src)
1349 != targetm.secondary_memory_needed_mode (GET_MODE (src)))))
1351 *sec_mem_p = true;
1352 return false;
1354 if (! REG_P (dreg) || ! REG_P (sreg))
1355 return false;
1356 sri.prev_sri = NULL;
1357 sri.icode = CODE_FOR_nothing;
1358 sri.extra_cost = 0;
1359 secondary_class = NO_REGS;
1360 /* Set up hard register for a reload pseudo for hook
1361 secondary_reload because some targets just ignore unassigned
1362 pseudos in the hook. */
1363 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1365 dregno = REGNO (dreg);
1366 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1368 else
1369 dregno = -1;
1370 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1372 sregno = REGNO (sreg);
1373 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1375 else
1376 sregno = -1;
1377 if (sclass != NO_REGS)
1378 secondary_class
1379 = (enum reg_class) targetm.secondary_reload (false, dest,
1380 (reg_class_t) sclass,
1381 GET_MODE (src), &sri);
1382 if (sclass == NO_REGS
1383 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1384 && dclass != NO_REGS))
1386 enum reg_class old_sclass = secondary_class;
1387 secondary_reload_info old_sri = sri;
1389 sri.prev_sri = NULL;
1390 sri.icode = CODE_FOR_nothing;
1391 sri.extra_cost = 0;
1392 secondary_class
1393 = (enum reg_class) targetm.secondary_reload (true, src,
1394 (reg_class_t) dclass,
1395 GET_MODE (src), &sri);
1396 /* Check the target hook consistency. */
1397 lra_assert
1398 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1399 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1400 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1402 if (sregno >= 0)
1403 reg_renumber [sregno] = -1;
1404 if (dregno >= 0)
1405 reg_renumber [dregno] = -1;
1406 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1407 return false;
1408 *change_p = true;
1409 new_reg = NULL_RTX;
1410 if (secondary_class != NO_REGS)
1411 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1412 secondary_class, NULL,
1413 "secondary");
1414 start_sequence ();
1415 if (sri.icode == CODE_FOR_nothing)
1416 lra_emit_move (new_reg, src);
1417 else
1419 enum reg_class scratch_class;
1421 scratch_class = (reg_class_from_constraints
1422 (insn_data[sri.icode].operand[2].constraint));
1423 scratch_reg = (lra_create_new_reg_with_unique_value
1424 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1425 scratch_class, NULL, "scratch"));
1426 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1427 src, scratch_reg));
1429 before = get_insns ();
1430 end_sequence ();
1431 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1432 if (new_reg != NULL_RTX)
1433 SET_SRC (curr_insn_set) = new_reg;
1434 else
1436 if (lra_dump_file != NULL)
1438 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1439 dump_insn_slim (lra_dump_file, curr_insn);
1441 lra_set_insn_deleted (curr_insn);
1442 return true;
1444 return false;
1447 /* The following data describe the result of process_alt_operands.
1448 The data are used in curr_insn_transform to generate reloads. */
1450 /* The chosen reg classes which should be used for the corresponding
1451 operands. */
1452 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1453 /* Hard registers which cannot be a start hard register for the corresponding
1454 operands. */
1455 static HARD_REG_SET goal_alt_exclude_start_hard_regs[MAX_RECOG_OPERANDS];
1456 /* True if the operand should be the same as another operand and that
1457 other operand does not need a reload. */
1458 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1459 /* True if the operand does not need a reload. */
1460 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1461 /* True if the operand can be offsetable memory. */
1462 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1463 /* The number of an operand to which given operand can be matched to. */
1464 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1465 /* The number of elements in the following array. */
1466 static int goal_alt_dont_inherit_ops_num;
1467 /* Numbers of operands whose reload pseudos should not be inherited. */
1468 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1469 /* True if we should try only this alternative for the next constraint sub-pass
1470 to speed up the sub-pass. */
1471 static bool goal_reuse_alt_p;
1472 /* True if the insn commutative operands should be swapped. */
1473 static bool goal_alt_swapped;
1474 /* The chosen insn alternative. */
1475 static int goal_alt_number;
1476 /* True if output reload of the stack pointer should be generated. */
1477 static bool goal_alt_out_sp_reload_p;
1479 /* True if the corresponding operand is the result of an equivalence
1480 substitution. */
1481 static bool equiv_substition_p[MAX_RECOG_OPERANDS];
1483 /* The following five variables are used to choose the best insn
1484 alternative. They reflect final characteristics of the best
1485 alternative. */
1487 /* Number of necessary reloads and overall cost reflecting the
1488 previous value and other unpleasantness of the best alternative. */
1489 static int best_losers, best_overall;
1490 /* Overall number hard registers used for reloads. For example, on
1491 some targets we need 2 general registers to reload DFmode and only
1492 one floating point register. */
1493 static int best_reload_nregs;
1494 /* Overall number reflecting distances of previous reloading the same
1495 value. The distances are counted from the current BB start. It is
1496 used to improve inheritance chances. */
1497 static int best_reload_sum;
1499 /* True if the current insn should have no correspondingly input or
1500 output reloads. */
1501 static bool no_input_reloads_p, no_output_reloads_p;
1503 /* True if we swapped the commutative operands in the current
1504 insn. */
1505 static int curr_swapped;
1507 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1508 register of class CL. Add any input reloads to list BEFORE. AFTER
1509 is nonnull if *LOC is an automodified value; handle that case by
1510 adding the required output reloads to list AFTER. Return true if
1511 the RTL was changed.
1513 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1514 register. Return false if the address register is correct. */
1515 static bool
1516 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1517 enum reg_class cl)
1519 int regno;
1520 enum reg_class rclass, new_class;
1521 rtx reg;
1522 rtx new_reg;
1523 machine_mode mode;
1524 bool subreg_p, before_p = false;
1526 subreg_p = GET_CODE (*loc) == SUBREG;
1527 if (subreg_p)
1529 reg = SUBREG_REG (*loc);
1530 mode = GET_MODE (reg);
1532 /* For mode with size bigger than ptr_mode, there unlikely to be "mov"
1533 between two registers with different classes, but there normally will
1534 be "mov" which transfers element of vector register into the general
1535 register, and this normally will be a subreg which should be reloaded
1536 as a whole. This is particularly likely to be triggered when
1537 -fno-split-wide-types specified. */
1538 if (!REG_P (reg)
1539 || in_class_p (reg, cl, &new_class)
1540 || known_le (GET_MODE_SIZE (mode), GET_MODE_SIZE (ptr_mode)))
1541 loc = &SUBREG_REG (*loc);
1544 reg = *loc;
1545 mode = GET_MODE (reg);
1546 if (! REG_P (reg))
1548 if (check_only_p)
1549 return true;
1550 /* Always reload memory in an address even if the target supports
1551 such addresses. */
1552 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, NULL,
1553 "address");
1554 before_p = true;
1556 else
1558 regno = REGNO (reg);
1559 rclass = get_reg_class (regno);
1560 if (! check_only_p
1561 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1563 if (lra_dump_file != NULL)
1565 fprintf (lra_dump_file,
1566 "Changing pseudo %d in address of insn %u on equiv ",
1567 REGNO (reg), INSN_UID (curr_insn));
1568 dump_value_slim (lra_dump_file, *loc, 1);
1569 fprintf (lra_dump_file, "\n");
1571 *loc = copy_rtx (*loc);
1573 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1575 if (check_only_p)
1576 return true;
1577 reg = *loc;
1578 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1579 mode, reg, cl, NULL,
1580 subreg_p, "address", &new_reg))
1581 before_p = true;
1583 else if (new_class != NO_REGS && rclass != new_class)
1585 if (check_only_p)
1586 return true;
1587 lra_change_class (regno, new_class, " Change to", true);
1588 return false;
1590 else
1591 return false;
1593 if (before_p)
1595 push_to_sequence (*before);
1596 lra_emit_move (new_reg, reg);
1597 *before = get_insns ();
1598 end_sequence ();
1600 *loc = new_reg;
1601 if (after != NULL)
1603 start_sequence ();
1604 lra_emit_move (before_p ? copy_rtx (reg) : reg, new_reg);
1605 emit_insn (*after);
1606 *after = get_insns ();
1607 end_sequence ();
1609 return true;
1612 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1613 the insn to be inserted before curr insn. AFTER returns the
1614 the insn to be inserted after curr insn. ORIGREG and NEWREG
1615 are the original reg and new reg for reload. */
1616 static void
1617 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1618 rtx newreg)
1620 if (before)
1622 push_to_sequence (*before);
1623 lra_emit_move (newreg, origreg);
1624 *before = get_insns ();
1625 end_sequence ();
1627 if (after)
1629 start_sequence ();
1630 lra_emit_move (origreg, newreg);
1631 emit_insn (*after);
1632 *after = get_insns ();
1633 end_sequence ();
1637 static bool valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1638 static bool process_address (int, bool, rtx_insn **, rtx_insn **);
1640 /* Make reloads for subreg in operand NOP with internal subreg mode
1641 REG_MODE, add new reloads for further processing. Return true if
1642 any change was done. */
1643 static bool
1644 simplify_operand_subreg (int nop, machine_mode reg_mode)
1646 int hard_regno, inner_hard_regno;
1647 rtx_insn *before, *after;
1648 machine_mode mode, innermode;
1649 rtx reg, new_reg;
1650 rtx operand = *curr_id->operand_loc[nop];
1651 enum reg_class regclass;
1652 enum op_type type;
1654 before = after = NULL;
1656 if (GET_CODE (operand) != SUBREG)
1657 return false;
1659 mode = GET_MODE (operand);
1660 reg = SUBREG_REG (operand);
1661 innermode = GET_MODE (reg);
1662 type = curr_static_id->operand[nop].type;
1663 if (MEM_P (reg))
1665 const bool addr_was_valid
1666 = valid_address_p (innermode, XEXP (reg, 0), MEM_ADDR_SPACE (reg));
1667 alter_subreg (curr_id->operand_loc[nop], false);
1668 rtx subst = *curr_id->operand_loc[nop];
1669 lra_assert (MEM_P (subst));
1670 const bool addr_is_valid = valid_address_p (GET_MODE (subst),
1671 XEXP (subst, 0),
1672 MEM_ADDR_SPACE (subst));
1673 if (!addr_was_valid
1674 || addr_is_valid
1675 || ((get_constraint_type (lookup_constraint
1676 (curr_static_id->operand[nop].constraint))
1677 != CT_SPECIAL_MEMORY)
1678 /* We still can reload address and if the address is
1679 valid, we can remove subreg without reloading its
1680 inner memory. */
1681 && valid_address_p (GET_MODE (subst),
1682 regno_reg_rtx
1683 [ira_class_hard_regs
1684 [base_reg_class (GET_MODE (subst),
1685 MEM_ADDR_SPACE (subst),
1686 ADDRESS, SCRATCH)][0]],
1687 MEM_ADDR_SPACE (subst))))
1689 /* If we change the address for a paradoxical subreg of memory, the
1690 new address might violate the necessary alignment or the access
1691 might be slow; take this into consideration. We need not worry
1692 about accesses beyond allocated memory for paradoxical memory
1693 subregs as we don't substitute such equiv memory (see processing
1694 equivalences in function lra_constraints) and because for spilled
1695 pseudos we allocate stack memory enough for the biggest
1696 corresponding paradoxical subreg.
1698 However, do not blindly simplify a (subreg (mem ...)) for
1699 WORD_REGISTER_OPERATIONS targets as this may lead to loading junk
1700 data into a register when the inner is narrower than outer or
1701 missing important data from memory when the inner is wider than
1702 outer. This rule only applies to modes that are no wider than
1703 a word.
1705 If valid memory becomes invalid after subreg elimination
1706 and address might be different we still have to reload
1707 memory.
1709 if ((! addr_was_valid
1710 || addr_is_valid
1711 || known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (innermode)))
1712 && !(maybe_ne (GET_MODE_PRECISION (mode),
1713 GET_MODE_PRECISION (innermode))
1714 && known_le (GET_MODE_SIZE (mode), UNITS_PER_WORD)
1715 && known_le (GET_MODE_SIZE (innermode), UNITS_PER_WORD)
1716 && WORD_REGISTER_OPERATIONS)
1717 && (!(MEM_ALIGN (subst) < GET_MODE_ALIGNMENT (mode)
1718 && targetm.slow_unaligned_access (mode, MEM_ALIGN (subst)))
1719 || (MEM_ALIGN (reg) < GET_MODE_ALIGNMENT (innermode)
1720 && targetm.slow_unaligned_access (innermode,
1721 MEM_ALIGN (reg)))))
1722 return true;
1724 *curr_id->operand_loc[nop] = operand;
1726 /* But if the address was not valid, we cannot reload the MEM without
1727 reloading the address first. */
1728 if (!addr_was_valid)
1729 process_address (nop, false, &before, &after);
1731 /* INNERMODE is fast, MODE slow. Reload the mem in INNERMODE. */
1732 enum reg_class rclass
1733 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1734 if (get_reload_reg (curr_static_id->operand[nop].type, innermode,
1735 reg, rclass, NULL,
1736 true, "slow/invalid mem", &new_reg))
1738 bool insert_before, insert_after;
1739 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1741 insert_before = (type != OP_OUT
1742 || partial_subreg_p (mode, innermode));
1743 insert_after = type != OP_IN;
1744 insert_move_for_subreg (insert_before ? &before : NULL,
1745 insert_after ? &after : NULL,
1746 reg, new_reg);
1748 SUBREG_REG (operand) = new_reg;
1750 /* Convert to MODE. */
1751 reg = operand;
1752 rclass
1753 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1754 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1755 rclass, NULL,
1756 true, "slow/invalid mem", &new_reg))
1758 bool insert_before, insert_after;
1759 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1761 insert_before = type != OP_OUT;
1762 insert_after = type != OP_IN;
1763 insert_move_for_subreg (insert_before ? &before : NULL,
1764 insert_after ? &after : NULL,
1765 reg, new_reg);
1767 *curr_id->operand_loc[nop] = new_reg;
1768 lra_process_new_insns (curr_insn, before, after,
1769 "Inserting slow/invalid mem reload");
1770 return true;
1773 /* If the address was valid and became invalid, prefer to reload
1774 the memory. Typical case is when the index scale should
1775 correspond the memory. */
1776 *curr_id->operand_loc[nop] = operand;
1777 /* Do not return false here as the MEM_P (reg) will be processed
1778 later in this function. */
1780 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1782 alter_subreg (curr_id->operand_loc[nop], false);
1783 return true;
1785 else if (CONSTANT_P (reg))
1787 /* Try to simplify subreg of constant. It is usually result of
1788 equivalence substitution. */
1789 if (innermode == VOIDmode
1790 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1791 innermode = curr_static_id->operand[nop].mode;
1792 if ((new_reg = simplify_subreg (mode, reg, innermode,
1793 SUBREG_BYTE (operand))) != NULL_RTX)
1795 *curr_id->operand_loc[nop] = new_reg;
1796 return true;
1799 /* Put constant into memory when we have mixed modes. It generates
1800 a better code in most cases as it does not need a secondary
1801 reload memory. It also prevents LRA looping when LRA is using
1802 secondary reload memory again and again. */
1803 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1804 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1806 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1807 alter_subreg (curr_id->operand_loc[nop], false);
1808 return true;
1810 auto fp_subreg_can_be_simplified_after_reload_p = [] (machine_mode innermode,
1811 poly_uint64 offset,
1812 machine_mode mode) {
1813 reload_completed = 1;
1814 bool res = simplify_subreg_regno (FRAME_POINTER_REGNUM,
1815 innermode,
1816 offset, mode) >= 0;
1817 reload_completed = 0;
1818 return res;
1820 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1821 if there may be a problem accessing OPERAND in the outer
1822 mode. */
1823 if ((REG_P (reg)
1824 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1825 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1826 /* Don't reload paradoxical subregs because we could be looping
1827 having repeatedly final regno out of hard regs range. */
1828 && (hard_regno_nregs (hard_regno, innermode)
1829 >= hard_regno_nregs (hard_regno, mode))
1830 && simplify_subreg_regno (hard_regno, innermode,
1831 SUBREG_BYTE (operand), mode) < 0
1832 /* Exclude reloading of frame pointer in subreg if frame pointer can not
1833 be simplified here only because the reload is not finished yet. */
1834 && (hard_regno != FRAME_POINTER_REGNUM
1835 || !fp_subreg_can_be_simplified_after_reload_p (innermode,
1836 SUBREG_BYTE (operand),
1837 mode))
1838 /* Don't reload subreg for matching reload. It is actually
1839 valid subreg in LRA. */
1840 && ! LRA_SUBREG_P (operand))
1841 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1843 enum reg_class rclass;
1845 if (REG_P (reg))
1846 /* There is a big probability that we will get the same class
1847 for the new pseudo and we will get the same insn which
1848 means infinite looping. So spill the new pseudo. */
1849 rclass = NO_REGS;
1850 else
1851 /* The class will be defined later in curr_insn_transform. */
1852 rclass
1853 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1855 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1856 rclass, NULL,
1857 true, "subreg reg", &new_reg))
1859 bool insert_before, insert_after;
1860 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1862 insert_before = (type != OP_OUT
1863 || read_modify_subreg_p (operand));
1864 insert_after = (type != OP_IN);
1865 insert_move_for_subreg (insert_before ? &before : NULL,
1866 insert_after ? &after : NULL,
1867 reg, new_reg);
1869 SUBREG_REG (operand) = new_reg;
1870 lra_process_new_insns (curr_insn, before, after,
1871 "Inserting subreg reload");
1872 return true;
1874 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1875 IRA allocates hardreg to the inner pseudo reg according to its mode
1876 instead of the outermode, so the size of the hardreg may not be enough
1877 to contain the outermode operand, in that case we may need to insert
1878 reload for the reg. For the following two types of paradoxical subreg,
1879 we need to insert reload:
1880 1. If the op_type is OP_IN, and the hardreg could not be paired with
1881 other hardreg to contain the outermode operand
1882 (checked by in_hard_reg_set_p), we need to insert the reload.
1883 2. If the op_type is OP_OUT or OP_INOUT.
1885 Here is a paradoxical subreg example showing how the reload is generated:
1887 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1888 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1890 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1891 here, if reg107 is assigned to hardreg R15, because R15 is the last
1892 hardreg, compiler cannot find another hardreg to pair with R15 to
1893 contain TImode data. So we insert a TImode reload reg180 for it.
1894 After reload is inserted:
1896 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1897 (reg:DI 107 [ __comp ])) -1
1898 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1899 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1901 Two reload hard registers will be allocated to reg180 to save TImode data
1902 in LRA_assign.
1904 For LRA pseudos this should normally be handled by the biggest_mode
1905 mechanism. However, it's possible for new uses of an LRA pseudo
1906 to be introduced after we've allocated it, such as when undoing
1907 inheritance, and the allocated register might not then be appropriate
1908 for the new uses. */
1909 else if (REG_P (reg)
1910 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1911 && paradoxical_subreg_p (operand)
1912 && (inner_hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1913 && ((hard_regno
1914 = simplify_subreg_regno (inner_hard_regno, innermode,
1915 SUBREG_BYTE (operand), mode)) < 0
1916 || ((hard_regno_nregs (inner_hard_regno, innermode)
1917 < hard_regno_nregs (hard_regno, mode))
1918 && (regclass = lra_get_allocno_class (REGNO (reg)))
1919 && (type != OP_IN
1920 || !in_hard_reg_set_p (reg_class_contents[regclass],
1921 mode, hard_regno)
1922 || overlaps_hard_reg_set_p (lra_no_alloc_regs,
1923 mode, hard_regno)))))
1925 /* The class will be defined later in curr_insn_transform. */
1926 enum reg_class rclass
1927 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1929 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1930 rclass, NULL,
1931 true, "paradoxical subreg", &new_reg))
1933 rtx subreg;
1934 bool insert_before, insert_after;
1936 PUT_MODE (new_reg, mode);
1937 subreg = gen_lowpart_SUBREG (innermode, new_reg);
1938 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1940 insert_before = (type != OP_OUT);
1941 insert_after = (type != OP_IN);
1942 insert_move_for_subreg (insert_before ? &before : NULL,
1943 insert_after ? &after : NULL,
1944 reg, subreg);
1946 SUBREG_REG (operand) = new_reg;
1947 lra_process_new_insns (curr_insn, before, after,
1948 "Inserting paradoxical subreg reload");
1949 return true;
1951 return false;
1954 /* Return TRUE if X refers for a hard register from SET. */
1955 static bool
1956 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1958 int i, j, x_hard_regno;
1959 machine_mode mode;
1960 const char *fmt;
1961 enum rtx_code code;
1963 if (x == NULL_RTX)
1964 return false;
1965 code = GET_CODE (x);
1966 mode = GET_MODE (x);
1968 if (code == SUBREG)
1970 /* For all SUBREGs we want to check whether the full multi-register
1971 overlaps the set. For normal SUBREGs this means 'get_hard_regno' of
1972 the inner register, for paradoxical SUBREGs this means the
1973 'get_hard_regno' of the full SUBREG and for complete SUBREGs either is
1974 fine. Use the wider mode for all cases. */
1975 rtx subreg = SUBREG_REG (x);
1976 mode = wider_subreg_mode (x);
1977 if (mode == GET_MODE (subreg))
1979 x = subreg;
1980 code = GET_CODE (x);
1984 if (REG_P (x) || SUBREG_P (x))
1986 x_hard_regno = get_hard_regno (x);
1987 return (x_hard_regno >= 0
1988 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1990 fmt = GET_RTX_FORMAT (code);
1991 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1993 if (fmt[i] == 'e')
1995 if (uses_hard_regs_p (XEXP (x, i), set))
1996 return true;
1998 else if (fmt[i] == 'E')
2000 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2001 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
2002 return true;
2005 return false;
2008 /* Return true if OP is a spilled pseudo. */
2009 static inline bool
2010 spilled_pseudo_p (rtx op)
2012 return (REG_P (op)
2013 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
2016 /* Return true if X is a general constant. */
2017 static inline bool
2018 general_constant_p (rtx x)
2020 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
2023 static bool
2024 reg_in_class_p (rtx reg, enum reg_class cl)
2026 if (cl == NO_REGS)
2027 return get_reg_class (REGNO (reg)) == NO_REGS;
2028 return in_class_p (reg, cl, NULL);
2031 /* Return true if SET of RCLASS contains no hard regs which can be
2032 used in MODE. */
2033 static bool
2034 prohibited_class_reg_set_mode_p (enum reg_class rclass,
2035 HARD_REG_SET &set,
2036 machine_mode mode)
2038 HARD_REG_SET temp;
2040 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
2041 temp = set & ~lra_no_alloc_regs;
2042 return (hard_reg_set_subset_p
2043 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
2047 /* Used to check validity info about small class input operands. It
2048 should be incremented at start of processing an insn
2049 alternative. */
2050 static unsigned int curr_small_class_check = 0;
2052 /* Update number of used inputs of class OP_CLASS for operand NOP
2053 of alternative NALT. Return true if we have more such class operands
2054 than the number of available regs. */
2055 static bool
2056 update_and_check_small_class_inputs (int nop, int nalt,
2057 enum reg_class op_class)
2059 static unsigned int small_class_check[LIM_REG_CLASSES];
2060 static int small_class_input_nums[LIM_REG_CLASSES];
2062 if (SMALL_REGISTER_CLASS_P (op_class)
2063 /* We are interesting in classes became small because of fixing
2064 some hard regs, e.g. by an user through GCC options. */
2065 && hard_reg_set_intersect_p (reg_class_contents[op_class],
2066 ira_no_alloc_regs)
2067 && (curr_static_id->operand[nop].type != OP_OUT
2068 || TEST_BIT (curr_static_id->operand[nop].early_clobber_alts, nalt)))
2070 if (small_class_check[op_class] == curr_small_class_check)
2071 small_class_input_nums[op_class]++;
2072 else
2074 small_class_check[op_class] = curr_small_class_check;
2075 small_class_input_nums[op_class] = 1;
2077 if (small_class_input_nums[op_class] > ira_class_hard_regs_num[op_class])
2078 return true;
2080 return false;
2083 /* Print operand constraints for alternative ALT_NUMBER of the current
2084 insn. */
2085 static void
2086 print_curr_insn_alt (int alt_number)
2088 for (int i = 0; i < curr_static_id->n_operands; i++)
2090 const char *p = (curr_static_id->operand_alternative
2091 [alt_number * curr_static_id->n_operands + i].constraint);
2092 if (*p == '\0')
2093 continue;
2094 fprintf (lra_dump_file, " (%d) ", i);
2095 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
2096 fputc (*p, lra_dump_file);
2100 /* Major function to choose the current insn alternative and what
2101 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
2102 negative we should consider only this alternative. Return false if
2103 we cannot choose the alternative or find how to reload the
2104 operands. */
2105 static bool
2106 process_alt_operands (int only_alternative)
2108 bool ok_p = false;
2109 int nop, overall, nalt;
2110 int n_alternatives = curr_static_id->n_alternatives;
2111 int n_operands = curr_static_id->n_operands;
2112 /* LOSERS counts the operands that don't fit this alternative and
2113 would require loading. */
2114 int losers;
2115 int addr_losers;
2116 /* REJECT is a count of how undesirable this alternative says it is
2117 if any reloading is required. If the alternative matches exactly
2118 then REJECT is ignored, but otherwise it gets this much counted
2119 against it in addition to the reloading needed. */
2120 int reject;
2121 /* This is defined by '!' or '?' alternative constraint and added to
2122 reject. But in some cases it can be ignored. */
2123 int static_reject;
2124 int op_reject;
2125 /* The number of elements in the following array. */
2126 int early_clobbered_regs_num;
2127 /* Numbers of operands which are early clobber registers. */
2128 int early_clobbered_nops[MAX_RECOG_OPERANDS];
2129 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
2130 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
2131 HARD_REG_SET curr_alt_exclude_start_hard_regs[MAX_RECOG_OPERANDS];
2132 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
2133 bool curr_alt_win[MAX_RECOG_OPERANDS];
2134 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
2135 int curr_alt_matches[MAX_RECOG_OPERANDS];
2136 /* The number of elements in the following array. */
2137 int curr_alt_dont_inherit_ops_num;
2138 /* Numbers of operands whose reload pseudos should not be inherited. */
2139 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
2140 bool curr_reuse_alt_p;
2141 /* True if output stack pointer reload should be generated for the current
2142 alternative. */
2143 bool curr_alt_out_sp_reload_p;
2144 rtx op;
2145 /* The register when the operand is a subreg of register, otherwise the
2146 operand itself. */
2147 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
2148 /* The register if the operand is a register or subreg of register,
2149 otherwise NULL. */
2150 rtx operand_reg[MAX_RECOG_OPERANDS];
2151 int hard_regno[MAX_RECOG_OPERANDS];
2152 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
2153 int reload_nregs, reload_sum;
2154 bool costly_p;
2155 enum reg_class cl;
2156 const HARD_REG_SET *cl_filter;
2158 /* Calculate some data common for all alternatives to speed up the
2159 function. */
2160 for (nop = 0; nop < n_operands; nop++)
2162 rtx reg;
2164 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
2165 /* The real hard regno of the operand after the allocation. */
2166 hard_regno[nop] = get_hard_regno (op);
2168 operand_reg[nop] = reg = op;
2169 biggest_mode[nop] = GET_MODE (op);
2170 if (GET_CODE (op) == SUBREG)
2172 biggest_mode[nop] = wider_subreg_mode (op);
2173 operand_reg[nop] = reg = SUBREG_REG (op);
2175 if (! REG_P (reg))
2176 operand_reg[nop] = NULL_RTX;
2177 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
2178 || ((int) REGNO (reg)
2179 == lra_get_elimination_hard_regno (REGNO (reg))))
2180 no_subreg_reg_operand[nop] = reg;
2181 else
2182 operand_reg[nop] = no_subreg_reg_operand[nop]
2183 /* Just use natural mode for elimination result. It should
2184 be enough for extra constraints hooks. */
2185 = regno_reg_rtx[hard_regno[nop]];
2188 /* The constraints are made of several alternatives. Each operand's
2189 constraint looks like foo,bar,... with commas separating the
2190 alternatives. The first alternatives for all operands go
2191 together, the second alternatives go together, etc.
2193 First loop over alternatives. */
2194 alternative_mask preferred = curr_id->preferred_alternatives;
2195 if (only_alternative >= 0)
2196 preferred &= ALTERNATIVE_BIT (only_alternative);
2198 for (nalt = 0; nalt < n_alternatives; nalt++)
2200 /* Loop over operands for one constraint alternative. */
2201 if (!TEST_BIT (preferred, nalt))
2202 continue;
2204 if (lra_dump_file != NULL)
2206 fprintf (lra_dump_file, " Considering alt=%d of insn %d: ",
2207 nalt, INSN_UID (curr_insn));
2208 print_curr_insn_alt (nalt);
2209 fprintf (lra_dump_file, "\n");
2212 bool matching_early_clobber[MAX_RECOG_OPERANDS];
2213 curr_small_class_check++;
2214 overall = losers = addr_losers = 0;
2215 static_reject = reject = reload_nregs = reload_sum = 0;
2216 for (nop = 0; nop < n_operands; nop++)
2218 int inc = (curr_static_id
2219 ->operand_alternative[nalt * n_operands + nop].reject);
2220 if (lra_dump_file != NULL && inc != 0)
2221 fprintf (lra_dump_file,
2222 " Staticly defined alt reject+=%d\n", inc);
2223 static_reject += inc;
2224 matching_early_clobber[nop] = 0;
2226 reject += static_reject;
2227 early_clobbered_regs_num = 0;
2228 curr_alt_out_sp_reload_p = false;
2229 curr_reuse_alt_p = true;
2231 for (nop = 0; nop < n_operands; nop++)
2233 const char *p;
2234 char *end;
2235 int len, c, m, i, opalt_num, this_alternative_matches;
2236 bool win, did_match, offmemok, early_clobber_p;
2237 /* false => this operand can be reloaded somehow for this
2238 alternative. */
2239 bool badop;
2240 /* true => this operand can be reloaded if the alternative
2241 allows regs. */
2242 bool winreg;
2243 /* True if a constant forced into memory would be OK for
2244 this operand. */
2245 bool constmemok;
2246 enum reg_class this_alternative, this_costly_alternative;
2247 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
2248 HARD_REG_SET this_alternative_exclude_start_hard_regs;
2249 bool this_alternative_match_win, this_alternative_win;
2250 bool this_alternative_offmemok;
2251 bool scratch_p;
2252 machine_mode mode;
2253 enum constraint_num cn;
2255 opalt_num = nalt * n_operands + nop;
2256 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
2258 /* Fast track for no constraints at all. */
2259 curr_alt[nop] = NO_REGS;
2260 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
2261 curr_alt_win[nop] = true;
2262 curr_alt_match_win[nop] = false;
2263 curr_alt_offmemok[nop] = false;
2264 curr_alt_matches[nop] = -1;
2265 continue;
2268 op = no_subreg_reg_operand[nop];
2269 mode = curr_operand_mode[nop];
2271 win = did_match = winreg = offmemok = constmemok = false;
2272 badop = true;
2274 early_clobber_p = false;
2275 p = curr_static_id->operand_alternative[opalt_num].constraint;
2277 this_costly_alternative = this_alternative = NO_REGS;
2278 /* We update set of possible hard regs besides its class
2279 because reg class might be inaccurate. For example,
2280 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
2281 is translated in HI_REGS because classes are merged by
2282 pairs and there is no accurate intermediate class. */
2283 CLEAR_HARD_REG_SET (this_alternative_set);
2284 CLEAR_HARD_REG_SET (this_costly_alternative_set);
2285 CLEAR_HARD_REG_SET (this_alternative_exclude_start_hard_regs);
2286 this_alternative_win = false;
2287 this_alternative_match_win = false;
2288 this_alternative_offmemok = false;
2289 this_alternative_matches = -1;
2291 /* An empty constraint should be excluded by the fast
2292 track. */
2293 lra_assert (*p != 0 && *p != ',');
2295 op_reject = 0;
2296 /* Scan this alternative's specs for this operand; set WIN
2297 if the operand fits any letter in this alternative.
2298 Otherwise, clear BADOP if this operand could fit some
2299 letter after reloads, or set WINREG if this operand could
2300 fit after reloads provided the constraint allows some
2301 registers. */
2302 costly_p = false;
2305 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
2307 case '\0':
2308 len = 0;
2309 break;
2310 case ',':
2311 c = '\0';
2312 break;
2314 case '&':
2315 early_clobber_p = true;
2316 break;
2318 case '$':
2319 op_reject += LRA_MAX_REJECT;
2320 break;
2321 case '^':
2322 op_reject += LRA_LOSER_COST_FACTOR;
2323 break;
2325 case '#':
2326 /* Ignore rest of this alternative. */
2327 c = '\0';
2328 break;
2330 case '0': case '1': case '2': case '3': case '4':
2331 case '5': case '6': case '7': case '8': case '9':
2333 int m_hregno;
2334 bool match_p;
2336 m = strtoul (p, &end, 10);
2337 p = end;
2338 len = 0;
2339 lra_assert (nop > m);
2341 /* Reject matches if we don't know which operand is
2342 bigger. This situation would arguably be a bug in
2343 an .md pattern, but could also occur in a user asm. */
2344 if (!ordered_p (GET_MODE_SIZE (biggest_mode[m]),
2345 GET_MODE_SIZE (biggest_mode[nop])))
2346 break;
2348 /* Don't match wrong asm insn operands for proper
2349 diagnostic later. */
2350 if (INSN_CODE (curr_insn) < 0
2351 && (curr_operand_mode[m] == BLKmode
2352 || curr_operand_mode[nop] == BLKmode)
2353 && curr_operand_mode[m] != curr_operand_mode[nop])
2354 break;
2356 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
2357 /* We are supposed to match a previous operand.
2358 If we do, we win if that one did. If we do
2359 not, count both of the operands as losers.
2360 (This is too conservative, since most of the
2361 time only a single reload insn will be needed
2362 to make the two operands win. As a result,
2363 this alternative may be rejected when it is
2364 actually desirable.) */
2365 match_p = false;
2366 if (operands_match_p (*curr_id->operand_loc[nop],
2367 *curr_id->operand_loc[m], m_hregno))
2369 /* We should reject matching of an early
2370 clobber operand if the matching operand is
2371 not dying in the insn. */
2372 if (!TEST_BIT (curr_static_id->operand[m]
2373 .early_clobber_alts, nalt)
2374 || operand_reg[nop] == NULL_RTX
2375 || (find_regno_note (curr_insn, REG_DEAD,
2376 REGNO (op))
2377 || REGNO (op) == REGNO (operand_reg[m])))
2378 match_p = true;
2380 if (match_p)
2382 /* If we are matching a non-offsettable
2383 address where an offsettable address was
2384 expected, then we must reject this
2385 combination, because we can't reload
2386 it. */
2387 if (curr_alt_offmemok[m]
2388 && MEM_P (*curr_id->operand_loc[m])
2389 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
2390 continue;
2392 else
2394 /* If the operands do not match and one
2395 operand is INOUT, we can not match them.
2396 Try other possibilities, e.g. other
2397 alternatives or commutative operand
2398 exchange. */
2399 if (curr_static_id->operand[nop].type == OP_INOUT
2400 || curr_static_id->operand[m].type == OP_INOUT)
2401 break;
2402 /* Operands don't match. If the operands are
2403 different user defined explicit hard
2404 registers, then we cannot make them match
2405 when one is early clobber operand. */
2406 if ((REG_P (*curr_id->operand_loc[nop])
2407 || SUBREG_P (*curr_id->operand_loc[nop]))
2408 && (REG_P (*curr_id->operand_loc[m])
2409 || SUBREG_P (*curr_id->operand_loc[m])))
2411 rtx nop_reg = *curr_id->operand_loc[nop];
2412 if (SUBREG_P (nop_reg))
2413 nop_reg = SUBREG_REG (nop_reg);
2414 rtx m_reg = *curr_id->operand_loc[m];
2415 if (SUBREG_P (m_reg))
2416 m_reg = SUBREG_REG (m_reg);
2418 if (REG_P (nop_reg)
2419 && HARD_REGISTER_P (nop_reg)
2420 && REG_USERVAR_P (nop_reg)
2421 && REG_P (m_reg)
2422 && HARD_REGISTER_P (m_reg)
2423 && REG_USERVAR_P (m_reg))
2425 int i;
2427 for (i = 0; i < early_clobbered_regs_num; i++)
2428 if (m == early_clobbered_nops[i])
2429 break;
2430 if (i < early_clobbered_regs_num
2431 || early_clobber_p)
2432 break;
2435 /* Both operands must allow a reload register,
2436 otherwise we cannot make them match. */
2437 if (curr_alt[m] == NO_REGS)
2438 break;
2439 /* Retroactively mark the operand we had to
2440 match as a loser, if it wasn't already and
2441 it wasn't matched to a register constraint
2442 (e.g it might be matched by memory). */
2443 if (curr_alt_win[m]
2444 && (operand_reg[m] == NULL_RTX
2445 || hard_regno[m] < 0))
2447 losers++;
2448 reload_nregs
2449 += (ira_reg_class_max_nregs[curr_alt[m]]
2450 [GET_MODE (*curr_id->operand_loc[m])]);
2453 /* Prefer matching earlyclobber alternative as
2454 it results in less hard regs required for
2455 the insn than a non-matching earlyclobber
2456 alternative. */
2457 if (TEST_BIT (curr_static_id->operand[m]
2458 .early_clobber_alts, nalt))
2460 if (lra_dump_file != NULL)
2461 fprintf
2462 (lra_dump_file,
2463 " %d Matching earlyclobber alt:"
2464 " reject--\n",
2465 nop);
2466 if (!matching_early_clobber[m])
2468 reject--;
2469 matching_early_clobber[m] = 1;
2472 /* Otherwise we prefer no matching
2473 alternatives because it gives more freedom
2474 in RA. */
2475 else if (operand_reg[nop] == NULL_RTX
2476 || (find_regno_note (curr_insn, REG_DEAD,
2477 REGNO (operand_reg[nop]))
2478 == NULL_RTX))
2480 if (lra_dump_file != NULL)
2481 fprintf
2482 (lra_dump_file,
2483 " %d Matching alt: reject+=2\n",
2484 nop);
2485 reject += 2;
2488 /* If we have to reload this operand and some
2489 previous operand also had to match the same
2490 thing as this operand, we don't know how to do
2491 that. */
2492 if (!match_p || !curr_alt_win[m])
2494 for (i = 0; i < nop; i++)
2495 if (curr_alt_matches[i] == m)
2496 break;
2497 if (i < nop)
2498 break;
2500 else
2501 did_match = true;
2503 this_alternative_matches = m;
2504 /* This can be fixed with reloads if the operand
2505 we are supposed to match can be fixed with
2506 reloads. */
2507 badop = false;
2508 this_alternative = curr_alt[m];
2509 this_alternative_set = curr_alt_set[m];
2510 this_alternative_exclude_start_hard_regs
2511 = curr_alt_exclude_start_hard_regs[m];
2512 winreg = this_alternative != NO_REGS;
2513 break;
2516 case 'g':
2517 if (MEM_P (op)
2518 || general_constant_p (op)
2519 || spilled_pseudo_p (op))
2520 win = true;
2521 cl = GENERAL_REGS;
2522 cl_filter = nullptr;
2523 goto reg;
2525 default:
2526 cn = lookup_constraint (p);
2527 switch (get_constraint_type (cn))
2529 case CT_REGISTER:
2530 cl = reg_class_for_constraint (cn);
2531 if (cl != NO_REGS)
2533 cl_filter = get_register_filter (cn);
2534 goto reg;
2536 break;
2538 case CT_CONST_INT:
2539 if (CONST_INT_P (op)
2540 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2541 win = true;
2542 break;
2544 case CT_MEMORY:
2545 case CT_RELAXED_MEMORY:
2546 if (MEM_P (op)
2547 && satisfies_memory_constraint_p (op, cn))
2548 win = true;
2549 else if (spilled_pseudo_p (op))
2550 win = true;
2552 /* If we didn't already win, we can reload constants
2553 via force_const_mem or put the pseudo value into
2554 memory, or make other memory by reloading the
2555 address like for 'o'. */
2556 if (CONST_POOL_OK_P (mode, op)
2557 || MEM_P (op) || REG_P (op)
2558 /* We can restore the equiv insn by a
2559 reload. */
2560 || equiv_substition_p[nop])
2561 badop = false;
2562 constmemok = true;
2563 offmemok = true;
2564 break;
2566 case CT_ADDRESS:
2567 /* An asm operand with an address constraint
2568 that doesn't satisfy address_operand has
2569 is_address cleared, so that we don't try to
2570 make a non-address fit. */
2571 if (!curr_static_id->operand[nop].is_address)
2572 break;
2573 /* If we didn't already win, we can reload the address
2574 into a base register. */
2575 if (satisfies_address_constraint_p (op, cn))
2576 win = true;
2577 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2578 ADDRESS, SCRATCH);
2579 cl_filter = nullptr;
2580 badop = false;
2581 goto reg;
2583 case CT_FIXED_FORM:
2584 if (constraint_satisfied_p (op, cn))
2585 win = true;
2586 break;
2588 case CT_SPECIAL_MEMORY:
2589 if (satisfies_memory_constraint_p (op, cn))
2590 win = true;
2591 else if (spilled_pseudo_p (op))
2593 curr_reuse_alt_p = false;
2594 win = true;
2596 break;
2598 break;
2600 reg:
2601 if (mode == BLKmode)
2602 break;
2603 this_alternative = reg_class_subunion[this_alternative][cl];
2604 if (hard_reg_set_subset_p (this_alternative_set,
2605 reg_class_contents[cl]))
2606 this_alternative_exclude_start_hard_regs
2607 = ira_exclude_class_mode_regs[cl][mode];
2608 else if (!hard_reg_set_subset_p (reg_class_contents[cl],
2609 this_alternative_set))
2610 this_alternative_exclude_start_hard_regs
2611 |= ira_exclude_class_mode_regs[cl][mode];
2612 this_alternative_set |= reg_class_contents[cl];
2613 if (cl_filter)
2614 this_alternative_exclude_start_hard_regs |= ~*cl_filter;
2615 if (costly_p)
2617 this_costly_alternative
2618 = reg_class_subunion[this_costly_alternative][cl];
2619 this_costly_alternative_set |= reg_class_contents[cl];
2621 winreg = true;
2622 if (REG_P (op))
2624 tree decl;
2625 if (hard_regno[nop] >= 0
2626 && in_hard_reg_set_p (this_alternative_set,
2627 mode, hard_regno[nop])
2628 && (!cl_filter
2629 || TEST_HARD_REG_BIT (*cl_filter,
2630 hard_regno[nop]))
2631 && ((REG_ATTRS (op) && (decl = REG_EXPR (op)) != NULL
2632 && VAR_P (decl) && DECL_HARD_REGISTER (decl))
2633 || !(TEST_HARD_REG_BIT
2634 (this_alternative_exclude_start_hard_regs,
2635 hard_regno[nop]))))
2636 win = true;
2637 else if (hard_regno[nop] < 0
2638 && in_class_p (op, this_alternative, NULL, true))
2639 win = true;
2641 break;
2643 if (c != ' ' && c != '\t')
2644 costly_p = c == '*';
2646 while ((p += len), c);
2648 scratch_p = (operand_reg[nop] != NULL_RTX
2649 && ira_former_scratch_p (REGNO (operand_reg[nop])));
2650 /* Record which operands fit this alternative. */
2651 if (win)
2653 this_alternative_win = true;
2654 if (operand_reg[nop] != NULL_RTX)
2656 if (hard_regno[nop] >= 0)
2658 if (in_hard_reg_set_p (this_costly_alternative_set,
2659 mode, hard_regno[nop]))
2661 if (lra_dump_file != NULL)
2662 fprintf (lra_dump_file,
2663 " %d Costly set: reject++\n",
2664 nop);
2665 reject++;
2668 else
2670 /* Prefer won reg to spilled pseudo under other
2671 equal conditions for possibe inheritance. */
2672 if (! scratch_p)
2674 if (lra_dump_file != NULL)
2675 fprintf
2676 (lra_dump_file,
2677 " %d Non pseudo reload: reject++\n",
2678 nop);
2679 reject++;
2681 if (in_class_p (operand_reg[nop],
2682 this_costly_alternative, NULL, true))
2684 if (lra_dump_file != NULL)
2685 fprintf
2686 (lra_dump_file,
2687 " %d Non pseudo costly reload:"
2688 " reject++\n",
2689 nop);
2690 reject++;
2693 /* We simulate the behavior of old reload here.
2694 Although scratches need hard registers and it
2695 might result in spilling other pseudos, no reload
2696 insns are generated for the scratches. So it
2697 might cost something but probably less than old
2698 reload pass believes. */
2699 if (scratch_p)
2701 if (lra_dump_file != NULL)
2702 fprintf (lra_dump_file,
2703 " %d Scratch win: reject+=2\n",
2704 nop);
2705 reject += 2;
2709 else if (did_match)
2710 this_alternative_match_win = true;
2711 else
2713 int const_to_mem = 0;
2714 bool no_regs_p;
2716 reject += op_reject;
2717 /* Mark output reload of the stack pointer. */
2718 if (op == stack_pointer_rtx
2719 && curr_static_id->operand[nop].type != OP_IN)
2720 curr_alt_out_sp_reload_p = true;
2722 /* If this alternative asks for a specific reg class, see if there
2723 is at least one allocatable register in that class. */
2724 no_regs_p
2725 = (this_alternative == NO_REGS
2726 || (hard_reg_set_subset_p
2727 (reg_class_contents[this_alternative],
2728 lra_no_alloc_regs)));
2730 /* For asms, verify that the class for this alternative is possible
2731 for the mode that is specified. */
2732 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2734 int i;
2735 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2736 if (targetm.hard_regno_mode_ok (i, mode)
2737 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2738 mode, i))
2739 break;
2740 if (i == FIRST_PSEUDO_REGISTER)
2741 winreg = false;
2744 /* If this operand accepts a register, and if the
2745 register class has at least one allocatable register,
2746 then this operand can be reloaded. */
2747 if (winreg && !no_regs_p)
2748 badop = false;
2750 if (badop)
2752 if (lra_dump_file != NULL)
2753 fprintf (lra_dump_file,
2754 " Bad operand -- refuse\n");
2755 goto fail;
2758 if (this_alternative != NO_REGS)
2760 HARD_REG_SET available_regs
2761 = (reg_class_contents[this_alternative]
2762 & ~((ira_prohibited_class_mode_regs
2763 [this_alternative][mode])
2764 | lra_no_alloc_regs));
2765 if (hard_reg_set_empty_p (available_regs))
2767 /* There are no hard regs holding a value of given
2768 mode. */
2769 if (offmemok)
2771 this_alternative = NO_REGS;
2772 if (lra_dump_file != NULL)
2773 fprintf (lra_dump_file,
2774 " %d Using memory because of"
2775 " a bad mode: reject+=2\n",
2776 nop);
2777 reject += 2;
2779 else
2781 if (lra_dump_file != NULL)
2782 fprintf (lra_dump_file,
2783 " Wrong mode -- refuse\n");
2784 goto fail;
2789 /* If not assigned pseudo has a class which a subset of
2790 required reg class, it is a less costly alternative
2791 as the pseudo still can get a hard reg of necessary
2792 class. */
2793 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2794 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2795 && ira_class_subset_p[this_alternative][cl])
2797 if (lra_dump_file != NULL)
2798 fprintf
2799 (lra_dump_file,
2800 " %d Super set class reg: reject-=3\n", nop);
2801 reject -= 3;
2804 this_alternative_offmemok = offmemok;
2805 if (this_costly_alternative != NO_REGS)
2807 if (lra_dump_file != NULL)
2808 fprintf (lra_dump_file,
2809 " %d Costly loser: reject++\n", nop);
2810 reject++;
2812 /* If the operand is dying, has a matching constraint,
2813 and satisfies constraints of the matched operand
2814 which failed to satisfy the own constraints, most probably
2815 the reload for this operand will be gone. */
2816 if (this_alternative_matches >= 0
2817 && !curr_alt_win[this_alternative_matches]
2818 && REG_P (op)
2819 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2820 && (hard_regno[nop] >= 0
2821 ? in_hard_reg_set_p (this_alternative_set,
2822 mode, hard_regno[nop])
2823 : in_class_p (op, this_alternative, NULL)))
2825 if (lra_dump_file != NULL)
2826 fprintf
2827 (lra_dump_file,
2828 " %d Dying matched operand reload: reject++\n",
2829 nop);
2830 reject++;
2832 else
2834 /* Strict_low_part requires to reload the register
2835 not the sub-register. In this case we should
2836 check that a final reload hard reg can hold the
2837 value mode. */
2838 if (curr_static_id->operand[nop].strict_low
2839 && REG_P (op)
2840 && hard_regno[nop] < 0
2841 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2842 && ira_class_hard_regs_num[this_alternative] > 0
2843 && (!targetm.hard_regno_mode_ok
2844 (ira_class_hard_regs[this_alternative][0],
2845 GET_MODE (*curr_id->operand_loc[nop]))))
2847 if (lra_dump_file != NULL)
2848 fprintf
2849 (lra_dump_file,
2850 " Strict low subreg reload -- refuse\n");
2851 goto fail;
2853 losers++;
2855 if (operand_reg[nop] != NULL_RTX
2856 /* Output operands and matched input operands are
2857 not inherited. The following conditions do not
2858 exactly describe the previous statement but they
2859 are pretty close. */
2860 && curr_static_id->operand[nop].type != OP_OUT
2861 && (this_alternative_matches < 0
2862 || curr_static_id->operand[nop].type != OP_IN))
2864 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2865 (operand_reg[nop])]
2866 .last_reload);
2868 /* The value of reload_sum has sense only if we
2869 process insns in their order. It happens only on
2870 the first constraints sub-pass when we do most of
2871 reload work. */
2872 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2873 reload_sum += last_reload - bb_reload_num;
2875 /* If this is a constant that is reloaded into the
2876 desired class by copying it to memory first, count
2877 that as another reload. This is consistent with
2878 other code and is required to avoid choosing another
2879 alternative when the constant is moved into memory.
2880 Note that the test here is precisely the same as in
2881 the code below that calls force_const_mem. */
2882 if (CONST_POOL_OK_P (mode, op)
2883 && ((targetm.preferred_reload_class
2884 (op, this_alternative) == NO_REGS)
2885 || no_input_reloads_p))
2887 const_to_mem = 1;
2888 if (! no_regs_p)
2889 losers++;
2892 /* Alternative loses if it requires a type of reload not
2893 permitted for this insn. We can always reload
2894 objects with a REG_UNUSED note. */
2895 if ((curr_static_id->operand[nop].type != OP_IN
2896 && no_output_reloads_p
2897 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2898 || (curr_static_id->operand[nop].type != OP_OUT
2899 && no_input_reloads_p && ! const_to_mem)
2900 || (this_alternative_matches >= 0
2901 && (no_input_reloads_p
2902 || (no_output_reloads_p
2903 && (curr_static_id->operand
2904 [this_alternative_matches].type != OP_IN)
2905 && ! find_reg_note (curr_insn, REG_UNUSED,
2906 no_subreg_reg_operand
2907 [this_alternative_matches])))))
2909 if (lra_dump_file != NULL)
2910 fprintf
2911 (lra_dump_file,
2912 " No input/output reload -- refuse\n");
2913 goto fail;
2916 /* Alternative loses if it required class pseudo cannot
2917 hold value of required mode. Such insns can be
2918 described by insn definitions with mode iterators. */
2919 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2920 && ! hard_reg_set_empty_p (this_alternative_set)
2921 /* It is common practice for constraints to use a
2922 class which does not have actually enough regs to
2923 hold the value (e.g. x86 AREG for mode requiring
2924 more one general reg). Therefore we have 2
2925 conditions to check that the reload pseudo cannot
2926 hold the mode value. */
2927 && (!targetm.hard_regno_mode_ok
2928 (ira_class_hard_regs[this_alternative][0],
2929 GET_MODE (*curr_id->operand_loc[nop])))
2930 /* The above condition is not enough as the first
2931 reg in ira_class_hard_regs can be not aligned for
2932 multi-words mode values. */
2933 && (prohibited_class_reg_set_mode_p
2934 (this_alternative, this_alternative_set,
2935 GET_MODE (*curr_id->operand_loc[nop]))))
2937 if (lra_dump_file != NULL)
2938 fprintf (lra_dump_file,
2939 " reload pseudo for op %d "
2940 "cannot hold the mode value -- refuse\n",
2941 nop);
2942 goto fail;
2945 /* Check strong discouragement of reload of non-constant
2946 into class THIS_ALTERNATIVE. */
2947 if (! CONSTANT_P (op) && ! no_regs_p
2948 && (targetm.preferred_reload_class
2949 (op, this_alternative) == NO_REGS
2950 || (curr_static_id->operand[nop].type == OP_OUT
2951 && (targetm.preferred_output_reload_class
2952 (op, this_alternative) == NO_REGS))))
2954 if (offmemok && REG_P (op))
2956 if (lra_dump_file != NULL)
2957 fprintf
2958 (lra_dump_file,
2959 " %d Spill pseudo into memory: reject+=3\n",
2960 nop);
2961 reject += 3;
2963 else
2965 if (lra_dump_file != NULL)
2966 fprintf
2967 (lra_dump_file,
2968 " %d Non-prefered reload: reject+=%d\n",
2969 nop, LRA_MAX_REJECT);
2970 reject += LRA_MAX_REJECT;
2974 if (! (MEM_P (op) && offmemok)
2975 && ! (const_to_mem && constmemok))
2977 /* We prefer to reload pseudos over reloading other
2978 things, since such reloads may be able to be
2979 eliminated later. So bump REJECT in other cases.
2980 Don't do this in the case where we are forcing a
2981 constant into memory and it will then win since
2982 we don't want to have a different alternative
2983 match then. */
2984 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2986 if (lra_dump_file != NULL)
2987 fprintf
2988 (lra_dump_file,
2989 " %d Non-pseudo reload: reject+=2\n",
2990 nop);
2991 reject += 2;
2994 if (! no_regs_p)
2995 reload_nregs
2996 += ira_reg_class_max_nregs[this_alternative][mode];
2998 if (SMALL_REGISTER_CLASS_P (this_alternative))
3000 if (lra_dump_file != NULL)
3001 fprintf
3002 (lra_dump_file,
3003 " %d Small class reload: reject+=%d\n",
3004 nop, LRA_LOSER_COST_FACTOR / 2);
3005 reject += LRA_LOSER_COST_FACTOR / 2;
3009 /* We are trying to spill pseudo into memory. It is
3010 usually more costly than moving to a hard register
3011 although it might takes the same number of
3012 reloads.
3014 Non-pseudo spill may happen also. Suppose a target allows both
3015 register and memory in the operand constraint alternatives,
3016 then it's typical that an eliminable register has a substition
3017 of "base + offset" which can either be reloaded by a simple
3018 "new_reg <= base + offset" which will match the register
3019 constraint, or a similar reg addition followed by further spill
3020 to and reload from memory which will match the memory
3021 constraint, but this memory spill will be much more costly
3022 usually.
3024 Code below increases the reject for both pseudo and non-pseudo
3025 spill. */
3026 if (no_regs_p
3027 && !(MEM_P (op) && offmemok)
3028 && !(REG_P (op) && hard_regno[nop] < 0))
3030 if (lra_dump_file != NULL)
3031 fprintf
3032 (lra_dump_file,
3033 " %d Spill %spseudo into memory: reject+=3\n",
3034 nop, REG_P (op) ? "" : "Non-");
3035 reject += 3;
3036 if (VECTOR_MODE_P (mode))
3038 /* Spilling vectors into memory is usually more
3039 costly as they contain big values. */
3040 if (lra_dump_file != NULL)
3041 fprintf
3042 (lra_dump_file,
3043 " %d Spill vector pseudo: reject+=2\n",
3044 nop);
3045 reject += 2;
3049 /* When we use an operand requiring memory in given
3050 alternative, the insn should write *and* read the
3051 value to/from memory it is costly in comparison with
3052 an insn alternative which does not use memory
3053 (e.g. register or immediate operand). We exclude
3054 memory operand for such case as we can satisfy the
3055 memory constraints by reloading address. */
3056 if (no_regs_p && offmemok && !MEM_P (op))
3058 if (lra_dump_file != NULL)
3059 fprintf
3060 (lra_dump_file,
3061 " Using memory insn operand %d: reject+=3\n",
3062 nop);
3063 reject += 3;
3066 /* If reload requires moving value through secondary
3067 memory, it will need one more insn at least. */
3068 if (this_alternative != NO_REGS
3069 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
3070 && ((curr_static_id->operand[nop].type != OP_OUT
3071 && targetm.secondary_memory_needed (GET_MODE (op), cl,
3072 this_alternative))
3073 || (curr_static_id->operand[nop].type != OP_IN
3074 && (targetm.secondary_memory_needed
3075 (GET_MODE (op), this_alternative, cl)))))
3076 losers++;
3078 if (MEM_P (op) && offmemok)
3079 addr_losers++;
3080 else
3082 /* Input reloads can be inherited more often than
3083 output reloads can be removed, so penalize output
3084 reloads. */
3085 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
3087 if (lra_dump_file != NULL)
3088 fprintf
3089 (lra_dump_file,
3090 " %d Non input pseudo reload: reject++\n",
3091 nop);
3092 reject++;
3095 if (curr_static_id->operand[nop].type == OP_INOUT)
3097 if (lra_dump_file != NULL)
3098 fprintf
3099 (lra_dump_file,
3100 " %d Input/Output reload: reject+=%d\n",
3101 nop, LRA_LOSER_COST_FACTOR);
3102 reject += LRA_LOSER_COST_FACTOR;
3107 if (early_clobber_p && ! scratch_p)
3109 if (lra_dump_file != NULL)
3110 fprintf (lra_dump_file,
3111 " %d Early clobber: reject++\n", nop);
3112 reject++;
3114 /* ??? We check early clobbers after processing all operands
3115 (see loop below) and there we update the costs more.
3116 Should we update the cost (may be approximately) here
3117 because of early clobber register reloads or it is a rare
3118 or non-important thing to be worth to do it. */
3119 overall = (losers * LRA_LOSER_COST_FACTOR + reject
3120 - (addr_losers == losers ? static_reject : 0));
3121 if ((best_losers == 0 || losers != 0) && best_overall < overall)
3123 if (lra_dump_file != NULL)
3124 fprintf (lra_dump_file,
3125 " overall=%d,losers=%d -- refuse\n",
3126 overall, losers);
3127 goto fail;
3130 if (update_and_check_small_class_inputs (nop, nalt,
3131 this_alternative))
3133 if (lra_dump_file != NULL)
3134 fprintf (lra_dump_file,
3135 " not enough small class regs -- refuse\n");
3136 goto fail;
3138 curr_alt[nop] = this_alternative;
3139 curr_alt_set[nop] = this_alternative_set;
3140 curr_alt_exclude_start_hard_regs[nop]
3141 = this_alternative_exclude_start_hard_regs;
3142 curr_alt_win[nop] = this_alternative_win;
3143 curr_alt_match_win[nop] = this_alternative_match_win;
3144 curr_alt_offmemok[nop] = this_alternative_offmemok;
3145 curr_alt_matches[nop] = this_alternative_matches;
3147 if (this_alternative_matches >= 0
3148 && !did_match && !this_alternative_win)
3149 curr_alt_win[this_alternative_matches] = false;
3151 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
3152 early_clobbered_nops[early_clobbered_regs_num++] = nop;
3155 if (curr_insn_set != NULL_RTX && n_operands == 2
3156 /* Prevent processing non-move insns. */
3157 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
3158 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
3159 && ((! curr_alt_win[0] && ! curr_alt_win[1]
3160 && REG_P (no_subreg_reg_operand[0])
3161 && REG_P (no_subreg_reg_operand[1])
3162 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
3163 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
3164 || (! curr_alt_win[0] && curr_alt_win[1]
3165 && REG_P (no_subreg_reg_operand[1])
3166 /* Check that we reload memory not the memory
3167 address. */
3168 && ! (curr_alt_offmemok[0]
3169 && MEM_P (no_subreg_reg_operand[0]))
3170 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
3171 || (curr_alt_win[0] && ! curr_alt_win[1]
3172 && REG_P (no_subreg_reg_operand[0])
3173 /* Check that we reload memory not the memory
3174 address. */
3175 && ! (curr_alt_offmemok[1]
3176 && MEM_P (no_subreg_reg_operand[1]))
3177 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
3178 && (! CONST_POOL_OK_P (curr_operand_mode[1],
3179 no_subreg_reg_operand[1])
3180 || (targetm.preferred_reload_class
3181 (no_subreg_reg_operand[1],
3182 (enum reg_class) curr_alt[1]) != NO_REGS))
3183 /* If it is a result of recent elimination in move
3184 insn we can transform it into an add still by
3185 using this alternative. */
3186 && GET_CODE (no_subreg_reg_operand[1]) != PLUS
3187 /* Likewise if the source has been replaced with an
3188 equivalent value. This only happens once -- the reload
3189 will use the equivalent value instead of the register it
3190 replaces -- so there should be no danger of cycling. */
3191 && !equiv_substition_p[1])))
3193 /* We have a move insn and a new reload insn will be similar
3194 to the current insn. We should avoid such situation as
3195 it results in LRA cycling. */
3196 if (lra_dump_file != NULL)
3197 fprintf (lra_dump_file,
3198 " Cycle danger: overall += LRA_MAX_REJECT\n");
3199 overall += LRA_MAX_REJECT;
3201 ok_p = true;
3202 curr_alt_dont_inherit_ops_num = 0;
3203 for (nop = 0; nop < early_clobbered_regs_num; nop++)
3205 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
3206 HARD_REG_SET temp_set;
3208 i = early_clobbered_nops[nop];
3209 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
3210 || hard_regno[i] < 0)
3211 continue;
3212 lra_assert (operand_reg[i] != NULL_RTX);
3213 clobbered_hard_regno = hard_regno[i];
3214 CLEAR_HARD_REG_SET (temp_set);
3215 add_to_hard_reg_set (&temp_set, GET_MODE (*curr_id->operand_loc[i]),
3216 clobbered_hard_regno);
3217 first_conflict_j = last_conflict_j = -1;
3218 for (j = 0; j < n_operands; j++)
3219 if (j == i
3220 /* We don't want process insides of match_operator and
3221 match_parallel because otherwise we would process
3222 their operands once again generating a wrong
3223 code. */
3224 || curr_static_id->operand[j].is_operator)
3225 continue;
3226 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
3227 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
3228 continue;
3229 /* If we don't reload j-th operand, check conflicts. */
3230 else if ((curr_alt_win[j] || curr_alt_match_win[j])
3231 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
3233 if (first_conflict_j < 0)
3234 first_conflict_j = j;
3235 last_conflict_j = j;
3236 /* Both the earlyclobber operand and conflicting operand
3237 cannot both be user defined hard registers. */
3238 if (HARD_REGISTER_P (operand_reg[i])
3239 && REG_USERVAR_P (operand_reg[i])
3240 && operand_reg[j] != NULL_RTX
3241 && HARD_REGISTER_P (operand_reg[j])
3242 && REG_USERVAR_P (operand_reg[j]))
3244 /* For asm, let curr_insn_transform diagnose it. */
3245 if (INSN_CODE (curr_insn) < 0)
3246 return false;
3247 fatal_insn ("unable to generate reloads for "
3248 "impossible constraints:", curr_insn);
3251 if (last_conflict_j < 0)
3252 continue;
3254 /* If an earlyclobber operand conflicts with another non-matching
3255 operand (ie, they have been assigned the same hard register),
3256 then it is better to reload the other operand, as there may
3257 exist yet another operand with a matching constraint associated
3258 with the earlyclobber operand. However, if one of the operands
3259 is an explicit use of a hard register, then we must reload the
3260 other non-hard register operand. */
3261 if (HARD_REGISTER_P (operand_reg[i])
3262 || (first_conflict_j == last_conflict_j
3263 && operand_reg[last_conflict_j] != NULL_RTX
3264 && !curr_alt_match_win[last_conflict_j]
3265 && !HARD_REGISTER_P (operand_reg[last_conflict_j])))
3267 curr_alt_win[last_conflict_j] = false;
3268 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
3269 = last_conflict_j;
3270 losers++;
3271 if (lra_dump_file != NULL)
3272 fprintf
3273 (lra_dump_file,
3274 " %d Conflict early clobber reload: reject--\n",
3277 else
3279 /* We need to reload early clobbered register and the
3280 matched registers. */
3281 for (j = 0; j < n_operands; j++)
3282 if (curr_alt_matches[j] == i)
3284 curr_alt_match_win[j] = false;
3285 losers++;
3286 overall += LRA_LOSER_COST_FACTOR;
3288 if (! curr_alt_match_win[i])
3289 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
3290 else
3292 /* Remember pseudos used for match reloads are never
3293 inherited. */
3294 lra_assert (curr_alt_matches[i] >= 0);
3295 curr_alt_win[curr_alt_matches[i]] = false;
3297 curr_alt_win[i] = curr_alt_match_win[i] = false;
3298 losers++;
3299 if (lra_dump_file != NULL)
3300 fprintf
3301 (lra_dump_file,
3302 " %d Matched conflict early clobber reloads: "
3303 "reject--\n",
3306 /* Early clobber was already reflected in REJECT. */
3307 if (!matching_early_clobber[i])
3309 lra_assert (reject > 0);
3310 reject--;
3311 matching_early_clobber[i] = 1;
3313 overall += LRA_LOSER_COST_FACTOR - 1;
3315 if (lra_dump_file != NULL)
3316 fprintf (lra_dump_file, " overall=%d,losers=%d,rld_nregs=%d\n",
3317 overall, losers, reload_nregs);
3319 /* If this alternative can be made to work by reloading, and it
3320 needs less reloading than the others checked so far, record
3321 it as the chosen goal for reloading. */
3322 if ((best_losers != 0 && losers == 0)
3323 || (((best_losers == 0 && losers == 0)
3324 || (best_losers != 0 && losers != 0))
3325 && (best_overall > overall
3326 || (best_overall == overall
3327 /* If the cost of the reloads is the same,
3328 prefer alternative which requires minimal
3329 number of reload regs. */
3330 && (reload_nregs < best_reload_nregs
3331 || (reload_nregs == best_reload_nregs
3332 && (best_reload_sum < reload_sum
3333 || (best_reload_sum == reload_sum
3334 && nalt < goal_alt_number))))))))
3336 for (nop = 0; nop < n_operands; nop++)
3338 goal_alt_win[nop] = curr_alt_win[nop];
3339 goal_alt_match_win[nop] = curr_alt_match_win[nop];
3340 goal_alt_matches[nop] = curr_alt_matches[nop];
3341 goal_alt[nop] = curr_alt[nop];
3342 goal_alt_exclude_start_hard_regs[nop]
3343 = curr_alt_exclude_start_hard_regs[nop];
3344 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
3346 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
3347 goal_reuse_alt_p = curr_reuse_alt_p;
3348 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
3349 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
3350 goal_alt_swapped = curr_swapped;
3351 goal_alt_out_sp_reload_p = curr_alt_out_sp_reload_p;
3352 best_overall = overall;
3353 best_losers = losers;
3354 best_reload_nregs = reload_nregs;
3355 best_reload_sum = reload_sum;
3356 goal_alt_number = nalt;
3358 if (losers == 0)
3359 /* Everything is satisfied. Do not process alternatives
3360 anymore. */
3361 break;
3362 fail:
3365 return ok_p;
3368 /* Make reload base reg from address AD. */
3369 static rtx
3370 base_to_reg (struct address_info *ad)
3372 enum reg_class cl;
3373 int code = -1;
3374 rtx new_inner = NULL_RTX;
3375 rtx new_reg = NULL_RTX;
3376 rtx_insn *insn;
3377 rtx_insn *last_insn = get_last_insn();
3379 lra_assert (ad->disp == ad->disp_term);
3380 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
3381 get_index_code (ad));
3382 new_reg = lra_create_new_reg (GET_MODE (*ad->base), NULL_RTX, cl, NULL,
3383 "base");
3384 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
3385 ad->disp_term == NULL
3386 ? const0_rtx
3387 : *ad->disp_term);
3388 if (!valid_address_p (ad->mode, new_inner, ad->as))
3389 return NULL_RTX;
3390 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base));
3391 code = recog_memoized (insn);
3392 if (code < 0)
3394 delete_insns_since (last_insn);
3395 return NULL_RTX;
3398 return new_inner;
3401 /* Make reload base reg + DISP from address AD. Return the new pseudo. */
3402 static rtx
3403 base_plus_disp_to_reg (struct address_info *ad, rtx disp)
3405 enum reg_class cl;
3406 rtx new_reg;
3408 lra_assert (ad->base == ad->base_term);
3409 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
3410 get_index_code (ad));
3411 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX, cl, NULL,
3412 "base + disp");
3413 lra_emit_add (new_reg, *ad->base_term, disp);
3414 return new_reg;
3417 /* Make reload of index part of address AD. Return the new
3418 pseudo. */
3419 static rtx
3420 index_part_to_reg (struct address_info *ad, enum reg_class index_class)
3422 rtx new_reg;
3424 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
3425 index_class, NULL, "index term");
3426 expand_mult (GET_MODE (*ad->index), *ad->index_term,
3427 GEN_INT (get_index_scale (ad)), new_reg, 1);
3428 return new_reg;
3431 /* Return true if we can add a displacement to address AD, even if that
3432 makes the address invalid. The fix-up code requires any new address
3433 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
3434 static bool
3435 can_add_disp_p (struct address_info *ad)
3437 return (!ad->autoinc_p
3438 && ad->segment == NULL
3439 && ad->base == ad->base_term
3440 && ad->disp == ad->disp_term);
3443 /* Make equiv substitution in address AD. Return true if a substitution
3444 was made. */
3445 static bool
3446 equiv_address_substitution (struct address_info *ad)
3448 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
3449 poly_int64 disp;
3450 HOST_WIDE_INT scale;
3451 bool change_p;
3453 base_term = strip_subreg (ad->base_term);
3454 if (base_term == NULL)
3455 base_reg = new_base_reg = NULL_RTX;
3456 else
3458 base_reg = *base_term;
3459 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
3461 index_term = strip_subreg (ad->index_term);
3462 if (index_term == NULL)
3463 index_reg = new_index_reg = NULL_RTX;
3464 else
3466 index_reg = *index_term;
3467 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
3469 if (base_reg == new_base_reg && index_reg == new_index_reg)
3470 return false;
3471 disp = 0;
3472 change_p = false;
3473 if (lra_dump_file != NULL)
3475 fprintf (lra_dump_file, "Changing address in insn %d ",
3476 INSN_UID (curr_insn));
3477 dump_value_slim (lra_dump_file, *ad->outer, 1);
3479 if (base_reg != new_base_reg)
3481 poly_int64 offset;
3482 if (REG_P (new_base_reg))
3484 *base_term = new_base_reg;
3485 change_p = true;
3487 else if (GET_CODE (new_base_reg) == PLUS
3488 && REG_P (XEXP (new_base_reg, 0))
3489 && poly_int_rtx_p (XEXP (new_base_reg, 1), &offset)
3490 && can_add_disp_p (ad))
3492 disp += offset;
3493 *base_term = XEXP (new_base_reg, 0);
3494 change_p = true;
3496 if (ad->base_term2 != NULL)
3497 *ad->base_term2 = *ad->base_term;
3499 if (index_reg != new_index_reg)
3501 poly_int64 offset;
3502 if (REG_P (new_index_reg))
3504 *index_term = new_index_reg;
3505 change_p = true;
3507 else if (GET_CODE (new_index_reg) == PLUS
3508 && REG_P (XEXP (new_index_reg, 0))
3509 && poly_int_rtx_p (XEXP (new_index_reg, 1), &offset)
3510 && can_add_disp_p (ad)
3511 && (scale = get_index_scale (ad)))
3513 disp += offset * scale;
3514 *index_term = XEXP (new_index_reg, 0);
3515 change_p = true;
3518 if (maybe_ne (disp, 0))
3520 if (ad->disp != NULL)
3521 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
3522 else
3524 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
3525 update_address (ad);
3527 change_p = true;
3529 if (lra_dump_file != NULL)
3531 if (! change_p)
3532 fprintf (lra_dump_file, " -- no change\n");
3533 else
3535 fprintf (lra_dump_file, " on equiv ");
3536 dump_value_slim (lra_dump_file, *ad->outer, 1);
3537 fprintf (lra_dump_file, "\n");
3540 return change_p;
3543 /* Skip all modifiers and whitespaces in constraint STR and return the
3544 result. */
3545 static const char *
3546 skip_constraint_modifiers (const char *str)
3548 for (;;str++)
3549 switch (*str)
3551 case '+': case '&' : case '=': case '*': case ' ': case '\t':
3552 case '$': case '^' : case '%': case '?': case '!':
3553 break;
3554 default: return str;
3558 /* Takes a string of 0 or more comma-separated constraints. When more
3559 than one constraint is present, evaluate whether they all correspond
3560 to a single, repeated constraint (e.g. "r,r") or whether we have
3561 more than one distinct constraints (e.g. "r,m"). */
3562 static bool
3563 constraint_unique (const char *cstr)
3565 enum constraint_num ca, cb;
3566 ca = CONSTRAINT__UNKNOWN;
3567 for (;;)
3569 cstr = skip_constraint_modifiers (cstr);
3570 if (*cstr == '\0' || *cstr == ',')
3571 cb = CONSTRAINT_X;
3572 else
3574 cb = lookup_constraint (cstr);
3575 if (cb == CONSTRAINT__UNKNOWN)
3576 return false;
3577 cstr += CONSTRAINT_LEN (cstr[0], cstr);
3579 /* Handle the first iteration of the loop. */
3580 if (ca == CONSTRAINT__UNKNOWN)
3581 ca = cb;
3582 /* Handle the general case of comparing ca with subsequent
3583 constraints. */
3584 else if (ca != cb)
3585 return false;
3586 if (*cstr == '\0')
3587 return true;
3588 if (*cstr == ',')
3589 cstr += 1;
3593 /* Major function to make reloads for an address in operand NOP or
3594 check its correctness (If CHECK_ONLY_P is true). The supported
3595 cases are:
3597 1) an address that existed before LRA started, at which point it
3598 must have been valid. These addresses are subject to elimination
3599 and may have become invalid due to the elimination offset being out
3600 of range.
3602 2) an address created by forcing a constant to memory
3603 (force_const_to_mem). The initial form of these addresses might
3604 not be valid, and it is this function's job to make them valid.
3606 3) a frame address formed from a register and a (possibly zero)
3607 constant offset. As above, these addresses might not be valid and
3608 this function must make them so.
3610 Add reloads to the lists *BEFORE and *AFTER. We might need to add
3611 reloads to *AFTER because of inc/dec, {pre, post} modify in the
3612 address. Return true for any RTL change.
3614 The function is a helper function which does not produce all
3615 transformations (when CHECK_ONLY_P is false) which can be
3616 necessary. It does just basic steps. To do all necessary
3617 transformations use function process_address. */
3618 static bool
3619 process_address_1 (int nop, bool check_only_p,
3620 rtx_insn **before, rtx_insn **after)
3622 struct address_info ad;
3623 rtx new_reg;
3624 HOST_WIDE_INT scale;
3625 rtx op = *curr_id->operand_loc[nop];
3626 rtx mem = extract_mem_from_operand (op);
3627 const char *constraint;
3628 enum constraint_num cn;
3629 bool change_p = false;
3631 if (MEM_P (mem)
3632 && GET_MODE (mem) == BLKmode
3633 && GET_CODE (XEXP (mem, 0)) == SCRATCH)
3634 return false;
3636 constraint
3637 = skip_constraint_modifiers (curr_static_id->operand[nop].constraint);
3638 if (IN_RANGE (constraint[0], '0', '9'))
3640 char *end;
3641 unsigned long dup = strtoul (constraint, &end, 10);
3642 constraint
3643 = skip_constraint_modifiers (curr_static_id->operand[dup].constraint);
3645 cn = lookup_constraint (*constraint == '\0' ? "X" : constraint);
3646 /* If we have several alternatives or/and several constraints in an
3647 alternative and we can not say at this stage what constraint will be used,
3648 use unknown constraint. The exception is an address constraint. If
3649 operand has one address constraint, probably all others constraints are
3650 address ones. */
3651 if (constraint[0] != '\0' && get_constraint_type (cn) != CT_ADDRESS
3652 && !constraint_unique (constraint))
3653 cn = CONSTRAINT__UNKNOWN;
3654 if (insn_extra_address_constraint (cn)
3655 /* When we find an asm operand with an address constraint that
3656 doesn't satisfy address_operand to begin with, we clear
3657 is_address, so that we don't try to make a non-address fit.
3658 If the asm statement got this far, it's because other
3659 constraints are available, and we'll use them, disregarding
3660 the unsatisfiable address ones. */
3661 && curr_static_id->operand[nop].is_address)
3662 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
3663 /* Do not attempt to decompose arbitrary addresses generated by combine
3664 for asm operands with loose constraints, e.g 'X'.
3665 Need to extract memory from op for special memory constraint,
3666 i.e. bcst_mem_operand in i386 backend. */
3667 else if (MEM_P (mem)
3668 && !(INSN_CODE (curr_insn) < 0
3669 && get_constraint_type (cn) == CT_FIXED_FORM
3670 && constraint_satisfied_p (op, cn)))
3671 decompose_mem_address (&ad, mem);
3672 else if (GET_CODE (op) == SUBREG
3673 && MEM_P (SUBREG_REG (op)))
3674 decompose_mem_address (&ad, SUBREG_REG (op));
3675 else
3676 return false;
3677 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
3678 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
3679 when INDEX_REG_CLASS is a single register class. */
3680 enum reg_class index_cl = index_reg_class (curr_insn);
3681 if (ad.base_term != NULL
3682 && ad.index_term != NULL
3683 && ira_class_hard_regs_num[index_cl] == 1
3684 && REG_P (*ad.base_term)
3685 && REG_P (*ad.index_term)
3686 && in_class_p (*ad.base_term, index_cl, NULL)
3687 && ! in_class_p (*ad.index_term, index_cl, NULL))
3689 std::swap (ad.base, ad.index);
3690 std::swap (ad.base_term, ad.index_term);
3692 if (! check_only_p)
3693 change_p = equiv_address_substitution (&ad);
3694 if (ad.base_term != NULL
3695 && (process_addr_reg
3696 (ad.base_term, check_only_p, before,
3697 (ad.autoinc_p
3698 && !(REG_P (*ad.base_term)
3699 && find_regno_note (curr_insn, REG_DEAD,
3700 REGNO (*ad.base_term)) != NULL_RTX)
3701 ? after : NULL),
3702 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3703 get_index_code (&ad), curr_insn))))
3705 change_p = true;
3706 if (ad.base_term2 != NULL)
3707 *ad.base_term2 = *ad.base_term;
3709 if (ad.index_term != NULL
3710 && process_addr_reg (ad.index_term, check_only_p,
3711 before, NULL, index_cl))
3712 change_p = true;
3714 /* Target hooks sometimes don't treat extra-constraint addresses as
3715 legitimate address_operands, so handle them specially. */
3716 if (insn_extra_address_constraint (cn)
3717 && satisfies_address_constraint_p (&ad, cn))
3718 return change_p;
3720 if (check_only_p)
3721 return change_p;
3723 /* There are three cases where the shape of *AD.INNER may now be invalid:
3725 1) the original address was valid, but either elimination or
3726 equiv_address_substitution was applied and that made
3727 the address invalid.
3729 2) the address is an invalid symbolic address created by
3730 force_const_to_mem.
3732 3) the address is a frame address with an invalid offset.
3734 4) the address is a frame address with an invalid base.
3736 All these cases involve a non-autoinc address, so there is no
3737 point revalidating other types. */
3738 if (ad.autoinc_p || valid_address_p (op, &ad, cn))
3739 return change_p;
3741 /* Any index existed before LRA started, so we can assume that the
3742 presence and shape of the index is valid. */
3743 push_to_sequence (*before);
3744 lra_assert (ad.disp == ad.disp_term);
3745 if (ad.base == NULL)
3747 if (ad.index == NULL)
3749 rtx_insn *insn;
3750 rtx_insn *last = get_last_insn ();
3751 int code = -1;
3752 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3753 SCRATCH, SCRATCH,
3754 curr_insn);
3755 rtx addr = *ad.inner;
3757 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, NULL, "addr");
3758 if (HAVE_lo_sum)
3760 /* addr => lo_sum (new_base, addr), case (2) above. */
3761 insn = emit_insn (gen_rtx_SET
3762 (new_reg,
3763 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
3764 code = recog_memoized (insn);
3765 if (code >= 0)
3767 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
3768 if (!valid_address_p (op, &ad, cn))
3770 /* Try to put lo_sum into register. */
3771 insn = emit_insn (gen_rtx_SET
3772 (new_reg,
3773 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
3774 code = recog_memoized (insn);
3775 if (code >= 0)
3777 *ad.inner = new_reg;
3778 if (!valid_address_p (op, &ad, cn))
3780 *ad.inner = addr;
3781 code = -1;
3787 if (code < 0)
3788 delete_insns_since (last);
3791 if (code < 0)
3793 /* addr => new_base, case (2) above. */
3794 lra_emit_move (new_reg, addr);
3796 for (insn = last == NULL_RTX ? get_insns () : NEXT_INSN (last);
3797 insn != NULL_RTX;
3798 insn = NEXT_INSN (insn))
3799 if (recog_memoized (insn) < 0)
3800 break;
3801 if (insn != NULL_RTX)
3803 /* Do nothing if we cannot generate right insns.
3804 This is analogous to reload pass behavior. */
3805 delete_insns_since (last);
3806 end_sequence ();
3807 return false;
3809 *ad.inner = new_reg;
3812 else
3814 /* index * scale + disp => new base + index * scale,
3815 case (1) above. */
3816 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3817 GET_CODE (*ad.index),
3818 curr_insn);
3820 lra_assert (index_cl != NO_REGS);
3821 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, NULL, "disp");
3822 lra_emit_move (new_reg, *ad.disp);
3823 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3824 new_reg, *ad.index);
3827 else if (ad.index == NULL)
3829 int regno;
3830 enum reg_class cl;
3831 rtx set;
3832 rtx_insn *insns, *last_insn;
3833 /* Try to reload base into register only if the base is invalid
3834 for the address but with valid offset, case (4) above. */
3835 start_sequence ();
3836 new_reg = base_to_reg (&ad);
3838 /* base + disp => new base, cases (1) and (3) above. */
3839 /* Another option would be to reload the displacement into an
3840 index register. However, postreload has code to optimize
3841 address reloads that have the same base and different
3842 displacements, so reloading into an index register would
3843 not necessarily be a win. */
3844 if (new_reg == NULL_RTX)
3846 /* See if the target can split the displacement into a
3847 legitimate new displacement from a local anchor. */
3848 gcc_assert (ad.disp == ad.disp_term);
3849 poly_int64 orig_offset;
3850 rtx offset1, offset2;
3851 if (poly_int_rtx_p (*ad.disp, &orig_offset)
3852 && targetm.legitimize_address_displacement (&offset1, &offset2,
3853 orig_offset,
3854 ad.mode))
3856 new_reg = base_plus_disp_to_reg (&ad, offset1);
3857 new_reg = gen_rtx_PLUS (GET_MODE (new_reg), new_reg, offset2);
3859 else
3860 new_reg = base_plus_disp_to_reg (&ad, *ad.disp);
3862 insns = get_insns ();
3863 last_insn = get_last_insn ();
3864 /* If we generated at least two insns, try last insn source as
3865 an address. If we succeed, we generate one less insn. */
3866 if (REG_P (new_reg)
3867 && last_insn != insns
3868 && (set = single_set (last_insn)) != NULL_RTX
3869 && GET_CODE (SET_SRC (set)) == PLUS
3870 && REG_P (XEXP (SET_SRC (set), 0))
3871 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3873 *ad.inner = SET_SRC (set);
3874 if (valid_address_p (op, &ad, cn))
3876 *ad.base_term = XEXP (SET_SRC (set), 0);
3877 *ad.disp_term = XEXP (SET_SRC (set), 1);
3878 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3879 get_index_code (&ad), curr_insn);
3880 regno = REGNO (*ad.base_term);
3881 if (regno >= FIRST_PSEUDO_REGISTER
3882 && cl != lra_get_allocno_class (regno))
3883 lra_change_class (regno, cl, " Change to", true);
3884 new_reg = SET_SRC (set);
3885 delete_insns_since (PREV_INSN (last_insn));
3888 end_sequence ();
3889 emit_insn (insns);
3890 *ad.inner = new_reg;
3892 else if (ad.disp_term != NULL)
3894 /* base + scale * index + disp => new base + scale * index,
3895 case (1) above. */
3896 gcc_assert (ad.disp == ad.disp_term);
3897 new_reg = base_plus_disp_to_reg (&ad, *ad.disp);
3898 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3899 new_reg, *ad.index);
3901 else if ((scale = get_index_scale (&ad)) == 1)
3903 /* The last transformation to one reg will be made in
3904 curr_insn_transform function. */
3905 end_sequence ();
3906 return false;
3908 else if (scale != 0)
3910 /* base + scale * index => base + new_reg,
3911 case (1) above.
3912 Index part of address may become invalid. For example, we
3913 changed pseudo on the equivalent memory and a subreg of the
3914 pseudo onto the memory of different mode for which the scale is
3915 prohibitted. */
3916 new_reg = index_part_to_reg (&ad, index_cl);
3917 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3918 *ad.base_term, new_reg);
3920 else
3922 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3923 SCRATCH, SCRATCH,
3924 curr_insn);
3925 rtx addr = *ad.inner;
3927 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, NULL, "addr");
3928 /* addr => new_base. */
3929 lra_emit_move (new_reg, addr);
3930 *ad.inner = new_reg;
3932 *before = get_insns ();
3933 end_sequence ();
3934 return true;
3937 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3938 Use process_address_1 as a helper function. Return true for any
3939 RTL changes.
3941 If CHECK_ONLY_P is true, just check address correctness. Return
3942 false if the address correct. */
3943 static bool
3944 process_address (int nop, bool check_only_p,
3945 rtx_insn **before, rtx_insn **after)
3947 bool res = false;
3949 while (process_address_1 (nop, check_only_p, before, after))
3951 if (check_only_p)
3952 return true;
3953 res = true;
3955 return res;
3958 /* Override the generic address_reload_context in order to
3959 control the creation of reload pseudos. */
3960 class lra_autoinc_reload_context : public address_reload_context
3962 machine_mode mode;
3963 enum reg_class rclass;
3965 public:
3966 lra_autoinc_reload_context (machine_mode mode, enum reg_class new_rclass)
3967 : mode (mode), rclass (new_rclass) {}
3969 rtx get_reload_reg () const override final
3971 return lra_create_new_reg (mode, NULL_RTX, rclass, NULL, "INC/DEC result");
3975 /* Emit insns to reload VALUE into a new register. VALUE is an
3976 auto-increment or auto-decrement RTX whose operand is a register or
3977 memory location; so reloading involves incrementing that location.
3979 INC_AMOUNT is the number to increment or decrement by (always
3980 positive and ignored for POST_MODIFY/PRE_MODIFY).
3982 Return a pseudo containing the result. */
3983 static rtx
3984 emit_inc (enum reg_class new_rclass, rtx value, poly_int64 inc_amount)
3986 lra_autoinc_reload_context context (GET_MODE (value), new_rclass);
3987 return context.emit_autoinc (value, inc_amount);
3990 /* Return true if the current move insn does not need processing as we
3991 already know that it satisfies its constraints. */
3992 static bool
3993 simple_move_p (void)
3995 rtx dest, src;
3996 enum reg_class dclass, sclass;
3998 lra_assert (curr_insn_set != NULL_RTX);
3999 dest = SET_DEST (curr_insn_set);
4000 src = SET_SRC (curr_insn_set);
4002 /* If the instruction has multiple sets we need to process it even if it
4003 is single_set. This can happen if one or more of the SETs are dead.
4004 See PR73650. */
4005 if (multiple_sets (curr_insn))
4006 return false;
4008 return ((dclass = get_op_class (dest)) != NO_REGS
4009 && (sclass = get_op_class (src)) != NO_REGS
4010 /* The backend guarantees that register moves of cost 2
4011 never need reloads. */
4012 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
4015 /* Swap operands NOP and NOP + 1. */
4016 static inline void
4017 swap_operands (int nop)
4019 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
4020 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
4021 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
4022 std::swap (equiv_substition_p[nop], equiv_substition_p[nop + 1]);
4023 /* Swap the duplicates too. */
4024 lra_update_dup (curr_id, nop);
4025 lra_update_dup (curr_id, nop + 1);
4028 /* Main entry point of the constraint code: search the body of the
4029 current insn to choose the best alternative. It is mimicking insn
4030 alternative cost calculation model of former reload pass. That is
4031 because machine descriptions were written to use this model. This
4032 model can be changed in future. Make commutative operand exchange
4033 if it is chosen.
4035 if CHECK_ONLY_P is false, do RTL changes to satisfy the
4036 constraints. Return true if any change happened during function
4037 call.
4039 If CHECK_ONLY_P is true then don't do any transformation. Just
4040 check that the insn satisfies all constraints. If the insn does
4041 not satisfy any constraint, return true. */
4042 static bool
4043 curr_insn_transform (bool check_only_p)
4045 int i, j, k;
4046 int n_operands;
4047 int n_alternatives;
4048 int n_outputs;
4049 int commutative;
4050 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
4051 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
4052 signed char outputs[MAX_RECOG_OPERANDS + 1];
4053 rtx_insn *before, *after;
4054 bool alt_p = false;
4055 /* Flag that the insn has been changed through a transformation. */
4056 bool change_p;
4057 bool sec_mem_p;
4058 bool use_sec_mem_p;
4059 int max_regno_before;
4060 int reused_alternative_num;
4062 curr_insn_set = single_set (curr_insn);
4063 if (curr_insn_set != NULL_RTX && simple_move_p ())
4065 /* We assume that the corresponding insn alternative has no
4066 earlier clobbers. If it is not the case, don't define move
4067 cost equal to 2 for the corresponding register classes. */
4068 lra_set_used_insn_alternative (curr_insn, LRA_NON_CLOBBERED_ALT);
4069 return false;
4072 no_input_reloads_p = no_output_reloads_p = false;
4073 goal_alt_number = -1;
4074 change_p = sec_mem_p = false;
4076 /* CALL_INSNs are not allowed to have any output reloads. */
4077 if (CALL_P (curr_insn))
4078 no_output_reloads_p = true;
4080 n_operands = curr_static_id->n_operands;
4081 n_alternatives = curr_static_id->n_alternatives;
4083 /* Just return "no reloads" if insn has no operands with
4084 constraints. */
4085 if (n_operands == 0 || n_alternatives == 0)
4086 return false;
4088 max_regno_before = max_reg_num ();
4090 for (i = 0; i < n_operands; i++)
4092 goal_alt_matched[i][0] = -1;
4093 goal_alt_matches[i] = -1;
4096 commutative = curr_static_id->commutative;
4098 /* Now see what we need for pseudos that didn't get hard regs or got
4099 the wrong kind of hard reg. For this, we must consider all the
4100 operands together against the register constraints. */
4102 best_losers = best_overall = INT_MAX;
4103 best_reload_sum = 0;
4105 curr_swapped = false;
4106 goal_alt_swapped = false;
4108 if (! check_only_p)
4109 /* Make equivalence substitution and memory subreg elimination
4110 before address processing because an address legitimacy can
4111 depend on memory mode. */
4112 for (i = 0; i < n_operands; i++)
4114 rtx op, subst, old;
4115 bool op_change_p = false;
4117 if (curr_static_id->operand[i].is_operator)
4118 continue;
4120 old = op = *curr_id->operand_loc[i];
4121 if (GET_CODE (old) == SUBREG)
4122 old = SUBREG_REG (old);
4123 subst = get_equiv_with_elimination (old, curr_insn);
4124 original_subreg_reg_mode[i] = VOIDmode;
4125 equiv_substition_p[i] = false;
4126 if (subst != old)
4128 equiv_substition_p[i] = true;
4129 subst = copy_rtx (subst);
4130 lra_assert (REG_P (old));
4131 if (GET_CODE (op) != SUBREG)
4132 *curr_id->operand_loc[i] = subst;
4133 else
4135 SUBREG_REG (op) = subst;
4136 if (GET_MODE (subst) == VOIDmode)
4137 original_subreg_reg_mode[i] = GET_MODE (old);
4139 if (lra_dump_file != NULL)
4141 fprintf (lra_dump_file,
4142 "Changing pseudo %d in operand %i of insn %u on equiv ",
4143 REGNO (old), i, INSN_UID (curr_insn));
4144 dump_value_slim (lra_dump_file, subst, 1);
4145 fprintf (lra_dump_file, "\n");
4147 op_change_p = change_p = true;
4149 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
4151 change_p = true;
4152 lra_update_dup (curr_id, i);
4156 /* Reload address registers and displacements. We do it before
4157 finding an alternative because of memory constraints. */
4158 before = after = NULL;
4159 for (i = 0; i < n_operands; i++)
4160 if (! curr_static_id->operand[i].is_operator
4161 && process_address (i, check_only_p, &before, &after))
4163 if (check_only_p)
4164 return true;
4165 change_p = true;
4166 lra_update_dup (curr_id, i);
4169 if (change_p)
4170 /* If we've changed the instruction then any alternative that
4171 we chose previously may no longer be valid. */
4172 lra_set_used_insn_alternative (curr_insn, LRA_UNKNOWN_ALT);
4174 if (! check_only_p && curr_insn_set != NULL_RTX
4175 && check_and_process_move (&change_p, &sec_mem_p))
4176 return change_p;
4178 try_swapped:
4180 reused_alternative_num = check_only_p ? LRA_UNKNOWN_ALT : curr_id->used_insn_alternative;
4181 if (lra_dump_file != NULL && reused_alternative_num >= 0)
4182 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
4183 reused_alternative_num, INSN_UID (curr_insn));
4185 if (process_alt_operands (reused_alternative_num))
4186 alt_p = true;
4188 if (check_only_p)
4189 return ! alt_p || best_losers != 0;
4191 /* If insn is commutative (it's safe to exchange a certain pair of
4192 operands) then we need to try each alternative twice, the second
4193 time matching those two operands as if we had exchanged them. To
4194 do this, really exchange them in operands.
4196 If we have just tried the alternatives the second time, return
4197 operands to normal and drop through. */
4199 if (reused_alternative_num < 0 && commutative >= 0)
4201 curr_swapped = !curr_swapped;
4202 if (curr_swapped)
4204 swap_operands (commutative);
4205 goto try_swapped;
4207 else
4208 swap_operands (commutative);
4211 if (! alt_p && ! sec_mem_p)
4213 /* No alternative works with reloads?? */
4214 if (INSN_CODE (curr_insn) >= 0)
4215 fatal_insn ("unable to generate reloads for:", curr_insn);
4216 error_for_asm (curr_insn,
4217 "inconsistent operand constraints in an %<asm%>");
4218 lra_asm_error_p = true;
4219 if (! JUMP_P (curr_insn))
4221 /* Avoid further trouble with this insn. Don't generate use
4222 pattern here as we could use the insn SP offset. */
4223 lra_set_insn_deleted (curr_insn);
4225 else
4227 lra_invalidate_insn_data (curr_insn);
4228 ira_nullify_asm_goto (curr_insn);
4229 lra_update_insn_regno_info (curr_insn);
4231 return true;
4234 /* If the best alternative is with operands 1 and 2 swapped, swap
4235 them. Update the operand numbers of any reloads already
4236 pushed. */
4238 if (goal_alt_swapped)
4240 if (lra_dump_file != NULL)
4241 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
4242 INSN_UID (curr_insn));
4244 /* Swap the duplicates too. */
4245 swap_operands (commutative);
4246 change_p = true;
4249 /* Some targets' TARGET_SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
4250 too conservatively. So we use the secondary memory only if there
4251 is no any alternative without reloads. */
4252 use_sec_mem_p = false;
4253 if (! alt_p)
4254 use_sec_mem_p = true;
4255 else if (sec_mem_p)
4257 for (i = 0; i < n_operands; i++)
4258 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
4259 break;
4260 use_sec_mem_p = i < n_operands;
4263 if (use_sec_mem_p)
4265 int in = -1, out = -1;
4266 rtx new_reg, src, dest, rld;
4267 machine_mode sec_mode, rld_mode;
4269 lra_assert (curr_insn_set != NULL_RTX && sec_mem_p);
4270 dest = SET_DEST (curr_insn_set);
4271 src = SET_SRC (curr_insn_set);
4272 for (i = 0; i < n_operands; i++)
4273 if (*curr_id->operand_loc[i] == dest)
4274 out = i;
4275 else if (*curr_id->operand_loc[i] == src)
4276 in = i;
4277 for (i = 0; i < curr_static_id->n_dups; i++)
4278 if (out < 0 && *curr_id->dup_loc[i] == dest)
4279 out = curr_static_id->dup_num[i];
4280 else if (in < 0 && *curr_id->dup_loc[i] == src)
4281 in = curr_static_id->dup_num[i];
4282 lra_assert (out >= 0 && in >= 0
4283 && curr_static_id->operand[out].type == OP_OUT
4284 && curr_static_id->operand[in].type == OP_IN);
4285 rld = partial_subreg_p (GET_MODE (src), GET_MODE (dest)) ? src : dest;
4286 rld_mode = GET_MODE (rld);
4287 sec_mode = targetm.secondary_memory_needed_mode (rld_mode);
4288 new_reg = lra_create_new_reg (sec_mode, NULL_RTX, NO_REGS, NULL,
4289 "secondary");
4290 /* If the mode is changed, it should be wider. */
4291 lra_assert (!partial_subreg_p (sec_mode, rld_mode));
4292 if (sec_mode != rld_mode)
4294 /* If the target says specifically to use another mode for
4295 secondary memory moves we cannot reuse the original
4296 insn. */
4297 after = emit_spill_move (false, new_reg, dest);
4298 lra_process_new_insns (curr_insn, NULL, after,
4299 "Inserting the sec. move");
4300 /* We may have non null BEFORE here (e.g. after address
4301 processing. */
4302 push_to_sequence (before);
4303 before = emit_spill_move (true, new_reg, src);
4304 emit_insn (before);
4305 before = get_insns ();
4306 end_sequence ();
4307 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
4308 lra_set_insn_deleted (curr_insn);
4310 else if (dest == rld)
4312 *curr_id->operand_loc[out] = new_reg;
4313 lra_update_dup (curr_id, out);
4314 after = emit_spill_move (false, new_reg, dest);
4315 lra_process_new_insns (curr_insn, NULL, after,
4316 "Inserting the sec. move");
4318 else
4320 *curr_id->operand_loc[in] = new_reg;
4321 lra_update_dup (curr_id, in);
4322 /* See comments above. */
4323 push_to_sequence (before);
4324 before = emit_spill_move (true, new_reg, src);
4325 emit_insn (before);
4326 before = get_insns ();
4327 end_sequence ();
4328 lra_process_new_insns (curr_insn, before, NULL,
4329 "Inserting the sec. move");
4331 lra_update_insn_regno_info (curr_insn);
4332 return true;
4335 lra_assert (goal_alt_number >= 0);
4336 lra_set_used_insn_alternative (curr_insn, goal_reuse_alt_p
4337 ? goal_alt_number : LRA_UNKNOWN_ALT);
4339 if (lra_dump_file != NULL)
4341 const char *p;
4343 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
4344 goal_alt_number, INSN_UID (curr_insn));
4345 print_curr_insn_alt (goal_alt_number);
4346 if (INSN_CODE (curr_insn) >= 0
4347 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
4348 fprintf (lra_dump_file, " {%s}", p);
4349 if (maybe_ne (curr_id->sp_offset, 0))
4351 fprintf (lra_dump_file, " (sp_off=");
4352 print_dec (curr_id->sp_offset, lra_dump_file);
4353 fprintf (lra_dump_file, ")");
4355 fprintf (lra_dump_file, "\n");
4358 /* Right now, for any pair of operands I and J that are required to
4359 match, with J < I, goal_alt_matches[I] is J. Add I to
4360 goal_alt_matched[J]. */
4362 for (i = 0; i < n_operands; i++)
4363 if ((j = goal_alt_matches[i]) >= 0)
4365 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
4367 /* We allow matching one output operand and several input
4368 operands. */
4369 lra_assert (k == 0
4370 || (curr_static_id->operand[j].type == OP_OUT
4371 && curr_static_id->operand[i].type == OP_IN
4372 && (curr_static_id->operand
4373 [goal_alt_matched[j][0]].type == OP_IN)));
4374 goal_alt_matched[j][k] = i;
4375 goal_alt_matched[j][k + 1] = -1;
4378 for (i = 0; i < n_operands; i++)
4379 goal_alt_win[i] |= goal_alt_match_win[i];
4381 /* Any constants that aren't allowed and can't be reloaded into
4382 registers are here changed into memory references. */
4383 for (i = 0; i < n_operands; i++)
4384 if (goal_alt_win[i])
4386 int regno;
4387 enum reg_class new_class;
4388 rtx reg = *curr_id->operand_loc[i];
4390 if (GET_CODE (reg) == SUBREG)
4391 reg = SUBREG_REG (reg);
4393 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
4395 bool ok_p = in_class_p (reg, goal_alt[i], &new_class, true);
4397 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
4399 lra_assert (ok_p);
4400 lra_change_class (regno, new_class, " Change to", true);
4404 else
4406 const char *constraint;
4407 char c;
4408 rtx op = *curr_id->operand_loc[i];
4409 rtx subreg = NULL_RTX;
4410 machine_mode mode = curr_operand_mode[i];
4412 if (GET_CODE (op) == SUBREG)
4414 subreg = op;
4415 op = SUBREG_REG (op);
4416 mode = GET_MODE (op);
4419 if (CONST_POOL_OK_P (mode, op)
4420 && ((targetm.preferred_reload_class
4421 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
4422 || no_input_reloads_p))
4424 rtx tem = force_const_mem (mode, op);
4426 change_p = true;
4427 if (subreg != NULL_RTX)
4428 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
4430 *curr_id->operand_loc[i] = tem;
4431 lra_update_dup (curr_id, i);
4432 process_address (i, false, &before, &after);
4434 /* If the alternative accepts constant pool refs directly
4435 there will be no reload needed at all. */
4436 if (subreg != NULL_RTX)
4437 continue;
4438 /* Skip alternatives before the one requested. */
4439 constraint = (curr_static_id->operand_alternative
4440 [goal_alt_number * n_operands + i].constraint);
4441 for (;
4442 (c = *constraint) && c != ',' && c != '#';
4443 constraint += CONSTRAINT_LEN (c, constraint))
4445 enum constraint_num cn = lookup_constraint (constraint);
4446 if ((insn_extra_memory_constraint (cn)
4447 || insn_extra_special_memory_constraint (cn)
4448 || insn_extra_relaxed_memory_constraint (cn))
4449 && satisfies_memory_constraint_p (tem, cn))
4450 break;
4452 if (c == '\0' || c == ',' || c == '#')
4453 continue;
4455 goal_alt_win[i] = true;
4459 n_outputs = 0;
4460 for (i = 0; i < n_operands; i++)
4461 if (curr_static_id->operand[i].type == OP_OUT)
4462 outputs[n_outputs++] = i;
4463 outputs[n_outputs] = -1;
4464 for (i = 0; i < n_operands; i++)
4466 int regno;
4467 bool optional_p = false;
4468 rtx old, new_reg;
4469 rtx op = *curr_id->operand_loc[i];
4471 if (goal_alt_win[i])
4473 if (goal_alt[i] == NO_REGS
4474 && REG_P (op)
4475 /* When we assign NO_REGS it means that we will not
4476 assign a hard register to the scratch pseudo by
4477 assigment pass and the scratch pseudo will be
4478 spilled. Spilled scratch pseudos are transformed
4479 back to scratches at the LRA end. */
4480 && ira_former_scratch_operand_p (curr_insn, i)
4481 && ira_former_scratch_p (REGNO (op)))
4483 int regno = REGNO (op);
4484 lra_change_class (regno, NO_REGS, " Change to", true);
4485 if (lra_get_regno_hard_regno (regno) >= 0)
4486 /* We don't have to mark all insn affected by the
4487 spilled pseudo as there is only one such insn, the
4488 current one. */
4489 reg_renumber[regno] = -1;
4490 lra_assert (bitmap_single_bit_set_p
4491 (&lra_reg_info[REGNO (op)].insn_bitmap));
4493 /* We can do an optional reload. If the pseudo got a hard
4494 reg, we might improve the code through inheritance. If
4495 it does not get a hard register we coalesce memory/memory
4496 moves later. Ignore move insns to avoid cycling. */
4497 if (! lra_simple_p
4498 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
4499 && goal_alt[i] != NO_REGS && REG_P (op)
4500 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
4501 && regno < new_regno_start
4502 && ! ira_former_scratch_p (regno)
4503 && reg_renumber[regno] < 0
4504 /* Check that the optional reload pseudo will be able to
4505 hold given mode value. */
4506 && ! (prohibited_class_reg_set_mode_p
4507 (goal_alt[i], reg_class_contents[goal_alt[i]],
4508 PSEUDO_REGNO_MODE (regno)))
4509 && (curr_insn_set == NULL_RTX
4510 || !((REG_P (SET_SRC (curr_insn_set))
4511 || MEM_P (SET_SRC (curr_insn_set))
4512 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
4513 && (REG_P (SET_DEST (curr_insn_set))
4514 || MEM_P (SET_DEST (curr_insn_set))
4515 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
4516 optional_p = true;
4517 else if (goal_alt_matched[i][0] != -1
4518 && curr_static_id->operand[i].type == OP_OUT
4519 && (curr_static_id->operand_alternative
4520 [goal_alt_number * n_operands + i].earlyclobber)
4521 && REG_P (op))
4523 for (j = 0; goal_alt_matched[i][j] != -1; j++)
4525 rtx op2 = *curr_id->operand_loc[goal_alt_matched[i][j]];
4527 if (REG_P (op2) && REGNO (op) != REGNO (op2))
4528 break;
4530 if (goal_alt_matched[i][j] != -1)
4532 /* Generate reloads for different output and matched
4533 input registers. This is the easiest way to avoid
4534 creation of non-existing register conflicts in
4535 lra-lives.cc. */
4536 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i],
4537 &goal_alt_exclude_start_hard_regs[i], &before,
4538 &after, true);
4540 continue;
4542 else
4544 enum reg_class rclass, common_class;
4546 if (REG_P (op) && goal_alt[i] != NO_REGS
4547 && (regno = REGNO (op)) >= new_regno_start
4548 && (rclass = get_reg_class (regno)) == ALL_REGS
4549 && ((common_class = ira_reg_class_subset[rclass][goal_alt[i]])
4550 != NO_REGS)
4551 && common_class != ALL_REGS
4552 && enough_allocatable_hard_regs_p (common_class,
4553 GET_MODE (op)))
4554 /* Refine reload pseudo class from chosen alternative
4555 constraint. */
4556 lra_change_class (regno, common_class, " Change to", true);
4557 continue;
4561 /* Operands that match previous ones have already been handled. */
4562 if (goal_alt_matches[i] >= 0)
4563 continue;
4565 /* We should not have an operand with a non-offsettable address
4566 appearing where an offsettable address will do. It also may
4567 be a case when the address should be special in other words
4568 not a general one (e.g. it needs no index reg). */
4569 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
4571 enum reg_class rclass;
4572 rtx *loc = &XEXP (op, 0);
4573 enum rtx_code code = GET_CODE (*loc);
4575 push_to_sequence (before);
4576 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
4577 MEM, SCRATCH, curr_insn);
4578 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
4579 new_reg = emit_inc (rclass, *loc,
4580 /* This value does not matter for MODIFY. */
4581 GET_MODE_SIZE (GET_MODE (op)));
4582 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass,
4583 NULL, false,
4584 "offsetable address", &new_reg))
4586 rtx addr = *loc;
4587 enum rtx_code code = GET_CODE (addr);
4588 bool align_p = false;
4590 if (code == AND && CONST_INT_P (XEXP (addr, 1)))
4592 /* (and ... (const_int -X)) is used to align to X bytes. */
4593 align_p = true;
4594 addr = XEXP (*loc, 0);
4596 else
4597 addr = canonicalize_reload_addr (addr);
4599 lra_emit_move (new_reg, addr);
4600 if (align_p)
4601 emit_move_insn (new_reg, gen_rtx_AND (GET_MODE (new_reg), new_reg, XEXP (*loc, 1)));
4603 before = get_insns ();
4604 end_sequence ();
4605 *loc = new_reg;
4606 lra_update_dup (curr_id, i);
4608 else if (goal_alt_matched[i][0] == -1)
4610 machine_mode mode;
4611 rtx reg, *loc;
4612 int hard_regno;
4613 enum op_type type = curr_static_id->operand[i].type;
4615 loc = curr_id->operand_loc[i];
4616 mode = curr_operand_mode[i];
4617 if (GET_CODE (*loc) == SUBREG)
4619 reg = SUBREG_REG (*loc);
4620 poly_int64 byte = SUBREG_BYTE (*loc);
4621 if (REG_P (reg)
4622 /* Strict_low_part requires reloading the register and not
4623 just the subreg. Likewise for a strict subreg no wider
4624 than a word for WORD_REGISTER_OPERATIONS targets. */
4625 && (curr_static_id->operand[i].strict_low
4626 || (!paradoxical_subreg_p (mode, GET_MODE (reg))
4627 && (hard_regno
4628 = get_try_hard_regno (REGNO (reg))) >= 0
4629 && (simplify_subreg_regno
4630 (hard_regno,
4631 GET_MODE (reg), byte, mode) < 0)
4632 && (goal_alt[i] == NO_REGS
4633 || (simplify_subreg_regno
4634 (ira_class_hard_regs[goal_alt[i]][0],
4635 GET_MODE (reg), byte, mode) >= 0)))
4636 || (partial_subreg_p (mode, GET_MODE (reg))
4637 && known_le (GET_MODE_SIZE (GET_MODE (reg)),
4638 UNITS_PER_WORD)
4639 && WORD_REGISTER_OPERATIONS))
4640 /* Avoid the situation when there are no available hard regs
4641 for the pseudo mode but there are ones for the subreg
4642 mode: */
4643 && !(goal_alt[i] != NO_REGS
4644 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
4645 && (prohibited_class_reg_set_mode_p
4646 (goal_alt[i], reg_class_contents[goal_alt[i]],
4647 GET_MODE (reg)))
4648 && !(prohibited_class_reg_set_mode_p
4649 (goal_alt[i], reg_class_contents[goal_alt[i]],
4650 mode))))
4652 /* An OP_INOUT is required when reloading a subreg of a
4653 mode wider than a word to ensure that data beyond the
4654 word being reloaded is preserved. Also automatically
4655 ensure that strict_low_part reloads are made into
4656 OP_INOUT which should already be true from the backend
4657 constraints. */
4658 if (type == OP_OUT
4659 && (curr_static_id->operand[i].strict_low
4660 || read_modify_subreg_p (*loc)))
4661 type = OP_INOUT;
4662 loc = &SUBREG_REG (*loc);
4663 mode = GET_MODE (*loc);
4666 old = *loc;
4667 if (get_reload_reg (type, mode, old, goal_alt[i],
4668 &goal_alt_exclude_start_hard_regs[i],
4669 loc != curr_id->operand_loc[i], "", &new_reg)
4670 && type != OP_OUT)
4672 push_to_sequence (before);
4673 lra_emit_move (new_reg, old);
4674 before = get_insns ();
4675 end_sequence ();
4677 *loc = new_reg;
4678 if (type != OP_IN
4679 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
4681 start_sequence ();
4682 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
4683 emit_insn (after);
4684 after = get_insns ();
4685 end_sequence ();
4686 *loc = new_reg;
4688 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
4689 if (goal_alt_dont_inherit_ops[j] == i)
4691 lra_set_regno_unique_value (REGNO (new_reg));
4692 break;
4694 lra_update_dup (curr_id, i);
4696 else if (curr_static_id->operand[i].type == OP_IN
4697 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4698 == OP_OUT
4699 || (curr_static_id->operand[goal_alt_matched[i][0]].type
4700 == OP_INOUT
4701 && (operands_match_p
4702 (*curr_id->operand_loc[i],
4703 *curr_id->operand_loc[goal_alt_matched[i][0]],
4704 -1)))))
4706 /* generate reloads for input and matched outputs. */
4707 match_inputs[0] = i;
4708 match_inputs[1] = -1;
4709 match_reload (goal_alt_matched[i][0], match_inputs, outputs,
4710 goal_alt[i], &goal_alt_exclude_start_hard_regs[i],
4711 &before, &after,
4712 curr_static_id->operand_alternative
4713 [goal_alt_number * n_operands + goal_alt_matched[i][0]]
4714 .earlyclobber);
4716 else if ((curr_static_id->operand[i].type == OP_OUT
4717 || (curr_static_id->operand[i].type == OP_INOUT
4718 && (operands_match_p
4719 (*curr_id->operand_loc[i],
4720 *curr_id->operand_loc[goal_alt_matched[i][0]],
4721 -1))))
4722 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4723 == OP_IN))
4724 /* Generate reloads for output and matched inputs. */
4725 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i],
4726 &goal_alt_exclude_start_hard_regs[i], &before, &after,
4727 curr_static_id->operand_alternative
4728 [goal_alt_number * n_operands + i].earlyclobber);
4729 else if (curr_static_id->operand[i].type == OP_IN
4730 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4731 == OP_IN))
4733 /* Generate reloads for matched inputs. */
4734 match_inputs[0] = i;
4735 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
4736 match_inputs[j + 1] = k;
4737 match_inputs[j + 1] = -1;
4738 match_reload (-1, match_inputs, outputs, goal_alt[i],
4739 &goal_alt_exclude_start_hard_regs[i],
4740 &before, &after, false);
4742 else
4743 /* We must generate code in any case when function
4744 process_alt_operands decides that it is possible. */
4745 gcc_unreachable ();
4747 if (optional_p)
4749 rtx reg = op;
4751 lra_assert (REG_P (reg));
4752 regno = REGNO (reg);
4753 op = *curr_id->operand_loc[i]; /* Substitution. */
4754 if (GET_CODE (op) == SUBREG)
4755 op = SUBREG_REG (op);
4756 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
4757 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
4758 lra_reg_info[REGNO (op)].restore_rtx = reg;
4759 if (lra_dump_file != NULL)
4760 fprintf (lra_dump_file,
4761 " Making reload reg %d for reg %d optional\n",
4762 REGNO (op), regno);
4765 if (before != NULL_RTX || after != NULL_RTX
4766 || max_regno_before != max_reg_num ())
4767 change_p = true;
4768 if (change_p)
4770 lra_update_operator_dups (curr_id);
4771 /* Something changes -- process the insn. */
4772 lra_update_insn_regno_info (curr_insn);
4773 if (asm_noperands (PATTERN (curr_insn)) >= 0
4774 && ++curr_id->asm_reloads_num >= FIRST_PSEUDO_REGISTER)
4775 /* Most probably there are no enough registers to satisfy asm insn: */
4776 lra_asm_insn_error (curr_insn);
4778 if (goal_alt_out_sp_reload_p)
4780 /* We have an output stack pointer reload -- update sp offset: */
4781 rtx set;
4782 bool done_p = false;
4783 poly_int64 sp_offset = curr_id->sp_offset;
4784 for (rtx_insn *insn = after; insn != NULL_RTX; insn = NEXT_INSN (insn))
4785 if ((set = single_set (insn)) != NULL_RTX
4786 && SET_DEST (set) == stack_pointer_rtx)
4788 lra_assert (!done_p);
4789 done_p = true;
4790 curr_id->sp_offset = 0;
4791 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
4792 id->sp_offset = sp_offset;
4793 if (lra_dump_file != NULL)
4794 fprintf (lra_dump_file,
4795 " Moving sp offset from insn %u to %u\n",
4796 INSN_UID (curr_insn), INSN_UID (insn));
4798 lra_assert (done_p);
4800 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
4801 return change_p;
4804 /* Return true if INSN satisfies all constraints. In other words, no
4805 reload insns are needed. */
4806 bool
4807 lra_constrain_insn (rtx_insn *insn)
4809 int saved_new_regno_start = new_regno_start;
4810 int saved_new_insn_uid_start = new_insn_uid_start;
4811 bool change_p;
4813 curr_insn = insn;
4814 curr_id = lra_get_insn_recog_data (curr_insn);
4815 curr_static_id = curr_id->insn_static_data;
4816 new_insn_uid_start = get_max_uid ();
4817 new_regno_start = max_reg_num ();
4818 change_p = curr_insn_transform (true);
4819 new_regno_start = saved_new_regno_start;
4820 new_insn_uid_start = saved_new_insn_uid_start;
4821 return ! change_p;
4824 /* Return true if X is in LIST. */
4825 static bool
4826 in_list_p (rtx x, rtx list)
4828 for (; list != NULL_RTX; list = XEXP (list, 1))
4829 if (XEXP (list, 0) == x)
4830 return true;
4831 return false;
4834 /* Return true if X contains an allocatable hard register (if
4835 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
4836 static bool
4837 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
4839 int i, j;
4840 const char *fmt;
4841 enum rtx_code code;
4843 code = GET_CODE (x);
4844 if (REG_P (x))
4846 int regno = REGNO (x);
4847 HARD_REG_SET alloc_regs;
4849 if (hard_reg_p)
4851 if (regno >= FIRST_PSEUDO_REGISTER)
4852 regno = lra_get_regno_hard_regno (regno);
4853 if (regno < 0)
4854 return false;
4855 alloc_regs = ~lra_no_alloc_regs;
4856 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
4858 else
4860 if (regno < FIRST_PSEUDO_REGISTER)
4861 return false;
4862 if (! spilled_p)
4863 return true;
4864 return lra_get_regno_hard_regno (regno) < 0;
4867 fmt = GET_RTX_FORMAT (code);
4868 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4870 if (fmt[i] == 'e')
4872 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
4873 return true;
4875 else if (fmt[i] == 'E')
4877 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4878 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4879 return true;
4882 return false;
4885 /* Process all regs in location *LOC and change them on equivalent
4886 substitution. Return true if any change was done. */
4887 static bool
4888 loc_equivalence_change_p (rtx *loc)
4890 rtx subst, reg, x = *loc;
4891 bool result = false;
4892 enum rtx_code code = GET_CODE (x);
4893 const char *fmt;
4894 int i, j;
4896 if (code == SUBREG)
4898 reg = SUBREG_REG (x);
4899 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4900 && GET_MODE (subst) == VOIDmode)
4902 /* We cannot reload debug location. Simplify subreg here
4903 while we know the inner mode. */
4904 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4905 GET_MODE (reg), SUBREG_BYTE (x));
4906 return true;
4909 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4911 *loc = subst;
4912 return true;
4915 /* Scan all the operand sub-expressions. */
4916 fmt = GET_RTX_FORMAT (code);
4917 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4919 if (fmt[i] == 'e')
4920 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4921 else if (fmt[i] == 'E')
4922 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4923 result
4924 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4926 return result;
4929 /* Similar to loc_equivalence_change_p, but for use as
4930 simplify_replace_fn_rtx callback. DATA is insn for which the
4931 elimination is done. If it null we don't do the elimination. */
4932 static rtx
4933 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4935 if (!REG_P (loc))
4936 return NULL_RTX;
4938 rtx subst = (data == NULL
4939 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4940 if (subst != loc)
4941 return subst;
4943 return NULL_RTX;
4946 /* Maximum number of generated reload insns per an insn. It is for
4947 preventing this pass cycling in a bug case. */
4948 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4950 /* The current iteration number of this LRA pass. */
4951 int lra_constraint_iter;
4953 /* True if we should during assignment sub-pass check assignment
4954 correctness for all pseudos and spill some of them to correct
4955 conflicts. It can be necessary when we substitute equiv which
4956 needs checking register allocation correctness because the
4957 equivalent value contains allocatable hard registers, or when we
4958 restore multi-register pseudo, or when we change the insn code and
4959 its operand became INOUT operand when it was IN one before. */
4960 bool check_and_force_assignment_correctness_p;
4962 /* Return true if REGNO is referenced in more than one block. */
4963 static bool
4964 multi_block_pseudo_p (int regno)
4966 basic_block bb = NULL;
4967 unsigned int uid;
4968 bitmap_iterator bi;
4970 if (regno < FIRST_PSEUDO_REGISTER)
4971 return false;
4973 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4974 if (bb == NULL)
4975 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4976 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4977 return true;
4978 return false;
4981 /* Return true if LIST contains a deleted insn. */
4982 static bool
4983 contains_deleted_insn_p (rtx_insn_list *list)
4985 for (; list != NULL_RTX; list = list->next ())
4986 if (NOTE_P (list->insn ())
4987 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4988 return true;
4989 return false;
4992 /* Return true if X contains a pseudo dying in INSN. */
4993 static bool
4994 dead_pseudo_p (rtx x, rtx_insn *insn)
4996 int i, j;
4997 const char *fmt;
4998 enum rtx_code code;
5000 if (REG_P (x))
5001 return (insn != NULL_RTX
5002 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
5003 code = GET_CODE (x);
5004 fmt = GET_RTX_FORMAT (code);
5005 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5007 if (fmt[i] == 'e')
5009 if (dead_pseudo_p (XEXP (x, i), insn))
5010 return true;
5012 else if (fmt[i] == 'E')
5014 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5015 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
5016 return true;
5019 return false;
5022 /* Return true if INSN contains a dying pseudo in INSN right hand
5023 side. */
5024 static bool
5025 insn_rhs_dead_pseudo_p (rtx_insn *insn)
5027 rtx set = single_set (insn);
5029 gcc_assert (set != NULL);
5030 return dead_pseudo_p (SET_SRC (set), insn);
5033 /* Return true if any init insn of REGNO contains a dying pseudo in
5034 insn right hand side. */
5035 static bool
5036 init_insn_rhs_dead_pseudo_p (int regno)
5038 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
5040 if (insns == NULL)
5041 return false;
5042 for (; insns != NULL_RTX; insns = insns->next ())
5043 if (insn_rhs_dead_pseudo_p (insns->insn ()))
5044 return true;
5045 return false;
5048 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
5049 reverse only if we have one init insn with given REGNO as a
5050 source. */
5051 static bool
5052 reverse_equiv_p (int regno)
5054 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
5055 rtx set;
5057 if (insns == NULL)
5058 return false;
5059 if (! INSN_P (insns->insn ())
5060 || insns->next () != NULL)
5061 return false;
5062 if ((set = single_set (insns->insn ())) == NULL_RTX)
5063 return false;
5064 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
5067 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
5068 call this function only for non-reverse equivalence. */
5069 static bool
5070 contains_reloaded_insn_p (int regno)
5072 rtx set;
5073 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
5075 for (; list != NULL; list = list->next ())
5076 if ((set = single_set (list->insn ())) == NULL_RTX
5077 || ! REG_P (SET_DEST (set))
5078 || (int) REGNO (SET_DEST (set)) != regno)
5079 return true;
5080 return false;
5083 /* Try combine secondary memory reload insn FROM for insn TO into TO insn.
5084 FROM should be a load insn (usually a secondary memory reload insn). Return
5085 TRUE in case of success. */
5086 static bool
5087 combine_reload_insn (rtx_insn *from, rtx_insn *to)
5089 bool ok_p;
5090 rtx_insn *saved_insn;
5091 rtx set, from_reg, to_reg, op;
5092 enum reg_class to_class, from_class;
5093 int n, nop;
5094 signed char changed_nops[MAX_RECOG_OPERANDS + 1];
5096 /* Check conditions for second memory reload and original insn: */
5097 if ((targetm.secondary_memory_needed
5098 == hook_bool_mode_reg_class_t_reg_class_t_false)
5099 || NEXT_INSN (from) != to
5100 || !NONDEBUG_INSN_P (to)
5101 || CALL_P (to))
5102 return false;
5104 lra_insn_recog_data_t id = lra_get_insn_recog_data (to);
5105 struct lra_static_insn_data *static_id = id->insn_static_data;
5107 if (id->used_insn_alternative == LRA_UNKNOWN_ALT
5108 || (set = single_set (from)) == NULL_RTX)
5109 return false;
5110 from_reg = SET_DEST (set);
5111 to_reg = SET_SRC (set);
5112 /* Ignore optional reloads: */
5113 if (! REG_P (from_reg) || ! REG_P (to_reg)
5114 || bitmap_bit_p (&lra_optional_reload_pseudos, REGNO (from_reg)))
5115 return false;
5116 to_class = lra_get_allocno_class (REGNO (to_reg));
5117 from_class = lra_get_allocno_class (REGNO (from_reg));
5118 /* Check that reload insn is a load: */
5119 if (to_class != NO_REGS || from_class == NO_REGS)
5120 return false;
5121 for (n = nop = 0; nop < static_id->n_operands; nop++)
5123 if (static_id->operand[nop].type != OP_IN)
5124 continue;
5125 op = *id->operand_loc[nop];
5126 if (!REG_P (op) || REGNO (op) != REGNO (from_reg))
5127 continue;
5128 *id->operand_loc[nop] = to_reg;
5129 changed_nops[n++] = nop;
5131 changed_nops[n] = -1;
5132 lra_update_dups (id, changed_nops);
5133 lra_update_insn_regno_info (to);
5134 ok_p = recog_memoized (to) >= 0;
5135 if (ok_p)
5137 /* Check that combined insn does not need any reloads: */
5138 saved_insn = curr_insn;
5139 curr_insn = to;
5140 curr_id = lra_get_insn_recog_data (curr_insn);
5141 curr_static_id = curr_id->insn_static_data;
5142 for (bool swapped_p = false;;)
5144 ok_p = !curr_insn_transform (true);
5145 if (ok_p || curr_static_id->commutative < 0)
5146 break;
5147 swap_operands (curr_static_id->commutative);
5148 if (lra_dump_file != NULL)
5150 fprintf (lra_dump_file,
5151 " Swapping %scombined insn operands:\n",
5152 swapped_p ? "back " : "");
5153 dump_insn_slim (lra_dump_file, to);
5155 if (swapped_p)
5156 break;
5157 swapped_p = true;
5159 curr_insn = saved_insn;
5160 curr_id = lra_get_insn_recog_data (curr_insn);
5161 curr_static_id = curr_id->insn_static_data;
5163 if (ok_p)
5165 id->used_insn_alternative = -1;
5166 lra_push_insn_and_update_insn_regno_info (to);
5167 if (lra_dump_file != NULL)
5169 fprintf (lra_dump_file, " Use combined insn:\n");
5170 dump_insn_slim (lra_dump_file, to);
5172 return true;
5174 if (lra_dump_file != NULL)
5176 fprintf (lra_dump_file, " Failed combined insn:\n");
5177 dump_insn_slim (lra_dump_file, to);
5179 for (int i = 0; i < n; i++)
5181 nop = changed_nops[i];
5182 *id->operand_loc[nop] = from_reg;
5184 lra_update_dups (id, changed_nops);
5185 lra_update_insn_regno_info (to);
5186 if (lra_dump_file != NULL)
5188 fprintf (lra_dump_file, " Restoring insn after failed combining:\n");
5189 dump_insn_slim (lra_dump_file, to);
5191 return false;
5194 /* Entry function of LRA constraint pass. Return true if the
5195 constraint pass did change the code. */
5196 bool
5197 lra_constraints (bool first_p)
5199 bool changed_p;
5200 int i, hard_regno, new_insns_num;
5201 unsigned int min_len, new_min_len, uid;
5202 rtx set, x, reg, dest_reg;
5203 rtx_insn *original_insn;
5204 basic_block last_bb;
5205 bitmap_iterator bi;
5207 lra_constraint_iter++;
5208 if (lra_dump_file != NULL)
5209 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
5210 lra_constraint_iter);
5211 changed_p = false;
5212 if (pic_offset_table_rtx
5213 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
5214 check_and_force_assignment_correctness_p = true;
5215 else if (first_p)
5216 /* On the first iteration we should check IRA assignment
5217 correctness. In rare cases, the assignments can be wrong as
5218 early clobbers operands are ignored in IRA or usages of
5219 paradoxical sub-registers are not taken into account by
5220 IRA. */
5221 check_and_force_assignment_correctness_p = true;
5222 new_insn_uid_start = get_max_uid ();
5223 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
5224 /* Mark used hard regs for target stack size calulations. */
5225 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5226 if (lra_reg_info[i].nrefs != 0
5227 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
5229 int j, nregs;
5231 nregs = hard_regno_nregs (hard_regno, lra_reg_info[i].biggest_mode);
5232 for (j = 0; j < nregs; j++)
5233 df_set_regs_ever_live (hard_regno + j, true);
5235 /* Do elimination before the equivalence processing as we can spill
5236 some pseudos during elimination. */
5237 lra_eliminate (false, first_p);
5238 auto_bitmap equiv_insn_bitmap (&reg_obstack);
5239 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5240 if (lra_reg_info[i].nrefs != 0)
5242 ira_reg_equiv[i].profitable_p = true;
5243 reg = regno_reg_rtx[i];
5244 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
5246 bool pseudo_p = contains_reg_p (x, false, false);
5248 /* After RTL transformation, we cannot guarantee that
5249 pseudo in the substitution was not reloaded which might
5250 make equivalence invalid. For example, in reverse
5251 equiv of p0
5253 p0 <- ...
5255 equiv_mem <- p0
5257 the memory address register was reloaded before the 2nd
5258 insn. */
5259 if ((! first_p && pseudo_p)
5260 /* We don't use DF for compilation speed sake. So it
5261 is problematic to update live info when we use an
5262 equivalence containing pseudos in more than one
5263 BB. */
5264 || (pseudo_p && multi_block_pseudo_p (i))
5265 /* If an init insn was deleted for some reason, cancel
5266 the equiv. We could update the equiv insns after
5267 transformations including an equiv insn deletion
5268 but it is not worthy as such cases are extremely
5269 rare. */
5270 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
5271 /* If it is not a reverse equivalence, we check that a
5272 pseudo in rhs of the init insn is not dying in the
5273 insn. Otherwise, the live info at the beginning of
5274 the corresponding BB might be wrong after we
5275 removed the insn. When the equiv can be a
5276 constant, the right hand side of the init insn can
5277 be a pseudo. */
5278 || (! reverse_equiv_p (i)
5279 && (init_insn_rhs_dead_pseudo_p (i)
5280 /* If we reloaded the pseudo in an equivalence
5281 init insn, we cannot remove the equiv init
5282 insns and the init insns might write into
5283 const memory in this case. */
5284 || contains_reloaded_insn_p (i)))
5285 /* Prevent access beyond equivalent memory for
5286 paradoxical subregs. */
5287 || (MEM_P (x)
5288 && maybe_gt (GET_MODE_SIZE (lra_reg_info[i].biggest_mode),
5289 GET_MODE_SIZE (GET_MODE (x))))
5290 || (pic_offset_table_rtx
5291 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
5292 && (targetm.preferred_reload_class
5293 (x, lra_get_allocno_class (i)) == NO_REGS))
5294 || contains_symbol_ref_p (x))))
5295 ira_reg_equiv[i].defined_p
5296 = ira_reg_equiv[i].caller_save_p = false;
5297 if (contains_reg_p (x, false, true))
5298 ira_reg_equiv[i].profitable_p = false;
5299 if (get_equiv (reg) != reg)
5300 bitmap_ior_into (equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
5303 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5304 update_equiv (i);
5305 /* We should add all insns containing pseudos which should be
5306 substituted by their equivalences. */
5307 EXECUTE_IF_SET_IN_BITMAP (equiv_insn_bitmap, 0, uid, bi)
5308 lra_push_insn_by_uid (uid);
5309 min_len = lra_insn_stack_length ();
5310 new_insns_num = 0;
5311 last_bb = NULL;
5312 changed_p = false;
5313 original_insn = NULL;
5314 while ((new_min_len = lra_insn_stack_length ()) != 0)
5316 curr_insn = lra_pop_insn ();
5317 --new_min_len;
5318 curr_bb = BLOCK_FOR_INSN (curr_insn);
5319 if (curr_bb != last_bb)
5321 last_bb = curr_bb;
5322 bb_reload_num = lra_curr_reload_num;
5324 if (min_len > new_min_len)
5326 min_len = new_min_len;
5327 new_insns_num = 0;
5328 original_insn = curr_insn;
5330 else if (combine_reload_insn (curr_insn, original_insn))
5332 continue;
5334 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
5335 internal_error
5336 ("maximum number of generated reload insns per insn achieved (%d)",
5337 MAX_RELOAD_INSNS_NUMBER);
5338 new_insns_num++;
5339 if (DEBUG_INSN_P (curr_insn))
5341 /* We need to check equivalence in debug insn and change
5342 pseudo to the equivalent value if necessary. */
5343 curr_id = lra_get_insn_recog_data (curr_insn);
5344 if (bitmap_bit_p (equiv_insn_bitmap, INSN_UID (curr_insn)))
5346 rtx old = *curr_id->operand_loc[0];
5347 *curr_id->operand_loc[0]
5348 = simplify_replace_fn_rtx (old, NULL_RTX,
5349 loc_equivalence_callback, curr_insn);
5350 if (old != *curr_id->operand_loc[0])
5352 /* If we substitute pseudo by shared equivalence, we can fail
5353 to update LRA reg info and this can result in many
5354 unexpected consequences. So keep rtl unshared: */
5355 *curr_id->operand_loc[0]
5356 = copy_rtx (*curr_id->operand_loc[0]);
5357 lra_update_insn_regno_info (curr_insn);
5358 changed_p = true;
5362 else if (INSN_P (curr_insn))
5364 if ((set = single_set (curr_insn)) != NULL_RTX)
5366 dest_reg = SET_DEST (set);
5367 /* The equivalence pseudo could be set up as SUBREG in a
5368 case when it is a call restore insn in a mode
5369 different from the pseudo mode. */
5370 if (GET_CODE (dest_reg) == SUBREG)
5371 dest_reg = SUBREG_REG (dest_reg);
5372 if ((REG_P (dest_reg)
5373 && (x = get_equiv (dest_reg)) != dest_reg
5374 /* Remove insns which set up a pseudo whose value
5375 cannot be changed. Such insns might be not in
5376 init_insns because we don't update equiv data
5377 during insn transformations.
5379 As an example, let suppose that a pseudo got
5380 hard register and on the 1st pass was not
5381 changed to equivalent constant. We generate an
5382 additional insn setting up the pseudo because of
5383 secondary memory movement. Then the pseudo is
5384 spilled and we use the equiv constant. In this
5385 case we should remove the additional insn and
5386 this insn is not init_insns list. */
5387 && (! MEM_P (x) || MEM_READONLY_P (x)
5388 /* Check that this is actually an insn setting
5389 up the equivalence. */
5390 || in_list_p (curr_insn,
5391 ira_reg_equiv
5392 [REGNO (dest_reg)].init_insns)))
5393 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
5394 && in_list_p (curr_insn,
5395 ira_reg_equiv
5396 [REGNO (SET_SRC (set))].init_insns)))
5398 /* This is equiv init insn of pseudo which did not get a
5399 hard register -- remove the insn. */
5400 if (lra_dump_file != NULL)
5402 fprintf (lra_dump_file,
5403 " Removing equiv init insn %i (freq=%d)\n",
5404 INSN_UID (curr_insn),
5405 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
5406 dump_insn_slim (lra_dump_file, curr_insn);
5408 if (contains_reg_p (x, true, false))
5409 check_and_force_assignment_correctness_p = true;
5410 lra_set_insn_deleted (curr_insn);
5411 continue;
5414 curr_id = lra_get_insn_recog_data (curr_insn);
5415 curr_static_id = curr_id->insn_static_data;
5416 init_curr_insn_input_reloads ();
5417 init_curr_operand_mode ();
5418 if (curr_insn_transform (false))
5419 changed_p = true;
5420 /* Check non-transformed insns too for equiv change as USE
5421 or CLOBBER don't need reloads but can contain pseudos
5422 being changed on their equivalences. */
5423 else if (bitmap_bit_p (equiv_insn_bitmap, INSN_UID (curr_insn))
5424 && loc_equivalence_change_p (&PATTERN (curr_insn)))
5426 lra_update_insn_regno_info (curr_insn);
5427 changed_p = true;
5432 /* If we used a new hard regno, changed_p should be true because the
5433 hard reg is assigned to a new pseudo. */
5434 if (flag_checking && !changed_p)
5436 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5437 if (lra_reg_info[i].nrefs != 0
5438 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
5440 int j, nregs = hard_regno_nregs (hard_regno,
5441 PSEUDO_REGNO_MODE (i));
5443 for (j = 0; j < nregs; j++)
5444 lra_assert (df_regs_ever_live_p (hard_regno + j));
5447 if (changed_p)
5448 lra_dump_insns_if_possible ("changed func after local");
5449 return changed_p;
5452 static void initiate_invariants (void);
5453 static void finish_invariants (void);
5455 /* Initiate the LRA constraint pass. It is done once per
5456 function. */
5457 void
5458 lra_constraints_init (void)
5460 initiate_invariants ();
5463 /* Finalize the LRA constraint pass. It is done once per
5464 function. */
5465 void
5466 lra_constraints_finish (void)
5468 finish_invariants ();
5473 /* Structure describes invariants for ineheritance. */
5474 struct lra_invariant
5476 /* The order number of the invariant. */
5477 int num;
5478 /* The invariant RTX. */
5479 rtx invariant_rtx;
5480 /* The origin insn of the invariant. */
5481 rtx_insn *insn;
5484 typedef lra_invariant invariant_t;
5485 typedef invariant_t *invariant_ptr_t;
5486 typedef const invariant_t *const_invariant_ptr_t;
5488 /* Pointer to the inheritance invariants. */
5489 static vec<invariant_ptr_t> invariants;
5491 /* Allocation pool for the invariants. */
5492 static object_allocator<lra_invariant> *invariants_pool;
5494 /* Hash table for the invariants. */
5495 static htab_t invariant_table;
5497 /* Hash function for INVARIANT. */
5498 static hashval_t
5499 invariant_hash (const void *invariant)
5501 rtx inv = ((const_invariant_ptr_t) invariant)->invariant_rtx;
5502 return lra_rtx_hash (inv);
5505 /* Equal function for invariants INVARIANT1 and INVARIANT2. */
5506 static int
5507 invariant_eq_p (const void *invariant1, const void *invariant2)
5509 rtx inv1 = ((const_invariant_ptr_t) invariant1)->invariant_rtx;
5510 rtx inv2 = ((const_invariant_ptr_t) invariant2)->invariant_rtx;
5512 return rtx_equal_p (inv1, inv2);
5515 /* Insert INVARIANT_RTX into the table if it is not there yet. Return
5516 invariant which is in the table. */
5517 static invariant_ptr_t
5518 insert_invariant (rtx invariant_rtx)
5520 void **entry_ptr;
5521 invariant_t invariant;
5522 invariant_ptr_t invariant_ptr;
5524 invariant.invariant_rtx = invariant_rtx;
5525 entry_ptr = htab_find_slot (invariant_table, &invariant, INSERT);
5526 if (*entry_ptr == NULL)
5528 invariant_ptr = invariants_pool->allocate ();
5529 invariant_ptr->invariant_rtx = invariant_rtx;
5530 invariant_ptr->insn = NULL;
5531 invariants.safe_push (invariant_ptr);
5532 *entry_ptr = (void *) invariant_ptr;
5534 return (invariant_ptr_t) *entry_ptr;
5537 /* Initiate the invariant table. */
5538 static void
5539 initiate_invariants (void)
5541 invariants.create (100);
5542 invariants_pool
5543 = new object_allocator<lra_invariant> ("Inheritance invariants");
5544 invariant_table = htab_create (100, invariant_hash, invariant_eq_p, NULL);
5547 /* Finish the invariant table. */
5548 static void
5549 finish_invariants (void)
5551 htab_delete (invariant_table);
5552 delete invariants_pool;
5553 invariants.release ();
5556 /* Make the invariant table empty. */
5557 static void
5558 clear_invariants (void)
5560 htab_empty (invariant_table);
5561 invariants_pool->release ();
5562 invariants.truncate (0);
5567 /* This page contains code to do inheritance/split
5568 transformations. */
5570 /* Number of reloads passed so far in current EBB. */
5571 static int reloads_num;
5573 /* Number of calls passed so far in current EBB. */
5574 static int calls_num;
5576 /* Index ID is the CALLS_NUM associated the last call we saw with
5577 ABI identifier ID. */
5578 static int last_call_for_abi[NUM_ABI_IDS];
5580 /* Which registers have been fully or partially clobbered by a call
5581 since they were last used. */
5582 static HARD_REG_SET full_and_partial_call_clobbers;
5584 /* Current reload pseudo check for validity of elements in
5585 USAGE_INSNS. */
5586 static int curr_usage_insns_check;
5588 /* Info about last usage of registers in EBB to do inheritance/split
5589 transformation. Inheritance transformation is done from a spilled
5590 pseudo and split transformations from a hard register or a pseudo
5591 assigned to a hard register. */
5592 struct usage_insns
5594 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
5595 value INSNS is valid. The insns is chain of optional debug insns
5596 and a finishing non-debug insn using the corresponding reg. The
5597 value is also used to mark the registers which are set up in the
5598 current insn. The negated insn uid is used for this. */
5599 int check;
5600 /* Value of global reloads_num at the last insn in INSNS. */
5601 int reloads_num;
5602 /* Value of global reloads_nums at the last insn in INSNS. */
5603 int calls_num;
5604 /* It can be true only for splitting. And it means that the restore
5605 insn should be put after insn given by the following member. */
5606 bool after_p;
5607 /* Next insns in the current EBB which use the original reg and the
5608 original reg value is not changed between the current insn and
5609 the next insns. In order words, e.g. for inheritance, if we need
5610 to use the original reg value again in the next insns we can try
5611 to use the value in a hard register from a reload insn of the
5612 current insn. */
5613 rtx insns;
5616 /* Map: regno -> corresponding pseudo usage insns. */
5617 static struct usage_insns *usage_insns;
5619 static void
5620 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
5622 usage_insns[regno].check = curr_usage_insns_check;
5623 usage_insns[regno].insns = insn;
5624 usage_insns[regno].reloads_num = reloads_num;
5625 usage_insns[regno].calls_num = calls_num;
5626 usage_insns[regno].after_p = after_p;
5627 if (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0)
5628 remove_from_hard_reg_set (&full_and_partial_call_clobbers,
5629 PSEUDO_REGNO_MODE (regno),
5630 reg_renumber[regno]);
5633 /* The function is used to form list REGNO usages which consists of
5634 optional debug insns finished by a non-debug insn using REGNO.
5635 RELOADS_NUM is current number of reload insns processed so far. */
5636 static void
5637 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
5639 rtx next_usage_insns;
5641 if (usage_insns[regno].check == curr_usage_insns_check
5642 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
5643 && DEBUG_INSN_P (insn))
5645 /* Check that we did not add the debug insn yet. */
5646 if (next_usage_insns != insn
5647 && (GET_CODE (next_usage_insns) != INSN_LIST
5648 || XEXP (next_usage_insns, 0) != insn))
5649 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
5650 next_usage_insns);
5652 else if (NONDEBUG_INSN_P (insn))
5653 setup_next_usage_insn (regno, insn, reloads_num, false);
5654 else
5655 usage_insns[regno].check = 0;
5658 /* Return first non-debug insn in list USAGE_INSNS. */
5659 static rtx_insn *
5660 skip_usage_debug_insns (rtx usage_insns)
5662 rtx insn;
5664 /* Skip debug insns. */
5665 for (insn = usage_insns;
5666 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
5667 insn = XEXP (insn, 1))
5669 return safe_as_a <rtx_insn *> (insn);
5672 /* Return true if we need secondary memory moves for insn in
5673 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
5674 into the insn. */
5675 static bool
5676 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
5677 rtx usage_insns ATTRIBUTE_UNUSED)
5679 rtx_insn *insn;
5680 rtx set, dest;
5681 enum reg_class cl;
5683 if (inher_cl == ALL_REGS
5684 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
5685 return false;
5686 lra_assert (INSN_P (insn));
5687 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
5688 return false;
5689 dest = SET_DEST (set);
5690 if (! REG_P (dest))
5691 return false;
5692 lra_assert (inher_cl != NO_REGS);
5693 cl = get_reg_class (REGNO (dest));
5694 return (cl != NO_REGS && cl != ALL_REGS
5695 && targetm.secondary_memory_needed (GET_MODE (dest), inher_cl, cl));
5698 /* Registers involved in inheritance/split in the current EBB
5699 (inheritance/split pseudos and original registers). */
5700 static bitmap_head check_only_regs;
5702 /* Reload pseudos cannot be involded in invariant inheritance in the
5703 current EBB. */
5704 static bitmap_head invalid_invariant_regs;
5706 /* Do inheritance transformations for insn INSN, which defines (if
5707 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
5708 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
5709 form as the "insns" field of usage_insns. Return true if we
5710 succeed in such transformation.
5712 The transformations look like:
5714 p <- ... i <- ...
5715 ... p <- i (new insn)
5716 ... =>
5717 <- ... p ... <- ... i ...
5719 ... i <- p (new insn)
5720 <- ... p ... <- ... i ...
5721 ... =>
5722 <- ... p ... <- ... i ...
5723 where p is a spilled original pseudo and i is a new inheritance pseudo.
5726 The inheritance pseudo has the smallest class of two classes CL and
5727 class of ORIGINAL REGNO. */
5728 static bool
5729 inherit_reload_reg (bool def_p, int original_regno,
5730 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
5732 if (optimize_function_for_size_p (cfun))
5733 return false;
5735 enum reg_class rclass = lra_get_allocno_class (original_regno);
5736 rtx original_reg = regno_reg_rtx[original_regno];
5737 rtx new_reg, usage_insn;
5738 rtx_insn *new_insns;
5740 lra_assert (! usage_insns[original_regno].after_p);
5741 if (lra_dump_file != NULL)
5742 fprintf (lra_dump_file,
5743 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
5744 if (! ira_reg_classes_intersect_p[cl][rclass])
5746 if (lra_dump_file != NULL)
5748 fprintf (lra_dump_file,
5749 " Rejecting inheritance for %d "
5750 "because of disjoint classes %s and %s\n",
5751 original_regno, reg_class_names[cl],
5752 reg_class_names[rclass]);
5753 fprintf (lra_dump_file,
5754 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5756 return false;
5758 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
5759 /* We don't use a subset of two classes because it can be
5760 NO_REGS. This transformation is still profitable in most
5761 cases even if the classes are not intersected as register
5762 move is probably cheaper than a memory load. */
5763 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
5765 if (lra_dump_file != NULL)
5766 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
5767 reg_class_names[cl], reg_class_names[rclass]);
5769 rclass = cl;
5771 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
5773 /* Reject inheritance resulting in secondary memory moves.
5774 Otherwise, there is a danger in LRA cycling. Also such
5775 transformation will be unprofitable. */
5776 if (lra_dump_file != NULL)
5778 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
5779 rtx set = single_set (insn);
5781 lra_assert (set != NULL_RTX);
5783 rtx dest = SET_DEST (set);
5785 lra_assert (REG_P (dest));
5786 fprintf (lra_dump_file,
5787 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
5788 "as secondary mem is needed\n",
5789 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
5790 original_regno, reg_class_names[rclass]);
5791 fprintf (lra_dump_file,
5792 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5794 return false;
5796 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
5797 rclass, NULL, "inheritance");
5798 start_sequence ();
5799 if (def_p)
5800 lra_emit_move (original_reg, new_reg);
5801 else
5802 lra_emit_move (new_reg, original_reg);
5803 new_insns = get_insns ();
5804 end_sequence ();
5805 if (NEXT_INSN (new_insns) != NULL_RTX)
5807 if (lra_dump_file != NULL)
5809 fprintf (lra_dump_file,
5810 " Rejecting inheritance %d->%d "
5811 "as it results in 2 or more insns:\n",
5812 original_regno, REGNO (new_reg));
5813 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
5814 fprintf (lra_dump_file,
5815 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5817 return false;
5819 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
5820 lra_update_insn_regno_info (insn);
5821 if (! def_p)
5822 /* We now have a new usage insn for original regno. */
5823 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
5824 if (lra_dump_file != NULL)
5825 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
5826 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
5827 lra_reg_info[REGNO (new_reg)].restore_rtx = regno_reg_rtx[original_regno];
5828 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5829 bitmap_set_bit (&check_only_regs, original_regno);
5830 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
5831 if (def_p)
5832 lra_process_new_insns (insn, NULL, new_insns,
5833 "Add original<-inheritance");
5834 else
5835 lra_process_new_insns (insn, new_insns, NULL,
5836 "Add inheritance<-original");
5837 while (next_usage_insns != NULL_RTX)
5839 if (GET_CODE (next_usage_insns) != INSN_LIST)
5841 usage_insn = next_usage_insns;
5842 lra_assert (NONDEBUG_INSN_P (usage_insn));
5843 next_usage_insns = NULL;
5845 else
5847 usage_insn = XEXP (next_usage_insns, 0);
5848 lra_assert (DEBUG_INSN_P (usage_insn));
5849 next_usage_insns = XEXP (next_usage_insns, 1);
5851 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false,
5852 DEBUG_INSN_P (usage_insn));
5853 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5854 if (lra_dump_file != NULL)
5856 basic_block bb = BLOCK_FOR_INSN (usage_insn);
5857 fprintf (lra_dump_file,
5858 " Inheritance reuse change %d->%d (bb%d):\n",
5859 original_regno, REGNO (new_reg),
5860 bb ? bb->index : -1);
5861 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5864 if (lra_dump_file != NULL)
5865 fprintf (lra_dump_file,
5866 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5867 return true;
5870 /* Return true if we need a caller save/restore for pseudo REGNO which
5871 was assigned to a hard register. */
5872 static inline bool
5873 need_for_call_save_p (int regno)
5875 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
5876 if (usage_insns[regno].calls_num < calls_num)
5878 unsigned int abis = 0;
5879 for (unsigned int i = 0; i < NUM_ABI_IDS; ++i)
5880 if (last_call_for_abi[i] > usage_insns[regno].calls_num)
5881 abis |= 1 << i;
5882 gcc_assert (abis);
5883 if (call_clobbered_in_region_p (abis, full_and_partial_call_clobbers,
5884 PSEUDO_REGNO_MODE (regno),
5885 reg_renumber[regno]))
5886 return true;
5888 return false;
5891 /* Global registers occurring in the current EBB. */
5892 static bitmap_head ebb_global_regs;
5894 /* Return true if we need a split for hard register REGNO or pseudo
5895 REGNO which was assigned to a hard register.
5896 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
5897 used for reloads since the EBB end. It is an approximation of the
5898 used hard registers in the split range. The exact value would
5899 require expensive calculations. If we were aggressive with
5900 splitting because of the approximation, the split pseudo will save
5901 the same hard register assignment and will be removed in the undo
5902 pass. We still need the approximation because too aggressive
5903 splitting would result in too inaccurate cost calculation in the
5904 assignment pass because of too many generated moves which will be
5905 probably removed in the undo pass. */
5906 static inline bool
5907 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
5909 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
5911 lra_assert (hard_regno >= 0);
5912 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
5913 /* Don't split eliminable hard registers, otherwise we can
5914 split hard registers like hard frame pointer, which
5915 lives on BB start/end according to DF-infrastructure,
5916 when there is a pseudo assigned to the register and
5917 living in the same BB. */
5918 && (regno >= FIRST_PSEUDO_REGISTER
5919 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
5920 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
5921 /* Don't split call clobbered hard regs living through
5922 calls, otherwise we might have a check problem in the
5923 assign sub-pass as in the most cases (exception is a
5924 situation when check_and_force_assignment_correctness_p value is
5925 true) the assign pass assumes that all pseudos living
5926 through calls are assigned to call saved hard regs. */
5927 && (regno >= FIRST_PSEUDO_REGISTER
5928 || !TEST_HARD_REG_BIT (full_and_partial_call_clobbers, regno))
5929 /* We need at least 2 reloads to make pseudo splitting
5930 profitable. We should provide hard regno splitting in
5931 any case to solve 1st insn scheduling problem when
5932 moving hard register definition up might result in
5933 impossibility to find hard register for reload pseudo of
5934 small register class. */
5935 && (usage_insns[regno].reloads_num
5936 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
5937 && (regno < FIRST_PSEUDO_REGISTER
5938 /* For short living pseudos, spilling + inheritance can
5939 be considered a substitution for splitting.
5940 Therefore we do not splitting for local pseudos. It
5941 decreases also aggressiveness of splitting. The
5942 minimal number of references is chosen taking into
5943 account that for 2 references splitting has no sense
5944 as we can just spill the pseudo. */
5945 || (regno >= FIRST_PSEUDO_REGISTER
5946 && lra_reg_info[regno].nrefs > 3
5947 && bitmap_bit_p (&ebb_global_regs, regno))))
5948 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
5951 /* Return class for the split pseudo created from original pseudo with
5952 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
5953 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
5954 results in no secondary memory movements. */
5955 static enum reg_class
5956 choose_split_class (enum reg_class allocno_class,
5957 int hard_regno ATTRIBUTE_UNUSED,
5958 machine_mode mode ATTRIBUTE_UNUSED)
5960 int i;
5961 enum reg_class cl, best_cl = NO_REGS;
5962 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
5963 = REGNO_REG_CLASS (hard_regno);
5965 if (! targetm.secondary_memory_needed (mode, allocno_class, allocno_class)
5966 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
5967 return allocno_class;
5968 for (i = 0;
5969 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
5970 i++)
5971 if (! targetm.secondary_memory_needed (mode, cl, hard_reg_class)
5972 && ! targetm.secondary_memory_needed (mode, hard_reg_class, cl)
5973 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
5974 && (best_cl == NO_REGS
5975 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
5976 best_cl = cl;
5977 return best_cl;
5980 /* Copy any equivalence information from ORIGINAL_REGNO to NEW_REGNO. It only
5981 makes sense to call this function if NEW_REGNO is always equal to
5982 ORIGINAL_REGNO. Set up defined_p flag when caller_save_p flag is set up and
5983 CALL_SAVE_P is true. */
5985 static void
5986 lra_copy_reg_equiv (unsigned int new_regno, unsigned int original_regno,
5987 bool call_save_p)
5989 if (!ira_reg_equiv[original_regno].defined_p
5990 && !(call_save_p && ira_reg_equiv[original_regno].caller_save_p))
5991 return;
5993 ira_expand_reg_equiv ();
5994 ira_reg_equiv[new_regno].defined_p = true;
5995 if (ira_reg_equiv[original_regno].memory)
5996 ira_reg_equiv[new_regno].memory
5997 = copy_rtx (ira_reg_equiv[original_regno].memory);
5998 if (ira_reg_equiv[original_regno].constant)
5999 ira_reg_equiv[new_regno].constant
6000 = copy_rtx (ira_reg_equiv[original_regno].constant);
6001 if (ira_reg_equiv[original_regno].invariant)
6002 ira_reg_equiv[new_regno].invariant
6003 = copy_rtx (ira_reg_equiv[original_regno].invariant);
6006 /* Do split transformations for insn INSN, which defines or uses
6007 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
6008 the EBB next uses ORIGINAL_REGNO; it has the same form as the
6009 "insns" field of usage_insns. If TO is not NULL, we don't use
6010 usage_insns, we put restore insns after TO insn. It is a case when
6011 we call it from lra_split_hard_reg_for, outside the inheritance
6012 pass.
6014 The transformations look like:
6016 p <- ... p <- ...
6017 ... s <- p (new insn -- save)
6018 ... =>
6019 ... p <- s (new insn -- restore)
6020 <- ... p ... <- ... p ...
6022 <- ... p ... <- ... p ...
6023 ... s <- p (new insn -- save)
6024 ... =>
6025 ... p <- s (new insn -- restore)
6026 <- ... p ... <- ... p ...
6028 where p is an original pseudo got a hard register or a hard
6029 register and s is a new split pseudo. The save is put before INSN
6030 if BEFORE_P is true. Return true if we succeed in such
6031 transformation. */
6032 static bool
6033 split_reg (bool before_p, int original_regno, rtx_insn *insn,
6034 rtx next_usage_insns, rtx_insn *to)
6036 enum reg_class rclass;
6037 rtx original_reg;
6038 int hard_regno, nregs;
6039 rtx new_reg, usage_insn;
6040 rtx_insn *restore, *save;
6041 bool after_p;
6042 bool call_save_p;
6043 machine_mode mode;
6045 if (original_regno < FIRST_PSEUDO_REGISTER)
6047 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
6048 hard_regno = original_regno;
6049 call_save_p = false;
6050 nregs = 1;
6051 mode = lra_reg_info[hard_regno].biggest_mode;
6052 machine_mode reg_rtx_mode = GET_MODE (regno_reg_rtx[hard_regno]);
6053 /* A reg can have a biggest_mode of VOIDmode if it was only ever seen as
6054 part of a multi-word register. In that case, just use the reg_rtx
6055 mode. Do the same also if the biggest mode was larger than a register
6056 or we can not compare the modes. Otherwise, limit the size to that of
6057 the biggest access in the function or to the natural mode at least. */
6058 if (mode == VOIDmode
6059 || !ordered_p (GET_MODE_PRECISION (mode),
6060 GET_MODE_PRECISION (reg_rtx_mode))
6061 || paradoxical_subreg_p (mode, reg_rtx_mode)
6062 || maybe_gt (GET_MODE_PRECISION (reg_rtx_mode), GET_MODE_PRECISION (mode)))
6064 original_reg = regno_reg_rtx[hard_regno];
6065 mode = reg_rtx_mode;
6067 else
6068 original_reg = gen_rtx_REG (mode, hard_regno);
6070 else
6072 mode = PSEUDO_REGNO_MODE (original_regno);
6073 hard_regno = reg_renumber[original_regno];
6074 nregs = hard_regno_nregs (hard_regno, mode);
6075 rclass = lra_get_allocno_class (original_regno);
6076 original_reg = regno_reg_rtx[original_regno];
6077 call_save_p = need_for_call_save_p (original_regno);
6079 lra_assert (hard_regno >= 0);
6080 if (lra_dump_file != NULL)
6081 fprintf (lra_dump_file,
6082 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
6084 if (call_save_p)
6086 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
6087 hard_regno_nregs (hard_regno, mode),
6088 mode);
6089 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, NULL, "save");
6091 else
6093 rclass = choose_split_class (rclass, hard_regno, mode);
6094 if (rclass == NO_REGS)
6096 if (lra_dump_file != NULL)
6098 fprintf (lra_dump_file,
6099 " Rejecting split of %d(%s): "
6100 "no good reg class for %d(%s)\n",
6101 original_regno,
6102 reg_class_names[lra_get_allocno_class (original_regno)],
6103 hard_regno,
6104 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
6105 fprintf
6106 (lra_dump_file,
6107 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6109 return false;
6111 /* Split_if_necessary can split hard registers used as part of a
6112 multi-register mode but splits each register individually. The
6113 mode used for each independent register may not be supported
6114 so reject the split. Splitting the wider mode should theoretically
6115 be possible but is not implemented. */
6116 if (!targetm.hard_regno_mode_ok (hard_regno, mode))
6118 if (lra_dump_file != NULL)
6120 fprintf (lra_dump_file,
6121 " Rejecting split of %d(%s): unsuitable mode %s\n",
6122 original_regno,
6123 reg_class_names[lra_get_allocno_class (original_regno)],
6124 GET_MODE_NAME (mode));
6125 fprintf
6126 (lra_dump_file,
6127 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6129 return false;
6131 new_reg = lra_create_new_reg (mode, original_reg, rclass, NULL, "split");
6132 reg_renumber[REGNO (new_reg)] = hard_regno;
6134 int new_regno = REGNO (new_reg);
6135 save = emit_spill_move (true, new_reg, original_reg);
6136 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
6138 if (lra_dump_file != NULL)
6140 fprintf
6141 (lra_dump_file,
6142 " Rejecting split %d->%d resulting in > 2 save insns:\n",
6143 original_regno, new_regno);
6144 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
6145 fprintf (lra_dump_file,
6146 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6148 return false;
6150 restore = emit_spill_move (false, new_reg, original_reg);
6151 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
6153 if (lra_dump_file != NULL)
6155 fprintf (lra_dump_file,
6156 " Rejecting split %d->%d "
6157 "resulting in > 2 restore insns:\n",
6158 original_regno, new_regno);
6159 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
6160 fprintf (lra_dump_file,
6161 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6163 return false;
6165 /* Transfer equivalence information to the spill register, so that
6166 if we fail to allocate the spill register, we have the option of
6167 rematerializing the original value instead of spilling to the stack. */
6168 if (!HARD_REGISTER_NUM_P (original_regno)
6169 && mode == PSEUDO_REGNO_MODE (original_regno))
6170 lra_copy_reg_equiv (new_regno, original_regno, call_save_p);
6171 lra_reg_info[new_regno].restore_rtx = regno_reg_rtx[original_regno];
6172 bitmap_set_bit (&lra_split_regs, new_regno);
6173 if (to != NULL)
6175 lra_assert (next_usage_insns == NULL);
6176 usage_insn = to;
6177 after_p = true;
6179 else
6181 /* We need check_only_regs only inside the inheritance pass. */
6182 bitmap_set_bit (&check_only_regs, new_regno);
6183 bitmap_set_bit (&check_only_regs, original_regno);
6184 after_p = usage_insns[original_regno].after_p;
6185 for (;;)
6187 if (GET_CODE (next_usage_insns) != INSN_LIST)
6189 usage_insn = next_usage_insns;
6190 break;
6192 usage_insn = XEXP (next_usage_insns, 0);
6193 lra_assert (DEBUG_INSN_P (usage_insn));
6194 next_usage_insns = XEXP (next_usage_insns, 1);
6195 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false,
6196 true);
6197 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
6198 if (lra_dump_file != NULL)
6200 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
6201 original_regno, new_regno);
6202 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
6206 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
6207 lra_assert (usage_insn != insn || (after_p && before_p));
6208 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
6209 after_p ? NULL : restore,
6210 after_p ? restore : NULL,
6211 call_save_p
6212 ? "Add reg<-save" : "Add reg<-split");
6213 lra_process_new_insns (insn, before_p ? save : NULL,
6214 before_p ? NULL : save,
6215 call_save_p
6216 ? "Add save<-reg" : "Add split<-reg");
6217 if (nregs > 1 || original_regno < FIRST_PSEUDO_REGISTER)
6218 /* If we are trying to split multi-register. We should check
6219 conflicts on the next assignment sub-pass. IRA can allocate on
6220 sub-register levels, LRA do this on pseudos level right now and
6221 this discrepancy may create allocation conflicts after
6222 splitting.
6224 If we are trying to split hard register we should also check conflicts
6225 as such splitting can create artificial conflict of the hard register
6226 with another pseudo because of simplified conflict calculation in
6227 LRA. */
6228 check_and_force_assignment_correctness_p = true;
6229 if (lra_dump_file != NULL)
6230 fprintf (lra_dump_file,
6231 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6232 return true;
6235 /* Split a hard reg for reload pseudo REGNO having RCLASS and living
6236 in the range [FROM, TO]. Return true if did a split. Otherwise,
6237 return false. */
6238 bool
6239 spill_hard_reg_in_range (int regno, enum reg_class rclass, rtx_insn *from, rtx_insn *to)
6241 int i, hard_regno;
6242 int rclass_size;
6243 rtx_insn *insn;
6244 unsigned int uid;
6245 bitmap_iterator bi;
6246 HARD_REG_SET ignore;
6248 lra_assert (from != NULL && to != NULL);
6249 ignore = lra_no_alloc_regs;
6250 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
6252 lra_insn_recog_data_t id = lra_insn_recog_data[uid];
6253 struct lra_static_insn_data *static_id = id->insn_static_data;
6254 struct lra_insn_reg *reg;
6256 for (reg = id->regs; reg != NULL; reg = reg->next)
6257 if (reg->regno < FIRST_PSEUDO_REGISTER)
6258 SET_HARD_REG_BIT (ignore, reg->regno);
6259 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
6260 SET_HARD_REG_BIT (ignore, reg->regno);
6262 rclass_size = ira_class_hard_regs_num[rclass];
6263 for (i = 0; i < rclass_size; i++)
6265 hard_regno = ira_class_hard_regs[rclass][i];
6266 if (! TEST_HARD_REG_BIT (lra_reg_info[regno].conflict_hard_regs, hard_regno)
6267 || TEST_HARD_REG_BIT (ignore, hard_regno))
6268 continue;
6269 for (insn = from; insn != NEXT_INSN (to); insn = NEXT_INSN (insn))
6271 struct lra_static_insn_data *static_id;
6272 struct lra_insn_reg *reg;
6274 if (!INSN_P (insn))
6275 continue;
6276 if (bitmap_bit_p (&lra_reg_info[hard_regno].insn_bitmap,
6277 INSN_UID (insn)))
6278 break;
6279 static_id = lra_get_insn_recog_data (insn)->insn_static_data;
6280 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
6281 if (reg->regno == hard_regno)
6282 break;
6283 if (reg != NULL)
6284 break;
6286 if (insn != NEXT_INSN (to))
6287 continue;
6288 if (split_reg (true, hard_regno, from, NULL, to))
6289 return true;
6291 return false;
6294 /* Recognize that we need a split transformation for insn INSN, which
6295 defines or uses REGNO in its insn biggest MODE (we use it only if
6296 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
6297 hard registers which might be used for reloads since the EBB end.
6298 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
6299 uid before starting INSN processing. Return true if we succeed in
6300 such transformation. */
6301 static bool
6302 split_if_necessary (int regno, machine_mode mode,
6303 HARD_REG_SET potential_reload_hard_regs,
6304 bool before_p, rtx_insn *insn, int max_uid)
6306 bool res = false;
6307 int i, nregs = 1;
6308 rtx next_usage_insns;
6310 if (regno < FIRST_PSEUDO_REGISTER)
6311 nregs = hard_regno_nregs (regno, mode);
6312 for (i = 0; i < nregs; i++)
6313 if (usage_insns[regno + i].check == curr_usage_insns_check
6314 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
6315 /* To avoid processing the register twice or more. */
6316 && ((GET_CODE (next_usage_insns) != INSN_LIST
6317 && INSN_UID (next_usage_insns) < max_uid)
6318 || (GET_CODE (next_usage_insns) == INSN_LIST
6319 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
6320 && need_for_split_p (potential_reload_hard_regs, regno + i)
6321 && split_reg (before_p, regno + i, insn, next_usage_insns, NULL))
6322 res = true;
6323 return res;
6326 /* Return TRUE if rtx X is considered as an invariant for
6327 inheritance. */
6328 static bool
6329 invariant_p (const_rtx x)
6331 machine_mode mode;
6332 const char *fmt;
6333 enum rtx_code code;
6334 int i, j;
6336 if (side_effects_p (x))
6337 return false;
6339 code = GET_CODE (x);
6340 mode = GET_MODE (x);
6341 if (code == SUBREG)
6343 x = SUBREG_REG (x);
6344 code = GET_CODE (x);
6345 mode = wider_subreg_mode (mode, GET_MODE (x));
6348 if (MEM_P (x))
6349 return false;
6351 if (REG_P (x))
6353 int i, nregs, regno = REGNO (x);
6355 if (regno >= FIRST_PSEUDO_REGISTER || regno == STACK_POINTER_REGNUM
6356 || TEST_HARD_REG_BIT (eliminable_regset, regno)
6357 || GET_MODE_CLASS (GET_MODE (x)) == MODE_CC)
6358 return false;
6359 nregs = hard_regno_nregs (regno, mode);
6360 for (i = 0; i < nregs; i++)
6361 if (! fixed_regs[regno + i]
6362 /* A hard register may be clobbered in the current insn
6363 but we can ignore this case because if the hard
6364 register is used it should be set somewhere after the
6365 clobber. */
6366 || bitmap_bit_p (&invalid_invariant_regs, regno + i))
6367 return false;
6369 fmt = GET_RTX_FORMAT (code);
6370 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6372 if (fmt[i] == 'e')
6374 if (! invariant_p (XEXP (x, i)))
6375 return false;
6377 else if (fmt[i] == 'E')
6379 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6380 if (! invariant_p (XVECEXP (x, i, j)))
6381 return false;
6384 return true;
6387 /* We have 'dest_reg <- invariant'. Let us try to make an invariant
6388 inheritance transformation (using dest_reg instead invariant in a
6389 subsequent insn). */
6390 static bool
6391 process_invariant_for_inheritance (rtx dst_reg, rtx invariant_rtx)
6393 invariant_ptr_t invariant_ptr;
6394 rtx_insn *insn, *new_insns;
6395 rtx insn_set, insn_reg, new_reg;
6396 int insn_regno;
6397 bool succ_p = false;
6398 int dst_regno = REGNO (dst_reg);
6399 machine_mode dst_mode = GET_MODE (dst_reg);
6400 enum reg_class cl = lra_get_allocno_class (dst_regno), insn_reg_cl;
6402 invariant_ptr = insert_invariant (invariant_rtx);
6403 if ((insn = invariant_ptr->insn) != NULL_RTX)
6405 /* We have a subsequent insn using the invariant. */
6406 insn_set = single_set (insn);
6407 lra_assert (insn_set != NULL);
6408 insn_reg = SET_DEST (insn_set);
6409 lra_assert (REG_P (insn_reg));
6410 insn_regno = REGNO (insn_reg);
6411 insn_reg_cl = lra_get_allocno_class (insn_regno);
6413 if (dst_mode == GET_MODE (insn_reg)
6414 /* We should consider only result move reg insns which are
6415 cheap. */
6416 && targetm.register_move_cost (dst_mode, cl, insn_reg_cl) == 2
6417 && targetm.register_move_cost (dst_mode, cl, cl) == 2)
6419 if (lra_dump_file != NULL)
6420 fprintf (lra_dump_file,
6421 " [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[\n");
6422 new_reg = lra_create_new_reg (dst_mode, dst_reg, cl, NULL,
6423 "invariant inheritance");
6424 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
6425 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
6426 lra_reg_info[REGNO (new_reg)].restore_rtx = PATTERN (insn);
6427 start_sequence ();
6428 lra_emit_move (new_reg, dst_reg);
6429 new_insns = get_insns ();
6430 end_sequence ();
6431 lra_process_new_insns (curr_insn, NULL, new_insns,
6432 "Add invariant inheritance<-original");
6433 start_sequence ();
6434 lra_emit_move (SET_DEST (insn_set), new_reg);
6435 new_insns = get_insns ();
6436 end_sequence ();
6437 lra_process_new_insns (insn, NULL, new_insns,
6438 "Changing reload<-inheritance");
6439 lra_set_insn_deleted (insn);
6440 succ_p = true;
6441 if (lra_dump_file != NULL)
6443 fprintf (lra_dump_file,
6444 " Invariant inheritance reuse change %d (bb%d):\n",
6445 REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
6446 dump_insn_slim (lra_dump_file, insn);
6447 fprintf (lra_dump_file,
6448 " ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]\n");
6452 invariant_ptr->insn = curr_insn;
6453 return succ_p;
6456 /* Check only registers living at the current program point in the
6457 current EBB. */
6458 static bitmap_head live_regs;
6460 /* Update live info in EBB given by its HEAD and TAIL insns after
6461 inheritance/split transformation. The function removes dead moves
6462 too. */
6463 static void
6464 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
6466 unsigned int j;
6467 int i, regno;
6468 bool live_p;
6469 rtx_insn *prev_insn;
6470 rtx set;
6471 bool remove_p;
6472 basic_block last_bb, prev_bb, curr_bb;
6473 bitmap_iterator bi;
6474 struct lra_insn_reg *reg;
6475 edge e;
6476 edge_iterator ei;
6478 last_bb = BLOCK_FOR_INSN (tail);
6479 prev_bb = NULL;
6480 for (curr_insn = tail;
6481 curr_insn != PREV_INSN (head);
6482 curr_insn = prev_insn)
6484 prev_insn = PREV_INSN (curr_insn);
6485 /* We need to process empty blocks too. They contain
6486 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
6487 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
6488 continue;
6489 curr_bb = BLOCK_FOR_INSN (curr_insn);
6490 if (curr_bb != prev_bb)
6492 if (prev_bb != NULL)
6494 /* Update df_get_live_in (prev_bb): */
6495 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
6496 if (bitmap_bit_p (&live_regs, j))
6497 bitmap_set_bit (df_get_live_in (prev_bb), j);
6498 else
6499 bitmap_clear_bit (df_get_live_in (prev_bb), j);
6501 if (curr_bb != last_bb)
6503 /* Update df_get_live_out (curr_bb): */
6504 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
6506 live_p = bitmap_bit_p (&live_regs, j);
6507 if (! live_p)
6508 FOR_EACH_EDGE (e, ei, curr_bb->succs)
6509 if (bitmap_bit_p (df_get_live_in (e->dest), j))
6511 live_p = true;
6512 break;
6514 if (live_p)
6515 bitmap_set_bit (df_get_live_out (curr_bb), j);
6516 else
6517 bitmap_clear_bit (df_get_live_out (curr_bb), j);
6520 prev_bb = curr_bb;
6521 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
6523 if (! NONDEBUG_INSN_P (curr_insn))
6524 continue;
6525 curr_id = lra_get_insn_recog_data (curr_insn);
6526 curr_static_id = curr_id->insn_static_data;
6527 remove_p = false;
6528 if ((set = single_set (curr_insn)) != NULL_RTX
6529 && REG_P (SET_DEST (set))
6530 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
6531 && SET_DEST (set) != pic_offset_table_rtx
6532 && bitmap_bit_p (&check_only_regs, regno)
6533 && ! bitmap_bit_p (&live_regs, regno))
6534 remove_p = true;
6535 /* See which defined values die here. */
6536 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6537 if (reg->type == OP_OUT && ! reg->subreg_p)
6538 bitmap_clear_bit (&live_regs, reg->regno);
6539 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6540 if (reg->type == OP_OUT && ! reg->subreg_p)
6541 bitmap_clear_bit (&live_regs, reg->regno);
6542 if (curr_id->arg_hard_regs != NULL)
6543 /* Make clobbered argument hard registers die. */
6544 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6545 if (regno >= FIRST_PSEUDO_REGISTER)
6546 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
6547 /* Mark each used value as live. */
6548 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6549 if (reg->type != OP_OUT
6550 && bitmap_bit_p (&check_only_regs, reg->regno))
6551 bitmap_set_bit (&live_regs, reg->regno);
6552 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6553 if (reg->type != OP_OUT
6554 && bitmap_bit_p (&check_only_regs, reg->regno))
6555 bitmap_set_bit (&live_regs, reg->regno);
6556 if (curr_id->arg_hard_regs != NULL)
6557 /* Make used argument hard registers live. */
6558 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6559 if (regno < FIRST_PSEUDO_REGISTER
6560 && bitmap_bit_p (&check_only_regs, regno))
6561 bitmap_set_bit (&live_regs, regno);
6562 /* It is quite important to remove dead move insns because it
6563 means removing dead store. We don't need to process them for
6564 constraints. */
6565 if (remove_p)
6567 if (lra_dump_file != NULL)
6569 fprintf (lra_dump_file, " Removing dead insn:\n ");
6570 dump_insn_slim (lra_dump_file, curr_insn);
6572 lra_set_insn_deleted (curr_insn);
6577 /* The structure describes info to do an inheritance for the current
6578 insn. We need to collect such info first before doing the
6579 transformations because the transformations change the insn
6580 internal representation. */
6581 struct to_inherit
6583 /* Original regno. */
6584 int regno;
6585 /* Subsequent insns which can inherit original reg value. */
6586 rtx insns;
6589 /* Array containing all info for doing inheritance from the current
6590 insn. */
6591 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
6593 /* Number elements in the previous array. */
6594 static int to_inherit_num;
6596 /* Add inheritance info REGNO and INSNS. Their meaning is described in
6597 structure to_inherit. */
6598 static void
6599 add_to_inherit (int regno, rtx insns)
6601 int i;
6603 for (i = 0; i < to_inherit_num; i++)
6604 if (to_inherit[i].regno == regno)
6605 return;
6606 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
6607 to_inherit[to_inherit_num].regno = regno;
6608 to_inherit[to_inherit_num++].insns = insns;
6611 /* Return the last non-debug insn in basic block BB, or the block begin
6612 note if none. */
6613 static rtx_insn *
6614 get_last_insertion_point (basic_block bb)
6616 rtx_insn *insn;
6618 FOR_BB_INSNS_REVERSE (bb, insn)
6619 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
6620 return insn;
6621 gcc_unreachable ();
6624 /* Set up RES by registers living on edges FROM except the edge (FROM,
6625 TO) or by registers set up in a jump insn in BB FROM. */
6626 static void
6627 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
6629 rtx_insn *last;
6630 struct lra_insn_reg *reg;
6631 edge e;
6632 edge_iterator ei;
6634 lra_assert (to != NULL);
6635 bitmap_clear (res);
6636 FOR_EACH_EDGE (e, ei, from->succs)
6637 if (e->dest != to)
6638 bitmap_ior_into (res, df_get_live_in (e->dest));
6639 last = get_last_insertion_point (from);
6640 if (! JUMP_P (last))
6641 return;
6642 curr_id = lra_get_insn_recog_data (last);
6643 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6644 if (reg->type != OP_IN)
6645 bitmap_set_bit (res, reg->regno);
6648 /* Used as a temporary results of some bitmap calculations. */
6649 static bitmap_head temp_bitmap;
6651 /* We split for reloads of small class of hard regs. The following
6652 defines how many hard regs the class should have to be qualified as
6653 small. The code is mostly oriented to x86/x86-64 architecture
6654 where some insns need to use only specific register or pair of
6655 registers and these register can live in RTL explicitly, e.g. for
6656 parameter passing. */
6657 static const int max_small_class_regs_num = 2;
6659 /* Do inheritance/split transformations in EBB starting with HEAD and
6660 finishing on TAIL. We process EBB insns in the reverse order.
6661 Return true if we did any inheritance/split transformation in the
6662 EBB.
6664 We should avoid excessive splitting which results in worse code
6665 because of inaccurate cost calculations for spilling new split
6666 pseudos in such case. To achieve this we do splitting only if
6667 register pressure is high in given basic block and there are reload
6668 pseudos requiring hard registers. We could do more register
6669 pressure calculations at any given program point to avoid necessary
6670 splitting even more but it is to expensive and the current approach
6671 works well enough. */
6672 static bool
6673 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
6675 int i, src_regno, dst_regno, nregs;
6676 bool change_p, succ_p, update_reloads_num_p;
6677 rtx_insn *prev_insn, *last_insn;
6678 rtx next_usage_insns, curr_set;
6679 enum reg_class cl;
6680 struct lra_insn_reg *reg;
6681 basic_block last_processed_bb, curr_bb = NULL;
6682 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
6683 bitmap to_process;
6684 unsigned int j;
6685 bitmap_iterator bi;
6686 bool head_p, after_p;
6688 change_p = false;
6689 curr_usage_insns_check++;
6690 clear_invariants ();
6691 reloads_num = calls_num = 0;
6692 for (unsigned int i = 0; i < NUM_ABI_IDS; ++i)
6693 last_call_for_abi[i] = 0;
6694 CLEAR_HARD_REG_SET (full_and_partial_call_clobbers);
6695 bitmap_clear (&check_only_regs);
6696 bitmap_clear (&invalid_invariant_regs);
6697 last_processed_bb = NULL;
6698 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
6699 live_hard_regs = eliminable_regset | lra_no_alloc_regs;
6700 /* We don't process new insns generated in the loop. */
6701 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
6703 prev_insn = PREV_INSN (curr_insn);
6704 if (BLOCK_FOR_INSN (curr_insn) != NULL)
6705 curr_bb = BLOCK_FOR_INSN (curr_insn);
6706 if (last_processed_bb != curr_bb)
6708 /* We are at the end of BB. Add qualified living
6709 pseudos for potential splitting. */
6710 to_process = df_get_live_out (curr_bb);
6711 if (last_processed_bb != NULL)
6713 /* We are somewhere in the middle of EBB. */
6714 get_live_on_other_edges (curr_bb, last_processed_bb,
6715 &temp_bitmap);
6716 to_process = &temp_bitmap;
6718 last_processed_bb = curr_bb;
6719 last_insn = get_last_insertion_point (curr_bb);
6720 after_p = (! JUMP_P (last_insn)
6721 && (! CALL_P (last_insn)
6722 || (find_reg_note (last_insn,
6723 REG_NORETURN, NULL_RTX) == NULL_RTX
6724 && ! SIBLING_CALL_P (last_insn))));
6725 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
6726 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
6728 if ((int) j >= lra_constraint_new_regno_start)
6729 break;
6730 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
6732 if (j < FIRST_PSEUDO_REGISTER)
6733 SET_HARD_REG_BIT (live_hard_regs, j);
6734 else
6735 add_to_hard_reg_set (&live_hard_regs,
6736 PSEUDO_REGNO_MODE (j),
6737 reg_renumber[j]);
6738 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
6742 src_regno = dst_regno = -1;
6743 curr_set = single_set (curr_insn);
6744 if (curr_set != NULL_RTX && REG_P (SET_DEST (curr_set)))
6745 dst_regno = REGNO (SET_DEST (curr_set));
6746 if (curr_set != NULL_RTX && REG_P (SET_SRC (curr_set)))
6747 src_regno = REGNO (SET_SRC (curr_set));
6748 update_reloads_num_p = true;
6749 if (src_regno < lra_constraint_new_regno_start
6750 && src_regno >= FIRST_PSEUDO_REGISTER
6751 && reg_renumber[src_regno] < 0
6752 && dst_regno >= lra_constraint_new_regno_start
6753 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
6755 /* 'reload_pseudo <- original_pseudo'. */
6756 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6757 reloads_num++;
6758 update_reloads_num_p = false;
6759 succ_p = false;
6760 if (usage_insns[src_regno].check == curr_usage_insns_check
6761 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
6762 succ_p = inherit_reload_reg (false, src_regno, cl,
6763 curr_insn, next_usage_insns);
6764 if (succ_p)
6765 change_p = true;
6766 else
6767 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
6768 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6769 potential_reload_hard_regs |= reg_class_contents[cl];
6771 else if (src_regno < 0
6772 && dst_regno >= lra_constraint_new_regno_start
6773 && invariant_p (SET_SRC (curr_set))
6774 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS
6775 && ! bitmap_bit_p (&invalid_invariant_regs, dst_regno)
6776 && ! bitmap_bit_p (&invalid_invariant_regs,
6777 ORIGINAL_REGNO(regno_reg_rtx[dst_regno])))
6779 /* 'reload_pseudo <- invariant'. */
6780 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6781 reloads_num++;
6782 update_reloads_num_p = false;
6783 if (process_invariant_for_inheritance (SET_DEST (curr_set), SET_SRC (curr_set)))
6784 change_p = true;
6785 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6786 potential_reload_hard_regs |= reg_class_contents[cl];
6788 else if (src_regno >= lra_constraint_new_regno_start
6789 && dst_regno < lra_constraint_new_regno_start
6790 && dst_regno >= FIRST_PSEUDO_REGISTER
6791 && reg_renumber[dst_regno] < 0
6792 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
6793 && usage_insns[dst_regno].check == curr_usage_insns_check
6794 && (next_usage_insns
6795 = usage_insns[dst_regno].insns) != NULL_RTX)
6797 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6798 reloads_num++;
6799 update_reloads_num_p = false;
6800 /* 'original_pseudo <- reload_pseudo'. */
6801 if (! JUMP_P (curr_insn)
6802 && inherit_reload_reg (true, dst_regno, cl,
6803 curr_insn, next_usage_insns))
6804 change_p = true;
6805 /* Invalidate. */
6806 usage_insns[dst_regno].check = 0;
6807 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6808 potential_reload_hard_regs |= reg_class_contents[cl];
6810 else if (INSN_P (curr_insn))
6812 int iter;
6813 int max_uid = get_max_uid ();
6815 curr_id = lra_get_insn_recog_data (curr_insn);
6816 curr_static_id = curr_id->insn_static_data;
6817 to_inherit_num = 0;
6818 /* Process insn definitions. */
6819 for (iter = 0; iter < 2; iter++)
6820 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
6821 reg != NULL;
6822 reg = reg->next)
6823 if (reg->type != OP_IN
6824 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
6826 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
6827 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
6828 && usage_insns[dst_regno].check == curr_usage_insns_check
6829 && (next_usage_insns
6830 = usage_insns[dst_regno].insns) != NULL_RTX)
6832 struct lra_insn_reg *r;
6834 for (r = curr_id->regs; r != NULL; r = r->next)
6835 if (r->type != OP_OUT && r->regno == dst_regno)
6836 break;
6837 /* Don't do inheritance if the pseudo is also
6838 used in the insn. */
6839 if (r == NULL)
6840 /* We cannot do inheritance right now
6841 because the current insn reg info (chain
6842 regs) can change after that. */
6843 add_to_inherit (dst_regno, next_usage_insns);
6845 /* We cannot process one reg twice here because of
6846 usage_insns invalidation. */
6847 if ((dst_regno < FIRST_PSEUDO_REGISTER
6848 || reg_renumber[dst_regno] >= 0)
6849 && ! reg->subreg_p && reg->type != OP_IN)
6851 HARD_REG_SET s;
6853 if (split_if_necessary (dst_regno, reg->biggest_mode,
6854 potential_reload_hard_regs,
6855 false, curr_insn, max_uid))
6856 change_p = true;
6857 CLEAR_HARD_REG_SET (s);
6858 if (dst_regno < FIRST_PSEUDO_REGISTER)
6859 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
6860 else
6861 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
6862 reg_renumber[dst_regno]);
6863 live_hard_regs &= ~s;
6864 potential_reload_hard_regs &= ~s;
6866 /* We should invalidate potential inheritance or
6867 splitting for the current insn usages to the next
6868 usage insns (see code below) as the output pseudo
6869 prevents this. */
6870 if ((dst_regno >= FIRST_PSEUDO_REGISTER
6871 && reg_renumber[dst_regno] < 0)
6872 || (reg->type == OP_OUT && ! reg->subreg_p
6873 && (dst_regno < FIRST_PSEUDO_REGISTER
6874 || reg_renumber[dst_regno] >= 0)))
6876 /* Invalidate and mark definitions. */
6877 if (dst_regno >= FIRST_PSEUDO_REGISTER)
6878 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
6879 else
6881 nregs = hard_regno_nregs (dst_regno,
6882 reg->biggest_mode);
6883 for (i = 0; i < nregs; i++)
6884 usage_insns[dst_regno + i].check
6885 = -(int) INSN_UID (curr_insn);
6889 /* Process clobbered call regs. */
6890 if (curr_id->arg_hard_regs != NULL)
6891 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6892 if (dst_regno >= FIRST_PSEUDO_REGISTER)
6893 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
6894 = -(int) INSN_UID (curr_insn);
6895 if (! JUMP_P (curr_insn))
6896 for (i = 0; i < to_inherit_num; i++)
6897 if (inherit_reload_reg (true, to_inherit[i].regno,
6898 ALL_REGS, curr_insn,
6899 to_inherit[i].insns))
6900 change_p = true;
6901 if (CALL_P (curr_insn))
6903 rtx cheap, pat, dest;
6904 rtx_insn *restore;
6905 int regno, hard_regno;
6907 calls_num++;
6908 function_abi callee_abi = insn_callee_abi (curr_insn);
6909 last_call_for_abi[callee_abi.id ()] = calls_num;
6910 full_and_partial_call_clobbers
6911 |= callee_abi.full_and_partial_reg_clobbers ();
6912 if ((cheap = find_reg_note (curr_insn,
6913 REG_RETURNED, NULL_RTX)) != NULL_RTX
6914 && ((cheap = XEXP (cheap, 0)), true)
6915 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
6916 && (hard_regno = reg_renumber[regno]) >= 0
6917 && usage_insns[regno].check == curr_usage_insns_check
6918 /* If there are pending saves/restores, the
6919 optimization is not worth. */
6920 && usage_insns[regno].calls_num == calls_num - 1
6921 && callee_abi.clobbers_reg_p (GET_MODE (cheap), hard_regno))
6923 /* Restore the pseudo from the call result as
6924 REG_RETURNED note says that the pseudo value is
6925 in the call result and the pseudo is an argument
6926 of the call. */
6927 pat = PATTERN (curr_insn);
6928 if (GET_CODE (pat) == PARALLEL)
6929 pat = XVECEXP (pat, 0, 0);
6930 dest = SET_DEST (pat);
6931 /* For multiple return values dest is PARALLEL.
6932 Currently we handle only single return value case. */
6933 if (REG_P (dest))
6935 start_sequence ();
6936 emit_move_insn (cheap, copy_rtx (dest));
6937 restore = get_insns ();
6938 end_sequence ();
6939 lra_process_new_insns (curr_insn, NULL, restore,
6940 "Inserting call parameter restore");
6941 /* We don't need to save/restore of the pseudo from
6942 this call. */
6943 usage_insns[regno].calls_num = calls_num;
6944 remove_from_hard_reg_set
6945 (&full_and_partial_call_clobbers,
6946 GET_MODE (cheap), hard_regno);
6947 bitmap_set_bit (&check_only_regs, regno);
6951 to_inherit_num = 0;
6952 /* Process insn usages. */
6953 for (iter = 0; iter < 2; iter++)
6954 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
6955 reg != NULL;
6956 reg = reg->next)
6957 if ((reg->type != OP_OUT
6958 || (reg->type == OP_OUT && reg->subreg_p))
6959 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
6961 if (src_regno >= FIRST_PSEUDO_REGISTER
6962 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
6964 if (usage_insns[src_regno].check == curr_usage_insns_check
6965 && (next_usage_insns
6966 = usage_insns[src_regno].insns) != NULL_RTX
6967 && NONDEBUG_INSN_P (curr_insn))
6968 add_to_inherit (src_regno, next_usage_insns);
6969 else if (usage_insns[src_regno].check
6970 != -(int) INSN_UID (curr_insn))
6971 /* Add usages but only if the reg is not set up
6972 in the same insn. */
6973 add_next_usage_insn (src_regno, curr_insn, reloads_num);
6975 else if (src_regno < FIRST_PSEUDO_REGISTER
6976 || reg_renumber[src_regno] >= 0)
6978 bool before_p;
6979 rtx_insn *use_insn = curr_insn;
6981 before_p = (JUMP_P (curr_insn)
6982 || (CALL_P (curr_insn) && reg->type == OP_IN));
6983 if (NONDEBUG_INSN_P (curr_insn)
6984 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
6985 && split_if_necessary (src_regno, reg->biggest_mode,
6986 potential_reload_hard_regs,
6987 before_p, curr_insn, max_uid))
6989 if (reg->subreg_p)
6990 check_and_force_assignment_correctness_p = true;
6991 change_p = true;
6992 /* Invalidate. */
6993 usage_insns[src_regno].check = 0;
6994 if (before_p)
6995 use_insn = PREV_INSN (curr_insn);
6997 if (NONDEBUG_INSN_P (curr_insn))
6999 if (src_regno < FIRST_PSEUDO_REGISTER)
7000 add_to_hard_reg_set (&live_hard_regs,
7001 reg->biggest_mode, src_regno);
7002 else
7003 add_to_hard_reg_set (&live_hard_regs,
7004 PSEUDO_REGNO_MODE (src_regno),
7005 reg_renumber[src_regno]);
7007 if (src_regno >= FIRST_PSEUDO_REGISTER)
7008 add_next_usage_insn (src_regno, use_insn, reloads_num);
7009 else
7011 for (i = 0; i < hard_regno_nregs (src_regno, reg->biggest_mode); i++)
7012 add_next_usage_insn (src_regno + i, use_insn, reloads_num);
7016 /* Process used call regs. */
7017 if (curr_id->arg_hard_regs != NULL)
7018 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
7019 if (src_regno < FIRST_PSEUDO_REGISTER)
7021 SET_HARD_REG_BIT (live_hard_regs, src_regno);
7022 add_next_usage_insn (src_regno, curr_insn, reloads_num);
7024 for (i = 0; i < to_inherit_num; i++)
7026 src_regno = to_inherit[i].regno;
7027 if (inherit_reload_reg (false, src_regno, ALL_REGS,
7028 curr_insn, to_inherit[i].insns))
7029 change_p = true;
7030 else
7031 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
7034 if (update_reloads_num_p
7035 && NONDEBUG_INSN_P (curr_insn) && curr_set != NULL_RTX)
7037 int regno = -1;
7038 if ((REG_P (SET_DEST (curr_set))
7039 && (regno = REGNO (SET_DEST (curr_set))) >= lra_constraint_new_regno_start
7040 && reg_renumber[regno] < 0
7041 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
7042 || (REG_P (SET_SRC (curr_set))
7043 && (regno = REGNO (SET_SRC (curr_set))) >= lra_constraint_new_regno_start
7044 && reg_renumber[regno] < 0
7045 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
7047 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
7048 reloads_num++;
7049 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
7050 potential_reload_hard_regs |= reg_class_contents[cl];
7053 if (NONDEBUG_INSN_P (curr_insn))
7055 int regno;
7057 /* Invalidate invariants with changed regs. */
7058 curr_id = lra_get_insn_recog_data (curr_insn);
7059 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
7060 if (reg->type != OP_IN)
7062 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
7063 bitmap_set_bit (&invalid_invariant_regs,
7064 ORIGINAL_REGNO (regno_reg_rtx[reg->regno]));
7066 curr_static_id = curr_id->insn_static_data;
7067 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
7068 if (reg->type != OP_IN)
7069 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
7070 if (curr_id->arg_hard_regs != NULL)
7071 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
7072 if (regno >= FIRST_PSEUDO_REGISTER)
7073 bitmap_set_bit (&invalid_invariant_regs,
7074 regno - FIRST_PSEUDO_REGISTER);
7076 /* We reached the start of the current basic block. */
7077 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
7078 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
7080 /* We reached the beginning of the current block -- do
7081 rest of spliting in the current BB. */
7082 to_process = df_get_live_in (curr_bb);
7083 if (BLOCK_FOR_INSN (head) != curr_bb)
7085 /* We are somewhere in the middle of EBB. */
7086 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
7087 curr_bb, &temp_bitmap);
7088 to_process = &temp_bitmap;
7090 head_p = true;
7091 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
7093 if ((int) j >= lra_constraint_new_regno_start)
7094 break;
7095 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
7096 && usage_insns[j].check == curr_usage_insns_check
7097 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
7099 if (need_for_split_p (potential_reload_hard_regs, j))
7101 if (lra_dump_file != NULL && head_p)
7103 fprintf (lra_dump_file,
7104 " ----------------------------------\n");
7105 head_p = false;
7107 if (split_reg (false, j, bb_note (curr_bb),
7108 next_usage_insns, NULL))
7109 change_p = true;
7111 usage_insns[j].check = 0;
7116 return change_p;
7119 /* This value affects EBB forming. If probability of edge from EBB to
7120 a BB is not greater than the following value, we don't add the BB
7121 to EBB. */
7122 #define EBB_PROBABILITY_CUTOFF \
7123 ((REG_BR_PROB_BASE * param_lra_inheritance_ebb_probability_cutoff) / 100)
7125 /* Current number of inheritance/split iteration. */
7126 int lra_inheritance_iter;
7128 /* Entry function for inheritance/split pass. */
7129 void
7130 lra_inheritance (void)
7132 int i;
7133 basic_block bb, start_bb;
7134 edge e;
7136 lra_inheritance_iter++;
7137 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
7138 return;
7139 timevar_push (TV_LRA_INHERITANCE);
7140 if (lra_dump_file != NULL)
7141 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
7142 lra_inheritance_iter);
7143 curr_usage_insns_check = 0;
7144 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
7145 for (i = 0; i < lra_constraint_new_regno_start; i++)
7146 usage_insns[i].check = 0;
7147 bitmap_initialize (&check_only_regs, &reg_obstack);
7148 bitmap_initialize (&invalid_invariant_regs, &reg_obstack);
7149 bitmap_initialize (&live_regs, &reg_obstack);
7150 bitmap_initialize (&temp_bitmap, &reg_obstack);
7151 bitmap_initialize (&ebb_global_regs, &reg_obstack);
7152 FOR_EACH_BB_FN (bb, cfun)
7154 start_bb = bb;
7155 if (lra_dump_file != NULL)
7156 fprintf (lra_dump_file, "EBB");
7157 /* Form a EBB starting with BB. */
7158 bitmap_clear (&ebb_global_regs);
7159 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
7160 for (;;)
7162 if (lra_dump_file != NULL)
7163 fprintf (lra_dump_file, " %d", bb->index);
7164 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
7165 || LABEL_P (BB_HEAD (bb->next_bb)))
7166 break;
7167 e = find_fallthru_edge (bb->succs);
7168 if (! e)
7169 break;
7170 if (e->probability.initialized_p ()
7171 && e->probability.to_reg_br_prob_base () < EBB_PROBABILITY_CUTOFF)
7172 break;
7173 bb = bb->next_bb;
7175 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
7176 if (lra_dump_file != NULL)
7177 fprintf (lra_dump_file, "\n");
7178 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
7179 /* Remember that the EBB head and tail can change in
7180 inherit_in_ebb. */
7181 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
7183 bitmap_release (&ebb_global_regs);
7184 bitmap_release (&temp_bitmap);
7185 bitmap_release (&live_regs);
7186 bitmap_release (&invalid_invariant_regs);
7187 bitmap_release (&check_only_regs);
7188 free (usage_insns);
7189 lra_dump_insns_if_possible ("func after inheritance");
7190 timevar_pop (TV_LRA_INHERITANCE);
7195 /* This page contains code to undo failed inheritance/split
7196 transformations. */
7198 /* Current number of iteration undoing inheritance/split. */
7199 int lra_undo_inheritance_iter;
7201 /* Fix BB live info LIVE after removing pseudos created on pass doing
7202 inheritance/split which are REMOVED_PSEUDOS. */
7203 static void
7204 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
7206 unsigned int regno;
7207 bitmap_iterator bi;
7209 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
7210 if (bitmap_clear_bit (live, regno)
7211 && REG_P (lra_reg_info[regno].restore_rtx))
7212 bitmap_set_bit (live, REGNO (lra_reg_info[regno].restore_rtx));
7215 /* Return regno of the (subreg of) REG. Otherwise, return a negative
7216 number. */
7217 static int
7218 get_regno (rtx reg)
7220 if (GET_CODE (reg) == SUBREG)
7221 reg = SUBREG_REG (reg);
7222 if (REG_P (reg))
7223 return REGNO (reg);
7224 return -1;
7227 /* Delete a move INSN with destination reg DREGNO and a previous
7228 clobber insn with the same regno. The inheritance/split code can
7229 generate moves with preceding clobber and when we delete such moves
7230 we should delete the clobber insn too to keep the correct life
7231 info. */
7232 static void
7233 delete_move_and_clobber (rtx_insn *insn, int dregno)
7235 rtx_insn *prev_insn = PREV_INSN (insn);
7237 lra_set_insn_deleted (insn);
7238 lra_assert (dregno >= 0);
7239 if (prev_insn != NULL && NONDEBUG_INSN_P (prev_insn)
7240 && GET_CODE (PATTERN (prev_insn)) == CLOBBER
7241 && dregno == get_regno (XEXP (PATTERN (prev_insn), 0)))
7242 lra_set_insn_deleted (prev_insn);
7245 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
7246 return true if we did any change. The undo transformations for
7247 inheritance looks like
7248 i <- i2
7249 p <- i => p <- i2
7250 or removing
7251 p <- i, i <- p, and i <- i3
7252 where p is original pseudo from which inheritance pseudo i was
7253 created, i and i3 are removed inheritance pseudos, i2 is another
7254 not removed inheritance pseudo. All split pseudos or other
7255 occurrences of removed inheritance pseudos are changed on the
7256 corresponding original pseudos.
7258 The function also schedules insns changed and created during
7259 inheritance/split pass for processing by the subsequent constraint
7260 pass. */
7261 static bool
7262 remove_inheritance_pseudos (bitmap remove_pseudos)
7264 basic_block bb;
7265 int regno, sregno, prev_sregno, dregno;
7266 rtx restore_rtx;
7267 rtx set, prev_set;
7268 rtx_insn *prev_insn;
7269 bool change_p, done_p;
7271 change_p = ! bitmap_empty_p (remove_pseudos);
7272 /* We cannot finish the function right away if CHANGE_P is true
7273 because we need to marks insns affected by previous
7274 inheritance/split pass for processing by the subsequent
7275 constraint pass. */
7276 FOR_EACH_BB_FN (bb, cfun)
7278 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
7279 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
7280 FOR_BB_INSNS_REVERSE (bb, curr_insn)
7282 if (! INSN_P (curr_insn))
7283 continue;
7284 done_p = false;
7285 sregno = dregno = -1;
7286 if (change_p && NONDEBUG_INSN_P (curr_insn)
7287 && (set = single_set (curr_insn)) != NULL_RTX)
7289 dregno = get_regno (SET_DEST (set));
7290 sregno = get_regno (SET_SRC (set));
7293 if (sregno >= 0 && dregno >= 0)
7295 if (bitmap_bit_p (remove_pseudos, dregno)
7296 && ! REG_P (lra_reg_info[dregno].restore_rtx))
7298 /* invariant inheritance pseudo <- original pseudo */
7299 if (lra_dump_file != NULL)
7301 fprintf (lra_dump_file, " Removing invariant inheritance:\n");
7302 dump_insn_slim (lra_dump_file, curr_insn);
7303 fprintf (lra_dump_file, "\n");
7305 delete_move_and_clobber (curr_insn, dregno);
7306 done_p = true;
7308 else if (bitmap_bit_p (remove_pseudos, sregno)
7309 && ! REG_P (lra_reg_info[sregno].restore_rtx))
7311 /* reload pseudo <- invariant inheritance pseudo */
7312 start_sequence ();
7313 /* We cannot just change the source. It might be
7314 an insn different from the move. */
7315 emit_insn (lra_reg_info[sregno].restore_rtx);
7316 rtx_insn *new_insns = get_insns ();
7317 end_sequence ();
7318 lra_assert (single_set (new_insns) != NULL
7319 && SET_DEST (set) == SET_DEST (single_set (new_insns)));
7320 lra_process_new_insns (curr_insn, NULL, new_insns,
7321 "Changing reload<-invariant inheritance");
7322 delete_move_and_clobber (curr_insn, dregno);
7323 done_p = true;
7325 else if ((bitmap_bit_p (remove_pseudos, sregno)
7326 && (get_regno (lra_reg_info[sregno].restore_rtx) == dregno
7327 || (bitmap_bit_p (remove_pseudos, dregno)
7328 && get_regno (lra_reg_info[sregno].restore_rtx) >= 0
7329 && (get_regno (lra_reg_info[sregno].restore_rtx)
7330 == get_regno (lra_reg_info[dregno].restore_rtx)))))
7331 || (bitmap_bit_p (remove_pseudos, dregno)
7332 && get_regno (lra_reg_info[dregno].restore_rtx) == sregno))
7333 /* One of the following cases:
7334 original <- removed inheritance pseudo
7335 removed inherit pseudo <- another removed inherit pseudo
7336 removed inherit pseudo <- original pseudo
7338 removed_split_pseudo <- original_reg
7339 original_reg <- removed_split_pseudo */
7341 if (lra_dump_file != NULL)
7343 fprintf (lra_dump_file, " Removing %s:\n",
7344 bitmap_bit_p (&lra_split_regs, sregno)
7345 || bitmap_bit_p (&lra_split_regs, dregno)
7346 ? "split" : "inheritance");
7347 dump_insn_slim (lra_dump_file, curr_insn);
7349 delete_move_and_clobber (curr_insn, dregno);
7350 done_p = true;
7352 else if (bitmap_bit_p (remove_pseudos, sregno)
7353 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
7355 /* Search the following pattern:
7356 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
7357 original_pseudo <- inherit_or_split_pseudo1
7358 where the 2nd insn is the current insn and
7359 inherit_or_split_pseudo2 is not removed. If it is found,
7360 change the current insn onto:
7361 original_pseudo <- inherit_or_split_pseudo2. */
7362 for (prev_insn = PREV_INSN (curr_insn);
7363 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
7364 prev_insn = PREV_INSN (prev_insn))
7366 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
7367 && (prev_set = single_set (prev_insn)) != NULL_RTX
7368 /* There should be no subregs in insn we are
7369 searching because only the original reg might
7370 be in subreg when we changed the mode of
7371 load/store for splitting. */
7372 && REG_P (SET_DEST (prev_set))
7373 && REG_P (SET_SRC (prev_set))
7374 && (int) REGNO (SET_DEST (prev_set)) == sregno
7375 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
7376 >= FIRST_PSEUDO_REGISTER)
7377 && (lra_reg_info[prev_sregno].restore_rtx == NULL_RTX
7379 /* As we consider chain of inheritance or
7380 splitting described in above comment we should
7381 check that sregno and prev_sregno were
7382 inheritance/split pseudos created from the
7383 same original regno. */
7384 (get_regno (lra_reg_info[sregno].restore_rtx) >= 0
7385 && (get_regno (lra_reg_info[sregno].restore_rtx)
7386 == get_regno (lra_reg_info[prev_sregno].restore_rtx))))
7387 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
7389 int restore_regno = get_regno (lra_reg_info[sregno].restore_rtx);
7390 if (restore_regno < 0)
7391 restore_regno = prev_sregno;
7392 lra_assert (GET_MODE (SET_SRC (prev_set))
7393 == GET_MODE (regno_reg_rtx[restore_regno]));
7394 /* Although we have a single set, the insn can
7395 contain more one sregno register occurrence
7396 as a source. Change all occurrences. */
7397 lra_substitute_pseudo_within_insn (curr_insn, sregno,
7398 regno_reg_rtx[restore_regno],
7399 false);
7400 /* As we are finishing with processing the insn
7401 here, check the destination too as it might
7402 inheritance pseudo for another pseudo. */
7403 if (bitmap_bit_p (remove_pseudos, dregno)
7404 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
7405 && (restore_rtx
7406 = lra_reg_info[dregno].restore_rtx) != NULL_RTX)
7408 if (GET_CODE (SET_DEST (set)) == SUBREG)
7409 SUBREG_REG (SET_DEST (set)) = restore_rtx;
7410 else
7411 SET_DEST (set) = restore_rtx;
7413 lra_push_insn_and_update_insn_regno_info (curr_insn);
7414 lra_set_used_insn_alternative_by_uid
7415 (INSN_UID (curr_insn), LRA_UNKNOWN_ALT);
7416 done_p = true;
7417 if (lra_dump_file != NULL)
7419 fprintf (lra_dump_file, " Change reload insn:\n");
7420 dump_insn_slim (lra_dump_file, curr_insn);
7425 if (! done_p)
7427 struct lra_insn_reg *reg;
7428 bool restored_regs_p = false;
7429 bool kept_regs_p = false;
7431 curr_id = lra_get_insn_recog_data (curr_insn);
7432 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
7434 regno = reg->regno;
7435 restore_rtx = lra_reg_info[regno].restore_rtx;
7436 if (restore_rtx != NULL_RTX)
7438 if (change_p && bitmap_bit_p (remove_pseudos, regno))
7440 lra_substitute_pseudo_within_insn
7441 (curr_insn, regno, restore_rtx, false);
7442 restored_regs_p = true;
7444 else
7445 kept_regs_p = true;
7448 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
7450 /* The instruction has changed since the previous
7451 constraints pass. */
7452 lra_push_insn_and_update_insn_regno_info (curr_insn);
7453 lra_set_used_insn_alternative_by_uid
7454 (INSN_UID (curr_insn), LRA_UNKNOWN_ALT);
7456 else if (restored_regs_p)
7457 /* The instruction has been restored to the form that
7458 it had during the previous constraints pass. */
7459 lra_update_insn_regno_info (curr_insn);
7460 if (restored_regs_p && lra_dump_file != NULL)
7462 fprintf (lra_dump_file, " Insn after restoring regs:\n");
7463 dump_insn_slim (lra_dump_file, curr_insn);
7468 return change_p;
7471 /* If optional reload pseudos failed to get a hard register or was not
7472 inherited, it is better to remove optional reloads. We do this
7473 transformation after undoing inheritance to figure out necessity to
7474 remove optional reloads easier. Return true if we do any
7475 change. */
7476 static bool
7477 undo_optional_reloads (void)
7479 bool change_p, keep_p;
7480 unsigned int regno, uid;
7481 bitmap_iterator bi, bi2;
7482 rtx_insn *insn;
7483 rtx set, src, dest;
7484 auto_bitmap removed_optional_reload_pseudos (&reg_obstack);
7486 bitmap_copy (removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
7487 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
7489 keep_p = false;
7490 /* Keep optional reloads from previous subpasses. */
7491 if (lra_reg_info[regno].restore_rtx == NULL_RTX
7492 /* If the original pseudo changed its allocation, just
7493 removing the optional pseudo is dangerous as the original
7494 pseudo will have longer live range. */
7495 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] >= 0)
7496 keep_p = true;
7497 else if (reg_renumber[regno] >= 0)
7498 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
7500 insn = lra_insn_recog_data[uid]->insn;
7501 if ((set = single_set (insn)) == NULL_RTX)
7502 continue;
7503 src = SET_SRC (set);
7504 dest = SET_DEST (set);
7505 if ((! REG_P (src) && ! SUBREG_P (src))
7506 || (! REG_P (dest) && ! SUBREG_P (dest)))
7507 continue;
7508 if (get_regno (dest) == (int) regno
7509 /* Ignore insn for optional reloads itself. */
7510 && (get_regno (lra_reg_info[regno].restore_rtx)
7511 != get_regno (src))
7512 /* Check only inheritance on last inheritance pass. */
7513 && get_regno (src) >= new_regno_start
7514 /* Check that the optional reload was inherited. */
7515 && bitmap_bit_p (&lra_inheritance_pseudos, get_regno (src)))
7517 keep_p = true;
7518 break;
7521 if (keep_p)
7523 bitmap_clear_bit (removed_optional_reload_pseudos, regno);
7524 if (lra_dump_file != NULL)
7525 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
7528 change_p = ! bitmap_empty_p (removed_optional_reload_pseudos);
7529 auto_bitmap insn_bitmap (&reg_obstack);
7530 EXECUTE_IF_SET_IN_BITMAP (removed_optional_reload_pseudos, 0, regno, bi)
7532 if (lra_dump_file != NULL)
7533 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
7534 bitmap_copy (insn_bitmap, &lra_reg_info[regno].insn_bitmap);
7535 EXECUTE_IF_SET_IN_BITMAP (insn_bitmap, 0, uid, bi2)
7537 /* We may have already removed a clobber. */
7538 if (!lra_insn_recog_data[uid])
7539 continue;
7540 insn = lra_insn_recog_data[uid]->insn;
7541 if ((set = single_set (insn)) != NULL_RTX)
7543 src = SET_SRC (set);
7544 dest = SET_DEST (set);
7545 if ((REG_P (src) || SUBREG_P (src))
7546 && (REG_P (dest) || SUBREG_P (dest))
7547 && ((get_regno (src) == (int) regno
7548 && (get_regno (lra_reg_info[regno].restore_rtx)
7549 == get_regno (dest)))
7550 || (get_regno (dest) == (int) regno
7551 && (get_regno (lra_reg_info[regno].restore_rtx)
7552 == get_regno (src)))))
7554 if (lra_dump_file != NULL)
7556 fprintf (lra_dump_file, " Deleting move %u\n",
7557 INSN_UID (insn));
7558 dump_insn_slim (lra_dump_file, insn);
7560 delete_move_and_clobber (insn, get_regno (dest));
7561 continue;
7563 /* We should not worry about generation memory-memory
7564 moves here as if the corresponding inheritance did
7565 not work (inheritance pseudo did not get a hard reg),
7566 we remove the inheritance pseudo and the optional
7567 reload. */
7569 if (GET_CODE (PATTERN (insn)) == CLOBBER
7570 && REG_P (SET_DEST (insn))
7571 && get_regno (SET_DEST (insn)) == (int) regno)
7572 /* Refuse to remap clobbers to preexisting pseudos. */
7573 gcc_unreachable ();
7574 lra_substitute_pseudo_within_insn
7575 (insn, regno, lra_reg_info[regno].restore_rtx, false);
7576 lra_update_insn_regno_info (insn);
7577 if (lra_dump_file != NULL)
7579 fprintf (lra_dump_file,
7580 " Restoring original insn:\n");
7581 dump_insn_slim (lra_dump_file, insn);
7585 /* Clear restore_regnos. */
7586 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
7587 lra_reg_info[regno].restore_rtx = NULL_RTX;
7588 return change_p;
7591 /* Entry function for undoing inheritance/split transformation. Return true
7592 if we did any RTL change in this pass. */
7593 bool
7594 lra_undo_inheritance (void)
7596 unsigned int regno;
7597 int hard_regno;
7598 int n_all_inherit, n_inherit, n_all_split, n_split;
7599 rtx restore_rtx;
7600 bitmap_iterator bi;
7601 bool change_p;
7603 lra_undo_inheritance_iter++;
7604 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
7605 return false;
7606 if (lra_dump_file != NULL)
7607 fprintf (lra_dump_file,
7608 "\n********** Undoing inheritance #%d: **********\n\n",
7609 lra_undo_inheritance_iter);
7610 auto_bitmap remove_pseudos (&reg_obstack);
7611 n_inherit = n_all_inherit = 0;
7612 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
7613 if (lra_reg_info[regno].restore_rtx != NULL_RTX)
7615 n_all_inherit++;
7616 if (reg_renumber[regno] < 0
7617 /* If the original pseudo changed its allocation, just
7618 removing inheritance is dangerous as for changing
7619 allocation we used shorter live-ranges. */
7620 && (! REG_P (lra_reg_info[regno].restore_rtx)
7621 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] < 0))
7622 bitmap_set_bit (remove_pseudos, regno);
7623 else
7624 n_inherit++;
7626 if (lra_dump_file != NULL && n_all_inherit != 0)
7627 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
7628 n_inherit, n_all_inherit,
7629 (double) n_inherit / n_all_inherit * 100);
7630 n_split = n_all_split = 0;
7631 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
7632 if ((restore_rtx = lra_reg_info[regno].restore_rtx) != NULL_RTX)
7634 int restore_regno = REGNO (restore_rtx);
7636 n_all_split++;
7637 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
7638 ? reg_renumber[restore_regno] : restore_regno);
7639 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
7640 bitmap_set_bit (remove_pseudos, regno);
7641 else
7643 n_split++;
7644 if (lra_dump_file != NULL)
7645 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
7646 regno, restore_regno);
7649 if (lra_dump_file != NULL && n_all_split != 0)
7650 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
7651 n_split, n_all_split,
7652 (double) n_split / n_all_split * 100);
7653 change_p = remove_inheritance_pseudos (remove_pseudos);
7654 /* Clear restore_regnos. */
7655 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
7656 lra_reg_info[regno].restore_rtx = NULL_RTX;
7657 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
7658 lra_reg_info[regno].restore_rtx = NULL_RTX;
7659 change_p = undo_optional_reloads () || change_p;
7660 if (change_p)
7661 lra_dump_insns_if_possible ("changed func after undoing inheritance");
7662 return change_p;