ada: Fix wrong finalization for call to BIP function in conditional expression
[official-gcc.git] / gcc / lra-constraints.cc
blob4dc2d70c402ca2b893b72774a571f3010840f531
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2023 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file contains code for 3 passes: constraint pass,
23 inheritance/split pass, and pass for undoing failed inheritance and
24 split.
26 The major goal of constraint pass is to transform RTL to satisfy
27 insn and address constraints by:
28 o choosing insn alternatives;
29 o generating *reload insns* (or reloads in brief) and *reload
30 pseudos* which will get necessary hard registers later;
31 o substituting pseudos with equivalent values and removing the
32 instructions that initialized those pseudos.
34 The constraint pass has biggest and most complicated code in LRA.
35 There are a lot of important details like:
36 o reuse of input reload pseudos to simplify reload pseudo
37 allocations;
38 o some heuristics to choose insn alternative to improve the
39 inheritance;
40 o early clobbers etc.
42 The pass is mimicking former reload pass in alternative choosing
43 because the reload pass is oriented to current machine description
44 model. It might be changed if the machine description model is
45 changed.
47 There is special code for preventing all LRA and this pass cycling
48 in case of bugs.
50 On the first iteration of the pass we process every instruction and
51 choose an alternative for each one. On subsequent iterations we try
52 to avoid reprocessing instructions if we can be sure that the old
53 choice is still valid.
55 The inheritance/spilt pass is to transform code to achieve
56 ineheritance and live range splitting. It is done on backward
57 traversal of EBBs.
59 The inheritance optimization goal is to reuse values in hard
60 registers. There is analogous optimization in old reload pass. The
61 inheritance is achieved by following transformation:
63 reload_p1 <- p reload_p1 <- p
64 ... new_p <- reload_p1
65 ... => ...
66 reload_p2 <- p reload_p2 <- new_p
68 where p is spilled and not changed between the insns. Reload_p1 is
69 also called *original pseudo* and new_p is called *inheritance
70 pseudo*.
72 The subsequent assignment pass will try to assign the same (or
73 another if it is not possible) hard register to new_p as to
74 reload_p1 or reload_p2.
76 If the assignment pass fails to assign a hard register to new_p,
77 this file will undo the inheritance and restore the original code.
78 This is because implementing the above sequence with a spilled
79 new_p would make the code much worse. The inheritance is done in
80 EBB scope. The above is just a simplified example to get an idea
81 of the inheritance as the inheritance is also done for non-reload
82 insns.
84 Splitting (transformation) is also done in EBB scope on the same
85 pass as the inheritance:
87 r <- ... or ... <- r r <- ... or ... <- r
88 ... s <- r (new insn -- save)
89 ... =>
90 ... r <- s (new insn -- restore)
91 ... <- r ... <- r
93 The *split pseudo* s is assigned to the hard register of the
94 original pseudo or hard register r.
96 Splitting is done:
97 o In EBBs with high register pressure for global pseudos (living
98 in at least 2 BBs) and assigned to hard registers when there
99 are more one reloads needing the hard registers;
100 o for pseudos needing save/restore code around calls.
102 If the split pseudo still has the same hard register as the
103 original pseudo after the subsequent assignment pass or the
104 original pseudo was split, the opposite transformation is done on
105 the same pass for undoing inheritance. */
107 #undef REG_OK_STRICT
109 #include "config.h"
110 #include "system.h"
111 #include "coretypes.h"
112 #include "backend.h"
113 #include "hooks.h"
114 #include "target.h"
115 #include "rtl.h"
116 #include "tree.h"
117 #include "predict.h"
118 #include "df.h"
119 #include "memmodel.h"
120 #include "tm_p.h"
121 #include "expmed.h"
122 #include "optabs.h"
123 #include "regs.h"
124 #include "ira.h"
125 #include "recog.h"
126 #include "output.h"
127 #include "addresses.h"
128 #include "expr.h"
129 #include "cfgrtl.h"
130 #include "rtl-error.h"
131 #include "lra.h"
132 #include "lra-int.h"
133 #include "print-rtl.h"
134 #include "function-abi.h"
135 #include "rtl-iter.h"
137 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
138 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
139 reload insns. */
140 static int bb_reload_num;
142 /* The current insn being processed and corresponding its single set
143 (NULL otherwise), its data (basic block, the insn data, the insn
144 static data, and the mode of each operand). */
145 static rtx_insn *curr_insn;
146 static rtx curr_insn_set;
147 static basic_block curr_bb;
148 static lra_insn_recog_data_t curr_id;
149 static struct lra_static_insn_data *curr_static_id;
150 static machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
151 /* Mode of the register substituted by its equivalence with VOIDmode
152 (e.g. constant) and whose subreg is given operand of the current
153 insn. VOIDmode in all other cases. */
154 static machine_mode original_subreg_reg_mode[MAX_RECOG_OPERANDS];
158 /* Start numbers for new registers and insns at the current constraints
159 pass start. */
160 static int new_regno_start;
161 static int new_insn_uid_start;
163 /* If LOC is nonnull, strip any outer subreg from it. */
164 static inline rtx *
165 strip_subreg (rtx *loc)
167 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
170 /* Return hard regno of REGNO or if it is was not assigned to a hard
171 register, use a hard register from its allocno class. */
172 static int
173 get_try_hard_regno (int regno)
175 int hard_regno;
176 enum reg_class rclass;
178 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
179 hard_regno = lra_get_regno_hard_regno (regno);
180 if (hard_regno >= 0)
181 return hard_regno;
182 rclass = lra_get_allocno_class (regno);
183 if (rclass == NO_REGS)
184 return -1;
185 return ira_class_hard_regs[rclass][0];
188 /* Return the hard regno of X after removing its subreg. If X is not a
189 register or a subreg of a register, return -1. If X is a pseudo, use its
190 assignment. If X is a hard regno, return the final hard regno which will be
191 after elimination. */
192 static int
193 get_hard_regno (rtx x)
195 rtx reg;
196 int hard_regno;
198 reg = x;
199 if (SUBREG_P (x))
200 reg = SUBREG_REG (x);
201 if (! REG_P (reg))
202 return -1;
203 if (! HARD_REGISTER_NUM_P (hard_regno = REGNO (reg)))
204 hard_regno = lra_get_regno_hard_regno (hard_regno);
205 if (hard_regno < 0)
206 return -1;
207 if (HARD_REGISTER_NUM_P (REGNO (reg)))
208 hard_regno = lra_get_elimination_hard_regno (hard_regno);
209 if (SUBREG_P (x))
210 hard_regno += subreg_regno_offset (hard_regno, GET_MODE (reg),
211 SUBREG_BYTE (x), GET_MODE (x));
212 return hard_regno;
215 /* If REGNO is a hard register or has been allocated a hard register,
216 return the class of that register. If REGNO is a reload pseudo
217 created by the current constraints pass, return its allocno class.
218 Return NO_REGS otherwise. */
219 static enum reg_class
220 get_reg_class (int regno)
222 int hard_regno;
224 if (! HARD_REGISTER_NUM_P (hard_regno = regno))
225 hard_regno = lra_get_regno_hard_regno (regno);
226 if (hard_regno >= 0)
228 hard_regno = lra_get_elimination_hard_regno (hard_regno);
229 return REGNO_REG_CLASS (hard_regno);
231 if (regno >= new_regno_start)
232 return lra_get_allocno_class (regno);
233 return NO_REGS;
236 /* Return true if REG satisfies (or will satisfy) reg class constraint
237 CL. Use elimination first if REG is a hard register. If REG is a
238 reload pseudo created by this constraints pass, assume that it will
239 be allocated a hard register from its allocno class, but allow that
240 class to be narrowed to CL if it is currently a superset of CL and
241 if either:
243 - ALLOW_ALL_RELOAD_CLASS_CHANGES_P is true or
244 - the instruction we're processing is not a reload move.
246 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
247 REGNO (reg), or NO_REGS if no change in its class was needed. */
248 static bool
249 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class,
250 bool allow_all_reload_class_changes_p = false)
252 enum reg_class rclass, common_class;
253 machine_mode reg_mode;
254 rtx src;
255 int class_size, hard_regno, nregs, i, j;
256 int regno = REGNO (reg);
258 if (new_class != NULL)
259 *new_class = NO_REGS;
260 if (regno < FIRST_PSEUDO_REGISTER)
262 rtx final_reg = reg;
263 rtx *final_loc = &final_reg;
265 lra_eliminate_reg_if_possible (final_loc);
266 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
268 reg_mode = GET_MODE (reg);
269 rclass = get_reg_class (regno);
270 src = curr_insn_set != NULL ? SET_SRC (curr_insn_set) : NULL;
271 if (regno < new_regno_start
272 /* Do not allow the constraints for reload instructions to
273 influence the classes of new pseudos. These reloads are
274 typically moves that have many alternatives, and restricting
275 reload pseudos for one alternative may lead to situations
276 where other reload pseudos are no longer allocatable. */
277 || (!allow_all_reload_class_changes_p
278 && INSN_UID (curr_insn) >= new_insn_uid_start
279 && src != NULL
280 && ((REG_P (src) || MEM_P (src))
281 || (GET_CODE (src) == SUBREG
282 && (REG_P (SUBREG_REG (src)) || MEM_P (SUBREG_REG (src)))))))
283 /* When we don't know what class will be used finally for reload
284 pseudos, we use ALL_REGS. */
285 return ((regno >= new_regno_start && rclass == ALL_REGS)
286 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
287 && ! hard_reg_set_subset_p (reg_class_contents[cl],
288 lra_no_alloc_regs)));
289 else
291 common_class = ira_reg_class_subset[rclass][cl];
292 if (new_class != NULL)
293 *new_class = common_class;
294 if (hard_reg_set_subset_p (reg_class_contents[common_class],
295 lra_no_alloc_regs))
296 return false;
297 /* Check that there are enough allocatable regs. */
298 class_size = ira_class_hard_regs_num[common_class];
299 for (i = 0; i < class_size; i++)
301 hard_regno = ira_class_hard_regs[common_class][i];
302 nregs = hard_regno_nregs (hard_regno, reg_mode);
303 if (nregs == 1)
304 return true;
305 for (j = 0; j < nregs; j++)
306 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
307 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
308 hard_regno + j))
309 break;
310 if (j >= nregs)
311 return true;
313 return false;
317 /* Return true if REGNO satisfies a memory constraint. */
318 static bool
319 in_mem_p (int regno)
321 return get_reg_class (regno) == NO_REGS;
324 /* Return 1 if ADDR is a valid memory address for mode MODE in address
325 space AS, and check that each pseudo has the proper kind of hard
326 reg. */
327 static int
328 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
329 rtx addr, addr_space_t as)
331 #ifdef GO_IF_LEGITIMATE_ADDRESS
332 lra_assert (ADDR_SPACE_GENERIC_P (as));
333 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
334 return 0;
336 win:
337 return 1;
338 #else
339 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
340 #endif
343 namespace {
344 /* Temporarily eliminates registers in an address (for the lifetime of
345 the object). */
346 class address_eliminator {
347 public:
348 address_eliminator (struct address_info *ad);
349 ~address_eliminator ();
351 private:
352 struct address_info *m_ad;
353 rtx *m_base_loc;
354 rtx m_base_reg;
355 rtx *m_index_loc;
356 rtx m_index_reg;
360 address_eliminator::address_eliminator (struct address_info *ad)
361 : m_ad (ad),
362 m_base_loc (strip_subreg (ad->base_term)),
363 m_base_reg (NULL_RTX),
364 m_index_loc (strip_subreg (ad->index_term)),
365 m_index_reg (NULL_RTX)
367 if (m_base_loc != NULL)
369 m_base_reg = *m_base_loc;
370 /* If we have non-legitimate address which is decomposed not in
371 the way we expected, don't do elimination here. In such case
372 the address will be reloaded and elimination will be done in
373 reload insn finally. */
374 if (REG_P (m_base_reg))
375 lra_eliminate_reg_if_possible (m_base_loc);
376 if (m_ad->base_term2 != NULL)
377 *m_ad->base_term2 = *m_ad->base_term;
379 if (m_index_loc != NULL)
381 m_index_reg = *m_index_loc;
382 if (REG_P (m_index_reg))
383 lra_eliminate_reg_if_possible (m_index_loc);
387 address_eliminator::~address_eliminator ()
389 if (m_base_loc && *m_base_loc != m_base_reg)
391 *m_base_loc = m_base_reg;
392 if (m_ad->base_term2 != NULL)
393 *m_ad->base_term2 = *m_ad->base_term;
395 if (m_index_loc && *m_index_loc != m_index_reg)
396 *m_index_loc = m_index_reg;
399 /* Return true if the eliminated form of AD is a legitimate target address.
400 If OP is a MEM, AD is the address within OP, otherwise OP should be
401 ignored. CONSTRAINT is one constraint that the operand may need
402 to meet. */
403 static bool
404 valid_address_p (rtx op, struct address_info *ad,
405 enum constraint_num constraint)
407 address_eliminator eliminator (ad);
409 /* Allow a memory OP if it matches CONSTRAINT, even if CONSTRAINT is more
410 forgiving than "m".
411 Need to extract memory from op for special memory constraint,
412 i.e. bcst_mem_operand in i386 backend. */
413 if (MEM_P (extract_mem_from_operand (op))
414 && insn_extra_relaxed_memory_constraint (constraint)
415 && constraint_satisfied_p (op, constraint))
416 return true;
418 return valid_address_p (ad->mode, *ad->outer, ad->as);
421 /* For special_memory_operand, it could be false for MEM_P (op),
422 i.e. bcst_mem_operand in i386 backend.
423 Extract and return real memory operand or op. */
425 extract_mem_from_operand (rtx op)
427 for (rtx x = op;; x = XEXP (x, 0))
429 if (MEM_P (x))
430 return x;
431 if (GET_RTX_LENGTH (GET_CODE (x)) != 1
432 || GET_RTX_FORMAT (GET_CODE (x))[0] != 'e')
433 break;
435 return op;
438 /* Return true if the eliminated form of memory reference OP satisfies
439 extra (special) memory constraint CONSTRAINT. */
440 static bool
441 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
443 struct address_info ad;
444 rtx mem = extract_mem_from_operand (op);
445 if (!MEM_P (mem))
446 return false;
448 decompose_mem_address (&ad, mem);
449 address_eliminator eliminator (&ad);
450 return constraint_satisfied_p (op, constraint);
453 /* Return true if the eliminated form of address AD satisfies extra
454 address constraint CONSTRAINT. */
455 static bool
456 satisfies_address_constraint_p (struct address_info *ad,
457 enum constraint_num constraint)
459 address_eliminator eliminator (ad);
460 return constraint_satisfied_p (*ad->outer, constraint);
463 /* Return true if the eliminated form of address OP satisfies extra
464 address constraint CONSTRAINT. */
465 static bool
466 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
468 struct address_info ad;
470 decompose_lea_address (&ad, &op);
471 return satisfies_address_constraint_p (&ad, constraint);
474 /* Initiate equivalences for LRA. As we keep original equivalences
475 before any elimination, we need to make copies otherwise any change
476 in insns might change the equivalences. */
477 void
478 lra_init_equiv (void)
480 ira_expand_reg_equiv ();
481 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
483 rtx res;
485 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
486 ira_reg_equiv[i].memory = copy_rtx (res);
487 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
488 ira_reg_equiv[i].invariant = copy_rtx (res);
492 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
494 /* Update equivalence for REGNO. We need to this as the equivalence
495 might contain other pseudos which are changed by their
496 equivalences. */
497 static void
498 update_equiv (int regno)
500 rtx x;
502 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
503 ira_reg_equiv[regno].memory
504 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
505 NULL_RTX);
506 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
507 ira_reg_equiv[regno].invariant
508 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
509 NULL_RTX);
512 /* If we have decided to substitute X with another value, return that
513 value, otherwise return X. */
514 static rtx
515 get_equiv (rtx x)
517 int regno;
518 rtx res;
520 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
521 || ! ira_reg_equiv[regno].defined_p
522 || ! ira_reg_equiv[regno].profitable_p
523 || lra_get_regno_hard_regno (regno) >= 0)
524 return x;
525 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
527 if (targetm.cannot_substitute_mem_equiv_p (res))
528 return x;
529 return res;
531 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
532 return res;
533 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
534 return res;
535 gcc_unreachable ();
538 /* If we have decided to substitute X with the equivalent value,
539 return that value after elimination for INSN, otherwise return
540 X. */
541 static rtx
542 get_equiv_with_elimination (rtx x, rtx_insn *insn)
544 rtx res = get_equiv (x);
546 if (x == res || CONSTANT_P (res))
547 return res;
548 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
549 false, false, 0, true);
552 /* Set up curr_operand_mode. */
553 static void
554 init_curr_operand_mode (void)
556 int nop = curr_static_id->n_operands;
557 for (int i = 0; i < nop; i++)
559 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
560 if (mode == VOIDmode)
562 /* The .md mode for address operands is the mode of the
563 addressed value rather than the mode of the address itself. */
564 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
565 mode = Pmode;
566 else
567 mode = curr_static_id->operand[i].mode;
569 curr_operand_mode[i] = mode;
575 /* The page contains code to reuse input reloads. */
577 /* Structure describes input reload of the current insns. */
578 struct input_reload
580 /* True for input reload of matched operands. */
581 bool match_p;
582 /* Reloaded value. */
583 rtx input;
584 /* Reload pseudo used. */
585 rtx reg;
588 /* The number of elements in the following array. */
589 static int curr_insn_input_reloads_num;
590 /* Array containing info about input reloads. It is used to find the
591 same input reload and reuse the reload pseudo in this case. */
592 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
594 /* Initiate data concerning reuse of input reloads for the current
595 insn. */
596 static void
597 init_curr_insn_input_reloads (void)
599 curr_insn_input_reloads_num = 0;
602 /* The canonical form of an rtx inside a MEM is not necessarily the same as the
603 canonical form of the rtx outside the MEM. Fix this up in the case that
604 we're reloading an address (and therefore pulling it outside a MEM). */
605 static rtx
606 canonicalize_reload_addr (rtx addr)
608 subrtx_var_iterator::array_type array;
609 FOR_EACH_SUBRTX_VAR (iter, array, addr, NONCONST)
611 rtx x = *iter;
612 if (GET_CODE (x) == MULT && CONST_INT_P (XEXP (x, 1)))
614 const HOST_WIDE_INT ci = INTVAL (XEXP (x, 1));
615 const int pwr2 = exact_log2 (ci);
616 if (pwr2 > 0)
618 /* Rewrite this to use a shift instead, which is canonical when
619 outside of a MEM. */
620 PUT_CODE (x, ASHIFT);
621 XEXP (x, 1) = GEN_INT (pwr2);
626 return addr;
629 /* Create a new pseudo using MODE, RCLASS, EXCLUDE_START_HARD_REGS, ORIGINAL or
630 reuse an existing reload pseudo. Don't reuse an existing reload pseudo if
631 IN_SUBREG_P is true and the reused pseudo should be wrapped up in a SUBREG.
632 The result pseudo is returned through RESULT_REG. Return TRUE if we created
633 a new pseudo, FALSE if we reused an existing reload pseudo. Use TITLE to
634 describe new registers for debug purposes. */
635 static bool
636 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
637 enum reg_class rclass, HARD_REG_SET *exclude_start_hard_regs,
638 bool in_subreg_p, const char *title, rtx *result_reg)
640 int i, regno;
641 enum reg_class new_class;
642 bool unique_p = false;
644 if (type == OP_OUT)
646 /* Output reload registers tend to start out with a conservative
647 choice of register class. Usually this is ALL_REGS, although
648 a target might narrow it (for performance reasons) through
649 targetm.preferred_reload_class. It's therefore quite common
650 for a reload instruction to require a more restrictive class
651 than the class that was originally assigned to the reload register.
653 In these situations, it's more efficient to refine the choice
654 of register class rather than create a second reload register.
655 This also helps to avoid cycling for registers that are only
656 used by reload instructions. */
657 if (REG_P (original)
658 && (int) REGNO (original) >= new_regno_start
659 && INSN_UID (curr_insn) >= new_insn_uid_start
660 && in_class_p (original, rclass, &new_class, true))
662 unsigned int regno = REGNO (original);
663 if (lra_dump_file != NULL)
665 fprintf (lra_dump_file, " Reuse r%d for output ", regno);
666 dump_value_slim (lra_dump_file, original, 1);
668 if (new_class != lra_get_allocno_class (regno))
669 lra_change_class (regno, new_class, ", change to", false);
670 if (lra_dump_file != NULL)
671 fprintf (lra_dump_file, "\n");
672 *result_reg = original;
673 return false;
675 *result_reg
676 = lra_create_new_reg_with_unique_value (mode, original, rclass,
677 exclude_start_hard_regs, title);
678 return true;
680 /* Prevent reuse value of expression with side effects,
681 e.g. volatile memory. */
682 if (! side_effects_p (original))
683 for (i = 0; i < curr_insn_input_reloads_num; i++)
685 if (! curr_insn_input_reloads[i].match_p
686 && rtx_equal_p (curr_insn_input_reloads[i].input, original)
687 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
689 rtx reg = curr_insn_input_reloads[i].reg;
690 regno = REGNO (reg);
691 /* If input is equal to original and both are VOIDmode,
692 GET_MODE (reg) might be still different from mode.
693 Ensure we don't return *result_reg with wrong mode. */
694 if (GET_MODE (reg) != mode)
696 if (in_subreg_p)
697 continue;
698 if (maybe_lt (GET_MODE_SIZE (GET_MODE (reg)),
699 GET_MODE_SIZE (mode)))
700 continue;
701 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
702 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
703 continue;
705 *result_reg = reg;
706 if (lra_dump_file != NULL)
708 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
709 dump_value_slim (lra_dump_file, original, 1);
711 if (new_class != lra_get_allocno_class (regno))
712 lra_change_class (regno, new_class, ", change to", false);
713 if (lra_dump_file != NULL)
714 fprintf (lra_dump_file, "\n");
715 return false;
717 /* If we have an input reload with a different mode, make sure it
718 will get a different hard reg. */
719 else if (REG_P (original)
720 && REG_P (curr_insn_input_reloads[i].input)
721 && REGNO (original) == REGNO (curr_insn_input_reloads[i].input)
722 && (GET_MODE (original)
723 != GET_MODE (curr_insn_input_reloads[i].input)))
724 unique_p = true;
726 *result_reg = (unique_p
727 ? lra_create_new_reg_with_unique_value
728 : lra_create_new_reg) (mode, original, rclass,
729 exclude_start_hard_regs, title);
730 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
731 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
732 curr_insn_input_reloads[curr_insn_input_reloads_num].match_p = false;
733 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
734 return true;
738 /* The page contains major code to choose the current insn alternative
739 and generate reloads for it. */
741 /* Return the offset from REGNO of the least significant register
742 in (reg:MODE REGNO).
744 This function is used to tell whether two registers satisfy
745 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
747 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
748 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
750 lra_constraint_offset (int regno, machine_mode mode)
752 lra_assert (regno < FIRST_PSEUDO_REGISTER);
754 scalar_int_mode int_mode;
755 if (WORDS_BIG_ENDIAN
756 && is_a <scalar_int_mode> (mode, &int_mode)
757 && GET_MODE_SIZE (int_mode) > UNITS_PER_WORD)
758 return hard_regno_nregs (regno, mode) - 1;
759 return 0;
762 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
763 if they are the same hard reg, and has special hacks for
764 auto-increment and auto-decrement. This is specifically intended for
765 process_alt_operands to use in determining whether two operands
766 match. X is the operand whose number is the lower of the two.
768 It is supposed that X is the output operand and Y is the input
769 operand. Y_HARD_REGNO is the final hard regno of register Y or
770 register in subreg Y as we know it now. Otherwise, it is a
771 negative value. */
772 static bool
773 operands_match_p (rtx x, rtx y, int y_hard_regno)
775 int i;
776 RTX_CODE code = GET_CODE (x);
777 const char *fmt;
779 if (x == y)
780 return true;
781 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
782 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
784 int j;
786 i = get_hard_regno (x);
787 if (i < 0)
788 goto slow;
790 if ((j = y_hard_regno) < 0)
791 goto slow;
793 i += lra_constraint_offset (i, GET_MODE (x));
794 j += lra_constraint_offset (j, GET_MODE (y));
796 return i == j;
799 /* If two operands must match, because they are really a single
800 operand of an assembler insn, then two post-increments are invalid
801 because the assembler insn would increment only once. On the
802 other hand, a post-increment matches ordinary indexing if the
803 post-increment is the output operand. */
804 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
805 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
807 /* Two pre-increments are invalid because the assembler insn would
808 increment only once. On the other hand, a pre-increment matches
809 ordinary indexing if the pre-increment is the input operand. */
810 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
811 || GET_CODE (y) == PRE_MODIFY)
812 return operands_match_p (x, XEXP (y, 0), -1);
814 slow:
816 if (code == REG && REG_P (y))
817 return REGNO (x) == REGNO (y);
819 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
820 && x == SUBREG_REG (y))
821 return true;
822 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
823 && SUBREG_REG (x) == y)
824 return true;
826 /* Now we have disposed of all the cases in which different rtx
827 codes can match. */
828 if (code != GET_CODE (y))
829 return false;
831 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
832 if (GET_MODE (x) != GET_MODE (y))
833 return false;
835 switch (code)
837 CASE_CONST_UNIQUE:
838 return false;
840 case CONST_VECTOR:
841 if (!same_vector_encodings_p (x, y))
842 return false;
843 break;
845 case LABEL_REF:
846 return label_ref_label (x) == label_ref_label (y);
847 case SYMBOL_REF:
848 return XSTR (x, 0) == XSTR (y, 0);
850 default:
851 break;
854 /* Compare the elements. If any pair of corresponding elements fail
855 to match, return false for the whole things. */
857 fmt = GET_RTX_FORMAT (code);
858 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
860 int val, j;
861 switch (fmt[i])
863 case 'w':
864 if (XWINT (x, i) != XWINT (y, i))
865 return false;
866 break;
868 case 'i':
869 if (XINT (x, i) != XINT (y, i))
870 return false;
871 break;
873 case 'p':
874 if (maybe_ne (SUBREG_BYTE (x), SUBREG_BYTE (y)))
875 return false;
876 break;
878 case 'e':
879 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
880 if (val == 0)
881 return false;
882 break;
884 case '0':
885 break;
887 case 'E':
888 if (XVECLEN (x, i) != XVECLEN (y, i))
889 return false;
890 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
892 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
893 if (val == 0)
894 return false;
896 break;
898 /* It is believed that rtx's at this level will never
899 contain anything but integers and other rtx's, except for
900 within LABEL_REFs and SYMBOL_REFs. */
901 default:
902 gcc_unreachable ();
905 return true;
908 /* True if X is a constant that can be forced into the constant pool.
909 MODE is the mode of the operand, or VOIDmode if not known. */
910 #define CONST_POOL_OK_P(MODE, X) \
911 ((MODE) != VOIDmode \
912 && CONSTANT_P (X) \
913 && GET_CODE (X) != HIGH \
914 && GET_MODE_SIZE (MODE).is_constant () \
915 && !targetm.cannot_force_const_mem (MODE, X))
917 /* True if C is a non-empty register class that has too few registers
918 to be safely used as a reload target class. */
919 #define SMALL_REGISTER_CLASS_P(C) \
920 (ira_class_hard_regs_num [(C)] == 1 \
921 || (ira_class_hard_regs_num [(C)] >= 1 \
922 && targetm.class_likely_spilled_p (C)))
924 /* If REG is a reload pseudo, try to make its class satisfying CL. */
925 static void
926 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
928 enum reg_class rclass;
930 /* Do not make more accurate class from reloads generated. They are
931 mostly moves with a lot of constraints. Making more accurate
932 class may results in very narrow class and impossibility of find
933 registers for several reloads of one insn. */
934 if (INSN_UID (curr_insn) >= new_insn_uid_start)
935 return;
936 if (GET_CODE (reg) == SUBREG)
937 reg = SUBREG_REG (reg);
938 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
939 return;
940 if (in_class_p (reg, cl, &rclass) && rclass != cl)
941 lra_change_class (REGNO (reg), rclass, " Change to", true);
944 /* Searches X for any reference to a reg with the same value as REGNO,
945 returning the rtx of the reference found if any. Otherwise,
946 returns NULL_RTX. */
947 static rtx
948 regno_val_use_in (unsigned int regno, rtx x)
950 const char *fmt;
951 int i, j;
952 rtx tem;
954 if (REG_P (x) && lra_reg_info[REGNO (x)].val == lra_reg_info[regno].val)
955 return x;
957 fmt = GET_RTX_FORMAT (GET_CODE (x));
958 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
960 if (fmt[i] == 'e')
962 if ((tem = regno_val_use_in (regno, XEXP (x, i))))
963 return tem;
965 else if (fmt[i] == 'E')
966 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
967 if ((tem = regno_val_use_in (regno , XVECEXP (x, i, j))))
968 return tem;
971 return NULL_RTX;
974 /* Return true if all current insn non-output operands except INS (it
975 has a negaitve end marker) do not use pseudos with the same value
976 as REGNO. */
977 static bool
978 check_conflict_input_operands (int regno, signed char *ins)
980 int in;
981 int n_operands = curr_static_id->n_operands;
983 for (int nop = 0; nop < n_operands; nop++)
984 if (! curr_static_id->operand[nop].is_operator
985 && curr_static_id->operand[nop].type != OP_OUT)
987 for (int i = 0; (in = ins[i]) >= 0; i++)
988 if (in == nop)
989 break;
990 if (in < 0
991 && regno_val_use_in (regno, *curr_id->operand_loc[nop]) != NULL_RTX)
992 return false;
994 return true;
997 /* Generate reloads for matching OUT and INS (array of input operand numbers
998 with end marker -1) with reg class GOAL_CLASS and EXCLUDE_START_HARD_REGS,
999 considering output operands OUTS (similar array to INS) needing to be in
1000 different registers. Add input and output reloads correspondingly to the
1001 lists *BEFORE and *AFTER. OUT might be negative. In this case we generate
1002 input reloads for matched input operands INS. EARLY_CLOBBER_P is a flag
1003 that the output operand is early clobbered for chosen alternative. */
1004 static void
1005 match_reload (signed char out, signed char *ins, signed char *outs,
1006 enum reg_class goal_class, HARD_REG_SET *exclude_start_hard_regs,
1007 rtx_insn **before, rtx_insn **after, bool early_clobber_p)
1009 bool out_conflict;
1010 int i, in;
1011 rtx new_in_reg, new_out_reg, reg;
1012 machine_mode inmode, outmode;
1013 rtx in_rtx = *curr_id->operand_loc[ins[0]];
1014 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
1016 inmode = curr_operand_mode[ins[0]];
1017 outmode = out < 0 ? inmode : curr_operand_mode[out];
1018 push_to_sequence (*before);
1019 if (inmode != outmode)
1021 /* process_alt_operands has already checked that the mode sizes
1022 are ordered. */
1023 if (partial_subreg_p (outmode, inmode))
1025 bool asm_p = asm_noperands (PATTERN (curr_insn)) >= 0;
1026 int hr;
1027 HARD_REG_SET temp_hard_reg_set;
1029 if (asm_p && (hr = get_hard_regno (out_rtx)) >= 0
1030 && hard_regno_nregs (hr, inmode) > 1)
1032 /* See gcc.c-torture/execute/20030222-1.c.
1033 Consider the code for 32-bit (e.g. BE) target:
1034 int i, v; long x; x = v; asm ("" : "=r" (i) : "0" (x));
1035 We generate the following RTL with reload insns:
1036 1. subreg:si(x:di, 0) = 0;
1037 2. subreg:si(x:di, 4) = v:si;
1038 3. t:di = x:di, dead x;
1039 4. asm ("" : "=r" (subreg:si(t:di,4)) : "0" (t:di))
1040 5. i:si = subreg:si(t:di,4);
1041 If we assign hard reg of x to t, dead code elimination
1042 will remove insn #2 and we will use unitialized hard reg.
1043 So exclude the hard reg of x for t. We could ignore this
1044 problem for non-empty asm using all x value but it is hard to
1045 check that the asm are expanded into insn realy using x
1046 and setting r. */
1047 CLEAR_HARD_REG_SET (temp_hard_reg_set);
1048 if (exclude_start_hard_regs != NULL)
1049 temp_hard_reg_set = *exclude_start_hard_regs;
1050 SET_HARD_REG_BIT (temp_hard_reg_set, hr);
1051 exclude_start_hard_regs = &temp_hard_reg_set;
1053 reg = new_in_reg
1054 = lra_create_new_reg_with_unique_value (inmode, in_rtx, goal_class,
1055 exclude_start_hard_regs,
1056 "");
1057 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
1058 LRA_SUBREG_P (new_out_reg) = 1;
1059 /* If the input reg is dying here, we can use the same hard
1060 register for REG and IN_RTX. We do it only for original
1061 pseudos as reload pseudos can die although original
1062 pseudos still live where reload pseudos dies. */
1063 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
1064 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
1065 && (!early_clobber_p
1066 || check_conflict_input_operands(REGNO (in_rtx), ins)))
1067 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
1069 else
1071 reg = new_out_reg
1072 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
1073 goal_class,
1074 exclude_start_hard_regs,
1075 "");
1076 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
1077 /* NEW_IN_REG is non-paradoxical subreg. We don't want
1078 NEW_OUT_REG living above. We add clobber clause for
1079 this. This is just a temporary clobber. We can remove
1080 it at the end of LRA work. */
1081 rtx_insn *clobber = emit_clobber (new_out_reg);
1082 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
1083 LRA_SUBREG_P (new_in_reg) = 1;
1084 if (GET_CODE (in_rtx) == SUBREG)
1086 rtx subreg_reg = SUBREG_REG (in_rtx);
1088 /* If SUBREG_REG is dying here and sub-registers IN_RTX
1089 and NEW_IN_REG are similar, we can use the same hard
1090 register for REG and SUBREG_REG. */
1091 if (REG_P (subreg_reg)
1092 && (int) REGNO (subreg_reg) < lra_new_regno_start
1093 && GET_MODE (subreg_reg) == outmode
1094 && known_eq (SUBREG_BYTE (in_rtx), SUBREG_BYTE (new_in_reg))
1095 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg))
1096 && (! early_clobber_p
1097 || check_conflict_input_operands (REGNO (subreg_reg),
1098 ins)))
1099 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
1103 else
1105 /* Pseudos have values -- see comments for lra_reg_info.
1106 Different pseudos with the same value do not conflict even if
1107 they live in the same place. When we create a pseudo we
1108 assign value of original pseudo (if any) from which we
1109 created the new pseudo. If we create the pseudo from the
1110 input pseudo, the new pseudo will have no conflict with the
1111 input pseudo which is wrong when the input pseudo lives after
1112 the insn and as the new pseudo value is changed by the insn
1113 output. Therefore we create the new pseudo from the output
1114 except the case when we have single matched dying input
1115 pseudo.
1117 We cannot reuse the current output register because we might
1118 have a situation like "a <- a op b", where the constraints
1119 force the second input operand ("b") to match the output
1120 operand ("a"). "b" must then be copied into a new register
1121 so that it doesn't clobber the current value of "a".
1123 We cannot use the same value if the output pseudo is
1124 early clobbered or the input pseudo is mentioned in the
1125 output, e.g. as an address part in memory, because
1126 output reload will actually extend the pseudo liveness.
1127 We don't care about eliminable hard regs here as we are
1128 interesting only in pseudos. */
1130 /* Matching input's register value is the same as one of the other
1131 output operand. Output operands in a parallel insn must be in
1132 different registers. */
1133 out_conflict = false;
1134 if (REG_P (in_rtx))
1136 for (i = 0; outs[i] >= 0; i++)
1138 rtx other_out_rtx = *curr_id->operand_loc[outs[i]];
1139 if (outs[i] != out && REG_P (other_out_rtx)
1140 && (regno_val_use_in (REGNO (in_rtx), other_out_rtx)
1141 != NULL_RTX))
1143 out_conflict = true;
1144 break;
1149 new_in_reg = new_out_reg
1150 = (! early_clobber_p && ins[1] < 0 && REG_P (in_rtx)
1151 && (int) REGNO (in_rtx) < lra_new_regno_start
1152 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
1153 && (! early_clobber_p
1154 || check_conflict_input_operands (REGNO (in_rtx), ins))
1155 && (out < 0
1156 || regno_val_use_in (REGNO (in_rtx), out_rtx) == NULL_RTX)
1157 && !out_conflict
1158 ? lra_create_new_reg (inmode, in_rtx, goal_class,
1159 exclude_start_hard_regs, "")
1160 : lra_create_new_reg_with_unique_value (outmode, out_rtx, goal_class,
1161 exclude_start_hard_regs,
1162 ""));
1164 /* In operand can be got from transformations before processing insn
1165 constraints. One example of such transformations is subreg
1166 reloading (see function simplify_operand_subreg). The new
1167 pseudos created by the transformations might have inaccurate
1168 class (ALL_REGS) and we should make their classes more
1169 accurate. */
1170 narrow_reload_pseudo_class (in_rtx, goal_class);
1171 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
1172 *before = get_insns ();
1173 end_sequence ();
1174 /* Add the new pseudo to consider values of subsequent input reload
1175 pseudos. */
1176 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
1177 curr_insn_input_reloads[curr_insn_input_reloads_num].input = in_rtx;
1178 curr_insn_input_reloads[curr_insn_input_reloads_num].match_p = true;
1179 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = new_in_reg;
1180 for (i = 0; (in = ins[i]) >= 0; i++)
1181 if (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
1182 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]))
1183 *curr_id->operand_loc[in] = new_in_reg;
1184 else
1186 lra_assert
1187 (GET_MODE (new_out_reg) == GET_MODE (*curr_id->operand_loc[in]));
1188 *curr_id->operand_loc[in] = new_out_reg;
1190 lra_update_dups (curr_id, ins);
1191 if (out < 0)
1192 return;
1193 /* See a comment for the input operand above. */
1194 narrow_reload_pseudo_class (out_rtx, goal_class);
1195 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
1197 reg = SUBREG_P (out_rtx) ? SUBREG_REG (out_rtx) : out_rtx;
1198 start_sequence ();
1199 /* If we had strict_low_part, use it also in reload to keep other
1200 parts unchanged but do it only for regs as strict_low_part
1201 has no sense for memory and probably there is no insn pattern
1202 to match the reload insn in memory case. */
1203 if (out >= 0 && curr_static_id->operand[out].strict_low && REG_P (reg))
1204 out_rtx = gen_rtx_STRICT_LOW_PART (VOIDmode, out_rtx);
1205 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
1206 emit_insn (*after);
1207 *after = get_insns ();
1208 end_sequence ();
1210 *curr_id->operand_loc[out] = new_out_reg;
1211 lra_update_dup (curr_id, out);
1214 /* Return register class which is union of all reg classes in insn
1215 constraint alternative string starting with P. */
1216 static enum reg_class
1217 reg_class_from_constraints (const char *p)
1219 int c, len;
1220 enum reg_class op_class = NO_REGS;
1223 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1225 case '#':
1226 case ',':
1227 return op_class;
1229 case 'g':
1230 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1231 break;
1233 default:
1234 enum constraint_num cn = lookup_constraint (p);
1235 enum reg_class cl = reg_class_for_constraint (cn);
1236 if (cl == NO_REGS)
1238 if (insn_extra_address_constraint (cn))
1239 op_class
1240 = (reg_class_subunion
1241 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1242 ADDRESS, SCRATCH)]);
1243 break;
1246 op_class = reg_class_subunion[op_class][cl];
1247 break;
1249 while ((p += len), c);
1250 return op_class;
1253 /* If OP is a register, return the class of the register as per
1254 get_reg_class, otherwise return NO_REGS. */
1255 static inline enum reg_class
1256 get_op_class (rtx op)
1258 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1261 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1262 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1263 SUBREG for VAL to make them equal. */
1264 static rtx_insn *
1265 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1267 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1269 /* Usually size of mem_pseudo is greater than val size but in
1270 rare cases it can be less as it can be defined by target
1271 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1272 if (! MEM_P (val))
1274 val = gen_lowpart_SUBREG (GET_MODE (mem_pseudo),
1275 GET_CODE (val) == SUBREG
1276 ? SUBREG_REG (val) : val);
1277 LRA_SUBREG_P (val) = 1;
1279 else
1281 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1282 LRA_SUBREG_P (mem_pseudo) = 1;
1285 return to_p ? gen_move_insn (mem_pseudo, val)
1286 : gen_move_insn (val, mem_pseudo);
1289 /* Process a special case insn (register move), return true if we
1290 don't need to process it anymore. INSN should be a single set
1291 insn. Set up that RTL was changed through CHANGE_P and that hook
1292 TARGET_SECONDARY_MEMORY_NEEDED says to use secondary memory through
1293 SEC_MEM_P. */
1294 static bool
1295 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1297 int sregno, dregno;
1298 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1299 rtx_insn *before;
1300 enum reg_class dclass, sclass, secondary_class;
1301 secondary_reload_info sri;
1303 lra_assert (curr_insn_set != NULL_RTX);
1304 dreg = dest = SET_DEST (curr_insn_set);
1305 sreg = src = SET_SRC (curr_insn_set);
1306 if (GET_CODE (dest) == SUBREG)
1307 dreg = SUBREG_REG (dest);
1308 if (GET_CODE (src) == SUBREG)
1309 sreg = SUBREG_REG (src);
1310 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1311 return false;
1312 sclass = dclass = NO_REGS;
1313 if (REG_P (dreg))
1314 dclass = get_reg_class (REGNO (dreg));
1315 gcc_assert (dclass < LIM_REG_CLASSES && dclass >= NO_REGS);
1316 if (dclass == ALL_REGS)
1317 /* ALL_REGS is used for new pseudos created by transformations
1318 like reload of SUBREG_REG (see function
1319 simplify_operand_subreg). We don't know their class yet. We
1320 should figure out the class from processing the insn
1321 constraints not in this fast path function. Even if ALL_REGS
1322 were a right class for the pseudo, secondary_... hooks usually
1323 are not define for ALL_REGS. */
1324 return false;
1325 if (REG_P (sreg))
1326 sclass = get_reg_class (REGNO (sreg));
1327 gcc_assert (sclass < LIM_REG_CLASSES && sclass >= NO_REGS);
1328 if (sclass == ALL_REGS)
1329 /* See comments above. */
1330 return false;
1331 if (sclass == NO_REGS && dclass == NO_REGS)
1332 return false;
1333 if (targetm.secondary_memory_needed (GET_MODE (src), sclass, dclass)
1334 && ((sclass != NO_REGS && dclass != NO_REGS)
1335 || (GET_MODE (src)
1336 != targetm.secondary_memory_needed_mode (GET_MODE (src)))))
1338 *sec_mem_p = true;
1339 return false;
1341 if (! REG_P (dreg) || ! REG_P (sreg))
1342 return false;
1343 sri.prev_sri = NULL;
1344 sri.icode = CODE_FOR_nothing;
1345 sri.extra_cost = 0;
1346 secondary_class = NO_REGS;
1347 /* Set up hard register for a reload pseudo for hook
1348 secondary_reload because some targets just ignore unassigned
1349 pseudos in the hook. */
1350 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1352 dregno = REGNO (dreg);
1353 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1355 else
1356 dregno = -1;
1357 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1359 sregno = REGNO (sreg);
1360 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1362 else
1363 sregno = -1;
1364 if (sclass != NO_REGS)
1365 secondary_class
1366 = (enum reg_class) targetm.secondary_reload (false, dest,
1367 (reg_class_t) sclass,
1368 GET_MODE (src), &sri);
1369 if (sclass == NO_REGS
1370 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1371 && dclass != NO_REGS))
1373 enum reg_class old_sclass = secondary_class;
1374 secondary_reload_info old_sri = sri;
1376 sri.prev_sri = NULL;
1377 sri.icode = CODE_FOR_nothing;
1378 sri.extra_cost = 0;
1379 secondary_class
1380 = (enum reg_class) targetm.secondary_reload (true, src,
1381 (reg_class_t) dclass,
1382 GET_MODE (src), &sri);
1383 /* Check the target hook consistency. */
1384 lra_assert
1385 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1386 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1387 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1389 if (sregno >= 0)
1390 reg_renumber [sregno] = -1;
1391 if (dregno >= 0)
1392 reg_renumber [dregno] = -1;
1393 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1394 return false;
1395 *change_p = true;
1396 new_reg = NULL_RTX;
1397 if (secondary_class != NO_REGS)
1398 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1399 secondary_class, NULL,
1400 "secondary");
1401 start_sequence ();
1402 if (sri.icode == CODE_FOR_nothing)
1403 lra_emit_move (new_reg, src);
1404 else
1406 enum reg_class scratch_class;
1408 scratch_class = (reg_class_from_constraints
1409 (insn_data[sri.icode].operand[2].constraint));
1410 scratch_reg = (lra_create_new_reg_with_unique_value
1411 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1412 scratch_class, NULL, "scratch"));
1413 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1414 src, scratch_reg));
1416 before = get_insns ();
1417 end_sequence ();
1418 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1419 if (new_reg != NULL_RTX)
1420 SET_SRC (curr_insn_set) = new_reg;
1421 else
1423 if (lra_dump_file != NULL)
1425 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1426 dump_insn_slim (lra_dump_file, curr_insn);
1428 lra_set_insn_deleted (curr_insn);
1429 return true;
1431 return false;
1434 /* The following data describe the result of process_alt_operands.
1435 The data are used in curr_insn_transform to generate reloads. */
1437 /* The chosen reg classes which should be used for the corresponding
1438 operands. */
1439 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1440 /* Hard registers which cannot be a start hard register for the corresponding
1441 operands. */
1442 static HARD_REG_SET goal_alt_exclude_start_hard_regs[MAX_RECOG_OPERANDS];
1443 /* True if the operand should be the same as another operand and that
1444 other operand does not need a reload. */
1445 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1446 /* True if the operand does not need a reload. */
1447 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1448 /* True if the operand can be offsetable memory. */
1449 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1450 /* The number of an operand to which given operand can be matched to. */
1451 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1452 /* The number of elements in the following array. */
1453 static int goal_alt_dont_inherit_ops_num;
1454 /* Numbers of operands whose reload pseudos should not be inherited. */
1455 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1456 /* True if the insn commutative operands should be swapped. */
1457 static bool goal_alt_swapped;
1458 /* The chosen insn alternative. */
1459 static int goal_alt_number;
1461 /* True if the corresponding operand is the result of an equivalence
1462 substitution. */
1463 static bool equiv_substition_p[MAX_RECOG_OPERANDS];
1465 /* The following five variables are used to choose the best insn
1466 alternative. They reflect final characteristics of the best
1467 alternative. */
1469 /* Number of necessary reloads and overall cost reflecting the
1470 previous value and other unpleasantness of the best alternative. */
1471 static int best_losers, best_overall;
1472 /* Overall number hard registers used for reloads. For example, on
1473 some targets we need 2 general registers to reload DFmode and only
1474 one floating point register. */
1475 static int best_reload_nregs;
1476 /* Overall number reflecting distances of previous reloading the same
1477 value. The distances are counted from the current BB start. It is
1478 used to improve inheritance chances. */
1479 static int best_reload_sum;
1481 /* True if the current insn should have no correspondingly input or
1482 output reloads. */
1483 static bool no_input_reloads_p, no_output_reloads_p;
1485 /* True if we swapped the commutative operands in the current
1486 insn. */
1487 static int curr_swapped;
1489 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1490 register of class CL. Add any input reloads to list BEFORE. AFTER
1491 is nonnull if *LOC is an automodified value; handle that case by
1492 adding the required output reloads to list AFTER. Return true if
1493 the RTL was changed.
1495 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1496 register. Return false if the address register is correct. */
1497 static bool
1498 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1499 enum reg_class cl)
1501 int regno;
1502 enum reg_class rclass, new_class;
1503 rtx reg;
1504 rtx new_reg;
1505 machine_mode mode;
1506 bool subreg_p, before_p = false;
1508 subreg_p = GET_CODE (*loc) == SUBREG;
1509 if (subreg_p)
1511 reg = SUBREG_REG (*loc);
1512 mode = GET_MODE (reg);
1514 /* For mode with size bigger than ptr_mode, there unlikely to be "mov"
1515 between two registers with different classes, but there normally will
1516 be "mov" which transfers element of vector register into the general
1517 register, and this normally will be a subreg which should be reloaded
1518 as a whole. This is particularly likely to be triggered when
1519 -fno-split-wide-types specified. */
1520 if (!REG_P (reg)
1521 || in_class_p (reg, cl, &new_class)
1522 || known_le (GET_MODE_SIZE (mode), GET_MODE_SIZE (ptr_mode)))
1523 loc = &SUBREG_REG (*loc);
1526 reg = *loc;
1527 mode = GET_MODE (reg);
1528 if (! REG_P (reg))
1530 if (check_only_p)
1531 return true;
1532 /* Always reload memory in an address even if the target supports
1533 such addresses. */
1534 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, NULL,
1535 "address");
1536 before_p = true;
1538 else
1540 regno = REGNO (reg);
1541 rclass = get_reg_class (regno);
1542 if (! check_only_p
1543 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1545 if (lra_dump_file != NULL)
1547 fprintf (lra_dump_file,
1548 "Changing pseudo %d in address of insn %u on equiv ",
1549 REGNO (reg), INSN_UID (curr_insn));
1550 dump_value_slim (lra_dump_file, *loc, 1);
1551 fprintf (lra_dump_file, "\n");
1553 *loc = copy_rtx (*loc);
1555 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1557 if (check_only_p)
1558 return true;
1559 reg = *loc;
1560 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1561 mode, reg, cl, NULL,
1562 subreg_p, "address", &new_reg))
1563 before_p = true;
1565 else if (new_class != NO_REGS && rclass != new_class)
1567 if (check_only_p)
1568 return true;
1569 lra_change_class (regno, new_class, " Change to", true);
1570 return false;
1572 else
1573 return false;
1575 if (before_p)
1577 push_to_sequence (*before);
1578 lra_emit_move (new_reg, reg);
1579 *before = get_insns ();
1580 end_sequence ();
1582 *loc = new_reg;
1583 if (after != NULL)
1585 start_sequence ();
1586 lra_emit_move (before_p ? copy_rtx (reg) : reg, new_reg);
1587 emit_insn (*after);
1588 *after = get_insns ();
1589 end_sequence ();
1591 return true;
1594 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1595 the insn to be inserted before curr insn. AFTER returns the
1596 the insn to be inserted after curr insn. ORIGREG and NEWREG
1597 are the original reg and new reg for reload. */
1598 static void
1599 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1600 rtx newreg)
1602 if (before)
1604 push_to_sequence (*before);
1605 lra_emit_move (newreg, origreg);
1606 *before = get_insns ();
1607 end_sequence ();
1609 if (after)
1611 start_sequence ();
1612 lra_emit_move (origreg, newreg);
1613 emit_insn (*after);
1614 *after = get_insns ();
1615 end_sequence ();
1619 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1620 static bool process_address (int, bool, rtx_insn **, rtx_insn **);
1622 /* Make reloads for subreg in operand NOP with internal subreg mode
1623 REG_MODE, add new reloads for further processing. Return true if
1624 any change was done. */
1625 static bool
1626 simplify_operand_subreg (int nop, machine_mode reg_mode)
1628 int hard_regno, inner_hard_regno;
1629 rtx_insn *before, *after;
1630 machine_mode mode, innermode;
1631 rtx reg, new_reg;
1632 rtx operand = *curr_id->operand_loc[nop];
1633 enum reg_class regclass;
1634 enum op_type type;
1636 before = after = NULL;
1638 if (GET_CODE (operand) != SUBREG)
1639 return false;
1641 mode = GET_MODE (operand);
1642 reg = SUBREG_REG (operand);
1643 innermode = GET_MODE (reg);
1644 type = curr_static_id->operand[nop].type;
1645 if (MEM_P (reg))
1647 const bool addr_was_valid
1648 = valid_address_p (innermode, XEXP (reg, 0), MEM_ADDR_SPACE (reg));
1649 alter_subreg (curr_id->operand_loc[nop], false);
1650 rtx subst = *curr_id->operand_loc[nop];
1651 lra_assert (MEM_P (subst));
1652 const bool addr_is_valid = valid_address_p (GET_MODE (subst),
1653 XEXP (subst, 0),
1654 MEM_ADDR_SPACE (subst));
1655 if (!addr_was_valid
1656 || addr_is_valid
1657 || ((get_constraint_type (lookup_constraint
1658 (curr_static_id->operand[nop].constraint))
1659 != CT_SPECIAL_MEMORY)
1660 /* We still can reload address and if the address is
1661 valid, we can remove subreg without reloading its
1662 inner memory. */
1663 && valid_address_p (GET_MODE (subst),
1664 regno_reg_rtx
1665 [ira_class_hard_regs
1666 [base_reg_class (GET_MODE (subst),
1667 MEM_ADDR_SPACE (subst),
1668 ADDRESS, SCRATCH)][0]],
1669 MEM_ADDR_SPACE (subst))))
1671 /* If we change the address for a paradoxical subreg of memory, the
1672 new address might violate the necessary alignment or the access
1673 might be slow; take this into consideration. We need not worry
1674 about accesses beyond allocated memory for paradoxical memory
1675 subregs as we don't substitute such equiv memory (see processing
1676 equivalences in function lra_constraints) and because for spilled
1677 pseudos we allocate stack memory enough for the biggest
1678 corresponding paradoxical subreg.
1680 However, do not blindly simplify a (subreg (mem ...)) for
1681 WORD_REGISTER_OPERATIONS targets as this may lead to loading junk
1682 data into a register when the inner is narrower than outer or
1683 missing important data from memory when the inner is wider than
1684 outer. This rule only applies to modes that are no wider than
1685 a word.
1687 If valid memory becomes invalid after subreg elimination
1688 and address might be different we still have to reload
1689 memory.
1691 if ((! addr_was_valid
1692 || addr_is_valid
1693 || known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (innermode)))
1694 && !(maybe_ne (GET_MODE_PRECISION (mode),
1695 GET_MODE_PRECISION (innermode))
1696 && known_le (GET_MODE_SIZE (mode), UNITS_PER_WORD)
1697 && known_le (GET_MODE_SIZE (innermode), UNITS_PER_WORD)
1698 && WORD_REGISTER_OPERATIONS)
1699 && (!(MEM_ALIGN (subst) < GET_MODE_ALIGNMENT (mode)
1700 && targetm.slow_unaligned_access (mode, MEM_ALIGN (subst)))
1701 || (MEM_ALIGN (reg) < GET_MODE_ALIGNMENT (innermode)
1702 && targetm.slow_unaligned_access (innermode,
1703 MEM_ALIGN (reg)))))
1704 return true;
1706 *curr_id->operand_loc[nop] = operand;
1708 /* But if the address was not valid, we cannot reload the MEM without
1709 reloading the address first. */
1710 if (!addr_was_valid)
1711 process_address (nop, false, &before, &after);
1713 /* INNERMODE is fast, MODE slow. Reload the mem in INNERMODE. */
1714 enum reg_class rclass
1715 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1716 if (get_reload_reg (curr_static_id->operand[nop].type, innermode,
1717 reg, rclass, NULL,
1718 TRUE, "slow/invalid mem", &new_reg))
1720 bool insert_before, insert_after;
1721 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1723 insert_before = (type != OP_OUT
1724 || partial_subreg_p (mode, innermode));
1725 insert_after = type != OP_IN;
1726 insert_move_for_subreg (insert_before ? &before : NULL,
1727 insert_after ? &after : NULL,
1728 reg, new_reg);
1730 SUBREG_REG (operand) = new_reg;
1732 /* Convert to MODE. */
1733 reg = operand;
1734 rclass
1735 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1736 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1737 rclass, NULL,
1738 TRUE, "slow/invalid mem", &new_reg))
1740 bool insert_before, insert_after;
1741 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1743 insert_before = type != OP_OUT;
1744 insert_after = type != OP_IN;
1745 insert_move_for_subreg (insert_before ? &before : NULL,
1746 insert_after ? &after : NULL,
1747 reg, new_reg);
1749 *curr_id->operand_loc[nop] = new_reg;
1750 lra_process_new_insns (curr_insn, before, after,
1751 "Inserting slow/invalid mem reload");
1752 return true;
1755 /* If the address was valid and became invalid, prefer to reload
1756 the memory. Typical case is when the index scale should
1757 correspond the memory. */
1758 *curr_id->operand_loc[nop] = operand;
1759 /* Do not return false here as the MEM_P (reg) will be processed
1760 later in this function. */
1762 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1764 alter_subreg (curr_id->operand_loc[nop], false);
1765 return true;
1767 else if (CONSTANT_P (reg))
1769 /* Try to simplify subreg of constant. It is usually result of
1770 equivalence substitution. */
1771 if (innermode == VOIDmode
1772 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1773 innermode = curr_static_id->operand[nop].mode;
1774 if ((new_reg = simplify_subreg (mode, reg, innermode,
1775 SUBREG_BYTE (operand))) != NULL_RTX)
1777 *curr_id->operand_loc[nop] = new_reg;
1778 return true;
1781 /* Put constant into memory when we have mixed modes. It generates
1782 a better code in most cases as it does not need a secondary
1783 reload memory. It also prevents LRA looping when LRA is using
1784 secondary reload memory again and again. */
1785 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1786 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1788 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1789 alter_subreg (curr_id->operand_loc[nop], false);
1790 return true;
1792 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1793 if there may be a problem accessing OPERAND in the outer
1794 mode. */
1795 if ((REG_P (reg)
1796 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1797 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1798 /* Don't reload paradoxical subregs because we could be looping
1799 having repeatedly final regno out of hard regs range. */
1800 && (hard_regno_nregs (hard_regno, innermode)
1801 >= hard_regno_nregs (hard_regno, mode))
1802 && simplify_subreg_regno (hard_regno, innermode,
1803 SUBREG_BYTE (operand), mode) < 0
1804 /* Don't reload subreg for matching reload. It is actually
1805 valid subreg in LRA. */
1806 && ! LRA_SUBREG_P (operand))
1807 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1809 enum reg_class rclass;
1811 if (REG_P (reg))
1812 /* There is a big probability that we will get the same class
1813 for the new pseudo and we will get the same insn which
1814 means infinite looping. So spill the new pseudo. */
1815 rclass = NO_REGS;
1816 else
1817 /* The class will be defined later in curr_insn_transform. */
1818 rclass
1819 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1821 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1822 rclass, NULL,
1823 TRUE, "subreg reg", &new_reg))
1825 bool insert_before, insert_after;
1826 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1828 insert_before = (type != OP_OUT
1829 || read_modify_subreg_p (operand));
1830 insert_after = (type != OP_IN);
1831 insert_move_for_subreg (insert_before ? &before : NULL,
1832 insert_after ? &after : NULL,
1833 reg, new_reg);
1835 SUBREG_REG (operand) = new_reg;
1836 lra_process_new_insns (curr_insn, before, after,
1837 "Inserting subreg reload");
1838 return true;
1840 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1841 IRA allocates hardreg to the inner pseudo reg according to its mode
1842 instead of the outermode, so the size of the hardreg may not be enough
1843 to contain the outermode operand, in that case we may need to insert
1844 reload for the reg. For the following two types of paradoxical subreg,
1845 we need to insert reload:
1846 1. If the op_type is OP_IN, and the hardreg could not be paired with
1847 other hardreg to contain the outermode operand
1848 (checked by in_hard_reg_set_p), we need to insert the reload.
1849 2. If the op_type is OP_OUT or OP_INOUT.
1851 Here is a paradoxical subreg example showing how the reload is generated:
1853 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1854 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1856 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1857 here, if reg107 is assigned to hardreg R15, because R15 is the last
1858 hardreg, compiler cannot find another hardreg to pair with R15 to
1859 contain TImode data. So we insert a TImode reload reg180 for it.
1860 After reload is inserted:
1862 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1863 (reg:DI 107 [ __comp ])) -1
1864 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1865 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1867 Two reload hard registers will be allocated to reg180 to save TImode data
1868 in LRA_assign.
1870 For LRA pseudos this should normally be handled by the biggest_mode
1871 mechanism. However, it's possible for new uses of an LRA pseudo
1872 to be introduced after we've allocated it, such as when undoing
1873 inheritance, and the allocated register might not then be appropriate
1874 for the new uses. */
1875 else if (REG_P (reg)
1876 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1877 && paradoxical_subreg_p (operand)
1878 && (inner_hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1879 && ((hard_regno
1880 = simplify_subreg_regno (inner_hard_regno, innermode,
1881 SUBREG_BYTE (operand), mode)) < 0
1882 || ((hard_regno_nregs (inner_hard_regno, innermode)
1883 < hard_regno_nregs (hard_regno, mode))
1884 && (regclass = lra_get_allocno_class (REGNO (reg)))
1885 && (type != OP_IN
1886 || !in_hard_reg_set_p (reg_class_contents[regclass],
1887 mode, hard_regno)
1888 || overlaps_hard_reg_set_p (lra_no_alloc_regs,
1889 mode, hard_regno)))))
1891 /* The class will be defined later in curr_insn_transform. */
1892 enum reg_class rclass
1893 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1895 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1896 rclass, NULL,
1897 TRUE, "paradoxical subreg", &new_reg))
1899 rtx subreg;
1900 bool insert_before, insert_after;
1902 PUT_MODE (new_reg, mode);
1903 subreg = gen_lowpart_SUBREG (innermode, new_reg);
1904 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1906 insert_before = (type != OP_OUT);
1907 insert_after = (type != OP_IN);
1908 insert_move_for_subreg (insert_before ? &before : NULL,
1909 insert_after ? &after : NULL,
1910 reg, subreg);
1912 SUBREG_REG (operand) = new_reg;
1913 lra_process_new_insns (curr_insn, before, after,
1914 "Inserting paradoxical subreg reload");
1915 return true;
1917 return false;
1920 /* Return TRUE if X refers for a hard register from SET. */
1921 static bool
1922 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1924 int i, j, x_hard_regno;
1925 machine_mode mode;
1926 const char *fmt;
1927 enum rtx_code code;
1929 if (x == NULL_RTX)
1930 return false;
1931 code = GET_CODE (x);
1932 mode = GET_MODE (x);
1934 if (code == SUBREG)
1936 /* For all SUBREGs we want to check whether the full multi-register
1937 overlaps the set. For normal SUBREGs this means 'get_hard_regno' of
1938 the inner register, for paradoxical SUBREGs this means the
1939 'get_hard_regno' of the full SUBREG and for complete SUBREGs either is
1940 fine. Use the wider mode for all cases. */
1941 rtx subreg = SUBREG_REG (x);
1942 mode = wider_subreg_mode (x);
1943 if (mode == GET_MODE (subreg))
1945 x = subreg;
1946 code = GET_CODE (x);
1950 if (REG_P (x) || SUBREG_P (x))
1952 x_hard_regno = get_hard_regno (x);
1953 return (x_hard_regno >= 0
1954 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1956 fmt = GET_RTX_FORMAT (code);
1957 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1959 if (fmt[i] == 'e')
1961 if (uses_hard_regs_p (XEXP (x, i), set))
1962 return true;
1964 else if (fmt[i] == 'E')
1966 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1967 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1968 return true;
1971 return false;
1974 /* Return true if OP is a spilled pseudo. */
1975 static inline bool
1976 spilled_pseudo_p (rtx op)
1978 return (REG_P (op)
1979 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1982 /* Return true if X is a general constant. */
1983 static inline bool
1984 general_constant_p (rtx x)
1986 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1989 static bool
1990 reg_in_class_p (rtx reg, enum reg_class cl)
1992 if (cl == NO_REGS)
1993 return get_reg_class (REGNO (reg)) == NO_REGS;
1994 return in_class_p (reg, cl, NULL);
1997 /* Return true if SET of RCLASS contains no hard regs which can be
1998 used in MODE. */
1999 static bool
2000 prohibited_class_reg_set_mode_p (enum reg_class rclass,
2001 HARD_REG_SET &set,
2002 machine_mode mode)
2004 HARD_REG_SET temp;
2006 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
2007 temp = set & ~lra_no_alloc_regs;
2008 return (hard_reg_set_subset_p
2009 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
2013 /* Used to check validity info about small class input operands. It
2014 should be incremented at start of processing an insn
2015 alternative. */
2016 static unsigned int curr_small_class_check = 0;
2018 /* Update number of used inputs of class OP_CLASS for operand NOP
2019 of alternative NALT. Return true if we have more such class operands
2020 than the number of available regs. */
2021 static bool
2022 update_and_check_small_class_inputs (int nop, int nalt,
2023 enum reg_class op_class)
2025 static unsigned int small_class_check[LIM_REG_CLASSES];
2026 static int small_class_input_nums[LIM_REG_CLASSES];
2028 if (SMALL_REGISTER_CLASS_P (op_class)
2029 /* We are interesting in classes became small because of fixing
2030 some hard regs, e.g. by an user through GCC options. */
2031 && hard_reg_set_intersect_p (reg_class_contents[op_class],
2032 ira_no_alloc_regs)
2033 && (curr_static_id->operand[nop].type != OP_OUT
2034 || TEST_BIT (curr_static_id->operand[nop].early_clobber_alts, nalt)))
2036 if (small_class_check[op_class] == curr_small_class_check)
2037 small_class_input_nums[op_class]++;
2038 else
2040 small_class_check[op_class] = curr_small_class_check;
2041 small_class_input_nums[op_class] = 1;
2043 if (small_class_input_nums[op_class] > ira_class_hard_regs_num[op_class])
2044 return true;
2046 return false;
2049 /* Major function to choose the current insn alternative and what
2050 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
2051 negative we should consider only this alternative. Return false if
2052 we cannot choose the alternative or find how to reload the
2053 operands. */
2054 static bool
2055 process_alt_operands (int only_alternative)
2057 bool ok_p = false;
2058 int nop, overall, nalt;
2059 int n_alternatives = curr_static_id->n_alternatives;
2060 int n_operands = curr_static_id->n_operands;
2061 /* LOSERS counts the operands that don't fit this alternative and
2062 would require loading. */
2063 int losers;
2064 int addr_losers;
2065 /* REJECT is a count of how undesirable this alternative says it is
2066 if any reloading is required. If the alternative matches exactly
2067 then REJECT is ignored, but otherwise it gets this much counted
2068 against it in addition to the reloading needed. */
2069 int reject;
2070 /* This is defined by '!' or '?' alternative constraint and added to
2071 reject. But in some cases it can be ignored. */
2072 int static_reject;
2073 int op_reject;
2074 /* The number of elements in the following array. */
2075 int early_clobbered_regs_num;
2076 /* Numbers of operands which are early clobber registers. */
2077 int early_clobbered_nops[MAX_RECOG_OPERANDS];
2078 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
2079 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
2080 HARD_REG_SET curr_alt_exclude_start_hard_regs[MAX_RECOG_OPERANDS];
2081 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
2082 bool curr_alt_win[MAX_RECOG_OPERANDS];
2083 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
2084 int curr_alt_matches[MAX_RECOG_OPERANDS];
2085 /* The number of elements in the following array. */
2086 int curr_alt_dont_inherit_ops_num;
2087 /* Numbers of operands whose reload pseudos should not be inherited. */
2088 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
2089 rtx op;
2090 /* The register when the operand is a subreg of register, otherwise the
2091 operand itself. */
2092 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
2093 /* The register if the operand is a register or subreg of register,
2094 otherwise NULL. */
2095 rtx operand_reg[MAX_RECOG_OPERANDS];
2096 int hard_regno[MAX_RECOG_OPERANDS];
2097 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
2098 int reload_nregs, reload_sum;
2099 bool costly_p;
2100 enum reg_class cl;
2102 /* Calculate some data common for all alternatives to speed up the
2103 function. */
2104 for (nop = 0; nop < n_operands; nop++)
2106 rtx reg;
2108 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
2109 /* The real hard regno of the operand after the allocation. */
2110 hard_regno[nop] = get_hard_regno (op);
2112 operand_reg[nop] = reg = op;
2113 biggest_mode[nop] = GET_MODE (op);
2114 if (GET_CODE (op) == SUBREG)
2116 biggest_mode[nop] = wider_subreg_mode (op);
2117 operand_reg[nop] = reg = SUBREG_REG (op);
2119 if (! REG_P (reg))
2120 operand_reg[nop] = NULL_RTX;
2121 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
2122 || ((int) REGNO (reg)
2123 == lra_get_elimination_hard_regno (REGNO (reg))))
2124 no_subreg_reg_operand[nop] = reg;
2125 else
2126 operand_reg[nop] = no_subreg_reg_operand[nop]
2127 /* Just use natural mode for elimination result. It should
2128 be enough for extra constraints hooks. */
2129 = regno_reg_rtx[hard_regno[nop]];
2132 /* The constraints are made of several alternatives. Each operand's
2133 constraint looks like foo,bar,... with commas separating the
2134 alternatives. The first alternatives for all operands go
2135 together, the second alternatives go together, etc.
2137 First loop over alternatives. */
2138 alternative_mask preferred = curr_id->preferred_alternatives;
2139 if (only_alternative >= 0)
2140 preferred &= ALTERNATIVE_BIT (only_alternative);
2142 for (nalt = 0; nalt < n_alternatives; nalt++)
2144 /* Loop over operands for one constraint alternative. */
2145 if (!TEST_BIT (preferred, nalt))
2146 continue;
2148 bool matching_early_clobber[MAX_RECOG_OPERANDS];
2149 curr_small_class_check++;
2150 overall = losers = addr_losers = 0;
2151 static_reject = reject = reload_nregs = reload_sum = 0;
2152 for (nop = 0; nop < n_operands; nop++)
2154 int inc = (curr_static_id
2155 ->operand_alternative[nalt * n_operands + nop].reject);
2156 if (lra_dump_file != NULL && inc != 0)
2157 fprintf (lra_dump_file,
2158 " Staticly defined alt reject+=%d\n", inc);
2159 static_reject += inc;
2160 matching_early_clobber[nop] = 0;
2162 reject += static_reject;
2163 early_clobbered_regs_num = 0;
2165 for (nop = 0; nop < n_operands; nop++)
2167 const char *p;
2168 char *end;
2169 int len, c, m, i, opalt_num, this_alternative_matches;
2170 bool win, did_match, offmemok, early_clobber_p;
2171 /* false => this operand can be reloaded somehow for this
2172 alternative. */
2173 bool badop;
2174 /* true => this operand can be reloaded if the alternative
2175 allows regs. */
2176 bool winreg;
2177 /* True if a constant forced into memory would be OK for
2178 this operand. */
2179 bool constmemok;
2180 enum reg_class this_alternative, this_costly_alternative;
2181 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
2182 HARD_REG_SET this_alternative_exclude_start_hard_regs;
2183 bool this_alternative_match_win, this_alternative_win;
2184 bool this_alternative_offmemok;
2185 bool scratch_p;
2186 machine_mode mode;
2187 enum constraint_num cn;
2189 opalt_num = nalt * n_operands + nop;
2190 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
2192 /* Fast track for no constraints at all. */
2193 curr_alt[nop] = NO_REGS;
2194 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
2195 curr_alt_win[nop] = true;
2196 curr_alt_match_win[nop] = false;
2197 curr_alt_offmemok[nop] = false;
2198 curr_alt_matches[nop] = -1;
2199 continue;
2202 op = no_subreg_reg_operand[nop];
2203 mode = curr_operand_mode[nop];
2205 win = did_match = winreg = offmemok = constmemok = false;
2206 badop = true;
2208 early_clobber_p = false;
2209 p = curr_static_id->operand_alternative[opalt_num].constraint;
2211 this_costly_alternative = this_alternative = NO_REGS;
2212 /* We update set of possible hard regs besides its class
2213 because reg class might be inaccurate. For example,
2214 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
2215 is translated in HI_REGS because classes are merged by
2216 pairs and there is no accurate intermediate class. */
2217 CLEAR_HARD_REG_SET (this_alternative_set);
2218 CLEAR_HARD_REG_SET (this_costly_alternative_set);
2219 CLEAR_HARD_REG_SET (this_alternative_exclude_start_hard_regs);
2220 this_alternative_win = false;
2221 this_alternative_match_win = false;
2222 this_alternative_offmemok = false;
2223 this_alternative_matches = -1;
2225 /* An empty constraint should be excluded by the fast
2226 track. */
2227 lra_assert (*p != 0 && *p != ',');
2229 op_reject = 0;
2230 /* Scan this alternative's specs for this operand; set WIN
2231 if the operand fits any letter in this alternative.
2232 Otherwise, clear BADOP if this operand could fit some
2233 letter after reloads, or set WINREG if this operand could
2234 fit after reloads provided the constraint allows some
2235 registers. */
2236 costly_p = false;
2239 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
2241 case '\0':
2242 len = 0;
2243 break;
2244 case ',':
2245 c = '\0';
2246 break;
2248 case '&':
2249 early_clobber_p = true;
2250 break;
2252 case '$':
2253 op_reject += LRA_MAX_REJECT;
2254 break;
2255 case '^':
2256 op_reject += LRA_LOSER_COST_FACTOR;
2257 break;
2259 case '#':
2260 /* Ignore rest of this alternative. */
2261 c = '\0';
2262 break;
2264 case '0': case '1': case '2': case '3': case '4':
2265 case '5': case '6': case '7': case '8': case '9':
2267 int m_hregno;
2268 bool match_p;
2270 m = strtoul (p, &end, 10);
2271 p = end;
2272 len = 0;
2273 lra_assert (nop > m);
2275 /* Reject matches if we don't know which operand is
2276 bigger. This situation would arguably be a bug in
2277 an .md pattern, but could also occur in a user asm. */
2278 if (!ordered_p (GET_MODE_SIZE (biggest_mode[m]),
2279 GET_MODE_SIZE (biggest_mode[nop])))
2280 break;
2282 /* Don't match wrong asm insn operands for proper
2283 diagnostic later. */
2284 if (INSN_CODE (curr_insn) < 0
2285 && (curr_operand_mode[m] == BLKmode
2286 || curr_operand_mode[nop] == BLKmode)
2287 && curr_operand_mode[m] != curr_operand_mode[nop])
2288 break;
2290 m_hregno = get_hard_regno (*curr_id->operand_loc[m]);
2291 /* We are supposed to match a previous operand.
2292 If we do, we win if that one did. If we do
2293 not, count both of the operands as losers.
2294 (This is too conservative, since most of the
2295 time only a single reload insn will be needed
2296 to make the two operands win. As a result,
2297 this alternative may be rejected when it is
2298 actually desirable.) */
2299 match_p = false;
2300 if (operands_match_p (*curr_id->operand_loc[nop],
2301 *curr_id->operand_loc[m], m_hregno))
2303 /* We should reject matching of an early
2304 clobber operand if the matching operand is
2305 not dying in the insn. */
2306 if (!TEST_BIT (curr_static_id->operand[m]
2307 .early_clobber_alts, nalt)
2308 || operand_reg[nop] == NULL_RTX
2309 || (find_regno_note (curr_insn, REG_DEAD,
2310 REGNO (op))
2311 || REGNO (op) == REGNO (operand_reg[m])))
2312 match_p = true;
2314 if (match_p)
2316 /* If we are matching a non-offsettable
2317 address where an offsettable address was
2318 expected, then we must reject this
2319 combination, because we can't reload
2320 it. */
2321 if (curr_alt_offmemok[m]
2322 && MEM_P (*curr_id->operand_loc[m])
2323 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
2324 continue;
2326 else
2328 /* If the operands do not match and one
2329 operand is INOUT, we can not match them.
2330 Try other possibilities, e.g. other
2331 alternatives or commutative operand
2332 exchange. */
2333 if (curr_static_id->operand[nop].type == OP_INOUT
2334 || curr_static_id->operand[m].type == OP_INOUT)
2335 break;
2336 /* Operands don't match. If the operands are
2337 different user defined explicit hard
2338 registers, then we cannot make them match
2339 when one is early clobber operand. */
2340 if ((REG_P (*curr_id->operand_loc[nop])
2341 || SUBREG_P (*curr_id->operand_loc[nop]))
2342 && (REG_P (*curr_id->operand_loc[m])
2343 || SUBREG_P (*curr_id->operand_loc[m])))
2345 rtx nop_reg = *curr_id->operand_loc[nop];
2346 if (SUBREG_P (nop_reg))
2347 nop_reg = SUBREG_REG (nop_reg);
2348 rtx m_reg = *curr_id->operand_loc[m];
2349 if (SUBREG_P (m_reg))
2350 m_reg = SUBREG_REG (m_reg);
2352 if (REG_P (nop_reg)
2353 && HARD_REGISTER_P (nop_reg)
2354 && REG_USERVAR_P (nop_reg)
2355 && REG_P (m_reg)
2356 && HARD_REGISTER_P (m_reg)
2357 && REG_USERVAR_P (m_reg))
2359 int i;
2361 for (i = 0; i < early_clobbered_regs_num; i++)
2362 if (m == early_clobbered_nops[i])
2363 break;
2364 if (i < early_clobbered_regs_num
2365 || early_clobber_p)
2366 break;
2369 /* Both operands must allow a reload register,
2370 otherwise we cannot make them match. */
2371 if (curr_alt[m] == NO_REGS)
2372 break;
2373 /* Retroactively mark the operand we had to
2374 match as a loser, if it wasn't already and
2375 it wasn't matched to a register constraint
2376 (e.g it might be matched by memory). */
2377 if (curr_alt_win[m]
2378 && (operand_reg[m] == NULL_RTX
2379 || hard_regno[m] < 0))
2381 losers++;
2382 reload_nregs
2383 += (ira_reg_class_max_nregs[curr_alt[m]]
2384 [GET_MODE (*curr_id->operand_loc[m])]);
2387 /* Prefer matching earlyclobber alternative as
2388 it results in less hard regs required for
2389 the insn than a non-matching earlyclobber
2390 alternative. */
2391 if (TEST_BIT (curr_static_id->operand[m]
2392 .early_clobber_alts, nalt))
2394 if (lra_dump_file != NULL)
2395 fprintf
2396 (lra_dump_file,
2397 " %d Matching earlyclobber alt:"
2398 " reject--\n",
2399 nop);
2400 if (!matching_early_clobber[m])
2402 reject--;
2403 matching_early_clobber[m] = 1;
2406 /* Otherwise we prefer no matching
2407 alternatives because it gives more freedom
2408 in RA. */
2409 else if (operand_reg[nop] == NULL_RTX
2410 || (find_regno_note (curr_insn, REG_DEAD,
2411 REGNO (operand_reg[nop]))
2412 == NULL_RTX))
2414 if (lra_dump_file != NULL)
2415 fprintf
2416 (lra_dump_file,
2417 " %d Matching alt: reject+=2\n",
2418 nop);
2419 reject += 2;
2422 /* If we have to reload this operand and some
2423 previous operand also had to match the same
2424 thing as this operand, we don't know how to do
2425 that. */
2426 if (!match_p || !curr_alt_win[m])
2428 for (i = 0; i < nop; i++)
2429 if (curr_alt_matches[i] == m)
2430 break;
2431 if (i < nop)
2432 break;
2434 else
2435 did_match = true;
2437 this_alternative_matches = m;
2438 /* This can be fixed with reloads if the operand
2439 we are supposed to match can be fixed with
2440 reloads. */
2441 badop = false;
2442 this_alternative = curr_alt[m];
2443 this_alternative_set = curr_alt_set[m];
2444 this_alternative_exclude_start_hard_regs
2445 = curr_alt_exclude_start_hard_regs[m];
2446 winreg = this_alternative != NO_REGS;
2447 break;
2450 case 'g':
2451 if (MEM_P (op)
2452 || general_constant_p (op)
2453 || spilled_pseudo_p (op))
2454 win = true;
2455 cl = GENERAL_REGS;
2456 goto reg;
2458 default:
2459 cn = lookup_constraint (p);
2460 switch (get_constraint_type (cn))
2462 case CT_REGISTER:
2463 cl = reg_class_for_constraint (cn);
2464 if (cl != NO_REGS)
2465 goto reg;
2466 break;
2468 case CT_CONST_INT:
2469 if (CONST_INT_P (op)
2470 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2471 win = true;
2472 break;
2474 case CT_MEMORY:
2475 case CT_RELAXED_MEMORY:
2476 if (MEM_P (op)
2477 && satisfies_memory_constraint_p (op, cn))
2478 win = true;
2479 else if (spilled_pseudo_p (op))
2480 win = true;
2482 /* If we didn't already win, we can reload constants
2483 via force_const_mem or put the pseudo value into
2484 memory, or make other memory by reloading the
2485 address like for 'o'. */
2486 if (CONST_POOL_OK_P (mode, op)
2487 || MEM_P (op) || REG_P (op)
2488 /* We can restore the equiv insn by a
2489 reload. */
2490 || equiv_substition_p[nop])
2491 badop = false;
2492 constmemok = true;
2493 offmemok = true;
2494 break;
2496 case CT_ADDRESS:
2497 /* An asm operand with an address constraint
2498 that doesn't satisfy address_operand has
2499 is_address cleared, so that we don't try to
2500 make a non-address fit. */
2501 if (!curr_static_id->operand[nop].is_address)
2502 break;
2503 /* If we didn't already win, we can reload the address
2504 into a base register. */
2505 if (satisfies_address_constraint_p (op, cn))
2506 win = true;
2507 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2508 ADDRESS, SCRATCH);
2509 badop = false;
2510 goto reg;
2512 case CT_FIXED_FORM:
2513 if (constraint_satisfied_p (op, cn))
2514 win = true;
2515 break;
2517 case CT_SPECIAL_MEMORY:
2518 if (satisfies_memory_constraint_p (op, cn))
2519 win = true;
2520 else if (spilled_pseudo_p (op))
2521 win = true;
2522 break;
2524 break;
2526 reg:
2527 if (mode == BLKmode)
2528 break;
2529 this_alternative = reg_class_subunion[this_alternative][cl];
2530 if (hard_reg_set_subset_p (this_alternative_set,
2531 reg_class_contents[cl]))
2532 this_alternative_exclude_start_hard_regs
2533 = ira_exclude_class_mode_regs[cl][mode];
2534 else if (!hard_reg_set_subset_p (reg_class_contents[cl],
2535 this_alternative_set))
2536 this_alternative_exclude_start_hard_regs
2537 |= ira_exclude_class_mode_regs[cl][mode];
2538 this_alternative_set |= reg_class_contents[cl];
2539 if (costly_p)
2541 this_costly_alternative
2542 = reg_class_subunion[this_costly_alternative][cl];
2543 this_costly_alternative_set |= reg_class_contents[cl];
2545 winreg = true;
2546 if (REG_P (op))
2548 if (hard_regno[nop] >= 0
2549 && in_hard_reg_set_p (this_alternative_set,
2550 mode, hard_regno[nop])
2551 && !TEST_HARD_REG_BIT
2552 (this_alternative_exclude_start_hard_regs,
2553 hard_regno[nop]))
2554 win = true;
2555 else if (hard_regno[nop] < 0
2556 && in_class_p (op, this_alternative, NULL))
2557 win = true;
2559 break;
2561 if (c != ' ' && c != '\t')
2562 costly_p = c == '*';
2564 while ((p += len), c);
2566 scratch_p = (operand_reg[nop] != NULL_RTX
2567 && ira_former_scratch_p (REGNO (operand_reg[nop])));
2568 /* Record which operands fit this alternative. */
2569 if (win)
2571 this_alternative_win = true;
2572 if (operand_reg[nop] != NULL_RTX)
2574 if (hard_regno[nop] >= 0)
2576 if (in_hard_reg_set_p (this_costly_alternative_set,
2577 mode, hard_regno[nop]))
2579 if (lra_dump_file != NULL)
2580 fprintf (lra_dump_file,
2581 " %d Costly set: reject++\n",
2582 nop);
2583 reject++;
2586 else
2588 /* Prefer won reg to spilled pseudo under other
2589 equal conditions for possibe inheritance. */
2590 if (! scratch_p)
2592 if (lra_dump_file != NULL)
2593 fprintf
2594 (lra_dump_file,
2595 " %d Non pseudo reload: reject++\n",
2596 nop);
2597 reject++;
2599 if (in_class_p (operand_reg[nop],
2600 this_costly_alternative, NULL))
2602 if (lra_dump_file != NULL)
2603 fprintf
2604 (lra_dump_file,
2605 " %d Non pseudo costly reload:"
2606 " reject++\n",
2607 nop);
2608 reject++;
2611 /* We simulate the behavior of old reload here.
2612 Although scratches need hard registers and it
2613 might result in spilling other pseudos, no reload
2614 insns are generated for the scratches. So it
2615 might cost something but probably less than old
2616 reload pass believes. */
2617 if (scratch_p)
2619 if (lra_dump_file != NULL)
2620 fprintf (lra_dump_file,
2621 " %d Scratch win: reject+=2\n",
2622 nop);
2623 reject += 2;
2627 else if (did_match)
2628 this_alternative_match_win = true;
2629 else
2631 int const_to_mem = 0;
2632 bool no_regs_p;
2634 reject += op_reject;
2635 /* Never do output reload of stack pointer. It makes
2636 impossible to do elimination when SP is changed in
2637 RTL. */
2638 if (op == stack_pointer_rtx && ! frame_pointer_needed
2639 && curr_static_id->operand[nop].type != OP_IN)
2640 goto fail;
2642 /* If this alternative asks for a specific reg class, see if there
2643 is at least one allocatable register in that class. */
2644 no_regs_p
2645 = (this_alternative == NO_REGS
2646 || (hard_reg_set_subset_p
2647 (reg_class_contents[this_alternative],
2648 lra_no_alloc_regs)));
2650 /* For asms, verify that the class for this alternative is possible
2651 for the mode that is specified. */
2652 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2654 int i;
2655 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2656 if (targetm.hard_regno_mode_ok (i, mode)
2657 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2658 mode, i))
2659 break;
2660 if (i == FIRST_PSEUDO_REGISTER)
2661 winreg = false;
2664 /* If this operand accepts a register, and if the
2665 register class has at least one allocatable register,
2666 then this operand can be reloaded. */
2667 if (winreg && !no_regs_p)
2668 badop = false;
2670 if (badop)
2672 if (lra_dump_file != NULL)
2673 fprintf (lra_dump_file,
2674 " alt=%d: Bad operand -- refuse\n",
2675 nalt);
2676 goto fail;
2679 if (this_alternative != NO_REGS)
2681 HARD_REG_SET available_regs
2682 = (reg_class_contents[this_alternative]
2683 & ~((ira_prohibited_class_mode_regs
2684 [this_alternative][mode])
2685 | lra_no_alloc_regs));
2686 if (hard_reg_set_empty_p (available_regs))
2688 /* There are no hard regs holding a value of given
2689 mode. */
2690 if (offmemok)
2692 this_alternative = NO_REGS;
2693 if (lra_dump_file != NULL)
2694 fprintf (lra_dump_file,
2695 " %d Using memory because of"
2696 " a bad mode: reject+=2\n",
2697 nop);
2698 reject += 2;
2700 else
2702 if (lra_dump_file != NULL)
2703 fprintf (lra_dump_file,
2704 " alt=%d: Wrong mode -- refuse\n",
2705 nalt);
2706 goto fail;
2711 /* If not assigned pseudo has a class which a subset of
2712 required reg class, it is a less costly alternative
2713 as the pseudo still can get a hard reg of necessary
2714 class. */
2715 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2716 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2717 && ira_class_subset_p[this_alternative][cl])
2719 if (lra_dump_file != NULL)
2720 fprintf
2721 (lra_dump_file,
2722 " %d Super set class reg: reject-=3\n", nop);
2723 reject -= 3;
2726 this_alternative_offmemok = offmemok;
2727 if (this_costly_alternative != NO_REGS)
2729 if (lra_dump_file != NULL)
2730 fprintf (lra_dump_file,
2731 " %d Costly loser: reject++\n", nop);
2732 reject++;
2734 /* If the operand is dying, has a matching constraint,
2735 and satisfies constraints of the matched operand
2736 which failed to satisfy the own constraints, most probably
2737 the reload for this operand will be gone. */
2738 if (this_alternative_matches >= 0
2739 && !curr_alt_win[this_alternative_matches]
2740 && REG_P (op)
2741 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2742 && (hard_regno[nop] >= 0
2743 ? in_hard_reg_set_p (this_alternative_set,
2744 mode, hard_regno[nop])
2745 : in_class_p (op, this_alternative, NULL)))
2747 if (lra_dump_file != NULL)
2748 fprintf
2749 (lra_dump_file,
2750 " %d Dying matched operand reload: reject++\n",
2751 nop);
2752 reject++;
2754 else
2756 /* Strict_low_part requires to reload the register
2757 not the sub-register. In this case we should
2758 check that a final reload hard reg can hold the
2759 value mode. */
2760 if (curr_static_id->operand[nop].strict_low
2761 && REG_P (op)
2762 && hard_regno[nop] < 0
2763 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2764 && ira_class_hard_regs_num[this_alternative] > 0
2765 && (!targetm.hard_regno_mode_ok
2766 (ira_class_hard_regs[this_alternative][0],
2767 GET_MODE (*curr_id->operand_loc[nop]))))
2769 if (lra_dump_file != NULL)
2770 fprintf
2771 (lra_dump_file,
2772 " alt=%d: Strict low subreg reload -- refuse\n",
2773 nalt);
2774 goto fail;
2776 losers++;
2778 if (operand_reg[nop] != NULL_RTX
2779 /* Output operands and matched input operands are
2780 not inherited. The following conditions do not
2781 exactly describe the previous statement but they
2782 are pretty close. */
2783 && curr_static_id->operand[nop].type != OP_OUT
2784 && (this_alternative_matches < 0
2785 || curr_static_id->operand[nop].type != OP_IN))
2787 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2788 (operand_reg[nop])]
2789 .last_reload);
2791 /* The value of reload_sum has sense only if we
2792 process insns in their order. It happens only on
2793 the first constraints sub-pass when we do most of
2794 reload work. */
2795 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2796 reload_sum += last_reload - bb_reload_num;
2798 /* If this is a constant that is reloaded into the
2799 desired class by copying it to memory first, count
2800 that as another reload. This is consistent with
2801 other code and is required to avoid choosing another
2802 alternative when the constant is moved into memory.
2803 Note that the test here is precisely the same as in
2804 the code below that calls force_const_mem. */
2805 if (CONST_POOL_OK_P (mode, op)
2806 && ((targetm.preferred_reload_class
2807 (op, this_alternative) == NO_REGS)
2808 || no_input_reloads_p))
2810 const_to_mem = 1;
2811 if (! no_regs_p)
2812 losers++;
2815 /* Alternative loses if it requires a type of reload not
2816 permitted for this insn. We can always reload
2817 objects with a REG_UNUSED note. */
2818 if ((curr_static_id->operand[nop].type != OP_IN
2819 && no_output_reloads_p
2820 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2821 || (curr_static_id->operand[nop].type != OP_OUT
2822 && no_input_reloads_p && ! const_to_mem)
2823 || (this_alternative_matches >= 0
2824 && (no_input_reloads_p
2825 || (no_output_reloads_p
2826 && (curr_static_id->operand
2827 [this_alternative_matches].type != OP_IN)
2828 && ! find_reg_note (curr_insn, REG_UNUSED,
2829 no_subreg_reg_operand
2830 [this_alternative_matches])))))
2832 if (lra_dump_file != NULL)
2833 fprintf
2834 (lra_dump_file,
2835 " alt=%d: No input/output reload -- refuse\n",
2836 nalt);
2837 goto fail;
2840 /* Alternative loses if it required class pseudo cannot
2841 hold value of required mode. Such insns can be
2842 described by insn definitions with mode iterators. */
2843 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2844 && ! hard_reg_set_empty_p (this_alternative_set)
2845 /* It is common practice for constraints to use a
2846 class which does not have actually enough regs to
2847 hold the value (e.g. x86 AREG for mode requiring
2848 more one general reg). Therefore we have 2
2849 conditions to check that the reload pseudo cannot
2850 hold the mode value. */
2851 && (!targetm.hard_regno_mode_ok
2852 (ira_class_hard_regs[this_alternative][0],
2853 GET_MODE (*curr_id->operand_loc[nop])))
2854 /* The above condition is not enough as the first
2855 reg in ira_class_hard_regs can be not aligned for
2856 multi-words mode values. */
2857 && (prohibited_class_reg_set_mode_p
2858 (this_alternative, this_alternative_set,
2859 GET_MODE (*curr_id->operand_loc[nop]))))
2861 if (lra_dump_file != NULL)
2862 fprintf (lra_dump_file,
2863 " alt=%d: reload pseudo for op %d "
2864 "cannot hold the mode value -- refuse\n",
2865 nalt, nop);
2866 goto fail;
2869 /* Check strong discouragement of reload of non-constant
2870 into class THIS_ALTERNATIVE. */
2871 if (! CONSTANT_P (op) && ! no_regs_p
2872 && (targetm.preferred_reload_class
2873 (op, this_alternative) == NO_REGS
2874 || (curr_static_id->operand[nop].type == OP_OUT
2875 && (targetm.preferred_output_reload_class
2876 (op, this_alternative) == NO_REGS))))
2878 if (offmemok && REG_P (op))
2880 if (lra_dump_file != NULL)
2881 fprintf
2882 (lra_dump_file,
2883 " %d Spill pseudo into memory: reject+=3\n",
2884 nop);
2885 reject += 3;
2887 else
2889 if (lra_dump_file != NULL)
2890 fprintf
2891 (lra_dump_file,
2892 " %d Non-prefered reload: reject+=%d\n",
2893 nop, LRA_MAX_REJECT);
2894 reject += LRA_MAX_REJECT;
2898 if (! (MEM_P (op) && offmemok)
2899 && ! (const_to_mem && constmemok))
2901 /* We prefer to reload pseudos over reloading other
2902 things, since such reloads may be able to be
2903 eliminated later. So bump REJECT in other cases.
2904 Don't do this in the case where we are forcing a
2905 constant into memory and it will then win since
2906 we don't want to have a different alternative
2907 match then. */
2908 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2910 if (lra_dump_file != NULL)
2911 fprintf
2912 (lra_dump_file,
2913 " %d Non-pseudo reload: reject+=2\n",
2914 nop);
2915 reject += 2;
2918 if (! no_regs_p)
2919 reload_nregs
2920 += ira_reg_class_max_nregs[this_alternative][mode];
2922 if (SMALL_REGISTER_CLASS_P (this_alternative))
2924 if (lra_dump_file != NULL)
2925 fprintf
2926 (lra_dump_file,
2927 " %d Small class reload: reject+=%d\n",
2928 nop, LRA_LOSER_COST_FACTOR / 2);
2929 reject += LRA_LOSER_COST_FACTOR / 2;
2933 /* We are trying to spill pseudo into memory. It is
2934 usually more costly than moving to a hard register
2935 although it might takes the same number of
2936 reloads.
2938 Non-pseudo spill may happen also. Suppose a target allows both
2939 register and memory in the operand constraint alternatives,
2940 then it's typical that an eliminable register has a substition
2941 of "base + offset" which can either be reloaded by a simple
2942 "new_reg <= base + offset" which will match the register
2943 constraint, or a similar reg addition followed by further spill
2944 to and reload from memory which will match the memory
2945 constraint, but this memory spill will be much more costly
2946 usually.
2948 Code below increases the reject for both pseudo and non-pseudo
2949 spill. */
2950 if (no_regs_p
2951 && !(MEM_P (op) && offmemok)
2952 && !(REG_P (op) && hard_regno[nop] < 0))
2954 if (lra_dump_file != NULL)
2955 fprintf
2956 (lra_dump_file,
2957 " %d Spill %spseudo into memory: reject+=3\n",
2958 nop, REG_P (op) ? "" : "Non-");
2959 reject += 3;
2960 if (VECTOR_MODE_P (mode))
2962 /* Spilling vectors into memory is usually more
2963 costly as they contain big values. */
2964 if (lra_dump_file != NULL)
2965 fprintf
2966 (lra_dump_file,
2967 " %d Spill vector pseudo: reject+=2\n",
2968 nop);
2969 reject += 2;
2973 /* When we use an operand requiring memory in given
2974 alternative, the insn should write *and* read the
2975 value to/from memory it is costly in comparison with
2976 an insn alternative which does not use memory
2977 (e.g. register or immediate operand). We exclude
2978 memory operand for such case as we can satisfy the
2979 memory constraints by reloading address. */
2980 if (no_regs_p && offmemok && !MEM_P (op))
2982 if (lra_dump_file != NULL)
2983 fprintf
2984 (lra_dump_file,
2985 " Using memory insn operand %d: reject+=3\n",
2986 nop);
2987 reject += 3;
2990 /* If reload requires moving value through secondary
2991 memory, it will need one more insn at least. */
2992 if (this_alternative != NO_REGS
2993 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2994 && ((curr_static_id->operand[nop].type != OP_OUT
2995 && targetm.secondary_memory_needed (GET_MODE (op), cl,
2996 this_alternative))
2997 || (curr_static_id->operand[nop].type != OP_IN
2998 && (targetm.secondary_memory_needed
2999 (GET_MODE (op), this_alternative, cl)))))
3000 losers++;
3002 if (MEM_P (op) && offmemok)
3003 addr_losers++;
3004 else
3006 /* Input reloads can be inherited more often than
3007 output reloads can be removed, so penalize output
3008 reloads. */
3009 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
3011 if (lra_dump_file != NULL)
3012 fprintf
3013 (lra_dump_file,
3014 " %d Non input pseudo reload: reject++\n",
3015 nop);
3016 reject++;
3019 if (curr_static_id->operand[nop].type == OP_INOUT)
3021 if (lra_dump_file != NULL)
3022 fprintf
3023 (lra_dump_file,
3024 " %d Input/Output reload: reject+=%d\n",
3025 nop, LRA_LOSER_COST_FACTOR);
3026 reject += LRA_LOSER_COST_FACTOR;
3031 if (early_clobber_p && ! scratch_p)
3033 if (lra_dump_file != NULL)
3034 fprintf (lra_dump_file,
3035 " %d Early clobber: reject++\n", nop);
3036 reject++;
3038 /* ??? We check early clobbers after processing all operands
3039 (see loop below) and there we update the costs more.
3040 Should we update the cost (may be approximately) here
3041 because of early clobber register reloads or it is a rare
3042 or non-important thing to be worth to do it. */
3043 overall = (losers * LRA_LOSER_COST_FACTOR + reject
3044 - (addr_losers == losers ? static_reject : 0));
3045 if ((best_losers == 0 || losers != 0) && best_overall < overall)
3047 if (lra_dump_file != NULL)
3048 fprintf (lra_dump_file,
3049 " alt=%d,overall=%d,losers=%d -- refuse\n",
3050 nalt, overall, losers);
3051 goto fail;
3054 if (update_and_check_small_class_inputs (nop, nalt,
3055 this_alternative))
3057 if (lra_dump_file != NULL)
3058 fprintf (lra_dump_file,
3059 " alt=%d, not enough small class regs -- refuse\n",
3060 nalt);
3061 goto fail;
3063 curr_alt[nop] = this_alternative;
3064 curr_alt_set[nop] = this_alternative_set;
3065 curr_alt_exclude_start_hard_regs[nop]
3066 = this_alternative_exclude_start_hard_regs;
3067 curr_alt_win[nop] = this_alternative_win;
3068 curr_alt_match_win[nop] = this_alternative_match_win;
3069 curr_alt_offmemok[nop] = this_alternative_offmemok;
3070 curr_alt_matches[nop] = this_alternative_matches;
3072 if (this_alternative_matches >= 0
3073 && !did_match && !this_alternative_win)
3074 curr_alt_win[this_alternative_matches] = false;
3076 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
3077 early_clobbered_nops[early_clobbered_regs_num++] = nop;
3080 if (curr_insn_set != NULL_RTX && n_operands == 2
3081 /* Prevent processing non-move insns. */
3082 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
3083 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
3084 && ((! curr_alt_win[0] && ! curr_alt_win[1]
3085 && REG_P (no_subreg_reg_operand[0])
3086 && REG_P (no_subreg_reg_operand[1])
3087 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
3088 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
3089 || (! curr_alt_win[0] && curr_alt_win[1]
3090 && REG_P (no_subreg_reg_operand[1])
3091 /* Check that we reload memory not the memory
3092 address. */
3093 && ! (curr_alt_offmemok[0]
3094 && MEM_P (no_subreg_reg_operand[0]))
3095 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
3096 || (curr_alt_win[0] && ! curr_alt_win[1]
3097 && REG_P (no_subreg_reg_operand[0])
3098 /* Check that we reload memory not the memory
3099 address. */
3100 && ! (curr_alt_offmemok[1]
3101 && MEM_P (no_subreg_reg_operand[1]))
3102 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
3103 && (! CONST_POOL_OK_P (curr_operand_mode[1],
3104 no_subreg_reg_operand[1])
3105 || (targetm.preferred_reload_class
3106 (no_subreg_reg_operand[1],
3107 (enum reg_class) curr_alt[1]) != NO_REGS))
3108 /* If it is a result of recent elimination in move
3109 insn we can transform it into an add still by
3110 using this alternative. */
3111 && GET_CODE (no_subreg_reg_operand[1]) != PLUS
3112 /* Likewise if the source has been replaced with an
3113 equivalent value. This only happens once -- the reload
3114 will use the equivalent value instead of the register it
3115 replaces -- so there should be no danger of cycling. */
3116 && !equiv_substition_p[1])))
3118 /* We have a move insn and a new reload insn will be similar
3119 to the current insn. We should avoid such situation as
3120 it results in LRA cycling. */
3121 if (lra_dump_file != NULL)
3122 fprintf (lra_dump_file,
3123 " Cycle danger: overall += LRA_MAX_REJECT\n");
3124 overall += LRA_MAX_REJECT;
3126 ok_p = true;
3127 curr_alt_dont_inherit_ops_num = 0;
3128 for (nop = 0; nop < early_clobbered_regs_num; nop++)
3130 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
3131 HARD_REG_SET temp_set;
3133 i = early_clobbered_nops[nop];
3134 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
3135 || hard_regno[i] < 0)
3136 continue;
3137 lra_assert (operand_reg[i] != NULL_RTX);
3138 clobbered_hard_regno = hard_regno[i];
3139 CLEAR_HARD_REG_SET (temp_set);
3140 add_to_hard_reg_set (&temp_set, GET_MODE (*curr_id->operand_loc[i]),
3141 clobbered_hard_regno);
3142 first_conflict_j = last_conflict_j = -1;
3143 for (j = 0; j < n_operands; j++)
3144 if (j == i
3145 /* We don't want process insides of match_operator and
3146 match_parallel because otherwise we would process
3147 their operands once again generating a wrong
3148 code. */
3149 || curr_static_id->operand[j].is_operator)
3150 continue;
3151 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
3152 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
3153 continue;
3154 /* If we don't reload j-th operand, check conflicts. */
3155 else if ((curr_alt_win[j] || curr_alt_match_win[j])
3156 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
3158 if (first_conflict_j < 0)
3159 first_conflict_j = j;
3160 last_conflict_j = j;
3161 /* Both the earlyclobber operand and conflicting operand
3162 cannot both be user defined hard registers. */
3163 if (HARD_REGISTER_P (operand_reg[i])
3164 && REG_USERVAR_P (operand_reg[i])
3165 && operand_reg[j] != NULL_RTX
3166 && HARD_REGISTER_P (operand_reg[j])
3167 && REG_USERVAR_P (operand_reg[j]))
3169 /* For asm, let curr_insn_transform diagnose it. */
3170 if (INSN_CODE (curr_insn) < 0)
3171 return false;
3172 fatal_insn ("unable to generate reloads for "
3173 "impossible constraints:", curr_insn);
3176 if (last_conflict_j < 0)
3177 continue;
3179 /* If an earlyclobber operand conflicts with another non-matching
3180 operand (ie, they have been assigned the same hard register),
3181 then it is better to reload the other operand, as there may
3182 exist yet another operand with a matching constraint associated
3183 with the earlyclobber operand. However, if one of the operands
3184 is an explicit use of a hard register, then we must reload the
3185 other non-hard register operand. */
3186 if (HARD_REGISTER_P (operand_reg[i])
3187 || (first_conflict_j == last_conflict_j
3188 && operand_reg[last_conflict_j] != NULL_RTX
3189 && !curr_alt_match_win[last_conflict_j]
3190 && !HARD_REGISTER_P (operand_reg[last_conflict_j])))
3192 curr_alt_win[last_conflict_j] = false;
3193 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
3194 = last_conflict_j;
3195 losers++;
3196 if (lra_dump_file != NULL)
3197 fprintf
3198 (lra_dump_file,
3199 " %d Conflict early clobber reload: reject--\n",
3202 else
3204 /* We need to reload early clobbered register and the
3205 matched registers. */
3206 for (j = 0; j < n_operands; j++)
3207 if (curr_alt_matches[j] == i)
3209 curr_alt_match_win[j] = false;
3210 losers++;
3211 overall += LRA_LOSER_COST_FACTOR;
3213 if (! curr_alt_match_win[i])
3214 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
3215 else
3217 /* Remember pseudos used for match reloads are never
3218 inherited. */
3219 lra_assert (curr_alt_matches[i] >= 0);
3220 curr_alt_win[curr_alt_matches[i]] = false;
3222 curr_alt_win[i] = curr_alt_match_win[i] = false;
3223 losers++;
3224 if (lra_dump_file != NULL)
3225 fprintf
3226 (lra_dump_file,
3227 " %d Matched conflict early clobber reloads: "
3228 "reject--\n",
3231 /* Early clobber was already reflected in REJECT. */
3232 if (!matching_early_clobber[i])
3234 lra_assert (reject > 0);
3235 reject--;
3236 matching_early_clobber[i] = 1;
3238 overall += LRA_LOSER_COST_FACTOR - 1;
3240 if (lra_dump_file != NULL)
3241 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
3242 nalt, overall, losers, reload_nregs);
3244 /* If this alternative can be made to work by reloading, and it
3245 needs less reloading than the others checked so far, record
3246 it as the chosen goal for reloading. */
3247 if ((best_losers != 0 && losers == 0)
3248 || (((best_losers == 0 && losers == 0)
3249 || (best_losers != 0 && losers != 0))
3250 && (best_overall > overall
3251 || (best_overall == overall
3252 /* If the cost of the reloads is the same,
3253 prefer alternative which requires minimal
3254 number of reload regs. */
3255 && (reload_nregs < best_reload_nregs
3256 || (reload_nregs == best_reload_nregs
3257 && (best_reload_sum < reload_sum
3258 || (best_reload_sum == reload_sum
3259 && nalt < goal_alt_number))))))))
3261 for (nop = 0; nop < n_operands; nop++)
3263 goal_alt_win[nop] = curr_alt_win[nop];
3264 goal_alt_match_win[nop] = curr_alt_match_win[nop];
3265 goal_alt_matches[nop] = curr_alt_matches[nop];
3266 goal_alt[nop] = curr_alt[nop];
3267 goal_alt_exclude_start_hard_regs[nop]
3268 = curr_alt_exclude_start_hard_regs[nop];
3269 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
3271 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
3272 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
3273 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
3274 goal_alt_swapped = curr_swapped;
3275 best_overall = overall;
3276 best_losers = losers;
3277 best_reload_nregs = reload_nregs;
3278 best_reload_sum = reload_sum;
3279 goal_alt_number = nalt;
3281 if (losers == 0)
3282 /* Everything is satisfied. Do not process alternatives
3283 anymore. */
3284 break;
3285 fail:
3288 return ok_p;
3291 /* Make reload base reg from address AD. */
3292 static rtx
3293 base_to_reg (struct address_info *ad)
3295 enum reg_class cl;
3296 int code = -1;
3297 rtx new_inner = NULL_RTX;
3298 rtx new_reg = NULL_RTX;
3299 rtx_insn *insn;
3300 rtx_insn *last_insn = get_last_insn();
3302 lra_assert (ad->disp == ad->disp_term);
3303 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
3304 get_index_code (ad));
3305 new_reg = lra_create_new_reg (GET_MODE (*ad->base), NULL_RTX, cl, NULL,
3306 "base");
3307 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
3308 ad->disp_term == NULL
3309 ? const0_rtx
3310 : *ad->disp_term);
3311 if (!valid_address_p (ad->mode, new_inner, ad->as))
3312 return NULL_RTX;
3313 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base));
3314 code = recog_memoized (insn);
3315 if (code < 0)
3317 delete_insns_since (last_insn);
3318 return NULL_RTX;
3321 return new_inner;
3324 /* Make reload base reg + DISP from address AD. Return the new pseudo. */
3325 static rtx
3326 base_plus_disp_to_reg (struct address_info *ad, rtx disp)
3328 enum reg_class cl;
3329 rtx new_reg;
3331 lra_assert (ad->base == ad->base_term);
3332 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
3333 get_index_code (ad));
3334 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX, cl, NULL,
3335 "base + disp");
3336 lra_emit_add (new_reg, *ad->base_term, disp);
3337 return new_reg;
3340 /* Make reload of index part of address AD. Return the new
3341 pseudo. */
3342 static rtx
3343 index_part_to_reg (struct address_info *ad)
3345 rtx new_reg;
3347 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
3348 INDEX_REG_CLASS, NULL, "index term");
3349 expand_mult (GET_MODE (*ad->index), *ad->index_term,
3350 GEN_INT (get_index_scale (ad)), new_reg, 1);
3351 return new_reg;
3354 /* Return true if we can add a displacement to address AD, even if that
3355 makes the address invalid. The fix-up code requires any new address
3356 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
3357 static bool
3358 can_add_disp_p (struct address_info *ad)
3360 return (!ad->autoinc_p
3361 && ad->segment == NULL
3362 && ad->base == ad->base_term
3363 && ad->disp == ad->disp_term);
3366 /* Make equiv substitution in address AD. Return true if a substitution
3367 was made. */
3368 static bool
3369 equiv_address_substitution (struct address_info *ad)
3371 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
3372 poly_int64 disp;
3373 HOST_WIDE_INT scale;
3374 bool change_p;
3376 base_term = strip_subreg (ad->base_term);
3377 if (base_term == NULL)
3378 base_reg = new_base_reg = NULL_RTX;
3379 else
3381 base_reg = *base_term;
3382 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
3384 index_term = strip_subreg (ad->index_term);
3385 if (index_term == NULL)
3386 index_reg = new_index_reg = NULL_RTX;
3387 else
3389 index_reg = *index_term;
3390 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
3392 if (base_reg == new_base_reg && index_reg == new_index_reg)
3393 return false;
3394 disp = 0;
3395 change_p = false;
3396 if (lra_dump_file != NULL)
3398 fprintf (lra_dump_file, "Changing address in insn %d ",
3399 INSN_UID (curr_insn));
3400 dump_value_slim (lra_dump_file, *ad->outer, 1);
3402 if (base_reg != new_base_reg)
3404 poly_int64 offset;
3405 if (REG_P (new_base_reg))
3407 *base_term = new_base_reg;
3408 change_p = true;
3410 else if (GET_CODE (new_base_reg) == PLUS
3411 && REG_P (XEXP (new_base_reg, 0))
3412 && poly_int_rtx_p (XEXP (new_base_reg, 1), &offset)
3413 && can_add_disp_p (ad))
3415 disp += offset;
3416 *base_term = XEXP (new_base_reg, 0);
3417 change_p = true;
3419 if (ad->base_term2 != NULL)
3420 *ad->base_term2 = *ad->base_term;
3422 if (index_reg != new_index_reg)
3424 poly_int64 offset;
3425 if (REG_P (new_index_reg))
3427 *index_term = new_index_reg;
3428 change_p = true;
3430 else if (GET_CODE (new_index_reg) == PLUS
3431 && REG_P (XEXP (new_index_reg, 0))
3432 && poly_int_rtx_p (XEXP (new_index_reg, 1), &offset)
3433 && can_add_disp_p (ad)
3434 && (scale = get_index_scale (ad)))
3436 disp += offset * scale;
3437 *index_term = XEXP (new_index_reg, 0);
3438 change_p = true;
3441 if (maybe_ne (disp, 0))
3443 if (ad->disp != NULL)
3444 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
3445 else
3447 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
3448 update_address (ad);
3450 change_p = true;
3452 if (lra_dump_file != NULL)
3454 if (! change_p)
3455 fprintf (lra_dump_file, " -- no change\n");
3456 else
3458 fprintf (lra_dump_file, " on equiv ");
3459 dump_value_slim (lra_dump_file, *ad->outer, 1);
3460 fprintf (lra_dump_file, "\n");
3463 return change_p;
3466 /* Skip all modifiers and whitespaces in constraint STR and return the
3467 result. */
3468 static const char *
3469 skip_constraint_modifiers (const char *str)
3471 for (;;str++)
3472 switch (*str)
3474 case '+': case '&' : case '=': case '*': case ' ': case '\t':
3475 case '$': case '^' : case '%': case '?': case '!':
3476 break;
3477 default: return str;
3481 /* Takes a string of 0 or more comma-separated constraints. When more
3482 than one constraint is present, evaluate whether they all correspond
3483 to a single, repeated constraint (e.g. "r,r") or whether we have
3484 more than one distinct constraints (e.g. "r,m"). */
3485 static bool
3486 constraint_unique (const char *cstr)
3488 enum constraint_num ca, cb;
3489 ca = CONSTRAINT__UNKNOWN;
3490 for (;;)
3492 cstr = skip_constraint_modifiers (cstr);
3493 if (*cstr == '\0' || *cstr == ',')
3494 cb = CONSTRAINT_X;
3495 else
3497 cb = lookup_constraint (cstr);
3498 if (cb == CONSTRAINT__UNKNOWN)
3499 return false;
3500 cstr += CONSTRAINT_LEN (cstr[0], cstr);
3502 /* Handle the first iteration of the loop. */
3503 if (ca == CONSTRAINT__UNKNOWN)
3504 ca = cb;
3505 /* Handle the general case of comparing ca with subsequent
3506 constraints. */
3507 else if (ca != cb)
3508 return false;
3509 if (*cstr == '\0')
3510 return true;
3511 if (*cstr == ',')
3512 cstr += 1;
3516 /* Major function to make reloads for an address in operand NOP or
3517 check its correctness (If CHECK_ONLY_P is true). The supported
3518 cases are:
3520 1) an address that existed before LRA started, at which point it
3521 must have been valid. These addresses are subject to elimination
3522 and may have become invalid due to the elimination offset being out
3523 of range.
3525 2) an address created by forcing a constant to memory
3526 (force_const_to_mem). The initial form of these addresses might
3527 not be valid, and it is this function's job to make them valid.
3529 3) a frame address formed from a register and a (possibly zero)
3530 constant offset. As above, these addresses might not be valid and
3531 this function must make them so.
3533 Add reloads to the lists *BEFORE and *AFTER. We might need to add
3534 reloads to *AFTER because of inc/dec, {pre, post} modify in the
3535 address. Return true for any RTL change.
3537 The function is a helper function which does not produce all
3538 transformations (when CHECK_ONLY_P is false) which can be
3539 necessary. It does just basic steps. To do all necessary
3540 transformations use function process_address. */
3541 static bool
3542 process_address_1 (int nop, bool check_only_p,
3543 rtx_insn **before, rtx_insn **after)
3545 struct address_info ad;
3546 rtx new_reg;
3547 HOST_WIDE_INT scale;
3548 rtx op = *curr_id->operand_loc[nop];
3549 rtx mem = extract_mem_from_operand (op);
3550 const char *constraint;
3551 enum constraint_num cn;
3552 bool change_p = false;
3554 if (MEM_P (mem)
3555 && GET_MODE (mem) == BLKmode
3556 && GET_CODE (XEXP (mem, 0)) == SCRATCH)
3557 return false;
3559 constraint
3560 = skip_constraint_modifiers (curr_static_id->operand[nop].constraint);
3561 if (IN_RANGE (constraint[0], '0', '9'))
3563 char *end;
3564 unsigned long dup = strtoul (constraint, &end, 10);
3565 constraint
3566 = skip_constraint_modifiers (curr_static_id->operand[dup].constraint);
3568 cn = lookup_constraint (*constraint == '\0' ? "X" : constraint);
3569 /* If we have several alternatives or/and several constraints in an
3570 alternative and we can not say at this stage what constraint will be used,
3571 use unknown constraint. The exception is an address constraint. If
3572 operand has one address constraint, probably all others constraints are
3573 address ones. */
3574 if (constraint[0] != '\0' && get_constraint_type (cn) != CT_ADDRESS
3575 && !constraint_unique (constraint))
3576 cn = CONSTRAINT__UNKNOWN;
3577 if (insn_extra_address_constraint (cn)
3578 /* When we find an asm operand with an address constraint that
3579 doesn't satisfy address_operand to begin with, we clear
3580 is_address, so that we don't try to make a non-address fit.
3581 If the asm statement got this far, it's because other
3582 constraints are available, and we'll use them, disregarding
3583 the unsatisfiable address ones. */
3584 && curr_static_id->operand[nop].is_address)
3585 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
3586 /* Do not attempt to decompose arbitrary addresses generated by combine
3587 for asm operands with loose constraints, e.g 'X'.
3588 Need to extract memory from op for special memory constraint,
3589 i.e. bcst_mem_operand in i386 backend. */
3590 else if (MEM_P (mem)
3591 && !(INSN_CODE (curr_insn) < 0
3592 && get_constraint_type (cn) == CT_FIXED_FORM
3593 && constraint_satisfied_p (op, cn)))
3594 decompose_mem_address (&ad, mem);
3595 else if (GET_CODE (op) == SUBREG
3596 && MEM_P (SUBREG_REG (op)))
3597 decompose_mem_address (&ad, SUBREG_REG (op));
3598 else
3599 return false;
3600 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
3601 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
3602 when INDEX_REG_CLASS is a single register class. */
3603 if (ad.base_term != NULL
3604 && ad.index_term != NULL
3605 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
3606 && REG_P (*ad.base_term)
3607 && REG_P (*ad.index_term)
3608 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
3609 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
3611 std::swap (ad.base, ad.index);
3612 std::swap (ad.base_term, ad.index_term);
3614 if (! check_only_p)
3615 change_p = equiv_address_substitution (&ad);
3616 if (ad.base_term != NULL
3617 && (process_addr_reg
3618 (ad.base_term, check_only_p, before,
3619 (ad.autoinc_p
3620 && !(REG_P (*ad.base_term)
3621 && find_regno_note (curr_insn, REG_DEAD,
3622 REGNO (*ad.base_term)) != NULL_RTX)
3623 ? after : NULL),
3624 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3625 get_index_code (&ad)))))
3627 change_p = true;
3628 if (ad.base_term2 != NULL)
3629 *ad.base_term2 = *ad.base_term;
3631 if (ad.index_term != NULL
3632 && process_addr_reg (ad.index_term, check_only_p,
3633 before, NULL, INDEX_REG_CLASS))
3634 change_p = true;
3636 /* Target hooks sometimes don't treat extra-constraint addresses as
3637 legitimate address_operands, so handle them specially. */
3638 if (insn_extra_address_constraint (cn)
3639 && satisfies_address_constraint_p (&ad, cn))
3640 return change_p;
3642 if (check_only_p)
3643 return change_p;
3645 /* There are three cases where the shape of *AD.INNER may now be invalid:
3647 1) the original address was valid, but either elimination or
3648 equiv_address_substitution was applied and that made
3649 the address invalid.
3651 2) the address is an invalid symbolic address created by
3652 force_const_to_mem.
3654 3) the address is a frame address with an invalid offset.
3656 4) the address is a frame address with an invalid base.
3658 All these cases involve a non-autoinc address, so there is no
3659 point revalidating other types. */
3660 if (ad.autoinc_p || valid_address_p (op, &ad, cn))
3661 return change_p;
3663 /* Any index existed before LRA started, so we can assume that the
3664 presence and shape of the index is valid. */
3665 push_to_sequence (*before);
3666 lra_assert (ad.disp == ad.disp_term);
3667 if (ad.base == NULL)
3669 if (ad.index == NULL)
3671 rtx_insn *insn;
3672 rtx_insn *last = get_last_insn ();
3673 int code = -1;
3674 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3675 SCRATCH, SCRATCH);
3676 rtx addr = *ad.inner;
3678 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, NULL, "addr");
3679 if (HAVE_lo_sum)
3681 /* addr => lo_sum (new_base, addr), case (2) above. */
3682 insn = emit_insn (gen_rtx_SET
3683 (new_reg,
3684 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
3685 code = recog_memoized (insn);
3686 if (code >= 0)
3688 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
3689 if (!valid_address_p (op, &ad, cn))
3691 /* Try to put lo_sum into register. */
3692 insn = emit_insn (gen_rtx_SET
3693 (new_reg,
3694 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
3695 code = recog_memoized (insn);
3696 if (code >= 0)
3698 *ad.inner = new_reg;
3699 if (!valid_address_p (op, &ad, cn))
3701 *ad.inner = addr;
3702 code = -1;
3708 if (code < 0)
3709 delete_insns_since (last);
3712 if (code < 0)
3714 /* addr => new_base, case (2) above. */
3715 lra_emit_move (new_reg, addr);
3717 for (insn = last == NULL_RTX ? get_insns () : NEXT_INSN (last);
3718 insn != NULL_RTX;
3719 insn = NEXT_INSN (insn))
3720 if (recog_memoized (insn) < 0)
3721 break;
3722 if (insn != NULL_RTX)
3724 /* Do nothing if we cannot generate right insns.
3725 This is analogous to reload pass behavior. */
3726 delete_insns_since (last);
3727 end_sequence ();
3728 return false;
3730 *ad.inner = new_reg;
3733 else
3735 /* index * scale + disp => new base + index * scale,
3736 case (1) above. */
3737 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3738 GET_CODE (*ad.index));
3740 lra_assert (INDEX_REG_CLASS != NO_REGS);
3741 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, NULL, "disp");
3742 lra_emit_move (new_reg, *ad.disp);
3743 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3744 new_reg, *ad.index);
3747 else if (ad.index == NULL)
3749 int regno;
3750 enum reg_class cl;
3751 rtx set;
3752 rtx_insn *insns, *last_insn;
3753 /* Try to reload base into register only if the base is invalid
3754 for the address but with valid offset, case (4) above. */
3755 start_sequence ();
3756 new_reg = base_to_reg (&ad);
3758 /* base + disp => new base, cases (1) and (3) above. */
3759 /* Another option would be to reload the displacement into an
3760 index register. However, postreload has code to optimize
3761 address reloads that have the same base and different
3762 displacements, so reloading into an index register would
3763 not necessarily be a win. */
3764 if (new_reg == NULL_RTX)
3766 /* See if the target can split the displacement into a
3767 legitimate new displacement from a local anchor. */
3768 gcc_assert (ad.disp == ad.disp_term);
3769 poly_int64 orig_offset;
3770 rtx offset1, offset2;
3771 if (poly_int_rtx_p (*ad.disp, &orig_offset)
3772 && targetm.legitimize_address_displacement (&offset1, &offset2,
3773 orig_offset,
3774 ad.mode))
3776 new_reg = base_plus_disp_to_reg (&ad, offset1);
3777 new_reg = gen_rtx_PLUS (GET_MODE (new_reg), new_reg, offset2);
3779 else
3780 new_reg = base_plus_disp_to_reg (&ad, *ad.disp);
3782 insns = get_insns ();
3783 last_insn = get_last_insn ();
3784 /* If we generated at least two insns, try last insn source as
3785 an address. If we succeed, we generate one less insn. */
3786 if (REG_P (new_reg)
3787 && last_insn != insns
3788 && (set = single_set (last_insn)) != NULL_RTX
3789 && GET_CODE (SET_SRC (set)) == PLUS
3790 && REG_P (XEXP (SET_SRC (set), 0))
3791 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3793 *ad.inner = SET_SRC (set);
3794 if (valid_address_p (op, &ad, cn))
3796 *ad.base_term = XEXP (SET_SRC (set), 0);
3797 *ad.disp_term = XEXP (SET_SRC (set), 1);
3798 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3799 get_index_code (&ad));
3800 regno = REGNO (*ad.base_term);
3801 if (regno >= FIRST_PSEUDO_REGISTER
3802 && cl != lra_get_allocno_class (regno))
3803 lra_change_class (regno, cl, " Change to", true);
3804 new_reg = SET_SRC (set);
3805 delete_insns_since (PREV_INSN (last_insn));
3808 end_sequence ();
3809 emit_insn (insns);
3810 *ad.inner = new_reg;
3812 else if (ad.disp_term != NULL)
3814 /* base + scale * index + disp => new base + scale * index,
3815 case (1) above. */
3816 gcc_assert (ad.disp == ad.disp_term);
3817 new_reg = base_plus_disp_to_reg (&ad, *ad.disp);
3818 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3819 new_reg, *ad.index);
3821 else if ((scale = get_index_scale (&ad)) == 1)
3823 /* The last transformation to one reg will be made in
3824 curr_insn_transform function. */
3825 end_sequence ();
3826 return false;
3828 else if (scale != 0)
3830 /* base + scale * index => base + new_reg,
3831 case (1) above.
3832 Index part of address may become invalid. For example, we
3833 changed pseudo on the equivalent memory and a subreg of the
3834 pseudo onto the memory of different mode for which the scale is
3835 prohibitted. */
3836 new_reg = index_part_to_reg (&ad);
3837 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3838 *ad.base_term, new_reg);
3840 else
3842 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3843 SCRATCH, SCRATCH);
3844 rtx addr = *ad.inner;
3846 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, NULL, "addr");
3847 /* addr => new_base. */
3848 lra_emit_move (new_reg, addr);
3849 *ad.inner = new_reg;
3851 *before = get_insns ();
3852 end_sequence ();
3853 return true;
3856 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3857 Use process_address_1 as a helper function. Return true for any
3858 RTL changes.
3860 If CHECK_ONLY_P is true, just check address correctness. Return
3861 false if the address correct. */
3862 static bool
3863 process_address (int nop, bool check_only_p,
3864 rtx_insn **before, rtx_insn **after)
3866 bool res = false;
3868 while (process_address_1 (nop, check_only_p, before, after))
3870 if (check_only_p)
3871 return true;
3872 res = true;
3874 return res;
3877 /* Emit insns to reload VALUE into a new register. VALUE is an
3878 auto-increment or auto-decrement RTX whose operand is a register or
3879 memory location; so reloading involves incrementing that location.
3880 IN is either identical to VALUE, or some cheaper place to reload
3881 value being incremented/decremented from.
3883 INC_AMOUNT is the number to increment or decrement by (always
3884 positive and ignored for POST_MODIFY/PRE_MODIFY).
3886 Return pseudo containing the result. */
3887 static rtx
3888 emit_inc (enum reg_class new_rclass, rtx in, rtx value, poly_int64 inc_amount)
3890 /* REG or MEM to be copied and incremented. */
3891 rtx incloc = XEXP (value, 0);
3892 /* Nonzero if increment after copying. */
3893 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3894 || GET_CODE (value) == POST_MODIFY);
3895 rtx_insn *last;
3896 rtx inc;
3897 rtx_insn *add_insn;
3898 int code;
3899 rtx real_in = in == value ? incloc : in;
3900 rtx result;
3901 bool plus_p = true;
3903 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3905 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3906 || GET_CODE (XEXP (value, 1)) == MINUS);
3907 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3908 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3909 inc = XEXP (XEXP (value, 1), 1);
3911 else
3913 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3914 inc_amount = -inc_amount;
3916 inc = gen_int_mode (inc_amount, GET_MODE (value));
3919 if (! post && REG_P (incloc))
3920 result = incloc;
3921 else
3922 result = lra_create_new_reg (GET_MODE (value), value, new_rclass, NULL,
3923 "INC/DEC result");
3925 if (real_in != result)
3927 /* First copy the location to the result register. */
3928 lra_assert (REG_P (result));
3929 emit_insn (gen_move_insn (result, real_in));
3932 /* We suppose that there are insns to add/sub with the constant
3933 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3934 old reload worked with this assumption. If the assumption
3935 becomes wrong, we should use approach in function
3936 base_plus_disp_to_reg. */
3937 if (in == value)
3939 /* See if we can directly increment INCLOC. */
3940 last = get_last_insn ();
3941 add_insn = emit_insn (plus_p
3942 ? gen_add2_insn (incloc, inc)
3943 : gen_sub2_insn (incloc, inc));
3945 code = recog_memoized (add_insn);
3946 if (code >= 0)
3948 if (! post && result != incloc)
3949 emit_insn (gen_move_insn (result, incloc));
3950 return result;
3952 delete_insns_since (last);
3955 /* If couldn't do the increment directly, must increment in RESULT.
3956 The way we do this depends on whether this is pre- or
3957 post-increment. For pre-increment, copy INCLOC to the reload
3958 register, increment it there, then save back. */
3959 if (! post)
3961 if (real_in != result)
3962 emit_insn (gen_move_insn (result, real_in));
3963 if (plus_p)
3964 emit_insn (gen_add2_insn (result, inc));
3965 else
3966 emit_insn (gen_sub2_insn (result, inc));
3967 if (result != incloc)
3968 emit_insn (gen_move_insn (incloc, result));
3970 else
3972 /* Post-increment.
3974 Because this might be a jump insn or a compare, and because
3975 RESULT may not be available after the insn in an input
3976 reload, we must do the incrementing before the insn being
3977 reloaded for.
3979 We have already copied IN to RESULT. Increment the copy in
3980 RESULT, save that back, then decrement RESULT so it has
3981 the original value. */
3982 if (plus_p)
3983 emit_insn (gen_add2_insn (result, inc));
3984 else
3985 emit_insn (gen_sub2_insn (result, inc));
3986 emit_insn (gen_move_insn (incloc, result));
3987 /* Restore non-modified value for the result. We prefer this
3988 way because it does not require an additional hard
3989 register. */
3990 if (plus_p)
3992 poly_int64 offset;
3993 if (poly_int_rtx_p (inc, &offset))
3994 emit_insn (gen_add2_insn (result,
3995 gen_int_mode (-offset,
3996 GET_MODE (result))));
3997 else
3998 emit_insn (gen_sub2_insn (result, inc));
4000 else
4001 emit_insn (gen_add2_insn (result, inc));
4003 return result;
4006 /* Return true if the current move insn does not need processing as we
4007 already know that it satisfies its constraints. */
4008 static bool
4009 simple_move_p (void)
4011 rtx dest, src;
4012 enum reg_class dclass, sclass;
4014 lra_assert (curr_insn_set != NULL_RTX);
4015 dest = SET_DEST (curr_insn_set);
4016 src = SET_SRC (curr_insn_set);
4018 /* If the instruction has multiple sets we need to process it even if it
4019 is single_set. This can happen if one or more of the SETs are dead.
4020 See PR73650. */
4021 if (multiple_sets (curr_insn))
4022 return false;
4024 return ((dclass = get_op_class (dest)) != NO_REGS
4025 && (sclass = get_op_class (src)) != NO_REGS
4026 /* The backend guarantees that register moves of cost 2
4027 never need reloads. */
4028 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
4031 /* Swap operands NOP and NOP + 1. */
4032 static inline void
4033 swap_operands (int nop)
4035 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
4036 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
4037 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
4038 std::swap (equiv_substition_p[nop], equiv_substition_p[nop + 1]);
4039 /* Swap the duplicates too. */
4040 lra_update_dup (curr_id, nop);
4041 lra_update_dup (curr_id, nop + 1);
4044 /* Main entry point of the constraint code: search the body of the
4045 current insn to choose the best alternative. It is mimicking insn
4046 alternative cost calculation model of former reload pass. That is
4047 because machine descriptions were written to use this model. This
4048 model can be changed in future. Make commutative operand exchange
4049 if it is chosen.
4051 if CHECK_ONLY_P is false, do RTL changes to satisfy the
4052 constraints. Return true if any change happened during function
4053 call.
4055 If CHECK_ONLY_P is true then don't do any transformation. Just
4056 check that the insn satisfies all constraints. If the insn does
4057 not satisfy any constraint, return true. */
4058 static bool
4059 curr_insn_transform (bool check_only_p)
4061 int i, j, k;
4062 int n_operands;
4063 int n_alternatives;
4064 int n_outputs;
4065 int commutative;
4066 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
4067 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
4068 signed char outputs[MAX_RECOG_OPERANDS + 1];
4069 rtx_insn *before, *after;
4070 bool alt_p = false;
4071 /* Flag that the insn has been changed through a transformation. */
4072 bool change_p;
4073 bool sec_mem_p;
4074 bool use_sec_mem_p;
4075 int max_regno_before;
4076 int reused_alternative_num;
4078 curr_insn_set = single_set (curr_insn);
4079 if (curr_insn_set != NULL_RTX && simple_move_p ())
4081 /* We assume that the corresponding insn alternative has no
4082 earlier clobbers. If it is not the case, don't define move
4083 cost equal to 2 for the corresponding register classes. */
4084 lra_set_used_insn_alternative (curr_insn, LRA_NON_CLOBBERED_ALT);
4085 return false;
4088 no_input_reloads_p = no_output_reloads_p = false;
4089 goal_alt_number = -1;
4090 change_p = sec_mem_p = false;
4092 /* CALL_INSNs are not allowed to have any output reloads. */
4093 if (CALL_P (curr_insn))
4094 no_output_reloads_p = true;
4096 n_operands = curr_static_id->n_operands;
4097 n_alternatives = curr_static_id->n_alternatives;
4099 /* Just return "no reloads" if insn has no operands with
4100 constraints. */
4101 if (n_operands == 0 || n_alternatives == 0)
4102 return false;
4104 max_regno_before = max_reg_num ();
4106 for (i = 0; i < n_operands; i++)
4108 goal_alt_matched[i][0] = -1;
4109 goal_alt_matches[i] = -1;
4112 commutative = curr_static_id->commutative;
4114 /* Now see what we need for pseudos that didn't get hard regs or got
4115 the wrong kind of hard reg. For this, we must consider all the
4116 operands together against the register constraints. */
4118 best_losers = best_overall = INT_MAX;
4119 best_reload_sum = 0;
4121 curr_swapped = false;
4122 goal_alt_swapped = false;
4124 if (! check_only_p)
4125 /* Make equivalence substitution and memory subreg elimination
4126 before address processing because an address legitimacy can
4127 depend on memory mode. */
4128 for (i = 0; i < n_operands; i++)
4130 rtx op, subst, old;
4131 bool op_change_p = false;
4133 if (curr_static_id->operand[i].is_operator)
4134 continue;
4136 old = op = *curr_id->operand_loc[i];
4137 if (GET_CODE (old) == SUBREG)
4138 old = SUBREG_REG (old);
4139 subst = get_equiv_with_elimination (old, curr_insn);
4140 original_subreg_reg_mode[i] = VOIDmode;
4141 equiv_substition_p[i] = false;
4142 if (subst != old)
4144 equiv_substition_p[i] = true;
4145 subst = copy_rtx (subst);
4146 lra_assert (REG_P (old));
4147 if (GET_CODE (op) != SUBREG)
4148 *curr_id->operand_loc[i] = subst;
4149 else
4151 SUBREG_REG (op) = subst;
4152 if (GET_MODE (subst) == VOIDmode)
4153 original_subreg_reg_mode[i] = GET_MODE (old);
4155 if (lra_dump_file != NULL)
4157 fprintf (lra_dump_file,
4158 "Changing pseudo %d in operand %i of insn %u on equiv ",
4159 REGNO (old), i, INSN_UID (curr_insn));
4160 dump_value_slim (lra_dump_file, subst, 1);
4161 fprintf (lra_dump_file, "\n");
4163 op_change_p = change_p = true;
4165 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
4167 change_p = true;
4168 lra_update_dup (curr_id, i);
4172 /* Reload address registers and displacements. We do it before
4173 finding an alternative because of memory constraints. */
4174 before = after = NULL;
4175 for (i = 0; i < n_operands; i++)
4176 if (! curr_static_id->operand[i].is_operator
4177 && process_address (i, check_only_p, &before, &after))
4179 if (check_only_p)
4180 return true;
4181 change_p = true;
4182 lra_update_dup (curr_id, i);
4185 if (change_p)
4186 /* If we've changed the instruction then any alternative that
4187 we chose previously may no longer be valid. */
4188 lra_set_used_insn_alternative (curr_insn, LRA_UNKNOWN_ALT);
4190 if (! check_only_p && curr_insn_set != NULL_RTX
4191 && check_and_process_move (&change_p, &sec_mem_p))
4192 return change_p;
4194 try_swapped:
4196 reused_alternative_num = check_only_p ? LRA_UNKNOWN_ALT : curr_id->used_insn_alternative;
4197 if (lra_dump_file != NULL && reused_alternative_num >= 0)
4198 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
4199 reused_alternative_num, INSN_UID (curr_insn));
4201 if (process_alt_operands (reused_alternative_num))
4202 alt_p = true;
4204 if (check_only_p)
4205 return ! alt_p || best_losers != 0;
4207 /* If insn is commutative (it's safe to exchange a certain pair of
4208 operands) then we need to try each alternative twice, the second
4209 time matching those two operands as if we had exchanged them. To
4210 do this, really exchange them in operands.
4212 If we have just tried the alternatives the second time, return
4213 operands to normal and drop through. */
4215 if (reused_alternative_num < 0 && commutative >= 0)
4217 curr_swapped = !curr_swapped;
4218 if (curr_swapped)
4220 swap_operands (commutative);
4221 goto try_swapped;
4223 else
4224 swap_operands (commutative);
4227 if (! alt_p && ! sec_mem_p)
4229 /* No alternative works with reloads?? */
4230 if (INSN_CODE (curr_insn) >= 0)
4231 fatal_insn ("unable to generate reloads for:", curr_insn);
4232 error_for_asm (curr_insn,
4233 "inconsistent operand constraints in an %<asm%>");
4234 lra_asm_error_p = true;
4235 if (! JUMP_P (curr_insn))
4237 /* Avoid further trouble with this insn. Don't generate use
4238 pattern here as we could use the insn SP offset. */
4239 lra_set_insn_deleted (curr_insn);
4241 else
4243 lra_invalidate_insn_data (curr_insn);
4244 ira_nullify_asm_goto (curr_insn);
4245 lra_update_insn_regno_info (curr_insn);
4247 return true;
4250 /* If the best alternative is with operands 1 and 2 swapped, swap
4251 them. Update the operand numbers of any reloads already
4252 pushed. */
4254 if (goal_alt_swapped)
4256 if (lra_dump_file != NULL)
4257 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
4258 INSN_UID (curr_insn));
4260 /* Swap the duplicates too. */
4261 swap_operands (commutative);
4262 change_p = true;
4265 /* Some targets' TARGET_SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
4266 too conservatively. So we use the secondary memory only if there
4267 is no any alternative without reloads. */
4268 use_sec_mem_p = false;
4269 if (! alt_p)
4270 use_sec_mem_p = true;
4271 else if (sec_mem_p)
4273 for (i = 0; i < n_operands; i++)
4274 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
4275 break;
4276 use_sec_mem_p = i < n_operands;
4279 if (use_sec_mem_p)
4281 int in = -1, out = -1;
4282 rtx new_reg, src, dest, rld;
4283 machine_mode sec_mode, rld_mode;
4285 lra_assert (curr_insn_set != NULL_RTX && sec_mem_p);
4286 dest = SET_DEST (curr_insn_set);
4287 src = SET_SRC (curr_insn_set);
4288 for (i = 0; i < n_operands; i++)
4289 if (*curr_id->operand_loc[i] == dest)
4290 out = i;
4291 else if (*curr_id->operand_loc[i] == src)
4292 in = i;
4293 for (i = 0; i < curr_static_id->n_dups; i++)
4294 if (out < 0 && *curr_id->dup_loc[i] == dest)
4295 out = curr_static_id->dup_num[i];
4296 else if (in < 0 && *curr_id->dup_loc[i] == src)
4297 in = curr_static_id->dup_num[i];
4298 lra_assert (out >= 0 && in >= 0
4299 && curr_static_id->operand[out].type == OP_OUT
4300 && curr_static_id->operand[in].type == OP_IN);
4301 rld = partial_subreg_p (GET_MODE (src), GET_MODE (dest)) ? src : dest;
4302 rld_mode = GET_MODE (rld);
4303 sec_mode = targetm.secondary_memory_needed_mode (rld_mode);
4304 new_reg = lra_create_new_reg (sec_mode, NULL_RTX, NO_REGS, NULL,
4305 "secondary");
4306 /* If the mode is changed, it should be wider. */
4307 lra_assert (!partial_subreg_p (sec_mode, rld_mode));
4308 if (sec_mode != rld_mode)
4310 /* If the target says specifically to use another mode for
4311 secondary memory moves we cannot reuse the original
4312 insn. */
4313 after = emit_spill_move (false, new_reg, dest);
4314 lra_process_new_insns (curr_insn, NULL, after,
4315 "Inserting the sec. move");
4316 /* We may have non null BEFORE here (e.g. after address
4317 processing. */
4318 push_to_sequence (before);
4319 before = emit_spill_move (true, new_reg, src);
4320 emit_insn (before);
4321 before = get_insns ();
4322 end_sequence ();
4323 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
4324 lra_set_insn_deleted (curr_insn);
4326 else if (dest == rld)
4328 *curr_id->operand_loc[out] = new_reg;
4329 lra_update_dup (curr_id, out);
4330 after = emit_spill_move (false, new_reg, dest);
4331 lra_process_new_insns (curr_insn, NULL, after,
4332 "Inserting the sec. move");
4334 else
4336 *curr_id->operand_loc[in] = new_reg;
4337 lra_update_dup (curr_id, in);
4338 /* See comments above. */
4339 push_to_sequence (before);
4340 before = emit_spill_move (true, new_reg, src);
4341 emit_insn (before);
4342 before = get_insns ();
4343 end_sequence ();
4344 lra_process_new_insns (curr_insn, before, NULL,
4345 "Inserting the sec. move");
4347 lra_update_insn_regno_info (curr_insn);
4348 return true;
4351 lra_assert (goal_alt_number >= 0);
4352 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
4354 if (lra_dump_file != NULL)
4356 const char *p;
4358 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
4359 goal_alt_number, INSN_UID (curr_insn));
4360 for (i = 0; i < n_operands; i++)
4362 p = (curr_static_id->operand_alternative
4363 [goal_alt_number * n_operands + i].constraint);
4364 if (*p == '\0')
4365 continue;
4366 fprintf (lra_dump_file, " (%d) ", i);
4367 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
4368 fputc (*p, lra_dump_file);
4370 if (INSN_CODE (curr_insn) >= 0
4371 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
4372 fprintf (lra_dump_file, " {%s}", p);
4373 if (maybe_ne (curr_id->sp_offset, 0))
4375 fprintf (lra_dump_file, " (sp_off=");
4376 print_dec (curr_id->sp_offset, lra_dump_file);
4377 fprintf (lra_dump_file, ")");
4379 fprintf (lra_dump_file, "\n");
4382 /* Right now, for any pair of operands I and J that are required to
4383 match, with J < I, goal_alt_matches[I] is J. Add I to
4384 goal_alt_matched[J]. */
4386 for (i = 0; i < n_operands; i++)
4387 if ((j = goal_alt_matches[i]) >= 0)
4389 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
4391 /* We allow matching one output operand and several input
4392 operands. */
4393 lra_assert (k == 0
4394 || (curr_static_id->operand[j].type == OP_OUT
4395 && curr_static_id->operand[i].type == OP_IN
4396 && (curr_static_id->operand
4397 [goal_alt_matched[j][0]].type == OP_IN)));
4398 goal_alt_matched[j][k] = i;
4399 goal_alt_matched[j][k + 1] = -1;
4402 for (i = 0; i < n_operands; i++)
4403 goal_alt_win[i] |= goal_alt_match_win[i];
4405 /* Any constants that aren't allowed and can't be reloaded into
4406 registers are here changed into memory references. */
4407 for (i = 0; i < n_operands; i++)
4408 if (goal_alt_win[i])
4410 int regno;
4411 enum reg_class new_class;
4412 rtx reg = *curr_id->operand_loc[i];
4414 if (GET_CODE (reg) == SUBREG)
4415 reg = SUBREG_REG (reg);
4417 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
4419 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
4421 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
4423 lra_assert (ok_p);
4424 lra_change_class (regno, new_class, " Change to", true);
4428 else
4430 const char *constraint;
4431 char c;
4432 rtx op = *curr_id->operand_loc[i];
4433 rtx subreg = NULL_RTX;
4434 machine_mode mode = curr_operand_mode[i];
4436 if (GET_CODE (op) == SUBREG)
4438 subreg = op;
4439 op = SUBREG_REG (op);
4440 mode = GET_MODE (op);
4443 if (CONST_POOL_OK_P (mode, op)
4444 && ((targetm.preferred_reload_class
4445 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
4446 || no_input_reloads_p))
4448 rtx tem = force_const_mem (mode, op);
4450 change_p = true;
4451 if (subreg != NULL_RTX)
4452 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
4454 *curr_id->operand_loc[i] = tem;
4455 lra_update_dup (curr_id, i);
4456 process_address (i, false, &before, &after);
4458 /* If the alternative accepts constant pool refs directly
4459 there will be no reload needed at all. */
4460 if (subreg != NULL_RTX)
4461 continue;
4462 /* Skip alternatives before the one requested. */
4463 constraint = (curr_static_id->operand_alternative
4464 [goal_alt_number * n_operands + i].constraint);
4465 for (;
4466 (c = *constraint) && c != ',' && c != '#';
4467 constraint += CONSTRAINT_LEN (c, constraint))
4469 enum constraint_num cn = lookup_constraint (constraint);
4470 if ((insn_extra_memory_constraint (cn)
4471 || insn_extra_special_memory_constraint (cn)
4472 || insn_extra_relaxed_memory_constraint (cn))
4473 && satisfies_memory_constraint_p (tem, cn))
4474 break;
4476 if (c == '\0' || c == ',' || c == '#')
4477 continue;
4479 goal_alt_win[i] = true;
4483 n_outputs = 0;
4484 for (i = 0; i < n_operands; i++)
4485 if (curr_static_id->operand[i].type == OP_OUT)
4486 outputs[n_outputs++] = i;
4487 outputs[n_outputs] = -1;
4488 for (i = 0; i < n_operands; i++)
4490 int regno;
4491 bool optional_p = false;
4492 rtx old, new_reg;
4493 rtx op = *curr_id->operand_loc[i];
4495 if (goal_alt_win[i])
4497 if (goal_alt[i] == NO_REGS
4498 && REG_P (op)
4499 /* When we assign NO_REGS it means that we will not
4500 assign a hard register to the scratch pseudo by
4501 assigment pass and the scratch pseudo will be
4502 spilled. Spilled scratch pseudos are transformed
4503 back to scratches at the LRA end. */
4504 && ira_former_scratch_operand_p (curr_insn, i)
4505 && ira_former_scratch_p (REGNO (op)))
4507 int regno = REGNO (op);
4508 lra_change_class (regno, NO_REGS, " Change to", true);
4509 if (lra_get_regno_hard_regno (regno) >= 0)
4510 /* We don't have to mark all insn affected by the
4511 spilled pseudo as there is only one such insn, the
4512 current one. */
4513 reg_renumber[regno] = -1;
4514 lra_assert (bitmap_single_bit_set_p
4515 (&lra_reg_info[REGNO (op)].insn_bitmap));
4517 /* We can do an optional reload. If the pseudo got a hard
4518 reg, we might improve the code through inheritance. If
4519 it does not get a hard register we coalesce memory/memory
4520 moves later. Ignore move insns to avoid cycling. */
4521 if (! lra_simple_p
4522 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
4523 && goal_alt[i] != NO_REGS && REG_P (op)
4524 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
4525 && regno < new_regno_start
4526 && ! ira_former_scratch_p (regno)
4527 && reg_renumber[regno] < 0
4528 /* Check that the optional reload pseudo will be able to
4529 hold given mode value. */
4530 && ! (prohibited_class_reg_set_mode_p
4531 (goal_alt[i], reg_class_contents[goal_alt[i]],
4532 PSEUDO_REGNO_MODE (regno)))
4533 && (curr_insn_set == NULL_RTX
4534 || !((REG_P (SET_SRC (curr_insn_set))
4535 || MEM_P (SET_SRC (curr_insn_set))
4536 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
4537 && (REG_P (SET_DEST (curr_insn_set))
4538 || MEM_P (SET_DEST (curr_insn_set))
4539 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
4540 optional_p = true;
4541 else if (goal_alt_matched[i][0] != -1
4542 && curr_static_id->operand[i].type == OP_OUT
4543 && (curr_static_id->operand_alternative
4544 [goal_alt_number * n_operands + i].earlyclobber)
4545 && REG_P (op))
4547 for (j = 0; goal_alt_matched[i][j] != -1; j++)
4549 rtx op2 = *curr_id->operand_loc[goal_alt_matched[i][j]];
4551 if (REG_P (op2) && REGNO (op) != REGNO (op2))
4552 break;
4554 if (goal_alt_matched[i][j] != -1)
4556 /* Generate reloads for different output and matched
4557 input registers. This is the easiest way to avoid
4558 creation of non-existing register conflicts in
4559 lra-lives.cc. */
4560 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i],
4561 &goal_alt_exclude_start_hard_regs[i], &before,
4562 &after, TRUE);
4564 continue;
4566 else
4567 continue;
4570 /* Operands that match previous ones have already been handled. */
4571 if (goal_alt_matches[i] >= 0)
4572 continue;
4574 /* We should not have an operand with a non-offsettable address
4575 appearing where an offsettable address will do. It also may
4576 be a case when the address should be special in other words
4577 not a general one (e.g. it needs no index reg). */
4578 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
4580 enum reg_class rclass;
4581 rtx *loc = &XEXP (op, 0);
4582 enum rtx_code code = GET_CODE (*loc);
4584 push_to_sequence (before);
4585 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
4586 MEM, SCRATCH);
4587 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
4588 new_reg = emit_inc (rclass, *loc, *loc,
4589 /* This value does not matter for MODIFY. */
4590 GET_MODE_SIZE (GET_MODE (op)));
4591 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass,
4592 NULL, FALSE,
4593 "offsetable address", &new_reg))
4595 rtx addr = *loc;
4596 enum rtx_code code = GET_CODE (addr);
4597 bool align_p = false;
4599 if (code == AND && CONST_INT_P (XEXP (addr, 1)))
4601 /* (and ... (const_int -X)) is used to align to X bytes. */
4602 align_p = true;
4603 addr = XEXP (*loc, 0);
4605 else
4606 addr = canonicalize_reload_addr (addr);
4608 lra_emit_move (new_reg, addr);
4609 if (align_p)
4610 emit_move_insn (new_reg, gen_rtx_AND (GET_MODE (new_reg), new_reg, XEXP (*loc, 1)));
4612 before = get_insns ();
4613 end_sequence ();
4614 *loc = new_reg;
4615 lra_update_dup (curr_id, i);
4617 else if (goal_alt_matched[i][0] == -1)
4619 machine_mode mode;
4620 rtx reg, *loc;
4621 int hard_regno;
4622 enum op_type type = curr_static_id->operand[i].type;
4624 loc = curr_id->operand_loc[i];
4625 mode = curr_operand_mode[i];
4626 if (GET_CODE (*loc) == SUBREG)
4628 reg = SUBREG_REG (*loc);
4629 poly_int64 byte = SUBREG_BYTE (*loc);
4630 if (REG_P (reg)
4631 /* Strict_low_part requires reloading the register and not
4632 just the subreg. Likewise for a strict subreg no wider
4633 than a word for WORD_REGISTER_OPERATIONS targets. */
4634 && (curr_static_id->operand[i].strict_low
4635 || (!paradoxical_subreg_p (mode, GET_MODE (reg))
4636 && (hard_regno
4637 = get_try_hard_regno (REGNO (reg))) >= 0
4638 && (simplify_subreg_regno
4639 (hard_regno,
4640 GET_MODE (reg), byte, mode) < 0)
4641 && (goal_alt[i] == NO_REGS
4642 || (simplify_subreg_regno
4643 (ira_class_hard_regs[goal_alt[i]][0],
4644 GET_MODE (reg), byte, mode) >= 0)))
4645 || (partial_subreg_p (mode, GET_MODE (reg))
4646 && known_le (GET_MODE_SIZE (GET_MODE (reg)),
4647 UNITS_PER_WORD)
4648 && WORD_REGISTER_OPERATIONS))
4649 /* Avoid the situation when there are no available hard regs
4650 for the pseudo mode but there are ones for the subreg
4651 mode: */
4652 && !(goal_alt[i] != NO_REGS
4653 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
4654 && (prohibited_class_reg_set_mode_p
4655 (goal_alt[i], reg_class_contents[goal_alt[i]],
4656 GET_MODE (reg)))
4657 && !(prohibited_class_reg_set_mode_p
4658 (goal_alt[i], reg_class_contents[goal_alt[i]],
4659 mode))))
4661 /* An OP_INOUT is required when reloading a subreg of a
4662 mode wider than a word to ensure that data beyond the
4663 word being reloaded is preserved. Also automatically
4664 ensure that strict_low_part reloads are made into
4665 OP_INOUT which should already be true from the backend
4666 constraints. */
4667 if (type == OP_OUT
4668 && (curr_static_id->operand[i].strict_low
4669 || read_modify_subreg_p (*loc)))
4670 type = OP_INOUT;
4671 loc = &SUBREG_REG (*loc);
4672 mode = GET_MODE (*loc);
4675 old = *loc;
4676 if (get_reload_reg (type, mode, old, goal_alt[i],
4677 &goal_alt_exclude_start_hard_regs[i],
4678 loc != curr_id->operand_loc[i], "", &new_reg)
4679 && type != OP_OUT)
4681 push_to_sequence (before);
4682 lra_emit_move (new_reg, old);
4683 before = get_insns ();
4684 end_sequence ();
4686 *loc = new_reg;
4687 if (type != OP_IN
4688 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
4690 start_sequence ();
4691 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
4692 emit_insn (after);
4693 after = get_insns ();
4694 end_sequence ();
4695 *loc = new_reg;
4697 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
4698 if (goal_alt_dont_inherit_ops[j] == i)
4700 lra_set_regno_unique_value (REGNO (new_reg));
4701 break;
4703 lra_update_dup (curr_id, i);
4705 else if (curr_static_id->operand[i].type == OP_IN
4706 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4707 == OP_OUT
4708 || (curr_static_id->operand[goal_alt_matched[i][0]].type
4709 == OP_INOUT
4710 && (operands_match_p
4711 (*curr_id->operand_loc[i],
4712 *curr_id->operand_loc[goal_alt_matched[i][0]],
4713 -1)))))
4715 /* generate reloads for input and matched outputs. */
4716 match_inputs[0] = i;
4717 match_inputs[1] = -1;
4718 match_reload (goal_alt_matched[i][0], match_inputs, outputs,
4719 goal_alt[i], &goal_alt_exclude_start_hard_regs[i],
4720 &before, &after,
4721 curr_static_id->operand_alternative
4722 [goal_alt_number * n_operands + goal_alt_matched[i][0]]
4723 .earlyclobber);
4725 else if ((curr_static_id->operand[i].type == OP_OUT
4726 || (curr_static_id->operand[i].type == OP_INOUT
4727 && (operands_match_p
4728 (*curr_id->operand_loc[i],
4729 *curr_id->operand_loc[goal_alt_matched[i][0]],
4730 -1))))
4731 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4732 == OP_IN))
4733 /* Generate reloads for output and matched inputs. */
4734 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i],
4735 &goal_alt_exclude_start_hard_regs[i], &before, &after,
4736 curr_static_id->operand_alternative
4737 [goal_alt_number * n_operands + i].earlyclobber);
4738 else if (curr_static_id->operand[i].type == OP_IN
4739 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4740 == OP_IN))
4742 /* Generate reloads for matched inputs. */
4743 match_inputs[0] = i;
4744 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
4745 match_inputs[j + 1] = k;
4746 match_inputs[j + 1] = -1;
4747 match_reload (-1, match_inputs, outputs, goal_alt[i],
4748 &goal_alt_exclude_start_hard_regs[i],
4749 &before, &after, false);
4751 else
4752 /* We must generate code in any case when function
4753 process_alt_operands decides that it is possible. */
4754 gcc_unreachable ();
4756 if (optional_p)
4758 rtx reg = op;
4760 lra_assert (REG_P (reg));
4761 regno = REGNO (reg);
4762 op = *curr_id->operand_loc[i]; /* Substitution. */
4763 if (GET_CODE (op) == SUBREG)
4764 op = SUBREG_REG (op);
4765 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
4766 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
4767 lra_reg_info[REGNO (op)].restore_rtx = reg;
4768 if (lra_dump_file != NULL)
4769 fprintf (lra_dump_file,
4770 " Making reload reg %d for reg %d optional\n",
4771 REGNO (op), regno);
4774 if (before != NULL_RTX || after != NULL_RTX
4775 || max_regno_before != max_reg_num ())
4776 change_p = true;
4777 if (change_p)
4779 lra_update_operator_dups (curr_id);
4780 /* Something changes -- process the insn. */
4781 lra_update_insn_regno_info (curr_insn);
4783 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
4784 return change_p;
4787 /* Return true if INSN satisfies all constraints. In other words, no
4788 reload insns are needed. */
4789 bool
4790 lra_constrain_insn (rtx_insn *insn)
4792 int saved_new_regno_start = new_regno_start;
4793 int saved_new_insn_uid_start = new_insn_uid_start;
4794 bool change_p;
4796 curr_insn = insn;
4797 curr_id = lra_get_insn_recog_data (curr_insn);
4798 curr_static_id = curr_id->insn_static_data;
4799 new_insn_uid_start = get_max_uid ();
4800 new_regno_start = max_reg_num ();
4801 change_p = curr_insn_transform (true);
4802 new_regno_start = saved_new_regno_start;
4803 new_insn_uid_start = saved_new_insn_uid_start;
4804 return ! change_p;
4807 /* Return true if X is in LIST. */
4808 static bool
4809 in_list_p (rtx x, rtx list)
4811 for (; list != NULL_RTX; list = XEXP (list, 1))
4812 if (XEXP (list, 0) == x)
4813 return true;
4814 return false;
4817 /* Return true if X contains an allocatable hard register (if
4818 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
4819 static bool
4820 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
4822 int i, j;
4823 const char *fmt;
4824 enum rtx_code code;
4826 code = GET_CODE (x);
4827 if (REG_P (x))
4829 int regno = REGNO (x);
4830 HARD_REG_SET alloc_regs;
4832 if (hard_reg_p)
4834 if (regno >= FIRST_PSEUDO_REGISTER)
4835 regno = lra_get_regno_hard_regno (regno);
4836 if (regno < 0)
4837 return false;
4838 alloc_regs = ~lra_no_alloc_regs;
4839 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
4841 else
4843 if (regno < FIRST_PSEUDO_REGISTER)
4844 return false;
4845 if (! spilled_p)
4846 return true;
4847 return lra_get_regno_hard_regno (regno) < 0;
4850 fmt = GET_RTX_FORMAT (code);
4851 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4853 if (fmt[i] == 'e')
4855 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
4856 return true;
4858 else if (fmt[i] == 'E')
4860 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4861 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4862 return true;
4865 return false;
4868 /* Process all regs in location *LOC and change them on equivalent
4869 substitution. Return true if any change was done. */
4870 static bool
4871 loc_equivalence_change_p (rtx *loc)
4873 rtx subst, reg, x = *loc;
4874 bool result = false;
4875 enum rtx_code code = GET_CODE (x);
4876 const char *fmt;
4877 int i, j;
4879 if (code == SUBREG)
4881 reg = SUBREG_REG (x);
4882 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4883 && GET_MODE (subst) == VOIDmode)
4885 /* We cannot reload debug location. Simplify subreg here
4886 while we know the inner mode. */
4887 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4888 GET_MODE (reg), SUBREG_BYTE (x));
4889 return true;
4892 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4894 *loc = subst;
4895 return true;
4898 /* Scan all the operand sub-expressions. */
4899 fmt = GET_RTX_FORMAT (code);
4900 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4902 if (fmt[i] == 'e')
4903 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4904 else if (fmt[i] == 'E')
4905 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4906 result
4907 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4909 return result;
4912 /* Similar to loc_equivalence_change_p, but for use as
4913 simplify_replace_fn_rtx callback. DATA is insn for which the
4914 elimination is done. If it null we don't do the elimination. */
4915 static rtx
4916 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4918 if (!REG_P (loc))
4919 return NULL_RTX;
4921 rtx subst = (data == NULL
4922 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4923 if (subst != loc)
4924 return subst;
4926 return NULL_RTX;
4929 /* Maximum number of generated reload insns per an insn. It is for
4930 preventing this pass cycling in a bug case. */
4931 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4933 /* The current iteration number of this LRA pass. */
4934 int lra_constraint_iter;
4936 /* True if we should during assignment sub-pass check assignment
4937 correctness for all pseudos and spill some of them to correct
4938 conflicts. It can be necessary when we substitute equiv which
4939 needs checking register allocation correctness because the
4940 equivalent value contains allocatable hard registers, or when we
4941 restore multi-register pseudo, or when we change the insn code and
4942 its operand became INOUT operand when it was IN one before. */
4943 bool check_and_force_assignment_correctness_p;
4945 /* Return true if REGNO is referenced in more than one block. */
4946 static bool
4947 multi_block_pseudo_p (int regno)
4949 basic_block bb = NULL;
4950 unsigned int uid;
4951 bitmap_iterator bi;
4953 if (regno < FIRST_PSEUDO_REGISTER)
4954 return false;
4956 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4957 if (bb == NULL)
4958 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4959 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4960 return true;
4961 return false;
4964 /* Return true if LIST contains a deleted insn. */
4965 static bool
4966 contains_deleted_insn_p (rtx_insn_list *list)
4968 for (; list != NULL_RTX; list = list->next ())
4969 if (NOTE_P (list->insn ())
4970 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4971 return true;
4972 return false;
4975 /* Return true if X contains a pseudo dying in INSN. */
4976 static bool
4977 dead_pseudo_p (rtx x, rtx_insn *insn)
4979 int i, j;
4980 const char *fmt;
4981 enum rtx_code code;
4983 if (REG_P (x))
4984 return (insn != NULL_RTX
4985 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4986 code = GET_CODE (x);
4987 fmt = GET_RTX_FORMAT (code);
4988 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4990 if (fmt[i] == 'e')
4992 if (dead_pseudo_p (XEXP (x, i), insn))
4993 return true;
4995 else if (fmt[i] == 'E')
4997 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4998 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4999 return true;
5002 return false;
5005 /* Return true if INSN contains a dying pseudo in INSN right hand
5006 side. */
5007 static bool
5008 insn_rhs_dead_pseudo_p (rtx_insn *insn)
5010 rtx set = single_set (insn);
5012 gcc_assert (set != NULL);
5013 return dead_pseudo_p (SET_SRC (set), insn);
5016 /* Return true if any init insn of REGNO contains a dying pseudo in
5017 insn right hand side. */
5018 static bool
5019 init_insn_rhs_dead_pseudo_p (int regno)
5021 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
5023 if (insns == NULL)
5024 return false;
5025 for (; insns != NULL_RTX; insns = insns->next ())
5026 if (insn_rhs_dead_pseudo_p (insns->insn ()))
5027 return true;
5028 return false;
5031 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
5032 reverse only if we have one init insn with given REGNO as a
5033 source. */
5034 static bool
5035 reverse_equiv_p (int regno)
5037 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
5038 rtx set;
5040 if (insns == NULL)
5041 return false;
5042 if (! INSN_P (insns->insn ())
5043 || insns->next () != NULL)
5044 return false;
5045 if ((set = single_set (insns->insn ())) == NULL_RTX)
5046 return false;
5047 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
5050 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
5051 call this function only for non-reverse equivalence. */
5052 static bool
5053 contains_reloaded_insn_p (int regno)
5055 rtx set;
5056 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
5058 for (; list != NULL; list = list->next ())
5059 if ((set = single_set (list->insn ())) == NULL_RTX
5060 || ! REG_P (SET_DEST (set))
5061 || (int) REGNO (SET_DEST (set)) != regno)
5062 return true;
5063 return false;
5066 /* Try combine secondary memory reload insn FROM for insn TO into TO insn.
5067 FROM should be a load insn (usually a secondary memory reload insn). Return
5068 TRUE in case of success. */
5069 static bool
5070 combine_reload_insn (rtx_insn *from, rtx_insn *to)
5072 bool ok_p;
5073 rtx_insn *saved_insn;
5074 rtx set, from_reg, to_reg, op;
5075 enum reg_class to_class, from_class;
5076 int n, nop;
5077 signed char changed_nops[MAX_RECOG_OPERANDS + 1];
5079 /* Check conditions for second memory reload and original insn: */
5080 if ((targetm.secondary_memory_needed
5081 == hook_bool_mode_reg_class_t_reg_class_t_false)
5082 || NEXT_INSN (from) != to
5083 || !NONDEBUG_INSN_P (to)
5084 || CALL_P (to))
5085 return false;
5087 lra_insn_recog_data_t id = lra_get_insn_recog_data (to);
5088 struct lra_static_insn_data *static_id = id->insn_static_data;
5090 if (id->used_insn_alternative == LRA_UNKNOWN_ALT
5091 || (set = single_set (from)) == NULL_RTX)
5092 return false;
5093 from_reg = SET_DEST (set);
5094 to_reg = SET_SRC (set);
5095 /* Ignore optional reloads: */
5096 if (! REG_P (from_reg) || ! REG_P (to_reg)
5097 || bitmap_bit_p (&lra_optional_reload_pseudos, REGNO (from_reg)))
5098 return false;
5099 to_class = lra_get_allocno_class (REGNO (to_reg));
5100 from_class = lra_get_allocno_class (REGNO (from_reg));
5101 /* Check that reload insn is a load: */
5102 if (to_class != NO_REGS || from_class == NO_REGS)
5103 return false;
5104 for (n = nop = 0; nop < static_id->n_operands; nop++)
5106 if (static_id->operand[nop].type != OP_IN)
5107 continue;
5108 op = *id->operand_loc[nop];
5109 if (!REG_P (op) || REGNO (op) != REGNO (from_reg))
5110 continue;
5111 *id->operand_loc[nop] = to_reg;
5112 changed_nops[n++] = nop;
5114 changed_nops[n] = -1;
5115 lra_update_dups (id, changed_nops);
5116 lra_update_insn_regno_info (to);
5117 ok_p = recog_memoized (to) >= 0;
5118 if (ok_p)
5120 /* Check that combined insn does not need any reloads: */
5121 saved_insn = curr_insn;
5122 curr_insn = to;
5123 curr_id = lra_get_insn_recog_data (curr_insn);
5124 curr_static_id = curr_id->insn_static_data;
5125 for (bool swapped_p = false;;)
5127 ok_p = !curr_insn_transform (true);
5128 if (ok_p || curr_static_id->commutative < 0)
5129 break;
5130 swap_operands (curr_static_id->commutative);
5131 if (lra_dump_file != NULL)
5133 fprintf (lra_dump_file,
5134 " Swapping %scombined insn operands:\n",
5135 swapped_p ? "back " : "");
5136 dump_insn_slim (lra_dump_file, to);
5138 if (swapped_p)
5139 break;
5140 swapped_p = true;
5142 curr_insn = saved_insn;
5143 curr_id = lra_get_insn_recog_data (curr_insn);
5144 curr_static_id = curr_id->insn_static_data;
5146 if (ok_p)
5148 id->used_insn_alternative = -1;
5149 lra_push_insn_and_update_insn_regno_info (to);
5150 if (lra_dump_file != NULL)
5152 fprintf (lra_dump_file, " Use combined insn:\n");
5153 dump_insn_slim (lra_dump_file, to);
5155 return true;
5157 if (lra_dump_file != NULL)
5159 fprintf (lra_dump_file, " Failed combined insn:\n");
5160 dump_insn_slim (lra_dump_file, to);
5162 for (int i = 0; i < n; i++)
5164 nop = changed_nops[i];
5165 *id->operand_loc[nop] = from_reg;
5167 lra_update_dups (id, changed_nops);
5168 lra_update_insn_regno_info (to);
5169 if (lra_dump_file != NULL)
5171 fprintf (lra_dump_file, " Restoring insn after failed combining:\n");
5172 dump_insn_slim (lra_dump_file, to);
5174 return false;
5177 /* Entry function of LRA constraint pass. Return true if the
5178 constraint pass did change the code. */
5179 bool
5180 lra_constraints (bool first_p)
5182 bool changed_p;
5183 int i, hard_regno, new_insns_num;
5184 unsigned int min_len, new_min_len, uid;
5185 rtx set, x, reg, dest_reg;
5186 rtx_insn *original_insn;
5187 basic_block last_bb;
5188 bitmap_iterator bi;
5190 lra_constraint_iter++;
5191 if (lra_dump_file != NULL)
5192 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
5193 lra_constraint_iter);
5194 changed_p = false;
5195 if (pic_offset_table_rtx
5196 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
5197 check_and_force_assignment_correctness_p = true;
5198 else if (first_p)
5199 /* On the first iteration we should check IRA assignment
5200 correctness. In rare cases, the assignments can be wrong as
5201 early clobbers operands are ignored in IRA or usages of
5202 paradoxical sub-registers are not taken into account by
5203 IRA. */
5204 check_and_force_assignment_correctness_p = true;
5205 new_insn_uid_start = get_max_uid ();
5206 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
5207 /* Mark used hard regs for target stack size calulations. */
5208 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5209 if (lra_reg_info[i].nrefs != 0
5210 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
5212 int j, nregs;
5214 nregs = hard_regno_nregs (hard_regno, lra_reg_info[i].biggest_mode);
5215 for (j = 0; j < nregs; j++)
5216 df_set_regs_ever_live (hard_regno + j, true);
5218 /* Do elimination before the equivalence processing as we can spill
5219 some pseudos during elimination. */
5220 lra_eliminate (false, first_p);
5221 auto_bitmap equiv_insn_bitmap (&reg_obstack);
5222 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5223 if (lra_reg_info[i].nrefs != 0)
5225 ira_reg_equiv[i].profitable_p = true;
5226 reg = regno_reg_rtx[i];
5227 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
5229 bool pseudo_p = contains_reg_p (x, false, false);
5231 /* After RTL transformation, we cannot guarantee that
5232 pseudo in the substitution was not reloaded which might
5233 make equivalence invalid. For example, in reverse
5234 equiv of p0
5236 p0 <- ...
5238 equiv_mem <- p0
5240 the memory address register was reloaded before the 2nd
5241 insn. */
5242 if ((! first_p && pseudo_p)
5243 /* We don't use DF for compilation speed sake. So it
5244 is problematic to update live info when we use an
5245 equivalence containing pseudos in more than one
5246 BB. */
5247 || (pseudo_p && multi_block_pseudo_p (i))
5248 /* If an init insn was deleted for some reason, cancel
5249 the equiv. We could update the equiv insns after
5250 transformations including an equiv insn deletion
5251 but it is not worthy as such cases are extremely
5252 rare. */
5253 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
5254 /* If it is not a reverse equivalence, we check that a
5255 pseudo in rhs of the init insn is not dying in the
5256 insn. Otherwise, the live info at the beginning of
5257 the corresponding BB might be wrong after we
5258 removed the insn. When the equiv can be a
5259 constant, the right hand side of the init insn can
5260 be a pseudo. */
5261 || (! reverse_equiv_p (i)
5262 && (init_insn_rhs_dead_pseudo_p (i)
5263 /* If we reloaded the pseudo in an equivalence
5264 init insn, we cannot remove the equiv init
5265 insns and the init insns might write into
5266 const memory in this case. */
5267 || contains_reloaded_insn_p (i)))
5268 /* Prevent access beyond equivalent memory for
5269 paradoxical subregs. */
5270 || (MEM_P (x)
5271 && maybe_gt (GET_MODE_SIZE (lra_reg_info[i].biggest_mode),
5272 GET_MODE_SIZE (GET_MODE (x))))
5273 || (pic_offset_table_rtx
5274 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
5275 && (targetm.preferred_reload_class
5276 (x, lra_get_allocno_class (i)) == NO_REGS))
5277 || contains_symbol_ref_p (x))))
5278 ira_reg_equiv[i].defined_p
5279 = ira_reg_equiv[i].caller_save_p = false;
5280 if (contains_reg_p (x, false, true))
5281 ira_reg_equiv[i].profitable_p = false;
5282 if (get_equiv (reg) != reg)
5283 bitmap_ior_into (equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
5286 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5287 update_equiv (i);
5288 /* We should add all insns containing pseudos which should be
5289 substituted by their equivalences. */
5290 EXECUTE_IF_SET_IN_BITMAP (equiv_insn_bitmap, 0, uid, bi)
5291 lra_push_insn_by_uid (uid);
5292 min_len = lra_insn_stack_length ();
5293 new_insns_num = 0;
5294 last_bb = NULL;
5295 changed_p = false;
5296 original_insn = NULL;
5297 while ((new_min_len = lra_insn_stack_length ()) != 0)
5299 curr_insn = lra_pop_insn ();
5300 --new_min_len;
5301 curr_bb = BLOCK_FOR_INSN (curr_insn);
5302 if (curr_bb != last_bb)
5304 last_bb = curr_bb;
5305 bb_reload_num = lra_curr_reload_num;
5307 if (min_len > new_min_len)
5309 min_len = new_min_len;
5310 new_insns_num = 0;
5311 original_insn = curr_insn;
5313 else if (combine_reload_insn (curr_insn, original_insn))
5315 continue;
5317 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
5318 internal_error
5319 ("maximum number of generated reload insns per insn achieved (%d)",
5320 MAX_RELOAD_INSNS_NUMBER);
5321 new_insns_num++;
5322 if (DEBUG_INSN_P (curr_insn))
5324 /* We need to check equivalence in debug insn and change
5325 pseudo to the equivalent value if necessary. */
5326 curr_id = lra_get_insn_recog_data (curr_insn);
5327 if (bitmap_bit_p (equiv_insn_bitmap, INSN_UID (curr_insn)))
5329 rtx old = *curr_id->operand_loc[0];
5330 *curr_id->operand_loc[0]
5331 = simplify_replace_fn_rtx (old, NULL_RTX,
5332 loc_equivalence_callback, curr_insn);
5333 if (old != *curr_id->operand_loc[0])
5335 lra_update_insn_regno_info (curr_insn);
5336 changed_p = true;
5340 else if (INSN_P (curr_insn))
5342 if ((set = single_set (curr_insn)) != NULL_RTX)
5344 dest_reg = SET_DEST (set);
5345 /* The equivalence pseudo could be set up as SUBREG in a
5346 case when it is a call restore insn in a mode
5347 different from the pseudo mode. */
5348 if (GET_CODE (dest_reg) == SUBREG)
5349 dest_reg = SUBREG_REG (dest_reg);
5350 if ((REG_P (dest_reg)
5351 && (x = get_equiv (dest_reg)) != dest_reg
5352 /* Remove insns which set up a pseudo whose value
5353 cannot be changed. Such insns might be not in
5354 init_insns because we don't update equiv data
5355 during insn transformations.
5357 As an example, let suppose that a pseudo got
5358 hard register and on the 1st pass was not
5359 changed to equivalent constant. We generate an
5360 additional insn setting up the pseudo because of
5361 secondary memory movement. Then the pseudo is
5362 spilled and we use the equiv constant. In this
5363 case we should remove the additional insn and
5364 this insn is not init_insns list. */
5365 && (! MEM_P (x) || MEM_READONLY_P (x)
5366 /* Check that this is actually an insn setting
5367 up the equivalence. */
5368 || in_list_p (curr_insn,
5369 ira_reg_equiv
5370 [REGNO (dest_reg)].init_insns)))
5371 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
5372 && in_list_p (curr_insn,
5373 ira_reg_equiv
5374 [REGNO (SET_SRC (set))].init_insns)))
5376 /* This is equiv init insn of pseudo which did not get a
5377 hard register -- remove the insn. */
5378 if (lra_dump_file != NULL)
5380 fprintf (lra_dump_file,
5381 " Removing equiv init insn %i (freq=%d)\n",
5382 INSN_UID (curr_insn),
5383 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
5384 dump_insn_slim (lra_dump_file, curr_insn);
5386 if (contains_reg_p (x, true, false))
5387 check_and_force_assignment_correctness_p = true;
5388 lra_set_insn_deleted (curr_insn);
5389 continue;
5392 curr_id = lra_get_insn_recog_data (curr_insn);
5393 curr_static_id = curr_id->insn_static_data;
5394 init_curr_insn_input_reloads ();
5395 init_curr_operand_mode ();
5396 if (curr_insn_transform (false))
5397 changed_p = true;
5398 /* Check non-transformed insns too for equiv change as USE
5399 or CLOBBER don't need reloads but can contain pseudos
5400 being changed on their equivalences. */
5401 else if (bitmap_bit_p (equiv_insn_bitmap, INSN_UID (curr_insn))
5402 && loc_equivalence_change_p (&PATTERN (curr_insn)))
5404 lra_update_insn_regno_info (curr_insn);
5405 changed_p = true;
5410 /* If we used a new hard regno, changed_p should be true because the
5411 hard reg is assigned to a new pseudo. */
5412 if (flag_checking && !changed_p)
5414 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5415 if (lra_reg_info[i].nrefs != 0
5416 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
5418 int j, nregs = hard_regno_nregs (hard_regno,
5419 PSEUDO_REGNO_MODE (i));
5421 for (j = 0; j < nregs; j++)
5422 lra_assert (df_regs_ever_live_p (hard_regno + j));
5425 return changed_p;
5428 static void initiate_invariants (void);
5429 static void finish_invariants (void);
5431 /* Initiate the LRA constraint pass. It is done once per
5432 function. */
5433 void
5434 lra_constraints_init (void)
5436 initiate_invariants ();
5439 /* Finalize the LRA constraint pass. It is done once per
5440 function. */
5441 void
5442 lra_constraints_finish (void)
5444 finish_invariants ();
5449 /* Structure describes invariants for ineheritance. */
5450 struct lra_invariant
5452 /* The order number of the invariant. */
5453 int num;
5454 /* The invariant RTX. */
5455 rtx invariant_rtx;
5456 /* The origin insn of the invariant. */
5457 rtx_insn *insn;
5460 typedef lra_invariant invariant_t;
5461 typedef invariant_t *invariant_ptr_t;
5462 typedef const invariant_t *const_invariant_ptr_t;
5464 /* Pointer to the inheritance invariants. */
5465 static vec<invariant_ptr_t> invariants;
5467 /* Allocation pool for the invariants. */
5468 static object_allocator<lra_invariant> *invariants_pool;
5470 /* Hash table for the invariants. */
5471 static htab_t invariant_table;
5473 /* Hash function for INVARIANT. */
5474 static hashval_t
5475 invariant_hash (const void *invariant)
5477 rtx inv = ((const_invariant_ptr_t) invariant)->invariant_rtx;
5478 return lra_rtx_hash (inv);
5481 /* Equal function for invariants INVARIANT1 and INVARIANT2. */
5482 static int
5483 invariant_eq_p (const void *invariant1, const void *invariant2)
5485 rtx inv1 = ((const_invariant_ptr_t) invariant1)->invariant_rtx;
5486 rtx inv2 = ((const_invariant_ptr_t) invariant2)->invariant_rtx;
5488 return rtx_equal_p (inv1, inv2);
5491 /* Insert INVARIANT_RTX into the table if it is not there yet. Return
5492 invariant which is in the table. */
5493 static invariant_ptr_t
5494 insert_invariant (rtx invariant_rtx)
5496 void **entry_ptr;
5497 invariant_t invariant;
5498 invariant_ptr_t invariant_ptr;
5500 invariant.invariant_rtx = invariant_rtx;
5501 entry_ptr = htab_find_slot (invariant_table, &invariant, INSERT);
5502 if (*entry_ptr == NULL)
5504 invariant_ptr = invariants_pool->allocate ();
5505 invariant_ptr->invariant_rtx = invariant_rtx;
5506 invariant_ptr->insn = NULL;
5507 invariants.safe_push (invariant_ptr);
5508 *entry_ptr = (void *) invariant_ptr;
5510 return (invariant_ptr_t) *entry_ptr;
5513 /* Initiate the invariant table. */
5514 static void
5515 initiate_invariants (void)
5517 invariants.create (100);
5518 invariants_pool
5519 = new object_allocator<lra_invariant> ("Inheritance invariants");
5520 invariant_table = htab_create (100, invariant_hash, invariant_eq_p, NULL);
5523 /* Finish the invariant table. */
5524 static void
5525 finish_invariants (void)
5527 htab_delete (invariant_table);
5528 delete invariants_pool;
5529 invariants.release ();
5532 /* Make the invariant table empty. */
5533 static void
5534 clear_invariants (void)
5536 htab_empty (invariant_table);
5537 invariants_pool->release ();
5538 invariants.truncate (0);
5543 /* This page contains code to do inheritance/split
5544 transformations. */
5546 /* Number of reloads passed so far in current EBB. */
5547 static int reloads_num;
5549 /* Number of calls passed so far in current EBB. */
5550 static int calls_num;
5552 /* Index ID is the CALLS_NUM associated the last call we saw with
5553 ABI identifier ID. */
5554 static int last_call_for_abi[NUM_ABI_IDS];
5556 /* Which registers have been fully or partially clobbered by a call
5557 since they were last used. */
5558 static HARD_REG_SET full_and_partial_call_clobbers;
5560 /* Current reload pseudo check for validity of elements in
5561 USAGE_INSNS. */
5562 static int curr_usage_insns_check;
5564 /* Info about last usage of registers in EBB to do inheritance/split
5565 transformation. Inheritance transformation is done from a spilled
5566 pseudo and split transformations from a hard register or a pseudo
5567 assigned to a hard register. */
5568 struct usage_insns
5570 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
5571 value INSNS is valid. The insns is chain of optional debug insns
5572 and a finishing non-debug insn using the corresponding reg. The
5573 value is also used to mark the registers which are set up in the
5574 current insn. The negated insn uid is used for this. */
5575 int check;
5576 /* Value of global reloads_num at the last insn in INSNS. */
5577 int reloads_num;
5578 /* Value of global reloads_nums at the last insn in INSNS. */
5579 int calls_num;
5580 /* It can be true only for splitting. And it means that the restore
5581 insn should be put after insn given by the following member. */
5582 bool after_p;
5583 /* Next insns in the current EBB which use the original reg and the
5584 original reg value is not changed between the current insn and
5585 the next insns. In order words, e.g. for inheritance, if we need
5586 to use the original reg value again in the next insns we can try
5587 to use the value in a hard register from a reload insn of the
5588 current insn. */
5589 rtx insns;
5592 /* Map: regno -> corresponding pseudo usage insns. */
5593 static struct usage_insns *usage_insns;
5595 static void
5596 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
5598 usage_insns[regno].check = curr_usage_insns_check;
5599 usage_insns[regno].insns = insn;
5600 usage_insns[regno].reloads_num = reloads_num;
5601 usage_insns[regno].calls_num = calls_num;
5602 usage_insns[regno].after_p = after_p;
5603 if (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0)
5604 remove_from_hard_reg_set (&full_and_partial_call_clobbers,
5605 PSEUDO_REGNO_MODE (regno),
5606 reg_renumber[regno]);
5609 /* The function is used to form list REGNO usages which consists of
5610 optional debug insns finished by a non-debug insn using REGNO.
5611 RELOADS_NUM is current number of reload insns processed so far. */
5612 static void
5613 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
5615 rtx next_usage_insns;
5617 if (usage_insns[regno].check == curr_usage_insns_check
5618 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
5619 && DEBUG_INSN_P (insn))
5621 /* Check that we did not add the debug insn yet. */
5622 if (next_usage_insns != insn
5623 && (GET_CODE (next_usage_insns) != INSN_LIST
5624 || XEXP (next_usage_insns, 0) != insn))
5625 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
5626 next_usage_insns);
5628 else if (NONDEBUG_INSN_P (insn))
5629 setup_next_usage_insn (regno, insn, reloads_num, false);
5630 else
5631 usage_insns[regno].check = 0;
5634 /* Return first non-debug insn in list USAGE_INSNS. */
5635 static rtx_insn *
5636 skip_usage_debug_insns (rtx usage_insns)
5638 rtx insn;
5640 /* Skip debug insns. */
5641 for (insn = usage_insns;
5642 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
5643 insn = XEXP (insn, 1))
5645 return safe_as_a <rtx_insn *> (insn);
5648 /* Return true if we need secondary memory moves for insn in
5649 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
5650 into the insn. */
5651 static bool
5652 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
5653 rtx usage_insns ATTRIBUTE_UNUSED)
5655 rtx_insn *insn;
5656 rtx set, dest;
5657 enum reg_class cl;
5659 if (inher_cl == ALL_REGS
5660 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
5661 return false;
5662 lra_assert (INSN_P (insn));
5663 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
5664 return false;
5665 dest = SET_DEST (set);
5666 if (! REG_P (dest))
5667 return false;
5668 lra_assert (inher_cl != NO_REGS);
5669 cl = get_reg_class (REGNO (dest));
5670 return (cl != NO_REGS && cl != ALL_REGS
5671 && targetm.secondary_memory_needed (GET_MODE (dest), inher_cl, cl));
5674 /* Registers involved in inheritance/split in the current EBB
5675 (inheritance/split pseudos and original registers). */
5676 static bitmap_head check_only_regs;
5678 /* Reload pseudos cannot be involded in invariant inheritance in the
5679 current EBB. */
5680 static bitmap_head invalid_invariant_regs;
5682 /* Do inheritance transformations for insn INSN, which defines (if
5683 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
5684 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
5685 form as the "insns" field of usage_insns. Return true if we
5686 succeed in such transformation.
5688 The transformations look like:
5690 p <- ... i <- ...
5691 ... p <- i (new insn)
5692 ... =>
5693 <- ... p ... <- ... i ...
5695 ... i <- p (new insn)
5696 <- ... p ... <- ... i ...
5697 ... =>
5698 <- ... p ... <- ... i ...
5699 where p is a spilled original pseudo and i is a new inheritance pseudo.
5702 The inheritance pseudo has the smallest class of two classes CL and
5703 class of ORIGINAL REGNO. */
5704 static bool
5705 inherit_reload_reg (bool def_p, int original_regno,
5706 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
5708 if (optimize_function_for_size_p (cfun))
5709 return false;
5711 enum reg_class rclass = lra_get_allocno_class (original_regno);
5712 rtx original_reg = regno_reg_rtx[original_regno];
5713 rtx new_reg, usage_insn;
5714 rtx_insn *new_insns;
5716 lra_assert (! usage_insns[original_regno].after_p);
5717 if (lra_dump_file != NULL)
5718 fprintf (lra_dump_file,
5719 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
5720 if (! ira_reg_classes_intersect_p[cl][rclass])
5722 if (lra_dump_file != NULL)
5724 fprintf (lra_dump_file,
5725 " Rejecting inheritance for %d "
5726 "because of disjoint classes %s and %s\n",
5727 original_regno, reg_class_names[cl],
5728 reg_class_names[rclass]);
5729 fprintf (lra_dump_file,
5730 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5732 return false;
5734 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
5735 /* We don't use a subset of two classes because it can be
5736 NO_REGS. This transformation is still profitable in most
5737 cases even if the classes are not intersected as register
5738 move is probably cheaper than a memory load. */
5739 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
5741 if (lra_dump_file != NULL)
5742 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
5743 reg_class_names[cl], reg_class_names[rclass]);
5745 rclass = cl;
5747 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
5749 /* Reject inheritance resulting in secondary memory moves.
5750 Otherwise, there is a danger in LRA cycling. Also such
5751 transformation will be unprofitable. */
5752 if (lra_dump_file != NULL)
5754 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
5755 rtx set = single_set (insn);
5757 lra_assert (set != NULL_RTX);
5759 rtx dest = SET_DEST (set);
5761 lra_assert (REG_P (dest));
5762 fprintf (lra_dump_file,
5763 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
5764 "as secondary mem is needed\n",
5765 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
5766 original_regno, reg_class_names[rclass]);
5767 fprintf (lra_dump_file,
5768 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5770 return false;
5772 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
5773 rclass, NULL, "inheritance");
5774 start_sequence ();
5775 if (def_p)
5776 lra_emit_move (original_reg, new_reg);
5777 else
5778 lra_emit_move (new_reg, original_reg);
5779 new_insns = get_insns ();
5780 end_sequence ();
5781 if (NEXT_INSN (new_insns) != NULL_RTX)
5783 if (lra_dump_file != NULL)
5785 fprintf (lra_dump_file,
5786 " Rejecting inheritance %d->%d "
5787 "as it results in 2 or more insns:\n",
5788 original_regno, REGNO (new_reg));
5789 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
5790 fprintf (lra_dump_file,
5791 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5793 return false;
5795 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
5796 lra_update_insn_regno_info (insn);
5797 if (! def_p)
5798 /* We now have a new usage insn for original regno. */
5799 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
5800 if (lra_dump_file != NULL)
5801 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
5802 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
5803 lra_reg_info[REGNO (new_reg)].restore_rtx = regno_reg_rtx[original_regno];
5804 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5805 bitmap_set_bit (&check_only_regs, original_regno);
5806 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
5807 if (def_p)
5808 lra_process_new_insns (insn, NULL, new_insns,
5809 "Add original<-inheritance");
5810 else
5811 lra_process_new_insns (insn, new_insns, NULL,
5812 "Add inheritance<-original");
5813 while (next_usage_insns != NULL_RTX)
5815 if (GET_CODE (next_usage_insns) != INSN_LIST)
5817 usage_insn = next_usage_insns;
5818 lra_assert (NONDEBUG_INSN_P (usage_insn));
5819 next_usage_insns = NULL;
5821 else
5823 usage_insn = XEXP (next_usage_insns, 0);
5824 lra_assert (DEBUG_INSN_P (usage_insn));
5825 next_usage_insns = XEXP (next_usage_insns, 1);
5827 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false,
5828 DEBUG_INSN_P (usage_insn));
5829 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5830 if (lra_dump_file != NULL)
5832 basic_block bb = BLOCK_FOR_INSN (usage_insn);
5833 fprintf (lra_dump_file,
5834 " Inheritance reuse change %d->%d (bb%d):\n",
5835 original_regno, REGNO (new_reg),
5836 bb ? bb->index : -1);
5837 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5840 if (lra_dump_file != NULL)
5841 fprintf (lra_dump_file,
5842 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5843 return true;
5846 /* Return true if we need a caller save/restore for pseudo REGNO which
5847 was assigned to a hard register. */
5848 static inline bool
5849 need_for_call_save_p (int regno)
5851 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
5852 if (usage_insns[regno].calls_num < calls_num)
5854 unsigned int abis = 0;
5855 for (unsigned int i = 0; i < NUM_ABI_IDS; ++i)
5856 if (last_call_for_abi[i] > usage_insns[regno].calls_num)
5857 abis |= 1 << i;
5858 gcc_assert (abis);
5859 if (call_clobbered_in_region_p (abis, full_and_partial_call_clobbers,
5860 PSEUDO_REGNO_MODE (regno),
5861 reg_renumber[regno]))
5862 return true;
5864 return false;
5867 /* Global registers occurring in the current EBB. */
5868 static bitmap_head ebb_global_regs;
5870 /* Return true if we need a split for hard register REGNO or pseudo
5871 REGNO which was assigned to a hard register.
5872 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
5873 used for reloads since the EBB end. It is an approximation of the
5874 used hard registers in the split range. The exact value would
5875 require expensive calculations. If we were aggressive with
5876 splitting because of the approximation, the split pseudo will save
5877 the same hard register assignment and will be removed in the undo
5878 pass. We still need the approximation because too aggressive
5879 splitting would result in too inaccurate cost calculation in the
5880 assignment pass because of too many generated moves which will be
5881 probably removed in the undo pass. */
5882 static inline bool
5883 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
5885 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
5887 lra_assert (hard_regno >= 0);
5888 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
5889 /* Don't split eliminable hard registers, otherwise we can
5890 split hard registers like hard frame pointer, which
5891 lives on BB start/end according to DF-infrastructure,
5892 when there is a pseudo assigned to the register and
5893 living in the same BB. */
5894 && (regno >= FIRST_PSEUDO_REGISTER
5895 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
5896 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
5897 /* Don't split call clobbered hard regs living through
5898 calls, otherwise we might have a check problem in the
5899 assign sub-pass as in the most cases (exception is a
5900 situation when check_and_force_assignment_correctness_p value is
5901 true) the assign pass assumes that all pseudos living
5902 through calls are assigned to call saved hard regs. */
5903 && (regno >= FIRST_PSEUDO_REGISTER
5904 || !TEST_HARD_REG_BIT (full_and_partial_call_clobbers, regno))
5905 /* We need at least 2 reloads to make pseudo splitting
5906 profitable. We should provide hard regno splitting in
5907 any case to solve 1st insn scheduling problem when
5908 moving hard register definition up might result in
5909 impossibility to find hard register for reload pseudo of
5910 small register class. */
5911 && (usage_insns[regno].reloads_num
5912 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
5913 && (regno < FIRST_PSEUDO_REGISTER
5914 /* For short living pseudos, spilling + inheritance can
5915 be considered a substitution for splitting.
5916 Therefore we do not splitting for local pseudos. It
5917 decreases also aggressiveness of splitting. The
5918 minimal number of references is chosen taking into
5919 account that for 2 references splitting has no sense
5920 as we can just spill the pseudo. */
5921 || (regno >= FIRST_PSEUDO_REGISTER
5922 && lra_reg_info[regno].nrefs > 3
5923 && bitmap_bit_p (&ebb_global_regs, regno))))
5924 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
5927 /* Return class for the split pseudo created from original pseudo with
5928 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
5929 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
5930 results in no secondary memory movements. */
5931 static enum reg_class
5932 choose_split_class (enum reg_class allocno_class,
5933 int hard_regno ATTRIBUTE_UNUSED,
5934 machine_mode mode ATTRIBUTE_UNUSED)
5936 int i;
5937 enum reg_class cl, best_cl = NO_REGS;
5938 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
5939 = REGNO_REG_CLASS (hard_regno);
5941 if (! targetm.secondary_memory_needed (mode, allocno_class, allocno_class)
5942 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
5943 return allocno_class;
5944 for (i = 0;
5945 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
5946 i++)
5947 if (! targetm.secondary_memory_needed (mode, cl, hard_reg_class)
5948 && ! targetm.secondary_memory_needed (mode, hard_reg_class, cl)
5949 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
5950 && (best_cl == NO_REGS
5951 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
5952 best_cl = cl;
5953 return best_cl;
5956 /* Copy any equivalence information from ORIGINAL_REGNO to NEW_REGNO. It only
5957 makes sense to call this function if NEW_REGNO is always equal to
5958 ORIGINAL_REGNO. Set up defined_p flag when caller_save_p flag is set up and
5959 CALL_SAVE_P is true. */
5961 static void
5962 lra_copy_reg_equiv (unsigned int new_regno, unsigned int original_regno,
5963 bool call_save_p)
5965 if (!ira_reg_equiv[original_regno].defined_p
5966 && !(call_save_p && ira_reg_equiv[original_regno].caller_save_p))
5967 return;
5969 ira_expand_reg_equiv ();
5970 ira_reg_equiv[new_regno].defined_p = true;
5971 if (ira_reg_equiv[original_regno].memory)
5972 ira_reg_equiv[new_regno].memory
5973 = copy_rtx (ira_reg_equiv[original_regno].memory);
5974 if (ira_reg_equiv[original_regno].constant)
5975 ira_reg_equiv[new_regno].constant
5976 = copy_rtx (ira_reg_equiv[original_regno].constant);
5977 if (ira_reg_equiv[original_regno].invariant)
5978 ira_reg_equiv[new_regno].invariant
5979 = copy_rtx (ira_reg_equiv[original_regno].invariant);
5982 /* Do split transformations for insn INSN, which defines or uses
5983 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
5984 the EBB next uses ORIGINAL_REGNO; it has the same form as the
5985 "insns" field of usage_insns. If TO is not NULL, we don't use
5986 usage_insns, we put restore insns after TO insn. It is a case when
5987 we call it from lra_split_hard_reg_for, outside the inheritance
5988 pass.
5990 The transformations look like:
5992 p <- ... p <- ...
5993 ... s <- p (new insn -- save)
5994 ... =>
5995 ... p <- s (new insn -- restore)
5996 <- ... p ... <- ... p ...
5998 <- ... p ... <- ... p ...
5999 ... s <- p (new insn -- save)
6000 ... =>
6001 ... p <- s (new insn -- restore)
6002 <- ... p ... <- ... p ...
6004 where p is an original pseudo got a hard register or a hard
6005 register and s is a new split pseudo. The save is put before INSN
6006 if BEFORE_P is true. Return true if we succeed in such
6007 transformation. */
6008 static bool
6009 split_reg (bool before_p, int original_regno, rtx_insn *insn,
6010 rtx next_usage_insns, rtx_insn *to)
6012 enum reg_class rclass;
6013 rtx original_reg;
6014 int hard_regno, nregs;
6015 rtx new_reg, usage_insn;
6016 rtx_insn *restore, *save;
6017 bool after_p;
6018 bool call_save_p;
6019 machine_mode mode;
6021 if (original_regno < FIRST_PSEUDO_REGISTER)
6023 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
6024 hard_regno = original_regno;
6025 call_save_p = false;
6026 nregs = 1;
6027 mode = lra_reg_info[hard_regno].biggest_mode;
6028 machine_mode reg_rtx_mode = GET_MODE (regno_reg_rtx[hard_regno]);
6029 /* A reg can have a biggest_mode of VOIDmode if it was only ever seen as
6030 part of a multi-word register. In that case, just use the reg_rtx
6031 mode. Do the same also if the biggest mode was larger than a register
6032 or we can not compare the modes. Otherwise, limit the size to that of
6033 the biggest access in the function or to the natural mode at least. */
6034 if (mode == VOIDmode
6035 || !ordered_p (GET_MODE_PRECISION (mode),
6036 GET_MODE_PRECISION (reg_rtx_mode))
6037 || paradoxical_subreg_p (mode, reg_rtx_mode)
6038 || maybe_gt (GET_MODE_PRECISION (reg_rtx_mode), GET_MODE_PRECISION (mode)))
6040 original_reg = regno_reg_rtx[hard_regno];
6041 mode = reg_rtx_mode;
6043 else
6044 original_reg = gen_rtx_REG (mode, hard_regno);
6046 else
6048 mode = PSEUDO_REGNO_MODE (original_regno);
6049 hard_regno = reg_renumber[original_regno];
6050 nregs = hard_regno_nregs (hard_regno, mode);
6051 rclass = lra_get_allocno_class (original_regno);
6052 original_reg = regno_reg_rtx[original_regno];
6053 call_save_p = need_for_call_save_p (original_regno);
6055 lra_assert (hard_regno >= 0);
6056 if (lra_dump_file != NULL)
6057 fprintf (lra_dump_file,
6058 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
6060 if (call_save_p)
6062 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
6063 hard_regno_nregs (hard_regno, mode),
6064 mode);
6065 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, NULL, "save");
6067 else
6069 rclass = choose_split_class (rclass, hard_regno, mode);
6070 if (rclass == NO_REGS)
6072 if (lra_dump_file != NULL)
6074 fprintf (lra_dump_file,
6075 " Rejecting split of %d(%s): "
6076 "no good reg class for %d(%s)\n",
6077 original_regno,
6078 reg_class_names[lra_get_allocno_class (original_regno)],
6079 hard_regno,
6080 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
6081 fprintf
6082 (lra_dump_file,
6083 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6085 return false;
6087 /* Split_if_necessary can split hard registers used as part of a
6088 multi-register mode but splits each register individually. The
6089 mode used for each independent register may not be supported
6090 so reject the split. Splitting the wider mode should theoretically
6091 be possible but is not implemented. */
6092 if (!targetm.hard_regno_mode_ok (hard_regno, mode))
6094 if (lra_dump_file != NULL)
6096 fprintf (lra_dump_file,
6097 " Rejecting split of %d(%s): unsuitable mode %s\n",
6098 original_regno,
6099 reg_class_names[lra_get_allocno_class (original_regno)],
6100 GET_MODE_NAME (mode));
6101 fprintf
6102 (lra_dump_file,
6103 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6105 return false;
6107 new_reg = lra_create_new_reg (mode, original_reg, rclass, NULL, "split");
6108 reg_renumber[REGNO (new_reg)] = hard_regno;
6110 int new_regno = REGNO (new_reg);
6111 save = emit_spill_move (true, new_reg, original_reg);
6112 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
6114 if (lra_dump_file != NULL)
6116 fprintf
6117 (lra_dump_file,
6118 " Rejecting split %d->%d resulting in > 2 save insns:\n",
6119 original_regno, new_regno);
6120 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
6121 fprintf (lra_dump_file,
6122 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6124 return false;
6126 restore = emit_spill_move (false, new_reg, original_reg);
6127 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
6129 if (lra_dump_file != NULL)
6131 fprintf (lra_dump_file,
6132 " Rejecting split %d->%d "
6133 "resulting in > 2 restore insns:\n",
6134 original_regno, new_regno);
6135 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
6136 fprintf (lra_dump_file,
6137 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6139 return false;
6141 /* Transfer equivalence information to the spill register, so that
6142 if we fail to allocate the spill register, we have the option of
6143 rematerializing the original value instead of spilling to the stack. */
6144 if (!HARD_REGISTER_NUM_P (original_regno)
6145 && mode == PSEUDO_REGNO_MODE (original_regno))
6146 lra_copy_reg_equiv (new_regno, original_regno, call_save_p);
6147 lra_reg_info[new_regno].restore_rtx = regno_reg_rtx[original_regno];
6148 bitmap_set_bit (&lra_split_regs, new_regno);
6149 if (to != NULL)
6151 lra_assert (next_usage_insns == NULL);
6152 usage_insn = to;
6153 after_p = TRUE;
6155 else
6157 /* We need check_only_regs only inside the inheritance pass. */
6158 bitmap_set_bit (&check_only_regs, new_regno);
6159 bitmap_set_bit (&check_only_regs, original_regno);
6160 after_p = usage_insns[original_regno].after_p;
6161 for (;;)
6163 if (GET_CODE (next_usage_insns) != INSN_LIST)
6165 usage_insn = next_usage_insns;
6166 break;
6168 usage_insn = XEXP (next_usage_insns, 0);
6169 lra_assert (DEBUG_INSN_P (usage_insn));
6170 next_usage_insns = XEXP (next_usage_insns, 1);
6171 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false,
6172 true);
6173 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
6174 if (lra_dump_file != NULL)
6176 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
6177 original_regno, new_regno);
6178 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
6182 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
6183 lra_assert (usage_insn != insn || (after_p && before_p));
6184 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
6185 after_p ? NULL : restore,
6186 after_p ? restore : NULL,
6187 call_save_p
6188 ? "Add reg<-save" : "Add reg<-split");
6189 lra_process_new_insns (insn, before_p ? save : NULL,
6190 before_p ? NULL : save,
6191 call_save_p
6192 ? "Add save<-reg" : "Add split<-reg");
6193 if (nregs > 1 || original_regno < FIRST_PSEUDO_REGISTER)
6194 /* If we are trying to split multi-register. We should check
6195 conflicts on the next assignment sub-pass. IRA can allocate on
6196 sub-register levels, LRA do this on pseudos level right now and
6197 this discrepancy may create allocation conflicts after
6198 splitting.
6200 If we are trying to split hard register we should also check conflicts
6201 as such splitting can create artificial conflict of the hard register
6202 with another pseudo because of simplified conflict calculation in
6203 LRA. */
6204 check_and_force_assignment_correctness_p = true;
6205 if (lra_dump_file != NULL)
6206 fprintf (lra_dump_file,
6207 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
6208 return true;
6211 /* Split a hard reg for reload pseudo REGNO having RCLASS and living
6212 in the range [FROM, TO]. Return true if did a split. Otherwise,
6213 return false. */
6214 bool
6215 spill_hard_reg_in_range (int regno, enum reg_class rclass, rtx_insn *from, rtx_insn *to)
6217 int i, hard_regno;
6218 int rclass_size;
6219 rtx_insn *insn;
6220 unsigned int uid;
6221 bitmap_iterator bi;
6222 HARD_REG_SET ignore;
6224 lra_assert (from != NULL && to != NULL);
6225 ignore = lra_no_alloc_regs;
6226 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
6228 lra_insn_recog_data_t id = lra_insn_recog_data[uid];
6229 struct lra_static_insn_data *static_id = id->insn_static_data;
6230 struct lra_insn_reg *reg;
6232 for (reg = id->regs; reg != NULL; reg = reg->next)
6233 if (reg->regno < FIRST_PSEUDO_REGISTER)
6234 SET_HARD_REG_BIT (ignore, reg->regno);
6235 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
6236 SET_HARD_REG_BIT (ignore, reg->regno);
6238 rclass_size = ira_class_hard_regs_num[rclass];
6239 for (i = 0; i < rclass_size; i++)
6241 hard_regno = ira_class_hard_regs[rclass][i];
6242 if (! TEST_HARD_REG_BIT (lra_reg_info[regno].conflict_hard_regs, hard_regno)
6243 || TEST_HARD_REG_BIT (ignore, hard_regno))
6244 continue;
6245 for (insn = from; insn != NEXT_INSN (to); insn = NEXT_INSN (insn))
6247 struct lra_static_insn_data *static_id;
6248 struct lra_insn_reg *reg;
6250 if (!INSN_P (insn))
6251 continue;
6252 if (bitmap_bit_p (&lra_reg_info[hard_regno].insn_bitmap,
6253 INSN_UID (insn)))
6254 break;
6255 static_id = lra_get_insn_recog_data (insn)->insn_static_data;
6256 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
6257 if (reg->regno == hard_regno)
6258 break;
6259 if (reg != NULL)
6260 break;
6262 if (insn != NEXT_INSN (to))
6263 continue;
6264 if (split_reg (TRUE, hard_regno, from, NULL, to))
6265 return true;
6267 return false;
6270 /* Recognize that we need a split transformation for insn INSN, which
6271 defines or uses REGNO in its insn biggest MODE (we use it only if
6272 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
6273 hard registers which might be used for reloads since the EBB end.
6274 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
6275 uid before starting INSN processing. Return true if we succeed in
6276 such transformation. */
6277 static bool
6278 split_if_necessary (int regno, machine_mode mode,
6279 HARD_REG_SET potential_reload_hard_regs,
6280 bool before_p, rtx_insn *insn, int max_uid)
6282 bool res = false;
6283 int i, nregs = 1;
6284 rtx next_usage_insns;
6286 if (regno < FIRST_PSEUDO_REGISTER)
6287 nregs = hard_regno_nregs (regno, mode);
6288 for (i = 0; i < nregs; i++)
6289 if (usage_insns[regno + i].check == curr_usage_insns_check
6290 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
6291 /* To avoid processing the register twice or more. */
6292 && ((GET_CODE (next_usage_insns) != INSN_LIST
6293 && INSN_UID (next_usage_insns) < max_uid)
6294 || (GET_CODE (next_usage_insns) == INSN_LIST
6295 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
6296 && need_for_split_p (potential_reload_hard_regs, regno + i)
6297 && split_reg (before_p, regno + i, insn, next_usage_insns, NULL))
6298 res = true;
6299 return res;
6302 /* Return TRUE if rtx X is considered as an invariant for
6303 inheritance. */
6304 static bool
6305 invariant_p (const_rtx x)
6307 machine_mode mode;
6308 const char *fmt;
6309 enum rtx_code code;
6310 int i, j;
6312 if (side_effects_p (x))
6313 return false;
6315 code = GET_CODE (x);
6316 mode = GET_MODE (x);
6317 if (code == SUBREG)
6319 x = SUBREG_REG (x);
6320 code = GET_CODE (x);
6321 mode = wider_subreg_mode (mode, GET_MODE (x));
6324 if (MEM_P (x))
6325 return false;
6327 if (REG_P (x))
6329 int i, nregs, regno = REGNO (x);
6331 if (regno >= FIRST_PSEUDO_REGISTER || regno == STACK_POINTER_REGNUM
6332 || TEST_HARD_REG_BIT (eliminable_regset, regno)
6333 || GET_MODE_CLASS (GET_MODE (x)) == MODE_CC)
6334 return false;
6335 nregs = hard_regno_nregs (regno, mode);
6336 for (i = 0; i < nregs; i++)
6337 if (! fixed_regs[regno + i]
6338 /* A hard register may be clobbered in the current insn
6339 but we can ignore this case because if the hard
6340 register is used it should be set somewhere after the
6341 clobber. */
6342 || bitmap_bit_p (&invalid_invariant_regs, regno + i))
6343 return false;
6345 fmt = GET_RTX_FORMAT (code);
6346 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6348 if (fmt[i] == 'e')
6350 if (! invariant_p (XEXP (x, i)))
6351 return false;
6353 else if (fmt[i] == 'E')
6355 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6356 if (! invariant_p (XVECEXP (x, i, j)))
6357 return false;
6360 return true;
6363 /* We have 'dest_reg <- invariant'. Let us try to make an invariant
6364 inheritance transformation (using dest_reg instead invariant in a
6365 subsequent insn). */
6366 static bool
6367 process_invariant_for_inheritance (rtx dst_reg, rtx invariant_rtx)
6369 invariant_ptr_t invariant_ptr;
6370 rtx_insn *insn, *new_insns;
6371 rtx insn_set, insn_reg, new_reg;
6372 int insn_regno;
6373 bool succ_p = false;
6374 int dst_regno = REGNO (dst_reg);
6375 machine_mode dst_mode = GET_MODE (dst_reg);
6376 enum reg_class cl = lra_get_allocno_class (dst_regno), insn_reg_cl;
6378 invariant_ptr = insert_invariant (invariant_rtx);
6379 if ((insn = invariant_ptr->insn) != NULL_RTX)
6381 /* We have a subsequent insn using the invariant. */
6382 insn_set = single_set (insn);
6383 lra_assert (insn_set != NULL);
6384 insn_reg = SET_DEST (insn_set);
6385 lra_assert (REG_P (insn_reg));
6386 insn_regno = REGNO (insn_reg);
6387 insn_reg_cl = lra_get_allocno_class (insn_regno);
6389 if (dst_mode == GET_MODE (insn_reg)
6390 /* We should consider only result move reg insns which are
6391 cheap. */
6392 && targetm.register_move_cost (dst_mode, cl, insn_reg_cl) == 2
6393 && targetm.register_move_cost (dst_mode, cl, cl) == 2)
6395 if (lra_dump_file != NULL)
6396 fprintf (lra_dump_file,
6397 " [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[\n");
6398 new_reg = lra_create_new_reg (dst_mode, dst_reg, cl, NULL,
6399 "invariant inheritance");
6400 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
6401 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
6402 lra_reg_info[REGNO (new_reg)].restore_rtx = PATTERN (insn);
6403 start_sequence ();
6404 lra_emit_move (new_reg, dst_reg);
6405 new_insns = get_insns ();
6406 end_sequence ();
6407 lra_process_new_insns (curr_insn, NULL, new_insns,
6408 "Add invariant inheritance<-original");
6409 start_sequence ();
6410 lra_emit_move (SET_DEST (insn_set), new_reg);
6411 new_insns = get_insns ();
6412 end_sequence ();
6413 lra_process_new_insns (insn, NULL, new_insns,
6414 "Changing reload<-inheritance");
6415 lra_set_insn_deleted (insn);
6416 succ_p = true;
6417 if (lra_dump_file != NULL)
6419 fprintf (lra_dump_file,
6420 " Invariant inheritance reuse change %d (bb%d):\n",
6421 REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
6422 dump_insn_slim (lra_dump_file, insn);
6423 fprintf (lra_dump_file,
6424 " ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]\n");
6428 invariant_ptr->insn = curr_insn;
6429 return succ_p;
6432 /* Check only registers living at the current program point in the
6433 current EBB. */
6434 static bitmap_head live_regs;
6436 /* Update live info in EBB given by its HEAD and TAIL insns after
6437 inheritance/split transformation. The function removes dead moves
6438 too. */
6439 static void
6440 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
6442 unsigned int j;
6443 int i, regno;
6444 bool live_p;
6445 rtx_insn *prev_insn;
6446 rtx set;
6447 bool remove_p;
6448 basic_block last_bb, prev_bb, curr_bb;
6449 bitmap_iterator bi;
6450 struct lra_insn_reg *reg;
6451 edge e;
6452 edge_iterator ei;
6454 last_bb = BLOCK_FOR_INSN (tail);
6455 prev_bb = NULL;
6456 for (curr_insn = tail;
6457 curr_insn != PREV_INSN (head);
6458 curr_insn = prev_insn)
6460 prev_insn = PREV_INSN (curr_insn);
6461 /* We need to process empty blocks too. They contain
6462 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
6463 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
6464 continue;
6465 curr_bb = BLOCK_FOR_INSN (curr_insn);
6466 if (curr_bb != prev_bb)
6468 if (prev_bb != NULL)
6470 /* Update df_get_live_in (prev_bb): */
6471 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
6472 if (bitmap_bit_p (&live_regs, j))
6473 bitmap_set_bit (df_get_live_in (prev_bb), j);
6474 else
6475 bitmap_clear_bit (df_get_live_in (prev_bb), j);
6477 if (curr_bb != last_bb)
6479 /* Update df_get_live_out (curr_bb): */
6480 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
6482 live_p = bitmap_bit_p (&live_regs, j);
6483 if (! live_p)
6484 FOR_EACH_EDGE (e, ei, curr_bb->succs)
6485 if (bitmap_bit_p (df_get_live_in (e->dest), j))
6487 live_p = true;
6488 break;
6490 if (live_p)
6491 bitmap_set_bit (df_get_live_out (curr_bb), j);
6492 else
6493 bitmap_clear_bit (df_get_live_out (curr_bb), j);
6496 prev_bb = curr_bb;
6497 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
6499 if (! NONDEBUG_INSN_P (curr_insn))
6500 continue;
6501 curr_id = lra_get_insn_recog_data (curr_insn);
6502 curr_static_id = curr_id->insn_static_data;
6503 remove_p = false;
6504 if ((set = single_set (curr_insn)) != NULL_RTX
6505 && REG_P (SET_DEST (set))
6506 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
6507 && SET_DEST (set) != pic_offset_table_rtx
6508 && bitmap_bit_p (&check_only_regs, regno)
6509 && ! bitmap_bit_p (&live_regs, regno))
6510 remove_p = true;
6511 /* See which defined values die here. */
6512 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6513 if (reg->type == OP_OUT && ! reg->subreg_p)
6514 bitmap_clear_bit (&live_regs, reg->regno);
6515 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6516 if (reg->type == OP_OUT && ! reg->subreg_p)
6517 bitmap_clear_bit (&live_regs, reg->regno);
6518 if (curr_id->arg_hard_regs != NULL)
6519 /* Make clobbered argument hard registers die. */
6520 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6521 if (regno >= FIRST_PSEUDO_REGISTER)
6522 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
6523 /* Mark each used value as live. */
6524 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6525 if (reg->type != OP_OUT
6526 && bitmap_bit_p (&check_only_regs, reg->regno))
6527 bitmap_set_bit (&live_regs, reg->regno);
6528 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6529 if (reg->type != OP_OUT
6530 && bitmap_bit_p (&check_only_regs, reg->regno))
6531 bitmap_set_bit (&live_regs, reg->regno);
6532 if (curr_id->arg_hard_regs != NULL)
6533 /* Make used argument hard registers live. */
6534 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6535 if (regno < FIRST_PSEUDO_REGISTER
6536 && bitmap_bit_p (&check_only_regs, regno))
6537 bitmap_set_bit (&live_regs, regno);
6538 /* It is quite important to remove dead move insns because it
6539 means removing dead store. We don't need to process them for
6540 constraints. */
6541 if (remove_p)
6543 if (lra_dump_file != NULL)
6545 fprintf (lra_dump_file, " Removing dead insn:\n ");
6546 dump_insn_slim (lra_dump_file, curr_insn);
6548 lra_set_insn_deleted (curr_insn);
6553 /* The structure describes info to do an inheritance for the current
6554 insn. We need to collect such info first before doing the
6555 transformations because the transformations change the insn
6556 internal representation. */
6557 struct to_inherit
6559 /* Original regno. */
6560 int regno;
6561 /* Subsequent insns which can inherit original reg value. */
6562 rtx insns;
6565 /* Array containing all info for doing inheritance from the current
6566 insn. */
6567 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
6569 /* Number elements in the previous array. */
6570 static int to_inherit_num;
6572 /* Add inheritance info REGNO and INSNS. Their meaning is described in
6573 structure to_inherit. */
6574 static void
6575 add_to_inherit (int regno, rtx insns)
6577 int i;
6579 for (i = 0; i < to_inherit_num; i++)
6580 if (to_inherit[i].regno == regno)
6581 return;
6582 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
6583 to_inherit[to_inherit_num].regno = regno;
6584 to_inherit[to_inherit_num++].insns = insns;
6587 /* Return the last non-debug insn in basic block BB, or the block begin
6588 note if none. */
6589 static rtx_insn *
6590 get_last_insertion_point (basic_block bb)
6592 rtx_insn *insn;
6594 FOR_BB_INSNS_REVERSE (bb, insn)
6595 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
6596 return insn;
6597 gcc_unreachable ();
6600 /* Set up RES by registers living on edges FROM except the edge (FROM,
6601 TO) or by registers set up in a jump insn in BB FROM. */
6602 static void
6603 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
6605 rtx_insn *last;
6606 struct lra_insn_reg *reg;
6607 edge e;
6608 edge_iterator ei;
6610 lra_assert (to != NULL);
6611 bitmap_clear (res);
6612 FOR_EACH_EDGE (e, ei, from->succs)
6613 if (e->dest != to)
6614 bitmap_ior_into (res, df_get_live_in (e->dest));
6615 last = get_last_insertion_point (from);
6616 if (! JUMP_P (last))
6617 return;
6618 curr_id = lra_get_insn_recog_data (last);
6619 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6620 if (reg->type != OP_IN)
6621 bitmap_set_bit (res, reg->regno);
6624 /* Used as a temporary results of some bitmap calculations. */
6625 static bitmap_head temp_bitmap;
6627 /* We split for reloads of small class of hard regs. The following
6628 defines how many hard regs the class should have to be qualified as
6629 small. The code is mostly oriented to x86/x86-64 architecture
6630 where some insns need to use only specific register or pair of
6631 registers and these register can live in RTL explicitly, e.g. for
6632 parameter passing. */
6633 static const int max_small_class_regs_num = 2;
6635 /* Do inheritance/split transformations in EBB starting with HEAD and
6636 finishing on TAIL. We process EBB insns in the reverse order.
6637 Return true if we did any inheritance/split transformation in the
6638 EBB.
6640 We should avoid excessive splitting which results in worse code
6641 because of inaccurate cost calculations for spilling new split
6642 pseudos in such case. To achieve this we do splitting only if
6643 register pressure is high in given basic block and there are reload
6644 pseudos requiring hard registers. We could do more register
6645 pressure calculations at any given program point to avoid necessary
6646 splitting even more but it is to expensive and the current approach
6647 works well enough. */
6648 static bool
6649 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
6651 int i, src_regno, dst_regno, nregs;
6652 bool change_p, succ_p, update_reloads_num_p;
6653 rtx_insn *prev_insn, *last_insn;
6654 rtx next_usage_insns, curr_set;
6655 enum reg_class cl;
6656 struct lra_insn_reg *reg;
6657 basic_block last_processed_bb, curr_bb = NULL;
6658 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
6659 bitmap to_process;
6660 unsigned int j;
6661 bitmap_iterator bi;
6662 bool head_p, after_p;
6664 change_p = false;
6665 curr_usage_insns_check++;
6666 clear_invariants ();
6667 reloads_num = calls_num = 0;
6668 for (unsigned int i = 0; i < NUM_ABI_IDS; ++i)
6669 last_call_for_abi[i] = 0;
6670 CLEAR_HARD_REG_SET (full_and_partial_call_clobbers);
6671 bitmap_clear (&check_only_regs);
6672 bitmap_clear (&invalid_invariant_regs);
6673 last_processed_bb = NULL;
6674 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
6675 live_hard_regs = eliminable_regset | lra_no_alloc_regs;
6676 /* We don't process new insns generated in the loop. */
6677 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
6679 prev_insn = PREV_INSN (curr_insn);
6680 if (BLOCK_FOR_INSN (curr_insn) != NULL)
6681 curr_bb = BLOCK_FOR_INSN (curr_insn);
6682 if (last_processed_bb != curr_bb)
6684 /* We are at the end of BB. Add qualified living
6685 pseudos for potential splitting. */
6686 to_process = df_get_live_out (curr_bb);
6687 if (last_processed_bb != NULL)
6689 /* We are somewhere in the middle of EBB. */
6690 get_live_on_other_edges (curr_bb, last_processed_bb,
6691 &temp_bitmap);
6692 to_process = &temp_bitmap;
6694 last_processed_bb = curr_bb;
6695 last_insn = get_last_insertion_point (curr_bb);
6696 after_p = (! JUMP_P (last_insn)
6697 && (! CALL_P (last_insn)
6698 || (find_reg_note (last_insn,
6699 REG_NORETURN, NULL_RTX) == NULL_RTX
6700 && ! SIBLING_CALL_P (last_insn))));
6701 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
6702 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
6704 if ((int) j >= lra_constraint_new_regno_start)
6705 break;
6706 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
6708 if (j < FIRST_PSEUDO_REGISTER)
6709 SET_HARD_REG_BIT (live_hard_regs, j);
6710 else
6711 add_to_hard_reg_set (&live_hard_regs,
6712 PSEUDO_REGNO_MODE (j),
6713 reg_renumber[j]);
6714 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
6718 src_regno = dst_regno = -1;
6719 curr_set = single_set (curr_insn);
6720 if (curr_set != NULL_RTX && REG_P (SET_DEST (curr_set)))
6721 dst_regno = REGNO (SET_DEST (curr_set));
6722 if (curr_set != NULL_RTX && REG_P (SET_SRC (curr_set)))
6723 src_regno = REGNO (SET_SRC (curr_set));
6724 update_reloads_num_p = true;
6725 if (src_regno < lra_constraint_new_regno_start
6726 && src_regno >= FIRST_PSEUDO_REGISTER
6727 && reg_renumber[src_regno] < 0
6728 && dst_regno >= lra_constraint_new_regno_start
6729 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
6731 /* 'reload_pseudo <- original_pseudo'. */
6732 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6733 reloads_num++;
6734 update_reloads_num_p = false;
6735 succ_p = false;
6736 if (usage_insns[src_regno].check == curr_usage_insns_check
6737 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
6738 succ_p = inherit_reload_reg (false, src_regno, cl,
6739 curr_insn, next_usage_insns);
6740 if (succ_p)
6741 change_p = true;
6742 else
6743 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
6744 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6745 potential_reload_hard_regs |= reg_class_contents[cl];
6747 else if (src_regno < 0
6748 && dst_regno >= lra_constraint_new_regno_start
6749 && invariant_p (SET_SRC (curr_set))
6750 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS
6751 && ! bitmap_bit_p (&invalid_invariant_regs, dst_regno)
6752 && ! bitmap_bit_p (&invalid_invariant_regs,
6753 ORIGINAL_REGNO(regno_reg_rtx[dst_regno])))
6755 /* 'reload_pseudo <- invariant'. */
6756 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6757 reloads_num++;
6758 update_reloads_num_p = false;
6759 if (process_invariant_for_inheritance (SET_DEST (curr_set), SET_SRC (curr_set)))
6760 change_p = true;
6761 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6762 potential_reload_hard_regs |= reg_class_contents[cl];
6764 else if (src_regno >= lra_constraint_new_regno_start
6765 && dst_regno < lra_constraint_new_regno_start
6766 && dst_regno >= FIRST_PSEUDO_REGISTER
6767 && reg_renumber[dst_regno] < 0
6768 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
6769 && usage_insns[dst_regno].check == curr_usage_insns_check
6770 && (next_usage_insns
6771 = usage_insns[dst_regno].insns) != NULL_RTX)
6773 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6774 reloads_num++;
6775 update_reloads_num_p = false;
6776 /* 'original_pseudo <- reload_pseudo'. */
6777 if (! JUMP_P (curr_insn)
6778 && inherit_reload_reg (true, dst_regno, cl,
6779 curr_insn, next_usage_insns))
6780 change_p = true;
6781 /* Invalidate. */
6782 usage_insns[dst_regno].check = 0;
6783 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6784 potential_reload_hard_regs |= reg_class_contents[cl];
6786 else if (INSN_P (curr_insn))
6788 int iter;
6789 int max_uid = get_max_uid ();
6791 curr_id = lra_get_insn_recog_data (curr_insn);
6792 curr_static_id = curr_id->insn_static_data;
6793 to_inherit_num = 0;
6794 /* Process insn definitions. */
6795 for (iter = 0; iter < 2; iter++)
6796 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
6797 reg != NULL;
6798 reg = reg->next)
6799 if (reg->type != OP_IN
6800 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
6802 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
6803 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
6804 && usage_insns[dst_regno].check == curr_usage_insns_check
6805 && (next_usage_insns
6806 = usage_insns[dst_regno].insns) != NULL_RTX)
6808 struct lra_insn_reg *r;
6810 for (r = curr_id->regs; r != NULL; r = r->next)
6811 if (r->type != OP_OUT && r->regno == dst_regno)
6812 break;
6813 /* Don't do inheritance if the pseudo is also
6814 used in the insn. */
6815 if (r == NULL)
6816 /* We cannot do inheritance right now
6817 because the current insn reg info (chain
6818 regs) can change after that. */
6819 add_to_inherit (dst_regno, next_usage_insns);
6821 /* We cannot process one reg twice here because of
6822 usage_insns invalidation. */
6823 if ((dst_regno < FIRST_PSEUDO_REGISTER
6824 || reg_renumber[dst_regno] >= 0)
6825 && ! reg->subreg_p && reg->type != OP_IN)
6827 HARD_REG_SET s;
6829 if (split_if_necessary (dst_regno, reg->biggest_mode,
6830 potential_reload_hard_regs,
6831 false, curr_insn, max_uid))
6832 change_p = true;
6833 CLEAR_HARD_REG_SET (s);
6834 if (dst_regno < FIRST_PSEUDO_REGISTER)
6835 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
6836 else
6837 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
6838 reg_renumber[dst_regno]);
6839 live_hard_regs &= ~s;
6840 potential_reload_hard_regs &= ~s;
6842 /* We should invalidate potential inheritance or
6843 splitting for the current insn usages to the next
6844 usage insns (see code below) as the output pseudo
6845 prevents this. */
6846 if ((dst_regno >= FIRST_PSEUDO_REGISTER
6847 && reg_renumber[dst_regno] < 0)
6848 || (reg->type == OP_OUT && ! reg->subreg_p
6849 && (dst_regno < FIRST_PSEUDO_REGISTER
6850 || reg_renumber[dst_regno] >= 0)))
6852 /* Invalidate and mark definitions. */
6853 if (dst_regno >= FIRST_PSEUDO_REGISTER)
6854 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
6855 else
6857 nregs = hard_regno_nregs (dst_regno,
6858 reg->biggest_mode);
6859 for (i = 0; i < nregs; i++)
6860 usage_insns[dst_regno + i].check
6861 = -(int) INSN_UID (curr_insn);
6865 /* Process clobbered call regs. */
6866 if (curr_id->arg_hard_regs != NULL)
6867 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6868 if (dst_regno >= FIRST_PSEUDO_REGISTER)
6869 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
6870 = -(int) INSN_UID (curr_insn);
6871 if (! JUMP_P (curr_insn))
6872 for (i = 0; i < to_inherit_num; i++)
6873 if (inherit_reload_reg (true, to_inherit[i].regno,
6874 ALL_REGS, curr_insn,
6875 to_inherit[i].insns))
6876 change_p = true;
6877 if (CALL_P (curr_insn))
6879 rtx cheap, pat, dest;
6880 rtx_insn *restore;
6881 int regno, hard_regno;
6883 calls_num++;
6884 function_abi callee_abi = insn_callee_abi (curr_insn);
6885 last_call_for_abi[callee_abi.id ()] = calls_num;
6886 full_and_partial_call_clobbers
6887 |= callee_abi.full_and_partial_reg_clobbers ();
6888 if ((cheap = find_reg_note (curr_insn,
6889 REG_RETURNED, NULL_RTX)) != NULL_RTX
6890 && ((cheap = XEXP (cheap, 0)), true)
6891 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
6892 && (hard_regno = reg_renumber[regno]) >= 0
6893 && usage_insns[regno].check == curr_usage_insns_check
6894 /* If there are pending saves/restores, the
6895 optimization is not worth. */
6896 && usage_insns[regno].calls_num == calls_num - 1
6897 && callee_abi.clobbers_reg_p (GET_MODE (cheap), hard_regno))
6899 /* Restore the pseudo from the call result as
6900 REG_RETURNED note says that the pseudo value is
6901 in the call result and the pseudo is an argument
6902 of the call. */
6903 pat = PATTERN (curr_insn);
6904 if (GET_CODE (pat) == PARALLEL)
6905 pat = XVECEXP (pat, 0, 0);
6906 dest = SET_DEST (pat);
6907 /* For multiple return values dest is PARALLEL.
6908 Currently we handle only single return value case. */
6909 if (REG_P (dest))
6911 start_sequence ();
6912 emit_move_insn (cheap, copy_rtx (dest));
6913 restore = get_insns ();
6914 end_sequence ();
6915 lra_process_new_insns (curr_insn, NULL, restore,
6916 "Inserting call parameter restore");
6917 /* We don't need to save/restore of the pseudo from
6918 this call. */
6919 usage_insns[regno].calls_num = calls_num;
6920 remove_from_hard_reg_set
6921 (&full_and_partial_call_clobbers,
6922 GET_MODE (cheap), hard_regno);
6923 bitmap_set_bit (&check_only_regs, regno);
6927 to_inherit_num = 0;
6928 /* Process insn usages. */
6929 for (iter = 0; iter < 2; iter++)
6930 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
6931 reg != NULL;
6932 reg = reg->next)
6933 if ((reg->type != OP_OUT
6934 || (reg->type == OP_OUT && reg->subreg_p))
6935 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
6937 if (src_regno >= FIRST_PSEUDO_REGISTER
6938 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
6940 if (usage_insns[src_regno].check == curr_usage_insns_check
6941 && (next_usage_insns
6942 = usage_insns[src_regno].insns) != NULL_RTX
6943 && NONDEBUG_INSN_P (curr_insn))
6944 add_to_inherit (src_regno, next_usage_insns);
6945 else if (usage_insns[src_regno].check
6946 != -(int) INSN_UID (curr_insn))
6947 /* Add usages but only if the reg is not set up
6948 in the same insn. */
6949 add_next_usage_insn (src_regno, curr_insn, reloads_num);
6951 else if (src_regno < FIRST_PSEUDO_REGISTER
6952 || reg_renumber[src_regno] >= 0)
6954 bool before_p;
6955 rtx_insn *use_insn = curr_insn;
6957 before_p = (JUMP_P (curr_insn)
6958 || (CALL_P (curr_insn) && reg->type == OP_IN));
6959 if (NONDEBUG_INSN_P (curr_insn)
6960 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
6961 && split_if_necessary (src_regno, reg->biggest_mode,
6962 potential_reload_hard_regs,
6963 before_p, curr_insn, max_uid))
6965 if (reg->subreg_p)
6966 check_and_force_assignment_correctness_p = true;
6967 change_p = true;
6968 /* Invalidate. */
6969 usage_insns[src_regno].check = 0;
6970 if (before_p)
6971 use_insn = PREV_INSN (curr_insn);
6973 if (NONDEBUG_INSN_P (curr_insn))
6975 if (src_regno < FIRST_PSEUDO_REGISTER)
6976 add_to_hard_reg_set (&live_hard_regs,
6977 reg->biggest_mode, src_regno);
6978 else
6979 add_to_hard_reg_set (&live_hard_regs,
6980 PSEUDO_REGNO_MODE (src_regno),
6981 reg_renumber[src_regno]);
6983 if (src_regno >= FIRST_PSEUDO_REGISTER)
6984 add_next_usage_insn (src_regno, use_insn, reloads_num);
6985 else
6987 for (i = 0; i < hard_regno_nregs (src_regno, reg->biggest_mode); i++)
6988 add_next_usage_insn (src_regno + i, use_insn, reloads_num);
6992 /* Process used call regs. */
6993 if (curr_id->arg_hard_regs != NULL)
6994 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6995 if (src_regno < FIRST_PSEUDO_REGISTER)
6997 SET_HARD_REG_BIT (live_hard_regs, src_regno);
6998 add_next_usage_insn (src_regno, curr_insn, reloads_num);
7000 for (i = 0; i < to_inherit_num; i++)
7002 src_regno = to_inherit[i].regno;
7003 if (inherit_reload_reg (false, src_regno, ALL_REGS,
7004 curr_insn, to_inherit[i].insns))
7005 change_p = true;
7006 else
7007 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
7010 if (update_reloads_num_p
7011 && NONDEBUG_INSN_P (curr_insn) && curr_set != NULL_RTX)
7013 int regno = -1;
7014 if ((REG_P (SET_DEST (curr_set))
7015 && (regno = REGNO (SET_DEST (curr_set))) >= lra_constraint_new_regno_start
7016 && reg_renumber[regno] < 0
7017 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
7018 || (REG_P (SET_SRC (curr_set))
7019 && (regno = REGNO (SET_SRC (curr_set))) >= lra_constraint_new_regno_start
7020 && reg_renumber[regno] < 0
7021 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
7023 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
7024 reloads_num++;
7025 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
7026 potential_reload_hard_regs |= reg_class_contents[cl];
7029 if (NONDEBUG_INSN_P (curr_insn))
7031 int regno;
7033 /* Invalidate invariants with changed regs. */
7034 curr_id = lra_get_insn_recog_data (curr_insn);
7035 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
7036 if (reg->type != OP_IN)
7038 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
7039 bitmap_set_bit (&invalid_invariant_regs,
7040 ORIGINAL_REGNO (regno_reg_rtx[reg->regno]));
7042 curr_static_id = curr_id->insn_static_data;
7043 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
7044 if (reg->type != OP_IN)
7045 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
7046 if (curr_id->arg_hard_regs != NULL)
7047 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
7048 if (regno >= FIRST_PSEUDO_REGISTER)
7049 bitmap_set_bit (&invalid_invariant_regs,
7050 regno - FIRST_PSEUDO_REGISTER);
7052 /* We reached the start of the current basic block. */
7053 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
7054 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
7056 /* We reached the beginning of the current block -- do
7057 rest of spliting in the current BB. */
7058 to_process = df_get_live_in (curr_bb);
7059 if (BLOCK_FOR_INSN (head) != curr_bb)
7061 /* We are somewhere in the middle of EBB. */
7062 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
7063 curr_bb, &temp_bitmap);
7064 to_process = &temp_bitmap;
7066 head_p = true;
7067 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
7069 if ((int) j >= lra_constraint_new_regno_start)
7070 break;
7071 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
7072 && usage_insns[j].check == curr_usage_insns_check
7073 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
7075 if (need_for_split_p (potential_reload_hard_regs, j))
7077 if (lra_dump_file != NULL && head_p)
7079 fprintf (lra_dump_file,
7080 " ----------------------------------\n");
7081 head_p = false;
7083 if (split_reg (false, j, bb_note (curr_bb),
7084 next_usage_insns, NULL))
7085 change_p = true;
7087 usage_insns[j].check = 0;
7092 return change_p;
7095 /* This value affects EBB forming. If probability of edge from EBB to
7096 a BB is not greater than the following value, we don't add the BB
7097 to EBB. */
7098 #define EBB_PROBABILITY_CUTOFF \
7099 ((REG_BR_PROB_BASE * param_lra_inheritance_ebb_probability_cutoff) / 100)
7101 /* Current number of inheritance/split iteration. */
7102 int lra_inheritance_iter;
7104 /* Entry function for inheritance/split pass. */
7105 void
7106 lra_inheritance (void)
7108 int i;
7109 basic_block bb, start_bb;
7110 edge e;
7112 lra_inheritance_iter++;
7113 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
7114 return;
7115 timevar_push (TV_LRA_INHERITANCE);
7116 if (lra_dump_file != NULL)
7117 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
7118 lra_inheritance_iter);
7119 curr_usage_insns_check = 0;
7120 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
7121 for (i = 0; i < lra_constraint_new_regno_start; i++)
7122 usage_insns[i].check = 0;
7123 bitmap_initialize (&check_only_regs, &reg_obstack);
7124 bitmap_initialize (&invalid_invariant_regs, &reg_obstack);
7125 bitmap_initialize (&live_regs, &reg_obstack);
7126 bitmap_initialize (&temp_bitmap, &reg_obstack);
7127 bitmap_initialize (&ebb_global_regs, &reg_obstack);
7128 FOR_EACH_BB_FN (bb, cfun)
7130 start_bb = bb;
7131 if (lra_dump_file != NULL)
7132 fprintf (lra_dump_file, "EBB");
7133 /* Form a EBB starting with BB. */
7134 bitmap_clear (&ebb_global_regs);
7135 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
7136 for (;;)
7138 if (lra_dump_file != NULL)
7139 fprintf (lra_dump_file, " %d", bb->index);
7140 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
7141 || LABEL_P (BB_HEAD (bb->next_bb)))
7142 break;
7143 e = find_fallthru_edge (bb->succs);
7144 if (! e)
7145 break;
7146 if (e->probability.initialized_p ()
7147 && e->probability.to_reg_br_prob_base () < EBB_PROBABILITY_CUTOFF)
7148 break;
7149 bb = bb->next_bb;
7151 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
7152 if (lra_dump_file != NULL)
7153 fprintf (lra_dump_file, "\n");
7154 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
7155 /* Remember that the EBB head and tail can change in
7156 inherit_in_ebb. */
7157 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
7159 bitmap_release (&ebb_global_regs);
7160 bitmap_release (&temp_bitmap);
7161 bitmap_release (&live_regs);
7162 bitmap_release (&invalid_invariant_regs);
7163 bitmap_release (&check_only_regs);
7164 free (usage_insns);
7166 timevar_pop (TV_LRA_INHERITANCE);
7171 /* This page contains code to undo failed inheritance/split
7172 transformations. */
7174 /* Current number of iteration undoing inheritance/split. */
7175 int lra_undo_inheritance_iter;
7177 /* Fix BB live info LIVE after removing pseudos created on pass doing
7178 inheritance/split which are REMOVED_PSEUDOS. */
7179 static void
7180 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
7182 unsigned int regno;
7183 bitmap_iterator bi;
7185 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
7186 if (bitmap_clear_bit (live, regno)
7187 && REG_P (lra_reg_info[regno].restore_rtx))
7188 bitmap_set_bit (live, REGNO (lra_reg_info[regno].restore_rtx));
7191 /* Return regno of the (subreg of) REG. Otherwise, return a negative
7192 number. */
7193 static int
7194 get_regno (rtx reg)
7196 if (GET_CODE (reg) == SUBREG)
7197 reg = SUBREG_REG (reg);
7198 if (REG_P (reg))
7199 return REGNO (reg);
7200 return -1;
7203 /* Delete a move INSN with destination reg DREGNO and a previous
7204 clobber insn with the same regno. The inheritance/split code can
7205 generate moves with preceding clobber and when we delete such moves
7206 we should delete the clobber insn too to keep the correct life
7207 info. */
7208 static void
7209 delete_move_and_clobber (rtx_insn *insn, int dregno)
7211 rtx_insn *prev_insn = PREV_INSN (insn);
7213 lra_set_insn_deleted (insn);
7214 lra_assert (dregno >= 0);
7215 if (prev_insn != NULL && NONDEBUG_INSN_P (prev_insn)
7216 && GET_CODE (PATTERN (prev_insn)) == CLOBBER
7217 && dregno == get_regno (XEXP (PATTERN (prev_insn), 0)))
7218 lra_set_insn_deleted (prev_insn);
7221 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
7222 return true if we did any change. The undo transformations for
7223 inheritance looks like
7224 i <- i2
7225 p <- i => p <- i2
7226 or removing
7227 p <- i, i <- p, and i <- i3
7228 where p is original pseudo from which inheritance pseudo i was
7229 created, i and i3 are removed inheritance pseudos, i2 is another
7230 not removed inheritance pseudo. All split pseudos or other
7231 occurrences of removed inheritance pseudos are changed on the
7232 corresponding original pseudos.
7234 The function also schedules insns changed and created during
7235 inheritance/split pass for processing by the subsequent constraint
7236 pass. */
7237 static bool
7238 remove_inheritance_pseudos (bitmap remove_pseudos)
7240 basic_block bb;
7241 int regno, sregno, prev_sregno, dregno;
7242 rtx restore_rtx;
7243 rtx set, prev_set;
7244 rtx_insn *prev_insn;
7245 bool change_p, done_p;
7247 change_p = ! bitmap_empty_p (remove_pseudos);
7248 /* We cannot finish the function right away if CHANGE_P is true
7249 because we need to marks insns affected by previous
7250 inheritance/split pass for processing by the subsequent
7251 constraint pass. */
7252 FOR_EACH_BB_FN (bb, cfun)
7254 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
7255 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
7256 FOR_BB_INSNS_REVERSE (bb, curr_insn)
7258 if (! INSN_P (curr_insn))
7259 continue;
7260 done_p = false;
7261 sregno = dregno = -1;
7262 if (change_p && NONDEBUG_INSN_P (curr_insn)
7263 && (set = single_set (curr_insn)) != NULL_RTX)
7265 dregno = get_regno (SET_DEST (set));
7266 sregno = get_regno (SET_SRC (set));
7269 if (sregno >= 0 && dregno >= 0)
7271 if (bitmap_bit_p (remove_pseudos, dregno)
7272 && ! REG_P (lra_reg_info[dregno].restore_rtx))
7274 /* invariant inheritance pseudo <- original pseudo */
7275 if (lra_dump_file != NULL)
7277 fprintf (lra_dump_file, " Removing invariant inheritance:\n");
7278 dump_insn_slim (lra_dump_file, curr_insn);
7279 fprintf (lra_dump_file, "\n");
7281 delete_move_and_clobber (curr_insn, dregno);
7282 done_p = true;
7284 else if (bitmap_bit_p (remove_pseudos, sregno)
7285 && ! REG_P (lra_reg_info[sregno].restore_rtx))
7287 /* reload pseudo <- invariant inheritance pseudo */
7288 start_sequence ();
7289 /* We cannot just change the source. It might be
7290 an insn different from the move. */
7291 emit_insn (lra_reg_info[sregno].restore_rtx);
7292 rtx_insn *new_insns = get_insns ();
7293 end_sequence ();
7294 lra_assert (single_set (new_insns) != NULL
7295 && SET_DEST (set) == SET_DEST (single_set (new_insns)));
7296 lra_process_new_insns (curr_insn, NULL, new_insns,
7297 "Changing reload<-invariant inheritance");
7298 delete_move_and_clobber (curr_insn, dregno);
7299 done_p = true;
7301 else if ((bitmap_bit_p (remove_pseudos, sregno)
7302 && (get_regno (lra_reg_info[sregno].restore_rtx) == dregno
7303 || (bitmap_bit_p (remove_pseudos, dregno)
7304 && get_regno (lra_reg_info[sregno].restore_rtx) >= 0
7305 && (get_regno (lra_reg_info[sregno].restore_rtx)
7306 == get_regno (lra_reg_info[dregno].restore_rtx)))))
7307 || (bitmap_bit_p (remove_pseudos, dregno)
7308 && get_regno (lra_reg_info[dregno].restore_rtx) == sregno))
7309 /* One of the following cases:
7310 original <- removed inheritance pseudo
7311 removed inherit pseudo <- another removed inherit pseudo
7312 removed inherit pseudo <- original pseudo
7314 removed_split_pseudo <- original_reg
7315 original_reg <- removed_split_pseudo */
7317 if (lra_dump_file != NULL)
7319 fprintf (lra_dump_file, " Removing %s:\n",
7320 bitmap_bit_p (&lra_split_regs, sregno)
7321 || bitmap_bit_p (&lra_split_regs, dregno)
7322 ? "split" : "inheritance");
7323 dump_insn_slim (lra_dump_file, curr_insn);
7325 delete_move_and_clobber (curr_insn, dregno);
7326 done_p = true;
7328 else if (bitmap_bit_p (remove_pseudos, sregno)
7329 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
7331 /* Search the following pattern:
7332 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
7333 original_pseudo <- inherit_or_split_pseudo1
7334 where the 2nd insn is the current insn and
7335 inherit_or_split_pseudo2 is not removed. If it is found,
7336 change the current insn onto:
7337 original_pseudo <- inherit_or_split_pseudo2. */
7338 for (prev_insn = PREV_INSN (curr_insn);
7339 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
7340 prev_insn = PREV_INSN (prev_insn))
7342 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
7343 && (prev_set = single_set (prev_insn)) != NULL_RTX
7344 /* There should be no subregs in insn we are
7345 searching because only the original reg might
7346 be in subreg when we changed the mode of
7347 load/store for splitting. */
7348 && REG_P (SET_DEST (prev_set))
7349 && REG_P (SET_SRC (prev_set))
7350 && (int) REGNO (SET_DEST (prev_set)) == sregno
7351 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
7352 >= FIRST_PSEUDO_REGISTER)
7353 && (lra_reg_info[prev_sregno].restore_rtx == NULL_RTX
7355 /* As we consider chain of inheritance or
7356 splitting described in above comment we should
7357 check that sregno and prev_sregno were
7358 inheritance/split pseudos created from the
7359 same original regno. */
7360 (get_regno (lra_reg_info[sregno].restore_rtx) >= 0
7361 && (get_regno (lra_reg_info[sregno].restore_rtx)
7362 == get_regno (lra_reg_info[prev_sregno].restore_rtx))))
7363 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
7365 lra_assert (GET_MODE (SET_SRC (prev_set))
7366 == GET_MODE (regno_reg_rtx[sregno]));
7367 /* Although we have a single set, the insn can
7368 contain more one sregno register occurrence
7369 as a source. Change all occurrences. */
7370 lra_substitute_pseudo_within_insn (curr_insn, sregno,
7371 SET_SRC (prev_set),
7372 false);
7373 /* As we are finishing with processing the insn
7374 here, check the destination too as it might
7375 inheritance pseudo for another pseudo. */
7376 if (bitmap_bit_p (remove_pseudos, dregno)
7377 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
7378 && (restore_rtx
7379 = lra_reg_info[dregno].restore_rtx) != NULL_RTX)
7381 if (GET_CODE (SET_DEST (set)) == SUBREG)
7382 SUBREG_REG (SET_DEST (set)) = restore_rtx;
7383 else
7384 SET_DEST (set) = restore_rtx;
7386 lra_push_insn_and_update_insn_regno_info (curr_insn);
7387 lra_set_used_insn_alternative_by_uid
7388 (INSN_UID (curr_insn), LRA_UNKNOWN_ALT);
7389 done_p = true;
7390 if (lra_dump_file != NULL)
7392 fprintf (lra_dump_file, " Change reload insn:\n");
7393 dump_insn_slim (lra_dump_file, curr_insn);
7398 if (! done_p)
7400 struct lra_insn_reg *reg;
7401 bool restored_regs_p = false;
7402 bool kept_regs_p = false;
7404 curr_id = lra_get_insn_recog_data (curr_insn);
7405 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
7407 regno = reg->regno;
7408 restore_rtx = lra_reg_info[regno].restore_rtx;
7409 if (restore_rtx != NULL_RTX)
7411 if (change_p && bitmap_bit_p (remove_pseudos, regno))
7413 lra_substitute_pseudo_within_insn
7414 (curr_insn, regno, restore_rtx, false);
7415 restored_regs_p = true;
7417 else
7418 kept_regs_p = true;
7421 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
7423 /* The instruction has changed since the previous
7424 constraints pass. */
7425 lra_push_insn_and_update_insn_regno_info (curr_insn);
7426 lra_set_used_insn_alternative_by_uid
7427 (INSN_UID (curr_insn), LRA_UNKNOWN_ALT);
7429 else if (restored_regs_p)
7430 /* The instruction has been restored to the form that
7431 it had during the previous constraints pass. */
7432 lra_update_insn_regno_info (curr_insn);
7433 if (restored_regs_p && lra_dump_file != NULL)
7435 fprintf (lra_dump_file, " Insn after restoring regs:\n");
7436 dump_insn_slim (lra_dump_file, curr_insn);
7441 return change_p;
7444 /* If optional reload pseudos failed to get a hard register or was not
7445 inherited, it is better to remove optional reloads. We do this
7446 transformation after undoing inheritance to figure out necessity to
7447 remove optional reloads easier. Return true if we do any
7448 change. */
7449 static bool
7450 undo_optional_reloads (void)
7452 bool change_p, keep_p;
7453 unsigned int regno, uid;
7454 bitmap_iterator bi, bi2;
7455 rtx_insn *insn;
7456 rtx set, src, dest;
7457 auto_bitmap removed_optional_reload_pseudos (&reg_obstack);
7459 bitmap_copy (removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
7460 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
7462 keep_p = false;
7463 /* Keep optional reloads from previous subpasses. */
7464 if (lra_reg_info[regno].restore_rtx == NULL_RTX
7465 /* If the original pseudo changed its allocation, just
7466 removing the optional pseudo is dangerous as the original
7467 pseudo will have longer live range. */
7468 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] >= 0)
7469 keep_p = true;
7470 else if (reg_renumber[regno] >= 0)
7471 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
7473 insn = lra_insn_recog_data[uid]->insn;
7474 if ((set = single_set (insn)) == NULL_RTX)
7475 continue;
7476 src = SET_SRC (set);
7477 dest = SET_DEST (set);
7478 if ((! REG_P (src) && ! SUBREG_P (src))
7479 || (! REG_P (dest) && ! SUBREG_P (dest)))
7480 continue;
7481 if (get_regno (dest) == (int) regno
7482 /* Ignore insn for optional reloads itself. */
7483 && (get_regno (lra_reg_info[regno].restore_rtx)
7484 != get_regno (src))
7485 /* Check only inheritance on last inheritance pass. */
7486 && get_regno (src) >= new_regno_start
7487 /* Check that the optional reload was inherited. */
7488 && bitmap_bit_p (&lra_inheritance_pseudos, get_regno (src)))
7490 keep_p = true;
7491 break;
7494 if (keep_p)
7496 bitmap_clear_bit (removed_optional_reload_pseudos, regno);
7497 if (lra_dump_file != NULL)
7498 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
7501 change_p = ! bitmap_empty_p (removed_optional_reload_pseudos);
7502 auto_bitmap insn_bitmap (&reg_obstack);
7503 EXECUTE_IF_SET_IN_BITMAP (removed_optional_reload_pseudos, 0, regno, bi)
7505 if (lra_dump_file != NULL)
7506 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
7507 bitmap_copy (insn_bitmap, &lra_reg_info[regno].insn_bitmap);
7508 EXECUTE_IF_SET_IN_BITMAP (insn_bitmap, 0, uid, bi2)
7510 /* We may have already removed a clobber. */
7511 if (!lra_insn_recog_data[uid])
7512 continue;
7513 insn = lra_insn_recog_data[uid]->insn;
7514 if ((set = single_set (insn)) != NULL_RTX)
7516 src = SET_SRC (set);
7517 dest = SET_DEST (set);
7518 if ((REG_P (src) || SUBREG_P (src))
7519 && (REG_P (dest) || SUBREG_P (dest))
7520 && ((get_regno (src) == (int) regno
7521 && (get_regno (lra_reg_info[regno].restore_rtx)
7522 == get_regno (dest)))
7523 || (get_regno (dest) == (int) regno
7524 && (get_regno (lra_reg_info[regno].restore_rtx)
7525 == get_regno (src)))))
7527 if (lra_dump_file != NULL)
7529 fprintf (lra_dump_file, " Deleting move %u\n",
7530 INSN_UID (insn));
7531 dump_insn_slim (lra_dump_file, insn);
7533 delete_move_and_clobber (insn, get_regno (dest));
7534 continue;
7536 /* We should not worry about generation memory-memory
7537 moves here as if the corresponding inheritance did
7538 not work (inheritance pseudo did not get a hard reg),
7539 we remove the inheritance pseudo and the optional
7540 reload. */
7542 if (GET_CODE (PATTERN (insn)) == CLOBBER
7543 && REG_P (SET_DEST (insn))
7544 && get_regno (SET_DEST (insn)) == (int) regno)
7545 /* Refuse to remap clobbers to preexisting pseudos. */
7546 gcc_unreachable ();
7547 lra_substitute_pseudo_within_insn
7548 (insn, regno, lra_reg_info[regno].restore_rtx, false);
7549 lra_update_insn_regno_info (insn);
7550 if (lra_dump_file != NULL)
7552 fprintf (lra_dump_file,
7553 " Restoring original insn:\n");
7554 dump_insn_slim (lra_dump_file, insn);
7558 /* Clear restore_regnos. */
7559 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
7560 lra_reg_info[regno].restore_rtx = NULL_RTX;
7561 return change_p;
7564 /* Entry function for undoing inheritance/split transformation. Return true
7565 if we did any RTL change in this pass. */
7566 bool
7567 lra_undo_inheritance (void)
7569 unsigned int regno;
7570 int hard_regno;
7571 int n_all_inherit, n_inherit, n_all_split, n_split;
7572 rtx restore_rtx;
7573 bitmap_iterator bi;
7574 bool change_p;
7576 lra_undo_inheritance_iter++;
7577 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
7578 return false;
7579 if (lra_dump_file != NULL)
7580 fprintf (lra_dump_file,
7581 "\n********** Undoing inheritance #%d: **********\n\n",
7582 lra_undo_inheritance_iter);
7583 auto_bitmap remove_pseudos (&reg_obstack);
7584 n_inherit = n_all_inherit = 0;
7585 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
7586 if (lra_reg_info[regno].restore_rtx != NULL_RTX)
7588 n_all_inherit++;
7589 if (reg_renumber[regno] < 0
7590 /* If the original pseudo changed its allocation, just
7591 removing inheritance is dangerous as for changing
7592 allocation we used shorter live-ranges. */
7593 && (! REG_P (lra_reg_info[regno].restore_rtx)
7594 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] < 0))
7595 bitmap_set_bit (remove_pseudos, regno);
7596 else
7597 n_inherit++;
7599 if (lra_dump_file != NULL && n_all_inherit != 0)
7600 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
7601 n_inherit, n_all_inherit,
7602 (double) n_inherit / n_all_inherit * 100);
7603 n_split = n_all_split = 0;
7604 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
7605 if ((restore_rtx = lra_reg_info[regno].restore_rtx) != NULL_RTX)
7607 int restore_regno = REGNO (restore_rtx);
7609 n_all_split++;
7610 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
7611 ? reg_renumber[restore_regno] : restore_regno);
7612 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
7613 bitmap_set_bit (remove_pseudos, regno);
7614 else
7616 n_split++;
7617 if (lra_dump_file != NULL)
7618 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
7619 regno, restore_regno);
7622 if (lra_dump_file != NULL && n_all_split != 0)
7623 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
7624 n_split, n_all_split,
7625 (double) n_split / n_all_split * 100);
7626 change_p = remove_inheritance_pseudos (remove_pseudos);
7627 /* Clear restore_regnos. */
7628 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
7629 lra_reg_info[regno].restore_rtx = NULL_RTX;
7630 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
7631 lra_reg_info[regno].restore_rtx = NULL_RTX;
7632 change_p = undo_optional_reloads () || change_p;
7633 return change_p;