Disable tests for strdup/strndup on __hpux__
[official-gcc.git] / gcc / lra-constraints.cc
blobdc41bc3d6c6ac8f03898a66c9d16880ff32ba683
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2024 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file contains code for 3 passes: constraint pass,
23 inheritance/split pass, and pass for undoing failed inheritance and
24 split.
26 The major goal of constraint pass is to transform RTL to satisfy
27 insn and address constraints by:
28 o choosing insn alternatives;
29 o generating *reload insns* (or reloads in brief) and *reload
30 pseudos* which will get necessary hard registers later;
31 o substituting pseudos with equivalent values and removing the
32 instructions that initialized those pseudos.
34 The constraint pass has biggest and most complicated code in LRA.
35 There are a lot of important details like:
36 o reuse of input reload pseudos to simplify reload pseudo
37 allocations;
38 o some heuristics to choose insn alternative to improve the
39 inheritance;
40 o early clobbers etc.
42 The pass is mimicking former reload pass in alternative choosing
43 because the reload pass is oriented to current machine description
44 model. It might be changed if the machine description model is
45 changed.
47 There is special code for preventing all LRA and this pass cycling
48 in case of bugs.
50 On the first iteration of the pass we process every instruction and
51 choose an alternative for each one. On subsequent iterations we try
52 to avoid reprocessing instructions if we can be sure that the old
53 choice is still valid.
55 The inheritance/spilt pass is to transform code to achieve
56 ineheritance and live range splitting. It is done on backward
57 traversal of EBBs.
59 The inheritance optimization goal is to reuse values in hard
60 registers. There is analogous optimization in old reload pass. The
61 inheritance is achieved by following transformation:
63 reload_p1 <- p reload_p1 <- p
64 ... new_p <- reload_p1
65 ... => ...
66 reload_p2 <- p reload_p2 <- new_p
68 where p is spilled and not changed between the insns. Reload_p1 is
69 also called *original pseudo* and new_p is called *inheritance
70 pseudo*.
72 The subsequent assignment pass will try to assign the same (or
73 another if it is not possible) hard register to new_p as to
74 reload_p1 or reload_p2.
76 If the assignment pass fails to assign a hard register to new_p,
77 this file will undo the inheritance and restore the original code.
78 This is because implementing the above sequence with a spilled
79 new_p would make the code much worse. The inheritance is done in
80 EBB scope. The above is just a simplified example to get an idea
81 of the inheritance as the inheritance is also done for non-reload
82 insns.
84 Splitting (transformation) is also done in EBB scope on the same
85 pass as the inheritance:
87 r <- ... or ... <- r r <- ... or ... <- r
88 ... s <- r (new insn -- save)
89 ... =>
90 ... r <- s (new insn -- restore)
91 ... <- r ... <- r
93 The *split pseudo* s is assigned to the hard register of the
94 original pseudo or hard register r.
96 Splitting is done:
97 o In EBBs with high register pressure for global pseudos (living
98 in at least 2 BBs) and assigned to hard registers when there
99 are more one reloads needing the hard registers;
100 o for pseudos needing save/restore code around calls.
102 If the split pseudo still has the same hard register as the
103 original pseudo after the subsequent assignment pass or the
104 original pseudo was split, the opposite transformation is done on
105 the same pass for undoing inheritance. */
107 #undef REG_OK_STRICT
109 #include "config.h"
110 #include "system.h"
111 #include "coretypes.h"
112 #include "backend.h"
113 #include "hooks.h"
114 #include "target.h"
115 #include "rtl.h"
116 #include "tree.h"
117 #include "predict.h"
118 #include "df.h"
119 #include "memmodel.h"
120 #include "tm_p.h"
121 #include "expmed.h"
122 #include "optabs.h"
123 #include "regs.h"
124 #include "ira.h"
125 #include "recog.h"
126 #include "output.h"
127 #include "addresses.h"
128 #include "expr.h"
129 #include "cfgrtl.h"
130 #include "rtl-error.h"
131 #include "lra.h"
132 #include "lra-int.h"
133 #include "print-rtl.h"
134 #include "function-abi.h"
135 #include "rtl-iter.h"
137 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
138 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
139 reload insns. */
140 static int bb_reload_num;
142 /* The current insn being processed and corresponding its single set
143 (NULL otherwise), its data (basic block, the insn data, the insn
144 static data, and the mode of each operand). */
145 static rtx_insn *curr_insn;
146 static rtx curr_insn_set;
147 static basic_block curr_bb;
148 static lra_insn_recog_data_t curr_id;
149 static struct lra_static_insn_data *curr_static_id;
150 static machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
151 /* Mode of the register substituted by its equivalence with VOIDmode
152 (e.g. constant) and whose subreg is given operand of the current
153 insn. VOIDmode in all other cases. */
154 static machine_mode original_subreg_reg_mode[MAX_RECOG_OPERANDS];
158 /* Start numbers for new registers and insns at the current constraints
159 pass start. */
160 static int new_regno_start;
161 static int new_insn_uid_start;
163 /* If LOC is nonnull, strip any outer subreg from it. */
164 static inline rtx *
165 strip_subreg (rtx *loc)
167 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
170 /* Return hard regno of REGNO or if it is was not assigned to a hard
171 register, use a hard register from its allocno class. */
172 static int
173 get_try_hard_regno (int regno)
175 int hard_regno;
176 enum reg_class rclass;
178 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
179 hard_regno = lra_get_regno_hard_regno (regno);
180 if (hard_regno >= 0)
181 return hard_regno;
182 rclass = lra_get_allocno_class (regno);
183 if (rclass == NO_REGS)
184 return -1;
185 return ira_class_hard_regs[rclass][0];
188 /* Return the hard regno of X after removing its subreg. If X is not a
189 register or a subreg of a register, return -1. If X is a pseudo, use its
190 assignment. If X is a hard regno, return the final hard regno which will be
191 after elimination. */
192 static int
193 get_hard_regno (rtx x)
195 rtx reg;
196 int hard_regno;
198 reg = x;
199 if (SUBREG_P (x))
200 reg = SUBREG_REG (x);
201 if (! REG_P (reg))
202 return -1;
203 if (! HARD_REGISTER_NUM_P (hard_regno = REGNO (reg)))
204 hard_regno = lra_get_regno_hard_regno (hard_regno);
205 if (hard_regno < 0)
206 return -1;
207 if (HARD_REGISTER_NUM_P (REGNO (reg)))
208 hard_regno = lra_get_elimination_hard_regno (hard_regno);
209 if (SUBREG_P (x))
210 hard_regno += subreg_regno_offset (hard_regno, GET_MODE (reg),
211 SUBREG_BYTE (x), GET_MODE (x));
212 return hard_regno;
215 /* If REGNO is a hard register or has been allocated a hard register,
216 return the class of that register. If REGNO is a reload pseudo
217 created by the current constraints pass, return its allocno class.
218 Return NO_REGS otherwise. */
219 static enum reg_class
220 get_reg_class (int regno)
222 int hard_regno;
224 if (! HARD_REGISTER_NUM_P (hard_regno = regno))
225 hard_regno = lra_get_regno_hard_regno (regno);
226 if (hard_regno >= 0)
228 hard_regno = lra_get_elimination_hard_regno (hard_regno);
229 return REGNO_REG_CLASS (hard_regno);
231 if (regno >= new_regno_start)
232 return lra_get_allocno_class (regno);
233 return NO_REGS;
236 /* Return true if REG_CLASS has enough allocatable hard regs to keep value of
237 REG_MODE. */
238 static bool
239 enough_allocatable_hard_regs_p (enum reg_class reg_class,
240 enum machine_mode reg_mode)
242 int i, j, hard_regno, class_size, nregs;
244 if (hard_reg_set_subset_p (reg_class_contents[reg_class], lra_no_alloc_regs))
245 return false;
246 class_size = ira_class_hard_regs_num[reg_class];
247 for (i = 0; i < class_size; i++)
249 hard_regno = ira_class_hard_regs[reg_class][i];
250 nregs = hard_regno_nregs (hard_regno, reg_mode);
251 if (nregs == 1)
252 return true;
253 for (j = 0; j < nregs; j++)
254 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
255 || ! TEST_HARD_REG_BIT (reg_class_contents[reg_class],
256 hard_regno + j))
257 break;
258 if (j >= nregs)
259 return true;
261 return false;
264 /* True if C is a non-empty register class that has too few registers
265 to be safely used as a reload target class. */
266 #define SMALL_REGISTER_CLASS_P(C) \
267 (ira_class_hard_regs_num [(C)] == 1 \
268 || (ira_class_hard_regs_num [(C)] >= 1 \
269 && targetm.class_likely_spilled_p (C)))
271 /* Return true if REG satisfies (or will satisfy) reg class constraint
272 CL. Use elimination first if REG is a hard register. If REG is a
273 reload pseudo created by this constraints pass, assume that it will
274 be allocated a hard register from its allocno class, but allow that
275 class to be narrowed to CL if it is currently a superset of CL and
276 if either:
278 - ALLOW_ALL_RELOAD_CLASS_CHANGES_P is true or
279 - the instruction we're processing is not a reload move.
281 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
282 REGNO (reg), or NO_REGS if no change in its class was needed. */
283 static bool
284 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class,
285 bool allow_all_reload_class_changes_p = false)
287 enum reg_class rclass, common_class;
288 machine_mode reg_mode;
289 rtx src;
290 int regno = REGNO (reg);
292 if (new_class != NULL)
293 *new_class = NO_REGS;
294 if (regno < FIRST_PSEUDO_REGISTER)
296 rtx final_reg = reg;
297 rtx *final_loc = &final_reg;
299 lra_eliminate_reg_if_possible (final_loc);
300 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
302 reg_mode = GET_MODE (reg);
303 rclass = get_reg_class (regno);
304 src = curr_insn_set != NULL ? SET_SRC (curr_insn_set) : NULL;
305 if (regno < new_regno_start
306 /* Do not allow the constraints for reload instructions to
307 influence the classes of new pseudos. These reloads are
308 typically moves that have many alternatives, and restricting
309 reload pseudos for one alternative may lead to situations
310 where other reload pseudos are no longer allocatable. */
311 || (!allow_all_reload_class_changes_p
312 && INSN_UID (curr_insn) >= new_insn_uid_start
313 && src != NULL
314 && ((REG_P (src) || MEM_P (src))
315 || (GET_CODE (src) == SUBREG
316 && (REG_P (SUBREG_REG (src)) || MEM_P (SUBREG_REG (src)))))))
317 /* When we don't know what class will be used finally for reload
318 pseudos, we use ALL_REGS. */
319 return ((regno >= new_regno_start && rclass == ALL_REGS)
320 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
321 && ! hard_reg_set_subset_p (reg_class_contents[cl],
322 lra_no_alloc_regs)));
323 else
325 common_class = ira_reg_class_subset[rclass][cl];
326 if (new_class != NULL)
327 *new_class = common_class;
328 return (enough_allocatable_hard_regs_p (common_class, reg_mode)
329 /* Do not permit reload insn operand matching (new_class == NULL
330 case) if the new class is too small. */
331 && (new_class != NULL || common_class == rclass
332 || !SMALL_REGISTER_CLASS_P (common_class)));
336 /* Return true if REGNO satisfies a memory constraint. */
337 static bool
338 in_mem_p (int regno)
340 return get_reg_class (regno) == NO_REGS;
343 /* Return true if ADDR is a valid memory address for mode MODE in address
344 space AS, and check that each pseudo has the proper kind of hard
345 reg. */
346 static bool
347 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
348 rtx addr, addr_space_t as)
350 #ifdef GO_IF_LEGITIMATE_ADDRESS
351 lra_assert (ADDR_SPACE_GENERIC_P (as));
352 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
353 return false;
355 win:
356 return true;
357 #else
358 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as,
359 ERROR_MARK);
360 #endif
363 namespace {
364 /* Temporarily eliminates registers in an address (for the lifetime of
365 the object). */
366 class address_eliminator {
367 public:
368 address_eliminator (struct address_info *ad);
369 ~address_eliminator ();
371 private:
372 struct address_info *m_ad;
373 rtx *m_base_loc;
374 rtx m_base_reg;
375 rtx *m_index_loc;
376 rtx m_index_reg;
380 address_eliminator::address_eliminator (struct address_info *ad)
381 : m_ad (ad),
382 m_base_loc (strip_subreg (ad->base_term)),
383 m_base_reg (NULL_RTX),
384 m_index_loc (strip_subreg (ad->index_term)),
385 m_index_reg (NULL_RTX)
387 if (m_base_loc != NULL)
389 m_base_reg = *m_base_loc;
390 /* If we have non-legitimate address which is decomposed not in
391 the way we expected, don't do elimination here. In such case
392 the address will be reloaded and elimination will be done in
393 reload insn finally. */
394 if (REG_P (m_base_reg))
395 lra_eliminate_reg_if_possible (m_base_loc);
396 if (m_ad->base_term2 != NULL)
397 *m_ad->base_term2 = *m_ad->base_term;
399 if (m_index_loc != NULL)
401 m_index_reg = *m_index_loc;
402 if (REG_P (m_index_reg))
403 lra_eliminate_reg_if_possible (m_index_loc);
407 address_eliminator::~address_eliminator ()
409 if (m_base_loc && *m_base_loc != m_base_reg)
411 *m_base_loc = m_base_reg;
412 if (m_ad->base_term2 != NULL)
413 *m_ad->base_term2 = *m_ad->base_term;
415 if (m_index_loc && *m_index_loc != m_index_reg)
416 *m_index_loc = m_index_reg;
419 /* Return true if the eliminated form of AD is a legitimate target address.
420 If OP is a MEM, AD is the address within OP, otherwise OP should be
421 ignored. CONSTRAINT is one constraint that the operand may need
422 to meet. */
423 static bool
424 valid_address_p (rtx op, struct address_info *ad,
425 enum constraint_num constraint)
427 address_eliminator eliminator (ad);
429 /* Allow a memory OP if it matches CONSTRAINT, even if CONSTRAINT is more
430 forgiving than "m".
431 Need to extract memory from op for special memory constraint,
432 i.e. bcst_mem_operand in i386 backend. */
433 if (MEM_P (extract_mem_from_operand (op))
434 && insn_extra_relaxed_memory_constraint (constraint)
435 && constraint_satisfied_p (op, constraint))
436 return true;
438 return valid_address_p (ad->mode, *ad->outer, ad->as);
441 /* For special_memory_operand, it could be false for MEM_P (op),
442 i.e. bcst_mem_operand in i386 backend.
443 Extract and return real memory operand or op. */
445 extract_mem_from_operand (rtx op)
447 for (rtx x = op;; x = XEXP (x, 0))
449 if (MEM_P (x))
450 return x;
451 if (GET_RTX_LENGTH (GET_CODE (x)) != 1
452 || GET_RTX_FORMAT (GET_CODE (x))[0] != 'e')
453 break;
455 return op;
458 /* Return true if the eliminated form of memory reference OP satisfies
459 extra (special) memory constraint CONSTRAINT. */
460 static bool
461 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
463 struct address_info ad;
464 rtx mem = extract_mem_from_operand (op);
465 if (!MEM_P (mem))
466 return false;
468 decompose_mem_address (&ad, mem);
469 address_eliminator eliminator (&ad);
470 return constraint_satisfied_p (op, constraint);
473 /* Return true if the eliminated form of address AD satisfies extra
474 address constraint CONSTRAINT. */
475 static bool
476 satisfies_address_constraint_p (struct address_info *ad,
477 enum constraint_num constraint)
479 address_eliminator eliminator (ad);
480 return constraint_satisfied_p (*ad->outer, constraint);
483 /* Return true if the eliminated form of address OP satisfies extra
484 address constraint CONSTRAINT. */
485 static bool
486 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
488 struct address_info ad;
490 decompose_lea_address (&ad, &op);
491 return satisfies_address_constraint_p (&ad, constraint);
494 /* Initiate equivalences for LRA. As we keep original equivalences
495 before any elimination, we need to make copies otherwise any change
496 in insns might change the equivalences. */
497 void
498 lra_init_equiv (void)
500 ira_expand_reg_equiv ();
501 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
503 rtx res;
505 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
506 ira_reg_equiv[i].memory = copy_rtx (res);
507 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
508 ira_reg_equiv[i].invariant = copy_rtx (res);
512 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
514 /* Update equivalence for REGNO. We need to this as the equivalence
515 might contain other pseudos which are changed by their
516 equivalences. */
517 static void
518 update_equiv (int regno)
520 rtx x;
522 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
523 ira_reg_equiv[regno].memory
524 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
525 NULL_RTX);
526 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
527 ira_reg_equiv[regno].invariant
528 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
529 NULL_RTX);
532 /* If we have decided to substitute X with another value, return that
533 value, otherwise return X. */
534 static rtx
535 get_equiv (rtx x)
537 int regno;
538 rtx res;
540 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
541 || ! ira_reg_equiv[regno].defined_p
542 || ! ira_reg_equiv[regno].profitable_p
543 || lra_get_regno_hard_regno (regno) >= 0)
544 return x;
545 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
547 if (targetm.cannot_substitute_mem_equiv_p (res))
548 return x;
549 return res;
551 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
552 return res;
553 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
554 return res;
555 gcc_unreachable ();
558 /* If we have decided to substitute X with the equivalent value,
559 return that value after elimination for INSN, otherwise return
560 X. */
561 static rtx
562 get_equiv_with_elimination (rtx x, rtx_insn *insn)
564 rtx res = get_equiv (x);
566 if (x == res || CONSTANT_P (res))
567 return res;
568 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
569 false, false, 0, true);
572 /* Set up curr_operand_mode. */
573 static void
574 init_curr_operand_mode (void)
576 int nop = curr_static_id->n_operands;
577 for (int i = 0; i < nop; i++)
579 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
580 if (mode == VOIDmode)
582 /* The .md mode for address operands is the mode of the
583 addressed value rather than the mode of the address itself. */
584 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
585 mode = Pmode;
586 else
587 mode = curr_static_id->operand[i].mode;
589 curr_operand_mode[i] = mode;
595 /* The page contains code to reuse input reloads. */
597 /* Structure describes input reload of the current insns. */
598 struct input_reload
600 /* True for input reload of matched operands. */
601 bool match_p;
602 /* Reloaded value. */
603 rtx input;
604 /* Reload pseudo used. */
605 rtx reg;
608 /* The number of elements in the following array. */
609 static int curr_insn_input_reloads_num;
610 /* Array containing info about input reloads. It is used to find the
611 same input reload and reuse the reload pseudo in this case. */
612 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
614 /* Initiate data concerning reuse of input reloads for the current
615 insn. */
616 static void
617 init_curr_insn_input_reloads (void)
619 curr_insn_input_reloads_num = 0;
622 /* The canonical form of an rtx inside a MEM is not necessarily the same as the
623 canonical form of the rtx outside the MEM. Fix this up in the case that
624 we're reloading an address (and therefore pulling it outside a MEM). */
625 static rtx
626 canonicalize_reload_addr (rtx addr)
628 subrtx_var_iterator::array_type array;
629 FOR_EACH_SUBRTX_VAR (iter, array, addr, NONCONST)
631 rtx x = *iter;
632 if (GET_CODE (x) == MULT && CONST_INT_P (XEXP (x, 1)))
634 const HOST_WIDE_INT ci = INTVAL (XEXP (x, 1));
635 const int pwr2 = exact_log2 (ci);
636 if (pwr2 > 0)
638 /* Rewrite this to use a shift instead, which is canonical when
639 outside of a MEM. */
640 PUT_CODE (x, ASHIFT);
641 XEXP (x, 1) = GEN_INT (pwr2);
646 return addr;
649 /* Create a new pseudo using MODE, RCLASS, EXCLUDE_START_HARD_REGS, ORIGINAL or
650 reuse an existing reload pseudo. Don't reuse an existing reload pseudo if
651 IN_SUBREG_P is true and the reused pseudo should be wrapped up in a SUBREG.
652 The result pseudo is returned through RESULT_REG. Return TRUE if we created
653 a new pseudo, FALSE if we reused an existing reload pseudo. Use TITLE to
654 describe new registers for debug purposes. */
655 static bool
656 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
657 enum reg_class rclass, HARD_REG_SET *exclude_start_hard_regs,
658 bool in_subreg_p, const char *title, rtx *result_reg)
660 int i, regno;
661 enum reg_class new_class;
662 bool unique_p = false;
664 if (type == OP_OUT)
666 /* Output reload registers tend to start out with a conservative
667 choice of register class. Usually this is ALL_REGS, although
668 a target might narrow it (for performance reasons) through
669 targetm.preferred_reload_class. It's therefore quite common
670 for a reload instruction to require a more restrictive class
671 than the class that was originally assigned to the reload register.
673 In these situations, it's more efficient to refine the choice
674 of register class rather than create a second reload register.
675 This also helps to avoid cycling for registers that are only
676 used by reload instructions. */
677 if (REG_P (original)
678 && (int) REGNO (original) >= new_regno_start
679 && INSN_UID (curr_insn) >= new_insn_uid_start
680 && in_class_p (original, rclass, &new_class, true))
682 unsigned int regno = REGNO (original);
683 if (lra_dump_file != NULL)
685 fprintf (lra_dump_file, " Reuse r%d for output ", regno);
686 dump_value_slim (lra_dump_file, original, 1);
688 if (new_class != lra_get_allocno_class (regno))
689 lra_change_class (regno, new_class, ", change to", false);
690 if (lra_dump_file != NULL)
691 fprintf (lra_dump_file, "\n");
692 *result_reg = original;
693 return false;
695 *result_reg
696 = lra_create_new_reg_with_unique_value (mode, original, rclass,
697 exclude_start_hard_regs, title);
698 return true;
700 /* Prevent reuse value of expression with side effects,
701 e.g. volatile memory. */
702 if (! side_effects_p (original))
703 for (i = 0; i < curr_insn_input_reloads_num; i++)
705 if (! curr_insn_input_reloads[i].match_p
706 && rtx_equal_p (curr_insn_input_reloads[i].input, original)
707 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
709 rtx reg = curr_insn_input_reloads[i].reg;
710 regno = REGNO (reg);
711 /* If input is equal to original and both are VOIDmode,
712 GET_MODE (reg) might be still different from mode.
713 Ensure we don't return *result_reg with wrong mode. */
714 if (GET_MODE (reg) != mode)
716 if (in_subreg_p)
717 continue;
718 if (maybe_lt (GET_MODE_SIZE (GET_MODE (reg)),
719 GET_MODE_SIZE (mode)))
720 continue;
721 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
722 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
723 continue;
725 *result_reg = reg;
726 if (lra_dump_file != NULL)
728 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
729 dump_value_slim (lra_dump_file, original, 1);
731 if (new_class != lra_get_allocno_class (regno))
732 lra_change_class (regno, new_class, ", change to", false);
733 if (lra_dump_file != NULL)
734 fprintf (lra_dump_file, "\n");
735 return false;
737 /* If we have an input reload with a different mode, make sure it
738 will get a different hard reg. */
739 else if (REG_P (original)
740 && REG_P (curr_insn_input_reloads[i].input)
741 && REGNO (original) == REGNO (curr_insn_input_reloads[i].input)
742 && (GET_MODE (original)
743 != GET_MODE (curr_insn_input_reloads[i].input)))
744 unique_p = true;
746 *result_reg = (unique_p
747 ? lra_create_new_reg_with_unique_value
748 : lra_create_new_reg) (mode, original, rclass,
749 exclude_start_hard_regs, title);
750 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
751 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
752 curr_insn_input_reloads[curr_insn_input_reloads_num].match_p = false;
753 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
754 return true;
758 /* The page contains major code to choose the current insn alternative
759 and generate reloads for it. */
761 /* Return the offset from REGNO of the least significant register
762 in (reg:MODE REGNO).
764 This function is used to tell whether two registers satisfy
765 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
767 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
768 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
770 lra_constraint_offset (int regno, machine_mode mode)
772 lra_assert (regno < FIRST_PSEUDO_REGISTER);
774 scalar_int_mode int_mode;
775 if (WORDS_BIG_ENDIAN
776 && is_a <scalar_int_mode> (mode, &int_mode)
777 && GET_MODE_SIZE (int_mode) > UNITS_PER_WORD)
778 return hard_regno_nregs (regno, mode) - 1;
779 return 0;
782 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
783 if they are the same hard reg, and has special hacks for
784 auto-increment and auto-decrement. This is specifically intended for
785 process_alt_operands to use in determining whether two operands
786 match. X is the operand whose number is the lower of the two.
788 It is supposed that X is the output operand and Y is the input
789 operand. Y_HARD_REGNO is the final hard regno of register Y or
790 register in subreg Y as we know it now. Otherwise, it is a
791 negative value. */
792 static bool
793 operands_match_p (rtx x, rtx y, int y_hard_regno)
795 int i;
796 RTX_CODE code = GET_CODE (x);
797 const char *fmt;
799 if (x == y)
800 return true;
801 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
802 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
804 int j;
806 i = get_hard_regno (x);
807 if (i < 0)
808 goto slow;
810 if ((j = y_hard_regno) < 0)
811 goto slow;
813 i += lra_constraint_offset (i, GET_MODE (x));
814 j += lra_constraint_offset (j, GET_MODE (y));
816 return i == j;
819 /* If two operands must match, because they are really a single
820 operand of an assembler insn, then two post-increments are invalid
821 because the assembler insn would increment only once. On the
822 other hand, a post-increment matches ordinary indexing if the
823 post-increment is the output operand. */
824 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
825 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
827 /* Two pre-increments are invalid because the assembler insn would
828 increment only once. On the other hand, a pre-increment matches
829 ordinary indexing if the pre-increment is the input operand. */
830 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
831 || GET_CODE (y) == PRE_MODIFY)
832 return operands_match_p (x, XEXP (y, 0), -1);
834 slow:
836 if (code == REG && REG_P (y))
837 return REGNO (x) == REGNO (y);
839 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
840 && x == SUBREG_REG (y))
841 return true;
842 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
843 && SUBREG_REG (x) == y)
844 return true;
846 /* Now we have disposed of all the cases in which different rtx
847 codes can match. */
848 if (code != GET_CODE (y))
849 return false;
851 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
852 if (GET_MODE (x) != GET_MODE (y))
853 return false;
855 switch (code)
857 CASE_CONST_UNIQUE:
858 return false;
860 case CONST_VECTOR:
861 if (!same_vector_encodings_p (x, y))
862 return false;
863 break;
865 case LABEL_REF:
866 return label_ref_label (x) == label_ref_label (y);
867 case SYMBOL_REF:
868 return XSTR (x, 0) == XSTR (y, 0);
870 default:
871 break;
874 /* Compare the elements. If any pair of corresponding elements fail
875 to match, return false for the whole things. */
877 fmt = GET_RTX_FORMAT (code);
878 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
880 int val, j;
881 switch (fmt[i])
883 case 'w':
884 if (XWINT (x, i) != XWINT (y, i))
885 return false;
886 break;
888 case 'i':
889 if (XINT (x, i) != XINT (y, i))
890 return false;
891 break;
893 case 'p':
894 if (maybe_ne (SUBREG_BYTE (x), SUBREG_BYTE (y)))
895 return false;
896 break;
898 case 'e':
899 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
900 if (val == 0)
901 return false;
902 break;
904 case '0':
905 break;
907 case 'E':
908 if (XVECLEN (x, i) != XVECLEN (y, i))
909 return false;
910 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
912 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
913 if (val == 0)
914 return false;
916 break;
918 /* It is believed that rtx's at this level will never
919 contain anything but integers and other rtx's, except for
920 within LABEL_REFs and SYMBOL_REFs. */
921 default:
922 gcc_unreachable ();
925 return true;
928 /* True if X is a constant that can be forced into the constant pool.
929 MODE is the mode of the operand, or VOIDmode if not known. */
930 #define CONST_POOL_OK_P(MODE, X) \
931 ((MODE) != VOIDmode \
932 && CONSTANT_P (X) \
933 && GET_CODE (X) != HIGH \
934 && GET_MODE_SIZE (MODE).is_constant () \
935 && !targetm.cannot_force_const_mem (MODE, X))
937 /* If REG is a reload pseudo, try to make its class satisfying CL. */
938 static void
939 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
941 enum reg_class rclass;
943 /* Do not make more accurate class from reloads generated. They are
944 mostly moves with a lot of constraints. Making more accurate
945 class may results in very narrow class and impossibility of find
946 registers for several reloads of one insn. */
947 if (INSN_UID (curr_insn) >= new_insn_uid_start)
948 return;
949 if (GET_CODE (reg) == SUBREG)
950 reg = SUBREG_REG (reg);
951 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
952 return;
953 if (in_class_p (reg, cl, &rclass) && rclass != cl)
954 lra_change_class (REGNO (reg), rclass, " Change to", true);
957 /* Searches X for any reference to a reg with the same value as REGNO,
958 returning the rtx of the reference found if any. Otherwise,
959 returns NULL_RTX. */
960 static rtx
961 regno_val_use_in (unsigned int regno, rtx x)
963 const char *fmt;
964 int i, j;
965 rtx tem;
967 if (REG_P (x) && lra_reg_info[REGNO (x)].val == lra_reg_info[regno].val)
968 return x;
970 fmt = GET_RTX_FORMAT (GET_CODE (x));
971 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
973 if (fmt[i] == 'e')
975 if ((tem = regno_val_use_in (regno, XEXP (x, i))))
976 return tem;
978 else if (fmt[i] == 'E')
979 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
980 if ((tem = regno_val_use_in (regno , XVECEXP (x, i, j))))
981 return tem;
984 return NULL_RTX;
987 /* Return true if all current insn non-output operands except INS (it
988 has a negaitve end marker) do not use pseudos with the same value
989 as REGNO. */
990 static bool
991 check_conflict_input_operands (int regno, signed char *ins)
993 int in;
994 int n_operands = curr_static_id->n_operands;
996 for (int nop = 0; nop < n_operands; nop++)
997 if (! curr_static_id->operand[nop].is_operator
998 && curr_static_id->operand[nop].type != OP_OUT)
1000 for (int i = 0; (in = ins[i]) >= 0; i++)
1001 if (in == nop)
1002 break;
1003 if (in < 0
1004 && regno_val_use_in (regno, *curr_id->operand_loc[nop]) != NULL_RTX)
1005 return false;
1007 return true;
1010 /* Generate reloads for matching OUT and INS (array of input operand numbers
1011 with end marker -1) with reg class GOAL_CLASS and EXCLUDE_START_HARD_REGS,
1012 considering output operands OUTS (similar array to INS) needing to be in
1013 different registers. Add input and output reloads correspondingly to the
1014 lists *BEFORE and *AFTER. OUT might be negative. In this case we generate
1015 input reloads for matched input operands INS. EARLY_CLOBBER_P is a flag
1016 that the output operand is early clobbered for chosen alternative. */
1017 static void
1018 match_reload (signed char out, signed char *ins, signed char *outs,
1019 enum reg_class goal_class, HARD_REG_SET *exclude_start_hard_regs,
1020 rtx_insn **before, rtx_insn **after, bool early_clobber_p)
1022 bool out_conflict;
1023 int i, in;
1024 rtx new_in_reg, new_out_reg, reg;
1025 machine_mode inmode, outmode;
1026 rtx in_rtx = *curr_id->operand_loc[ins[0]];
1027 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
1029 inmode = curr_operand_mode[ins[0]];
1030 outmode = out < 0 ? inmode : curr_operand_mode[out];
1031 push_to_sequence (*before);
1032 if (inmode != outmode)
1034 /* process_alt_operands has already checked that the mode sizes
1035 are ordered. */
1036 if (partial_subreg_p (outmode, inmode))
1038 bool asm_p = asm_noperands (PATTERN (curr_insn)) >= 0;
1039 int hr;
1040 HARD_REG_SET temp_hard_reg_set;
1042 if (asm_p && (hr = get_hard_regno (out_rtx)) >= 0
1043 && hard_regno_nregs (hr, inmode) > 1)
1045 /* See gcc.c-torture/execute/20030222-1.c.
1046 Consider the code for 32-bit (e.g. BE) target:
1047 int i, v; long x; x = v; asm ("" : "=r" (i) : "0" (x));
1048 We generate the following RTL with reload insns:
1049 1. subreg:si(x:di, 0) = 0;
1050 2. subreg:si(x:di, 4) = v:si;
1051 3. t:di = x:di, dead x;
1052 4. asm ("" : "=r" (subreg:si(t:di,4)) : "0" (t:di))
1053 5. i:si = subreg:si(t:di,4);
1054 If we assign hard reg of x to t, dead code elimination
1055 will remove insn #2 and we will use unitialized hard reg.
1056 So exclude the hard reg of x for t. We could ignore this
1057 problem for non-empty asm using all x value but it is hard to
1058 check that the asm are expanded into insn realy using x
1059 and setting r. */
1060 CLEAR_HARD_REG_SET (temp_hard_reg_set);
1061 if (exclude_start_hard_regs != NULL)
1062 temp_hard_reg_set = *exclude_start_hard_regs;
1063 SET_HARD_REG_BIT (temp_hard_reg_set, hr);
1064 exclude_start_hard_regs = &temp_hard_reg_set;
1066 reg = new_in_reg
1067 = lra_create_new_reg_with_unique_value (inmode, in_rtx, goal_class,
1068 exclude_start_hard_regs,
1069 "");
1070 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
1071 LRA_SUBREG_P (new_out_reg) = 1;
1072 /* If the input reg is dying here, we can use the same hard
1073 register for REG and IN_RTX. We do it only for original
1074 pseudos as reload pseudos can die although original
1075 pseudos still live where reload pseudos dies. */
1076 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
1077 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
1078 && (!early_clobber_p
1079 || check_conflict_input_operands(REGNO (in_rtx), ins)))
1080 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
1082 else
1084 reg = new_out_reg
1085 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
1086 goal_class,
1087 exclude_start_hard_regs,
1088 "");
1089 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
1090 /* NEW_IN_REG is non-paradoxical subreg. We don't want
1091 NEW_OUT_REG living above. We add clobber clause for
1092 this. This is just a temporary clobber. We can remove
1093 it at the end of LRA work. */
1094 rtx_insn *clobber = emit_clobber (new_out_reg);
1095 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
1096 LRA_SUBREG_P (new_in_reg) = 1;
1097 if (GET_CODE (in_rtx) == SUBREG)
1099 rtx subreg_reg = SUBREG_REG (in_rtx);
1101 /* If SUBREG_REG is dying here and sub-registers IN_RTX
1102 and NEW_IN_REG are similar, we can use the same hard
1103 register for REG and SUBREG_REG. */
1104 if (REG_P (subreg_reg)
1105 && (int) REGNO (subreg_reg) < lra_new_regno_start
1106 && GET_MODE (subreg_reg) == outmode
1107 && known_eq (SUBREG_BYTE (in_rtx), SUBREG_BYTE (new_in_reg))
1108 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg))
1109 && (! early_clobber_p
1110 || check_conflict_input_operands (REGNO (subreg_reg),
1111 ins)))
1112 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
1116 else
1118 /* Pseudos have values -- see comments for lra_reg_info.
1119 Different pseudos with the same value do not conflict even if
1120 they live in the same place. When we create a pseudo we
1121 assign value of original pseudo (if any) from which we
1122 created the new pseudo. If we create the pseudo from the
1123 input pseudo, the new pseudo will have no conflict with the
1124 input pseudo which is wrong when the input pseudo lives after
1125 the insn and as the new pseudo value is changed by the insn
1126 output. Therefore we create the new pseudo from the output
1127 except the case when we have single matched dying input
1128 pseudo.
1130 We cannot reuse the current output register because we might
1131 have a situation like "a <- a op b", where the constraints
1132 force the second input operand ("b") to match the output
1133 operand ("a"). "b" must then be copied into a new register
1134 so that it doesn't clobber the current value of "a".
1136 We cannot use the same value if the output pseudo is
1137 early clobbered or the input pseudo is mentioned in the
1138 output, e.g. as an address part in memory, because
1139 output reload will actually extend the pseudo liveness.
1140 We don't care about eliminable hard regs here as we are
1141 interesting only in pseudos. */
1143 /* Matching input's register value is the same as one of the other
1144 output operand. Output operands in a parallel insn must be in
1145 different registers. */
1146 out_conflict = false;
1147 if (REG_P (in_rtx))
1149 for (i = 0; outs[i] >= 0; i++)
1151 rtx other_out_rtx = *curr_id->operand_loc[outs[i]];
1152 if (outs[i] != out && REG_P (other_out_rtx)
1153 && (regno_val_use_in (REGNO (in_rtx), other_out_rtx)
1154 != NULL_RTX))
1156 out_conflict = true;
1157 break;
1162 new_in_reg = new_out_reg
1163 = (! early_clobber_p && ins[1] < 0 && REG_P (in_rtx)
1164 && (int) REGNO (in_rtx) < lra_new_regno_start
1165 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
1166 && (! early_clobber_p
1167 || check_conflict_input_operands (REGNO (in_rtx), ins))
1168 && (out < 0
1169 || regno_val_use_in (REGNO (in_rtx), out_rtx) == NULL_RTX)
1170 && !out_conflict
1171 ? lra_create_new_reg (inmode, in_rtx, goal_class,
1172 exclude_start_hard_regs, "")
1173 : lra_create_new_reg_with_unique_value (outmode, out_rtx, goal_class,
1174 exclude_start_hard_regs,
1175 ""));
1177 /* In operand can be got from transformations before processing insn
1178 constraints. One example of such transformations is subreg
1179 reloading (see function simplify_operand_subreg). The new
1180 pseudos created by the transformations might have inaccurate
1181 class (ALL_REGS) and we should make their classes more
1182 accurate. */
1183 narrow_reload_pseudo_class (in_rtx, goal_class);
1184 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
1185 *before = get_insns ();
1186 end_sequence ();
1187 /* Add the new pseudo to consider values of subsequent input reload
1188 pseudos. */
1189 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
1190 curr_insn_input_reloads[curr_insn_input_reloads_num].input = in_rtx;
1191 curr_insn_input_reloads[curr_insn_input_reloads_num].match_p = true;
1192 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = new_in_reg;
1193 for (i = 0; (in = ins[i]) >= 0; i++)
1194 if (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
1195 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]))
1196 *curr_id->operand_loc[in] = new_in_reg;
1197 else
1199 lra_assert
1200 (GET_MODE (new_out_reg) == GET_MODE (*curr_id->operand_loc[in]));
1201 *curr_id->operand_loc[in] = new_out_reg;
1203 lra_update_dups (curr_id, ins);
1204 if (out < 0)
1205 return;
1206 /* See a comment for the input operand above. */
1207 narrow_reload_pseudo_class (out_rtx, goal_class);
1208 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
1210 reg = SUBREG_P (out_rtx) ? SUBREG_REG (out_rtx) : out_rtx;
1211 start_sequence ();
1212 /* If we had strict_low_part, use it also in reload to keep other
1213 parts unchanged but do it only for regs as strict_low_part
1214 has no sense for memory and probably there is no insn pattern
1215 to match the reload insn in memory case. */
1216 if (out >= 0 && curr_static_id->operand[out].strict_low && REG_P (reg))
1217 out_rtx = gen_rtx_STRICT_LOW_PART (VOIDmode, out_rtx);
1218 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
1219 emit_insn (*after);
1220 *after = get_insns ();
1221 end_sequence ();
1223 *curr_id->operand_loc[out] = new_out_reg;
1224 lra_update_dup (curr_id, out);
1227 /* Return register class which is union of all reg classes in insn
1228 constraint alternative string starting with P. */
1229 static enum reg_class
1230 reg_class_from_constraints (const char *p)
1232 int c, len;
1233 enum reg_class op_class = NO_REGS;
1236 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1238 case '#':
1239 case ',':
1240 return op_class;
1242 case 'g':
1243 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1244 break;
1246 default:
1247 enum constraint_num cn = lookup_constraint (p);
1248 enum reg_class cl = reg_class_for_constraint (cn);
1249 if (cl == NO_REGS)
1251 if (insn_extra_address_constraint (cn))
1252 op_class
1253 = (reg_class_subunion
1254 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1255 ADDRESS, SCRATCH)]);
1256 break;
1259 op_class = reg_class_subunion[op_class][cl];
1260 break;
1262 while ((p += len), c);
1263 return op_class;
1266 /* If OP is a register, return the class of the register as per
1267 get_reg_class, otherwise return NO_REGS. */
1268 static inline enum reg_class
1269 get_op_class (rtx op)
1271 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1274 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1275 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1276 SUBREG for VAL to make them equal. */
1277 static rtx_insn *
1278 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1280 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1282 /* Usually size of mem_pseudo is greater than val size but in
1283 rare cases it can be less as it can be defined by target
1284 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1285 if (! MEM_P (val))
1287 val = gen_lowpart_SUBREG (GET_MODE (mem_pseudo),
1288 GET_CODE (val) == SUBREG
1289 ? SUBREG_REG (val) : val);
1290 LRA_SUBREG_P (val) = 1;
1292 else
1294 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1295 LRA_SUBREG_P (mem_pseudo) = 1;
1298 return to_p ? gen_move_insn (mem_pseudo, val)
1299 : gen_move_insn (val, mem_pseudo);
1302 /* Process a special case insn (register move), return true if we
1303 don't need to process it anymore. INSN should be a single set
1304 insn. Set up that RTL was changed through CHANGE_P and that hook
1305 TARGET_SECONDARY_MEMORY_NEEDED says to use secondary memory through
1306 SEC_MEM_P. */
1307 static bool
1308 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1310 int sregno, dregno;
1311 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1312 rtx_insn *before;
1313 enum reg_class dclass, sclass, secondary_class;
1314 secondary_reload_info sri;
1316 lra_assert (curr_insn_set != NULL_RTX);
1317 dreg = dest = SET_DEST (curr_insn_set);
1318 sreg = src = SET_SRC (curr_insn_set);
1319 if (GET_CODE (dest) == SUBREG)
1320 dreg = SUBREG_REG (dest);
1321 if (GET_CODE (src) == SUBREG)
1322 sreg = SUBREG_REG (src);
1323 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1324 return false;
1325 sclass = dclass = NO_REGS;
1326 if (REG_P (dreg))
1327 dclass = get_reg_class (REGNO (dreg));
1328 gcc_assert (dclass < LIM_REG_CLASSES && dclass >= NO_REGS);
1329 if (dclass == ALL_REGS)
1330 /* ALL_REGS is used for new pseudos created by transformations
1331 like reload of SUBREG_REG (see function
1332 simplify_operand_subreg). We don't know their class yet. We
1333 should figure out the class from processing the insn
1334 constraints not in this fast path function. Even if ALL_REGS
1335 were a right class for the pseudo, secondary_... hooks usually
1336 are not define for ALL_REGS. */
1337 return false;
1338 if (REG_P (sreg))
1339 sclass = get_reg_class (REGNO (sreg));
1340 gcc_assert (sclass < LIM_REG_CLASSES && sclass >= NO_REGS);
1341 if (sclass == ALL_REGS)
1342 /* See comments above. */
1343 return false;
1344 if (sclass == NO_REGS && dclass == NO_REGS)
1345 return false;
1346 if (targetm.secondary_memory_needed (GET_MODE (src), sclass, dclass)
1347 && ((sclass != NO_REGS && dclass != NO_REGS)
1348 || (GET_MODE (src)
1349 != targetm.secondary_memory_needed_mode (GET_MODE (src)))))
1351 *sec_mem_p = true;
1352 return false;
1354 if (! REG_P (dreg) || ! REG_P (sreg))
1355 return false;
1356 sri.prev_sri = NULL;
1357 sri.icode = CODE_FOR_nothing;
1358 sri.extra_cost = 0;
1359 secondary_class = NO_REGS;
1360 /* Set up hard register for a reload pseudo for hook
1361 secondary_reload because some targets just ignore unassigned
1362 pseudos in the hook. */
1363 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1365 dregno = REGNO (dreg);
1366 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1368 else
1369 dregno = -1;
1370 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1372 sregno = REGNO (sreg);
1373 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1375 else
1376 sregno = -1;
1377 if (sclass != NO_REGS)
1378 secondary_class
1379 = (enum reg_class) targetm.secondary_reload (false, dest,
1380 (reg_class_t) sclass,
1381 GET_MODE (src), &sri);
1382 if (sclass == NO_REGS
1383 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1384 && dclass != NO_REGS))
1386 enum reg_class old_sclass = secondary_class;
1387 secondary_reload_info old_sri = sri;
1389 sri.prev_sri = NULL;
1390 sri.icode = CODE_FOR_nothing;
1391 sri.extra_cost = 0;
1392 secondary_class
1393 = (enum reg_class) targetm.secondary_reload (true, src,
1394 (reg_class_t) dclass,
1395 GET_MODE (src), &sri);
1396 /* Check the target hook consistency. */
1397 lra_assert
1398 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1399 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1400 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1402 if (sregno >= 0)
1403 reg_renumber [sregno] = -1;
1404 if (dregno >= 0)
1405 reg_renumber [dregno] = -1;
1406 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1407 return false;
1408 *change_p = true;
1409 new_reg = NULL_RTX;
1410 if (secondary_class != NO_REGS)
1411 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1412 secondary_class, NULL,
1413 "secondary");
1414 start_sequence ();
1415 if (sri.icode == CODE_FOR_nothing)
1416 lra_emit_move (new_reg, src);
1417 else
1419 enum reg_class scratch_class;
1421 scratch_class = (reg_class_from_constraints
1422 (insn_data[sri.icode].operand[2].constraint));
1423 scratch_reg = (lra_create_new_reg_with_unique_value
1424 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1425 scratch_class, NULL, "scratch"));
1426 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1427 src, scratch_reg));
1429 before = get_insns ();
1430 end_sequence ();
1431 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1432 if (new_reg != NULL_RTX)
1433 SET_SRC (curr_insn_set) = new_reg;
1434 else
1436 if (lra_dump_file != NULL)
1438 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1439 dump_insn_slim (lra_dump_file, curr_insn);
1441 lra_set_insn_deleted (curr_insn);
1442 return true;
1444 return false;
1447 /* The following data describe the result of process_alt_operands.
1448 The data are used in curr_insn_transform to generate reloads. */
1450 /* The chosen reg classes which should be used for the corresponding
1451 operands. */
1452 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1453 /* Hard registers which cannot be a start hard register for the corresponding
1454 operands. */
1455 static HARD_REG_SET goal_alt_exclude_start_hard_regs[MAX_RECOG_OPERANDS];
1456 /* True if the operand should be the same as another operand and that
1457 other operand does not need a reload. */
1458 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1459 /* True if the operand does not need a reload. */
1460 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1461 /* True if the operand can be offsetable memory. */
1462 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1463 /* The number of an operand to which given operand can be matched to. */
1464 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1465 /* The number of elements in the following array. */
1466 static int goal_alt_dont_inherit_ops_num;
1467 /* Numbers of operands whose reload pseudos should not be inherited. */
1468 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1469 /* True if we should try only this alternative for the next constraint sub-pass
1470 to speed up the sub-pass. */
1471 static bool goal_reuse_alt_p;
1472 /* True if the insn commutative operands should be swapped. */
1473 static bool goal_alt_swapped;
1474 /* The chosen insn alternative. */
1475 static int goal_alt_number;
1476 /* True if output reload of the stack pointer should be generated. */
1477 static bool goal_alt_out_sp_reload_p;
1479 /* True if the corresponding operand is the result of an equivalence
1480 substitution. */
1481 static bool equiv_substition_p[MAX_RECOG_OPERANDS];
1483 /* The following five variables are used to choose the best insn
1484 alternative. They reflect final characteristics of the best
1485 alternative. */
1487 /* Number of necessary reloads and overall cost reflecting the
1488 previous value and other unpleasantness of the best alternative. */
1489 static int best_losers, best_overall;
1490 /* Overall number hard registers used for reloads. For example, on
1491 some targets we need 2 general registers to reload DFmode and only
1492 one floating point register. */
1493 static int best_reload_nregs;
1494 /* Overall number reflecting distances of previous reloading the same
1495 value. The distances are counted from the current BB start. It is
1496 used to improve inheritance chances. */
1497 static int best_reload_sum;
1499 /* True if the current insn should have no correspondingly input or
1500 output reloads. */
1501 static bool no_input_reloads_p, no_output_reloads_p;
1503 /* True if we swapped the commutative operands in the current
1504 insn. */
1505 static int curr_swapped;
1507 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1508 register of class CL. Add any input reloads to list BEFORE. AFTER
1509 is nonnull if *LOC is an automodified value; handle that case by
1510 adding the required output reloads to list AFTER. Return true if
1511 the RTL was changed.
1513 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1514 register. Return false if the address register is correct. */
1515 static bool
1516 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1517 enum reg_class cl)
1519 int regno;
1520 enum reg_class rclass, new_class;
1521 rtx reg;
1522 rtx new_reg;
1523 machine_mode mode;
1524 bool subreg_p, before_p = false;
1526 subreg_p = GET_CODE (*loc) == SUBREG;
1527 if (subreg_p)
1529 reg = SUBREG_REG (*loc);
1530 mode = GET_MODE (reg);
1532 /* For mode with size bigger than ptr_mode, there unlikely to be "mov"
1533 between two registers with different classes, but there normally will
1534 be "mov" which transfers element of vector register into the general
1535 register, and this normally will be a subreg which should be reloaded
1536 as a whole. This is particularly likely to be triggered when
1537 -fno-split-wide-types specified. */
1538 if (!REG_P (reg)
1539 || in_class_p (reg, cl, &new_class)
1540 || known_le (GET_MODE_SIZE (mode), GET_MODE_SIZE (ptr_mode)))
1541 loc = &SUBREG_REG (*loc);
1544 reg = *loc;
1545 mode = GET_MODE (reg);
1546 if (! REG_P (reg))
1548 if (check_only_p)
1549 return true;
1550 /* Always reload memory in an address even if the target supports
1551 such addresses. */
1552 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, NULL,
1553 "address");
1554 before_p = true;
1556 else
1558 regno = REGNO (reg);
1559 rclass = get_reg_class (regno);
1560 if (! check_only_p
1561 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1563 if (lra_dump_file != NULL)
1565 fprintf (lra_dump_file,
1566 "Changing pseudo %d in address of insn %u on equiv ",
1567 REGNO (reg), INSN_UID (curr_insn));
1568 dump_value_slim (lra_dump_file, *loc, 1);
1569 fprintf (lra_dump_file, "\n");
1571 *loc = copy_rtx (*loc);
1573 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1575 if (check_only_p)
1576 return true;
1577 reg = *loc;
1578 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1579 mode, reg, cl, NULL,
1580 subreg_p, "address", &new_reg))
1581 before_p = true;
1583 else if (new_class != NO_REGS && rclass != new_class)
1585 if (check_only_p)
1586 return true;
1587 lra_change_class (regno, new_class, " Change to", true);
1588 return false;
1590 else
1591 return false;
1593 if (before_p)
1595 push_to_sequence (*before);
1596 lra_emit_move (new_reg, reg);
1597 *before = get_insns ();
1598 end_sequence ();
1600 *loc = new_reg;
1601 if (after != NULL)
1603 start_sequence ();
1604 lra_emit_move (before_p ? copy_rtx (reg) : reg, new_reg);
1605 emit_insn (*after);
1606 *after = get_insns ();
1607 end_sequence ();
1609 return true;
1612 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1613 the insn to be inserted before curr insn. AFTER returns the
1614 the insn to be inserted after curr insn. ORIGREG and NEWREG
1615 are the original reg and new reg for reload. */
1616 static void
1617 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1618 rtx newreg)
1620 if (before)
1622 push_to_sequence (*before);
1623 lra_emit_move (newreg, origreg);
1624 *before = get_insns ();
1625 end_sequence ();
1627 if (after)
1629 start_sequence ();
1630 lra_emit_move (origreg, newreg);
1631 emit_insn (*after);
1632 *after = get_insns ();
1633 end_sequence ();
1637 static bool valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1638 static bool process_address (int, bool, rtx_insn **, rtx_insn **);
1640 /* Make reloads for subreg in operand NOP with internal subreg mode
1641 REG_MODE, add new reloads for further processing. Return true if
1642 any change was done. */
1643 static bool
1644 simplify_operand_subreg (int nop, machine_mode reg_mode)
1646 int hard_regno, inner_hard_regno;
1647 rtx_insn *before, *after;
1648 machine_mode mode, innermode;
1649 rtx reg, new_reg;
1650 rtx operand = *curr_id->operand_loc[nop];
1651 enum reg_class regclass;
1652 enum op_type type;
1654 before = after = NULL;
1656 if (GET_CODE (operand) != SUBREG)
1657 return false;
1659 mode = GET_MODE (operand);
1660 reg = SUBREG_REG (operand);
1661 innermode = GET_MODE (reg);
1662 type = curr_static_id->operand[nop].type;
1663 if (MEM_P (reg))
1665 const bool addr_was_valid
1666 = valid_address_p (innermode, XEXP (reg, 0), MEM_ADDR_SPACE (reg));
1667 alter_subreg (curr_id->operand_loc[nop], false);
1668 rtx subst = *curr_id->operand_loc[nop];
1669 lra_assert (MEM_P (subst));
1670 const bool addr_is_valid = valid_address_p (GET_MODE (subst),
1671 XEXP (subst, 0),
1672 MEM_ADDR_SPACE (subst));
1673 if (!addr_was_valid
1674 || addr_is_valid
1675 || ((get_constraint_type (lookup_constraint
1676 (curr_static_id->operand[nop].constraint))
1677 != CT_SPECIAL_MEMORY)
1678 /* We still can reload address and if the address is
1679 valid, we can remove subreg without reloading its
1680 inner memory. */
1681 && valid_address_p (GET_MODE (subst),
1682 regno_reg_rtx
1683 [ira_class_hard_regs
1684 [base_reg_class (GET_MODE (subst),
1685 MEM_ADDR_SPACE (subst),
1686 ADDRESS, SCRATCH)][0]],
1687 MEM_ADDR_SPACE (subst))))
1689 /* If we change the address for a paradoxical subreg of memory, the
1690 new address might violate the necessary alignment or the access
1691 might be slow; take this into consideration. We need not worry
1692 about accesses beyond allocated memory for paradoxical memory
1693 subregs as we don't substitute such equiv memory (see processing
1694 equivalences in function lra_constraints) and because for spilled
1695 pseudos we allocate stack memory enough for the biggest
1696 corresponding paradoxical subreg.
1698 However, do not blindly simplify a (subreg (mem ...)) for
1699 WORD_REGISTER_OPERATIONS targets as this may lead to loading junk
1700 data into a register when the inner is narrower than outer or
1701 missing important data from memory when the inner is wider than
1702 outer. This rule only applies to modes that are no wider than
1703 a word.
1705 If valid memory becomes invalid after subreg elimination
1706 and address might be different we still have to reload
1707 memory.
1709 if ((! addr_was_valid
1710 || addr_is_valid
1711 || known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (innermode)))
1712 && !(maybe_ne (GET_MODE_PRECISION (mode),
1713 GET_MODE_PRECISION (innermode))
1714 && known_le (GET_MODE_SIZE (mode), UNITS_PER_WORD)
1715 && known_le (GET_MODE_SIZE (innermode), UNITS_PER_WORD)
1716 && WORD_REGISTER_OPERATIONS)
1717 && (!(MEM_ALIGN (subst) < GET_MODE_ALIGNMENT (mode)
1718 && targetm.slow_unaligned_access (mode, MEM_ALIGN (subst)))
1719 || (MEM_ALIGN (reg) < GET_MODE_ALIGNMENT (innermode)
1720 && targetm.slow_unaligned_access (innermode,
1721 MEM_ALIGN (reg)))))
1722 return true;
1724 *curr_id->operand_loc[nop] = operand;
1726 /* But if the address was not valid, we cannot reload the MEM without
1727 reloading the address first. */
1728 if (!addr_was_valid)
1729 process_address (nop, false, &before, &after);
1731 /* INNERMODE is fast, MODE slow. Reload the mem in INNERMODE. */
1732 enum reg_class rclass
1733 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1734 if (get_reload_reg (curr_static_id->operand[nop].type, innermode,
1735 reg, rclass, NULL,
1736 true, "slow/invalid mem", &new_reg))
1738 bool insert_before, insert_after;
1739 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1741 insert_before = (type != OP_OUT
1742 || partial_subreg_p (mode, innermode));
1743 insert_after = type != OP_IN;
1744 insert_move_for_subreg (insert_before ? &before : NULL,
1745 insert_after ? &after : NULL,
1746 reg, new_reg);
1748 SUBREG_REG (operand) = new_reg;
1750 /* Convert to MODE. */
1751 reg = operand;
1752 rclass
1753 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1754 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1755 rclass, NULL,
1756 true, "slow/invalid mem", &new_reg))
1758 bool insert_before, insert_after;
1759 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1761 insert_before = type != OP_OUT;
1762 insert_after = type != OP_IN;
1763 insert_move_for_subreg (insert_before ? &before : NULL,
1764 insert_after ? &after : NULL,
1765 reg, new_reg);
1767 *curr_id->operand_loc[nop] = new_reg;
1768 lra_process_new_insns (curr_insn, before, after,
1769 "Inserting slow/invalid mem reload");
1770 return true;
1773 /* If the address was valid and became invalid, prefer to reload
1774 the memory. Typical case is when the index scale should
1775 correspond the memory. */
1776 *curr_id->operand_loc[nop] = operand;
1777 /* Do not return false here as the MEM_P (reg) will be processed
1778 later in this function. */
1780 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1782 alter_subreg (curr_id->operand_loc[nop], false);
1783 return true;
1785 else if (CONSTANT_P (reg))
1787 /* Try to simplify subreg of constant. It is usually result of
1788 equivalence substitution. */
1789 if (innermode == VOIDmode
1790 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1791 innermode = curr_static_id->operand[nop].mode;
1792 if ((new_reg = simplify_subreg (mode, reg, innermode,
1793 SUBREG_BYTE (operand))) != NULL_RTX)
1795 *curr_id->operand_loc[nop] = new_reg;
1796 return true;
1799 /* Put constant into memory when we have mixed modes. It generates
1800 a better code in most cases as it does not need a secondary
1801 reload memory. It also prevents LRA looping when LRA is using
1802 secondary reload memory again and again. */
1803 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1804 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1806 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1807 alter_subreg (curr_id->operand_loc[nop], false);
1808 return true;
1810 auto fp_subreg_can_be_simplified_after_reload_p = [] (machine_mode innermode,
1811 poly_uint64 offset,
1812 machine_mode mode) {
1813 reload_completed = 1;
1814 bool res = simplify_subreg_regno (FRAME_POINTER_REGNUM,
1815 innermode,
1816 offset, mode) >= 0;
1817 reload_completed = 0;
1818 return res;
1820 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1821 if there may be a problem accessing OPERAND in the outer
1822 mode. */
1823 if ((REG_P (reg)
1824 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1825 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1826 /* Don't reload paradoxical subregs because we could be looping
1827 having repeatedly final regno out of hard regs range. */
1828 && (hard_regno_nregs (hard_regno, innermode)
1829 >= hard_regno_nregs (hard_regno, mode))
1830 && simplify_subreg_regno (hard_regno, innermode,
1831 SUBREG_BYTE (operand), mode) < 0
1832 /* Exclude reloading of frame pointer in subreg if frame pointer can not
1833 be simplified here only because the reload is not finished yet. */
1834 && (hard_regno != FRAME_POINTER_REGNUM
1835 || !fp_subreg_can_be_simplified_after_reload_p (innermode,
1836 SUBREG_BYTE (operand),
1837 mode))
1838 /* Don't reload subreg for matching reload. It is actually
1839 valid subreg in LRA. */
1840 && ! LRA_SUBREG_P (operand))
1841 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1843 enum reg_class rclass;
1845 if (REG_P (reg))
1846 /* There is a big probability that we will get the same class
1847 for the new pseudo and we will get the same insn which
1848 means infinite looping. So spill the new pseudo. */
1849 rclass = NO_REGS;
1850 else
1851 /* The class will be defined later in curr_insn_transform. */
1852 rclass
1853 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1855 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1856 rclass, NULL,
1857 true, "subreg reg", &new_reg))
1859 bool insert_before, insert_after;
1860 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1862 insert_before = (type != OP_OUT
1863 || read_modify_subreg_p (operand));
1864 insert_after = (type != OP_IN);
1865 insert_move_for_subreg (insert_before ? &before : NULL,
1866 insert_after ? &after : NULL,
1867 reg, new_reg);
1869 SUBREG_REG (operand) = new_reg;
1870 lra_process_new_insns (curr_insn, before, after,
1871 "Inserting subreg reload");
1872 return true;
1874 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1875 IRA allocates hardreg to the inner pseudo reg according to its mode
1876 instead of the outermode, so the size of the hardreg may not be enough
1877 to contain the outermode operand, in that case we may need to insert
1878 reload for the reg. For the following two types of paradoxical subreg,
1879 we need to insert reload:
1880 1. If the op_type is OP_IN, and the hardreg could not be paired with
1881 other hardreg to contain the outermode operand
1882 (checked by in_hard_reg_set_p), we need to insert the reload.
1883 2. If the op_type is OP_OUT or OP_INOUT.
1885 Here is a paradoxical subreg example showing how the reload is generated:
1887 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1888 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1890 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1891 here, if reg107 is assigned to hardreg R15, because R15 is the last
1892 hardreg, compiler cannot find another hardreg to pair with R15 to
1893 contain TImode data. So we insert a TImode reload reg180 for it.
1894 After reload is inserted:
1896 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1897 (reg:DI 107 [ __comp ])) -1
1898 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1899 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1901 Two reload hard registers will be allocated to reg180 to save TImode data
1902 in LRA_assign.
1904 For LRA pseudos this should normally be handled by the biggest_mode
1905 mechanism. However, it's possible for new uses of an LRA pseudo
1906 to be introduced after we've allocated it, such as when undoing
1907 inheritance, and the allocated register might not then be appropriate
1908 for the new uses. */
1909 else if (REG_P (reg)
1910 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1911 && paradoxical_subreg_p (operand)
1912 && (inner_hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1913 && ((hard_regno
1914 = simplify_subreg_regno (inner_hard_regno, innermode,
1915 SUBREG_BYTE (operand), mode)) < 0
1916 || ((hard_regno_nregs (inner_hard_regno, innermode)
1917 < hard_regno_nregs (hard_regno, mode))
1918 && (regclass = lra_get_allocno_class (REGNO (reg)))
1919 && (type != OP_IN
1920 || !in_hard_reg_set_p (reg_class_contents[regclass],
1921 mode, hard_regno)
1922 || overlaps_hard_reg_set_p (lra_no_alloc_regs,
1923 mode, hard_regno)))))
1925 /* The class will be defined later in curr_insn_transform. */
1926 enum reg_class rclass
1927 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1929 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1930 rclass, NULL,
1931 true, "paradoxical subreg", &new_reg))
1933 rtx subreg;
1934 bool insert_before, insert_after;
1936 PUT_MODE (new_reg, mode);
1937 subreg = gen_lowpart_SUBREG (innermode, new_reg);
1938 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1940 insert_before = (type != OP_OUT);
1941 insert_after = (type != OP_IN);
1942 insert_move_for_subreg (insert_before ? &before : NULL,
1943 insert_after ? &after : NULL,
1944 reg, subreg);
1946 SUBREG_REG (operand) = new_reg;
1947 lra_process_new_insns (curr_insn, before, after,
1948 "Inserting paradoxical subreg reload");
1949 return true;
1951 return false;
1954 /* Return TRUE if X refers for a hard register from SET. */
1955 static bool
1956 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1958 int i, j, x_hard_regno;
1959 machine_mode mode;
1960 const char *fmt;
1961 enum rtx_code code;
1963 if (x == NULL_RTX)
1964 return false;
1965 code = GET_CODE (x);
1966 mode = GET_MODE (x);
1968 if (code == SUBREG)
1970 /* For all SUBREGs we want to check whether the full multi-register
1971 overlaps the set. For normal SUBREGs this means 'get_hard_regno' of
1972 the inner register, for paradoxical SUBREGs this means the
1973 'get_hard_regno' of the full SUBREG and for complete SUBREGs either is
1974 fine. Use the wider mode for all cases. */
1975 rtx subreg = SUBREG_REG (x);
1976 mode = wider_subreg_mode (x);
1977 if (mode == GET_MODE (subreg))
1979 x = subreg;
1980 code = GET_CODE (x);
1984 if (REG_P (x) || SUBREG_P (x))
1986 x_hard_regno = get_hard_regno (x);
1987 return (x_hard_regno >= 0
1988 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1990 fmt = GET_RTX_FORMAT (code);
1991 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1993 if (fmt[i] == 'e')
1995 if (uses_hard_regs_p (XEXP (x, i), set))
1996 return true;
1998 else if (fmt[i] == 'E')
2000 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2001 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
2002 return true;
2005 return false;
2008 /* Return true if OP is a spilled pseudo. */
2009 static inline bool
2010 spilled_pseudo_p (rtx op)
2012 return (REG_P (op)
2013 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
2016 /* Return true if X is a general constant. */
2017 static inline bool
2018 general_constant_p (rtx x)
2020 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
2023 static bool
2024 reg_in_class_p (rtx reg, enum reg_class cl)
2026 if (cl == NO_REGS)
2027 return get_reg_class (REGNO (reg)) == NO_REGS;
2028 return in_class_p (reg, cl, NULL);
2031 /* Return true if SET of RCLASS contains no hard regs which can be
2032 used in MODE. */
2033 static bool
2034 prohibited_class_reg_set_mode_p (enum reg_class rclass,
2035 HARD_REG_SET &set,
2036 machine_mode mode)
2038 HARD_REG_SET temp;
2040 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
2041 temp = set & ~lra_no_alloc_regs;
2042 return (hard_reg_set_subset_p
2043 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
2047 /* Used to check validity info about small class input operands. It
2048 should be incremented at start of processing an insn
2049 alternative. */
2050 static unsigned int curr_small_class_check = 0;
2052 /* Update number of used inputs of class OP_CLASS for operand NOP
2053 of alternative NALT. Return true if we have more such class operands
2054 than the number of available regs. */
2055 static bool
2056 update_and_check_small_class_inputs (int nop, int nalt,
2057 enum reg_class op_class)
2059 static unsigned int small_class_check[LIM_REG_CLASSES];
2060 static int small_class_input_nums[LIM_REG_CLASSES];
2062 if (SMALL_REGISTER_CLASS_P (op_class)
2063 /* We are interesting in classes became small because of fixing
2064 some hard regs, e.g. by an user through GCC options. */
2065 && hard_reg_set_intersect_p (reg_class_contents[op_class],
2066 ira_no_alloc_regs)
2067 && (curr_static_id->operand[nop].type != OP_OUT
2068 || TEST_BIT (curr_static_id->operand[nop].early_clobber_alts, nalt)))
2070 if (small_class_check[op_class] == curr_small_class_check)
2071 small_class_input_nums[op_class]++;
2072 else
2074 small_class_check[op_class] = curr_small_class_check;
2075 small_class_input_nums[op_class] = 1;
2077 if (small_class_input_nums[op_class] > ira_class_hard_regs_num[op_class])
2078 return true;
2080 return false;
2083 /* Print operand constraints for alternative ALT_NUMBER of the current
2084 insn. */
2085 static void
2086 print_curr_insn_alt (int alt_number)
2088 for (int i = 0; i < curr_static_id->n_operands; i++)
2090 const char *p = (curr_static_id->operand_alternative
2091 [alt_number * curr_static_id->n_operands + i].constraint);
2092 if (*p == '\0')
2093 continue;
2094 fprintf (lra_dump_file, " (%d) ", i);
2095 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
2096 fputc (*p, lra_dump_file);
2100 /* Major function to choose the current insn alternative and what
2101 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
2102 negative we should consider only this alternative. Return false if
2103 we cannot choose the alternative or find how to reload the
2104 operands. */
2105 static bool
2106 process_alt_operands (int only_alternative)
2108 bool ok_p = false;
2109 int nop, overall, nalt;
2110 int n_alternatives = curr_static_id->n_alternatives;
2111 int n_operands = curr_static_id->n_operands;
2112 /* LOSERS counts the operands that don't fit this alternative and
2113 would require loading. */
2114 int losers;
2115 int addr_losers;
2116 /* REJECT is a count of how undesirable this alternative says it is
2117 if any reloading is required. If the alternative matches exactly
2118 then REJECT is ignored, but otherwise it gets this much counted
2119 against it in addition to the reloading needed. */
2120 int reject;
2121 /* This is defined by '!' or '?' alternative constraint and added to
2122 reject. But in some cases it can be ignored. */
2123 int static_reject;
2124 int op_reject;
2125 /* The number of elements in the following array. */
2126 int early_clobbered_regs_num;
2127 /* Numbers of operands which are early clobber registers. */
2128 int early_clobbered_nops[MAX_RECOG_OPERANDS];
2129 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
2130 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
2131 HARD_REG_SET curr_alt_exclude_start_hard_regs[MAX_RECOG_OPERANDS];
2132 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
2133 bool curr_alt_win[MAX_RECOG_OPERANDS];
2134 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
2135 int curr_alt_matches[MAX_RECOG_OPERANDS];
2136 /* The number of elements in the following array. */
2137 int curr_alt_dont_inherit_ops_num;
2138 /* Numbers of operands whose reload pseudos should not be inherited. */
2139 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
2140 bool curr_reuse_alt_p;
2141 /* True if output stack pointer reload should be generated for the current
2142 alternative. */
2143 bool curr_alt_out_sp_reload_p;
2144 bool curr_alt_class_change_p;
2145 rtx op;
2146 /* The register when the operand is a subreg of register, otherwise the
2147 operand itself. */
2148 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
2149 /* The register if the operand is a register or subreg of register,
2150 otherwise NULL. */
2151 rtx operand_reg[MAX_RECOG_OPERANDS];
2152 int hard_regno[MAX_RECOG_OPERANDS];
2153 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
2154 int reload_nregs, reload_sum;
2155 bool costly_p;
2156 enum reg_class cl;
2157 const HARD_REG_SET *cl_filter;
2159 /* Calculate some data common for all alternatives to speed up the
2160 function. */
2161 for (nop = 0; nop < n_operands; nop++)
2163 rtx reg;
2165 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
2166 /* The real hard regno of the operand after the allocation. */
2167 hard_regno[nop] = get_hard_regno (op);
2169 operand_reg[nop] = reg = op;
2170 biggest_mode[nop] = GET_MODE (op);
2171 if (GET_CODE (op) == SUBREG)
2173 biggest_mode[nop] = wider_subreg_mode (op);
2174 operand_reg[nop] = reg = SUBREG_REG (op);
2176 if (! REG_P (reg))
2177 operand_reg[nop] = NULL_RTX;
2178 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
2179 || ((int) REGNO (reg)
2180 == lra_get_elimination_hard_regno (REGNO (reg))))
2181 no_subreg_reg_operand[nop] = reg;
2182 else
2183 operand_reg[nop] = no_subreg_reg_operand[nop]
2184 /* Just use natural mode for elimination result. It should
2185 be enough for extra constraints hooks. */
2186 = regno_reg_rtx[hard_regno[nop]];
2189 /* The constraints are made of several alternatives. Each operand's
2190 constraint looks like foo,bar,... with commas separating the
2191 alternatives. The first alternatives for all operands go
2192 together, the second alternatives go together, etc.
2194 First loop over alternatives. */
2195 alternative_mask preferred = curr_id->preferred_alternatives;
2196 if (only_alternative >= 0)
2197 preferred &= ALTERNATIVE_BIT (only_alternative);
2199 for (nalt = 0; nalt < n_alternatives; nalt++)
2201 /* Loop over operands for one constraint alternative. */
2202 if (!TEST_BIT (preferred, nalt))
2203 continue;
2205 if (lra_dump_file != NULL)
2207 fprintf (lra_dump_file, " Considering alt=%d of insn %d: ",
2208 nalt, INSN_UID (curr_insn));
2209 print_curr_insn_alt (nalt);
2210 fprintf (lra_dump_file, "\n");
2213 bool matching_early_clobber[MAX_RECOG_OPERANDS];
2214 curr_small_class_check++;
2215 overall = losers = addr_losers = 0;
2216 static_reject = reject = reload_nregs = reload_sum = 0;
2217 for (nop = 0; nop < n_operands; nop++)
2219 int inc = (curr_static_id
2220 ->operand_alternative[nalt * n_operands + nop].reject);
2221 if (lra_dump_file != NULL && inc != 0)
2222 fprintf (lra_dump_file,
2223 " Staticly defined alt reject+=%d\n", inc);
2224 static_reject += inc;
2225 matching_early_clobber[nop] = 0;
2227 reject += static_reject;
2228 early_clobbered_regs_num = 0;
2229 curr_alt_out_sp_reload_p = false;
2230 curr_reuse_alt_p = true;
2231 curr_alt_class_change_p = false;
2233 for (nop = 0; nop < n_operands; nop++)
2235 const char *p;
2236 char *end;
2237 int len, c, m, i, opalt_num, this_alternative_matches;
2238 bool win, did_match, offmemok, early_clobber_p;
2239 /* false => this operand can be reloaded somehow for this
2240 alternative. */
2241 bool badop;
2242 /* true => this operand can be reloaded if the alternative
2243 allows regs. */
2244 bool winreg;
2245 /* True if a constant forced into memory would be OK for
2246 this operand. */
2247 bool constmemok;
2248 enum reg_class this_alternative, this_costly_alternative;
2249 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
2250 HARD_REG_SET this_alternative_exclude_start_hard_regs;
2251 bool this_alternative_match_win, this_alternative_win;
2252 bool this_alternative_offmemok;
2253 bool scratch_p;
2254 machine_mode mode;
2255 enum constraint_num cn;
2256 bool class_change_p = false;
2258 opalt_num = nalt * n_operands + nop;
2259 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
2261 /* Fast track for no constraints at all. */
2262 curr_alt[nop] = NO_REGS;
2263 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
2264 curr_alt_win[nop] = true;
2265 curr_alt_match_win[nop] = false;
2266 curr_alt_offmemok[nop] = false;
2267 curr_alt_matches[nop] = -1;
2268 continue;
2271 op = no_subreg_reg_operand[nop];
2272 mode = curr_operand_mode[nop];
2274 win = did_match = winreg = offmemok = constmemok = false;
2275 badop = true;
2277 early_clobber_p = false;
2278 p = curr_static_id->operand_alternative[opalt_num].constraint;
2280 this_costly_alternative = this_alternative = NO_REGS;
2281 /* We update set of possible hard regs besides its class
2282 because reg class might be inaccurate. For example,
2283 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
2284 is translated in HI_REGS because classes are merged by
2285 pairs and there is no accurate intermediate class. */
2286 CLEAR_HARD_REG_SET (this_alternative_set);
2287 CLEAR_HARD_REG_SET (this_costly_alternative_set);
2288 CLEAR_HARD_REG_SET (this_alternative_exclude_start_hard_regs);
2289 this_alternative_win = false;
2290 this_alternative_match_win = false;
2291 this_alternative_offmemok = false;
2292 this_alternative_matches = -1;
2294 /* An empty constraint should be excluded by the fast
2295 track. */
2296 lra_assert (*p != 0 && *p != ',');
2298 op_reject = 0;
2299 /* Scan this alternative's specs for this operand; set WIN
2300 if the operand fits any letter in this alternative.
2301 Otherwise, clear BADOP if this operand could fit some
2302 letter after reloads, or set WINREG if this operand could
2303 fit after reloads provided the constraint allows some
2304 registers. */
2305 costly_p = false;
2308 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
2310 case '\0':
2311 len = 0;
2312 break;
2313 case ',':
2314 c = '\0';
2315 break;
2317 case '&':
2318 early_clobber_p = true;
2319 break;
2321 case '$':
2322 op_reject += LRA_MAX_REJECT;
2323 break;
2324 case '^':
2325 op_reject += LRA_LOSER_COST_FACTOR;
2326 break;
2328 case '#':
2329 /* Ignore rest of this alternative. */
2330 c = '\0';
2331 break;
2333 case '0': case '1': case '2': case '3': case '4':
2334 case '5': case '6': case '7': case '8': case '9':
2336 int m_hregno;
2337 bool match_p;
2339 m = strtoul (p, &end, 10);
2340 p = end;
2341 len = 0;
2342 lra_assert (nop > m);
2344 /* Reject matches if we don't know which operand is
2345 bigger. This situation would arguably be a bug in
2346 an .md pattern, but could also occur in a user asm. */
2347 if (!ordered_p (GET_MODE_SIZE (biggest_mode[m]),
2348 GET_MODE_SIZE (biggest_mode[nop])))
2349 break;
2351 /* Don't match wrong asm insn operands for proper
2352 diagnostic later. */
2353 if (INSN_CODE (curr_insn) < 0
2354 && (curr_operand_mode[m] == BLKmode
2355 || curr_operand_mode[nop] == BLKmode)
2356 && curr_operand_mode[m] != curr_operand_mode[nop])
2357 break;
2359 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
2360 /* We are supposed to match a previous operand.
2361 If we do, we win if that one did. If we do
2362 not, count both of the operands as losers.
2363 (This is too conservative, since most of the
2364 time only a single reload insn will be needed
2365 to make the two operands win. As a result,
2366 this alternative may be rejected when it is
2367 actually desirable.) */
2368 match_p = false;
2369 if (operands_match_p (*curr_id->operand_loc[nop],
2370 *curr_id->operand_loc[m], m_hregno))
2372 /* We should reject matching of an early
2373 clobber operand if the matching operand is
2374 not dying in the insn. */
2375 if (!TEST_BIT (curr_static_id->operand[m]
2376 .early_clobber_alts, nalt)
2377 || operand_reg[nop] == NULL_RTX
2378 || (find_regno_note (curr_insn, REG_DEAD,
2379 REGNO (op))
2380 || REGNO (op) == REGNO (operand_reg[m])))
2381 match_p = true;
2383 if (match_p)
2385 /* If we are matching a non-offsettable
2386 address where an offsettable address was
2387 expected, then we must reject this
2388 combination, because we can't reload
2389 it. */
2390 if (curr_alt_offmemok[m]
2391 && MEM_P (*curr_id->operand_loc[m])
2392 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
2393 continue;
2395 else
2397 /* If the operands do not match and one
2398 operand is INOUT, we can not match them.
2399 Try other possibilities, e.g. other
2400 alternatives or commutative operand
2401 exchange. */
2402 if (curr_static_id->operand[nop].type == OP_INOUT
2403 || curr_static_id->operand[m].type == OP_INOUT)
2404 break;
2405 /* Operands don't match. If the operands are
2406 different user defined explicit hard
2407 registers, then we cannot make them match
2408 when one is early clobber operand. */
2409 if ((REG_P (*curr_id->operand_loc[nop])
2410 || SUBREG_P (*curr_id->operand_loc[nop]))
2411 && (REG_P (*curr_id->operand_loc[m])
2412 || SUBREG_P (*curr_id->operand_loc[m])))
2414 rtx nop_reg = *curr_id->operand_loc[nop];
2415 if (SUBREG_P (nop_reg))
2416 nop_reg = SUBREG_REG (nop_reg);
2417 rtx m_reg = *curr_id->operand_loc[m];
2418 if (SUBREG_P (m_reg))
2419 m_reg = SUBREG_REG (m_reg);
2421 if (REG_P (nop_reg)
2422 && HARD_REGISTER_P (nop_reg)
2423 && REG_USERVAR_P (nop_reg)
2424 && REG_P (m_reg)
2425 && HARD_REGISTER_P (m_reg)
2426 && REG_USERVAR_P (m_reg))
2428 int i;
2430 for (i = 0; i < early_clobbered_regs_num; i++)
2431 if (m == early_clobbered_nops[i])
2432 break;
2433 if (i < early_clobbered_regs_num
2434 || early_clobber_p)
2435 break;
2438 /* Both operands must allow a reload register,
2439 otherwise we cannot make them match. */
2440 if (curr_alt[m] == NO_REGS)
2441 break;
2442 /* Retroactively mark the operand we had to
2443 match as a loser, if it wasn't already and
2444 it wasn't matched to a register constraint
2445 (e.g it might be matched by memory). */
2446 if (curr_alt_win[m]
2447 && (operand_reg[m] == NULL_RTX
2448 || hard_regno[m] < 0))
2450 losers++;
2451 reload_nregs
2452 += (ira_reg_class_max_nregs[curr_alt[m]]
2453 [GET_MODE (*curr_id->operand_loc[m])]);
2456 /* Prefer matching earlyclobber alternative as
2457 it results in less hard regs required for
2458 the insn than a non-matching earlyclobber
2459 alternative. */
2460 if (TEST_BIT (curr_static_id->operand[m]
2461 .early_clobber_alts, nalt))
2463 if (lra_dump_file != NULL)
2464 fprintf
2465 (lra_dump_file,
2466 " %d Matching earlyclobber alt:"
2467 " reject--\n",
2468 nop);
2469 if (!matching_early_clobber[m])
2471 reject--;
2472 matching_early_clobber[m] = 1;
2475 /* Otherwise we prefer no matching
2476 alternatives because it gives more freedom
2477 in RA. */
2478 else if (operand_reg[nop] == NULL_RTX
2479 || (find_regno_note (curr_insn, REG_DEAD,
2480 REGNO (operand_reg[nop]))
2481 == NULL_RTX))
2483 if (lra_dump_file != NULL)
2484 fprintf
2485 (lra_dump_file,
2486 " %d Matching alt: reject+=2\n",
2487 nop);
2488 reject += 2;
2491 /* If we have to reload this operand and some
2492 previous operand also had to match the same
2493 thing as this operand, we don't know how to do
2494 that. */
2495 if (!match_p || !curr_alt_win[m])
2497 for (i = 0; i < nop; i++)
2498 if (curr_alt_matches[i] == m)
2499 break;
2500 if (i < nop)
2501 break;
2503 else
2504 did_match = true;
2506 this_alternative_matches = m;
2507 /* This can be fixed with reloads if the operand
2508 we are supposed to match can be fixed with
2509 reloads. */
2510 badop = false;
2511 this_alternative = curr_alt[m];
2512 this_alternative_set = curr_alt_set[m];
2513 this_alternative_exclude_start_hard_regs
2514 = curr_alt_exclude_start_hard_regs[m];
2515 winreg = this_alternative != NO_REGS;
2516 break;
2519 case 'g':
2520 if (MEM_P (op)
2521 || general_constant_p (op)
2522 || spilled_pseudo_p (op))
2523 win = true;
2524 cl = GENERAL_REGS;
2525 cl_filter = nullptr;
2526 goto reg;
2528 default:
2529 cn = lookup_constraint (p);
2530 switch (get_constraint_type (cn))
2532 case CT_REGISTER:
2533 cl = reg_class_for_constraint (cn);
2534 if (cl != NO_REGS)
2536 cl_filter = get_register_filter (cn);
2537 goto reg;
2539 break;
2541 case CT_CONST_INT:
2542 if (CONST_INT_P (op)
2543 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2544 win = true;
2545 break;
2547 case CT_MEMORY:
2548 case CT_RELAXED_MEMORY:
2549 if (MEM_P (op)
2550 && satisfies_memory_constraint_p (op, cn))
2551 win = true;
2552 else if (spilled_pseudo_p (op))
2553 win = true;
2555 /* If we didn't already win, we can reload constants
2556 via force_const_mem or put the pseudo value into
2557 memory, or make other memory by reloading the
2558 address like for 'o'. */
2559 if (CONST_POOL_OK_P (mode, op)
2560 || MEM_P (op) || REG_P (op)
2561 /* We can restore the equiv insn by a
2562 reload. */
2563 || equiv_substition_p[nop])
2564 badop = false;
2565 constmemok = true;
2566 offmemok = true;
2567 break;
2569 case CT_ADDRESS:
2570 /* An asm operand with an address constraint
2571 that doesn't satisfy address_operand has
2572 is_address cleared, so that we don't try to
2573 make a non-address fit. */
2574 if (!curr_static_id->operand[nop].is_address)
2575 break;
2576 /* If we didn't already win, we can reload the address
2577 into a base register. */
2578 if (satisfies_address_constraint_p (op, cn))
2579 win = true;
2580 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2581 ADDRESS, SCRATCH);
2582 cl_filter = nullptr;
2583 badop = false;
2584 goto reg;
2586 case CT_FIXED_FORM:
2587 if (constraint_satisfied_p (op, cn))
2588 win = true;
2589 break;
2591 case CT_SPECIAL_MEMORY:
2592 if (satisfies_memory_constraint_p (op, cn))
2593 win = true;
2594 else if (spilled_pseudo_p (op))
2596 curr_reuse_alt_p = false;
2597 win = true;
2599 break;
2601 break;
2603 reg:
2604 if (mode == BLKmode)
2605 break;
2606 this_alternative = reg_class_subunion[this_alternative][cl];
2607 if (hard_reg_set_subset_p (this_alternative_set,
2608 reg_class_contents[cl]))
2609 this_alternative_exclude_start_hard_regs
2610 = ira_exclude_class_mode_regs[cl][mode];
2611 else if (!hard_reg_set_subset_p (reg_class_contents[cl],
2612 this_alternative_set))
2613 this_alternative_exclude_start_hard_regs
2614 |= ira_exclude_class_mode_regs[cl][mode];
2615 this_alternative_set |= reg_class_contents[cl];
2616 if (cl_filter)
2617 this_alternative_exclude_start_hard_regs |= ~*cl_filter;
2618 if (costly_p)
2620 this_costly_alternative
2621 = reg_class_subunion[this_costly_alternative][cl];
2622 this_costly_alternative_set |= reg_class_contents[cl];
2624 winreg = true;
2625 if (REG_P (op))
2627 tree decl;
2628 if (hard_regno[nop] >= 0
2629 && in_hard_reg_set_p (this_alternative_set,
2630 mode, hard_regno[nop])
2631 && (!cl_filter
2632 || TEST_HARD_REG_BIT (*cl_filter,
2633 hard_regno[nop]))
2634 && ((REG_ATTRS (op) && (decl = REG_EXPR (op)) != NULL
2635 && VAR_P (decl) && DECL_HARD_REGISTER (decl))
2636 || !(TEST_HARD_REG_BIT
2637 (this_alternative_exclude_start_hard_regs,
2638 hard_regno[nop]))))
2639 win = true;
2640 else if (hard_regno[nop] < 0)
2642 if (in_class_p (op, this_alternative, NULL))
2643 win = true;
2644 else if (in_class_p (op, this_alternative, NULL, true))
2646 class_change_p = true;
2647 win = true;
2651 break;
2653 if (c != ' ' && c != '\t')
2654 costly_p = c == '*';
2656 while ((p += len), c);
2658 scratch_p = (operand_reg[nop] != NULL_RTX
2659 && ira_former_scratch_p (REGNO (operand_reg[nop])));
2660 /* Record which operands fit this alternative. */
2661 if (win)
2663 this_alternative_win = true;
2664 if (class_change_p)
2666 curr_alt_class_change_p = true;
2667 if (lra_dump_file != NULL)
2668 fprintf (lra_dump_file,
2669 " %d Narrowing class: reject+=3\n",
2670 nop);
2671 reject += 3;
2673 if (operand_reg[nop] != NULL_RTX)
2675 if (hard_regno[nop] >= 0)
2677 if (in_hard_reg_set_p (this_costly_alternative_set,
2678 mode, hard_regno[nop]))
2680 if (lra_dump_file != NULL)
2681 fprintf (lra_dump_file,
2682 " %d Costly set: reject++\n",
2683 nop);
2684 reject++;
2687 else
2689 /* Prefer won reg to spilled pseudo under other
2690 equal conditions for possibe inheritance. */
2691 if (! scratch_p)
2693 if (lra_dump_file != NULL)
2694 fprintf
2695 (lra_dump_file,
2696 " %d Non pseudo reload: reject++\n",
2697 nop);
2698 reject++;
2700 if (in_class_p (operand_reg[nop],
2701 this_costly_alternative, NULL, true))
2703 if (lra_dump_file != NULL)
2704 fprintf
2705 (lra_dump_file,
2706 " %d Non pseudo costly reload:"
2707 " reject++\n",
2708 nop);
2709 reject++;
2712 /* We simulate the behavior of old reload here.
2713 Although scratches need hard registers and it
2714 might result in spilling other pseudos, no reload
2715 insns are generated for the scratches. So it
2716 might cost something but probably less than old
2717 reload pass believes. */
2718 if (scratch_p)
2720 if (lra_dump_file != NULL)
2721 fprintf (lra_dump_file,
2722 " %d Scratch win: reject+=2\n",
2723 nop);
2724 reject += 2;
2728 else if (did_match)
2729 this_alternative_match_win = true;
2730 else
2732 int const_to_mem = 0;
2733 bool no_regs_p;
2735 reject += op_reject;
2736 /* Mark output reload of the stack pointer. */
2737 if (op == stack_pointer_rtx
2738 && curr_static_id->operand[nop].type != OP_IN)
2739 curr_alt_out_sp_reload_p = true;
2741 /* If this alternative asks for a specific reg class, see if there
2742 is at least one allocatable register in that class. */
2743 no_regs_p
2744 = (this_alternative == NO_REGS
2745 || (hard_reg_set_subset_p
2746 (reg_class_contents[this_alternative],
2747 lra_no_alloc_regs)));
2749 /* For asms, verify that the class for this alternative is possible
2750 for the mode that is specified. */
2751 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2753 int i;
2754 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2755 if (targetm.hard_regno_mode_ok (i, mode)
2756 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2757 mode, i))
2758 break;
2759 if (i == FIRST_PSEUDO_REGISTER)
2760 winreg = false;
2763 /* If this operand accepts a register, and if the
2764 register class has at least one allocatable register,
2765 then this operand can be reloaded. */
2766 if (winreg && !no_regs_p)
2767 badop = false;
2769 if (badop)
2771 if (lra_dump_file != NULL)
2772 fprintf (lra_dump_file,
2773 " Bad operand -- refuse\n");
2774 goto fail;
2777 if (this_alternative != NO_REGS)
2779 HARD_REG_SET available_regs
2780 = (reg_class_contents[this_alternative]
2781 & ~((ira_prohibited_class_mode_regs
2782 [this_alternative][mode])
2783 | lra_no_alloc_regs));
2784 if (hard_reg_set_empty_p (available_regs))
2786 /* There are no hard regs holding a value of given
2787 mode. */
2788 if (offmemok)
2790 this_alternative = NO_REGS;
2791 if (lra_dump_file != NULL)
2792 fprintf (lra_dump_file,
2793 " %d Using memory because of"
2794 " a bad mode: reject+=2\n",
2795 nop);
2796 reject += 2;
2798 else
2800 if (lra_dump_file != NULL)
2801 fprintf (lra_dump_file,
2802 " Wrong mode -- refuse\n");
2803 goto fail;
2808 /* If not assigned pseudo has a class which a subset of
2809 required reg class, it is a less costly alternative
2810 as the pseudo still can get a hard reg of necessary
2811 class. */
2812 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2813 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2814 && ira_class_subset_p[this_alternative][cl])
2816 if (lra_dump_file != NULL)
2817 fprintf
2818 (lra_dump_file,
2819 " %d Super set class reg: reject-=3\n", nop);
2820 reject -= 3;
2823 this_alternative_offmemok = offmemok;
2824 if (this_costly_alternative != NO_REGS)
2826 if (lra_dump_file != NULL)
2827 fprintf (lra_dump_file,
2828 " %d Costly loser: reject++\n", nop);
2829 reject++;
2831 /* If the operand is dying, has a matching constraint,
2832 and satisfies constraints of the matched operand
2833 which failed to satisfy the own constraints, most probably
2834 the reload for this operand will be gone. */
2835 if (this_alternative_matches >= 0
2836 && !curr_alt_win[this_alternative_matches]
2837 && REG_P (op)
2838 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2839 && (hard_regno[nop] >= 0
2840 ? in_hard_reg_set_p (this_alternative_set,
2841 mode, hard_regno[nop])
2842 : in_class_p (op, this_alternative, NULL)))
2844 if (lra_dump_file != NULL)
2845 fprintf
2846 (lra_dump_file,
2847 " %d Dying matched operand reload: reject++\n",
2848 nop);
2849 reject++;
2851 else
2853 /* Strict_low_part requires to reload the register
2854 not the sub-register. In this case we should
2855 check that a final reload hard reg can hold the
2856 value mode. */
2857 if (curr_static_id->operand[nop].strict_low
2858 && REG_P (op)
2859 && hard_regno[nop] < 0
2860 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2861 && ira_class_hard_regs_num[this_alternative] > 0
2862 && (!targetm.hard_regno_mode_ok
2863 (ira_class_hard_regs[this_alternative][0],
2864 GET_MODE (*curr_id->operand_loc[nop]))))
2866 if (lra_dump_file != NULL)
2867 fprintf
2868 (lra_dump_file,
2869 " Strict low subreg reload -- refuse\n");
2870 goto fail;
2872 losers++;
2874 if (operand_reg[nop] != NULL_RTX
2875 /* Output operands and matched input operands are
2876 not inherited. The following conditions do not
2877 exactly describe the previous statement but they
2878 are pretty close. */
2879 && curr_static_id->operand[nop].type != OP_OUT
2880 && (this_alternative_matches < 0
2881 || curr_static_id->operand[nop].type != OP_IN))
2883 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2884 (operand_reg[nop])]
2885 .last_reload);
2887 /* The value of reload_sum has sense only if we
2888 process insns in their order. It happens only on
2889 the first constraints sub-pass when we do most of
2890 reload work. */
2891 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2892 reload_sum += last_reload - bb_reload_num;
2894 /* If this is a constant that is reloaded into the
2895 desired class by copying it to memory first, count
2896 that as another reload. This is consistent with
2897 other code and is required to avoid choosing another
2898 alternative when the constant is moved into memory.
2899 Note that the test here is precisely the same as in
2900 the code below that calls force_const_mem. */
2901 if (CONST_POOL_OK_P (mode, op)
2902 && ((targetm.preferred_reload_class
2903 (op, this_alternative) == NO_REGS)
2904 || no_input_reloads_p))
2906 const_to_mem = 1;
2907 if (! no_regs_p)
2908 losers++;
2911 /* Alternative loses if it requires a type of reload not
2912 permitted for this insn. We can always reload
2913 objects with a REG_UNUSED note. */
2914 if ((curr_static_id->operand[nop].type != OP_IN
2915 && no_output_reloads_p
2916 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2917 || (curr_static_id->operand[nop].type != OP_OUT
2918 && no_input_reloads_p && ! const_to_mem)
2919 || (this_alternative_matches >= 0
2920 && (no_input_reloads_p
2921 || (no_output_reloads_p
2922 && (curr_static_id->operand
2923 [this_alternative_matches].type != OP_IN)
2924 && ! find_reg_note (curr_insn, REG_UNUSED,
2925 no_subreg_reg_operand
2926 [this_alternative_matches])))))
2928 if (lra_dump_file != NULL)
2929 fprintf
2930 (lra_dump_file,
2931 " No input/output reload -- refuse\n");
2932 goto fail;
2935 /* Alternative loses if it required class pseudo cannot
2936 hold value of required mode. Such insns can be
2937 described by insn definitions with mode iterators. */
2938 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2939 && ! hard_reg_set_empty_p (this_alternative_set)
2940 /* It is common practice for constraints to use a
2941 class which does not have actually enough regs to
2942 hold the value (e.g. x86 AREG for mode requiring
2943 more one general reg). Therefore we have 2
2944 conditions to check that the reload pseudo cannot
2945 hold the mode value. */
2946 && (!targetm.hard_regno_mode_ok
2947 (ira_class_hard_regs[this_alternative][0],
2948 GET_MODE (*curr_id->operand_loc[nop])))
2949 /* The above condition is not enough as the first
2950 reg in ira_class_hard_regs can be not aligned for
2951 multi-words mode values. */
2952 && (prohibited_class_reg_set_mode_p
2953 (this_alternative, this_alternative_set,
2954 GET_MODE (*curr_id->operand_loc[nop]))))
2956 if (lra_dump_file != NULL)
2957 fprintf (lra_dump_file,
2958 " reload pseudo for op %d "
2959 "cannot hold the mode value -- refuse\n",
2960 nop);
2961 goto fail;
2964 /* Check strong discouragement of reload of non-constant
2965 into class THIS_ALTERNATIVE. */
2966 if (! CONSTANT_P (op) && ! no_regs_p
2967 && (targetm.preferred_reload_class
2968 (op, this_alternative) == NO_REGS
2969 || (curr_static_id->operand[nop].type == OP_OUT
2970 && (targetm.preferred_output_reload_class
2971 (op, this_alternative) == NO_REGS))))
2973 if (offmemok && REG_P (op))
2975 if (lra_dump_file != NULL)
2976 fprintf
2977 (lra_dump_file,
2978 " %d Spill pseudo into memory: reject+=3\n",
2979 nop);
2980 reject += 3;
2982 else
2984 if (lra_dump_file != NULL)
2985 fprintf
2986 (lra_dump_file,
2987 " %d Non-prefered reload: reject+=%d\n",
2988 nop, LRA_MAX_REJECT);
2989 reject += LRA_MAX_REJECT;
2993 if (! (MEM_P (op) && offmemok)
2994 && ! (const_to_mem && constmemok))
2996 /* We prefer to reload pseudos over reloading other
2997 things, since such reloads may be able to be
2998 eliminated later. So bump REJECT in other cases.
2999 Don't do this in the case where we are forcing a
3000 constant into memory and it will then win since
3001 we don't want to have a different alternative
3002 match then. */
3003 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
3005 if (lra_dump_file != NULL)
3006 fprintf
3007 (lra_dump_file,
3008 " %d Non-pseudo reload: reject+=2\n",
3009 nop);
3010 reject += 2;
3013 if (! no_regs_p)
3014 reload_nregs
3015 += ira_reg_class_max_nregs[this_alternative][mode];
3017 if (SMALL_REGISTER_CLASS_P (this_alternative))
3019 if (lra_dump_file != NULL)
3020 fprintf
3021 (lra_dump_file,
3022 " %d Small class reload: reject+=%d\n",
3023 nop, LRA_LOSER_COST_FACTOR / 2);
3024 reject += LRA_LOSER_COST_FACTOR / 2;
3028 /* We are trying to spill pseudo into memory. It is
3029 usually more costly than moving to a hard register
3030 although it might takes the same number of
3031 reloads.
3033 Non-pseudo spill may happen also. Suppose a target allows both
3034 register and memory in the operand constraint alternatives,
3035 then it's typical that an eliminable register has a substition
3036 of "base + offset" which can either be reloaded by a simple
3037 "new_reg <= base + offset" which will match the register
3038 constraint, or a similar reg addition followed by further spill
3039 to and reload from memory which will match the memory
3040 constraint, but this memory spill will be much more costly
3041 usually.
3043 Code below increases the reject for both pseudo and non-pseudo
3044 spill. */
3045 if (no_regs_p
3046 && !(MEM_P (op) && offmemok)
3047 && !(REG_P (op) && hard_regno[nop] < 0))
3049 if (lra_dump_file != NULL)
3050 fprintf
3051 (lra_dump_file,
3052 " %d Spill %spseudo into memory: reject+=3\n",
3053 nop, REG_P (op) ? "" : "Non-");
3054 reject += 3;
3055 if (VECTOR_MODE_P (mode))
3057 /* Spilling vectors into memory is usually more
3058 costly as they contain big values. */
3059 if (lra_dump_file != NULL)
3060 fprintf
3061 (lra_dump_file,
3062 " %d Spill vector pseudo: reject+=2\n",
3063 nop);
3064 reject += 2;
3068 /* When we use an operand requiring memory in given
3069 alternative, the insn should write *and* read the
3070 value to/from memory it is costly in comparison with
3071 an insn alternative which does not use memory
3072 (e.g. register or immediate operand). We exclude
3073 memory operand for such case as we can satisfy the
3074 memory constraints by reloading address. */
3075 if (no_regs_p && offmemok && !MEM_P (op))
3077 if (lra_dump_file != NULL)
3078 fprintf
3079 (lra_dump_file,
3080 " Using memory insn operand %d: reject+=3\n",
3081 nop);
3082 reject += 3;
3085 /* If reload requires moving value through secondary
3086 memory, it will need one more insn at least. */
3087 if (this_alternative != NO_REGS
3088 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
3089 && ((curr_static_id->operand[nop].type != OP_OUT
3090 && targetm.secondary_memory_needed (GET_MODE (op), cl,
3091 this_alternative))
3092 || (curr_static_id->operand[nop].type != OP_IN
3093 && (targetm.secondary_memory_needed
3094 (GET_MODE (op), this_alternative, cl)))))
3095 losers++;
3097 if (MEM_P (op) && offmemok)
3098 addr_losers++;
3099 else
3101 /* Input reloads can be inherited more often than
3102 output reloads can be removed, so penalize output
3103 reloads. */
3104 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
3106 if (lra_dump_file != NULL)
3107 fprintf
3108 (lra_dump_file,
3109 " %d Non input pseudo reload: reject++\n",
3110 nop);
3111 reject++;
3114 if (curr_static_id->operand[nop].type == OP_INOUT)
3116 if (lra_dump_file != NULL)
3117 fprintf
3118 (lra_dump_file,
3119 " %d Input/Output reload: reject+=%d\n",
3120 nop, LRA_LOSER_COST_FACTOR);
3121 reject += LRA_LOSER_COST_FACTOR;
3126 if (early_clobber_p && ! scratch_p)
3128 if (lra_dump_file != NULL)
3129 fprintf (lra_dump_file,
3130 " %d Early clobber: reject++\n", nop);
3131 reject++;
3133 /* ??? We check early clobbers after processing all operands
3134 (see loop below) and there we update the costs more.
3135 Should we update the cost (may be approximately) here
3136 because of early clobber register reloads or it is a rare
3137 or non-important thing to be worth to do it. */
3138 overall = (losers * LRA_LOSER_COST_FACTOR + reject
3139 - (addr_losers == losers ? static_reject : 0));
3140 if ((best_losers == 0 || losers != 0) && best_overall < overall)
3142 if (lra_dump_file != NULL)
3143 fprintf (lra_dump_file,
3144 " overall=%d,losers=%d -- refuse\n",
3145 overall, losers);
3146 goto fail;
3149 if (update_and_check_small_class_inputs (nop, nalt,
3150 this_alternative))
3152 if (lra_dump_file != NULL)
3153 fprintf (lra_dump_file,
3154 " not enough small class regs -- refuse\n");
3155 goto fail;
3157 curr_alt[nop] = this_alternative;
3158 curr_alt_set[nop] = this_alternative_set;
3159 curr_alt_exclude_start_hard_regs[nop]
3160 = this_alternative_exclude_start_hard_regs;
3161 curr_alt_win[nop] = this_alternative_win;
3162 curr_alt_match_win[nop] = this_alternative_match_win;
3163 curr_alt_offmemok[nop] = this_alternative_offmemok;
3164 curr_alt_matches[nop] = this_alternative_matches;
3166 if (this_alternative_matches >= 0
3167 && !did_match && !this_alternative_win)
3168 curr_alt_win[this_alternative_matches] = false;
3170 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
3171 early_clobbered_nops[early_clobbered_regs_num++] = nop;
3174 if (curr_insn_set != NULL_RTX && n_operands == 2
3175 /* Prevent processing non-move insns. */
3176 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
3177 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
3178 && ((! curr_alt_win[0] && ! curr_alt_win[1]
3179 && REG_P (no_subreg_reg_operand[0])
3180 && REG_P (no_subreg_reg_operand[1])
3181 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
3182 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
3183 || (! curr_alt_win[0] && curr_alt_win[1]
3184 && REG_P (no_subreg_reg_operand[1])
3185 /* Check that we reload memory not the memory
3186 address. */
3187 && ! (curr_alt_offmemok[0]
3188 && MEM_P (no_subreg_reg_operand[0]))
3189 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
3190 || (curr_alt_win[0] && ! curr_alt_win[1]
3191 && REG_P (no_subreg_reg_operand[0])
3192 /* Check that we reload memory not the memory
3193 address. */
3194 && ! (curr_alt_offmemok[1]
3195 && MEM_P (no_subreg_reg_operand[1]))
3196 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
3197 && (! CONST_POOL_OK_P (curr_operand_mode[1],
3198 no_subreg_reg_operand[1])
3199 || (targetm.preferred_reload_class
3200 (no_subreg_reg_operand[1],
3201 (enum reg_class) curr_alt[1]) != NO_REGS))
3202 /* If it is a result of recent elimination in move
3203 insn we can transform it into an add still by
3204 using this alternative. */
3205 && GET_CODE (no_subreg_reg_operand[1]) != PLUS
3206 /* Likewise if the source has been replaced with an
3207 equivalent value. This only happens once -- the reload
3208 will use the equivalent value instead of the register it
3209 replaces -- so there should be no danger of cycling. */
3210 && !equiv_substition_p[1])))
3212 /* We have a move insn and a new reload insn will be similar
3213 to the current insn. We should avoid such situation as
3214 it results in LRA cycling. */
3215 if (lra_dump_file != NULL)
3216 fprintf (lra_dump_file,
3217 " Cycle danger: overall += LRA_MAX_REJECT\n");
3218 overall += LRA_MAX_REJECT;
3220 ok_p = true;
3221 curr_alt_dont_inherit_ops_num = 0;
3222 for (nop = 0; nop < early_clobbered_regs_num; nop++)
3224 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
3225 HARD_REG_SET temp_set;
3227 i = early_clobbered_nops[nop];
3228 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
3229 || hard_regno[i] < 0)
3230 continue;
3231 lra_assert (operand_reg[i] != NULL_RTX);
3232 clobbered_hard_regno = hard_regno[i];
3233 CLEAR_HARD_REG_SET (temp_set);
3234 add_to_hard_reg_set (&temp_set, GET_MODE (*curr_id->operand_loc[i]),
3235 clobbered_hard_regno);
3236 first_conflict_j = last_conflict_j = -1;
3237 for (j = 0; j < n_operands; j++)
3238 if (j == i
3239 /* We don't want process insides of match_operator and
3240 match_parallel because otherwise we would process
3241 their operands once again generating a wrong
3242 code. */
3243 || curr_static_id->operand[j].is_operator)
3244 continue;
3245 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
3246 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
3247 continue;
3248 /* If we don't reload j-th operand, check conflicts. */
3249 else if ((curr_alt_win[j] || curr_alt_match_win[j])
3250 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
3252 if (first_conflict_j < 0)
3253 first_conflict_j = j;
3254 last_conflict_j = j;
3255 /* Both the earlyclobber operand and conflicting operand
3256 cannot both be user defined hard registers. */
3257 if (HARD_REGISTER_P (operand_reg[i])
3258 && REG_USERVAR_P (operand_reg[i])
3259 && operand_reg[j] != NULL_RTX
3260 && HARD_REGISTER_P (operand_reg[j])
3261 && REG_USERVAR_P (operand_reg[j]))
3263 /* For asm, let curr_insn_transform diagnose it. */
3264 if (INSN_CODE (curr_insn) < 0)
3265 return false;
3266 fatal_insn ("unable to generate reloads for "
3267 "impossible constraints:", curr_insn);
3270 if (last_conflict_j < 0)
3271 continue;
3273 /* If an earlyclobber operand conflicts with another non-matching
3274 operand (ie, they have been assigned the same hard register),
3275 then it is better to reload the other operand, as there may
3276 exist yet another operand with a matching constraint associated
3277 with the earlyclobber operand. However, if one of the operands
3278 is an explicit use of a hard register, then we must reload the
3279 other non-hard register operand. */
3280 if (HARD_REGISTER_P (operand_reg[i])
3281 || (first_conflict_j == last_conflict_j
3282 && operand_reg[last_conflict_j] != NULL_RTX
3283 && !curr_alt_match_win[last_conflict_j]
3284 && !HARD_REGISTER_P (operand_reg[last_conflict_j])))
3286 curr_alt_win[last_conflict_j] = false;
3287 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
3288 = last_conflict_j;
3289 losers++;
3290 if (lra_dump_file != NULL)
3291 fprintf
3292 (lra_dump_file,
3293 " %d Conflict early clobber reload: reject--\n",
3296 else
3298 /* We need to reload early clobbered register and the
3299 matched registers. */
3300 for (j = 0; j < n_operands; j++)
3301 if (curr_alt_matches[j] == i)
3303 curr_alt_match_win[j] = false;
3304 losers++;
3305 overall += LRA_LOSER_COST_FACTOR;
3307 if (! curr_alt_match_win[i])
3308 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
3309 else
3311 /* Remember pseudos used for match reloads are never
3312 inherited. */
3313 lra_assert (curr_alt_matches[i] >= 0);
3314 curr_alt_win[curr_alt_matches[i]] = false;
3316 curr_alt_win[i] = curr_alt_match_win[i] = false;
3317 losers++;
3318 if (lra_dump_file != NULL)
3319 fprintf
3320 (lra_dump_file,
3321 " %d Matched conflict early clobber reloads: "
3322 "reject--\n",
3325 /* Early clobber was already reflected in REJECT. */
3326 if (!matching_early_clobber[i])
3328 lra_assert (reject > 0);
3329 reject--;
3330 matching_early_clobber[i] = 1;
3332 overall += LRA_LOSER_COST_FACTOR - 1;
3334 if (lra_dump_file != NULL)
3335 fprintf (lra_dump_file, " overall=%d,losers=%d,rld_nregs=%d\n",
3336 overall, losers, reload_nregs);
3338 /* If this alternative can be made to work by reloading, and it
3339 needs less reloading than the others checked so far, record
3340 it as the chosen goal for reloading. */
3341 if ((best_losers != 0 && losers == 0)
3342 || (((best_losers == 0 && losers == 0)
3343 || (best_losers != 0 && losers != 0))
3344 && (best_overall > overall
3345 || (best_overall == overall
3346 /* If the cost of the reloads is the same,
3347 prefer alternative which requires minimal
3348 number of reload regs. */
3349 && (reload_nregs < best_reload_nregs
3350 || (reload_nregs == best_reload_nregs
3351 && (best_reload_sum < reload_sum
3352 || (best_reload_sum == reload_sum
3353 && nalt < goal_alt_number))))))))
3355 for (nop = 0; nop < n_operands; nop++)
3357 goal_alt_win[nop] = curr_alt_win[nop];
3358 goal_alt_match_win[nop] = curr_alt_match_win[nop];
3359 goal_alt_matches[nop] = curr_alt_matches[nop];
3360 goal_alt[nop] = curr_alt[nop];
3361 goal_alt_exclude_start_hard_regs[nop]
3362 = curr_alt_exclude_start_hard_regs[nop];
3363 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
3365 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
3366 goal_reuse_alt_p = curr_reuse_alt_p;
3367 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
3368 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
3369 goal_alt_swapped = curr_swapped;
3370 goal_alt_out_sp_reload_p = curr_alt_out_sp_reload_p;
3371 best_overall = overall;
3372 best_losers = losers;
3373 best_reload_nregs = reload_nregs;
3374 best_reload_sum = reload_sum;
3375 goal_alt_number = nalt;
3377 if (losers == 0 && !curr_alt_class_change_p)
3378 /* Everything is satisfied. Do not process alternatives
3379 anymore. */
3380 break;
3381 fail:
3384 return ok_p;
3387 /* Make reload base reg from address AD. */
3388 static rtx
3389 base_to_reg (struct address_info *ad)
3391 enum reg_class cl;
3392 int code = -1;
3393 rtx new_inner = NULL_RTX;
3394 rtx new_reg = NULL_RTX;
3395 rtx_insn *insn;
3396 rtx_insn *last_insn = get_last_insn();
3398 lra_assert (ad->disp == ad->disp_term);
3399 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
3400 get_index_code (ad));
3401 new_reg = lra_create_new_reg (GET_MODE (*ad->base), NULL_RTX, cl, NULL,
3402 "base");
3403 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
3404 ad->disp_term == NULL
3405 ? const0_rtx
3406 : *ad->disp_term);
3407 if (!valid_address_p (ad->mode, new_inner, ad->as))
3408 return NULL_RTX;
3409 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base));
3410 code = recog_memoized (insn);
3411 if (code < 0)
3413 delete_insns_since (last_insn);
3414 return NULL_RTX;
3417 return new_inner;
3420 /* Make reload base reg + DISP from address AD. Return the new pseudo. */
3421 static rtx
3422 base_plus_disp_to_reg (struct address_info *ad, rtx disp)
3424 enum reg_class cl;
3425 rtx new_reg;
3427 lra_assert (ad->base == ad->base_term);
3428 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
3429 get_index_code (ad));
3430 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX, cl, NULL,
3431 "base + disp");
3432 lra_emit_add (new_reg, *ad->base_term, disp);
3433 return new_reg;
3436 /* Make reload of index part of address AD. Return the new
3437 pseudo. */
3438 static rtx
3439 index_part_to_reg (struct address_info *ad, enum reg_class index_class)
3441 rtx new_reg;
3443 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
3444 index_class, NULL, "index term");
3445 expand_mult (GET_MODE (*ad->index), *ad->index_term,
3446 GEN_INT (get_index_scale (ad)), new_reg, 1);
3447 return new_reg;
3450 /* Return true if we can add a displacement to address AD, even if that
3451 makes the address invalid. The fix-up code requires any new address
3452 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
3453 static bool
3454 can_add_disp_p (struct address_info *ad)
3456 return (!ad->autoinc_p
3457 && ad->segment == NULL
3458 && ad->base == ad->base_term
3459 && ad->disp == ad->disp_term);
3462 /* Make equiv substitution in address AD. Return true if a substitution
3463 was made. */
3464 static bool
3465 equiv_address_substitution (struct address_info *ad)
3467 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
3468 poly_int64 disp;
3469 HOST_WIDE_INT scale;
3470 bool change_p;
3472 base_term = strip_subreg (ad->base_term);
3473 if (base_term == NULL)
3474 base_reg = new_base_reg = NULL_RTX;
3475 else
3477 base_reg = *base_term;
3478 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
3480 index_term = strip_subreg (ad->index_term);
3481 if (index_term == NULL)
3482 index_reg = new_index_reg = NULL_RTX;
3483 else
3485 index_reg = *index_term;
3486 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
3488 if (base_reg == new_base_reg && index_reg == new_index_reg)
3489 return false;
3490 disp = 0;
3491 change_p = false;
3492 if (lra_dump_file != NULL)
3494 fprintf (lra_dump_file, "Changing address in insn %d ",
3495 INSN_UID (curr_insn));
3496 dump_value_slim (lra_dump_file, *ad->outer, 1);
3498 if (base_reg != new_base_reg)
3500 poly_int64 offset;
3501 if (REG_P (new_base_reg))
3503 *base_term = new_base_reg;
3504 change_p = true;
3506 else if (GET_CODE (new_base_reg) == PLUS
3507 && REG_P (XEXP (new_base_reg, 0))
3508 && poly_int_rtx_p (XEXP (new_base_reg, 1), &offset)
3509 && can_add_disp_p (ad))
3511 disp += offset;
3512 *base_term = XEXP (new_base_reg, 0);
3513 change_p = true;
3515 if (ad->base_term2 != NULL)
3516 *ad->base_term2 = *ad->base_term;
3518 if (index_reg != new_index_reg)
3520 poly_int64 offset;
3521 if (REG_P (new_index_reg))
3523 *index_term = new_index_reg;
3524 change_p = true;
3526 else if (GET_CODE (new_index_reg) == PLUS
3527 && REG_P (XEXP (new_index_reg, 0))
3528 && poly_int_rtx_p (XEXP (new_index_reg, 1), &offset)
3529 && can_add_disp_p (ad)
3530 && (scale = get_index_scale (ad)))
3532 disp += offset * scale;
3533 *index_term = XEXP (new_index_reg, 0);
3534 change_p = true;
3537 if (maybe_ne (disp, 0))
3539 if (ad->disp != NULL)
3540 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
3541 else
3543 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
3544 update_address (ad);
3546 change_p = true;
3548 if (lra_dump_file != NULL)
3550 if (! change_p)
3551 fprintf (lra_dump_file, " -- no change\n");
3552 else
3554 fprintf (lra_dump_file, " on equiv ");
3555 dump_value_slim (lra_dump_file, *ad->outer, 1);
3556 fprintf (lra_dump_file, "\n");
3559 return change_p;
3562 /* Skip all modifiers and whitespaces in constraint STR and return the
3563 result. */
3564 static const char *
3565 skip_constraint_modifiers (const char *str)
3567 for (;;str++)
3568 switch (*str)
3570 case '+': case '&' : case '=': case '*': case ' ': case '\t':
3571 case '$': case '^' : case '%': case '?': case '!':
3572 break;
3573 default: return str;
3577 /* Takes a string of 0 or more comma-separated constraints. When more
3578 than one constraint is present, evaluate whether they all correspond
3579 to a single, repeated constraint (e.g. "r,r") or whether we have
3580 more than one distinct constraints (e.g. "r,m"). */
3581 static bool
3582 constraint_unique (const char *cstr)
3584 enum constraint_num ca, cb;
3585 ca = CONSTRAINT__UNKNOWN;
3586 for (;;)
3588 cstr = skip_constraint_modifiers (cstr);
3589 if (*cstr == '\0' || *cstr == ',')
3590 cb = CONSTRAINT_X;
3591 else
3593 cb = lookup_constraint (cstr);
3594 if (cb == CONSTRAINT__UNKNOWN)
3595 return false;
3596 cstr += CONSTRAINT_LEN (cstr[0], cstr);
3598 /* Handle the first iteration of the loop. */
3599 if (ca == CONSTRAINT__UNKNOWN)
3600 ca = cb;
3601 /* Handle the general case of comparing ca with subsequent
3602 constraints. */
3603 else if (ca != cb)
3604 return false;
3605 if (*cstr == '\0')
3606 return true;
3607 if (*cstr == ',')
3608 cstr += 1;
3612 /* Major function to make reloads for an address in operand NOP or
3613 check its correctness (If CHECK_ONLY_P is true). The supported
3614 cases are:
3616 1) an address that existed before LRA started, at which point it
3617 must have been valid. These addresses are subject to elimination
3618 and may have become invalid due to the elimination offset being out
3619 of range.
3621 2) an address created by forcing a constant to memory
3622 (force_const_to_mem). The initial form of these addresses might
3623 not be valid, and it is this function's job to make them valid.
3625 3) a frame address formed from a register and a (possibly zero)
3626 constant offset. As above, these addresses might not be valid and
3627 this function must make them so.
3629 Add reloads to the lists *BEFORE and *AFTER. We might need to add
3630 reloads to *AFTER because of inc/dec, {pre, post} modify in the
3631 address. Return true for any RTL change.
3633 The function is a helper function which does not produce all
3634 transformations (when CHECK_ONLY_P is false) which can be
3635 necessary. It does just basic steps. To do all necessary
3636 transformations use function process_address. */
3637 static bool
3638 process_address_1 (int nop, bool check_only_p,
3639 rtx_insn **before, rtx_insn **after)
3641 struct address_info ad;
3642 rtx new_reg;
3643 HOST_WIDE_INT scale;
3644 rtx op = *curr_id->operand_loc[nop];
3645 rtx mem = extract_mem_from_operand (op);
3646 const char *constraint;
3647 enum constraint_num cn;
3648 bool change_p = false;
3650 if (MEM_P (mem)
3651 && GET_MODE (mem) == BLKmode
3652 && GET_CODE (XEXP (mem, 0)) == SCRATCH)
3653 return false;
3655 constraint
3656 = skip_constraint_modifiers (curr_static_id->operand[nop].constraint);
3657 if (IN_RANGE (constraint[0], '0', '9'))
3659 char *end;
3660 unsigned long dup = strtoul (constraint, &end, 10);
3661 constraint
3662 = skip_constraint_modifiers (curr_static_id->operand[dup].constraint);
3664 cn = lookup_constraint (*constraint == '\0' ? "X" : constraint);
3665 /* If we have several alternatives or/and several constraints in an
3666 alternative and we can not say at this stage what constraint will be used,
3667 use unknown constraint. The exception is an address constraint. If
3668 operand has one address constraint, probably all others constraints are
3669 address ones. */
3670 if (constraint[0] != '\0' && get_constraint_type (cn) != CT_ADDRESS
3671 && !constraint_unique (constraint))
3672 cn = CONSTRAINT__UNKNOWN;
3673 if (insn_extra_address_constraint (cn)
3674 /* When we find an asm operand with an address constraint that
3675 doesn't satisfy address_operand to begin with, we clear
3676 is_address, so that we don't try to make a non-address fit.
3677 If the asm statement got this far, it's because other
3678 constraints are available, and we'll use them, disregarding
3679 the unsatisfiable address ones. */
3680 && curr_static_id->operand[nop].is_address)
3681 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
3682 /* Do not attempt to decompose arbitrary addresses generated by combine
3683 for asm operands with loose constraints, e.g 'X'.
3684 Need to extract memory from op for special memory constraint,
3685 i.e. bcst_mem_operand in i386 backend. */
3686 else if (MEM_P (mem)
3687 && !(INSN_CODE (curr_insn) < 0
3688 && get_constraint_type (cn) == CT_FIXED_FORM
3689 && constraint_satisfied_p (op, cn)))
3690 decompose_mem_address (&ad, mem);
3691 else if (GET_CODE (op) == SUBREG
3692 && MEM_P (SUBREG_REG (op)))
3693 decompose_mem_address (&ad, SUBREG_REG (op));
3694 else
3695 return false;
3696 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
3697 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
3698 when INDEX_REG_CLASS is a single register class. */
3699 enum reg_class index_cl = index_reg_class (curr_insn);
3700 if (ad.base_term != NULL
3701 && ad.index_term != NULL
3702 && ira_class_hard_regs_num[index_cl] == 1
3703 && REG_P (*ad.base_term)
3704 && REG_P (*ad.index_term)
3705 && in_class_p (*ad.base_term, index_cl, NULL)
3706 && ! in_class_p (*ad.index_term, index_cl, NULL))
3708 std::swap (ad.base, ad.index);
3709 std::swap (ad.base_term, ad.index_term);
3711 if (! check_only_p)
3712 change_p = equiv_address_substitution (&ad);
3713 if (ad.base_term != NULL
3714 && (process_addr_reg
3715 (ad.base_term, check_only_p, before,
3716 (ad.autoinc_p
3717 && !(REG_P (*ad.base_term)
3718 && find_regno_note (curr_insn, REG_DEAD,
3719 REGNO (*ad.base_term)) != NULL_RTX)
3720 ? after : NULL),
3721 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3722 get_index_code (&ad), curr_insn))))
3724 change_p = true;
3725 if (ad.base_term2 != NULL)
3726 *ad.base_term2 = *ad.base_term;
3728 if (ad.index_term != NULL
3729 && process_addr_reg (ad.index_term, check_only_p,
3730 before, NULL, index_cl))
3731 change_p = true;
3733 /* Target hooks sometimes don't treat extra-constraint addresses as
3734 legitimate address_operands, so handle them specially. */
3735 if (insn_extra_address_constraint (cn)
3736 && satisfies_address_constraint_p (&ad, cn))
3737 return change_p;
3739 if (check_only_p)
3740 return change_p;
3742 /* There are three cases where the shape of *AD.INNER may now be invalid:
3744 1) the original address was valid, but either elimination or
3745 equiv_address_substitution was applied and that made
3746 the address invalid.
3748 2) the address is an invalid symbolic address created by
3749 force_const_to_mem.
3751 3) the address is a frame address with an invalid offset.
3753 4) the address is a frame address with an invalid base.
3755 All these cases involve a non-autoinc address, so there is no
3756 point revalidating other types. */
3757 if (ad.autoinc_p || valid_address_p (op, &ad, cn))
3758 return change_p;
3760 /* Any index existed before LRA started, so we can assume that the
3761 presence and shape of the index is valid. */
3762 push_to_sequence (*before);
3763 lra_assert (ad.disp == ad.disp_term);
3764 if (ad.base == NULL)
3766 if (ad.index == NULL)
3768 rtx_insn *insn;
3769 rtx_insn *last = get_last_insn ();
3770 int code = -1;
3771 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3772 SCRATCH, SCRATCH,
3773 curr_insn);
3774 rtx addr = *ad.inner;
3776 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, NULL, "addr");
3777 if (HAVE_lo_sum)
3779 /* addr => lo_sum (new_base, addr), case (2) above. */
3780 insn = emit_insn (gen_rtx_SET
3781 (new_reg,
3782 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
3783 code = recog_memoized (insn);
3784 if (code >= 0)
3786 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
3787 if (!valid_address_p (op, &ad, cn))
3789 /* Try to put lo_sum into register. */
3790 insn = emit_insn (gen_rtx_SET
3791 (new_reg,
3792 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
3793 code = recog_memoized (insn);
3794 if (code >= 0)
3796 *ad.inner = new_reg;
3797 if (!valid_address_p (op, &ad, cn))
3799 *ad.inner = addr;
3800 code = -1;
3806 if (code < 0)
3807 delete_insns_since (last);
3810 if (code < 0)
3812 /* addr => new_base, case (2) above. */
3813 lra_emit_move (new_reg, addr);
3815 for (insn = last == NULL_RTX ? get_insns () : NEXT_INSN (last);
3816 insn != NULL_RTX;
3817 insn = NEXT_INSN (insn))
3818 if (recog_memoized (insn) < 0)
3819 break;
3820 if (insn != NULL_RTX)
3822 /* Do nothing if we cannot generate right insns.
3823 This is analogous to reload pass behavior. */
3824 delete_insns_since (last);
3825 end_sequence ();
3826 return false;
3828 *ad.inner = new_reg;
3831 else
3833 /* index * scale + disp => new base + index * scale,
3834 case (1) above. */
3835 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3836 GET_CODE (*ad.index),
3837 curr_insn);
3839 lra_assert (index_cl != NO_REGS);
3840 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, NULL, "disp");
3841 lra_emit_move (new_reg, *ad.disp);
3842 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3843 new_reg, *ad.index);
3846 else if (ad.index == NULL)
3848 int regno;
3849 enum reg_class cl;
3850 rtx set;
3851 rtx_insn *insns, *last_insn;
3852 /* Try to reload base into register only if the base is invalid
3853 for the address but with valid offset, case (4) above. */
3854 start_sequence ();
3855 new_reg = base_to_reg (&ad);
3857 /* base + disp => new base, cases (1) and (3) above. */
3858 /* Another option would be to reload the displacement into an
3859 index register. However, postreload has code to optimize
3860 address reloads that have the same base and different
3861 displacements, so reloading into an index register would
3862 not necessarily be a win. */
3863 if (new_reg == NULL_RTX)
3865 /* See if the target can split the displacement into a
3866 legitimate new displacement from a local anchor. */
3867 gcc_assert (ad.disp == ad.disp_term);
3868 poly_int64 orig_offset;
3869 rtx offset1, offset2;
3870 if (poly_int_rtx_p (*ad.disp, &orig_offset)
3871 && targetm.legitimize_address_displacement (&offset1, &offset2,
3872 orig_offset,
3873 ad.mode))
3875 new_reg = base_plus_disp_to_reg (&ad, offset1);
3876 new_reg = gen_rtx_PLUS (GET_MODE (new_reg), new_reg, offset2);
3878 else
3879 new_reg = base_plus_disp_to_reg (&ad, *ad.disp);
3881 insns = get_insns ();
3882 last_insn = get_last_insn ();
3883 /* If we generated at least two insns, try last insn source as
3884 an address. If we succeed, we generate one less insn. */
3885 if (REG_P (new_reg)
3886 && last_insn != insns
3887 && (set = single_set (last_insn)) != NULL_RTX
3888 && GET_CODE (SET_SRC (set)) == PLUS
3889 && REG_P (XEXP (SET_SRC (set), 0))
3890 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3892 *ad.inner = SET_SRC (set);
3893 if (valid_address_p (op, &ad, cn))
3895 *ad.base_term = XEXP (SET_SRC (set), 0);
3896 *ad.disp_term = XEXP (SET_SRC (set), 1);
3897 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3898 get_index_code (&ad), curr_insn);
3899 regno = REGNO (*ad.base_term);
3900 if (regno >= FIRST_PSEUDO_REGISTER
3901 && cl != lra_get_allocno_class (regno))
3902 lra_change_class (regno, cl, " Change to", true);
3903 new_reg = SET_SRC (set);
3904 delete_insns_since (PREV_INSN (last_insn));
3907 end_sequence ();
3908 emit_insn (insns);
3909 *ad.inner = new_reg;
3911 else if (ad.disp_term != NULL)
3913 /* base + scale * index + disp => new base + scale * index,
3914 case (1) above. */
3915 gcc_assert (ad.disp == ad.disp_term);
3916 new_reg = base_plus_disp_to_reg (&ad, *ad.disp);
3917 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3918 new_reg, *ad.index);
3920 else if ((scale = get_index_scale (&ad)) == 1)
3922 /* The last transformation to one reg will be made in
3923 curr_insn_transform function. */
3924 end_sequence ();
3925 return false;
3927 else if (scale != 0)
3929 /* base + scale * index => base + new_reg,
3930 case (1) above.
3931 Index part of address may become invalid. For example, we
3932 changed pseudo on the equivalent memory and a subreg of the
3933 pseudo onto the memory of different mode for which the scale is
3934 prohibitted. */
3935 new_reg = index_part_to_reg (&ad, index_cl);
3936 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3937 *ad.base_term, new_reg);
3939 else
3941 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3942 SCRATCH, SCRATCH,
3943 curr_insn);
3944 rtx addr = *ad.inner;
3946 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, NULL, "addr");
3947 /* addr => new_base. */
3948 lra_emit_move (new_reg, addr);
3949 *ad.inner = new_reg;
3951 *before = get_insns ();
3952 end_sequence ();
3953 return true;
3956 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3957 Use process_address_1 as a helper function. Return true for any
3958 RTL changes.
3960 If CHECK_ONLY_P is true, just check address correctness. Return
3961 false if the address correct. */
3962 static bool
3963 process_address (int nop, bool check_only_p,
3964 rtx_insn **before, rtx_insn **after)
3966 bool res = false;
3968 while (process_address_1 (nop, check_only_p, before, after))
3970 if (check_only_p)
3971 return true;
3972 res = true;
3974 return res;
3977 /* Override the generic address_reload_context in order to
3978 control the creation of reload pseudos. */
3979 class lra_autoinc_reload_context : public address_reload_context
3981 machine_mode mode;
3982 enum reg_class rclass;
3984 public:
3985 lra_autoinc_reload_context (machine_mode mode, enum reg_class new_rclass)
3986 : mode (mode), rclass (new_rclass) {}
3988 rtx get_reload_reg () const override final
3990 return lra_create_new_reg (mode, NULL_RTX, rclass, NULL, "INC/DEC result");
3994 /* Emit insns to reload VALUE into a new register. VALUE is an
3995 auto-increment or auto-decrement RTX whose operand is a register or
3996 memory location; so reloading involves incrementing that location.
3998 INC_AMOUNT is the number to increment or decrement by (always
3999 positive and ignored for POST_MODIFY/PRE_MODIFY).
4001 Return a pseudo containing the result. */
4002 static rtx
4003 emit_inc (enum reg_class new_rclass, rtx value, poly_int64 inc_amount)
4005 lra_autoinc_reload_context context (GET_MODE (value), new_rclass);
4006 return context.emit_autoinc (value, inc_amount);
4009 /* Return true if the current move insn does not need processing as we
4010 already know that it satisfies its constraints. */
4011 static bool
4012 simple_move_p (void)
4014 rtx dest, src;
4015 enum reg_class dclass, sclass;
4017 lra_assert (curr_insn_set != NULL_RTX);
4018 dest = SET_DEST (curr_insn_set);
4019 src = SET_SRC (curr_insn_set);
4021 /* If the instruction has multiple sets we need to process it even if it
4022 is single_set. This can happen if one or more of the SETs are dead.
4023 See PR73650. */
4024 if (multiple_sets (curr_insn))
4025 return false;
4027 return ((dclass = get_op_class (dest)) != NO_REGS
4028 && (sclass = get_op_class (src)) != NO_REGS
4029 /* The backend guarantees that register moves of cost 2
4030 never need reloads. */
4031 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
4034 /* Swap operands NOP and NOP + 1. */
4035 static inline void
4036 swap_operands (int nop)
4038 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
4039 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
4040 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
4041 std::swap (equiv_substition_p[nop], equiv_substition_p[nop + 1]);
4042 /* Swap the duplicates too. */
4043 lra_update_dup (curr_id, nop);
4044 lra_update_dup (curr_id, nop + 1);
4047 /* Main entry point of the constraint code: search the body of the
4048 current insn to choose the best alternative. It is mimicking insn
4049 alternative cost calculation model of former reload pass. That is
4050 because machine descriptions were written to use this model. This
4051 model can be changed in future. Make commutative operand exchange
4052 if it is chosen.
4054 if CHECK_ONLY_P is false, do RTL changes to satisfy the
4055 constraints. Return true if any change happened during function
4056 call.
4058 If CHECK_ONLY_P is true then don't do any transformation. Just
4059 check that the insn satisfies all constraints. If the insn does
4060 not satisfy any constraint, return true. */
4061 static bool
4062 curr_insn_transform (bool check_only_p)
4064 int i, j, k;
4065 int n_operands;
4066 int n_alternatives;
4067 int n_outputs;
4068 int commutative;
4069 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
4070 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
4071 signed char outputs[MAX_RECOG_OPERANDS + 1];
4072 rtx_insn *before, *after;
4073 bool alt_p = false;
4074 /* Flag that the insn has been changed through a transformation. */
4075 bool change_p;
4076 bool sec_mem_p;
4077 bool use_sec_mem_p;
4078 int max_regno_before;
4079 int reused_alternative_num;
4081 curr_insn_set = single_set (curr_insn);
4082 if (curr_insn_set != NULL_RTX && simple_move_p ())
4084 /* We assume that the corresponding insn alternative has no
4085 earlier clobbers. If it is not the case, don't define move
4086 cost equal to 2 for the corresponding register classes. */
4087 lra_set_used_insn_alternative (curr_insn, LRA_NON_CLOBBERED_ALT);
4088 return false;
4091 no_input_reloads_p = no_output_reloads_p = false;
4092 goal_alt_number = -1;
4093 change_p = sec_mem_p = false;
4095 /* CALL_INSNs are not allowed to have any output reloads. */
4096 if (CALL_P (curr_insn))
4097 no_output_reloads_p = true;
4099 n_operands = curr_static_id->n_operands;
4100 n_alternatives = curr_static_id->n_alternatives;
4102 /* Just return "no reloads" if insn has no operands with
4103 constraints. */
4104 if (n_operands == 0 || n_alternatives == 0)
4105 return false;
4107 max_regno_before = max_reg_num ();
4109 for (i = 0; i < n_operands; i++)
4111 goal_alt_matched[i][0] = -1;
4112 goal_alt_matches[i] = -1;
4115 commutative = curr_static_id->commutative;
4117 /* Now see what we need for pseudos that didn't get hard regs or got
4118 the wrong kind of hard reg. For this, we must consider all the
4119 operands together against the register constraints. */
4121 best_losers = best_overall = INT_MAX;
4122 best_reload_sum = 0;
4124 curr_swapped = false;
4125 goal_alt_swapped = false;
4127 if (! check_only_p)
4128 /* Make equivalence substitution and memory subreg elimination
4129 before address processing because an address legitimacy can
4130 depend on memory mode. */
4131 for (i = 0; i < n_operands; i++)
4133 rtx op, subst, old;
4134 bool op_change_p = false;
4136 if (curr_static_id->operand[i].is_operator)
4137 continue;
4139 old = op = *curr_id->operand_loc[i];
4140 if (GET_CODE (old) == SUBREG)
4141 old = SUBREG_REG (old);
4142 subst = get_equiv_with_elimination (old, curr_insn);
4143 original_subreg_reg_mode[i] = VOIDmode;
4144 equiv_substition_p[i] = false;
4145 if (subst != old)
4147 equiv_substition_p[i] = true;
4148 subst = copy_rtx (subst);
4149 lra_assert (REG_P (old));
4150 if (GET_CODE (op) != SUBREG)
4151 *curr_id->operand_loc[i] = subst;
4152 else
4154 SUBREG_REG (op) = subst;
4155 if (GET_MODE (subst) == VOIDmode)
4156 original_subreg_reg_mode[i] = GET_MODE (old);
4158 if (lra_dump_file != NULL)
4160 fprintf (lra_dump_file,
4161 "Changing pseudo %d in operand %i of insn %u on equiv ",
4162 REGNO (old), i, INSN_UID (curr_insn));
4163 dump_value_slim (lra_dump_file, subst, 1);
4164 fprintf (lra_dump_file, "\n");
4166 op_change_p = change_p = true;
4168 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
4170 change_p = true;
4171 lra_update_dup (curr_id, i);
4175 /* Reload address registers and displacements. We do it before
4176 finding an alternative because of memory constraints. */
4177 before = after = NULL;
4178 for (i = 0; i < n_operands; i++)
4179 if (! curr_static_id->operand[i].is_operator
4180 && process_address (i, check_only_p, &before, &after))
4182 if (check_only_p)
4183 return true;
4184 change_p = true;
4185 lra_update_dup (curr_id, i);
4188 if (change_p)
4189 /* If we've changed the instruction then any alternative that
4190 we chose previously may no longer be valid. */
4191 lra_set_used_insn_alternative (curr_insn, LRA_UNKNOWN_ALT);
4193 if (! check_only_p && curr_insn_set != NULL_RTX
4194 && check_and_process_move (&change_p, &sec_mem_p))
4195 return change_p;
4197 try_swapped:
4199 reused_alternative_num = check_only_p ? LRA_UNKNOWN_ALT : curr_id->used_insn_alternative;
4200 if (lra_dump_file != NULL && reused_alternative_num >= 0)
4201 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
4202 reused_alternative_num, INSN_UID (curr_insn));
4204 if (process_alt_operands (reused_alternative_num))
4205 alt_p = true;
4207 if (check_only_p)
4208 return ! alt_p || best_losers != 0;
4210 /* If insn is commutative (it's safe to exchange a certain pair of
4211 operands) then we need to try each alternative twice, the second
4212 time matching those two operands as if we had exchanged them. To
4213 do this, really exchange them in operands.
4215 If we have just tried the alternatives the second time, return
4216 operands to normal and drop through. */
4218 if (reused_alternative_num < 0 && commutative >= 0)
4220 curr_swapped = !curr_swapped;
4221 if (curr_swapped)
4223 swap_operands (commutative);
4224 goto try_swapped;
4226 else
4227 swap_operands (commutative);
4230 if (! alt_p && ! sec_mem_p)
4232 /* No alternative works with reloads?? */
4233 if (INSN_CODE (curr_insn) >= 0)
4234 fatal_insn ("unable to generate reloads for:", curr_insn);
4235 error_for_asm (curr_insn,
4236 "inconsistent operand constraints in an %<asm%>");
4237 lra_asm_error_p = true;
4238 if (! JUMP_P (curr_insn))
4240 /* Avoid further trouble with this insn. Don't generate use
4241 pattern here as we could use the insn SP offset. */
4242 lra_set_insn_deleted (curr_insn);
4244 else
4246 lra_invalidate_insn_data (curr_insn);
4247 ira_nullify_asm_goto (curr_insn);
4248 lra_update_insn_regno_info (curr_insn);
4250 return true;
4253 /* If the best alternative is with operands 1 and 2 swapped, swap
4254 them. Update the operand numbers of any reloads already
4255 pushed. */
4257 if (goal_alt_swapped)
4259 if (lra_dump_file != NULL)
4260 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
4261 INSN_UID (curr_insn));
4263 /* Swap the duplicates too. */
4264 swap_operands (commutative);
4265 change_p = true;
4268 /* Some targets' TARGET_SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
4269 too conservatively. So we use the secondary memory only if there
4270 is no any alternative without reloads. */
4271 use_sec_mem_p = false;
4272 if (! alt_p)
4273 use_sec_mem_p = true;
4274 else if (sec_mem_p)
4276 for (i = 0; i < n_operands; i++)
4277 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
4278 break;
4279 use_sec_mem_p = i < n_operands;
4282 if (use_sec_mem_p)
4284 int in = -1, out = -1;
4285 rtx new_reg, src, dest, rld;
4286 machine_mode sec_mode, rld_mode;
4288 lra_assert (curr_insn_set != NULL_RTX && sec_mem_p);
4289 dest = SET_DEST (curr_insn_set);
4290 src = SET_SRC (curr_insn_set);
4291 for (i = 0; i < n_operands; i++)
4292 if (*curr_id->operand_loc[i] == dest)
4293 out = i;
4294 else if (*curr_id->operand_loc[i] == src)
4295 in = i;
4296 for (i = 0; i < curr_static_id->n_dups; i++)
4297 if (out < 0 && *curr_id->dup_loc[i] == dest)
4298 out = curr_static_id->dup_num[i];
4299 else if (in < 0 && *curr_id->dup_loc[i] == src)
4300 in = curr_static_id->dup_num[i];
4301 lra_assert (out >= 0 && in >= 0
4302 && curr_static_id->operand[out].type == OP_OUT
4303 && curr_static_id->operand[in].type == OP_IN);
4304 rld = partial_subreg_p (GET_MODE (src), GET_MODE (dest)) ? src : dest;
4305 rld_mode = GET_MODE (rld);
4306 sec_mode = targetm.secondary_memory_needed_mode (rld_mode);
4307 new_reg = lra_create_new_reg (sec_mode, NULL_RTX, NO_REGS, NULL,
4308 "secondary");
4309 /* If the mode is changed, it should be wider. */
4310 lra_assert (!partial_subreg_p (sec_mode, rld_mode));
4311 if (sec_mode != rld_mode)
4313 /* If the target says specifically to use another mode for
4314 secondary memory moves we cannot reuse the original
4315 insn. */
4316 after = emit_spill_move (false, new_reg, dest);
4317 lra_process_new_insns (curr_insn, NULL, after,
4318 "Inserting the sec. move");
4319 /* We may have non null BEFORE here (e.g. after address
4320 processing. */
4321 push_to_sequence (before);
4322 before = emit_spill_move (true, new_reg, src);
4323 emit_insn (before);
4324 before = get_insns ();
4325 end_sequence ();
4326 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
4327 lra_set_insn_deleted (curr_insn);
4329 else if (dest == rld)
4331 *curr_id->operand_loc[out] = new_reg;
4332 lra_update_dup (curr_id, out);
4333 after = emit_spill_move (false, new_reg, dest);
4334 lra_process_new_insns (curr_insn, NULL, after,
4335 "Inserting the sec. move");
4337 else
4339 *curr_id->operand_loc[in] = new_reg;
4340 lra_update_dup (curr_id, in);
4341 /* See comments above. */
4342 push_to_sequence (before);
4343 before = emit_spill_move (true, new_reg, src);
4344 emit_insn (before);
4345 before = get_insns ();
4346 end_sequence ();
4347 lra_process_new_insns (curr_insn, before, NULL,
4348 "Inserting the sec. move");
4350 lra_update_insn_regno_info (curr_insn);
4351 return true;
4354 lra_assert (goal_alt_number >= 0);
4355 lra_set_used_insn_alternative (curr_insn, goal_reuse_alt_p
4356 ? goal_alt_number : LRA_UNKNOWN_ALT);
4358 if (lra_dump_file != NULL)
4360 const char *p;
4362 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
4363 goal_alt_number, INSN_UID (curr_insn));
4364 print_curr_insn_alt (goal_alt_number);
4365 if (INSN_CODE (curr_insn) >= 0
4366 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
4367 fprintf (lra_dump_file, " {%s}", p);
4368 if (maybe_ne (curr_id->sp_offset, 0))
4370 fprintf (lra_dump_file, " (sp_off=");
4371 print_dec (curr_id->sp_offset, lra_dump_file);
4372 fprintf (lra_dump_file, ")");
4374 fprintf (lra_dump_file, "\n");
4377 /* Right now, for any pair of operands I and J that are required to
4378 match, with J < I, goal_alt_matches[I] is J. Add I to
4379 goal_alt_matched[J]. */
4381 for (i = 0; i < n_operands; i++)
4382 if ((j = goal_alt_matches[i]) >= 0)
4384 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
4386 /* We allow matching one output operand and several input
4387 operands. */
4388 lra_assert (k == 0
4389 || (curr_static_id->operand[j].type == OP_OUT
4390 && curr_static_id->operand[i].type == OP_IN
4391 && (curr_static_id->operand
4392 [goal_alt_matched[j][0]].type == OP_IN)));
4393 goal_alt_matched[j][k] = i;
4394 goal_alt_matched[j][k + 1] = -1;
4397 for (i = 0; i < n_operands; i++)
4398 goal_alt_win[i] |= goal_alt_match_win[i];
4400 /* Any constants that aren't allowed and can't be reloaded into
4401 registers are here changed into memory references. */
4402 for (i = 0; i < n_operands; i++)
4403 if (goal_alt_win[i])
4405 int regno;
4406 enum reg_class new_class;
4407 rtx reg = *curr_id->operand_loc[i];
4409 if (GET_CODE (reg) == SUBREG)
4410 reg = SUBREG_REG (reg);
4412 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
4414 bool ok_p = in_class_p (reg, goal_alt[i], &new_class, true);
4416 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
4418 lra_assert (ok_p);
4419 lra_change_class (regno, new_class, " Change to", true);
4423 else
4425 const char *constraint;
4426 char c;
4427 rtx op = *curr_id->operand_loc[i];
4428 rtx subreg = NULL_RTX;
4429 machine_mode mode = curr_operand_mode[i];
4431 if (GET_CODE (op) == SUBREG)
4433 subreg = op;
4434 op = SUBREG_REG (op);
4435 mode = GET_MODE (op);
4438 if (CONST_POOL_OK_P (mode, op)
4439 && ((targetm.preferred_reload_class
4440 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
4441 || no_input_reloads_p))
4443 rtx tem = force_const_mem (mode, op);
4445 change_p = true;
4446 if (subreg != NULL_RTX)
4447 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
4449 *curr_id->operand_loc[i] = tem;
4450 lra_update_dup (curr_id, i);
4451 process_address (i, false, &before, &after);
4453 /* If the alternative accepts constant pool refs directly
4454 there will be no reload needed at all. */
4455 if (subreg != NULL_RTX)
4456 continue;
4457 /* Skip alternatives before the one requested. */
4458 constraint = (curr_static_id->operand_alternative
4459 [goal_alt_number * n_operands + i].constraint);
4460 for (;
4461 (c = *constraint) && c != ',' && c != '#';
4462 constraint += CONSTRAINT_LEN (c, constraint))
4464 enum constraint_num cn = lookup_constraint (constraint);
4465 if ((insn_extra_memory_constraint (cn)
4466 || insn_extra_special_memory_constraint (cn)
4467 || insn_extra_relaxed_memory_constraint (cn))
4468 && satisfies_memory_constraint_p (tem, cn))
4469 break;
4471 if (c == '\0' || c == ',' || c == '#')
4472 continue;
4474 goal_alt_win[i] = true;
4478 n_outputs = 0;
4479 for (i = 0; i < n_operands; i++)
4480 if (curr_static_id->operand[i].type == OP_OUT)
4481 outputs[n_outputs++] = i;
4482 outputs[n_outputs] = -1;
4483 for (i = 0; i < n_operands; i++)
4485 int regno;
4486 bool optional_p = false;
4487 rtx old, new_reg;
4488 rtx op = *curr_id->operand_loc[i];
4490 if (goal_alt_win[i])
4492 if (goal_alt[i] == NO_REGS
4493 && REG_P (op)
4494 /* When we assign NO_REGS it means that we will not
4495 assign a hard register to the scratch pseudo by
4496 assigment pass and the scratch pseudo will be
4497 spilled. Spilled scratch pseudos are transformed
4498 back to scratches at the LRA end. */
4499 && ira_former_scratch_operand_p (curr_insn, i)
4500 && ira_former_scratch_p (REGNO (op)))
4502 int regno = REGNO (op);
4503 lra_change_class (regno, NO_REGS, " Change to", true);
4504 if (lra_get_regno_hard_regno (regno) >= 0)
4505 /* We don't have to mark all insn affected by the
4506 spilled pseudo as there is only one such insn, the
4507 current one. */
4508 reg_renumber[regno] = -1;
4509 lra_assert (bitmap_single_bit_set_p
4510 (&lra_reg_info[REGNO (op)].insn_bitmap));
4512 /* We can do an optional reload. If the pseudo got a hard
4513 reg, we might improve the code through inheritance. If
4514 it does not get a hard register we coalesce memory/memory
4515 moves later. Ignore move insns to avoid cycling. */
4516 if (! lra_simple_p
4517 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
4518 && goal_alt[i] != NO_REGS && REG_P (op)
4519 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
4520 && regno < new_regno_start
4521 && ! ira_former_scratch_p (regno)
4522 && reg_renumber[regno] < 0
4523 /* Check that the optional reload pseudo will be able to
4524 hold given mode value. */
4525 && ! (prohibited_class_reg_set_mode_p
4526 (goal_alt[i], reg_class_contents[goal_alt[i]],
4527 PSEUDO_REGNO_MODE (regno)))
4528 && (curr_insn_set == NULL_RTX
4529 || !((REG_P (SET_SRC (curr_insn_set))
4530 || MEM_P (SET_SRC (curr_insn_set))
4531 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
4532 && (REG_P (SET_DEST (curr_insn_set))
4533 || MEM_P (SET_DEST (curr_insn_set))
4534 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
4535 optional_p = true;
4536 else if (goal_alt_matched[i][0] != -1
4537 && curr_static_id->operand[i].type == OP_OUT
4538 && (curr_static_id->operand_alternative
4539 [goal_alt_number * n_operands + i].earlyclobber)
4540 && REG_P (op))
4542 for (j = 0; goal_alt_matched[i][j] != -1; j++)
4544 rtx op2 = *curr_id->operand_loc[goal_alt_matched[i][j]];
4546 if (REG_P (op2) && REGNO (op) != REGNO (op2))
4547 break;
4549 if (goal_alt_matched[i][j] != -1)
4551 /* Generate reloads for different output and matched
4552 input registers. This is the easiest way to avoid
4553 creation of non-existing register conflicts in
4554 lra-lives.cc. */
4555 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i],
4556 &goal_alt_exclude_start_hard_regs[i], &before,
4557 &after, true);
4559 continue;
4561 else
4563 enum reg_class rclass, common_class;
4565 if (REG_P (op) && goal_alt[i] != NO_REGS
4566 && (regno = REGNO (op)) >= new_regno_start
4567 && (rclass = get_reg_class (regno)) == ALL_REGS
4568 && ((common_class = ira_reg_class_subset[rclass][goal_alt[i]])
4569 != NO_REGS)
4570 && common_class != ALL_REGS
4571 && enough_allocatable_hard_regs_p (common_class,
4572 GET_MODE (op)))
4573 /* Refine reload pseudo class from chosen alternative
4574 constraint. */
4575 lra_change_class (regno, common_class, " Change to", true);
4576 continue;
4580 /* Operands that match previous ones have already been handled. */
4581 if (goal_alt_matches[i] >= 0)
4582 continue;
4584 /* We should not have an operand with a non-offsettable address
4585 appearing where an offsettable address will do. It also may
4586 be a case when the address should be special in other words
4587 not a general one (e.g. it needs no index reg). */
4588 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
4590 enum reg_class rclass;
4591 rtx *loc = &XEXP (op, 0);
4592 enum rtx_code code = GET_CODE (*loc);
4594 push_to_sequence (before);
4595 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
4596 MEM, SCRATCH, curr_insn);
4597 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
4598 new_reg = emit_inc (rclass, *loc,
4599 /* This value does not matter for MODIFY. */
4600 GET_MODE_SIZE (GET_MODE (op)));
4601 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass,
4602 NULL, false,
4603 "offsetable address", &new_reg))
4605 rtx addr = *loc;
4606 enum rtx_code code = GET_CODE (addr);
4607 bool align_p = false;
4609 if (code == AND && CONST_INT_P (XEXP (addr, 1)))
4611 /* (and ... (const_int -X)) is used to align to X bytes. */
4612 align_p = true;
4613 addr = XEXP (*loc, 0);
4615 else
4616 addr = canonicalize_reload_addr (addr);
4618 lra_emit_move (new_reg, addr);
4619 if (align_p)
4620 emit_move_insn (new_reg, gen_rtx_AND (GET_MODE (new_reg), new_reg, XEXP (*loc, 1)));
4622 before = get_insns ();
4623 end_sequence ();
4624 *loc = new_reg;
4625 lra_update_dup (curr_id, i);
4627 else if (goal_alt_matched[i][0] == -1)
4629 machine_mode mode;
4630 rtx reg, *loc;
4631 int hard_regno;
4632 enum op_type type = curr_static_id->operand[i].type;
4634 loc = curr_id->operand_loc[i];
4635 mode = curr_operand_mode[i];
4636 if (GET_CODE (*loc) == SUBREG)
4638 reg = SUBREG_REG (*loc);
4639 poly_int64 byte = SUBREG_BYTE (*loc);
4640 if (REG_P (reg)
4641 /* Strict_low_part requires reloading the register and not
4642 just the subreg. Likewise for a strict subreg no wider
4643 than a word for WORD_REGISTER_OPERATIONS targets. */
4644 && (curr_static_id->operand[i].strict_low
4645 || (!paradoxical_subreg_p (mode, GET_MODE (reg))
4646 && (hard_regno
4647 = get_try_hard_regno (REGNO (reg))) >= 0
4648 && (simplify_subreg_regno
4649 (hard_regno,
4650 GET_MODE (reg), byte, mode) < 0)
4651 && (goal_alt[i] == NO_REGS
4652 || (simplify_subreg_regno
4653 (ira_class_hard_regs[goal_alt[i]][0],
4654 GET_MODE (reg), byte, mode) >= 0)))
4655 || (partial_subreg_p (mode, GET_MODE (reg))
4656 && known_le (GET_MODE_SIZE (GET_MODE (reg)),
4657 UNITS_PER_WORD)
4658 && WORD_REGISTER_OPERATIONS))
4659 /* Avoid the situation when there are no available hard regs
4660 for the pseudo mode but there are ones for the subreg
4661 mode: */
4662 && !(goal_alt[i] != NO_REGS
4663 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
4664 && (prohibited_class_reg_set_mode_p
4665 (goal_alt[i], reg_class_contents[goal_alt[i]],
4666 GET_MODE (reg)))
4667 && !(prohibited_class_reg_set_mode_p
4668 (goal_alt[i], reg_class_contents[goal_alt[i]],
4669 mode))))
4671 /* An OP_INOUT is required when reloading a subreg of a
4672 mode wider than a word to ensure that data beyond the
4673 word being reloaded is preserved. Also automatically
4674 ensure that strict_low_part reloads are made into
4675 OP_INOUT which should already be true from the backend
4676 constraints. */
4677 if (type == OP_OUT
4678 && (curr_static_id->operand[i].strict_low
4679 || read_modify_subreg_p (*loc)))
4680 type = OP_INOUT;
4681 loc = &SUBREG_REG (*loc);
4682 mode = GET_MODE (*loc);
4685 old = *loc;
4686 if (get_reload_reg (type, mode, old, goal_alt[i],
4687 &goal_alt_exclude_start_hard_regs[i],
4688 loc != curr_id->operand_loc[i], "", &new_reg)
4689 && type != OP_OUT)
4691 push_to_sequence (before);
4692 lra_emit_move (new_reg, old);
4693 before = get_insns ();
4694 end_sequence ();
4696 *loc = new_reg;
4697 if (type != OP_IN
4698 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
4700 start_sequence ();
4701 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
4702 emit_insn (after);
4703 after = get_insns ();
4704 end_sequence ();
4705 *loc = new_reg;
4707 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
4708 if (goal_alt_dont_inherit_ops[j] == i)
4710 lra_set_regno_unique_value (REGNO (new_reg));
4711 break;
4713 lra_update_dup (curr_id, i);
4715 else if (curr_static_id->operand[i].type == OP_IN
4716 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4717 == OP_OUT
4718 || (curr_static_id->operand[goal_alt_matched[i][0]].type
4719 == OP_INOUT
4720 && (operands_match_p
4721 (*curr_id->operand_loc[i],
4722 *curr_id->operand_loc[goal_alt_matched[i][0]],
4723 -1)))))
4725 /* generate reloads for input and matched outputs. */
4726 match_inputs[0] = i;
4727 match_inputs[1] = -1;
4728 match_reload (goal_alt_matched[i][0], match_inputs, outputs,
4729 goal_alt[i], &goal_alt_exclude_start_hard_regs[i],
4730 &before, &after,
4731 curr_static_id->operand_alternative
4732 [goal_alt_number * n_operands + goal_alt_matched[i][0]]
4733 .earlyclobber);
4735 else if ((curr_static_id->operand[i].type == OP_OUT
4736 || (curr_static_id->operand[i].type == OP_INOUT
4737 && (operands_match_p
4738 (*curr_id->operand_loc[i],
4739 *curr_id->operand_loc[goal_alt_matched[i][0]],
4740 -1))))
4741 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4742 == OP_IN))
4743 /* Generate reloads for output and matched inputs. */
4744 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i],
4745 &goal_alt_exclude_start_hard_regs[i], &before, &after,
4746 curr_static_id->operand_alternative
4747 [goal_alt_number * n_operands + i].earlyclobber);
4748 else if (curr_static_id->operand[i].type == OP_IN
4749 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4750 == OP_IN))
4752 /* Generate reloads for matched inputs. */
4753 match_inputs[0] = i;
4754 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
4755 match_inputs[j + 1] = k;
4756 match_inputs[j + 1] = -1;
4757 match_reload (-1, match_inputs, outputs, goal_alt[i],
4758 &goal_alt_exclude_start_hard_regs[i],
4759 &before, &after, false);
4761 else
4762 /* We must generate code in any case when function
4763 process_alt_operands decides that it is possible. */
4764 gcc_unreachable ();
4766 if (optional_p)
4768 rtx reg = op;
4770 lra_assert (REG_P (reg));
4771 regno = REGNO (reg);
4772 op = *curr_id->operand_loc[i]; /* Substitution. */
4773 if (GET_CODE (op) == SUBREG)
4774 op = SUBREG_REG (op);
4775 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
4776 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
4777 lra_reg_info[REGNO (op)].restore_rtx = reg;
4778 if (lra_dump_file != NULL)
4779 fprintf (lra_dump_file,
4780 " Making reload reg %d for reg %d optional\n",
4781 REGNO (op), regno);
4784 if (before != NULL_RTX || after != NULL_RTX
4785 || max_regno_before != max_reg_num ())
4786 change_p = true;
4787 if (change_p)
4789 lra_update_operator_dups (curr_id);
4790 /* Something changes -- process the insn. */
4791 lra_update_insn_regno_info (curr_insn);
4792 if (asm_noperands (PATTERN (curr_insn)) >= 0
4793 && ++curr_id->asm_reloads_num >= FIRST_PSEUDO_REGISTER)
4794 /* Most probably there are no enough registers to satisfy asm insn: */
4795 lra_asm_insn_error (curr_insn);
4797 if (goal_alt_out_sp_reload_p)
4799 /* We have an output stack pointer reload -- update sp offset: */
4800 rtx set;
4801 bool done_p = false;
4802 poly_int64 sp_offset = curr_id->sp_offset;
4803 for (rtx_insn *insn = after; insn != NULL_RTX; insn = NEXT_INSN (insn))
4804 if ((set = single_set (insn)) != NULL_RTX
4805 && SET_DEST (set) == stack_pointer_rtx)
4807 lra_assert (!done_p);
4808 done_p = true;
4809 curr_id->sp_offset = 0;
4810 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
4811 id->sp_offset = sp_offset;
4812 if (lra_dump_file != NULL)
4813 fprintf (lra_dump_file,
4814 " Moving sp offset from insn %u to %u\n",
4815 INSN_UID (curr_insn), INSN_UID (insn));
4817 lra_assert (done_p);
4819 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
4820 return change_p;
4823 /* Return true if INSN satisfies all constraints. In other words, no
4824 reload insns are needed. */
4825 bool
4826 lra_constrain_insn (rtx_insn *insn)
4828 int saved_new_regno_start = new_regno_start;
4829 int saved_new_insn_uid_start = new_insn_uid_start;
4830 bool change_p;
4832 curr_insn = insn;
4833 curr_id = lra_get_insn_recog_data (curr_insn);
4834 curr_static_id = curr_id->insn_static_data;
4835 new_insn_uid_start = get_max_uid ();
4836 new_regno_start = max_reg_num ();
4837 change_p = curr_insn_transform (true);
4838 new_regno_start = saved_new_regno_start;
4839 new_insn_uid_start = saved_new_insn_uid_start;
4840 return ! change_p;
4843 /* Return true if X is in LIST. */
4844 static bool
4845 in_list_p (rtx x, rtx list)
4847 for (; list != NULL_RTX; list = XEXP (list, 1))
4848 if (XEXP (list, 0) == x)
4849 return true;
4850 return false;
4853 /* Return true if X contains an allocatable hard register (if
4854 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
4855 static bool
4856 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
4858 int i, j;
4859 const char *fmt;
4860 enum rtx_code code;
4862 code = GET_CODE (x);
4863 if (REG_P (x))
4865 int regno = REGNO (x);
4866 HARD_REG_SET alloc_regs;
4868 if (hard_reg_p)
4870 if (regno >= FIRST_PSEUDO_REGISTER)
4871 regno = lra_get_regno_hard_regno (regno);
4872 if (regno < 0)
4873 return false;
4874 alloc_regs = ~lra_no_alloc_regs;
4875 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
4877 else
4879 if (regno < FIRST_PSEUDO_REGISTER)
4880 return false;
4881 if (! spilled_p)
4882 return true;
4883 return lra_get_regno_hard_regno (regno) < 0;
4886 fmt = GET_RTX_FORMAT (code);
4887 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4889 if (fmt[i] == 'e')
4891 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
4892 return true;
4894 else if (fmt[i] == 'E')
4896 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4897 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4898 return true;
4901 return false;
4904 /* Process all regs in location *LOC and change them on equivalent
4905 substitution. Return true if any change was done. */
4906 static bool
4907 loc_equivalence_change_p (rtx *loc)
4909 rtx subst, reg, x = *loc;
4910 bool result = false;
4911 enum rtx_code code = GET_CODE (x);
4912 const char *fmt;
4913 int i, j;
4915 if (code == SUBREG)
4917 reg = SUBREG_REG (x);
4918 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4919 && GET_MODE (subst) == VOIDmode)
4921 /* We cannot reload debug location. Simplify subreg here
4922 while we know the inner mode. */
4923 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4924 GET_MODE (reg), SUBREG_BYTE (x));
4925 return true;
4928 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4930 *loc = subst;
4931 return true;
4934 /* Scan all the operand sub-expressions. */
4935 fmt = GET_RTX_FORMAT (code);
4936 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4938 if (fmt[i] == 'e')
4939 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4940 else if (fmt[i] == 'E')
4941 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4942 result
4943 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4945 return result;
4948 /* Similar to loc_equivalence_change_p, but for use as
4949 simplify_replace_fn_rtx callback. DATA is insn for which the
4950 elimination is done. If it null we don't do the elimination. */
4951 static rtx
4952 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4954 if (!REG_P (loc))
4955 return NULL_RTX;
4957 rtx subst = (data == NULL
4958 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4959 if (subst != loc)
4960 return subst;
4962 return NULL_RTX;
4965 /* Maximum number of generated reload insns per an insn. It is for
4966 preventing this pass cycling in a bug case. */
4967 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4969 /* The current iteration number of this LRA pass. */
4970 int lra_constraint_iter;
4972 /* True if we should during assignment sub-pass check assignment
4973 correctness for all pseudos and spill some of them to correct
4974 conflicts. It can be necessary when we substitute equiv which
4975 needs checking register allocation correctness because the
4976 equivalent value contains allocatable hard registers, or when we
4977 restore multi-register pseudo, or when we change the insn code and
4978 its operand became INOUT operand when it was IN one before. */
4979 bool check_and_force_assignment_correctness_p;
4981 /* Return true if REGNO is referenced in more than one block. */
4982 static bool
4983 multi_block_pseudo_p (int regno)
4985 basic_block bb = NULL;
4986 unsigned int uid;
4987 bitmap_iterator bi;
4989 if (regno < FIRST_PSEUDO_REGISTER)
4990 return false;
4992 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4993 if (bb == NULL)
4994 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4995 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4996 return true;
4997 return false;
5000 /* Return true if LIST contains a deleted insn. */
5001 static bool
5002 contains_deleted_insn_p (rtx_insn_list *list)
5004 for (; list != NULL_RTX; list = list->next ())
5005 if (NOTE_P (list->insn ())
5006 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
5007 return true;
5008 return false;
5011 /* Return true if X contains a pseudo dying in INSN. */
5012 static bool
5013 dead_pseudo_p (rtx x, rtx_insn *insn)
5015 int i, j;
5016 const char *fmt;
5017 enum rtx_code code;
5019 if (REG_P (x))
5020 return (insn != NULL_RTX
5021 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
5022 code = GET_CODE (x);
5023 fmt = GET_RTX_FORMAT (code);
5024 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5026 if (fmt[i] == 'e')
5028 if (dead_pseudo_p (XEXP (x, i), insn))
5029 return true;
5031 else if (fmt[i] == 'E')
5033 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5034 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
5035 return true;
5038 return false;
5041 /* Return true if INSN contains a dying pseudo in INSN right hand
5042 side. */
5043 static bool
5044 insn_rhs_dead_pseudo_p (rtx_insn *insn)
5046 rtx set = single_set (insn);
5048 gcc_assert (set != NULL);
5049 return dead_pseudo_p (SET_SRC (set), insn);
5052 /* Return true if any init insn of REGNO contains a dying pseudo in
5053 insn right hand side. */
5054 static bool
5055 init_insn_rhs_dead_pseudo_p (int regno)
5057 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
5059 if (insns == NULL)
5060 return false;
5061 for (; insns != NULL_RTX; insns = insns->next ())
5062 if (insn_rhs_dead_pseudo_p (insns->insn ()))
5063 return true;
5064 return false;
5067 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
5068 reverse only if we have one init insn with given REGNO as a
5069 source. */
5070 static bool
5071 reverse_equiv_p (int regno)
5073 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
5074 rtx set;
5076 if (insns == NULL)
5077 return false;
5078 if (! INSN_P (insns->insn ())
5079 || insns->next () != NULL)
5080 return false;
5081 if ((set = single_set (insns->insn ())) == NULL_RTX)
5082 return false;
5083 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
5086 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
5087 call this function only for non-reverse equivalence. */
5088 static bool
5089 contains_reloaded_insn_p (int regno)
5091 rtx set;
5092 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
5094 for (; list != NULL; list = list->next ())
5095 if ((set = single_set (list->insn ())) == NULL_RTX
5096 || ! REG_P (SET_DEST (set))
5097 || (int) REGNO (SET_DEST (set)) != regno)
5098 return true;
5099 return false;
5102 /* Try combine secondary memory reload insn FROM for insn TO into TO insn.
5103 FROM should be a load insn (usually a secondary memory reload insn). Return
5104 TRUE in case of success. */
5105 static bool
5106 combine_reload_insn (rtx_insn *from, rtx_insn *to)
5108 bool ok_p;
5109 rtx_insn *saved_insn;
5110 rtx set, from_reg, to_reg, op;
5111 enum reg_class to_class, from_class;
5112 int n, nop;
5113 signed char changed_nops[MAX_RECOG_OPERANDS + 1];
5115 /* Check conditions for second memory reload and original insn: */
5116 if ((targetm.secondary_memory_needed
5117 == hook_bool_mode_reg_class_t_reg_class_t_false)
5118 || NEXT_INSN (from) != to
5119 || !NONDEBUG_INSN_P (to)
5120 || CALL_P (to))
5121 return false;
5123 lra_insn_recog_data_t id = lra_get_insn_recog_data (to);
5124 struct lra_static_insn_data *static_id = id->insn_static_data;
5126 if (id->used_insn_alternative == LRA_UNKNOWN_ALT
5127 || (set = single_set (from)) == NULL_RTX)
5128 return false;
5129 from_reg = SET_DEST (set);
5130 to_reg = SET_SRC (set);
5131 /* Ignore optional reloads: */
5132 if (! REG_P (from_reg) || ! REG_P (to_reg)
5133 || bitmap_bit_p (&lra_optional_reload_pseudos, REGNO (from_reg)))
5134 return false;
5135 to_class = lra_get_allocno_class (REGNO (to_reg));
5136 from_class = lra_get_allocno_class (REGNO (from_reg));
5137 /* Check that reload insn is a load: */
5138 if (to_class != NO_REGS || from_class == NO_REGS)
5139 return false;
5140 for (n = nop = 0; nop < static_id->n_operands; nop++)
5142 if (static_id->operand[nop].type != OP_IN)
5143 continue;
5144 op = *id->operand_loc[nop];
5145 if (!REG_P (op) || REGNO (op) != REGNO (from_reg))
5146 continue;
5147 *id->operand_loc[nop] = to_reg;
5148 changed_nops[n++] = nop;
5150 changed_nops[n] = -1;
5151 lra_update_dups (id, changed_nops);
5152 lra_update_insn_regno_info (to);
5153 ok_p = recog_memoized (to) >= 0;
5154 if (ok_p)
5156 /* Check that combined insn does not need any reloads: */
5157 saved_insn = curr_insn;
5158 curr_insn = to;
5159 curr_id = lra_get_insn_recog_data (curr_insn);
5160 curr_static_id = curr_id->insn_static_data;
5161 for (bool swapped_p = false;;)
5163 ok_p = !curr_insn_transform (true);
5164 if (ok_p || curr_static_id->commutative < 0)
5165 break;
5166 swap_operands (curr_static_id->commutative);
5167 if (lra_dump_file != NULL)
5169 fprintf (lra_dump_file,
5170 " Swapping %scombined insn operands:\n",
5171 swapped_p ? "back " : "");
5172 dump_insn_slim (lra_dump_file, to);
5174 if (swapped_p)
5175 break;
5176 swapped_p = true;
5178 curr_insn = saved_insn;
5179 curr_id = lra_get_insn_recog_data (curr_insn);
5180 curr_static_id = curr_id->insn_static_data;
5182 if (ok_p)
5184 id->used_insn_alternative = -1;
5185 lra_push_insn_and_update_insn_regno_info (to);
5186 if (lra_dump_file != NULL)
5188 fprintf (lra_dump_file, " Use combined insn:\n");
5189 dump_insn_slim (lra_dump_file, to);
5191 return true;
5193 if (lra_dump_file != NULL)
5195 fprintf (lra_dump_file, " Failed combined insn:\n");
5196 dump_insn_slim (lra_dump_file, to);
5198 for (int i = 0; i < n; i++)
5200 nop = changed_nops[i];
5201 *id->operand_loc[nop] = from_reg;
5203 lra_update_dups (id, changed_nops);
5204 lra_update_insn_regno_info (to);
5205 if (lra_dump_file != NULL)
5207 fprintf (lra_dump_file, " Restoring insn after failed combining:\n");
5208 dump_insn_slim (lra_dump_file, to);
5210 return false;
5213 /* Entry function of LRA constraint pass. Return true if the
5214 constraint pass did change the code. */
5215 bool
5216 lra_constraints (bool first_p)
5218 bool changed_p;
5219 int i, hard_regno, new_insns_num;
5220 unsigned int min_len, new_min_len, uid;
5221 rtx set, x, reg, dest_reg;
5222 rtx_insn *original_insn;
5223 basic_block last_bb;
5224 bitmap_iterator bi;
5226 lra_constraint_iter++;
5227 if (lra_dump_file != NULL)
5228 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
5229 lra_constraint_iter);
5230 changed_p = false;
5231 if (pic_offset_table_rtx
5232 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
5233 check_and_force_assignment_correctness_p = true;
5234 else if (first_p)
5235 /* On the first iteration we should check IRA assignment
5236 correctness. In rare cases, the assignments can be wrong as
5237 early clobbers operands are ignored in IRA or usages of
5238 paradoxical sub-registers are not taken into account by
5239 IRA. */
5240 check_and_force_assignment_correctness_p = true;
5241 new_insn_uid_start = get_max_uid ();
5242 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
5243 /* Mark used hard regs for target stack size calulations. */
5244 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5245 if (lra_reg_info[i].nrefs != 0
5246 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
5248 int j, nregs;
5250 nregs = hard_regno_nregs (hard_regno, lra_reg_info[i].biggest_mode);
5251 for (j = 0; j < nregs; j++)
5252 df_set_regs_ever_live (hard_regno + j, true);
5254 /* Do elimination before the equivalence processing as we can spill
5255 some pseudos during elimination. */
5256 lra_eliminate (false, first_p);
5257 auto_bitmap equiv_insn_bitmap (&reg_obstack);
5258 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5259 if (lra_reg_info[i].nrefs != 0)
5261 ira_reg_equiv[i].profitable_p = true;
5262 reg = regno_reg_rtx[i];
5263 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
5265 bool pseudo_p = contains_reg_p (x, false, false);
5267 /* After RTL transformation, we cannot guarantee that
5268 pseudo in the substitution was not reloaded which might
5269 make equivalence invalid. For example, in reverse
5270 equiv of p0
5272 p0 <- ...
5274 equiv_mem <- p0
5276 the memory address register was reloaded before the 2nd
5277 insn. */
5278 if ((! first_p && pseudo_p)
5279 /* We don't use DF for compilation speed sake. So it
5280 is problematic to update live info when we use an
5281 equivalence containing pseudos in more than one
5282 BB. */
5283 || (pseudo_p && multi_block_pseudo_p (i))
5284 /* If an init insn was deleted for some reason, cancel
5285 the equiv. We could update the equiv insns after
5286 transformations including an equiv insn deletion
5287 but it is not worthy as such cases are extremely
5288 rare. */
5289 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
5290 /* If it is not a reverse equivalence, we check that a
5291 pseudo in rhs of the init insn is not dying in the
5292 insn. Otherwise, the live info at the beginning of
5293 the corresponding BB might be wrong after we
5294 removed the insn. When the equiv can be a
5295 constant, the right hand side of the init insn can
5296 be a pseudo. */
5297 || (! reverse_equiv_p (i)
5298 && (init_insn_rhs_dead_pseudo_p (i)
5299 /* If we reloaded the pseudo in an equivalence
5300 init insn, we cannot remove the equiv init
5301 insns and the init insns might write into
5302 const memory in this case. */
5303 || contains_reloaded_insn_p (i)))
5304 /* Prevent access beyond equivalent memory for
5305 paradoxical subregs. */
5306 || (MEM_P (x)
5307 && maybe_gt (GET_MODE_SIZE (lra_reg_info[i].biggest_mode),
5308 GET_MODE_SIZE (GET_MODE (x))))
5309 || (pic_offset_table_rtx
5310 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
5311 && (targetm.preferred_reload_class
5312 (x, lra_get_allocno_class (i)) == NO_REGS))
5313 || contains_symbol_ref_p (x))))
5314 ira_reg_equiv[i].defined_p
5315 = ira_reg_equiv[i].caller_save_p = false;
5316 if (contains_reg_p (x, false, true))
5317 ira_reg_equiv[i].profitable_p = false;
5318 if (get_equiv (reg) != reg)
5319 bitmap_ior_into (equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
5322 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5323 update_equiv (i);
5324 /* We should add all insns containing pseudos which should be
5325 substituted by their equivalences. */
5326 EXECUTE_IF_SET_IN_BITMAP (equiv_insn_bitmap, 0, uid, bi)
5327 lra_push_insn_by_uid (uid);
5328 min_len = lra_insn_stack_length ();
5329 new_insns_num = 0;
5330 last_bb = NULL;
5331 changed_p = false;
5332 original_insn = NULL;
5333 while ((new_min_len = lra_insn_stack_length ()) != 0)
5335 curr_insn = lra_pop_insn ();
5336 --new_min_len;
5337 curr_bb = BLOCK_FOR_INSN (curr_insn);
5338 if (curr_bb != last_bb)
5340 last_bb = curr_bb;
5341 bb_reload_num = lra_curr_reload_num;
5343 if (min_len > new_min_len)
5345 min_len = new_min_len;
5346 new_insns_num = 0;
5347 original_insn = curr_insn;
5349 else if (combine_reload_insn (curr_insn, original_insn))
5351 continue;
5353 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
5354 internal_error
5355 ("maximum number of generated reload insns per insn achieved (%d)",
5356 MAX_RELOAD_INSNS_NUMBER);
5357 new_insns_num++;
5358 if (DEBUG_INSN_P (curr_insn))
5360 /* We need to check equivalence in debug insn and change
5361 pseudo to the equivalent value if necessary. */
5362 curr_id = lra_get_insn_recog_data (curr_insn);
5363 if (bitmap_bit_p (equiv_insn_bitmap, INSN_UID (curr_insn)))
5365 rtx old = *curr_id->operand_loc[0];
5366 *curr_id->operand_loc[0]
5367 = simplify_replace_fn_rtx (old, NULL_RTX,
5368 loc_equivalence_callback, curr_insn);
5369 if (old != *curr_id->operand_loc[0])
5371 /* If we substitute pseudo by shared equivalence, we can fail
5372 to update LRA reg info and this can result in many
5373 unexpected consequences. So keep rtl unshared: */
5374 *curr_id->operand_loc[0]
5375 = copy_rtx (*curr_id->operand_loc[0]);
5376 lra_update_insn_regno_info (curr_insn);
5377 changed_p = true;
5381 else if (INSN_P (curr_insn))
5383 if ((set = single_set (curr_insn)) != NULL_RTX)
5385 dest_reg = SET_DEST (set);
5386 /* The equivalence pseudo could be set up as SUBREG in a
5387 case when it is a call restore insn in a mode
5388 different from the pseudo mode. */
5389 if (GET_CODE (dest_reg) == SUBREG)
5390 dest_reg = SUBREG_REG (dest_reg);
5391 if ((REG_P (dest_reg)
5392 && (x = get_equiv (dest_reg)) != dest_reg
5393 /* Remove insns which set up a pseudo whose value
5394 cannot be changed. Such insns might be not in
5395 init_insns because we don't update equiv data
5396 during insn transformations.
5398 As an example, let suppose that a pseudo got
5399 hard register and on the 1st pass was not
5400 changed to equivalent constant. We generate an
5401 additional insn setting up the pseudo because of
5402 secondary memory movement. Then the pseudo is
5403 spilled and we use the equiv constant. In this
5404 case we should remove the additional insn and
5405 this insn is not init_insns list. */
5406 && (! MEM_P (x) || MEM_READONLY_P (x)
5407 /* Check that this is actually an insn setting
5408 up the equivalence. */
5409 || in_list_p (curr_insn,
5410 ira_reg_equiv
5411 [REGNO (dest_reg)].init_insns)))
5412 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
5413 && in_list_p (curr_insn,
5414 ira_reg_equiv
5415 [REGNO (SET_SRC (set))].init_insns)))
5417 /* This is equiv init insn of pseudo which did not get a
5418 hard register -- remove the insn. */
5419 if (lra_dump_file != NULL)
5421 fprintf (lra_dump_file,
5422 " Removing equiv init insn %i (freq=%d)\n",
5423 INSN_UID (curr_insn),
5424 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
5425 dump_insn_slim (lra_dump_file, curr_insn);
5427 if (contains_reg_p (x, true, false))
5428 check_and_force_assignment_correctness_p = true;
5429 lra_set_insn_deleted (curr_insn);
5430 continue;
5433 curr_id = lra_get_insn_recog_data (curr_insn);
5434 curr_static_id = curr_id->insn_static_data;
5435 init_curr_insn_input_reloads ();
5436 init_curr_operand_mode ();
5437 if (curr_insn_transform (false))
5438 changed_p = true;
5439 /* Check non-transformed insns too for equiv change as USE
5440 or CLOBBER don't need reloads but can contain pseudos
5441 being changed on their equivalences. */
5442 else if (bitmap_bit_p (equiv_insn_bitmap, INSN_UID (curr_insn))
5443 && loc_equivalence_change_p (&PATTERN (curr_insn)))
5445 lra_update_insn_regno_info (curr_insn);
5446 changed_p = true;
5451 /* If we used a new hard regno, changed_p should be true because the
5452 hard reg is assigned to a new pseudo. */
5453 if (flag_checking && !changed_p)
5455 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5456 if (lra_reg_info[i].nrefs != 0
5457 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
5459 int j, nregs = hard_regno_nregs (hard_regno,
5460 PSEUDO_REGNO_MODE (i));
5462 for (j = 0; j < nregs; j++)
5463 lra_assert (df_regs_ever_live_p (hard_regno + j));
5466 if (changed_p)
5467 lra_dump_insns_if_possible ("changed func after local");
5468 return changed_p;
5471 static void initiate_invariants (void);
5472 static void finish_invariants (void);
5474 /* Initiate the LRA constraint pass. It is done once per
5475 function. */
5476 void
5477 lra_constraints_init (void)
5479 initiate_invariants ();
5482 /* Finalize the LRA constraint pass. It is done once per
5483 function. */
5484 void
5485 lra_constraints_finish (void)
5487 finish_invariants ();
5492 /* Structure describes invariants for ineheritance. */
5493 struct lra_invariant
5495 /* The order number of the invariant. */
5496 int num;
5497 /* The invariant RTX. */
5498 rtx invariant_rtx;
5499 /* The origin insn of the invariant. */
5500 rtx_insn *insn;
5503 typedef lra_invariant invariant_t;
5504 typedef invariant_t *invariant_ptr_t;
5505 typedef const invariant_t *const_invariant_ptr_t;
5507 /* Pointer to the inheritance invariants. */
5508 static vec<invariant_ptr_t> invariants;
5510 /* Allocation pool for the invariants. */
5511 static object_allocator<lra_invariant> *invariants_pool;
5513 /* Hash table for the invariants. */
5514 static htab_t invariant_table;
5516 /* Hash function for INVARIANT. */
5517 static hashval_t
5518 invariant_hash (const void *invariant)
5520 rtx inv = ((const_invariant_ptr_t) invariant)->invariant_rtx;
5521 return lra_rtx_hash (inv);
5524 /* Equal function for invariants INVARIANT1 and INVARIANT2. */
5525 static int
5526 invariant_eq_p (const void *invariant1, const void *invariant2)
5528 rtx inv1 = ((const_invariant_ptr_t) invariant1)->invariant_rtx;
5529 rtx inv2 = ((const_invariant_ptr_t) invariant2)->invariant_rtx;
5531 return rtx_equal_p (inv1, inv2);
5534 /* Insert INVARIANT_RTX into the table if it is not there yet. Return
5535 invariant which is in the table. */
5536 static invariant_ptr_t
5537 insert_invariant (rtx invariant_rtx)
5539 void **entry_ptr;
5540 invariant_t invariant;
5541 invariant_ptr_t invariant_ptr;
5543 invariant.invariant_rtx = invariant_rtx;
5544 entry_ptr = htab_find_slot (invariant_table, &invariant, INSERT);
5545 if (*entry_ptr == NULL)
5547 invariant_ptr = invariants_pool->allocate ();
5548 invariant_ptr->invariant_rtx = invariant_rtx;
5549 invariant_ptr->insn = NULL;
5550 invariants.safe_push (invariant_ptr);
5551 *entry_ptr = (void *) invariant_ptr;
5553 return (invariant_ptr_t) *entry_ptr;
5556 /* Initiate the invariant table. */
5557 static void
5558 initiate_invariants (void)
5560 invariants.create (100);
5561 invariants_pool
5562 = new object_allocator<lra_invariant> ("Inheritance invariants");
5563 invariant_table = htab_create (100, invariant_hash, invariant_eq_p, NULL);
5566 /* Finish the invariant table. */
5567 static void
5568 finish_invariants (void)
5570 htab_delete (invariant_table);
5571 delete invariants_pool;
5572 invariants.release ();
5575 /* Make the invariant table empty. */
5576 static void
5577 clear_invariants (void)
5579 htab_empty (invariant_table);
5580 invariants_pool->release ();
5581 invariants.truncate (0);
5586 /* This page contains code to do inheritance/split
5587 transformations. */
5589 /* Number of reloads passed so far in current EBB. */
5590 static int reloads_num;
5592 /* Number of calls passed so far in current EBB. */
5593 static int calls_num;
5595 /* Index ID is the CALLS_NUM associated the last call we saw with
5596 ABI identifier ID. */
5597 static int last_call_for_abi[NUM_ABI_IDS];
5599 /* Which registers have been fully or partially clobbered by a call
5600 since they were last used. */
5601 static HARD_REG_SET full_and_partial_call_clobbers;
5603 /* Current reload pseudo check for validity of elements in
5604 USAGE_INSNS. */
5605 static int curr_usage_insns_check;
5607 /* Info about last usage of registers in EBB to do inheritance/split
5608 transformation. Inheritance transformation is done from a spilled
5609 pseudo and split transformations from a hard register or a pseudo
5610 assigned to a hard register. */
5611 struct usage_insns
5613 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
5614 value INSNS is valid. The insns is chain of optional debug insns
5615 and a finishing non-debug insn using the corresponding reg. The
5616 value is also used to mark the registers which are set up in the
5617 current insn. The negated insn uid is used for this. */
5618 int check;
5619 /* Value of global reloads_num at the last insn in INSNS. */
5620 int reloads_num;
5621 /* Value of global reloads_nums at the last insn in INSNS. */
5622 int calls_num;
5623 /* It can be true only for splitting. And it means that the restore
5624 insn should be put after insn given by the following member. */
5625 bool after_p;
5626 /* Next insns in the current EBB which use the original reg and the
5627 original reg value is not changed between the current insn and
5628 the next insns. In order words, e.g. for inheritance, if we need
5629 to use the original reg value again in the next insns we can try
5630 to use the value in a hard register from a reload insn of the
5631 current insn. */
5632 rtx insns;
5635 /* Map: regno -> corresponding pseudo usage insns. */
5636 static struct usage_insns *usage_insns;
5638 static void
5639 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
5641 usage_insns[regno].check = curr_usage_insns_check;
5642 usage_insns[regno].insns = insn;
5643 usage_insns[regno].reloads_num = reloads_num;
5644 usage_insns[regno].calls_num = calls_num;
5645 usage_insns[regno].after_p = after_p;
5646 if (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0)
5647 remove_from_hard_reg_set (&full_and_partial_call_clobbers,
5648 PSEUDO_REGNO_MODE (regno),
5649 reg_renumber[regno]);
5652 /* The function is used to form list REGNO usages which consists of
5653 optional debug insns finished by a non-debug insn using REGNO.
5654 RELOADS_NUM is current number of reload insns processed so far. */
5655 static void
5656 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
5658 rtx next_usage_insns;
5660 if (usage_insns[regno].check == curr_usage_insns_check
5661 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
5662 && DEBUG_INSN_P (insn))
5664 /* Check that we did not add the debug insn yet. */
5665 if (next_usage_insns != insn
5666 && (GET_CODE (next_usage_insns) != INSN_LIST
5667 || XEXP (next_usage_insns, 0) != insn))
5668 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
5669 next_usage_insns);
5671 else if (NONDEBUG_INSN_P (insn))
5672 setup_next_usage_insn (regno, insn, reloads_num, false);
5673 else
5674 usage_insns[regno].check = 0;
5677 /* Return first non-debug insn in list USAGE_INSNS. */
5678 static rtx_insn *
5679 skip_usage_debug_insns (rtx usage_insns)
5681 rtx insn;
5683 /* Skip debug insns. */
5684 for (insn = usage_insns;
5685 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
5686 insn = XEXP (insn, 1))
5688 return safe_as_a <rtx_insn *> (insn);
5691 /* Return true if we need secondary memory moves for insn in
5692 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
5693 into the insn. */
5694 static bool
5695 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
5696 rtx usage_insns ATTRIBUTE_UNUSED)
5698 rtx_insn *insn;
5699 rtx set, dest;
5700 enum reg_class cl;
5702 if (inher_cl == ALL_REGS
5703 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
5704 return false;
5705 lra_assert (INSN_P (insn));
5706 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
5707 return false;
5708 dest = SET_DEST (set);
5709 if (! REG_P (dest))
5710 return false;
5711 lra_assert (inher_cl != NO_REGS);
5712 cl = get_reg_class (REGNO (dest));
5713 return (cl != NO_REGS && cl != ALL_REGS
5714 && targetm.secondary_memory_needed (GET_MODE (dest), inher_cl, cl));
5717 /* Registers involved in inheritance/split in the current EBB
5718 (inheritance/split pseudos and original registers). */
5719 static bitmap_head check_only_regs;
5721 /* Reload pseudos cannot be involded in invariant inheritance in the
5722 current EBB. */
5723 static bitmap_head invalid_invariant_regs;
5725 /* Do inheritance transformations for insn INSN, which defines (if
5726 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
5727 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
5728 form as the "insns" field of usage_insns. Return true if we
5729 succeed in such transformation.
5731 The transformations look like:
5733 p <- ... i <- ...
5734 ... p <- i (new insn)
5735 ... =>
5736 <- ... p ... <- ... i ...
5738 ... i <- p (new insn)
5739 <- ... p ... <- ... i ...
5740 ... =>
5741 <- ... p ... <- ... i ...
5742 where p is a spilled original pseudo and i is a new inheritance pseudo.
5745 The inheritance pseudo has the smallest class of two classes CL and
5746 class of ORIGINAL REGNO. */
5747 static bool
5748 inherit_reload_reg (bool def_p, int original_regno,
5749 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
5751 if (optimize_function_for_size_p (cfun))
5752 return false;
5754 enum reg_class rclass = lra_get_allocno_class (original_regno);
5755 rtx original_reg = regno_reg_rtx[original_regno];
5756 rtx new_reg, usage_insn;
5757 rtx_insn *new_insns;
5759 lra_assert (! usage_insns[original_regno].after_p);
5760 if (lra_dump_file != NULL)
5761 fprintf (lra_dump_file,
5762 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
5763 if (! ira_reg_classes_intersect_p[cl][rclass])
5765 if (lra_dump_file != NULL)
5767 fprintf (lra_dump_file,
5768 " Rejecting inheritance for %d "
5769 "because of disjoint classes %s and %s\n",
5770 original_regno, reg_class_names[cl],
5771 reg_class_names[rclass]);
5772 fprintf (lra_dump_file,
5773 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5775 return false;
5777 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
5778 /* We don't use a subset of two classes because it can be
5779 NO_REGS. This transformation is still profitable in most
5780 cases even if the classes are not intersected as register
5781 move is probably cheaper than a memory load. */
5782 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
5784 if (lra_dump_file != NULL)
5785 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
5786 reg_class_names[cl], reg_class_names[rclass]);
5788 rclass = cl;
5790 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
5792 /* Reject inheritance resulting in secondary memory moves.
5793 Otherwise, there is a danger in LRA cycling. Also such
5794 transformation will be unprofitable. */
5795 if (lra_dump_file != NULL)
5797 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
5798 rtx set = single_set (insn);
5800 lra_assert (set != NULL_RTX);
5802 rtx dest = SET_DEST (set);
5804 lra_assert (REG_P (dest));
5805 fprintf (lra_dump_file,
5806 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
5807 "as secondary mem is needed\n",
5808 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
5809 original_regno, reg_class_names[rclass]);
5810 fprintf (lra_dump_file,
5811 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5813 return false;
5815 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
5816 rclass, NULL, "inheritance");
5817 start_sequence ();
5818 if (def_p)
5819 lra_emit_move (original_reg, new_reg);
5820 else
5821 lra_emit_move (new_reg, original_reg);
5822 new_insns = get_insns ();
5823 end_sequence ();
5824 if (NEXT_INSN (new_insns) != NULL_RTX)
5826 if (lra_dump_file != NULL)
5828 fprintf (lra_dump_file,
5829 " Rejecting inheritance %d->%d "
5830 "as it results in 2 or more insns:\n",
5831 original_regno, REGNO (new_reg));
5832 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
5833 fprintf (lra_dump_file,
5834 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5836 return false;
5838 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
5839 lra_update_insn_regno_info (insn);
5840 if (! def_p)
5841 /* We now have a new usage insn for original regno. */
5842 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
5843 if (lra_dump_file != NULL)
5844 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
5845 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
5846 lra_reg_info[REGNO (new_reg)].restore_rtx = regno_reg_rtx[original_regno];
5847 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5848 bitmap_set_bit (&check_only_regs, original_regno);
5849 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
5850 if (def_p)
5851 lra_process_new_insns (insn, NULL, new_insns,
5852 "Add original<-inheritance");
5853 else
5854 lra_process_new_insns (insn, new_insns, NULL,
5855 "Add inheritance<-original");
5856 while (next_usage_insns != NULL_RTX)
5858 if (GET_CODE (next_usage_insns) != INSN_LIST)
5860 usage_insn = next_usage_insns;
5861 lra_assert (NONDEBUG_INSN_P (usage_insn));
5862 next_usage_insns = NULL;
5864 else
5866 usage_insn = XEXP (next_usage_insns, 0);
5867 lra_assert (DEBUG_INSN_P (usage_insn));
5868 next_usage_insns = XEXP (next_usage_insns, 1);
5870 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false,
5871 DEBUG_INSN_P (usage_insn));
5872 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5873 if (lra_dump_file != NULL)
5875 basic_block bb = BLOCK_FOR_INSN (usage_insn);
5876 fprintf (lra_dump_file,
5877 " Inheritance reuse change %d->%d (bb%d):\n",
5878 original_regno, REGNO (new_reg),
5879 bb ? bb->index : -1);
5880 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5883 if (lra_dump_file != NULL)
5884 fprintf (lra_dump_file,
5885 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5886 return true;
5889 /* Return true if we need a caller save/restore for pseudo REGNO which
5890 was assigned to a hard register. */
5891 static inline bool
5892 need_for_call_save_p (int regno)
5894 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
5895 if (usage_insns[regno].calls_num < calls_num)
5897 unsigned int abis = 0;
5898 for (unsigned int i = 0; i < NUM_ABI_IDS; ++i)
5899 if (last_call_for_abi[i] > usage_insns[regno].calls_num)
5900 abis |= 1 << i;
5901 gcc_assert (abis);
5902 if (call_clobbered_in_region_p (abis, full_and_partial_call_clobbers,
5903 PSEUDO_REGNO_MODE (regno),
5904 reg_renumber[regno]))
5905 return true;
5907 return false;
5910 /* Global registers occurring in the current EBB. */
5911 static bitmap_head ebb_global_regs;
5913 /* Return true if we need a split for hard register REGNO or pseudo
5914 REGNO which was assigned to a hard register.
5915 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
5916 used for reloads since the EBB end. It is an approximation of the
5917 used hard registers in the split range. The exact value would
5918 require expensive calculations. If we were aggressive with
5919 splitting because of the approximation, the split pseudo will save
5920 the same hard register assignment and will be removed in the undo
5921 pass. We still need the approximation because too aggressive
5922 splitting would result in too inaccurate cost calculation in the
5923 assignment pass because of too many generated moves which will be
5924 probably removed in the undo pass. */
5925 static inline bool
5926 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
5928 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
5930 lra_assert (hard_regno >= 0);
5931 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
5932 /* Don't split eliminable hard registers, otherwise we can
5933 split hard registers like hard frame pointer, which
5934 lives on BB start/end according to DF-infrastructure,
5935 when there is a pseudo assigned to the register and
5936 living in the same BB. */
5937 && (regno >= FIRST_PSEUDO_REGISTER
5938 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
5939 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
5940 /* Don't split call clobbered hard regs living through
5941 calls, otherwise we might have a check problem in the
5942 assign sub-pass as in the most cases (exception is a
5943 situation when check_and_force_assignment_correctness_p value is
5944 true) the assign pass assumes that all pseudos living
5945 through calls are assigned to call saved hard regs. */
5946 && (regno >= FIRST_PSEUDO_REGISTER
5947 || !TEST_HARD_REG_BIT (full_and_partial_call_clobbers, regno))
5948 /* We need at least 2 reloads to make pseudo splitting
5949 profitable. We should provide hard regno splitting in
5950 any case to solve 1st insn scheduling problem when
5951 moving hard register definition up might result in
5952 impossibility to find hard register for reload pseudo of
5953 small register class. */
5954 && (usage_insns[regno].reloads_num
5955 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
5956 && (regno < FIRST_PSEUDO_REGISTER
5957 /* For short living pseudos, spilling + inheritance can
5958 be considered a substitution for splitting.
5959 Therefore we do not splitting for local pseudos. It
5960 decreases also aggressiveness of splitting. The
5961 minimal number of references is chosen taking into
5962 account that for 2 references splitting has no sense
5963 as we can just spill the pseudo. */
5964 || (regno >= FIRST_PSEUDO_REGISTER
5965 && lra_reg_info[regno].nrefs > 3
5966 && bitmap_bit_p (&ebb_global_regs, regno))))
5967 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
5970 /* Return class for the split pseudo created from original pseudo with
5971 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
5972 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
5973 results in no secondary memory movements. */
5974 static enum reg_class
5975 choose_split_class (enum reg_class allocno_class,
5976 int hard_regno ATTRIBUTE_UNUSED,
5977 machine_mode mode ATTRIBUTE_UNUSED)
5979 int i;
5980 enum reg_class cl, best_cl = NO_REGS;
5981 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
5982 = REGNO_REG_CLASS (hard_regno);
5984 if (! targetm.secondary_memory_needed (mode, allocno_class, allocno_class)
5985 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
5986 return allocno_class;
5987 for (i = 0;
5988 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
5989 i++)
5990 if (! targetm.secondary_memory_needed (mode, cl, hard_reg_class)
5991 && ! targetm.secondary_memory_needed (mode, hard_reg_class, cl)
5992 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
5993 && (best_cl == NO_REGS
5994 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
5995 best_cl = cl;
5996 return best_cl;
5999 /* Copy any equivalence information from ORIGINAL_REGNO to NEW_REGNO. It only
6000 makes sense to call this function if NEW_REGNO is always equal to
6001 ORIGINAL_REGNO. Set up defined_p flag when caller_save_p flag is set up and
6002 CALL_SAVE_P is true. */
6004 static void
6005 lra_copy_reg_equiv (unsigned int new_regno, unsigned int original_regno,
6006 bool call_save_p)
6008 if (!ira_reg_equiv[original_regno].defined_p
6009 && !(call_save_p && ira_reg_equiv[original_regno].caller_save_p))
6010 return;
6012 ira_expand_reg_equiv ();
6013 ira_reg_equiv[new_regno].defined_p = true;
6014 if (ira_reg_equiv[original_regno].memory)
6015 ira_reg_equiv[new_regno].memory
6016 = copy_rtx (ira_reg_equiv[original_regno].memory);
6017 if (ira_reg_equiv[original_regno].constant)
6018 ira_reg_equiv[new_regno].constant
6019 = copy_rtx (ira_reg_equiv[original_regno].constant);
6020 if (ira_reg_equiv[original_regno].invariant)
6021 ira_reg_equiv[new_regno].invariant
6022 = copy_rtx (ira_reg_equiv[original_regno].invariant);
6025 /* Do split transformations for insn INSN, which defines or uses
6026 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
6027 the EBB next uses ORIGINAL_REGNO; it has the same form as the
6028 "insns" field of usage_insns. If TO is not NULL, we don't use
6029 usage_insns, we put restore insns after TO insn. It is a case when
6030 we call it from lra_split_hard_reg_for, outside the inheritance
6031 pass.
6033 The transformations look like:
6035 p <- ... p <- ...
6036 ... s <- p (new insn -- save)
6037 ... =>
6038 ... p <- s (new insn -- restore)
6039 <- ... p ... <- ... p ...
6041 <- ... p ... <- ... p ...
6042 ... s <- p (new insn -- save)
6043 ... =>
6044 ... p <- s (new insn -- restore)
6045 <- ... p ... <- ... p ...
6047 where p is an original pseudo got a hard register or a hard
6048 register and s is a new split pseudo. The save is put before INSN
6049 if BEFORE_P is true. Return true if we succeed in such
6050 transformation. */
6051 static bool
6052 split_reg (bool before_p, int original_regno, rtx_insn *insn,
6053 rtx next_usage_insns, rtx_insn *to)
6055 enum reg_class rclass;
6056 rtx original_reg;
6057 int hard_regno, nregs;
6058 rtx new_reg, usage_insn;
6059 rtx_insn *restore, *save;
6060 bool after_p;
6061 bool call_save_p;
6062 machine_mode mode;
6064 if (original_regno < FIRST_PSEUDO_REGISTER)
6066 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
6067 hard_regno = original_regno;
6068 call_save_p = false;
6069 nregs = 1;
6070 mode = lra_reg_info[hard_regno].biggest_mode;
6071 machine_mode reg_rtx_mode = GET_MODE (regno_reg_rtx[hard_regno]);
6072 /* A reg can have a biggest_mode of VOIDmode if it was only ever seen as
6073 part of a multi-word register. In that case, just use the reg_rtx
6074 mode. Do the same also if the biggest mode was larger than a register
6075 or we can not compare the modes. Otherwise, limit the size to that of
6076 the biggest access in the function or to the natural mode at least. */
6077 if (mode == VOIDmode
6078 || !ordered_p (GET_MODE_PRECISION (mode),
6079 GET_MODE_PRECISION (reg_rtx_mode))
6080 || paradoxical_subreg_p (mode, reg_rtx_mode)
6081 || maybe_gt (GET_MODE_PRECISION (reg_rtx_mode), GET_MODE_PRECISION (mode)))
6083 original_reg = regno_reg_rtx[hard_regno];
6084 mode = reg_rtx_mode;
6086 else
6087 original_reg = gen_rtx_REG (mode, hard_regno);
6089 else
6091 mode = PSEUDO_REGNO_MODE (original_regno);
6092 hard_regno = reg_renumber[original_regno];
6093 nregs = hard_regno_nregs (hard_regno, mode);
6094 rclass = lra_get_allocno_class (original_regno);
6095 original_reg = regno_reg_rtx[original_regno];
6096 call_save_p = need_for_call_save_p (original_regno);
6098 lra_assert (hard_regno >= 0);
6099 if (lra_dump_file != NULL)
6100 fprintf (lra_dump_file,
6101 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
6103 if (call_save_p)
6105 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
6106 hard_regno_nregs (hard_regno, mode),
6107 mode);
6108 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, NULL, "save");
6110 else
6112 rclass = choose_split_class (rclass, hard_regno, mode);
6113 if (rclass == NO_REGS)
6115 if (lra_dump_file != NULL)
6117 fprintf (lra_dump_file,
6118 " Rejecting split of %d(%s): "
6119 "no good reg class for %d(%s)\n",
6120 original_regno,
6121 reg_class_names[lra_get_allocno_class (original_regno)],
6122 hard_regno,
6123 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
6124 fprintf
6125 (lra_dump_file,
6126 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6128 return false;
6130 /* Split_if_necessary can split hard registers used as part of a
6131 multi-register mode but splits each register individually. The
6132 mode used for each independent register may not be supported
6133 so reject the split. Splitting the wider mode should theoretically
6134 be possible but is not implemented. */
6135 if (!targetm.hard_regno_mode_ok (hard_regno, mode))
6137 if (lra_dump_file != NULL)
6139 fprintf (lra_dump_file,
6140 " Rejecting split of %d(%s): unsuitable mode %s\n",
6141 original_regno,
6142 reg_class_names[lra_get_allocno_class (original_regno)],
6143 GET_MODE_NAME (mode));
6144 fprintf
6145 (lra_dump_file,
6146 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6148 return false;
6150 new_reg = lra_create_new_reg (mode, original_reg, rclass, NULL, "split");
6151 reg_renumber[REGNO (new_reg)] = hard_regno;
6153 int new_regno = REGNO (new_reg);
6154 save = emit_spill_move (true, new_reg, original_reg);
6155 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
6157 if (lra_dump_file != NULL)
6159 fprintf
6160 (lra_dump_file,
6161 " Rejecting split %d->%d resulting in > 2 save insns:\n",
6162 original_regno, new_regno);
6163 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
6164 fprintf (lra_dump_file,
6165 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6167 return false;
6169 restore = emit_spill_move (false, new_reg, original_reg);
6170 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
6172 if (lra_dump_file != NULL)
6174 fprintf (lra_dump_file,
6175 " Rejecting split %d->%d "
6176 "resulting in > 2 restore insns:\n",
6177 original_regno, new_regno);
6178 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
6179 fprintf (lra_dump_file,
6180 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6182 return false;
6184 /* Transfer equivalence information to the spill register, so that
6185 if we fail to allocate the spill register, we have the option of
6186 rematerializing the original value instead of spilling to the stack. */
6187 if (!HARD_REGISTER_NUM_P (original_regno)
6188 && mode == PSEUDO_REGNO_MODE (original_regno))
6189 lra_copy_reg_equiv (new_regno, original_regno, call_save_p);
6190 lra_reg_info[new_regno].restore_rtx = regno_reg_rtx[original_regno];
6191 bitmap_set_bit (&lra_split_regs, new_regno);
6192 if (to != NULL)
6194 lra_assert (next_usage_insns == NULL);
6195 usage_insn = to;
6196 after_p = true;
6198 else
6200 /* We need check_only_regs only inside the inheritance pass. */
6201 bitmap_set_bit (&check_only_regs, new_regno);
6202 bitmap_set_bit (&check_only_regs, original_regno);
6203 after_p = usage_insns[original_regno].after_p;
6204 for (;;)
6206 if (GET_CODE (next_usage_insns) != INSN_LIST)
6208 usage_insn = next_usage_insns;
6209 break;
6211 usage_insn = XEXP (next_usage_insns, 0);
6212 lra_assert (DEBUG_INSN_P (usage_insn));
6213 next_usage_insns = XEXP (next_usage_insns, 1);
6214 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false,
6215 true);
6216 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
6217 if (lra_dump_file != NULL)
6219 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
6220 original_regno, new_regno);
6221 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
6225 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
6226 lra_assert (usage_insn != insn || (after_p && before_p));
6227 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
6228 after_p ? NULL : restore,
6229 after_p ? restore : NULL,
6230 call_save_p
6231 ? "Add reg<-save" : "Add reg<-split");
6232 lra_process_new_insns (insn, before_p ? save : NULL,
6233 before_p ? NULL : save,
6234 call_save_p
6235 ? "Add save<-reg" : "Add split<-reg");
6236 if (nregs > 1 || original_regno < FIRST_PSEUDO_REGISTER)
6237 /* If we are trying to split multi-register. We should check
6238 conflicts on the next assignment sub-pass. IRA can allocate on
6239 sub-register levels, LRA do this on pseudos level right now and
6240 this discrepancy may create allocation conflicts after
6241 splitting.
6243 If we are trying to split hard register we should also check conflicts
6244 as such splitting can create artificial conflict of the hard register
6245 with another pseudo because of simplified conflict calculation in
6246 LRA. */
6247 check_and_force_assignment_correctness_p = true;
6248 if (lra_dump_file != NULL)
6249 fprintf (lra_dump_file,
6250 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6251 return true;
6254 /* Split a hard reg for reload pseudo REGNO having RCLASS and living
6255 in the range [FROM, TO]. Return true if did a split. Otherwise,
6256 return false. */
6257 bool
6258 spill_hard_reg_in_range (int regno, enum reg_class rclass, rtx_insn *from, rtx_insn *to)
6260 int i, hard_regno;
6261 int rclass_size;
6262 rtx_insn *insn;
6263 unsigned int uid;
6264 bitmap_iterator bi;
6265 HARD_REG_SET ignore;
6267 lra_assert (from != NULL && to != NULL);
6268 ignore = lra_no_alloc_regs;
6269 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
6271 lra_insn_recog_data_t id = lra_insn_recog_data[uid];
6272 struct lra_static_insn_data *static_id = id->insn_static_data;
6273 struct lra_insn_reg *reg;
6275 for (reg = id->regs; reg != NULL; reg = reg->next)
6276 if (reg->regno < FIRST_PSEUDO_REGISTER)
6277 SET_HARD_REG_BIT (ignore, reg->regno);
6278 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
6279 SET_HARD_REG_BIT (ignore, reg->regno);
6281 rclass_size = ira_class_hard_regs_num[rclass];
6282 for (i = 0; i < rclass_size; i++)
6284 hard_regno = ira_class_hard_regs[rclass][i];
6285 if (! TEST_HARD_REG_BIT (lra_reg_info[regno].conflict_hard_regs, hard_regno)
6286 || TEST_HARD_REG_BIT (ignore, hard_regno))
6287 continue;
6288 for (insn = from; insn != NEXT_INSN (to); insn = NEXT_INSN (insn))
6290 struct lra_static_insn_data *static_id;
6291 struct lra_insn_reg *reg;
6293 if (!INSN_P (insn))
6294 continue;
6295 if (bitmap_bit_p (&lra_reg_info[hard_regno].insn_bitmap,
6296 INSN_UID (insn)))
6297 break;
6298 static_id = lra_get_insn_recog_data (insn)->insn_static_data;
6299 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
6300 if (reg->regno == hard_regno)
6301 break;
6302 if (reg != NULL)
6303 break;
6305 if (insn != NEXT_INSN (to))
6306 continue;
6307 if (split_reg (true, hard_regno, from, NULL, to))
6308 return true;
6310 return false;
6313 /* Recognize that we need a split transformation for insn INSN, which
6314 defines or uses REGNO in its insn biggest MODE (we use it only if
6315 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
6316 hard registers which might be used for reloads since the EBB end.
6317 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
6318 uid before starting INSN processing. Return true if we succeed in
6319 such transformation. */
6320 static bool
6321 split_if_necessary (int regno, machine_mode mode,
6322 HARD_REG_SET potential_reload_hard_regs,
6323 bool before_p, rtx_insn *insn, int max_uid)
6325 bool res = false;
6326 int i, nregs = 1;
6327 rtx next_usage_insns;
6329 if (regno < FIRST_PSEUDO_REGISTER)
6330 nregs = hard_regno_nregs (regno, mode);
6331 for (i = 0; i < nregs; i++)
6332 if (usage_insns[regno + i].check == curr_usage_insns_check
6333 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
6334 /* To avoid processing the register twice or more. */
6335 && ((GET_CODE (next_usage_insns) != INSN_LIST
6336 && INSN_UID (next_usage_insns) < max_uid)
6337 || (GET_CODE (next_usage_insns) == INSN_LIST
6338 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
6339 && need_for_split_p (potential_reload_hard_regs, regno + i)
6340 && split_reg (before_p, regno + i, insn, next_usage_insns, NULL))
6341 res = true;
6342 return res;
6345 /* Return TRUE if rtx X is considered as an invariant for
6346 inheritance. */
6347 static bool
6348 invariant_p (const_rtx x)
6350 machine_mode mode;
6351 const char *fmt;
6352 enum rtx_code code;
6353 int i, j;
6355 if (side_effects_p (x))
6356 return false;
6358 code = GET_CODE (x);
6359 mode = GET_MODE (x);
6360 if (code == SUBREG)
6362 x = SUBREG_REG (x);
6363 code = GET_CODE (x);
6364 mode = wider_subreg_mode (mode, GET_MODE (x));
6367 if (MEM_P (x))
6368 return false;
6370 if (REG_P (x))
6372 int i, nregs, regno = REGNO (x);
6374 if (regno >= FIRST_PSEUDO_REGISTER || regno == STACK_POINTER_REGNUM
6375 || TEST_HARD_REG_BIT (eliminable_regset, regno)
6376 || GET_MODE_CLASS (GET_MODE (x)) == MODE_CC)
6377 return false;
6378 nregs = hard_regno_nregs (regno, mode);
6379 for (i = 0; i < nregs; i++)
6380 if (! fixed_regs[regno + i]
6381 /* A hard register may be clobbered in the current insn
6382 but we can ignore this case because if the hard
6383 register is used it should be set somewhere after the
6384 clobber. */
6385 || bitmap_bit_p (&invalid_invariant_regs, regno + i))
6386 return false;
6388 fmt = GET_RTX_FORMAT (code);
6389 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6391 if (fmt[i] == 'e')
6393 if (! invariant_p (XEXP (x, i)))
6394 return false;
6396 else if (fmt[i] == 'E')
6398 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6399 if (! invariant_p (XVECEXP (x, i, j)))
6400 return false;
6403 return true;
6406 /* We have 'dest_reg <- invariant'. Let us try to make an invariant
6407 inheritance transformation (using dest_reg instead invariant in a
6408 subsequent insn). */
6409 static bool
6410 process_invariant_for_inheritance (rtx dst_reg, rtx invariant_rtx)
6412 invariant_ptr_t invariant_ptr;
6413 rtx_insn *insn, *new_insns;
6414 rtx insn_set, insn_reg, new_reg;
6415 int insn_regno;
6416 bool succ_p = false;
6417 int dst_regno = REGNO (dst_reg);
6418 machine_mode dst_mode = GET_MODE (dst_reg);
6419 enum reg_class cl = lra_get_allocno_class (dst_regno), insn_reg_cl;
6421 invariant_ptr = insert_invariant (invariant_rtx);
6422 if ((insn = invariant_ptr->insn) != NULL_RTX)
6424 /* We have a subsequent insn using the invariant. */
6425 insn_set = single_set (insn);
6426 lra_assert (insn_set != NULL);
6427 insn_reg = SET_DEST (insn_set);
6428 lra_assert (REG_P (insn_reg));
6429 insn_regno = REGNO (insn_reg);
6430 insn_reg_cl = lra_get_allocno_class (insn_regno);
6432 if (dst_mode == GET_MODE (insn_reg)
6433 /* We should consider only result move reg insns which are
6434 cheap. */
6435 && targetm.register_move_cost (dst_mode, cl, insn_reg_cl) == 2
6436 && targetm.register_move_cost (dst_mode, cl, cl) == 2)
6438 if (lra_dump_file != NULL)
6439 fprintf (lra_dump_file,
6440 " [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[\n");
6441 new_reg = lra_create_new_reg (dst_mode, dst_reg, cl, NULL,
6442 "invariant inheritance");
6443 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
6444 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
6445 lra_reg_info[REGNO (new_reg)].restore_rtx = PATTERN (insn);
6446 start_sequence ();
6447 lra_emit_move (new_reg, dst_reg);
6448 new_insns = get_insns ();
6449 end_sequence ();
6450 lra_process_new_insns (curr_insn, NULL, new_insns,
6451 "Add invariant inheritance<-original");
6452 start_sequence ();
6453 lra_emit_move (SET_DEST (insn_set), new_reg);
6454 new_insns = get_insns ();
6455 end_sequence ();
6456 lra_process_new_insns (insn, NULL, new_insns,
6457 "Changing reload<-inheritance");
6458 lra_set_insn_deleted (insn);
6459 succ_p = true;
6460 if (lra_dump_file != NULL)
6462 fprintf (lra_dump_file,
6463 " Invariant inheritance reuse change %d (bb%d):\n",
6464 REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
6465 dump_insn_slim (lra_dump_file, insn);
6466 fprintf (lra_dump_file,
6467 " ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]\n");
6471 invariant_ptr->insn = curr_insn;
6472 return succ_p;
6475 /* Check only registers living at the current program point in the
6476 current EBB. */
6477 static bitmap_head live_regs;
6479 /* Update live info in EBB given by its HEAD and TAIL insns after
6480 inheritance/split transformation. The function removes dead moves
6481 too. */
6482 static void
6483 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
6485 unsigned int j;
6486 int i, regno;
6487 bool live_p;
6488 rtx_insn *prev_insn;
6489 rtx set;
6490 bool remove_p;
6491 basic_block last_bb, prev_bb, curr_bb;
6492 bitmap_iterator bi;
6493 struct lra_insn_reg *reg;
6494 edge e;
6495 edge_iterator ei;
6497 last_bb = BLOCK_FOR_INSN (tail);
6498 prev_bb = NULL;
6499 for (curr_insn = tail;
6500 curr_insn != PREV_INSN (head);
6501 curr_insn = prev_insn)
6503 prev_insn = PREV_INSN (curr_insn);
6504 /* We need to process empty blocks too. They contain
6505 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
6506 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
6507 continue;
6508 curr_bb = BLOCK_FOR_INSN (curr_insn);
6509 if (curr_bb != prev_bb)
6511 if (prev_bb != NULL)
6513 /* Update df_get_live_in (prev_bb): */
6514 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
6515 if (bitmap_bit_p (&live_regs, j))
6516 bitmap_set_bit (df_get_live_in (prev_bb), j);
6517 else
6518 bitmap_clear_bit (df_get_live_in (prev_bb), j);
6520 if (curr_bb != last_bb)
6522 /* Update df_get_live_out (curr_bb): */
6523 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
6525 live_p = bitmap_bit_p (&live_regs, j);
6526 if (! live_p)
6527 FOR_EACH_EDGE (e, ei, curr_bb->succs)
6528 if (bitmap_bit_p (df_get_live_in (e->dest), j))
6530 live_p = true;
6531 break;
6533 if (live_p)
6534 bitmap_set_bit (df_get_live_out (curr_bb), j);
6535 else
6536 bitmap_clear_bit (df_get_live_out (curr_bb), j);
6539 prev_bb = curr_bb;
6540 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
6542 if (! NONDEBUG_INSN_P (curr_insn))
6543 continue;
6544 curr_id = lra_get_insn_recog_data (curr_insn);
6545 curr_static_id = curr_id->insn_static_data;
6546 remove_p = false;
6547 if ((set = single_set (curr_insn)) != NULL_RTX
6548 && REG_P (SET_DEST (set))
6549 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
6550 && SET_DEST (set) != pic_offset_table_rtx
6551 && bitmap_bit_p (&check_only_regs, regno)
6552 && ! bitmap_bit_p (&live_regs, regno))
6553 remove_p = true;
6554 /* See which defined values die here. */
6555 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6556 if (reg->type == OP_OUT && ! reg->subreg_p)
6557 bitmap_clear_bit (&live_regs, reg->regno);
6558 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6559 if (reg->type == OP_OUT && ! reg->subreg_p)
6560 bitmap_clear_bit (&live_regs, reg->regno);
6561 if (curr_id->arg_hard_regs != NULL)
6562 /* Make clobbered argument hard registers die. */
6563 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6564 if (regno >= FIRST_PSEUDO_REGISTER)
6565 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
6566 /* Mark each used value as live. */
6567 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6568 if (reg->type != OP_OUT
6569 && bitmap_bit_p (&check_only_regs, reg->regno))
6570 bitmap_set_bit (&live_regs, reg->regno);
6571 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6572 if (reg->type != OP_OUT
6573 && bitmap_bit_p (&check_only_regs, reg->regno))
6574 bitmap_set_bit (&live_regs, reg->regno);
6575 if (curr_id->arg_hard_regs != NULL)
6576 /* Make used argument hard registers live. */
6577 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6578 if (regno < FIRST_PSEUDO_REGISTER
6579 && bitmap_bit_p (&check_only_regs, regno))
6580 bitmap_set_bit (&live_regs, regno);
6581 /* It is quite important to remove dead move insns because it
6582 means removing dead store. We don't need to process them for
6583 constraints. */
6584 if (remove_p)
6586 if (lra_dump_file != NULL)
6588 fprintf (lra_dump_file, " Removing dead insn:\n ");
6589 dump_insn_slim (lra_dump_file, curr_insn);
6591 lra_set_insn_deleted (curr_insn);
6596 /* The structure describes info to do an inheritance for the current
6597 insn. We need to collect such info first before doing the
6598 transformations because the transformations change the insn
6599 internal representation. */
6600 struct to_inherit
6602 /* Original regno. */
6603 int regno;
6604 /* Subsequent insns which can inherit original reg value. */
6605 rtx insns;
6608 /* Array containing all info for doing inheritance from the current
6609 insn. */
6610 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
6612 /* Number elements in the previous array. */
6613 static int to_inherit_num;
6615 /* Add inheritance info REGNO and INSNS. Their meaning is described in
6616 structure to_inherit. */
6617 static void
6618 add_to_inherit (int regno, rtx insns)
6620 int i;
6622 for (i = 0; i < to_inherit_num; i++)
6623 if (to_inherit[i].regno == regno)
6624 return;
6625 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
6626 to_inherit[to_inherit_num].regno = regno;
6627 to_inherit[to_inherit_num++].insns = insns;
6630 /* Return the last non-debug insn in basic block BB, or the block begin
6631 note if none. */
6632 static rtx_insn *
6633 get_last_insertion_point (basic_block bb)
6635 rtx_insn *insn;
6637 FOR_BB_INSNS_REVERSE (bb, insn)
6638 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
6639 return insn;
6640 gcc_unreachable ();
6643 /* Set up RES by registers living on edges FROM except the edge (FROM,
6644 TO) or by registers set up in a jump insn in BB FROM. */
6645 static void
6646 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
6648 rtx_insn *last;
6649 struct lra_insn_reg *reg;
6650 edge e;
6651 edge_iterator ei;
6653 lra_assert (to != NULL);
6654 bitmap_clear (res);
6655 FOR_EACH_EDGE (e, ei, from->succs)
6656 if (e->dest != to)
6657 bitmap_ior_into (res, df_get_live_in (e->dest));
6658 last = get_last_insertion_point (from);
6659 if (! JUMP_P (last))
6660 return;
6661 curr_id = lra_get_insn_recog_data (last);
6662 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6663 if (reg->type != OP_IN)
6664 bitmap_set_bit (res, reg->regno);
6667 /* Used as a temporary results of some bitmap calculations. */
6668 static bitmap_head temp_bitmap;
6670 /* We split for reloads of small class of hard regs. The following
6671 defines how many hard regs the class should have to be qualified as
6672 small. The code is mostly oriented to x86/x86-64 architecture
6673 where some insns need to use only specific register or pair of
6674 registers and these register can live in RTL explicitly, e.g. for
6675 parameter passing. */
6676 static const int max_small_class_regs_num = 2;
6678 /* Do inheritance/split transformations in EBB starting with HEAD and
6679 finishing on TAIL. We process EBB insns in the reverse order.
6680 Return true if we did any inheritance/split transformation in the
6681 EBB.
6683 We should avoid excessive splitting which results in worse code
6684 because of inaccurate cost calculations for spilling new split
6685 pseudos in such case. To achieve this we do splitting only if
6686 register pressure is high in given basic block and there are reload
6687 pseudos requiring hard registers. We could do more register
6688 pressure calculations at any given program point to avoid necessary
6689 splitting even more but it is to expensive and the current approach
6690 works well enough. */
6691 static bool
6692 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
6694 int i, src_regno, dst_regno, nregs;
6695 bool change_p, succ_p, update_reloads_num_p;
6696 rtx_insn *prev_insn, *last_insn;
6697 rtx next_usage_insns, curr_set;
6698 enum reg_class cl;
6699 struct lra_insn_reg *reg;
6700 basic_block last_processed_bb, curr_bb = NULL;
6701 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
6702 bitmap to_process;
6703 unsigned int j;
6704 bitmap_iterator bi;
6705 bool head_p, after_p;
6707 change_p = false;
6708 curr_usage_insns_check++;
6709 clear_invariants ();
6710 reloads_num = calls_num = 0;
6711 for (unsigned int i = 0; i < NUM_ABI_IDS; ++i)
6712 last_call_for_abi[i] = 0;
6713 CLEAR_HARD_REG_SET (full_and_partial_call_clobbers);
6714 bitmap_clear (&check_only_regs);
6715 bitmap_clear (&invalid_invariant_regs);
6716 last_processed_bb = NULL;
6717 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
6718 live_hard_regs = eliminable_regset | lra_no_alloc_regs;
6719 /* We don't process new insns generated in the loop. */
6720 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
6722 prev_insn = PREV_INSN (curr_insn);
6723 if (BLOCK_FOR_INSN (curr_insn) != NULL)
6724 curr_bb = BLOCK_FOR_INSN (curr_insn);
6725 if (last_processed_bb != curr_bb)
6727 /* We are at the end of BB. Add qualified living
6728 pseudos for potential splitting. */
6729 to_process = df_get_live_out (curr_bb);
6730 if (last_processed_bb != NULL)
6732 /* We are somewhere in the middle of EBB. */
6733 get_live_on_other_edges (curr_bb, last_processed_bb,
6734 &temp_bitmap);
6735 to_process = &temp_bitmap;
6737 last_processed_bb = curr_bb;
6738 last_insn = get_last_insertion_point (curr_bb);
6739 after_p = (! JUMP_P (last_insn)
6740 && (! CALL_P (last_insn)
6741 || (find_reg_note (last_insn,
6742 REG_NORETURN, NULL_RTX) == NULL_RTX
6743 && ! SIBLING_CALL_P (last_insn))));
6744 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
6745 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
6747 if ((int) j >= lra_constraint_new_regno_start)
6748 break;
6749 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
6751 if (j < FIRST_PSEUDO_REGISTER)
6752 SET_HARD_REG_BIT (live_hard_regs, j);
6753 else
6754 add_to_hard_reg_set (&live_hard_regs,
6755 PSEUDO_REGNO_MODE (j),
6756 reg_renumber[j]);
6757 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
6761 src_regno = dst_regno = -1;
6762 curr_set = single_set (curr_insn);
6763 if (curr_set != NULL_RTX && REG_P (SET_DEST (curr_set)))
6764 dst_regno = REGNO (SET_DEST (curr_set));
6765 if (curr_set != NULL_RTX && REG_P (SET_SRC (curr_set)))
6766 src_regno = REGNO (SET_SRC (curr_set));
6767 update_reloads_num_p = true;
6768 if (src_regno < lra_constraint_new_regno_start
6769 && src_regno >= FIRST_PSEUDO_REGISTER
6770 && reg_renumber[src_regno] < 0
6771 && dst_regno >= lra_constraint_new_regno_start
6772 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
6774 /* 'reload_pseudo <- original_pseudo'. */
6775 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6776 reloads_num++;
6777 update_reloads_num_p = false;
6778 succ_p = false;
6779 if (usage_insns[src_regno].check == curr_usage_insns_check
6780 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
6781 succ_p = inherit_reload_reg (false, src_regno, cl,
6782 curr_insn, next_usage_insns);
6783 if (succ_p)
6784 change_p = true;
6785 else
6786 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
6787 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6788 potential_reload_hard_regs |= reg_class_contents[cl];
6790 else if (src_regno < 0
6791 && dst_regno >= lra_constraint_new_regno_start
6792 && invariant_p (SET_SRC (curr_set))
6793 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS
6794 && ! bitmap_bit_p (&invalid_invariant_regs, dst_regno)
6795 && ! bitmap_bit_p (&invalid_invariant_regs,
6796 ORIGINAL_REGNO(regno_reg_rtx[dst_regno])))
6798 /* 'reload_pseudo <- invariant'. */
6799 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6800 reloads_num++;
6801 update_reloads_num_p = false;
6802 if (process_invariant_for_inheritance (SET_DEST (curr_set), SET_SRC (curr_set)))
6803 change_p = true;
6804 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6805 potential_reload_hard_regs |= reg_class_contents[cl];
6807 else if (src_regno >= lra_constraint_new_regno_start
6808 && dst_regno < lra_constraint_new_regno_start
6809 && dst_regno >= FIRST_PSEUDO_REGISTER
6810 && reg_renumber[dst_regno] < 0
6811 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
6812 && usage_insns[dst_regno].check == curr_usage_insns_check
6813 && (next_usage_insns
6814 = usage_insns[dst_regno].insns) != NULL_RTX)
6816 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6817 reloads_num++;
6818 update_reloads_num_p = false;
6819 /* 'original_pseudo <- reload_pseudo'. */
6820 if (! JUMP_P (curr_insn)
6821 && inherit_reload_reg (true, dst_regno, cl,
6822 curr_insn, next_usage_insns))
6823 change_p = true;
6824 /* Invalidate. */
6825 usage_insns[dst_regno].check = 0;
6826 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6827 potential_reload_hard_regs |= reg_class_contents[cl];
6829 else if (INSN_P (curr_insn))
6831 int iter;
6832 int max_uid = get_max_uid ();
6834 curr_id = lra_get_insn_recog_data (curr_insn);
6835 curr_static_id = curr_id->insn_static_data;
6836 to_inherit_num = 0;
6837 /* Process insn definitions. */
6838 for (iter = 0; iter < 2; iter++)
6839 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
6840 reg != NULL;
6841 reg = reg->next)
6842 if (reg->type != OP_IN
6843 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
6845 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
6846 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
6847 && usage_insns[dst_regno].check == curr_usage_insns_check
6848 && (next_usage_insns
6849 = usage_insns[dst_regno].insns) != NULL_RTX)
6851 struct lra_insn_reg *r;
6853 for (r = curr_id->regs; r != NULL; r = r->next)
6854 if (r->type != OP_OUT && r->regno == dst_regno)
6855 break;
6856 /* Don't do inheritance if the pseudo is also
6857 used in the insn. */
6858 if (r == NULL)
6859 /* We cannot do inheritance right now
6860 because the current insn reg info (chain
6861 regs) can change after that. */
6862 add_to_inherit (dst_regno, next_usage_insns);
6864 /* We cannot process one reg twice here because of
6865 usage_insns invalidation. */
6866 if ((dst_regno < FIRST_PSEUDO_REGISTER
6867 || reg_renumber[dst_regno] >= 0)
6868 && ! reg->subreg_p && reg->type != OP_IN)
6870 HARD_REG_SET s;
6872 if (split_if_necessary (dst_regno, reg->biggest_mode,
6873 potential_reload_hard_regs,
6874 false, curr_insn, max_uid))
6875 change_p = true;
6876 CLEAR_HARD_REG_SET (s);
6877 if (dst_regno < FIRST_PSEUDO_REGISTER)
6878 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
6879 else
6880 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
6881 reg_renumber[dst_regno]);
6882 live_hard_regs &= ~s;
6883 potential_reload_hard_regs &= ~s;
6885 /* We should invalidate potential inheritance or
6886 splitting for the current insn usages to the next
6887 usage insns (see code below) as the output pseudo
6888 prevents this. */
6889 if ((dst_regno >= FIRST_PSEUDO_REGISTER
6890 && reg_renumber[dst_regno] < 0)
6891 || (reg->type == OP_OUT && ! reg->subreg_p
6892 && (dst_regno < FIRST_PSEUDO_REGISTER
6893 || reg_renumber[dst_regno] >= 0)))
6895 /* Invalidate and mark definitions. */
6896 if (dst_regno >= FIRST_PSEUDO_REGISTER)
6897 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
6898 else
6900 nregs = hard_regno_nregs (dst_regno,
6901 reg->biggest_mode);
6902 for (i = 0; i < nregs; i++)
6903 usage_insns[dst_regno + i].check
6904 = -(int) INSN_UID (curr_insn);
6908 /* Process clobbered call regs. */
6909 if (curr_id->arg_hard_regs != NULL)
6910 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6911 if (dst_regno >= FIRST_PSEUDO_REGISTER)
6912 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
6913 = -(int) INSN_UID (curr_insn);
6914 if (! JUMP_P (curr_insn))
6915 for (i = 0; i < to_inherit_num; i++)
6916 if (inherit_reload_reg (true, to_inherit[i].regno,
6917 ALL_REGS, curr_insn,
6918 to_inherit[i].insns))
6919 change_p = true;
6920 if (CALL_P (curr_insn))
6922 rtx cheap, pat, dest;
6923 rtx_insn *restore;
6924 int regno, hard_regno;
6926 calls_num++;
6927 function_abi callee_abi = insn_callee_abi (curr_insn);
6928 last_call_for_abi[callee_abi.id ()] = calls_num;
6929 full_and_partial_call_clobbers
6930 |= callee_abi.full_and_partial_reg_clobbers ();
6931 if ((cheap = find_reg_note (curr_insn,
6932 REG_RETURNED, NULL_RTX)) != NULL_RTX
6933 && ((cheap = XEXP (cheap, 0)), true)
6934 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
6935 && (hard_regno = reg_renumber[regno]) >= 0
6936 && usage_insns[regno].check == curr_usage_insns_check
6937 /* If there are pending saves/restores, the
6938 optimization is not worth. */
6939 && usage_insns[regno].calls_num == calls_num - 1
6940 && callee_abi.clobbers_reg_p (GET_MODE (cheap), hard_regno))
6942 /* Restore the pseudo from the call result as
6943 REG_RETURNED note says that the pseudo value is
6944 in the call result and the pseudo is an argument
6945 of the call. */
6946 pat = PATTERN (curr_insn);
6947 if (GET_CODE (pat) == PARALLEL)
6948 pat = XVECEXP (pat, 0, 0);
6949 dest = SET_DEST (pat);
6950 /* For multiple return values dest is PARALLEL.
6951 Currently we handle only single return value case. */
6952 if (REG_P (dest))
6954 start_sequence ();
6955 emit_move_insn (cheap, copy_rtx (dest));
6956 restore = get_insns ();
6957 end_sequence ();
6958 lra_process_new_insns (curr_insn, NULL, restore,
6959 "Inserting call parameter restore");
6960 /* We don't need to save/restore of the pseudo from
6961 this call. */
6962 usage_insns[regno].calls_num = calls_num;
6963 remove_from_hard_reg_set
6964 (&full_and_partial_call_clobbers,
6965 GET_MODE (cheap), hard_regno);
6966 bitmap_set_bit (&check_only_regs, regno);
6970 to_inherit_num = 0;
6971 /* Process insn usages. */
6972 for (iter = 0; iter < 2; iter++)
6973 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
6974 reg != NULL;
6975 reg = reg->next)
6976 if ((reg->type != OP_OUT
6977 || (reg->type == OP_OUT && reg->subreg_p))
6978 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
6980 if (src_regno >= FIRST_PSEUDO_REGISTER
6981 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
6983 if (usage_insns[src_regno].check == curr_usage_insns_check
6984 && (next_usage_insns
6985 = usage_insns[src_regno].insns) != NULL_RTX
6986 && NONDEBUG_INSN_P (curr_insn))
6987 add_to_inherit (src_regno, next_usage_insns);
6988 else if (usage_insns[src_regno].check
6989 != -(int) INSN_UID (curr_insn))
6990 /* Add usages but only if the reg is not set up
6991 in the same insn. */
6992 add_next_usage_insn (src_regno, curr_insn, reloads_num);
6994 else if (src_regno < FIRST_PSEUDO_REGISTER
6995 || reg_renumber[src_regno] >= 0)
6997 bool before_p;
6998 rtx_insn *use_insn = curr_insn;
7000 before_p = (JUMP_P (curr_insn)
7001 || (CALL_P (curr_insn) && reg->type == OP_IN));
7002 if (NONDEBUG_INSN_P (curr_insn)
7003 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
7004 && split_if_necessary (src_regno, reg->biggest_mode,
7005 potential_reload_hard_regs,
7006 before_p, curr_insn, max_uid))
7008 if (reg->subreg_p)
7009 check_and_force_assignment_correctness_p = true;
7010 change_p = true;
7011 /* Invalidate. */
7012 usage_insns[src_regno].check = 0;
7013 if (before_p)
7014 use_insn = PREV_INSN (curr_insn);
7016 if (NONDEBUG_INSN_P (curr_insn))
7018 if (src_regno < FIRST_PSEUDO_REGISTER)
7019 add_to_hard_reg_set (&live_hard_regs,
7020 reg->biggest_mode, src_regno);
7021 else
7022 add_to_hard_reg_set (&live_hard_regs,
7023 PSEUDO_REGNO_MODE (src_regno),
7024 reg_renumber[src_regno]);
7026 if (src_regno >= FIRST_PSEUDO_REGISTER)
7027 add_next_usage_insn (src_regno, use_insn, reloads_num);
7028 else
7030 for (i = 0; i < hard_regno_nregs (src_regno, reg->biggest_mode); i++)
7031 add_next_usage_insn (src_regno + i, use_insn, reloads_num);
7035 /* Process used call regs. */
7036 if (curr_id->arg_hard_regs != NULL)
7037 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
7038 if (src_regno < FIRST_PSEUDO_REGISTER)
7040 SET_HARD_REG_BIT (live_hard_regs, src_regno);
7041 add_next_usage_insn (src_regno, curr_insn, reloads_num);
7043 for (i = 0; i < to_inherit_num; i++)
7045 src_regno = to_inherit[i].regno;
7046 if (inherit_reload_reg (false, src_regno, ALL_REGS,
7047 curr_insn, to_inherit[i].insns))
7048 change_p = true;
7049 else
7050 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
7053 if (update_reloads_num_p
7054 && NONDEBUG_INSN_P (curr_insn) && curr_set != NULL_RTX)
7056 int regno = -1;
7057 if ((REG_P (SET_DEST (curr_set))
7058 && (regno = REGNO (SET_DEST (curr_set))) >= lra_constraint_new_regno_start
7059 && reg_renumber[regno] < 0
7060 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
7061 || (REG_P (SET_SRC (curr_set))
7062 && (regno = REGNO (SET_SRC (curr_set))) >= lra_constraint_new_regno_start
7063 && reg_renumber[regno] < 0
7064 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
7066 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
7067 reloads_num++;
7068 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
7069 potential_reload_hard_regs |= reg_class_contents[cl];
7072 if (NONDEBUG_INSN_P (curr_insn))
7074 int regno;
7076 /* Invalidate invariants with changed regs. */
7077 curr_id = lra_get_insn_recog_data (curr_insn);
7078 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
7079 if (reg->type != OP_IN)
7081 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
7082 bitmap_set_bit (&invalid_invariant_regs,
7083 ORIGINAL_REGNO (regno_reg_rtx[reg->regno]));
7085 curr_static_id = curr_id->insn_static_data;
7086 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
7087 if (reg->type != OP_IN)
7088 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
7089 if (curr_id->arg_hard_regs != NULL)
7090 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
7091 if (regno >= FIRST_PSEUDO_REGISTER)
7092 bitmap_set_bit (&invalid_invariant_regs,
7093 regno - FIRST_PSEUDO_REGISTER);
7095 /* We reached the start of the current basic block. */
7096 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
7097 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
7099 /* We reached the beginning of the current block -- do
7100 rest of spliting in the current BB. */
7101 to_process = df_get_live_in (curr_bb);
7102 if (BLOCK_FOR_INSN (head) != curr_bb)
7104 /* We are somewhere in the middle of EBB. */
7105 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
7106 curr_bb, &temp_bitmap);
7107 to_process = &temp_bitmap;
7109 head_p = true;
7110 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
7112 if ((int) j >= lra_constraint_new_regno_start)
7113 break;
7114 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
7115 && usage_insns[j].check == curr_usage_insns_check
7116 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
7118 if (need_for_split_p (potential_reload_hard_regs, j))
7120 if (lra_dump_file != NULL && head_p)
7122 fprintf (lra_dump_file,
7123 " ----------------------------------\n");
7124 head_p = false;
7126 if (split_reg (false, j, bb_note (curr_bb),
7127 next_usage_insns, NULL))
7128 change_p = true;
7130 usage_insns[j].check = 0;
7135 return change_p;
7138 /* This value affects EBB forming. If probability of edge from EBB to
7139 a BB is not greater than the following value, we don't add the BB
7140 to EBB. */
7141 #define EBB_PROBABILITY_CUTOFF \
7142 ((REG_BR_PROB_BASE * param_lra_inheritance_ebb_probability_cutoff) / 100)
7144 /* Current number of inheritance/split iteration. */
7145 int lra_inheritance_iter;
7147 /* Entry function for inheritance/split pass. */
7148 void
7149 lra_inheritance (void)
7151 int i;
7152 basic_block bb, start_bb;
7153 edge e;
7155 lra_inheritance_iter++;
7156 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
7157 return;
7158 timevar_push (TV_LRA_INHERITANCE);
7159 if (lra_dump_file != NULL)
7160 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
7161 lra_inheritance_iter);
7162 curr_usage_insns_check = 0;
7163 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
7164 for (i = 0; i < lra_constraint_new_regno_start; i++)
7165 usage_insns[i].check = 0;
7166 bitmap_initialize (&check_only_regs, &reg_obstack);
7167 bitmap_initialize (&invalid_invariant_regs, &reg_obstack);
7168 bitmap_initialize (&live_regs, &reg_obstack);
7169 bitmap_initialize (&temp_bitmap, &reg_obstack);
7170 bitmap_initialize (&ebb_global_regs, &reg_obstack);
7171 FOR_EACH_BB_FN (bb, cfun)
7173 start_bb = bb;
7174 if (lra_dump_file != NULL)
7175 fprintf (lra_dump_file, "EBB");
7176 /* Form a EBB starting with BB. */
7177 bitmap_clear (&ebb_global_regs);
7178 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
7179 for (;;)
7181 if (lra_dump_file != NULL)
7182 fprintf (lra_dump_file, " %d", bb->index);
7183 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
7184 || LABEL_P (BB_HEAD (bb->next_bb)))
7185 break;
7186 e = find_fallthru_edge (bb->succs);
7187 if (! e)
7188 break;
7189 if (e->probability.initialized_p ()
7190 && e->probability.to_reg_br_prob_base () < EBB_PROBABILITY_CUTOFF)
7191 break;
7192 bb = bb->next_bb;
7194 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
7195 if (lra_dump_file != NULL)
7196 fprintf (lra_dump_file, "\n");
7197 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
7198 /* Remember that the EBB head and tail can change in
7199 inherit_in_ebb. */
7200 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
7202 bitmap_release (&ebb_global_regs);
7203 bitmap_release (&temp_bitmap);
7204 bitmap_release (&live_regs);
7205 bitmap_release (&invalid_invariant_regs);
7206 bitmap_release (&check_only_regs);
7207 free (usage_insns);
7208 lra_dump_insns_if_possible ("func after inheritance");
7209 timevar_pop (TV_LRA_INHERITANCE);
7214 /* This page contains code to undo failed inheritance/split
7215 transformations. */
7217 /* Current number of iteration undoing inheritance/split. */
7218 int lra_undo_inheritance_iter;
7220 /* Fix BB live info LIVE after removing pseudos created on pass doing
7221 inheritance/split which are REMOVED_PSEUDOS. */
7222 static void
7223 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
7225 unsigned int regno;
7226 bitmap_iterator bi;
7228 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
7229 if (bitmap_clear_bit (live, regno)
7230 && REG_P (lra_reg_info[regno].restore_rtx))
7231 bitmap_set_bit (live, REGNO (lra_reg_info[regno].restore_rtx));
7234 /* Return regno of the (subreg of) REG. Otherwise, return a negative
7235 number. */
7236 static int
7237 get_regno (rtx reg)
7239 if (GET_CODE (reg) == SUBREG)
7240 reg = SUBREG_REG (reg);
7241 if (REG_P (reg))
7242 return REGNO (reg);
7243 return -1;
7246 /* Delete a move INSN with destination reg DREGNO and a previous
7247 clobber insn with the same regno. The inheritance/split code can
7248 generate moves with preceding clobber and when we delete such moves
7249 we should delete the clobber insn too to keep the correct life
7250 info. */
7251 static void
7252 delete_move_and_clobber (rtx_insn *insn, int dregno)
7254 rtx_insn *prev_insn = PREV_INSN (insn);
7256 lra_set_insn_deleted (insn);
7257 lra_assert (dregno >= 0);
7258 if (prev_insn != NULL && NONDEBUG_INSN_P (prev_insn)
7259 && GET_CODE (PATTERN (prev_insn)) == CLOBBER
7260 && dregno == get_regno (XEXP (PATTERN (prev_insn), 0)))
7261 lra_set_insn_deleted (prev_insn);
7264 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
7265 return true if we did any change. The undo transformations for
7266 inheritance looks like
7267 i <- i2
7268 p <- i => p <- i2
7269 or removing
7270 p <- i, i <- p, and i <- i3
7271 where p is original pseudo from which inheritance pseudo i was
7272 created, i and i3 are removed inheritance pseudos, i2 is another
7273 not removed inheritance pseudo. All split pseudos or other
7274 occurrences of removed inheritance pseudos are changed on the
7275 corresponding original pseudos.
7277 The function also schedules insns changed and created during
7278 inheritance/split pass for processing by the subsequent constraint
7279 pass. */
7280 static bool
7281 remove_inheritance_pseudos (bitmap remove_pseudos)
7283 basic_block bb;
7284 int regno, sregno, prev_sregno, dregno;
7285 rtx restore_rtx;
7286 rtx set, prev_set;
7287 rtx_insn *prev_insn;
7288 bool change_p, done_p;
7290 change_p = ! bitmap_empty_p (remove_pseudos);
7291 /* We cannot finish the function right away if CHANGE_P is true
7292 because we need to marks insns affected by previous
7293 inheritance/split pass for processing by the subsequent
7294 constraint pass. */
7295 FOR_EACH_BB_FN (bb, cfun)
7297 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
7298 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
7299 FOR_BB_INSNS_REVERSE (bb, curr_insn)
7301 if (! INSN_P (curr_insn))
7302 continue;
7303 done_p = false;
7304 sregno = dregno = -1;
7305 if (change_p && NONDEBUG_INSN_P (curr_insn)
7306 && (set = single_set (curr_insn)) != NULL_RTX)
7308 dregno = get_regno (SET_DEST (set));
7309 sregno = get_regno (SET_SRC (set));
7312 if (sregno >= 0 && dregno >= 0)
7314 if (bitmap_bit_p (remove_pseudos, dregno)
7315 && ! REG_P (lra_reg_info[dregno].restore_rtx))
7317 /* invariant inheritance pseudo <- original pseudo */
7318 if (lra_dump_file != NULL)
7320 fprintf (lra_dump_file, " Removing invariant inheritance:\n");
7321 dump_insn_slim (lra_dump_file, curr_insn);
7322 fprintf (lra_dump_file, "\n");
7324 delete_move_and_clobber (curr_insn, dregno);
7325 done_p = true;
7327 else if (bitmap_bit_p (remove_pseudos, sregno)
7328 && ! REG_P (lra_reg_info[sregno].restore_rtx))
7330 /* reload pseudo <- invariant inheritance pseudo */
7331 start_sequence ();
7332 /* We cannot just change the source. It might be
7333 an insn different from the move. */
7334 emit_insn (lra_reg_info[sregno].restore_rtx);
7335 rtx_insn *new_insns = get_insns ();
7336 end_sequence ();
7337 lra_assert (single_set (new_insns) != NULL
7338 && SET_DEST (set) == SET_DEST (single_set (new_insns)));
7339 lra_process_new_insns (curr_insn, NULL, new_insns,
7340 "Changing reload<-invariant inheritance");
7341 delete_move_and_clobber (curr_insn, dregno);
7342 done_p = true;
7344 else if ((bitmap_bit_p (remove_pseudos, sregno)
7345 && (get_regno (lra_reg_info[sregno].restore_rtx) == dregno
7346 || (bitmap_bit_p (remove_pseudos, dregno)
7347 && get_regno (lra_reg_info[sregno].restore_rtx) >= 0
7348 && (get_regno (lra_reg_info[sregno].restore_rtx)
7349 == get_regno (lra_reg_info[dregno].restore_rtx)))))
7350 || (bitmap_bit_p (remove_pseudos, dregno)
7351 && get_regno (lra_reg_info[dregno].restore_rtx) == sregno))
7352 /* One of the following cases:
7353 original <- removed inheritance pseudo
7354 removed inherit pseudo <- another removed inherit pseudo
7355 removed inherit pseudo <- original pseudo
7357 removed_split_pseudo <- original_reg
7358 original_reg <- removed_split_pseudo */
7360 if (lra_dump_file != NULL)
7362 fprintf (lra_dump_file, " Removing %s:\n",
7363 bitmap_bit_p (&lra_split_regs, sregno)
7364 || bitmap_bit_p (&lra_split_regs, dregno)
7365 ? "split" : "inheritance");
7366 dump_insn_slim (lra_dump_file, curr_insn);
7368 delete_move_and_clobber (curr_insn, dregno);
7369 done_p = true;
7371 else if (bitmap_bit_p (remove_pseudos, sregno)
7372 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
7374 /* Search the following pattern:
7375 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
7376 original_pseudo <- inherit_or_split_pseudo1
7377 where the 2nd insn is the current insn and
7378 inherit_or_split_pseudo2 is not removed. If it is found,
7379 change the current insn onto:
7380 original_pseudo <- inherit_or_split_pseudo2. */
7381 for (prev_insn = PREV_INSN (curr_insn);
7382 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
7383 prev_insn = PREV_INSN (prev_insn))
7385 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
7386 && (prev_set = single_set (prev_insn)) != NULL_RTX
7387 /* There should be no subregs in insn we are
7388 searching because only the original reg might
7389 be in subreg when we changed the mode of
7390 load/store for splitting. */
7391 && REG_P (SET_DEST (prev_set))
7392 && REG_P (SET_SRC (prev_set))
7393 && (int) REGNO (SET_DEST (prev_set)) == sregno
7394 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
7395 >= FIRST_PSEUDO_REGISTER)
7396 && (lra_reg_info[prev_sregno].restore_rtx == NULL_RTX
7398 /* As we consider chain of inheritance or
7399 splitting described in above comment we should
7400 check that sregno and prev_sregno were
7401 inheritance/split pseudos created from the
7402 same original regno. */
7403 (get_regno (lra_reg_info[sregno].restore_rtx) >= 0
7404 && (get_regno (lra_reg_info[sregno].restore_rtx)
7405 == get_regno (lra_reg_info[prev_sregno].restore_rtx))))
7406 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
7408 int restore_regno = get_regno (lra_reg_info[sregno].restore_rtx);
7409 if (restore_regno < 0)
7410 restore_regno = prev_sregno;
7411 lra_assert (GET_MODE (SET_SRC (prev_set))
7412 == GET_MODE (regno_reg_rtx[restore_regno]));
7413 /* Although we have a single set, the insn can
7414 contain more one sregno register occurrence
7415 as a source. Change all occurrences. */
7416 lra_substitute_pseudo_within_insn (curr_insn, sregno,
7417 regno_reg_rtx[restore_regno],
7418 false);
7419 /* As we are finishing with processing the insn
7420 here, check the destination too as it might
7421 inheritance pseudo for another pseudo. */
7422 if (bitmap_bit_p (remove_pseudos, dregno)
7423 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
7424 && (restore_rtx
7425 = lra_reg_info[dregno].restore_rtx) != NULL_RTX)
7427 if (GET_CODE (SET_DEST (set)) == SUBREG)
7428 SUBREG_REG (SET_DEST (set)) = restore_rtx;
7429 else
7430 SET_DEST (set) = restore_rtx;
7432 lra_push_insn_and_update_insn_regno_info (curr_insn);
7433 lra_set_used_insn_alternative_by_uid
7434 (INSN_UID (curr_insn), LRA_UNKNOWN_ALT);
7435 done_p = true;
7436 if (lra_dump_file != NULL)
7438 fprintf (lra_dump_file, " Change reload insn:\n");
7439 dump_insn_slim (lra_dump_file, curr_insn);
7444 if (! done_p)
7446 struct lra_insn_reg *reg;
7447 bool restored_regs_p = false;
7448 bool kept_regs_p = false;
7450 curr_id = lra_get_insn_recog_data (curr_insn);
7451 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
7453 regno = reg->regno;
7454 restore_rtx = lra_reg_info[regno].restore_rtx;
7455 if (restore_rtx != NULL_RTX)
7457 if (change_p && bitmap_bit_p (remove_pseudos, regno))
7459 lra_substitute_pseudo_within_insn
7460 (curr_insn, regno, restore_rtx, false);
7461 restored_regs_p = true;
7463 else
7464 kept_regs_p = true;
7467 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
7469 /* The instruction has changed since the previous
7470 constraints pass. */
7471 lra_push_insn_and_update_insn_regno_info (curr_insn);
7472 lra_set_used_insn_alternative_by_uid
7473 (INSN_UID (curr_insn), LRA_UNKNOWN_ALT);
7475 else if (restored_regs_p)
7476 /* The instruction has been restored to the form that
7477 it had during the previous constraints pass. */
7478 lra_update_insn_regno_info (curr_insn);
7479 if (restored_regs_p && lra_dump_file != NULL)
7481 fprintf (lra_dump_file, " Insn after restoring regs:\n");
7482 dump_insn_slim (lra_dump_file, curr_insn);
7487 return change_p;
7490 /* If optional reload pseudos failed to get a hard register or was not
7491 inherited, it is better to remove optional reloads. We do this
7492 transformation after undoing inheritance to figure out necessity to
7493 remove optional reloads easier. Return true if we do any
7494 change. */
7495 static bool
7496 undo_optional_reloads (void)
7498 bool change_p, keep_p;
7499 unsigned int regno, uid;
7500 bitmap_iterator bi, bi2;
7501 rtx_insn *insn;
7502 rtx set, src, dest;
7503 auto_bitmap removed_optional_reload_pseudos (&reg_obstack);
7505 bitmap_copy (removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
7506 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
7508 keep_p = false;
7509 /* Keep optional reloads from previous subpasses. */
7510 if (lra_reg_info[regno].restore_rtx == NULL_RTX
7511 /* If the original pseudo changed its allocation, just
7512 removing the optional pseudo is dangerous as the original
7513 pseudo will have longer live range. */
7514 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] >= 0)
7515 keep_p = true;
7516 else if (reg_renumber[regno] >= 0)
7517 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
7519 insn = lra_insn_recog_data[uid]->insn;
7520 if ((set = single_set (insn)) == NULL_RTX)
7521 continue;
7522 src = SET_SRC (set);
7523 dest = SET_DEST (set);
7524 if ((! REG_P (src) && ! SUBREG_P (src))
7525 || (! REG_P (dest) && ! SUBREG_P (dest)))
7526 continue;
7527 if (get_regno (dest) == (int) regno
7528 /* Ignore insn for optional reloads itself. */
7529 && (get_regno (lra_reg_info[regno].restore_rtx)
7530 != get_regno (src))
7531 /* Check only inheritance on last inheritance pass. */
7532 && get_regno (src) >= new_regno_start
7533 /* Check that the optional reload was inherited. */
7534 && bitmap_bit_p (&lra_inheritance_pseudos, get_regno (src)))
7536 keep_p = true;
7537 break;
7540 if (keep_p)
7542 bitmap_clear_bit (removed_optional_reload_pseudos, regno);
7543 if (lra_dump_file != NULL)
7544 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
7547 change_p = ! bitmap_empty_p (removed_optional_reload_pseudos);
7548 auto_bitmap insn_bitmap (&reg_obstack);
7549 EXECUTE_IF_SET_IN_BITMAP (removed_optional_reload_pseudos, 0, regno, bi)
7551 if (lra_dump_file != NULL)
7552 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
7553 bitmap_copy (insn_bitmap, &lra_reg_info[regno].insn_bitmap);
7554 EXECUTE_IF_SET_IN_BITMAP (insn_bitmap, 0, uid, bi2)
7556 /* We may have already removed a clobber. */
7557 if (!lra_insn_recog_data[uid])
7558 continue;
7559 insn = lra_insn_recog_data[uid]->insn;
7560 if ((set = single_set (insn)) != NULL_RTX)
7562 src = SET_SRC (set);
7563 dest = SET_DEST (set);
7564 if ((REG_P (src) || SUBREG_P (src))
7565 && (REG_P (dest) || SUBREG_P (dest))
7566 && ((get_regno (src) == (int) regno
7567 && (get_regno (lra_reg_info[regno].restore_rtx)
7568 == get_regno (dest)))
7569 || (get_regno (dest) == (int) regno
7570 && (get_regno (lra_reg_info[regno].restore_rtx)
7571 == get_regno (src)))))
7573 if (lra_dump_file != NULL)
7575 fprintf (lra_dump_file, " Deleting move %u\n",
7576 INSN_UID (insn));
7577 dump_insn_slim (lra_dump_file, insn);
7579 delete_move_and_clobber (insn, get_regno (dest));
7580 continue;
7582 /* We should not worry about generation memory-memory
7583 moves here as if the corresponding inheritance did
7584 not work (inheritance pseudo did not get a hard reg),
7585 we remove the inheritance pseudo and the optional
7586 reload. */
7588 if (GET_CODE (PATTERN (insn)) == CLOBBER
7589 && REG_P (SET_DEST (insn))
7590 && get_regno (SET_DEST (insn)) == (int) regno)
7591 /* Refuse to remap clobbers to preexisting pseudos. */
7592 gcc_unreachable ();
7593 lra_substitute_pseudo_within_insn
7594 (insn, regno, lra_reg_info[regno].restore_rtx, false);
7595 lra_update_insn_regno_info (insn);
7596 if (lra_dump_file != NULL)
7598 fprintf (lra_dump_file,
7599 " Restoring original insn:\n");
7600 dump_insn_slim (lra_dump_file, insn);
7604 /* Clear restore_regnos. */
7605 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
7606 lra_reg_info[regno].restore_rtx = NULL_RTX;
7607 return change_p;
7610 /* Entry function for undoing inheritance/split transformation. Return true
7611 if we did any RTL change in this pass. */
7612 bool
7613 lra_undo_inheritance (void)
7615 unsigned int regno;
7616 int hard_regno;
7617 int n_all_inherit, n_inherit, n_all_split, n_split;
7618 rtx restore_rtx;
7619 bitmap_iterator bi;
7620 bool change_p;
7622 lra_undo_inheritance_iter++;
7623 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
7624 return false;
7625 if (lra_dump_file != NULL)
7626 fprintf (lra_dump_file,
7627 "\n********** Undoing inheritance #%d: **********\n\n",
7628 lra_undo_inheritance_iter);
7629 auto_bitmap remove_pseudos (&reg_obstack);
7630 n_inherit = n_all_inherit = 0;
7631 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
7632 if (lra_reg_info[regno].restore_rtx != NULL_RTX)
7634 n_all_inherit++;
7635 if (reg_renumber[regno] < 0
7636 /* If the original pseudo changed its allocation, just
7637 removing inheritance is dangerous as for changing
7638 allocation we used shorter live-ranges. */
7639 && (! REG_P (lra_reg_info[regno].restore_rtx)
7640 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] < 0))
7641 bitmap_set_bit (remove_pseudos, regno);
7642 else
7643 n_inherit++;
7645 if (lra_dump_file != NULL && n_all_inherit != 0)
7646 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
7647 n_inherit, n_all_inherit,
7648 (double) n_inherit / n_all_inherit * 100);
7649 n_split = n_all_split = 0;
7650 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
7651 if ((restore_rtx = lra_reg_info[regno].restore_rtx) != NULL_RTX)
7653 int restore_regno = REGNO (restore_rtx);
7655 n_all_split++;
7656 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
7657 ? reg_renumber[restore_regno] : restore_regno);
7658 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
7659 bitmap_set_bit (remove_pseudos, regno);
7660 else
7662 n_split++;
7663 if (lra_dump_file != NULL)
7664 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
7665 regno, restore_regno);
7668 if (lra_dump_file != NULL && n_all_split != 0)
7669 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
7670 n_split, n_all_split,
7671 (double) n_split / n_all_split * 100);
7672 change_p = remove_inheritance_pseudos (remove_pseudos);
7673 /* Clear restore_regnos. */
7674 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
7675 lra_reg_info[regno].restore_rtx = NULL_RTX;
7676 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
7677 lra_reg_info[regno].restore_rtx = NULL_RTX;
7678 change_p = undo_optional_reloads () || change_p;
7679 if (change_p)
7680 lra_dump_insns_if_possible ("changed func after undoing inheritance");
7681 return change_p;