Daily bump.
[official-gcc.git] / gcc / lra-constraints.c
blob161b721efb13b0f17e05adc109fbb40baf055b76
1 /* Code for RTL transformations to satisfy insn constraints.
2 Copyright (C) 2010-2020 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file contains code for 3 passes: constraint pass,
23 inheritance/split pass, and pass for undoing failed inheritance and
24 split.
26 The major goal of constraint pass is to transform RTL to satisfy
27 insn and address constraints by:
28 o choosing insn alternatives;
29 o generating *reload insns* (or reloads in brief) and *reload
30 pseudos* which will get necessary hard registers later;
31 o substituting pseudos with equivalent values and removing the
32 instructions that initialized those pseudos.
34 The constraint pass has biggest and most complicated code in LRA.
35 There are a lot of important details like:
36 o reuse of input reload pseudos to simplify reload pseudo
37 allocations;
38 o some heuristics to choose insn alternative to improve the
39 inheritance;
40 o early clobbers etc.
42 The pass is mimicking former reload pass in alternative choosing
43 because the reload pass is oriented to current machine description
44 model. It might be changed if the machine description model is
45 changed.
47 There is special code for preventing all LRA and this pass cycling
48 in case of bugs.
50 On the first iteration of the pass we process every instruction and
51 choose an alternative for each one. On subsequent iterations we try
52 to avoid reprocessing instructions if we can be sure that the old
53 choice is still valid.
55 The inheritance/spilt pass is to transform code to achieve
56 ineheritance and live range splitting. It is done on backward
57 traversal of EBBs.
59 The inheritance optimization goal is to reuse values in hard
60 registers. There is analogous optimization in old reload pass. The
61 inheritance is achieved by following transformation:
63 reload_p1 <- p reload_p1 <- p
64 ... new_p <- reload_p1
65 ... => ...
66 reload_p2 <- p reload_p2 <- new_p
68 where p is spilled and not changed between the insns. Reload_p1 is
69 also called *original pseudo* and new_p is called *inheritance
70 pseudo*.
72 The subsequent assignment pass will try to assign the same (or
73 another if it is not possible) hard register to new_p as to
74 reload_p1 or reload_p2.
76 If the assignment pass fails to assign a hard register to new_p,
77 this file will undo the inheritance and restore the original code.
78 This is because implementing the above sequence with a spilled
79 new_p would make the code much worse. The inheritance is done in
80 EBB scope. The above is just a simplified example to get an idea
81 of the inheritance as the inheritance is also done for non-reload
82 insns.
84 Splitting (transformation) is also done in EBB scope on the same
85 pass as the inheritance:
87 r <- ... or ... <- r r <- ... or ... <- r
88 ... s <- r (new insn -- save)
89 ... =>
90 ... r <- s (new insn -- restore)
91 ... <- r ... <- r
93 The *split pseudo* s is assigned to the hard register of the
94 original pseudo or hard register r.
96 Splitting is done:
97 o In EBBs with high register pressure for global pseudos (living
98 in at least 2 BBs) and assigned to hard registers when there
99 are more one reloads needing the hard registers;
100 o for pseudos needing save/restore code around calls.
102 If the split pseudo still has the same hard register as the
103 original pseudo after the subsequent assignment pass or the
104 original pseudo was split, the opposite transformation is done on
105 the same pass for undoing inheritance. */
107 #undef REG_OK_STRICT
109 #include "config.h"
110 #include "system.h"
111 #include "coretypes.h"
112 #include "backend.h"
113 #include "target.h"
114 #include "rtl.h"
115 #include "tree.h"
116 #include "predict.h"
117 #include "df.h"
118 #include "memmodel.h"
119 #include "tm_p.h"
120 #include "expmed.h"
121 #include "optabs.h"
122 #include "regs.h"
123 #include "ira.h"
124 #include "recog.h"
125 #include "output.h"
126 #include "addresses.h"
127 #include "expr.h"
128 #include "cfgrtl.h"
129 #include "rtl-error.h"
130 #include "lra.h"
131 #include "lra-int.h"
132 #include "print-rtl.h"
133 #include "function-abi.h"
134 #include "rtl-iter.h"
136 /* Value of LRA_CURR_RELOAD_NUM at the beginning of BB of the current
137 insn. Remember that LRA_CURR_RELOAD_NUM is the number of emitted
138 reload insns. */
139 static int bb_reload_num;
141 /* The current insn being processed and corresponding its single set
142 (NULL otherwise), its data (basic block, the insn data, the insn
143 static data, and the mode of each operand). */
144 static rtx_insn *curr_insn;
145 static rtx curr_insn_set;
146 static basic_block curr_bb;
147 static lra_insn_recog_data_t curr_id;
148 static struct lra_static_insn_data *curr_static_id;
149 static machine_mode curr_operand_mode[MAX_RECOG_OPERANDS];
150 /* Mode of the register substituted by its equivalence with VOIDmode
151 (e.g. constant) and whose subreg is given operand of the current
152 insn. VOIDmode in all other cases. */
153 static machine_mode original_subreg_reg_mode[MAX_RECOG_OPERANDS];
157 /* Start numbers for new registers and insns at the current constraints
158 pass start. */
159 static int new_regno_start;
160 static int new_insn_uid_start;
162 /* If LOC is nonnull, strip any outer subreg from it. */
163 static inline rtx *
164 strip_subreg (rtx *loc)
166 return loc && GET_CODE (*loc) == SUBREG ? &SUBREG_REG (*loc) : loc;
169 /* Return hard regno of REGNO or if it is was not assigned to a hard
170 register, use a hard register from its allocno class. */
171 static int
172 get_try_hard_regno (int regno)
174 int hard_regno;
175 enum reg_class rclass;
177 if ((hard_regno = regno) >= FIRST_PSEUDO_REGISTER)
178 hard_regno = lra_get_regno_hard_regno (regno);
179 if (hard_regno >= 0)
180 return hard_regno;
181 rclass = lra_get_allocno_class (regno);
182 if (rclass == NO_REGS)
183 return -1;
184 return ira_class_hard_regs[rclass][0];
187 /* Return the hard regno of X after removing its subreg. If X is not
188 a register or a subreg of a register, return -1. If X is a pseudo,
189 use its assignment. If FINAL_P return the final hard regno which will
190 be after elimination. */
191 static int
192 get_hard_regno (rtx x, bool final_p)
194 rtx reg;
195 int hard_regno;
197 reg = x;
198 if (SUBREG_P (x))
199 reg = SUBREG_REG (x);
200 if (! REG_P (reg))
201 return -1;
202 if (! HARD_REGISTER_NUM_P (hard_regno = REGNO (reg)))
203 hard_regno = lra_get_regno_hard_regno (hard_regno);
204 if (hard_regno < 0)
205 return -1;
206 if (final_p)
207 hard_regno = lra_get_elimination_hard_regno (hard_regno);
208 if (SUBREG_P (x))
209 hard_regno += subreg_regno_offset (hard_regno, GET_MODE (reg),
210 SUBREG_BYTE (x), GET_MODE (x));
211 return hard_regno;
214 /* If REGNO is a hard register or has been allocated a hard register,
215 return the class of that register. If REGNO is a reload pseudo
216 created by the current constraints pass, return its allocno class.
217 Return NO_REGS otherwise. */
218 static enum reg_class
219 get_reg_class (int regno)
221 int hard_regno;
223 if (! HARD_REGISTER_NUM_P (hard_regno = regno))
224 hard_regno = lra_get_regno_hard_regno (regno);
225 if (hard_regno >= 0)
227 hard_regno = lra_get_elimination_hard_regno (hard_regno);
228 return REGNO_REG_CLASS (hard_regno);
230 if (regno >= new_regno_start)
231 return lra_get_allocno_class (regno);
232 return NO_REGS;
235 /* Return true if REG satisfies (or will satisfy) reg class constraint
236 CL. Use elimination first if REG is a hard register. If REG is a
237 reload pseudo created by this constraints pass, assume that it will
238 be allocated a hard register from its allocno class, but allow that
239 class to be narrowed to CL if it is currently a superset of CL and
240 if either:
242 - ALLOW_ALL_RELOAD_CLASS_CHANGES_P is true or
243 - the instruction we're processing is not a reload move.
245 If NEW_CLASS is nonnull, set *NEW_CLASS to the new allocno class of
246 REGNO (reg), or NO_REGS if no change in its class was needed. */
247 static bool
248 in_class_p (rtx reg, enum reg_class cl, enum reg_class *new_class,
249 bool allow_all_reload_class_changes_p = false)
251 enum reg_class rclass, common_class;
252 machine_mode reg_mode;
253 int class_size, hard_regno, nregs, i, j;
254 int regno = REGNO (reg);
256 if (new_class != NULL)
257 *new_class = NO_REGS;
258 if (regno < FIRST_PSEUDO_REGISTER)
260 rtx final_reg = reg;
261 rtx *final_loc = &final_reg;
263 lra_eliminate_reg_if_possible (final_loc);
264 return TEST_HARD_REG_BIT (reg_class_contents[cl], REGNO (*final_loc));
266 reg_mode = GET_MODE (reg);
267 rclass = get_reg_class (regno);
268 if (regno < new_regno_start
269 /* Do not allow the constraints for reload instructions to
270 influence the classes of new pseudos. These reloads are
271 typically moves that have many alternatives, and restricting
272 reload pseudos for one alternative may lead to situations
273 where other reload pseudos are no longer allocatable. */
274 || (!allow_all_reload_class_changes_p
275 && INSN_UID (curr_insn) >= new_insn_uid_start
276 && curr_insn_set != NULL
277 && ((OBJECT_P (SET_SRC (curr_insn_set))
278 && ! CONSTANT_P (SET_SRC (curr_insn_set)))
279 || (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
280 && OBJECT_P (SUBREG_REG (SET_SRC (curr_insn_set)))
281 && ! CONSTANT_P (SUBREG_REG (SET_SRC (curr_insn_set)))))))
282 /* When we don't know what class will be used finally for reload
283 pseudos, we use ALL_REGS. */
284 return ((regno >= new_regno_start && rclass == ALL_REGS)
285 || (rclass != NO_REGS && ira_class_subset_p[rclass][cl]
286 && ! hard_reg_set_subset_p (reg_class_contents[cl],
287 lra_no_alloc_regs)));
288 else
290 common_class = ira_reg_class_subset[rclass][cl];
291 if (new_class != NULL)
292 *new_class = common_class;
293 if (hard_reg_set_subset_p (reg_class_contents[common_class],
294 lra_no_alloc_regs))
295 return false;
296 /* Check that there are enough allocatable regs. */
297 class_size = ira_class_hard_regs_num[common_class];
298 for (i = 0; i < class_size; i++)
300 hard_regno = ira_class_hard_regs[common_class][i];
301 nregs = hard_regno_nregs (hard_regno, reg_mode);
302 if (nregs == 1)
303 return true;
304 for (j = 0; j < nregs; j++)
305 if (TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno + j)
306 || ! TEST_HARD_REG_BIT (reg_class_contents[common_class],
307 hard_regno + j))
308 break;
309 if (j >= nregs)
310 return true;
312 return false;
316 /* Return true if REGNO satisfies a memory constraint. */
317 static bool
318 in_mem_p (int regno)
320 return get_reg_class (regno) == NO_REGS;
323 /* Return 1 if ADDR is a valid memory address for mode MODE in address
324 space AS, and check that each pseudo has the proper kind of hard
325 reg. */
326 static int
327 valid_address_p (machine_mode mode ATTRIBUTE_UNUSED,
328 rtx addr, addr_space_t as)
330 #ifdef GO_IF_LEGITIMATE_ADDRESS
331 lra_assert (ADDR_SPACE_GENERIC_P (as));
332 GO_IF_LEGITIMATE_ADDRESS (mode, addr, win);
333 return 0;
335 win:
336 return 1;
337 #else
338 return targetm.addr_space.legitimate_address_p (mode, addr, 0, as);
339 #endif
342 namespace {
343 /* Temporarily eliminates registers in an address (for the lifetime of
344 the object). */
345 class address_eliminator {
346 public:
347 address_eliminator (struct address_info *ad);
348 ~address_eliminator ();
350 private:
351 struct address_info *m_ad;
352 rtx *m_base_loc;
353 rtx m_base_reg;
354 rtx *m_index_loc;
355 rtx m_index_reg;
359 address_eliminator::address_eliminator (struct address_info *ad)
360 : m_ad (ad),
361 m_base_loc (strip_subreg (ad->base_term)),
362 m_base_reg (NULL_RTX),
363 m_index_loc (strip_subreg (ad->index_term)),
364 m_index_reg (NULL_RTX)
366 if (m_base_loc != NULL)
368 m_base_reg = *m_base_loc;
369 /* If we have non-legitimate address which is decomposed not in
370 the way we expected, don't do elimination here. In such case
371 the address will be reloaded and elimination will be done in
372 reload insn finally. */
373 if (REG_P (m_base_reg))
374 lra_eliminate_reg_if_possible (m_base_loc);
375 if (m_ad->base_term2 != NULL)
376 *m_ad->base_term2 = *m_ad->base_term;
378 if (m_index_loc != NULL)
380 m_index_reg = *m_index_loc;
381 if (REG_P (m_index_reg))
382 lra_eliminate_reg_if_possible (m_index_loc);
386 address_eliminator::~address_eliminator ()
388 if (m_base_loc && *m_base_loc != m_base_reg)
390 *m_base_loc = m_base_reg;
391 if (m_ad->base_term2 != NULL)
392 *m_ad->base_term2 = *m_ad->base_term;
394 if (m_index_loc && *m_index_loc != m_index_reg)
395 *m_index_loc = m_index_reg;
398 /* Return true if the eliminated form of AD is a legitimate target address.
399 If OP is a MEM, AD is the address within OP, otherwise OP should be
400 ignored. CONSTRAINT is one constraint that the operand may need
401 to meet. */
402 static bool
403 valid_address_p (rtx op, struct address_info *ad,
404 enum constraint_num constraint)
406 address_eliminator eliminator (ad);
408 /* Allow a memory OP if it matches CONSTRAINT, even if CONSTRAINT is more
409 forgiving than "m". */
410 if (MEM_P (op)
411 && (insn_extra_memory_constraint (constraint)
412 || insn_extra_special_memory_constraint (constraint))
413 && constraint_satisfied_p (op, constraint))
414 return true;
416 return valid_address_p (ad->mode, *ad->outer, ad->as);
419 /* Return true if the eliminated form of memory reference OP satisfies
420 extra (special) memory constraint CONSTRAINT. */
421 static bool
422 satisfies_memory_constraint_p (rtx op, enum constraint_num constraint)
424 struct address_info ad;
426 decompose_mem_address (&ad, op);
427 address_eliminator eliminator (&ad);
428 return constraint_satisfied_p (op, constraint);
431 /* Return true if the eliminated form of address AD satisfies extra
432 address constraint CONSTRAINT. */
433 static bool
434 satisfies_address_constraint_p (struct address_info *ad,
435 enum constraint_num constraint)
437 address_eliminator eliminator (ad);
438 return constraint_satisfied_p (*ad->outer, constraint);
441 /* Return true if the eliminated form of address OP satisfies extra
442 address constraint CONSTRAINT. */
443 static bool
444 satisfies_address_constraint_p (rtx op, enum constraint_num constraint)
446 struct address_info ad;
448 decompose_lea_address (&ad, &op);
449 return satisfies_address_constraint_p (&ad, constraint);
452 /* Initiate equivalences for LRA. As we keep original equivalences
453 before any elimination, we need to make copies otherwise any change
454 in insns might change the equivalences. */
455 void
456 lra_init_equiv (void)
458 ira_expand_reg_equiv ();
459 for (int i = FIRST_PSEUDO_REGISTER; i < max_reg_num (); i++)
461 rtx res;
463 if ((res = ira_reg_equiv[i].memory) != NULL_RTX)
464 ira_reg_equiv[i].memory = copy_rtx (res);
465 if ((res = ira_reg_equiv[i].invariant) != NULL_RTX)
466 ira_reg_equiv[i].invariant = copy_rtx (res);
470 static rtx loc_equivalence_callback (rtx, const_rtx, void *);
472 /* Update equivalence for REGNO. We need to this as the equivalence
473 might contain other pseudos which are changed by their
474 equivalences. */
475 static void
476 update_equiv (int regno)
478 rtx x;
480 if ((x = ira_reg_equiv[regno].memory) != NULL_RTX)
481 ira_reg_equiv[regno].memory
482 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
483 NULL_RTX);
484 if ((x = ira_reg_equiv[regno].invariant) != NULL_RTX)
485 ira_reg_equiv[regno].invariant
486 = simplify_replace_fn_rtx (x, NULL_RTX, loc_equivalence_callback,
487 NULL_RTX);
490 /* If we have decided to substitute X with another value, return that
491 value, otherwise return X. */
492 static rtx
493 get_equiv (rtx x)
495 int regno;
496 rtx res;
498 if (! REG_P (x) || (regno = REGNO (x)) < FIRST_PSEUDO_REGISTER
499 || ! ira_reg_equiv[regno].defined_p
500 || ! ira_reg_equiv[regno].profitable_p
501 || lra_get_regno_hard_regno (regno) >= 0)
502 return x;
503 if ((res = ira_reg_equiv[regno].memory) != NULL_RTX)
505 if (targetm.cannot_substitute_mem_equiv_p (res))
506 return x;
507 return res;
509 if ((res = ira_reg_equiv[regno].constant) != NULL_RTX)
510 return res;
511 if ((res = ira_reg_equiv[regno].invariant) != NULL_RTX)
512 return res;
513 gcc_unreachable ();
516 /* If we have decided to substitute X with the equivalent value,
517 return that value after elimination for INSN, otherwise return
518 X. */
519 static rtx
520 get_equiv_with_elimination (rtx x, rtx_insn *insn)
522 rtx res = get_equiv (x);
524 if (x == res || CONSTANT_P (res))
525 return res;
526 return lra_eliminate_regs_1 (insn, res, GET_MODE (res),
527 false, false, 0, true);
530 /* Set up curr_operand_mode. */
531 static void
532 init_curr_operand_mode (void)
534 int nop = curr_static_id->n_operands;
535 for (int i = 0; i < nop; i++)
537 machine_mode mode = GET_MODE (*curr_id->operand_loc[i]);
538 if (mode == VOIDmode)
540 /* The .md mode for address operands is the mode of the
541 addressed value rather than the mode of the address itself. */
542 if (curr_id->icode >= 0 && curr_static_id->operand[i].is_address)
543 mode = Pmode;
544 else
545 mode = curr_static_id->operand[i].mode;
547 curr_operand_mode[i] = mode;
553 /* The page contains code to reuse input reloads. */
555 /* Structure describes input reload of the current insns. */
556 struct input_reload
558 /* True for input reload of matched operands. */
559 bool match_p;
560 /* Reloaded value. */
561 rtx input;
562 /* Reload pseudo used. */
563 rtx reg;
566 /* The number of elements in the following array. */
567 static int curr_insn_input_reloads_num;
568 /* Array containing info about input reloads. It is used to find the
569 same input reload and reuse the reload pseudo in this case. */
570 static struct input_reload curr_insn_input_reloads[LRA_MAX_INSN_RELOADS];
572 /* Initiate data concerning reuse of input reloads for the current
573 insn. */
574 static void
575 init_curr_insn_input_reloads (void)
577 curr_insn_input_reloads_num = 0;
580 /* The canonical form of an rtx inside a MEM is not necessarily the same as the
581 canonical form of the rtx outside the MEM. Fix this up in the case that
582 we're reloading an address (and therefore pulling it outside a MEM). */
583 static rtx
584 canonicalize_reload_addr (rtx addr)
586 subrtx_var_iterator::array_type array;
587 FOR_EACH_SUBRTX_VAR (iter, array, addr, NONCONST)
589 rtx x = *iter;
590 if (GET_CODE (x) == MULT && CONST_INT_P (XEXP (x, 1)))
592 const HOST_WIDE_INT ci = INTVAL (XEXP (x, 1));
593 const int pwr2 = exact_log2 (ci);
594 if (pwr2 > 0)
596 /* Rewrite this to use a shift instead, which is canonical when
597 outside of a MEM. */
598 PUT_CODE (x, ASHIFT);
599 XEXP (x, 1) = GEN_INT (pwr2);
604 return addr;
607 /* Create a new pseudo using MODE, RCLASS, ORIGINAL or reuse an existing
608 reload pseudo. Don't reuse an existing reload pseudo if IN_SUBREG_P
609 is true and the reused pseudo should be wrapped up in a SUBREG.
610 The result pseudo is returned through RESULT_REG. Return TRUE if we
611 created a new pseudo, FALSE if we reused an existing reload pseudo.
612 Use TITLE to describe new registers for debug purposes. */
613 static bool
614 get_reload_reg (enum op_type type, machine_mode mode, rtx original,
615 enum reg_class rclass, bool in_subreg_p,
616 const char *title, rtx *result_reg)
618 int i, regno;
619 enum reg_class new_class;
620 bool unique_p = false;
622 if (type == OP_OUT)
624 /* Output reload registers tend to start out with a conservative
625 choice of register class. Usually this is ALL_REGS, although
626 a target might narrow it (for performance reasons) through
627 targetm.preferred_reload_class. It's therefore quite common
628 for a reload instruction to require a more restrictive class
629 than the class that was originally assigned to the reload register.
631 In these situations, it's more efficient to refine the choice
632 of register class rather than create a second reload register.
633 This also helps to avoid cycling for registers that are only
634 used by reload instructions. */
635 if (REG_P (original)
636 && (int) REGNO (original) >= new_regno_start
637 && INSN_UID (curr_insn) >= new_insn_uid_start
638 && in_class_p (original, rclass, &new_class, true))
640 unsigned int regno = REGNO (original);
641 if (lra_dump_file != NULL)
643 fprintf (lra_dump_file, " Reuse r%d for output ", regno);
644 dump_value_slim (lra_dump_file, original, 1);
646 if (new_class != lra_get_allocno_class (regno))
647 lra_change_class (regno, new_class, ", change to", false);
648 if (lra_dump_file != NULL)
649 fprintf (lra_dump_file, "\n");
650 *result_reg = original;
651 return false;
653 *result_reg
654 = lra_create_new_reg_with_unique_value (mode, original, rclass, title);
655 return true;
657 /* Prevent reuse value of expression with side effects,
658 e.g. volatile memory. */
659 if (! side_effects_p (original))
660 for (i = 0; i < curr_insn_input_reloads_num; i++)
662 if (! curr_insn_input_reloads[i].match_p
663 && rtx_equal_p (curr_insn_input_reloads[i].input, original)
664 && in_class_p (curr_insn_input_reloads[i].reg, rclass, &new_class))
666 rtx reg = curr_insn_input_reloads[i].reg;
667 regno = REGNO (reg);
668 /* If input is equal to original and both are VOIDmode,
669 GET_MODE (reg) might be still different from mode.
670 Ensure we don't return *result_reg with wrong mode. */
671 if (GET_MODE (reg) != mode)
673 if (in_subreg_p)
674 continue;
675 if (maybe_lt (GET_MODE_SIZE (GET_MODE (reg)),
676 GET_MODE_SIZE (mode)))
677 continue;
678 reg = lowpart_subreg (mode, reg, GET_MODE (reg));
679 if (reg == NULL_RTX || GET_CODE (reg) != SUBREG)
680 continue;
682 *result_reg = reg;
683 if (lra_dump_file != NULL)
685 fprintf (lra_dump_file, " Reuse r%d for reload ", regno);
686 dump_value_slim (lra_dump_file, original, 1);
688 if (new_class != lra_get_allocno_class (regno))
689 lra_change_class (regno, new_class, ", change to", false);
690 if (lra_dump_file != NULL)
691 fprintf (lra_dump_file, "\n");
692 return false;
694 /* If we have an input reload with a different mode, make sure it
695 will get a different hard reg. */
696 else if (REG_P (original)
697 && REG_P (curr_insn_input_reloads[i].input)
698 && REGNO (original) == REGNO (curr_insn_input_reloads[i].input)
699 && (GET_MODE (original)
700 != GET_MODE (curr_insn_input_reloads[i].input)))
701 unique_p = true;
703 *result_reg = (unique_p
704 ? lra_create_new_reg_with_unique_value
705 : lra_create_new_reg) (mode, original, rclass, title);
706 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
707 curr_insn_input_reloads[curr_insn_input_reloads_num].input = original;
708 curr_insn_input_reloads[curr_insn_input_reloads_num].match_p = false;
709 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = *result_reg;
710 return true;
714 /* The page contains major code to choose the current insn alternative
715 and generate reloads for it. */
717 /* Return the offset from REGNO of the least significant register
718 in (reg:MODE REGNO).
720 This function is used to tell whether two registers satisfy
721 a matching constraint. (reg:MODE1 REGNO1) matches (reg:MODE2 REGNO2) if:
723 REGNO1 + lra_constraint_offset (REGNO1, MODE1)
724 == REGNO2 + lra_constraint_offset (REGNO2, MODE2) */
726 lra_constraint_offset (int regno, machine_mode mode)
728 lra_assert (regno < FIRST_PSEUDO_REGISTER);
730 scalar_int_mode int_mode;
731 if (WORDS_BIG_ENDIAN
732 && is_a <scalar_int_mode> (mode, &int_mode)
733 && GET_MODE_SIZE (int_mode) > UNITS_PER_WORD)
734 return hard_regno_nregs (regno, mode) - 1;
735 return 0;
738 /* Like rtx_equal_p except that it allows a REG and a SUBREG to match
739 if they are the same hard reg, and has special hacks for
740 auto-increment and auto-decrement. This is specifically intended for
741 process_alt_operands to use in determining whether two operands
742 match. X is the operand whose number is the lower of the two.
744 It is supposed that X is the output operand and Y is the input
745 operand. Y_HARD_REGNO is the final hard regno of register Y or
746 register in subreg Y as we know it now. Otherwise, it is a
747 negative value. */
748 static bool
749 operands_match_p (rtx x, rtx y, int y_hard_regno)
751 int i;
752 RTX_CODE code = GET_CODE (x);
753 const char *fmt;
755 if (x == y)
756 return true;
757 if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
758 && (REG_P (y) || (GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y)))))
760 int j;
762 i = get_hard_regno (x, false);
763 if (i < 0)
764 goto slow;
766 if ((j = y_hard_regno) < 0)
767 goto slow;
769 i += lra_constraint_offset (i, GET_MODE (x));
770 j += lra_constraint_offset (j, GET_MODE (y));
772 return i == j;
775 /* If two operands must match, because they are really a single
776 operand of an assembler insn, then two post-increments are invalid
777 because the assembler insn would increment only once. On the
778 other hand, a post-increment matches ordinary indexing if the
779 post-increment is the output operand. */
780 if (code == POST_DEC || code == POST_INC || code == POST_MODIFY)
781 return operands_match_p (XEXP (x, 0), y, y_hard_regno);
783 /* Two pre-increments are invalid because the assembler insn would
784 increment only once. On the other hand, a pre-increment matches
785 ordinary indexing if the pre-increment is the input operand. */
786 if (GET_CODE (y) == PRE_DEC || GET_CODE (y) == PRE_INC
787 || GET_CODE (y) == PRE_MODIFY)
788 return operands_match_p (x, XEXP (y, 0), -1);
790 slow:
792 if (code == REG && REG_P (y))
793 return REGNO (x) == REGNO (y);
795 if (code == REG && GET_CODE (y) == SUBREG && REG_P (SUBREG_REG (y))
796 && x == SUBREG_REG (y))
797 return true;
798 if (GET_CODE (y) == REG && code == SUBREG && REG_P (SUBREG_REG (x))
799 && SUBREG_REG (x) == y)
800 return true;
802 /* Now we have disposed of all the cases in which different rtx
803 codes can match. */
804 if (code != GET_CODE (y))
805 return false;
807 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
808 if (GET_MODE (x) != GET_MODE (y))
809 return false;
811 switch (code)
813 CASE_CONST_UNIQUE:
814 return false;
816 case LABEL_REF:
817 return label_ref_label (x) == label_ref_label (y);
818 case SYMBOL_REF:
819 return XSTR (x, 0) == XSTR (y, 0);
821 default:
822 break;
825 /* Compare the elements. If any pair of corresponding elements fail
826 to match, return false for the whole things. */
828 fmt = GET_RTX_FORMAT (code);
829 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
831 int val, j;
832 switch (fmt[i])
834 case 'w':
835 if (XWINT (x, i) != XWINT (y, i))
836 return false;
837 break;
839 case 'i':
840 if (XINT (x, i) != XINT (y, i))
841 return false;
842 break;
844 case 'p':
845 if (maybe_ne (SUBREG_BYTE (x), SUBREG_BYTE (y)))
846 return false;
847 break;
849 case 'e':
850 val = operands_match_p (XEXP (x, i), XEXP (y, i), -1);
851 if (val == 0)
852 return false;
853 break;
855 case '0':
856 break;
858 case 'E':
859 if (XVECLEN (x, i) != XVECLEN (y, i))
860 return false;
861 for (j = XVECLEN (x, i) - 1; j >= 0; --j)
863 val = operands_match_p (XVECEXP (x, i, j), XVECEXP (y, i, j), -1);
864 if (val == 0)
865 return false;
867 break;
869 /* It is believed that rtx's at this level will never
870 contain anything but integers and other rtx's, except for
871 within LABEL_REFs and SYMBOL_REFs. */
872 default:
873 gcc_unreachable ();
876 return true;
879 /* True if X is a constant that can be forced into the constant pool.
880 MODE is the mode of the operand, or VOIDmode if not known. */
881 #define CONST_POOL_OK_P(MODE, X) \
882 ((MODE) != VOIDmode \
883 && CONSTANT_P (X) \
884 && GET_CODE (X) != HIGH \
885 && GET_MODE_SIZE (MODE).is_constant () \
886 && !targetm.cannot_force_const_mem (MODE, X))
888 /* True if C is a non-empty register class that has too few registers
889 to be safely used as a reload target class. */
890 #define SMALL_REGISTER_CLASS_P(C) \
891 (ira_class_hard_regs_num [(C)] == 1 \
892 || (ira_class_hard_regs_num [(C)] >= 1 \
893 && targetm.class_likely_spilled_p (C)))
895 /* If REG is a reload pseudo, try to make its class satisfying CL. */
896 static void
897 narrow_reload_pseudo_class (rtx reg, enum reg_class cl)
899 enum reg_class rclass;
901 /* Do not make more accurate class from reloads generated. They are
902 mostly moves with a lot of constraints. Making more accurate
903 class may results in very narrow class and impossibility of find
904 registers for several reloads of one insn. */
905 if (INSN_UID (curr_insn) >= new_insn_uid_start)
906 return;
907 if (GET_CODE (reg) == SUBREG)
908 reg = SUBREG_REG (reg);
909 if (! REG_P (reg) || (int) REGNO (reg) < new_regno_start)
910 return;
911 if (in_class_p (reg, cl, &rclass) && rclass != cl)
912 lra_change_class (REGNO (reg), rclass, " Change to", true);
915 /* Searches X for any reference to a reg with the same value as REGNO,
916 returning the rtx of the reference found if any. Otherwise,
917 returns NULL_RTX. */
918 static rtx
919 regno_val_use_in (unsigned int regno, rtx x)
921 const char *fmt;
922 int i, j;
923 rtx tem;
925 if (REG_P (x) && lra_reg_info[REGNO (x)].val == lra_reg_info[regno].val)
926 return x;
928 fmt = GET_RTX_FORMAT (GET_CODE (x));
929 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
931 if (fmt[i] == 'e')
933 if ((tem = regno_val_use_in (regno, XEXP (x, i))))
934 return tem;
936 else if (fmt[i] == 'E')
937 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
938 if ((tem = regno_val_use_in (regno , XVECEXP (x, i, j))))
939 return tem;
942 return NULL_RTX;
945 /* Return true if all current insn non-output operands except INS (it
946 has a negaitve end marker) do not use pseudos with the same value
947 as REGNO. */
948 static bool
949 check_conflict_input_operands (int regno, signed char *ins)
951 int in;
952 int n_operands = curr_static_id->n_operands;
954 for (int nop = 0; nop < n_operands; nop++)
955 if (! curr_static_id->operand[nop].is_operator
956 && curr_static_id->operand[nop].type != OP_OUT)
958 for (int i = 0; (in = ins[i]) >= 0; i++)
959 if (in == nop)
960 break;
961 if (in < 0
962 && regno_val_use_in (regno, *curr_id->operand_loc[nop]) != NULL_RTX)
963 return false;
965 return true;
968 /* Generate reloads for matching OUT and INS (array of input operand
969 numbers with end marker -1) with reg class GOAL_CLASS, considering
970 output operands OUTS (similar array to INS) needing to be in different
971 registers. Add input and output reloads correspondingly to the lists
972 *BEFORE and *AFTER. OUT might be negative. In this case we generate
973 input reloads for matched input operands INS. EARLY_CLOBBER_P is a flag
974 that the output operand is early clobbered for chosen alternative. */
975 static void
976 match_reload (signed char out, signed char *ins, signed char *outs,
977 enum reg_class goal_class, rtx_insn **before,
978 rtx_insn **after, bool early_clobber_p)
980 bool out_conflict;
981 int i, in;
982 rtx new_in_reg, new_out_reg, reg;
983 machine_mode inmode, outmode;
984 rtx in_rtx = *curr_id->operand_loc[ins[0]];
985 rtx out_rtx = out < 0 ? in_rtx : *curr_id->operand_loc[out];
987 inmode = curr_operand_mode[ins[0]];
988 outmode = out < 0 ? inmode : curr_operand_mode[out];
989 push_to_sequence (*before);
990 if (inmode != outmode)
992 /* process_alt_operands has already checked that the mode sizes
993 are ordered. */
994 if (partial_subreg_p (outmode, inmode))
996 reg = new_in_reg
997 = lra_create_new_reg_with_unique_value (inmode, in_rtx,
998 goal_class, "");
999 new_out_reg = gen_lowpart_SUBREG (outmode, reg);
1000 LRA_SUBREG_P (new_out_reg) = 1;
1001 /* If the input reg is dying here, we can use the same hard
1002 register for REG and IN_RTX. We do it only for original
1003 pseudos as reload pseudos can die although original
1004 pseudos still live where reload pseudos dies. */
1005 if (REG_P (in_rtx) && (int) REGNO (in_rtx) < lra_new_regno_start
1006 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
1007 && (!early_clobber_p
1008 || check_conflict_input_operands(REGNO (in_rtx), ins)))
1009 lra_assign_reg_val (REGNO (in_rtx), REGNO (reg));
1011 else
1013 reg = new_out_reg
1014 = lra_create_new_reg_with_unique_value (outmode, out_rtx,
1015 goal_class, "");
1016 new_in_reg = gen_lowpart_SUBREG (inmode, reg);
1017 /* NEW_IN_REG is non-paradoxical subreg. We don't want
1018 NEW_OUT_REG living above. We add clobber clause for
1019 this. This is just a temporary clobber. We can remove
1020 it at the end of LRA work. */
1021 rtx_insn *clobber = emit_clobber (new_out_reg);
1022 LRA_TEMP_CLOBBER_P (PATTERN (clobber)) = 1;
1023 LRA_SUBREG_P (new_in_reg) = 1;
1024 if (GET_CODE (in_rtx) == SUBREG)
1026 rtx subreg_reg = SUBREG_REG (in_rtx);
1028 /* If SUBREG_REG is dying here and sub-registers IN_RTX
1029 and NEW_IN_REG are similar, we can use the same hard
1030 register for REG and SUBREG_REG. */
1031 if (REG_P (subreg_reg)
1032 && (int) REGNO (subreg_reg) < lra_new_regno_start
1033 && GET_MODE (subreg_reg) == outmode
1034 && known_eq (SUBREG_BYTE (in_rtx), SUBREG_BYTE (new_in_reg))
1035 && find_regno_note (curr_insn, REG_DEAD, REGNO (subreg_reg))
1036 && (! early_clobber_p
1037 || check_conflict_input_operands (REGNO (subreg_reg),
1038 ins)))
1039 lra_assign_reg_val (REGNO (subreg_reg), REGNO (reg));
1043 else
1045 /* Pseudos have values -- see comments for lra_reg_info.
1046 Different pseudos with the same value do not conflict even if
1047 they live in the same place. When we create a pseudo we
1048 assign value of original pseudo (if any) from which we
1049 created the new pseudo. If we create the pseudo from the
1050 input pseudo, the new pseudo will have no conflict with the
1051 input pseudo which is wrong when the input pseudo lives after
1052 the insn and as the new pseudo value is changed by the insn
1053 output. Therefore we create the new pseudo from the output
1054 except the case when we have single matched dying input
1055 pseudo.
1057 We cannot reuse the current output register because we might
1058 have a situation like "a <- a op b", where the constraints
1059 force the second input operand ("b") to match the output
1060 operand ("a"). "b" must then be copied into a new register
1061 so that it doesn't clobber the current value of "a".
1063 We cannot use the same value if the output pseudo is
1064 early clobbered or the input pseudo is mentioned in the
1065 output, e.g. as an address part in memory, because
1066 output reload will actually extend the pseudo liveness.
1067 We don't care about eliminable hard regs here as we are
1068 interesting only in pseudos. */
1070 /* Matching input's register value is the same as one of the other
1071 output operand. Output operands in a parallel insn must be in
1072 different registers. */
1073 out_conflict = false;
1074 if (REG_P (in_rtx))
1076 for (i = 0; outs[i] >= 0; i++)
1078 rtx other_out_rtx = *curr_id->operand_loc[outs[i]];
1079 if (REG_P (other_out_rtx)
1080 && (regno_val_use_in (REGNO (in_rtx), other_out_rtx)
1081 != NULL_RTX))
1083 out_conflict = true;
1084 break;
1089 new_in_reg = new_out_reg
1090 = (! early_clobber_p && ins[1] < 0 && REG_P (in_rtx)
1091 && (int) REGNO (in_rtx) < lra_new_regno_start
1092 && find_regno_note (curr_insn, REG_DEAD, REGNO (in_rtx))
1093 && (! early_clobber_p
1094 || check_conflict_input_operands (REGNO (in_rtx), ins))
1095 && (out < 0
1096 || regno_val_use_in (REGNO (in_rtx), out_rtx) == NULL_RTX)
1097 && !out_conflict
1098 ? lra_create_new_reg (inmode, in_rtx, goal_class, "")
1099 : lra_create_new_reg_with_unique_value (outmode, out_rtx,
1100 goal_class, ""));
1102 /* In operand can be got from transformations before processing insn
1103 constraints. One example of such transformations is subreg
1104 reloading (see function simplify_operand_subreg). The new
1105 pseudos created by the transformations might have inaccurate
1106 class (ALL_REGS) and we should make their classes more
1107 accurate. */
1108 narrow_reload_pseudo_class (in_rtx, goal_class);
1109 lra_emit_move (copy_rtx (new_in_reg), in_rtx);
1110 *before = get_insns ();
1111 end_sequence ();
1112 /* Add the new pseudo to consider values of subsequent input reload
1113 pseudos. */
1114 lra_assert (curr_insn_input_reloads_num < LRA_MAX_INSN_RELOADS);
1115 curr_insn_input_reloads[curr_insn_input_reloads_num].input = in_rtx;
1116 curr_insn_input_reloads[curr_insn_input_reloads_num].match_p = true;
1117 curr_insn_input_reloads[curr_insn_input_reloads_num++].reg = new_in_reg;
1118 for (i = 0; (in = ins[i]) >= 0; i++)
1119 if (GET_MODE (*curr_id->operand_loc[in]) == VOIDmode
1120 || GET_MODE (new_in_reg) == GET_MODE (*curr_id->operand_loc[in]))
1121 *curr_id->operand_loc[in] = new_in_reg;
1122 else
1124 lra_assert
1125 (GET_MODE (new_out_reg) == GET_MODE (*curr_id->operand_loc[in]));
1126 *curr_id->operand_loc[in] = new_out_reg;
1128 lra_update_dups (curr_id, ins);
1129 if (out < 0)
1130 return;
1131 /* See a comment for the input operand above. */
1132 narrow_reload_pseudo_class (out_rtx, goal_class);
1133 if (find_reg_note (curr_insn, REG_UNUSED, out_rtx) == NULL_RTX)
1135 start_sequence ();
1136 if (out >= 0 && curr_static_id->operand[out].strict_low)
1137 out_rtx = gen_rtx_STRICT_LOW_PART (VOIDmode, out_rtx);
1138 lra_emit_move (out_rtx, copy_rtx (new_out_reg));
1139 emit_insn (*after);
1140 *after = get_insns ();
1141 end_sequence ();
1143 *curr_id->operand_loc[out] = new_out_reg;
1144 lra_update_dup (curr_id, out);
1147 /* Return register class which is union of all reg classes in insn
1148 constraint alternative string starting with P. */
1149 static enum reg_class
1150 reg_class_from_constraints (const char *p)
1152 int c, len;
1153 enum reg_class op_class = NO_REGS;
1156 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
1158 case '#':
1159 case ',':
1160 return op_class;
1162 case 'g':
1163 op_class = reg_class_subunion[op_class][GENERAL_REGS];
1164 break;
1166 default:
1167 enum constraint_num cn = lookup_constraint (p);
1168 enum reg_class cl = reg_class_for_constraint (cn);
1169 if (cl == NO_REGS)
1171 if (insn_extra_address_constraint (cn))
1172 op_class
1173 = (reg_class_subunion
1174 [op_class][base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1175 ADDRESS, SCRATCH)]);
1176 break;
1179 op_class = reg_class_subunion[op_class][cl];
1180 break;
1182 while ((p += len), c);
1183 return op_class;
1186 /* If OP is a register, return the class of the register as per
1187 get_reg_class, otherwise return NO_REGS. */
1188 static inline enum reg_class
1189 get_op_class (rtx op)
1191 return REG_P (op) ? get_reg_class (REGNO (op)) : NO_REGS;
1194 /* Return generated insn mem_pseudo:=val if TO_P or val:=mem_pseudo
1195 otherwise. If modes of MEM_PSEUDO and VAL are different, use
1196 SUBREG for VAL to make them equal. */
1197 static rtx_insn *
1198 emit_spill_move (bool to_p, rtx mem_pseudo, rtx val)
1200 if (GET_MODE (mem_pseudo) != GET_MODE (val))
1202 /* Usually size of mem_pseudo is greater than val size but in
1203 rare cases it can be less as it can be defined by target
1204 dependent macro HARD_REGNO_CALLER_SAVE_MODE. */
1205 if (! MEM_P (val))
1207 val = gen_lowpart_SUBREG (GET_MODE (mem_pseudo),
1208 GET_CODE (val) == SUBREG
1209 ? SUBREG_REG (val) : val);
1210 LRA_SUBREG_P (val) = 1;
1212 else
1214 mem_pseudo = gen_lowpart_SUBREG (GET_MODE (val), mem_pseudo);
1215 LRA_SUBREG_P (mem_pseudo) = 1;
1218 return to_p ? gen_move_insn (mem_pseudo, val)
1219 : gen_move_insn (val, mem_pseudo);
1222 /* Process a special case insn (register move), return true if we
1223 don't need to process it anymore. INSN should be a single set
1224 insn. Set up that RTL was changed through CHANGE_P and that hook
1225 TARGET_SECONDARY_MEMORY_NEEDED says to use secondary memory through
1226 SEC_MEM_P. */
1227 static bool
1228 check_and_process_move (bool *change_p, bool *sec_mem_p ATTRIBUTE_UNUSED)
1230 int sregno, dregno;
1231 rtx dest, src, dreg, sreg, new_reg, scratch_reg;
1232 rtx_insn *before;
1233 enum reg_class dclass, sclass, secondary_class;
1234 secondary_reload_info sri;
1236 lra_assert (curr_insn_set != NULL_RTX);
1237 dreg = dest = SET_DEST (curr_insn_set);
1238 sreg = src = SET_SRC (curr_insn_set);
1239 if (GET_CODE (dest) == SUBREG)
1240 dreg = SUBREG_REG (dest);
1241 if (GET_CODE (src) == SUBREG)
1242 sreg = SUBREG_REG (src);
1243 if (! (REG_P (dreg) || MEM_P (dreg)) || ! (REG_P (sreg) || MEM_P (sreg)))
1244 return false;
1245 sclass = dclass = NO_REGS;
1246 if (REG_P (dreg))
1247 dclass = get_reg_class (REGNO (dreg));
1248 gcc_assert (dclass < LIM_REG_CLASSES);
1249 if (dclass == ALL_REGS)
1250 /* ALL_REGS is used for new pseudos created by transformations
1251 like reload of SUBREG_REG (see function
1252 simplify_operand_subreg). We don't know their class yet. We
1253 should figure out the class from processing the insn
1254 constraints not in this fast path function. Even if ALL_REGS
1255 were a right class for the pseudo, secondary_... hooks usually
1256 are not define for ALL_REGS. */
1257 return false;
1258 if (REG_P (sreg))
1259 sclass = get_reg_class (REGNO (sreg));
1260 gcc_assert (sclass < LIM_REG_CLASSES);
1261 if (sclass == ALL_REGS)
1262 /* See comments above. */
1263 return false;
1264 if (sclass == NO_REGS && dclass == NO_REGS)
1265 return false;
1266 if (targetm.secondary_memory_needed (GET_MODE (src), sclass, dclass)
1267 && ((sclass != NO_REGS && dclass != NO_REGS)
1268 || (GET_MODE (src)
1269 != targetm.secondary_memory_needed_mode (GET_MODE (src)))))
1271 *sec_mem_p = true;
1272 return false;
1274 if (! REG_P (dreg) || ! REG_P (sreg))
1275 return false;
1276 sri.prev_sri = NULL;
1277 sri.icode = CODE_FOR_nothing;
1278 sri.extra_cost = 0;
1279 secondary_class = NO_REGS;
1280 /* Set up hard register for a reload pseudo for hook
1281 secondary_reload because some targets just ignore unassigned
1282 pseudos in the hook. */
1283 if (dclass != NO_REGS && lra_get_regno_hard_regno (REGNO (dreg)) < 0)
1285 dregno = REGNO (dreg);
1286 reg_renumber[dregno] = ira_class_hard_regs[dclass][0];
1288 else
1289 dregno = -1;
1290 if (sclass != NO_REGS && lra_get_regno_hard_regno (REGNO (sreg)) < 0)
1292 sregno = REGNO (sreg);
1293 reg_renumber[sregno] = ira_class_hard_regs[sclass][0];
1295 else
1296 sregno = -1;
1297 if (sclass != NO_REGS)
1298 secondary_class
1299 = (enum reg_class) targetm.secondary_reload (false, dest,
1300 (reg_class_t) sclass,
1301 GET_MODE (src), &sri);
1302 if (sclass == NO_REGS
1303 || ((secondary_class != NO_REGS || sri.icode != CODE_FOR_nothing)
1304 && dclass != NO_REGS))
1306 enum reg_class old_sclass = secondary_class;
1307 secondary_reload_info old_sri = sri;
1309 sri.prev_sri = NULL;
1310 sri.icode = CODE_FOR_nothing;
1311 sri.extra_cost = 0;
1312 secondary_class
1313 = (enum reg_class) targetm.secondary_reload (true, src,
1314 (reg_class_t) dclass,
1315 GET_MODE (src), &sri);
1316 /* Check the target hook consistency. */
1317 lra_assert
1318 ((secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1319 || (old_sclass == NO_REGS && old_sri.icode == CODE_FOR_nothing)
1320 || (secondary_class == old_sclass && sri.icode == old_sri.icode));
1322 if (sregno >= 0)
1323 reg_renumber [sregno] = -1;
1324 if (dregno >= 0)
1325 reg_renumber [dregno] = -1;
1326 if (secondary_class == NO_REGS && sri.icode == CODE_FOR_nothing)
1327 return false;
1328 *change_p = true;
1329 new_reg = NULL_RTX;
1330 if (secondary_class != NO_REGS)
1331 new_reg = lra_create_new_reg_with_unique_value (GET_MODE (src), NULL_RTX,
1332 secondary_class,
1333 "secondary");
1334 start_sequence ();
1335 if (sri.icode == CODE_FOR_nothing)
1336 lra_emit_move (new_reg, src);
1337 else
1339 enum reg_class scratch_class;
1341 scratch_class = (reg_class_from_constraints
1342 (insn_data[sri.icode].operand[2].constraint));
1343 scratch_reg = (lra_create_new_reg_with_unique_value
1344 (insn_data[sri.icode].operand[2].mode, NULL_RTX,
1345 scratch_class, "scratch"));
1346 emit_insn (GEN_FCN (sri.icode) (new_reg != NULL_RTX ? new_reg : dest,
1347 src, scratch_reg));
1349 before = get_insns ();
1350 end_sequence ();
1351 lra_process_new_insns (curr_insn, before, NULL, "Inserting the move");
1352 if (new_reg != NULL_RTX)
1353 SET_SRC (curr_insn_set) = new_reg;
1354 else
1356 if (lra_dump_file != NULL)
1358 fprintf (lra_dump_file, "Deleting move %u\n", INSN_UID (curr_insn));
1359 dump_insn_slim (lra_dump_file, curr_insn);
1361 lra_set_insn_deleted (curr_insn);
1362 return true;
1364 return false;
1367 /* The following data describe the result of process_alt_operands.
1368 The data are used in curr_insn_transform to generate reloads. */
1370 /* The chosen reg classes which should be used for the corresponding
1371 operands. */
1372 static enum reg_class goal_alt[MAX_RECOG_OPERANDS];
1373 /* True if the operand should be the same as another operand and that
1374 other operand does not need a reload. */
1375 static bool goal_alt_match_win[MAX_RECOG_OPERANDS];
1376 /* True if the operand does not need a reload. */
1377 static bool goal_alt_win[MAX_RECOG_OPERANDS];
1378 /* True if the operand can be offsetable memory. */
1379 static bool goal_alt_offmemok[MAX_RECOG_OPERANDS];
1380 /* The number of an operand to which given operand can be matched to. */
1381 static int goal_alt_matches[MAX_RECOG_OPERANDS];
1382 /* The number of elements in the following array. */
1383 static int goal_alt_dont_inherit_ops_num;
1384 /* Numbers of operands whose reload pseudos should not be inherited. */
1385 static int goal_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
1386 /* True if the insn commutative operands should be swapped. */
1387 static bool goal_alt_swapped;
1388 /* The chosen insn alternative. */
1389 static int goal_alt_number;
1391 /* True if the corresponding operand is the result of an equivalence
1392 substitution. */
1393 static bool equiv_substition_p[MAX_RECOG_OPERANDS];
1395 /* The following five variables are used to choose the best insn
1396 alternative. They reflect final characteristics of the best
1397 alternative. */
1399 /* Number of necessary reloads and overall cost reflecting the
1400 previous value and other unpleasantness of the best alternative. */
1401 static int best_losers, best_overall;
1402 /* Overall number hard registers used for reloads. For example, on
1403 some targets we need 2 general registers to reload DFmode and only
1404 one floating point register. */
1405 static int best_reload_nregs;
1406 /* Overall number reflecting distances of previous reloading the same
1407 value. The distances are counted from the current BB start. It is
1408 used to improve inheritance chances. */
1409 static int best_reload_sum;
1411 /* True if the current insn should have no correspondingly input or
1412 output reloads. */
1413 static bool no_input_reloads_p, no_output_reloads_p;
1415 /* True if we swapped the commutative operands in the current
1416 insn. */
1417 static int curr_swapped;
1419 /* if CHECK_ONLY_P is false, arrange for address element *LOC to be a
1420 register of class CL. Add any input reloads to list BEFORE. AFTER
1421 is nonnull if *LOC is an automodified value; handle that case by
1422 adding the required output reloads to list AFTER. Return true if
1423 the RTL was changed.
1425 if CHECK_ONLY_P is true, check that the *LOC is a correct address
1426 register. Return false if the address register is correct. */
1427 static bool
1428 process_addr_reg (rtx *loc, bool check_only_p, rtx_insn **before, rtx_insn **after,
1429 enum reg_class cl)
1431 int regno;
1432 enum reg_class rclass, new_class;
1433 rtx reg;
1434 rtx new_reg;
1435 machine_mode mode;
1436 bool subreg_p, before_p = false;
1438 subreg_p = GET_CODE (*loc) == SUBREG;
1439 if (subreg_p)
1441 reg = SUBREG_REG (*loc);
1442 mode = GET_MODE (reg);
1444 /* For mode with size bigger than ptr_mode, there unlikely to be "mov"
1445 between two registers with different classes, but there normally will
1446 be "mov" which transfers element of vector register into the general
1447 register, and this normally will be a subreg which should be reloaded
1448 as a whole. This is particularly likely to be triggered when
1449 -fno-split-wide-types specified. */
1450 if (!REG_P (reg)
1451 || in_class_p (reg, cl, &new_class)
1452 || known_le (GET_MODE_SIZE (mode), GET_MODE_SIZE (ptr_mode)))
1453 loc = &SUBREG_REG (*loc);
1456 reg = *loc;
1457 mode = GET_MODE (reg);
1458 if (! REG_P (reg))
1460 if (check_only_p)
1461 return true;
1462 /* Always reload memory in an address even if the target supports
1463 such addresses. */
1464 new_reg = lra_create_new_reg_with_unique_value (mode, reg, cl, "address");
1465 before_p = true;
1467 else
1469 regno = REGNO (reg);
1470 rclass = get_reg_class (regno);
1471 if (! check_only_p
1472 && (*loc = get_equiv_with_elimination (reg, curr_insn)) != reg)
1474 if (lra_dump_file != NULL)
1476 fprintf (lra_dump_file,
1477 "Changing pseudo %d in address of insn %u on equiv ",
1478 REGNO (reg), INSN_UID (curr_insn));
1479 dump_value_slim (lra_dump_file, *loc, 1);
1480 fprintf (lra_dump_file, "\n");
1482 *loc = copy_rtx (*loc);
1484 if (*loc != reg || ! in_class_p (reg, cl, &new_class))
1486 if (check_only_p)
1487 return true;
1488 reg = *loc;
1489 if (get_reload_reg (after == NULL ? OP_IN : OP_INOUT,
1490 mode, reg, cl, subreg_p, "address", &new_reg))
1491 before_p = true;
1493 else if (new_class != NO_REGS && rclass != new_class)
1495 if (check_only_p)
1496 return true;
1497 lra_change_class (regno, new_class, " Change to", true);
1498 return false;
1500 else
1501 return false;
1503 if (before_p)
1505 push_to_sequence (*before);
1506 lra_emit_move (new_reg, reg);
1507 *before = get_insns ();
1508 end_sequence ();
1510 *loc = new_reg;
1511 if (after != NULL)
1513 start_sequence ();
1514 lra_emit_move (before_p ? copy_rtx (reg) : reg, new_reg);
1515 emit_insn (*after);
1516 *after = get_insns ();
1517 end_sequence ();
1519 return true;
1522 /* Insert move insn in simplify_operand_subreg. BEFORE returns
1523 the insn to be inserted before curr insn. AFTER returns the
1524 the insn to be inserted after curr insn. ORIGREG and NEWREG
1525 are the original reg and new reg for reload. */
1526 static void
1527 insert_move_for_subreg (rtx_insn **before, rtx_insn **after, rtx origreg,
1528 rtx newreg)
1530 if (before)
1532 push_to_sequence (*before);
1533 lra_emit_move (newreg, origreg);
1534 *before = get_insns ();
1535 end_sequence ();
1537 if (after)
1539 start_sequence ();
1540 lra_emit_move (origreg, newreg);
1541 emit_insn (*after);
1542 *after = get_insns ();
1543 end_sequence ();
1547 static int valid_address_p (machine_mode mode, rtx addr, addr_space_t as);
1548 static bool process_address (int, bool, rtx_insn **, rtx_insn **);
1550 /* Make reloads for subreg in operand NOP with internal subreg mode
1551 REG_MODE, add new reloads for further processing. Return true if
1552 any change was done. */
1553 static bool
1554 simplify_operand_subreg (int nop, machine_mode reg_mode)
1556 int hard_regno, inner_hard_regno;
1557 rtx_insn *before, *after;
1558 machine_mode mode, innermode;
1559 rtx reg, new_reg;
1560 rtx operand = *curr_id->operand_loc[nop];
1561 enum reg_class regclass;
1562 enum op_type type;
1564 before = after = NULL;
1566 if (GET_CODE (operand) != SUBREG)
1567 return false;
1569 mode = GET_MODE (operand);
1570 reg = SUBREG_REG (operand);
1571 innermode = GET_MODE (reg);
1572 type = curr_static_id->operand[nop].type;
1573 if (MEM_P (reg))
1575 const bool addr_was_valid
1576 = valid_address_p (innermode, XEXP (reg, 0), MEM_ADDR_SPACE (reg));
1577 alter_subreg (curr_id->operand_loc[nop], false);
1578 rtx subst = *curr_id->operand_loc[nop];
1579 lra_assert (MEM_P (subst));
1580 const bool addr_is_valid = valid_address_p (GET_MODE (subst),
1581 XEXP (subst, 0),
1582 MEM_ADDR_SPACE (subst));
1583 if (!addr_was_valid
1584 || addr_is_valid
1585 || ((get_constraint_type (lookup_constraint
1586 (curr_static_id->operand[nop].constraint))
1587 != CT_SPECIAL_MEMORY)
1588 /* We still can reload address and if the address is
1589 valid, we can remove subreg without reloading its
1590 inner memory. */
1591 && valid_address_p (GET_MODE (subst),
1592 regno_reg_rtx
1593 [ira_class_hard_regs
1594 [base_reg_class (GET_MODE (subst),
1595 MEM_ADDR_SPACE (subst),
1596 ADDRESS, SCRATCH)][0]],
1597 MEM_ADDR_SPACE (subst))))
1599 /* If we change the address for a paradoxical subreg of memory, the
1600 new address might violate the necessary alignment or the access
1601 might be slow; take this into consideration. We need not worry
1602 about accesses beyond allocated memory for paradoxical memory
1603 subregs as we don't substitute such equiv memory (see processing
1604 equivalences in function lra_constraints) and because for spilled
1605 pseudos we allocate stack memory enough for the biggest
1606 corresponding paradoxical subreg.
1608 However, do not blindly simplify a (subreg (mem ...)) for
1609 WORD_REGISTER_OPERATIONS targets as this may lead to loading junk
1610 data into a register when the inner is narrower than outer or
1611 missing important data from memory when the inner is wider than
1612 outer. This rule only applies to modes that are no wider than
1613 a word.
1615 If valid memory becomes invalid after subreg elimination
1616 and address might be different we still have to reload
1617 memory.
1619 if ((! addr_was_valid
1620 || addr_is_valid
1621 || known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (innermode)))
1622 && !(maybe_ne (GET_MODE_PRECISION (mode),
1623 GET_MODE_PRECISION (innermode))
1624 && known_le (GET_MODE_SIZE (mode), UNITS_PER_WORD)
1625 && known_le (GET_MODE_SIZE (innermode), UNITS_PER_WORD)
1626 && WORD_REGISTER_OPERATIONS)
1627 && (!(MEM_ALIGN (subst) < GET_MODE_ALIGNMENT (mode)
1628 && targetm.slow_unaligned_access (mode, MEM_ALIGN (subst)))
1629 || (MEM_ALIGN (reg) < GET_MODE_ALIGNMENT (innermode)
1630 && targetm.slow_unaligned_access (innermode,
1631 MEM_ALIGN (reg)))))
1632 return true;
1634 *curr_id->operand_loc[nop] = operand;
1636 /* But if the address was not valid, we cannot reload the MEM without
1637 reloading the address first. */
1638 if (!addr_was_valid)
1639 process_address (nop, false, &before, &after);
1641 /* INNERMODE is fast, MODE slow. Reload the mem in INNERMODE. */
1642 enum reg_class rclass
1643 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1644 if (get_reload_reg (curr_static_id->operand[nop].type, innermode,
1645 reg, rclass, TRUE, "slow/invalid mem", &new_reg))
1647 bool insert_before, insert_after;
1648 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1650 insert_before = (type != OP_OUT
1651 || partial_subreg_p (mode, innermode));
1652 insert_after = type != OP_IN;
1653 insert_move_for_subreg (insert_before ? &before : NULL,
1654 insert_after ? &after : NULL,
1655 reg, new_reg);
1657 SUBREG_REG (operand) = new_reg;
1659 /* Convert to MODE. */
1660 reg = operand;
1661 rclass
1662 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1663 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1664 rclass, TRUE, "slow/invalid mem", &new_reg))
1666 bool insert_before, insert_after;
1667 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1669 insert_before = type != OP_OUT;
1670 insert_after = type != OP_IN;
1671 insert_move_for_subreg (insert_before ? &before : NULL,
1672 insert_after ? &after : NULL,
1673 reg, new_reg);
1675 *curr_id->operand_loc[nop] = new_reg;
1676 lra_process_new_insns (curr_insn, before, after,
1677 "Inserting slow/invalid mem reload");
1678 return true;
1681 /* If the address was valid and became invalid, prefer to reload
1682 the memory. Typical case is when the index scale should
1683 correspond the memory. */
1684 *curr_id->operand_loc[nop] = operand;
1685 /* Do not return false here as the MEM_P (reg) will be processed
1686 later in this function. */
1688 else if (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER)
1690 alter_subreg (curr_id->operand_loc[nop], false);
1691 return true;
1693 else if (CONSTANT_P (reg))
1695 /* Try to simplify subreg of constant. It is usually result of
1696 equivalence substitution. */
1697 if (innermode == VOIDmode
1698 && (innermode = original_subreg_reg_mode[nop]) == VOIDmode)
1699 innermode = curr_static_id->operand[nop].mode;
1700 if ((new_reg = simplify_subreg (mode, reg, innermode,
1701 SUBREG_BYTE (operand))) != NULL_RTX)
1703 *curr_id->operand_loc[nop] = new_reg;
1704 return true;
1707 /* Put constant into memory when we have mixed modes. It generates
1708 a better code in most cases as it does not need a secondary
1709 reload memory. It also prevents LRA looping when LRA is using
1710 secondary reload memory again and again. */
1711 if (CONSTANT_P (reg) && CONST_POOL_OK_P (reg_mode, reg)
1712 && SCALAR_INT_MODE_P (reg_mode) != SCALAR_INT_MODE_P (mode))
1714 SUBREG_REG (operand) = force_const_mem (reg_mode, reg);
1715 alter_subreg (curr_id->operand_loc[nop], false);
1716 return true;
1718 /* Force a reload of the SUBREG_REG if this is a constant or PLUS or
1719 if there may be a problem accessing OPERAND in the outer
1720 mode. */
1721 if ((REG_P (reg)
1722 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1723 && (hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1724 /* Don't reload paradoxical subregs because we could be looping
1725 having repeatedly final regno out of hard regs range. */
1726 && (hard_regno_nregs (hard_regno, innermode)
1727 >= hard_regno_nregs (hard_regno, mode))
1728 && simplify_subreg_regno (hard_regno, innermode,
1729 SUBREG_BYTE (operand), mode) < 0
1730 /* Don't reload subreg for matching reload. It is actually
1731 valid subreg in LRA. */
1732 && ! LRA_SUBREG_P (operand))
1733 || CONSTANT_P (reg) || GET_CODE (reg) == PLUS || MEM_P (reg))
1735 enum reg_class rclass;
1737 if (REG_P (reg))
1738 /* There is a big probability that we will get the same class
1739 for the new pseudo and we will get the same insn which
1740 means infinite looping. So spill the new pseudo. */
1741 rclass = NO_REGS;
1742 else
1743 /* The class will be defined later in curr_insn_transform. */
1744 rclass
1745 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1747 if (get_reload_reg (curr_static_id->operand[nop].type, reg_mode, reg,
1748 rclass, TRUE, "subreg reg", &new_reg))
1750 bool insert_before, insert_after;
1751 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1753 insert_before = (type != OP_OUT
1754 || read_modify_subreg_p (operand));
1755 insert_after = (type != OP_IN);
1756 insert_move_for_subreg (insert_before ? &before : NULL,
1757 insert_after ? &after : NULL,
1758 reg, new_reg);
1760 SUBREG_REG (operand) = new_reg;
1761 lra_process_new_insns (curr_insn, before, after,
1762 "Inserting subreg reload");
1763 return true;
1765 /* Force a reload for a paradoxical subreg. For paradoxical subreg,
1766 IRA allocates hardreg to the inner pseudo reg according to its mode
1767 instead of the outermode, so the size of the hardreg may not be enough
1768 to contain the outermode operand, in that case we may need to insert
1769 reload for the reg. For the following two types of paradoxical subreg,
1770 we need to insert reload:
1771 1. If the op_type is OP_IN, and the hardreg could not be paired with
1772 other hardreg to contain the outermode operand
1773 (checked by in_hard_reg_set_p), we need to insert the reload.
1774 2. If the op_type is OP_OUT or OP_INOUT.
1776 Here is a paradoxical subreg example showing how the reload is generated:
1778 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1779 (subreg:TI (reg:DI 107 [ __comp ]) 0)) {*movti_internal_rex64}
1781 In IRA, reg107 is allocated to a DImode hardreg. We use x86-64 as example
1782 here, if reg107 is assigned to hardreg R15, because R15 is the last
1783 hardreg, compiler cannot find another hardreg to pair with R15 to
1784 contain TImode data. So we insert a TImode reload reg180 for it.
1785 After reload is inserted:
1787 (insn 283 0 0 (set (subreg:DI (reg:TI 180 [orig:107 __comp ] [107]) 0)
1788 (reg:DI 107 [ __comp ])) -1
1789 (insn 5 4 7 2 (set (reg:TI 106 [ __comp ])
1790 (subreg:TI (reg:TI 180 [orig:107 __comp ] [107]) 0)) {*movti_internal_rex64}
1792 Two reload hard registers will be allocated to reg180 to save TImode data
1793 in LRA_assign.
1795 For LRA pseudos this should normally be handled by the biggest_mode
1796 mechanism. However, it's possible for new uses of an LRA pseudo
1797 to be introduced after we've allocated it, such as when undoing
1798 inheritance, and the allocated register might not then be appropriate
1799 for the new uses. */
1800 else if (REG_P (reg)
1801 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1802 && paradoxical_subreg_p (operand)
1803 && (inner_hard_regno = lra_get_regno_hard_regno (REGNO (reg))) >= 0
1804 && ((hard_regno
1805 = simplify_subreg_regno (inner_hard_regno, innermode,
1806 SUBREG_BYTE (operand), mode)) < 0
1807 || ((hard_regno_nregs (inner_hard_regno, innermode)
1808 < hard_regno_nregs (hard_regno, mode))
1809 && (regclass = lra_get_allocno_class (REGNO (reg)))
1810 && (type != OP_IN
1811 || !in_hard_reg_set_p (reg_class_contents[regclass],
1812 mode, hard_regno)
1813 || overlaps_hard_reg_set_p (lra_no_alloc_regs,
1814 mode, hard_regno)))))
1816 /* The class will be defined later in curr_insn_transform. */
1817 enum reg_class rclass
1818 = (enum reg_class) targetm.preferred_reload_class (reg, ALL_REGS);
1820 if (get_reload_reg (curr_static_id->operand[nop].type, mode, reg,
1821 rclass, TRUE, "paradoxical subreg", &new_reg))
1823 rtx subreg;
1824 bool insert_before, insert_after;
1826 PUT_MODE (new_reg, mode);
1827 subreg = gen_lowpart_SUBREG (innermode, new_reg);
1828 bitmap_set_bit (&lra_subreg_reload_pseudos, REGNO (new_reg));
1830 insert_before = (type != OP_OUT);
1831 insert_after = (type != OP_IN);
1832 insert_move_for_subreg (insert_before ? &before : NULL,
1833 insert_after ? &after : NULL,
1834 reg, subreg);
1836 SUBREG_REG (operand) = new_reg;
1837 lra_process_new_insns (curr_insn, before, after,
1838 "Inserting paradoxical subreg reload");
1839 return true;
1841 return false;
1844 /* Return TRUE if X refers for a hard register from SET. */
1845 static bool
1846 uses_hard_regs_p (rtx x, HARD_REG_SET set)
1848 int i, j, x_hard_regno;
1849 machine_mode mode;
1850 const char *fmt;
1851 enum rtx_code code;
1853 if (x == NULL_RTX)
1854 return false;
1855 code = GET_CODE (x);
1856 mode = GET_MODE (x);
1858 if (code == SUBREG)
1860 /* For all SUBREGs we want to check whether the full multi-register
1861 overlaps the set. For normal SUBREGs this means 'get_hard_regno' of
1862 the inner register, for paradoxical SUBREGs this means the
1863 'get_hard_regno' of the full SUBREG and for complete SUBREGs either is
1864 fine. Use the wider mode for all cases. */
1865 rtx subreg = SUBREG_REG (x);
1866 mode = wider_subreg_mode (x);
1867 if (mode == GET_MODE (subreg))
1869 x = subreg;
1870 code = GET_CODE (x);
1874 if (REG_P (x) || SUBREG_P (x))
1876 x_hard_regno = get_hard_regno (x, true);
1877 return (x_hard_regno >= 0
1878 && overlaps_hard_reg_set_p (set, mode, x_hard_regno));
1880 if (MEM_P (x))
1882 struct address_info ad;
1884 decompose_mem_address (&ad, x);
1885 if (ad.base_term != NULL && uses_hard_regs_p (*ad.base_term, set))
1886 return true;
1887 if (ad.index_term != NULL && uses_hard_regs_p (*ad.index_term, set))
1888 return true;
1890 fmt = GET_RTX_FORMAT (code);
1891 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1893 if (fmt[i] == 'e')
1895 if (uses_hard_regs_p (XEXP (x, i), set))
1896 return true;
1898 else if (fmt[i] == 'E')
1900 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1901 if (uses_hard_regs_p (XVECEXP (x, i, j), set))
1902 return true;
1905 return false;
1908 /* Return true if OP is a spilled pseudo. */
1909 static inline bool
1910 spilled_pseudo_p (rtx op)
1912 return (REG_P (op)
1913 && REGNO (op) >= FIRST_PSEUDO_REGISTER && in_mem_p (REGNO (op)));
1916 /* Return true if X is a general constant. */
1917 static inline bool
1918 general_constant_p (rtx x)
1920 return CONSTANT_P (x) && (! flag_pic || LEGITIMATE_PIC_OPERAND_P (x));
1923 static bool
1924 reg_in_class_p (rtx reg, enum reg_class cl)
1926 if (cl == NO_REGS)
1927 return get_reg_class (REGNO (reg)) == NO_REGS;
1928 return in_class_p (reg, cl, NULL);
1931 /* Return true if SET of RCLASS contains no hard regs which can be
1932 used in MODE. */
1933 static bool
1934 prohibited_class_reg_set_mode_p (enum reg_class rclass,
1935 HARD_REG_SET &set,
1936 machine_mode mode)
1938 HARD_REG_SET temp;
1940 lra_assert (hard_reg_set_subset_p (reg_class_contents[rclass], set));
1941 temp = set & ~lra_no_alloc_regs;
1942 return (hard_reg_set_subset_p
1943 (temp, ira_prohibited_class_mode_regs[rclass][mode]));
1947 /* Used to check validity info about small class input operands. It
1948 should be incremented at start of processing an insn
1949 alternative. */
1950 static unsigned int curr_small_class_check = 0;
1952 /* Update number of used inputs of class OP_CLASS for operand NOP
1953 of alternative NALT. Return true if we have more such class operands
1954 than the number of available regs. */
1955 static bool
1956 update_and_check_small_class_inputs (int nop, int nalt,
1957 enum reg_class op_class)
1959 static unsigned int small_class_check[LIM_REG_CLASSES];
1960 static int small_class_input_nums[LIM_REG_CLASSES];
1962 if (SMALL_REGISTER_CLASS_P (op_class)
1963 /* We are interesting in classes became small because of fixing
1964 some hard regs, e.g. by an user through GCC options. */
1965 && hard_reg_set_intersect_p (reg_class_contents[op_class],
1966 ira_no_alloc_regs)
1967 && (curr_static_id->operand[nop].type != OP_OUT
1968 || TEST_BIT (curr_static_id->operand[nop].early_clobber_alts, nalt)))
1970 if (small_class_check[op_class] == curr_small_class_check)
1971 small_class_input_nums[op_class]++;
1972 else
1974 small_class_check[op_class] = curr_small_class_check;
1975 small_class_input_nums[op_class] = 1;
1977 if (small_class_input_nums[op_class] > ira_class_hard_regs_num[op_class])
1978 return true;
1980 return false;
1983 /* Major function to choose the current insn alternative and what
1984 operands should be reloaded and how. If ONLY_ALTERNATIVE is not
1985 negative we should consider only this alternative. Return false if
1986 we cannot choose the alternative or find how to reload the
1987 operands. */
1988 static bool
1989 process_alt_operands (int only_alternative)
1991 bool ok_p = false;
1992 int nop, overall, nalt;
1993 int n_alternatives = curr_static_id->n_alternatives;
1994 int n_operands = curr_static_id->n_operands;
1995 /* LOSERS counts the operands that don't fit this alternative and
1996 would require loading. */
1997 int losers;
1998 int addr_losers;
1999 /* REJECT is a count of how undesirable this alternative says it is
2000 if any reloading is required. If the alternative matches exactly
2001 then REJECT is ignored, but otherwise it gets this much counted
2002 against it in addition to the reloading needed. */
2003 int reject;
2004 /* This is defined by '!' or '?' alternative constraint and added to
2005 reject. But in some cases it can be ignored. */
2006 int static_reject;
2007 int op_reject;
2008 /* The number of elements in the following array. */
2009 int early_clobbered_regs_num;
2010 /* Numbers of operands which are early clobber registers. */
2011 int early_clobbered_nops[MAX_RECOG_OPERANDS];
2012 enum reg_class curr_alt[MAX_RECOG_OPERANDS];
2013 HARD_REG_SET curr_alt_set[MAX_RECOG_OPERANDS];
2014 bool curr_alt_match_win[MAX_RECOG_OPERANDS];
2015 bool curr_alt_win[MAX_RECOG_OPERANDS];
2016 bool curr_alt_offmemok[MAX_RECOG_OPERANDS];
2017 int curr_alt_matches[MAX_RECOG_OPERANDS];
2018 /* The number of elements in the following array. */
2019 int curr_alt_dont_inherit_ops_num;
2020 /* Numbers of operands whose reload pseudos should not be inherited. */
2021 int curr_alt_dont_inherit_ops[MAX_RECOG_OPERANDS];
2022 rtx op;
2023 /* The register when the operand is a subreg of register, otherwise the
2024 operand itself. */
2025 rtx no_subreg_reg_operand[MAX_RECOG_OPERANDS];
2026 /* The register if the operand is a register or subreg of register,
2027 otherwise NULL. */
2028 rtx operand_reg[MAX_RECOG_OPERANDS];
2029 int hard_regno[MAX_RECOG_OPERANDS];
2030 machine_mode biggest_mode[MAX_RECOG_OPERANDS];
2031 int reload_nregs, reload_sum;
2032 bool costly_p;
2033 enum reg_class cl;
2035 /* Calculate some data common for all alternatives to speed up the
2036 function. */
2037 for (nop = 0; nop < n_operands; nop++)
2039 rtx reg;
2041 op = no_subreg_reg_operand[nop] = *curr_id->operand_loc[nop];
2042 /* The real hard regno of the operand after the allocation. */
2043 hard_regno[nop] = get_hard_regno (op, true);
2045 operand_reg[nop] = reg = op;
2046 biggest_mode[nop] = GET_MODE (op);
2047 if (GET_CODE (op) == SUBREG)
2049 biggest_mode[nop] = wider_subreg_mode (op);
2050 operand_reg[nop] = reg = SUBREG_REG (op);
2052 if (! REG_P (reg))
2053 operand_reg[nop] = NULL_RTX;
2054 else if (REGNO (reg) >= FIRST_PSEUDO_REGISTER
2055 || ((int) REGNO (reg)
2056 == lra_get_elimination_hard_regno (REGNO (reg))))
2057 no_subreg_reg_operand[nop] = reg;
2058 else
2059 operand_reg[nop] = no_subreg_reg_operand[nop]
2060 /* Just use natural mode for elimination result. It should
2061 be enough for extra constraints hooks. */
2062 = regno_reg_rtx[hard_regno[nop]];
2065 /* The constraints are made of several alternatives. Each operand's
2066 constraint looks like foo,bar,... with commas separating the
2067 alternatives. The first alternatives for all operands go
2068 together, the second alternatives go together, etc.
2070 First loop over alternatives. */
2071 alternative_mask preferred = curr_id->preferred_alternatives;
2072 if (only_alternative >= 0)
2073 preferred &= ALTERNATIVE_BIT (only_alternative);
2075 for (nalt = 0; nalt < n_alternatives; nalt++)
2077 /* Loop over operands for one constraint alternative. */
2078 if (!TEST_BIT (preferred, nalt))
2079 continue;
2081 bool matching_early_clobber[MAX_RECOG_OPERANDS];
2082 curr_small_class_check++;
2083 overall = losers = addr_losers = 0;
2084 static_reject = reject = reload_nregs = reload_sum = 0;
2085 for (nop = 0; nop < n_operands; nop++)
2087 int inc = (curr_static_id
2088 ->operand_alternative[nalt * n_operands + nop].reject);
2089 if (lra_dump_file != NULL && inc != 0)
2090 fprintf (lra_dump_file,
2091 " Staticly defined alt reject+=%d\n", inc);
2092 static_reject += inc;
2093 matching_early_clobber[nop] = 0;
2095 reject += static_reject;
2096 early_clobbered_regs_num = 0;
2098 for (nop = 0; nop < n_operands; nop++)
2100 const char *p;
2101 char *end;
2102 int len, c, m, i, opalt_num, this_alternative_matches;
2103 bool win, did_match, offmemok, early_clobber_p;
2104 /* false => this operand can be reloaded somehow for this
2105 alternative. */
2106 bool badop;
2107 /* true => this operand can be reloaded if the alternative
2108 allows regs. */
2109 bool winreg;
2110 /* True if a constant forced into memory would be OK for
2111 this operand. */
2112 bool constmemok;
2113 enum reg_class this_alternative, this_costly_alternative;
2114 HARD_REG_SET this_alternative_set, this_costly_alternative_set;
2115 bool this_alternative_match_win, this_alternative_win;
2116 bool this_alternative_offmemok;
2117 bool scratch_p;
2118 machine_mode mode;
2119 enum constraint_num cn;
2121 opalt_num = nalt * n_operands + nop;
2122 if (curr_static_id->operand_alternative[opalt_num].anything_ok)
2124 /* Fast track for no constraints at all. */
2125 curr_alt[nop] = NO_REGS;
2126 CLEAR_HARD_REG_SET (curr_alt_set[nop]);
2127 curr_alt_win[nop] = true;
2128 curr_alt_match_win[nop] = false;
2129 curr_alt_offmemok[nop] = false;
2130 curr_alt_matches[nop] = -1;
2131 continue;
2134 op = no_subreg_reg_operand[nop];
2135 mode = curr_operand_mode[nop];
2137 win = did_match = winreg = offmemok = constmemok = false;
2138 badop = true;
2140 early_clobber_p = false;
2141 p = curr_static_id->operand_alternative[opalt_num].constraint;
2143 this_costly_alternative = this_alternative = NO_REGS;
2144 /* We update set of possible hard regs besides its class
2145 because reg class might be inaccurate. For example,
2146 union of LO_REGS (l), HI_REGS(h), and STACK_REG(k) in ARM
2147 is translated in HI_REGS because classes are merged by
2148 pairs and there is no accurate intermediate class. */
2149 CLEAR_HARD_REG_SET (this_alternative_set);
2150 CLEAR_HARD_REG_SET (this_costly_alternative_set);
2151 this_alternative_win = false;
2152 this_alternative_match_win = false;
2153 this_alternative_offmemok = false;
2154 this_alternative_matches = -1;
2156 /* An empty constraint should be excluded by the fast
2157 track. */
2158 lra_assert (*p != 0 && *p != ',');
2160 op_reject = 0;
2161 /* Scan this alternative's specs for this operand; set WIN
2162 if the operand fits any letter in this alternative.
2163 Otherwise, clear BADOP if this operand could fit some
2164 letter after reloads, or set WINREG if this operand could
2165 fit after reloads provided the constraint allows some
2166 registers. */
2167 costly_p = false;
2170 switch ((c = *p, len = CONSTRAINT_LEN (c, p)), c)
2172 case '\0':
2173 len = 0;
2174 break;
2175 case ',':
2176 c = '\0';
2177 break;
2179 case '&':
2180 early_clobber_p = true;
2181 break;
2183 case '$':
2184 op_reject += LRA_MAX_REJECT;
2185 break;
2186 case '^':
2187 op_reject += LRA_LOSER_COST_FACTOR;
2188 break;
2190 case '#':
2191 /* Ignore rest of this alternative. */
2192 c = '\0';
2193 break;
2195 case '0': case '1': case '2': case '3': case '4':
2196 case '5': case '6': case '7': case '8': case '9':
2198 int m_hregno;
2199 bool match_p;
2201 m = strtoul (p, &end, 10);
2202 p = end;
2203 len = 0;
2204 lra_assert (nop > m);
2206 /* Reject matches if we don't know which operand is
2207 bigger. This situation would arguably be a bug in
2208 an .md pattern, but could also occur in a user asm. */
2209 if (!ordered_p (GET_MODE_SIZE (biggest_mode[m]),
2210 GET_MODE_SIZE (biggest_mode[nop])))
2211 break;
2213 /* Don't match wrong asm insn operands for proper
2214 diagnostic later. */
2215 if (INSN_CODE (curr_insn) < 0
2216 && (curr_operand_mode[m] == BLKmode
2217 || curr_operand_mode[nop] == BLKmode)
2218 && curr_operand_mode[m] != curr_operand_mode[nop])
2219 break;
2221 m_hregno = get_hard_regno (*curr_id->operand_loc[m], false);
2222 /* We are supposed to match a previous operand.
2223 If we do, we win if that one did. If we do
2224 not, count both of the operands as losers.
2225 (This is too conservative, since most of the
2226 time only a single reload insn will be needed
2227 to make the two operands win. As a result,
2228 this alternative may be rejected when it is
2229 actually desirable.) */
2230 match_p = false;
2231 if (operands_match_p (*curr_id->operand_loc[nop],
2232 *curr_id->operand_loc[m], m_hregno))
2234 /* We should reject matching of an early
2235 clobber operand if the matching operand is
2236 not dying in the insn. */
2237 if (!TEST_BIT (curr_static_id->operand[m]
2238 .early_clobber_alts, nalt)
2239 || operand_reg[nop] == NULL_RTX
2240 || (find_regno_note (curr_insn, REG_DEAD,
2241 REGNO (op))
2242 || REGNO (op) == REGNO (operand_reg[m])))
2243 match_p = true;
2245 if (match_p)
2247 /* If we are matching a non-offsettable
2248 address where an offsettable address was
2249 expected, then we must reject this
2250 combination, because we can't reload
2251 it. */
2252 if (curr_alt_offmemok[m]
2253 && MEM_P (*curr_id->operand_loc[m])
2254 && curr_alt[m] == NO_REGS && ! curr_alt_win[m])
2255 continue;
2257 else
2259 /* If the operands do not match and one
2260 operand is INOUT, we can not match them.
2261 Try other possibilities, e.g. other
2262 alternatives or commutative operand
2263 exchange. */
2264 if (curr_static_id->operand[nop].type == OP_INOUT
2265 || curr_static_id->operand[m].type == OP_INOUT)
2266 break;
2267 /* Operands don't match. If the operands are
2268 different user defined explicit hard
2269 registers, then we cannot make them match
2270 when one is early clobber operand. */
2271 if ((REG_P (*curr_id->operand_loc[nop])
2272 || SUBREG_P (*curr_id->operand_loc[nop]))
2273 && (REG_P (*curr_id->operand_loc[m])
2274 || SUBREG_P (*curr_id->operand_loc[m])))
2276 rtx nop_reg = *curr_id->operand_loc[nop];
2277 if (SUBREG_P (nop_reg))
2278 nop_reg = SUBREG_REG (nop_reg);
2279 rtx m_reg = *curr_id->operand_loc[m];
2280 if (SUBREG_P (m_reg))
2281 m_reg = SUBREG_REG (m_reg);
2283 if (REG_P (nop_reg)
2284 && HARD_REGISTER_P (nop_reg)
2285 && REG_USERVAR_P (nop_reg)
2286 && REG_P (m_reg)
2287 && HARD_REGISTER_P (m_reg)
2288 && REG_USERVAR_P (m_reg))
2290 int i;
2292 for (i = 0; i < early_clobbered_regs_num; i++)
2293 if (m == early_clobbered_nops[i])
2294 break;
2295 if (i < early_clobbered_regs_num
2296 || early_clobber_p)
2297 break;
2300 /* Both operands must allow a reload register,
2301 otherwise we cannot make them match. */
2302 if (curr_alt[m] == NO_REGS)
2303 break;
2304 /* Retroactively mark the operand we had to
2305 match as a loser, if it wasn't already and
2306 it wasn't matched to a register constraint
2307 (e.g it might be matched by memory). */
2308 if (curr_alt_win[m]
2309 && (operand_reg[m] == NULL_RTX
2310 || hard_regno[m] < 0))
2312 losers++;
2313 reload_nregs
2314 += (ira_reg_class_max_nregs[curr_alt[m]]
2315 [GET_MODE (*curr_id->operand_loc[m])]);
2318 /* Prefer matching earlyclobber alternative as
2319 it results in less hard regs required for
2320 the insn than a non-matching earlyclobber
2321 alternative. */
2322 if (TEST_BIT (curr_static_id->operand[m]
2323 .early_clobber_alts, nalt))
2325 if (lra_dump_file != NULL)
2326 fprintf
2327 (lra_dump_file,
2328 " %d Matching earlyclobber alt:"
2329 " reject--\n",
2330 nop);
2331 if (!matching_early_clobber[m])
2333 reject--;
2334 matching_early_clobber[m] = 1;
2337 /* Otherwise we prefer no matching
2338 alternatives because it gives more freedom
2339 in RA. */
2340 else if (operand_reg[nop] == NULL_RTX
2341 || (find_regno_note (curr_insn, REG_DEAD,
2342 REGNO (operand_reg[nop]))
2343 == NULL_RTX))
2345 if (lra_dump_file != NULL)
2346 fprintf
2347 (lra_dump_file,
2348 " %d Matching alt: reject+=2\n",
2349 nop);
2350 reject += 2;
2353 /* If we have to reload this operand and some
2354 previous operand also had to match the same
2355 thing as this operand, we don't know how to do
2356 that. */
2357 if (!match_p || !curr_alt_win[m])
2359 for (i = 0; i < nop; i++)
2360 if (curr_alt_matches[i] == m)
2361 break;
2362 if (i < nop)
2363 break;
2365 else
2366 did_match = true;
2368 this_alternative_matches = m;
2369 /* This can be fixed with reloads if the operand
2370 we are supposed to match can be fixed with
2371 reloads. */
2372 badop = false;
2373 this_alternative = curr_alt[m];
2374 this_alternative_set = curr_alt_set[m];
2375 winreg = this_alternative != NO_REGS;
2376 break;
2379 case 'g':
2380 if (MEM_P (op)
2381 || general_constant_p (op)
2382 || spilled_pseudo_p (op))
2383 win = true;
2384 cl = GENERAL_REGS;
2385 goto reg;
2387 default:
2388 cn = lookup_constraint (p);
2389 switch (get_constraint_type (cn))
2391 case CT_REGISTER:
2392 cl = reg_class_for_constraint (cn);
2393 if (cl != NO_REGS)
2394 goto reg;
2395 break;
2397 case CT_CONST_INT:
2398 if (CONST_INT_P (op)
2399 && insn_const_int_ok_for_constraint (INTVAL (op), cn))
2400 win = true;
2401 break;
2403 case CT_MEMORY:
2404 if (MEM_P (op)
2405 && satisfies_memory_constraint_p (op, cn))
2406 win = true;
2407 else if (spilled_pseudo_p (op))
2408 win = true;
2410 /* If we didn't already win, we can reload constants
2411 via force_const_mem or put the pseudo value into
2412 memory, or make other memory by reloading the
2413 address like for 'o'. */
2414 if (CONST_POOL_OK_P (mode, op)
2415 || MEM_P (op) || REG_P (op)
2416 /* We can restore the equiv insn by a
2417 reload. */
2418 || equiv_substition_p[nop])
2419 badop = false;
2420 constmemok = true;
2421 offmemok = true;
2422 break;
2424 case CT_ADDRESS:
2425 /* An asm operand with an address constraint
2426 that doesn't satisfy address_operand has
2427 is_address cleared, so that we don't try to
2428 make a non-address fit. */
2429 if (!curr_static_id->operand[nop].is_address)
2430 break;
2431 /* If we didn't already win, we can reload the address
2432 into a base register. */
2433 if (satisfies_address_constraint_p (op, cn))
2434 win = true;
2435 cl = base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
2436 ADDRESS, SCRATCH);
2437 badop = false;
2438 goto reg;
2440 case CT_FIXED_FORM:
2441 if (constraint_satisfied_p (op, cn))
2442 win = true;
2443 break;
2445 case CT_SPECIAL_MEMORY:
2446 if (MEM_P (op)
2447 && satisfies_memory_constraint_p (op, cn))
2448 win = true;
2449 else if (spilled_pseudo_p (op))
2450 win = true;
2451 break;
2453 break;
2455 reg:
2456 if (mode == BLKmode)
2457 break;
2458 this_alternative = reg_class_subunion[this_alternative][cl];
2459 this_alternative_set |= reg_class_contents[cl];
2460 if (costly_p)
2462 this_costly_alternative
2463 = reg_class_subunion[this_costly_alternative][cl];
2464 this_costly_alternative_set |= reg_class_contents[cl];
2466 winreg = true;
2467 if (REG_P (op))
2469 if (hard_regno[nop] >= 0
2470 && in_hard_reg_set_p (this_alternative_set,
2471 mode, hard_regno[nop]))
2472 win = true;
2473 else if (hard_regno[nop] < 0
2474 && in_class_p (op, this_alternative, NULL))
2475 win = true;
2477 break;
2479 if (c != ' ' && c != '\t')
2480 costly_p = c == '*';
2482 while ((p += len), c);
2484 scratch_p = (operand_reg[nop] != NULL_RTX
2485 && lra_former_scratch_p (REGNO (operand_reg[nop])));
2486 /* Record which operands fit this alternative. */
2487 if (win)
2489 this_alternative_win = true;
2490 if (operand_reg[nop] != NULL_RTX)
2492 if (hard_regno[nop] >= 0)
2494 if (in_hard_reg_set_p (this_costly_alternative_set,
2495 mode, hard_regno[nop]))
2497 if (lra_dump_file != NULL)
2498 fprintf (lra_dump_file,
2499 " %d Costly set: reject++\n",
2500 nop);
2501 reject++;
2504 else
2506 /* Prefer won reg to spilled pseudo under other
2507 equal conditions for possibe inheritance. */
2508 if (! scratch_p)
2510 if (lra_dump_file != NULL)
2511 fprintf
2512 (lra_dump_file,
2513 " %d Non pseudo reload: reject++\n",
2514 nop);
2515 reject++;
2517 if (in_class_p (operand_reg[nop],
2518 this_costly_alternative, NULL))
2520 if (lra_dump_file != NULL)
2521 fprintf
2522 (lra_dump_file,
2523 " %d Non pseudo costly reload:"
2524 " reject++\n",
2525 nop);
2526 reject++;
2529 /* We simulate the behavior of old reload here.
2530 Although scratches need hard registers and it
2531 might result in spilling other pseudos, no reload
2532 insns are generated for the scratches. So it
2533 might cost something but probably less than old
2534 reload pass believes. */
2535 if (scratch_p)
2537 if (lra_dump_file != NULL)
2538 fprintf (lra_dump_file,
2539 " %d Scratch win: reject+=2\n",
2540 nop);
2541 reject += 2;
2545 else if (did_match)
2546 this_alternative_match_win = true;
2547 else
2549 int const_to_mem = 0;
2550 bool no_regs_p;
2552 reject += op_reject;
2553 /* Never do output reload of stack pointer. It makes
2554 impossible to do elimination when SP is changed in
2555 RTL. */
2556 if (op == stack_pointer_rtx && ! frame_pointer_needed
2557 && curr_static_id->operand[nop].type != OP_IN)
2558 goto fail;
2560 /* If this alternative asks for a specific reg class, see if there
2561 is at least one allocatable register in that class. */
2562 no_regs_p
2563 = (this_alternative == NO_REGS
2564 || (hard_reg_set_subset_p
2565 (reg_class_contents[this_alternative],
2566 lra_no_alloc_regs)));
2568 /* For asms, verify that the class for this alternative is possible
2569 for the mode that is specified. */
2570 if (!no_regs_p && INSN_CODE (curr_insn) < 0)
2572 int i;
2573 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2574 if (targetm.hard_regno_mode_ok (i, mode)
2575 && in_hard_reg_set_p (reg_class_contents[this_alternative],
2576 mode, i))
2577 break;
2578 if (i == FIRST_PSEUDO_REGISTER)
2579 winreg = false;
2582 /* If this operand accepts a register, and if the
2583 register class has at least one allocatable register,
2584 then this operand can be reloaded. */
2585 if (winreg && !no_regs_p)
2586 badop = false;
2588 if (badop)
2590 if (lra_dump_file != NULL)
2591 fprintf (lra_dump_file,
2592 " alt=%d: Bad operand -- refuse\n",
2593 nalt);
2594 goto fail;
2597 if (this_alternative != NO_REGS)
2599 HARD_REG_SET available_regs
2600 = (reg_class_contents[this_alternative]
2601 & ~((ira_prohibited_class_mode_regs
2602 [this_alternative][mode])
2603 | lra_no_alloc_regs));
2604 if (hard_reg_set_empty_p (available_regs))
2606 /* There are no hard regs holding a value of given
2607 mode. */
2608 if (offmemok)
2610 this_alternative = NO_REGS;
2611 if (lra_dump_file != NULL)
2612 fprintf (lra_dump_file,
2613 " %d Using memory because of"
2614 " a bad mode: reject+=2\n",
2615 nop);
2616 reject += 2;
2618 else
2620 if (lra_dump_file != NULL)
2621 fprintf (lra_dump_file,
2622 " alt=%d: Wrong mode -- refuse\n",
2623 nalt);
2624 goto fail;
2629 /* If not assigned pseudo has a class which a subset of
2630 required reg class, it is a less costly alternative
2631 as the pseudo still can get a hard reg of necessary
2632 class. */
2633 if (! no_regs_p && REG_P (op) && hard_regno[nop] < 0
2634 && (cl = get_reg_class (REGNO (op))) != NO_REGS
2635 && ira_class_subset_p[this_alternative][cl])
2637 if (lra_dump_file != NULL)
2638 fprintf
2639 (lra_dump_file,
2640 " %d Super set class reg: reject-=3\n", nop);
2641 reject -= 3;
2644 this_alternative_offmemok = offmemok;
2645 if (this_costly_alternative != NO_REGS)
2647 if (lra_dump_file != NULL)
2648 fprintf (lra_dump_file,
2649 " %d Costly loser: reject++\n", nop);
2650 reject++;
2652 /* If the operand is dying, has a matching constraint,
2653 and satisfies constraints of the matched operand
2654 which failed to satisfy the own constraints, most probably
2655 the reload for this operand will be gone. */
2656 if (this_alternative_matches >= 0
2657 && !curr_alt_win[this_alternative_matches]
2658 && REG_P (op)
2659 && find_regno_note (curr_insn, REG_DEAD, REGNO (op))
2660 && (hard_regno[nop] >= 0
2661 ? in_hard_reg_set_p (this_alternative_set,
2662 mode, hard_regno[nop])
2663 : in_class_p (op, this_alternative, NULL)))
2665 if (lra_dump_file != NULL)
2666 fprintf
2667 (lra_dump_file,
2668 " %d Dying matched operand reload: reject++\n",
2669 nop);
2670 reject++;
2672 else
2674 /* Strict_low_part requires to reload the register
2675 not the sub-register. In this case we should
2676 check that a final reload hard reg can hold the
2677 value mode. */
2678 if (curr_static_id->operand[nop].strict_low
2679 && REG_P (op)
2680 && hard_regno[nop] < 0
2681 && GET_CODE (*curr_id->operand_loc[nop]) == SUBREG
2682 && ira_class_hard_regs_num[this_alternative] > 0
2683 && (!targetm.hard_regno_mode_ok
2684 (ira_class_hard_regs[this_alternative][0],
2685 GET_MODE (*curr_id->operand_loc[nop]))))
2687 if (lra_dump_file != NULL)
2688 fprintf
2689 (lra_dump_file,
2690 " alt=%d: Strict low subreg reload -- refuse\n",
2691 nalt);
2692 goto fail;
2694 losers++;
2696 if (operand_reg[nop] != NULL_RTX
2697 /* Output operands and matched input operands are
2698 not inherited. The following conditions do not
2699 exactly describe the previous statement but they
2700 are pretty close. */
2701 && curr_static_id->operand[nop].type != OP_OUT
2702 && (this_alternative_matches < 0
2703 || curr_static_id->operand[nop].type != OP_IN))
2705 int last_reload = (lra_reg_info[ORIGINAL_REGNO
2706 (operand_reg[nop])]
2707 .last_reload);
2709 /* The value of reload_sum has sense only if we
2710 process insns in their order. It happens only on
2711 the first constraints sub-pass when we do most of
2712 reload work. */
2713 if (lra_constraint_iter == 1 && last_reload > bb_reload_num)
2714 reload_sum += last_reload - bb_reload_num;
2716 /* If this is a constant that is reloaded into the
2717 desired class by copying it to memory first, count
2718 that as another reload. This is consistent with
2719 other code and is required to avoid choosing another
2720 alternative when the constant is moved into memory.
2721 Note that the test here is precisely the same as in
2722 the code below that calls force_const_mem. */
2723 if (CONST_POOL_OK_P (mode, op)
2724 && ((targetm.preferred_reload_class
2725 (op, this_alternative) == NO_REGS)
2726 || no_input_reloads_p))
2728 const_to_mem = 1;
2729 if (! no_regs_p)
2730 losers++;
2733 /* Alternative loses if it requires a type of reload not
2734 permitted for this insn. We can always reload
2735 objects with a REG_UNUSED note. */
2736 if ((curr_static_id->operand[nop].type != OP_IN
2737 && no_output_reloads_p
2738 && ! find_reg_note (curr_insn, REG_UNUSED, op))
2739 || (curr_static_id->operand[nop].type != OP_OUT
2740 && no_input_reloads_p && ! const_to_mem)
2741 || (this_alternative_matches >= 0
2742 && (no_input_reloads_p
2743 || (no_output_reloads_p
2744 && (curr_static_id->operand
2745 [this_alternative_matches].type != OP_IN)
2746 && ! find_reg_note (curr_insn, REG_UNUSED,
2747 no_subreg_reg_operand
2748 [this_alternative_matches])))))
2750 if (lra_dump_file != NULL)
2751 fprintf
2752 (lra_dump_file,
2753 " alt=%d: No input/otput reload -- refuse\n",
2754 nalt);
2755 goto fail;
2758 /* Alternative loses if it required class pseudo cannot
2759 hold value of required mode. Such insns can be
2760 described by insn definitions with mode iterators. */
2761 if (GET_MODE (*curr_id->operand_loc[nop]) != VOIDmode
2762 && ! hard_reg_set_empty_p (this_alternative_set)
2763 /* It is common practice for constraints to use a
2764 class which does not have actually enough regs to
2765 hold the value (e.g. x86 AREG for mode requiring
2766 more one general reg). Therefore we have 2
2767 conditions to check that the reload pseudo cannot
2768 hold the mode value. */
2769 && (!targetm.hard_regno_mode_ok
2770 (ira_class_hard_regs[this_alternative][0],
2771 GET_MODE (*curr_id->operand_loc[nop])))
2772 /* The above condition is not enough as the first
2773 reg in ira_class_hard_regs can be not aligned for
2774 multi-words mode values. */
2775 && (prohibited_class_reg_set_mode_p
2776 (this_alternative, this_alternative_set,
2777 GET_MODE (*curr_id->operand_loc[nop]))))
2779 if (lra_dump_file != NULL)
2780 fprintf (lra_dump_file,
2781 " alt=%d: reload pseudo for op %d "
2782 "cannot hold the mode value -- refuse\n",
2783 nalt, nop);
2784 goto fail;
2787 /* Check strong discouragement of reload of non-constant
2788 into class THIS_ALTERNATIVE. */
2789 if (! CONSTANT_P (op) && ! no_regs_p
2790 && (targetm.preferred_reload_class
2791 (op, this_alternative) == NO_REGS
2792 || (curr_static_id->operand[nop].type == OP_OUT
2793 && (targetm.preferred_output_reload_class
2794 (op, this_alternative) == NO_REGS))))
2796 if (offmemok && REG_P (op))
2798 if (lra_dump_file != NULL)
2799 fprintf
2800 (lra_dump_file,
2801 " %d Spill pseudo into memory: reject+=3\n",
2802 nop);
2803 reject += 3;
2805 else
2807 if (lra_dump_file != NULL)
2808 fprintf
2809 (lra_dump_file,
2810 " %d Non-prefered reload: reject+=%d\n",
2811 nop, LRA_MAX_REJECT);
2812 reject += LRA_MAX_REJECT;
2816 if (! (MEM_P (op) && offmemok)
2817 && ! (const_to_mem && constmemok))
2819 /* We prefer to reload pseudos over reloading other
2820 things, since such reloads may be able to be
2821 eliminated later. So bump REJECT in other cases.
2822 Don't do this in the case where we are forcing a
2823 constant into memory and it will then win since
2824 we don't want to have a different alternative
2825 match then. */
2826 if (! (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER))
2828 if (lra_dump_file != NULL)
2829 fprintf
2830 (lra_dump_file,
2831 " %d Non-pseudo reload: reject+=2\n",
2832 nop);
2833 reject += 2;
2836 if (! no_regs_p)
2837 reload_nregs
2838 += ira_reg_class_max_nregs[this_alternative][mode];
2840 if (SMALL_REGISTER_CLASS_P (this_alternative))
2842 if (lra_dump_file != NULL)
2843 fprintf
2844 (lra_dump_file,
2845 " %d Small class reload: reject+=%d\n",
2846 nop, LRA_LOSER_COST_FACTOR / 2);
2847 reject += LRA_LOSER_COST_FACTOR / 2;
2851 /* We are trying to spill pseudo into memory. It is
2852 usually more costly than moving to a hard register
2853 although it might takes the same number of
2854 reloads.
2856 Non-pseudo spill may happen also. Suppose a target allows both
2857 register and memory in the operand constraint alternatives,
2858 then it's typical that an eliminable register has a substition
2859 of "base + offset" which can either be reloaded by a simple
2860 "new_reg <= base + offset" which will match the register
2861 constraint, or a similar reg addition followed by further spill
2862 to and reload from memory which will match the memory
2863 constraint, but this memory spill will be much more costly
2864 usually.
2866 Code below increases the reject for both pseudo and non-pseudo
2867 spill. */
2868 if (no_regs_p
2869 && !(MEM_P (op) && offmemok)
2870 && !(REG_P (op) && hard_regno[nop] < 0))
2872 if (lra_dump_file != NULL)
2873 fprintf
2874 (lra_dump_file,
2875 " %d Spill %spseudo into memory: reject+=3\n",
2876 nop, REG_P (op) ? "" : "Non-");
2877 reject += 3;
2878 if (VECTOR_MODE_P (mode))
2880 /* Spilling vectors into memory is usually more
2881 costly as they contain big values. */
2882 if (lra_dump_file != NULL)
2883 fprintf
2884 (lra_dump_file,
2885 " %d Spill vector pseudo: reject+=2\n",
2886 nop);
2887 reject += 2;
2891 /* When we use an operand requiring memory in given
2892 alternative, the insn should write *and* read the
2893 value to/from memory it is costly in comparison with
2894 an insn alternative which does not use memory
2895 (e.g. register or immediate operand). We exclude
2896 memory operand for such case as we can satisfy the
2897 memory constraints by reloading address. */
2898 if (no_regs_p && offmemok && !MEM_P (op))
2900 if (lra_dump_file != NULL)
2901 fprintf
2902 (lra_dump_file,
2903 " Using memory insn operand %d: reject+=3\n",
2904 nop);
2905 reject += 3;
2908 /* If reload requires moving value through secondary
2909 memory, it will need one more insn at least. */
2910 if (this_alternative != NO_REGS
2911 && REG_P (op) && (cl = get_reg_class (REGNO (op))) != NO_REGS
2912 && ((curr_static_id->operand[nop].type != OP_OUT
2913 && targetm.secondary_memory_needed (GET_MODE (op), cl,
2914 this_alternative))
2915 || (curr_static_id->operand[nop].type != OP_IN
2916 && (targetm.secondary_memory_needed
2917 (GET_MODE (op), this_alternative, cl)))))
2918 losers++;
2920 if (MEM_P (op) && offmemok)
2921 addr_losers++;
2922 else
2924 /* Input reloads can be inherited more often than
2925 output reloads can be removed, so penalize output
2926 reloads. */
2927 if (!REG_P (op) || curr_static_id->operand[nop].type != OP_IN)
2929 if (lra_dump_file != NULL)
2930 fprintf
2931 (lra_dump_file,
2932 " %d Non input pseudo reload: reject++\n",
2933 nop);
2934 reject++;
2937 if (curr_static_id->operand[nop].type == OP_INOUT)
2939 if (lra_dump_file != NULL)
2940 fprintf
2941 (lra_dump_file,
2942 " %d Input/Output reload: reject+=%d\n",
2943 nop, LRA_LOSER_COST_FACTOR);
2944 reject += LRA_LOSER_COST_FACTOR;
2949 if (early_clobber_p && ! scratch_p)
2951 if (lra_dump_file != NULL)
2952 fprintf (lra_dump_file,
2953 " %d Early clobber: reject++\n", nop);
2954 reject++;
2956 /* ??? We check early clobbers after processing all operands
2957 (see loop below) and there we update the costs more.
2958 Should we update the cost (may be approximately) here
2959 because of early clobber register reloads or it is a rare
2960 or non-important thing to be worth to do it. */
2961 overall = (losers * LRA_LOSER_COST_FACTOR + reject
2962 - (addr_losers == losers ? static_reject : 0));
2963 if ((best_losers == 0 || losers != 0) && best_overall < overall)
2965 if (lra_dump_file != NULL)
2966 fprintf (lra_dump_file,
2967 " alt=%d,overall=%d,losers=%d -- refuse\n",
2968 nalt, overall, losers);
2969 goto fail;
2972 if (update_and_check_small_class_inputs (nop, nalt,
2973 this_alternative))
2975 if (lra_dump_file != NULL)
2976 fprintf (lra_dump_file,
2977 " alt=%d, not enough small class regs -- refuse\n",
2978 nalt);
2979 goto fail;
2981 curr_alt[nop] = this_alternative;
2982 curr_alt_set[nop] = this_alternative_set;
2983 curr_alt_win[nop] = this_alternative_win;
2984 curr_alt_match_win[nop] = this_alternative_match_win;
2985 curr_alt_offmemok[nop] = this_alternative_offmemok;
2986 curr_alt_matches[nop] = this_alternative_matches;
2988 if (this_alternative_matches >= 0
2989 && !did_match && !this_alternative_win)
2990 curr_alt_win[this_alternative_matches] = false;
2992 if (early_clobber_p && operand_reg[nop] != NULL_RTX)
2993 early_clobbered_nops[early_clobbered_regs_num++] = nop;
2996 if (curr_insn_set != NULL_RTX && n_operands == 2
2997 /* Prevent processing non-move insns. */
2998 && (GET_CODE (SET_SRC (curr_insn_set)) == SUBREG
2999 || SET_SRC (curr_insn_set) == no_subreg_reg_operand[1])
3000 && ((! curr_alt_win[0] && ! curr_alt_win[1]
3001 && REG_P (no_subreg_reg_operand[0])
3002 && REG_P (no_subreg_reg_operand[1])
3003 && (reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
3004 || reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0])))
3005 || (! curr_alt_win[0] && curr_alt_win[1]
3006 && REG_P (no_subreg_reg_operand[1])
3007 /* Check that we reload memory not the memory
3008 address. */
3009 && ! (curr_alt_offmemok[0]
3010 && MEM_P (no_subreg_reg_operand[0]))
3011 && reg_in_class_p (no_subreg_reg_operand[1], curr_alt[0]))
3012 || (curr_alt_win[0] && ! curr_alt_win[1]
3013 && REG_P (no_subreg_reg_operand[0])
3014 /* Check that we reload memory not the memory
3015 address. */
3016 && ! (curr_alt_offmemok[1]
3017 && MEM_P (no_subreg_reg_operand[1]))
3018 && reg_in_class_p (no_subreg_reg_operand[0], curr_alt[1])
3019 && (! CONST_POOL_OK_P (curr_operand_mode[1],
3020 no_subreg_reg_operand[1])
3021 || (targetm.preferred_reload_class
3022 (no_subreg_reg_operand[1],
3023 (enum reg_class) curr_alt[1]) != NO_REGS))
3024 /* If it is a result of recent elimination in move
3025 insn we can transform it into an add still by
3026 using this alternative. */
3027 && GET_CODE (no_subreg_reg_operand[1]) != PLUS
3028 /* Likewise if the source has been replaced with an
3029 equivalent value. This only happens once -- the reload
3030 will use the equivalent value instead of the register it
3031 replaces -- so there should be no danger of cycling. */
3032 && !equiv_substition_p[1])))
3034 /* We have a move insn and a new reload insn will be similar
3035 to the current insn. We should avoid such situation as
3036 it results in LRA cycling. */
3037 if (lra_dump_file != NULL)
3038 fprintf (lra_dump_file,
3039 " Cycle danger: overall += LRA_MAX_REJECT\n");
3040 overall += LRA_MAX_REJECT;
3042 ok_p = true;
3043 curr_alt_dont_inherit_ops_num = 0;
3044 for (nop = 0; nop < early_clobbered_regs_num; nop++)
3046 int i, j, clobbered_hard_regno, first_conflict_j, last_conflict_j;
3047 HARD_REG_SET temp_set;
3049 i = early_clobbered_nops[nop];
3050 if ((! curr_alt_win[i] && ! curr_alt_match_win[i])
3051 || hard_regno[i] < 0)
3052 continue;
3053 lra_assert (operand_reg[i] != NULL_RTX);
3054 clobbered_hard_regno = hard_regno[i];
3055 CLEAR_HARD_REG_SET (temp_set);
3056 add_to_hard_reg_set (&temp_set, biggest_mode[i], clobbered_hard_regno);
3057 first_conflict_j = last_conflict_j = -1;
3058 for (j = 0; j < n_operands; j++)
3059 if (j == i
3060 /* We don't want process insides of match_operator and
3061 match_parallel because otherwise we would process
3062 their operands once again generating a wrong
3063 code. */
3064 || curr_static_id->operand[j].is_operator)
3065 continue;
3066 else if ((curr_alt_matches[j] == i && curr_alt_match_win[j])
3067 || (curr_alt_matches[i] == j && curr_alt_match_win[i]))
3068 continue;
3069 /* If we don't reload j-th operand, check conflicts. */
3070 else if ((curr_alt_win[j] || curr_alt_match_win[j])
3071 && uses_hard_regs_p (*curr_id->operand_loc[j], temp_set))
3073 if (first_conflict_j < 0)
3074 first_conflict_j = j;
3075 last_conflict_j = j;
3076 /* Both the earlyclobber operand and conflicting operand
3077 cannot both be user defined hard registers. */
3078 if (HARD_REGISTER_P (operand_reg[i])
3079 && REG_USERVAR_P (operand_reg[i])
3080 && operand_reg[j] != NULL_RTX
3081 && HARD_REGISTER_P (operand_reg[j])
3082 && REG_USERVAR_P (operand_reg[j]))
3083 fatal_insn ("unable to generate reloads for "
3084 "impossible constraints:", curr_insn);
3086 if (last_conflict_j < 0)
3087 continue;
3089 /* If an earlyclobber operand conflicts with another non-matching
3090 operand (ie, they have been assigned the same hard register),
3091 then it is better to reload the other operand, as there may
3092 exist yet another operand with a matching constraint associated
3093 with the earlyclobber operand. However, if one of the operands
3094 is an explicit use of a hard register, then we must reload the
3095 other non-hard register operand. */
3096 if (HARD_REGISTER_P (operand_reg[i])
3097 || (first_conflict_j == last_conflict_j
3098 && operand_reg[last_conflict_j] != NULL_RTX
3099 && !curr_alt_match_win[last_conflict_j]
3100 && !HARD_REGISTER_P (operand_reg[last_conflict_j])))
3102 curr_alt_win[last_conflict_j] = false;
3103 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++]
3104 = last_conflict_j;
3105 losers++;
3106 if (lra_dump_file != NULL)
3107 fprintf
3108 (lra_dump_file,
3109 " %d Conflict early clobber reload: reject--\n",
3112 else
3114 /* We need to reload early clobbered register and the
3115 matched registers. */
3116 for (j = 0; j < n_operands; j++)
3117 if (curr_alt_matches[j] == i)
3119 curr_alt_match_win[j] = false;
3120 losers++;
3121 overall += LRA_LOSER_COST_FACTOR;
3123 if (! curr_alt_match_win[i])
3124 curr_alt_dont_inherit_ops[curr_alt_dont_inherit_ops_num++] = i;
3125 else
3127 /* Remember pseudos used for match reloads are never
3128 inherited. */
3129 lra_assert (curr_alt_matches[i] >= 0);
3130 curr_alt_win[curr_alt_matches[i]] = false;
3132 curr_alt_win[i] = curr_alt_match_win[i] = false;
3133 losers++;
3134 if (lra_dump_file != NULL)
3135 fprintf
3136 (lra_dump_file,
3137 " %d Matched conflict early clobber reloads: "
3138 "reject--\n",
3141 /* Early clobber was already reflected in REJECT. */
3142 if (!matching_early_clobber[i])
3144 lra_assert (reject > 0);
3145 reject--;
3146 matching_early_clobber[i] = 1;
3148 overall += LRA_LOSER_COST_FACTOR - 1;
3150 if (lra_dump_file != NULL)
3151 fprintf (lra_dump_file, " alt=%d,overall=%d,losers=%d,rld_nregs=%d\n",
3152 nalt, overall, losers, reload_nregs);
3154 /* If this alternative can be made to work by reloading, and it
3155 needs less reloading than the others checked so far, record
3156 it as the chosen goal for reloading. */
3157 if ((best_losers != 0 && losers == 0)
3158 || (((best_losers == 0 && losers == 0)
3159 || (best_losers != 0 && losers != 0))
3160 && (best_overall > overall
3161 || (best_overall == overall
3162 /* If the cost of the reloads is the same,
3163 prefer alternative which requires minimal
3164 number of reload regs. */
3165 && (reload_nregs < best_reload_nregs
3166 || (reload_nregs == best_reload_nregs
3167 && (best_reload_sum < reload_sum
3168 || (best_reload_sum == reload_sum
3169 && nalt < goal_alt_number))))))))
3171 for (nop = 0; nop < n_operands; nop++)
3173 goal_alt_win[nop] = curr_alt_win[nop];
3174 goal_alt_match_win[nop] = curr_alt_match_win[nop];
3175 goal_alt_matches[nop] = curr_alt_matches[nop];
3176 goal_alt[nop] = curr_alt[nop];
3177 goal_alt_offmemok[nop] = curr_alt_offmemok[nop];
3179 goal_alt_dont_inherit_ops_num = curr_alt_dont_inherit_ops_num;
3180 for (nop = 0; nop < curr_alt_dont_inherit_ops_num; nop++)
3181 goal_alt_dont_inherit_ops[nop] = curr_alt_dont_inherit_ops[nop];
3182 goal_alt_swapped = curr_swapped;
3183 best_overall = overall;
3184 best_losers = losers;
3185 best_reload_nregs = reload_nregs;
3186 best_reload_sum = reload_sum;
3187 goal_alt_number = nalt;
3189 if (losers == 0)
3190 /* Everything is satisfied. Do not process alternatives
3191 anymore. */
3192 break;
3193 fail:
3196 return ok_p;
3199 /* Make reload base reg from address AD. */
3200 static rtx
3201 base_to_reg (struct address_info *ad)
3203 enum reg_class cl;
3204 int code = -1;
3205 rtx new_inner = NULL_RTX;
3206 rtx new_reg = NULL_RTX;
3207 rtx_insn *insn;
3208 rtx_insn *last_insn = get_last_insn();
3210 lra_assert (ad->disp == ad->disp_term);
3211 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
3212 get_index_code (ad));
3213 new_reg = lra_create_new_reg (GET_MODE (*ad->base), NULL_RTX,
3214 cl, "base");
3215 new_inner = simplify_gen_binary (PLUS, GET_MODE (new_reg), new_reg,
3216 ad->disp_term == NULL
3217 ? const0_rtx
3218 : *ad->disp_term);
3219 if (!valid_address_p (ad->mode, new_inner, ad->as))
3220 return NULL_RTX;
3221 insn = emit_insn (gen_rtx_SET (new_reg, *ad->base));
3222 code = recog_memoized (insn);
3223 if (code < 0)
3225 delete_insns_since (last_insn);
3226 return NULL_RTX;
3229 return new_inner;
3232 /* Make reload base reg + DISP from address AD. Return the new pseudo. */
3233 static rtx
3234 base_plus_disp_to_reg (struct address_info *ad, rtx disp)
3236 enum reg_class cl;
3237 rtx new_reg;
3239 lra_assert (ad->base == ad->base_term);
3240 cl = base_reg_class (ad->mode, ad->as, ad->base_outer_code,
3241 get_index_code (ad));
3242 new_reg = lra_create_new_reg (GET_MODE (*ad->base_term), NULL_RTX,
3243 cl, "base + disp");
3244 lra_emit_add (new_reg, *ad->base_term, disp);
3245 return new_reg;
3248 /* Make reload of index part of address AD. Return the new
3249 pseudo. */
3250 static rtx
3251 index_part_to_reg (struct address_info *ad)
3253 rtx new_reg;
3255 new_reg = lra_create_new_reg (GET_MODE (*ad->index), NULL_RTX,
3256 INDEX_REG_CLASS, "index term");
3257 expand_mult (GET_MODE (*ad->index), *ad->index_term,
3258 GEN_INT (get_index_scale (ad)), new_reg, 1);
3259 return new_reg;
3262 /* Return true if we can add a displacement to address AD, even if that
3263 makes the address invalid. The fix-up code requires any new address
3264 to be the sum of the BASE_TERM, INDEX and DISP_TERM fields. */
3265 static bool
3266 can_add_disp_p (struct address_info *ad)
3268 return (!ad->autoinc_p
3269 && ad->segment == NULL
3270 && ad->base == ad->base_term
3271 && ad->disp == ad->disp_term);
3274 /* Make equiv substitution in address AD. Return true if a substitution
3275 was made. */
3276 static bool
3277 equiv_address_substitution (struct address_info *ad)
3279 rtx base_reg, new_base_reg, index_reg, new_index_reg, *base_term, *index_term;
3280 poly_int64 disp;
3281 HOST_WIDE_INT scale;
3282 bool change_p;
3284 base_term = strip_subreg (ad->base_term);
3285 if (base_term == NULL)
3286 base_reg = new_base_reg = NULL_RTX;
3287 else
3289 base_reg = *base_term;
3290 new_base_reg = get_equiv_with_elimination (base_reg, curr_insn);
3292 index_term = strip_subreg (ad->index_term);
3293 if (index_term == NULL)
3294 index_reg = new_index_reg = NULL_RTX;
3295 else
3297 index_reg = *index_term;
3298 new_index_reg = get_equiv_with_elimination (index_reg, curr_insn);
3300 if (base_reg == new_base_reg && index_reg == new_index_reg)
3301 return false;
3302 disp = 0;
3303 change_p = false;
3304 if (lra_dump_file != NULL)
3306 fprintf (lra_dump_file, "Changing address in insn %d ",
3307 INSN_UID (curr_insn));
3308 dump_value_slim (lra_dump_file, *ad->outer, 1);
3310 if (base_reg != new_base_reg)
3312 poly_int64 offset;
3313 if (REG_P (new_base_reg))
3315 *base_term = new_base_reg;
3316 change_p = true;
3318 else if (GET_CODE (new_base_reg) == PLUS
3319 && REG_P (XEXP (new_base_reg, 0))
3320 && poly_int_rtx_p (XEXP (new_base_reg, 1), &offset)
3321 && can_add_disp_p (ad))
3323 disp += offset;
3324 *base_term = XEXP (new_base_reg, 0);
3325 change_p = true;
3327 if (ad->base_term2 != NULL)
3328 *ad->base_term2 = *ad->base_term;
3330 if (index_reg != new_index_reg)
3332 poly_int64 offset;
3333 if (REG_P (new_index_reg))
3335 *index_term = new_index_reg;
3336 change_p = true;
3338 else if (GET_CODE (new_index_reg) == PLUS
3339 && REG_P (XEXP (new_index_reg, 0))
3340 && poly_int_rtx_p (XEXP (new_index_reg, 1), &offset)
3341 && can_add_disp_p (ad)
3342 && (scale = get_index_scale (ad)))
3344 disp += offset * scale;
3345 *index_term = XEXP (new_index_reg, 0);
3346 change_p = true;
3349 if (maybe_ne (disp, 0))
3351 if (ad->disp != NULL)
3352 *ad->disp = plus_constant (GET_MODE (*ad->inner), *ad->disp, disp);
3353 else
3355 *ad->inner = plus_constant (GET_MODE (*ad->inner), *ad->inner, disp);
3356 update_address (ad);
3358 change_p = true;
3360 if (lra_dump_file != NULL)
3362 if (! change_p)
3363 fprintf (lra_dump_file, " -- no change\n");
3364 else
3366 fprintf (lra_dump_file, " on equiv ");
3367 dump_value_slim (lra_dump_file, *ad->outer, 1);
3368 fprintf (lra_dump_file, "\n");
3371 return change_p;
3374 /* Major function to make reloads for an address in operand NOP or
3375 check its correctness (If CHECK_ONLY_P is true). The supported
3376 cases are:
3378 1) an address that existed before LRA started, at which point it
3379 must have been valid. These addresses are subject to elimination
3380 and may have become invalid due to the elimination offset being out
3381 of range.
3383 2) an address created by forcing a constant to memory
3384 (force_const_to_mem). The initial form of these addresses might
3385 not be valid, and it is this function's job to make them valid.
3387 3) a frame address formed from a register and a (possibly zero)
3388 constant offset. As above, these addresses might not be valid and
3389 this function must make them so.
3391 Add reloads to the lists *BEFORE and *AFTER. We might need to add
3392 reloads to *AFTER because of inc/dec, {pre, post} modify in the
3393 address. Return true for any RTL change.
3395 The function is a helper function which does not produce all
3396 transformations (when CHECK_ONLY_P is false) which can be
3397 necessary. It does just basic steps. To do all necessary
3398 transformations use function process_address. */
3399 static bool
3400 process_address_1 (int nop, bool check_only_p,
3401 rtx_insn **before, rtx_insn **after)
3403 struct address_info ad;
3404 rtx new_reg;
3405 HOST_WIDE_INT scale;
3406 rtx op = *curr_id->operand_loc[nop];
3407 const char *constraint = curr_static_id->operand[nop].constraint;
3408 enum constraint_num cn = lookup_constraint (constraint);
3409 bool change_p = false;
3411 if (MEM_P (op)
3412 && GET_MODE (op) == BLKmode
3413 && GET_CODE (XEXP (op, 0)) == SCRATCH)
3414 return false;
3416 if (insn_extra_address_constraint (cn)
3417 /* When we find an asm operand with an address constraint that
3418 doesn't satisfy address_operand to begin with, we clear
3419 is_address, so that we don't try to make a non-address fit.
3420 If the asm statement got this far, it's because other
3421 constraints are available, and we'll use them, disregarding
3422 the unsatisfiable address ones. */
3423 && curr_static_id->operand[nop].is_address)
3424 decompose_lea_address (&ad, curr_id->operand_loc[nop]);
3425 /* Do not attempt to decompose arbitrary addresses generated by combine
3426 for asm operands with loose constraints, e.g 'X'. */
3427 else if (MEM_P (op)
3428 && !(INSN_CODE (curr_insn) < 0
3429 && get_constraint_type (cn) == CT_FIXED_FORM
3430 && constraint_satisfied_p (op, cn)))
3431 decompose_mem_address (&ad, op);
3432 else if (GET_CODE (op) == SUBREG
3433 && MEM_P (SUBREG_REG (op)))
3434 decompose_mem_address (&ad, SUBREG_REG (op));
3435 else
3436 return false;
3437 /* If INDEX_REG_CLASS is assigned to base_term already and isn't to
3438 index_term, swap them so to avoid assigning INDEX_REG_CLASS to both
3439 when INDEX_REG_CLASS is a single register class. */
3440 if (ad.base_term != NULL
3441 && ad.index_term != NULL
3442 && ira_class_hard_regs_num[INDEX_REG_CLASS] == 1
3443 && REG_P (*ad.base_term)
3444 && REG_P (*ad.index_term)
3445 && in_class_p (*ad.base_term, INDEX_REG_CLASS, NULL)
3446 && ! in_class_p (*ad.index_term, INDEX_REG_CLASS, NULL))
3448 std::swap (ad.base, ad.index);
3449 std::swap (ad.base_term, ad.index_term);
3451 if (! check_only_p)
3452 change_p = equiv_address_substitution (&ad);
3453 if (ad.base_term != NULL
3454 && (process_addr_reg
3455 (ad.base_term, check_only_p, before,
3456 (ad.autoinc_p
3457 && !(REG_P (*ad.base_term)
3458 && find_regno_note (curr_insn, REG_DEAD,
3459 REGNO (*ad.base_term)) != NULL_RTX)
3460 ? after : NULL),
3461 base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3462 get_index_code (&ad)))))
3464 change_p = true;
3465 if (ad.base_term2 != NULL)
3466 *ad.base_term2 = *ad.base_term;
3468 if (ad.index_term != NULL
3469 && process_addr_reg (ad.index_term, check_only_p,
3470 before, NULL, INDEX_REG_CLASS))
3471 change_p = true;
3473 /* Target hooks sometimes don't treat extra-constraint addresses as
3474 legitimate address_operands, so handle them specially. */
3475 if (insn_extra_address_constraint (cn)
3476 && satisfies_address_constraint_p (&ad, cn))
3477 return change_p;
3479 if (check_only_p)
3480 return change_p;
3482 /* There are three cases where the shape of *AD.INNER may now be invalid:
3484 1) the original address was valid, but either elimination or
3485 equiv_address_substitution was applied and that made
3486 the address invalid.
3488 2) the address is an invalid symbolic address created by
3489 force_const_to_mem.
3491 3) the address is a frame address with an invalid offset.
3493 4) the address is a frame address with an invalid base.
3495 All these cases involve a non-autoinc address, so there is no
3496 point revalidating other types. */
3497 if (ad.autoinc_p || valid_address_p (op, &ad, cn))
3498 return change_p;
3500 /* Any index existed before LRA started, so we can assume that the
3501 presence and shape of the index is valid. */
3502 push_to_sequence (*before);
3503 lra_assert (ad.disp == ad.disp_term);
3504 if (ad.base == NULL)
3506 if (ad.index == NULL)
3508 rtx_insn *insn;
3509 rtx_insn *last = get_last_insn ();
3510 int code = -1;
3511 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3512 SCRATCH, SCRATCH);
3513 rtx addr = *ad.inner;
3515 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
3516 if (HAVE_lo_sum)
3518 /* addr => lo_sum (new_base, addr), case (2) above. */
3519 insn = emit_insn (gen_rtx_SET
3520 (new_reg,
3521 gen_rtx_HIGH (Pmode, copy_rtx (addr))));
3522 code = recog_memoized (insn);
3523 if (code >= 0)
3525 *ad.inner = gen_rtx_LO_SUM (Pmode, new_reg, addr);
3526 if (!valid_address_p (op, &ad, cn))
3528 /* Try to put lo_sum into register. */
3529 insn = emit_insn (gen_rtx_SET
3530 (new_reg,
3531 gen_rtx_LO_SUM (Pmode, new_reg, addr)));
3532 code = recog_memoized (insn);
3533 if (code >= 0)
3535 *ad.inner = new_reg;
3536 if (!valid_address_p (op, &ad, cn))
3538 *ad.inner = addr;
3539 code = -1;
3545 if (code < 0)
3546 delete_insns_since (last);
3549 if (code < 0)
3551 /* addr => new_base, case (2) above. */
3552 lra_emit_move (new_reg, addr);
3554 for (insn = last == NULL_RTX ? get_insns () : NEXT_INSN (last);
3555 insn != NULL_RTX;
3556 insn = NEXT_INSN (insn))
3557 if (recog_memoized (insn) < 0)
3558 break;
3559 if (insn != NULL_RTX)
3561 /* Do nothing if we cannot generate right insns.
3562 This is analogous to reload pass behavior. */
3563 delete_insns_since (last);
3564 end_sequence ();
3565 return false;
3567 *ad.inner = new_reg;
3570 else
3572 /* index * scale + disp => new base + index * scale,
3573 case (1) above. */
3574 enum reg_class cl = base_reg_class (ad.mode, ad.as, PLUS,
3575 GET_CODE (*ad.index));
3577 lra_assert (INDEX_REG_CLASS != NO_REGS);
3578 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "disp");
3579 lra_emit_move (new_reg, *ad.disp);
3580 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3581 new_reg, *ad.index);
3584 else if (ad.index == NULL)
3586 int regno;
3587 enum reg_class cl;
3588 rtx set;
3589 rtx_insn *insns, *last_insn;
3590 /* Try to reload base into register only if the base is invalid
3591 for the address but with valid offset, case (4) above. */
3592 start_sequence ();
3593 new_reg = base_to_reg (&ad);
3595 /* base + disp => new base, cases (1) and (3) above. */
3596 /* Another option would be to reload the displacement into an
3597 index register. However, postreload has code to optimize
3598 address reloads that have the same base and different
3599 displacements, so reloading into an index register would
3600 not necessarily be a win. */
3601 if (new_reg == NULL_RTX)
3603 /* See if the target can split the displacement into a
3604 legitimate new displacement from a local anchor. */
3605 gcc_assert (ad.disp == ad.disp_term);
3606 poly_int64 orig_offset;
3607 rtx offset1, offset2;
3608 if (poly_int_rtx_p (*ad.disp, &orig_offset)
3609 && targetm.legitimize_address_displacement (&offset1, &offset2,
3610 orig_offset,
3611 ad.mode))
3613 new_reg = base_plus_disp_to_reg (&ad, offset1);
3614 new_reg = gen_rtx_PLUS (GET_MODE (new_reg), new_reg, offset2);
3616 else
3617 new_reg = base_plus_disp_to_reg (&ad, *ad.disp);
3619 insns = get_insns ();
3620 last_insn = get_last_insn ();
3621 /* If we generated at least two insns, try last insn source as
3622 an address. If we succeed, we generate one less insn. */
3623 if (REG_P (new_reg)
3624 && last_insn != insns
3625 && (set = single_set (last_insn)) != NULL_RTX
3626 && GET_CODE (SET_SRC (set)) == PLUS
3627 && REG_P (XEXP (SET_SRC (set), 0))
3628 && CONSTANT_P (XEXP (SET_SRC (set), 1)))
3630 *ad.inner = SET_SRC (set);
3631 if (valid_address_p (op, &ad, cn))
3633 *ad.base_term = XEXP (SET_SRC (set), 0);
3634 *ad.disp_term = XEXP (SET_SRC (set), 1);
3635 cl = base_reg_class (ad.mode, ad.as, ad.base_outer_code,
3636 get_index_code (&ad));
3637 regno = REGNO (*ad.base_term);
3638 if (regno >= FIRST_PSEUDO_REGISTER
3639 && cl != lra_get_allocno_class (regno))
3640 lra_change_class (regno, cl, " Change to", true);
3641 new_reg = SET_SRC (set);
3642 delete_insns_since (PREV_INSN (last_insn));
3645 end_sequence ();
3646 emit_insn (insns);
3647 *ad.inner = new_reg;
3649 else if (ad.disp_term != NULL)
3651 /* base + scale * index + disp => new base + scale * index,
3652 case (1) above. */
3653 gcc_assert (ad.disp == ad.disp_term);
3654 new_reg = base_plus_disp_to_reg (&ad, *ad.disp);
3655 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3656 new_reg, *ad.index);
3658 else if ((scale = get_index_scale (&ad)) == 1)
3660 /* The last transformation to one reg will be made in
3661 curr_insn_transform function. */
3662 end_sequence ();
3663 return false;
3665 else if (scale != 0)
3667 /* base + scale * index => base + new_reg,
3668 case (1) above.
3669 Index part of address may become invalid. For example, we
3670 changed pseudo on the equivalent memory and a subreg of the
3671 pseudo onto the memory of different mode for which the scale is
3672 prohibitted. */
3673 new_reg = index_part_to_reg (&ad);
3674 *ad.inner = simplify_gen_binary (PLUS, GET_MODE (new_reg),
3675 *ad.base_term, new_reg);
3677 else
3679 enum reg_class cl = base_reg_class (ad.mode, ad.as,
3680 SCRATCH, SCRATCH);
3681 rtx addr = *ad.inner;
3683 new_reg = lra_create_new_reg (Pmode, NULL_RTX, cl, "addr");
3684 /* addr => new_base. */
3685 lra_emit_move (new_reg, addr);
3686 *ad.inner = new_reg;
3688 *before = get_insns ();
3689 end_sequence ();
3690 return true;
3693 /* If CHECK_ONLY_P is false, do address reloads until it is necessary.
3694 Use process_address_1 as a helper function. Return true for any
3695 RTL changes.
3697 If CHECK_ONLY_P is true, just check address correctness. Return
3698 false if the address correct. */
3699 static bool
3700 process_address (int nop, bool check_only_p,
3701 rtx_insn **before, rtx_insn **after)
3703 bool res = false;
3705 while (process_address_1 (nop, check_only_p, before, after))
3707 if (check_only_p)
3708 return true;
3709 res = true;
3711 return res;
3714 /* Emit insns to reload VALUE into a new register. VALUE is an
3715 auto-increment or auto-decrement RTX whose operand is a register or
3716 memory location; so reloading involves incrementing that location.
3717 IN is either identical to VALUE, or some cheaper place to reload
3718 value being incremented/decremented from.
3720 INC_AMOUNT is the number to increment or decrement by (always
3721 positive and ignored for POST_MODIFY/PRE_MODIFY).
3723 Return pseudo containing the result. */
3724 static rtx
3725 emit_inc (enum reg_class new_rclass, rtx in, rtx value, poly_int64 inc_amount)
3727 /* REG or MEM to be copied and incremented. */
3728 rtx incloc = XEXP (value, 0);
3729 /* Nonzero if increment after copying. */
3730 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
3731 || GET_CODE (value) == POST_MODIFY);
3732 rtx_insn *last;
3733 rtx inc;
3734 rtx_insn *add_insn;
3735 int code;
3736 rtx real_in = in == value ? incloc : in;
3737 rtx result;
3738 bool plus_p = true;
3740 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
3742 lra_assert (GET_CODE (XEXP (value, 1)) == PLUS
3743 || GET_CODE (XEXP (value, 1)) == MINUS);
3744 lra_assert (rtx_equal_p (XEXP (XEXP (value, 1), 0), XEXP (value, 0)));
3745 plus_p = GET_CODE (XEXP (value, 1)) == PLUS;
3746 inc = XEXP (XEXP (value, 1), 1);
3748 else
3750 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
3751 inc_amount = -inc_amount;
3753 inc = gen_int_mode (inc_amount, GET_MODE (value));
3756 if (! post && REG_P (incloc))
3757 result = incloc;
3758 else
3759 result = lra_create_new_reg (GET_MODE (value), value, new_rclass,
3760 "INC/DEC result");
3762 if (real_in != result)
3764 /* First copy the location to the result register. */
3765 lra_assert (REG_P (result));
3766 emit_insn (gen_move_insn (result, real_in));
3769 /* We suppose that there are insns to add/sub with the constant
3770 increment permitted in {PRE/POST)_{DEC/INC/MODIFY}. At least the
3771 old reload worked with this assumption. If the assumption
3772 becomes wrong, we should use approach in function
3773 base_plus_disp_to_reg. */
3774 if (in == value)
3776 /* See if we can directly increment INCLOC. */
3777 last = get_last_insn ();
3778 add_insn = emit_insn (plus_p
3779 ? gen_add2_insn (incloc, inc)
3780 : gen_sub2_insn (incloc, inc));
3782 code = recog_memoized (add_insn);
3783 if (code >= 0)
3785 if (! post && result != incloc)
3786 emit_insn (gen_move_insn (result, incloc));
3787 return result;
3789 delete_insns_since (last);
3792 /* If couldn't do the increment directly, must increment in RESULT.
3793 The way we do this depends on whether this is pre- or
3794 post-increment. For pre-increment, copy INCLOC to the reload
3795 register, increment it there, then save back. */
3796 if (! post)
3798 if (real_in != result)
3799 emit_insn (gen_move_insn (result, real_in));
3800 if (plus_p)
3801 emit_insn (gen_add2_insn (result, inc));
3802 else
3803 emit_insn (gen_sub2_insn (result, inc));
3804 if (result != incloc)
3805 emit_insn (gen_move_insn (incloc, result));
3807 else
3809 /* Post-increment.
3811 Because this might be a jump insn or a compare, and because
3812 RESULT may not be available after the insn in an input
3813 reload, we must do the incrementing before the insn being
3814 reloaded for.
3816 We have already copied IN to RESULT. Increment the copy in
3817 RESULT, save that back, then decrement RESULT so it has
3818 the original value. */
3819 if (plus_p)
3820 emit_insn (gen_add2_insn (result, inc));
3821 else
3822 emit_insn (gen_sub2_insn (result, inc));
3823 emit_insn (gen_move_insn (incloc, result));
3824 /* Restore non-modified value for the result. We prefer this
3825 way because it does not require an additional hard
3826 register. */
3827 if (plus_p)
3829 poly_int64 offset;
3830 if (poly_int_rtx_p (inc, &offset))
3831 emit_insn (gen_add2_insn (result,
3832 gen_int_mode (-offset,
3833 GET_MODE (result))));
3834 else
3835 emit_insn (gen_sub2_insn (result, inc));
3837 else
3838 emit_insn (gen_add2_insn (result, inc));
3840 return result;
3843 /* Return true if the current move insn does not need processing as we
3844 already know that it satisfies its constraints. */
3845 static bool
3846 simple_move_p (void)
3848 rtx dest, src;
3849 enum reg_class dclass, sclass;
3851 lra_assert (curr_insn_set != NULL_RTX);
3852 dest = SET_DEST (curr_insn_set);
3853 src = SET_SRC (curr_insn_set);
3855 /* If the instruction has multiple sets we need to process it even if it
3856 is single_set. This can happen if one or more of the SETs are dead.
3857 See PR73650. */
3858 if (multiple_sets (curr_insn))
3859 return false;
3861 return ((dclass = get_op_class (dest)) != NO_REGS
3862 && (sclass = get_op_class (src)) != NO_REGS
3863 /* The backend guarantees that register moves of cost 2
3864 never need reloads. */
3865 && targetm.register_move_cost (GET_MODE (src), sclass, dclass) == 2);
3868 /* Swap operands NOP and NOP + 1. */
3869 static inline void
3870 swap_operands (int nop)
3872 std::swap (curr_operand_mode[nop], curr_operand_mode[nop + 1]);
3873 std::swap (original_subreg_reg_mode[nop], original_subreg_reg_mode[nop + 1]);
3874 std::swap (*curr_id->operand_loc[nop], *curr_id->operand_loc[nop + 1]);
3875 std::swap (equiv_substition_p[nop], equiv_substition_p[nop + 1]);
3876 /* Swap the duplicates too. */
3877 lra_update_dup (curr_id, nop);
3878 lra_update_dup (curr_id, nop + 1);
3881 /* Main entry point of the constraint code: search the body of the
3882 current insn to choose the best alternative. It is mimicking insn
3883 alternative cost calculation model of former reload pass. That is
3884 because machine descriptions were written to use this model. This
3885 model can be changed in future. Make commutative operand exchange
3886 if it is chosen.
3888 if CHECK_ONLY_P is false, do RTL changes to satisfy the
3889 constraints. Return true if any change happened during function
3890 call.
3892 If CHECK_ONLY_P is true then don't do any transformation. Just
3893 check that the insn satisfies all constraints. If the insn does
3894 not satisfy any constraint, return true. */
3895 static bool
3896 curr_insn_transform (bool check_only_p)
3898 int i, j, k;
3899 int n_operands;
3900 int n_alternatives;
3901 int n_outputs;
3902 int commutative;
3903 signed char goal_alt_matched[MAX_RECOG_OPERANDS][MAX_RECOG_OPERANDS];
3904 signed char match_inputs[MAX_RECOG_OPERANDS + 1];
3905 signed char outputs[MAX_RECOG_OPERANDS + 1];
3906 rtx_insn *before, *after;
3907 bool alt_p = false;
3908 /* Flag that the insn has been changed through a transformation. */
3909 bool change_p;
3910 bool sec_mem_p;
3911 bool use_sec_mem_p;
3912 int max_regno_before;
3913 int reused_alternative_num;
3915 curr_insn_set = single_set (curr_insn);
3916 if (curr_insn_set != NULL_RTX && simple_move_p ())
3918 /* We assume that the corresponding insn alternative has no
3919 earlier clobbers. If it is not the case, don't define move
3920 cost equal to 2 for the corresponding register classes. */
3921 lra_set_used_insn_alternative (curr_insn, LRA_NON_CLOBBERED_ALT);
3922 return false;
3925 no_input_reloads_p = no_output_reloads_p = false;
3926 goal_alt_number = -1;
3927 change_p = sec_mem_p = false;
3928 /* JUMP_INSNs and CALL_INSNs are not allowed to have any output
3929 reloads; neither are insns that SET cc0. Insns that use CC0 are
3930 not allowed to have any input reloads. */
3931 if (JUMP_P (curr_insn) || CALL_P (curr_insn))
3932 no_output_reloads_p = true;
3934 if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (curr_insn)))
3935 no_input_reloads_p = true;
3936 if (HAVE_cc0 && reg_set_p (cc0_rtx, PATTERN (curr_insn)))
3937 no_output_reloads_p = true;
3939 n_operands = curr_static_id->n_operands;
3940 n_alternatives = curr_static_id->n_alternatives;
3942 /* Just return "no reloads" if insn has no operands with
3943 constraints. */
3944 if (n_operands == 0 || n_alternatives == 0)
3945 return false;
3947 max_regno_before = max_reg_num ();
3949 for (i = 0; i < n_operands; i++)
3951 goal_alt_matched[i][0] = -1;
3952 goal_alt_matches[i] = -1;
3955 commutative = curr_static_id->commutative;
3957 /* Now see what we need for pseudos that didn't get hard regs or got
3958 the wrong kind of hard reg. For this, we must consider all the
3959 operands together against the register constraints. */
3961 best_losers = best_overall = INT_MAX;
3962 best_reload_sum = 0;
3964 curr_swapped = false;
3965 goal_alt_swapped = false;
3967 if (! check_only_p)
3968 /* Make equivalence substitution and memory subreg elimination
3969 before address processing because an address legitimacy can
3970 depend on memory mode. */
3971 for (i = 0; i < n_operands; i++)
3973 rtx op, subst, old;
3974 bool op_change_p = false;
3976 if (curr_static_id->operand[i].is_operator)
3977 continue;
3979 old = op = *curr_id->operand_loc[i];
3980 if (GET_CODE (old) == SUBREG)
3981 old = SUBREG_REG (old);
3982 subst = get_equiv_with_elimination (old, curr_insn);
3983 original_subreg_reg_mode[i] = VOIDmode;
3984 equiv_substition_p[i] = false;
3985 if (subst != old)
3987 equiv_substition_p[i] = true;
3988 subst = copy_rtx (subst);
3989 lra_assert (REG_P (old));
3990 if (GET_CODE (op) != SUBREG)
3991 *curr_id->operand_loc[i] = subst;
3992 else
3994 SUBREG_REG (op) = subst;
3995 if (GET_MODE (subst) == VOIDmode)
3996 original_subreg_reg_mode[i] = GET_MODE (old);
3998 if (lra_dump_file != NULL)
4000 fprintf (lra_dump_file,
4001 "Changing pseudo %d in operand %i of insn %u on equiv ",
4002 REGNO (old), i, INSN_UID (curr_insn));
4003 dump_value_slim (lra_dump_file, subst, 1);
4004 fprintf (lra_dump_file, "\n");
4006 op_change_p = change_p = true;
4008 if (simplify_operand_subreg (i, GET_MODE (old)) || op_change_p)
4010 change_p = true;
4011 lra_update_dup (curr_id, i);
4015 /* Reload address registers and displacements. We do it before
4016 finding an alternative because of memory constraints. */
4017 before = after = NULL;
4018 for (i = 0; i < n_operands; i++)
4019 if (! curr_static_id->operand[i].is_operator
4020 && process_address (i, check_only_p, &before, &after))
4022 if (check_only_p)
4023 return true;
4024 change_p = true;
4025 lra_update_dup (curr_id, i);
4028 if (change_p)
4029 /* If we've changed the instruction then any alternative that
4030 we chose previously may no longer be valid. */
4031 lra_set_used_insn_alternative (curr_insn, LRA_UNKNOWN_ALT);
4033 if (! check_only_p && curr_insn_set != NULL_RTX
4034 && check_and_process_move (&change_p, &sec_mem_p))
4035 return change_p;
4037 try_swapped:
4039 reused_alternative_num = check_only_p ? LRA_UNKNOWN_ALT : curr_id->used_insn_alternative;
4040 if (lra_dump_file != NULL && reused_alternative_num >= 0)
4041 fprintf (lra_dump_file, "Reusing alternative %d for insn #%u\n",
4042 reused_alternative_num, INSN_UID (curr_insn));
4044 if (process_alt_operands (reused_alternative_num))
4045 alt_p = true;
4047 if (check_only_p)
4048 return ! alt_p || best_losers != 0;
4050 /* If insn is commutative (it's safe to exchange a certain pair of
4051 operands) then we need to try each alternative twice, the second
4052 time matching those two operands as if we had exchanged them. To
4053 do this, really exchange them in operands.
4055 If we have just tried the alternatives the second time, return
4056 operands to normal and drop through. */
4058 if (reused_alternative_num < 0 && commutative >= 0)
4060 curr_swapped = !curr_swapped;
4061 if (curr_swapped)
4063 swap_operands (commutative);
4064 goto try_swapped;
4066 else
4067 swap_operands (commutative);
4070 if (! alt_p && ! sec_mem_p)
4072 /* No alternative works with reloads?? */
4073 if (INSN_CODE (curr_insn) >= 0)
4074 fatal_insn ("unable to generate reloads for:", curr_insn);
4075 error_for_asm (curr_insn,
4076 "inconsistent operand constraints in an %<asm%>");
4077 lra_asm_error_p = true;
4078 /* Avoid further trouble with this insn. Don't generate use
4079 pattern here as we could use the insn SP offset. */
4080 lra_set_insn_deleted (curr_insn);
4081 return true;
4084 /* If the best alternative is with operands 1 and 2 swapped, swap
4085 them. Update the operand numbers of any reloads already
4086 pushed. */
4088 if (goal_alt_swapped)
4090 if (lra_dump_file != NULL)
4091 fprintf (lra_dump_file, " Commutative operand exchange in insn %u\n",
4092 INSN_UID (curr_insn));
4094 /* Swap the duplicates too. */
4095 swap_operands (commutative);
4096 change_p = true;
4099 /* Some targets' TARGET_SECONDARY_MEMORY_NEEDED (e.g. x86) are defined
4100 too conservatively. So we use the secondary memory only if there
4101 is no any alternative without reloads. */
4102 use_sec_mem_p = false;
4103 if (! alt_p)
4104 use_sec_mem_p = true;
4105 else if (sec_mem_p)
4107 for (i = 0; i < n_operands; i++)
4108 if (! goal_alt_win[i] && ! goal_alt_match_win[i])
4109 break;
4110 use_sec_mem_p = i < n_operands;
4113 if (use_sec_mem_p)
4115 int in = -1, out = -1;
4116 rtx new_reg, src, dest, rld;
4117 machine_mode sec_mode, rld_mode;
4119 lra_assert (curr_insn_set != NULL_RTX && sec_mem_p);
4120 dest = SET_DEST (curr_insn_set);
4121 src = SET_SRC (curr_insn_set);
4122 for (i = 0; i < n_operands; i++)
4123 if (*curr_id->operand_loc[i] == dest)
4124 out = i;
4125 else if (*curr_id->operand_loc[i] == src)
4126 in = i;
4127 for (i = 0; i < curr_static_id->n_dups; i++)
4128 if (out < 0 && *curr_id->dup_loc[i] == dest)
4129 out = curr_static_id->dup_num[i];
4130 else if (in < 0 && *curr_id->dup_loc[i] == src)
4131 in = curr_static_id->dup_num[i];
4132 lra_assert (out >= 0 && in >= 0
4133 && curr_static_id->operand[out].type == OP_OUT
4134 && curr_static_id->operand[in].type == OP_IN);
4135 rld = partial_subreg_p (GET_MODE (src), GET_MODE (dest)) ? src : dest;
4136 rld_mode = GET_MODE (rld);
4137 sec_mode = targetm.secondary_memory_needed_mode (rld_mode);
4138 new_reg = lra_create_new_reg (sec_mode, NULL_RTX,
4139 NO_REGS, "secondary");
4140 /* If the mode is changed, it should be wider. */
4141 lra_assert (!partial_subreg_p (sec_mode, rld_mode));
4142 if (sec_mode != rld_mode)
4144 /* If the target says specifically to use another mode for
4145 secondary memory moves we cannot reuse the original
4146 insn. */
4147 after = emit_spill_move (false, new_reg, dest);
4148 lra_process_new_insns (curr_insn, NULL, after,
4149 "Inserting the sec. move");
4150 /* We may have non null BEFORE here (e.g. after address
4151 processing. */
4152 push_to_sequence (before);
4153 before = emit_spill_move (true, new_reg, src);
4154 emit_insn (before);
4155 before = get_insns ();
4156 end_sequence ();
4157 lra_process_new_insns (curr_insn, before, NULL, "Changing on");
4158 lra_set_insn_deleted (curr_insn);
4160 else if (dest == rld)
4162 *curr_id->operand_loc[out] = new_reg;
4163 lra_update_dup (curr_id, out);
4164 after = emit_spill_move (false, new_reg, dest);
4165 lra_process_new_insns (curr_insn, NULL, after,
4166 "Inserting the sec. move");
4168 else
4170 *curr_id->operand_loc[in] = new_reg;
4171 lra_update_dup (curr_id, in);
4172 /* See comments above. */
4173 push_to_sequence (before);
4174 before = emit_spill_move (true, new_reg, src);
4175 emit_insn (before);
4176 before = get_insns ();
4177 end_sequence ();
4178 lra_process_new_insns (curr_insn, before, NULL,
4179 "Inserting the sec. move");
4181 lra_update_insn_regno_info (curr_insn);
4182 return true;
4185 lra_assert (goal_alt_number >= 0);
4186 lra_set_used_insn_alternative (curr_insn, goal_alt_number);
4188 if (lra_dump_file != NULL)
4190 const char *p;
4192 fprintf (lra_dump_file, " Choosing alt %d in insn %u:",
4193 goal_alt_number, INSN_UID (curr_insn));
4194 for (i = 0; i < n_operands; i++)
4196 p = (curr_static_id->operand_alternative
4197 [goal_alt_number * n_operands + i].constraint);
4198 if (*p == '\0')
4199 continue;
4200 fprintf (lra_dump_file, " (%d) ", i);
4201 for (; *p != '\0' && *p != ',' && *p != '#'; p++)
4202 fputc (*p, lra_dump_file);
4204 if (INSN_CODE (curr_insn) >= 0
4205 && (p = get_insn_name (INSN_CODE (curr_insn))) != NULL)
4206 fprintf (lra_dump_file, " {%s}", p);
4207 if (maybe_ne (curr_id->sp_offset, 0))
4209 fprintf (lra_dump_file, " (sp_off=");
4210 print_dec (curr_id->sp_offset, lra_dump_file);
4211 fprintf (lra_dump_file, ")");
4213 fprintf (lra_dump_file, "\n");
4216 /* Right now, for any pair of operands I and J that are required to
4217 match, with J < I, goal_alt_matches[I] is J. Add I to
4218 goal_alt_matched[J]. */
4220 for (i = 0; i < n_operands; i++)
4221 if ((j = goal_alt_matches[i]) >= 0)
4223 for (k = 0; goal_alt_matched[j][k] >= 0; k++)
4225 /* We allow matching one output operand and several input
4226 operands. */
4227 lra_assert (k == 0
4228 || (curr_static_id->operand[j].type == OP_OUT
4229 && curr_static_id->operand[i].type == OP_IN
4230 && (curr_static_id->operand
4231 [goal_alt_matched[j][0]].type == OP_IN)));
4232 goal_alt_matched[j][k] = i;
4233 goal_alt_matched[j][k + 1] = -1;
4236 for (i = 0; i < n_operands; i++)
4237 goal_alt_win[i] |= goal_alt_match_win[i];
4239 /* Any constants that aren't allowed and can't be reloaded into
4240 registers are here changed into memory references. */
4241 for (i = 0; i < n_operands; i++)
4242 if (goal_alt_win[i])
4244 int regno;
4245 enum reg_class new_class;
4246 rtx reg = *curr_id->operand_loc[i];
4248 if (GET_CODE (reg) == SUBREG)
4249 reg = SUBREG_REG (reg);
4251 if (REG_P (reg) && (regno = REGNO (reg)) >= FIRST_PSEUDO_REGISTER)
4253 bool ok_p = in_class_p (reg, goal_alt[i], &new_class);
4255 if (new_class != NO_REGS && get_reg_class (regno) != new_class)
4257 lra_assert (ok_p);
4258 lra_change_class (regno, new_class, " Change to", true);
4262 else
4264 const char *constraint;
4265 char c;
4266 rtx op = *curr_id->operand_loc[i];
4267 rtx subreg = NULL_RTX;
4268 machine_mode mode = curr_operand_mode[i];
4270 if (GET_CODE (op) == SUBREG)
4272 subreg = op;
4273 op = SUBREG_REG (op);
4274 mode = GET_MODE (op);
4277 if (CONST_POOL_OK_P (mode, op)
4278 && ((targetm.preferred_reload_class
4279 (op, (enum reg_class) goal_alt[i]) == NO_REGS)
4280 || no_input_reloads_p))
4282 rtx tem = force_const_mem (mode, op);
4284 change_p = true;
4285 if (subreg != NULL_RTX)
4286 tem = gen_rtx_SUBREG (mode, tem, SUBREG_BYTE (subreg));
4288 *curr_id->operand_loc[i] = tem;
4289 lra_update_dup (curr_id, i);
4290 process_address (i, false, &before, &after);
4292 /* If the alternative accepts constant pool refs directly
4293 there will be no reload needed at all. */
4294 if (subreg != NULL_RTX)
4295 continue;
4296 /* Skip alternatives before the one requested. */
4297 constraint = (curr_static_id->operand_alternative
4298 [goal_alt_number * n_operands + i].constraint);
4299 for (;
4300 (c = *constraint) && c != ',' && c != '#';
4301 constraint += CONSTRAINT_LEN (c, constraint))
4303 enum constraint_num cn = lookup_constraint (constraint);
4304 if ((insn_extra_memory_constraint (cn)
4305 || insn_extra_special_memory_constraint (cn))
4306 && satisfies_memory_constraint_p (tem, cn))
4307 break;
4309 if (c == '\0' || c == ',' || c == '#')
4310 continue;
4312 goal_alt_win[i] = true;
4316 n_outputs = 0;
4317 outputs[0] = -1;
4318 for (i = 0; i < n_operands; i++)
4320 int regno;
4321 bool optional_p = false;
4322 rtx old, new_reg;
4323 rtx op = *curr_id->operand_loc[i];
4325 if (goal_alt_win[i])
4327 if (goal_alt[i] == NO_REGS
4328 && REG_P (op)
4329 /* When we assign NO_REGS it means that we will not
4330 assign a hard register to the scratch pseudo by
4331 assigment pass and the scratch pseudo will be
4332 spilled. Spilled scratch pseudos are transformed
4333 back to scratches at the LRA end. */
4334 && lra_former_scratch_operand_p (curr_insn, i)
4335 && lra_former_scratch_p (REGNO (op)))
4337 int regno = REGNO (op);
4338 lra_change_class (regno, NO_REGS, " Change to", true);
4339 if (lra_get_regno_hard_regno (regno) >= 0)
4340 /* We don't have to mark all insn affected by the
4341 spilled pseudo as there is only one such insn, the
4342 current one. */
4343 reg_renumber[regno] = -1;
4344 lra_assert (bitmap_single_bit_set_p
4345 (&lra_reg_info[REGNO (op)].insn_bitmap));
4347 /* We can do an optional reload. If the pseudo got a hard
4348 reg, we might improve the code through inheritance. If
4349 it does not get a hard register we coalesce memory/memory
4350 moves later. Ignore move insns to avoid cycling. */
4351 if (! lra_simple_p
4352 && lra_undo_inheritance_iter < LRA_MAX_INHERITANCE_PASSES
4353 && goal_alt[i] != NO_REGS && REG_P (op)
4354 && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER
4355 && regno < new_regno_start
4356 && ! lra_former_scratch_p (regno)
4357 && reg_renumber[regno] < 0
4358 /* Check that the optional reload pseudo will be able to
4359 hold given mode value. */
4360 && ! (prohibited_class_reg_set_mode_p
4361 (goal_alt[i], reg_class_contents[goal_alt[i]],
4362 PSEUDO_REGNO_MODE (regno)))
4363 && (curr_insn_set == NULL_RTX
4364 || !((REG_P (SET_SRC (curr_insn_set))
4365 || MEM_P (SET_SRC (curr_insn_set))
4366 || GET_CODE (SET_SRC (curr_insn_set)) == SUBREG)
4367 && (REG_P (SET_DEST (curr_insn_set))
4368 || MEM_P (SET_DEST (curr_insn_set))
4369 || GET_CODE (SET_DEST (curr_insn_set)) == SUBREG))))
4370 optional_p = true;
4371 else if (goal_alt_matched[i][0] != -1
4372 && curr_static_id->operand[i].type == OP_OUT
4373 && (curr_static_id->operand_alternative
4374 [goal_alt_number * n_operands + i].earlyclobber)
4375 && REG_P (op))
4377 for (j = 0; goal_alt_matched[i][j] != -1; j++)
4379 rtx op2 = *curr_id->operand_loc[goal_alt_matched[i][j]];
4381 if (REG_P (op2) && REGNO (op) != REGNO (op2))
4382 break;
4384 if (goal_alt_matched[i][j] != -1)
4386 /* Generate reloads for different output and matched
4387 input registers. This is the easiest way to avoid
4388 creation of non-existing register conflicts in
4389 lra-lives.c. */
4390 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i], &before,
4391 &after, TRUE);
4392 outputs[n_outputs++] = i;
4393 outputs[n_outputs] = -1;
4395 continue;
4397 else
4398 continue;
4401 /* Operands that match previous ones have already been handled. */
4402 if (goal_alt_matches[i] >= 0)
4403 continue;
4405 /* We should not have an operand with a non-offsettable address
4406 appearing where an offsettable address will do. It also may
4407 be a case when the address should be special in other words
4408 not a general one (e.g. it needs no index reg). */
4409 if (goal_alt_matched[i][0] == -1 && goal_alt_offmemok[i] && MEM_P (op))
4411 enum reg_class rclass;
4412 rtx *loc = &XEXP (op, 0);
4413 enum rtx_code code = GET_CODE (*loc);
4415 push_to_sequence (before);
4416 rclass = base_reg_class (GET_MODE (op), MEM_ADDR_SPACE (op),
4417 MEM, SCRATCH);
4418 if (GET_RTX_CLASS (code) == RTX_AUTOINC)
4419 new_reg = emit_inc (rclass, *loc, *loc,
4420 /* This value does not matter for MODIFY. */
4421 GET_MODE_SIZE (GET_MODE (op)));
4422 else if (get_reload_reg (OP_IN, Pmode, *loc, rclass, FALSE,
4423 "offsetable address", &new_reg))
4425 rtx addr = *loc;
4426 enum rtx_code code = GET_CODE (addr);
4427 bool align_p = false;
4429 if (code == AND && CONST_INT_P (XEXP (addr, 1)))
4431 /* (and ... (const_int -X)) is used to align to X bytes. */
4432 align_p = true;
4433 addr = XEXP (*loc, 0);
4435 else
4436 addr = canonicalize_reload_addr (addr);
4438 lra_emit_move (new_reg, addr);
4439 if (align_p)
4440 emit_move_insn (new_reg, gen_rtx_AND (GET_MODE (new_reg), new_reg, XEXP (*loc, 1)));
4442 before = get_insns ();
4443 end_sequence ();
4444 *loc = new_reg;
4445 lra_update_dup (curr_id, i);
4447 else if (goal_alt_matched[i][0] == -1)
4449 machine_mode mode;
4450 rtx reg, *loc;
4451 int hard_regno;
4452 enum op_type type = curr_static_id->operand[i].type;
4454 loc = curr_id->operand_loc[i];
4455 mode = curr_operand_mode[i];
4456 if (GET_CODE (*loc) == SUBREG)
4458 reg = SUBREG_REG (*loc);
4459 poly_int64 byte = SUBREG_BYTE (*loc);
4460 if (REG_P (reg)
4461 /* Strict_low_part requires reloading the register and not
4462 just the subreg. Likewise for a strict subreg no wider
4463 than a word for WORD_REGISTER_OPERATIONS targets. */
4464 && (curr_static_id->operand[i].strict_low
4465 || (!paradoxical_subreg_p (mode, GET_MODE (reg))
4466 && (hard_regno
4467 = get_try_hard_regno (REGNO (reg))) >= 0
4468 && (simplify_subreg_regno
4469 (hard_regno,
4470 GET_MODE (reg), byte, mode) < 0)
4471 && (goal_alt[i] == NO_REGS
4472 || (simplify_subreg_regno
4473 (ira_class_hard_regs[goal_alt[i]][0],
4474 GET_MODE (reg), byte, mode) >= 0)))
4475 || (partial_subreg_p (mode, GET_MODE (reg))
4476 && known_le (GET_MODE_SIZE (GET_MODE (reg)),
4477 UNITS_PER_WORD)
4478 && WORD_REGISTER_OPERATIONS)))
4480 /* An OP_INOUT is required when reloading a subreg of a
4481 mode wider than a word to ensure that data beyond the
4482 word being reloaded is preserved. Also automatically
4483 ensure that strict_low_part reloads are made into
4484 OP_INOUT which should already be true from the backend
4485 constraints. */
4486 if (type == OP_OUT
4487 && (curr_static_id->operand[i].strict_low
4488 || read_modify_subreg_p (*loc)))
4489 type = OP_INOUT;
4490 loc = &SUBREG_REG (*loc);
4491 mode = GET_MODE (*loc);
4494 old = *loc;
4495 if (get_reload_reg (type, mode, old, goal_alt[i],
4496 loc != curr_id->operand_loc[i], "", &new_reg)
4497 && type != OP_OUT)
4499 push_to_sequence (before);
4500 lra_emit_move (new_reg, old);
4501 before = get_insns ();
4502 end_sequence ();
4504 *loc = new_reg;
4505 if (type != OP_IN
4506 && find_reg_note (curr_insn, REG_UNUSED, old) == NULL_RTX)
4508 start_sequence ();
4509 lra_emit_move (type == OP_INOUT ? copy_rtx (old) : old, new_reg);
4510 emit_insn (after);
4511 after = get_insns ();
4512 end_sequence ();
4513 *loc = new_reg;
4515 for (j = 0; j < goal_alt_dont_inherit_ops_num; j++)
4516 if (goal_alt_dont_inherit_ops[j] == i)
4518 lra_set_regno_unique_value (REGNO (new_reg));
4519 break;
4521 lra_update_dup (curr_id, i);
4523 else if (curr_static_id->operand[i].type == OP_IN
4524 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4525 == OP_OUT
4526 || (curr_static_id->operand[goal_alt_matched[i][0]].type
4527 == OP_INOUT
4528 && (operands_match_p
4529 (*curr_id->operand_loc[i],
4530 *curr_id->operand_loc[goal_alt_matched[i][0]],
4531 -1)))))
4533 /* generate reloads for input and matched outputs. */
4534 match_inputs[0] = i;
4535 match_inputs[1] = -1;
4536 match_reload (goal_alt_matched[i][0], match_inputs, outputs,
4537 goal_alt[i], &before, &after,
4538 curr_static_id->operand_alternative
4539 [goal_alt_number * n_operands + goal_alt_matched[i][0]]
4540 .earlyclobber);
4542 else if ((curr_static_id->operand[i].type == OP_OUT
4543 || (curr_static_id->operand[i].type == OP_INOUT
4544 && (operands_match_p
4545 (*curr_id->operand_loc[i],
4546 *curr_id->operand_loc[goal_alt_matched[i][0]],
4547 -1))))
4548 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4549 == OP_IN))
4550 /* Generate reloads for output and matched inputs. */
4551 match_reload (i, goal_alt_matched[i], outputs, goal_alt[i], &before,
4552 &after, curr_static_id->operand_alternative
4553 [goal_alt_number * n_operands + i].earlyclobber);
4554 else if (curr_static_id->operand[i].type == OP_IN
4555 && (curr_static_id->operand[goal_alt_matched[i][0]].type
4556 == OP_IN))
4558 /* Generate reloads for matched inputs. */
4559 match_inputs[0] = i;
4560 for (j = 0; (k = goal_alt_matched[i][j]) >= 0; j++)
4561 match_inputs[j + 1] = k;
4562 match_inputs[j + 1] = -1;
4563 match_reload (-1, match_inputs, outputs, goal_alt[i], &before,
4564 &after, false);
4566 else
4567 /* We must generate code in any case when function
4568 process_alt_operands decides that it is possible. */
4569 gcc_unreachable ();
4571 /* Memorise processed outputs so that output remaining to be processed
4572 can avoid using the same register value (see match_reload). */
4573 if (curr_static_id->operand[i].type == OP_OUT)
4575 outputs[n_outputs++] = i;
4576 outputs[n_outputs] = -1;
4579 if (optional_p)
4581 rtx reg = op;
4583 lra_assert (REG_P (reg));
4584 regno = REGNO (reg);
4585 op = *curr_id->operand_loc[i]; /* Substitution. */
4586 if (GET_CODE (op) == SUBREG)
4587 op = SUBREG_REG (op);
4588 gcc_assert (REG_P (op) && (int) REGNO (op) >= new_regno_start);
4589 bitmap_set_bit (&lra_optional_reload_pseudos, REGNO (op));
4590 lra_reg_info[REGNO (op)].restore_rtx = reg;
4591 if (lra_dump_file != NULL)
4592 fprintf (lra_dump_file,
4593 " Making reload reg %d for reg %d optional\n",
4594 REGNO (op), regno);
4597 if (before != NULL_RTX || after != NULL_RTX
4598 || max_regno_before != max_reg_num ())
4599 change_p = true;
4600 if (change_p)
4602 lra_update_operator_dups (curr_id);
4603 /* Something changes -- process the insn. */
4604 lra_update_insn_regno_info (curr_insn);
4606 lra_process_new_insns (curr_insn, before, after, "Inserting insn reload");
4607 return change_p;
4610 /* Return true if INSN satisfies all constraints. In other words, no
4611 reload insns are needed. */
4612 bool
4613 lra_constrain_insn (rtx_insn *insn)
4615 int saved_new_regno_start = new_regno_start;
4616 int saved_new_insn_uid_start = new_insn_uid_start;
4617 bool change_p;
4619 curr_insn = insn;
4620 curr_id = lra_get_insn_recog_data (curr_insn);
4621 curr_static_id = curr_id->insn_static_data;
4622 new_insn_uid_start = get_max_uid ();
4623 new_regno_start = max_reg_num ();
4624 change_p = curr_insn_transform (true);
4625 new_regno_start = saved_new_regno_start;
4626 new_insn_uid_start = saved_new_insn_uid_start;
4627 return ! change_p;
4630 /* Return true if X is in LIST. */
4631 static bool
4632 in_list_p (rtx x, rtx list)
4634 for (; list != NULL_RTX; list = XEXP (list, 1))
4635 if (XEXP (list, 0) == x)
4636 return true;
4637 return false;
4640 /* Return true if X contains an allocatable hard register (if
4641 HARD_REG_P) or a (spilled if SPILLED_P) pseudo. */
4642 static bool
4643 contains_reg_p (rtx x, bool hard_reg_p, bool spilled_p)
4645 int i, j;
4646 const char *fmt;
4647 enum rtx_code code;
4649 code = GET_CODE (x);
4650 if (REG_P (x))
4652 int regno = REGNO (x);
4653 HARD_REG_SET alloc_regs;
4655 if (hard_reg_p)
4657 if (regno >= FIRST_PSEUDO_REGISTER)
4658 regno = lra_get_regno_hard_regno (regno);
4659 if (regno < 0)
4660 return false;
4661 alloc_regs = ~lra_no_alloc_regs;
4662 return overlaps_hard_reg_set_p (alloc_regs, GET_MODE (x), regno);
4664 else
4666 if (regno < FIRST_PSEUDO_REGISTER)
4667 return false;
4668 if (! spilled_p)
4669 return true;
4670 return lra_get_regno_hard_regno (regno) < 0;
4673 fmt = GET_RTX_FORMAT (code);
4674 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4676 if (fmt[i] == 'e')
4678 if (contains_reg_p (XEXP (x, i), hard_reg_p, spilled_p))
4679 return true;
4681 else if (fmt[i] == 'E')
4683 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4684 if (contains_reg_p (XVECEXP (x, i, j), hard_reg_p, spilled_p))
4685 return true;
4688 return false;
4691 /* Process all regs in location *LOC and change them on equivalent
4692 substitution. Return true if any change was done. */
4693 static bool
4694 loc_equivalence_change_p (rtx *loc)
4696 rtx subst, reg, x = *loc;
4697 bool result = false;
4698 enum rtx_code code = GET_CODE (x);
4699 const char *fmt;
4700 int i, j;
4702 if (code == SUBREG)
4704 reg = SUBREG_REG (x);
4705 if ((subst = get_equiv_with_elimination (reg, curr_insn)) != reg
4706 && GET_MODE (subst) == VOIDmode)
4708 /* We cannot reload debug location. Simplify subreg here
4709 while we know the inner mode. */
4710 *loc = simplify_gen_subreg (GET_MODE (x), subst,
4711 GET_MODE (reg), SUBREG_BYTE (x));
4712 return true;
4715 if (code == REG && (subst = get_equiv_with_elimination (x, curr_insn)) != x)
4717 *loc = subst;
4718 return true;
4721 /* Scan all the operand sub-expressions. */
4722 fmt = GET_RTX_FORMAT (code);
4723 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4725 if (fmt[i] == 'e')
4726 result = loc_equivalence_change_p (&XEXP (x, i)) || result;
4727 else if (fmt[i] == 'E')
4728 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4729 result
4730 = loc_equivalence_change_p (&XVECEXP (x, i, j)) || result;
4732 return result;
4735 /* Similar to loc_equivalence_change_p, but for use as
4736 simplify_replace_fn_rtx callback. DATA is insn for which the
4737 elimination is done. If it null we don't do the elimination. */
4738 static rtx
4739 loc_equivalence_callback (rtx loc, const_rtx, void *data)
4741 if (!REG_P (loc))
4742 return NULL_RTX;
4744 rtx subst = (data == NULL
4745 ? get_equiv (loc) : get_equiv_with_elimination (loc, (rtx_insn *) data));
4746 if (subst != loc)
4747 return subst;
4749 return NULL_RTX;
4752 /* Maximum number of generated reload insns per an insn. It is for
4753 preventing this pass cycling in a bug case. */
4754 #define MAX_RELOAD_INSNS_NUMBER LRA_MAX_INSN_RELOADS
4756 /* The current iteration number of this LRA pass. */
4757 int lra_constraint_iter;
4759 /* True if we should during assignment sub-pass check assignment
4760 correctness for all pseudos and spill some of them to correct
4761 conflicts. It can be necessary when we substitute equiv which
4762 needs checking register allocation correctness because the
4763 equivalent value contains allocatable hard registers, or when we
4764 restore multi-register pseudo, or when we change the insn code and
4765 its operand became INOUT operand when it was IN one before. */
4766 bool check_and_force_assignment_correctness_p;
4768 /* Return true if REGNO is referenced in more than one block. */
4769 static bool
4770 multi_block_pseudo_p (int regno)
4772 basic_block bb = NULL;
4773 unsigned int uid;
4774 bitmap_iterator bi;
4776 if (regno < FIRST_PSEUDO_REGISTER)
4777 return false;
4779 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
4780 if (bb == NULL)
4781 bb = BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn);
4782 else if (BLOCK_FOR_INSN (lra_insn_recog_data[uid]->insn) != bb)
4783 return true;
4784 return false;
4787 /* Return true if LIST contains a deleted insn. */
4788 static bool
4789 contains_deleted_insn_p (rtx_insn_list *list)
4791 for (; list != NULL_RTX; list = list->next ())
4792 if (NOTE_P (list->insn ())
4793 && NOTE_KIND (list->insn ()) == NOTE_INSN_DELETED)
4794 return true;
4795 return false;
4798 /* Return true if X contains a pseudo dying in INSN. */
4799 static bool
4800 dead_pseudo_p (rtx x, rtx_insn *insn)
4802 int i, j;
4803 const char *fmt;
4804 enum rtx_code code;
4806 if (REG_P (x))
4807 return (insn != NULL_RTX
4808 && find_regno_note (insn, REG_DEAD, REGNO (x)) != NULL_RTX);
4809 code = GET_CODE (x);
4810 fmt = GET_RTX_FORMAT (code);
4811 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4813 if (fmt[i] == 'e')
4815 if (dead_pseudo_p (XEXP (x, i), insn))
4816 return true;
4818 else if (fmt[i] == 'E')
4820 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4821 if (dead_pseudo_p (XVECEXP (x, i, j), insn))
4822 return true;
4825 return false;
4828 /* Return true if INSN contains a dying pseudo in INSN right hand
4829 side. */
4830 static bool
4831 insn_rhs_dead_pseudo_p (rtx_insn *insn)
4833 rtx set = single_set (insn);
4835 gcc_assert (set != NULL);
4836 return dead_pseudo_p (SET_SRC (set), insn);
4839 /* Return true if any init insn of REGNO contains a dying pseudo in
4840 insn right hand side. */
4841 static bool
4842 init_insn_rhs_dead_pseudo_p (int regno)
4844 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4846 if (insns == NULL)
4847 return false;
4848 for (; insns != NULL_RTX; insns = insns->next ())
4849 if (insn_rhs_dead_pseudo_p (insns->insn ()))
4850 return true;
4851 return false;
4854 /* Return TRUE if REGNO has a reverse equivalence. The equivalence is
4855 reverse only if we have one init insn with given REGNO as a
4856 source. */
4857 static bool
4858 reverse_equiv_p (int regno)
4860 rtx_insn_list *insns = ira_reg_equiv[regno].init_insns;
4861 rtx set;
4863 if (insns == NULL)
4864 return false;
4865 if (! INSN_P (insns->insn ())
4866 || insns->next () != NULL)
4867 return false;
4868 if ((set = single_set (insns->insn ())) == NULL_RTX)
4869 return false;
4870 return REG_P (SET_SRC (set)) && (int) REGNO (SET_SRC (set)) == regno;
4873 /* Return TRUE if REGNO was reloaded in an equivalence init insn. We
4874 call this function only for non-reverse equivalence. */
4875 static bool
4876 contains_reloaded_insn_p (int regno)
4878 rtx set;
4879 rtx_insn_list *list = ira_reg_equiv[regno].init_insns;
4881 for (; list != NULL; list = list->next ())
4882 if ((set = single_set (list->insn ())) == NULL_RTX
4883 || ! REG_P (SET_DEST (set))
4884 || (int) REGNO (SET_DEST (set)) != regno)
4885 return true;
4886 return false;
4889 /* Entry function of LRA constraint pass. Return true if the
4890 constraint pass did change the code. */
4891 bool
4892 lra_constraints (bool first_p)
4894 bool changed_p;
4895 int i, hard_regno, new_insns_num;
4896 unsigned int min_len, new_min_len, uid;
4897 rtx set, x, reg, dest_reg;
4898 basic_block last_bb;
4899 bitmap_iterator bi;
4901 lra_constraint_iter++;
4902 if (lra_dump_file != NULL)
4903 fprintf (lra_dump_file, "\n********** Local #%d: **********\n\n",
4904 lra_constraint_iter);
4905 changed_p = false;
4906 if (pic_offset_table_rtx
4907 && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
4908 check_and_force_assignment_correctness_p = true;
4909 else if (first_p)
4910 /* On the first iteration we should check IRA assignment
4911 correctness. In rare cases, the assignments can be wrong as
4912 early clobbers operands are ignored in IRA or usages of
4913 paradoxical sub-registers are not taken into account by
4914 IRA. */
4915 check_and_force_assignment_correctness_p = true;
4916 new_insn_uid_start = get_max_uid ();
4917 new_regno_start = first_p ? lra_constraint_new_regno_start : max_reg_num ();
4918 /* Mark used hard regs for target stack size calulations. */
4919 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4920 if (lra_reg_info[i].nrefs != 0
4921 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
4923 int j, nregs;
4925 nregs = hard_regno_nregs (hard_regno, lra_reg_info[i].biggest_mode);
4926 for (j = 0; j < nregs; j++)
4927 df_set_regs_ever_live (hard_regno + j, true);
4929 /* Do elimination before the equivalence processing as we can spill
4930 some pseudos during elimination. */
4931 lra_eliminate (false, first_p);
4932 auto_bitmap equiv_insn_bitmap (&reg_obstack);
4933 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4934 if (lra_reg_info[i].nrefs != 0)
4936 ira_reg_equiv[i].profitable_p = true;
4937 reg = regno_reg_rtx[i];
4938 if (lra_get_regno_hard_regno (i) < 0 && (x = get_equiv (reg)) != reg)
4940 bool pseudo_p = contains_reg_p (x, false, false);
4942 /* After RTL transformation, we cannot guarantee that
4943 pseudo in the substitution was not reloaded which might
4944 make equivalence invalid. For example, in reverse
4945 equiv of p0
4947 p0 <- ...
4949 equiv_mem <- p0
4951 the memory address register was reloaded before the 2nd
4952 insn. */
4953 if ((! first_p && pseudo_p)
4954 /* We don't use DF for compilation speed sake. So it
4955 is problematic to update live info when we use an
4956 equivalence containing pseudos in more than one
4957 BB. */
4958 || (pseudo_p && multi_block_pseudo_p (i))
4959 /* If an init insn was deleted for some reason, cancel
4960 the equiv. We could update the equiv insns after
4961 transformations including an equiv insn deletion
4962 but it is not worthy as such cases are extremely
4963 rare. */
4964 || contains_deleted_insn_p (ira_reg_equiv[i].init_insns)
4965 /* If it is not a reverse equivalence, we check that a
4966 pseudo in rhs of the init insn is not dying in the
4967 insn. Otherwise, the live info at the beginning of
4968 the corresponding BB might be wrong after we
4969 removed the insn. When the equiv can be a
4970 constant, the right hand side of the init insn can
4971 be a pseudo. */
4972 || (! reverse_equiv_p (i)
4973 && (init_insn_rhs_dead_pseudo_p (i)
4974 /* If we reloaded the pseudo in an equivalence
4975 init insn, we cannot remove the equiv init
4976 insns and the init insns might write into
4977 const memory in this case. */
4978 || contains_reloaded_insn_p (i)))
4979 /* Prevent access beyond equivalent memory for
4980 paradoxical subregs. */
4981 || (MEM_P (x)
4982 && maybe_gt (GET_MODE_SIZE (lra_reg_info[i].biggest_mode),
4983 GET_MODE_SIZE (GET_MODE (x))))
4984 || (pic_offset_table_rtx
4985 && ((CONST_POOL_OK_P (PSEUDO_REGNO_MODE (i), x)
4986 && (targetm.preferred_reload_class
4987 (x, lra_get_allocno_class (i)) == NO_REGS))
4988 || contains_symbol_ref_p (x))))
4989 ira_reg_equiv[i].defined_p = false;
4990 if (contains_reg_p (x, false, true))
4991 ira_reg_equiv[i].profitable_p = false;
4992 if (get_equiv (reg) != reg)
4993 bitmap_ior_into (equiv_insn_bitmap, &lra_reg_info[i].insn_bitmap);
4996 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
4997 update_equiv (i);
4998 /* We should add all insns containing pseudos which should be
4999 substituted by their equivalences. */
5000 EXECUTE_IF_SET_IN_BITMAP (equiv_insn_bitmap, 0, uid, bi)
5001 lra_push_insn_by_uid (uid);
5002 min_len = lra_insn_stack_length ();
5003 new_insns_num = 0;
5004 last_bb = NULL;
5005 changed_p = false;
5006 while ((new_min_len = lra_insn_stack_length ()) != 0)
5008 curr_insn = lra_pop_insn ();
5009 --new_min_len;
5010 curr_bb = BLOCK_FOR_INSN (curr_insn);
5011 if (curr_bb != last_bb)
5013 last_bb = curr_bb;
5014 bb_reload_num = lra_curr_reload_num;
5016 if (min_len > new_min_len)
5018 min_len = new_min_len;
5019 new_insns_num = 0;
5021 if (new_insns_num > MAX_RELOAD_INSNS_NUMBER)
5022 internal_error
5023 ("maximum number of generated reload insns per insn achieved (%d)",
5024 MAX_RELOAD_INSNS_NUMBER);
5025 new_insns_num++;
5026 if (DEBUG_INSN_P (curr_insn))
5028 /* We need to check equivalence in debug insn and change
5029 pseudo to the equivalent value if necessary. */
5030 curr_id = lra_get_insn_recog_data (curr_insn);
5031 if (bitmap_bit_p (equiv_insn_bitmap, INSN_UID (curr_insn)))
5033 rtx old = *curr_id->operand_loc[0];
5034 *curr_id->operand_loc[0]
5035 = simplify_replace_fn_rtx (old, NULL_RTX,
5036 loc_equivalence_callback, curr_insn);
5037 if (old != *curr_id->operand_loc[0])
5039 lra_update_insn_regno_info (curr_insn);
5040 changed_p = true;
5044 else if (INSN_P (curr_insn))
5046 if ((set = single_set (curr_insn)) != NULL_RTX)
5048 dest_reg = SET_DEST (set);
5049 /* The equivalence pseudo could be set up as SUBREG in a
5050 case when it is a call restore insn in a mode
5051 different from the pseudo mode. */
5052 if (GET_CODE (dest_reg) == SUBREG)
5053 dest_reg = SUBREG_REG (dest_reg);
5054 if ((REG_P (dest_reg)
5055 && (x = get_equiv (dest_reg)) != dest_reg
5056 /* Remove insns which set up a pseudo whose value
5057 cannot be changed. Such insns might be not in
5058 init_insns because we don't update equiv data
5059 during insn transformations.
5061 As an example, let suppose that a pseudo got
5062 hard register and on the 1st pass was not
5063 changed to equivalent constant. We generate an
5064 additional insn setting up the pseudo because of
5065 secondary memory movement. Then the pseudo is
5066 spilled and we use the equiv constant. In this
5067 case we should remove the additional insn and
5068 this insn is not init_insns list. */
5069 && (! MEM_P (x) || MEM_READONLY_P (x)
5070 /* Check that this is actually an insn setting
5071 up the equivalence. */
5072 || in_list_p (curr_insn,
5073 ira_reg_equiv
5074 [REGNO (dest_reg)].init_insns)))
5075 || (((x = get_equiv (SET_SRC (set))) != SET_SRC (set))
5076 && in_list_p (curr_insn,
5077 ira_reg_equiv
5078 [REGNO (SET_SRC (set))].init_insns)))
5080 /* This is equiv init insn of pseudo which did not get a
5081 hard register -- remove the insn. */
5082 if (lra_dump_file != NULL)
5084 fprintf (lra_dump_file,
5085 " Removing equiv init insn %i (freq=%d)\n",
5086 INSN_UID (curr_insn),
5087 REG_FREQ_FROM_BB (BLOCK_FOR_INSN (curr_insn)));
5088 dump_insn_slim (lra_dump_file, curr_insn);
5090 if (contains_reg_p (x, true, false))
5091 check_and_force_assignment_correctness_p = true;
5092 lra_set_insn_deleted (curr_insn);
5093 continue;
5096 curr_id = lra_get_insn_recog_data (curr_insn);
5097 curr_static_id = curr_id->insn_static_data;
5098 init_curr_insn_input_reloads ();
5099 init_curr_operand_mode ();
5100 if (curr_insn_transform (false))
5101 changed_p = true;
5102 /* Check non-transformed insns too for equiv change as USE
5103 or CLOBBER don't need reloads but can contain pseudos
5104 being changed on their equivalences. */
5105 else if (bitmap_bit_p (equiv_insn_bitmap, INSN_UID (curr_insn))
5106 && loc_equivalence_change_p (&PATTERN (curr_insn)))
5108 lra_update_insn_regno_info (curr_insn);
5109 changed_p = true;
5114 /* If we used a new hard regno, changed_p should be true because the
5115 hard reg is assigned to a new pseudo. */
5116 if (flag_checking && !changed_p)
5118 for (i = FIRST_PSEUDO_REGISTER; i < new_regno_start; i++)
5119 if (lra_reg_info[i].nrefs != 0
5120 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
5122 int j, nregs = hard_regno_nregs (hard_regno,
5123 PSEUDO_REGNO_MODE (i));
5125 for (j = 0; j < nregs; j++)
5126 lra_assert (df_regs_ever_live_p (hard_regno + j));
5129 return changed_p;
5132 static void initiate_invariants (void);
5133 static void finish_invariants (void);
5135 /* Initiate the LRA constraint pass. It is done once per
5136 function. */
5137 void
5138 lra_constraints_init (void)
5140 initiate_invariants ();
5143 /* Finalize the LRA constraint pass. It is done once per
5144 function. */
5145 void
5146 lra_constraints_finish (void)
5148 finish_invariants ();
5153 /* Structure describes invariants for ineheritance. */
5154 struct lra_invariant
5156 /* The order number of the invariant. */
5157 int num;
5158 /* The invariant RTX. */
5159 rtx invariant_rtx;
5160 /* The origin insn of the invariant. */
5161 rtx_insn *insn;
5164 typedef lra_invariant invariant_t;
5165 typedef invariant_t *invariant_ptr_t;
5166 typedef const invariant_t *const_invariant_ptr_t;
5168 /* Pointer to the inheritance invariants. */
5169 static vec<invariant_ptr_t> invariants;
5171 /* Allocation pool for the invariants. */
5172 static object_allocator<lra_invariant> *invariants_pool;
5174 /* Hash table for the invariants. */
5175 static htab_t invariant_table;
5177 /* Hash function for INVARIANT. */
5178 static hashval_t
5179 invariant_hash (const void *invariant)
5181 rtx inv = ((const_invariant_ptr_t) invariant)->invariant_rtx;
5182 return lra_rtx_hash (inv);
5185 /* Equal function for invariants INVARIANT1 and INVARIANT2. */
5186 static int
5187 invariant_eq_p (const void *invariant1, const void *invariant2)
5189 rtx inv1 = ((const_invariant_ptr_t) invariant1)->invariant_rtx;
5190 rtx inv2 = ((const_invariant_ptr_t) invariant2)->invariant_rtx;
5192 return rtx_equal_p (inv1, inv2);
5195 /* Insert INVARIANT_RTX into the table if it is not there yet. Return
5196 invariant which is in the table. */
5197 static invariant_ptr_t
5198 insert_invariant (rtx invariant_rtx)
5200 void **entry_ptr;
5201 invariant_t invariant;
5202 invariant_ptr_t invariant_ptr;
5204 invariant.invariant_rtx = invariant_rtx;
5205 entry_ptr = htab_find_slot (invariant_table, &invariant, INSERT);
5206 if (*entry_ptr == NULL)
5208 invariant_ptr = invariants_pool->allocate ();
5209 invariant_ptr->invariant_rtx = invariant_rtx;
5210 invariant_ptr->insn = NULL;
5211 invariants.safe_push (invariant_ptr);
5212 *entry_ptr = (void *) invariant_ptr;
5214 return (invariant_ptr_t) *entry_ptr;
5217 /* Initiate the invariant table. */
5218 static void
5219 initiate_invariants (void)
5221 invariants.create (100);
5222 invariants_pool
5223 = new object_allocator<lra_invariant> ("Inheritance invariants");
5224 invariant_table = htab_create (100, invariant_hash, invariant_eq_p, NULL);
5227 /* Finish the invariant table. */
5228 static void
5229 finish_invariants (void)
5231 htab_delete (invariant_table);
5232 delete invariants_pool;
5233 invariants.release ();
5236 /* Make the invariant table empty. */
5237 static void
5238 clear_invariants (void)
5240 htab_empty (invariant_table);
5241 invariants_pool->release ();
5242 invariants.truncate (0);
5247 /* This page contains code to do inheritance/split
5248 transformations. */
5250 /* Number of reloads passed so far in current EBB. */
5251 static int reloads_num;
5253 /* Number of calls passed so far in current EBB. */
5254 static int calls_num;
5256 /* Index ID is the CALLS_NUM associated the last call we saw with
5257 ABI identifier ID. */
5258 static int last_call_for_abi[NUM_ABI_IDS];
5260 /* Which registers have been fully or partially clobbered by a call
5261 since they were last used. */
5262 static HARD_REG_SET full_and_partial_call_clobbers;
5264 /* Current reload pseudo check for validity of elements in
5265 USAGE_INSNS. */
5266 static int curr_usage_insns_check;
5268 /* Info about last usage of registers in EBB to do inheritance/split
5269 transformation. Inheritance transformation is done from a spilled
5270 pseudo and split transformations from a hard register or a pseudo
5271 assigned to a hard register. */
5272 struct usage_insns
5274 /* If the value is equal to CURR_USAGE_INSNS_CHECK, then the member
5275 value INSNS is valid. The insns is chain of optional debug insns
5276 and a finishing non-debug insn using the corresponding reg. The
5277 value is also used to mark the registers which are set up in the
5278 current insn. The negated insn uid is used for this. */
5279 int check;
5280 /* Value of global reloads_num at the last insn in INSNS. */
5281 int reloads_num;
5282 /* Value of global reloads_nums at the last insn in INSNS. */
5283 int calls_num;
5284 /* It can be true only for splitting. And it means that the restore
5285 insn should be put after insn given by the following member. */
5286 bool after_p;
5287 /* Next insns in the current EBB which use the original reg and the
5288 original reg value is not changed between the current insn and
5289 the next insns. In order words, e.g. for inheritance, if we need
5290 to use the original reg value again in the next insns we can try
5291 to use the value in a hard register from a reload insn of the
5292 current insn. */
5293 rtx insns;
5296 /* Map: regno -> corresponding pseudo usage insns. */
5297 static struct usage_insns *usage_insns;
5299 static void
5300 setup_next_usage_insn (int regno, rtx insn, int reloads_num, bool after_p)
5302 usage_insns[regno].check = curr_usage_insns_check;
5303 usage_insns[regno].insns = insn;
5304 usage_insns[regno].reloads_num = reloads_num;
5305 usage_insns[regno].calls_num = calls_num;
5306 usage_insns[regno].after_p = after_p;
5307 if (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0)
5308 remove_from_hard_reg_set (&full_and_partial_call_clobbers,
5309 PSEUDO_REGNO_MODE (regno),
5310 reg_renumber[regno]);
5313 /* The function is used to form list REGNO usages which consists of
5314 optional debug insns finished by a non-debug insn using REGNO.
5315 RELOADS_NUM is current number of reload insns processed so far. */
5316 static void
5317 add_next_usage_insn (int regno, rtx_insn *insn, int reloads_num)
5319 rtx next_usage_insns;
5321 if (usage_insns[regno].check == curr_usage_insns_check
5322 && (next_usage_insns = usage_insns[regno].insns) != NULL_RTX
5323 && DEBUG_INSN_P (insn))
5325 /* Check that we did not add the debug insn yet. */
5326 if (next_usage_insns != insn
5327 && (GET_CODE (next_usage_insns) != INSN_LIST
5328 || XEXP (next_usage_insns, 0) != insn))
5329 usage_insns[regno].insns = gen_rtx_INSN_LIST (VOIDmode, insn,
5330 next_usage_insns);
5332 else if (NONDEBUG_INSN_P (insn))
5333 setup_next_usage_insn (regno, insn, reloads_num, false);
5334 else
5335 usage_insns[regno].check = 0;
5338 /* Return first non-debug insn in list USAGE_INSNS. */
5339 static rtx_insn *
5340 skip_usage_debug_insns (rtx usage_insns)
5342 rtx insn;
5344 /* Skip debug insns. */
5345 for (insn = usage_insns;
5346 insn != NULL_RTX && GET_CODE (insn) == INSN_LIST;
5347 insn = XEXP (insn, 1))
5349 return safe_as_a <rtx_insn *> (insn);
5352 /* Return true if we need secondary memory moves for insn in
5353 USAGE_INSNS after inserting inherited pseudo of class INHER_CL
5354 into the insn. */
5355 static bool
5356 check_secondary_memory_needed_p (enum reg_class inher_cl ATTRIBUTE_UNUSED,
5357 rtx usage_insns ATTRIBUTE_UNUSED)
5359 rtx_insn *insn;
5360 rtx set, dest;
5361 enum reg_class cl;
5363 if (inher_cl == ALL_REGS
5364 || (insn = skip_usage_debug_insns (usage_insns)) == NULL_RTX)
5365 return false;
5366 lra_assert (INSN_P (insn));
5367 if ((set = single_set (insn)) == NULL_RTX || ! REG_P (SET_DEST (set)))
5368 return false;
5369 dest = SET_DEST (set);
5370 if (! REG_P (dest))
5371 return false;
5372 lra_assert (inher_cl != NO_REGS);
5373 cl = get_reg_class (REGNO (dest));
5374 return (cl != NO_REGS && cl != ALL_REGS
5375 && targetm.secondary_memory_needed (GET_MODE (dest), inher_cl, cl));
5378 /* Registers involved in inheritance/split in the current EBB
5379 (inheritance/split pseudos and original registers). */
5380 static bitmap_head check_only_regs;
5382 /* Reload pseudos cannot be involded in invariant inheritance in the
5383 current EBB. */
5384 static bitmap_head invalid_invariant_regs;
5386 /* Do inheritance transformations for insn INSN, which defines (if
5387 DEF_P) or uses ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which
5388 instruction in the EBB next uses ORIGINAL_REGNO; it has the same
5389 form as the "insns" field of usage_insns. Return true if we
5390 succeed in such transformation.
5392 The transformations look like:
5394 p <- ... i <- ...
5395 ... p <- i (new insn)
5396 ... =>
5397 <- ... p ... <- ... i ...
5399 ... i <- p (new insn)
5400 <- ... p ... <- ... i ...
5401 ... =>
5402 <- ... p ... <- ... i ...
5403 where p is a spilled original pseudo and i is a new inheritance pseudo.
5406 The inheritance pseudo has the smallest class of two classes CL and
5407 class of ORIGINAL REGNO. */
5408 static bool
5409 inherit_reload_reg (bool def_p, int original_regno,
5410 enum reg_class cl, rtx_insn *insn, rtx next_usage_insns)
5412 if (optimize_function_for_size_p (cfun))
5413 return false;
5415 enum reg_class rclass = lra_get_allocno_class (original_regno);
5416 rtx original_reg = regno_reg_rtx[original_regno];
5417 rtx new_reg, usage_insn;
5418 rtx_insn *new_insns;
5420 lra_assert (! usage_insns[original_regno].after_p);
5421 if (lra_dump_file != NULL)
5422 fprintf (lra_dump_file,
5423 " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
5424 if (! ira_reg_classes_intersect_p[cl][rclass])
5426 if (lra_dump_file != NULL)
5428 fprintf (lra_dump_file,
5429 " Rejecting inheritance for %d "
5430 "because of disjoint classes %s and %s\n",
5431 original_regno, reg_class_names[cl],
5432 reg_class_names[rclass]);
5433 fprintf (lra_dump_file,
5434 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5436 return false;
5438 if ((ira_class_subset_p[cl][rclass] && cl != rclass)
5439 /* We don't use a subset of two classes because it can be
5440 NO_REGS. This transformation is still profitable in most
5441 cases even if the classes are not intersected as register
5442 move is probably cheaper than a memory load. */
5443 || ira_class_hard_regs_num[cl] < ira_class_hard_regs_num[rclass])
5445 if (lra_dump_file != NULL)
5446 fprintf (lra_dump_file, " Use smallest class of %s and %s\n",
5447 reg_class_names[cl], reg_class_names[rclass]);
5449 rclass = cl;
5451 if (check_secondary_memory_needed_p (rclass, next_usage_insns))
5453 /* Reject inheritance resulting in secondary memory moves.
5454 Otherwise, there is a danger in LRA cycling. Also such
5455 transformation will be unprofitable. */
5456 if (lra_dump_file != NULL)
5458 rtx_insn *insn = skip_usage_debug_insns (next_usage_insns);
5459 rtx set = single_set (insn);
5461 lra_assert (set != NULL_RTX);
5463 rtx dest = SET_DEST (set);
5465 lra_assert (REG_P (dest));
5466 fprintf (lra_dump_file,
5467 " Rejecting inheritance for insn %d(%s)<-%d(%s) "
5468 "as secondary mem is needed\n",
5469 REGNO (dest), reg_class_names[get_reg_class (REGNO (dest))],
5470 original_regno, reg_class_names[rclass]);
5471 fprintf (lra_dump_file,
5472 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5474 return false;
5476 new_reg = lra_create_new_reg (GET_MODE (original_reg), original_reg,
5477 rclass, "inheritance");
5478 start_sequence ();
5479 if (def_p)
5480 lra_emit_move (original_reg, new_reg);
5481 else
5482 lra_emit_move (new_reg, original_reg);
5483 new_insns = get_insns ();
5484 end_sequence ();
5485 if (NEXT_INSN (new_insns) != NULL_RTX)
5487 if (lra_dump_file != NULL)
5489 fprintf (lra_dump_file,
5490 " Rejecting inheritance %d->%d "
5491 "as it results in 2 or more insns:\n",
5492 original_regno, REGNO (new_reg));
5493 dump_rtl_slim (lra_dump_file, new_insns, NULL, -1, 0);
5494 fprintf (lra_dump_file,
5495 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5497 return false;
5499 lra_substitute_pseudo_within_insn (insn, original_regno, new_reg, false);
5500 lra_update_insn_regno_info (insn);
5501 if (! def_p)
5502 /* We now have a new usage insn for original regno. */
5503 setup_next_usage_insn (original_regno, new_insns, reloads_num, false);
5504 if (lra_dump_file != NULL)
5505 fprintf (lra_dump_file, " Original reg change %d->%d (bb%d):\n",
5506 original_regno, REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
5507 lra_reg_info[REGNO (new_reg)].restore_rtx = regno_reg_rtx[original_regno];
5508 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
5509 bitmap_set_bit (&check_only_regs, original_regno);
5510 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
5511 if (def_p)
5512 lra_process_new_insns (insn, NULL, new_insns,
5513 "Add original<-inheritance");
5514 else
5515 lra_process_new_insns (insn, new_insns, NULL,
5516 "Add inheritance<-original");
5517 while (next_usage_insns != NULL_RTX)
5519 if (GET_CODE (next_usage_insns) != INSN_LIST)
5521 usage_insn = next_usage_insns;
5522 lra_assert (NONDEBUG_INSN_P (usage_insn));
5523 next_usage_insns = NULL;
5525 else
5527 usage_insn = XEXP (next_usage_insns, 0);
5528 lra_assert (DEBUG_INSN_P (usage_insn));
5529 next_usage_insns = XEXP (next_usage_insns, 1);
5531 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false,
5532 DEBUG_INSN_P (usage_insn));
5533 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5534 if (lra_dump_file != NULL)
5536 basic_block bb = BLOCK_FOR_INSN (usage_insn);
5537 fprintf (lra_dump_file,
5538 " Inheritance reuse change %d->%d (bb%d):\n",
5539 original_regno, REGNO (new_reg),
5540 bb ? bb->index : -1);
5541 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5544 if (lra_dump_file != NULL)
5545 fprintf (lra_dump_file,
5546 " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
5547 return true;
5550 /* Return true if we need a caller save/restore for pseudo REGNO which
5551 was assigned to a hard register. */
5552 static inline bool
5553 need_for_call_save_p (int regno)
5555 lra_assert (regno >= FIRST_PSEUDO_REGISTER && reg_renumber[regno] >= 0);
5556 if (usage_insns[regno].calls_num < calls_num)
5558 unsigned int abis = 0;
5559 for (unsigned int i = 0; i < NUM_ABI_IDS; ++i)
5560 if (last_call_for_abi[i] > usage_insns[regno].calls_num)
5561 abis |= 1 << i;
5562 gcc_assert (abis);
5563 if (call_clobbered_in_region_p (abis, full_and_partial_call_clobbers,
5564 PSEUDO_REGNO_MODE (regno),
5565 reg_renumber[regno]))
5566 return true;
5568 return false;
5571 /* Global registers occurring in the current EBB. */
5572 static bitmap_head ebb_global_regs;
5574 /* Return true if we need a split for hard register REGNO or pseudo
5575 REGNO which was assigned to a hard register.
5576 POTENTIAL_RELOAD_HARD_REGS contains hard registers which might be
5577 used for reloads since the EBB end. It is an approximation of the
5578 used hard registers in the split range. The exact value would
5579 require expensive calculations. If we were aggressive with
5580 splitting because of the approximation, the split pseudo will save
5581 the same hard register assignment and will be removed in the undo
5582 pass. We still need the approximation because too aggressive
5583 splitting would result in too inaccurate cost calculation in the
5584 assignment pass because of too many generated moves which will be
5585 probably removed in the undo pass. */
5586 static inline bool
5587 need_for_split_p (HARD_REG_SET potential_reload_hard_regs, int regno)
5589 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
5591 lra_assert (hard_regno >= 0);
5592 return ((TEST_HARD_REG_BIT (potential_reload_hard_regs, hard_regno)
5593 /* Don't split eliminable hard registers, otherwise we can
5594 split hard registers like hard frame pointer, which
5595 lives on BB start/end according to DF-infrastructure,
5596 when there is a pseudo assigned to the register and
5597 living in the same BB. */
5598 && (regno >= FIRST_PSEUDO_REGISTER
5599 || ! TEST_HARD_REG_BIT (eliminable_regset, hard_regno))
5600 && ! TEST_HARD_REG_BIT (lra_no_alloc_regs, hard_regno)
5601 /* Don't split call clobbered hard regs living through
5602 calls, otherwise we might have a check problem in the
5603 assign sub-pass as in the most cases (exception is a
5604 situation when check_and_force_assignment_correctness_p value is
5605 true) the assign pass assumes that all pseudos living
5606 through calls are assigned to call saved hard regs. */
5607 && (regno >= FIRST_PSEUDO_REGISTER
5608 || !TEST_HARD_REG_BIT (full_and_partial_call_clobbers, regno))
5609 /* We need at least 2 reloads to make pseudo splitting
5610 profitable. We should provide hard regno splitting in
5611 any case to solve 1st insn scheduling problem when
5612 moving hard register definition up might result in
5613 impossibility to find hard register for reload pseudo of
5614 small register class. */
5615 && (usage_insns[regno].reloads_num
5616 + (regno < FIRST_PSEUDO_REGISTER ? 0 : 3) < reloads_num)
5617 && (regno < FIRST_PSEUDO_REGISTER
5618 /* For short living pseudos, spilling + inheritance can
5619 be considered a substitution for splitting.
5620 Therefore we do not splitting for local pseudos. It
5621 decreases also aggressiveness of splitting. The
5622 minimal number of references is chosen taking into
5623 account that for 2 references splitting has no sense
5624 as we can just spill the pseudo. */
5625 || (regno >= FIRST_PSEUDO_REGISTER
5626 && lra_reg_info[regno].nrefs > 3
5627 && bitmap_bit_p (&ebb_global_regs, regno))))
5628 || (regno >= FIRST_PSEUDO_REGISTER && need_for_call_save_p (regno)));
5631 /* Return class for the split pseudo created from original pseudo with
5632 ALLOCNO_CLASS and MODE which got a hard register HARD_REGNO. We
5633 choose subclass of ALLOCNO_CLASS which contains HARD_REGNO and
5634 results in no secondary memory movements. */
5635 static enum reg_class
5636 choose_split_class (enum reg_class allocno_class,
5637 int hard_regno ATTRIBUTE_UNUSED,
5638 machine_mode mode ATTRIBUTE_UNUSED)
5640 int i;
5641 enum reg_class cl, best_cl = NO_REGS;
5642 enum reg_class hard_reg_class ATTRIBUTE_UNUSED
5643 = REGNO_REG_CLASS (hard_regno);
5645 if (! targetm.secondary_memory_needed (mode, allocno_class, allocno_class)
5646 && TEST_HARD_REG_BIT (reg_class_contents[allocno_class], hard_regno))
5647 return allocno_class;
5648 for (i = 0;
5649 (cl = reg_class_subclasses[allocno_class][i]) != LIM_REG_CLASSES;
5650 i++)
5651 if (! targetm.secondary_memory_needed (mode, cl, hard_reg_class)
5652 && ! targetm.secondary_memory_needed (mode, hard_reg_class, cl)
5653 && TEST_HARD_REG_BIT (reg_class_contents[cl], hard_regno)
5654 && (best_cl == NO_REGS
5655 || ira_class_hard_regs_num[best_cl] < ira_class_hard_regs_num[cl]))
5656 best_cl = cl;
5657 return best_cl;
5660 /* Copy any equivalence information from ORIGINAL_REGNO to NEW_REGNO.
5661 It only makes sense to call this function if NEW_REGNO is always
5662 equal to ORIGINAL_REGNO. */
5664 static void
5665 lra_copy_reg_equiv (unsigned int new_regno, unsigned int original_regno)
5667 if (!ira_reg_equiv[original_regno].defined_p)
5668 return;
5670 ira_expand_reg_equiv ();
5671 ira_reg_equiv[new_regno].defined_p = true;
5672 if (ira_reg_equiv[original_regno].memory)
5673 ira_reg_equiv[new_regno].memory
5674 = copy_rtx (ira_reg_equiv[original_regno].memory);
5675 if (ira_reg_equiv[original_regno].constant)
5676 ira_reg_equiv[new_regno].constant
5677 = copy_rtx (ira_reg_equiv[original_regno].constant);
5678 if (ira_reg_equiv[original_regno].invariant)
5679 ira_reg_equiv[new_regno].invariant
5680 = copy_rtx (ira_reg_equiv[original_regno].invariant);
5683 /* Do split transformations for insn INSN, which defines or uses
5684 ORIGINAL_REGNO. NEXT_USAGE_INSNS specifies which instruction in
5685 the EBB next uses ORIGINAL_REGNO; it has the same form as the
5686 "insns" field of usage_insns. If TO is not NULL, we don't use
5687 usage_insns, we put restore insns after TO insn. It is a case when
5688 we call it from lra_split_hard_reg_for, outside the inheritance
5689 pass.
5691 The transformations look like:
5693 p <- ... p <- ...
5694 ... s <- p (new insn -- save)
5695 ... =>
5696 ... p <- s (new insn -- restore)
5697 <- ... p ... <- ... p ...
5699 <- ... p ... <- ... p ...
5700 ... s <- p (new insn -- save)
5701 ... =>
5702 ... p <- s (new insn -- restore)
5703 <- ... p ... <- ... p ...
5705 where p is an original pseudo got a hard register or a hard
5706 register and s is a new split pseudo. The save is put before INSN
5707 if BEFORE_P is true. Return true if we succeed in such
5708 transformation. */
5709 static bool
5710 split_reg (bool before_p, int original_regno, rtx_insn *insn,
5711 rtx next_usage_insns, rtx_insn *to)
5713 enum reg_class rclass;
5714 rtx original_reg;
5715 int hard_regno, nregs;
5716 rtx new_reg, usage_insn;
5717 rtx_insn *restore, *save;
5718 bool after_p;
5719 bool call_save_p;
5720 machine_mode mode;
5722 if (original_regno < FIRST_PSEUDO_REGISTER)
5724 rclass = ira_allocno_class_translate[REGNO_REG_CLASS (original_regno)];
5725 hard_regno = original_regno;
5726 call_save_p = false;
5727 nregs = 1;
5728 mode = lra_reg_info[hard_regno].biggest_mode;
5729 machine_mode reg_rtx_mode = GET_MODE (regno_reg_rtx[hard_regno]);
5730 /* A reg can have a biggest_mode of VOIDmode if it was only ever seen
5731 as part of a multi-word register. In that case, or if the biggest
5732 mode was larger than a register, just use the reg_rtx. Otherwise,
5733 limit the size to that of the biggest access in the function. */
5734 if (mode == VOIDmode
5735 || paradoxical_subreg_p (mode, reg_rtx_mode))
5737 original_reg = regno_reg_rtx[hard_regno];
5738 mode = reg_rtx_mode;
5740 else
5741 original_reg = gen_rtx_REG (mode, hard_regno);
5743 else
5745 mode = PSEUDO_REGNO_MODE (original_regno);
5746 hard_regno = reg_renumber[original_regno];
5747 nregs = hard_regno_nregs (hard_regno, mode);
5748 rclass = lra_get_allocno_class (original_regno);
5749 original_reg = regno_reg_rtx[original_regno];
5750 call_save_p = need_for_call_save_p (original_regno);
5752 lra_assert (hard_regno >= 0);
5753 if (lra_dump_file != NULL)
5754 fprintf (lra_dump_file,
5755 " ((((((((((((((((((((((((((((((((((((((((((((((((\n");
5757 if (call_save_p)
5759 mode = HARD_REGNO_CALLER_SAVE_MODE (hard_regno,
5760 hard_regno_nregs (hard_regno, mode),
5761 mode);
5762 new_reg = lra_create_new_reg (mode, NULL_RTX, NO_REGS, "save");
5764 else
5766 rclass = choose_split_class (rclass, hard_regno, mode);
5767 if (rclass == NO_REGS)
5769 if (lra_dump_file != NULL)
5771 fprintf (lra_dump_file,
5772 " Rejecting split of %d(%s): "
5773 "no good reg class for %d(%s)\n",
5774 original_regno,
5775 reg_class_names[lra_get_allocno_class (original_regno)],
5776 hard_regno,
5777 reg_class_names[REGNO_REG_CLASS (hard_regno)]);
5778 fprintf
5779 (lra_dump_file,
5780 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5782 return false;
5784 /* Split_if_necessary can split hard registers used as part of a
5785 multi-register mode but splits each register individually. The
5786 mode used for each independent register may not be supported
5787 so reject the split. Splitting the wider mode should theoretically
5788 be possible but is not implemented. */
5789 if (!targetm.hard_regno_mode_ok (hard_regno, mode))
5791 if (lra_dump_file != NULL)
5793 fprintf (lra_dump_file,
5794 " Rejecting split of %d(%s): unsuitable mode %s\n",
5795 original_regno,
5796 reg_class_names[lra_get_allocno_class (original_regno)],
5797 GET_MODE_NAME (mode));
5798 fprintf
5799 (lra_dump_file,
5800 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5802 return false;
5804 new_reg = lra_create_new_reg (mode, original_reg, rclass, "split");
5805 reg_renumber[REGNO (new_reg)] = hard_regno;
5807 int new_regno = REGNO (new_reg);
5808 save = emit_spill_move (true, new_reg, original_reg);
5809 if (NEXT_INSN (save) != NULL_RTX && !call_save_p)
5811 if (lra_dump_file != NULL)
5813 fprintf
5814 (lra_dump_file,
5815 " Rejecting split %d->%d resulting in > 2 save insns:\n",
5816 original_regno, new_regno);
5817 dump_rtl_slim (lra_dump_file, save, NULL, -1, 0);
5818 fprintf (lra_dump_file,
5819 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5821 return false;
5823 restore = emit_spill_move (false, new_reg, original_reg);
5824 if (NEXT_INSN (restore) != NULL_RTX && !call_save_p)
5826 if (lra_dump_file != NULL)
5828 fprintf (lra_dump_file,
5829 " Rejecting split %d->%d "
5830 "resulting in > 2 restore insns:\n",
5831 original_regno, new_regno);
5832 dump_rtl_slim (lra_dump_file, restore, NULL, -1, 0);
5833 fprintf (lra_dump_file,
5834 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5836 return false;
5838 /* Transfer equivalence information to the spill register, so that
5839 if we fail to allocate the spill register, we have the option of
5840 rematerializing the original value instead of spilling to the stack. */
5841 if (!HARD_REGISTER_NUM_P (original_regno)
5842 && mode == PSEUDO_REGNO_MODE (original_regno))
5843 lra_copy_reg_equiv (new_regno, original_regno);
5844 lra_reg_info[new_regno].restore_rtx = regno_reg_rtx[original_regno];
5845 bitmap_set_bit (&lra_split_regs, new_regno);
5846 if (to != NULL)
5848 lra_assert (next_usage_insns == NULL);
5849 usage_insn = to;
5850 after_p = TRUE;
5852 else
5854 /* We need check_only_regs only inside the inheritance pass. */
5855 bitmap_set_bit (&check_only_regs, new_regno);
5856 bitmap_set_bit (&check_only_regs, original_regno);
5857 after_p = usage_insns[original_regno].after_p;
5858 for (;;)
5860 if (GET_CODE (next_usage_insns) != INSN_LIST)
5862 usage_insn = next_usage_insns;
5863 break;
5865 usage_insn = XEXP (next_usage_insns, 0);
5866 lra_assert (DEBUG_INSN_P (usage_insn));
5867 next_usage_insns = XEXP (next_usage_insns, 1);
5868 lra_substitute_pseudo (&usage_insn, original_regno, new_reg, false,
5869 true);
5870 lra_update_insn_regno_info (as_a <rtx_insn *> (usage_insn));
5871 if (lra_dump_file != NULL)
5873 fprintf (lra_dump_file, " Split reuse change %d->%d:\n",
5874 original_regno, new_regno);
5875 dump_insn_slim (lra_dump_file, as_a <rtx_insn *> (usage_insn));
5879 lra_assert (NOTE_P (usage_insn) || NONDEBUG_INSN_P (usage_insn));
5880 lra_assert (usage_insn != insn || (after_p && before_p));
5881 lra_process_new_insns (as_a <rtx_insn *> (usage_insn),
5882 after_p ? NULL : restore,
5883 after_p ? restore : NULL,
5884 call_save_p
5885 ? "Add reg<-save" : "Add reg<-split");
5886 lra_process_new_insns (insn, before_p ? save : NULL,
5887 before_p ? NULL : save,
5888 call_save_p
5889 ? "Add save<-reg" : "Add split<-reg");
5890 if (nregs > 1)
5891 /* If we are trying to split multi-register. We should check
5892 conflicts on the next assignment sub-pass. IRA can allocate on
5893 sub-register levels, LRA do this on pseudos level right now and
5894 this discrepancy may create allocation conflicts after
5895 splitting. */
5896 check_and_force_assignment_correctness_p = true;
5897 if (lra_dump_file != NULL)
5898 fprintf (lra_dump_file,
5899 " ))))))))))))))))))))))))))))))))))))))))))))))))\n");
5900 return true;
5903 /* Split a hard reg for reload pseudo REGNO having RCLASS and living
5904 in the range [FROM, TO]. Return true if did a split. Otherwise,
5905 return false. */
5906 bool
5907 spill_hard_reg_in_range (int regno, enum reg_class rclass, rtx_insn *from, rtx_insn *to)
5909 int i, hard_regno;
5910 int rclass_size;
5911 rtx_insn *insn;
5912 unsigned int uid;
5913 bitmap_iterator bi;
5914 HARD_REG_SET ignore;
5916 lra_assert (from != NULL && to != NULL);
5917 CLEAR_HARD_REG_SET (ignore);
5918 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
5920 lra_insn_recog_data_t id = lra_insn_recog_data[uid];
5921 struct lra_static_insn_data *static_id = id->insn_static_data;
5922 struct lra_insn_reg *reg;
5924 for (reg = id->regs; reg != NULL; reg = reg->next)
5925 if (reg->regno < FIRST_PSEUDO_REGISTER)
5926 SET_HARD_REG_BIT (ignore, reg->regno);
5927 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
5928 SET_HARD_REG_BIT (ignore, reg->regno);
5930 rclass_size = ira_class_hard_regs_num[rclass];
5931 for (i = 0; i < rclass_size; i++)
5933 hard_regno = ira_class_hard_regs[rclass][i];
5934 if (! TEST_HARD_REG_BIT (lra_reg_info[regno].conflict_hard_regs, hard_regno)
5935 || TEST_HARD_REG_BIT (ignore, hard_regno))
5936 continue;
5937 for (insn = from; insn != NEXT_INSN (to); insn = NEXT_INSN (insn))
5939 struct lra_static_insn_data *static_id;
5940 struct lra_insn_reg *reg;
5942 if (!INSN_P (insn))
5943 continue;
5944 if (bitmap_bit_p (&lra_reg_info[hard_regno].insn_bitmap,
5945 INSN_UID (insn)))
5946 break;
5947 static_id = lra_get_insn_recog_data (insn)->insn_static_data;
5948 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
5949 if (reg->regno == hard_regno)
5950 break;
5951 if (reg != NULL)
5952 break;
5954 if (insn != NEXT_INSN (to))
5955 continue;
5956 if (split_reg (TRUE, hard_regno, from, NULL, to))
5957 return true;
5959 return false;
5962 /* Recognize that we need a split transformation for insn INSN, which
5963 defines or uses REGNO in its insn biggest MODE (we use it only if
5964 REGNO is a hard register). POTENTIAL_RELOAD_HARD_REGS contains
5965 hard registers which might be used for reloads since the EBB end.
5966 Put the save before INSN if BEFORE_P is true. MAX_UID is maximla
5967 uid before starting INSN processing. Return true if we succeed in
5968 such transformation. */
5969 static bool
5970 split_if_necessary (int regno, machine_mode mode,
5971 HARD_REG_SET potential_reload_hard_regs,
5972 bool before_p, rtx_insn *insn, int max_uid)
5974 bool res = false;
5975 int i, nregs = 1;
5976 rtx next_usage_insns;
5978 if (regno < FIRST_PSEUDO_REGISTER)
5979 nregs = hard_regno_nregs (regno, mode);
5980 for (i = 0; i < nregs; i++)
5981 if (usage_insns[regno + i].check == curr_usage_insns_check
5982 && (next_usage_insns = usage_insns[regno + i].insns) != NULL_RTX
5983 /* To avoid processing the register twice or more. */
5984 && ((GET_CODE (next_usage_insns) != INSN_LIST
5985 && INSN_UID (next_usage_insns) < max_uid)
5986 || (GET_CODE (next_usage_insns) == INSN_LIST
5987 && (INSN_UID (XEXP (next_usage_insns, 0)) < max_uid)))
5988 && need_for_split_p (potential_reload_hard_regs, regno + i)
5989 && split_reg (before_p, regno + i, insn, next_usage_insns, NULL))
5990 res = true;
5991 return res;
5994 /* Return TRUE if rtx X is considered as an invariant for
5995 inheritance. */
5996 static bool
5997 invariant_p (const_rtx x)
5999 machine_mode mode;
6000 const char *fmt;
6001 enum rtx_code code;
6002 int i, j;
6004 if (side_effects_p (x))
6005 return false;
6007 code = GET_CODE (x);
6008 mode = GET_MODE (x);
6009 if (code == SUBREG)
6011 x = SUBREG_REG (x);
6012 code = GET_CODE (x);
6013 mode = wider_subreg_mode (mode, GET_MODE (x));
6016 if (MEM_P (x))
6017 return false;
6019 if (REG_P (x))
6021 int i, nregs, regno = REGNO (x);
6023 if (regno >= FIRST_PSEUDO_REGISTER || regno == STACK_POINTER_REGNUM
6024 || TEST_HARD_REG_BIT (eliminable_regset, regno)
6025 || GET_MODE_CLASS (GET_MODE (x)) == MODE_CC)
6026 return false;
6027 nregs = hard_regno_nregs (regno, mode);
6028 for (i = 0; i < nregs; i++)
6029 if (! fixed_regs[regno + i]
6030 /* A hard register may be clobbered in the current insn
6031 but we can ignore this case because if the hard
6032 register is used it should be set somewhere after the
6033 clobber. */
6034 || bitmap_bit_p (&invalid_invariant_regs, regno + i))
6035 return false;
6037 fmt = GET_RTX_FORMAT (code);
6038 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6040 if (fmt[i] == 'e')
6042 if (! invariant_p (XEXP (x, i)))
6043 return false;
6045 else if (fmt[i] == 'E')
6047 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6048 if (! invariant_p (XVECEXP (x, i, j)))
6049 return false;
6052 return true;
6055 /* We have 'dest_reg <- invariant'. Let us try to make an invariant
6056 inheritance transformation (using dest_reg instead invariant in a
6057 subsequent insn). */
6058 static bool
6059 process_invariant_for_inheritance (rtx dst_reg, rtx invariant_rtx)
6061 invariant_ptr_t invariant_ptr;
6062 rtx_insn *insn, *new_insns;
6063 rtx insn_set, insn_reg, new_reg;
6064 int insn_regno;
6065 bool succ_p = false;
6066 int dst_regno = REGNO (dst_reg);
6067 machine_mode dst_mode = GET_MODE (dst_reg);
6068 enum reg_class cl = lra_get_allocno_class (dst_regno), insn_reg_cl;
6070 invariant_ptr = insert_invariant (invariant_rtx);
6071 if ((insn = invariant_ptr->insn) != NULL_RTX)
6073 /* We have a subsequent insn using the invariant. */
6074 insn_set = single_set (insn);
6075 lra_assert (insn_set != NULL);
6076 insn_reg = SET_DEST (insn_set);
6077 lra_assert (REG_P (insn_reg));
6078 insn_regno = REGNO (insn_reg);
6079 insn_reg_cl = lra_get_allocno_class (insn_regno);
6081 if (dst_mode == GET_MODE (insn_reg)
6082 /* We should consider only result move reg insns which are
6083 cheap. */
6084 && targetm.register_move_cost (dst_mode, cl, insn_reg_cl) == 2
6085 && targetm.register_move_cost (dst_mode, cl, cl) == 2)
6087 if (lra_dump_file != NULL)
6088 fprintf (lra_dump_file,
6089 " [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[\n");
6090 new_reg = lra_create_new_reg (dst_mode, dst_reg,
6091 cl, "invariant inheritance");
6092 bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
6093 bitmap_set_bit (&check_only_regs, REGNO (new_reg));
6094 lra_reg_info[REGNO (new_reg)].restore_rtx = PATTERN (insn);
6095 start_sequence ();
6096 lra_emit_move (new_reg, dst_reg);
6097 new_insns = get_insns ();
6098 end_sequence ();
6099 lra_process_new_insns (curr_insn, NULL, new_insns,
6100 "Add invariant inheritance<-original");
6101 start_sequence ();
6102 lra_emit_move (SET_DEST (insn_set), new_reg);
6103 new_insns = get_insns ();
6104 end_sequence ();
6105 lra_process_new_insns (insn, NULL, new_insns,
6106 "Changing reload<-inheritance");
6107 lra_set_insn_deleted (insn);
6108 succ_p = true;
6109 if (lra_dump_file != NULL)
6111 fprintf (lra_dump_file,
6112 " Invariant inheritance reuse change %d (bb%d):\n",
6113 REGNO (new_reg), BLOCK_FOR_INSN (insn)->index);
6114 dump_insn_slim (lra_dump_file, insn);
6115 fprintf (lra_dump_file,
6116 " ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]\n");
6120 invariant_ptr->insn = curr_insn;
6121 return succ_p;
6124 /* Check only registers living at the current program point in the
6125 current EBB. */
6126 static bitmap_head live_regs;
6128 /* Update live info in EBB given by its HEAD and TAIL insns after
6129 inheritance/split transformation. The function removes dead moves
6130 too. */
6131 static void
6132 update_ebb_live_info (rtx_insn *head, rtx_insn *tail)
6134 unsigned int j;
6135 int i, regno;
6136 bool live_p;
6137 rtx_insn *prev_insn;
6138 rtx set;
6139 bool remove_p;
6140 basic_block last_bb, prev_bb, curr_bb;
6141 bitmap_iterator bi;
6142 struct lra_insn_reg *reg;
6143 edge e;
6144 edge_iterator ei;
6146 last_bb = BLOCK_FOR_INSN (tail);
6147 prev_bb = NULL;
6148 for (curr_insn = tail;
6149 curr_insn != PREV_INSN (head);
6150 curr_insn = prev_insn)
6152 prev_insn = PREV_INSN (curr_insn);
6153 /* We need to process empty blocks too. They contain
6154 NOTE_INSN_BASIC_BLOCK referring for the basic block. */
6155 if (NOTE_P (curr_insn) && NOTE_KIND (curr_insn) != NOTE_INSN_BASIC_BLOCK)
6156 continue;
6157 curr_bb = BLOCK_FOR_INSN (curr_insn);
6158 if (curr_bb != prev_bb)
6160 if (prev_bb != NULL)
6162 /* Update df_get_live_in (prev_bb): */
6163 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
6164 if (bitmap_bit_p (&live_regs, j))
6165 bitmap_set_bit (df_get_live_in (prev_bb), j);
6166 else
6167 bitmap_clear_bit (df_get_live_in (prev_bb), j);
6169 if (curr_bb != last_bb)
6171 /* Update df_get_live_out (curr_bb): */
6172 EXECUTE_IF_SET_IN_BITMAP (&check_only_regs, 0, j, bi)
6174 live_p = bitmap_bit_p (&live_regs, j);
6175 if (! live_p)
6176 FOR_EACH_EDGE (e, ei, curr_bb->succs)
6177 if (bitmap_bit_p (df_get_live_in (e->dest), j))
6179 live_p = true;
6180 break;
6182 if (live_p)
6183 bitmap_set_bit (df_get_live_out (curr_bb), j);
6184 else
6185 bitmap_clear_bit (df_get_live_out (curr_bb), j);
6188 prev_bb = curr_bb;
6189 bitmap_and (&live_regs, &check_only_regs, df_get_live_out (curr_bb));
6191 if (! NONDEBUG_INSN_P (curr_insn))
6192 continue;
6193 curr_id = lra_get_insn_recog_data (curr_insn);
6194 curr_static_id = curr_id->insn_static_data;
6195 remove_p = false;
6196 if ((set = single_set (curr_insn)) != NULL_RTX
6197 && REG_P (SET_DEST (set))
6198 && (regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
6199 && SET_DEST (set) != pic_offset_table_rtx
6200 && bitmap_bit_p (&check_only_regs, regno)
6201 && ! bitmap_bit_p (&live_regs, regno))
6202 remove_p = true;
6203 /* See which defined values die here. */
6204 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6205 if (reg->type == OP_OUT && ! reg->subreg_p)
6206 bitmap_clear_bit (&live_regs, reg->regno);
6207 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6208 if (reg->type == OP_OUT && ! reg->subreg_p)
6209 bitmap_clear_bit (&live_regs, reg->regno);
6210 if (curr_id->arg_hard_regs != NULL)
6211 /* Make clobbered argument hard registers die. */
6212 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6213 if (regno >= FIRST_PSEUDO_REGISTER)
6214 bitmap_clear_bit (&live_regs, regno - FIRST_PSEUDO_REGISTER);
6215 /* Mark each used value as live. */
6216 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6217 if (reg->type != OP_OUT
6218 && bitmap_bit_p (&check_only_regs, reg->regno))
6219 bitmap_set_bit (&live_regs, reg->regno);
6220 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6221 if (reg->type != OP_OUT
6222 && bitmap_bit_p (&check_only_regs, reg->regno))
6223 bitmap_set_bit (&live_regs, reg->regno);
6224 if (curr_id->arg_hard_regs != NULL)
6225 /* Make used argument hard registers live. */
6226 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6227 if (regno < FIRST_PSEUDO_REGISTER
6228 && bitmap_bit_p (&check_only_regs, regno))
6229 bitmap_set_bit (&live_regs, regno);
6230 /* It is quite important to remove dead move insns because it
6231 means removing dead store. We don't need to process them for
6232 constraints. */
6233 if (remove_p)
6235 if (lra_dump_file != NULL)
6237 fprintf (lra_dump_file, " Removing dead insn:\n ");
6238 dump_insn_slim (lra_dump_file, curr_insn);
6240 lra_set_insn_deleted (curr_insn);
6245 /* The structure describes info to do an inheritance for the current
6246 insn. We need to collect such info first before doing the
6247 transformations because the transformations change the insn
6248 internal representation. */
6249 struct to_inherit
6251 /* Original regno. */
6252 int regno;
6253 /* Subsequent insns which can inherit original reg value. */
6254 rtx insns;
6257 /* Array containing all info for doing inheritance from the current
6258 insn. */
6259 static struct to_inherit to_inherit[LRA_MAX_INSN_RELOADS];
6261 /* Number elements in the previous array. */
6262 static int to_inherit_num;
6264 /* Add inheritance info REGNO and INSNS. Their meaning is described in
6265 structure to_inherit. */
6266 static void
6267 add_to_inherit (int regno, rtx insns)
6269 int i;
6271 for (i = 0; i < to_inherit_num; i++)
6272 if (to_inherit[i].regno == regno)
6273 return;
6274 lra_assert (to_inherit_num < LRA_MAX_INSN_RELOADS);
6275 to_inherit[to_inherit_num].regno = regno;
6276 to_inherit[to_inherit_num++].insns = insns;
6279 /* Return the last non-debug insn in basic block BB, or the block begin
6280 note if none. */
6281 static rtx_insn *
6282 get_last_insertion_point (basic_block bb)
6284 rtx_insn *insn;
6286 FOR_BB_INSNS_REVERSE (bb, insn)
6287 if (NONDEBUG_INSN_P (insn) || NOTE_INSN_BASIC_BLOCK_P (insn))
6288 return insn;
6289 gcc_unreachable ();
6292 /* Set up RES by registers living on edges FROM except the edge (FROM,
6293 TO) or by registers set up in a jump insn in BB FROM. */
6294 static void
6295 get_live_on_other_edges (basic_block from, basic_block to, bitmap res)
6297 rtx_insn *last;
6298 struct lra_insn_reg *reg;
6299 edge e;
6300 edge_iterator ei;
6302 lra_assert (to != NULL);
6303 bitmap_clear (res);
6304 FOR_EACH_EDGE (e, ei, from->succs)
6305 if (e->dest != to)
6306 bitmap_ior_into (res, df_get_live_in (e->dest));
6307 last = get_last_insertion_point (from);
6308 if (! JUMP_P (last))
6309 return;
6310 curr_id = lra_get_insn_recog_data (last);
6311 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6312 if (reg->type != OP_IN)
6313 bitmap_set_bit (res, reg->regno);
6316 /* Used as a temporary results of some bitmap calculations. */
6317 static bitmap_head temp_bitmap;
6319 /* We split for reloads of small class of hard regs. The following
6320 defines how many hard regs the class should have to be qualified as
6321 small. The code is mostly oriented to x86/x86-64 architecture
6322 where some insns need to use only specific register or pair of
6323 registers and these register can live in RTL explicitly, e.g. for
6324 parameter passing. */
6325 static const int max_small_class_regs_num = 2;
6327 /* Do inheritance/split transformations in EBB starting with HEAD and
6328 finishing on TAIL. We process EBB insns in the reverse order.
6329 Return true if we did any inheritance/split transformation in the
6330 EBB.
6332 We should avoid excessive splitting which results in worse code
6333 because of inaccurate cost calculations for spilling new split
6334 pseudos in such case. To achieve this we do splitting only if
6335 register pressure is high in given basic block and there are reload
6336 pseudos requiring hard registers. We could do more register
6337 pressure calculations at any given program point to avoid necessary
6338 splitting even more but it is to expensive and the current approach
6339 works well enough. */
6340 static bool
6341 inherit_in_ebb (rtx_insn *head, rtx_insn *tail)
6343 int i, src_regno, dst_regno, nregs;
6344 bool change_p, succ_p, update_reloads_num_p;
6345 rtx_insn *prev_insn, *last_insn;
6346 rtx next_usage_insns, curr_set;
6347 enum reg_class cl;
6348 struct lra_insn_reg *reg;
6349 basic_block last_processed_bb, curr_bb = NULL;
6350 HARD_REG_SET potential_reload_hard_regs, live_hard_regs;
6351 bitmap to_process;
6352 unsigned int j;
6353 bitmap_iterator bi;
6354 bool head_p, after_p;
6356 change_p = false;
6357 curr_usage_insns_check++;
6358 clear_invariants ();
6359 reloads_num = calls_num = 0;
6360 for (unsigned int i = 0; i < NUM_ABI_IDS; ++i)
6361 last_call_for_abi[i] = 0;
6362 CLEAR_HARD_REG_SET (full_and_partial_call_clobbers);
6363 bitmap_clear (&check_only_regs);
6364 bitmap_clear (&invalid_invariant_regs);
6365 last_processed_bb = NULL;
6366 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
6367 live_hard_regs = eliminable_regset | lra_no_alloc_regs;
6368 /* We don't process new insns generated in the loop. */
6369 for (curr_insn = tail; curr_insn != PREV_INSN (head); curr_insn = prev_insn)
6371 prev_insn = PREV_INSN (curr_insn);
6372 if (BLOCK_FOR_INSN (curr_insn) != NULL)
6373 curr_bb = BLOCK_FOR_INSN (curr_insn);
6374 if (last_processed_bb != curr_bb)
6376 /* We are at the end of BB. Add qualified living
6377 pseudos for potential splitting. */
6378 to_process = df_get_live_out (curr_bb);
6379 if (last_processed_bb != NULL)
6381 /* We are somewhere in the middle of EBB. */
6382 get_live_on_other_edges (curr_bb, last_processed_bb,
6383 &temp_bitmap);
6384 to_process = &temp_bitmap;
6386 last_processed_bb = curr_bb;
6387 last_insn = get_last_insertion_point (curr_bb);
6388 after_p = (! JUMP_P (last_insn)
6389 && (! CALL_P (last_insn)
6390 || (find_reg_note (last_insn,
6391 REG_NORETURN, NULL_RTX) == NULL_RTX
6392 && ! SIBLING_CALL_P (last_insn))));
6393 CLEAR_HARD_REG_SET (potential_reload_hard_regs);
6394 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
6396 if ((int) j >= lra_constraint_new_regno_start)
6397 break;
6398 if (j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
6400 if (j < FIRST_PSEUDO_REGISTER)
6401 SET_HARD_REG_BIT (live_hard_regs, j);
6402 else
6403 add_to_hard_reg_set (&live_hard_regs,
6404 PSEUDO_REGNO_MODE (j),
6405 reg_renumber[j]);
6406 setup_next_usage_insn (j, last_insn, reloads_num, after_p);
6410 src_regno = dst_regno = -1;
6411 curr_set = single_set (curr_insn);
6412 if (curr_set != NULL_RTX && REG_P (SET_DEST (curr_set)))
6413 dst_regno = REGNO (SET_DEST (curr_set));
6414 if (curr_set != NULL_RTX && REG_P (SET_SRC (curr_set)))
6415 src_regno = REGNO (SET_SRC (curr_set));
6416 update_reloads_num_p = true;
6417 if (src_regno < lra_constraint_new_regno_start
6418 && src_regno >= FIRST_PSEUDO_REGISTER
6419 && reg_renumber[src_regno] < 0
6420 && dst_regno >= lra_constraint_new_regno_start
6421 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS)
6423 /* 'reload_pseudo <- original_pseudo'. */
6424 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6425 reloads_num++;
6426 update_reloads_num_p = false;
6427 succ_p = false;
6428 if (usage_insns[src_regno].check == curr_usage_insns_check
6429 && (next_usage_insns = usage_insns[src_regno].insns) != NULL_RTX)
6430 succ_p = inherit_reload_reg (false, src_regno, cl,
6431 curr_insn, next_usage_insns);
6432 if (succ_p)
6433 change_p = true;
6434 else
6435 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
6436 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6437 potential_reload_hard_regs |= reg_class_contents[cl];
6439 else if (src_regno < 0
6440 && dst_regno >= lra_constraint_new_regno_start
6441 && invariant_p (SET_SRC (curr_set))
6442 && (cl = lra_get_allocno_class (dst_regno)) != NO_REGS
6443 && ! bitmap_bit_p (&invalid_invariant_regs, dst_regno)
6444 && ! bitmap_bit_p (&invalid_invariant_regs,
6445 ORIGINAL_REGNO(regno_reg_rtx[dst_regno])))
6447 /* 'reload_pseudo <- invariant'. */
6448 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6449 reloads_num++;
6450 update_reloads_num_p = false;
6451 if (process_invariant_for_inheritance (SET_DEST (curr_set), SET_SRC (curr_set)))
6452 change_p = true;
6453 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6454 potential_reload_hard_regs |= reg_class_contents[cl];
6456 else if (src_regno >= lra_constraint_new_regno_start
6457 && dst_regno < lra_constraint_new_regno_start
6458 && dst_regno >= FIRST_PSEUDO_REGISTER
6459 && reg_renumber[dst_regno] < 0
6460 && (cl = lra_get_allocno_class (src_regno)) != NO_REGS
6461 && usage_insns[dst_regno].check == curr_usage_insns_check
6462 && (next_usage_insns
6463 = usage_insns[dst_regno].insns) != NULL_RTX)
6465 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6466 reloads_num++;
6467 update_reloads_num_p = false;
6468 /* 'original_pseudo <- reload_pseudo'. */
6469 if (! JUMP_P (curr_insn)
6470 && inherit_reload_reg (true, dst_regno, cl,
6471 curr_insn, next_usage_insns))
6472 change_p = true;
6473 /* Invalidate. */
6474 usage_insns[dst_regno].check = 0;
6475 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6476 potential_reload_hard_regs |= reg_class_contents[cl];
6478 else if (INSN_P (curr_insn))
6480 int iter;
6481 int max_uid = get_max_uid ();
6483 curr_id = lra_get_insn_recog_data (curr_insn);
6484 curr_static_id = curr_id->insn_static_data;
6485 to_inherit_num = 0;
6486 /* Process insn definitions. */
6487 for (iter = 0; iter < 2; iter++)
6488 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
6489 reg != NULL;
6490 reg = reg->next)
6491 if (reg->type != OP_IN
6492 && (dst_regno = reg->regno) < lra_constraint_new_regno_start)
6494 if (dst_regno >= FIRST_PSEUDO_REGISTER && reg->type == OP_OUT
6495 && reg_renumber[dst_regno] < 0 && ! reg->subreg_p
6496 && usage_insns[dst_regno].check == curr_usage_insns_check
6497 && (next_usage_insns
6498 = usage_insns[dst_regno].insns) != NULL_RTX)
6500 struct lra_insn_reg *r;
6502 for (r = curr_id->regs; r != NULL; r = r->next)
6503 if (r->type != OP_OUT && r->regno == dst_regno)
6504 break;
6505 /* Don't do inheritance if the pseudo is also
6506 used in the insn. */
6507 if (r == NULL)
6508 /* We cannot do inheritance right now
6509 because the current insn reg info (chain
6510 regs) can change after that. */
6511 add_to_inherit (dst_regno, next_usage_insns);
6513 /* We cannot process one reg twice here because of
6514 usage_insns invalidation. */
6515 if ((dst_regno < FIRST_PSEUDO_REGISTER
6516 || reg_renumber[dst_regno] >= 0)
6517 && ! reg->subreg_p && reg->type != OP_IN)
6519 HARD_REG_SET s;
6521 if (split_if_necessary (dst_regno, reg->biggest_mode,
6522 potential_reload_hard_regs,
6523 false, curr_insn, max_uid))
6524 change_p = true;
6525 CLEAR_HARD_REG_SET (s);
6526 if (dst_regno < FIRST_PSEUDO_REGISTER)
6527 add_to_hard_reg_set (&s, reg->biggest_mode, dst_regno);
6528 else
6529 add_to_hard_reg_set (&s, PSEUDO_REGNO_MODE (dst_regno),
6530 reg_renumber[dst_regno]);
6531 live_hard_regs &= ~s;
6532 potential_reload_hard_regs &= ~s;
6534 /* We should invalidate potential inheritance or
6535 splitting for the current insn usages to the next
6536 usage insns (see code below) as the output pseudo
6537 prevents this. */
6538 if ((dst_regno >= FIRST_PSEUDO_REGISTER
6539 && reg_renumber[dst_regno] < 0)
6540 || (reg->type == OP_OUT && ! reg->subreg_p
6541 && (dst_regno < FIRST_PSEUDO_REGISTER
6542 || reg_renumber[dst_regno] >= 0)))
6544 /* Invalidate and mark definitions. */
6545 if (dst_regno >= FIRST_PSEUDO_REGISTER)
6546 usage_insns[dst_regno].check = -(int) INSN_UID (curr_insn);
6547 else
6549 nregs = hard_regno_nregs (dst_regno,
6550 reg->biggest_mode);
6551 for (i = 0; i < nregs; i++)
6552 usage_insns[dst_regno + i].check
6553 = -(int) INSN_UID (curr_insn);
6557 /* Process clobbered call regs. */
6558 if (curr_id->arg_hard_regs != NULL)
6559 for (i = 0; (dst_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6560 if (dst_regno >= FIRST_PSEUDO_REGISTER)
6561 usage_insns[dst_regno - FIRST_PSEUDO_REGISTER].check
6562 = -(int) INSN_UID (curr_insn);
6563 if (! JUMP_P (curr_insn))
6564 for (i = 0; i < to_inherit_num; i++)
6565 if (inherit_reload_reg (true, to_inherit[i].regno,
6566 ALL_REGS, curr_insn,
6567 to_inherit[i].insns))
6568 change_p = true;
6569 if (CALL_P (curr_insn))
6571 rtx cheap, pat, dest;
6572 rtx_insn *restore;
6573 int regno, hard_regno;
6575 calls_num++;
6576 function_abi callee_abi = insn_callee_abi (curr_insn);
6577 last_call_for_abi[callee_abi.id ()] = calls_num;
6578 full_and_partial_call_clobbers
6579 |= callee_abi.full_and_partial_reg_clobbers ();
6580 if ((cheap = find_reg_note (curr_insn,
6581 REG_RETURNED, NULL_RTX)) != NULL_RTX
6582 && ((cheap = XEXP (cheap, 0)), true)
6583 && (regno = REGNO (cheap)) >= FIRST_PSEUDO_REGISTER
6584 && (hard_regno = reg_renumber[regno]) >= 0
6585 && usage_insns[regno].check == curr_usage_insns_check
6586 /* If there are pending saves/restores, the
6587 optimization is not worth. */
6588 && usage_insns[regno].calls_num == calls_num - 1
6589 && callee_abi.clobbers_reg_p (GET_MODE (cheap), hard_regno))
6591 /* Restore the pseudo from the call result as
6592 REG_RETURNED note says that the pseudo value is
6593 in the call result and the pseudo is an argument
6594 of the call. */
6595 pat = PATTERN (curr_insn);
6596 if (GET_CODE (pat) == PARALLEL)
6597 pat = XVECEXP (pat, 0, 0);
6598 dest = SET_DEST (pat);
6599 /* For multiple return values dest is PARALLEL.
6600 Currently we handle only single return value case. */
6601 if (REG_P (dest))
6603 start_sequence ();
6604 emit_move_insn (cheap, copy_rtx (dest));
6605 restore = get_insns ();
6606 end_sequence ();
6607 lra_process_new_insns (curr_insn, NULL, restore,
6608 "Inserting call parameter restore");
6609 /* We don't need to save/restore of the pseudo from
6610 this call. */
6611 usage_insns[regno].calls_num = calls_num;
6612 remove_from_hard_reg_set
6613 (&full_and_partial_call_clobbers,
6614 GET_MODE (cheap), hard_regno);
6615 bitmap_set_bit (&check_only_regs, regno);
6619 to_inherit_num = 0;
6620 /* Process insn usages. */
6621 for (iter = 0; iter < 2; iter++)
6622 for (reg = iter == 0 ? curr_id->regs : curr_static_id->hard_regs;
6623 reg != NULL;
6624 reg = reg->next)
6625 if ((reg->type != OP_OUT
6626 || (reg->type == OP_OUT && reg->subreg_p))
6627 && (src_regno = reg->regno) < lra_constraint_new_regno_start)
6629 if (src_regno >= FIRST_PSEUDO_REGISTER
6630 && reg_renumber[src_regno] < 0 && reg->type == OP_IN)
6632 if (usage_insns[src_regno].check == curr_usage_insns_check
6633 && (next_usage_insns
6634 = usage_insns[src_regno].insns) != NULL_RTX
6635 && NONDEBUG_INSN_P (curr_insn))
6636 add_to_inherit (src_regno, next_usage_insns);
6637 else if (usage_insns[src_regno].check
6638 != -(int) INSN_UID (curr_insn))
6639 /* Add usages but only if the reg is not set up
6640 in the same insn. */
6641 add_next_usage_insn (src_regno, curr_insn, reloads_num);
6643 else if (src_regno < FIRST_PSEUDO_REGISTER
6644 || reg_renumber[src_regno] >= 0)
6646 bool before_p;
6647 rtx_insn *use_insn = curr_insn;
6649 before_p = (JUMP_P (curr_insn)
6650 || (CALL_P (curr_insn) && reg->type == OP_IN));
6651 if (NONDEBUG_INSN_P (curr_insn)
6652 && (! JUMP_P (curr_insn) || reg->type == OP_IN)
6653 && split_if_necessary (src_regno, reg->biggest_mode,
6654 potential_reload_hard_regs,
6655 before_p, curr_insn, max_uid))
6657 if (reg->subreg_p)
6658 check_and_force_assignment_correctness_p = true;
6659 change_p = true;
6660 /* Invalidate. */
6661 usage_insns[src_regno].check = 0;
6662 if (before_p)
6663 use_insn = PREV_INSN (curr_insn);
6665 if (NONDEBUG_INSN_P (curr_insn))
6667 if (src_regno < FIRST_PSEUDO_REGISTER)
6668 add_to_hard_reg_set (&live_hard_regs,
6669 reg->biggest_mode, src_regno);
6670 else
6671 add_to_hard_reg_set (&live_hard_regs,
6672 PSEUDO_REGNO_MODE (src_regno),
6673 reg_renumber[src_regno]);
6675 if (src_regno >= FIRST_PSEUDO_REGISTER)
6676 add_next_usage_insn (src_regno, use_insn, reloads_num);
6677 else
6679 for (i = 0; i < hard_regno_nregs (src_regno, reg->biggest_mode); i++)
6680 add_next_usage_insn (src_regno + i, use_insn, reloads_num);
6684 /* Process used call regs. */
6685 if (curr_id->arg_hard_regs != NULL)
6686 for (i = 0; (src_regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6687 if (src_regno < FIRST_PSEUDO_REGISTER)
6689 SET_HARD_REG_BIT (live_hard_regs, src_regno);
6690 add_next_usage_insn (src_regno, curr_insn, reloads_num);
6692 for (i = 0; i < to_inherit_num; i++)
6694 src_regno = to_inherit[i].regno;
6695 if (inherit_reload_reg (false, src_regno, ALL_REGS,
6696 curr_insn, to_inherit[i].insns))
6697 change_p = true;
6698 else
6699 setup_next_usage_insn (src_regno, curr_insn, reloads_num, false);
6702 if (update_reloads_num_p
6703 && NONDEBUG_INSN_P (curr_insn) && curr_set != NULL_RTX)
6705 int regno = -1;
6706 if ((REG_P (SET_DEST (curr_set))
6707 && (regno = REGNO (SET_DEST (curr_set))) >= lra_constraint_new_regno_start
6708 && reg_renumber[regno] < 0
6709 && (cl = lra_get_allocno_class (regno)) != NO_REGS)
6710 || (REG_P (SET_SRC (curr_set))
6711 && (regno = REGNO (SET_SRC (curr_set))) >= lra_constraint_new_regno_start
6712 && reg_renumber[regno] < 0
6713 && (cl = lra_get_allocno_class (regno)) != NO_REGS))
6715 if (ira_class_hard_regs_num[cl] <= max_small_class_regs_num)
6716 reloads_num++;
6717 if (hard_reg_set_subset_p (reg_class_contents[cl], live_hard_regs))
6718 potential_reload_hard_regs |= reg_class_contents[cl];
6721 if (NONDEBUG_INSN_P (curr_insn))
6723 int regno;
6725 /* Invalidate invariants with changed regs. */
6726 curr_id = lra_get_insn_recog_data (curr_insn);
6727 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
6728 if (reg->type != OP_IN)
6730 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
6731 bitmap_set_bit (&invalid_invariant_regs,
6732 ORIGINAL_REGNO (regno_reg_rtx[reg->regno]));
6734 curr_static_id = curr_id->insn_static_data;
6735 for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
6736 if (reg->type != OP_IN)
6737 bitmap_set_bit (&invalid_invariant_regs, reg->regno);
6738 if (curr_id->arg_hard_regs != NULL)
6739 for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
6740 if (regno >= FIRST_PSEUDO_REGISTER)
6741 bitmap_set_bit (&invalid_invariant_regs,
6742 regno - FIRST_PSEUDO_REGISTER);
6744 /* We reached the start of the current basic block. */
6745 if (prev_insn == NULL_RTX || prev_insn == PREV_INSN (head)
6746 || BLOCK_FOR_INSN (prev_insn) != curr_bb)
6748 /* We reached the beginning of the current block -- do
6749 rest of spliting in the current BB. */
6750 to_process = df_get_live_in (curr_bb);
6751 if (BLOCK_FOR_INSN (head) != curr_bb)
6753 /* We are somewhere in the middle of EBB. */
6754 get_live_on_other_edges (EDGE_PRED (curr_bb, 0)->src,
6755 curr_bb, &temp_bitmap);
6756 to_process = &temp_bitmap;
6758 head_p = true;
6759 EXECUTE_IF_SET_IN_BITMAP (to_process, 0, j, bi)
6761 if ((int) j >= lra_constraint_new_regno_start)
6762 break;
6763 if (((int) j < FIRST_PSEUDO_REGISTER || reg_renumber[j] >= 0)
6764 && usage_insns[j].check == curr_usage_insns_check
6765 && (next_usage_insns = usage_insns[j].insns) != NULL_RTX)
6767 if (need_for_split_p (potential_reload_hard_regs, j))
6769 if (lra_dump_file != NULL && head_p)
6771 fprintf (lra_dump_file,
6772 " ----------------------------------\n");
6773 head_p = false;
6775 if (split_reg (false, j, bb_note (curr_bb),
6776 next_usage_insns, NULL))
6777 change_p = true;
6779 usage_insns[j].check = 0;
6784 return change_p;
6787 /* This value affects EBB forming. If probability of edge from EBB to
6788 a BB is not greater than the following value, we don't add the BB
6789 to EBB. */
6790 #define EBB_PROBABILITY_CUTOFF \
6791 ((REG_BR_PROB_BASE * param_lra_inheritance_ebb_probability_cutoff) / 100)
6793 /* Current number of inheritance/split iteration. */
6794 int lra_inheritance_iter;
6796 /* Entry function for inheritance/split pass. */
6797 void
6798 lra_inheritance (void)
6800 int i;
6801 basic_block bb, start_bb;
6802 edge e;
6804 lra_inheritance_iter++;
6805 if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
6806 return;
6807 timevar_push (TV_LRA_INHERITANCE);
6808 if (lra_dump_file != NULL)
6809 fprintf (lra_dump_file, "\n********** Inheritance #%d: **********\n\n",
6810 lra_inheritance_iter);
6811 curr_usage_insns_check = 0;
6812 usage_insns = XNEWVEC (struct usage_insns, lra_constraint_new_regno_start);
6813 for (i = 0; i < lra_constraint_new_regno_start; i++)
6814 usage_insns[i].check = 0;
6815 bitmap_initialize (&check_only_regs, &reg_obstack);
6816 bitmap_initialize (&invalid_invariant_regs, &reg_obstack);
6817 bitmap_initialize (&live_regs, &reg_obstack);
6818 bitmap_initialize (&temp_bitmap, &reg_obstack);
6819 bitmap_initialize (&ebb_global_regs, &reg_obstack);
6820 FOR_EACH_BB_FN (bb, cfun)
6822 start_bb = bb;
6823 if (lra_dump_file != NULL)
6824 fprintf (lra_dump_file, "EBB");
6825 /* Form a EBB starting with BB. */
6826 bitmap_clear (&ebb_global_regs);
6827 bitmap_ior_into (&ebb_global_regs, df_get_live_in (bb));
6828 for (;;)
6830 if (lra_dump_file != NULL)
6831 fprintf (lra_dump_file, " %d", bb->index);
6832 if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
6833 || LABEL_P (BB_HEAD (bb->next_bb)))
6834 break;
6835 e = find_fallthru_edge (bb->succs);
6836 if (! e)
6837 break;
6838 if (e->probability.initialized_p ()
6839 && e->probability.to_reg_br_prob_base () < EBB_PROBABILITY_CUTOFF)
6840 break;
6841 bb = bb->next_bb;
6843 bitmap_ior_into (&ebb_global_regs, df_get_live_out (bb));
6844 if (lra_dump_file != NULL)
6845 fprintf (lra_dump_file, "\n");
6846 if (inherit_in_ebb (BB_HEAD (start_bb), BB_END (bb)))
6847 /* Remember that the EBB head and tail can change in
6848 inherit_in_ebb. */
6849 update_ebb_live_info (BB_HEAD (start_bb), BB_END (bb));
6851 bitmap_release (&ebb_global_regs);
6852 bitmap_release (&temp_bitmap);
6853 bitmap_release (&live_regs);
6854 bitmap_release (&invalid_invariant_regs);
6855 bitmap_release (&check_only_regs);
6856 free (usage_insns);
6858 timevar_pop (TV_LRA_INHERITANCE);
6863 /* This page contains code to undo failed inheritance/split
6864 transformations. */
6866 /* Current number of iteration undoing inheritance/split. */
6867 int lra_undo_inheritance_iter;
6869 /* Fix BB live info LIVE after removing pseudos created on pass doing
6870 inheritance/split which are REMOVED_PSEUDOS. */
6871 static void
6872 fix_bb_live_info (bitmap live, bitmap removed_pseudos)
6874 unsigned int regno;
6875 bitmap_iterator bi;
6877 EXECUTE_IF_SET_IN_BITMAP (removed_pseudos, 0, regno, bi)
6878 if (bitmap_clear_bit (live, regno)
6879 && REG_P (lra_reg_info[regno].restore_rtx))
6880 bitmap_set_bit (live, REGNO (lra_reg_info[regno].restore_rtx));
6883 /* Return regno of the (subreg of) REG. Otherwise, return a negative
6884 number. */
6885 static int
6886 get_regno (rtx reg)
6888 if (GET_CODE (reg) == SUBREG)
6889 reg = SUBREG_REG (reg);
6890 if (REG_P (reg))
6891 return REGNO (reg);
6892 return -1;
6895 /* Delete a move INSN with destination reg DREGNO and a previous
6896 clobber insn with the same regno. The inheritance/split code can
6897 generate moves with preceding clobber and when we delete such moves
6898 we should delete the clobber insn too to keep the correct life
6899 info. */
6900 static void
6901 delete_move_and_clobber (rtx_insn *insn, int dregno)
6903 rtx_insn *prev_insn = PREV_INSN (insn);
6905 lra_set_insn_deleted (insn);
6906 lra_assert (dregno >= 0);
6907 if (prev_insn != NULL && NONDEBUG_INSN_P (prev_insn)
6908 && GET_CODE (PATTERN (prev_insn)) == CLOBBER
6909 && dregno == get_regno (XEXP (PATTERN (prev_insn), 0)))
6910 lra_set_insn_deleted (prev_insn);
6913 /* Remove inheritance/split pseudos which are in REMOVE_PSEUDOS and
6914 return true if we did any change. The undo transformations for
6915 inheritance looks like
6916 i <- i2
6917 p <- i => p <- i2
6918 or removing
6919 p <- i, i <- p, and i <- i3
6920 where p is original pseudo from which inheritance pseudo i was
6921 created, i and i3 are removed inheritance pseudos, i2 is another
6922 not removed inheritance pseudo. All split pseudos or other
6923 occurrences of removed inheritance pseudos are changed on the
6924 corresponding original pseudos.
6926 The function also schedules insns changed and created during
6927 inheritance/split pass for processing by the subsequent constraint
6928 pass. */
6929 static bool
6930 remove_inheritance_pseudos (bitmap remove_pseudos)
6932 basic_block bb;
6933 int regno, sregno, prev_sregno, dregno;
6934 rtx restore_rtx;
6935 rtx set, prev_set;
6936 rtx_insn *prev_insn;
6937 bool change_p, done_p;
6939 change_p = ! bitmap_empty_p (remove_pseudos);
6940 /* We cannot finish the function right away if CHANGE_P is true
6941 because we need to marks insns affected by previous
6942 inheritance/split pass for processing by the subsequent
6943 constraint pass. */
6944 FOR_EACH_BB_FN (bb, cfun)
6946 fix_bb_live_info (df_get_live_in (bb), remove_pseudos);
6947 fix_bb_live_info (df_get_live_out (bb), remove_pseudos);
6948 FOR_BB_INSNS_REVERSE (bb, curr_insn)
6950 if (! INSN_P (curr_insn))
6951 continue;
6952 done_p = false;
6953 sregno = dregno = -1;
6954 if (change_p && NONDEBUG_INSN_P (curr_insn)
6955 && (set = single_set (curr_insn)) != NULL_RTX)
6957 dregno = get_regno (SET_DEST (set));
6958 sregno = get_regno (SET_SRC (set));
6961 if (sregno >= 0 && dregno >= 0)
6963 if (bitmap_bit_p (remove_pseudos, dregno)
6964 && ! REG_P (lra_reg_info[dregno].restore_rtx))
6966 /* invariant inheritance pseudo <- original pseudo */
6967 if (lra_dump_file != NULL)
6969 fprintf (lra_dump_file, " Removing invariant inheritance:\n");
6970 dump_insn_slim (lra_dump_file, curr_insn);
6971 fprintf (lra_dump_file, "\n");
6973 delete_move_and_clobber (curr_insn, dregno);
6974 done_p = true;
6976 else if (bitmap_bit_p (remove_pseudos, sregno)
6977 && ! REG_P (lra_reg_info[sregno].restore_rtx))
6979 /* reload pseudo <- invariant inheritance pseudo */
6980 start_sequence ();
6981 /* We cannot just change the source. It might be
6982 an insn different from the move. */
6983 emit_insn (lra_reg_info[sregno].restore_rtx);
6984 rtx_insn *new_insns = get_insns ();
6985 end_sequence ();
6986 lra_assert (single_set (new_insns) != NULL
6987 && SET_DEST (set) == SET_DEST (single_set (new_insns)));
6988 lra_process_new_insns (curr_insn, NULL, new_insns,
6989 "Changing reload<-invariant inheritance");
6990 delete_move_and_clobber (curr_insn, dregno);
6991 done_p = true;
6993 else if ((bitmap_bit_p (remove_pseudos, sregno)
6994 && (get_regno (lra_reg_info[sregno].restore_rtx) == dregno
6995 || (bitmap_bit_p (remove_pseudos, dregno)
6996 && get_regno (lra_reg_info[sregno].restore_rtx) >= 0
6997 && (get_regno (lra_reg_info[sregno].restore_rtx)
6998 == get_regno (lra_reg_info[dregno].restore_rtx)))))
6999 || (bitmap_bit_p (remove_pseudos, dregno)
7000 && get_regno (lra_reg_info[dregno].restore_rtx) == sregno))
7001 /* One of the following cases:
7002 original <- removed inheritance pseudo
7003 removed inherit pseudo <- another removed inherit pseudo
7004 removed inherit pseudo <- original pseudo
7006 removed_split_pseudo <- original_reg
7007 original_reg <- removed_split_pseudo */
7009 if (lra_dump_file != NULL)
7011 fprintf (lra_dump_file, " Removing %s:\n",
7012 bitmap_bit_p (&lra_split_regs, sregno)
7013 || bitmap_bit_p (&lra_split_regs, dregno)
7014 ? "split" : "inheritance");
7015 dump_insn_slim (lra_dump_file, curr_insn);
7017 delete_move_and_clobber (curr_insn, dregno);
7018 done_p = true;
7020 else if (bitmap_bit_p (remove_pseudos, sregno)
7021 && bitmap_bit_p (&lra_inheritance_pseudos, sregno))
7023 /* Search the following pattern:
7024 inherit_or_split_pseudo1 <- inherit_or_split_pseudo2
7025 original_pseudo <- inherit_or_split_pseudo1
7026 where the 2nd insn is the current insn and
7027 inherit_or_split_pseudo2 is not removed. If it is found,
7028 change the current insn onto:
7029 original_pseudo <- inherit_or_split_pseudo2. */
7030 for (prev_insn = PREV_INSN (curr_insn);
7031 prev_insn != NULL_RTX && ! NONDEBUG_INSN_P (prev_insn);
7032 prev_insn = PREV_INSN (prev_insn))
7034 if (prev_insn != NULL_RTX && BLOCK_FOR_INSN (prev_insn) == bb
7035 && (prev_set = single_set (prev_insn)) != NULL_RTX
7036 /* There should be no subregs in insn we are
7037 searching because only the original reg might
7038 be in subreg when we changed the mode of
7039 load/store for splitting. */
7040 && REG_P (SET_DEST (prev_set))
7041 && REG_P (SET_SRC (prev_set))
7042 && (int) REGNO (SET_DEST (prev_set)) == sregno
7043 && ((prev_sregno = REGNO (SET_SRC (prev_set)))
7044 >= FIRST_PSEUDO_REGISTER)
7045 && (lra_reg_info[prev_sregno].restore_rtx == NULL_RTX
7047 /* As we consider chain of inheritance or
7048 splitting described in above comment we should
7049 check that sregno and prev_sregno were
7050 inheritance/split pseudos created from the
7051 same original regno. */
7052 (get_regno (lra_reg_info[sregno].restore_rtx) >= 0
7053 && (get_regno (lra_reg_info[sregno].restore_rtx)
7054 == get_regno (lra_reg_info[prev_sregno].restore_rtx))))
7055 && ! bitmap_bit_p (remove_pseudos, prev_sregno))
7057 lra_assert (GET_MODE (SET_SRC (prev_set))
7058 == GET_MODE (regno_reg_rtx[sregno]));
7059 /* Although we have a single set, the insn can
7060 contain more one sregno register occurrence
7061 as a source. Change all occurrences. */
7062 lra_substitute_pseudo_within_insn (curr_insn, sregno,
7063 SET_SRC (prev_set),
7064 false);
7065 /* As we are finishing with processing the insn
7066 here, check the destination too as it might
7067 inheritance pseudo for another pseudo. */
7068 if (bitmap_bit_p (remove_pseudos, dregno)
7069 && bitmap_bit_p (&lra_inheritance_pseudos, dregno)
7070 && (restore_rtx
7071 = lra_reg_info[dregno].restore_rtx) != NULL_RTX)
7073 if (GET_CODE (SET_DEST (set)) == SUBREG)
7074 SUBREG_REG (SET_DEST (set)) = restore_rtx;
7075 else
7076 SET_DEST (set) = restore_rtx;
7078 lra_push_insn_and_update_insn_regno_info (curr_insn);
7079 lra_set_used_insn_alternative_by_uid
7080 (INSN_UID (curr_insn), LRA_UNKNOWN_ALT);
7081 done_p = true;
7082 if (lra_dump_file != NULL)
7084 fprintf (lra_dump_file, " Change reload insn:\n");
7085 dump_insn_slim (lra_dump_file, curr_insn);
7090 if (! done_p)
7092 struct lra_insn_reg *reg;
7093 bool restored_regs_p = false;
7094 bool kept_regs_p = false;
7096 curr_id = lra_get_insn_recog_data (curr_insn);
7097 for (reg = curr_id->regs; reg != NULL; reg = reg->next)
7099 regno = reg->regno;
7100 restore_rtx = lra_reg_info[regno].restore_rtx;
7101 if (restore_rtx != NULL_RTX)
7103 if (change_p && bitmap_bit_p (remove_pseudos, regno))
7105 lra_substitute_pseudo_within_insn
7106 (curr_insn, regno, restore_rtx, false);
7107 restored_regs_p = true;
7109 else
7110 kept_regs_p = true;
7113 if (NONDEBUG_INSN_P (curr_insn) && kept_regs_p)
7115 /* The instruction has changed since the previous
7116 constraints pass. */
7117 lra_push_insn_and_update_insn_regno_info (curr_insn);
7118 lra_set_used_insn_alternative_by_uid
7119 (INSN_UID (curr_insn), LRA_UNKNOWN_ALT);
7121 else if (restored_regs_p)
7122 /* The instruction has been restored to the form that
7123 it had during the previous constraints pass. */
7124 lra_update_insn_regno_info (curr_insn);
7125 if (restored_regs_p && lra_dump_file != NULL)
7127 fprintf (lra_dump_file, " Insn after restoring regs:\n");
7128 dump_insn_slim (lra_dump_file, curr_insn);
7133 return change_p;
7136 /* If optional reload pseudos failed to get a hard register or was not
7137 inherited, it is better to remove optional reloads. We do this
7138 transformation after undoing inheritance to figure out necessity to
7139 remove optional reloads easier. Return true if we do any
7140 change. */
7141 static bool
7142 undo_optional_reloads (void)
7144 bool change_p, keep_p;
7145 unsigned int regno, uid;
7146 bitmap_iterator bi, bi2;
7147 rtx_insn *insn;
7148 rtx set, src, dest;
7149 auto_bitmap removed_optional_reload_pseudos (&reg_obstack);
7151 bitmap_copy (removed_optional_reload_pseudos, &lra_optional_reload_pseudos);
7152 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
7154 keep_p = false;
7155 /* Keep optional reloads from previous subpasses. */
7156 if (lra_reg_info[regno].restore_rtx == NULL_RTX
7157 /* If the original pseudo changed its allocation, just
7158 removing the optional pseudo is dangerous as the original
7159 pseudo will have longer live range. */
7160 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] >= 0)
7161 keep_p = true;
7162 else if (reg_renumber[regno] >= 0)
7163 EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi2)
7165 insn = lra_insn_recog_data[uid]->insn;
7166 if ((set = single_set (insn)) == NULL_RTX)
7167 continue;
7168 src = SET_SRC (set);
7169 dest = SET_DEST (set);
7170 if (! REG_P (src) || ! REG_P (dest))
7171 continue;
7172 if (REGNO (dest) == regno
7173 /* Ignore insn for optional reloads itself. */
7174 && REGNO (lra_reg_info[regno].restore_rtx) != REGNO (src)
7175 /* Check only inheritance on last inheritance pass. */
7176 && (int) REGNO (src) >= new_regno_start
7177 /* Check that the optional reload was inherited. */
7178 && bitmap_bit_p (&lra_inheritance_pseudos, REGNO (src)))
7180 keep_p = true;
7181 break;
7184 if (keep_p)
7186 bitmap_clear_bit (removed_optional_reload_pseudos, regno);
7187 if (lra_dump_file != NULL)
7188 fprintf (lra_dump_file, "Keep optional reload reg %d\n", regno);
7191 change_p = ! bitmap_empty_p (removed_optional_reload_pseudos);
7192 auto_bitmap insn_bitmap (&reg_obstack);
7193 EXECUTE_IF_SET_IN_BITMAP (removed_optional_reload_pseudos, 0, regno, bi)
7195 if (lra_dump_file != NULL)
7196 fprintf (lra_dump_file, "Remove optional reload reg %d\n", regno);
7197 bitmap_copy (insn_bitmap, &lra_reg_info[regno].insn_bitmap);
7198 EXECUTE_IF_SET_IN_BITMAP (insn_bitmap, 0, uid, bi2)
7200 insn = lra_insn_recog_data[uid]->insn;
7201 if ((set = single_set (insn)) != NULL_RTX)
7203 src = SET_SRC (set);
7204 dest = SET_DEST (set);
7205 if (REG_P (src) && REG_P (dest)
7206 && ((REGNO (src) == regno
7207 && (REGNO (lra_reg_info[regno].restore_rtx)
7208 == REGNO (dest)))
7209 || (REGNO (dest) == regno
7210 && (REGNO (lra_reg_info[regno].restore_rtx)
7211 == REGNO (src)))))
7213 if (lra_dump_file != NULL)
7215 fprintf (lra_dump_file, " Deleting move %u\n",
7216 INSN_UID (insn));
7217 dump_insn_slim (lra_dump_file, insn);
7219 delete_move_and_clobber (insn, REGNO (dest));
7220 continue;
7222 /* We should not worry about generation memory-memory
7223 moves here as if the corresponding inheritance did
7224 not work (inheritance pseudo did not get a hard reg),
7225 we remove the inheritance pseudo and the optional
7226 reload. */
7228 lra_substitute_pseudo_within_insn
7229 (insn, regno, lra_reg_info[regno].restore_rtx, false);
7230 lra_update_insn_regno_info (insn);
7231 if (lra_dump_file != NULL)
7233 fprintf (lra_dump_file,
7234 " Restoring original insn:\n");
7235 dump_insn_slim (lra_dump_file, insn);
7239 /* Clear restore_regnos. */
7240 EXECUTE_IF_SET_IN_BITMAP (&lra_optional_reload_pseudos, 0, regno, bi)
7241 lra_reg_info[regno].restore_rtx = NULL_RTX;
7242 return change_p;
7245 /* Entry function for undoing inheritance/split transformation. Return true
7246 if we did any RTL change in this pass. */
7247 bool
7248 lra_undo_inheritance (void)
7250 unsigned int regno;
7251 int hard_regno;
7252 int n_all_inherit, n_inherit, n_all_split, n_split;
7253 rtx restore_rtx;
7254 bitmap_iterator bi;
7255 bool change_p;
7257 lra_undo_inheritance_iter++;
7258 if (lra_undo_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
7259 return false;
7260 if (lra_dump_file != NULL)
7261 fprintf (lra_dump_file,
7262 "\n********** Undoing inheritance #%d: **********\n\n",
7263 lra_undo_inheritance_iter);
7264 auto_bitmap remove_pseudos (&reg_obstack);
7265 n_inherit = n_all_inherit = 0;
7266 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
7267 if (lra_reg_info[regno].restore_rtx != NULL_RTX)
7269 n_all_inherit++;
7270 if (reg_renumber[regno] < 0
7271 /* If the original pseudo changed its allocation, just
7272 removing inheritance is dangerous as for changing
7273 allocation we used shorter live-ranges. */
7274 && (! REG_P (lra_reg_info[regno].restore_rtx)
7275 || reg_renumber[REGNO (lra_reg_info[regno].restore_rtx)] < 0))
7276 bitmap_set_bit (remove_pseudos, regno);
7277 else
7278 n_inherit++;
7280 if (lra_dump_file != NULL && n_all_inherit != 0)
7281 fprintf (lra_dump_file, "Inherit %d out of %d (%.2f%%)\n",
7282 n_inherit, n_all_inherit,
7283 (double) n_inherit / n_all_inherit * 100);
7284 n_split = n_all_split = 0;
7285 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
7286 if ((restore_rtx = lra_reg_info[regno].restore_rtx) != NULL_RTX)
7288 int restore_regno = REGNO (restore_rtx);
7290 n_all_split++;
7291 hard_regno = (restore_regno >= FIRST_PSEUDO_REGISTER
7292 ? reg_renumber[restore_regno] : restore_regno);
7293 if (hard_regno < 0 || reg_renumber[regno] == hard_regno)
7294 bitmap_set_bit (remove_pseudos, regno);
7295 else
7297 n_split++;
7298 if (lra_dump_file != NULL)
7299 fprintf (lra_dump_file, " Keep split r%d (orig=r%d)\n",
7300 regno, restore_regno);
7303 if (lra_dump_file != NULL && n_all_split != 0)
7304 fprintf (lra_dump_file, "Split %d out of %d (%.2f%%)\n",
7305 n_split, n_all_split,
7306 (double) n_split / n_all_split * 100);
7307 change_p = remove_inheritance_pseudos (remove_pseudos);
7308 /* Clear restore_regnos. */
7309 EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, regno, bi)
7310 lra_reg_info[regno].restore_rtx = NULL_RTX;
7311 EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, regno, bi)
7312 lra_reg_info[regno].restore_rtx = NULL_RTX;
7313 change_p = undo_optional_reloads () || change_p;
7314 return change_p;