c++: Implement __is_member_function_pointer built-in trait
[official-gcc.git] / gcc / lra-constraints.cc
blob177c765ca1333e3a39f96b0f17db97e57c1fd762
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 /* Return true if REG satisfies (or will satisfy) reg class constraint
265 CL. Use elimination first if REG is a hard register. If REG is a
266 reload pseudo created by this constraints pass, assume that it will
267 be allocated a hard register from its allocno class, but allow that
268 class to be narrowed to CL if it is currently a superset of CL and
269 if either:
271 - ALLOW_ALL_RELOAD_CLASS_CHANGES_P is true or
272 - the instruction we're processing is not a reload move.
274 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
275 REGNO (reg), or NO_REGS if no change in its class was needed. */
276 static bool
277 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class,
278 bool allow_all_reload_class_changes_p = false)
280 enum reg_class rclass, common_class;
281 machine_mode reg_mode;
282 rtx src;
283 int regno = REGNO (reg);
285 if (new_class != NULL)
286 *new_class = NO_REGS;
287 if (regno < FIRST_PSEUDO_REGISTER)
289 rtx final_reg = reg;
290 rtx *final_loc = &final_reg;
292 lra_eliminate_reg_if_possible (final_loc);
293 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
295 reg_mode = GET_MODE (reg);
296 rclass = get_reg_class (regno);
297 src = curr_insn_set != NULL ? SET_SRC (curr_insn_set) : NULL;
298 if (regno < new_regno_start
299 /* Do not allow the constraints for reload instructions to
300 influence the classes of new pseudos. These reloads are
301 typically moves that have many alternatives, and restricting
302 reload pseudos for one alternative may lead to situations
303 where other reload pseudos are no longer allocatable. */
304 || (!allow_all_reload_class_changes_p
305 && INSN_UID (curr_insn) >= new_insn_uid_start
306 && src != NULL
307 && ((REG_P (src) || MEM_P (src))
308 || (GET_CODE (src) == SUBREG
309 && (REG_P (SUBREG_REG (src)) || MEM_P (SUBREG_REG (src)))))))
310 /* When we don't know what class will be used finally for reload
311 pseudos, we use ALL_REGS. */
312 return ((regno >= new_regno_start && rclass == ALL_REGS)
313 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
314 && ! hard_reg_set_subset_p (reg_class_contents[cl],
315 lra_no_alloc_regs)));
316 else
318 common_class = ira_reg_class_subset[rclass][cl];
319 if (new_class != NULL)
320 *new_class = common_class;
321 return enough_allocatable_hard_regs_p (common_class, reg_mode);
325 /* Return true if REGNO satisfies a memory constraint. */
326 static bool
327 in_mem_p (int regno)
329 return get_reg_class (regno) == NO_REGS;
332 /* Return true if ADDR is a valid memory address for mode MODE in address
333 space AS, and check that each pseudo has the proper kind of hard
334 reg. */
335 static bool
336 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
337 rtx addr, addr_space_t as)
339 #ifdef GO_IF_LEGITIMATE_ADDRESS
340 lra_assert (ADDR_SPACE_GENERIC_P (as));
341 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
342 return false;
344 win:
345 return true;
346 #else
347 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as,
348 ERROR_MARK);
349 #endif
352 namespace {
353 /* Temporarily eliminates registers in an address (for the lifetime of
354 the object). */
355 class address_eliminator {
356 public:
357 address_eliminator (struct address_info *ad);
358 ~address_eliminator ();
360 private:
361 struct address_info *m_ad;
362 rtx *m_base_loc;
363 rtx m_base_reg;
364 rtx *m_index_loc;
365 rtx m_index_reg;
369 address_eliminator::address_eliminator (struct address_info *ad)
370 : m_ad (ad),
371 m_base_loc (strip_subreg (ad->base_term)),
372 m_base_reg (NULL_RTX),
373 m_index_loc (strip_subreg (ad->index_term)),
374 m_index_reg (NULL_RTX)
376 if (m_base_loc != NULL)
378 m_base_reg = *m_base_loc;
379 /* If we have non-legitimate address which is decomposed not in
380 the way we expected, don't do elimination here. In such case
381 the address will be reloaded and elimination will be done in
382 reload insn finally. */
383 if (REG_P (m_base_reg))
384 lra_eliminate_reg_if_possible (m_base_loc);
385 if (m_ad->base_term2 != NULL)
386 *m_ad->base_term2 = *m_ad->base_term;
388 if (m_index_loc != NULL)
390 m_index_reg = *m_index_loc;
391 if (REG_P (m_index_reg))
392 lra_eliminate_reg_if_possible (m_index_loc);
396 address_eliminator::~address_eliminator ()
398 if (m_base_loc && *m_base_loc != m_base_reg)
400 *m_base_loc = m_base_reg;
401 if (m_ad->base_term2 != NULL)
402 *m_ad->base_term2 = *m_ad->base_term;
404 if (m_index_loc && *m_index_loc != m_index_reg)
405 *m_index_loc = m_index_reg;
408 /* Return true if the eliminated form of AD is a legitimate target address.
409 If OP is a MEM, AD is the address within OP, otherwise OP should be
410 ignored. CONSTRAINT is one constraint that the operand may need
411 to meet. */
412 static bool
413 valid_address_p (rtx op, struct address_info *ad,
414 enum constraint_num constraint)
416 address_eliminator eliminator (ad);
418 /* Allow a memory OP if it matches CONSTRAINT, even if CONSTRAINT is more
419 forgiving than "m".
420 Need to extract memory from op for special memory constraint,
421 i.e. bcst_mem_operand in i386 backend. */
422 if (MEM_P (extract_mem_from_operand (op))
423 && insn_extra_relaxed_memory_constraint (constraint)
424 && constraint_satisfied_p (op, constraint))
425 return true;
427 return valid_address_p (ad->mode, *ad->outer, ad->as);
430 /* For special_memory_operand, it could be false for MEM_P (op),
431 i.e. bcst_mem_operand in i386 backend.
432 Extract and return real memory operand or op. */
434 extract_mem_from_operand (rtx op)
436 for (rtx x = op;; x = XEXP (x, 0))
438 if (MEM_P (x))
439 return x;
440 if (GET_RTX_LENGTH (GET_CODE (x)) != 1
441 || GET_RTX_FORMAT (GET_CODE (x))[0] != 'e')
442 break;
444 return op;
447 /* Return true if the eliminated form of memory reference OP satisfies
448 extra (special) memory constraint CONSTRAINT. */
449 static bool
450 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
452 struct address_info ad;
453 rtx mem = extract_mem_from_operand (op);
454 if (!MEM_P (mem))
455 return false;
457 decompose_mem_address (&ad, mem);
458 address_eliminator eliminator (&ad);
459 return constraint_satisfied_p (op, constraint);
462 /* Return true if the eliminated form of address AD satisfies extra
463 address constraint CONSTRAINT. */
464 static bool
465 satisfies_address_constraint_p (struct address_info *ad,
466 enum constraint_num constraint)
468 address_eliminator eliminator (ad);
469 return constraint_satisfied_p (*ad->outer, constraint);
472 /* Return true if the eliminated form of address OP satisfies extra
473 address constraint CONSTRAINT. */
474 static bool
475 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
477 struct address_info ad;
479 decompose_lea_address (&ad, &op);
480 return satisfies_address_constraint_p (&ad, constraint);
483 /* Initiate equivalences for LRA. As we keep original equivalences
484 before any elimination, we need to make copies otherwise any change
485 in insns might change the equivalences. */
486 void
487 lra_init_equiv (void)
489 ira_expand_reg_equiv ();
490 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
492 rtx res;
494 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
495 ira_reg_equiv[i].memory = copy_rtx (res);
496 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
497 ira_reg_equiv[i].invariant = copy_rtx (res);
501 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
503 /* Update equivalence for REGNO. We need to this as the equivalence
504 might contain other pseudos which are changed by their
505 equivalences. */
506 static void
507 update_equiv (int regno)
509 rtx x;
511 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
512 ira_reg_equiv[regno].memory
513 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
514 NULL_RTX);
515 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
516 ira_reg_equiv[regno].invariant
517 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
518 NULL_RTX);
521 /* If we have decided to substitute X with another value, return that
522 value, otherwise return X. */
523 static rtx
524 get_equiv (rtx x)
526 int regno;
527 rtx res;
529 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
530 || ! ira_reg_equiv[regno].defined_p
531 || ! ira_reg_equiv[regno].profitable_p
532 || lra_get_regno_hard_regno (regno) >= 0)
533 return x;
534 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
536 if (targetm.cannot_substitute_mem_equiv_p (res))
537 return x;
538 return res;
540 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
541 return res;
542 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
543 return res;
544 gcc_unreachable ();
547 /* If we have decided to substitute X with the equivalent value,
548 return that value after elimination for INSN, otherwise return
549 X. */
550 static rtx
551 get_equiv_with_elimination (rtx x, rtx_insn *insn)
553 rtx res = get_equiv (x);
555 if (x == res || CONSTANT_P (res))
556 return res;
557 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
558 false, false, 0, true);
561 /* Set up curr_operand_mode. */
562 static void
563 init_curr_operand_mode (void)
565 int nop = curr_static_id->n_operands;
566 for (int i = 0; i < nop; i++)
568 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
569 if (mode == VOIDmode)
571 /* The .md mode for address operands is the mode of the
572 addressed value rather than the mode of the address itself. */
573 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
574 mode = Pmode;
575 else
576 mode = curr_static_id->operand[i].mode;
578 curr_operand_mode[i] = mode;
584 /* The page contains code to reuse input reloads. */
586 /* Structure describes input reload of the current insns. */
587 struct input_reload
589 /* True for input reload of matched operands. */
590 bool match_p;
591 /* Reloaded value. */
592 rtx input;
593 /* Reload pseudo used. */
594 rtx reg;
597 /* The number of elements in the following array. */
598 static int curr_insn_input_reloads_num;
599 /* Array containing info about input reloads. It is used to find the
600 same input reload and reuse the reload pseudo in this case. */
601 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
603 /* Initiate data concerning reuse of input reloads for the current
604 insn. */
605 static void
606 init_curr_insn_input_reloads (void)
608 curr_insn_input_reloads_num = 0;
611 /* The canonical form of an rtx inside a MEM is not necessarily the same as the
612 canonical form of the rtx outside the MEM. Fix this up in the case that
613 we're reloading an address (and therefore pulling it outside a MEM). */
614 static rtx
615 canonicalize_reload_addr (rtx addr)
617 subrtx_var_iterator::array_type array;
618 FOR_EACH_SUBRTX_VAR (iter, array, addr, NONCONST)
620 rtx x = *iter;
621 if (GET_CODE (x) == MULT && CONST_INT_P (XEXP (x, 1)))
623 const HOST_WIDE_INT ci = INTVAL (XEXP (x, 1));
624 const int pwr2 = exact_log2 (ci);
625 if (pwr2 > 0)
627 /* Rewrite this to use a shift instead, which is canonical when
628 outside of a MEM. */
629 PUT_CODE (x, ASHIFT);
630 XEXP (x, 1) = GEN_INT (pwr2);
635 return addr;
638 /* Create a new pseudo using MODE, RCLASS, EXCLUDE_START_HARD_REGS, ORIGINAL or
639 reuse an existing reload pseudo. Don't reuse an existing reload pseudo if
640 IN_SUBREG_P is true and the reused pseudo should be wrapped up in a SUBREG.
641 The result pseudo is returned through RESULT_REG. Return TRUE if we created
642 a new pseudo, FALSE if we reused an existing reload pseudo. Use TITLE to
643 describe new registers for debug purposes. */
644 static bool
645 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
646 enum reg_class rclass, HARD_REG_SET *exclude_start_hard_regs,
647 bool in_subreg_p, const char *title, rtx *result_reg)
649 int i, regno;
650 enum reg_class new_class;
651 bool unique_p = false;
653 if (type == OP_OUT)
655 /* Output reload registers tend to start out with a conservative
656 choice of register class. Usually this is ALL_REGS, although
657 a target might narrow it (for performance reasons) through
658 targetm.preferred_reload_class. It's therefore quite common
659 for a reload instruction to require a more restrictive class
660 than the class that was originally assigned to the reload register.
662 In these situations, it's more efficient to refine the choice
663 of register class rather than create a second reload register.
664 This also helps to avoid cycling for registers that are only
665 used by reload instructions. */
666 if (REG_P (original)
667 && (int) REGNO (original) >= new_regno_start
668 && INSN_UID (curr_insn) >= new_insn_uid_start
669 && in_class_p (original, rclass, &new_class, true))
671 unsigned int regno = REGNO (original);
672 if (lra_dump_file != NULL)
674 fprintf (lra_dump_file, " Reuse r%d for output ", regno);
675 dump_value_slim (lra_dump_file, original, 1);
677 if (new_class != lra_get_allocno_class (regno))
678 lra_change_class (regno, new_class, ", change to", false);
679 if (lra_dump_file != NULL)
680 fprintf (lra_dump_file, "\n");
681 *result_reg = original;
682 return false;
684 *result_reg
685 = lra_create_new_reg_with_unique_value (mode, original, rclass,
686 exclude_start_hard_regs, title);
687 return true;
689 /* Prevent reuse value of expression with side effects,
690 e.g. volatile memory. */
691 if (! side_effects_p (original))
692 for (i = 0; i < curr_insn_input_reloads_num; i++)
694 if (! curr_insn_input_reloads[i].match_p
695 && rtx_equal_p (curr_insn_input_reloads[i].input, original)
696 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
698 rtx reg = curr_insn_input_reloads[i].reg;
699 regno = REGNO (reg);
700 /* If input is equal to original and both are VOIDmode,
701 GET_MODE (reg) might be still different from mode.
702 Ensure we don't return *result_reg with wrong mode. */
703 if (GET_MODE (reg) != mode)
705 if (in_subreg_p)
706 continue;
707 if (maybe_lt (GET_MODE_SIZE (GET_MODE (reg)),
708 GET_MODE_SIZE (mode)))
709 continue;
710 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
711 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
712 continue;
714 *result_reg = reg;
715 if (lra_dump_file != NULL)
717 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
718 dump_value_slim (lra_dump_file, original, 1);
720 if (new_class != lra_get_allocno_class (regno))
721 lra_change_class (regno, new_class, ", change to", false);
722 if (lra_dump_file != NULL)
723 fprintf (lra_dump_file, "\n");
724 return false;
726 /* If we have an input reload with a different mode, make sure it
727 will get a different hard reg. */
728 else if (REG_P (original)
729 && REG_P (curr_insn_input_reloads[i].input)
730 && REGNO (original) == REGNO (curr_insn_input_reloads[i].input)
731 && (GET_MODE (original)
732 != GET_MODE (curr_insn_input_reloads[i].input)))
733 unique_p = true;
735 *result_reg = (unique_p
736 ? lra_create_new_reg_with_unique_value
737 : lra_create_new_reg) (mode, original, rclass,
738 exclude_start_hard_regs, title);
739 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
740 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
741 curr_insn_input_reloads[curr_insn_input_reloads_num].match_p = false;
742 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
743 return true;
747 /* The page contains major code to choose the current insn alternative
748 and generate reloads for it. */
750 /* Return the offset from REGNO of the least significant register
751 in (reg:MODE REGNO).
753 This function is used to tell whether two registers satisfy
754 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
756 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
757 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
759 lra_constraint_offset (int regno, machine_mode mode)
761 lra_assert (regno < FIRST_PSEUDO_REGISTER);
763 scalar_int_mode int_mode;
764 if (WORDS_BIG_ENDIAN
765 && is_a <scalar_int_mode> (mode, &int_mode)
766 && GET_MODE_SIZE (int_mode) > UNITS_PER_WORD)
767 return hard_regno_nregs (regno, mode) - 1;
768 return 0;
771 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
772 if they are the same hard reg, and has special hacks for
773 auto-increment and auto-decrement. This is specifically intended for
774 process_alt_operands to use in determining whether two operands
775 match. X is the operand whose number is the lower of the two.
777 It is supposed that X is the output operand and Y is the input
778 operand. Y_HARD_REGNO is the final hard regno of register Y or
779 register in subreg Y as we know it now. Otherwise, it is a
780 negative value. */
781 static bool
782 operands_match_p (rtx x, rtx y, int y_hard_regno)
784 int i;
785 RTX_CODE code = GET_CODE (x);
786 const char *fmt;
788 if (x == y)
789 return true;
790 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
791 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
793 int j;
795 i = get_hard_regno (x);
796 if (i < 0)
797 goto slow;
799 if ((j = y_hard_regno) < 0)
800 goto slow;
802 i += lra_constraint_offset (i, GET_MODE (x));
803 j += lra_constraint_offset (j, GET_MODE (y));
805 return i == j;
808 /* If two operands must match, because they are really a single
809 operand of an assembler insn, then two post-increments are invalid
810 because the assembler insn would increment only once. On the
811 other hand, a post-increment matches ordinary indexing if the
812 post-increment is the output operand. */
813 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
814 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
816 /* Two pre-increments are invalid because the assembler insn would
817 increment only once. On the other hand, a pre-increment matches
818 ordinary indexing if the pre-increment is the input operand. */
819 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
820 || GET_CODE (y) == PRE_MODIFY)
821 return operands_match_p (x, XEXP (y, 0), -1);
823 slow:
825 if (code == REG && REG_P (y))
826 return REGNO (x) == REGNO (y);
828 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
829 && x == SUBREG_REG (y))
830 return true;
831 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
832 && SUBREG_REG (x) == y)
833 return true;
835 /* Now we have disposed of all the cases in which different rtx
836 codes can match. */
837 if (code != GET_CODE (y))
838 return false;
840 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
841 if (GET_MODE (x) != GET_MODE (y))
842 return false;
844 switch (code)
846 CASE_CONST_UNIQUE:
847 return false;
849 case CONST_VECTOR:
850 if (!same_vector_encodings_p (x, y))
851 return false;
852 break;
854 case LABEL_REF:
855 return label_ref_label (x) == label_ref_label (y);
856 case SYMBOL_REF:
857 return XSTR (x, 0) == XSTR (y, 0);
859 default:
860 break;
863 /* Compare the elements. If any pair of corresponding elements fail
864 to match, return false for the whole things. */
866 fmt = GET_RTX_FORMAT (code);
867 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
869 int val, j;
870 switch (fmt[i])
872 case 'w':
873 if (XWINT (x, i) != XWINT (y, i))
874 return false;
875 break;
877 case 'i':
878 if (XINT (x, i) != XINT (y, i))
879 return false;
880 break;
882 case 'p':
883 if (maybe_ne (SUBREG_BYTE (x), SUBREG_BYTE (y)))
884 return false;
885 break;
887 case 'e':
888 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
889 if (val == 0)
890 return false;
891 break;
893 case '0':
894 break;
896 case 'E':
897 if (XVECLEN (x, i) != XVECLEN (y, i))
898 return false;
899 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
901 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
902 if (val == 0)
903 return false;
905 break;
907 /* It is believed that rtx's at this level will never
908 contain anything but integers and other rtx's, except for
909 within LABEL_REFs and SYMBOL_REFs. */
910 default:
911 gcc_unreachable ();
914 return true;
917 /* True if X is a constant that can be forced into the constant pool.
918 MODE is the mode of the operand, or VOIDmode if not known. */
919 #define CONST_POOL_OK_P(MODE, X) \
920 ((MODE) != VOIDmode \
921 && CONSTANT_P (X) \
922 && GET_CODE (X) != HIGH \
923 && GET_MODE_SIZE (MODE).is_constant () \
924 && !targetm.cannot_force_const_mem (MODE, X))
926 /* True if C is a non-empty register class that has too few registers
927 to be safely used as a reload target class. */
928 #define SMALL_REGISTER_CLASS_P(C) \
929 (ira_class_hard_regs_num [(C)] == 1 \
930 || (ira_class_hard_regs_num [(C)] >= 1 \
931 && targetm.class_likely_spilled_p (C)))
933 /* If REG is a reload pseudo, try to make its class satisfying CL. */
934 static void
935 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
937 enum reg_class rclass;
939 /* Do not make more accurate class from reloads generated. They are
940 mostly moves with a lot of constraints. Making more accurate
941 class may results in very narrow class and impossibility of find
942 registers for several reloads of one insn. */
943 if (INSN_UID (curr_insn) >= new_insn_uid_start)
944 return;
945 if (GET_CODE (reg) == SUBREG)
946 reg = SUBREG_REG (reg);
947 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
948 return;
949 if (in_class_p (reg, cl, &rclass) && rclass != cl)
950 lra_change_class (REGNO (reg), rclass, " Change to", true);
953 /* Searches X for any reference to a reg with the same value as REGNO,
954 returning the rtx of the reference found if any. Otherwise,
955 returns NULL_RTX. */
956 static rtx
957 regno_val_use_in (unsigned int regno, rtx x)
959 const char *fmt;
960 int i, j;
961 rtx tem;
963 if (REG_P (x) && lra_reg_info[REGNO (x)].val == lra_reg_info[regno].val)
964 return x;
966 fmt = GET_RTX_FORMAT (GET_CODE (x));
967 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
969 if (fmt[i] == 'e')
971 if ((tem = regno_val_use_in (regno, XEXP (x, i))))
972 return tem;
974 else if (fmt[i] == 'E')
975 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
976 if ((tem = regno_val_use_in (regno , XVECEXP (x, i, j))))
977 return tem;
980 return NULL_RTX;
983 /* Return true if all current insn non-output operands except INS (it
984 has a negaitve end marker) do not use pseudos with the same value
985 as REGNO. */
986 static bool
987 check_conflict_input_operands (int regno, signed char *ins)
989 int in;
990 int n_operands = curr_static_id->n_operands;
992 for (int nop = 0; nop < n_operands; nop++)
993 if (! curr_static_id->operand[nop].is_operator
994 && curr_static_id->operand[nop].type != OP_OUT)
996 for (int i = 0; (in = ins[i]) >= 0; i++)
997 if (in == nop)
998 break;
999 if (in < 0
1000 && regno_val_use_in (regno, *curr_id->operand_loc[nop]) != NULL_RTX)
1001 return false;
1003 return true;
1006 /* Generate reloads for matching OUT and INS (array of input operand numbers
1007 with end marker -1) with reg class GOAL_CLASS and EXCLUDE_START_HARD_REGS,
1008 considering output operands OUTS (similar array to INS) needing to be in
1009 different registers. Add input and output reloads correspondingly to the
1010 lists *BEFORE and *AFTER. OUT might be negative. In this case we generate
1011 input reloads for matched input operands INS. EARLY_CLOBBER_P is a flag
1012 that the output operand is early clobbered for chosen alternative. */
1013 static void
1014 match_reload (signed char out, signed char *ins, signed char *outs,
1015 enum reg_class goal_class, HARD_REG_SET *exclude_start_hard_regs,
1016 rtx_insn **before, rtx_insn **after, bool early_clobber_p)
1018 bool out_conflict;
1019 int i, in;
1020 rtx new_in_reg, new_out_reg, reg;
1021 machine_mode inmode, outmode;
1022 rtx in_rtx = *curr_id->operand_loc[ins[0]];
1023 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
1025 inmode = curr_operand_mode[ins[0]];
1026 outmode = out < 0 ? inmode : curr_operand_mode[out];
1027 push_to_sequence (*before);
1028 if (inmode != outmode)
1030 /* process_alt_operands has already checked that the mode sizes
1031 are ordered. */
1032 if (partial_subreg_p (outmode, inmode))
1034 bool asm_p = asm_noperands (PATTERN (curr_insn)) >= 0;
1035 int hr;
1036 HARD_REG_SET temp_hard_reg_set;
1038 if (asm_p && (hr = get_hard_regno (out_rtx)) >= 0
1039 && hard_regno_nregs (hr, inmode) > 1)
1041 /* See gcc.c-torture/execute/20030222-1.c.
1042 Consider the code for 32-bit (e.g. BE) target:
1043 int i, v; long x; x = v; asm ("" : "=r" (i) : "0" (x));
1044 We generate the following RTL with reload insns:
1045 1. subreg:si(x:di, 0) = 0;
1046 2. subreg:si(x:di, 4) = v:si;
1047 3. t:di = x:di, dead x;
1048 4. asm ("" : "=r" (subreg:si(t:di,4)) : "0" (t:di))
1049 5. i:si = subreg:si(t:di,4);
1050 If we assign hard reg of x to t, dead code elimination
1051 will remove insn #2 and we will use unitialized hard reg.
1052 So exclude the hard reg of x for t. We could ignore this
1053 problem for non-empty asm using all x value but it is hard to
1054 check that the asm are expanded into insn realy using x
1055 and setting r. */
1056 CLEAR_HARD_REG_SET (temp_hard_reg_set);
1057 if (exclude_start_hard_regs != NULL)
1058 temp_hard_reg_set = *exclude_start_hard_regs;
1059 SET_HARD_REG_BIT (temp_hard_reg_set, hr);
1060 exclude_start_hard_regs = &temp_hard_reg_set;
1062 reg = new_in_reg
1063 = lra_create_new_reg_with_unique_value (inmode, in_rtx, goal_class,
1064 exclude_start_hard_regs,
1065 "");
1066 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
1067 LRA_SUBREG_P (new_out_reg) = 1;
1068 /* If the input reg is dying here, we can use the same hard
1069 register for REG and IN_RTX. We do it only for original
1070 pseudos as reload pseudos can die although original
1071 pseudos still live where reload pseudos dies. */
1072 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
1073 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
1074 && (!early_clobber_p
1075 || check_conflict_input_operands(REGNO (in_rtx), ins)))
1076 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
1078 else
1080 reg = new_out_reg
1081 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
1082 goal_class,
1083 exclude_start_hard_regs,
1084 "");
1085 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
1086 /* NEW_IN_REG is non-paradoxical subreg. We don't want
1087 NEW_OUT_REG living above. We add clobber clause for
1088 this. This is just a temporary clobber. We can remove
1089 it at the end of LRA work. */
1090 rtx_insn *clobber = emit_clobber (new_out_reg);
1091 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
1092 LRA_SUBREG_P (new_in_reg) = 1;
1093 if (GET_CODE (in_rtx) == SUBREG)
1095 rtx subreg_reg = SUBREG_REG (in_rtx);
1097 /* If SUBREG_REG is dying here and sub-registers IN_RTX
1098 and NEW_IN_REG are similar, we can use the same hard
1099 register for REG and SUBREG_REG. */
1100 if (REG_P (subreg_reg)
1101 && (int) REGNO (subreg_reg) < lra_new_regno_start
1102 && GET_MODE (subreg_reg) == outmode
1103 && known_eq (SUBREG_BYTE (in_rtx), SUBREG_BYTE (new_in_reg))
1104 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg))
1105 && (! early_clobber_p
1106 || check_conflict_input_operands (REGNO (subreg_reg),
1107 ins)))
1108 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
1112 else
1114 /* Pseudos have values -- see comments for lra_reg_info.
1115 Different pseudos with the same value do not conflict even if
1116 they live in the same place. When we create a pseudo we
1117 assign value of original pseudo (if any) from which we
1118 created the new pseudo. If we create the pseudo from the
1119 input pseudo, the new pseudo will have no conflict with the
1120 input pseudo which is wrong when the input pseudo lives after
1121 the insn and as the new pseudo value is changed by the insn
1122 output. Therefore we create the new pseudo from the output
1123 except the case when we have single matched dying input
1124 pseudo.
1126 We cannot reuse the current output register because we might
1127 have a situation like "a <- a op b", where the constraints
1128 force the second input operand ("b") to match the output
1129 operand ("a"). "b" must then be copied into a new register
1130 so that it doesn't clobber the current value of "a".
1132 We cannot use the same value if the output pseudo is
1133 early clobbered or the input pseudo is mentioned in the
1134 output, e.g. as an address part in memory, because
1135 output reload will actually extend the pseudo liveness.
1136 We don't care about eliminable hard regs here as we are
1137 interesting only in pseudos. */
1139 /* Matching input's register value is the same as one of the other
1140 output operand. Output operands in a parallel insn must be in
1141 different registers. */
1142 out_conflict = false;
1143 if (REG_P (in_rtx))
1145 for (i = 0; outs[i] >= 0; i++)
1147 rtx other_out_rtx = *curr_id->operand_loc[outs[i]];
1148 if (outs[i] != out && REG_P (other_out_rtx)
1149 && (regno_val_use_in (REGNO (in_rtx), other_out_rtx)
1150 != NULL_RTX))
1152 out_conflict = true;
1153 break;
1158 new_in_reg = new_out_reg
1159 = (! early_clobber_p && ins[1] < 0 && REG_P (in_rtx)
1160 && (int) REGNO (in_rtx) < lra_new_regno_start
1161 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
1162 && (! early_clobber_p
1163 || check_conflict_input_operands (REGNO (in_rtx), ins))
1164 && (out < 0
1165 || regno_val_use_in (REGNO (in_rtx), out_rtx) == NULL_RTX)
1166 && !out_conflict
1167 ? lra_create_new_reg (inmode, in_rtx, goal_class,
1168 exclude_start_hard_regs, "")
1169 : lra_create_new_reg_with_unique_value (outmode, out_rtx, goal_class,
1170 exclude_start_hard_regs,
1171 ""));
1173 /* In operand can be got from transformations before processing insn
1174 constraints. One example of such transformations is subreg
1175 reloading (see function simplify_operand_subreg). The new
1176 pseudos created by the transformations might have inaccurate
1177 class (ALL_REGS) and we should make their classes more
1178 accurate. */
1179 narrow_reload_pseudo_class (in_rtx, goal_class);
1180 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
1181 *before = get_insns ();
1182 end_sequence ();
1183 /* Add the new pseudo to consider values of subsequent input reload
1184 pseudos. */
1185 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
1186 curr_insn_input_reloads[curr_insn_input_reloads_num].input = in_rtx;
1187 curr_insn_input_reloads[curr_insn_input_reloads_num].match_p = true;
1188 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = new_in_reg;
1189 for (i = 0; (in = ins[i]) >= 0; i++)
1190 if (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
1191 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]))
1192 *curr_id->operand_loc[in] = new_in_reg;
1193 else
1195 lra_assert
1196 (GET_MODE (new_out_reg) == GET_MODE (*curr_id->operand_loc[in]));
1197 *curr_id->operand_loc[in] = new_out_reg;
1199 lra_update_dups (curr_id, ins);
1200 if (out < 0)
1201 return;
1202 /* See a comment for the input operand above. */
1203 narrow_reload_pseudo_class (out_rtx, goal_class);
1204 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
1206 reg = SUBREG_P (out_rtx) ? SUBREG_REG (out_rtx) : out_rtx;
1207 start_sequence ();
1208 /* If we had strict_low_part, use it also in reload to keep other
1209 parts unchanged but do it only for regs as strict_low_part
1210 has no sense for memory and probably there is no insn pattern
1211 to match the reload insn in memory case. */
1212 if (out >= 0 && curr_static_id->operand[out].strict_low && REG_P (reg))
1213 out_rtx = gen_rtx_STRICT_LOW_PART (VOIDmode, out_rtx);
1214 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
1215 emit_insn (*after);
1216 *after = get_insns ();
1217 end_sequence ();
1219 *curr_id->operand_loc[out] = new_out_reg;
1220 lra_update_dup (curr_id, out);
1223 /* Return register class which is union of all reg classes in insn
1224 constraint alternative string starting with P. */
1225 static enum reg_class
1226 reg_class_from_constraints (const char *p)
1228 int c, len;
1229 enum reg_class op_class = NO_REGS;
1232 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1234 case '#':
1235 case ',':
1236 return op_class;
1238 case 'g':
1239 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1240 break;
1242 default:
1243 enum constraint_num cn = lookup_constraint (p);
1244 enum reg_class cl = reg_class_for_constraint (cn);
1245 if (cl == NO_REGS)
1247 if (insn_extra_address_constraint (cn))
1248 op_class
1249 = (reg_class_subunion
1250 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1251 ADDRESS, SCRATCH)]);
1252 break;
1255 op_class = reg_class_subunion[op_class][cl];
1256 break;
1258 while ((p += len), c);
1259 return op_class;
1262 /* If OP is a register, return the class of the register as per
1263 get_reg_class, otherwise return NO_REGS. */
1264 static inline enum reg_class
1265 get_op_class (rtx op)
1267 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1270 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1271 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1272 SUBREG for VAL to make them equal. */
1273 static rtx_insn *
1274 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1276 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1278 /* Usually size of mem_pseudo is greater than val size but in
1279 rare cases it can be less as it can be defined by target
1280 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1281 if (! MEM_P (val))
1283 val = gen_lowpart_SUBREG (GET_MODE (mem_pseudo),
1284 GET_CODE (val) == SUBREG
1285 ? SUBREG_REG (val) : val);
1286 LRA_SUBREG_P (val) = 1;
1288 else
1290 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1291 LRA_SUBREG_P (mem_pseudo) = 1;
1294 return to_p ? gen_move_insn (mem_pseudo, val)
1295 : gen_move_insn (val, mem_pseudo);
1298 /* Process a special case insn (register move), return true if we
1299 don't need to process it anymore. INSN should be a single set
1300 insn. Set up that RTL was changed through CHANGE_P and that hook
1301 TARGET_SECONDARY_MEMORY_NEEDED says to use secondary memory through
1302 SEC_MEM_P. */
1303 static bool
1304 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1306 int sregno, dregno;
1307 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1308 rtx_insn *before;
1309 enum reg_class dclass, sclass, secondary_class;
1310 secondary_reload_info sri;
1312 lra_assert (curr_insn_set != NULL_RTX);
1313 dreg = dest = SET_DEST (curr_insn_set);
1314 sreg = src = SET_SRC (curr_insn_set);
1315 if (GET_CODE (dest) == SUBREG)
1316 dreg = SUBREG_REG (dest);
1317 if (GET_CODE (src) == SUBREG)
1318 sreg = SUBREG_REG (src);
1319 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1320 return false;
1321 sclass = dclass = NO_REGS;
1322 if (REG_P (dreg))
1323 dclass = get_reg_class (REGNO (dreg));
1324 gcc_assert (dclass < LIM_REG_CLASSES && dclass >= NO_REGS);
1325 if (dclass == ALL_REGS)
1326 /* ALL_REGS is used for new pseudos created by transformations
1327 like reload of SUBREG_REG (see function
1328 simplify_operand_subreg). We don't know their class yet. We
1329 should figure out the class from processing the insn
1330 constraints not in this fast path function. Even if ALL_REGS
1331 were a right class for the pseudo, secondary_... hooks usually
1332 are not define for ALL_REGS. */
1333 return false;
1334 if (REG_P (sreg))
1335 sclass = get_reg_class (REGNO (sreg));
1336 gcc_assert (sclass < LIM_REG_CLASSES && sclass >= NO_REGS);
1337 if (sclass == ALL_REGS)
1338 /* See comments above. */
1339 return false;
1340 if (sclass == NO_REGS && dclass == NO_REGS)
1341 return false;
1342 if (targetm.secondary_memory_needed (GET_MODE (src), sclass, dclass)
1343 && ((sclass != NO_REGS && dclass != NO_REGS)
1344 || (GET_MODE (src)
1345 != targetm.secondary_memory_needed_mode (GET_MODE (src)))))
1347 *sec_mem_p = true;
1348 return false;
1350 if (! REG_P (dreg) || ! REG_P (sreg))
1351 return false;
1352 sri.prev_sri = NULL;
1353 sri.icode = CODE_FOR_nothing;
1354 sri.extra_cost = 0;
1355 secondary_class = NO_REGS;
1356 /* Set up hard register for a reload pseudo for hook
1357 secondary_reload because some targets just ignore unassigned
1358 pseudos in the hook. */
1359 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1361 dregno = REGNO (dreg);
1362 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1364 else
1365 dregno = -1;
1366 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1368 sregno = REGNO (sreg);
1369 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1371 else
1372 sregno = -1;
1373 if (sclass != NO_REGS)
1374 secondary_class
1375 = (enum reg_class) targetm.secondary_reload (false, dest,
1376 (reg_class_t) sclass,
1377 GET_MODE (src), &sri);
1378 if (sclass == NO_REGS
1379 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1380 && dclass != NO_REGS))
1382 enum reg_class old_sclass = secondary_class;
1383 secondary_reload_info old_sri = sri;
1385 sri.prev_sri = NULL;
1386 sri.icode = CODE_FOR_nothing;
1387 sri.extra_cost = 0;
1388 secondary_class
1389 = (enum reg_class) targetm.secondary_reload (true, src,
1390 (reg_class_t) dclass,
1391 GET_MODE (src), &sri);
1392 /* Check the target hook consistency. */
1393 lra_assert
1394 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1395 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1396 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1398 if (sregno >= 0)
1399 reg_renumber [sregno] = -1;
1400 if (dregno >= 0)
1401 reg_renumber [dregno] = -1;
1402 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1403 return false;
1404 *change_p = true;
1405 new_reg = NULL_RTX;
1406 if (secondary_class != NO_REGS)
1407 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1408 secondary_class, NULL,
1409 "secondary");
1410 start_sequence ();
1411 if (sri.icode == CODE_FOR_nothing)
1412 lra_emit_move (new_reg, src);
1413 else
1415 enum reg_class scratch_class;
1417 scratch_class = (reg_class_from_constraints
1418 (insn_data[sri.icode].operand[2].constraint));
1419 scratch_reg = (lra_create_new_reg_with_unique_value
1420 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1421 scratch_class, NULL, "scratch"));
1422 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1423 src, scratch_reg));
1425 before = get_insns ();
1426 end_sequence ();
1427 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1428 if (new_reg != NULL_RTX)
1429 SET_SRC (curr_insn_set) = new_reg;
1430 else
1432 if (lra_dump_file != NULL)
1434 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1435 dump_insn_slim (lra_dump_file, curr_insn);
1437 lra_set_insn_deleted (curr_insn);
1438 return true;
1440 return false;
1443 /* The following data describe the result of process_alt_operands.
1444 The data are used in curr_insn_transform to generate reloads. */
1446 /* The chosen reg classes which should be used for the corresponding
1447 operands. */
1448 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1449 /* Hard registers which cannot be a start hard register for the corresponding
1450 operands. */
1451 static HARD_REG_SET goal_alt_exclude_start_hard_regs[MAX_RECOG_OPERANDS];
1452 /* True if the operand should be the same as another operand and that
1453 other operand does not need a reload. */
1454 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1455 /* True if the operand does not need a reload. */
1456 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1457 /* True if the operand can be offsetable memory. */
1458 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1459 /* The number of an operand to which given operand can be matched to. */
1460 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1461 /* The number of elements in the following array. */
1462 static int goal_alt_dont_inherit_ops_num;
1463 /* Numbers of operands whose reload pseudos should not be inherited. */
1464 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1465 /* True if we should try only this alternative for the next constraint sub-pass
1466 to speed up the sub-pass. */
1467 static bool goal_reuse_alt_p;
1468 /* True if the insn commutative operands should be swapped. */
1469 static bool goal_alt_swapped;
1470 /* The chosen insn alternative. */
1471 static int goal_alt_number;
1472 /* True if output reload of the stack pointer should be generated. */
1473 static bool goal_alt_out_sp_reload_p;
1475 /* True if the corresponding operand is the result of an equivalence
1476 substitution. */
1477 static bool equiv_substition_p[MAX_RECOG_OPERANDS];
1479 /* The following five variables are used to choose the best insn
1480 alternative. They reflect final characteristics of the best
1481 alternative. */
1483 /* Number of necessary reloads and overall cost reflecting the
1484 previous value and other unpleasantness of the best alternative. */
1485 static int best_losers, best_overall;
1486 /* Overall number hard registers used for reloads. For example, on
1487 some targets we need 2 general registers to reload DFmode and only
1488 one floating point register. */
1489 static int best_reload_nregs;
1490 /* Overall number reflecting distances of previous reloading the same
1491 value. The distances are counted from the current BB start. It is
1492 used to improve inheritance chances. */
1493 static int best_reload_sum;
1495 /* True if the current insn should have no correspondingly input or
1496 output reloads. */
1497 static bool no_input_reloads_p, no_output_reloads_p;
1499 /* True if we swapped the commutative operands in the current
1500 insn. */
1501 static int curr_swapped;
1503 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1504 register of class CL. Add any input reloads to list BEFORE. AFTER
1505 is nonnull if *LOC is an automodified value; handle that case by
1506 adding the required output reloads to list AFTER. Return true if
1507 the RTL was changed.
1509 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1510 register. Return false if the address register is correct. */
1511 static bool
1512 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1513 enum reg_class cl)
1515 int regno;
1516 enum reg_class rclass, new_class;
1517 rtx reg;
1518 rtx new_reg;
1519 machine_mode mode;
1520 bool subreg_p, before_p = false;
1522 subreg_p = GET_CODE (*loc) == SUBREG;
1523 if (subreg_p)
1525 reg = SUBREG_REG (*loc);
1526 mode = GET_MODE (reg);
1528 /* For mode with size bigger than ptr_mode, there unlikely to be "mov"
1529 between two registers with different classes, but there normally will
1530 be "mov" which transfers element of vector register into the general
1531 register, and this normally will be a subreg which should be reloaded
1532 as a whole. This is particularly likely to be triggered when
1533 -fno-split-wide-types specified. */
1534 if (!REG_P (reg)
1535 || in_class_p (reg, cl, &new_class)
1536 || known_le (GET_MODE_SIZE (mode), GET_MODE_SIZE (ptr_mode)))
1537 loc = &SUBREG_REG (*loc);
1540 reg = *loc;
1541 mode = GET_MODE (reg);
1542 if (! REG_P (reg))
1544 if (check_only_p)
1545 return true;
1546 /* Always reload memory in an address even if the target supports
1547 such addresses. */
1548 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, NULL,
1549 "address");
1550 before_p = true;
1552 else
1554 regno = REGNO (reg);
1555 rclass = get_reg_class (regno);
1556 if (! check_only_p
1557 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1559 if (lra_dump_file != NULL)
1561 fprintf (lra_dump_file,
1562 "Changing pseudo %d in address of insn %u on equiv ",
1563 REGNO (reg), INSN_UID (curr_insn));
1564 dump_value_slim (lra_dump_file, *loc, 1);
1565 fprintf (lra_dump_file, "\n");
1567 *loc = copy_rtx (*loc);
1569 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1571 if (check_only_p)
1572 return true;
1573 reg = *loc;
1574 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1575 mode, reg, cl, NULL,
1576 subreg_p, "address", &new_reg))
1577 before_p = true;
1579 else if (new_class != NO_REGS && rclass != new_class)
1581 if (check_only_p)
1582 return true;
1583 lra_change_class (regno, new_class, " Change to", true);
1584 return false;
1586 else
1587 return false;
1589 if (before_p)
1591 push_to_sequence (*before);
1592 lra_emit_move (new_reg, reg);
1593 *before = get_insns ();
1594 end_sequence ();
1596 *loc = new_reg;
1597 if (after != NULL)
1599 start_sequence ();
1600 lra_emit_move (before_p ? copy_rtx (reg) : reg, new_reg);
1601 emit_insn (*after);
1602 *after = get_insns ();
1603 end_sequence ();
1605 return true;
1608 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1609 the insn to be inserted before curr insn. AFTER returns the
1610 the insn to be inserted after curr insn. ORIGREG and NEWREG
1611 are the original reg and new reg for reload. */
1612 static void
1613 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1614 rtx newreg)
1616 if (before)
1618 push_to_sequence (*before);
1619 lra_emit_move (newreg, origreg);
1620 *before = get_insns ();
1621 end_sequence ();
1623 if (after)
1625 start_sequence ();
1626 lra_emit_move (origreg, newreg);
1627 emit_insn (*after);
1628 *after = get_insns ();
1629 end_sequence ();
1633 static bool valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1634 static bool process_address (int, bool, rtx_insn **, rtx_insn **);
1636 /* Make reloads for subreg in operand NOP with internal subreg mode
1637 REG_MODE, add new reloads for further processing. Return true if
1638 any change was done. */
1639 static bool
1640 simplify_operand_subreg (int nop, machine_mode reg_mode)
1642 int hard_regno, inner_hard_regno;
1643 rtx_insn *before, *after;
1644 machine_mode mode, innermode;
1645 rtx reg, new_reg;
1646 rtx operand = *curr_id->operand_loc[nop];
1647 enum reg_class regclass;
1648 enum op_type type;
1650 before = after = NULL;
1652 if (GET_CODE (operand) != SUBREG)
1653 return false;
1655 mode = GET_MODE (operand);
1656 reg = SUBREG_REG (operand);
1657 innermode = GET_MODE (reg);
1658 type = curr_static_id->operand[nop].type;
1659 if (MEM_P (reg))
1661 const bool addr_was_valid
1662 = valid_address_p (innermode, XEXP (reg, 0), MEM_ADDR_SPACE (reg));
1663 alter_subreg (curr_id->operand_loc[nop], false);
1664 rtx subst = *curr_id->operand_loc[nop];
1665 lra_assert (MEM_P (subst));
1666 const bool addr_is_valid = valid_address_p (GET_MODE (subst),
1667 XEXP (subst, 0),
1668 MEM_ADDR_SPACE (subst));
1669 if (!addr_was_valid
1670 || addr_is_valid
1671 || ((get_constraint_type (lookup_constraint
1672 (curr_static_id->operand[nop].constraint))
1673 != CT_SPECIAL_MEMORY)
1674 /* We still can reload address and if the address is
1675 valid, we can remove subreg without reloading its
1676 inner memory. */
1677 && valid_address_p (GET_MODE (subst),
1678 regno_reg_rtx
1679 [ira_class_hard_regs
1680 [base_reg_class (GET_MODE (subst),
1681 MEM_ADDR_SPACE (subst),
1682 ADDRESS, SCRATCH)][0]],
1683 MEM_ADDR_SPACE (subst))))
1685 /* If we change the address for a paradoxical subreg of memory, the
1686 new address might violate the necessary alignment or the access
1687 might be slow; take this into consideration. We need not worry
1688 about accesses beyond allocated memory for paradoxical memory
1689 subregs as we don't substitute such equiv memory (see processing
1690 equivalences in function lra_constraints) and because for spilled
1691 pseudos we allocate stack memory enough for the biggest
1692 corresponding paradoxical subreg.
1694 However, do not blindly simplify a (subreg (mem ...)) for
1695 WORD_REGISTER_OPERATIONS targets as this may lead to loading junk
1696 data into a register when the inner is narrower than outer or
1697 missing important data from memory when the inner is wider than
1698 outer. This rule only applies to modes that are no wider than
1699 a word.
1701 If valid memory becomes invalid after subreg elimination
1702 and address might be different we still have to reload
1703 memory.
1705 if ((! addr_was_valid
1706 || addr_is_valid
1707 || known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (innermode)))
1708 && !(maybe_ne (GET_MODE_PRECISION (mode),
1709 GET_MODE_PRECISION (innermode))
1710 && known_le (GET_MODE_SIZE (mode), UNITS_PER_WORD)
1711 && known_le (GET_MODE_SIZE (innermode), UNITS_PER_WORD)
1712 && WORD_REGISTER_OPERATIONS)
1713 && (!(MEM_ALIGN (subst) < GET_MODE_ALIGNMENT (mode)
1714 && targetm.slow_unaligned_access (mode, MEM_ALIGN (subst)))
1715 || (MEM_ALIGN (reg) < GET_MODE_ALIGNMENT (innermode)
1716 && targetm.slow_unaligned_access (innermode,
1717 MEM_ALIGN (reg)))))
1718 return true;
1720 *curr_id->operand_loc[nop] = operand;
1722 /* But if the address was not valid, we cannot reload the MEM without
1723 reloading the address first. */
1724 if (!addr_was_valid)
1725 process_address (nop, false, &before, &after);
1727 /* INNERMODE is fast, MODE slow. Reload the mem in INNERMODE. */
1728 enum reg_class rclass
1729 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1730 if (get_reload_reg (curr_static_id->operand[nop].type, innermode,
1731 reg, rclass, NULL,
1732 true, "slow/invalid mem", &new_reg))
1734 bool insert_before, insert_after;
1735 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1737 insert_before = (type != OP_OUT
1738 || partial_subreg_p (mode, innermode));
1739 insert_after = type != OP_IN;
1740 insert_move_for_subreg (insert_before ? &before : NULL,
1741 insert_after ? &after : NULL,
1742 reg, new_reg);
1744 SUBREG_REG (operand) = new_reg;
1746 /* Convert to MODE. */
1747 reg = operand;
1748 rclass
1749 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1750 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1751 rclass, NULL,
1752 true, "slow/invalid mem", &new_reg))
1754 bool insert_before, insert_after;
1755 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1757 insert_before = type != OP_OUT;
1758 insert_after = type != OP_IN;
1759 insert_move_for_subreg (insert_before ? &before : NULL,
1760 insert_after ? &after : NULL,
1761 reg, new_reg);
1763 *curr_id->operand_loc[nop] = new_reg;
1764 lra_process_new_insns (curr_insn, before, after,
1765 "Inserting slow/invalid mem reload");
1766 return true;
1769 /* If the address was valid and became invalid, prefer to reload
1770 the memory. Typical case is when the index scale should
1771 correspond the memory. */
1772 *curr_id->operand_loc[nop] = operand;
1773 /* Do not return false here as the MEM_P (reg) will be processed
1774 later in this function. */
1776 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1778 alter_subreg (curr_id->operand_loc[nop], false);
1779 return true;
1781 else if (CONSTANT_P (reg))
1783 /* Try to simplify subreg of constant. It is usually result of
1784 equivalence substitution. */
1785 if (innermode == VOIDmode
1786 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1787 innermode = curr_static_id->operand[nop].mode;
1788 if ((new_reg = simplify_subreg (mode, reg, innermode,
1789 SUBREG_BYTE (operand))) != NULL_RTX)
1791 *curr_id->operand_loc[nop] = new_reg;
1792 return true;
1795 /* Put constant into memory when we have mixed modes. It generates
1796 a better code in most cases as it does not need a secondary
1797 reload memory. It also prevents LRA looping when LRA is using
1798 secondary reload memory again and again. */
1799 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1800 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1802 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1803 alter_subreg (curr_id->operand_loc[nop], false);
1804 return true;
1806 auto fp_subreg_can_be_simplified_after_reload_p = [] (machine_mode innermode,
1807 poly_uint64 offset,
1808 machine_mode mode) {
1809 reload_completed = 1;
1810 bool res = simplify_subreg_regno (FRAME_POINTER_REGNUM,
1811 innermode,
1812 offset, mode) >= 0;
1813 reload_completed = 0;
1814 return res;
1816 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1817 if there may be a problem accessing OPERAND in the outer
1818 mode. */
1819 if ((REG_P (reg)
1820 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1821 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1822 /* Don't reload paradoxical subregs because we could be looping
1823 having repeatedly final regno out of hard regs range. */
1824 && (hard_regno_nregs (hard_regno, innermode)
1825 >= hard_regno_nregs (hard_regno, mode))
1826 && simplify_subreg_regno (hard_regno, innermode,
1827 SUBREG_BYTE (operand), mode) < 0
1828 /* Exclude reloading of frame pointer in subreg if frame pointer can not
1829 be simplified here only because the reload is not finished yet. */
1830 && (hard_regno != FRAME_POINTER_REGNUM
1831 || !fp_subreg_can_be_simplified_after_reload_p (innermode,
1832 SUBREG_BYTE (operand),
1833 mode))
1834 /* Don't reload subreg for matching reload. It is actually
1835 valid subreg in LRA. */
1836 && ! LRA_SUBREG_P (operand))
1837 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1839 enum reg_class rclass;
1841 if (REG_P (reg))
1842 /* There is a big probability that we will get the same class
1843 for the new pseudo and we will get the same insn which
1844 means infinite looping. So spill the new pseudo. */
1845 rclass = NO_REGS;
1846 else
1847 /* The class will be defined later in curr_insn_transform. */
1848 rclass
1849 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1851 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1852 rclass, NULL,
1853 true, "subreg reg", &new_reg))
1855 bool insert_before, insert_after;
1856 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1858 insert_before = (type != OP_OUT
1859 || read_modify_subreg_p (operand));
1860 insert_after = (type != OP_IN);
1861 insert_move_for_subreg (insert_before ? &before : NULL,
1862 insert_after ? &after : NULL,
1863 reg, new_reg);
1865 SUBREG_REG (operand) = new_reg;
1866 lra_process_new_insns (curr_insn, before, after,
1867 "Inserting subreg reload");
1868 return true;
1870 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1871 IRA allocates hardreg to the inner pseudo reg according to its mode
1872 instead of the outermode, so the size of the hardreg may not be enough
1873 to contain the outermode operand, in that case we may need to insert
1874 reload for the reg. For the following two types of paradoxical subreg,
1875 we need to insert reload:
1876 1. If the op_type is OP_IN, and the hardreg could not be paired with
1877 other hardreg to contain the outermode operand
1878 (checked by in_hard_reg_set_p), we need to insert the reload.
1879 2. If the op_type is OP_OUT or OP_INOUT.
1881 Here is a paradoxical subreg example showing how the reload is generated:
1883 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1884 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1886 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1887 here, if reg107 is assigned to hardreg R15, because R15 is the last
1888 hardreg, compiler cannot find another hardreg to pair with R15 to
1889 contain TImode data. So we insert a TImode reload reg180 for it.
1890 After reload is inserted:
1892 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1893 (reg:DI 107 [ __comp ])) -1
1894 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1895 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1897 Two reload hard registers will be allocated to reg180 to save TImode data
1898 in LRA_assign.
1900 For LRA pseudos this should normally be handled by the biggest_mode
1901 mechanism. However, it's possible for new uses of an LRA pseudo
1902 to be introduced after we've allocated it, such as when undoing
1903 inheritance, and the allocated register might not then be appropriate
1904 for the new uses. */
1905 else if (REG_P (reg)
1906 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1907 && paradoxical_subreg_p (operand)
1908 && (inner_hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1909 && ((hard_regno
1910 = simplify_subreg_regno (inner_hard_regno, innermode,
1911 SUBREG_BYTE (operand), mode)) < 0
1912 || ((hard_regno_nregs (inner_hard_regno, innermode)
1913 < hard_regno_nregs (hard_regno, mode))
1914 && (regclass = lra_get_allocno_class (REGNO (reg)))
1915 && (type != OP_IN
1916 || !in_hard_reg_set_p (reg_class_contents[regclass],
1917 mode, hard_regno)
1918 || overlaps_hard_reg_set_p (lra_no_alloc_regs,
1919 mode, hard_regno)))))
1921 /* The class will be defined later in curr_insn_transform. */
1922 enum reg_class rclass
1923 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1925 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1926 rclass, NULL,
1927 true, "paradoxical subreg", &new_reg))
1929 rtx subreg;
1930 bool insert_before, insert_after;
1932 PUT_MODE (new_reg, mode);
1933 subreg = gen_lowpart_SUBREG (innermode, new_reg);
1934 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1936 insert_before = (type != OP_OUT);
1937 insert_after = (type != OP_IN);
1938 insert_move_for_subreg (insert_before ? &before : NULL,
1939 insert_after ? &after : NULL,
1940 reg, subreg);
1942 SUBREG_REG (operand) = new_reg;
1943 lra_process_new_insns (curr_insn, before, after,
1944 "Inserting paradoxical subreg reload");
1945 return true;
1947 return false;
1950 /* Return TRUE if X refers for a hard register from SET. */
1951 static bool
1952 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1954 int i, j, x_hard_regno;
1955 machine_mode mode;
1956 const char *fmt;
1957 enum rtx_code code;
1959 if (x == NULL_RTX)
1960 return false;
1961 code = GET_CODE (x);
1962 mode = GET_MODE (x);
1964 if (code == SUBREG)
1966 /* For all SUBREGs we want to check whether the full multi-register
1967 overlaps the set. For normal SUBREGs this means 'get_hard_regno' of
1968 the inner register, for paradoxical SUBREGs this means the
1969 'get_hard_regno' of the full SUBREG and for complete SUBREGs either is
1970 fine. Use the wider mode for all cases. */
1971 rtx subreg = SUBREG_REG (x);
1972 mode = wider_subreg_mode (x);
1973 if (mode == GET_MODE (subreg))
1975 x = subreg;
1976 code = GET_CODE (x);
1980 if (REG_P (x) || SUBREG_P (x))
1982 x_hard_regno = get_hard_regno (x);
1983 return (x_hard_regno >= 0
1984 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1986 fmt = GET_RTX_FORMAT (code);
1987 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1989 if (fmt[i] == 'e')
1991 if (uses_hard_regs_p (XEXP (x, i), set))
1992 return true;
1994 else if (fmt[i] == 'E')
1996 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1997 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1998 return true;
2001 return false;
2004 /* Return true if OP is a spilled pseudo. */
2005 static inline bool
2006 spilled_pseudo_p (rtx op)
2008 return (REG_P (op)
2009 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
2012 /* Return true if X is a general constant. */
2013 static inline bool
2014 general_constant_p (rtx x)
2016 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
2019 static bool
2020 reg_in_class_p (rtx reg, enum reg_class cl)
2022 if (cl == NO_REGS)
2023 return get_reg_class (REGNO (reg)) == NO_REGS;
2024 return in_class_p (reg, cl, NULL);
2027 /* Return true if SET of RCLASS contains no hard regs which can be
2028 used in MODE. */
2029 static bool
2030 prohibited_class_reg_set_mode_p (enum reg_class rclass,
2031 HARD_REG_SET &set,
2032 machine_mode mode)
2034 HARD_REG_SET temp;
2036 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
2037 temp = set & ~lra_no_alloc_regs;
2038 return (hard_reg_set_subset_p
2039 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
2043 /* Used to check validity info about small class input operands. It
2044 should be incremented at start of processing an insn
2045 alternative. */
2046 static unsigned int curr_small_class_check = 0;
2048 /* Update number of used inputs of class OP_CLASS for operand NOP
2049 of alternative NALT. Return true if we have more such class operands
2050 than the number of available regs. */
2051 static bool
2052 update_and_check_small_class_inputs (int nop, int nalt,
2053 enum reg_class op_class)
2055 static unsigned int small_class_check[LIM_REG_CLASSES];
2056 static int small_class_input_nums[LIM_REG_CLASSES];
2058 if (SMALL_REGISTER_CLASS_P (op_class)
2059 /* We are interesting in classes became small because of fixing
2060 some hard regs, e.g. by an user through GCC options. */
2061 && hard_reg_set_intersect_p (reg_class_contents[op_class],
2062 ira_no_alloc_regs)
2063 && (curr_static_id->operand[nop].type != OP_OUT
2064 || TEST_BIT (curr_static_id->operand[nop].early_clobber_alts, nalt)))
2066 if (small_class_check[op_class] == curr_small_class_check)
2067 small_class_input_nums[op_class]++;
2068 else
2070 small_class_check[op_class] = curr_small_class_check;
2071 small_class_input_nums[op_class] = 1;
2073 if (small_class_input_nums[op_class] > ira_class_hard_regs_num[op_class])
2074 return true;
2076 return false;
2079 /* Print operand constraints for alternative ALT_NUMBER of the current
2080 insn. */
2081 static void
2082 print_curr_insn_alt (int alt_number)
2084 for (int i = 0; i < curr_static_id->n_operands; i++)
2086 const char *p = (curr_static_id->operand_alternative
2087 [alt_number * curr_static_id->n_operands + i].constraint);
2088 if (*p == '\0')
2089 continue;
2090 fprintf (lra_dump_file, " (%d) ", i);
2091 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
2092 fputc (*p, lra_dump_file);
2096 /* Major function to choose the current insn alternative and what
2097 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
2098 negative we should consider only this alternative. Return false if
2099 we cannot choose the alternative or find how to reload the
2100 operands. */
2101 static bool
2102 process_alt_operands (int only_alternative)
2104 bool ok_p = false;
2105 int nop, overall, nalt;
2106 int n_alternatives = curr_static_id->n_alternatives;
2107 int n_operands = curr_static_id->n_operands;
2108 /* LOSERS counts the operands that don't fit this alternative and
2109 would require loading. */
2110 int losers;
2111 int addr_losers;
2112 /* REJECT is a count of how undesirable this alternative says it is
2113 if any reloading is required. If the alternative matches exactly
2114 then REJECT is ignored, but otherwise it gets this much counted
2115 against it in addition to the reloading needed. */
2116 int reject;
2117 /* This is defined by '!' or '?' alternative constraint and added to
2118 reject. But in some cases it can be ignored. */
2119 int static_reject;
2120 int op_reject;
2121 /* The number of elements in the following array. */
2122 int early_clobbered_regs_num;
2123 /* Numbers of operands which are early clobber registers. */
2124 int early_clobbered_nops[MAX_RECOG_OPERANDS];
2125 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
2126 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
2127 HARD_REG_SET curr_alt_exclude_start_hard_regs[MAX_RECOG_OPERANDS];
2128 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
2129 bool curr_alt_win[MAX_RECOG_OPERANDS];
2130 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
2131 int curr_alt_matches[MAX_RECOG_OPERANDS];
2132 /* The number of elements in the following array. */
2133 int curr_alt_dont_inherit_ops_num;
2134 /* Numbers of operands whose reload pseudos should not be inherited. */
2135 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
2136 bool curr_reuse_alt_p;
2137 /* True if output stack pointer reload should be generated for the current
2138 alternative. */
2139 bool curr_alt_out_sp_reload_p;
2140 rtx op;
2141 /* The register when the operand is a subreg of register, otherwise the
2142 operand itself. */
2143 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
2144 /* The register if the operand is a register or subreg of register,
2145 otherwise NULL. */
2146 rtx operand_reg[MAX_RECOG_OPERANDS];
2147 int hard_regno[MAX_RECOG_OPERANDS];
2148 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
2149 int reload_nregs, reload_sum;
2150 bool costly_p;
2151 enum reg_class cl;
2152 const HARD_REG_SET *cl_filter;
2154 /* Calculate some data common for all alternatives to speed up the
2155 function. */
2156 for (nop = 0; nop < n_operands; nop++)
2158 rtx reg;
2160 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
2161 /* The real hard regno of the operand after the allocation. */
2162 hard_regno[nop] = get_hard_regno (op);
2164 operand_reg[nop] = reg = op;
2165 biggest_mode[nop] = GET_MODE (op);
2166 if (GET_CODE (op) == SUBREG)
2168 biggest_mode[nop] = wider_subreg_mode (op);
2169 operand_reg[nop] = reg = SUBREG_REG (op);
2171 if (! REG_P (reg))
2172 operand_reg[nop] = NULL_RTX;
2173 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
2174 || ((int) REGNO (reg)
2175 == lra_get_elimination_hard_regno (REGNO (reg))))
2176 no_subreg_reg_operand[nop] = reg;
2177 else
2178 operand_reg[nop] = no_subreg_reg_operand[nop]
2179 /* Just use natural mode for elimination result. It should
2180 be enough for extra constraints hooks. */
2181 = regno_reg_rtx[hard_regno[nop]];
2184 /* The constraints are made of several alternatives. Each operand's
2185 constraint looks like foo,bar,... with commas separating the
2186 alternatives. The first alternatives for all operands go
2187 together, the second alternatives go together, etc.
2189 First loop over alternatives. */
2190 alternative_mask preferred = curr_id->preferred_alternatives;
2191 if (only_alternative >= 0)
2192 preferred &= ALTERNATIVE_BIT (only_alternative);
2194 for (nalt = 0; nalt < n_alternatives; nalt++)
2196 /* Loop over operands for one constraint alternative. */
2197 if (!TEST_BIT (preferred, nalt))
2198 continue;
2200 if (lra_dump_file != NULL)
2202 fprintf (lra_dump_file, " Considering alt=%d of insn %d: ",
2203 nalt, INSN_UID (curr_insn));
2204 print_curr_insn_alt (nalt);
2205 fprintf (lra_dump_file, "\n");
2208 bool matching_early_clobber[MAX_RECOG_OPERANDS];
2209 curr_small_class_check++;
2210 overall = losers = addr_losers = 0;
2211 static_reject = reject = reload_nregs = reload_sum = 0;
2212 for (nop = 0; nop < n_operands; nop++)
2214 int inc = (curr_static_id
2215 ->operand_alternative[nalt * n_operands + nop].reject);
2216 if (lra_dump_file != NULL && inc != 0)
2217 fprintf (lra_dump_file,
2218 " Staticly defined alt reject+=%d\n", inc);
2219 static_reject += inc;
2220 matching_early_clobber[nop] = 0;
2222 reject += static_reject;
2223 early_clobbered_regs_num = 0;
2224 curr_alt_out_sp_reload_p = false;
2225 curr_reuse_alt_p = true;
2227 for (nop = 0; nop < n_operands; nop++)
2229 const char *p;
2230 char *end;
2231 int len, c, m, i, opalt_num, this_alternative_matches;
2232 bool win, did_match, offmemok, early_clobber_p;
2233 /* false => this operand can be reloaded somehow for this
2234 alternative. */
2235 bool badop;
2236 /* true => this operand can be reloaded if the alternative
2237 allows regs. */
2238 bool winreg;
2239 /* True if a constant forced into memory would be OK for
2240 this operand. */
2241 bool constmemok;
2242 enum reg_class this_alternative, this_costly_alternative;
2243 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
2244 HARD_REG_SET this_alternative_exclude_start_hard_regs;
2245 bool this_alternative_match_win, this_alternative_win;
2246 bool this_alternative_offmemok;
2247 bool scratch_p;
2248 machine_mode mode;
2249 enum constraint_num cn;
2251 opalt_num = nalt * n_operands + nop;
2252 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
2254 /* Fast track for no constraints at all. */
2255 curr_alt[nop] = NO_REGS;
2256 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
2257 curr_alt_win[nop] = true;
2258 curr_alt_match_win[nop] = false;
2259 curr_alt_offmemok[nop] = false;
2260 curr_alt_matches[nop] = -1;
2261 continue;
2264 op = no_subreg_reg_operand[nop];
2265 mode = curr_operand_mode[nop];
2267 win = did_match = winreg = offmemok = constmemok = false;
2268 badop = true;
2270 early_clobber_p = false;
2271 p = curr_static_id->operand_alternative[opalt_num].constraint;
2273 this_costly_alternative = this_alternative = NO_REGS;
2274 /* We update set of possible hard regs besides its class
2275 because reg class might be inaccurate. For example,
2276 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
2277 is translated in HI_REGS because classes are merged by
2278 pairs and there is no accurate intermediate class. */
2279 CLEAR_HARD_REG_SET (this_alternative_set);
2280 CLEAR_HARD_REG_SET (this_costly_alternative_set);
2281 CLEAR_HARD_REG_SET (this_alternative_exclude_start_hard_regs);
2282 this_alternative_win = false;
2283 this_alternative_match_win = false;
2284 this_alternative_offmemok = false;
2285 this_alternative_matches = -1;
2287 /* An empty constraint should be excluded by the fast
2288 track. */
2289 lra_assert (*p != 0 && *p != ',');
2291 op_reject = 0;
2292 /* Scan this alternative's specs for this operand; set WIN
2293 if the operand fits any letter in this alternative.
2294 Otherwise, clear BADOP if this operand could fit some
2295 letter after reloads, or set WINREG if this operand could
2296 fit after reloads provided the constraint allows some
2297 registers. */
2298 costly_p = false;
2301 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
2303 case '\0':
2304 len = 0;
2305 break;
2306 case ',':
2307 c = '\0';
2308 break;
2310 case '&':
2311 early_clobber_p = true;
2312 break;
2314 case '$':
2315 op_reject += LRA_MAX_REJECT;
2316 break;
2317 case '^':
2318 op_reject += LRA_LOSER_COST_FACTOR;
2319 break;
2321 case '#':
2322 /* Ignore rest of this alternative. */
2323 c = '\0';
2324 break;
2326 case '0': case '1': case '2': case '3': case '4':
2327 case '5': case '6': case '7': case '8': case '9':
2329 int m_hregno;
2330 bool match_p;
2332 m = strtoul (p, &end, 10);
2333 p = end;
2334 len = 0;
2335 lra_assert (nop > m);
2337 /* Reject matches if we don't know which operand is
2338 bigger. This situation would arguably be a bug in
2339 an .md pattern, but could also occur in a user asm. */
2340 if (!ordered_p (GET_MODE_SIZE (biggest_mode[m]),
2341 GET_MODE_SIZE (biggest_mode[nop])))
2342 break;
2344 /* Don't match wrong asm insn operands for proper
2345 diagnostic later. */
2346 if (INSN_CODE (curr_insn) < 0
2347 && (curr_operand_mode[m] == BLKmode
2348 || curr_operand_mode[nop] == BLKmode)
2349 && curr_operand_mode[m] != curr_operand_mode[nop])
2350 break;
2352 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
2353 /* We are supposed to match a previous operand.
2354 If we do, we win if that one did. If we do
2355 not, count both of the operands as losers.
2356 (This is too conservative, since most of the
2357 time only a single reload insn will be needed
2358 to make the two operands win. As a result,
2359 this alternative may be rejected when it is
2360 actually desirable.) */
2361 match_p = false;
2362 if (operands_match_p (*curr_id->operand_loc[nop],
2363 *curr_id->operand_loc[m], m_hregno))
2365 /* We should reject matching of an early
2366 clobber operand if the matching operand is
2367 not dying in the insn. */
2368 if (!TEST_BIT (curr_static_id->operand[m]
2369 .early_clobber_alts, nalt)
2370 || operand_reg[nop] == NULL_RTX
2371 || (find_regno_note (curr_insn, REG_DEAD,
2372 REGNO (op))
2373 || REGNO (op) == REGNO (operand_reg[m])))
2374 match_p = true;
2376 if (match_p)
2378 /* If we are matching a non-offsettable
2379 address where an offsettable address was
2380 expected, then we must reject this
2381 combination, because we can't reload
2382 it. */
2383 if (curr_alt_offmemok[m]
2384 && MEM_P (*curr_id->operand_loc[m])
2385 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
2386 continue;
2388 else
2390 /* If the operands do not match and one
2391 operand is INOUT, we can not match them.
2392 Try other possibilities, e.g. other
2393 alternatives or commutative operand
2394 exchange. */
2395 if (curr_static_id->operand[nop].type == OP_INOUT
2396 || curr_static_id->operand[m].type == OP_INOUT)
2397 break;
2398 /* Operands don't match. If the operands are
2399 different user defined explicit hard
2400 registers, then we cannot make them match
2401 when one is early clobber operand. */
2402 if ((REG_P (*curr_id->operand_loc[nop])
2403 || SUBREG_P (*curr_id->operand_loc[nop]))
2404 && (REG_P (*curr_id->operand_loc[m])
2405 || SUBREG_P (*curr_id->operand_loc[m])))
2407 rtx nop_reg = *curr_id->operand_loc[nop];
2408 if (SUBREG_P (nop_reg))
2409 nop_reg = SUBREG_REG (nop_reg);
2410 rtx m_reg = *curr_id->operand_loc[m];
2411 if (SUBREG_P (m_reg))
2412 m_reg = SUBREG_REG (m_reg);
2414 if (REG_P (nop_reg)
2415 && HARD_REGISTER_P (nop_reg)
2416 && REG_USERVAR_P (nop_reg)
2417 && REG_P (m_reg)
2418 && HARD_REGISTER_P (m_reg)
2419 && REG_USERVAR_P (m_reg))
2421 int i;
2423 for (i = 0; i < early_clobbered_regs_num; i++)
2424 if (m == early_clobbered_nops[i])
2425 break;
2426 if (i < early_clobbered_regs_num
2427 || early_clobber_p)
2428 break;
2431 /* Both operands must allow a reload register,
2432 otherwise we cannot make them match. */
2433 if (curr_alt[m] == NO_REGS)
2434 break;
2435 /* Retroactively mark the operand we had to
2436 match as a loser, if it wasn't already and
2437 it wasn't matched to a register constraint
2438 (e.g it might be matched by memory). */
2439 if (curr_alt_win[m]
2440 && (operand_reg[m] == NULL_RTX
2441 || hard_regno[m] < 0))
2443 losers++;
2444 reload_nregs
2445 += (ira_reg_class_max_nregs[curr_alt[m]]
2446 [GET_MODE (*curr_id->operand_loc[m])]);
2449 /* Prefer matching earlyclobber alternative as
2450 it results in less hard regs required for
2451 the insn than a non-matching earlyclobber
2452 alternative. */
2453 if (TEST_BIT (curr_static_id->operand[m]
2454 .early_clobber_alts, nalt))
2456 if (lra_dump_file != NULL)
2457 fprintf
2458 (lra_dump_file,
2459 " %d Matching earlyclobber alt:"
2460 " reject--\n",
2461 nop);
2462 if (!matching_early_clobber[m])
2464 reject--;
2465 matching_early_clobber[m] = 1;
2468 /* Otherwise we prefer no matching
2469 alternatives because it gives more freedom
2470 in RA. */
2471 else if (operand_reg[nop] == NULL_RTX
2472 || (find_regno_note (curr_insn, REG_DEAD,
2473 REGNO (operand_reg[nop]))
2474 == NULL_RTX))
2476 if (lra_dump_file != NULL)
2477 fprintf
2478 (lra_dump_file,
2479 " %d Matching alt: reject+=2\n",
2480 nop);
2481 reject += 2;
2484 /* If we have to reload this operand and some
2485 previous operand also had to match the same
2486 thing as this operand, we don't know how to do
2487 that. */
2488 if (!match_p || !curr_alt_win[m])
2490 for (i = 0; i < nop; i++)
2491 if (curr_alt_matches[i] == m)
2492 break;
2493 if (i < nop)
2494 break;
2496 else
2497 did_match = true;
2499 this_alternative_matches = m;
2500 /* This can be fixed with reloads if the operand
2501 we are supposed to match can be fixed with
2502 reloads. */
2503 badop = false;
2504 this_alternative = curr_alt[m];
2505 this_alternative_set = curr_alt_set[m];
2506 this_alternative_exclude_start_hard_regs
2507 = curr_alt_exclude_start_hard_regs[m];
2508 winreg = this_alternative != NO_REGS;
2509 break;
2512 case 'g':
2513 if (MEM_P (op)
2514 || general_constant_p (op)
2515 || spilled_pseudo_p (op))
2516 win = true;
2517 cl = GENERAL_REGS;
2518 cl_filter = nullptr;
2519 goto reg;
2521 default:
2522 cn = lookup_constraint (p);
2523 switch (get_constraint_type (cn))
2525 case CT_REGISTER:
2526 cl = reg_class_for_constraint (cn);
2527 if (cl != NO_REGS)
2529 cl_filter = get_register_filter (cn);
2530 goto reg;
2532 break;
2534 case CT_CONST_INT:
2535 if (CONST_INT_P (op)
2536 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2537 win = true;
2538 break;
2540 case CT_MEMORY:
2541 case CT_RELAXED_MEMORY:
2542 if (MEM_P (op)
2543 && satisfies_memory_constraint_p (op, cn))
2544 win = true;
2545 else if (spilled_pseudo_p (op))
2546 win = true;
2548 /* If we didn't already win, we can reload constants
2549 via force_const_mem or put the pseudo value into
2550 memory, or make other memory by reloading the
2551 address like for 'o'. */
2552 if (CONST_POOL_OK_P (mode, op)
2553 || MEM_P (op) || REG_P (op)
2554 /* We can restore the equiv insn by a
2555 reload. */
2556 || equiv_substition_p[nop])
2557 badop = false;
2558 constmemok = true;
2559 offmemok = true;
2560 break;
2562 case CT_ADDRESS:
2563 /* An asm operand with an address constraint
2564 that doesn't satisfy address_operand has
2565 is_address cleared, so that we don't try to
2566 make a non-address fit. */
2567 if (!curr_static_id->operand[nop].is_address)
2568 break;
2569 /* If we didn't already win, we can reload the address
2570 into a base register. */
2571 if (satisfies_address_constraint_p (op, cn))
2572 win = true;
2573 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2574 ADDRESS, SCRATCH);
2575 cl_filter = nullptr;
2576 badop = false;
2577 goto reg;
2579 case CT_FIXED_FORM:
2580 if (constraint_satisfied_p (op, cn))
2581 win = true;
2582 break;
2584 case CT_SPECIAL_MEMORY:
2585 if (satisfies_memory_constraint_p (op, cn))
2586 win = true;
2587 else if (spilled_pseudo_p (op))
2589 curr_reuse_alt_p = false;
2590 win = true;
2592 break;
2594 break;
2596 reg:
2597 if (mode == BLKmode)
2598 break;
2599 this_alternative = reg_class_subunion[this_alternative][cl];
2600 if (hard_reg_set_subset_p (this_alternative_set,
2601 reg_class_contents[cl]))
2602 this_alternative_exclude_start_hard_regs
2603 = ira_exclude_class_mode_regs[cl][mode];
2604 else if (!hard_reg_set_subset_p (reg_class_contents[cl],
2605 this_alternative_set))
2606 this_alternative_exclude_start_hard_regs
2607 |= ira_exclude_class_mode_regs[cl][mode];
2608 this_alternative_set |= reg_class_contents[cl];
2609 if (cl_filter)
2610 this_alternative_exclude_start_hard_regs |= ~*cl_filter;
2611 if (costly_p)
2613 this_costly_alternative
2614 = reg_class_subunion[this_costly_alternative][cl];
2615 this_costly_alternative_set |= reg_class_contents[cl];
2617 winreg = true;
2618 if (REG_P (op))
2620 tree decl;
2621 if (hard_regno[nop] >= 0
2622 && in_hard_reg_set_p (this_alternative_set,
2623 mode, hard_regno[nop])
2624 && (!cl_filter
2625 || TEST_HARD_REG_BIT (*cl_filter,
2626 hard_regno[nop]))
2627 && ((REG_ATTRS (op) && (decl = REG_EXPR (op)) != NULL
2628 && VAR_P (decl) && DECL_HARD_REGISTER (decl))
2629 || !(TEST_HARD_REG_BIT
2630 (this_alternative_exclude_start_hard_regs,
2631 hard_regno[nop]))))
2632 win = true;
2633 else if (hard_regno[nop] < 0
2634 && in_class_p (op, this_alternative, NULL))
2635 win = true;
2637 break;
2639 if (c != ' ' && c != '\t')
2640 costly_p = c == '*';
2642 while ((p += len), c);
2644 scratch_p = (operand_reg[nop] != NULL_RTX
2645 && ira_former_scratch_p (REGNO (operand_reg[nop])));
2646 /* Record which operands fit this alternative. */
2647 if (win)
2649 this_alternative_win = true;
2650 if (operand_reg[nop] != NULL_RTX)
2652 if (hard_regno[nop] >= 0)
2654 if (in_hard_reg_set_p (this_costly_alternative_set,
2655 mode, hard_regno[nop]))
2657 if (lra_dump_file != NULL)
2658 fprintf (lra_dump_file,
2659 " %d Costly set: reject++\n",
2660 nop);
2661 reject++;
2664 else
2666 /* Prefer won reg to spilled pseudo under other
2667 equal conditions for possibe inheritance. */
2668 if (! scratch_p)
2670 if (lra_dump_file != NULL)
2671 fprintf
2672 (lra_dump_file,
2673 " %d Non pseudo reload: reject++\n",
2674 nop);
2675 reject++;
2677 if (in_class_p (operand_reg[nop],
2678 this_costly_alternative, NULL))
2680 if (lra_dump_file != NULL)
2681 fprintf
2682 (lra_dump_file,
2683 " %d Non pseudo costly reload:"
2684 " reject++\n",
2685 nop);
2686 reject++;
2689 /* We simulate the behavior of old reload here.
2690 Although scratches need hard registers and it
2691 might result in spilling other pseudos, no reload
2692 insns are generated for the scratches. So it
2693 might cost something but probably less than old
2694 reload pass believes. */
2695 if (scratch_p)
2697 if (lra_dump_file != NULL)
2698 fprintf (lra_dump_file,
2699 " %d Scratch win: reject+=2\n",
2700 nop);
2701 reject += 2;
2705 else if (did_match)
2706 this_alternative_match_win = true;
2707 else
2709 int const_to_mem = 0;
2710 bool no_regs_p;
2712 reject += op_reject;
2713 /* Mark output reload of the stack pointer. */
2714 if (op == stack_pointer_rtx
2715 && curr_static_id->operand[nop].type != OP_IN)
2716 curr_alt_out_sp_reload_p = true;
2718 /* If this alternative asks for a specific reg class, see if there
2719 is at least one allocatable register in that class. */
2720 no_regs_p
2721 = (this_alternative == NO_REGS
2722 || (hard_reg_set_subset_p
2723 (reg_class_contents[this_alternative],
2724 lra_no_alloc_regs)));
2726 /* For asms, verify that the class for this alternative is possible
2727 for the mode that is specified. */
2728 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2730 int i;
2731 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2732 if (targetm.hard_regno_mode_ok (i, mode)
2733 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2734 mode, i))
2735 break;
2736 if (i == FIRST_PSEUDO_REGISTER)
2737 winreg = false;
2740 /* If this operand accepts a register, and if the
2741 register class has at least one allocatable register,
2742 then this operand can be reloaded. */
2743 if (winreg && !no_regs_p)
2744 badop = false;
2746 if (badop)
2748 if (lra_dump_file != NULL)
2749 fprintf (lra_dump_file,
2750 " Bad operand -- refuse\n");
2751 goto fail;
2754 if (this_alternative != NO_REGS)
2756 HARD_REG_SET available_regs
2757 = (reg_class_contents[this_alternative]
2758 & ~((ira_prohibited_class_mode_regs
2759 [this_alternative][mode])
2760 | lra_no_alloc_regs));
2761 if (hard_reg_set_empty_p (available_regs))
2763 /* There are no hard regs holding a value of given
2764 mode. */
2765 if (offmemok)
2767 this_alternative = NO_REGS;
2768 if (lra_dump_file != NULL)
2769 fprintf (lra_dump_file,
2770 " %d Using memory because of"
2771 " a bad mode: reject+=2\n",
2772 nop);
2773 reject += 2;
2775 else
2777 if (lra_dump_file != NULL)
2778 fprintf (lra_dump_file,
2779 " Wrong mode -- refuse\n");
2780 goto fail;
2785 /* If not assigned pseudo has a class which a subset of
2786 required reg class, it is a less costly alternative
2787 as the pseudo still can get a hard reg of necessary
2788 class. */
2789 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2790 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2791 && ira_class_subset_p[this_alternative][cl])
2793 if (lra_dump_file != NULL)
2794 fprintf
2795 (lra_dump_file,
2796 " %d Super set class reg: reject-=3\n", nop);
2797 reject -= 3;
2800 this_alternative_offmemok = offmemok;
2801 if (this_costly_alternative != NO_REGS)
2803 if (lra_dump_file != NULL)
2804 fprintf (lra_dump_file,
2805 " %d Costly loser: reject++\n", nop);
2806 reject++;
2808 /* If the operand is dying, has a matching constraint,
2809 and satisfies constraints of the matched operand
2810 which failed to satisfy the own constraints, most probably
2811 the reload for this operand will be gone. */
2812 if (this_alternative_matches >= 0
2813 && !curr_alt_win[this_alternative_matches]
2814 && REG_P (op)
2815 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2816 && (hard_regno[nop] >= 0
2817 ? in_hard_reg_set_p (this_alternative_set,
2818 mode, hard_regno[nop])
2819 : in_class_p (op, this_alternative, NULL)))
2821 if (lra_dump_file != NULL)
2822 fprintf
2823 (lra_dump_file,
2824 " %d Dying matched operand reload: reject++\n",
2825 nop);
2826 reject++;
2828 else
2830 /* Strict_low_part requires to reload the register
2831 not the sub-register. In this case we should
2832 check that a final reload hard reg can hold the
2833 value mode. */
2834 if (curr_static_id->operand[nop].strict_low
2835 && REG_P (op)
2836 && hard_regno[nop] < 0
2837 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2838 && ira_class_hard_regs_num[this_alternative] > 0
2839 && (!targetm.hard_regno_mode_ok
2840 (ira_class_hard_regs[this_alternative][0],
2841 GET_MODE (*curr_id->operand_loc[nop]))))
2843 if (lra_dump_file != NULL)
2844 fprintf
2845 (lra_dump_file,
2846 " Strict low subreg reload -- refuse\n");
2847 goto fail;
2849 losers++;
2851 if (operand_reg[nop] != NULL_RTX
2852 /* Output operands and matched input operands are
2853 not inherited. The following conditions do not
2854 exactly describe the previous statement but they
2855 are pretty close. */
2856 && curr_static_id->operand[nop].type != OP_OUT
2857 && (this_alternative_matches < 0
2858 || curr_static_id->operand[nop].type != OP_IN))
2860 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2861 (operand_reg[nop])]
2862 .last_reload);
2864 /* The value of reload_sum has sense only if we
2865 process insns in their order. It happens only on
2866 the first constraints sub-pass when we do most of
2867 reload work. */
2868 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2869 reload_sum += last_reload - bb_reload_num;
2871 /* If this is a constant that is reloaded into the
2872 desired class by copying it to memory first, count
2873 that as another reload. This is consistent with
2874 other code and is required to avoid choosing another
2875 alternative when the constant is moved into memory.
2876 Note that the test here is precisely the same as in
2877 the code below that calls force_const_mem. */
2878 if (CONST_POOL_OK_P (mode, op)
2879 && ((targetm.preferred_reload_class
2880 (op, this_alternative) == NO_REGS)
2881 || no_input_reloads_p))
2883 const_to_mem = 1;
2884 if (! no_regs_p)
2885 losers++;
2888 /* Alternative loses if it requires a type of reload not
2889 permitted for this insn. We can always reload
2890 objects with a REG_UNUSED note. */
2891 if ((curr_static_id->operand[nop].type != OP_IN
2892 && no_output_reloads_p
2893 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2894 || (curr_static_id->operand[nop].type != OP_OUT
2895 && no_input_reloads_p && ! const_to_mem)
2896 || (this_alternative_matches >= 0
2897 && (no_input_reloads_p
2898 || (no_output_reloads_p
2899 && (curr_static_id->operand
2900 [this_alternative_matches].type != OP_IN)
2901 && ! find_reg_note (curr_insn, REG_UNUSED,
2902 no_subreg_reg_operand
2903 [this_alternative_matches])))))
2905 if (lra_dump_file != NULL)
2906 fprintf
2907 (lra_dump_file,
2908 " No input/output reload -- refuse\n");
2909 goto fail;
2912 /* Alternative loses if it required class pseudo cannot
2913 hold value of required mode. Such insns can be
2914 described by insn definitions with mode iterators. */
2915 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2916 && ! hard_reg_set_empty_p (this_alternative_set)
2917 /* It is common practice for constraints to use a
2918 class which does not have actually enough regs to
2919 hold the value (e.g. x86 AREG for mode requiring
2920 more one general reg). Therefore we have 2
2921 conditions to check that the reload pseudo cannot
2922 hold the mode value. */
2923 && (!targetm.hard_regno_mode_ok
2924 (ira_class_hard_regs[this_alternative][0],
2925 GET_MODE (*curr_id->operand_loc[nop])))
2926 /* The above condition is not enough as the first
2927 reg in ira_class_hard_regs can be not aligned for
2928 multi-words mode values. */
2929 && (prohibited_class_reg_set_mode_p
2930 (this_alternative, this_alternative_set,
2931 GET_MODE (*curr_id->operand_loc[nop]))))
2933 if (lra_dump_file != NULL)
2934 fprintf (lra_dump_file,
2935 " reload pseudo for op %d "
2936 "cannot hold the mode value -- refuse\n",
2937 nop);
2938 goto fail;
2941 /* Check strong discouragement of reload of non-constant
2942 into class THIS_ALTERNATIVE. */
2943 if (! CONSTANT_P (op) && ! no_regs_p
2944 && (targetm.preferred_reload_class
2945 (op, this_alternative) == NO_REGS
2946 || (curr_static_id->operand[nop].type == OP_OUT
2947 && (targetm.preferred_output_reload_class
2948 (op, this_alternative) == NO_REGS))))
2950 if (offmemok && REG_P (op))
2952 if (lra_dump_file != NULL)
2953 fprintf
2954 (lra_dump_file,
2955 " %d Spill pseudo into memory: reject+=3\n",
2956 nop);
2957 reject += 3;
2959 else
2961 if (lra_dump_file != NULL)
2962 fprintf
2963 (lra_dump_file,
2964 " %d Non-prefered reload: reject+=%d\n",
2965 nop, LRA_MAX_REJECT);
2966 reject += LRA_MAX_REJECT;
2970 if (! (MEM_P (op) && offmemok)
2971 && ! (const_to_mem && constmemok))
2973 /* We prefer to reload pseudos over reloading other
2974 things, since such reloads may be able to be
2975 eliminated later. So bump REJECT in other cases.
2976 Don't do this in the case where we are forcing a
2977 constant into memory and it will then win since
2978 we don't want to have a different alternative
2979 match then. */
2980 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2982 if (lra_dump_file != NULL)
2983 fprintf
2984 (lra_dump_file,
2985 " %d Non-pseudo reload: reject+=2\n",
2986 nop);
2987 reject += 2;
2990 if (! no_regs_p)
2991 reload_nregs
2992 += ira_reg_class_max_nregs[this_alternative][mode];
2994 if (SMALL_REGISTER_CLASS_P (this_alternative))
2996 if (lra_dump_file != NULL)
2997 fprintf
2998 (lra_dump_file,
2999 " %d Small class reload: reject+=%d\n",
3000 nop, LRA_LOSER_COST_FACTOR / 2);
3001 reject += LRA_LOSER_COST_FACTOR / 2;
3005 /* We are trying to spill pseudo into memory. It is
3006 usually more costly than moving to a hard register
3007 although it might takes the same number of
3008 reloads.
3010 Non-pseudo spill may happen also. Suppose a target allows both
3011 register and memory in the operand constraint alternatives,
3012 then it's typical that an eliminable register has a substition
3013 of "base + offset" which can either be reloaded by a simple
3014 "new_reg <= base + offset" which will match the register
3015 constraint, or a similar reg addition followed by further spill
3016 to and reload from memory which will match the memory
3017 constraint, but this memory spill will be much more costly
3018 usually.
3020 Code below increases the reject for both pseudo and non-pseudo
3021 spill. */
3022 if (no_regs_p
3023 && !(MEM_P (op) && offmemok)
3024 && !(REG_P (op) && hard_regno[nop] < 0))
3026 if (lra_dump_file != NULL)
3027 fprintf
3028 (lra_dump_file,
3029 " %d Spill %spseudo into memory: reject+=3\n",
3030 nop, REG_P (op) ? "" : "Non-");
3031 reject += 3;
3032 if (VECTOR_MODE_P (mode))
3034 /* Spilling vectors into memory is usually more
3035 costly as they contain big values. */
3036 if (lra_dump_file != NULL)
3037 fprintf
3038 (lra_dump_file,
3039 " %d Spill vector pseudo: reject+=2\n",
3040 nop);
3041 reject += 2;
3045 /* When we use an operand requiring memory in given
3046 alternative, the insn should write *and* read the
3047 value to/from memory it is costly in comparison with
3048 an insn alternative which does not use memory
3049 (e.g. register or immediate operand). We exclude
3050 memory operand for such case as we can satisfy the
3051 memory constraints by reloading address. */
3052 if (no_regs_p && offmemok && !MEM_P (op))
3054 if (lra_dump_file != NULL)
3055 fprintf
3056 (lra_dump_file,
3057 " Using memory insn operand %d: reject+=3\n",
3058 nop);
3059 reject += 3;
3062 /* If reload requires moving value through secondary
3063 memory, it will need one more insn at least. */
3064 if (this_alternative != NO_REGS
3065 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
3066 && ((curr_static_id->operand[nop].type != OP_OUT
3067 && targetm.secondary_memory_needed (GET_MODE (op), cl,
3068 this_alternative))
3069 || (curr_static_id->operand[nop].type != OP_IN
3070 && (targetm.secondary_memory_needed
3071 (GET_MODE (op), this_alternative, cl)))))
3072 losers++;
3074 if (MEM_P (op) && offmemok)
3075 addr_losers++;
3076 else
3078 /* Input reloads can be inherited more often than
3079 output reloads can be removed, so penalize output
3080 reloads. */
3081 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
3083 if (lra_dump_file != NULL)
3084 fprintf
3085 (lra_dump_file,
3086 " %d Non input pseudo reload: reject++\n",
3087 nop);
3088 reject++;
3091 if (curr_static_id->operand[nop].type == OP_INOUT)
3093 if (lra_dump_file != NULL)
3094 fprintf
3095 (lra_dump_file,
3096 " %d Input/Output reload: reject+=%d\n",
3097 nop, LRA_LOSER_COST_FACTOR);
3098 reject += LRA_LOSER_COST_FACTOR;
3103 if (early_clobber_p && ! scratch_p)
3105 if (lra_dump_file != NULL)
3106 fprintf (lra_dump_file,
3107 " %d Early clobber: reject++\n", nop);
3108 reject++;
3110 /* ??? We check early clobbers after processing all operands
3111 (see loop below) and there we update the costs more.
3112 Should we update the cost (may be approximately) here
3113 because of early clobber register reloads or it is a rare
3114 or non-important thing to be worth to do it. */
3115 overall = (losers * LRA_LOSER_COST_FACTOR + reject
3116 - (addr_losers == losers ? static_reject : 0));
3117 if ((best_losers == 0 || losers != 0) && best_overall < overall)
3119 if (lra_dump_file != NULL)
3120 fprintf (lra_dump_file,
3121 " overall=%d,losers=%d -- refuse\n",
3122 overall, losers);
3123 goto fail;
3126 if (update_and_check_small_class_inputs (nop, nalt,
3127 this_alternative))
3129 if (lra_dump_file != NULL)
3130 fprintf (lra_dump_file,
3131 " not enough small class regs -- refuse\n");
3132 goto fail;
3134 curr_alt[nop] = this_alternative;
3135 curr_alt_set[nop] = this_alternative_set;
3136 curr_alt_exclude_start_hard_regs[nop]
3137 = this_alternative_exclude_start_hard_regs;
3138 curr_alt_win[nop] = this_alternative_win;
3139 curr_alt_match_win[nop] = this_alternative_match_win;
3140 curr_alt_offmemok[nop] = this_alternative_offmemok;
3141 curr_alt_matches[nop] = this_alternative_matches;
3143 if (this_alternative_matches >= 0
3144 && !did_match && !this_alternative_win)
3145 curr_alt_win[this_alternative_matches] = false;
3147 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
3148 early_clobbered_nops[early_clobbered_regs_num++] = nop;
3151 if (curr_insn_set != NULL_RTX && n_operands == 2
3152 /* Prevent processing non-move insns. */
3153 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
3154 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
3155 && ((! curr_alt_win[0] && ! curr_alt_win[1]
3156 && REG_P (no_subreg_reg_operand[0])
3157 && REG_P (no_subreg_reg_operand[1])
3158 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
3159 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
3160 || (! curr_alt_win[0] && curr_alt_win[1]
3161 && REG_P (no_subreg_reg_operand[1])
3162 /* Check that we reload memory not the memory
3163 address. */
3164 && ! (curr_alt_offmemok[0]
3165 && MEM_P (no_subreg_reg_operand[0]))
3166 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
3167 || (curr_alt_win[0] && ! curr_alt_win[1]
3168 && REG_P (no_subreg_reg_operand[0])
3169 /* Check that we reload memory not the memory
3170 address. */
3171 && ! (curr_alt_offmemok[1]
3172 && MEM_P (no_subreg_reg_operand[1]))
3173 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
3174 && (! CONST_POOL_OK_P (curr_operand_mode[1],
3175 no_subreg_reg_operand[1])
3176 || (targetm.preferred_reload_class
3177 (no_subreg_reg_operand[1],
3178 (enum reg_class) curr_alt[1]) != NO_REGS))
3179 /* If it is a result of recent elimination in move
3180 insn we can transform it into an add still by
3181 using this alternative. */
3182 && GET_CODE (no_subreg_reg_operand[1]) != PLUS
3183 /* Likewise if the source has been replaced with an
3184 equivalent value. This only happens once -- the reload
3185 will use the equivalent value instead of the register it
3186 replaces -- so there should be no danger of cycling. */
3187 && !equiv_substition_p[1])))
3189 /* We have a move insn and a new reload insn will be similar
3190 to the current insn. We should avoid such situation as
3191 it results in LRA cycling. */
3192 if (lra_dump_file != NULL)
3193 fprintf (lra_dump_file,
3194 " Cycle danger: overall += LRA_MAX_REJECT\n");
3195 overall += LRA_MAX_REJECT;
3197 ok_p = true;
3198 curr_alt_dont_inherit_ops_num = 0;
3199 for (nop = 0; nop < early_clobbered_regs_num; nop++)
3201 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
3202 HARD_REG_SET temp_set;
3204 i = early_clobbered_nops[nop];
3205 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
3206 || hard_regno[i] < 0)
3207 continue;
3208 lra_assert (operand_reg[i] != NULL_RTX);
3209 clobbered_hard_regno = hard_regno[i];
3210 CLEAR_HARD_REG_SET (temp_set);
3211 add_to_hard_reg_set (&temp_set, GET_MODE (*curr_id->operand_loc[i]),
3212 clobbered_hard_regno);
3213 first_conflict_j = last_conflict_j = -1;
3214 for (j = 0; j < n_operands; j++)
3215 if (j == i
3216 /* We don't want process insides of match_operator and
3217 match_parallel because otherwise we would process
3218 their operands once again generating a wrong
3219 code. */
3220 || curr_static_id->operand[j].is_operator)
3221 continue;
3222 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
3223 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
3224 continue;
3225 /* If we don't reload j-th operand, check conflicts. */
3226 else if ((curr_alt_win[j] || curr_alt_match_win[j])
3227 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
3229 if (first_conflict_j < 0)
3230 first_conflict_j = j;
3231 last_conflict_j = j;
3232 /* Both the earlyclobber operand and conflicting operand
3233 cannot both be user defined hard registers. */
3234 if (HARD_REGISTER_P (operand_reg[i])
3235 && REG_USERVAR_P (operand_reg[i])
3236 && operand_reg[j] != NULL_RTX
3237 && HARD_REGISTER_P (operand_reg[j])
3238 && REG_USERVAR_P (operand_reg[j]))
3240 /* For asm, let curr_insn_transform diagnose it. */
3241 if (INSN_CODE (curr_insn) < 0)
3242 return false;
3243 fatal_insn ("unable to generate reloads for "
3244 "impossible constraints:", curr_insn);
3247 if (last_conflict_j < 0)
3248 continue;
3250 /* If an earlyclobber operand conflicts with another non-matching
3251 operand (ie, they have been assigned the same hard register),
3252 then it is better to reload the other operand, as there may
3253 exist yet another operand with a matching constraint associated
3254 with the earlyclobber operand. However, if one of the operands
3255 is an explicit use of a hard register, then we must reload the
3256 other non-hard register operand. */
3257 if (HARD_REGISTER_P (operand_reg[i])
3258 || (first_conflict_j == last_conflict_j
3259 && operand_reg[last_conflict_j] != NULL_RTX
3260 && !curr_alt_match_win[last_conflict_j]
3261 && !HARD_REGISTER_P (operand_reg[last_conflict_j])))
3263 curr_alt_win[last_conflict_j] = false;
3264 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
3265 = last_conflict_j;
3266 losers++;
3267 if (lra_dump_file != NULL)
3268 fprintf
3269 (lra_dump_file,
3270 " %d Conflict early clobber reload: reject--\n",
3273 else
3275 /* We need to reload early clobbered register and the
3276 matched registers. */
3277 for (j = 0; j < n_operands; j++)
3278 if (curr_alt_matches[j] == i)
3280 curr_alt_match_win[j] = false;
3281 losers++;
3282 overall += LRA_LOSER_COST_FACTOR;
3284 if (! curr_alt_match_win[i])
3285 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
3286 else
3288 /* Remember pseudos used for match reloads are never
3289 inherited. */
3290 lra_assert (curr_alt_matches[i] >= 0);
3291 curr_alt_win[curr_alt_matches[i]] = false;
3293 curr_alt_win[i] = curr_alt_match_win[i] = false;
3294 losers++;
3295 if (lra_dump_file != NULL)
3296 fprintf
3297 (lra_dump_file,
3298 " %d Matched conflict early clobber reloads: "
3299 "reject--\n",
3302 /* Early clobber was already reflected in REJECT. */
3303 if (!matching_early_clobber[i])
3305 lra_assert (reject > 0);
3306 reject--;
3307 matching_early_clobber[i] = 1;
3309 overall += LRA_LOSER_COST_FACTOR - 1;
3311 if (lra_dump_file != NULL)
3312 fprintf (lra_dump_file, " overall=%d,losers=%d,rld_nregs=%d\n",
3313 overall, losers, reload_nregs);
3315 /* If this alternative can be made to work by reloading, and it
3316 needs less reloading than the others checked so far, record
3317 it as the chosen goal for reloading. */
3318 if ((best_losers != 0 && losers == 0)
3319 || (((best_losers == 0 && losers == 0)
3320 || (best_losers != 0 && losers != 0))
3321 && (best_overall > overall
3322 || (best_overall == overall
3323 /* If the cost of the reloads is the same,
3324 prefer alternative which requires minimal
3325 number of reload regs. */
3326 && (reload_nregs < best_reload_nregs
3327 || (reload_nregs == best_reload_nregs
3328 && (best_reload_sum < reload_sum
3329 || (best_reload_sum == reload_sum
3330 && nalt < goal_alt_number))))))))
3332 for (nop = 0; nop < n_operands; nop++)
3334 goal_alt_win[nop] = curr_alt_win[nop];
3335 goal_alt_match_win[nop] = curr_alt_match_win[nop];
3336 goal_alt_matches[nop] = curr_alt_matches[nop];
3337 goal_alt[nop] = curr_alt[nop];
3338 goal_alt_exclude_start_hard_regs[nop]
3339 = curr_alt_exclude_start_hard_regs[nop];
3340 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
3342 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
3343 goal_reuse_alt_p = curr_reuse_alt_p;
3344 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
3345 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
3346 goal_alt_swapped = curr_swapped;
3347 goal_alt_out_sp_reload_p = curr_alt_out_sp_reload_p;
3348 best_overall = overall;
3349 best_losers = losers;
3350 best_reload_nregs = reload_nregs;
3351 best_reload_sum = reload_sum;
3352 goal_alt_number = nalt;
3354 if (losers == 0)
3355 /* Everything is satisfied. Do not process alternatives
3356 anymore. */
3357 break;
3358 fail:
3361 return ok_p;
3364 /* Make reload base reg from address AD. */
3365 static rtx
3366 base_to_reg (struct address_info *ad)
3368 enum reg_class cl;
3369 int code = -1;
3370 rtx new_inner = NULL_RTX;
3371 rtx new_reg = NULL_RTX;
3372 rtx_insn *insn;
3373 rtx_insn *last_insn = get_last_insn();
3375 lra_assert (ad->disp == ad->disp_term);
3376 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
3377 get_index_code (ad));
3378 new_reg = lra_create_new_reg (GET_MODE (*ad->base), NULL_RTX, cl, NULL,
3379 "base");
3380 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
3381 ad->disp_term == NULL
3382 ? const0_rtx
3383 : *ad->disp_term);
3384 if (!valid_address_p (ad->mode, new_inner, ad->as))
3385 return NULL_RTX;
3386 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base));
3387 code = recog_memoized (insn);
3388 if (code < 0)
3390 delete_insns_since (last_insn);
3391 return NULL_RTX;
3394 return new_inner;
3397 /* Make reload base reg + DISP from address AD. Return the new pseudo. */
3398 static rtx
3399 base_plus_disp_to_reg (struct address_info *ad, rtx disp)
3401 enum reg_class cl;
3402 rtx new_reg;
3404 lra_assert (ad->base == ad->base_term);
3405 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
3406 get_index_code (ad));
3407 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX, cl, NULL,
3408 "base + disp");
3409 lra_emit_add (new_reg, *ad->base_term, disp);
3410 return new_reg;
3413 /* Make reload of index part of address AD. Return the new
3414 pseudo. */
3415 static rtx
3416 index_part_to_reg (struct address_info *ad, enum reg_class index_class)
3418 rtx new_reg;
3420 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
3421 index_class, NULL, "index term");
3422 expand_mult (GET_MODE (*ad->index), *ad->index_term,
3423 GEN_INT (get_index_scale (ad)), new_reg, 1);
3424 return new_reg;
3427 /* Return true if we can add a displacement to address AD, even if that
3428 makes the address invalid. The fix-up code requires any new address
3429 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
3430 static bool
3431 can_add_disp_p (struct address_info *ad)
3433 return (!ad->autoinc_p
3434 && ad->segment == NULL
3435 && ad->base == ad->base_term
3436 && ad->disp == ad->disp_term);
3439 /* Make equiv substitution in address AD. Return true if a substitution
3440 was made. */
3441 static bool
3442 equiv_address_substitution (struct address_info *ad)
3444 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
3445 poly_int64 disp;
3446 HOST_WIDE_INT scale;
3447 bool change_p;
3449 base_term = strip_subreg (ad->base_term);
3450 if (base_term == NULL)
3451 base_reg = new_base_reg = NULL_RTX;
3452 else
3454 base_reg = *base_term;
3455 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
3457 index_term = strip_subreg (ad->index_term);
3458 if (index_term == NULL)
3459 index_reg = new_index_reg = NULL_RTX;
3460 else
3462 index_reg = *index_term;
3463 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
3465 if (base_reg == new_base_reg && index_reg == new_index_reg)
3466 return false;
3467 disp = 0;
3468 change_p = false;
3469 if (lra_dump_file != NULL)
3471 fprintf (lra_dump_file, "Changing address in insn %d ",
3472 INSN_UID (curr_insn));
3473 dump_value_slim (lra_dump_file, *ad->outer, 1);
3475 if (base_reg != new_base_reg)
3477 poly_int64 offset;
3478 if (REG_P (new_base_reg))
3480 *base_term = new_base_reg;
3481 change_p = true;
3483 else if (GET_CODE (new_base_reg) == PLUS
3484 && REG_P (XEXP (new_base_reg, 0))
3485 && poly_int_rtx_p (XEXP (new_base_reg, 1), &offset)
3486 && can_add_disp_p (ad))
3488 disp += offset;
3489 *base_term = XEXP (new_base_reg, 0);
3490 change_p = true;
3492 if (ad->base_term2 != NULL)
3493 *ad->base_term2 = *ad->base_term;
3495 if (index_reg != new_index_reg)
3497 poly_int64 offset;
3498 if (REG_P (new_index_reg))
3500 *index_term = new_index_reg;
3501 change_p = true;
3503 else if (GET_CODE (new_index_reg) == PLUS
3504 && REG_P (XEXP (new_index_reg, 0))
3505 && poly_int_rtx_p (XEXP (new_index_reg, 1), &offset)
3506 && can_add_disp_p (ad)
3507 && (scale = get_index_scale (ad)))
3509 disp += offset * scale;
3510 *index_term = XEXP (new_index_reg, 0);
3511 change_p = true;
3514 if (maybe_ne (disp, 0))
3516 if (ad->disp != NULL)
3517 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
3518 else
3520 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
3521 update_address (ad);
3523 change_p = true;
3525 if (lra_dump_file != NULL)
3527 if (! change_p)
3528 fprintf (lra_dump_file, " -- no change\n");
3529 else
3531 fprintf (lra_dump_file, " on equiv ");
3532 dump_value_slim (lra_dump_file, *ad->outer, 1);
3533 fprintf (lra_dump_file, "\n");
3536 return change_p;
3539 /* Skip all modifiers and whitespaces in constraint STR and return the
3540 result. */
3541 static const char *
3542 skip_constraint_modifiers (const char *str)
3544 for (;;str++)
3545 switch (*str)
3547 case '+': case '&' : case '=': case '*': case ' ': case '\t':
3548 case '$': case '^' : case '%': case '?': case '!':
3549 break;
3550 default: return str;
3554 /* Takes a string of 0 or more comma-separated constraints. When more
3555 than one constraint is present, evaluate whether they all correspond
3556 to a single, repeated constraint (e.g. "r,r") or whether we have
3557 more than one distinct constraints (e.g. "r,m"). */
3558 static bool
3559 constraint_unique (const char *cstr)
3561 enum constraint_num ca, cb;
3562 ca = CONSTRAINT__UNKNOWN;
3563 for (;;)
3565 cstr = skip_constraint_modifiers (cstr);
3566 if (*cstr == '\0' || *cstr == ',')
3567 cb = CONSTRAINT_X;
3568 else
3570 cb = lookup_constraint (cstr);
3571 if (cb == CONSTRAINT__UNKNOWN)
3572 return false;
3573 cstr += CONSTRAINT_LEN (cstr[0], cstr);
3575 /* Handle the first iteration of the loop. */
3576 if (ca == CONSTRAINT__UNKNOWN)
3577 ca = cb;
3578 /* Handle the general case of comparing ca with subsequent
3579 constraints. */
3580 else if (ca != cb)
3581 return false;
3582 if (*cstr == '\0')
3583 return true;
3584 if (*cstr == ',')
3585 cstr += 1;
3589 /* Major function to make reloads for an address in operand NOP or
3590 check its correctness (If CHECK_ONLY_P is true). The supported
3591 cases are:
3593 1) an address that existed before LRA started, at which point it
3594 must have been valid. These addresses are subject to elimination
3595 and may have become invalid due to the elimination offset being out
3596 of range.
3598 2) an address created by forcing a constant to memory
3599 (force_const_to_mem). The initial form of these addresses might
3600 not be valid, and it is this function's job to make them valid.
3602 3) a frame address formed from a register and a (possibly zero)
3603 constant offset. As above, these addresses might not be valid and
3604 this function must make them so.
3606 Add reloads to the lists *BEFORE and *AFTER. We might need to add
3607 reloads to *AFTER because of inc/dec, {pre, post} modify in the
3608 address. Return true for any RTL change.
3610 The function is a helper function which does not produce all
3611 transformations (when CHECK_ONLY_P is false) which can be
3612 necessary. It does just basic steps. To do all necessary
3613 transformations use function process_address. */
3614 static bool
3615 process_address_1 (int nop, bool check_only_p,
3616 rtx_insn **before, rtx_insn **after)
3618 struct address_info ad;
3619 rtx new_reg;
3620 HOST_WIDE_INT scale;
3621 rtx op = *curr_id->operand_loc[nop];
3622 rtx mem = extract_mem_from_operand (op);
3623 const char *constraint;
3624 enum constraint_num cn;
3625 bool change_p = false;
3627 if (MEM_P (mem)
3628 && GET_MODE (mem) == BLKmode
3629 && GET_CODE (XEXP (mem, 0)) == SCRATCH)
3630 return false;
3632 constraint
3633 = skip_constraint_modifiers (curr_static_id->operand[nop].constraint);
3634 if (IN_RANGE (constraint[0], '0', '9'))
3636 char *end;
3637 unsigned long dup = strtoul (constraint, &end, 10);
3638 constraint
3639 = skip_constraint_modifiers (curr_static_id->operand[dup].constraint);
3641 cn = lookup_constraint (*constraint == '\0' ? "X" : constraint);
3642 /* If we have several alternatives or/and several constraints in an
3643 alternative and we can not say at this stage what constraint will be used,
3644 use unknown constraint. The exception is an address constraint. If
3645 operand has one address constraint, probably all others constraints are
3646 address ones. */
3647 if (constraint[0] != '\0' && get_constraint_type (cn) != CT_ADDRESS
3648 && !constraint_unique (constraint))
3649 cn = CONSTRAINT__UNKNOWN;
3650 if (insn_extra_address_constraint (cn)
3651 /* When we find an asm operand with an address constraint that
3652 doesn't satisfy address_operand to begin with, we clear
3653 is_address, so that we don't try to make a non-address fit.
3654 If the asm statement got this far, it's because other
3655 constraints are available, and we'll use them, disregarding
3656 the unsatisfiable address ones. */
3657 && curr_static_id->operand[nop].is_address)
3658 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
3659 /* Do not attempt to decompose arbitrary addresses generated by combine
3660 for asm operands with loose constraints, e.g 'X'.
3661 Need to extract memory from op for special memory constraint,
3662 i.e. bcst_mem_operand in i386 backend. */
3663 else if (MEM_P (mem)
3664 && !(INSN_CODE (curr_insn) < 0
3665 && get_constraint_type (cn) == CT_FIXED_FORM
3666 && constraint_satisfied_p (op, cn)))
3667 decompose_mem_address (&ad, mem);
3668 else if (GET_CODE (op) == SUBREG
3669 && MEM_P (SUBREG_REG (op)))
3670 decompose_mem_address (&ad, SUBREG_REG (op));
3671 else
3672 return false;
3673 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
3674 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
3675 when INDEX_REG_CLASS is a single register class. */
3676 enum reg_class index_cl = index_reg_class (curr_insn);
3677 if (ad.base_term != NULL
3678 && ad.index_term != NULL
3679 && ira_class_hard_regs_num[index_cl] == 1
3680 && REG_P (*ad.base_term)
3681 && REG_P (*ad.index_term)
3682 && in_class_p (*ad.base_term, index_cl, NULL)
3683 && ! in_class_p (*ad.index_term, index_cl, NULL))
3685 std::swap (ad.base, ad.index);
3686 std::swap (ad.base_term, ad.index_term);
3688 if (! check_only_p)
3689 change_p = equiv_address_substitution (&ad);
3690 if (ad.base_term != NULL
3691 && (process_addr_reg
3692 (ad.base_term, check_only_p, before,
3693 (ad.autoinc_p
3694 && !(REG_P (*ad.base_term)
3695 && find_regno_note (curr_insn, REG_DEAD,
3696 REGNO (*ad.base_term)) != NULL_RTX)
3697 ? after : NULL),
3698 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3699 get_index_code (&ad), curr_insn))))
3701 change_p = true;
3702 if (ad.base_term2 != NULL)
3703 *ad.base_term2 = *ad.base_term;
3705 if (ad.index_term != NULL
3706 && process_addr_reg (ad.index_term, check_only_p,
3707 before, NULL, index_cl))
3708 change_p = true;
3710 /* Target hooks sometimes don't treat extra-constraint addresses as
3711 legitimate address_operands, so handle them specially. */
3712 if (insn_extra_address_constraint (cn)
3713 && satisfies_address_constraint_p (&ad, cn))
3714 return change_p;
3716 if (check_only_p)
3717 return change_p;
3719 /* There are three cases where the shape of *AD.INNER may now be invalid:
3721 1) the original address was valid, but either elimination or
3722 equiv_address_substitution was applied and that made
3723 the address invalid.
3725 2) the address is an invalid symbolic address created by
3726 force_const_to_mem.
3728 3) the address is a frame address with an invalid offset.
3730 4) the address is a frame address with an invalid base.
3732 All these cases involve a non-autoinc address, so there is no
3733 point revalidating other types. */
3734 if (ad.autoinc_p || valid_address_p (op, &ad, cn))
3735 return change_p;
3737 /* Any index existed before LRA started, so we can assume that the
3738 presence and shape of the index is valid. */
3739 push_to_sequence (*before);
3740 lra_assert (ad.disp == ad.disp_term);
3741 if (ad.base == NULL)
3743 if (ad.index == NULL)
3745 rtx_insn *insn;
3746 rtx_insn *last = get_last_insn ();
3747 int code = -1;
3748 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3749 SCRATCH, SCRATCH,
3750 curr_insn);
3751 rtx addr = *ad.inner;
3753 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, NULL, "addr");
3754 if (HAVE_lo_sum)
3756 /* addr => lo_sum (new_base, addr), case (2) above. */
3757 insn = emit_insn (gen_rtx_SET
3758 (new_reg,
3759 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
3760 code = recog_memoized (insn);
3761 if (code >= 0)
3763 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
3764 if (!valid_address_p (op, &ad, cn))
3766 /* Try to put lo_sum into register. */
3767 insn = emit_insn (gen_rtx_SET
3768 (new_reg,
3769 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
3770 code = recog_memoized (insn);
3771 if (code >= 0)
3773 *ad.inner = new_reg;
3774 if (!valid_address_p (op, &ad, cn))
3776 *ad.inner = addr;
3777 code = -1;
3783 if (code < 0)
3784 delete_insns_since (last);
3787 if (code < 0)
3789 /* addr => new_base, case (2) above. */
3790 lra_emit_move (new_reg, addr);
3792 for (insn = last == NULL_RTX ? get_insns () : NEXT_INSN (last);
3793 insn != NULL_RTX;
3794 insn = NEXT_INSN (insn))
3795 if (recog_memoized (insn) < 0)
3796 break;
3797 if (insn != NULL_RTX)
3799 /* Do nothing if we cannot generate right insns.
3800 This is analogous to reload pass behavior. */
3801 delete_insns_since (last);
3802 end_sequence ();
3803 return false;
3805 *ad.inner = new_reg;
3808 else
3810 /* index * scale + disp => new base + index * scale,
3811 case (1) above. */
3812 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3813 GET_CODE (*ad.index),
3814 curr_insn);
3816 lra_assert (index_cl != NO_REGS);
3817 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, NULL, "disp");
3818 lra_emit_move (new_reg, *ad.disp);
3819 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3820 new_reg, *ad.index);
3823 else if (ad.index == NULL)
3825 int regno;
3826 enum reg_class cl;
3827 rtx set;
3828 rtx_insn *insns, *last_insn;
3829 /* Try to reload base into register only if the base is invalid
3830 for the address but with valid offset, case (4) above. */
3831 start_sequence ();
3832 new_reg = base_to_reg (&ad);
3834 /* base + disp => new base, cases (1) and (3) above. */
3835 /* Another option would be to reload the displacement into an
3836 index register. However, postreload has code to optimize
3837 address reloads that have the same base and different
3838 displacements, so reloading into an index register would
3839 not necessarily be a win. */
3840 if (new_reg == NULL_RTX)
3842 /* See if the target can split the displacement into a
3843 legitimate new displacement from a local anchor. */
3844 gcc_assert (ad.disp == ad.disp_term);
3845 poly_int64 orig_offset;
3846 rtx offset1, offset2;
3847 if (poly_int_rtx_p (*ad.disp, &orig_offset)
3848 && targetm.legitimize_address_displacement (&offset1, &offset2,
3849 orig_offset,
3850 ad.mode))
3852 new_reg = base_plus_disp_to_reg (&ad, offset1);
3853 new_reg = gen_rtx_PLUS (GET_MODE (new_reg), new_reg, offset2);
3855 else
3856 new_reg = base_plus_disp_to_reg (&ad, *ad.disp);
3858 insns = get_insns ();
3859 last_insn = get_last_insn ();
3860 /* If we generated at least two insns, try last insn source as
3861 an address. If we succeed, we generate one less insn. */
3862 if (REG_P (new_reg)
3863 && last_insn != insns
3864 && (set = single_set (last_insn)) != NULL_RTX
3865 && GET_CODE (SET_SRC (set)) == PLUS
3866 && REG_P (XEXP (SET_SRC (set), 0))
3867 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3869 *ad.inner = SET_SRC (set);
3870 if (valid_address_p (op, &ad, cn))
3872 *ad.base_term = XEXP (SET_SRC (set), 0);
3873 *ad.disp_term = XEXP (SET_SRC (set), 1);
3874 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3875 get_index_code (&ad), curr_insn);
3876 regno = REGNO (*ad.base_term);
3877 if (regno >= FIRST_PSEUDO_REGISTER
3878 && cl != lra_get_allocno_class (regno))
3879 lra_change_class (regno, cl, " Change to", true);
3880 new_reg = SET_SRC (set);
3881 delete_insns_since (PREV_INSN (last_insn));
3884 end_sequence ();
3885 emit_insn (insns);
3886 *ad.inner = new_reg;
3888 else if (ad.disp_term != NULL)
3890 /* base + scale * index + disp => new base + scale * index,
3891 case (1) above. */
3892 gcc_assert (ad.disp == ad.disp_term);
3893 new_reg = base_plus_disp_to_reg (&ad, *ad.disp);
3894 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3895 new_reg, *ad.index);
3897 else if ((scale = get_index_scale (&ad)) == 1)
3899 /* The last transformation to one reg will be made in
3900 curr_insn_transform function. */
3901 end_sequence ();
3902 return false;
3904 else if (scale != 0)
3906 /* base + scale * index => base + new_reg,
3907 case (1) above.
3908 Index part of address may become invalid. For example, we
3909 changed pseudo on the equivalent memory and a subreg of the
3910 pseudo onto the memory of different mode for which the scale is
3911 prohibitted. */
3912 new_reg = index_part_to_reg (&ad, index_cl);
3913 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3914 *ad.base_term, new_reg);
3916 else
3918 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3919 SCRATCH, SCRATCH,
3920 curr_insn);
3921 rtx addr = *ad.inner;
3923 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, NULL, "addr");
3924 /* addr => new_base. */
3925 lra_emit_move (new_reg, addr);
3926 *ad.inner = new_reg;
3928 *before = get_insns ();
3929 end_sequence ();
3930 return true;
3933 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3934 Use process_address_1 as a helper function. Return true for any
3935 RTL changes.
3937 If CHECK_ONLY_P is true, just check address correctness. Return
3938 false if the address correct. */
3939 static bool
3940 process_address (int nop, bool check_only_p,
3941 rtx_insn **before, rtx_insn **after)
3943 bool res = false;
3945 while (process_address_1 (nop, check_only_p, before, after))
3947 if (check_only_p)
3948 return true;
3949 res = true;
3951 return res;
3954 /* Emit insns to reload VALUE into a new register. VALUE is an
3955 auto-increment or auto-decrement RTX whose operand is a register or
3956 memory location; so reloading involves incrementing that location.
3957 IN is either identical to VALUE, or some cheaper place to reload
3958 value being incremented/decremented from.
3960 INC_AMOUNT is the number to increment or decrement by (always
3961 positive and ignored for POST_MODIFY/PRE_MODIFY).
3963 Return pseudo containing the result. */
3964 static rtx
3965 emit_inc (enum reg_class new_rclass, rtx in, rtx value, poly_int64 inc_amount)
3967 /* REG or MEM to be copied and incremented. */
3968 rtx incloc = XEXP (value, 0);
3969 /* Nonzero if increment after copying. */
3970 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3971 || GET_CODE (value) == POST_MODIFY);
3972 rtx_insn *last;
3973 rtx inc;
3974 rtx_insn *add_insn;
3975 int code;
3976 rtx real_in = in == value ? incloc : in;
3977 rtx result;
3978 bool plus_p = true;
3980 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3982 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3983 || GET_CODE (XEXP (value, 1)) == MINUS);
3984 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3985 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3986 inc = XEXP (XEXP (value, 1), 1);
3988 else
3990 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3991 inc_amount = -inc_amount;
3993 inc = gen_int_mode (inc_amount, GET_MODE (value));
3996 if (! post && REG_P (incloc))
3997 result = incloc;
3998 else
3999 result = lra_create_new_reg (GET_MODE (value), value, new_rclass, NULL,
4000 "INC/DEC result");
4002 if (real_in != result)
4004 /* First copy the location to the result register. */
4005 lra_assert (REG_P (result));
4006 emit_insn (gen_move_insn (result, real_in));
4009 /* We suppose that there are insns to add/sub with the constant
4010 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
4011 old reload worked with this assumption. If the assumption
4012 becomes wrong, we should use approach in function
4013 base_plus_disp_to_reg. */
4014 if (in == value)
4016 /* See if we can directly increment INCLOC. */
4017 last = get_last_insn ();
4018 add_insn = emit_insn (plus_p
4019 ? gen_add2_insn (incloc, inc)
4020 : gen_sub2_insn (incloc, inc));
4022 code = recog_memoized (add_insn);
4023 if (code >= 0)
4025 if (! post && result != incloc)
4026 emit_insn (gen_move_insn (result, incloc));
4027 return result;
4029 delete_insns_since (last);
4032 /* If couldn't do the increment directly, must increment in RESULT.
4033 The way we do this depends on whether this is pre- or
4034 post-increment. For pre-increment, copy INCLOC to the reload
4035 register, increment it there, then save back. */
4036 if (! post)
4038 if (real_in != result)
4039 emit_insn (gen_move_insn (result, real_in));
4040 if (plus_p)
4041 emit_insn (gen_add2_insn (result, inc));
4042 else
4043 emit_insn (gen_sub2_insn (result, inc));
4044 if (result != incloc)
4045 emit_insn (gen_move_insn (incloc, result));
4047 else
4049 /* Post-increment.
4051 Because this might be a jump insn or a compare, and because
4052 RESULT may not be available after the insn in an input
4053 reload, we must do the incrementing before the insn being
4054 reloaded for.
4056 We have already copied IN to RESULT. Increment the copy in
4057 RESULT, save that back, then decrement RESULT so it has
4058 the original value. */
4059 if (plus_p)
4060 emit_insn (gen_add2_insn (result, inc));
4061 else
4062 emit_insn (gen_sub2_insn (result, inc));
4063 emit_insn (gen_move_insn (incloc, result));
4064 /* Restore non-modified value for the result. We prefer this
4065 way because it does not require an additional hard
4066 register. */
4067 if (plus_p)
4069 poly_int64 offset;
4070 if (poly_int_rtx_p (inc, &offset))
4071 emit_insn (gen_add2_insn (result,
4072 gen_int_mode (-offset,
4073 GET_MODE (result))));
4074 else
4075 emit_insn (gen_sub2_insn (result, inc));
4077 else
4078 emit_insn (gen_add2_insn (result, inc));
4080 return result;
4083 /* Return true if the current move insn does not need processing as we
4084 already know that it satisfies its constraints. */
4085 static bool
4086 simple_move_p (void)
4088 rtx dest, src;
4089 enum reg_class dclass, sclass;
4091 lra_assert (curr_insn_set != NULL_RTX);
4092 dest = SET_DEST (curr_insn_set);
4093 src = SET_SRC (curr_insn_set);
4095 /* If the instruction has multiple sets we need to process it even if it
4096 is single_set. This can happen if one or more of the SETs are dead.
4097 See PR73650. */
4098 if (multiple_sets (curr_insn))
4099 return false;
4101 return ((dclass = get_op_class (dest)) != NO_REGS
4102 && (sclass = get_op_class (src)) != NO_REGS
4103 /* The backend guarantees that register moves of cost 2
4104 never need reloads. */
4105 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
4108 /* Swap operands NOP and NOP + 1. */
4109 static inline void
4110 swap_operands (int nop)
4112 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
4113 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
4114 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
4115 std::swap (equiv_substition_p[nop], equiv_substition_p[nop + 1]);
4116 /* Swap the duplicates too. */
4117 lra_update_dup (curr_id, nop);
4118 lra_update_dup (curr_id, nop + 1);
4121 /* Main entry point of the constraint code: search the body of the
4122 current insn to choose the best alternative. It is mimicking insn
4123 alternative cost calculation model of former reload pass. That is
4124 because machine descriptions were written to use this model. This
4125 model can be changed in future. Make commutative operand exchange
4126 if it is chosen.
4128 if CHECK_ONLY_P is false, do RTL changes to satisfy the
4129 constraints. Return true if any change happened during function
4130 call.
4132 If CHECK_ONLY_P is true then don't do any transformation. Just
4133 check that the insn satisfies all constraints. If the insn does
4134 not satisfy any constraint, return true. */
4135 static bool
4136 curr_insn_transform (bool check_only_p)
4138 int i, j, k;
4139 int n_operands;
4140 int n_alternatives;
4141 int n_outputs;
4142 int commutative;
4143 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
4144 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
4145 signed char outputs[MAX_RECOG_OPERANDS + 1];
4146 rtx_insn *before, *after;
4147 bool alt_p = false;
4148 /* Flag that the insn has been changed through a transformation. */
4149 bool change_p;
4150 bool sec_mem_p;
4151 bool use_sec_mem_p;
4152 int max_regno_before;
4153 int reused_alternative_num;
4155 curr_insn_set = single_set (curr_insn);
4156 if (curr_insn_set != NULL_RTX && simple_move_p ())
4158 /* We assume that the corresponding insn alternative has no
4159 earlier clobbers. If it is not the case, don't define move
4160 cost equal to 2 for the corresponding register classes. */
4161 lra_set_used_insn_alternative (curr_insn, LRA_NON_CLOBBERED_ALT);
4162 return false;
4165 no_input_reloads_p = no_output_reloads_p = false;
4166 goal_alt_number = -1;
4167 change_p = sec_mem_p = false;
4169 /* CALL_INSNs are not allowed to have any output reloads. */
4170 if (CALL_P (curr_insn))
4171 no_output_reloads_p = true;
4173 n_operands = curr_static_id->n_operands;
4174 n_alternatives = curr_static_id->n_alternatives;
4176 /* Just return "no reloads" if insn has no operands with
4177 constraints. */
4178 if (n_operands == 0 || n_alternatives == 0)
4179 return false;
4181 max_regno_before = max_reg_num ();
4183 for (i = 0; i < n_operands; i++)
4185 goal_alt_matched[i][0] = -1;
4186 goal_alt_matches[i] = -1;
4189 commutative = curr_static_id->commutative;
4191 /* Now see what we need for pseudos that didn't get hard regs or got
4192 the wrong kind of hard reg. For this, we must consider all the
4193 operands together against the register constraints. */
4195 best_losers = best_overall = INT_MAX;
4196 best_reload_sum = 0;
4198 curr_swapped = false;
4199 goal_alt_swapped = false;
4201 if (! check_only_p)
4202 /* Make equivalence substitution and memory subreg elimination
4203 before address processing because an address legitimacy can
4204 depend on memory mode. */
4205 for (i = 0; i < n_operands; i++)
4207 rtx op, subst, old;
4208 bool op_change_p = false;
4210 if (curr_static_id->operand[i].is_operator)
4211 continue;
4213 old = op = *curr_id->operand_loc[i];
4214 if (GET_CODE (old) == SUBREG)
4215 old = SUBREG_REG (old);
4216 subst = get_equiv_with_elimination (old, curr_insn);
4217 original_subreg_reg_mode[i] = VOIDmode;
4218 equiv_substition_p[i] = false;
4219 if (subst != old)
4221 equiv_substition_p[i] = true;
4222 subst = copy_rtx (subst);
4223 lra_assert (REG_P (old));
4224 if (GET_CODE (op) != SUBREG)
4225 *curr_id->operand_loc[i] = subst;
4226 else
4228 SUBREG_REG (op) = subst;
4229 if (GET_MODE (subst) == VOIDmode)
4230 original_subreg_reg_mode[i] = GET_MODE (old);
4232 if (lra_dump_file != NULL)
4234 fprintf (lra_dump_file,
4235 "Changing pseudo %d in operand %i of insn %u on equiv ",
4236 REGNO (old), i, INSN_UID (curr_insn));
4237 dump_value_slim (lra_dump_file, subst, 1);
4238 fprintf (lra_dump_file, "\n");
4240 op_change_p = change_p = true;
4242 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
4244 change_p = true;
4245 lra_update_dup (curr_id, i);
4249 /* Reload address registers and displacements. We do it before
4250 finding an alternative because of memory constraints. */
4251 before = after = NULL;
4252 for (i = 0; i < n_operands; i++)
4253 if (! curr_static_id->operand[i].is_operator
4254 && process_address (i, check_only_p, &before, &after))
4256 if (check_only_p)
4257 return true;
4258 change_p = true;
4259 lra_update_dup (curr_id, i);
4262 if (change_p)
4263 /* If we've changed the instruction then any alternative that
4264 we chose previously may no longer be valid. */
4265 lra_set_used_insn_alternative (curr_insn, LRA_UNKNOWN_ALT);
4267 if (! check_only_p && curr_insn_set != NULL_RTX
4268 && check_and_process_move (&change_p, &sec_mem_p))
4269 return change_p;
4271 try_swapped:
4273 reused_alternative_num = check_only_p ? LRA_UNKNOWN_ALT : curr_id->used_insn_alternative;
4274 if (lra_dump_file != NULL && reused_alternative_num >= 0)
4275 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
4276 reused_alternative_num, INSN_UID (curr_insn));
4278 if (process_alt_operands (reused_alternative_num))
4279 alt_p = true;
4281 if (check_only_p)
4282 return ! alt_p || best_losers != 0;
4284 /* If insn is commutative (it's safe to exchange a certain pair of
4285 operands) then we need to try each alternative twice, the second
4286 time matching those two operands as if we had exchanged them. To
4287 do this, really exchange them in operands.
4289 If we have just tried the alternatives the second time, return
4290 operands to normal and drop through. */
4292 if (reused_alternative_num < 0 && commutative >= 0)
4294 curr_swapped = !curr_swapped;
4295 if (curr_swapped)
4297 swap_operands (commutative);
4298 goto try_swapped;
4300 else
4301 swap_operands (commutative);
4304 if (! alt_p && ! sec_mem_p)
4306 /* No alternative works with reloads?? */
4307 if (INSN_CODE (curr_insn) >= 0)
4308 fatal_insn ("unable to generate reloads for:", curr_insn);
4309 error_for_asm (curr_insn,
4310 "inconsistent operand constraints in an %<asm%>");
4311 lra_asm_error_p = true;
4312 if (! JUMP_P (curr_insn))
4314 /* Avoid further trouble with this insn. Don't generate use
4315 pattern here as we could use the insn SP offset. */
4316 lra_set_insn_deleted (curr_insn);
4318 else
4320 lra_invalidate_insn_data (curr_insn);
4321 ira_nullify_asm_goto (curr_insn);
4322 lra_update_insn_regno_info (curr_insn);
4324 return true;
4327 /* If the best alternative is with operands 1 and 2 swapped, swap
4328 them. Update the operand numbers of any reloads already
4329 pushed. */
4331 if (goal_alt_swapped)
4333 if (lra_dump_file != NULL)
4334 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
4335 INSN_UID (curr_insn));
4337 /* Swap the duplicates too. */
4338 swap_operands (commutative);
4339 change_p = true;
4342 /* Some targets' TARGET_SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
4343 too conservatively. So we use the secondary memory only if there
4344 is no any alternative without reloads. */
4345 use_sec_mem_p = false;
4346 if (! alt_p)
4347 use_sec_mem_p = true;
4348 else if (sec_mem_p)
4350 for (i = 0; i < n_operands; i++)
4351 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
4352 break;
4353 use_sec_mem_p = i < n_operands;
4356 if (use_sec_mem_p)
4358 int in = -1, out = -1;
4359 rtx new_reg, src, dest, rld;
4360 machine_mode sec_mode, rld_mode;
4362 lra_assert (curr_insn_set != NULL_RTX && sec_mem_p);
4363 dest = SET_DEST (curr_insn_set);
4364 src = SET_SRC (curr_insn_set);
4365 for (i = 0; i < n_operands; i++)
4366 if (*curr_id->operand_loc[i] == dest)
4367 out = i;
4368 else if (*curr_id->operand_loc[i] == src)
4369 in = i;
4370 for (i = 0; i < curr_static_id->n_dups; i++)
4371 if (out < 0 && *curr_id->dup_loc[i] == dest)
4372 out = curr_static_id->dup_num[i];
4373 else if (in < 0 && *curr_id->dup_loc[i] == src)
4374 in = curr_static_id->dup_num[i];
4375 lra_assert (out >= 0 && in >= 0
4376 && curr_static_id->operand[out].type == OP_OUT
4377 && curr_static_id->operand[in].type == OP_IN);
4378 rld = partial_subreg_p (GET_MODE (src), GET_MODE (dest)) ? src : dest;
4379 rld_mode = GET_MODE (rld);
4380 sec_mode = targetm.secondary_memory_needed_mode (rld_mode);
4381 new_reg = lra_create_new_reg (sec_mode, NULL_RTX, NO_REGS, NULL,
4382 "secondary");
4383 /* If the mode is changed, it should be wider. */
4384 lra_assert (!partial_subreg_p (sec_mode, rld_mode));
4385 if (sec_mode != rld_mode)
4387 /* If the target says specifically to use another mode for
4388 secondary memory moves we cannot reuse the original
4389 insn. */
4390 after = emit_spill_move (false, new_reg, dest);
4391 lra_process_new_insns (curr_insn, NULL, after,
4392 "Inserting the sec. move");
4393 /* We may have non null BEFORE here (e.g. after address
4394 processing. */
4395 push_to_sequence (before);
4396 before = emit_spill_move (true, new_reg, src);
4397 emit_insn (before);
4398 before = get_insns ();
4399 end_sequence ();
4400 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
4401 lra_set_insn_deleted (curr_insn);
4403 else if (dest == rld)
4405 *curr_id->operand_loc[out] = new_reg;
4406 lra_update_dup (curr_id, out);
4407 after = emit_spill_move (false, new_reg, dest);
4408 lra_process_new_insns (curr_insn, NULL, after,
4409 "Inserting the sec. move");
4411 else
4413 *curr_id->operand_loc[in] = new_reg;
4414 lra_update_dup (curr_id, in);
4415 /* See comments above. */
4416 push_to_sequence (before);
4417 before = emit_spill_move (true, new_reg, src);
4418 emit_insn (before);
4419 before = get_insns ();
4420 end_sequence ();
4421 lra_process_new_insns (curr_insn, before, NULL,
4422 "Inserting the sec. move");
4424 lra_update_insn_regno_info (curr_insn);
4425 return true;
4428 lra_assert (goal_alt_number >= 0);
4429 lra_set_used_insn_alternative (curr_insn, goal_reuse_alt_p
4430 ? goal_alt_number : LRA_UNKNOWN_ALT);
4432 if (lra_dump_file != NULL)
4434 const char *p;
4436 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
4437 goal_alt_number, INSN_UID (curr_insn));
4438 print_curr_insn_alt (goal_alt_number);
4439 if (INSN_CODE (curr_insn) >= 0
4440 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
4441 fprintf (lra_dump_file, " {%s}", p);
4442 if (maybe_ne (curr_id->sp_offset, 0))
4444 fprintf (lra_dump_file, " (sp_off=");
4445 print_dec (curr_id->sp_offset, lra_dump_file);
4446 fprintf (lra_dump_file, ")");
4448 fprintf (lra_dump_file, "\n");
4451 /* Right now, for any pair of operands I and J that are required to
4452 match, with J < I, goal_alt_matches[I] is J. Add I to
4453 goal_alt_matched[J]. */
4455 for (i = 0; i < n_operands; i++)
4456 if ((j = goal_alt_matches[i]) >= 0)
4458 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
4460 /* We allow matching one output operand and several input
4461 operands. */
4462 lra_assert (k == 0
4463 || (curr_static_id->operand[j].type == OP_OUT
4464 && curr_static_id->operand[i].type == OP_IN
4465 && (curr_static_id->operand
4466 [goal_alt_matched[j][0]].type == OP_IN)));
4467 goal_alt_matched[j][k] = i;
4468 goal_alt_matched[j][k + 1] = -1;
4471 for (i = 0; i < n_operands; i++)
4472 goal_alt_win[i] |= goal_alt_match_win[i];
4474 /* Any constants that aren't allowed and can't be reloaded into
4475 registers are here changed into memory references. */
4476 for (i = 0; i < n_operands; i++)
4477 if (goal_alt_win[i])
4479 int regno;
4480 enum reg_class new_class;
4481 rtx reg = *curr_id->operand_loc[i];
4483 if (GET_CODE (reg) == SUBREG)
4484 reg = SUBREG_REG (reg);
4486 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
4488 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
4490 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
4492 lra_assert (ok_p);
4493 lra_change_class (regno, new_class, " Change to", true);
4497 else
4499 const char *constraint;
4500 char c;
4501 rtx op = *curr_id->operand_loc[i];
4502 rtx subreg = NULL_RTX;
4503 machine_mode mode = curr_operand_mode[i];
4505 if (GET_CODE (op) == SUBREG)
4507 subreg = op;
4508 op = SUBREG_REG (op);
4509 mode = GET_MODE (op);
4512 if (CONST_POOL_OK_P (mode, op)
4513 && ((targetm.preferred_reload_class
4514 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
4515 || no_input_reloads_p))
4517 rtx tem = force_const_mem (mode, op);
4519 change_p = true;
4520 if (subreg != NULL_RTX)
4521 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
4523 *curr_id->operand_loc[i] = tem;
4524 lra_update_dup (curr_id, i);
4525 process_address (i, false, &before, &after);
4527 /* If the alternative accepts constant pool refs directly
4528 there will be no reload needed at all. */
4529 if (subreg != NULL_RTX)
4530 continue;
4531 /* Skip alternatives before the one requested. */
4532 constraint = (curr_static_id->operand_alternative
4533 [goal_alt_number * n_operands + i].constraint);
4534 for (;
4535 (c = *constraint) && c != ',' && c != '#';
4536 constraint += CONSTRAINT_LEN (c, constraint))
4538 enum constraint_num cn = lookup_constraint (constraint);
4539 if ((insn_extra_memory_constraint (cn)
4540 || insn_extra_special_memory_constraint (cn)
4541 || insn_extra_relaxed_memory_constraint (cn))
4542 && satisfies_memory_constraint_p (tem, cn))
4543 break;
4545 if (c == '\0' || c == ',' || c == '#')
4546 continue;
4548 goal_alt_win[i] = true;
4552 n_outputs = 0;
4553 for (i = 0; i < n_operands; i++)
4554 if (curr_static_id->operand[i].type == OP_OUT)
4555 outputs[n_outputs++] = i;
4556 outputs[n_outputs] = -1;
4557 for (i = 0; i < n_operands; i++)
4559 int regno;
4560 bool optional_p = false;
4561 rtx old, new_reg;
4562 rtx op = *curr_id->operand_loc[i];
4564 if (goal_alt_win[i])
4566 if (goal_alt[i] == NO_REGS
4567 && REG_P (op)
4568 /* When we assign NO_REGS it means that we will not
4569 assign a hard register to the scratch pseudo by
4570 assigment pass and the scratch pseudo will be
4571 spilled. Spilled scratch pseudos are transformed
4572 back to scratches at the LRA end. */
4573 && ira_former_scratch_operand_p (curr_insn, i)
4574 && ira_former_scratch_p (REGNO (op)))
4576 int regno = REGNO (op);
4577 lra_change_class (regno, NO_REGS, " Change to", true);
4578 if (lra_get_regno_hard_regno (regno) >= 0)
4579 /* We don't have to mark all insn affected by the
4580 spilled pseudo as there is only one such insn, the
4581 current one. */
4582 reg_renumber[regno] = -1;
4583 lra_assert (bitmap_single_bit_set_p
4584 (&lra_reg_info[REGNO (op)].insn_bitmap));
4586 /* We can do an optional reload. If the pseudo got a hard
4587 reg, we might improve the code through inheritance. If
4588 it does not get a hard register we coalesce memory/memory
4589 moves later. Ignore move insns to avoid cycling. */
4590 if (! lra_simple_p
4591 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
4592 && goal_alt[i] != NO_REGS && REG_P (op)
4593 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
4594 && regno < new_regno_start
4595 && ! ira_former_scratch_p (regno)
4596 && reg_renumber[regno] < 0
4597 /* Check that the optional reload pseudo will be able to
4598 hold given mode value. */
4599 && ! (prohibited_class_reg_set_mode_p
4600 (goal_alt[i], reg_class_contents[goal_alt[i]],
4601 PSEUDO_REGNO_MODE (regno)))
4602 && (curr_insn_set == NULL_RTX
4603 || !((REG_P (SET_SRC (curr_insn_set))
4604 || MEM_P (SET_SRC (curr_insn_set))
4605 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
4606 && (REG_P (SET_DEST (curr_insn_set))
4607 || MEM_P (SET_DEST (curr_insn_set))
4608 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
4609 optional_p = true;
4610 else if (goal_alt_matched[i][0] != -1
4611 && curr_static_id->operand[i].type == OP_OUT
4612 && (curr_static_id->operand_alternative
4613 [goal_alt_number * n_operands + i].earlyclobber)
4614 && REG_P (op))
4616 for (j = 0; goal_alt_matched[i][j] != -1; j++)
4618 rtx op2 = *curr_id->operand_loc[goal_alt_matched[i][j]];
4620 if (REG_P (op2) && REGNO (op) != REGNO (op2))
4621 break;
4623 if (goal_alt_matched[i][j] != -1)
4625 /* Generate reloads for different output and matched
4626 input registers. This is the easiest way to avoid
4627 creation of non-existing register conflicts in
4628 lra-lives.cc. */
4629 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i],
4630 &goal_alt_exclude_start_hard_regs[i], &before,
4631 &after, true);
4633 continue;
4635 else
4637 enum reg_class rclass, common_class;
4639 if (REG_P (op) && goal_alt[i] != NO_REGS
4640 && (regno = REGNO (op)) >= new_regno_start
4641 && (rclass = get_reg_class (regno)) == ALL_REGS
4642 && ((common_class = ira_reg_class_subset[rclass][goal_alt[i]])
4643 != NO_REGS)
4644 && common_class != ALL_REGS
4645 && enough_allocatable_hard_regs_p (common_class,
4646 GET_MODE (op)))
4647 /* Refine reload pseudo class from chosen alternative
4648 constraint. */
4649 lra_change_class (regno, common_class, " Change to", true);
4650 continue;
4654 /* Operands that match previous ones have already been handled. */
4655 if (goal_alt_matches[i] >= 0)
4656 continue;
4658 /* We should not have an operand with a non-offsettable address
4659 appearing where an offsettable address will do. It also may
4660 be a case when the address should be special in other words
4661 not a general one (e.g. it needs no index reg). */
4662 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
4664 enum reg_class rclass;
4665 rtx *loc = &XEXP (op, 0);
4666 enum rtx_code code = GET_CODE (*loc);
4668 push_to_sequence (before);
4669 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
4670 MEM, SCRATCH, curr_insn);
4671 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
4672 new_reg = emit_inc (rclass, *loc, *loc,
4673 /* This value does not matter for MODIFY. */
4674 GET_MODE_SIZE (GET_MODE (op)));
4675 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass,
4676 NULL, false,
4677 "offsetable address", &new_reg))
4679 rtx addr = *loc;
4680 enum rtx_code code = GET_CODE (addr);
4681 bool align_p = false;
4683 if (code == AND && CONST_INT_P (XEXP (addr, 1)))
4685 /* (and ... (const_int -X)) is used to align to X bytes. */
4686 align_p = true;
4687 addr = XEXP (*loc, 0);
4689 else
4690 addr = canonicalize_reload_addr (addr);
4692 lra_emit_move (new_reg, addr);
4693 if (align_p)
4694 emit_move_insn (new_reg, gen_rtx_AND (GET_MODE (new_reg), new_reg, XEXP (*loc, 1)));
4696 before = get_insns ();
4697 end_sequence ();
4698 *loc = new_reg;
4699 lra_update_dup (curr_id, i);
4701 else if (goal_alt_matched[i][0] == -1)
4703 machine_mode mode;
4704 rtx reg, *loc;
4705 int hard_regno;
4706 enum op_type type = curr_static_id->operand[i].type;
4708 loc = curr_id->operand_loc[i];
4709 mode = curr_operand_mode[i];
4710 if (GET_CODE (*loc) == SUBREG)
4712 reg = SUBREG_REG (*loc);
4713 poly_int64 byte = SUBREG_BYTE (*loc);
4714 if (REG_P (reg)
4715 /* Strict_low_part requires reloading the register and not
4716 just the subreg. Likewise for a strict subreg no wider
4717 than a word for WORD_REGISTER_OPERATIONS targets. */
4718 && (curr_static_id->operand[i].strict_low
4719 || (!paradoxical_subreg_p (mode, GET_MODE (reg))
4720 && (hard_regno
4721 = get_try_hard_regno (REGNO (reg))) >= 0
4722 && (simplify_subreg_regno
4723 (hard_regno,
4724 GET_MODE (reg), byte, mode) < 0)
4725 && (goal_alt[i] == NO_REGS
4726 || (simplify_subreg_regno
4727 (ira_class_hard_regs[goal_alt[i]][0],
4728 GET_MODE (reg), byte, mode) >= 0)))
4729 || (partial_subreg_p (mode, GET_MODE (reg))
4730 && known_le (GET_MODE_SIZE (GET_MODE (reg)),
4731 UNITS_PER_WORD)
4732 && WORD_REGISTER_OPERATIONS))
4733 /* Avoid the situation when there are no available hard regs
4734 for the pseudo mode but there are ones for the subreg
4735 mode: */
4736 && !(goal_alt[i] != NO_REGS
4737 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
4738 && (prohibited_class_reg_set_mode_p
4739 (goal_alt[i], reg_class_contents[goal_alt[i]],
4740 GET_MODE (reg)))
4741 && !(prohibited_class_reg_set_mode_p
4742 (goal_alt[i], reg_class_contents[goal_alt[i]],
4743 mode))))
4745 /* An OP_INOUT is required when reloading a subreg of a
4746 mode wider than a word to ensure that data beyond the
4747 word being reloaded is preserved. Also automatically
4748 ensure that strict_low_part reloads are made into
4749 OP_INOUT which should already be true from the backend
4750 constraints. */
4751 if (type == OP_OUT
4752 && (curr_static_id->operand[i].strict_low
4753 || read_modify_subreg_p (*loc)))
4754 type = OP_INOUT;
4755 loc = &SUBREG_REG (*loc);
4756 mode = GET_MODE (*loc);
4759 old = *loc;
4760 if (get_reload_reg (type, mode, old, goal_alt[i],
4761 &goal_alt_exclude_start_hard_regs[i],
4762 loc != curr_id->operand_loc[i], "", &new_reg)
4763 && type != OP_OUT)
4765 push_to_sequence (before);
4766 lra_emit_move (new_reg, old);
4767 before = get_insns ();
4768 end_sequence ();
4770 *loc = new_reg;
4771 if (type != OP_IN
4772 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
4774 start_sequence ();
4775 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
4776 emit_insn (after);
4777 after = get_insns ();
4778 end_sequence ();
4779 *loc = new_reg;
4781 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
4782 if (goal_alt_dont_inherit_ops[j] == i)
4784 lra_set_regno_unique_value (REGNO (new_reg));
4785 break;
4787 lra_update_dup (curr_id, i);
4789 else if (curr_static_id->operand[i].type == OP_IN
4790 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4791 == OP_OUT
4792 || (curr_static_id->operand[goal_alt_matched[i][0]].type
4793 == OP_INOUT
4794 && (operands_match_p
4795 (*curr_id->operand_loc[i],
4796 *curr_id->operand_loc[goal_alt_matched[i][0]],
4797 -1)))))
4799 /* generate reloads for input and matched outputs. */
4800 match_inputs[0] = i;
4801 match_inputs[1] = -1;
4802 match_reload (goal_alt_matched[i][0], match_inputs, outputs,
4803 goal_alt[i], &goal_alt_exclude_start_hard_regs[i],
4804 &before, &after,
4805 curr_static_id->operand_alternative
4806 [goal_alt_number * n_operands + goal_alt_matched[i][0]]
4807 .earlyclobber);
4809 else if ((curr_static_id->operand[i].type == OP_OUT
4810 || (curr_static_id->operand[i].type == OP_INOUT
4811 && (operands_match_p
4812 (*curr_id->operand_loc[i],
4813 *curr_id->operand_loc[goal_alt_matched[i][0]],
4814 -1))))
4815 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4816 == OP_IN))
4817 /* Generate reloads for output and matched inputs. */
4818 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i],
4819 &goal_alt_exclude_start_hard_regs[i], &before, &after,
4820 curr_static_id->operand_alternative
4821 [goal_alt_number * n_operands + i].earlyclobber);
4822 else if (curr_static_id->operand[i].type == OP_IN
4823 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4824 == OP_IN))
4826 /* Generate reloads for matched inputs. */
4827 match_inputs[0] = i;
4828 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
4829 match_inputs[j + 1] = k;
4830 match_inputs[j + 1] = -1;
4831 match_reload (-1, match_inputs, outputs, goal_alt[i],
4832 &goal_alt_exclude_start_hard_regs[i],
4833 &before, &after, false);
4835 else
4836 /* We must generate code in any case when function
4837 process_alt_operands decides that it is possible. */
4838 gcc_unreachable ();
4840 if (optional_p)
4842 rtx reg = op;
4844 lra_assert (REG_P (reg));
4845 regno = REGNO (reg);
4846 op = *curr_id->operand_loc[i]; /* Substitution. */
4847 if (GET_CODE (op) == SUBREG)
4848 op = SUBREG_REG (op);
4849 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
4850 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
4851 lra_reg_info[REGNO (op)].restore_rtx = reg;
4852 if (lra_dump_file != NULL)
4853 fprintf (lra_dump_file,
4854 " Making reload reg %d for reg %d optional\n",
4855 REGNO (op), regno);
4858 if (before != NULL_RTX || after != NULL_RTX
4859 || max_regno_before != max_reg_num ())
4860 change_p = true;
4861 if (change_p)
4863 lra_update_operator_dups (curr_id);
4864 /* Something changes -- process the insn. */
4865 lra_update_insn_regno_info (curr_insn);
4866 if (asm_noperands (PATTERN (curr_insn)) >= 0
4867 && ++curr_id->asm_reloads_num >= FIRST_PSEUDO_REGISTER)
4868 /* Most probably there are no enough registers to satisfy asm insn: */
4869 lra_asm_insn_error (curr_insn);
4871 if (goal_alt_out_sp_reload_p)
4873 /* We have an output stack pointer reload -- update sp offset: */
4874 rtx set;
4875 bool done_p = false;
4876 poly_int64 sp_offset = curr_id->sp_offset;
4877 for (rtx_insn *insn = after; insn != NULL_RTX; insn = NEXT_INSN (insn))
4878 if ((set = single_set (insn)) != NULL_RTX
4879 && SET_DEST (set) == stack_pointer_rtx)
4881 lra_assert (!done_p);
4882 done_p = true;
4883 curr_id->sp_offset = 0;
4884 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
4885 id->sp_offset = sp_offset;
4886 if (lra_dump_file != NULL)
4887 fprintf (lra_dump_file,
4888 " Moving sp offset from insn %u to %u\n",
4889 INSN_UID (curr_insn), INSN_UID (insn));
4891 lra_assert (done_p);
4893 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
4894 return change_p;
4897 /* Return true if INSN satisfies all constraints. In other words, no
4898 reload insns are needed. */
4899 bool
4900 lra_constrain_insn (rtx_insn *insn)
4902 int saved_new_regno_start = new_regno_start;
4903 int saved_new_insn_uid_start = new_insn_uid_start;
4904 bool change_p;
4906 curr_insn = insn;
4907 curr_id = lra_get_insn_recog_data (curr_insn);
4908 curr_static_id = curr_id->insn_static_data;
4909 new_insn_uid_start = get_max_uid ();
4910 new_regno_start = max_reg_num ();
4911 change_p = curr_insn_transform (true);
4912 new_regno_start = saved_new_regno_start;
4913 new_insn_uid_start = saved_new_insn_uid_start;
4914 return ! change_p;
4917 /* Return true if X is in LIST. */
4918 static bool
4919 in_list_p (rtx x, rtx list)
4921 for (; list != NULL_RTX; list = XEXP (list, 1))
4922 if (XEXP (list, 0) == x)
4923 return true;
4924 return false;
4927 /* Return true if X contains an allocatable hard register (if
4928 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
4929 static bool
4930 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
4932 int i, j;
4933 const char *fmt;
4934 enum rtx_code code;
4936 code = GET_CODE (x);
4937 if (REG_P (x))
4939 int regno = REGNO (x);
4940 HARD_REG_SET alloc_regs;
4942 if (hard_reg_p)
4944 if (regno >= FIRST_PSEUDO_REGISTER)
4945 regno = lra_get_regno_hard_regno (regno);
4946 if (regno < 0)
4947 return false;
4948 alloc_regs = ~lra_no_alloc_regs;
4949 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
4951 else
4953 if (regno < FIRST_PSEUDO_REGISTER)
4954 return false;
4955 if (! spilled_p)
4956 return true;
4957 return lra_get_regno_hard_regno (regno) < 0;
4960 fmt = GET_RTX_FORMAT (code);
4961 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4963 if (fmt[i] == 'e')
4965 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
4966 return true;
4968 else if (fmt[i] == 'E')
4970 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4971 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4972 return true;
4975 return false;
4978 /* Process all regs in location *LOC and change them on equivalent
4979 substitution. Return true if any change was done. */
4980 static bool
4981 loc_equivalence_change_p (rtx *loc)
4983 rtx subst, reg, x = *loc;
4984 bool result = false;
4985 enum rtx_code code = GET_CODE (x);
4986 const char *fmt;
4987 int i, j;
4989 if (code == SUBREG)
4991 reg = SUBREG_REG (x);
4992 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4993 && GET_MODE (subst) == VOIDmode)
4995 /* We cannot reload debug location. Simplify subreg here
4996 while we know the inner mode. */
4997 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4998 GET_MODE (reg), SUBREG_BYTE (x));
4999 return true;
5002 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
5004 *loc = subst;
5005 return true;
5008 /* Scan all the operand sub-expressions. */
5009 fmt = GET_RTX_FORMAT (code);
5010 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5012 if (fmt[i] == 'e')
5013 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
5014 else if (fmt[i] == 'E')
5015 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5016 result
5017 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
5019 return result;
5022 /* Similar to loc_equivalence_change_p, but for use as
5023 simplify_replace_fn_rtx callback. DATA is insn for which the
5024 elimination is done. If it null we don't do the elimination. */
5025 static rtx
5026 loc_equivalence_callback (rtx loc, const_rtx, void *data)
5028 if (!REG_P (loc))
5029 return NULL_RTX;
5031 rtx subst = (data == NULL
5032 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
5033 if (subst != loc)
5034 return subst;
5036 return NULL_RTX;
5039 /* Maximum number of generated reload insns per an insn. It is for
5040 preventing this pass cycling in a bug case. */
5041 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
5043 /* The current iteration number of this LRA pass. */
5044 int lra_constraint_iter;
5046 /* True if we should during assignment sub-pass check assignment
5047 correctness for all pseudos and spill some of them to correct
5048 conflicts. It can be necessary when we substitute equiv which
5049 needs checking register allocation correctness because the
5050 equivalent value contains allocatable hard registers, or when we
5051 restore multi-register pseudo, or when we change the insn code and
5052 its operand became INOUT operand when it was IN one before. */
5053 bool check_and_force_assignment_correctness_p;
5055 /* Return true if REGNO is referenced in more than one block. */
5056 static bool
5057 multi_block_pseudo_p (int regno)
5059 basic_block bb = NULL;
5060 unsigned int uid;
5061 bitmap_iterator bi;
5063 if (regno < FIRST_PSEUDO_REGISTER)
5064 return false;
5066 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
5067 if (bb == NULL)
5068 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
5069 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
5070 return true;
5071 return false;
5074 /* Return true if LIST contains a deleted insn. */
5075 static bool
5076 contains_deleted_insn_p (rtx_insn_list *list)
5078 for (; list != NULL_RTX; list = list->next ())
5079 if (NOTE_P (list->insn ())
5080 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
5081 return true;
5082 return false;
5085 /* Return true if X contains a pseudo dying in INSN. */
5086 static bool
5087 dead_pseudo_p (rtx x, rtx_insn *insn)
5089 int i, j;
5090 const char *fmt;
5091 enum rtx_code code;
5093 if (REG_P (x))
5094 return (insn != NULL_RTX
5095 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
5096 code = GET_CODE (x);
5097 fmt = GET_RTX_FORMAT (code);
5098 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5100 if (fmt[i] == 'e')
5102 if (dead_pseudo_p (XEXP (x, i), insn))
5103 return true;
5105 else if (fmt[i] == 'E')
5107 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5108 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
5109 return true;
5112 return false;
5115 /* Return true if INSN contains a dying pseudo in INSN right hand
5116 side. */
5117 static bool
5118 insn_rhs_dead_pseudo_p (rtx_insn *insn)
5120 rtx set = single_set (insn);
5122 gcc_assert (set != NULL);
5123 return dead_pseudo_p (SET_SRC (set), insn);
5126 /* Return true if any init insn of REGNO contains a dying pseudo in
5127 insn right hand side. */
5128 static bool
5129 init_insn_rhs_dead_pseudo_p (int regno)
5131 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
5133 if (insns == NULL)
5134 return false;
5135 for (; insns != NULL_RTX; insns = insns->next ())
5136 if (insn_rhs_dead_pseudo_p (insns->insn ()))
5137 return true;
5138 return false;
5141 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
5142 reverse only if we have one init insn with given REGNO as a
5143 source. */
5144 static bool
5145 reverse_equiv_p (int regno)
5147 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
5148 rtx set;
5150 if (insns == NULL)
5151 return false;
5152 if (! INSN_P (insns->insn ())
5153 || insns->next () != NULL)
5154 return false;
5155 if ((set = single_set (insns->insn ())) == NULL_RTX)
5156 return false;
5157 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
5160 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
5161 call this function only for non-reverse equivalence. */
5162 static bool
5163 contains_reloaded_insn_p (int regno)
5165 rtx set;
5166 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
5168 for (; list != NULL; list = list->next ())
5169 if ((set = single_set (list->insn ())) == NULL_RTX
5170 || ! REG_P (SET_DEST (set))
5171 || (int) REGNO (SET_DEST (set)) != regno)
5172 return true;
5173 return false;
5176 /* Try combine secondary memory reload insn FROM for insn TO into TO insn.
5177 FROM should be a load insn (usually a secondary memory reload insn). Return
5178 TRUE in case of success. */
5179 static bool
5180 combine_reload_insn (rtx_insn *from, rtx_insn *to)
5182 bool ok_p;
5183 rtx_insn *saved_insn;
5184 rtx set, from_reg, to_reg, op;
5185 enum reg_class to_class, from_class;
5186 int n, nop;
5187 signed char changed_nops[MAX_RECOG_OPERANDS + 1];
5189 /* Check conditions for second memory reload and original insn: */
5190 if ((targetm.secondary_memory_needed
5191 == hook_bool_mode_reg_class_t_reg_class_t_false)
5192 || NEXT_INSN (from) != to
5193 || !NONDEBUG_INSN_P (to)
5194 || CALL_P (to))
5195 return false;
5197 lra_insn_recog_data_t id = lra_get_insn_recog_data (to);
5198 struct lra_static_insn_data *static_id = id->insn_static_data;
5200 if (id->used_insn_alternative == LRA_UNKNOWN_ALT
5201 || (set = single_set (from)) == NULL_RTX)
5202 return false;
5203 from_reg = SET_DEST (set);
5204 to_reg = SET_SRC (set);
5205 /* Ignore optional reloads: */
5206 if (! REG_P (from_reg) || ! REG_P (to_reg)
5207 || bitmap_bit_p (&lra_optional_reload_pseudos, REGNO (from_reg)))
5208 return false;
5209 to_class = lra_get_allocno_class (REGNO (to_reg));
5210 from_class = lra_get_allocno_class (REGNO (from_reg));
5211 /* Check that reload insn is a load: */
5212 if (to_class != NO_REGS || from_class == NO_REGS)
5213 return false;
5214 for (n = nop = 0; nop < static_id->n_operands; nop++)
5216 if (static_id->operand[nop].type != OP_IN)
5217 continue;
5218 op = *id->operand_loc[nop];
5219 if (!REG_P (op) || REGNO (op) != REGNO (from_reg))
5220 continue;
5221 *id->operand_loc[nop] = to_reg;
5222 changed_nops[n++] = nop;
5224 changed_nops[n] = -1;
5225 lra_update_dups (id, changed_nops);
5226 lra_update_insn_regno_info (to);
5227 ok_p = recog_memoized (to) >= 0;
5228 if (ok_p)
5230 /* Check that combined insn does not need any reloads: */
5231 saved_insn = curr_insn;
5232 curr_insn = to;
5233 curr_id = lra_get_insn_recog_data (curr_insn);
5234 curr_static_id = curr_id->insn_static_data;
5235 for (bool swapped_p = false;;)
5237 ok_p = !curr_insn_transform (true);
5238 if (ok_p || curr_static_id->commutative < 0)
5239 break;
5240 swap_operands (curr_static_id->commutative);
5241 if (lra_dump_file != NULL)
5243 fprintf (lra_dump_file,
5244 " Swapping %scombined insn operands:\n",
5245 swapped_p ? "back " : "");
5246 dump_insn_slim (lra_dump_file, to);
5248 if (swapped_p)
5249 break;
5250 swapped_p = true;
5252 curr_insn = saved_insn;
5253 curr_id = lra_get_insn_recog_data (curr_insn);
5254 curr_static_id = curr_id->insn_static_data;
5256 if (ok_p)
5258 id->used_insn_alternative = -1;
5259 lra_push_insn_and_update_insn_regno_info (to);
5260 if (lra_dump_file != NULL)
5262 fprintf (lra_dump_file, " Use combined insn:\n");
5263 dump_insn_slim (lra_dump_file, to);
5265 return true;
5267 if (lra_dump_file != NULL)
5269 fprintf (lra_dump_file, " Failed combined insn:\n");
5270 dump_insn_slim (lra_dump_file, to);
5272 for (int i = 0; i < n; i++)
5274 nop = changed_nops[i];
5275 *id->operand_loc[nop] = from_reg;
5277 lra_update_dups (id, changed_nops);
5278 lra_update_insn_regno_info (to);
5279 if (lra_dump_file != NULL)
5281 fprintf (lra_dump_file, " Restoring insn after failed combining:\n");
5282 dump_insn_slim (lra_dump_file, to);
5284 return false;
5287 /* Entry function of LRA constraint pass. Return true if the
5288 constraint pass did change the code. */
5289 bool
5290 lra_constraints (bool first_p)
5292 bool changed_p;
5293 int i, hard_regno, new_insns_num;
5294 unsigned int min_len, new_min_len, uid;
5295 rtx set, x, reg, dest_reg;
5296 rtx_insn *original_insn;
5297 basic_block last_bb;
5298 bitmap_iterator bi;
5300 lra_constraint_iter++;
5301 if (lra_dump_file != NULL)
5302 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
5303 lra_constraint_iter);
5304 changed_p = false;
5305 if (pic_offset_table_rtx
5306 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
5307 check_and_force_assignment_correctness_p = true;
5308 else if (first_p)
5309 /* On the first iteration we should check IRA assignment
5310 correctness. In rare cases, the assignments can be wrong as
5311 early clobbers operands are ignored in IRA or usages of
5312 paradoxical sub-registers are not taken into account by
5313 IRA. */
5314 check_and_force_assignment_correctness_p = true;
5315 new_insn_uid_start = get_max_uid ();
5316 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
5317 /* Mark used hard regs for target stack size calulations. */
5318 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5319 if (lra_reg_info[i].nrefs != 0
5320 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
5322 int j, nregs;
5324 nregs = hard_regno_nregs (hard_regno, lra_reg_info[i].biggest_mode);
5325 for (j = 0; j < nregs; j++)
5326 df_set_regs_ever_live (hard_regno + j, true);
5328 /* Do elimination before the equivalence processing as we can spill
5329 some pseudos during elimination. */
5330 lra_eliminate (false, first_p);
5331 auto_bitmap equiv_insn_bitmap (&reg_obstack);
5332 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5333 if (lra_reg_info[i].nrefs != 0)
5335 ira_reg_equiv[i].profitable_p = true;
5336 reg = regno_reg_rtx[i];
5337 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
5339 bool pseudo_p = contains_reg_p (x, false, false);
5341 /* After RTL transformation, we cannot guarantee that
5342 pseudo in the substitution was not reloaded which might
5343 make equivalence invalid. For example, in reverse
5344 equiv of p0
5346 p0 <- ...
5348 equiv_mem <- p0
5350 the memory address register was reloaded before the 2nd
5351 insn. */
5352 if ((! first_p && pseudo_p)
5353 /* We don't use DF for compilation speed sake. So it
5354 is problematic to update live info when we use an
5355 equivalence containing pseudos in more than one
5356 BB. */
5357 || (pseudo_p && multi_block_pseudo_p (i))
5358 /* If an init insn was deleted for some reason, cancel
5359 the equiv. We could update the equiv insns after
5360 transformations including an equiv insn deletion
5361 but it is not worthy as such cases are extremely
5362 rare. */
5363 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
5364 /* If it is not a reverse equivalence, we check that a
5365 pseudo in rhs of the init insn is not dying in the
5366 insn. Otherwise, the live info at the beginning of
5367 the corresponding BB might be wrong after we
5368 removed the insn. When the equiv can be a
5369 constant, the right hand side of the init insn can
5370 be a pseudo. */
5371 || (! reverse_equiv_p (i)
5372 && (init_insn_rhs_dead_pseudo_p (i)
5373 /* If we reloaded the pseudo in an equivalence
5374 init insn, we cannot remove the equiv init
5375 insns and the init insns might write into
5376 const memory in this case. */
5377 || contains_reloaded_insn_p (i)))
5378 /* Prevent access beyond equivalent memory for
5379 paradoxical subregs. */
5380 || (MEM_P (x)
5381 && maybe_gt (GET_MODE_SIZE (lra_reg_info[i].biggest_mode),
5382 GET_MODE_SIZE (GET_MODE (x))))
5383 || (pic_offset_table_rtx
5384 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
5385 && (targetm.preferred_reload_class
5386 (x, lra_get_allocno_class (i)) == NO_REGS))
5387 || contains_symbol_ref_p (x))))
5388 ira_reg_equiv[i].defined_p
5389 = ira_reg_equiv[i].caller_save_p = false;
5390 if (contains_reg_p (x, false, true))
5391 ira_reg_equiv[i].profitable_p = false;
5392 if (get_equiv (reg) != reg)
5393 bitmap_ior_into (equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
5396 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5397 update_equiv (i);
5398 /* We should add all insns containing pseudos which should be
5399 substituted by their equivalences. */
5400 EXECUTE_IF_SET_IN_BITMAP (equiv_insn_bitmap, 0, uid, bi)
5401 lra_push_insn_by_uid (uid);
5402 min_len = lra_insn_stack_length ();
5403 new_insns_num = 0;
5404 last_bb = NULL;
5405 changed_p = false;
5406 original_insn = NULL;
5407 while ((new_min_len = lra_insn_stack_length ()) != 0)
5409 curr_insn = lra_pop_insn ();
5410 --new_min_len;
5411 curr_bb = BLOCK_FOR_INSN (curr_insn);
5412 if (curr_bb != last_bb)
5414 last_bb = curr_bb;
5415 bb_reload_num = lra_curr_reload_num;
5417 if (min_len > new_min_len)
5419 min_len = new_min_len;
5420 new_insns_num = 0;
5421 original_insn = curr_insn;
5423 else if (combine_reload_insn (curr_insn, original_insn))
5425 continue;
5427 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
5428 internal_error
5429 ("maximum number of generated reload insns per insn achieved (%d)",
5430 MAX_RELOAD_INSNS_NUMBER);
5431 new_insns_num++;
5432 if (DEBUG_INSN_P (curr_insn))
5434 /* We need to check equivalence in debug insn and change
5435 pseudo to the equivalent value if necessary. */
5436 curr_id = lra_get_insn_recog_data (curr_insn);
5437 if (bitmap_bit_p (equiv_insn_bitmap, INSN_UID (curr_insn)))
5439 rtx old = *curr_id->operand_loc[0];
5440 *curr_id->operand_loc[0]
5441 = simplify_replace_fn_rtx (old, NULL_RTX,
5442 loc_equivalence_callback, curr_insn);
5443 if (old != *curr_id->operand_loc[0])
5445 /* If we substitute pseudo by shared equivalence, we can fail
5446 to update LRA reg info and this can result in many
5447 unexpected consequences. So keep rtl unshared: */
5448 *curr_id->operand_loc[0]
5449 = copy_rtx (*curr_id->operand_loc[0]);
5450 lra_update_insn_regno_info (curr_insn);
5451 changed_p = true;
5455 else if (INSN_P (curr_insn))
5457 if ((set = single_set (curr_insn)) != NULL_RTX)
5459 dest_reg = SET_DEST (set);
5460 /* The equivalence pseudo could be set up as SUBREG in a
5461 case when it is a call restore insn in a mode
5462 different from the pseudo mode. */
5463 if (GET_CODE (dest_reg) == SUBREG)
5464 dest_reg = SUBREG_REG (dest_reg);
5465 if ((REG_P (dest_reg)
5466 && (x = get_equiv (dest_reg)) != dest_reg
5467 /* Remove insns which set up a pseudo whose value
5468 cannot be changed. Such insns might be not in
5469 init_insns because we don't update equiv data
5470 during insn transformations.
5472 As an example, let suppose that a pseudo got
5473 hard register and on the 1st pass was not
5474 changed to equivalent constant. We generate an
5475 additional insn setting up the pseudo because of
5476 secondary memory movement. Then the pseudo is
5477 spilled and we use the equiv constant. In this
5478 case we should remove the additional insn and
5479 this insn is not init_insns list. */
5480 && (! MEM_P (x) || MEM_READONLY_P (x)
5481 /* Check that this is actually an insn setting
5482 up the equivalence. */
5483 || in_list_p (curr_insn,
5484 ira_reg_equiv
5485 [REGNO (dest_reg)].init_insns)))
5486 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
5487 && in_list_p (curr_insn,
5488 ira_reg_equiv
5489 [REGNO (SET_SRC (set))].init_insns)))
5491 /* This is equiv init insn of pseudo which did not get a
5492 hard register -- remove the insn. */
5493 if (lra_dump_file != NULL)
5495 fprintf (lra_dump_file,
5496 " Removing equiv init insn %i (freq=%d)\n",
5497 INSN_UID (curr_insn),
5498 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
5499 dump_insn_slim (lra_dump_file, curr_insn);
5501 if (contains_reg_p (x, true, false))
5502 check_and_force_assignment_correctness_p = true;
5503 lra_set_insn_deleted (curr_insn);
5504 continue;
5507 curr_id = lra_get_insn_recog_data (curr_insn);
5508 curr_static_id = curr_id->insn_static_data;
5509 init_curr_insn_input_reloads ();
5510 init_curr_operand_mode ();
5511 if (curr_insn_transform (false))
5512 changed_p = true;
5513 /* Check non-transformed insns too for equiv change as USE
5514 or CLOBBER don't need reloads but can contain pseudos
5515 being changed on their equivalences. */
5516 else if (bitmap_bit_p (equiv_insn_bitmap, INSN_UID (curr_insn))
5517 && loc_equivalence_change_p (&PATTERN (curr_insn)))
5519 lra_update_insn_regno_info (curr_insn);
5520 changed_p = true;
5525 /* If we used a new hard regno, changed_p should be true because the
5526 hard reg is assigned to a new pseudo. */
5527 if (flag_checking && !changed_p)
5529 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5530 if (lra_reg_info[i].nrefs != 0
5531 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
5533 int j, nregs = hard_regno_nregs (hard_regno,
5534 PSEUDO_REGNO_MODE (i));
5536 for (j = 0; j < nregs; j++)
5537 lra_assert (df_regs_ever_live_p (hard_regno + j));
5540 if (changed_p)
5541 lra_dump_insns_if_possible ("changed func after local");
5542 return changed_p;
5545 static void initiate_invariants (void);
5546 static void finish_invariants (void);
5548 /* Initiate the LRA constraint pass. It is done once per
5549 function. */
5550 void
5551 lra_constraints_init (void)
5553 initiate_invariants ();
5556 /* Finalize the LRA constraint pass. It is done once per
5557 function. */
5558 void
5559 lra_constraints_finish (void)
5561 finish_invariants ();
5566 /* Structure describes invariants for ineheritance. */
5567 struct lra_invariant
5569 /* The order number of the invariant. */
5570 int num;
5571 /* The invariant RTX. */
5572 rtx invariant_rtx;
5573 /* The origin insn of the invariant. */
5574 rtx_insn *insn;
5577 typedef lra_invariant invariant_t;
5578 typedef invariant_t *invariant_ptr_t;
5579 typedef const invariant_t *const_invariant_ptr_t;
5581 /* Pointer to the inheritance invariants. */
5582 static vec<invariant_ptr_t> invariants;
5584 /* Allocation pool for the invariants. */
5585 static object_allocator<lra_invariant> *invariants_pool;
5587 /* Hash table for the invariants. */
5588 static htab_t invariant_table;
5590 /* Hash function for INVARIANT. */
5591 static hashval_t
5592 invariant_hash (const void *invariant)
5594 rtx inv = ((const_invariant_ptr_t) invariant)->invariant_rtx;
5595 return lra_rtx_hash (inv);
5598 /* Equal function for invariants INVARIANT1 and INVARIANT2. */
5599 static int
5600 invariant_eq_p (const void *invariant1, const void *invariant2)
5602 rtx inv1 = ((const_invariant_ptr_t) invariant1)->invariant_rtx;
5603 rtx inv2 = ((const_invariant_ptr_t) invariant2)->invariant_rtx;
5605 return rtx_equal_p (inv1, inv2);
5608 /* Insert INVARIANT_RTX into the table if it is not there yet. Return
5609 invariant which is in the table. */
5610 static invariant_ptr_t
5611 insert_invariant (rtx invariant_rtx)
5613 void **entry_ptr;
5614 invariant_t invariant;
5615 invariant_ptr_t invariant_ptr;
5617 invariant.invariant_rtx = invariant_rtx;
5618 entry_ptr = htab_find_slot (invariant_table, &invariant, INSERT);
5619 if (*entry_ptr == NULL)
5621 invariant_ptr = invariants_pool->allocate ();
5622 invariant_ptr->invariant_rtx = invariant_rtx;
5623 invariant_ptr->insn = NULL;
5624 invariants.safe_push (invariant_ptr);
5625 *entry_ptr = (void *) invariant_ptr;
5627 return (invariant_ptr_t) *entry_ptr;
5630 /* Initiate the invariant table. */
5631 static void
5632 initiate_invariants (void)
5634 invariants.create (100);
5635 invariants_pool
5636 = new object_allocator<lra_invariant> ("Inheritance invariants");
5637 invariant_table = htab_create (100, invariant_hash, invariant_eq_p, NULL);
5640 /* Finish the invariant table. */
5641 static void
5642 finish_invariants (void)
5644 htab_delete (invariant_table);
5645 delete invariants_pool;
5646 invariants.release ();
5649 /* Make the invariant table empty. */
5650 static void
5651 clear_invariants (void)
5653 htab_empty (invariant_table);
5654 invariants_pool->release ();
5655 invariants.truncate (0);
5660 /* This page contains code to do inheritance/split
5661 transformations. */
5663 /* Number of reloads passed so far in current EBB. */
5664 static int reloads_num;
5666 /* Number of calls passed so far in current EBB. */
5667 static int calls_num;
5669 /* Index ID is the CALLS_NUM associated the last call we saw with
5670 ABI identifier ID. */
5671 static int last_call_for_abi[NUM_ABI_IDS];
5673 /* Which registers have been fully or partially clobbered by a call
5674 since they were last used. */
5675 static HARD_REG_SET full_and_partial_call_clobbers;
5677 /* Current reload pseudo check for validity of elements in
5678 USAGE_INSNS. */
5679 static int curr_usage_insns_check;
5681 /* Info about last usage of registers in EBB to do inheritance/split
5682 transformation. Inheritance transformation is done from a spilled
5683 pseudo and split transformations from a hard register or a pseudo
5684 assigned to a hard register. */
5685 struct usage_insns
5687 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
5688 value INSNS is valid. The insns is chain of optional debug insns
5689 and a finishing non-debug insn using the corresponding reg. The
5690 value is also used to mark the registers which are set up in the
5691 current insn. The negated insn uid is used for this. */
5692 int check;
5693 /* Value of global reloads_num at the last insn in INSNS. */
5694 int reloads_num;
5695 /* Value of global reloads_nums at the last insn in INSNS. */
5696 int calls_num;
5697 /* It can be true only for splitting. And it means that the restore
5698 insn should be put after insn given by the following member. */
5699 bool after_p;
5700 /* Next insns in the current EBB which use the original reg and the
5701 original reg value is not changed between the current insn and
5702 the next insns. In order words, e.g. for inheritance, if we need
5703 to use the original reg value again in the next insns we can try
5704 to use the value in a hard register from a reload insn of the
5705 current insn. */
5706 rtx insns;
5709 /* Map: regno -> corresponding pseudo usage insns. */
5710 static struct usage_insns *usage_insns;
5712 static void
5713 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
5715 usage_insns[regno].check = curr_usage_insns_check;
5716 usage_insns[regno].insns = insn;
5717 usage_insns[regno].reloads_num = reloads_num;
5718 usage_insns[regno].calls_num = calls_num;
5719 usage_insns[regno].after_p = after_p;
5720 if (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0)
5721 remove_from_hard_reg_set (&full_and_partial_call_clobbers,
5722 PSEUDO_REGNO_MODE (regno),
5723 reg_renumber[regno]);
5726 /* The function is used to form list REGNO usages which consists of
5727 optional debug insns finished by a non-debug insn using REGNO.
5728 RELOADS_NUM is current number of reload insns processed so far. */
5729 static void
5730 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
5732 rtx next_usage_insns;
5734 if (usage_insns[regno].check == curr_usage_insns_check
5735 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
5736 && DEBUG_INSN_P (insn))
5738 /* Check that we did not add the debug insn yet. */
5739 if (next_usage_insns != insn
5740 && (GET_CODE (next_usage_insns) != INSN_LIST
5741 || XEXP (next_usage_insns, 0) != insn))
5742 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
5743 next_usage_insns);
5745 else if (NONDEBUG_INSN_P (insn))
5746 setup_next_usage_insn (regno, insn, reloads_num, false);
5747 else
5748 usage_insns[regno].check = 0;
5751 /* Return first non-debug insn in list USAGE_INSNS. */
5752 static rtx_insn *
5753 skip_usage_debug_insns (rtx usage_insns)
5755 rtx insn;
5757 /* Skip debug insns. */
5758 for (insn = usage_insns;
5759 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
5760 insn = XEXP (insn, 1))
5762 return safe_as_a <rtx_insn *> (insn);
5765 /* Return true if we need secondary memory moves for insn in
5766 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
5767 into the insn. */
5768 static bool
5769 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
5770 rtx usage_insns ATTRIBUTE_UNUSED)
5772 rtx_insn *insn;
5773 rtx set, dest;
5774 enum reg_class cl;
5776 if (inher_cl == ALL_REGS
5777 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
5778 return false;
5779 lra_assert (INSN_P (insn));
5780 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
5781 return false;
5782 dest = SET_DEST (set);
5783 if (! REG_P (dest))
5784 return false;
5785 lra_assert (inher_cl != NO_REGS);
5786 cl = get_reg_class (REGNO (dest));
5787 return (cl != NO_REGS && cl != ALL_REGS
5788 && targetm.secondary_memory_needed (GET_MODE (dest), inher_cl, cl));
5791 /* Registers involved in inheritance/split in the current EBB
5792 (inheritance/split pseudos and original registers). */
5793 static bitmap_head check_only_regs;
5795 /* Reload pseudos cannot be involded in invariant inheritance in the
5796 current EBB. */
5797 static bitmap_head invalid_invariant_regs;
5799 /* Do inheritance transformations for insn INSN, which defines (if
5800 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
5801 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
5802 form as the "insns" field of usage_insns. Return true if we
5803 succeed in such transformation.
5805 The transformations look like:
5807 p <- ... i <- ...
5808 ... p <- i (new insn)
5809 ... =>
5810 <- ... p ... <- ... i ...
5812 ... i <- p (new insn)
5813 <- ... p ... <- ... i ...
5814 ... =>
5815 <- ... p ... <- ... i ...
5816 where p is a spilled original pseudo and i is a new inheritance pseudo.
5819 The inheritance pseudo has the smallest class of two classes CL and
5820 class of ORIGINAL REGNO. */
5821 static bool
5822 inherit_reload_reg (bool def_p, int original_regno,
5823 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
5825 if (optimize_function_for_size_p (cfun))
5826 return false;
5828 enum reg_class rclass = lra_get_allocno_class (original_regno);
5829 rtx original_reg = regno_reg_rtx[original_regno];
5830 rtx new_reg, usage_insn;
5831 rtx_insn *new_insns;
5833 lra_assert (! usage_insns[original_regno].after_p);
5834 if (lra_dump_file != NULL)
5835 fprintf (lra_dump_file,
5836 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
5837 if (! ira_reg_classes_intersect_p[cl][rclass])
5839 if (lra_dump_file != NULL)
5841 fprintf (lra_dump_file,
5842 " Rejecting inheritance for %d "
5843 "because of disjoint classes %s and %s\n",
5844 original_regno, reg_class_names[cl],
5845 reg_class_names[rclass]);
5846 fprintf (lra_dump_file,
5847 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5849 return false;
5851 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
5852 /* We don't use a subset of two classes because it can be
5853 NO_REGS. This transformation is still profitable in most
5854 cases even if the classes are not intersected as register
5855 move is probably cheaper than a memory load. */
5856 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
5858 if (lra_dump_file != NULL)
5859 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
5860 reg_class_names[cl], reg_class_names[rclass]);
5862 rclass = cl;
5864 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
5866 /* Reject inheritance resulting in secondary memory moves.
5867 Otherwise, there is a danger in LRA cycling. Also such
5868 transformation will be unprofitable. */
5869 if (lra_dump_file != NULL)
5871 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
5872 rtx set = single_set (insn);
5874 lra_assert (set != NULL_RTX);
5876 rtx dest = SET_DEST (set);
5878 lra_assert (REG_P (dest));
5879 fprintf (lra_dump_file,
5880 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
5881 "as secondary mem is needed\n",
5882 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
5883 original_regno, reg_class_names[rclass]);
5884 fprintf (lra_dump_file,
5885 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5887 return false;
5889 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
5890 rclass, NULL, "inheritance");
5891 start_sequence ();
5892 if (def_p)
5893 lra_emit_move (original_reg, new_reg);
5894 else
5895 lra_emit_move (new_reg, original_reg);
5896 new_insns = get_insns ();
5897 end_sequence ();
5898 if (NEXT_INSN (new_insns) != NULL_RTX)
5900 if (lra_dump_file != NULL)
5902 fprintf (lra_dump_file,
5903 " Rejecting inheritance %d->%d "
5904 "as it results in 2 or more insns:\n",
5905 original_regno, REGNO (new_reg));
5906 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
5907 fprintf (lra_dump_file,
5908 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5910 return false;
5912 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
5913 lra_update_insn_regno_info (insn);
5914 if (! def_p)
5915 /* We now have a new usage insn for original regno. */
5916 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
5917 if (lra_dump_file != NULL)
5918 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
5919 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
5920 lra_reg_info[REGNO (new_reg)].restore_rtx = regno_reg_rtx[original_regno];
5921 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5922 bitmap_set_bit (&check_only_regs, original_regno);
5923 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
5924 if (def_p)
5925 lra_process_new_insns (insn, NULL, new_insns,
5926 "Add original<-inheritance");
5927 else
5928 lra_process_new_insns (insn, new_insns, NULL,
5929 "Add inheritance<-original");
5930 while (next_usage_insns != NULL_RTX)
5932 if (GET_CODE (next_usage_insns) != INSN_LIST)
5934 usage_insn = next_usage_insns;
5935 lra_assert (NONDEBUG_INSN_P (usage_insn));
5936 next_usage_insns = NULL;
5938 else
5940 usage_insn = XEXP (next_usage_insns, 0);
5941 lra_assert (DEBUG_INSN_P (usage_insn));
5942 next_usage_insns = XEXP (next_usage_insns, 1);
5944 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false,
5945 DEBUG_INSN_P (usage_insn));
5946 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5947 if (lra_dump_file != NULL)
5949 basic_block bb = BLOCK_FOR_INSN (usage_insn);
5950 fprintf (lra_dump_file,
5951 " Inheritance reuse change %d->%d (bb%d):\n",
5952 original_regno, REGNO (new_reg),
5953 bb ? bb->index : -1);
5954 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5957 if (lra_dump_file != NULL)
5958 fprintf (lra_dump_file,
5959 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5960 return true;
5963 /* Return true if we need a caller save/restore for pseudo REGNO which
5964 was assigned to a hard register. */
5965 static inline bool
5966 need_for_call_save_p (int regno)
5968 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
5969 if (usage_insns[regno].calls_num < calls_num)
5971 unsigned int abis = 0;
5972 for (unsigned int i = 0; i < NUM_ABI_IDS; ++i)
5973 if (last_call_for_abi[i] > usage_insns[regno].calls_num)
5974 abis |= 1 << i;
5975 gcc_assert (abis);
5976 if (call_clobbered_in_region_p (abis, full_and_partial_call_clobbers,
5977 PSEUDO_REGNO_MODE (regno),
5978 reg_renumber[regno]))
5979 return true;
5981 return false;
5984 /* Global registers occurring in the current EBB. */
5985 static bitmap_head ebb_global_regs;
5987 /* Return true if we need a split for hard register REGNO or pseudo
5988 REGNO which was assigned to a hard register.
5989 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
5990 used for reloads since the EBB end. It is an approximation of the
5991 used hard registers in the split range. The exact value would
5992 require expensive calculations. If we were aggressive with
5993 splitting because of the approximation, the split pseudo will save
5994 the same hard register assignment and will be removed in the undo
5995 pass. We still need the approximation because too aggressive
5996 splitting would result in too inaccurate cost calculation in the
5997 assignment pass because of too many generated moves which will be
5998 probably removed in the undo pass. */
5999 static inline bool
6000 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
6002 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
6004 lra_assert (hard_regno >= 0);
6005 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
6006 /* Don't split eliminable hard registers, otherwise we can
6007 split hard registers like hard frame pointer, which
6008 lives on BB start/end according to DF-infrastructure,
6009 when there is a pseudo assigned to the register and
6010 living in the same BB. */
6011 && (regno >= FIRST_PSEUDO_REGISTER
6012 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
6013 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
6014 /* Don't split call clobbered hard regs living through
6015 calls, otherwise we might have a check problem in the
6016 assign sub-pass as in the most cases (exception is a
6017 situation when check_and_force_assignment_correctness_p value is
6018 true) the assign pass assumes that all pseudos living
6019 through calls are assigned to call saved hard regs. */
6020 && (regno >= FIRST_PSEUDO_REGISTER
6021 || !TEST_HARD_REG_BIT (full_and_partial_call_clobbers, regno))
6022 /* We need at least 2 reloads to make pseudo splitting
6023 profitable. We should provide hard regno splitting in
6024 any case to solve 1st insn scheduling problem when
6025 moving hard register definition up might result in
6026 impossibility to find hard register for reload pseudo of
6027 small register class. */
6028 && (usage_insns[regno].reloads_num
6029 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
6030 && (regno < FIRST_PSEUDO_REGISTER
6031 /* For short living pseudos, spilling + inheritance can
6032 be considered a substitution for splitting.
6033 Therefore we do not splitting for local pseudos. It
6034 decreases also aggressiveness of splitting. The
6035 minimal number of references is chosen taking into
6036 account that for 2 references splitting has no sense
6037 as we can just spill the pseudo. */
6038 || (regno >= FIRST_PSEUDO_REGISTER
6039 && lra_reg_info[regno].nrefs > 3
6040 && bitmap_bit_p (&ebb_global_regs, regno))))
6041 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
6044 /* Return class for the split pseudo created from original pseudo with
6045 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
6046 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
6047 results in no secondary memory movements. */
6048 static enum reg_class
6049 choose_split_class (enum reg_class allocno_class,
6050 int hard_regno ATTRIBUTE_UNUSED,
6051 machine_mode mode ATTRIBUTE_UNUSED)
6053 int i;
6054 enum reg_class cl, best_cl = NO_REGS;
6055 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
6056 = REGNO_REG_CLASS (hard_regno);
6058 if (! targetm.secondary_memory_needed (mode, allocno_class, allocno_class)
6059 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
6060 return allocno_class;
6061 for (i = 0;
6062 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
6063 i++)
6064 if (! targetm.secondary_memory_needed (mode, cl, hard_reg_class)
6065 && ! targetm.secondary_memory_needed (mode, hard_reg_class, cl)
6066 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
6067 && (best_cl == NO_REGS
6068 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
6069 best_cl = cl;
6070 return best_cl;
6073 /* Copy any equivalence information from ORIGINAL_REGNO to NEW_REGNO. It only
6074 makes sense to call this function if NEW_REGNO is always equal to
6075 ORIGINAL_REGNO. Set up defined_p flag when caller_save_p flag is set up and
6076 CALL_SAVE_P is true. */
6078 static void
6079 lra_copy_reg_equiv (unsigned int new_regno, unsigned int original_regno,
6080 bool call_save_p)
6082 if (!ira_reg_equiv[original_regno].defined_p
6083 && !(call_save_p && ira_reg_equiv[original_regno].caller_save_p))
6084 return;
6086 ira_expand_reg_equiv ();
6087 ira_reg_equiv[new_regno].defined_p = true;
6088 if (ira_reg_equiv[original_regno].memory)
6089 ira_reg_equiv[new_regno].memory
6090 = copy_rtx (ira_reg_equiv[original_regno].memory);
6091 if (ira_reg_equiv[original_regno].constant)
6092 ira_reg_equiv[new_regno].constant
6093 = copy_rtx (ira_reg_equiv[original_regno].constant);
6094 if (ira_reg_equiv[original_regno].invariant)
6095 ira_reg_equiv[new_regno].invariant
6096 = copy_rtx (ira_reg_equiv[original_regno].invariant);
6099 /* Do split transformations for insn INSN, which defines or uses
6100 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
6101 the EBB next uses ORIGINAL_REGNO; it has the same form as the
6102 "insns" field of usage_insns. If TO is not NULL, we don't use
6103 usage_insns, we put restore insns after TO insn. It is a case when
6104 we call it from lra_split_hard_reg_for, outside the inheritance
6105 pass.
6107 The transformations look like:
6109 p <- ... p <- ...
6110 ... s <- p (new insn -- save)
6111 ... =>
6112 ... p <- s (new insn -- restore)
6113 <- ... p ... <- ... p ...
6115 <- ... p ... <- ... p ...
6116 ... s <- p (new insn -- save)
6117 ... =>
6118 ... p <- s (new insn -- restore)
6119 <- ... p ... <- ... p ...
6121 where p is an original pseudo got a hard register or a hard
6122 register and s is a new split pseudo. The save is put before INSN
6123 if BEFORE_P is true. Return true if we succeed in such
6124 transformation. */
6125 static bool
6126 split_reg (bool before_p, int original_regno, rtx_insn *insn,
6127 rtx next_usage_insns, rtx_insn *to)
6129 enum reg_class rclass;
6130 rtx original_reg;
6131 int hard_regno, nregs;
6132 rtx new_reg, usage_insn;
6133 rtx_insn *restore, *save;
6134 bool after_p;
6135 bool call_save_p;
6136 machine_mode mode;
6138 if (original_regno < FIRST_PSEUDO_REGISTER)
6140 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
6141 hard_regno = original_regno;
6142 call_save_p = false;
6143 nregs = 1;
6144 mode = lra_reg_info[hard_regno].biggest_mode;
6145 machine_mode reg_rtx_mode = GET_MODE (regno_reg_rtx[hard_regno]);
6146 /* A reg can have a biggest_mode of VOIDmode if it was only ever seen as
6147 part of a multi-word register. In that case, just use the reg_rtx
6148 mode. Do the same also if the biggest mode was larger than a register
6149 or we can not compare the modes. Otherwise, limit the size to that of
6150 the biggest access in the function or to the natural mode at least. */
6151 if (mode == VOIDmode
6152 || !ordered_p (GET_MODE_PRECISION (mode),
6153 GET_MODE_PRECISION (reg_rtx_mode))
6154 || paradoxical_subreg_p (mode, reg_rtx_mode)
6155 || maybe_gt (GET_MODE_PRECISION (reg_rtx_mode), GET_MODE_PRECISION (mode)))
6157 original_reg = regno_reg_rtx[hard_regno];
6158 mode = reg_rtx_mode;
6160 else
6161 original_reg = gen_rtx_REG (mode, hard_regno);
6163 else
6165 mode = PSEUDO_REGNO_MODE (original_regno);
6166 hard_regno = reg_renumber[original_regno];
6167 nregs = hard_regno_nregs (hard_regno, mode);
6168 rclass = lra_get_allocno_class (original_regno);
6169 original_reg = regno_reg_rtx[original_regno];
6170 call_save_p = need_for_call_save_p (original_regno);
6172 lra_assert (hard_regno >= 0);
6173 if (lra_dump_file != NULL)
6174 fprintf (lra_dump_file,
6175 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
6177 if (call_save_p)
6179 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
6180 hard_regno_nregs (hard_regno, mode),
6181 mode);
6182 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, NULL, "save");
6184 else
6186 rclass = choose_split_class (rclass, hard_regno, mode);
6187 if (rclass == NO_REGS)
6189 if (lra_dump_file != NULL)
6191 fprintf (lra_dump_file,
6192 " Rejecting split of %d(%s): "
6193 "no good reg class for %d(%s)\n",
6194 original_regno,
6195 reg_class_names[lra_get_allocno_class (original_regno)],
6196 hard_regno,
6197 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
6198 fprintf
6199 (lra_dump_file,
6200 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6202 return false;
6204 /* Split_if_necessary can split hard registers used as part of a
6205 multi-register mode but splits each register individually. The
6206 mode used for each independent register may not be supported
6207 so reject the split. Splitting the wider mode should theoretically
6208 be possible but is not implemented. */
6209 if (!targetm.hard_regno_mode_ok (hard_regno, mode))
6211 if (lra_dump_file != NULL)
6213 fprintf (lra_dump_file,
6214 " Rejecting split of %d(%s): unsuitable mode %s\n",
6215 original_regno,
6216 reg_class_names[lra_get_allocno_class (original_regno)],
6217 GET_MODE_NAME (mode));
6218 fprintf
6219 (lra_dump_file,
6220 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6222 return false;
6224 new_reg = lra_create_new_reg (mode, original_reg, rclass, NULL, "split");
6225 reg_renumber[REGNO (new_reg)] = hard_regno;
6227 int new_regno = REGNO (new_reg);
6228 save = emit_spill_move (true, new_reg, original_reg);
6229 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
6231 if (lra_dump_file != NULL)
6233 fprintf
6234 (lra_dump_file,
6235 " Rejecting split %d->%d resulting in > 2 save insns:\n",
6236 original_regno, new_regno);
6237 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
6238 fprintf (lra_dump_file,
6239 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6241 return false;
6243 restore = emit_spill_move (false, new_reg, original_reg);
6244 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
6246 if (lra_dump_file != NULL)
6248 fprintf (lra_dump_file,
6249 " Rejecting split %d->%d "
6250 "resulting in > 2 restore insns:\n",
6251 original_regno, new_regno);
6252 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
6253 fprintf (lra_dump_file,
6254 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6256 return false;
6258 /* Transfer equivalence information to the spill register, so that
6259 if we fail to allocate the spill register, we have the option of
6260 rematerializing the original value instead of spilling to the stack. */
6261 if (!HARD_REGISTER_NUM_P (original_regno)
6262 && mode == PSEUDO_REGNO_MODE (original_regno))
6263 lra_copy_reg_equiv (new_regno, original_regno, call_save_p);
6264 lra_reg_info[new_regno].restore_rtx = regno_reg_rtx[original_regno];
6265 bitmap_set_bit (&lra_split_regs, new_regno);
6266 if (to != NULL)
6268 lra_assert (next_usage_insns == NULL);
6269 usage_insn = to;
6270 after_p = true;
6272 else
6274 /* We need check_only_regs only inside the inheritance pass. */
6275 bitmap_set_bit (&check_only_regs, new_regno);
6276 bitmap_set_bit (&check_only_regs, original_regno);
6277 after_p = usage_insns[original_regno].after_p;
6278 for (;;)
6280 if (GET_CODE (next_usage_insns) != INSN_LIST)
6282 usage_insn = next_usage_insns;
6283 break;
6285 usage_insn = XEXP (next_usage_insns, 0);
6286 lra_assert (DEBUG_INSN_P (usage_insn));
6287 next_usage_insns = XEXP (next_usage_insns, 1);
6288 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false,
6289 true);
6290 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
6291 if (lra_dump_file != NULL)
6293 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
6294 original_regno, new_regno);
6295 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
6299 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
6300 lra_assert (usage_insn != insn || (after_p && before_p));
6301 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
6302 after_p ? NULL : restore,
6303 after_p ? restore : NULL,
6304 call_save_p
6305 ? "Add reg<-save" : "Add reg<-split");
6306 lra_process_new_insns (insn, before_p ? save : NULL,
6307 before_p ? NULL : save,
6308 call_save_p
6309 ? "Add save<-reg" : "Add split<-reg");
6310 if (nregs > 1 || original_regno < FIRST_PSEUDO_REGISTER)
6311 /* If we are trying to split multi-register. We should check
6312 conflicts on the next assignment sub-pass. IRA can allocate on
6313 sub-register levels, LRA do this on pseudos level right now and
6314 this discrepancy may create allocation conflicts after
6315 splitting.
6317 If we are trying to split hard register we should also check conflicts
6318 as such splitting can create artificial conflict of the hard register
6319 with another pseudo because of simplified conflict calculation in
6320 LRA. */
6321 check_and_force_assignment_correctness_p = true;
6322 if (lra_dump_file != NULL)
6323 fprintf (lra_dump_file,
6324 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6325 return true;
6328 /* Split a hard reg for reload pseudo REGNO having RCLASS and living
6329 in the range [FROM, TO]. Return true if did a split. Otherwise,
6330 return false. */
6331 bool
6332 spill_hard_reg_in_range (int regno, enum reg_class rclass, rtx_insn *from, rtx_insn *to)
6334 int i, hard_regno;
6335 int rclass_size;
6336 rtx_insn *insn;
6337 unsigned int uid;
6338 bitmap_iterator bi;
6339 HARD_REG_SET ignore;
6341 lra_assert (from != NULL && to != NULL);
6342 ignore = lra_no_alloc_regs;
6343 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
6345 lra_insn_recog_data_t id = lra_insn_recog_data[uid];
6346 struct lra_static_insn_data *static_id = id->insn_static_data;
6347 struct lra_insn_reg *reg;
6349 for (reg = id->regs; reg != NULL; reg = reg->next)
6350 if (reg->regno < FIRST_PSEUDO_REGISTER)
6351 SET_HARD_REG_BIT (ignore, reg->regno);
6352 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
6353 SET_HARD_REG_BIT (ignore, reg->regno);
6355 rclass_size = ira_class_hard_regs_num[rclass];
6356 for (i = 0; i < rclass_size; i++)
6358 hard_regno = ira_class_hard_regs[rclass][i];
6359 if (! TEST_HARD_REG_BIT (lra_reg_info[regno].conflict_hard_regs, hard_regno)
6360 || TEST_HARD_REG_BIT (ignore, hard_regno))
6361 continue;
6362 for (insn = from; insn != NEXT_INSN (to); insn = NEXT_INSN (insn))
6364 struct lra_static_insn_data *static_id;
6365 struct lra_insn_reg *reg;
6367 if (!INSN_P (insn))
6368 continue;
6369 if (bitmap_bit_p (&lra_reg_info[hard_regno].insn_bitmap,
6370 INSN_UID (insn)))
6371 break;
6372 static_id = lra_get_insn_recog_data (insn)->insn_static_data;
6373 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
6374 if (reg->regno == hard_regno)
6375 break;
6376 if (reg != NULL)
6377 break;
6379 if (insn != NEXT_INSN (to))
6380 continue;
6381 if (split_reg (true, hard_regno, from, NULL, to))
6382 return true;
6384 return false;
6387 /* Recognize that we need a split transformation for insn INSN, which
6388 defines or uses REGNO in its insn biggest MODE (we use it only if
6389 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
6390 hard registers which might be used for reloads since the EBB end.
6391 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
6392 uid before starting INSN processing. Return true if we succeed in
6393 such transformation. */
6394 static bool
6395 split_if_necessary (int regno, machine_mode mode,
6396 HARD_REG_SET potential_reload_hard_regs,
6397 bool before_p, rtx_insn *insn, int max_uid)
6399 bool res = false;
6400 int i, nregs = 1;
6401 rtx next_usage_insns;
6403 if (regno < FIRST_PSEUDO_REGISTER)
6404 nregs = hard_regno_nregs (regno, mode);
6405 for (i = 0; i < nregs; i++)
6406 if (usage_insns[regno + i].check == curr_usage_insns_check
6407 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
6408 /* To avoid processing the register twice or more. */
6409 && ((GET_CODE (next_usage_insns) != INSN_LIST
6410 && INSN_UID (next_usage_insns) < max_uid)
6411 || (GET_CODE (next_usage_insns) == INSN_LIST
6412 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
6413 && need_for_split_p (potential_reload_hard_regs, regno + i)
6414 && split_reg (before_p, regno + i, insn, next_usage_insns, NULL))
6415 res = true;
6416 return res;
6419 /* Return TRUE if rtx X is considered as an invariant for
6420 inheritance. */
6421 static bool
6422 invariant_p (const_rtx x)
6424 machine_mode mode;
6425 const char *fmt;
6426 enum rtx_code code;
6427 int i, j;
6429 if (side_effects_p (x))
6430 return false;
6432 code = GET_CODE (x);
6433 mode = GET_MODE (x);
6434 if (code == SUBREG)
6436 x = SUBREG_REG (x);
6437 code = GET_CODE (x);
6438 mode = wider_subreg_mode (mode, GET_MODE (x));
6441 if (MEM_P (x))
6442 return false;
6444 if (REG_P (x))
6446 int i, nregs, regno = REGNO (x);
6448 if (regno >= FIRST_PSEUDO_REGISTER || regno == STACK_POINTER_REGNUM
6449 || TEST_HARD_REG_BIT (eliminable_regset, regno)
6450 || GET_MODE_CLASS (GET_MODE (x)) == MODE_CC)
6451 return false;
6452 nregs = hard_regno_nregs (regno, mode);
6453 for (i = 0; i < nregs; i++)
6454 if (! fixed_regs[regno + i]
6455 /* A hard register may be clobbered in the current insn
6456 but we can ignore this case because if the hard
6457 register is used it should be set somewhere after the
6458 clobber. */
6459 || bitmap_bit_p (&invalid_invariant_regs, regno + i))
6460 return false;
6462 fmt = GET_RTX_FORMAT (code);
6463 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6465 if (fmt[i] == 'e')
6467 if (! invariant_p (XEXP (x, i)))
6468 return false;
6470 else if (fmt[i] == 'E')
6472 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6473 if (! invariant_p (XVECEXP (x, i, j)))
6474 return false;
6477 return true;
6480 /* We have 'dest_reg <- invariant'. Let us try to make an invariant
6481 inheritance transformation (using dest_reg instead invariant in a
6482 subsequent insn). */
6483 static bool
6484 process_invariant_for_inheritance (rtx dst_reg, rtx invariant_rtx)
6486 invariant_ptr_t invariant_ptr;
6487 rtx_insn *insn, *new_insns;
6488 rtx insn_set, insn_reg, new_reg;
6489 int insn_regno;
6490 bool succ_p = false;
6491 int dst_regno = REGNO (dst_reg);
6492 machine_mode dst_mode = GET_MODE (dst_reg);
6493 enum reg_class cl = lra_get_allocno_class (dst_regno), insn_reg_cl;
6495 invariant_ptr = insert_invariant (invariant_rtx);
6496 if ((insn = invariant_ptr->insn) != NULL_RTX)
6498 /* We have a subsequent insn using the invariant. */
6499 insn_set = single_set (insn);
6500 lra_assert (insn_set != NULL);
6501 insn_reg = SET_DEST (insn_set);
6502 lra_assert (REG_P (insn_reg));
6503 insn_regno = REGNO (insn_reg);
6504 insn_reg_cl = lra_get_allocno_class (insn_regno);
6506 if (dst_mode == GET_MODE (insn_reg)
6507 /* We should consider only result move reg insns which are
6508 cheap. */
6509 && targetm.register_move_cost (dst_mode, cl, insn_reg_cl) == 2
6510 && targetm.register_move_cost (dst_mode, cl, cl) == 2)
6512 if (lra_dump_file != NULL)
6513 fprintf (lra_dump_file,
6514 " [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[\n");
6515 new_reg = lra_create_new_reg (dst_mode, dst_reg, cl, NULL,
6516 "invariant inheritance");
6517 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
6518 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
6519 lra_reg_info[REGNO (new_reg)].restore_rtx = PATTERN (insn);
6520 start_sequence ();
6521 lra_emit_move (new_reg, dst_reg);
6522 new_insns = get_insns ();
6523 end_sequence ();
6524 lra_process_new_insns (curr_insn, NULL, new_insns,
6525 "Add invariant inheritance<-original");
6526 start_sequence ();
6527 lra_emit_move (SET_DEST (insn_set), new_reg);
6528 new_insns = get_insns ();
6529 end_sequence ();
6530 lra_process_new_insns (insn, NULL, new_insns,
6531 "Changing reload<-inheritance");
6532 lra_set_insn_deleted (insn);
6533 succ_p = true;
6534 if (lra_dump_file != NULL)
6536 fprintf (lra_dump_file,
6537 " Invariant inheritance reuse change %d (bb%d):\n",
6538 REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
6539 dump_insn_slim (lra_dump_file, insn);
6540 fprintf (lra_dump_file,
6541 " ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]\n");
6545 invariant_ptr->insn = curr_insn;
6546 return succ_p;
6549 /* Check only registers living at the current program point in the
6550 current EBB. */
6551 static bitmap_head live_regs;
6553 /* Update live info in EBB given by its HEAD and TAIL insns after
6554 inheritance/split transformation. The function removes dead moves
6555 too. */
6556 static void
6557 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
6559 unsigned int j;
6560 int i, regno;
6561 bool live_p;
6562 rtx_insn *prev_insn;
6563 rtx set;
6564 bool remove_p;
6565 basic_block last_bb, prev_bb, curr_bb;
6566 bitmap_iterator bi;
6567 struct lra_insn_reg *reg;
6568 edge e;
6569 edge_iterator ei;
6571 last_bb = BLOCK_FOR_INSN (tail);
6572 prev_bb = NULL;
6573 for (curr_insn = tail;
6574 curr_insn != PREV_INSN (head);
6575 curr_insn = prev_insn)
6577 prev_insn = PREV_INSN (curr_insn);
6578 /* We need to process empty blocks too. They contain
6579 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
6580 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
6581 continue;
6582 curr_bb = BLOCK_FOR_INSN (curr_insn);
6583 if (curr_bb != prev_bb)
6585 if (prev_bb != NULL)
6587 /* Update df_get_live_in (prev_bb): */
6588 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
6589 if (bitmap_bit_p (&live_regs, j))
6590 bitmap_set_bit (df_get_live_in (prev_bb), j);
6591 else
6592 bitmap_clear_bit (df_get_live_in (prev_bb), j);
6594 if (curr_bb != last_bb)
6596 /* Update df_get_live_out (curr_bb): */
6597 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
6599 live_p = bitmap_bit_p (&live_regs, j);
6600 if (! live_p)
6601 FOR_EACH_EDGE (e, ei, curr_bb->succs)
6602 if (bitmap_bit_p (df_get_live_in (e->dest), j))
6604 live_p = true;
6605 break;
6607 if (live_p)
6608 bitmap_set_bit (df_get_live_out (curr_bb), j);
6609 else
6610 bitmap_clear_bit (df_get_live_out (curr_bb), j);
6613 prev_bb = curr_bb;
6614 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
6616 if (! NONDEBUG_INSN_P (curr_insn))
6617 continue;
6618 curr_id = lra_get_insn_recog_data (curr_insn);
6619 curr_static_id = curr_id->insn_static_data;
6620 remove_p = false;
6621 if ((set = single_set (curr_insn)) != NULL_RTX
6622 && REG_P (SET_DEST (set))
6623 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
6624 && SET_DEST (set) != pic_offset_table_rtx
6625 && bitmap_bit_p (&check_only_regs, regno)
6626 && ! bitmap_bit_p (&live_regs, regno))
6627 remove_p = true;
6628 /* See which defined values die here. */
6629 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6630 if (reg->type == OP_OUT && ! reg->subreg_p)
6631 bitmap_clear_bit (&live_regs, reg->regno);
6632 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6633 if (reg->type == OP_OUT && ! reg->subreg_p)
6634 bitmap_clear_bit (&live_regs, reg->regno);
6635 if (curr_id->arg_hard_regs != NULL)
6636 /* Make clobbered argument hard registers die. */
6637 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6638 if (regno >= FIRST_PSEUDO_REGISTER)
6639 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
6640 /* Mark each used value as live. */
6641 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6642 if (reg->type != OP_OUT
6643 && bitmap_bit_p (&check_only_regs, reg->regno))
6644 bitmap_set_bit (&live_regs, reg->regno);
6645 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6646 if (reg->type != OP_OUT
6647 && bitmap_bit_p (&check_only_regs, reg->regno))
6648 bitmap_set_bit (&live_regs, reg->regno);
6649 if (curr_id->arg_hard_regs != NULL)
6650 /* Make used argument hard registers live. */
6651 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6652 if (regno < FIRST_PSEUDO_REGISTER
6653 && bitmap_bit_p (&check_only_regs, regno))
6654 bitmap_set_bit (&live_regs, regno);
6655 /* It is quite important to remove dead move insns because it
6656 means removing dead store. We don't need to process them for
6657 constraints. */
6658 if (remove_p)
6660 if (lra_dump_file != NULL)
6662 fprintf (lra_dump_file, " Removing dead insn:\n ");
6663 dump_insn_slim (lra_dump_file, curr_insn);
6665 lra_set_insn_deleted (curr_insn);
6670 /* The structure describes info to do an inheritance for the current
6671 insn. We need to collect such info first before doing the
6672 transformations because the transformations change the insn
6673 internal representation. */
6674 struct to_inherit
6676 /* Original regno. */
6677 int regno;
6678 /* Subsequent insns which can inherit original reg value. */
6679 rtx insns;
6682 /* Array containing all info for doing inheritance from the current
6683 insn. */
6684 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
6686 /* Number elements in the previous array. */
6687 static int to_inherit_num;
6689 /* Add inheritance info REGNO and INSNS. Their meaning is described in
6690 structure to_inherit. */
6691 static void
6692 add_to_inherit (int regno, rtx insns)
6694 int i;
6696 for (i = 0; i < to_inherit_num; i++)
6697 if (to_inherit[i].regno == regno)
6698 return;
6699 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
6700 to_inherit[to_inherit_num].regno = regno;
6701 to_inherit[to_inherit_num++].insns = insns;
6704 /* Return the last non-debug insn in basic block BB, or the block begin
6705 note if none. */
6706 static rtx_insn *
6707 get_last_insertion_point (basic_block bb)
6709 rtx_insn *insn;
6711 FOR_BB_INSNS_REVERSE (bb, insn)
6712 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
6713 return insn;
6714 gcc_unreachable ();
6717 /* Set up RES by registers living on edges FROM except the edge (FROM,
6718 TO) or by registers set up in a jump insn in BB FROM. */
6719 static void
6720 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
6722 rtx_insn *last;
6723 struct lra_insn_reg *reg;
6724 edge e;
6725 edge_iterator ei;
6727 lra_assert (to != NULL);
6728 bitmap_clear (res);
6729 FOR_EACH_EDGE (e, ei, from->succs)
6730 if (e->dest != to)
6731 bitmap_ior_into (res, df_get_live_in (e->dest));
6732 last = get_last_insertion_point (from);
6733 if (! JUMP_P (last))
6734 return;
6735 curr_id = lra_get_insn_recog_data (last);
6736 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6737 if (reg->type != OP_IN)
6738 bitmap_set_bit (res, reg->regno);
6741 /* Used as a temporary results of some bitmap calculations. */
6742 static bitmap_head temp_bitmap;
6744 /* We split for reloads of small class of hard regs. The following
6745 defines how many hard regs the class should have to be qualified as
6746 small. The code is mostly oriented to x86/x86-64 architecture
6747 where some insns need to use only specific register or pair of
6748 registers and these register can live in RTL explicitly, e.g. for
6749 parameter passing. */
6750 static const int max_small_class_regs_num = 2;
6752 /* Do inheritance/split transformations in EBB starting with HEAD and
6753 finishing on TAIL. We process EBB insns in the reverse order.
6754 Return true if we did any inheritance/split transformation in the
6755 EBB.
6757 We should avoid excessive splitting which results in worse code
6758 because of inaccurate cost calculations for spilling new split
6759 pseudos in such case. To achieve this we do splitting only if
6760 register pressure is high in given basic block and there are reload
6761 pseudos requiring hard registers. We could do more register
6762 pressure calculations at any given program point to avoid necessary
6763 splitting even more but it is to expensive and the current approach
6764 works well enough. */
6765 static bool
6766 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
6768 int i, src_regno, dst_regno, nregs;
6769 bool change_p, succ_p, update_reloads_num_p;
6770 rtx_insn *prev_insn, *last_insn;
6771 rtx next_usage_insns, curr_set;
6772 enum reg_class cl;
6773 struct lra_insn_reg *reg;
6774 basic_block last_processed_bb, curr_bb = NULL;
6775 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
6776 bitmap to_process;
6777 unsigned int j;
6778 bitmap_iterator bi;
6779 bool head_p, after_p;
6781 change_p = false;
6782 curr_usage_insns_check++;
6783 clear_invariants ();
6784 reloads_num = calls_num = 0;
6785 for (unsigned int i = 0; i < NUM_ABI_IDS; ++i)
6786 last_call_for_abi[i] = 0;
6787 CLEAR_HARD_REG_SET (full_and_partial_call_clobbers);
6788 bitmap_clear (&check_only_regs);
6789 bitmap_clear (&invalid_invariant_regs);
6790 last_processed_bb = NULL;
6791 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
6792 live_hard_regs = eliminable_regset | lra_no_alloc_regs;
6793 /* We don't process new insns generated in the loop. */
6794 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
6796 prev_insn = PREV_INSN (curr_insn);
6797 if (BLOCK_FOR_INSN (curr_insn) != NULL)
6798 curr_bb = BLOCK_FOR_INSN (curr_insn);
6799 if (last_processed_bb != curr_bb)
6801 /* We are at the end of BB. Add qualified living
6802 pseudos for potential splitting. */
6803 to_process = df_get_live_out (curr_bb);
6804 if (last_processed_bb != NULL)
6806 /* We are somewhere in the middle of EBB. */
6807 get_live_on_other_edges (curr_bb, last_processed_bb,
6808 &temp_bitmap);
6809 to_process = &temp_bitmap;
6811 last_processed_bb = curr_bb;
6812 last_insn = get_last_insertion_point (curr_bb);
6813 after_p = (! JUMP_P (last_insn)
6814 && (! CALL_P (last_insn)
6815 || (find_reg_note (last_insn,
6816 REG_NORETURN, NULL_RTX) == NULL_RTX
6817 && ! SIBLING_CALL_P (last_insn))));
6818 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
6819 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
6821 if ((int) j >= lra_constraint_new_regno_start)
6822 break;
6823 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
6825 if (j < FIRST_PSEUDO_REGISTER)
6826 SET_HARD_REG_BIT (live_hard_regs, j);
6827 else
6828 add_to_hard_reg_set (&live_hard_regs,
6829 PSEUDO_REGNO_MODE (j),
6830 reg_renumber[j]);
6831 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
6835 src_regno = dst_regno = -1;
6836 curr_set = single_set (curr_insn);
6837 if (curr_set != NULL_RTX && REG_P (SET_DEST (curr_set)))
6838 dst_regno = REGNO (SET_DEST (curr_set));
6839 if (curr_set != NULL_RTX && REG_P (SET_SRC (curr_set)))
6840 src_regno = REGNO (SET_SRC (curr_set));
6841 update_reloads_num_p = true;
6842 if (src_regno < lra_constraint_new_regno_start
6843 && src_regno >= FIRST_PSEUDO_REGISTER
6844 && reg_renumber[src_regno] < 0
6845 && dst_regno >= lra_constraint_new_regno_start
6846 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
6848 /* 'reload_pseudo <- original_pseudo'. */
6849 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6850 reloads_num++;
6851 update_reloads_num_p = false;
6852 succ_p = false;
6853 if (usage_insns[src_regno].check == curr_usage_insns_check
6854 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
6855 succ_p = inherit_reload_reg (false, src_regno, cl,
6856 curr_insn, next_usage_insns);
6857 if (succ_p)
6858 change_p = true;
6859 else
6860 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
6861 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6862 potential_reload_hard_regs |= reg_class_contents[cl];
6864 else if (src_regno < 0
6865 && dst_regno >= lra_constraint_new_regno_start
6866 && invariant_p (SET_SRC (curr_set))
6867 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS
6868 && ! bitmap_bit_p (&invalid_invariant_regs, dst_regno)
6869 && ! bitmap_bit_p (&invalid_invariant_regs,
6870 ORIGINAL_REGNO(regno_reg_rtx[dst_regno])))
6872 /* 'reload_pseudo <- invariant'. */
6873 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6874 reloads_num++;
6875 update_reloads_num_p = false;
6876 if (process_invariant_for_inheritance (SET_DEST (curr_set), SET_SRC (curr_set)))
6877 change_p = true;
6878 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6879 potential_reload_hard_regs |= reg_class_contents[cl];
6881 else if (src_regno >= lra_constraint_new_regno_start
6882 && dst_regno < lra_constraint_new_regno_start
6883 && dst_regno >= FIRST_PSEUDO_REGISTER
6884 && reg_renumber[dst_regno] < 0
6885 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
6886 && usage_insns[dst_regno].check == curr_usage_insns_check
6887 && (next_usage_insns
6888 = usage_insns[dst_regno].insns) != NULL_RTX)
6890 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6891 reloads_num++;
6892 update_reloads_num_p = false;
6893 /* 'original_pseudo <- reload_pseudo'. */
6894 if (! JUMP_P (curr_insn)
6895 && inherit_reload_reg (true, dst_regno, cl,
6896 curr_insn, next_usage_insns))
6897 change_p = true;
6898 /* Invalidate. */
6899 usage_insns[dst_regno].check = 0;
6900 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6901 potential_reload_hard_regs |= reg_class_contents[cl];
6903 else if (INSN_P (curr_insn))
6905 int iter;
6906 int max_uid = get_max_uid ();
6908 curr_id = lra_get_insn_recog_data (curr_insn);
6909 curr_static_id = curr_id->insn_static_data;
6910 to_inherit_num = 0;
6911 /* Process insn definitions. */
6912 for (iter = 0; iter < 2; iter++)
6913 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
6914 reg != NULL;
6915 reg = reg->next)
6916 if (reg->type != OP_IN
6917 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
6919 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
6920 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
6921 && usage_insns[dst_regno].check == curr_usage_insns_check
6922 && (next_usage_insns
6923 = usage_insns[dst_regno].insns) != NULL_RTX)
6925 struct lra_insn_reg *r;
6927 for (r = curr_id->regs; r != NULL; r = r->next)
6928 if (r->type != OP_OUT && r->regno == dst_regno)
6929 break;
6930 /* Don't do inheritance if the pseudo is also
6931 used in the insn. */
6932 if (r == NULL)
6933 /* We cannot do inheritance right now
6934 because the current insn reg info (chain
6935 regs) can change after that. */
6936 add_to_inherit (dst_regno, next_usage_insns);
6938 /* We cannot process one reg twice here because of
6939 usage_insns invalidation. */
6940 if ((dst_regno < FIRST_PSEUDO_REGISTER
6941 || reg_renumber[dst_regno] >= 0)
6942 && ! reg->subreg_p && reg->type != OP_IN)
6944 HARD_REG_SET s;
6946 if (split_if_necessary (dst_regno, reg->biggest_mode,
6947 potential_reload_hard_regs,
6948 false, curr_insn, max_uid))
6949 change_p = true;
6950 CLEAR_HARD_REG_SET (s);
6951 if (dst_regno < FIRST_PSEUDO_REGISTER)
6952 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
6953 else
6954 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
6955 reg_renumber[dst_regno]);
6956 live_hard_regs &= ~s;
6957 potential_reload_hard_regs &= ~s;
6959 /* We should invalidate potential inheritance or
6960 splitting for the current insn usages to the next
6961 usage insns (see code below) as the output pseudo
6962 prevents this. */
6963 if ((dst_regno >= FIRST_PSEUDO_REGISTER
6964 && reg_renumber[dst_regno] < 0)
6965 || (reg->type == OP_OUT && ! reg->subreg_p
6966 && (dst_regno < FIRST_PSEUDO_REGISTER
6967 || reg_renumber[dst_regno] >= 0)))
6969 /* Invalidate and mark definitions. */
6970 if (dst_regno >= FIRST_PSEUDO_REGISTER)
6971 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
6972 else
6974 nregs = hard_regno_nregs (dst_regno,
6975 reg->biggest_mode);
6976 for (i = 0; i < nregs; i++)
6977 usage_insns[dst_regno + i].check
6978 = -(int) INSN_UID (curr_insn);
6982 /* Process clobbered call regs. */
6983 if (curr_id->arg_hard_regs != NULL)
6984 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6985 if (dst_regno >= FIRST_PSEUDO_REGISTER)
6986 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
6987 = -(int) INSN_UID (curr_insn);
6988 if (! JUMP_P (curr_insn))
6989 for (i = 0; i < to_inherit_num; i++)
6990 if (inherit_reload_reg (true, to_inherit[i].regno,
6991 ALL_REGS, curr_insn,
6992 to_inherit[i].insns))
6993 change_p = true;
6994 if (CALL_P (curr_insn))
6996 rtx cheap, pat, dest;
6997 rtx_insn *restore;
6998 int regno, hard_regno;
7000 calls_num++;
7001 function_abi callee_abi = insn_callee_abi (curr_insn);
7002 last_call_for_abi[callee_abi.id ()] = calls_num;
7003 full_and_partial_call_clobbers
7004 |= callee_abi.full_and_partial_reg_clobbers ();
7005 if ((cheap = find_reg_note (curr_insn,
7006 REG_RETURNED, NULL_RTX)) != NULL_RTX
7007 && ((cheap = XEXP (cheap, 0)), true)
7008 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
7009 && (hard_regno = reg_renumber[regno]) >= 0
7010 && usage_insns[regno].check == curr_usage_insns_check
7011 /* If there are pending saves/restores, the
7012 optimization is not worth. */
7013 && usage_insns[regno].calls_num == calls_num - 1
7014 && callee_abi.clobbers_reg_p (GET_MODE (cheap), hard_regno))
7016 /* Restore the pseudo from the call result as
7017 REG_RETURNED note says that the pseudo value is
7018 in the call result and the pseudo is an argument
7019 of the call. */
7020 pat = PATTERN (curr_insn);
7021 if (GET_CODE (pat) == PARALLEL)
7022 pat = XVECEXP (pat, 0, 0);
7023 dest = SET_DEST (pat);
7024 /* For multiple return values dest is PARALLEL.
7025 Currently we handle only single return value case. */
7026 if (REG_P (dest))
7028 start_sequence ();
7029 emit_move_insn (cheap, copy_rtx (dest));
7030 restore = get_insns ();
7031 end_sequence ();
7032 lra_process_new_insns (curr_insn, NULL, restore,
7033 "Inserting call parameter restore");
7034 /* We don't need to save/restore of the pseudo from
7035 this call. */
7036 usage_insns[regno].calls_num = calls_num;
7037 remove_from_hard_reg_set
7038 (&full_and_partial_call_clobbers,
7039 GET_MODE (cheap), hard_regno);
7040 bitmap_set_bit (&check_only_regs, regno);
7044 to_inherit_num = 0;
7045 /* Process insn usages. */
7046 for (iter = 0; iter < 2; iter++)
7047 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
7048 reg != NULL;
7049 reg = reg->next)
7050 if ((reg->type != OP_OUT
7051 || (reg->type == OP_OUT && reg->subreg_p))
7052 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
7054 if (src_regno >= FIRST_PSEUDO_REGISTER
7055 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
7057 if (usage_insns[src_regno].check == curr_usage_insns_check
7058 && (next_usage_insns
7059 = usage_insns[src_regno].insns) != NULL_RTX
7060 && NONDEBUG_INSN_P (curr_insn))
7061 add_to_inherit (src_regno, next_usage_insns);
7062 else if (usage_insns[src_regno].check
7063 != -(int) INSN_UID (curr_insn))
7064 /* Add usages but only if the reg is not set up
7065 in the same insn. */
7066 add_next_usage_insn (src_regno, curr_insn, reloads_num);
7068 else if (src_regno < FIRST_PSEUDO_REGISTER
7069 || reg_renumber[src_regno] >= 0)
7071 bool before_p;
7072 rtx_insn *use_insn = curr_insn;
7074 before_p = (JUMP_P (curr_insn)
7075 || (CALL_P (curr_insn) && reg->type == OP_IN));
7076 if (NONDEBUG_INSN_P (curr_insn)
7077 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
7078 && split_if_necessary (src_regno, reg->biggest_mode,
7079 potential_reload_hard_regs,
7080 before_p, curr_insn, max_uid))
7082 if (reg->subreg_p)
7083 check_and_force_assignment_correctness_p = true;
7084 change_p = true;
7085 /* Invalidate. */
7086 usage_insns[src_regno].check = 0;
7087 if (before_p)
7088 use_insn = PREV_INSN (curr_insn);
7090 if (NONDEBUG_INSN_P (curr_insn))
7092 if (src_regno < FIRST_PSEUDO_REGISTER)
7093 add_to_hard_reg_set (&live_hard_regs,
7094 reg->biggest_mode, src_regno);
7095 else
7096 add_to_hard_reg_set (&live_hard_regs,
7097 PSEUDO_REGNO_MODE (src_regno),
7098 reg_renumber[src_regno]);
7100 if (src_regno >= FIRST_PSEUDO_REGISTER)
7101 add_next_usage_insn (src_regno, use_insn, reloads_num);
7102 else
7104 for (i = 0; i < hard_regno_nregs (src_regno, reg->biggest_mode); i++)
7105 add_next_usage_insn (src_regno + i, use_insn, reloads_num);
7109 /* Process used call regs. */
7110 if (curr_id->arg_hard_regs != NULL)
7111 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
7112 if (src_regno < FIRST_PSEUDO_REGISTER)
7114 SET_HARD_REG_BIT (live_hard_regs, src_regno);
7115 add_next_usage_insn (src_regno, curr_insn, reloads_num);
7117 for (i = 0; i < to_inherit_num; i++)
7119 src_regno = to_inherit[i].regno;
7120 if (inherit_reload_reg (false, src_regno, ALL_REGS,
7121 curr_insn, to_inherit[i].insns))
7122 change_p = true;
7123 else
7124 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
7127 if (update_reloads_num_p
7128 && NONDEBUG_INSN_P (curr_insn) && curr_set != NULL_RTX)
7130 int regno = -1;
7131 if ((REG_P (SET_DEST (curr_set))
7132 && (regno = REGNO (SET_DEST (curr_set))) >= lra_constraint_new_regno_start
7133 && reg_renumber[regno] < 0
7134 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
7135 || (REG_P (SET_SRC (curr_set))
7136 && (regno = REGNO (SET_SRC (curr_set))) >= lra_constraint_new_regno_start
7137 && reg_renumber[regno] < 0
7138 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
7140 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
7141 reloads_num++;
7142 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
7143 potential_reload_hard_regs |= reg_class_contents[cl];
7146 if (NONDEBUG_INSN_P (curr_insn))
7148 int regno;
7150 /* Invalidate invariants with changed regs. */
7151 curr_id = lra_get_insn_recog_data (curr_insn);
7152 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
7153 if (reg->type != OP_IN)
7155 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
7156 bitmap_set_bit (&invalid_invariant_regs,
7157 ORIGINAL_REGNO (regno_reg_rtx[reg->regno]));
7159 curr_static_id = curr_id->insn_static_data;
7160 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
7161 if (reg->type != OP_IN)
7162 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
7163 if (curr_id->arg_hard_regs != NULL)
7164 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
7165 if (regno >= FIRST_PSEUDO_REGISTER)
7166 bitmap_set_bit (&invalid_invariant_regs,
7167 regno - FIRST_PSEUDO_REGISTER);
7169 /* We reached the start of the current basic block. */
7170 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
7171 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
7173 /* We reached the beginning of the current block -- do
7174 rest of spliting in the current BB. */
7175 to_process = df_get_live_in (curr_bb);
7176 if (BLOCK_FOR_INSN (head) != curr_bb)
7178 /* We are somewhere in the middle of EBB. */
7179 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
7180 curr_bb, &temp_bitmap);
7181 to_process = &temp_bitmap;
7183 head_p = true;
7184 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
7186 if ((int) j >= lra_constraint_new_regno_start)
7187 break;
7188 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
7189 && usage_insns[j].check == curr_usage_insns_check
7190 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
7192 if (need_for_split_p (potential_reload_hard_regs, j))
7194 if (lra_dump_file != NULL && head_p)
7196 fprintf (lra_dump_file,
7197 " ----------------------------------\n");
7198 head_p = false;
7200 if (split_reg (false, j, bb_note (curr_bb),
7201 next_usage_insns, NULL))
7202 change_p = true;
7204 usage_insns[j].check = 0;
7209 return change_p;
7212 /* This value affects EBB forming. If probability of edge from EBB to
7213 a BB is not greater than the following value, we don't add the BB
7214 to EBB. */
7215 #define EBB_PROBABILITY_CUTOFF \
7216 ((REG_BR_PROB_BASE * param_lra_inheritance_ebb_probability_cutoff) / 100)
7218 /* Current number of inheritance/split iteration. */
7219 int lra_inheritance_iter;
7221 /* Entry function for inheritance/split pass. */
7222 void
7223 lra_inheritance (void)
7225 int i;
7226 basic_block bb, start_bb;
7227 edge e;
7229 lra_inheritance_iter++;
7230 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
7231 return;
7232 timevar_push (TV_LRA_INHERITANCE);
7233 if (lra_dump_file != NULL)
7234 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
7235 lra_inheritance_iter);
7236 curr_usage_insns_check = 0;
7237 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
7238 for (i = 0; i < lra_constraint_new_regno_start; i++)
7239 usage_insns[i].check = 0;
7240 bitmap_initialize (&check_only_regs, &reg_obstack);
7241 bitmap_initialize (&invalid_invariant_regs, &reg_obstack);
7242 bitmap_initialize (&live_regs, &reg_obstack);
7243 bitmap_initialize (&temp_bitmap, &reg_obstack);
7244 bitmap_initialize (&ebb_global_regs, &reg_obstack);
7245 FOR_EACH_BB_FN (bb, cfun)
7247 start_bb = bb;
7248 if (lra_dump_file != NULL)
7249 fprintf (lra_dump_file, "EBB");
7250 /* Form a EBB starting with BB. */
7251 bitmap_clear (&ebb_global_regs);
7252 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
7253 for (;;)
7255 if (lra_dump_file != NULL)
7256 fprintf (lra_dump_file, " %d", bb->index);
7257 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
7258 || LABEL_P (BB_HEAD (bb->next_bb)))
7259 break;
7260 e = find_fallthru_edge (bb->succs);
7261 if (! e)
7262 break;
7263 if (e->probability.initialized_p ()
7264 && e->probability.to_reg_br_prob_base () < EBB_PROBABILITY_CUTOFF)
7265 break;
7266 bb = bb->next_bb;
7268 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
7269 if (lra_dump_file != NULL)
7270 fprintf (lra_dump_file, "\n");
7271 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
7272 /* Remember that the EBB head and tail can change in
7273 inherit_in_ebb. */
7274 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
7276 bitmap_release (&ebb_global_regs);
7277 bitmap_release (&temp_bitmap);
7278 bitmap_release (&live_regs);
7279 bitmap_release (&invalid_invariant_regs);
7280 bitmap_release (&check_only_regs);
7281 free (usage_insns);
7282 lra_dump_insns_if_possible ("func after inheritance");
7283 timevar_pop (TV_LRA_INHERITANCE);
7288 /* This page contains code to undo failed inheritance/split
7289 transformations. */
7291 /* Current number of iteration undoing inheritance/split. */
7292 int lra_undo_inheritance_iter;
7294 /* Fix BB live info LIVE after removing pseudos created on pass doing
7295 inheritance/split which are REMOVED_PSEUDOS. */
7296 static void
7297 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
7299 unsigned int regno;
7300 bitmap_iterator bi;
7302 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
7303 if (bitmap_clear_bit (live, regno)
7304 && REG_P (lra_reg_info[regno].restore_rtx))
7305 bitmap_set_bit (live, REGNO (lra_reg_info[regno].restore_rtx));
7308 /* Return regno of the (subreg of) REG. Otherwise, return a negative
7309 number. */
7310 static int
7311 get_regno (rtx reg)
7313 if (GET_CODE (reg) == SUBREG)
7314 reg = SUBREG_REG (reg);
7315 if (REG_P (reg))
7316 return REGNO (reg);
7317 return -1;
7320 /* Delete a move INSN with destination reg DREGNO and a previous
7321 clobber insn with the same regno. The inheritance/split code can
7322 generate moves with preceding clobber and when we delete such moves
7323 we should delete the clobber insn too to keep the correct life
7324 info. */
7325 static void
7326 delete_move_and_clobber (rtx_insn *insn, int dregno)
7328 rtx_insn *prev_insn = PREV_INSN (insn);
7330 lra_set_insn_deleted (insn);
7331 lra_assert (dregno >= 0);
7332 if (prev_insn != NULL && NONDEBUG_INSN_P (prev_insn)
7333 && GET_CODE (PATTERN (prev_insn)) == CLOBBER
7334 && dregno == get_regno (XEXP (PATTERN (prev_insn), 0)))
7335 lra_set_insn_deleted (prev_insn);
7338 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
7339 return true if we did any change. The undo transformations for
7340 inheritance looks like
7341 i <- i2
7342 p <- i => p <- i2
7343 or removing
7344 p <- i, i <- p, and i <- i3
7345 where p is original pseudo from which inheritance pseudo i was
7346 created, i and i3 are removed inheritance pseudos, i2 is another
7347 not removed inheritance pseudo. All split pseudos or other
7348 occurrences of removed inheritance pseudos are changed on the
7349 corresponding original pseudos.
7351 The function also schedules insns changed and created during
7352 inheritance/split pass for processing by the subsequent constraint
7353 pass. */
7354 static bool
7355 remove_inheritance_pseudos (bitmap remove_pseudos)
7357 basic_block bb;
7358 int regno, sregno, prev_sregno, dregno;
7359 rtx restore_rtx;
7360 rtx set, prev_set;
7361 rtx_insn *prev_insn;
7362 bool change_p, done_p;
7364 change_p = ! bitmap_empty_p (remove_pseudos);
7365 /* We cannot finish the function right away if CHANGE_P is true
7366 because we need to marks insns affected by previous
7367 inheritance/split pass for processing by the subsequent
7368 constraint pass. */
7369 FOR_EACH_BB_FN (bb, cfun)
7371 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
7372 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
7373 FOR_BB_INSNS_REVERSE (bb, curr_insn)
7375 if (! INSN_P (curr_insn))
7376 continue;
7377 done_p = false;
7378 sregno = dregno = -1;
7379 if (change_p && NONDEBUG_INSN_P (curr_insn)
7380 && (set = single_set (curr_insn)) != NULL_RTX)
7382 dregno = get_regno (SET_DEST (set));
7383 sregno = get_regno (SET_SRC (set));
7386 if (sregno >= 0 && dregno >= 0)
7388 if (bitmap_bit_p (remove_pseudos, dregno)
7389 && ! REG_P (lra_reg_info[dregno].restore_rtx))
7391 /* invariant inheritance pseudo <- original pseudo */
7392 if (lra_dump_file != NULL)
7394 fprintf (lra_dump_file, " Removing invariant inheritance:\n");
7395 dump_insn_slim (lra_dump_file, curr_insn);
7396 fprintf (lra_dump_file, "\n");
7398 delete_move_and_clobber (curr_insn, dregno);
7399 done_p = true;
7401 else if (bitmap_bit_p (remove_pseudos, sregno)
7402 && ! REG_P (lra_reg_info[sregno].restore_rtx))
7404 /* reload pseudo <- invariant inheritance pseudo */
7405 start_sequence ();
7406 /* We cannot just change the source. It might be
7407 an insn different from the move. */
7408 emit_insn (lra_reg_info[sregno].restore_rtx);
7409 rtx_insn *new_insns = get_insns ();
7410 end_sequence ();
7411 lra_assert (single_set (new_insns) != NULL
7412 && SET_DEST (set) == SET_DEST (single_set (new_insns)));
7413 lra_process_new_insns (curr_insn, NULL, new_insns,
7414 "Changing reload<-invariant inheritance");
7415 delete_move_and_clobber (curr_insn, dregno);
7416 done_p = true;
7418 else if ((bitmap_bit_p (remove_pseudos, sregno)
7419 && (get_regno (lra_reg_info[sregno].restore_rtx) == dregno
7420 || (bitmap_bit_p (remove_pseudos, dregno)
7421 && get_regno (lra_reg_info[sregno].restore_rtx) >= 0
7422 && (get_regno (lra_reg_info[sregno].restore_rtx)
7423 == get_regno (lra_reg_info[dregno].restore_rtx)))))
7424 || (bitmap_bit_p (remove_pseudos, dregno)
7425 && get_regno (lra_reg_info[dregno].restore_rtx) == sregno))
7426 /* One of the following cases:
7427 original <- removed inheritance pseudo
7428 removed inherit pseudo <- another removed inherit pseudo
7429 removed inherit pseudo <- original pseudo
7431 removed_split_pseudo <- original_reg
7432 original_reg <- removed_split_pseudo */
7434 if (lra_dump_file != NULL)
7436 fprintf (lra_dump_file, " Removing %s:\n",
7437 bitmap_bit_p (&lra_split_regs, sregno)
7438 || bitmap_bit_p (&lra_split_regs, dregno)
7439 ? "split" : "inheritance");
7440 dump_insn_slim (lra_dump_file, curr_insn);
7442 delete_move_and_clobber (curr_insn, dregno);
7443 done_p = true;
7445 else if (bitmap_bit_p (remove_pseudos, sregno)
7446 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
7448 /* Search the following pattern:
7449 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
7450 original_pseudo <- inherit_or_split_pseudo1
7451 where the 2nd insn is the current insn and
7452 inherit_or_split_pseudo2 is not removed. If it is found,
7453 change the current insn onto:
7454 original_pseudo <- inherit_or_split_pseudo2. */
7455 for (prev_insn = PREV_INSN (curr_insn);
7456 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
7457 prev_insn = PREV_INSN (prev_insn))
7459 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
7460 && (prev_set = single_set (prev_insn)) != NULL_RTX
7461 /* There should be no subregs in insn we are
7462 searching because only the original reg might
7463 be in subreg when we changed the mode of
7464 load/store for splitting. */
7465 && REG_P (SET_DEST (prev_set))
7466 && REG_P (SET_SRC (prev_set))
7467 && (int) REGNO (SET_DEST (prev_set)) == sregno
7468 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
7469 >= FIRST_PSEUDO_REGISTER)
7470 && (lra_reg_info[prev_sregno].restore_rtx == NULL_RTX
7472 /* As we consider chain of inheritance or
7473 splitting described in above comment we should
7474 check that sregno and prev_sregno were
7475 inheritance/split pseudos created from the
7476 same original regno. */
7477 (get_regno (lra_reg_info[sregno].restore_rtx) >= 0
7478 && (get_regno (lra_reg_info[sregno].restore_rtx)
7479 == get_regno (lra_reg_info[prev_sregno].restore_rtx))))
7480 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
7482 int restore_regno = get_regno (lra_reg_info[sregno].restore_rtx);
7483 if (restore_regno < 0)
7484 restore_regno = prev_sregno;
7485 lra_assert (GET_MODE (SET_SRC (prev_set))
7486 == GET_MODE (regno_reg_rtx[restore_regno]));
7487 /* Although we have a single set, the insn can
7488 contain more one sregno register occurrence
7489 as a source. Change all occurrences. */
7490 lra_substitute_pseudo_within_insn (curr_insn, sregno,
7491 regno_reg_rtx[restore_regno],
7492 false);
7493 /* As we are finishing with processing the insn
7494 here, check the destination too as it might
7495 inheritance pseudo for another pseudo. */
7496 if (bitmap_bit_p (remove_pseudos, dregno)
7497 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
7498 && (restore_rtx
7499 = lra_reg_info[dregno].restore_rtx) != NULL_RTX)
7501 if (GET_CODE (SET_DEST (set)) == SUBREG)
7502 SUBREG_REG (SET_DEST (set)) = restore_rtx;
7503 else
7504 SET_DEST (set) = restore_rtx;
7506 lra_push_insn_and_update_insn_regno_info (curr_insn);
7507 lra_set_used_insn_alternative_by_uid
7508 (INSN_UID (curr_insn), LRA_UNKNOWN_ALT);
7509 done_p = true;
7510 if (lra_dump_file != NULL)
7512 fprintf (lra_dump_file, " Change reload insn:\n");
7513 dump_insn_slim (lra_dump_file, curr_insn);
7518 if (! done_p)
7520 struct lra_insn_reg *reg;
7521 bool restored_regs_p = false;
7522 bool kept_regs_p = false;
7524 curr_id = lra_get_insn_recog_data (curr_insn);
7525 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
7527 regno = reg->regno;
7528 restore_rtx = lra_reg_info[regno].restore_rtx;
7529 if (restore_rtx != NULL_RTX)
7531 if (change_p && bitmap_bit_p (remove_pseudos, regno))
7533 lra_substitute_pseudo_within_insn
7534 (curr_insn, regno, restore_rtx, false);
7535 restored_regs_p = true;
7537 else
7538 kept_regs_p = true;
7541 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
7543 /* The instruction has changed since the previous
7544 constraints pass. */
7545 lra_push_insn_and_update_insn_regno_info (curr_insn);
7546 lra_set_used_insn_alternative_by_uid
7547 (INSN_UID (curr_insn), LRA_UNKNOWN_ALT);
7549 else if (restored_regs_p)
7550 /* The instruction has been restored to the form that
7551 it had during the previous constraints pass. */
7552 lra_update_insn_regno_info (curr_insn);
7553 if (restored_regs_p && lra_dump_file != NULL)
7555 fprintf (lra_dump_file, " Insn after restoring regs:\n");
7556 dump_insn_slim (lra_dump_file, curr_insn);
7561 return change_p;
7564 /* If optional reload pseudos failed to get a hard register or was not
7565 inherited, it is better to remove optional reloads. We do this
7566 transformation after undoing inheritance to figure out necessity to
7567 remove optional reloads easier. Return true if we do any
7568 change. */
7569 static bool
7570 undo_optional_reloads (void)
7572 bool change_p, keep_p;
7573 unsigned int regno, uid;
7574 bitmap_iterator bi, bi2;
7575 rtx_insn *insn;
7576 rtx set, src, dest;
7577 auto_bitmap removed_optional_reload_pseudos (&reg_obstack);
7579 bitmap_copy (removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
7580 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
7582 keep_p = false;
7583 /* Keep optional reloads from previous subpasses. */
7584 if (lra_reg_info[regno].restore_rtx == NULL_RTX
7585 /* If the original pseudo changed its allocation, just
7586 removing the optional pseudo is dangerous as the original
7587 pseudo will have longer live range. */
7588 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] >= 0)
7589 keep_p = true;
7590 else if (reg_renumber[regno] >= 0)
7591 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
7593 insn = lra_insn_recog_data[uid]->insn;
7594 if ((set = single_set (insn)) == NULL_RTX)
7595 continue;
7596 src = SET_SRC (set);
7597 dest = SET_DEST (set);
7598 if ((! REG_P (src) && ! SUBREG_P (src))
7599 || (! REG_P (dest) && ! SUBREG_P (dest)))
7600 continue;
7601 if (get_regno (dest) == (int) regno
7602 /* Ignore insn for optional reloads itself. */
7603 && (get_regno (lra_reg_info[regno].restore_rtx)
7604 != get_regno (src))
7605 /* Check only inheritance on last inheritance pass. */
7606 && get_regno (src) >= new_regno_start
7607 /* Check that the optional reload was inherited. */
7608 && bitmap_bit_p (&lra_inheritance_pseudos, get_regno (src)))
7610 keep_p = true;
7611 break;
7614 if (keep_p)
7616 bitmap_clear_bit (removed_optional_reload_pseudos, regno);
7617 if (lra_dump_file != NULL)
7618 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
7621 change_p = ! bitmap_empty_p (removed_optional_reload_pseudos);
7622 auto_bitmap insn_bitmap (&reg_obstack);
7623 EXECUTE_IF_SET_IN_BITMAP (removed_optional_reload_pseudos, 0, regno, bi)
7625 if (lra_dump_file != NULL)
7626 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
7627 bitmap_copy (insn_bitmap, &lra_reg_info[regno].insn_bitmap);
7628 EXECUTE_IF_SET_IN_BITMAP (insn_bitmap, 0, uid, bi2)
7630 /* We may have already removed a clobber. */
7631 if (!lra_insn_recog_data[uid])
7632 continue;
7633 insn = lra_insn_recog_data[uid]->insn;
7634 if ((set = single_set (insn)) != NULL_RTX)
7636 src = SET_SRC (set);
7637 dest = SET_DEST (set);
7638 if ((REG_P (src) || SUBREG_P (src))
7639 && (REG_P (dest) || SUBREG_P (dest))
7640 && ((get_regno (src) == (int) regno
7641 && (get_regno (lra_reg_info[regno].restore_rtx)
7642 == get_regno (dest)))
7643 || (get_regno (dest) == (int) regno
7644 && (get_regno (lra_reg_info[regno].restore_rtx)
7645 == get_regno (src)))))
7647 if (lra_dump_file != NULL)
7649 fprintf (lra_dump_file, " Deleting move %u\n",
7650 INSN_UID (insn));
7651 dump_insn_slim (lra_dump_file, insn);
7653 delete_move_and_clobber (insn, get_regno (dest));
7654 continue;
7656 /* We should not worry about generation memory-memory
7657 moves here as if the corresponding inheritance did
7658 not work (inheritance pseudo did not get a hard reg),
7659 we remove the inheritance pseudo and the optional
7660 reload. */
7662 if (GET_CODE (PATTERN (insn)) == CLOBBER
7663 && REG_P (SET_DEST (insn))
7664 && get_regno (SET_DEST (insn)) == (int) regno)
7665 /* Refuse to remap clobbers to preexisting pseudos. */
7666 gcc_unreachable ();
7667 lra_substitute_pseudo_within_insn
7668 (insn, regno, lra_reg_info[regno].restore_rtx, false);
7669 lra_update_insn_regno_info (insn);
7670 if (lra_dump_file != NULL)
7672 fprintf (lra_dump_file,
7673 " Restoring original insn:\n");
7674 dump_insn_slim (lra_dump_file, insn);
7678 /* Clear restore_regnos. */
7679 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
7680 lra_reg_info[regno].restore_rtx = NULL_RTX;
7681 return change_p;
7684 /* Entry function for undoing inheritance/split transformation. Return true
7685 if we did any RTL change in this pass. */
7686 bool
7687 lra_undo_inheritance (void)
7689 unsigned int regno;
7690 int hard_regno;
7691 int n_all_inherit, n_inherit, n_all_split, n_split;
7692 rtx restore_rtx;
7693 bitmap_iterator bi;
7694 bool change_p;
7696 lra_undo_inheritance_iter++;
7697 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
7698 return false;
7699 if (lra_dump_file != NULL)
7700 fprintf (lra_dump_file,
7701 "\n********** Undoing inheritance #%d: **********\n\n",
7702 lra_undo_inheritance_iter);
7703 auto_bitmap remove_pseudos (&reg_obstack);
7704 n_inherit = n_all_inherit = 0;
7705 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
7706 if (lra_reg_info[regno].restore_rtx != NULL_RTX)
7708 n_all_inherit++;
7709 if (reg_renumber[regno] < 0
7710 /* If the original pseudo changed its allocation, just
7711 removing inheritance is dangerous as for changing
7712 allocation we used shorter live-ranges. */
7713 && (! REG_P (lra_reg_info[regno].restore_rtx)
7714 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] < 0))
7715 bitmap_set_bit (remove_pseudos, regno);
7716 else
7717 n_inherit++;
7719 if (lra_dump_file != NULL && n_all_inherit != 0)
7720 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
7721 n_inherit, n_all_inherit,
7722 (double) n_inherit / n_all_inherit * 100);
7723 n_split = n_all_split = 0;
7724 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
7725 if ((restore_rtx = lra_reg_info[regno].restore_rtx) != NULL_RTX)
7727 int restore_regno = REGNO (restore_rtx);
7729 n_all_split++;
7730 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
7731 ? reg_renumber[restore_regno] : restore_regno);
7732 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
7733 bitmap_set_bit (remove_pseudos, regno);
7734 else
7736 n_split++;
7737 if (lra_dump_file != NULL)
7738 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
7739 regno, restore_regno);
7742 if (lra_dump_file != NULL && n_all_split != 0)
7743 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
7744 n_split, n_all_split,
7745 (double) n_split / n_all_split * 100);
7746 change_p = remove_inheritance_pseudos (remove_pseudos);
7747 /* Clear restore_regnos. */
7748 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
7749 lra_reg_info[regno].restore_rtx = NULL_RTX;
7750 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
7751 lra_reg_info[regno].restore_rtx = NULL_RTX;
7752 change_p = undo_optional_reloads () || change_p;
7753 if (change_p)
7754 lra_dump_insns_if_possible ("changed func after undoing inheritance");
7755 return change_p;